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, 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 directory (may already exist). */
192 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_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 GOT_WORKTREE_GOT_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 GOT_WORKTREE_GOT_DIR) == -1) {
342 err = got_error_from_errno("asprintf");
343 path_got = NULL;
344 goto done;
347 err = got_object_open(&obj, repo, commit_id);
348 if (err)
349 return err;
351 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 err = got_error(GOT_ERR_OBJ_TYPE);
353 goto done;
356 /* Record our base commit. */
357 err = got_object_id_str(&id_str, commit_id);
358 if (err)
359 goto done;
360 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 if (err)
362 goto done;
364 free(worktree->base_commit_id);
365 worktree->base_commit_id = got_object_id_dup(commit_id);
366 if (worktree->base_commit_id == NULL) {
367 err = got_error_from_errno("got_object_id_dup");
368 goto done;
370 done:
371 if (obj)
372 got_object_close(obj);
373 free(id_str);
374 free(path_got);
375 return err;
378 const struct got_gotconfig *
379 got_worktree_get_gotconfig(struct got_worktree *worktree)
381 return worktree->gotconfig;
384 static const struct got_error *
385 lock_worktree(struct got_worktree *worktree, int operation)
387 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 : got_error_from_errno2("flock",
390 got_worktree_get_root_path(worktree)));
391 return NULL;
394 static const struct got_error *
395 add_dir_on_disk(struct got_worktree *worktree, const char *path)
397 const struct got_error *err = NULL;
398 char *abspath;
400 /* We only accept worktree-relative paths here. */
401 if (got_path_is_absolute(path)) {
402 return got_error_fmt(GOT_ERR_BAD_PATH,
403 "%s does not accept absolute paths: %s",
404 __func__, path);
407 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 return got_error_from_errno("asprintf");
410 err = got_path_mkdir(abspath);
411 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
412 struct stat sb;
413 err = NULL;
414 if (lstat(abspath, &sb) == -1) {
415 err = got_error_from_errno2("lstat", abspath);
416 } else if (!S_ISDIR(sb.st_mode)) {
417 /* TODO directory is obstructed; do something */
418 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
421 free(abspath);
422 return err;
425 static const struct got_error *
426 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
428 const struct got_error *err = NULL;
429 uint8_t fbuf1[8192];
430 uint8_t fbuf2[8192];
431 size_t flen1 = 0, flen2 = 0;
433 *same = 1;
435 for (;;) {
436 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 if (flen1 == 0 && ferror(f1)) {
438 err = got_error_from_errno("fread");
439 break;
441 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 if (flen2 == 0 && ferror(f2)) {
443 err = got_error_from_errno("fread");
444 break;
446 if (flen1 == 0) {
447 if (flen2 != 0)
448 *same = 0;
449 break;
450 } else if (flen2 == 0) {
451 if (flen1 != 0)
452 *same = 0;
453 break;
454 } else if (flen1 == flen2) {
455 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
456 *same = 0;
457 break;
459 } else {
460 *same = 0;
461 break;
465 return err;
468 static const struct got_error *
469 check_files_equal(int *same, FILE *f1, FILE *f2)
471 struct stat sb;
472 size_t size1, size2;
474 *same = 1;
476 if (fstat(fileno(f1), &sb) != 0)
477 return got_error_from_errno("fstat");
478 size1 = sb.st_size;
480 if (fstat(fileno(f2), &sb) != 0)
481 return got_error_from_errno("fstat");
482 size2 = sb.st_size;
484 if (size1 != size2) {
485 *same = 0;
486 return NULL;
489 if (fseek(f1, 0L, SEEK_SET) == -1)
490 return got_ferror(f1, GOT_ERR_IO);
491 if (fseek(f2, 0L, SEEK_SET) == -1)
492 return got_ferror(f2, GOT_ERR_IO);
494 return check_file_contents_equal(same, f1, f2);
497 static const struct got_error *
498 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
500 uint8_t fbuf[65536];
501 size_t flen;
502 ssize_t outlen;
504 *outsize = 0;
506 if (fseek(f, 0L, SEEK_SET) == -1)
507 return got_ferror(f, GOT_ERR_IO);
509 for (;;) {
510 flen = fread(fbuf, 1, sizeof(fbuf), f);
511 if (flen == 0) {
512 if (ferror(f))
513 return got_error_from_errno("fread");
514 if (feof(f))
515 break;
517 outlen = write(outfd, fbuf, flen);
518 if (outlen == -1)
519 return got_error_from_errno("write");
520 if (outlen != flen)
521 return got_error(GOT_ERR_IO);
522 *outsize += outlen;
525 return NULL;
528 static const struct got_error *
529 merge_binary_file(int *overlapcnt, int merged_fd,
530 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 const char *ondisk_path)
534 const struct got_error *err = NULL;
535 int same_content, changed_deriv, changed_deriv2;
536 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 char *base_path_orig = NULL, *base_path_deriv = NULL;
540 char *base_path_deriv2 = NULL;
542 *overlapcnt = 0;
544 err = check_files_equal(&same_content, f_deriv, f_deriv2);
545 if (err)
546 return err;
548 if (same_content)
549 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
551 err = check_files_equal(&same_content, f_deriv, f_orig);
552 if (err)
553 return err;
554 changed_deriv = !same_content;
555 err = check_files_equal(&same_content, f_deriv2, f_orig);
556 if (err)
557 return err;
558 changed_deriv2 = !same_content;
560 if (changed_deriv && changed_deriv2) {
561 *overlapcnt = 1;
562 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 err = got_error_from_errno("asprintf");
564 goto done;
566 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 err = got_error_from_errno("asprintf");
568 goto done;
570 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 err = got_error_from_errno("asprintf");
572 goto done;
574 err = got_opentemp_named_fd(&path_orig, &fd_orig,
575 base_path_orig, "");
576 if (err)
577 goto done;
578 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 base_path_deriv, "");
580 if (err)
581 goto done;
582 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 base_path_deriv2, "");
584 if (err)
585 goto done;
586 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
587 if (err)
588 goto done;
589 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
590 if (err)
591 goto done;
592 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
593 if (err)
594 goto done;
595 if (dprintf(merged_fd, "Binary files differ and cannot be "
596 "merged automatically:\n") < 0) {
597 err = got_error_from_errno("dprintf");
598 goto done;
600 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 label_deriv ? " " : "",
603 label_deriv ? label_deriv : "",
604 path_deriv) < 0) {
605 err = got_error_from_errno("dprintf");
606 goto done;
608 if (size_orig > 0) {
609 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 GOT_DIFF_CONFLICT_MARKER_ORIG,
611 label_orig ? " " : "",
612 label_orig ? label_orig : "",
613 path_orig) < 0) {
614 err = got_error_from_errno("dprintf");
615 goto done;
618 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 GOT_DIFF_CONFLICT_MARKER_SEP,
620 path_deriv2,
621 GOT_DIFF_CONFLICT_MARKER_END,
622 label_deriv2 ? " " : "",
623 label_deriv2 ? label_deriv2 : "") < 0) {
624 err = got_error_from_errno("dprintf");
625 goto done;
627 } else if (changed_deriv)
628 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 else if (changed_deriv2)
630 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
631 done:
632 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
633 err == NULL)
634 err = got_error_from_errno2("unlink", path_orig);
635 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 err = got_error_from_errno2("close", path_orig);
637 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 err = got_error_from_errno2("close", path_deriv);
639 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 err = got_error_from_errno2("close", path_deriv2);
641 free(path_orig);
642 free(path_deriv);
643 free(path_deriv2);
644 free(base_path_orig);
645 free(base_path_deriv);
646 free(base_path_deriv2);
647 return err;
650 /*
651 * Perform a 3-way merge where the file f_orig acts as the common
652 * ancestor, the file f_deriv acts as the first derived version,
653 * and the file f_deriv2 acts as the second derived version.
654 * The merge result will be written to a new file at ondisk_path; any
655 * existing file at this path will be replaced.
656 */
657 static const struct got_error *
658 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 const char *path, uint16_t st_mode,
661 const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 got_worktree_checkout_cb progress_cb, void *progress_arg)
665 const struct got_error *err = NULL;
666 int merged_fd = -1;
667 FILE *f_merged = NULL;
668 char *merged_path = NULL, *base_path = NULL;
669 int overlapcnt = 0;
670 char *parent = NULL;
672 *local_changes_subsumed = 0;
674 err = got_path_dirname(&parent, ondisk_path);
675 if (err)
676 return err;
678 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 err = got_error_from_errno("asprintf");
680 goto done;
683 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 if (err)
685 goto done;
687 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
689 if (err) {
690 if (err->code != GOT_ERR_FILE_BINARY)
691 goto done;
692 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
694 ondisk_path);
695 if (err)
696 goto done;
699 err = (*progress_cb)(progress_arg,
700 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 if (err)
702 goto done;
704 if (fsync(merged_fd) != 0) {
705 err = got_error_from_errno("fsync");
706 goto done;
709 f_merged = fdopen(merged_fd, "r");
710 if (f_merged == NULL) {
711 err = got_error_from_errno("fdopen");
712 goto done;
714 merged_fd = -1;
716 /* Check if a clean merge has subsumed all local changes. */
717 if (overlapcnt == 0) {
718 err = check_files_equal(local_changes_subsumed, f_deriv,
719 f_merged);
720 if (err)
721 goto done;
724 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 err = got_error_from_errno2("fchmod", merged_path);
726 goto done;
729 if (rename(merged_path, ondisk_path) != 0) {
730 err = got_error_from_errno3("rename", merged_path,
731 ondisk_path);
732 goto done;
734 done:
735 if (err) {
736 if (merged_path)
737 unlink(merged_path);
739 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 err = got_error_from_errno("close");
741 if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 err = got_error_from_errno("fclose");
743 free(merged_path);
744 free(base_path);
745 free(parent);
746 return err;
749 static const struct got_error *
750 update_symlink(const char *ondisk_path, const char *target_path,
751 size_t target_len)
753 /* This is not atomic but matches what 'ln -sf' does. */
754 if (unlink(ondisk_path) == -1)
755 return got_error_from_errno2("unlink", ondisk_path);
756 if (symlink(target_path, ondisk_path) == -1)
757 return got_error_from_errno3("symlink", target_path,
758 ondisk_path);
759 return NULL;
762 /*
763 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 * in the work tree with a file that contains conflict markers and the
765 * conflicting target paths of the original version, a "derived version"
766 * of a symlink from an incoming change, and a local version of the symlink.
768 * The original versions's target path can be NULL if it is not available,
769 * such as if both derived versions added a new symlink at the same path.
771 * The incoming derived symlink target is NULL in case the incoming change
772 * has deleted this symlink.
773 */
774 static const struct got_error *
775 install_symlink_conflict(const char *deriv_target,
776 struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 const char *label_orig, const char *local_target, const char *ondisk_path)
779 const struct got_error *err;
780 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
781 FILE *f = NULL;
783 err = got_object_id_str(&id_str, deriv_base_commit_id);
784 if (err)
785 return got_error_from_errno("asprintf");
787 if (asprintf(&label_deriv, "%s: commit %s",
788 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 err = got_error_from_errno("asprintf");
790 goto done;
793 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
794 if (err)
795 goto done;
797 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 err = got_error_from_errno2("fchmod", path);
799 goto done;
802 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 deriv_target ? deriv_target : "(symlink was deleted)",
805 orig_target ? label_orig : "",
806 orig_target ? "\n" : "",
807 orig_target ? orig_target : "",
808 orig_target ? "\n" : "",
809 GOT_DIFF_CONFLICT_MARKER_SEP,
810 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 err = got_error_from_errno2("fprintf", path);
812 goto done;
815 if (unlink(ondisk_path) == -1) {
816 err = got_error_from_errno2("unlink", ondisk_path);
817 goto done;
819 if (rename(path, ondisk_path) == -1) {
820 err = got_error_from_errno3("rename", path, ondisk_path);
821 goto done;
823 done:
824 if (f != NULL && fclose(f) == EOF && err == NULL)
825 err = got_error_from_errno2("fclose", path);
826 free(path);
827 free(id_str);
828 free(label_deriv);
829 return err;
832 /* forward declaration */
833 static const struct got_error *
834 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 const char *, const char *, uint16_t, const char *,
836 struct got_blob_object *, struct got_object_id *,
837 struct got_repository *, got_worktree_checkout_cb, void *);
839 /*
840 * Merge a symlink into the work tree, where blob_orig acts as the common
841 * ancestor, deriv_target is the link target of the first derived version,
842 * and the symlink on disk acts as the second derived version.
843 * Assume that contents of both blobs represent symlinks.
844 */
845 static const struct got_error *
846 merge_symlink(struct got_worktree *worktree,
847 struct got_blob_object *blob_orig, const char *ondisk_path,
848 const char *path, const char *label_orig, const char *deriv_target,
849 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 got_worktree_checkout_cb progress_cb, void *progress_arg)
852 const struct got_error *err = NULL;
853 char *ancestor_target = NULL;
854 struct stat sb;
855 ssize_t ondisk_len, deriv_len;
856 char ondisk_target[PATH_MAX];
857 int have_local_change = 0;
858 int have_incoming_change = 0;
860 if (lstat(ondisk_path, &sb) == -1)
861 return got_error_from_errno2("lstat", ondisk_path);
863 ondisk_len = readlink(ondisk_path, ondisk_target,
864 sizeof(ondisk_target));
865 if (ondisk_len == -1) {
866 err = got_error_from_errno2("readlink",
867 ondisk_path);
868 goto done;
870 ondisk_target[ondisk_len] = '\0';
872 if (blob_orig) {
873 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
874 if (err)
875 goto done;
878 if (ancestor_target == NULL ||
879 (ondisk_len != strlen(ancestor_target) ||
880 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 have_local_change = 1;
883 deriv_len = strlen(deriv_target);
884 if (ancestor_target == NULL ||
885 (deriv_len != strlen(ancestor_target) ||
886 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 have_incoming_change = 1;
889 if (!have_local_change && !have_incoming_change) {
890 if (ancestor_target) {
891 /* Both sides made the same change. */
892 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
893 path);
894 } else if (deriv_len == ondisk_len &&
895 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 /* Both sides added the same symlink. */
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
898 path);
899 } else {
900 /* Both sides added symlinks which don't match. */
901 err = install_symlink_conflict(deriv_target,
902 deriv_base_commit_id, ancestor_target,
903 label_orig, ondisk_target, ondisk_path);
904 if (err)
905 goto done;
906 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
907 path);
909 } else if (!have_local_change && have_incoming_change) {
910 /* Apply the incoming change. */
911 err = update_symlink(ondisk_path, deriv_target,
912 strlen(deriv_target));
913 if (err)
914 goto done;
915 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 } else if (have_local_change && have_incoming_change) {
917 if (deriv_len == ondisk_len &&
918 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 /* Both sides made the same change. */
920 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
921 path);
922 } else {
923 err = install_symlink_conflict(deriv_target,
924 deriv_base_commit_id, ancestor_target, label_orig,
925 ondisk_target, ondisk_path);
926 if (err)
927 goto done;
928 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
929 path);
933 done:
934 free(ancestor_target);
935 return err;
938 static const struct got_error *
939 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
941 const struct got_error *err = NULL;
942 char target_path[PATH_MAX];
943 ssize_t target_len;
944 size_t n;
945 FILE *f;
947 *outfile = NULL;
949 f = got_opentemp();
950 if (f == NULL)
951 return got_error_from_errno("got_opentemp");
952 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 if (target_len == -1) {
954 err = got_error_from_errno2("readlink", ondisk_path);
955 goto done;
957 n = fwrite(target_path, 1, target_len, f);
958 if (n != target_len) {
959 err = got_ferror(f, GOT_ERR_IO);
960 goto done;
962 if (fflush(f) == EOF) {
963 err = got_error_from_errno("fflush");
964 goto done;
966 if (fseek(f, 0L, SEEK_SET) == -1) {
967 err = got_ferror(f, GOT_ERR_IO);
968 goto done;
970 done:
971 if (err)
972 fclose(f);
973 else
974 *outfile = f;
975 return err;
978 /*
979 * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 * blob_deriv acts as the first derived version, and the file on disk
981 * acts as the second derived version.
982 */
983 static const struct got_error *
984 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 struct got_blob_object *blob_orig, const char *ondisk_path,
986 const char *path, uint16_t st_mode, const char *label_orig,
987 struct got_blob_object *blob_deriv,
988 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 got_worktree_checkout_cb progress_cb, void *progress_arg)
991 const struct got_error *err = NULL;
992 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 char *blob_orig_path = NULL;
994 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 char *label_deriv = NULL, *parent = NULL;
997 *local_changes_subsumed = 0;
999 err = got_path_dirname(&parent, ondisk_path);
1000 if (err)
1001 return err;
1003 if (blob_orig) {
1004 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1005 parent) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 base_path = NULL;
1008 goto done;
1011 err = got_opentemp_named(&blob_orig_path, &f_orig,
1012 base_path, "");
1013 if (err)
1014 goto done;
1015 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1016 blob_orig);
1017 if (err)
1018 goto done;
1019 free(base_path);
1020 } else {
1022 * No common ancestor exists. This is an "add vs add" conflict
1023 * and we simply use an empty ancestor file to make both files
1024 * appear in the merged result in their entirety.
1026 f_orig = got_opentemp();
1027 if (f_orig == NULL) {
1028 err = got_error_from_errno("got_opentemp");
1029 goto done;
1033 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 base_path = NULL;
1036 goto done;
1039 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1040 if (err)
1041 goto done;
1042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1043 blob_deriv);
1044 if (err)
1045 goto done;
1047 err = got_object_id_str(&id_str, deriv_base_commit_id);
1048 if (err)
1049 goto done;
1050 if (asprintf(&label_deriv, "%s: commit %s",
1051 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 err = got_error_from_errno("asprintf");
1053 goto done;
1057 * In order the run a 3-way merge with a symlink we copy the symlink's
1058 * target path into a temporary file and use that file with diff3.
1060 if (S_ISLNK(st_mode)) {
1061 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1062 if (err)
1063 goto done;
1064 } else {
1065 int fd;
1066 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1067 if (fd == -1) {
1068 err = got_error_from_errno2("open", ondisk_path);
1069 goto done;
1071 f_deriv2 = fdopen(fd, "r");
1072 if (f_deriv2 == NULL) {
1073 err = got_error_from_errno2("fdopen", ondisk_path);
1074 close(fd);
1075 goto done;
1079 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1082 done:
1083 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 err = got_error_from_errno("fclose");
1085 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 err = got_error_from_errno("fclose");
1087 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 err = got_error_from_errno("fclose");
1089 free(base_path);
1090 if (blob_orig_path) {
1091 unlink(blob_orig_path);
1092 free(blob_orig_path);
1094 if (blob_deriv_path) {
1095 unlink(blob_deriv_path);
1096 free(blob_deriv_path);
1098 free(id_str);
1099 free(label_deriv);
1100 free(parent);
1101 return err;
1104 static const struct got_error *
1105 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 int wt_fd, const char *path, struct got_object_id *blob_id)
1109 const struct got_error *err = NULL;
1110 struct got_fileindex_entry *new_ie;
1112 *new_iep = NULL;
1114 err = got_fileindex_entry_alloc(&new_ie, path);
1115 if (err)
1116 return err;
1118 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1119 blob_id->sha1, base_commit_id->sha1, 1);
1120 if (err)
1121 goto done;
1123 err = got_fileindex_entry_add(fileindex, new_ie);
1124 done:
1125 if (err)
1126 got_fileindex_entry_free(new_ie);
1127 else
1128 *new_iep = new_ie;
1129 return err;
1132 static mode_t
1133 get_ondisk_perms(int executable, mode_t st_mode)
1135 mode_t xbits = S_IXUSR;
1137 if (executable) {
1138 /* Map read bits to execute bits. */
1139 if (st_mode & S_IRGRP)
1140 xbits |= S_IXGRP;
1141 if (st_mode & S_IROTH)
1142 xbits |= S_IXOTH;
1143 return st_mode | xbits;
1146 return st_mode;
1149 /* forward declaration */
1150 static const struct got_error *
1151 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1152 const char *path, mode_t te_mode, mode_t st_mode,
1153 struct got_blob_object *blob, int restoring_missing_file,
1154 int reverting_versioned_file, int installing_bad_symlink,
1155 int path_is_unversioned, struct got_repository *repo,
1156 got_worktree_checkout_cb progress_cb, void *progress_arg);
1159 * This function assumes that the provided symlink target points at a
1160 * safe location in the work tree!
1162 static const struct got_error *
1163 replace_existing_symlink(int *did_something, const char *ondisk_path,
1164 const char *target_path, size_t target_len)
1166 const struct got_error *err = NULL;
1167 ssize_t elen;
1168 char etarget[PATH_MAX];
1169 int fd;
1171 *did_something = 0;
1174 * "Bad" symlinks (those pointing outside the work tree or into the
1175 * .got directory) are installed in the work tree as a regular file
1176 * which contains the bad symlink target path.
1177 * The new symlink target has already been checked for safety by our
1178 * caller. If we can successfully open a regular file then we simply
1179 * replace this file with a symlink below.
1181 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1182 if (fd == -1) {
1183 if (!got_err_open_nofollow_on_symlink())
1184 return got_error_from_errno2("open", ondisk_path);
1186 /* We are updating an existing on-disk symlink. */
1187 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1188 if (elen == -1)
1189 return got_error_from_errno2("readlink", ondisk_path);
1191 if (elen == target_len &&
1192 memcmp(etarget, target_path, target_len) == 0)
1193 return NULL; /* nothing to do */
1196 *did_something = 1;
1197 err = update_symlink(ondisk_path, target_path, target_len);
1198 if (fd != -1 && close(fd) == -1 && err == NULL)
1199 err = got_error_from_errno2("close", ondisk_path);
1200 return err;
1203 static const struct got_error *
1204 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1205 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1207 const struct got_error *err = NULL;
1208 char canonpath[PATH_MAX];
1209 char *path_got = NULL;
1211 *is_bad_symlink = 0;
1213 if (target_len >= sizeof(canonpath)) {
1214 *is_bad_symlink = 1;
1215 return NULL;
1219 * We do not use realpath(3) to resolve the symlink's target
1220 * path because we don't want to resolve symlinks recursively.
1221 * Instead we make the path absolute and then canonicalize it.
1222 * Relative symlink target lookup should begin at the directory
1223 * in which the blob object is being installed.
1225 if (!got_path_is_absolute(target_path)) {
1226 char *abspath, *parent;
1227 err = got_path_dirname(&parent, ondisk_path);
1228 if (err)
1229 return err;
1230 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1231 free(parent);
1232 return got_error_from_errno("asprintf");
1234 free(parent);
1235 if (strlen(abspath) >= sizeof(canonpath)) {
1236 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1237 free(abspath);
1238 return err;
1240 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1241 free(abspath);
1242 if (err)
1243 return err;
1244 } else {
1245 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1246 if (err)
1247 return err;
1250 /* Only allow symlinks pointing at paths within the work tree. */
1251 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1252 *is_bad_symlink = 1;
1253 return NULL;
1256 /* Do not allow symlinks pointing into the .got directory. */
1257 if (asprintf(&path_got, "%s/%s", wtroot_path,
1258 GOT_WORKTREE_GOT_DIR) == -1)
1259 return got_error_from_errno("asprintf");
1260 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1261 *is_bad_symlink = 1;
1263 free(path_got);
1264 return NULL;
1267 static const struct got_error *
1268 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1269 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1270 int restoring_missing_file, int reverting_versioned_file,
1271 int path_is_unversioned, int allow_bad_symlinks,
1272 struct got_repository *repo,
1273 got_worktree_checkout_cb progress_cb, void *progress_arg)
1275 const struct got_error *err = NULL;
1276 char target_path[PATH_MAX];
1277 size_t len, target_len = 0;
1278 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1279 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1281 *is_bad_symlink = 0;
1284 * Blob object content specifies the target path of the link.
1285 * If a symbolic link cannot be installed we instead create
1286 * a regular file which contains the link target path stored
1287 * in the blob object.
1289 do {
1290 err = got_object_blob_read_block(&len, blob);
1291 if (err)
1292 return err;
1294 if (len + target_len >= sizeof(target_path)) {
1295 /* Path too long; install as a regular file. */
1296 *is_bad_symlink = 1;
1297 got_object_blob_rewind(blob);
1298 return install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file,
1301 1, path_is_unversioned, repo, progress_cb,
1302 progress_arg);
1304 if (len > 0) {
1305 /* Skip blob object header first time around. */
1306 memcpy(target_path + target_len, buf + hdrlen,
1307 len - hdrlen);
1308 target_len += len - hdrlen;
1309 hdrlen = 0;
1311 } while (len != 0);
1312 target_path[target_len] = '\0';
1314 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1315 ondisk_path, worktree->root_path);
1316 if (err)
1317 return err;
1319 if (*is_bad_symlink && !allow_bad_symlinks) {
1320 /* install as a regular file */
1321 got_object_blob_rewind(blob);
1322 err = install_blob(worktree, ondisk_path, path,
1323 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1324 restoring_missing_file, reverting_versioned_file, 1,
1325 path_is_unversioned, repo, progress_cb, progress_arg);
1326 return err;
1329 if (symlink(target_path, ondisk_path) == -1) {
1330 if (errno == EEXIST) {
1331 int symlink_replaced;
1332 if (path_is_unversioned) {
1333 err = (*progress_cb)(progress_arg,
1334 GOT_STATUS_UNVERSIONED, path);
1335 return err;
1337 err = replace_existing_symlink(&symlink_replaced,
1338 ondisk_path, target_path, target_len);
1339 if (err)
1340 return err;
1341 if (progress_cb) {
1342 if (symlink_replaced) {
1343 err = (*progress_cb)(progress_arg,
1344 reverting_versioned_file ?
1345 GOT_STATUS_REVERT :
1346 GOT_STATUS_UPDATE, path);
1347 } else {
1348 err = (*progress_cb)(progress_arg,
1349 GOT_STATUS_EXISTS, path);
1352 return err; /* Nothing else to do. */
1355 if (errno == ENOENT) {
1356 char *parent;
1357 err = got_path_dirname(&parent, ondisk_path);
1358 if (err)
1359 return err;
1360 err = got_path_mkdir(parent);
1361 free(parent);
1362 if (err)
1363 return err;
1365 * Retry, and fall through to error handling
1366 * below if this second attempt fails.
1368 if (symlink(target_path, ondisk_path) != -1) {
1369 err = NULL; /* success */
1370 if (progress_cb) {
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1374 path);
1376 return err;
1380 /* Handle errors from first or second creation attempt. */
1381 if (errno == ENAMETOOLONG) {
1382 /* bad target path; install as a regular file */
1383 *is_bad_symlink = 1;
1384 got_object_blob_rewind(blob);
1385 err = install_blob(worktree, ondisk_path, path,
1386 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 restoring_missing_file, reverting_versioned_file, 1,
1388 path_is_unversioned, repo,
1389 progress_cb, progress_arg);
1390 } else if (errno == ENOTDIR) {
1391 err = got_error_path(ondisk_path,
1392 GOT_ERR_FILE_OBSTRUCTED);
1393 } else {
1394 err = got_error_from_errno3("symlink",
1395 target_path, ondisk_path);
1397 } else if (progress_cb)
1398 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1400 return err;
1403 static const struct got_error *
1404 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 const char *path, mode_t te_mode, mode_t st_mode,
1406 struct got_blob_object *blob, int restoring_missing_file,
1407 int reverting_versioned_file, int installing_bad_symlink,
1408 int path_is_unversioned, struct got_repository *repo,
1409 got_worktree_checkout_cb progress_cb, void *progress_arg)
1411 const struct got_error *err = NULL;
1412 int fd = -1;
1413 size_t len, hdrlen;
1414 int update = 0;
1415 char *tmppath = NULL;
1416 mode_t mode;
1418 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1419 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1420 O_CLOEXEC, mode);
1421 if (fd == -1) {
1422 if (errno == ENOENT || errno == ENOTDIR) {
1423 char *parent;
1424 err = got_path_dirname(&parent, path);
1425 if (err)
1426 return err;
1427 err = add_dir_on_disk(worktree, parent);
1428 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1429 err = got_error_path(path, err->code);
1430 free(parent);
1431 if (err)
1432 return err;
1433 fd = open(ondisk_path,
1434 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1435 mode);
1436 if (fd == -1)
1437 return got_error_from_errno2("open",
1438 ondisk_path);
1439 } else if (errno == EEXIST) {
1440 if (path_is_unversioned) {
1441 err = (*progress_cb)(progress_arg,
1442 GOT_STATUS_UNVERSIONED, path);
1443 goto done;
1445 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1446 !S_ISREG(st_mode) && !installing_bad_symlink) {
1447 /* TODO file is obstructed; do something */
1448 err = got_error_path(ondisk_path,
1449 GOT_ERR_FILE_OBSTRUCTED);
1450 goto done;
1451 } else {
1452 err = got_opentemp_named_fd(&tmppath, &fd,
1453 ondisk_path, "");
1454 if (err)
1455 goto done;
1456 update = 1;
1458 if (fchmod(fd, apply_umask(mode)) == -1) {
1459 err = got_error_from_errno2("fchmod",
1460 tmppath);
1461 goto done;
1464 } else
1465 return got_error_from_errno2("open", ondisk_path);
1468 if (progress_cb) {
1469 if (restoring_missing_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1471 path);
1472 else if (reverting_versioned_file)
1473 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1474 path);
1475 else
1476 err = (*progress_cb)(progress_arg,
1477 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1478 if (err)
1479 goto done;
1482 hdrlen = got_object_blob_get_hdrlen(blob);
1483 do {
1484 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1485 err = got_object_blob_read_block(&len, blob);
1486 if (err)
1487 break;
1488 if (len > 0) {
1489 /* Skip blob object header first time around. */
1490 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1491 if (outlen == -1) {
1492 err = got_error_from_errno("write");
1493 goto done;
1494 } else if (outlen != len - hdrlen) {
1495 err = got_error(GOT_ERR_IO);
1496 goto done;
1498 hdrlen = 0;
1500 } while (len != 0);
1502 if (fsync(fd) != 0) {
1503 err = got_error_from_errno("fsync");
1504 goto done;
1507 if (update) {
1508 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1509 err = got_error_from_errno2("unlink", ondisk_path);
1510 goto done;
1512 if (rename(tmppath, ondisk_path) != 0) {
1513 err = got_error_from_errno3("rename", tmppath,
1514 ondisk_path);
1515 goto done;
1517 free(tmppath);
1518 tmppath = NULL;
1521 done:
1522 if (fd != -1 && close(fd) == -1 && err == NULL)
1523 err = got_error_from_errno("close");
1524 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1525 err = got_error_from_errno2("unlink", tmppath);
1526 free(tmppath);
1527 return err;
1531 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1532 * conflict marker is found in newly added lines only.
1534 static const struct got_error *
1535 get_modified_file_content_status(unsigned char *status,
1536 struct got_blob_object *blob, const char *path, struct stat *sb,
1537 FILE *ondisk_file)
1539 const struct got_error *err, *free_err;
1540 const char *markers[3] = {
1541 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 GOT_DIFF_CONFLICT_MARKER_SEP,
1543 GOT_DIFF_CONFLICT_MARKER_END
1545 FILE *f1 = NULL;
1546 struct got_diffreg_result *diffreg_result = NULL;
1547 struct diff_result *r;
1548 int nchunks_parsed, n, i = 0, ln = 0;
1549 char *line = NULL;
1550 size_t linesize = 0;
1551 ssize_t linelen;
1553 if (*status != GOT_STATUS_MODIFY)
1554 return NULL;
1556 f1 = got_opentemp();
1557 if (f1 == NULL)
1558 return got_error_from_errno("got_opentemp");
1560 if (blob) {
1561 got_object_blob_rewind(blob);
1562 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1563 if (err)
1564 goto done;
1567 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1568 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1569 if (err)
1570 goto done;
1572 r = diffreg_result->result;
1574 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1575 struct diff_chunk *c;
1576 struct diff_chunk_context cc = {};
1577 off_t pos;
1580 * We can optimise a little by advancing straight
1581 * to the next chunk if this one has no added lines.
1583 c = diff_chunk_get(r, n);
1585 if (diff_chunk_type(c) != CHUNK_PLUS) {
1586 nchunks_parsed = 1;
1587 continue; /* removed or unchanged lines */
1590 pos = diff_chunk_get_right_start_pos(c);
1591 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1592 err = got_ferror(ondisk_file, GOT_ERR_IO);
1593 goto done;
1596 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1597 ln = cc.right.start;
1599 while (ln < cc.right.end) {
1600 linelen = getline(&line, &linesize, ondisk_file);
1601 if (linelen == -1) {
1602 if (feof(ondisk_file))
1603 break;
1604 err = got_ferror(ondisk_file, GOT_ERR_IO);
1605 break;
1608 if (line && strncmp(line, markers[i],
1609 strlen(markers[i])) == 0) {
1610 if (strcmp(markers[i],
1611 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1612 *status = GOT_STATUS_CONFLICT;
1613 goto done;
1614 } else
1615 i++;
1617 ++ln;
1621 done:
1622 free(line);
1623 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1624 err = got_error_from_errno("fclose");
1625 free_err = got_diffreg_result_free(diffreg_result);
1626 if (err == NULL)
1627 err = free_err;
1629 return err;
1632 static int
1633 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1635 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1639 static int
1640 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1642 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 ie->size == (sb->st_size & 0xffffffff) &&
1647 !xbit_differs(ie, sb->st_mode));
1650 static unsigned char
1651 get_staged_status(struct got_fileindex_entry *ie)
1653 switch (got_fileindex_entry_stage_get(ie)) {
1654 case GOT_FILEIDX_STAGE_ADD:
1655 return GOT_STATUS_ADD;
1656 case GOT_FILEIDX_STAGE_DELETE:
1657 return GOT_STATUS_DELETE;
1658 case GOT_FILEIDX_STAGE_MODIFY:
1659 return GOT_STATUS_MODIFY;
1660 default:
1661 return GOT_STATUS_NO_CHANGE;
1665 static const struct got_error *
1666 get_symlink_modification_status(unsigned char *status,
1667 struct got_fileindex_entry *ie, const char *abspath,
1668 int dirfd, const char *de_name, struct got_blob_object *blob)
1670 const struct got_error *err = NULL;
1671 char target_path[PATH_MAX];
1672 char etarget[PATH_MAX];
1673 ssize_t elen;
1674 size_t len, target_len = 0;
1675 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1678 *status = GOT_STATUS_NO_CHANGE;
1680 /* Blob object content specifies the target path of the link. */
1681 do {
1682 err = got_object_blob_read_block(&len, blob);
1683 if (err)
1684 return err;
1685 if (len + target_len >= sizeof(target_path)) {
1687 * Should not happen. The blob contents were OK
1688 * when this symlink was installed.
1690 return got_error(GOT_ERR_NO_SPACE);
1692 if (len > 0) {
1693 /* Skip blob object header first time around. */
1694 memcpy(target_path + target_len, buf + hdrlen,
1695 len - hdrlen);
1696 target_len += len - hdrlen;
1697 hdrlen = 0;
1699 } while (len != 0);
1700 target_path[target_len] = '\0';
1702 if (dirfd != -1) {
1703 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1704 if (elen == -1)
1705 return got_error_from_errno2("readlinkat", abspath);
1706 } else {
1707 elen = readlink(abspath, etarget, sizeof(etarget));
1708 if (elen == -1)
1709 return got_error_from_errno2("readlink", abspath);
1712 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 *status = GOT_STATUS_MODIFY;
1715 return NULL;
1718 static const struct got_error *
1719 get_file_status(unsigned char *status, struct stat *sb,
1720 struct got_fileindex_entry *ie, const char *abspath,
1721 int dirfd, const char *de_name, struct got_repository *repo)
1723 const struct got_error *err = NULL;
1724 struct got_object_id id;
1725 size_t hdrlen;
1726 int fd = -1, fd1 = -1;
1727 FILE *f = NULL;
1728 uint8_t fbuf[8192];
1729 struct got_blob_object *blob = NULL;
1730 size_t flen, blen;
1731 unsigned char staged_status;
1733 staged_status = get_staged_status(ie);
1734 *status = GOT_STATUS_NO_CHANGE;
1735 memset(sb, 0, sizeof(*sb));
1738 * Whenever the caller provides a directory descriptor and a
1739 * directory entry name for the file, use them! This prevents
1740 * race conditions if filesystem paths change beneath our feet.
1742 if (dirfd != -1) {
1743 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1744 if (errno == ENOENT) {
1745 if (got_fileindex_entry_has_file_on_disk(ie))
1746 *status = GOT_STATUS_MISSING;
1747 else
1748 *status = GOT_STATUS_DELETE;
1749 goto done;
1751 err = got_error_from_errno2("fstatat", abspath);
1752 goto done;
1754 } else {
1755 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1756 if (fd == -1 && errno != ENOENT &&
1757 !got_err_open_nofollow_on_symlink())
1758 return got_error_from_errno2("open", abspath);
1759 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1760 if (lstat(abspath, sb) == -1)
1761 return got_error_from_errno2("lstat", abspath);
1762 } else if (fd == -1 || fstat(fd, sb) == -1) {
1763 if (errno == ENOENT) {
1764 if (got_fileindex_entry_has_file_on_disk(ie))
1765 *status = GOT_STATUS_MISSING;
1766 else
1767 *status = GOT_STATUS_DELETE;
1768 goto done;
1770 err = got_error_from_errno2("fstat", abspath);
1771 goto done;
1775 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1776 *status = GOT_STATUS_OBSTRUCTED;
1777 goto done;
1780 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1781 *status = GOT_STATUS_DELETE;
1782 goto done;
1783 } else if (!got_fileindex_entry_has_blob(ie) &&
1784 staged_status != GOT_STATUS_ADD) {
1785 *status = GOT_STATUS_ADD;
1786 goto done;
1789 if (!stat_info_differs(ie, sb))
1790 goto done;
1792 if (S_ISLNK(sb->st_mode) &&
1793 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1794 *status = GOT_STATUS_MODIFY;
1795 goto done;
1798 if (staged_status == GOT_STATUS_MODIFY ||
1799 staged_status == GOT_STATUS_ADD)
1800 got_fileindex_entry_get_staged_blob_id(&id, ie);
1801 else
1802 got_fileindex_entry_get_blob_id(&id, ie);
1804 fd1 = got_opentempfd();
1805 if (fd1 == -1) {
1806 err = got_error_from_errno("got_opentempfd");
1807 goto done;
1809 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1810 if (err)
1811 goto done;
1813 if (S_ISLNK(sb->st_mode)) {
1814 err = get_symlink_modification_status(status, ie,
1815 abspath, dirfd, de_name, blob);
1816 goto done;
1819 if (dirfd != -1) {
1820 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1821 if (fd == -1) {
1822 err = got_error_from_errno2("openat", abspath);
1823 goto done;
1827 f = fdopen(fd, "r");
1828 if (f == NULL) {
1829 err = got_error_from_errno2("fdopen", abspath);
1830 goto done;
1832 fd = -1;
1833 hdrlen = got_object_blob_get_hdrlen(blob);
1834 for (;;) {
1835 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1836 err = got_object_blob_read_block(&blen, blob);
1837 if (err)
1838 goto done;
1839 /* Skip length of blob object header first time around. */
1840 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1841 if (flen == 0 && ferror(f)) {
1842 err = got_error_from_errno("fread");
1843 goto done;
1845 if (blen - hdrlen == 0) {
1846 if (flen != 0)
1847 *status = GOT_STATUS_MODIFY;
1848 break;
1849 } else if (flen == 0) {
1850 if (blen - hdrlen != 0)
1851 *status = GOT_STATUS_MODIFY;
1852 break;
1853 } else if (blen - hdrlen == flen) {
1854 /* Skip blob object header first time around. */
1855 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1856 *status = GOT_STATUS_MODIFY;
1857 break;
1859 } else {
1860 *status = GOT_STATUS_MODIFY;
1861 break;
1863 hdrlen = 0;
1866 if (*status == GOT_STATUS_MODIFY) {
1867 rewind(f);
1868 err = get_modified_file_content_status(status, blob, ie->path,
1869 sb, f);
1870 } else if (xbit_differs(ie, sb->st_mode))
1871 *status = GOT_STATUS_MODE_CHANGE;
1872 done:
1873 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1874 err = got_error_from_errno("close");
1875 if (blob)
1876 got_object_blob_close(blob);
1877 if (f != NULL && fclose(f) == EOF && err == NULL)
1878 err = got_error_from_errno2("fclose", abspath);
1879 if (fd != -1 && close(fd) == -1 && err == NULL)
1880 err = got_error_from_errno2("close", abspath);
1881 return err;
1885 * Update timestamps in the file index if a file is unmodified and
1886 * we had to run a full content comparison to find out.
1888 static const struct got_error *
1889 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1890 struct got_fileindex_entry *ie, struct stat *sb)
1892 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1893 return got_fileindex_entry_update(ie, wt_fd, path,
1894 ie->blob_sha1, ie->commit_sha1, 1);
1896 return NULL;
1899 static const struct got_error *remove_ondisk_file(const char *, const char *);
1901 static const struct got_error *
1902 update_blob(struct got_worktree *worktree,
1903 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1904 struct got_tree_entry *te, const char *path,
1905 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1906 void *progress_arg)
1908 const struct got_error *err = NULL;
1909 struct got_blob_object *blob = NULL;
1910 char *ondisk_path = NULL;
1911 unsigned char status = GOT_STATUS_NO_CHANGE;
1912 struct stat sb;
1913 int fd1 = -1, fd2 = -1;
1915 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1916 return got_error_from_errno("asprintf");
1918 if (ie) {
1919 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1920 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1921 goto done;
1923 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1924 repo);
1925 if (err)
1926 goto done;
1927 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1928 sb.st_mode = got_fileindex_perms_to_st(ie);
1929 } else {
1930 if (stat(ondisk_path, &sb) == -1) {
1931 if (errno != ENOENT && errno != ENOTDIR) {
1932 err = got_error_from_errno2("stat",
1933 ondisk_path);
1934 goto done;
1936 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1937 status = GOT_STATUS_UNVERSIONED;
1938 } else {
1939 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1940 status = GOT_STATUS_UNVERSIONED;
1941 else
1942 status = GOT_STATUS_OBSTRUCTED;
1946 if (status == GOT_STATUS_OBSTRUCTED) {
1947 if (ie)
1948 got_fileindex_entry_mark_skipped(ie);
1949 err = (*progress_cb)(progress_arg, status, path);
1950 goto done;
1952 if (status == GOT_STATUS_CONFLICT) {
1953 if (ie)
1954 got_fileindex_entry_mark_skipped(ie);
1955 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1956 path);
1957 goto done;
1960 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1961 if (status == GOT_STATUS_UNVERSIONED) {
1962 err = (*progress_cb)(progress_arg, status, path);
1963 } else if (status != GOT_STATUS_NO_CHANGE &&
1964 status != GOT_STATUS_DELETE &&
1965 status != GOT_STATUS_NONEXISTENT &&
1966 status != GOT_STATUS_MISSING) {
1967 err = (*progress_cb)(progress_arg,
1968 GOT_STATUS_CANNOT_DELETE, path);
1969 } else if (ie) {
1970 if (status != GOT_STATUS_DELETE &&
1971 status != GOT_STATUS_NONEXISTENT &&
1972 status != GOT_STATUS_MISSING) {
1973 err = remove_ondisk_file(worktree->root_path,
1974 ie->path);
1975 if (err && !(err->code == GOT_ERR_ERRNO &&
1976 errno == ENOENT))
1977 goto done;
1979 got_fileindex_entry_remove(fileindex, ie);
1980 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1981 ie->path);
1983 goto done; /* nothing else to do */
1986 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1987 (S_ISLNK(te->mode) ||
1988 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1990 * This is a regular file or an installed bad symlink.
1991 * If the file index indicates that this file is already
1992 * up-to-date with respect to the repository we can skip
1993 * updating contents of this file.
1995 if (got_fileindex_entry_has_commit(ie) &&
1996 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1997 SHA1_DIGEST_LENGTH) == 0) {
1998 /* Same commit. */
1999 err = sync_timestamps(worktree->root_fd,
2000 path, status, ie, &sb);
2001 if (err)
2002 goto done;
2003 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2004 path);
2005 goto done;
2007 if (got_fileindex_entry_has_blob(ie) &&
2008 memcmp(ie->blob_sha1, te->id.sha1,
2009 SHA1_DIGEST_LENGTH) == 0) {
2010 /* Different commit but the same blob. */
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 /* Update the base commit ID of this file. */
2013 memcpy(ie->commit_sha1,
2014 worktree->base_commit_id->sha1,
2015 sizeof(ie->commit_sha1));
2017 err = sync_timestamps(worktree->root_fd,
2018 path, status, ie, &sb);
2019 if (err)
2020 goto done;
2021 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2022 path);
2023 goto done;
2027 fd1 = got_opentempfd();
2028 if (fd1 == -1) {
2029 err = got_error_from_errno("got_opentempfd");
2030 goto done;
2032 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2033 if (err)
2034 goto done;
2036 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2037 int update_timestamps;
2038 struct got_blob_object *blob2 = NULL;
2039 char *label_orig = NULL;
2040 if (got_fileindex_entry_has_blob(ie)) {
2041 fd2 = got_opentempfd();
2042 if (fd2 == -1) {
2043 err = got_error_from_errno("got_opentempfd");
2044 goto done;
2046 struct got_object_id id2;
2047 got_fileindex_entry_get_blob_id(&id2, ie);
2048 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2049 fd2);
2050 if (err)
2051 goto done;
2053 if (got_fileindex_entry_has_commit(ie)) {
2054 char id_str[SHA1_DIGEST_STRING_LENGTH];
2055 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2056 sizeof(id_str)) == NULL) {
2057 err = got_error_path(id_str,
2058 GOT_ERR_BAD_OBJ_ID_STR);
2059 goto done;
2061 if (asprintf(&label_orig, "%s: commit %s",
2062 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2063 err = got_error_from_errno("asprintf");
2064 goto done;
2067 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2068 char *link_target;
2069 err = got_object_blob_read_to_str(&link_target, blob);
2070 if (err)
2071 goto done;
2072 err = merge_symlink(worktree, blob2, ondisk_path, path,
2073 label_orig, link_target, worktree->base_commit_id,
2074 repo, progress_cb, progress_arg);
2075 free(link_target);
2076 } else {
2077 err = merge_blob(&update_timestamps, worktree, blob2,
2078 ondisk_path, path, sb.st_mode, label_orig, blob,
2079 worktree->base_commit_id, repo,
2080 progress_cb, progress_arg);
2082 free(label_orig);
2083 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2084 err = got_error_from_errno("close");
2085 goto done;
2087 if (blob2)
2088 got_object_blob_close(blob2);
2089 if (err)
2090 goto done;
2092 * Do not update timestamps of files with local changes.
2093 * Otherwise, a future status walk would treat them as
2094 * unmodified files again.
2096 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2097 blob->id.sha1, worktree->base_commit_id->sha1,
2098 update_timestamps);
2099 } else if (status == GOT_STATUS_MODE_CHANGE) {
2100 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2101 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2102 } else if (status == GOT_STATUS_DELETE) {
2103 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2104 if (err)
2105 goto done;
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 if (err)
2109 goto done;
2110 } else {
2111 int is_bad_symlink = 0;
2112 if (S_ISLNK(te->mode)) {
2113 err = install_symlink(&is_bad_symlink, worktree,
2114 ondisk_path, path, blob,
2115 status == GOT_STATUS_MISSING, 0,
2116 status == GOT_STATUS_UNVERSIONED, 0,
2117 repo, progress_cb, progress_arg);
2118 } else {
2119 err = install_blob(worktree, ondisk_path, path,
2120 te->mode, sb.st_mode, blob,
2121 status == GOT_STATUS_MISSING, 0, 0,
2122 status == GOT_STATUS_UNVERSIONED, repo,
2123 progress_cb, progress_arg);
2125 if (err)
2126 goto done;
2128 if (ie) {
2129 err = got_fileindex_entry_update(ie,
2130 worktree->root_fd, path, blob->id.sha1,
2131 worktree->base_commit_id->sha1, 1);
2132 } else {
2133 err = create_fileindex_entry(&ie, fileindex,
2134 worktree->base_commit_id, worktree->root_fd, path,
2135 &blob->id);
2137 if (err)
2138 goto done;
2140 if (is_bad_symlink) {
2141 got_fileindex_entry_filetype_set(ie,
2142 GOT_FILEIDX_MODE_BAD_SYMLINK);
2146 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2147 err = got_error_from_errno("close");
2148 goto done;
2150 got_object_blob_close(blob);
2151 done:
2152 free(ondisk_path);
2153 return err;
2156 static const struct got_error *
2157 remove_ondisk_file(const char *root_path, const char *path)
2159 const struct got_error *err = NULL;
2160 char *ondisk_path = NULL, *parent = NULL;
2162 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2163 return got_error_from_errno("asprintf");
2165 if (unlink(ondisk_path) == -1) {
2166 if (errno != ENOENT)
2167 err = got_error_from_errno2("unlink", ondisk_path);
2168 } else {
2169 size_t root_len = strlen(root_path);
2170 err = got_path_dirname(&parent, ondisk_path);
2171 if (err)
2172 goto done;
2173 while (got_path_cmp(parent, root_path,
2174 strlen(parent), root_len) != 0) {
2175 free(ondisk_path);
2176 ondisk_path = parent;
2177 parent = NULL;
2178 if (rmdir(ondisk_path) == -1) {
2179 if (errno != ENOTEMPTY)
2180 err = got_error_from_errno2("rmdir",
2181 ondisk_path);
2182 break;
2184 err = got_path_dirname(&parent, ondisk_path);
2185 if (err)
2186 break;
2189 done:
2190 free(ondisk_path);
2191 free(parent);
2192 return err;
2195 static const struct got_error *
2196 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2197 struct got_fileindex_entry *ie, struct got_repository *repo,
2198 got_worktree_checkout_cb progress_cb, void *progress_arg)
2200 const struct got_error *err = NULL;
2201 unsigned char status;
2202 struct stat sb;
2203 char *ondisk_path;
2205 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2206 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2208 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2209 == -1)
2210 return got_error_from_errno("asprintf");
2212 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2213 if (err)
2214 goto done;
2216 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2217 char ondisk_target[PATH_MAX];
2218 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2219 sizeof(ondisk_target));
2220 if (ondisk_len == -1) {
2221 err = got_error_from_errno2("readlink", ondisk_path);
2222 goto done;
2224 ondisk_target[ondisk_len] = '\0';
2225 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2226 NULL, NULL, /* XXX pass common ancestor info? */
2227 ondisk_target, ondisk_path);
2228 if (err)
2229 goto done;
2230 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2231 ie->path);
2232 goto done;
2235 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2236 status == GOT_STATUS_ADD) {
2237 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2238 if (err)
2239 goto done;
2241 * Preserve the working file and change the deleted blob's
2242 * entry into a schedule-add entry.
2244 err = got_fileindex_entry_update(ie, worktree->root_fd,
2245 ie->path, NULL, NULL, 0);
2246 } else {
2247 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2248 if (err)
2249 goto done;
2250 if (status == GOT_STATUS_NO_CHANGE) {
2251 err = remove_ondisk_file(worktree->root_path, ie->path);
2252 if (err)
2253 goto done;
2255 got_fileindex_entry_remove(fileindex, ie);
2257 done:
2258 free(ondisk_path);
2259 return err;
2262 struct diff_cb_arg {
2263 struct got_fileindex *fileindex;
2264 struct got_worktree *worktree;
2265 struct got_repository *repo;
2266 got_worktree_checkout_cb progress_cb;
2267 void *progress_arg;
2268 got_cancel_cb cancel_cb;
2269 void *cancel_arg;
2272 static const struct got_error *
2273 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2274 struct got_tree_entry *te, const char *parent_path)
2276 struct diff_cb_arg *a = arg;
2278 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2279 return got_error(GOT_ERR_CANCELLED);
2281 return update_blob(a->worktree, a->fileindex, ie, te,
2282 ie->path, a->repo, a->progress_cb, a->progress_arg);
2285 static const struct got_error *
2286 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2288 struct diff_cb_arg *a = arg;
2290 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 return got_error(GOT_ERR_CANCELLED);
2293 return delete_blob(a->worktree, a->fileindex, ie,
2294 a->repo, a->progress_cb, a->progress_arg);
2297 static const struct got_error *
2298 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2300 struct diff_cb_arg *a = arg;
2301 const struct got_error *err;
2302 char *path;
2304 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2305 return got_error(GOT_ERR_CANCELLED);
2307 if (got_object_tree_entry_is_submodule(te))
2308 return NULL;
2310 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2311 return NULL;
2313 if (asprintf(&path, "%s%s%s", parent_path,
2314 parent_path[0] ? "/" : "", te->name)
2315 == -1)
2316 return got_error_from_errno("asprintf");
2318 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2319 a->repo, a->progress_cb, a->progress_arg);
2321 free(path);
2322 return err;
2325 const struct got_error *
2326 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2328 uint32_t uuid_status;
2330 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2331 if (uuid_status != uuid_s_ok) {
2332 *uuidstr = NULL;
2333 return got_error_uuid(uuid_status, "uuid_to_string");
2336 return NULL;
2339 static const struct got_error *
2340 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2342 const struct got_error *err = NULL;
2343 char *uuidstr = NULL;
2345 *refname = NULL;
2347 err = got_worktree_get_uuid(&uuidstr, worktree);
2348 if (err)
2349 return err;
2351 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2352 err = got_error_from_errno("asprintf");
2353 *refname = NULL;
2355 free(uuidstr);
2356 return err;
2359 const struct got_error *
2360 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2361 const char *prefix)
2363 return get_ref_name(refname, worktree, prefix);
2366 static const struct got_error *
2367 get_base_ref_name(char **refname, struct got_worktree *worktree)
2369 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2372 static const struct got_error *
2373 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2375 return get_ref_name(refname, worktree,
2376 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2379 static const struct got_error *
2380 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2382 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2385 static const struct got_error *
2386 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2388 return get_ref_name(refname, worktree,
2389 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2392 static const struct got_error *
2393 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2395 return get_ref_name(refname, worktree,
2396 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2399 static const struct got_error *
2400 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2402 return get_ref_name(refname, worktree,
2403 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2406 static const struct got_error *
2407 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2409 return get_ref_name(refname, worktree,
2410 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2413 static const struct got_error *
2414 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2416 return get_ref_name(refname, worktree,
2417 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2420 static const struct got_error *
2421 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2423 return get_ref_name(refname, worktree,
2424 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2427 const struct got_error *
2428 got_worktree_get_histedit_script_path(char **path,
2429 struct got_worktree *worktree)
2431 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2432 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2433 *path = NULL;
2434 return got_error_from_errno("asprintf");
2436 return NULL;
2439 static const struct got_error *
2440 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2442 return get_ref_name(refname, worktree,
2443 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2446 static const struct got_error *
2447 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2449 return get_ref_name(refname, worktree,
2450 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2454 * Prevent Git's garbage collector from deleting our base commit by
2455 * setting a reference to our base commit's ID.
2457 static const struct got_error *
2458 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2460 const struct got_error *err = NULL;
2461 struct got_reference *ref = NULL;
2462 char *refname;
2464 err = get_base_ref_name(&refname, worktree);
2465 if (err)
2466 return err;
2468 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2469 if (err)
2470 goto done;
2472 err = got_ref_write(ref, repo);
2473 done:
2474 free(refname);
2475 if (ref)
2476 got_ref_close(ref);
2477 return err;
2480 static const struct got_error *
2481 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2483 const struct got_error *err = NULL;
2485 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2486 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2487 err = got_error_from_errno("asprintf");
2488 *fileindex_path = NULL;
2490 return err;
2494 static const struct got_error *
2495 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2496 struct got_worktree *worktree)
2498 const struct got_error *err = NULL;
2499 FILE *index = NULL;
2501 *fileindex_path = NULL;
2502 *fileindex = got_fileindex_alloc();
2503 if (*fileindex == NULL)
2504 return got_error_from_errno("got_fileindex_alloc");
2506 err = get_fileindex_path(fileindex_path, worktree);
2507 if (err)
2508 goto done;
2510 index = fopen(*fileindex_path, "rbe");
2511 if (index == NULL) {
2512 if (errno != ENOENT)
2513 err = got_error_from_errno2("fopen", *fileindex_path);
2514 } else {
2515 err = got_fileindex_read(*fileindex, index);
2516 if (fclose(index) == EOF && err == NULL)
2517 err = got_error_from_errno("fclose");
2519 done:
2520 if (err) {
2521 free(*fileindex_path);
2522 *fileindex_path = NULL;
2523 got_fileindex_free(*fileindex);
2524 *fileindex = NULL;
2526 return err;
2529 struct bump_base_commit_id_arg {
2530 struct got_object_id *base_commit_id;
2531 const char *path;
2532 size_t path_len;
2533 const char *entry_name;
2534 got_worktree_checkout_cb progress_cb;
2535 void *progress_arg;
2538 static const struct got_error *
2539 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2541 const struct got_error *err;
2542 struct bump_base_commit_id_arg *a = arg;
2544 if (a->entry_name) {
2545 if (strcmp(ie->path, a->path) != 0)
2546 return NULL;
2547 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2548 return NULL;
2550 if (got_fileindex_entry_was_skipped(ie))
2551 return NULL;
2553 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2554 SHA1_DIGEST_LENGTH) == 0)
2555 return NULL;
2557 if (a->progress_cb) {
2558 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2559 ie->path);
2560 if (err)
2561 return err;
2563 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2564 return NULL;
2567 /* Bump base commit ID of all files within an updated part of the work tree. */
2568 static const struct got_error *
2569 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2570 struct got_fileindex *fileindex,
2571 got_worktree_checkout_cb progress_cb, void *progress_arg)
2573 struct bump_base_commit_id_arg bbc_arg;
2575 bbc_arg.base_commit_id = worktree->base_commit_id;
2576 bbc_arg.entry_name = NULL;
2577 bbc_arg.path = "";
2578 bbc_arg.path_len = 0;
2579 bbc_arg.progress_cb = progress_cb;
2580 bbc_arg.progress_arg = progress_arg;
2582 return got_fileindex_for_each_entry_safe(fileindex,
2583 bump_base_commit_id, &bbc_arg);
2586 static const struct got_error *
2587 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2589 const struct got_error *err = NULL;
2590 char *new_fileindex_path = NULL;
2591 FILE *new_index = NULL;
2592 struct timespec timeout;
2594 err = got_opentemp_named(&new_fileindex_path, &new_index,
2595 fileindex_path, "");
2596 if (err)
2597 goto done;
2599 err = got_fileindex_write(fileindex, new_index);
2600 if (err)
2601 goto done;
2603 if (rename(new_fileindex_path, fileindex_path) != 0) {
2604 err = got_error_from_errno3("rename", new_fileindex_path,
2605 fileindex_path);
2606 unlink(new_fileindex_path);
2610 * Sleep for a short amount of time to ensure that files modified after
2611 * this program exits have a different time stamp from the one which
2612 * was recorded in the file index.
2614 timeout.tv_sec = 0;
2615 timeout.tv_nsec = 1;
2616 nanosleep(&timeout, NULL);
2617 done:
2618 if (new_index)
2619 fclose(new_index);
2620 free(new_fileindex_path);
2621 return err;
2624 static const struct got_error *
2625 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2626 struct got_object_id **tree_id, const char *wt_relpath,
2627 struct got_commit_object *base_commit, struct got_worktree *worktree,
2628 struct got_repository *repo)
2630 const struct got_error *err = NULL;
2631 struct got_object_id *id = NULL;
2632 char *in_repo_path = NULL;
2633 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2635 *entry_type = GOT_OBJ_TYPE_ANY;
2636 *tree_relpath = NULL;
2637 *tree_id = NULL;
2639 if (wt_relpath[0] == '\0') {
2640 /* Check out all files within the work tree. */
2641 *entry_type = GOT_OBJ_TYPE_TREE;
2642 *tree_relpath = strdup("");
2643 if (*tree_relpath == NULL) {
2644 err = got_error_from_errno("strdup");
2645 goto done;
2647 err = got_object_id_by_path(tree_id, repo, base_commit,
2648 worktree->path_prefix);
2649 if (err)
2650 goto done;
2651 return NULL;
2654 /* Check out a subset of files in the work tree. */
2656 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2657 is_root_wt ? "" : "/", wt_relpath) == -1) {
2658 err = got_error_from_errno("asprintf");
2659 goto done;
2662 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2663 if (err)
2664 goto done;
2666 free(in_repo_path);
2667 in_repo_path = NULL;
2669 err = got_object_get_type(entry_type, repo, id);
2670 if (err)
2671 goto done;
2673 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2674 /* Check out a single file. */
2675 if (strchr(wt_relpath, '/') == NULL) {
2676 /* Check out a single file in work tree's root dir. */
2677 in_repo_path = strdup(worktree->path_prefix);
2678 if (in_repo_path == NULL) {
2679 err = got_error_from_errno("strdup");
2680 goto done;
2682 *tree_relpath = strdup("");
2683 if (*tree_relpath == NULL) {
2684 err = got_error_from_errno("strdup");
2685 goto done;
2687 } else {
2688 /* Check out a single file in a subdirectory. */
2689 err = got_path_dirname(tree_relpath, wt_relpath);
2690 if (err)
2691 return err;
2692 if (asprintf(&in_repo_path, "%s%s%s",
2693 worktree->path_prefix, is_root_wt ? "" : "/",
2694 *tree_relpath) == -1) {
2695 err = got_error_from_errno("asprintf");
2696 goto done;
2699 err = got_object_id_by_path(tree_id, repo,
2700 base_commit, in_repo_path);
2701 } else {
2702 /* Check out all files within a subdirectory. */
2703 *tree_id = got_object_id_dup(id);
2704 if (*tree_id == NULL) {
2705 err = got_error_from_errno("got_object_id_dup");
2706 goto done;
2708 *tree_relpath = strdup(wt_relpath);
2709 if (*tree_relpath == NULL) {
2710 err = got_error_from_errno("strdup");
2711 goto done;
2714 done:
2715 free(id);
2716 free(in_repo_path);
2717 if (err) {
2718 *entry_type = GOT_OBJ_TYPE_ANY;
2719 free(*tree_relpath);
2720 *tree_relpath = NULL;
2721 free(*tree_id);
2722 *tree_id = NULL;
2724 return err;
2727 static const struct got_error *
2728 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2729 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2730 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2731 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2733 const struct got_error *err = NULL;
2734 struct got_commit_object *commit = NULL;
2735 struct got_tree_object *tree = NULL;
2736 struct got_fileindex_diff_tree_cb diff_cb;
2737 struct diff_cb_arg arg;
2739 err = ref_base_commit(worktree, repo);
2740 if (err) {
2741 if (!(err->code == GOT_ERR_ERRNO &&
2742 (errno == EACCES || errno == EROFS)))
2743 goto done;
2744 err = (*progress_cb)(progress_arg,
2745 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2746 if (err)
2747 return err;
2750 err = got_object_open_as_commit(&commit, repo,
2751 worktree->base_commit_id);
2752 if (err)
2753 goto done;
2755 err = got_object_open_as_tree(&tree, repo, tree_id);
2756 if (err)
2757 goto done;
2759 if (entry_name &&
2760 got_object_tree_find_entry(tree, entry_name) == NULL) {
2761 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2762 goto done;
2765 diff_cb.diff_old_new = diff_old_new;
2766 diff_cb.diff_old = diff_old;
2767 diff_cb.diff_new = diff_new;
2768 arg.fileindex = fileindex;
2769 arg.worktree = worktree;
2770 arg.repo = repo;
2771 arg.progress_cb = progress_cb;
2772 arg.progress_arg = progress_arg;
2773 arg.cancel_cb = cancel_cb;
2774 arg.cancel_arg = cancel_arg;
2775 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2776 entry_name, repo, &diff_cb, &arg);
2777 done:
2778 if (tree)
2779 got_object_tree_close(tree);
2780 if (commit)
2781 got_object_commit_close(commit);
2782 return err;
2785 const struct got_error *
2786 got_worktree_checkout_files(struct got_worktree *worktree,
2787 struct got_pathlist_head *paths, struct got_repository *repo,
2788 got_worktree_checkout_cb progress_cb, void *progress_arg,
2789 got_cancel_cb cancel_cb, void *cancel_arg)
2791 const struct got_error *err = NULL, *sync_err, *unlockerr;
2792 struct got_commit_object *commit = NULL;
2793 struct got_tree_object *tree = NULL;
2794 struct got_fileindex *fileindex = NULL;
2795 char *fileindex_path = NULL;
2796 struct got_pathlist_entry *pe;
2797 struct tree_path_data {
2798 STAILQ_ENTRY(tree_path_data) entry;
2799 struct got_object_id *tree_id;
2800 int entry_type;
2801 char *relpath;
2802 char *entry_name;
2803 } *tpd = NULL;
2804 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2806 STAILQ_INIT(&tree_paths);
2808 err = lock_worktree(worktree, LOCK_EX);
2809 if (err)
2810 return err;
2812 err = got_object_open_as_commit(&commit, repo,
2813 worktree->base_commit_id);
2814 if (err)
2815 goto done;
2817 /* Map all specified paths to in-repository trees. */
2818 TAILQ_FOREACH(pe, paths, entry) {
2819 tpd = malloc(sizeof(*tpd));
2820 if (tpd == NULL) {
2821 err = got_error_from_errno("malloc");
2822 goto done;
2825 err = find_tree_entry_for_checkout(&tpd->entry_type,
2826 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2827 worktree, repo);
2828 if (err) {
2829 free(tpd);
2830 goto done;
2833 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2834 err = got_path_basename(&tpd->entry_name, pe->path);
2835 if (err) {
2836 free(tpd->relpath);
2837 free(tpd->tree_id);
2838 free(tpd);
2839 goto done;
2841 } else
2842 tpd->entry_name = NULL;
2844 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2848 * Read the file index.
2849 * Checking out files is supposed to be an idempotent operation.
2850 * If the on-disk file index is incomplete we will try to complete it.
2852 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2853 if (err)
2854 goto done;
2856 tpd = STAILQ_FIRST(&tree_paths);
2857 TAILQ_FOREACH(pe, paths, entry) {
2858 struct bump_base_commit_id_arg bbc_arg;
2860 err = checkout_files(worktree, fileindex, tpd->relpath,
2861 tpd->tree_id, tpd->entry_name, repo,
2862 progress_cb, progress_arg, cancel_cb, cancel_arg);
2863 if (err)
2864 break;
2866 bbc_arg.base_commit_id = worktree->base_commit_id;
2867 bbc_arg.entry_name = tpd->entry_name;
2868 bbc_arg.path = pe->path;
2869 bbc_arg.path_len = pe->path_len;
2870 bbc_arg.progress_cb = progress_cb;
2871 bbc_arg.progress_arg = progress_arg;
2872 err = got_fileindex_for_each_entry_safe(fileindex,
2873 bump_base_commit_id, &bbc_arg);
2874 if (err)
2875 break;
2877 tpd = STAILQ_NEXT(tpd, entry);
2879 sync_err = sync_fileindex(fileindex, fileindex_path);
2880 if (sync_err && err == NULL)
2881 err = sync_err;
2882 done:
2883 free(fileindex_path);
2884 if (tree)
2885 got_object_tree_close(tree);
2886 if (commit)
2887 got_object_commit_close(commit);
2888 if (fileindex)
2889 got_fileindex_free(fileindex);
2890 while (!STAILQ_EMPTY(&tree_paths)) {
2891 tpd = STAILQ_FIRST(&tree_paths);
2892 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2893 free(tpd->relpath);
2894 free(tpd->tree_id);
2895 free(tpd);
2897 unlockerr = lock_worktree(worktree, LOCK_SH);
2898 if (unlockerr && err == NULL)
2899 err = unlockerr;
2900 return err;
2903 static const struct got_error *
2904 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2905 struct got_fileindex_entry *ie, const char *ondisk_path,
2906 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2907 int restoring_missing_file, int reverting_versioned_file,
2908 int path_is_unversioned, int allow_bad_symlinks,
2909 struct got_repository *repo,
2910 got_worktree_checkout_cb progress_cb, void *progress_arg)
2912 const struct got_error *err = NULL;
2913 int is_bad_symlink = 0;
2915 if (S_ISLNK(mode2)) {
2916 err = install_symlink(&is_bad_symlink,
2917 worktree, ondisk_path, path2, blob2,
2918 restoring_missing_file,
2919 reverting_versioned_file,
2920 path_is_unversioned, allow_bad_symlinks,
2921 repo, progress_cb, progress_arg);
2922 } else {
2923 err = install_blob(worktree, ondisk_path, path2,
2924 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2925 restoring_missing_file, reverting_versioned_file, 0,
2926 path_is_unversioned, repo, progress_cb, progress_arg);
2928 if (err)
2929 return err;
2930 if (ie == NULL) {
2931 /* Adding an unversioned file. */
2932 err = got_fileindex_entry_alloc(&ie, path2);
2933 if (err)
2934 return err;
2935 err = got_fileindex_entry_update(ie,
2936 worktree->root_fd, path2, NULL, NULL, 1);
2937 if (err) {
2938 got_fileindex_entry_free(ie);
2939 return err;
2941 err = got_fileindex_entry_add(fileindex, ie);
2942 if (err) {
2943 got_fileindex_entry_free(ie);
2944 return err;
2946 } else {
2947 /* Re-adding a locally deleted file. */
2948 err = got_fileindex_entry_update(ie,
2949 worktree->root_fd, path2, ie->blob_sha1,
2950 worktree->base_commit_id->sha1, 0);
2951 if (err)
2952 return err;
2955 if (is_bad_symlink) {
2956 got_fileindex_entry_filetype_set(ie,
2957 GOT_FILEIDX_MODE_BAD_SYMLINK);
2960 return NULL;
2963 struct merge_file_cb_arg {
2964 struct got_worktree *worktree;
2965 struct got_fileindex *fileindex;
2966 got_worktree_checkout_cb progress_cb;
2967 void *progress_arg;
2968 got_cancel_cb cancel_cb;
2969 void *cancel_arg;
2970 const char *label_orig;
2971 struct got_object_id *commit_id2;
2972 int allow_bad_symlinks;
2975 static const struct got_error *
2976 merge_file_cb(void *arg, struct got_blob_object *blob1,
2977 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2978 struct got_object_id *id1, struct got_object_id *id2,
2979 const char *path1, const char *path2,
2980 mode_t mode1, mode_t mode2, struct got_repository *repo)
2982 static const struct got_error *err = NULL;
2983 struct merge_file_cb_arg *a = arg;
2984 struct got_fileindex_entry *ie;
2985 char *ondisk_path = NULL;
2986 struct stat sb;
2987 unsigned char status;
2988 int local_changes_subsumed;
2989 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2990 char *id_str = NULL, *label_deriv2 = NULL;
2992 if (blob1 && blob2) {
2993 ie = got_fileindex_entry_get(a->fileindex, path2,
2994 strlen(path2));
2995 if (ie == NULL)
2996 return (*a->progress_cb)(a->progress_arg,
2997 GOT_STATUS_MISSING, path2);
2999 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3000 path2) == -1)
3001 return got_error_from_errno("asprintf");
3003 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3004 repo);
3005 if (err)
3006 goto done;
3008 if (status == GOT_STATUS_DELETE) {
3009 err = (*a->progress_cb)(a->progress_arg,
3010 GOT_STATUS_MERGE, path2);
3011 goto done;
3013 if (status != GOT_STATUS_NO_CHANGE &&
3014 status != GOT_STATUS_MODIFY &&
3015 status != GOT_STATUS_CONFLICT &&
3016 status != GOT_STATUS_ADD) {
3017 err = (*a->progress_cb)(a->progress_arg, status, path2);
3018 goto done;
3021 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3022 char *link_target2;
3023 err = got_object_blob_read_to_str(&link_target2, blob2);
3024 if (err)
3025 goto done;
3026 err = merge_symlink(a->worktree, blob1, ondisk_path,
3027 path2, a->label_orig, link_target2, a->commit_id2,
3028 repo, a->progress_cb, a->progress_arg);
3029 free(link_target2);
3030 } else {
3031 int fd;
3033 f_orig = got_opentemp();
3034 if (f_orig == NULL) {
3035 err = got_error_from_errno("got_opentemp");
3036 goto done;
3038 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3039 f_orig, blob1);
3040 if (err)
3041 goto done;
3043 f_deriv2 = got_opentemp();
3044 if (f_deriv2 == NULL)
3045 goto done;
3046 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3047 f_deriv2, blob2);
3048 if (err)
3049 goto done;
3051 fd = open(ondisk_path,
3052 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3053 if (fd == -1) {
3054 err = got_error_from_errno2("open",
3055 ondisk_path);
3056 goto done;
3058 f_deriv = fdopen(fd, "r");
3059 if (f_deriv == NULL) {
3060 err = got_error_from_errno2("fdopen",
3061 ondisk_path);
3062 close(fd);
3063 goto done;
3065 err = got_object_id_str(&id_str, a->commit_id2);
3066 if (err)
3067 goto done;
3068 if (asprintf(&label_deriv2, "%s: commit %s",
3069 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3070 err = got_error_from_errno("asprintf");
3071 goto done;
3073 err = merge_file(&local_changes_subsumed, a->worktree,
3074 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3075 mode2, a->label_orig, NULL, label_deriv2,
3076 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3077 a->progress_cb, a->progress_arg);
3079 } else if (blob1) {
3080 ie = got_fileindex_entry_get(a->fileindex, path1,
3081 strlen(path1));
3082 if (ie == NULL)
3083 return (*a->progress_cb)(a->progress_arg,
3084 GOT_STATUS_MISSING, path1);
3086 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3087 path1) == -1)
3088 return got_error_from_errno("asprintf");
3090 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3091 repo);
3092 if (err)
3093 goto done;
3095 switch (status) {
3096 case GOT_STATUS_NO_CHANGE:
3097 err = (*a->progress_cb)(a->progress_arg,
3098 GOT_STATUS_DELETE, path1);
3099 if (err)
3100 goto done;
3101 err = remove_ondisk_file(a->worktree->root_path, path1);
3102 if (err)
3103 goto done;
3104 if (ie)
3105 got_fileindex_entry_mark_deleted_from_disk(ie);
3106 break;
3107 case GOT_STATUS_DELETE:
3108 case GOT_STATUS_MISSING:
3109 err = (*a->progress_cb)(a->progress_arg,
3110 GOT_STATUS_DELETE, path1);
3111 if (err)
3112 goto done;
3113 if (ie)
3114 got_fileindex_entry_mark_deleted_from_disk(ie);
3115 break;
3116 case GOT_STATUS_ADD: {
3117 struct got_object_id *id;
3118 FILE *blob1_f;
3119 off_t blob1_size;
3121 * Delete the added file only if its content already
3122 * exists in the repository.
3124 err = got_object_blob_file_create(&id, &blob1_f,
3125 &blob1_size, path1);
3126 if (err)
3127 goto done;
3128 if (got_object_id_cmp(id, id1) == 0) {
3129 err = (*a->progress_cb)(a->progress_arg,
3130 GOT_STATUS_DELETE, path1);
3131 if (err)
3132 goto done;
3133 err = remove_ondisk_file(a->worktree->root_path,
3134 path1);
3135 if (err)
3136 goto done;
3137 if (ie)
3138 got_fileindex_entry_remove(a->fileindex,
3139 ie);
3140 } else {
3141 err = (*a->progress_cb)(a->progress_arg,
3142 GOT_STATUS_CANNOT_DELETE, path1);
3144 if (fclose(blob1_f) == EOF && err == NULL)
3145 err = got_error_from_errno("fclose");
3146 free(id);
3147 if (err)
3148 goto done;
3149 break;
3151 case GOT_STATUS_MODIFY:
3152 case GOT_STATUS_CONFLICT:
3153 err = (*a->progress_cb)(a->progress_arg,
3154 GOT_STATUS_CANNOT_DELETE, path1);
3155 if (err)
3156 goto done;
3157 break;
3158 case GOT_STATUS_OBSTRUCTED:
3159 err = (*a->progress_cb)(a->progress_arg, status, path1);
3160 if (err)
3161 goto done;
3162 break;
3163 default:
3164 break;
3166 } else if (blob2) {
3167 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3168 path2) == -1)
3169 return got_error_from_errno("asprintf");
3170 ie = got_fileindex_entry_get(a->fileindex, path2,
3171 strlen(path2));
3172 if (ie) {
3173 err = get_file_status(&status, &sb, ie, ondisk_path,
3174 -1, NULL, repo);
3175 if (err)
3176 goto done;
3177 if (status != GOT_STATUS_NO_CHANGE &&
3178 status != GOT_STATUS_MODIFY &&
3179 status != GOT_STATUS_CONFLICT &&
3180 status != GOT_STATUS_ADD &&
3181 status != GOT_STATUS_DELETE) {
3182 err = (*a->progress_cb)(a->progress_arg,
3183 status, path2);
3184 goto done;
3186 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3187 char *link_target2;
3188 err = got_object_blob_read_to_str(&link_target2,
3189 blob2);
3190 if (err)
3191 goto done;
3192 err = merge_symlink(a->worktree, NULL,
3193 ondisk_path, path2, a->label_orig,
3194 link_target2, a->commit_id2, repo,
3195 a->progress_cb, a->progress_arg);
3196 free(link_target2);
3197 } else if (S_ISREG(sb.st_mode)) {
3198 err = merge_blob(&local_changes_subsumed,
3199 a->worktree, NULL, ondisk_path, path2,
3200 sb.st_mode, a->label_orig, blob2,
3201 a->commit_id2, repo, a->progress_cb,
3202 a->progress_arg);
3203 } else if (status != GOT_STATUS_DELETE) {
3204 err = got_error_path(ondisk_path,
3205 GOT_ERR_FILE_OBSTRUCTED);
3207 if (err)
3208 goto done;
3209 if (status == GOT_STATUS_DELETE) {
3210 /* Re-add file with content from new blob. */
3211 err = add_file(a->worktree, a->fileindex, ie,
3212 ondisk_path, path2, blob2, mode2,
3213 0, 0, 0, a->allow_bad_symlinks,
3214 repo, a->progress_cb, a->progress_arg);
3215 if (err)
3216 goto done;
3218 } else {
3219 err = add_file(a->worktree, a->fileindex, NULL,
3220 ondisk_path, path2, blob2, mode2,
3221 0, 0, 1, a->allow_bad_symlinks,
3222 repo, a->progress_cb, a->progress_arg);
3223 if (err)
3224 goto done;
3227 done:
3228 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3229 err = got_error_from_errno("fclose");
3230 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3231 err = got_error_from_errno("fclose");
3232 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3233 err = got_error_from_errno("fclose");
3234 free(id_str);
3235 free(label_deriv2);
3236 free(ondisk_path);
3237 return err;
3240 static const struct got_error *
3241 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3243 struct got_worktree *worktree = arg;
3245 /* Reject merges into a work tree with mixed base commits. */
3246 if (got_fileindex_entry_has_commit(ie) &&
3247 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3248 SHA1_DIGEST_LENGTH) != 0)
3249 return got_error(GOT_ERR_MIXED_COMMITS);
3251 return NULL;
3254 struct check_merge_conflicts_arg {
3255 struct got_worktree *worktree;
3256 struct got_fileindex *fileindex;
3257 struct got_repository *repo;
3260 static const struct got_error *
3261 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3262 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3263 struct got_object_id *id1, struct got_object_id *id2,
3264 const char *path1, const char *path2,
3265 mode_t mode1, mode_t mode2, struct got_repository *repo)
3267 const struct got_error *err = NULL;
3268 struct check_merge_conflicts_arg *a = arg;
3269 unsigned char status;
3270 struct stat sb;
3271 struct got_fileindex_entry *ie;
3272 const char *path = path2 ? path2 : path1;
3273 struct got_object_id *id = id2 ? id2 : id1;
3274 char *ondisk_path;
3276 if (id == NULL)
3277 return NULL;
3279 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3280 if (ie == NULL)
3281 return NULL;
3283 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3284 == -1)
3285 return got_error_from_errno("asprintf");
3287 /* Reject merges into a work tree with conflicted files. */
3288 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3289 free(ondisk_path);
3290 if (err)
3291 return err;
3292 if (status == GOT_STATUS_CONFLICT)
3293 return got_error(GOT_ERR_CONFLICTS);
3295 return NULL;
3298 static const struct got_error *
3299 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3300 const char *fileindex_path, struct got_object_id *commit_id1,
3301 struct got_object_id *commit_id2, struct got_repository *repo,
3302 got_worktree_checkout_cb progress_cb, void *progress_arg,
3303 got_cancel_cb cancel_cb, void *cancel_arg)
3305 const struct got_error *err = NULL, *sync_err;
3306 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3307 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3308 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3309 struct check_merge_conflicts_arg cmc_arg;
3310 struct merge_file_cb_arg arg;
3311 char *label_orig = NULL;
3312 FILE *f1 = NULL, *f2 = NULL;
3313 int fd1 = -1, fd2 = -1;
3315 if (commit_id1) {
3316 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3317 if (err)
3318 goto done;
3319 err = got_object_id_by_path(&tree_id1, repo, commit1,
3320 worktree->path_prefix);
3321 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3322 goto done;
3324 if (tree_id1) {
3325 char *id_str;
3327 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3328 if (err)
3329 goto done;
3331 err = got_object_id_str(&id_str, commit_id1);
3332 if (err)
3333 goto done;
3335 if (asprintf(&label_orig, "%s: commit %s",
3336 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3337 err = got_error_from_errno("asprintf");
3338 free(id_str);
3339 goto done;
3341 free(id_str);
3343 f1 = got_opentemp();
3344 if (f1 == NULL) {
3345 err = got_error_from_errno("got_opentemp");
3346 goto done;
3350 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3351 if (err)
3352 goto done;
3354 err = got_object_id_by_path(&tree_id2, repo, commit2,
3355 worktree->path_prefix);
3356 if (err)
3357 goto done;
3359 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3360 if (err)
3361 goto done;
3363 f2 = got_opentemp();
3364 if (f2 == NULL) {
3365 err = got_error_from_errno("got_opentemp");
3366 goto done;
3369 fd1 = got_opentempfd();
3370 if (fd1 == -1) {
3371 err = got_error_from_errno("got_opentempfd");
3372 goto done;
3375 fd2 = got_opentempfd();
3376 if (fd2 == -1) {
3377 err = got_error_from_errno("got_opentempfd");
3378 goto done;
3381 cmc_arg.worktree = worktree;
3382 cmc_arg.fileindex = fileindex;
3383 cmc_arg.repo = repo;
3384 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3385 check_merge_conflicts, &cmc_arg, 0);
3386 if (err)
3387 goto done;
3389 arg.worktree = worktree;
3390 arg.fileindex = fileindex;
3391 arg.progress_cb = progress_cb;
3392 arg.progress_arg = progress_arg;
3393 arg.cancel_cb = cancel_cb;
3394 arg.cancel_arg = cancel_arg;
3395 arg.label_orig = label_orig;
3396 arg.commit_id2 = commit_id2;
3397 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3398 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3399 merge_file_cb, &arg, 1);
3400 sync_err = sync_fileindex(fileindex, fileindex_path);
3401 if (sync_err && err == NULL)
3402 err = sync_err;
3403 done:
3404 if (commit1)
3405 got_object_commit_close(commit1);
3406 if (commit2)
3407 got_object_commit_close(commit2);
3408 if (tree1)
3409 got_object_tree_close(tree1);
3410 if (tree2)
3411 got_object_tree_close(tree2);
3412 if (f1 && fclose(f1) == EOF && err == NULL)
3413 err = got_error_from_errno("fclose");
3414 if (f2 && fclose(f2) == EOF && err == NULL)
3415 err = got_error_from_errno("fclose");
3416 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3417 err = got_error_from_errno("close");
3418 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3419 err = got_error_from_errno("close");
3420 free(label_orig);
3421 return err;
3424 const struct got_error *
3425 got_worktree_merge_files(struct got_worktree *worktree,
3426 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3427 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3428 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3430 const struct got_error *err, *unlockerr;
3431 char *fileindex_path = NULL;
3432 struct got_fileindex *fileindex = NULL;
3434 err = lock_worktree(worktree, LOCK_EX);
3435 if (err)
3436 return err;
3438 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3439 if (err)
3440 goto done;
3442 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3443 worktree);
3444 if (err)
3445 goto done;
3447 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3448 commit_id2, repo, progress_cb, progress_arg,
3449 cancel_cb, cancel_arg);
3450 done:
3451 if (fileindex)
3452 got_fileindex_free(fileindex);
3453 free(fileindex_path);
3454 unlockerr = lock_worktree(worktree, LOCK_SH);
3455 if (unlockerr && err == NULL)
3456 err = unlockerr;
3457 return err;
3460 struct diff_dir_cb_arg {
3461 struct got_fileindex *fileindex;
3462 struct got_worktree *worktree;
3463 const char *status_path;
3464 size_t status_path_len;
3465 struct got_repository *repo;
3466 got_worktree_status_cb status_cb;
3467 void *status_arg;
3468 got_cancel_cb cancel_cb;
3469 void *cancel_arg;
3470 /* A pathlist containing per-directory pathlists of ignore patterns. */
3471 struct got_pathlist_head *ignores;
3472 int report_unchanged;
3473 int no_ignores;
3476 static const struct got_error *
3477 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3478 int dirfd, const char *de_name,
3479 got_worktree_status_cb status_cb, void *status_arg,
3480 struct got_repository *repo, int report_unchanged)
3482 const struct got_error *err = NULL;
3483 unsigned char status = GOT_STATUS_NO_CHANGE;
3484 unsigned char staged_status;
3485 struct stat sb;
3486 struct got_object_id blob_id, commit_id, staged_blob_id;
3487 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3488 struct got_object_id *staged_blob_idp = NULL;
3490 staged_status = get_staged_status(ie);
3491 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3492 if (err)
3493 return err;
3495 if (status == GOT_STATUS_NO_CHANGE &&
3496 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3497 return NULL;
3499 if (got_fileindex_entry_has_blob(ie))
3500 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3501 if (got_fileindex_entry_has_commit(ie))
3502 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3503 if (staged_status == GOT_STATUS_ADD ||
3504 staged_status == GOT_STATUS_MODIFY) {
3505 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3506 &staged_blob_id, ie);
3509 return (*status_cb)(status_arg, status, staged_status,
3510 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3513 static const struct got_error *
3514 status_old_new(void *arg, struct got_fileindex_entry *ie,
3515 struct dirent *de, const char *parent_path, int dirfd)
3517 const struct got_error *err = NULL;
3518 struct diff_dir_cb_arg *a = arg;
3519 char *abspath;
3521 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3522 return got_error(GOT_ERR_CANCELLED);
3524 if (got_path_cmp(parent_path, a->status_path,
3525 strlen(parent_path), a->status_path_len) != 0 &&
3526 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3527 return NULL;
3529 if (parent_path[0]) {
3530 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3531 parent_path, de->d_name) == -1)
3532 return got_error_from_errno("asprintf");
3533 } else {
3534 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3535 de->d_name) == -1)
3536 return got_error_from_errno("asprintf");
3539 err = report_file_status(ie, abspath, dirfd, de->d_name,
3540 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3541 free(abspath);
3542 return err;
3545 static const struct got_error *
3546 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3548 struct diff_dir_cb_arg *a = arg;
3549 struct got_object_id blob_id, commit_id;
3550 unsigned char status;
3552 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3553 return got_error(GOT_ERR_CANCELLED);
3555 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3556 return NULL;
3558 got_fileindex_entry_get_blob_id(&blob_id, ie);
3559 got_fileindex_entry_get_commit_id(&commit_id, ie);
3560 if (got_fileindex_entry_has_file_on_disk(ie))
3561 status = GOT_STATUS_MISSING;
3562 else
3563 status = GOT_STATUS_DELETE;
3564 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3565 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3568 static void
3569 free_ignores(struct got_pathlist_head *ignores)
3571 struct got_pathlist_entry *pe;
3573 TAILQ_FOREACH(pe, ignores, entry) {
3574 struct got_pathlist_head *ignorelist = pe->data;
3576 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3578 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3581 static const struct got_error *
3582 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3584 const struct got_error *err = NULL;
3585 struct got_pathlist_entry *pe = NULL;
3586 struct got_pathlist_head *ignorelist;
3587 char *line = NULL, *pattern, *dirpath = NULL;
3588 size_t linesize = 0;
3589 ssize_t linelen;
3591 ignorelist = calloc(1, sizeof(*ignorelist));
3592 if (ignorelist == NULL)
3593 return got_error_from_errno("calloc");
3594 TAILQ_INIT(ignorelist);
3596 while ((linelen = getline(&line, &linesize, f)) != -1) {
3597 if (linelen > 0 && line[linelen - 1] == '\n')
3598 line[linelen - 1] = '\0';
3600 /* Git's ignores may contain comments. */
3601 if (line[0] == '#')
3602 continue;
3604 /* Git's negated patterns are not (yet?) supported. */
3605 if (line[0] == '!')
3606 continue;
3608 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3609 line) == -1) {
3610 err = got_error_from_errno("asprintf");
3611 goto done;
3613 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3614 if (err)
3615 goto done;
3617 if (ferror(f)) {
3618 err = got_error_from_errno("getline");
3619 goto done;
3622 dirpath = strdup(path);
3623 if (dirpath == NULL) {
3624 err = got_error_from_errno("strdup");
3625 goto done;
3627 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3628 done:
3629 free(line);
3630 if (err || pe == NULL) {
3631 free(dirpath);
3632 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3634 return err;
3637 static int
3638 match_path(const char *pattern, size_t pattern_len, const char *path,
3639 int flags)
3641 char buf[PATH_MAX];
3644 * Trailing slashes signify directories.
3645 * Append a * to make such patterns conform to fnmatch rules.
3647 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3648 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3649 return FNM_NOMATCH; /* XXX */
3651 return fnmatch(buf, path, flags);
3654 return fnmatch(pattern, path, flags);
3657 static int
3658 match_ignores(struct got_pathlist_head *ignores, const char *path)
3660 struct got_pathlist_entry *pe;
3662 /* Handle patterns which match in all directories. */
3663 TAILQ_FOREACH(pe, ignores, entry) {
3664 struct got_pathlist_head *ignorelist = pe->data;
3665 struct got_pathlist_entry *pi;
3667 TAILQ_FOREACH(pi, ignorelist, entry) {
3668 const char *p;
3670 if (pi->path_len < 3 ||
3671 strncmp(pi->path, "**/", 3) != 0)
3672 continue;
3673 p = path;
3674 while (*p) {
3675 if (match_path(pi->path + 3,
3676 pi->path_len - 3, p,
3677 FNM_PATHNAME | FNM_LEADING_DIR)) {
3678 /* Retry in next directory. */
3679 while (*p && *p != '/')
3680 p++;
3681 while (*p == '/')
3682 p++;
3683 continue;
3685 return 1;
3691 * The ignores pathlist contains ignore lists from children before
3692 * parents, so we can find the most specific ignorelist by walking
3693 * ignores backwards.
3695 pe = TAILQ_LAST(ignores, got_pathlist_head);
3696 while (pe) {
3697 if (got_path_is_child(path, pe->path, pe->path_len)) {
3698 struct got_pathlist_head *ignorelist = pe->data;
3699 struct got_pathlist_entry *pi;
3700 TAILQ_FOREACH(pi, ignorelist, entry) {
3701 int flags = FNM_LEADING_DIR;
3702 if (strstr(pi->path, "/**/") == NULL)
3703 flags |= FNM_PATHNAME;
3704 if (match_path(pi->path, pi->path_len,
3705 path, flags))
3706 continue;
3707 return 1;
3710 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3713 return 0;
3716 static const struct got_error *
3717 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3718 const char *path, int dirfd, const char *ignores_filename)
3720 const struct got_error *err = NULL;
3721 char *ignorespath;
3722 int fd = -1;
3723 FILE *ignoresfile = NULL;
3725 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3726 path[0] ? "/" : "", ignores_filename) == -1)
3727 return got_error_from_errno("asprintf");
3729 if (dirfd != -1) {
3730 fd = openat(dirfd, ignores_filename,
3731 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3732 if (fd == -1) {
3733 if (errno != ENOENT && errno != EACCES)
3734 err = got_error_from_errno2("openat",
3735 ignorespath);
3736 } else {
3737 ignoresfile = fdopen(fd, "r");
3738 if (ignoresfile == NULL)
3739 err = got_error_from_errno2("fdopen",
3740 ignorespath);
3741 else {
3742 fd = -1;
3743 err = read_ignores(ignores, path, ignoresfile);
3746 } else {
3747 ignoresfile = fopen(ignorespath, "re");
3748 if (ignoresfile == NULL) {
3749 if (errno != ENOENT && errno != EACCES)
3750 err = got_error_from_errno2("fopen",
3751 ignorespath);
3752 } else
3753 err = read_ignores(ignores, path, ignoresfile);
3756 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3757 err = got_error_from_errno2("fclose", path);
3758 if (fd != -1 && close(fd) == -1 && err == NULL)
3759 err = got_error_from_errno2("close", path);
3760 free(ignorespath);
3761 return err;
3764 static const struct got_error *
3765 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3766 int dirfd)
3768 const struct got_error *err = NULL;
3769 struct diff_dir_cb_arg *a = arg;
3770 char *path = NULL;
3772 if (ignore != NULL)
3773 *ignore = 0;
3775 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3776 return got_error(GOT_ERR_CANCELLED);
3778 if (parent_path[0]) {
3779 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3780 return got_error_from_errno("asprintf");
3781 } else {
3782 path = de->d_name;
3785 if (de->d_type == DT_DIR) {
3786 if (!a->no_ignores && ignore != NULL &&
3787 match_ignores(a->ignores, path))
3788 *ignore = 1;
3789 } else if (!match_ignores(a->ignores, path) &&
3790 got_path_is_child(path, a->status_path, a->status_path_len))
3791 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3792 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3793 if (parent_path[0])
3794 free(path);
3795 return err;
3798 static const struct got_error *
3799 status_traverse(void *arg, const char *path, int dirfd)
3801 const struct got_error *err = NULL;
3802 struct diff_dir_cb_arg *a = arg;
3804 if (a->no_ignores)
3805 return NULL;
3807 err = add_ignores(a->ignores, a->worktree->root_path,
3808 path, dirfd, ".cvsignore");
3809 if (err)
3810 return err;
3812 err = add_ignores(a->ignores, a->worktree->root_path, path,
3813 dirfd, ".gitignore");
3815 return err;
3818 static const struct got_error *
3819 report_single_file_status(const char *path, const char *ondisk_path,
3820 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3821 void *status_arg, struct got_repository *repo, int report_unchanged,
3822 struct got_pathlist_head *ignores, int no_ignores)
3824 struct got_fileindex_entry *ie;
3825 struct stat sb;
3827 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3828 if (ie)
3829 return report_file_status(ie, ondisk_path, -1, NULL,
3830 status_cb, status_arg, repo, report_unchanged);
3832 if (lstat(ondisk_path, &sb) == -1) {
3833 if (errno != ENOENT)
3834 return got_error_from_errno2("lstat", ondisk_path);
3835 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3836 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3839 if (!no_ignores && match_ignores(ignores, path))
3840 return NULL;
3842 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3843 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3844 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3846 return NULL;
3849 static const struct got_error *
3850 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3851 const char *root_path, const char *path)
3853 const struct got_error *err;
3854 char *parent_path, *next_parent_path = NULL;
3856 err = add_ignores(ignores, root_path, "", -1,
3857 ".cvsignore");
3858 if (err)
3859 return err;
3861 err = add_ignores(ignores, root_path, "", -1,
3862 ".gitignore");
3863 if (err)
3864 return err;
3866 err = got_path_dirname(&parent_path, path);
3867 if (err) {
3868 if (err->code == GOT_ERR_BAD_PATH)
3869 return NULL; /* cannot traverse parent */
3870 return err;
3872 for (;;) {
3873 err = add_ignores(ignores, root_path, parent_path, -1,
3874 ".cvsignore");
3875 if (err)
3876 break;
3877 err = add_ignores(ignores, root_path, parent_path, -1,
3878 ".gitignore");
3879 if (err)
3880 break;
3881 err = got_path_dirname(&next_parent_path, parent_path);
3882 if (err) {
3883 if (err->code == GOT_ERR_BAD_PATH)
3884 err = NULL; /* traversed everything */
3885 break;
3887 if (got_path_is_root_dir(parent_path))
3888 break;
3889 free(parent_path);
3890 parent_path = next_parent_path;
3891 next_parent_path = NULL;
3894 free(parent_path);
3895 free(next_parent_path);
3896 return err;
3899 struct find_missing_children_args {
3900 const char *parent_path;
3901 size_t parent_len;
3902 struct got_pathlist_head *children;
3903 got_cancel_cb cancel_cb;
3904 void *cancel_arg;
3907 static const struct got_error *
3908 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3910 const struct got_error *err = NULL;
3911 struct find_missing_children_args *a = arg;
3913 if (a->cancel_cb) {
3914 err = a->cancel_cb(a->cancel_arg);
3915 if (err)
3916 return err;
3919 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3920 err = got_pathlist_append(a->children, ie->path, NULL);
3922 return err;
3925 static const struct got_error *
3926 report_children(struct got_pathlist_head *children,
3927 struct got_worktree *worktree, struct got_fileindex *fileindex,
3928 struct got_repository *repo, int is_root_dir, int report_unchanged,
3929 struct got_pathlist_head *ignores, int no_ignores,
3930 got_worktree_status_cb status_cb, void *status_arg,
3931 got_cancel_cb cancel_cb, void *cancel_arg)
3933 const struct got_error *err = NULL;
3934 struct got_pathlist_entry *pe;
3935 char *ondisk_path = NULL;
3937 TAILQ_FOREACH(pe, children, entry) {
3938 if (cancel_cb) {
3939 err = cancel_cb(cancel_arg);
3940 if (err)
3941 break;
3944 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3945 !is_root_dir ? "/" : "", pe->path) == -1) {
3946 err = got_error_from_errno("asprintf");
3947 ondisk_path = NULL;
3948 break;
3951 err = report_single_file_status(pe->path, ondisk_path,
3952 fileindex, status_cb, status_arg, repo, report_unchanged,
3953 ignores, no_ignores);
3954 if (err)
3955 break;
3957 free(ondisk_path);
3958 ondisk_path = NULL;
3961 free(ondisk_path);
3962 return err;
3965 static const struct got_error *
3966 worktree_status(struct got_worktree *worktree, const char *path,
3967 struct got_fileindex *fileindex, struct got_repository *repo,
3968 got_worktree_status_cb status_cb, void *status_arg,
3969 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3970 int report_unchanged)
3972 const struct got_error *err = NULL;
3973 int fd = -1;
3974 struct got_fileindex_diff_dir_cb fdiff_cb;
3975 struct diff_dir_cb_arg arg;
3976 char *ondisk_path = NULL;
3977 struct got_pathlist_head ignores, missing_children;
3978 struct got_fileindex_entry *ie;
3980 TAILQ_INIT(&ignores);
3981 TAILQ_INIT(&missing_children);
3983 if (asprintf(&ondisk_path, "%s%s%s",
3984 worktree->root_path, path[0] ? "/" : "", path) == -1)
3985 return got_error_from_errno("asprintf");
3987 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3988 if (ie) {
3989 err = report_single_file_status(path, ondisk_path,
3990 fileindex, status_cb, status_arg, repo,
3991 report_unchanged, &ignores, no_ignores);
3992 goto done;
3993 } else {
3994 struct find_missing_children_args fmca;
3995 fmca.parent_path = path;
3996 fmca.parent_len = strlen(path);
3997 fmca.children = &missing_children;
3998 fmca.cancel_cb = cancel_cb;
3999 fmca.cancel_arg = cancel_arg;
4000 err = got_fileindex_for_each_entry_safe(fileindex,
4001 find_missing_children, &fmca);
4002 if (err)
4003 goto done;
4006 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4007 if (fd == -1) {
4008 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4009 !got_err_open_nofollow_on_symlink())
4010 err = got_error_from_errno2("open", ondisk_path);
4011 else {
4012 if (!no_ignores) {
4013 err = add_ignores_from_parent_paths(&ignores,
4014 worktree->root_path, ondisk_path);
4015 if (err)
4016 goto done;
4018 if (TAILQ_EMPTY(&missing_children)) {
4019 err = report_single_file_status(path,
4020 ondisk_path, fileindex,
4021 status_cb, status_arg, repo,
4022 report_unchanged, &ignores, no_ignores);
4023 if (err)
4024 goto done;
4025 } else {
4026 err = report_children(&missing_children,
4027 worktree, fileindex, repo,
4028 (path[0] == '\0'), report_unchanged,
4029 &ignores, no_ignores,
4030 status_cb, status_arg,
4031 cancel_cb, cancel_arg);
4032 if (err)
4033 goto done;
4036 } else {
4037 fdiff_cb.diff_old_new = status_old_new;
4038 fdiff_cb.diff_old = status_old;
4039 fdiff_cb.diff_new = status_new;
4040 fdiff_cb.diff_traverse = status_traverse;
4041 arg.fileindex = fileindex;
4042 arg.worktree = worktree;
4043 arg.status_path = path;
4044 arg.status_path_len = strlen(path);
4045 arg.repo = repo;
4046 arg.status_cb = status_cb;
4047 arg.status_arg = status_arg;
4048 arg.cancel_cb = cancel_cb;
4049 arg.cancel_arg = cancel_arg;
4050 arg.report_unchanged = report_unchanged;
4051 arg.no_ignores = no_ignores;
4052 if (!no_ignores) {
4053 err = add_ignores_from_parent_paths(&ignores,
4054 worktree->root_path, path);
4055 if (err)
4056 goto done;
4058 arg.ignores = &ignores;
4059 err = got_fileindex_diff_dir(fileindex, fd,
4060 worktree->root_path, path, repo, &fdiff_cb, &arg);
4062 done:
4063 free_ignores(&ignores);
4064 if (fd != -1 && close(fd) == -1 && err == NULL)
4065 err = got_error_from_errno("close");
4066 free(ondisk_path);
4067 return err;
4070 const struct got_error *
4071 got_worktree_status(struct got_worktree *worktree,
4072 struct got_pathlist_head *paths, struct got_repository *repo,
4073 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4074 got_cancel_cb cancel_cb, void *cancel_arg)
4076 const struct got_error *err = NULL;
4077 char *fileindex_path = NULL;
4078 struct got_fileindex *fileindex = NULL;
4079 struct got_pathlist_entry *pe;
4081 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4082 if (err)
4083 return err;
4085 TAILQ_FOREACH(pe, paths, entry) {
4086 err = worktree_status(worktree, pe->path, fileindex, repo,
4087 status_cb, status_arg, cancel_cb, cancel_arg,
4088 no_ignores, 0);
4089 if (err)
4090 break;
4092 free(fileindex_path);
4093 got_fileindex_free(fileindex);
4094 return err;
4097 const struct got_error *
4098 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4099 const char *arg)
4101 const struct got_error *err = NULL;
4102 char *resolved = NULL, *cwd = NULL, *path = NULL;
4103 size_t len;
4104 struct stat sb;
4105 char *abspath = NULL;
4106 char canonpath[PATH_MAX];
4108 *wt_path = NULL;
4110 cwd = getcwd(NULL, 0);
4111 if (cwd == NULL)
4112 return got_error_from_errno("getcwd");
4114 if (lstat(arg, &sb) == -1) {
4115 if (errno != ENOENT) {
4116 err = got_error_from_errno2("lstat", arg);
4117 goto done;
4119 sb.st_mode = 0;
4121 if (S_ISLNK(sb.st_mode)) {
4123 * We cannot use realpath(3) with symlinks since we want to
4124 * operate on the symlink itself.
4125 * But we can make the path absolute, assuming it is relative
4126 * to the current working directory, and then canonicalize it.
4128 if (!got_path_is_absolute(arg)) {
4129 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4130 err = got_error_from_errno("asprintf");
4131 goto done;
4135 err = got_canonpath(abspath ? abspath : arg, canonpath,
4136 sizeof(canonpath));
4137 if (err)
4138 goto done;
4139 resolved = strdup(canonpath);
4140 if (resolved == NULL) {
4141 err = got_error_from_errno("strdup");
4142 goto done;
4144 } else {
4145 resolved = realpath(arg, NULL);
4146 if (resolved == NULL) {
4147 if (errno != ENOENT) {
4148 err = got_error_from_errno2("realpath", arg);
4149 goto done;
4151 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4152 err = got_error_from_errno("asprintf");
4153 goto done;
4155 err = got_canonpath(abspath, canonpath,
4156 sizeof(canonpath));
4157 if (err)
4158 goto done;
4159 resolved = strdup(canonpath);
4160 if (resolved == NULL) {
4161 err = got_error_from_errno("strdup");
4162 goto done;
4167 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4168 strlen(got_worktree_get_root_path(worktree)))) {
4169 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4170 goto done;
4173 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4174 err = got_path_skip_common_ancestor(&path,
4175 got_worktree_get_root_path(worktree), resolved);
4176 if (err)
4177 goto done;
4178 } else {
4179 path = strdup("");
4180 if (path == NULL) {
4181 err = got_error_from_errno("strdup");
4182 goto done;
4186 /* XXX status walk can't deal with trailing slash! */
4187 len = strlen(path);
4188 while (len > 0 && path[len - 1] == '/') {
4189 path[len - 1] = '\0';
4190 len--;
4192 done:
4193 free(abspath);
4194 free(resolved);
4195 free(cwd);
4196 if (err == NULL)
4197 *wt_path = path;
4198 else
4199 free(path);
4200 return err;
4203 struct schedule_addition_args {
4204 struct got_worktree *worktree;
4205 struct got_fileindex *fileindex;
4206 got_worktree_checkout_cb progress_cb;
4207 void *progress_arg;
4208 struct got_repository *repo;
4211 static int
4212 add_noop_status(unsigned char status)
4214 return (status == GOT_STATUS_ADD ||
4215 status == GOT_STATUS_MODIFY ||
4216 status == GOT_STATUS_CONFLICT ||
4217 status == GOT_STATUS_MODE_CHANGE ||
4218 status == GOT_STATUS_NO_CHANGE);
4221 static const struct got_error *
4222 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4223 const char *relpath, struct got_object_id *blob_id,
4224 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4225 int dirfd, const char *de_name)
4227 struct schedule_addition_args *a = arg;
4228 const struct got_error *err = NULL;
4229 struct got_fileindex_entry *ie;
4230 struct stat sb;
4231 char *ondisk_path;
4233 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4234 relpath) == -1)
4235 return got_error_from_errno("asprintf");
4237 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4238 if (ie) {
4239 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4240 de_name, a->repo);
4241 if (err)
4242 goto done;
4243 /* Re-adding an existing entry is a no-op. */
4244 if (staged_status == GOT_STATUS_NO_CHANGE &&
4245 add_noop_status(status))
4246 goto done;
4247 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4248 if (err)
4249 goto done;
4252 if (status != GOT_STATUS_UNVERSIONED) {
4253 if (status == GOT_STATUS_NONEXISTENT)
4254 err = got_error_set_errno(ENOENT, ondisk_path);
4255 else
4256 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4257 goto done;
4260 err = got_fileindex_entry_alloc(&ie, relpath);
4261 if (err)
4262 goto done;
4263 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4264 relpath, NULL, NULL, 1);
4265 if (err) {
4266 got_fileindex_entry_free(ie);
4267 goto done;
4269 err = got_fileindex_entry_add(a->fileindex, ie);
4270 if (err) {
4271 got_fileindex_entry_free(ie);
4272 goto done;
4274 done:
4275 free(ondisk_path);
4276 if (err)
4277 return err;
4278 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4279 return NULL;
4280 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4283 const struct got_error *
4284 got_worktree_schedule_add(struct got_worktree *worktree,
4285 struct got_pathlist_head *paths,
4286 got_worktree_checkout_cb progress_cb, void *progress_arg,
4287 struct got_repository *repo, int no_ignores)
4289 struct got_fileindex *fileindex = NULL;
4290 char *fileindex_path = NULL;
4291 const struct got_error *err = NULL, *sync_err, *unlockerr;
4292 struct got_pathlist_entry *pe;
4293 struct schedule_addition_args saa;
4295 err = lock_worktree(worktree, LOCK_EX);
4296 if (err)
4297 return err;
4299 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4300 if (err)
4301 goto done;
4303 saa.worktree = worktree;
4304 saa.fileindex = fileindex;
4305 saa.progress_cb = progress_cb;
4306 saa.progress_arg = progress_arg;
4307 saa.repo = repo;
4309 TAILQ_FOREACH(pe, paths, entry) {
4310 err = worktree_status(worktree, pe->path, fileindex, repo,
4311 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4312 if (err)
4313 break;
4315 sync_err = sync_fileindex(fileindex, fileindex_path);
4316 if (sync_err && err == NULL)
4317 err = sync_err;
4318 done:
4319 free(fileindex_path);
4320 if (fileindex)
4321 got_fileindex_free(fileindex);
4322 unlockerr = lock_worktree(worktree, LOCK_SH);
4323 if (unlockerr && err == NULL)
4324 err = unlockerr;
4325 return err;
4328 struct schedule_deletion_args {
4329 struct got_worktree *worktree;
4330 struct got_fileindex *fileindex;
4331 got_worktree_delete_cb progress_cb;
4332 void *progress_arg;
4333 struct got_repository *repo;
4334 int delete_local_mods;
4335 int keep_on_disk;
4336 int ignore_missing_paths;
4337 const char *status_path;
4338 size_t status_path_len;
4339 const char *status_codes;
4342 static const struct got_error *
4343 schedule_for_deletion(void *arg, unsigned char status,
4344 unsigned char staged_status, const char *relpath,
4345 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4346 struct got_object_id *commit_id, int dirfd, const char *de_name)
4348 struct schedule_deletion_args *a = arg;
4349 const struct got_error *err = NULL;
4350 struct got_fileindex_entry *ie = NULL;
4351 struct stat sb;
4352 char *ondisk_path;
4354 if (status == GOT_STATUS_NONEXISTENT) {
4355 if (a->ignore_missing_paths)
4356 return NULL;
4357 return got_error_set_errno(ENOENT, relpath);
4360 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4361 if (ie == NULL)
4362 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4364 staged_status = get_staged_status(ie);
4365 if (staged_status != GOT_STATUS_NO_CHANGE) {
4366 if (staged_status == GOT_STATUS_DELETE)
4367 return NULL;
4368 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4371 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4372 relpath) == -1)
4373 return got_error_from_errno("asprintf");
4375 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4376 a->repo);
4377 if (err)
4378 goto done;
4380 if (a->status_codes) {
4381 size_t ncodes = strlen(a->status_codes);
4382 int i;
4383 for (i = 0; i < ncodes ; i++) {
4384 if (status == a->status_codes[i])
4385 break;
4387 if (i == ncodes) {
4388 /* Do not delete files in non-matching status. */
4389 free(ondisk_path);
4390 return NULL;
4392 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4393 a->status_codes[i] != GOT_STATUS_MISSING) {
4394 static char msg[64];
4395 snprintf(msg, sizeof(msg),
4396 "invalid status code '%c'", a->status_codes[i]);
4397 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4398 goto done;
4402 if (status != GOT_STATUS_NO_CHANGE) {
4403 if (status == GOT_STATUS_DELETE)
4404 goto done;
4405 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4406 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4407 goto done;
4409 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4410 err = got_error_set_errno(ENOENT, relpath);
4411 goto done;
4413 if (status != GOT_STATUS_MODIFY &&
4414 status != GOT_STATUS_MISSING) {
4415 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4416 goto done;
4420 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4421 size_t root_len;
4423 if (dirfd != -1) {
4424 if (unlinkat(dirfd, de_name, 0) == -1) {
4425 err = got_error_from_errno2("unlinkat",
4426 ondisk_path);
4427 goto done;
4429 } else if (unlink(ondisk_path) == -1) {
4430 err = got_error_from_errno2("unlink", ondisk_path);
4431 goto done;
4434 root_len = strlen(a->worktree->root_path);
4435 do {
4436 char *parent;
4438 err = got_path_dirname(&parent, ondisk_path);
4439 if (err)
4440 goto done;
4441 free(ondisk_path);
4442 ondisk_path = parent;
4443 if (got_path_cmp(ondisk_path, a->status_path,
4444 strlen(ondisk_path), a->status_path_len) != 0 &&
4445 !got_path_is_child(ondisk_path, a->status_path,
4446 a->status_path_len))
4447 break;
4448 if (rmdir(ondisk_path) == -1) {
4449 if (errno != ENOTEMPTY)
4450 err = got_error_from_errno2("rmdir",
4451 ondisk_path);
4452 break;
4454 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4455 strlen(ondisk_path), root_len) != 0);
4458 got_fileindex_entry_mark_deleted_from_disk(ie);
4459 done:
4460 free(ondisk_path);
4461 if (err)
4462 return err;
4463 if (status == GOT_STATUS_DELETE)
4464 return NULL;
4465 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4466 staged_status, relpath);
4469 const struct got_error *
4470 got_worktree_schedule_delete(struct got_worktree *worktree,
4471 struct got_pathlist_head *paths, int delete_local_mods,
4472 const char *status_codes,
4473 got_worktree_delete_cb progress_cb, void *progress_arg,
4474 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4476 struct got_fileindex *fileindex = NULL;
4477 char *fileindex_path = NULL;
4478 const struct got_error *err = NULL, *sync_err, *unlockerr;
4479 struct got_pathlist_entry *pe;
4480 struct schedule_deletion_args sda;
4482 err = lock_worktree(worktree, LOCK_EX);
4483 if (err)
4484 return err;
4486 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4487 if (err)
4488 goto done;
4490 sda.worktree = worktree;
4491 sda.fileindex = fileindex;
4492 sda.progress_cb = progress_cb;
4493 sda.progress_arg = progress_arg;
4494 sda.repo = repo;
4495 sda.delete_local_mods = delete_local_mods;
4496 sda.keep_on_disk = keep_on_disk;
4497 sda.ignore_missing_paths = ignore_missing_paths;
4498 sda.status_codes = status_codes;
4500 TAILQ_FOREACH(pe, paths, entry) {
4501 char *ondisk_status_path;
4503 if (asprintf(&ondisk_status_path, "%s%s%s",
4504 got_worktree_get_root_path(worktree),
4505 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4506 err = got_error_from_errno("asprintf");
4507 goto done;
4509 sda.status_path = ondisk_status_path;
4510 sda.status_path_len = strlen(ondisk_status_path);
4511 err = worktree_status(worktree, pe->path, fileindex, repo,
4512 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4513 free(ondisk_status_path);
4514 if (err)
4515 break;
4517 sync_err = sync_fileindex(fileindex, fileindex_path);
4518 if (sync_err && err == NULL)
4519 err = sync_err;
4520 done:
4521 free(fileindex_path);
4522 if (fileindex)
4523 got_fileindex_free(fileindex);
4524 unlockerr = lock_worktree(worktree, LOCK_SH);
4525 if (unlockerr && err == NULL)
4526 err = unlockerr;
4527 return err;
4530 static const struct got_error *
4531 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4533 const struct got_error *err = NULL;
4534 char *line = NULL;
4535 size_t linesize = 0, n;
4536 ssize_t linelen;
4538 linelen = getline(&line, &linesize, infile);
4539 if (linelen == -1) {
4540 if (ferror(infile)) {
4541 err = got_error_from_errno("getline");
4542 goto done;
4544 return NULL;
4546 if (outfile) {
4547 n = fwrite(line, 1, linelen, outfile);
4548 if (n != linelen) {
4549 err = got_ferror(outfile, GOT_ERR_IO);
4550 goto done;
4553 if (rejectfile) {
4554 n = fwrite(line, 1, linelen, rejectfile);
4555 if (n != linelen)
4556 err = got_ferror(rejectfile, GOT_ERR_IO);
4558 done:
4559 free(line);
4560 return err;
4563 static const struct got_error *
4564 skip_one_line(FILE *f)
4566 char *line = NULL;
4567 size_t linesize = 0;
4568 ssize_t linelen;
4570 linelen = getline(&line, &linesize, f);
4571 if (linelen == -1) {
4572 if (ferror(f))
4573 return got_error_from_errno("getline");
4574 return NULL;
4576 free(line);
4577 return NULL;
4580 static const struct got_error *
4581 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4582 int start_old, int end_old, int start_new, int end_new,
4583 FILE *outfile, FILE *rejectfile)
4585 const struct got_error *err;
4587 /* Copy old file's lines leading up to patch. */
4588 while (!feof(f1) && *line_cur1 < start_old) {
4589 err = copy_one_line(f1, outfile, NULL);
4590 if (err)
4591 return err;
4592 (*line_cur1)++;
4594 /* Skip new file's lines leading up to patch. */
4595 while (!feof(f2) && *line_cur2 < start_new) {
4596 if (rejectfile)
4597 err = copy_one_line(f2, NULL, rejectfile);
4598 else
4599 err = skip_one_line(f2);
4600 if (err)
4601 return err;
4602 (*line_cur2)++;
4604 /* Copy patched lines. */
4605 while (!feof(f2) && *line_cur2 <= end_new) {
4606 err = copy_one_line(f2, outfile, NULL);
4607 if (err)
4608 return err;
4609 (*line_cur2)++;
4611 /* Skip over old file's replaced lines. */
4612 while (!feof(f1) && *line_cur1 <= end_old) {
4613 if (rejectfile)
4614 err = copy_one_line(f1, NULL, rejectfile);
4615 else
4616 err = skip_one_line(f1);
4617 if (err)
4618 return err;
4619 (*line_cur1)++;
4622 return NULL;
4625 static const struct got_error *
4626 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4627 FILE *outfile, FILE *rejectfile)
4629 const struct got_error *err;
4631 if (outfile) {
4632 /* Copy old file's lines until EOF. */
4633 while (!feof(f1)) {
4634 err = copy_one_line(f1, outfile, NULL);
4635 if (err)
4636 return err;
4637 (*line_cur1)++;
4640 if (rejectfile) {
4641 /* Copy new file's lines until EOF. */
4642 while (!feof(f2)) {
4643 err = copy_one_line(f2, NULL, rejectfile);
4644 if (err)
4645 return err;
4646 (*line_cur2)++;
4650 return NULL;
4653 static const struct got_error *
4654 apply_or_reject_change(int *choice, int *nchunks_used,
4655 struct diff_result *diff_result, int n,
4656 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4657 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4658 got_worktree_patch_cb patch_cb, void *patch_arg)
4660 const struct got_error *err = NULL;
4661 struct diff_chunk_context cc = {};
4662 int start_old, end_old, start_new, end_new;
4663 FILE *hunkfile;
4664 struct diff_output_unidiff_state *diff_state;
4665 struct diff_input_info diff_info;
4666 int rc;
4668 *choice = GOT_PATCH_CHOICE_NONE;
4670 /* Get changed line numbers without context lines for copy_change(). */
4671 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4672 start_old = cc.left.start;
4673 end_old = cc.left.end;
4674 start_new = cc.right.start;
4675 end_new = cc.right.end;
4677 /* Get the same change with context lines for display. */
4678 memset(&cc, 0, sizeof(cc));
4679 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4681 memset(&diff_info, 0, sizeof(diff_info));
4682 diff_info.left_path = relpath;
4683 diff_info.right_path = relpath;
4685 diff_state = diff_output_unidiff_state_alloc();
4686 if (diff_state == NULL)
4687 return got_error_set_errno(ENOMEM,
4688 "diff_output_unidiff_state_alloc");
4690 hunkfile = got_opentemp();
4691 if (hunkfile == NULL) {
4692 err = got_error_from_errno("got_opentemp");
4693 goto done;
4696 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4697 diff_result, &cc);
4698 if (rc != DIFF_RC_OK) {
4699 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4700 goto done;
4703 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4704 err = got_ferror(hunkfile, GOT_ERR_IO);
4705 goto done;
4708 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4709 hunkfile, changeno, nchanges);
4710 if (err)
4711 goto done;
4713 switch (*choice) {
4714 case GOT_PATCH_CHOICE_YES:
4715 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4716 end_old, start_new, end_new, outfile, rejectfile);
4717 break;
4718 case GOT_PATCH_CHOICE_NO:
4719 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4720 end_old, start_new, end_new, rejectfile, outfile);
4721 break;
4722 case GOT_PATCH_CHOICE_QUIT:
4723 break;
4724 default:
4725 err = got_error(GOT_ERR_PATCH_CHOICE);
4726 break;
4728 done:
4729 diff_output_unidiff_state_free(diff_state);
4730 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4731 err = got_error_from_errno("fclose");
4732 return err;
4735 struct revert_file_args {
4736 struct got_worktree *worktree;
4737 struct got_fileindex *fileindex;
4738 got_worktree_checkout_cb progress_cb;
4739 void *progress_arg;
4740 got_worktree_patch_cb patch_cb;
4741 void *patch_arg;
4742 struct got_repository *repo;
4743 int unlink_added_files;
4744 struct got_pathlist_head *added_files_to_unlink;
4747 static const struct got_error *
4748 create_patched_content(char **path_outfile, int reverse_patch,
4749 struct got_object_id *blob_id, const char *path2,
4750 int dirfd2, const char *de_name2,
4751 const char *relpath, struct got_repository *repo,
4752 got_worktree_patch_cb patch_cb, void *patch_arg)
4754 const struct got_error *err, *free_err;
4755 struct got_blob_object *blob = NULL;
4756 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4757 int fd = -1, fd2 = -1;
4758 char link_target[PATH_MAX];
4759 ssize_t link_len = 0;
4760 char *path1 = NULL, *id_str = NULL;
4761 struct stat sb2;
4762 struct got_diffreg_result *diffreg_result = NULL;
4763 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4764 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4766 *path_outfile = NULL;
4768 err = got_object_id_str(&id_str, blob_id);
4769 if (err)
4770 return err;
4772 if (dirfd2 != -1) {
4773 fd2 = openat(dirfd2, de_name2,
4774 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4775 if (fd2 == -1) {
4776 if (!got_err_open_nofollow_on_symlink()) {
4777 err = got_error_from_errno2("openat", path2);
4778 goto done;
4780 link_len = readlinkat(dirfd2, de_name2,
4781 link_target, sizeof(link_target));
4782 if (link_len == -1) {
4783 return got_error_from_errno2("readlinkat",
4784 path2);
4786 sb2.st_mode = S_IFLNK;
4787 sb2.st_size = link_len;
4789 } else {
4790 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4791 if (fd2 == -1) {
4792 if (!got_err_open_nofollow_on_symlink()) {
4793 err = got_error_from_errno2("open", path2);
4794 goto done;
4796 link_len = readlink(path2, link_target,
4797 sizeof(link_target));
4798 if (link_len == -1)
4799 return got_error_from_errno2("readlink", path2);
4800 sb2.st_mode = S_IFLNK;
4801 sb2.st_size = link_len;
4804 if (fd2 != -1) {
4805 if (fstat(fd2, &sb2) == -1) {
4806 err = got_error_from_errno2("fstat", path2);
4807 goto done;
4810 f2 = fdopen(fd2, "r");
4811 if (f2 == NULL) {
4812 err = got_error_from_errno2("fdopen", path2);
4813 goto done;
4815 fd2 = -1;
4816 } else {
4817 size_t n;
4818 f2 = got_opentemp();
4819 if (f2 == NULL) {
4820 err = got_error_from_errno2("got_opentemp", path2);
4821 goto done;
4823 n = fwrite(link_target, 1, link_len, f2);
4824 if (n != link_len) {
4825 err = got_ferror(f2, GOT_ERR_IO);
4826 goto done;
4828 if (fflush(f2) == EOF) {
4829 err = got_error_from_errno("fflush");
4830 goto done;
4832 rewind(f2);
4835 fd = got_opentempfd();
4836 if (fd == -1) {
4837 err = got_error_from_errno("got_opentempfd");
4838 goto done;
4841 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4842 if (err)
4843 goto done;
4845 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4846 if (err)
4847 goto done;
4849 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4850 if (err)
4851 goto done;
4853 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4854 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4855 if (err)
4856 goto done;
4858 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4859 "");
4860 if (err)
4861 goto done;
4863 if (fseek(f1, 0L, SEEK_SET) == -1)
4864 return got_ferror(f1, GOT_ERR_IO);
4865 if (fseek(f2, 0L, SEEK_SET) == -1)
4866 return got_ferror(f2, GOT_ERR_IO);
4868 /* Count the number of actual changes in the diff result. */
4869 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4870 struct diff_chunk_context cc = {};
4871 diff_chunk_context_load_change(&cc, &nchunks_used,
4872 diffreg_result->result, n, 0);
4873 nchanges++;
4875 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4876 int choice;
4877 err = apply_or_reject_change(&choice, &nchunks_used,
4878 diffreg_result->result, n, relpath, f1, f2,
4879 &line_cur1, &line_cur2,
4880 reverse_patch ? NULL : outfile,
4881 reverse_patch ? outfile : NULL,
4882 ++i, nchanges, patch_cb, patch_arg);
4883 if (err)
4884 goto done;
4885 if (choice == GOT_PATCH_CHOICE_YES)
4886 have_content = 1;
4887 else if (choice == GOT_PATCH_CHOICE_QUIT)
4888 break;
4890 if (have_content) {
4891 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4892 reverse_patch ? NULL : outfile,
4893 reverse_patch ? outfile : NULL);
4894 if (err)
4895 goto done;
4897 if (!S_ISLNK(sb2.st_mode)) {
4898 mode_t mode;
4900 mode = apply_umask(sb2.st_mode);
4901 if (fchmod(fileno(outfile), mode) == -1) {
4902 err = got_error_from_errno2("fchmod", path2);
4903 goto done;
4907 done:
4908 free(id_str);
4909 if (fd != -1 && close(fd) == -1 && err == NULL)
4910 err = got_error_from_errno("close");
4911 if (blob)
4912 got_object_blob_close(blob);
4913 free_err = got_diffreg_result_free(diffreg_result);
4914 if (err == NULL)
4915 err = free_err;
4916 if (f1 && fclose(f1) == EOF && err == NULL)
4917 err = got_error_from_errno2("fclose", path1);
4918 if (f2 && fclose(f2) == EOF && err == NULL)
4919 err = got_error_from_errno2("fclose", path2);
4920 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4921 err = got_error_from_errno2("close", path2);
4922 if (outfile && fclose(outfile) == EOF && err == NULL)
4923 err = got_error_from_errno2("fclose", *path_outfile);
4924 if (path1 && unlink(path1) == -1 && err == NULL)
4925 err = got_error_from_errno2("unlink", path1);
4926 if (err || !have_content) {
4927 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4928 err = got_error_from_errno2("unlink", *path_outfile);
4929 free(*path_outfile);
4930 *path_outfile = NULL;
4932 free(path1);
4933 return err;
4936 static const struct got_error *
4937 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4938 const char *relpath, struct got_object_id *blob_id,
4939 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4940 int dirfd, const char *de_name)
4942 struct revert_file_args *a = arg;
4943 const struct got_error *err = NULL;
4944 char *parent_path = NULL;
4945 struct got_fileindex_entry *ie;
4946 struct got_commit_object *base_commit = NULL;
4947 struct got_tree_object *tree = NULL;
4948 struct got_object_id *tree_id = NULL;
4949 const struct got_tree_entry *te = NULL;
4950 char *tree_path = NULL, *te_name;
4951 char *ondisk_path = NULL, *path_content = NULL;
4952 struct got_blob_object *blob = NULL;
4953 int fd = -1;
4955 /* Reverting a staged deletion is a no-op. */
4956 if (status == GOT_STATUS_DELETE &&
4957 staged_status != GOT_STATUS_NO_CHANGE)
4958 return NULL;
4960 if (status == GOT_STATUS_UNVERSIONED)
4961 return (*a->progress_cb)(a->progress_arg,
4962 GOT_STATUS_UNVERSIONED, relpath);
4964 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4965 if (ie == NULL)
4966 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4968 /* Construct in-repository path of tree which contains this blob. */
4969 err = got_path_dirname(&parent_path, ie->path);
4970 if (err) {
4971 if (err->code != GOT_ERR_BAD_PATH)
4972 goto done;
4973 parent_path = strdup("/");
4974 if (parent_path == NULL) {
4975 err = got_error_from_errno("strdup");
4976 goto done;
4979 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4980 tree_path = strdup(parent_path);
4981 if (tree_path == NULL) {
4982 err = got_error_from_errno("strdup");
4983 goto done;
4985 } else {
4986 if (got_path_is_root_dir(parent_path)) {
4987 tree_path = strdup(a->worktree->path_prefix);
4988 if (tree_path == NULL) {
4989 err = got_error_from_errno("strdup");
4990 goto done;
4992 } else {
4993 if (asprintf(&tree_path, "%s/%s",
4994 a->worktree->path_prefix, parent_path) == -1) {
4995 err = got_error_from_errno("asprintf");
4996 goto done;
5001 err = got_object_open_as_commit(&base_commit, a->repo,
5002 a->worktree->base_commit_id);
5003 if (err)
5004 goto done;
5006 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5007 if (err) {
5008 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5009 (status == GOT_STATUS_ADD ||
5010 staged_status == GOT_STATUS_ADD)))
5011 goto done;
5012 } else {
5013 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5014 if (err)
5015 goto done;
5017 err = got_path_basename(&te_name, ie->path);
5018 if (err)
5019 goto done;
5021 te = got_object_tree_find_entry(tree, te_name);
5022 free(te_name);
5023 if (te == NULL && status != GOT_STATUS_ADD &&
5024 staged_status != GOT_STATUS_ADD) {
5025 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5026 goto done;
5030 switch (status) {
5031 case GOT_STATUS_ADD:
5032 if (a->patch_cb) {
5033 int choice = GOT_PATCH_CHOICE_NONE;
5034 err = (*a->patch_cb)(&choice, a->patch_arg,
5035 status, ie->path, NULL, 1, 1);
5036 if (err)
5037 goto done;
5038 if (choice != GOT_PATCH_CHOICE_YES)
5039 break;
5041 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5042 ie->path);
5043 if (err)
5044 goto done;
5045 got_fileindex_entry_remove(a->fileindex, ie);
5046 if (a->unlink_added_files) {
5047 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5049 if (a->added_files_to_unlink) {
5050 struct got_pathlist_entry *pe;
5052 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5053 entry) {
5054 if (got_path_cmp(pe->path, relpath,
5055 pe->path_len, strlen(relpath)))
5056 continue;
5057 do_unlink = 1;
5058 break;
5062 if (do_unlink) {
5063 if (asprintf(&ondisk_path, "%s/%s",
5064 got_worktree_get_root_path(a->worktree),
5065 relpath) == -1) {
5066 err = got_error_from_errno("asprintf");
5067 goto done;
5069 if (unlink(ondisk_path) == -1) {
5070 err = got_error_from_errno2("unlink",
5071 ondisk_path);
5072 break;
5076 break;
5077 case GOT_STATUS_DELETE:
5078 if (a->patch_cb) {
5079 int choice = GOT_PATCH_CHOICE_NONE;
5080 err = (*a->patch_cb)(&choice, a->patch_arg,
5081 status, ie->path, NULL, 1, 1);
5082 if (err)
5083 goto done;
5084 if (choice != GOT_PATCH_CHOICE_YES)
5085 break;
5087 /* fall through */
5088 case GOT_STATUS_MODIFY:
5089 case GOT_STATUS_MODE_CHANGE:
5090 case GOT_STATUS_CONFLICT:
5091 case GOT_STATUS_MISSING: {
5092 struct got_object_id id;
5093 if (staged_status == GOT_STATUS_ADD ||
5094 staged_status == GOT_STATUS_MODIFY)
5095 got_fileindex_entry_get_staged_blob_id(&id, ie);
5096 else
5097 got_fileindex_entry_get_blob_id(&id, ie);
5098 fd = got_opentempfd();
5099 if (fd == -1) {
5100 err = got_error_from_errno("got_opentempfd");
5101 goto done;
5104 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5105 if (err)
5106 goto done;
5108 if (asprintf(&ondisk_path, "%s/%s",
5109 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5110 err = got_error_from_errno("asprintf");
5111 goto done;
5114 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5115 status == GOT_STATUS_CONFLICT)) {
5116 int is_bad_symlink = 0;
5117 err = create_patched_content(&path_content, 1, &id,
5118 ondisk_path, dirfd, de_name, ie->path, a->repo,
5119 a->patch_cb, a->patch_arg);
5120 if (err || path_content == NULL)
5121 break;
5122 if (te && S_ISLNK(te->mode)) {
5123 if (unlink(path_content) == -1) {
5124 err = got_error_from_errno2("unlink",
5125 path_content);
5126 break;
5128 err = install_symlink(&is_bad_symlink,
5129 a->worktree, ondisk_path, ie->path,
5130 blob, 0, 1, 0, 0, a->repo,
5131 a->progress_cb, a->progress_arg);
5132 } else {
5133 if (rename(path_content, ondisk_path) == -1) {
5134 err = got_error_from_errno3("rename",
5135 path_content, ondisk_path);
5136 goto done;
5139 } else {
5140 int is_bad_symlink = 0;
5141 if (te && S_ISLNK(te->mode)) {
5142 err = install_symlink(&is_bad_symlink,
5143 a->worktree, ondisk_path, ie->path,
5144 blob, 0, 1, 0, 0, a->repo,
5145 a->progress_cb, a->progress_arg);
5146 } else {
5147 err = install_blob(a->worktree, ondisk_path,
5148 ie->path,
5149 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5150 got_fileindex_perms_to_st(ie), blob,
5151 0, 1, 0, 0, a->repo,
5152 a->progress_cb, a->progress_arg);
5154 if (err)
5155 goto done;
5156 if (status == GOT_STATUS_DELETE ||
5157 status == GOT_STATUS_MODE_CHANGE) {
5158 err = got_fileindex_entry_update(ie,
5159 a->worktree->root_fd, relpath,
5160 blob->id.sha1,
5161 a->worktree->base_commit_id->sha1, 1);
5162 if (err)
5163 goto done;
5165 if (is_bad_symlink) {
5166 got_fileindex_entry_filetype_set(ie,
5167 GOT_FILEIDX_MODE_BAD_SYMLINK);
5170 break;
5172 default:
5173 break;
5175 done:
5176 free(ondisk_path);
5177 free(path_content);
5178 free(parent_path);
5179 free(tree_path);
5180 if (fd != -1 && close(fd) == -1 && err == NULL)
5181 err = got_error_from_errno("close");
5182 if (blob)
5183 got_object_blob_close(blob);
5184 if (tree)
5185 got_object_tree_close(tree);
5186 free(tree_id);
5187 if (base_commit)
5188 got_object_commit_close(base_commit);
5189 return err;
5192 const struct got_error *
5193 got_worktree_revert(struct got_worktree *worktree,
5194 struct got_pathlist_head *paths,
5195 got_worktree_checkout_cb progress_cb, void *progress_arg,
5196 got_worktree_patch_cb patch_cb, void *patch_arg,
5197 struct got_repository *repo)
5199 struct got_fileindex *fileindex = NULL;
5200 char *fileindex_path = NULL;
5201 const struct got_error *err = NULL, *unlockerr = NULL;
5202 const struct got_error *sync_err = NULL;
5203 struct got_pathlist_entry *pe;
5204 struct revert_file_args rfa;
5206 err = lock_worktree(worktree, LOCK_EX);
5207 if (err)
5208 return err;
5210 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5211 if (err)
5212 goto done;
5214 rfa.worktree = worktree;
5215 rfa.fileindex = fileindex;
5216 rfa.progress_cb = progress_cb;
5217 rfa.progress_arg = progress_arg;
5218 rfa.patch_cb = patch_cb;
5219 rfa.patch_arg = patch_arg;
5220 rfa.repo = repo;
5221 rfa.unlink_added_files = 0;
5222 TAILQ_FOREACH(pe, paths, entry) {
5223 err = worktree_status(worktree, pe->path, fileindex, repo,
5224 revert_file, &rfa, NULL, NULL, 1, 0);
5225 if (err)
5226 break;
5228 sync_err = sync_fileindex(fileindex, fileindex_path);
5229 if (sync_err && err == NULL)
5230 err = sync_err;
5231 done:
5232 free(fileindex_path);
5233 if (fileindex)
5234 got_fileindex_free(fileindex);
5235 unlockerr = lock_worktree(worktree, LOCK_SH);
5236 if (unlockerr && err == NULL)
5237 err = unlockerr;
5238 return err;
5241 static void
5242 free_commitable(struct got_commitable *ct)
5244 free(ct->path);
5245 free(ct->in_repo_path);
5246 free(ct->ondisk_path);
5247 free(ct->blob_id);
5248 free(ct->base_blob_id);
5249 free(ct->staged_blob_id);
5250 free(ct->base_commit_id);
5251 free(ct);
5254 struct collect_commitables_arg {
5255 struct got_pathlist_head *commitable_paths;
5256 struct got_repository *repo;
5257 struct got_worktree *worktree;
5258 struct got_fileindex *fileindex;
5259 int have_staged_files;
5260 int allow_bad_symlinks;
5261 int diff_header_shown;
5262 int commit_conflicts;
5263 FILE *diff_outfile;
5264 FILE *f1;
5265 FILE *f2;
5269 * Create a file which contains the target path of a symlink so we can feed
5270 * it as content to the diff engine.
5272 static const struct got_error *
5273 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5274 const char *abspath)
5276 const struct got_error *err = NULL;
5277 char target_path[PATH_MAX];
5278 ssize_t target_len, outlen;
5280 *fd = -1;
5282 if (dirfd != -1) {
5283 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5284 if (target_len == -1)
5285 return got_error_from_errno2("readlinkat", abspath);
5286 } else {
5287 target_len = readlink(abspath, target_path, PATH_MAX);
5288 if (target_len == -1)
5289 return got_error_from_errno2("readlink", abspath);
5292 *fd = got_opentempfd();
5293 if (*fd == -1)
5294 return got_error_from_errno("got_opentempfd");
5296 outlen = write(*fd, target_path, target_len);
5297 if (outlen == -1) {
5298 err = got_error_from_errno("got_opentempfd");
5299 goto done;
5302 if (lseek(*fd, 0, SEEK_SET) == -1) {
5303 err = got_error_from_errno2("lseek", abspath);
5304 goto done;
5306 done:
5307 if (err) {
5308 close(*fd);
5309 *fd = -1;
5311 return err;
5314 static const struct got_error *
5315 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5316 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5317 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5319 const struct got_error *err = NULL;
5320 struct got_blob_object *blob1 = NULL;
5321 int fd = -1, fd1 = -1, fd2 = -1;
5322 FILE *ondisk_file = NULL;
5323 char *label1 = NULL;
5324 struct stat sb;
5325 off_t size1 = 0;
5326 int f2_exists = 0;
5327 char *id_str = NULL;
5329 memset(&sb, 0, sizeof(sb));
5331 if (diff_staged) {
5332 if (ct->staged_status != GOT_STATUS_MODIFY &&
5333 ct->staged_status != GOT_STATUS_ADD &&
5334 ct->staged_status != GOT_STATUS_DELETE)
5335 return NULL;
5336 } else {
5337 if (ct->status != GOT_STATUS_MODIFY &&
5338 ct->status != GOT_STATUS_ADD &&
5339 ct->status != GOT_STATUS_DELETE &&
5340 ct->status != GOT_STATUS_CONFLICT)
5341 return NULL;
5344 err = got_opentemp_truncate(f1);
5345 if (err)
5346 return got_error_from_errno("got_opentemp_truncate");
5347 err = got_opentemp_truncate(f2);
5348 if (err)
5349 return got_error_from_errno("got_opentemp_truncate");
5351 if (!*diff_header_shown) {
5352 err = got_object_id_str(&id_str, worktree->base_commit_id);
5353 if (err)
5354 return err;
5355 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5356 got_worktree_get_root_path(worktree));
5357 fprintf(diff_outfile, "commit - %s\n", id_str);
5358 fprintf(diff_outfile, "path + %s%s\n",
5359 got_worktree_get_root_path(worktree),
5360 diff_staged ? " (staged changes)" : "");
5361 *diff_header_shown = 1;
5364 if (diff_staged) {
5365 const char *label1 = NULL, *label2 = NULL;
5366 switch (ct->staged_status) {
5367 case GOT_STATUS_MODIFY:
5368 label1 = ct->path;
5369 label2 = ct->path;
5370 break;
5371 case GOT_STATUS_ADD:
5372 label2 = ct->path;
5373 break;
5374 case GOT_STATUS_DELETE:
5375 label1 = ct->path;
5376 break;
5377 default:
5378 return got_error(GOT_ERR_FILE_STATUS);
5380 fd1 = got_opentempfd();
5381 if (fd1 == -1) {
5382 err = got_error_from_errno("got_opentempfd");
5383 goto done;
5385 fd2 = got_opentempfd();
5386 if (fd2 == -1) {
5387 err = got_error_from_errno("got_opentempfd");
5388 goto done;
5390 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5391 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5392 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5393 NULL, repo, diff_outfile);
5394 goto done;
5397 fd1 = got_opentempfd();
5398 if (fd1 == -1) {
5399 err = got_error_from_errno("got_opentempfd");
5400 goto done;
5403 if (ct->status != GOT_STATUS_ADD) {
5404 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5405 8192, fd1);
5406 if (err)
5407 goto done;
5410 if (ct->status != GOT_STATUS_DELETE) {
5411 if (dirfd != -1) {
5412 fd = openat(dirfd, de_name,
5413 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5414 if (fd == -1) {
5415 if (!got_err_open_nofollow_on_symlink()) {
5416 err = got_error_from_errno2("openat",
5417 ct->ondisk_path);
5418 goto done;
5420 err = get_symlink_target_file(&fd, dirfd,
5421 de_name, ct->ondisk_path);
5422 if (err)
5423 goto done;
5425 } else {
5426 fd = open(ct->ondisk_path,
5427 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5428 if (fd == -1) {
5429 if (!got_err_open_nofollow_on_symlink()) {
5430 err = got_error_from_errno2("open",
5431 ct->ondisk_path);
5432 goto done;
5434 err = get_symlink_target_file(&fd, dirfd,
5435 de_name, ct->ondisk_path);
5436 if (err)
5437 goto done;
5440 if (fstatat(fd, ct->ondisk_path, &sb,
5441 AT_SYMLINK_NOFOLLOW) == -1) {
5442 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5443 goto done;
5445 ondisk_file = fdopen(fd, "r");
5446 if (ondisk_file == NULL) {
5447 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5448 goto done;
5450 fd = -1;
5451 f2_exists = 1;
5454 if (blob1) {
5455 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5456 f1, blob1);
5457 if (err)
5458 goto done;
5461 err = got_diff_blob_file(blob1, f1, size1, label1,
5462 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5463 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5464 done:
5465 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5466 err = got_error_from_errno("close");
5467 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5468 err = got_error_from_errno("close");
5469 if (blob1)
5470 got_object_blob_close(blob1);
5471 if (fd != -1 && close(fd) == -1 && err == NULL)
5472 err = got_error_from_errno("close");
5473 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5474 err = got_error_from_errno("fclose");
5475 return err;
5478 static const struct got_error *
5479 collect_commitables(void *arg, unsigned char status,
5480 unsigned char staged_status, const char *relpath,
5481 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5482 struct got_object_id *commit_id, int dirfd, const char *de_name)
5484 struct collect_commitables_arg *a = arg;
5485 const struct got_error *err = NULL;
5486 struct got_commitable *ct = NULL;
5487 struct got_pathlist_entry *new = NULL;
5488 char *parent_path = NULL, *path = NULL;
5489 struct stat sb;
5491 if (a->have_staged_files) {
5492 if (staged_status != GOT_STATUS_MODIFY &&
5493 staged_status != GOT_STATUS_ADD &&
5494 staged_status != GOT_STATUS_DELETE)
5495 return NULL;
5496 } else {
5497 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5498 printf("C %s\n", relpath);
5499 return got_error(GOT_ERR_COMMIT_CONFLICT);
5502 if (status != GOT_STATUS_MODIFY &&
5503 status != GOT_STATUS_MODE_CHANGE &&
5504 status != GOT_STATUS_ADD &&
5505 status != GOT_STATUS_DELETE &&
5506 status != GOT_STATUS_CONFLICT)
5507 return NULL;
5510 if (asprintf(&path, "/%s", relpath) == -1) {
5511 err = got_error_from_errno("asprintf");
5512 goto done;
5514 if (strcmp(path, "/") == 0) {
5515 parent_path = strdup("");
5516 if (parent_path == NULL)
5517 return got_error_from_errno("strdup");
5518 } else {
5519 err = got_path_dirname(&parent_path, path);
5520 if (err)
5521 return err;
5524 ct = calloc(1, sizeof(*ct));
5525 if (ct == NULL) {
5526 err = got_error_from_errno("calloc");
5527 goto done;
5530 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5531 relpath) == -1) {
5532 err = got_error_from_errno("asprintf");
5533 goto done;
5536 if (staged_status == GOT_STATUS_ADD ||
5537 staged_status == GOT_STATUS_MODIFY) {
5538 struct got_fileindex_entry *ie;
5539 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5540 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5541 case GOT_FILEIDX_MODE_REGULAR_FILE:
5542 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5543 ct->mode = S_IFREG;
5544 break;
5545 case GOT_FILEIDX_MODE_SYMLINK:
5546 ct->mode = S_IFLNK;
5547 break;
5548 default:
5549 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5550 goto done;
5552 ct->mode |= got_fileindex_entry_perms_get(ie);
5553 } else if (status != GOT_STATUS_DELETE &&
5554 staged_status != GOT_STATUS_DELETE) {
5555 if (dirfd != -1) {
5556 if (fstatat(dirfd, de_name, &sb,
5557 AT_SYMLINK_NOFOLLOW) == -1) {
5558 err = got_error_from_errno2("fstatat",
5559 ct->ondisk_path);
5560 goto done;
5562 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5563 err = got_error_from_errno2("lstat", ct->ondisk_path);
5564 goto done;
5566 ct->mode = sb.st_mode;
5569 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5570 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5571 relpath) == -1) {
5572 err = got_error_from_errno("asprintf");
5573 goto done;
5576 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5577 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5578 int is_bad_symlink;
5579 char target_path[PATH_MAX];
5580 ssize_t target_len;
5581 target_len = readlink(ct->ondisk_path, target_path,
5582 sizeof(target_path));
5583 if (target_len == -1) {
5584 err = got_error_from_errno2("readlink",
5585 ct->ondisk_path);
5586 goto done;
5588 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5589 target_len, ct->ondisk_path, a->worktree->root_path);
5590 if (err)
5591 goto done;
5592 if (is_bad_symlink) {
5593 err = got_error_path(ct->ondisk_path,
5594 GOT_ERR_BAD_SYMLINK);
5595 goto done;
5600 ct->status = status;
5601 ct->staged_status = staged_status;
5602 ct->blob_id = NULL; /* will be filled in when blob gets created */
5603 if (ct->status != GOT_STATUS_ADD &&
5604 ct->staged_status != GOT_STATUS_ADD) {
5605 ct->base_blob_id = got_object_id_dup(blob_id);
5606 if (ct->base_blob_id == NULL) {
5607 err = got_error_from_errno("got_object_id_dup");
5608 goto done;
5610 ct->base_commit_id = got_object_id_dup(commit_id);
5611 if (ct->base_commit_id == NULL) {
5612 err = got_error_from_errno("got_object_id_dup");
5613 goto done;
5616 if (ct->staged_status == GOT_STATUS_ADD ||
5617 ct->staged_status == GOT_STATUS_MODIFY) {
5618 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5619 if (ct->staged_blob_id == NULL) {
5620 err = got_error_from_errno("got_object_id_dup");
5621 goto done;
5624 ct->path = strdup(path);
5625 if (ct->path == NULL) {
5626 err = got_error_from_errno("strdup");
5627 goto done;
5629 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5630 if (err)
5631 goto done;
5633 if (a->diff_outfile && ct && new != NULL) {
5634 err = append_ct_diff(ct, &a->diff_header_shown,
5635 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5636 a->have_staged_files, a->repo, a->worktree);
5637 if (err)
5638 goto done;
5640 done:
5641 if (ct && (err || new == NULL))
5642 free_commitable(ct);
5643 free(parent_path);
5644 free(path);
5645 return err;
5648 static const struct got_error *write_tree(struct got_object_id **, int *,
5649 struct got_tree_object *, const char *, struct got_pathlist_head *,
5650 got_worktree_status_cb status_cb, void *status_arg,
5651 struct got_repository *);
5653 static const struct got_error *
5654 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5655 struct got_tree_entry *te, const char *parent_path,
5656 struct got_pathlist_head *commitable_paths,
5657 got_worktree_status_cb status_cb, void *status_arg,
5658 struct got_repository *repo)
5660 const struct got_error *err = NULL;
5661 struct got_tree_object *subtree;
5662 char *subpath;
5664 if (asprintf(&subpath, "%s%s%s", parent_path,
5665 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5666 return got_error_from_errno("asprintf");
5668 err = got_object_open_as_tree(&subtree, repo, &te->id);
5669 if (err)
5670 return err;
5672 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5673 commitable_paths, status_cb, status_arg, repo);
5674 got_object_tree_close(subtree);
5675 free(subpath);
5676 return err;
5679 static const struct got_error *
5680 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5682 const struct got_error *err = NULL;
5683 char *ct_parent_path = NULL;
5685 *match = 0;
5687 if (strchr(ct->in_repo_path, '/') == NULL) {
5688 *match = got_path_is_root_dir(path);
5689 return NULL;
5692 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5693 if (err)
5694 return err;
5695 *match = (strcmp(path, ct_parent_path) == 0);
5696 free(ct_parent_path);
5697 return err;
5700 static mode_t
5701 get_ct_file_mode(struct got_commitable *ct)
5703 if (S_ISLNK(ct->mode))
5704 return S_IFLNK;
5706 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5709 static const struct got_error *
5710 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5711 struct got_tree_entry *te, struct got_commitable *ct)
5713 const struct got_error *err = NULL;
5715 *new_te = NULL;
5717 err = got_object_tree_entry_dup(new_te, te);
5718 if (err)
5719 goto done;
5721 (*new_te)->mode = get_ct_file_mode(ct);
5723 if (ct->staged_status == GOT_STATUS_MODIFY)
5724 memcpy(&(*new_te)->id, ct->staged_blob_id,
5725 sizeof((*new_te)->id));
5726 else
5727 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5728 done:
5729 if (err && *new_te) {
5730 free(*new_te);
5731 *new_te = NULL;
5733 return err;
5736 static const struct got_error *
5737 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5738 struct got_commitable *ct)
5740 const struct got_error *err = NULL;
5741 char *ct_name = NULL;
5743 *new_te = NULL;
5745 *new_te = calloc(1, sizeof(**new_te));
5746 if (*new_te == NULL)
5747 return got_error_from_errno("calloc");
5749 err = got_path_basename(&ct_name, ct->path);
5750 if (err)
5751 goto done;
5752 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5753 sizeof((*new_te)->name)) {
5754 err = got_error(GOT_ERR_NO_SPACE);
5755 goto done;
5758 (*new_te)->mode = get_ct_file_mode(ct);
5760 if (ct->staged_status == GOT_STATUS_ADD)
5761 memcpy(&(*new_te)->id, ct->staged_blob_id,
5762 sizeof((*new_te)->id));
5763 else
5764 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5765 done:
5766 free(ct_name);
5767 if (err && *new_te) {
5768 free(*new_te);
5769 *new_te = NULL;
5771 return err;
5774 static const struct got_error *
5775 insert_tree_entry(struct got_tree_entry *new_te,
5776 struct got_pathlist_head *paths)
5778 const struct got_error *err = NULL;
5779 struct got_pathlist_entry *new_pe;
5781 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5782 if (err)
5783 return err;
5784 if (new_pe == NULL)
5785 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5786 return NULL;
5789 static const struct got_error *
5790 report_ct_status(struct got_commitable *ct,
5791 got_worktree_status_cb status_cb, void *status_arg)
5793 const char *ct_path = ct->path;
5794 unsigned char status;
5796 if (status_cb == NULL) /* no commit progress output desired */
5797 return NULL;
5799 while (ct_path[0] == '/')
5800 ct_path++;
5802 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5803 status = ct->staged_status;
5804 else
5805 status = ct->status;
5807 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5808 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5811 static const struct got_error *
5812 match_modified_subtree(int *modified, struct got_tree_entry *te,
5813 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5815 const struct got_error *err = NULL;
5816 struct got_pathlist_entry *pe;
5817 char *te_path;
5819 *modified = 0;
5821 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5822 got_path_is_root_dir(base_tree_path) ? "" : "/",
5823 te->name) == -1)
5824 return got_error_from_errno("asprintf");
5826 TAILQ_FOREACH(pe, commitable_paths, entry) {
5827 struct got_commitable *ct = pe->data;
5828 *modified = got_path_is_child(ct->in_repo_path, te_path,
5829 strlen(te_path));
5830 if (*modified)
5831 break;
5834 free(te_path);
5835 return err;
5838 static const struct got_error *
5839 match_deleted_or_modified_ct(struct got_commitable **ctp,
5840 struct got_tree_entry *te, const char *base_tree_path,
5841 struct got_pathlist_head *commitable_paths)
5843 const struct got_error *err = NULL;
5844 struct got_pathlist_entry *pe;
5846 *ctp = NULL;
5848 TAILQ_FOREACH(pe, commitable_paths, entry) {
5849 struct got_commitable *ct = pe->data;
5850 char *ct_name = NULL;
5851 int path_matches;
5853 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5854 if (ct->status != GOT_STATUS_MODIFY &&
5855 ct->status != GOT_STATUS_MODE_CHANGE &&
5856 ct->status != GOT_STATUS_DELETE &&
5857 ct->status != GOT_STATUS_CONFLICT)
5858 continue;
5859 } else {
5860 if (ct->staged_status != GOT_STATUS_MODIFY &&
5861 ct->staged_status != GOT_STATUS_DELETE)
5862 continue;
5865 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5866 continue;
5868 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5869 if (err)
5870 return err;
5871 if (!path_matches)
5872 continue;
5874 err = got_path_basename(&ct_name, pe->path);
5875 if (err)
5876 return err;
5878 if (strcmp(te->name, ct_name) != 0) {
5879 free(ct_name);
5880 continue;
5882 free(ct_name);
5884 *ctp = ct;
5885 break;
5888 return err;
5891 static const struct got_error *
5892 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5893 const char *child_path, const char *path_base_tree,
5894 struct got_pathlist_head *commitable_paths,
5895 got_worktree_status_cb status_cb, void *status_arg,
5896 struct got_repository *repo)
5898 const struct got_error *err = NULL;
5899 struct got_tree_entry *new_te;
5900 char *subtree_path;
5901 struct got_object_id *id = NULL;
5902 int nentries;
5904 *new_tep = NULL;
5906 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5907 got_path_is_root_dir(path_base_tree) ? "" : "/",
5908 child_path) == -1)
5909 return got_error_from_errno("asprintf");
5911 new_te = calloc(1, sizeof(*new_te));
5912 if (new_te == NULL)
5913 return got_error_from_errno("calloc");
5914 new_te->mode = S_IFDIR;
5916 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5917 sizeof(new_te->name)) {
5918 err = got_error(GOT_ERR_NO_SPACE);
5919 goto done;
5921 err = write_tree(&id, &nentries, NULL, subtree_path,
5922 commitable_paths, status_cb, status_arg, repo);
5923 if (err) {
5924 free(new_te);
5925 goto done;
5927 memcpy(&new_te->id, id, sizeof(new_te->id));
5928 done:
5929 free(id);
5930 free(subtree_path);
5931 if (err == NULL)
5932 *new_tep = new_te;
5933 return err;
5936 static const struct got_error *
5937 write_tree(struct got_object_id **new_tree_id, int *nentries,
5938 struct got_tree_object *base_tree, const char *path_base_tree,
5939 struct got_pathlist_head *commitable_paths,
5940 got_worktree_status_cb status_cb, void *status_arg,
5941 struct got_repository *repo)
5943 const struct got_error *err = NULL;
5944 struct got_pathlist_head paths;
5945 struct got_tree_entry *te, *new_te = NULL;
5946 struct got_pathlist_entry *pe;
5948 TAILQ_INIT(&paths);
5949 *nentries = 0;
5951 /* Insert, and recurse into, newly added entries first. */
5952 TAILQ_FOREACH(pe, commitable_paths, entry) {
5953 struct got_commitable *ct = pe->data;
5954 char *child_path = NULL, *slash;
5956 if ((ct->status != GOT_STATUS_ADD &&
5957 ct->staged_status != GOT_STATUS_ADD) ||
5958 (ct->flags & GOT_COMMITABLE_ADDED))
5959 continue;
5961 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5962 strlen(path_base_tree)))
5963 continue;
5965 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5966 ct->in_repo_path);
5967 if (err)
5968 goto done;
5970 slash = strchr(child_path, '/');
5971 if (slash == NULL) {
5972 err = alloc_added_blob_tree_entry(&new_te, ct);
5973 if (err)
5974 goto done;
5975 err = report_ct_status(ct, status_cb, status_arg);
5976 if (err)
5977 goto done;
5978 ct->flags |= GOT_COMMITABLE_ADDED;
5979 err = insert_tree_entry(new_te, &paths);
5980 if (err)
5981 goto done;
5982 (*nentries)++;
5983 } else {
5984 *slash = '\0'; /* trim trailing path components */
5985 if (base_tree == NULL ||
5986 got_object_tree_find_entry(base_tree, child_path)
5987 == NULL) {
5988 err = make_subtree_for_added_blob(&new_te,
5989 child_path, path_base_tree,
5990 commitable_paths, status_cb, status_arg,
5991 repo);
5992 if (err)
5993 goto done;
5994 err = insert_tree_entry(new_te, &paths);
5995 if (err)
5996 goto done;
5997 (*nentries)++;
6002 if (base_tree) {
6003 int i, nbase_entries;
6004 /* Handle modified and deleted entries. */
6005 nbase_entries = got_object_tree_get_nentries(base_tree);
6006 for (i = 0; i < nbase_entries; i++) {
6007 struct got_commitable *ct = NULL;
6009 te = got_object_tree_get_entry(base_tree, i);
6010 if (got_object_tree_entry_is_submodule(te)) {
6011 /* Entry is a submodule; just copy it. */
6012 err = got_object_tree_entry_dup(&new_te, te);
6013 if (err)
6014 goto done;
6015 err = insert_tree_entry(new_te, &paths);
6016 if (err)
6017 goto done;
6018 (*nentries)++;
6019 continue;
6022 if (S_ISDIR(te->mode)) {
6023 int modified;
6024 err = got_object_tree_entry_dup(&new_te, te);
6025 if (err)
6026 goto done;
6027 err = match_modified_subtree(&modified, te,
6028 path_base_tree, commitable_paths);
6029 if (err)
6030 goto done;
6031 /* Avoid recursion into unmodified subtrees. */
6032 if (modified) {
6033 struct got_object_id *new_id;
6034 int nsubentries;
6035 err = write_subtree(&new_id,
6036 &nsubentries, te,
6037 path_base_tree, commitable_paths,
6038 status_cb, status_arg, repo);
6039 if (err)
6040 goto done;
6041 if (nsubentries == 0) {
6042 /* All entries were deleted. */
6043 free(new_id);
6044 continue;
6046 memcpy(&new_te->id, new_id,
6047 sizeof(new_te->id));
6048 free(new_id);
6050 err = insert_tree_entry(new_te, &paths);
6051 if (err)
6052 goto done;
6053 (*nentries)++;
6054 continue;
6057 err = match_deleted_or_modified_ct(&ct, te,
6058 path_base_tree, commitable_paths);
6059 if (err)
6060 goto done;
6061 if (ct) {
6062 /* NB: Deleted entries get dropped here. */
6063 if (ct->status == GOT_STATUS_MODIFY ||
6064 ct->status == GOT_STATUS_MODE_CHANGE ||
6065 ct->status == GOT_STATUS_CONFLICT ||
6066 ct->staged_status == GOT_STATUS_MODIFY) {
6067 err = alloc_modified_blob_tree_entry(
6068 &new_te, te, ct);
6069 if (err)
6070 goto done;
6071 err = insert_tree_entry(new_te, &paths);
6072 if (err)
6073 goto done;
6074 (*nentries)++;
6076 err = report_ct_status(ct, status_cb,
6077 status_arg);
6078 if (err)
6079 goto done;
6080 } else {
6081 /* Entry is unchanged; just copy it. */
6082 err = got_object_tree_entry_dup(&new_te, te);
6083 if (err)
6084 goto done;
6085 err = insert_tree_entry(new_te, &paths);
6086 if (err)
6087 goto done;
6088 (*nentries)++;
6093 /* Write new list of entries; deleted entries have been dropped. */
6094 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6095 done:
6096 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6097 return err;
6100 static const struct got_error *
6101 update_fileindex_after_commit(struct got_worktree *worktree,
6102 struct got_pathlist_head *commitable_paths,
6103 struct got_object_id *new_base_commit_id,
6104 struct got_fileindex *fileindex, int have_staged_files)
6106 const struct got_error *err = NULL;
6107 struct got_pathlist_entry *pe;
6108 char *relpath = NULL;
6110 TAILQ_FOREACH(pe, commitable_paths, entry) {
6111 struct got_fileindex_entry *ie;
6112 struct got_commitable *ct = pe->data;
6114 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6116 err = got_path_skip_common_ancestor(&relpath,
6117 worktree->root_path, ct->ondisk_path);
6118 if (err)
6119 goto done;
6121 if (ie) {
6122 if (ct->status == GOT_STATUS_DELETE ||
6123 ct->staged_status == GOT_STATUS_DELETE) {
6124 got_fileindex_entry_remove(fileindex, ie);
6125 } else if (ct->staged_status == GOT_STATUS_ADD ||
6126 ct->staged_status == GOT_STATUS_MODIFY) {
6127 got_fileindex_entry_stage_set(ie,
6128 GOT_FILEIDX_STAGE_NONE);
6129 got_fileindex_entry_staged_filetype_set(ie, 0);
6131 err = got_fileindex_entry_update(ie,
6132 worktree->root_fd, relpath,
6133 ct->staged_blob_id->sha1,
6134 new_base_commit_id->sha1,
6135 !have_staged_files);
6136 } else
6137 err = got_fileindex_entry_update(ie,
6138 worktree->root_fd, relpath,
6139 ct->blob_id->sha1,
6140 new_base_commit_id->sha1,
6141 !have_staged_files);
6142 } else {
6143 err = got_fileindex_entry_alloc(&ie, pe->path);
6144 if (err)
6145 goto done;
6146 err = got_fileindex_entry_update(ie,
6147 worktree->root_fd, relpath, ct->blob_id->sha1,
6148 new_base_commit_id->sha1, 1);
6149 if (err) {
6150 got_fileindex_entry_free(ie);
6151 goto done;
6153 err = got_fileindex_entry_add(fileindex, ie);
6154 if (err) {
6155 got_fileindex_entry_free(ie);
6156 goto done;
6159 free(relpath);
6160 relpath = NULL;
6162 done:
6163 free(relpath);
6164 return err;
6168 static const struct got_error *
6169 check_out_of_date(const char *in_repo_path, unsigned char status,
6170 unsigned char staged_status, struct got_object_id *base_blob_id,
6171 struct got_object_id *base_commit_id,
6172 struct got_object_id *head_commit_id, struct got_repository *repo,
6173 int ood_errcode)
6175 const struct got_error *err = NULL;
6176 struct got_commit_object *commit = NULL;
6177 struct got_object_id *id = NULL;
6179 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6180 /* Trivial case: base commit == head commit */
6181 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6182 return NULL;
6184 * Ensure file content which local changes were based
6185 * on matches file content in the branch head.
6187 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6188 if (err)
6189 goto done;
6190 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6191 if (err) {
6192 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6193 err = got_error(ood_errcode);
6194 goto done;
6195 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6196 err = got_error(ood_errcode);
6197 } else {
6198 /* Require that added files don't exist in the branch head. */
6199 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6200 if (err)
6201 goto done;
6202 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6203 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6204 goto done;
6205 err = id ? got_error(ood_errcode) : NULL;
6207 done:
6208 free(id);
6209 if (commit)
6210 got_object_commit_close(commit);
6211 return err;
6214 static const struct got_error *
6215 commit_worktree(struct got_object_id **new_commit_id,
6216 struct got_pathlist_head *commitable_paths,
6217 struct got_object_id *head_commit_id,
6218 struct got_object_id *parent_id2,
6219 struct got_worktree *worktree,
6220 const char *author, const char *committer, char *diff_path,
6221 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6222 got_worktree_status_cb status_cb, void *status_arg,
6223 struct got_repository *repo)
6225 const struct got_error *err = NULL, *unlockerr = NULL;
6226 struct got_pathlist_entry *pe;
6227 const char *head_ref_name = NULL;
6228 struct got_commit_object *head_commit = NULL;
6229 struct got_reference *head_ref2 = NULL;
6230 struct got_object_id *head_commit_id2 = NULL;
6231 struct got_tree_object *head_tree = NULL;
6232 struct got_object_id *new_tree_id = NULL;
6233 int nentries, nparents = 0;
6234 struct got_object_id_queue parent_ids;
6235 struct got_object_qid *pid = NULL;
6236 char *logmsg = NULL;
6237 time_t timestamp;
6239 *new_commit_id = NULL;
6241 STAILQ_INIT(&parent_ids);
6243 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6244 if (err)
6245 goto done;
6247 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6248 if (err)
6249 goto done;
6251 if (commit_msg_cb != NULL) {
6252 err = commit_msg_cb(commitable_paths, diff_path,
6253 &logmsg, commit_arg);
6254 if (err)
6255 goto done;
6258 if (logmsg == NULL || strlen(logmsg) == 0) {
6259 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6260 goto done;
6263 /* Create blobs from added and modified files and record their IDs. */
6264 TAILQ_FOREACH(pe, commitable_paths, entry) {
6265 struct got_commitable *ct = pe->data;
6266 char *ondisk_path;
6268 /* Blobs for staged files already exist. */
6269 if (ct->staged_status == GOT_STATUS_ADD ||
6270 ct->staged_status == GOT_STATUS_MODIFY)
6271 continue;
6273 if (ct->status != GOT_STATUS_ADD &&
6274 ct->status != GOT_STATUS_MODIFY &&
6275 ct->status != GOT_STATUS_MODE_CHANGE &&
6276 ct->status != GOT_STATUS_CONFLICT)
6277 continue;
6279 if (asprintf(&ondisk_path, "%s/%s",
6280 worktree->root_path, pe->path) == -1) {
6281 err = got_error_from_errno("asprintf");
6282 goto done;
6284 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6285 free(ondisk_path);
6286 if (err)
6287 goto done;
6290 /* Recursively write new tree objects. */
6291 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6292 commitable_paths, status_cb, status_arg, repo);
6293 if (err)
6294 goto done;
6296 err = got_object_qid_alloc(&pid, head_commit_id);
6297 if (err)
6298 goto done;
6299 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6300 nparents++;
6301 if (parent_id2) {
6302 err = got_object_qid_alloc(&pid, parent_id2);
6303 if (err)
6304 goto done;
6305 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6306 nparents++;
6308 timestamp = time(NULL);
6309 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6310 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6311 if (logmsg != NULL)
6312 free(logmsg);
6313 if (err)
6314 goto done;
6316 /* Check if a concurrent commit to our branch has occurred. */
6317 head_ref_name = got_worktree_get_head_ref_name(worktree);
6318 if (head_ref_name == NULL) {
6319 err = got_error_from_errno("got_worktree_get_head_ref_name");
6320 goto done;
6322 /* Lock the reference here to prevent concurrent modification. */
6323 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6324 if (err)
6325 goto done;
6326 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6327 if (err)
6328 goto done;
6329 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6330 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6331 goto done;
6333 /* Update branch head in repository. */
6334 err = got_ref_change_ref(head_ref2, *new_commit_id);
6335 if (err)
6336 goto done;
6337 err = got_ref_write(head_ref2, repo);
6338 if (err)
6339 goto done;
6341 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6342 if (err)
6343 goto done;
6345 err = ref_base_commit(worktree, repo);
6346 if (err)
6347 goto done;
6348 done:
6349 got_object_id_queue_free(&parent_ids);
6350 if (head_tree)
6351 got_object_tree_close(head_tree);
6352 if (head_commit)
6353 got_object_commit_close(head_commit);
6354 free(head_commit_id2);
6355 if (head_ref2) {
6356 unlockerr = got_ref_unlock(head_ref2);
6357 if (unlockerr && err == NULL)
6358 err = unlockerr;
6359 got_ref_close(head_ref2);
6361 return err;
6364 static const struct got_error *
6365 check_path_is_commitable(const char *path,
6366 struct got_pathlist_head *commitable_paths)
6368 struct got_pathlist_entry *cpe = NULL;
6369 size_t path_len = strlen(path);
6371 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6372 struct got_commitable *ct = cpe->data;
6373 const char *ct_path = ct->path;
6375 while (ct_path[0] == '/')
6376 ct_path++;
6378 if (strcmp(path, ct_path) == 0 ||
6379 got_path_is_child(ct_path, path, path_len))
6380 break;
6383 if (cpe == NULL)
6384 return got_error_path(path, GOT_ERR_BAD_PATH);
6386 return NULL;
6389 static const struct got_error *
6390 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6392 int *have_staged_files = arg;
6394 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6395 *have_staged_files = 1;
6396 return got_error(GOT_ERR_CANCELLED);
6399 return NULL;
6402 static const struct got_error *
6403 check_non_staged_files(struct got_fileindex *fileindex,
6404 struct got_pathlist_head *paths)
6406 struct got_pathlist_entry *pe;
6407 struct got_fileindex_entry *ie;
6409 TAILQ_FOREACH(pe, paths, entry) {
6410 if (pe->path[0] == '\0')
6411 continue;
6412 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6413 if (ie == NULL)
6414 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6415 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6416 return got_error_path(pe->path,
6417 GOT_ERR_FILE_NOT_STAGED);
6420 return NULL;
6423 const struct got_error *
6424 got_worktree_commit(struct got_object_id **new_commit_id,
6425 struct got_worktree *worktree, struct got_pathlist_head *paths,
6426 const char *author, const char *committer, int allow_bad_symlinks,
6427 int show_diff, int commit_conflicts,
6428 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6429 got_worktree_status_cb status_cb, void *status_arg,
6430 struct got_repository *repo)
6432 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6433 struct got_fileindex *fileindex = NULL;
6434 char *fileindex_path = NULL;
6435 struct got_pathlist_head commitable_paths;
6436 struct collect_commitables_arg cc_arg;
6437 struct got_pathlist_entry *pe;
6438 struct got_reference *head_ref = NULL;
6439 struct got_object_id *head_commit_id = NULL;
6440 char *diff_path = NULL;
6441 int have_staged_files = 0;
6443 *new_commit_id = NULL;
6445 memset(&cc_arg, 0, sizeof(cc_arg));
6446 TAILQ_INIT(&commitable_paths);
6448 err = lock_worktree(worktree, LOCK_EX);
6449 if (err)
6450 goto done;
6452 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6453 if (err)
6454 goto done;
6456 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6457 if (err)
6458 goto done;
6460 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6461 if (err)
6462 goto done;
6464 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6465 &have_staged_files);
6466 if (err && err->code != GOT_ERR_CANCELLED)
6467 goto done;
6468 if (have_staged_files) {
6469 err = check_non_staged_files(fileindex, paths);
6470 if (err)
6471 goto done;
6474 cc_arg.commitable_paths = &commitable_paths;
6475 cc_arg.worktree = worktree;
6476 cc_arg.fileindex = fileindex;
6477 cc_arg.repo = repo;
6478 cc_arg.have_staged_files = have_staged_files;
6479 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6480 cc_arg.diff_header_shown = 0;
6481 cc_arg.commit_conflicts = commit_conflicts;
6482 if (show_diff) {
6483 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6484 GOT_TMPDIR_STR "/got", ".diff");
6485 if (err)
6486 goto done;
6487 cc_arg.f1 = got_opentemp();
6488 if (cc_arg.f1 == NULL) {
6489 err = got_error_from_errno("got_opentemp");
6490 goto done;
6492 cc_arg.f2 = got_opentemp();
6493 if (cc_arg.f2 == NULL) {
6494 err = got_error_from_errno("got_opentemp");
6495 goto done;
6499 TAILQ_FOREACH(pe, paths, entry) {
6500 err = worktree_status(worktree, pe->path, fileindex, repo,
6501 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6502 if (err)
6503 goto done;
6506 if (show_diff) {
6507 if (fflush(cc_arg.diff_outfile) == EOF) {
6508 err = got_error_from_errno("fflush");
6509 goto done;
6513 if (TAILQ_EMPTY(&commitable_paths)) {
6514 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6515 goto done;
6518 TAILQ_FOREACH(pe, paths, entry) {
6519 err = check_path_is_commitable(pe->path, &commitable_paths);
6520 if (err)
6521 goto done;
6524 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6525 struct got_commitable *ct = pe->data;
6526 const char *ct_path = ct->in_repo_path;
6528 while (ct_path[0] == '/')
6529 ct_path++;
6530 err = check_out_of_date(ct_path, ct->status,
6531 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6532 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6533 if (err)
6534 goto done;
6538 err = commit_worktree(new_commit_id, &commitable_paths,
6539 head_commit_id, NULL, worktree, author, committer,
6540 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6541 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6542 if (err)
6543 goto done;
6545 err = update_fileindex_after_commit(worktree, &commitable_paths,
6546 *new_commit_id, fileindex, have_staged_files);
6547 sync_err = sync_fileindex(fileindex, fileindex_path);
6548 if (sync_err && err == NULL)
6549 err = sync_err;
6550 done:
6551 if (fileindex)
6552 got_fileindex_free(fileindex);
6553 free(fileindex_path);
6554 unlockerr = lock_worktree(worktree, LOCK_SH);
6555 if (unlockerr && err == NULL)
6556 err = unlockerr;
6557 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6558 struct got_commitable *ct = pe->data;
6560 free_commitable(ct);
6562 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6563 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6564 err = got_error_from_errno2("unlink", diff_path);
6565 free(diff_path);
6566 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6567 err == NULL)
6568 err = got_error_from_errno("fclose");
6569 return err;
6572 const char *
6573 got_commitable_get_path(struct got_commitable *ct)
6575 return ct->path;
6578 unsigned int
6579 got_commitable_get_status(struct got_commitable *ct)
6581 return ct->status;
6584 struct check_rebase_ok_arg {
6585 struct got_worktree *worktree;
6586 struct got_repository *repo;
6589 static const struct got_error *
6590 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6592 const struct got_error *err = NULL;
6593 struct check_rebase_ok_arg *a = arg;
6594 unsigned char status;
6595 struct stat sb;
6596 char *ondisk_path;
6598 /* Reject rebase of a work tree with mixed base commits. */
6599 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6600 SHA1_DIGEST_LENGTH))
6601 return got_error(GOT_ERR_MIXED_COMMITS);
6603 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6604 == -1)
6605 return got_error_from_errno("asprintf");
6607 /* Reject rebase of a work tree with modified or staged files. */
6608 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6609 free(ondisk_path);
6610 if (err)
6611 return err;
6613 if (status != GOT_STATUS_NO_CHANGE)
6614 return got_error(GOT_ERR_MODIFIED);
6615 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6616 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6618 return NULL;
6621 const struct got_error *
6622 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6623 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6624 struct got_worktree *worktree, struct got_reference *branch,
6625 struct got_repository *repo)
6627 const struct got_error *err = NULL;
6628 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6629 char *branch_ref_name = NULL;
6630 char *fileindex_path = NULL;
6631 struct check_rebase_ok_arg ok_arg;
6632 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6633 struct got_object_id *wt_branch_tip = NULL;
6635 *new_base_branch_ref = NULL;
6636 *tmp_branch = NULL;
6637 *fileindex = NULL;
6639 err = lock_worktree(worktree, LOCK_EX);
6640 if (err)
6641 return err;
6643 err = open_fileindex(fileindex, &fileindex_path, worktree);
6644 if (err)
6645 goto done;
6647 ok_arg.worktree = worktree;
6648 ok_arg.repo = repo;
6649 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6650 &ok_arg);
6651 if (err)
6652 goto done;
6654 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6655 if (err)
6656 goto done;
6658 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6659 if (err)
6660 goto done;
6662 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6663 if (err)
6664 goto done;
6666 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6667 0);
6668 if (err)
6669 goto done;
6671 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6672 if (err)
6673 goto done;
6674 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6675 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6676 goto done;
6679 err = got_ref_alloc_symref(new_base_branch_ref,
6680 new_base_branch_ref_name, wt_branch);
6681 if (err)
6682 goto done;
6683 err = got_ref_write(*new_base_branch_ref, repo);
6684 if (err)
6685 goto done;
6687 /* TODO Lock original branch's ref while rebasing? */
6689 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6690 if (err)
6691 goto done;
6693 err = got_ref_write(branch_ref, repo);
6694 if (err)
6695 goto done;
6697 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6698 worktree->base_commit_id);
6699 if (err)
6700 goto done;
6701 err = got_ref_write(*tmp_branch, repo);
6702 if (err)
6703 goto done;
6705 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6706 if (err)
6707 goto done;
6708 done:
6709 free(fileindex_path);
6710 free(tmp_branch_name);
6711 free(new_base_branch_ref_name);
6712 free(branch_ref_name);
6713 if (branch_ref)
6714 got_ref_close(branch_ref);
6715 if (wt_branch)
6716 got_ref_close(wt_branch);
6717 free(wt_branch_tip);
6718 if (err) {
6719 if (*new_base_branch_ref) {
6720 got_ref_close(*new_base_branch_ref);
6721 *new_base_branch_ref = NULL;
6723 if (*tmp_branch) {
6724 got_ref_close(*tmp_branch);
6725 *tmp_branch = NULL;
6727 if (*fileindex) {
6728 got_fileindex_free(*fileindex);
6729 *fileindex = NULL;
6731 lock_worktree(worktree, LOCK_SH);
6733 return err;
6736 const struct got_error *
6737 got_worktree_rebase_continue(struct got_object_id **commit_id,
6738 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6739 struct got_reference **branch, struct got_fileindex **fileindex,
6740 struct got_worktree *worktree, struct got_repository *repo)
6742 const struct got_error *err;
6743 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6744 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6745 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6746 char *fileindex_path = NULL;
6747 int have_staged_files = 0;
6749 *commit_id = NULL;
6750 *new_base_branch = NULL;
6751 *tmp_branch = NULL;
6752 *branch = NULL;
6753 *fileindex = NULL;
6755 err = lock_worktree(worktree, LOCK_EX);
6756 if (err)
6757 return err;
6759 err = open_fileindex(fileindex, &fileindex_path, worktree);
6760 if (err)
6761 goto done;
6763 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6764 &have_staged_files);
6765 if (err && err->code != GOT_ERR_CANCELLED)
6766 goto done;
6767 if (have_staged_files) {
6768 err = got_error(GOT_ERR_STAGED_PATHS);
6769 goto done;
6772 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6773 if (err)
6774 goto done;
6776 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6777 if (err)
6778 goto done;
6780 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6781 if (err)
6782 goto done;
6784 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6785 if (err)
6786 goto done;
6788 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6789 if (err)
6790 goto done;
6792 err = got_ref_open(branch, repo,
6793 got_ref_get_symref_target(branch_ref), 0);
6794 if (err)
6795 goto done;
6797 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6798 if (err)
6799 goto done;
6801 err = got_ref_resolve(commit_id, repo, commit_ref);
6802 if (err)
6803 goto done;
6805 err = got_ref_open(new_base_branch, repo,
6806 new_base_branch_ref_name, 0);
6807 if (err)
6808 goto done;
6810 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6811 if (err)
6812 goto done;
6813 done:
6814 free(commit_ref_name);
6815 free(branch_ref_name);
6816 free(fileindex_path);
6817 if (commit_ref)
6818 got_ref_close(commit_ref);
6819 if (branch_ref)
6820 got_ref_close(branch_ref);
6821 if (err) {
6822 free(*commit_id);
6823 *commit_id = NULL;
6824 if (*tmp_branch) {
6825 got_ref_close(*tmp_branch);
6826 *tmp_branch = NULL;
6828 if (*new_base_branch) {
6829 got_ref_close(*new_base_branch);
6830 *new_base_branch = NULL;
6832 if (*branch) {
6833 got_ref_close(*branch);
6834 *branch = NULL;
6836 if (*fileindex) {
6837 got_fileindex_free(*fileindex);
6838 *fileindex = NULL;
6840 lock_worktree(worktree, LOCK_SH);
6842 return err;
6845 const struct got_error *
6846 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6848 const struct got_error *err;
6849 char *tmp_branch_name = NULL;
6851 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6852 if (err)
6853 return err;
6855 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6856 free(tmp_branch_name);
6857 return NULL;
6860 static const struct got_error *
6861 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6862 const char *diff_path, char **logmsg, void *arg)
6864 *logmsg = arg;
6865 return NULL;
6868 static const struct got_error *
6869 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6870 const char *path, struct got_object_id *blob_id,
6871 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6872 int dirfd, const char *de_name)
6874 return NULL;
6877 struct collect_merged_paths_arg {
6878 got_worktree_checkout_cb progress_cb;
6879 void *progress_arg;
6880 struct got_pathlist_head *merged_paths;
6883 static const struct got_error *
6884 collect_merged_paths(void *arg, unsigned char status, const char *path)
6886 const struct got_error *err;
6887 struct collect_merged_paths_arg *a = arg;
6888 char *p;
6889 struct got_pathlist_entry *new;
6891 err = (*a->progress_cb)(a->progress_arg, status, path);
6892 if (err)
6893 return err;
6895 if (status != GOT_STATUS_MERGE &&
6896 status != GOT_STATUS_ADD &&
6897 status != GOT_STATUS_DELETE &&
6898 status != GOT_STATUS_CONFLICT)
6899 return NULL;
6901 p = strdup(path);
6902 if (p == NULL)
6903 return got_error_from_errno("strdup");
6905 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6906 if (err || new == NULL)
6907 free(p);
6908 return err;
6911 static const struct got_error *
6912 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6913 int is_rebase, struct got_repository *repo)
6915 const struct got_error *err;
6916 struct got_reference *commit_ref = NULL;
6918 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6919 if (err) {
6920 if (err->code != GOT_ERR_NOT_REF)
6921 goto done;
6922 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6923 if (err)
6924 goto done;
6925 err = got_ref_write(commit_ref, repo);
6926 if (err)
6927 goto done;
6928 } else if (is_rebase) {
6929 struct got_object_id *stored_id;
6930 int cmp;
6932 err = got_ref_resolve(&stored_id, repo, commit_ref);
6933 if (err)
6934 goto done;
6935 cmp = got_object_id_cmp(commit_id, stored_id);
6936 free(stored_id);
6937 if (cmp != 0) {
6938 err = got_error(GOT_ERR_REBASE_COMMITID);
6939 goto done;
6942 done:
6943 if (commit_ref)
6944 got_ref_close(commit_ref);
6945 return err;
6948 static const struct got_error *
6949 rebase_merge_files(struct got_pathlist_head *merged_paths,
6950 const char *commit_ref_name, struct got_worktree *worktree,
6951 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6952 struct got_object_id *commit_id, struct got_repository *repo,
6953 got_worktree_checkout_cb progress_cb, void *progress_arg,
6954 got_cancel_cb cancel_cb, void *cancel_arg)
6956 const struct got_error *err;
6957 struct got_reference *commit_ref = NULL;
6958 struct collect_merged_paths_arg cmp_arg;
6959 char *fileindex_path;
6961 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6963 err = get_fileindex_path(&fileindex_path, worktree);
6964 if (err)
6965 return err;
6967 cmp_arg.progress_cb = progress_cb;
6968 cmp_arg.progress_arg = progress_arg;
6969 cmp_arg.merged_paths = merged_paths;
6970 err = merge_files(worktree, fileindex, fileindex_path,
6971 parent_commit_id, commit_id, repo, collect_merged_paths,
6972 &cmp_arg, cancel_cb, cancel_arg);
6973 if (commit_ref)
6974 got_ref_close(commit_ref);
6975 return err;
6978 const struct got_error *
6979 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6980 struct got_worktree *worktree, struct got_fileindex *fileindex,
6981 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6982 struct got_repository *repo,
6983 got_worktree_checkout_cb progress_cb, void *progress_arg,
6984 got_cancel_cb cancel_cb, void *cancel_arg)
6986 const struct got_error *err;
6987 char *commit_ref_name;
6989 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6990 if (err)
6991 return err;
6993 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6994 if (err)
6995 goto done;
6997 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6998 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6999 progress_arg, cancel_cb, cancel_arg);
7000 done:
7001 free(commit_ref_name);
7002 return err;
7005 const struct got_error *
7006 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7007 struct got_worktree *worktree, struct got_fileindex *fileindex,
7008 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7009 struct got_repository *repo,
7010 got_worktree_checkout_cb progress_cb, void *progress_arg,
7011 got_cancel_cb cancel_cb, void *cancel_arg)
7013 const struct got_error *err;
7014 char *commit_ref_name;
7016 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7017 if (err)
7018 return err;
7020 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7021 if (err)
7022 goto done;
7024 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7025 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7026 progress_arg, cancel_cb, cancel_arg);
7027 done:
7028 free(commit_ref_name);
7029 return err;
7032 static const struct got_error *
7033 rebase_commit(struct got_object_id **new_commit_id,
7034 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7035 struct got_worktree *worktree, struct got_fileindex *fileindex,
7036 struct got_reference *tmp_branch, const char *committer,
7037 struct got_commit_object *orig_commit, const char *new_logmsg,
7038 int allow_conflict, struct got_repository *repo)
7040 const struct got_error *err, *sync_err;
7041 struct got_pathlist_head commitable_paths;
7042 struct collect_commitables_arg cc_arg;
7043 char *fileindex_path = NULL;
7044 struct got_reference *head_ref = NULL;
7045 struct got_object_id *head_commit_id = NULL;
7046 char *logmsg = NULL;
7048 memset(&cc_arg, 0, sizeof(cc_arg));
7049 TAILQ_INIT(&commitable_paths);
7050 *new_commit_id = NULL;
7052 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7054 err = get_fileindex_path(&fileindex_path, worktree);
7055 if (err)
7056 return err;
7058 cc_arg.commitable_paths = &commitable_paths;
7059 cc_arg.worktree = worktree;
7060 cc_arg.repo = repo;
7061 cc_arg.have_staged_files = 0;
7062 cc_arg.commit_conflicts = allow_conflict;
7064 * If possible get the status of individual files directly to
7065 * avoid crawling the entire work tree once per rebased commit.
7067 * Ideally, merged_paths would contain a list of commitables
7068 * we could use so we could skip worktree_status() entirely.
7069 * However, we would then need carefully keep track of cumulative
7070 * effects of operations such as file additions and deletions
7071 * in 'got histedit -f' (folding multiple commits into one),
7072 * and this extra complexity is not really worth it.
7074 if (merged_paths) {
7075 struct got_pathlist_entry *pe;
7076 TAILQ_FOREACH(pe, merged_paths, entry) {
7077 err = worktree_status(worktree, pe->path, fileindex,
7078 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7079 0);
7080 if (err)
7081 goto done;
7083 } else {
7084 err = worktree_status(worktree, "", fileindex, repo,
7085 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7086 if (err)
7087 goto done;
7090 if (TAILQ_EMPTY(&commitable_paths)) {
7091 /* No-op change; commit will be elided. */
7092 err = got_ref_delete(commit_ref, repo);
7093 if (err)
7094 goto done;
7095 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7096 goto done;
7099 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7100 if (err)
7101 goto done;
7103 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7104 if (err)
7105 goto done;
7107 if (new_logmsg) {
7108 logmsg = strdup(new_logmsg);
7109 if (logmsg == NULL) {
7110 err = got_error_from_errno("strdup");
7111 goto done;
7113 } else {
7114 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7115 if (err)
7116 goto done;
7119 /* NB: commit_worktree will call free(logmsg) */
7120 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7121 NULL, worktree, got_object_commit_get_author(orig_commit),
7122 committer ? committer :
7123 got_object_commit_get_committer(orig_commit), NULL,
7124 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7125 if (err)
7126 goto done;
7128 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7129 if (err)
7130 goto done;
7132 err = got_ref_delete(commit_ref, repo);
7133 if (err)
7134 goto done;
7136 err = update_fileindex_after_commit(worktree, &commitable_paths,
7137 *new_commit_id, fileindex, 0);
7138 sync_err = sync_fileindex(fileindex, fileindex_path);
7139 if (sync_err && err == NULL)
7140 err = sync_err;
7141 done:
7142 free(fileindex_path);
7143 free(head_commit_id);
7144 if (head_ref)
7145 got_ref_close(head_ref);
7146 if (err) {
7147 free(*new_commit_id);
7148 *new_commit_id = NULL;
7150 return err;
7153 const struct got_error *
7154 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7155 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7156 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7157 const char *committer, struct got_commit_object *orig_commit,
7158 struct got_object_id *orig_commit_id, int allow_conflict,
7159 struct got_repository *repo)
7161 const struct got_error *err;
7162 char *commit_ref_name;
7163 struct got_reference *commit_ref = NULL;
7164 struct got_object_id *commit_id = NULL;
7166 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7167 if (err)
7168 return err;
7170 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7171 if (err)
7172 goto done;
7173 err = got_ref_resolve(&commit_id, repo, commit_ref);
7174 if (err)
7175 goto done;
7176 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7177 err = got_error(GOT_ERR_REBASE_COMMITID);
7178 goto done;
7181 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7182 worktree, fileindex, tmp_branch, committer, orig_commit,
7183 NULL, allow_conflict, repo);
7184 done:
7185 if (commit_ref)
7186 got_ref_close(commit_ref);
7187 free(commit_ref_name);
7188 free(commit_id);
7189 return err;
7192 const struct got_error *
7193 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7194 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7195 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7196 const char *committer, struct got_commit_object *orig_commit,
7197 struct got_object_id *orig_commit_id, const char *new_logmsg,
7198 int allow_conflict, struct got_repository *repo)
7200 const struct got_error *err;
7201 char *commit_ref_name;
7202 struct got_reference *commit_ref = NULL;
7204 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7205 if (err)
7206 return err;
7208 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7209 if (err)
7210 goto done;
7212 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7213 worktree, fileindex, tmp_branch, committer, orig_commit,
7214 new_logmsg, allow_conflict, repo);
7215 done:
7216 if (commit_ref)
7217 got_ref_close(commit_ref);
7218 free(commit_ref_name);
7219 return err;
7222 const struct got_error *
7223 got_worktree_rebase_postpone(struct got_worktree *worktree,
7224 struct got_fileindex *fileindex)
7226 if (fileindex)
7227 got_fileindex_free(fileindex);
7228 return lock_worktree(worktree, LOCK_SH);
7231 static const struct got_error *
7232 delete_ref(const char *name, struct got_repository *repo)
7234 const struct got_error *err;
7235 struct got_reference *ref;
7237 err = got_ref_open(&ref, repo, name, 0);
7238 if (err) {
7239 if (err->code == GOT_ERR_NOT_REF)
7240 return NULL;
7241 return err;
7244 err = got_ref_delete(ref, repo);
7245 got_ref_close(ref);
7246 return err;
7249 static const struct got_error *
7250 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7252 const struct got_error *err;
7253 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7254 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7256 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7257 if (err)
7258 goto done;
7259 err = delete_ref(tmp_branch_name, repo);
7260 if (err)
7261 goto done;
7263 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7264 if (err)
7265 goto done;
7266 err = delete_ref(new_base_branch_ref_name, repo);
7267 if (err)
7268 goto done;
7270 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7271 if (err)
7272 goto done;
7273 err = delete_ref(branch_ref_name, repo);
7274 if (err)
7275 goto done;
7277 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7278 if (err)
7279 goto done;
7280 err = delete_ref(commit_ref_name, repo);
7281 if (err)
7282 goto done;
7284 done:
7285 free(tmp_branch_name);
7286 free(new_base_branch_ref_name);
7287 free(branch_ref_name);
7288 free(commit_ref_name);
7289 return err;
7292 static const struct got_error *
7293 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7294 struct got_object_id *new_commit_id, struct got_repository *repo)
7296 const struct got_error *err;
7297 struct got_reference *ref = NULL;
7298 struct got_object_id *old_commit_id = NULL;
7299 const char *branch_name = NULL;
7300 char *new_id_str = NULL;
7301 char *refname = NULL;
7303 branch_name = got_ref_get_name(branch);
7304 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7305 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7306 branch_name += 11;
7308 err = got_object_id_str(&new_id_str, new_commit_id);
7309 if (err)
7310 return err;
7312 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7313 new_id_str) == -1) {
7314 err = got_error_from_errno("asprintf");
7315 goto done;
7318 err = got_ref_resolve(&old_commit_id, repo, branch);
7319 if (err)
7320 goto done;
7322 err = got_ref_alloc(&ref, refname, old_commit_id);
7323 if (err)
7324 goto done;
7326 err = got_ref_write(ref, repo);
7327 done:
7328 free(new_id_str);
7329 free(refname);
7330 free(old_commit_id);
7331 if (ref)
7332 got_ref_close(ref);
7333 return err;
7336 const struct got_error *
7337 got_worktree_rebase_complete(struct got_worktree *worktree,
7338 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7339 struct got_reference *rebased_branch, struct got_repository *repo,
7340 int create_backup)
7342 const struct got_error *err, *unlockerr, *sync_err;
7343 struct got_object_id *new_head_commit_id = NULL;
7344 char *fileindex_path = NULL;
7346 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7347 if (err)
7348 return err;
7350 if (create_backup) {
7351 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7352 rebased_branch, new_head_commit_id, repo);
7353 if (err)
7354 goto done;
7357 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7358 if (err)
7359 goto done;
7361 err = got_ref_write(rebased_branch, repo);
7362 if (err)
7363 goto done;
7365 err = got_worktree_set_head_ref(worktree, rebased_branch);
7366 if (err)
7367 goto done;
7369 err = delete_rebase_refs(worktree, repo);
7370 if (err)
7371 goto done;
7373 err = get_fileindex_path(&fileindex_path, worktree);
7374 if (err)
7375 goto done;
7376 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7377 sync_err = sync_fileindex(fileindex, fileindex_path);
7378 if (sync_err && err == NULL)
7379 err = sync_err;
7380 done:
7381 got_fileindex_free(fileindex);
7382 free(fileindex_path);
7383 free(new_head_commit_id);
7384 unlockerr = lock_worktree(worktree, LOCK_SH);
7385 if (unlockerr && err == NULL)
7386 err = unlockerr;
7387 return err;
7390 static const struct got_error *
7391 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7392 struct got_object_id *id1, struct got_object_id *id2,
7393 struct got_repository *repo)
7395 const struct got_error *err;
7396 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7397 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7399 if (id1) {
7400 err = got_object_open_as_commit(&commit1, repo, id1);
7401 if (err)
7402 goto done;
7404 err = got_object_open_as_tree(&tree1, repo,
7405 got_object_commit_get_tree_id(commit1));
7406 if (err)
7407 goto done;
7410 if (id2) {
7411 err = got_object_open_as_commit(&commit2, repo, id2);
7412 if (err)
7413 goto done;
7415 err = got_object_open_as_tree(&tree2, repo,
7416 got_object_commit_get_tree_id(commit2));
7417 if (err)
7418 goto done;
7421 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7422 got_diff_tree_collect_changed_paths, paths, 0);
7423 if (err)
7424 goto done;
7425 done:
7426 if (commit1)
7427 got_object_commit_close(commit1);
7428 if (commit2)
7429 got_object_commit_close(commit2);
7430 if (tree1)
7431 got_object_tree_close(tree1);
7432 if (tree2)
7433 got_object_tree_close(tree2);
7434 return err;
7437 static const struct got_error *
7438 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7439 struct got_object_id *id1, struct got_object_id *id2,
7440 const char *path_prefix, struct got_repository *repo)
7442 const struct got_error *err;
7443 struct got_pathlist_head merged_paths;
7444 struct got_pathlist_entry *pe;
7445 char *abspath = NULL, *wt_path = NULL;
7447 TAILQ_INIT(&merged_paths);
7449 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7450 if (err)
7451 goto done;
7453 TAILQ_FOREACH(pe, &merged_paths, entry) {
7454 struct got_diff_changed_path *change = pe->data;
7456 if (change->status != GOT_STATUS_ADD)
7457 continue;
7459 if (got_path_is_root_dir(path_prefix)) {
7460 wt_path = strdup(pe->path);
7461 if (wt_path == NULL) {
7462 err = got_error_from_errno("strdup");
7463 goto done;
7465 } else {
7466 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7467 err = got_error_from_errno("asprintf");
7468 goto done;
7471 err = got_path_skip_common_ancestor(&wt_path,
7472 path_prefix, abspath);
7473 if (err)
7474 goto done;
7475 free(abspath);
7476 abspath = NULL;
7479 err = got_pathlist_append(added_paths, wt_path, NULL);
7480 if (err)
7481 goto done;
7482 wt_path = NULL;
7485 done:
7486 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7487 free(abspath);
7488 free(wt_path);
7489 return err;
7492 static const struct got_error *
7493 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7494 struct got_object_id *id, const char *path_prefix,
7495 struct got_repository *repo)
7497 const struct got_error *err;
7498 struct got_commit_object *commit = NULL;
7499 struct got_object_qid *pid;
7501 err = got_object_open_as_commit(&commit, repo, id);
7502 if (err)
7503 goto done;
7505 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7507 err = get_paths_added_between_commits(added_paths,
7508 pid ? &pid->id : NULL, id, path_prefix, repo);
7509 if (err)
7510 goto done;
7511 done:
7512 if (commit)
7513 got_object_commit_close(commit);
7514 return err;
7517 const struct got_error *
7518 got_worktree_rebase_abort(struct got_worktree *worktree,
7519 struct got_fileindex *fileindex, struct got_repository *repo,
7520 struct got_reference *new_base_branch,
7521 got_worktree_checkout_cb progress_cb, void *progress_arg)
7523 const struct got_error *err, *unlockerr, *sync_err;
7524 struct got_reference *resolved = NULL;
7525 struct got_object_id *commit_id = NULL;
7526 struct got_object_id *merged_commit_id = NULL;
7527 struct got_commit_object *commit = NULL;
7528 char *fileindex_path = NULL;
7529 char *commit_ref_name = NULL;
7530 struct got_reference *commit_ref = NULL;
7531 struct revert_file_args rfa;
7532 struct got_object_id *tree_id = NULL;
7533 struct got_pathlist_head added_paths;
7535 TAILQ_INIT(&added_paths);
7537 err = lock_worktree(worktree, LOCK_EX);
7538 if (err)
7539 return err;
7541 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7542 if (err)
7543 goto done;
7545 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7546 if (err)
7547 goto done;
7549 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7550 if (err)
7551 goto done;
7554 * Determine which files in added status can be safely removed
7555 * from disk while reverting changes in the work tree.
7556 * We want to avoid deleting unrelated files which were added by
7557 * the user for conflict resolution purposes.
7559 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7560 got_worktree_get_path_prefix(worktree), repo);
7561 if (err)
7562 goto done;
7564 err = got_ref_open(&resolved, repo,
7565 got_ref_get_symref_target(new_base_branch), 0);
7566 if (err)
7567 goto done;
7569 err = got_worktree_set_head_ref(worktree, resolved);
7570 if (err)
7571 goto done;
7574 * XXX commits to the base branch could have happened while
7575 * we were busy rebasing; should we store the original commit ID
7576 * when rebase begins and read it back here?
7578 err = got_ref_resolve(&commit_id, repo, resolved);
7579 if (err)
7580 goto done;
7582 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7583 if (err)
7584 goto done;
7586 err = got_object_open_as_commit(&commit, repo,
7587 worktree->base_commit_id);
7588 if (err)
7589 goto done;
7591 err = got_object_id_by_path(&tree_id, repo, commit,
7592 worktree->path_prefix);
7593 if (err)
7594 goto done;
7596 err = delete_rebase_refs(worktree, repo);
7597 if (err)
7598 goto done;
7600 err = get_fileindex_path(&fileindex_path, worktree);
7601 if (err)
7602 goto done;
7604 rfa.worktree = worktree;
7605 rfa.fileindex = fileindex;
7606 rfa.progress_cb = progress_cb;
7607 rfa.progress_arg = progress_arg;
7608 rfa.patch_cb = NULL;
7609 rfa.patch_arg = NULL;
7610 rfa.repo = repo;
7611 rfa.unlink_added_files = 1;
7612 rfa.added_files_to_unlink = &added_paths;
7613 err = worktree_status(worktree, "", fileindex, repo,
7614 revert_file, &rfa, NULL, NULL, 1, 0);
7615 if (err)
7616 goto sync;
7618 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7619 repo, progress_cb, progress_arg, NULL, NULL);
7620 sync:
7621 sync_err = sync_fileindex(fileindex, fileindex_path);
7622 if (sync_err && err == NULL)
7623 err = sync_err;
7624 done:
7625 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7626 got_ref_close(resolved);
7627 free(tree_id);
7628 free(commit_id);
7629 free(merged_commit_id);
7630 if (commit)
7631 got_object_commit_close(commit);
7632 if (fileindex)
7633 got_fileindex_free(fileindex);
7634 free(fileindex_path);
7635 free(commit_ref_name);
7636 if (commit_ref)
7637 got_ref_close(commit_ref);
7639 unlockerr = lock_worktree(worktree, LOCK_SH);
7640 if (unlockerr && err == NULL)
7641 err = unlockerr;
7642 return err;
7645 const struct got_error *
7646 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7647 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7648 struct got_fileindex **fileindex, struct got_worktree *worktree,
7649 struct got_repository *repo)
7651 const struct got_error *err = NULL;
7652 char *tmp_branch_name = NULL;
7653 char *branch_ref_name = NULL;
7654 char *base_commit_ref_name = NULL;
7655 char *fileindex_path = NULL;
7656 struct check_rebase_ok_arg ok_arg;
7657 struct got_reference *wt_branch = NULL;
7658 struct got_reference *base_commit_ref = NULL;
7660 *tmp_branch = NULL;
7661 *branch_ref = NULL;
7662 *base_commit_id = NULL;
7663 *fileindex = NULL;
7665 err = lock_worktree(worktree, LOCK_EX);
7666 if (err)
7667 return err;
7669 err = open_fileindex(fileindex, &fileindex_path, worktree);
7670 if (err)
7671 goto done;
7673 ok_arg.worktree = worktree;
7674 ok_arg.repo = repo;
7675 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7676 &ok_arg);
7677 if (err)
7678 goto done;
7680 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7681 if (err)
7682 goto done;
7684 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7685 if (err)
7686 goto done;
7688 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7689 worktree);
7690 if (err)
7691 goto done;
7693 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7694 0);
7695 if (err)
7696 goto done;
7698 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7699 if (err)
7700 goto done;
7702 err = got_ref_write(*branch_ref, repo);
7703 if (err)
7704 goto done;
7706 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7707 worktree->base_commit_id);
7708 if (err)
7709 goto done;
7710 err = got_ref_write(base_commit_ref, repo);
7711 if (err)
7712 goto done;
7713 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7714 if (*base_commit_id == NULL) {
7715 err = got_error_from_errno("got_object_id_dup");
7716 goto done;
7719 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7720 worktree->base_commit_id);
7721 if (err)
7722 goto done;
7723 err = got_ref_write(*tmp_branch, repo);
7724 if (err)
7725 goto done;
7727 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7728 if (err)
7729 goto done;
7730 done:
7731 free(fileindex_path);
7732 free(tmp_branch_name);
7733 free(branch_ref_name);
7734 free(base_commit_ref_name);
7735 if (wt_branch)
7736 got_ref_close(wt_branch);
7737 if (err) {
7738 if (*branch_ref) {
7739 got_ref_close(*branch_ref);
7740 *branch_ref = NULL;
7742 if (*tmp_branch) {
7743 got_ref_close(*tmp_branch);
7744 *tmp_branch = NULL;
7746 free(*base_commit_id);
7747 if (*fileindex) {
7748 got_fileindex_free(*fileindex);
7749 *fileindex = NULL;
7751 lock_worktree(worktree, LOCK_SH);
7753 return err;
7756 const struct got_error *
7757 got_worktree_histedit_postpone(struct got_worktree *worktree,
7758 struct got_fileindex *fileindex)
7760 if (fileindex)
7761 got_fileindex_free(fileindex);
7762 return lock_worktree(worktree, LOCK_SH);
7765 const struct got_error *
7766 got_worktree_histedit_in_progress(int *in_progress,
7767 struct got_worktree *worktree)
7769 const struct got_error *err;
7770 char *tmp_branch_name = NULL;
7772 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7773 if (err)
7774 return err;
7776 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7777 free(tmp_branch_name);
7778 return NULL;
7781 const struct got_error *
7782 got_worktree_histedit_continue(struct got_object_id **commit_id,
7783 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7784 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7785 struct got_worktree *worktree, struct got_repository *repo)
7787 const struct got_error *err;
7788 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7789 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7790 struct got_reference *commit_ref = NULL;
7791 struct got_reference *base_commit_ref = NULL;
7792 char *fileindex_path = NULL;
7793 int have_staged_files = 0;
7795 *commit_id = NULL;
7796 *tmp_branch = NULL;
7797 *base_commit_id = NULL;
7798 *fileindex = NULL;
7800 err = lock_worktree(worktree, LOCK_EX);
7801 if (err)
7802 return err;
7804 err = open_fileindex(fileindex, &fileindex_path, worktree);
7805 if (err)
7806 goto done;
7808 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7809 &have_staged_files);
7810 if (err && err->code != GOT_ERR_CANCELLED)
7811 goto done;
7812 if (have_staged_files) {
7813 err = got_error(GOT_ERR_STAGED_PATHS);
7814 goto done;
7817 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7818 if (err)
7819 goto done;
7821 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7822 if (err)
7823 goto done;
7825 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7826 if (err)
7827 goto done;
7829 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7830 worktree);
7831 if (err)
7832 goto done;
7834 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7835 if (err)
7836 goto done;
7838 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7839 if (err)
7840 goto done;
7841 err = got_ref_resolve(commit_id, repo, commit_ref);
7842 if (err)
7843 goto done;
7845 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7846 if (err)
7847 goto done;
7848 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7849 if (err)
7850 goto done;
7852 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7853 if (err)
7854 goto done;
7855 done:
7856 free(commit_ref_name);
7857 free(branch_ref_name);
7858 free(fileindex_path);
7859 if (commit_ref)
7860 got_ref_close(commit_ref);
7861 if (base_commit_ref)
7862 got_ref_close(base_commit_ref);
7863 if (err) {
7864 free(*commit_id);
7865 *commit_id = NULL;
7866 free(*base_commit_id);
7867 *base_commit_id = NULL;
7868 if (*tmp_branch) {
7869 got_ref_close(*tmp_branch);
7870 *tmp_branch = NULL;
7872 if (*fileindex) {
7873 got_fileindex_free(*fileindex);
7874 *fileindex = NULL;
7876 lock_worktree(worktree, LOCK_EX);
7878 return err;
7881 static const struct got_error *
7882 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7884 const struct got_error *err;
7885 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7886 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7888 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7889 if (err)
7890 goto done;
7891 err = delete_ref(tmp_branch_name, repo);
7892 if (err)
7893 goto done;
7895 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7896 worktree);
7897 if (err)
7898 goto done;
7899 err = delete_ref(base_commit_ref_name, repo);
7900 if (err)
7901 goto done;
7903 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7904 if (err)
7905 goto done;
7906 err = delete_ref(branch_ref_name, repo);
7907 if (err)
7908 goto done;
7910 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7911 if (err)
7912 goto done;
7913 err = delete_ref(commit_ref_name, repo);
7914 if (err)
7915 goto done;
7916 done:
7917 free(tmp_branch_name);
7918 free(base_commit_ref_name);
7919 free(branch_ref_name);
7920 free(commit_ref_name);
7921 return err;
7924 const struct got_error *
7925 got_worktree_histedit_abort(struct got_worktree *worktree,
7926 struct got_fileindex *fileindex, struct got_repository *repo,
7927 struct got_reference *branch, struct got_object_id *base_commit_id,
7928 got_worktree_checkout_cb progress_cb, void *progress_arg)
7930 const struct got_error *err, *unlockerr, *sync_err;
7931 struct got_reference *resolved = NULL;
7932 char *fileindex_path = NULL;
7933 struct got_object_id *merged_commit_id = NULL;
7934 struct got_commit_object *commit = NULL;
7935 char *commit_ref_name = NULL;
7936 struct got_reference *commit_ref = NULL;
7937 struct got_object_id *tree_id = NULL;
7938 struct revert_file_args rfa;
7939 struct got_pathlist_head added_paths;
7941 TAILQ_INIT(&added_paths);
7943 err = lock_worktree(worktree, LOCK_EX);
7944 if (err)
7945 return err;
7947 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7948 if (err)
7949 goto done;
7951 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7952 if (err) {
7953 if (err->code != GOT_ERR_NOT_REF)
7954 goto done;
7955 /* Can happen on early abort due to invalid histedit script. */
7956 commit_ref = NULL;
7959 if (commit_ref) {
7960 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7961 if (err)
7962 goto done;
7965 * Determine which files in added status can be safely removed
7966 * from disk while reverting changes in the work tree.
7967 * We want to avoid deleting unrelated files added by the
7968 * user during conflict resolution or during histedit -e.
7970 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7971 got_worktree_get_path_prefix(worktree), repo);
7972 if (err)
7973 goto done;
7976 err = got_ref_open(&resolved, repo,
7977 got_ref_get_symref_target(branch), 0);
7978 if (err)
7979 goto done;
7981 err = got_worktree_set_head_ref(worktree, resolved);
7982 if (err)
7983 goto done;
7985 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7986 if (err)
7987 goto done;
7989 err = got_object_open_as_commit(&commit, repo,
7990 worktree->base_commit_id);
7991 if (err)
7992 goto done;
7994 err = got_object_id_by_path(&tree_id, repo, commit,
7995 worktree->path_prefix);
7996 if (err)
7997 goto done;
7999 err = delete_histedit_refs(worktree, repo);
8000 if (err)
8001 goto done;
8003 err = get_fileindex_path(&fileindex_path, worktree);
8004 if (err)
8005 goto done;
8007 rfa.worktree = worktree;
8008 rfa.fileindex = fileindex;
8009 rfa.progress_cb = progress_cb;
8010 rfa.progress_arg = progress_arg;
8011 rfa.patch_cb = NULL;
8012 rfa.patch_arg = NULL;
8013 rfa.repo = repo;
8014 rfa.unlink_added_files = 1;
8015 rfa.added_files_to_unlink = &added_paths;
8016 err = worktree_status(worktree, "", fileindex, repo,
8017 revert_file, &rfa, NULL, NULL, 1, 0);
8018 if (err)
8019 goto sync;
8021 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8022 repo, progress_cb, progress_arg, NULL, NULL);
8023 sync:
8024 sync_err = sync_fileindex(fileindex, fileindex_path);
8025 if (sync_err && err == NULL)
8026 err = sync_err;
8027 done:
8028 if (resolved)
8029 got_ref_close(resolved);
8030 if (commit_ref)
8031 got_ref_close(commit_ref);
8032 free(merged_commit_id);
8033 free(tree_id);
8034 free(fileindex_path);
8035 free(commit_ref_name);
8037 unlockerr = lock_worktree(worktree, LOCK_SH);
8038 if (unlockerr && err == NULL)
8039 err = unlockerr;
8040 return err;
8043 const struct got_error *
8044 got_worktree_histedit_complete(struct got_worktree *worktree,
8045 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8046 struct got_reference *edited_branch, struct got_repository *repo)
8048 const struct got_error *err, *unlockerr, *sync_err;
8049 struct got_object_id *new_head_commit_id = NULL;
8050 struct got_reference *resolved = NULL;
8051 char *fileindex_path = NULL;
8053 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8054 if (err)
8055 return err;
8057 err = got_ref_open(&resolved, repo,
8058 got_ref_get_symref_target(edited_branch), 0);
8059 if (err)
8060 goto done;
8062 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8063 resolved, new_head_commit_id, repo);
8064 if (err)
8065 goto done;
8067 err = got_ref_change_ref(resolved, new_head_commit_id);
8068 if (err)
8069 goto done;
8071 err = got_ref_write(resolved, repo);
8072 if (err)
8073 goto done;
8075 err = got_worktree_set_head_ref(worktree, resolved);
8076 if (err)
8077 goto done;
8079 err = delete_histedit_refs(worktree, repo);
8080 if (err)
8081 goto done;
8083 err = get_fileindex_path(&fileindex_path, worktree);
8084 if (err)
8085 goto done;
8086 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8087 sync_err = sync_fileindex(fileindex, fileindex_path);
8088 if (sync_err && err == NULL)
8089 err = sync_err;
8090 done:
8091 got_fileindex_free(fileindex);
8092 free(fileindex_path);
8093 free(new_head_commit_id);
8094 unlockerr = lock_worktree(worktree, LOCK_SH);
8095 if (unlockerr && err == NULL)
8096 err = unlockerr;
8097 return err;
8100 const struct got_error *
8101 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8102 struct got_object_id *commit_id, struct got_repository *repo)
8104 const struct got_error *err;
8105 char *commit_ref_name;
8107 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8108 if (err)
8109 return err;
8111 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8112 if (err)
8113 goto done;
8115 err = delete_ref(commit_ref_name, repo);
8116 done:
8117 free(commit_ref_name);
8118 return err;
8121 const struct got_error *
8122 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8123 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8124 struct got_worktree *worktree, const char *refname,
8125 struct got_repository *repo)
8127 const struct got_error *err = NULL;
8128 char *fileindex_path = NULL;
8129 struct check_rebase_ok_arg ok_arg;
8131 *fileindex = NULL;
8132 *branch_ref = NULL;
8133 *base_branch_ref = NULL;
8135 err = lock_worktree(worktree, LOCK_EX);
8136 if (err)
8137 return err;
8139 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8140 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8141 "cannot integrate a branch into itself; "
8142 "update -b or different branch name required");
8143 goto done;
8146 err = open_fileindex(fileindex, &fileindex_path, worktree);
8147 if (err)
8148 goto done;
8150 /* Preconditions are the same as for rebase. */
8151 ok_arg.worktree = worktree;
8152 ok_arg.repo = repo;
8153 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8154 &ok_arg);
8155 if (err)
8156 goto done;
8158 err = got_ref_open(branch_ref, repo, refname, 1);
8159 if (err)
8160 goto done;
8162 err = got_ref_open(base_branch_ref, repo,
8163 got_worktree_get_head_ref_name(worktree), 1);
8164 done:
8165 if (err) {
8166 if (*branch_ref) {
8167 got_ref_close(*branch_ref);
8168 *branch_ref = NULL;
8170 if (*base_branch_ref) {
8171 got_ref_close(*base_branch_ref);
8172 *base_branch_ref = NULL;
8174 if (*fileindex) {
8175 got_fileindex_free(*fileindex);
8176 *fileindex = NULL;
8178 lock_worktree(worktree, LOCK_SH);
8180 return err;
8183 const struct got_error *
8184 got_worktree_integrate_continue(struct got_worktree *worktree,
8185 struct got_fileindex *fileindex, struct got_repository *repo,
8186 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8187 got_worktree_checkout_cb progress_cb, void *progress_arg,
8188 got_cancel_cb cancel_cb, void *cancel_arg)
8190 const struct got_error *err = NULL, *sync_err, *unlockerr;
8191 char *fileindex_path = NULL;
8192 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8193 struct got_commit_object *commit = NULL;
8195 err = get_fileindex_path(&fileindex_path, worktree);
8196 if (err)
8197 goto done;
8199 err = got_ref_resolve(&commit_id, repo, branch_ref);
8200 if (err)
8201 goto done;
8203 err = got_object_open_as_commit(&commit, repo, commit_id);
8204 if (err)
8205 goto done;
8207 err = got_object_id_by_path(&tree_id, repo, commit,
8208 worktree->path_prefix);
8209 if (err)
8210 goto done;
8212 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8213 if (err)
8214 goto done;
8216 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8217 progress_cb, progress_arg, cancel_cb, cancel_arg);
8218 if (err)
8219 goto sync;
8221 err = got_ref_change_ref(base_branch_ref, commit_id);
8222 if (err)
8223 goto sync;
8225 err = got_ref_write(base_branch_ref, repo);
8226 if (err)
8227 goto sync;
8229 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8230 sync:
8231 sync_err = sync_fileindex(fileindex, fileindex_path);
8232 if (sync_err && err == NULL)
8233 err = sync_err;
8235 done:
8236 unlockerr = got_ref_unlock(branch_ref);
8237 if (unlockerr && err == NULL)
8238 err = unlockerr;
8239 got_ref_close(branch_ref);
8241 unlockerr = got_ref_unlock(base_branch_ref);
8242 if (unlockerr && err == NULL)
8243 err = unlockerr;
8244 got_ref_close(base_branch_ref);
8246 got_fileindex_free(fileindex);
8247 free(fileindex_path);
8248 free(tree_id);
8249 if (commit)
8250 got_object_commit_close(commit);
8252 unlockerr = lock_worktree(worktree, LOCK_SH);
8253 if (unlockerr && err == NULL)
8254 err = unlockerr;
8255 return err;
8258 const struct got_error *
8259 got_worktree_integrate_abort(struct got_worktree *worktree,
8260 struct got_fileindex *fileindex, struct got_repository *repo,
8261 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8263 const struct got_error *err = NULL, *unlockerr = NULL;
8265 got_fileindex_free(fileindex);
8267 err = lock_worktree(worktree, LOCK_SH);
8269 unlockerr = got_ref_unlock(branch_ref);
8270 if (unlockerr && err == NULL)
8271 err = unlockerr;
8272 got_ref_close(branch_ref);
8274 unlockerr = got_ref_unlock(base_branch_ref);
8275 if (unlockerr && err == NULL)
8276 err = unlockerr;
8277 got_ref_close(base_branch_ref);
8279 return err;
8282 const struct got_error *
8283 got_worktree_merge_postpone(struct got_worktree *worktree,
8284 struct got_fileindex *fileindex)
8286 const struct got_error *err, *sync_err;
8287 char *fileindex_path = NULL;
8289 err = get_fileindex_path(&fileindex_path, worktree);
8290 if (err)
8291 goto done;
8293 sync_err = sync_fileindex(fileindex, fileindex_path);
8295 err = lock_worktree(worktree, LOCK_SH);
8296 if (sync_err && err == NULL)
8297 err = sync_err;
8298 done:
8299 got_fileindex_free(fileindex);
8300 free(fileindex_path);
8301 return err;
8304 static const struct got_error *
8305 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8307 const struct got_error *err;
8308 char *branch_refname = NULL, *commit_refname = NULL;
8310 err = get_merge_branch_ref_name(&branch_refname, worktree);
8311 if (err)
8312 goto done;
8313 err = delete_ref(branch_refname, repo);
8314 if (err)
8315 goto done;
8317 err = get_merge_commit_ref_name(&commit_refname, worktree);
8318 if (err)
8319 goto done;
8320 err = delete_ref(commit_refname, repo);
8321 if (err)
8322 goto done;
8324 done:
8325 free(branch_refname);
8326 free(commit_refname);
8327 return err;
8330 struct merge_commit_msg_arg {
8331 struct got_worktree *worktree;
8332 const char *branch_name;
8335 static const struct got_error *
8336 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8337 const char *diff_path, char **logmsg, void *arg)
8339 struct merge_commit_msg_arg *a = arg;
8341 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8342 got_worktree_get_head_ref_name(a->worktree)) == -1)
8343 return got_error_from_errno("asprintf");
8345 return NULL;
8349 const struct got_error *
8350 got_worktree_merge_branch(struct got_worktree *worktree,
8351 struct got_fileindex *fileindex,
8352 struct got_object_id *yca_commit_id,
8353 struct got_object_id *branch_tip,
8354 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8355 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8357 const struct got_error *err;
8358 char *fileindex_path = NULL;
8360 err = get_fileindex_path(&fileindex_path, worktree);
8361 if (err)
8362 goto done;
8364 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8365 worktree);
8366 if (err)
8367 goto done;
8369 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8370 branch_tip, repo, progress_cb, progress_arg,
8371 cancel_cb, cancel_arg);
8372 done:
8373 free(fileindex_path);
8374 return err;
8377 const struct got_error *
8378 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8379 struct got_worktree *worktree, struct got_fileindex *fileindex,
8380 const char *author, const char *committer, int allow_bad_symlinks,
8381 struct got_object_id *branch_tip, const char *branch_name,
8382 int allow_conflict, struct got_repository *repo,
8383 got_worktree_status_cb status_cb, void *status_arg)
8386 const struct got_error *err = NULL, *sync_err;
8387 struct got_pathlist_head commitable_paths;
8388 struct collect_commitables_arg cc_arg;
8389 struct got_pathlist_entry *pe;
8390 struct got_reference *head_ref = NULL;
8391 struct got_object_id *head_commit_id = NULL;
8392 int have_staged_files = 0;
8393 struct merge_commit_msg_arg mcm_arg;
8394 char *fileindex_path = NULL;
8396 memset(&cc_arg, 0, sizeof(cc_arg));
8397 *new_commit_id = NULL;
8399 TAILQ_INIT(&commitable_paths);
8401 err = get_fileindex_path(&fileindex_path, worktree);
8402 if (err)
8403 goto done;
8405 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8406 if (err)
8407 goto done;
8409 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8410 if (err)
8411 goto done;
8413 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8414 &have_staged_files);
8415 if (err && err->code != GOT_ERR_CANCELLED)
8416 goto done;
8417 if (have_staged_files) {
8418 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8419 goto done;
8422 cc_arg.commitable_paths = &commitable_paths;
8423 cc_arg.worktree = worktree;
8424 cc_arg.fileindex = fileindex;
8425 cc_arg.repo = repo;
8426 cc_arg.have_staged_files = have_staged_files;
8427 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8428 cc_arg.commit_conflicts = allow_conflict;
8429 err = worktree_status(worktree, "", fileindex, repo,
8430 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8431 if (err)
8432 goto done;
8434 mcm_arg.worktree = worktree;
8435 mcm_arg.branch_name = branch_name;
8436 err = commit_worktree(new_commit_id, &commitable_paths,
8437 head_commit_id, branch_tip, worktree, author, committer, NULL,
8438 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8439 if (err)
8440 goto done;
8442 err = update_fileindex_after_commit(worktree, &commitable_paths,
8443 *new_commit_id, fileindex, have_staged_files);
8444 sync_err = sync_fileindex(fileindex, fileindex_path);
8445 if (sync_err && err == NULL)
8446 err = sync_err;
8447 done:
8448 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8449 struct got_commitable *ct = pe->data;
8451 free_commitable(ct);
8453 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8454 free(fileindex_path);
8455 return err;
8458 const struct got_error *
8459 got_worktree_merge_complete(struct got_worktree *worktree,
8460 struct got_fileindex *fileindex, struct got_repository *repo)
8462 const struct got_error *err, *unlockerr, *sync_err;
8463 char *fileindex_path = NULL;
8465 err = delete_merge_refs(worktree, repo);
8466 if (err)
8467 goto done;
8469 err = get_fileindex_path(&fileindex_path, worktree);
8470 if (err)
8471 goto done;
8472 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8473 sync_err = sync_fileindex(fileindex, fileindex_path);
8474 if (sync_err && err == NULL)
8475 err = sync_err;
8476 done:
8477 got_fileindex_free(fileindex);
8478 free(fileindex_path);
8479 unlockerr = lock_worktree(worktree, LOCK_SH);
8480 if (unlockerr && err == NULL)
8481 err = unlockerr;
8482 return err;
8485 const struct got_error *
8486 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8487 struct got_repository *repo)
8489 const struct got_error *err;
8490 char *branch_refname = NULL;
8491 struct got_reference *branch_ref = NULL;
8493 *in_progress = 0;
8495 err = get_merge_branch_ref_name(&branch_refname, worktree);
8496 if (err)
8497 return err;
8498 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8499 free(branch_refname);
8500 if (err) {
8501 if (err->code != GOT_ERR_NOT_REF)
8502 return err;
8503 } else
8504 *in_progress = 1;
8506 return NULL;
8509 const struct got_error *got_worktree_merge_prepare(
8510 struct got_fileindex **fileindex, struct got_worktree *worktree,
8511 struct got_repository *repo)
8513 const struct got_error *err = NULL;
8514 char *fileindex_path = NULL;
8515 struct got_reference *wt_branch = NULL;
8516 struct got_object_id *wt_branch_tip = NULL;
8517 struct check_rebase_ok_arg ok_arg;
8519 *fileindex = NULL;
8521 err = lock_worktree(worktree, LOCK_EX);
8522 if (err)
8523 return err;
8525 err = open_fileindex(fileindex, &fileindex_path, worktree);
8526 if (err)
8527 goto done;
8529 /* Preconditions are the same as for rebase. */
8530 ok_arg.worktree = worktree;
8531 ok_arg.repo = repo;
8532 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8533 &ok_arg);
8534 if (err)
8535 goto done;
8537 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8538 0);
8539 if (err)
8540 goto done;
8542 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8543 if (err)
8544 goto done;
8546 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8547 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8548 goto done;
8551 done:
8552 free(fileindex_path);
8553 if (wt_branch)
8554 got_ref_close(wt_branch);
8555 free(wt_branch_tip);
8556 if (err) {
8557 if (*fileindex) {
8558 got_fileindex_free(*fileindex);
8559 *fileindex = NULL;
8561 lock_worktree(worktree, LOCK_SH);
8563 return err;
8566 const struct got_error *got_worktree_merge_write_refs(
8567 struct got_worktree *worktree, struct got_reference *branch,
8568 struct got_repository *repo)
8570 const struct got_error *err = NULL;
8571 char *branch_refname = NULL, *commit_refname = NULL;
8572 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8573 struct got_object_id *branch_tip = NULL;
8575 err = get_merge_branch_ref_name(&branch_refname, worktree);
8576 if (err)
8577 return err;
8579 err = get_merge_commit_ref_name(&commit_refname, worktree);
8580 if (err)
8581 return err;
8583 err = got_ref_resolve(&branch_tip, repo, branch);
8584 if (err)
8585 goto done;
8587 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8588 if (err)
8589 goto done;
8590 err = got_ref_write(branch_ref, repo);
8591 if (err)
8592 goto done;
8594 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8595 if (err)
8596 goto done;
8597 err = got_ref_write(commit_ref, repo);
8598 if (err)
8599 goto done;
8601 done:
8602 free(branch_refname);
8603 free(commit_refname);
8604 if (branch_ref)
8605 got_ref_close(branch_ref);
8606 if (commit_ref)
8607 got_ref_close(commit_ref);
8608 free(branch_tip);
8609 return err;
8612 const struct got_error *
8613 got_worktree_merge_continue(char **branch_name,
8614 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8615 struct got_worktree *worktree, struct got_repository *repo)
8617 const struct got_error *err;
8618 char *commit_refname = NULL, *branch_refname = NULL;
8619 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8620 char *fileindex_path = NULL;
8621 int have_staged_files = 0;
8623 *branch_name = NULL;
8624 *branch_tip = NULL;
8625 *fileindex = NULL;
8627 err = lock_worktree(worktree, LOCK_EX);
8628 if (err)
8629 return err;
8631 err = open_fileindex(fileindex, &fileindex_path, worktree);
8632 if (err)
8633 goto done;
8635 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8636 &have_staged_files);
8637 if (err && err->code != GOT_ERR_CANCELLED)
8638 goto done;
8639 if (have_staged_files) {
8640 err = got_error(GOT_ERR_STAGED_PATHS);
8641 goto done;
8644 err = get_merge_branch_ref_name(&branch_refname, worktree);
8645 if (err)
8646 goto done;
8648 err = get_merge_commit_ref_name(&commit_refname, worktree);
8649 if (err)
8650 goto done;
8652 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8653 if (err)
8654 goto done;
8656 if (!got_ref_is_symbolic(branch_ref)) {
8657 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8658 "%s is not a symbolic reference",
8659 got_ref_get_name(branch_ref));
8660 goto done;
8662 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8663 if (*branch_name == NULL) {
8664 err = got_error_from_errno("strdup");
8665 goto done;
8668 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8669 if (err)
8670 goto done;
8672 err = got_ref_resolve(branch_tip, repo, commit_ref);
8673 if (err)
8674 goto done;
8675 done:
8676 free(commit_refname);
8677 free(branch_refname);
8678 free(fileindex_path);
8679 if (commit_ref)
8680 got_ref_close(commit_ref);
8681 if (branch_ref)
8682 got_ref_close(branch_ref);
8683 if (err) {
8684 if (*branch_name) {
8685 free(*branch_name);
8686 *branch_name = NULL;
8688 free(*branch_tip);
8689 *branch_tip = NULL;
8690 if (*fileindex) {
8691 got_fileindex_free(*fileindex);
8692 *fileindex = NULL;
8694 lock_worktree(worktree, LOCK_SH);
8696 return err;
8699 const struct got_error *
8700 got_worktree_merge_abort(struct got_worktree *worktree,
8701 struct got_fileindex *fileindex, struct got_repository *repo,
8702 got_worktree_checkout_cb progress_cb, void *progress_arg)
8704 const struct got_error *err, *unlockerr, *sync_err;
8705 struct got_commit_object *commit = NULL;
8706 char *fileindex_path = NULL;
8707 struct revert_file_args rfa;
8708 char *commit_ref_name = NULL;
8709 struct got_reference *commit_ref = NULL;
8710 struct got_object_id *merged_commit_id = NULL;
8711 struct got_object_id *tree_id = NULL;
8712 struct got_pathlist_head added_paths;
8714 TAILQ_INIT(&added_paths);
8716 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8717 if (err)
8718 goto done;
8720 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8721 if (err)
8722 goto done;
8724 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8725 if (err)
8726 goto done;
8729 * Determine which files in added status can be safely removed
8730 * from disk while reverting changes in the work tree.
8731 * We want to avoid deleting unrelated files which were added by
8732 * the user for conflict resolution purposes.
8734 err = get_paths_added_between_commits(&added_paths,
8735 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8736 got_worktree_get_path_prefix(worktree), repo);
8737 if (err)
8738 goto done;
8741 err = got_object_open_as_commit(&commit, repo,
8742 worktree->base_commit_id);
8743 if (err)
8744 goto done;
8746 err = got_object_id_by_path(&tree_id, repo, commit,
8747 worktree->path_prefix);
8748 if (err)
8749 goto done;
8751 err = delete_merge_refs(worktree, repo);
8752 if (err)
8753 goto done;
8755 err = get_fileindex_path(&fileindex_path, worktree);
8756 if (err)
8757 goto done;
8759 rfa.worktree = worktree;
8760 rfa.fileindex = fileindex;
8761 rfa.progress_cb = progress_cb;
8762 rfa.progress_arg = progress_arg;
8763 rfa.patch_cb = NULL;
8764 rfa.patch_arg = NULL;
8765 rfa.repo = repo;
8766 rfa.unlink_added_files = 1;
8767 rfa.added_files_to_unlink = &added_paths;
8768 err = worktree_status(worktree, "", fileindex, repo,
8769 revert_file, &rfa, NULL, NULL, 1, 0);
8770 if (err)
8771 goto sync;
8773 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8774 repo, progress_cb, progress_arg, NULL, NULL);
8775 sync:
8776 sync_err = sync_fileindex(fileindex, fileindex_path);
8777 if (sync_err && err == NULL)
8778 err = sync_err;
8779 done:
8780 free(tree_id);
8781 free(merged_commit_id);
8782 if (commit)
8783 got_object_commit_close(commit);
8784 if (fileindex)
8785 got_fileindex_free(fileindex);
8786 free(fileindex_path);
8787 if (commit_ref)
8788 got_ref_close(commit_ref);
8789 free(commit_ref_name);
8791 unlockerr = lock_worktree(worktree, LOCK_SH);
8792 if (unlockerr && err == NULL)
8793 err = unlockerr;
8794 return err;
8797 struct check_stage_ok_arg {
8798 struct got_object_id *head_commit_id;
8799 struct got_worktree *worktree;
8800 struct got_fileindex *fileindex;
8801 struct got_repository *repo;
8802 int have_changes;
8805 static const struct got_error *
8806 check_stage_ok(void *arg, unsigned char status,
8807 unsigned char staged_status, const char *relpath,
8808 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8809 struct got_object_id *commit_id, int dirfd, const char *de_name)
8811 struct check_stage_ok_arg *a = arg;
8812 const struct got_error *err = NULL;
8813 struct got_fileindex_entry *ie;
8814 struct got_object_id base_commit_id;
8815 struct got_object_id *base_commit_idp = NULL;
8816 char *in_repo_path = NULL, *p;
8818 if (status == GOT_STATUS_UNVERSIONED ||
8819 status == GOT_STATUS_NO_CHANGE)
8820 return NULL;
8821 if (status == GOT_STATUS_NONEXISTENT)
8822 return got_error_set_errno(ENOENT, relpath);
8824 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8825 if (ie == NULL)
8826 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8828 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8829 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8830 relpath) == -1)
8831 return got_error_from_errno("asprintf");
8833 if (got_fileindex_entry_has_commit(ie)) {
8834 base_commit_idp = got_fileindex_entry_get_commit_id(
8835 &base_commit_id, ie);
8838 if (status == GOT_STATUS_CONFLICT) {
8839 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8840 goto done;
8841 } else if (status != GOT_STATUS_ADD &&
8842 status != GOT_STATUS_MODIFY &&
8843 status != GOT_STATUS_DELETE) {
8844 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8845 goto done;
8848 a->have_changes = 1;
8850 p = in_repo_path;
8851 while (p[0] == '/')
8852 p++;
8853 err = check_out_of_date(p, status, staged_status,
8854 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8855 GOT_ERR_STAGE_OUT_OF_DATE);
8856 done:
8857 free(in_repo_path);
8858 return err;
8861 struct stage_path_arg {
8862 struct got_worktree *worktree;
8863 struct got_fileindex *fileindex;
8864 struct got_repository *repo;
8865 got_worktree_status_cb status_cb;
8866 void *status_arg;
8867 got_worktree_patch_cb patch_cb;
8868 void *patch_arg;
8869 int staged_something;
8870 int allow_bad_symlinks;
8873 static const struct got_error *
8874 stage_path(void *arg, unsigned char status,
8875 unsigned char staged_status, const char *relpath,
8876 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8877 struct got_object_id *commit_id, int dirfd, const char *de_name)
8879 struct stage_path_arg *a = arg;
8880 const struct got_error *err = NULL;
8881 struct got_fileindex_entry *ie;
8882 char *ondisk_path = NULL, *path_content = NULL;
8883 uint32_t stage;
8884 struct got_object_id *new_staged_blob_id = NULL;
8885 struct stat sb;
8887 if (status == GOT_STATUS_UNVERSIONED)
8888 return NULL;
8890 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8891 if (ie == NULL)
8892 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8894 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8895 relpath)== -1)
8896 return got_error_from_errno("asprintf");
8898 switch (status) {
8899 case GOT_STATUS_ADD:
8900 case GOT_STATUS_MODIFY:
8901 /* XXX could sb.st_mode be passed in by our caller? */
8902 if (lstat(ondisk_path, &sb) == -1) {
8903 err = got_error_from_errno2("lstat", ondisk_path);
8904 break;
8906 if (a->patch_cb) {
8907 if (status == GOT_STATUS_ADD) {
8908 int choice = GOT_PATCH_CHOICE_NONE;
8909 err = (*a->patch_cb)(&choice, a->patch_arg,
8910 status, ie->path, NULL, 1, 1);
8911 if (err)
8912 break;
8913 if (choice != GOT_PATCH_CHOICE_YES)
8914 break;
8915 } else {
8916 err = create_patched_content(&path_content, 0,
8917 staged_blob_id ? staged_blob_id : blob_id,
8918 ondisk_path, dirfd, de_name, ie->path,
8919 a->repo, a->patch_cb, a->patch_arg);
8920 if (err || path_content == NULL)
8921 break;
8924 err = got_object_blob_create(&new_staged_blob_id,
8925 path_content ? path_content : ondisk_path, a->repo);
8926 if (err)
8927 break;
8928 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8929 SHA1_DIGEST_LENGTH);
8930 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8931 stage = GOT_FILEIDX_STAGE_ADD;
8932 else
8933 stage = GOT_FILEIDX_STAGE_MODIFY;
8934 got_fileindex_entry_stage_set(ie, stage);
8935 if (S_ISLNK(sb.st_mode)) {
8936 int is_bad_symlink = 0;
8937 if (!a->allow_bad_symlinks) {
8938 char target_path[PATH_MAX];
8939 ssize_t target_len;
8940 target_len = readlink(ondisk_path, target_path,
8941 sizeof(target_path));
8942 if (target_len == -1) {
8943 err = got_error_from_errno2("readlink",
8944 ondisk_path);
8945 break;
8947 err = is_bad_symlink_target(&is_bad_symlink,
8948 target_path, target_len, ondisk_path,
8949 a->worktree->root_path);
8950 if (err)
8951 break;
8952 if (is_bad_symlink) {
8953 err = got_error_path(ondisk_path,
8954 GOT_ERR_BAD_SYMLINK);
8955 break;
8958 if (is_bad_symlink)
8959 got_fileindex_entry_staged_filetype_set(ie,
8960 GOT_FILEIDX_MODE_BAD_SYMLINK);
8961 else
8962 got_fileindex_entry_staged_filetype_set(ie,
8963 GOT_FILEIDX_MODE_SYMLINK);
8964 } else {
8965 got_fileindex_entry_staged_filetype_set(ie,
8966 GOT_FILEIDX_MODE_REGULAR_FILE);
8968 a->staged_something = 1;
8969 if (a->status_cb == NULL)
8970 break;
8971 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8972 get_staged_status(ie), relpath, blob_id,
8973 new_staged_blob_id, NULL, dirfd, de_name);
8974 if (err)
8975 break;
8977 * When staging the reverse of the staged diff,
8978 * implicitly unstage the file.
8980 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8981 sizeof(ie->blob_sha1)) == 0) {
8982 got_fileindex_entry_stage_set(ie,
8983 GOT_FILEIDX_STAGE_NONE);
8985 break;
8986 case GOT_STATUS_DELETE:
8987 if (staged_status == GOT_STATUS_DELETE)
8988 break;
8989 if (a->patch_cb) {
8990 int choice = GOT_PATCH_CHOICE_NONE;
8991 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8992 ie->path, NULL, 1, 1);
8993 if (err)
8994 break;
8995 if (choice == GOT_PATCH_CHOICE_NO)
8996 break;
8997 if (choice != GOT_PATCH_CHOICE_YES) {
8998 err = got_error(GOT_ERR_PATCH_CHOICE);
8999 break;
9002 stage = GOT_FILEIDX_STAGE_DELETE;
9003 got_fileindex_entry_stage_set(ie, stage);
9004 a->staged_something = 1;
9005 if (a->status_cb == NULL)
9006 break;
9007 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9008 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9009 de_name);
9010 break;
9011 case GOT_STATUS_NO_CHANGE:
9012 break;
9013 case GOT_STATUS_CONFLICT:
9014 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9015 break;
9016 case GOT_STATUS_NONEXISTENT:
9017 err = got_error_set_errno(ENOENT, relpath);
9018 break;
9019 default:
9020 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9021 break;
9024 if (path_content && unlink(path_content) == -1 && err == NULL)
9025 err = got_error_from_errno2("unlink", path_content);
9026 free(path_content);
9027 free(ondisk_path);
9028 free(new_staged_blob_id);
9029 return err;
9032 const struct got_error *
9033 got_worktree_stage(struct got_worktree *worktree,
9034 struct got_pathlist_head *paths,
9035 got_worktree_status_cb status_cb, void *status_arg,
9036 got_worktree_patch_cb patch_cb, void *patch_arg,
9037 int allow_bad_symlinks, struct got_repository *repo)
9039 const struct got_error *err = NULL, *sync_err, *unlockerr;
9040 struct got_pathlist_entry *pe;
9041 struct got_fileindex *fileindex = NULL;
9042 char *fileindex_path = NULL;
9043 struct got_reference *head_ref = NULL;
9044 struct got_object_id *head_commit_id = NULL;
9045 struct check_stage_ok_arg oka;
9046 struct stage_path_arg spa;
9048 err = lock_worktree(worktree, LOCK_EX);
9049 if (err)
9050 return err;
9052 err = got_ref_open(&head_ref, repo,
9053 got_worktree_get_head_ref_name(worktree), 0);
9054 if (err)
9055 goto done;
9056 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9057 if (err)
9058 goto done;
9059 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9060 if (err)
9061 goto done;
9063 /* Check pre-conditions before staging anything. */
9064 oka.head_commit_id = head_commit_id;
9065 oka.worktree = worktree;
9066 oka.fileindex = fileindex;
9067 oka.repo = repo;
9068 oka.have_changes = 0;
9069 TAILQ_FOREACH(pe, paths, entry) {
9070 err = worktree_status(worktree, pe->path, fileindex, repo,
9071 check_stage_ok, &oka, NULL, NULL, 1, 0);
9072 if (err)
9073 goto done;
9075 if (!oka.have_changes) {
9076 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9077 goto done;
9080 spa.worktree = worktree;
9081 spa.fileindex = fileindex;
9082 spa.repo = repo;
9083 spa.patch_cb = patch_cb;
9084 spa.patch_arg = patch_arg;
9085 spa.status_cb = status_cb;
9086 spa.status_arg = status_arg;
9087 spa.staged_something = 0;
9088 spa.allow_bad_symlinks = allow_bad_symlinks;
9089 TAILQ_FOREACH(pe, paths, entry) {
9090 err = worktree_status(worktree, pe->path, fileindex, repo,
9091 stage_path, &spa, NULL, NULL, 1, 0);
9092 if (err)
9093 goto done;
9095 if (!spa.staged_something) {
9096 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9097 goto done;
9100 sync_err = sync_fileindex(fileindex, fileindex_path);
9101 if (sync_err && err == NULL)
9102 err = sync_err;
9103 done:
9104 if (head_ref)
9105 got_ref_close(head_ref);
9106 free(head_commit_id);
9107 free(fileindex_path);
9108 if (fileindex)
9109 got_fileindex_free(fileindex);
9110 unlockerr = lock_worktree(worktree, LOCK_SH);
9111 if (unlockerr && err == NULL)
9112 err = unlockerr;
9113 return err;
9116 struct unstage_path_arg {
9117 struct got_worktree *worktree;
9118 struct got_fileindex *fileindex;
9119 struct got_repository *repo;
9120 got_worktree_checkout_cb progress_cb;
9121 void *progress_arg;
9122 got_worktree_patch_cb patch_cb;
9123 void *patch_arg;
9126 static const struct got_error *
9127 create_unstaged_content(char **path_unstaged_content,
9128 char **path_new_staged_content, struct got_object_id *blob_id,
9129 struct got_object_id *staged_blob_id, const char *relpath,
9130 struct got_repository *repo,
9131 got_worktree_patch_cb patch_cb, void *patch_arg)
9133 const struct got_error *err, *free_err;
9134 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9135 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9136 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9137 struct got_diffreg_result *diffreg_result = NULL;
9138 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9139 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9140 int fd1 = -1, fd2 = -1;
9142 *path_unstaged_content = NULL;
9143 *path_new_staged_content = NULL;
9145 err = got_object_id_str(&label1, blob_id);
9146 if (err)
9147 return err;
9149 fd1 = got_opentempfd();
9150 if (fd1 == -1) {
9151 err = got_error_from_errno("got_opentempfd");
9152 goto done;
9154 fd2 = got_opentempfd();
9155 if (fd2 == -1) {
9156 err = got_error_from_errno("got_opentempfd");
9157 goto done;
9160 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9161 if (err)
9162 goto done;
9164 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9165 if (err)
9166 goto done;
9168 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9169 if (err)
9170 goto done;
9172 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9173 fd2);
9174 if (err)
9175 goto done;
9177 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9178 if (err)
9179 goto done;
9181 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9182 if (err)
9183 goto done;
9185 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9186 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9187 if (err)
9188 goto done;
9190 err = got_opentemp_named(path_unstaged_content, &outfile,
9191 "got-unstaged-content", "");
9192 if (err)
9193 goto done;
9194 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9195 "got-new-staged-content", "");
9196 if (err)
9197 goto done;
9199 if (fseek(f1, 0L, SEEK_SET) == -1) {
9200 err = got_ferror(f1, GOT_ERR_IO);
9201 goto done;
9203 if (fseek(f2, 0L, SEEK_SET) == -1) {
9204 err = got_ferror(f2, GOT_ERR_IO);
9205 goto done;
9207 /* Count the number of actual changes in the diff result. */
9208 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9209 struct diff_chunk_context cc = {};
9210 diff_chunk_context_load_change(&cc, &nchunks_used,
9211 diffreg_result->result, n, 0);
9212 nchanges++;
9214 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9215 int choice;
9216 err = apply_or_reject_change(&choice, &nchunks_used,
9217 diffreg_result->result, n, relpath, f1, f2,
9218 &line_cur1, &line_cur2,
9219 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9220 if (err)
9221 goto done;
9222 if (choice == GOT_PATCH_CHOICE_YES)
9223 have_content = 1;
9224 else
9225 have_rejected_content = 1;
9226 if (choice == GOT_PATCH_CHOICE_QUIT)
9227 break;
9229 if (have_content || have_rejected_content)
9230 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9231 outfile, rejectfile);
9232 done:
9233 free(label1);
9234 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9235 err = got_error_from_errno("close");
9236 if (blob)
9237 got_object_blob_close(blob);
9238 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9239 err = got_error_from_errno("close");
9240 if (staged_blob)
9241 got_object_blob_close(staged_blob);
9242 free_err = got_diffreg_result_free(diffreg_result);
9243 if (free_err && err == NULL)
9244 err = free_err;
9245 if (f1 && fclose(f1) == EOF && err == NULL)
9246 err = got_error_from_errno2("fclose", path1);
9247 if (f2 && fclose(f2) == EOF && err == NULL)
9248 err = got_error_from_errno2("fclose", path2);
9249 if (outfile && fclose(outfile) == EOF && err == NULL)
9250 err = got_error_from_errno2("fclose", *path_unstaged_content);
9251 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9252 err = got_error_from_errno2("fclose", *path_new_staged_content);
9253 if (path1 && unlink(path1) == -1 && err == NULL)
9254 err = got_error_from_errno2("unlink", path1);
9255 if (path2 && unlink(path2) == -1 && err == NULL)
9256 err = got_error_from_errno2("unlink", path2);
9257 if (err || !have_content) {
9258 if (*path_unstaged_content &&
9259 unlink(*path_unstaged_content) == -1 && err == NULL)
9260 err = got_error_from_errno2("unlink",
9261 *path_unstaged_content);
9262 free(*path_unstaged_content);
9263 *path_unstaged_content = NULL;
9265 if (err || !have_content || !have_rejected_content) {
9266 if (*path_new_staged_content &&
9267 unlink(*path_new_staged_content) == -1 && err == NULL)
9268 err = got_error_from_errno2("unlink",
9269 *path_new_staged_content);
9270 free(*path_new_staged_content);
9271 *path_new_staged_content = NULL;
9273 free(path1);
9274 free(path2);
9275 return err;
9278 static const struct got_error *
9279 unstage_hunks(struct got_object_id *staged_blob_id,
9280 struct got_blob_object *blob_base,
9281 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9282 const char *ondisk_path, const char *label_orig,
9283 struct got_worktree *worktree, struct got_repository *repo,
9284 got_worktree_patch_cb patch_cb, void *patch_arg,
9285 got_worktree_checkout_cb progress_cb, void *progress_arg)
9287 const struct got_error *err = NULL;
9288 char *path_unstaged_content = NULL;
9289 char *path_new_staged_content = NULL;
9290 char *parent = NULL, *base_path = NULL;
9291 char *blob_base_path = NULL;
9292 struct got_object_id *new_staged_blob_id = NULL;
9293 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9294 struct stat sb;
9296 err = create_unstaged_content(&path_unstaged_content,
9297 &path_new_staged_content, blob_id, staged_blob_id,
9298 ie->path, repo, patch_cb, patch_arg);
9299 if (err)
9300 return err;
9302 if (path_unstaged_content == NULL)
9303 return NULL;
9305 if (path_new_staged_content) {
9306 err = got_object_blob_create(&new_staged_blob_id,
9307 path_new_staged_content, repo);
9308 if (err)
9309 goto done;
9312 f = fopen(path_unstaged_content, "re");
9313 if (f == NULL) {
9314 err = got_error_from_errno2("fopen",
9315 path_unstaged_content);
9316 goto done;
9318 if (fstat(fileno(f), &sb) == -1) {
9319 err = got_error_from_errno2("fstat", path_unstaged_content);
9320 goto done;
9322 if (got_fileindex_entry_staged_filetype_get(ie) ==
9323 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9324 char link_target[PATH_MAX];
9325 size_t r;
9326 r = fread(link_target, 1, sizeof(link_target), f);
9327 if (r == 0 && ferror(f)) {
9328 err = got_error_from_errno("fread");
9329 goto done;
9331 if (r >= sizeof(link_target)) { /* should not happen */
9332 err = got_error(GOT_ERR_NO_SPACE);
9333 goto done;
9335 link_target[r] = '\0';
9336 err = merge_symlink(worktree, blob_base,
9337 ondisk_path, ie->path, label_orig, link_target,
9338 worktree->base_commit_id, repo, progress_cb,
9339 progress_arg);
9340 } else {
9341 int local_changes_subsumed;
9343 err = got_path_dirname(&parent, ondisk_path);
9344 if (err)
9345 return err;
9347 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9348 parent) == -1) {
9349 err = got_error_from_errno("asprintf");
9350 base_path = NULL;
9351 goto done;
9354 err = got_opentemp_named(&blob_base_path, &f_base,
9355 base_path, "");
9356 if (err)
9357 goto done;
9358 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9359 blob_base);
9360 if (err)
9361 goto done;
9364 * In order the run a 3-way merge with a symlink we copy the symlink's
9365 * target path into a temporary file and use that file with diff3.
9367 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9368 err = dump_symlink_target_path_to_file(&f_deriv2,
9369 ondisk_path);
9370 if (err)
9371 goto done;
9372 } else {
9373 int fd;
9374 fd = open(ondisk_path,
9375 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9376 if (fd == -1) {
9377 err = got_error_from_errno2("open", ondisk_path);
9378 goto done;
9380 f_deriv2 = fdopen(fd, "r");
9381 if (f_deriv2 == NULL) {
9382 err = got_error_from_errno2("fdopen", ondisk_path);
9383 close(fd);
9384 goto done;
9388 err = merge_file(&local_changes_subsumed, worktree,
9389 f_base, f, f_deriv2, ondisk_path, ie->path,
9390 got_fileindex_perms_to_st(ie),
9391 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9392 repo, progress_cb, progress_arg);
9394 if (err)
9395 goto done;
9397 if (new_staged_blob_id) {
9398 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9399 SHA1_DIGEST_LENGTH);
9400 } else {
9401 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9402 got_fileindex_entry_staged_filetype_set(ie, 0);
9404 done:
9405 free(new_staged_blob_id);
9406 if (path_unstaged_content &&
9407 unlink(path_unstaged_content) == -1 && err == NULL)
9408 err = got_error_from_errno2("unlink", path_unstaged_content);
9409 if (path_new_staged_content &&
9410 unlink(path_new_staged_content) == -1 && err == NULL)
9411 err = got_error_from_errno2("unlink", path_new_staged_content);
9412 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9413 err = got_error_from_errno2("unlink", blob_base_path);
9414 if (f_base && fclose(f_base) == EOF && err == NULL)
9415 err = got_error_from_errno2("fclose", path_unstaged_content);
9416 if (f && fclose(f) == EOF && err == NULL)
9417 err = got_error_from_errno2("fclose", path_unstaged_content);
9418 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9419 err = got_error_from_errno2("fclose", ondisk_path);
9420 free(path_unstaged_content);
9421 free(path_new_staged_content);
9422 free(blob_base_path);
9423 free(parent);
9424 free(base_path);
9425 return err;
9428 static const struct got_error *
9429 unstage_path(void *arg, unsigned char status,
9430 unsigned char staged_status, const char *relpath,
9431 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9432 struct got_object_id *commit_id, int dirfd, const char *de_name)
9434 const struct got_error *err = NULL;
9435 struct unstage_path_arg *a = arg;
9436 struct got_fileindex_entry *ie;
9437 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9438 char *ondisk_path = NULL;
9439 char *id_str = NULL, *label_orig = NULL;
9440 int local_changes_subsumed;
9441 struct stat sb;
9442 int fd1 = -1, fd2 = -1;
9444 if (staged_status != GOT_STATUS_ADD &&
9445 staged_status != GOT_STATUS_MODIFY &&
9446 staged_status != GOT_STATUS_DELETE)
9447 return NULL;
9449 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9450 if (ie == NULL)
9451 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9453 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9454 == -1)
9455 return got_error_from_errno("asprintf");
9457 err = got_object_id_str(&id_str,
9458 commit_id ? commit_id : a->worktree->base_commit_id);
9459 if (err)
9460 goto done;
9461 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9462 id_str) == -1) {
9463 err = got_error_from_errno("asprintf");
9464 goto done;
9467 fd1 = got_opentempfd();
9468 if (fd1 == -1) {
9469 err = got_error_from_errno("got_opentempfd");
9470 goto done;
9472 fd2 = got_opentempfd();
9473 if (fd2 == -1) {
9474 err = got_error_from_errno("got_opentempfd");
9475 goto done;
9478 switch (staged_status) {
9479 case GOT_STATUS_MODIFY:
9480 err = got_object_open_as_blob(&blob_base, a->repo,
9481 blob_id, 8192, fd1);
9482 if (err)
9483 break;
9484 /* fall through */
9485 case GOT_STATUS_ADD:
9486 if (a->patch_cb) {
9487 if (staged_status == GOT_STATUS_ADD) {
9488 int choice = GOT_PATCH_CHOICE_NONE;
9489 err = (*a->patch_cb)(&choice, a->patch_arg,
9490 staged_status, ie->path, NULL, 1, 1);
9491 if (err)
9492 break;
9493 if (choice != GOT_PATCH_CHOICE_YES)
9494 break;
9495 } else {
9496 err = unstage_hunks(staged_blob_id,
9497 blob_base, blob_id, ie, ondisk_path,
9498 label_orig, a->worktree, a->repo,
9499 a->patch_cb, a->patch_arg,
9500 a->progress_cb, a->progress_arg);
9501 break; /* Done with this file. */
9504 err = got_object_open_as_blob(&blob_staged, a->repo,
9505 staged_blob_id, 8192, fd2);
9506 if (err)
9507 break;
9508 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9509 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9510 case GOT_FILEIDX_MODE_REGULAR_FILE:
9511 err = merge_blob(&local_changes_subsumed, a->worktree,
9512 blob_base, ondisk_path, relpath,
9513 got_fileindex_perms_to_st(ie), label_orig,
9514 blob_staged, commit_id ? commit_id :
9515 a->worktree->base_commit_id, a->repo,
9516 a->progress_cb, a->progress_arg);
9517 break;
9518 case GOT_FILEIDX_MODE_SYMLINK:
9519 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9520 char *staged_target;
9521 err = got_object_blob_read_to_str(
9522 &staged_target, blob_staged);
9523 if (err)
9524 goto done;
9525 err = merge_symlink(a->worktree, blob_base,
9526 ondisk_path, relpath, label_orig,
9527 staged_target, commit_id ? commit_id :
9528 a->worktree->base_commit_id,
9529 a->repo, a->progress_cb, a->progress_arg);
9530 free(staged_target);
9531 } else {
9532 err = merge_blob(&local_changes_subsumed,
9533 a->worktree, blob_base, ondisk_path,
9534 relpath, got_fileindex_perms_to_st(ie),
9535 label_orig, blob_staged,
9536 commit_id ? commit_id :
9537 a->worktree->base_commit_id, a->repo,
9538 a->progress_cb, a->progress_arg);
9540 break;
9541 default:
9542 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9543 break;
9545 if (err == NULL) {
9546 got_fileindex_entry_stage_set(ie,
9547 GOT_FILEIDX_STAGE_NONE);
9548 got_fileindex_entry_staged_filetype_set(ie, 0);
9550 break;
9551 case GOT_STATUS_DELETE:
9552 if (a->patch_cb) {
9553 int choice = GOT_PATCH_CHOICE_NONE;
9554 err = (*a->patch_cb)(&choice, a->patch_arg,
9555 staged_status, ie->path, NULL, 1, 1);
9556 if (err)
9557 break;
9558 if (choice == GOT_PATCH_CHOICE_NO)
9559 break;
9560 if (choice != GOT_PATCH_CHOICE_YES) {
9561 err = got_error(GOT_ERR_PATCH_CHOICE);
9562 break;
9565 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9566 got_fileindex_entry_staged_filetype_set(ie, 0);
9567 err = get_file_status(&status, &sb, ie, ondisk_path,
9568 dirfd, de_name, a->repo);
9569 if (err)
9570 break;
9571 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9572 break;
9574 done:
9575 free(ondisk_path);
9576 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9577 err = got_error_from_errno("close");
9578 if (blob_base)
9579 got_object_blob_close(blob_base);
9580 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9581 err = got_error_from_errno("close");
9582 if (blob_staged)
9583 got_object_blob_close(blob_staged);
9584 free(id_str);
9585 free(label_orig);
9586 return err;
9589 const struct got_error *
9590 got_worktree_unstage(struct got_worktree *worktree,
9591 struct got_pathlist_head *paths,
9592 got_worktree_checkout_cb progress_cb, void *progress_arg,
9593 got_worktree_patch_cb patch_cb, void *patch_arg,
9594 struct got_repository *repo)
9596 const struct got_error *err = NULL, *sync_err, *unlockerr;
9597 struct got_pathlist_entry *pe;
9598 struct got_fileindex *fileindex = NULL;
9599 char *fileindex_path = NULL;
9600 struct unstage_path_arg upa;
9602 err = lock_worktree(worktree, LOCK_EX);
9603 if (err)
9604 return err;
9606 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9607 if (err)
9608 goto done;
9610 upa.worktree = worktree;
9611 upa.fileindex = fileindex;
9612 upa.repo = repo;
9613 upa.progress_cb = progress_cb;
9614 upa.progress_arg = progress_arg;
9615 upa.patch_cb = patch_cb;
9616 upa.patch_arg = patch_arg;
9617 TAILQ_FOREACH(pe, paths, entry) {
9618 err = worktree_status(worktree, pe->path, fileindex, repo,
9619 unstage_path, &upa, NULL, NULL, 1, 0);
9620 if (err)
9621 goto done;
9624 sync_err = sync_fileindex(fileindex, fileindex_path);
9625 if (sync_err && err == NULL)
9626 err = sync_err;
9627 done:
9628 free(fileindex_path);
9629 if (fileindex)
9630 got_fileindex_free(fileindex);
9631 unlockerr = lock_worktree(worktree, LOCK_SH);
9632 if (unlockerr && err == NULL)
9633 err = unlockerr;
9634 return err;
9637 struct report_file_info_arg {
9638 struct got_worktree *worktree;
9639 got_worktree_path_info_cb info_cb;
9640 void *info_arg;
9641 struct got_pathlist_head *paths;
9642 got_cancel_cb cancel_cb;
9643 void *cancel_arg;
9646 static const struct got_error *
9647 report_file_info(void *arg, struct got_fileindex_entry *ie)
9649 struct report_file_info_arg *a = arg;
9650 struct got_pathlist_entry *pe;
9651 struct got_object_id blob_id, staged_blob_id, commit_id;
9652 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9653 struct got_object_id *commit_idp = NULL;
9654 int stage;
9656 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9657 return got_error(GOT_ERR_CANCELLED);
9659 TAILQ_FOREACH(pe, a->paths, entry) {
9660 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9661 got_path_is_child(ie->path, pe->path, pe->path_len))
9662 break;
9664 if (pe == NULL) /* not found */
9665 return NULL;
9667 if (got_fileindex_entry_has_blob(ie))
9668 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9669 stage = got_fileindex_entry_stage_get(ie);
9670 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9671 stage == GOT_FILEIDX_STAGE_ADD) {
9672 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9673 &staged_blob_id, ie);
9676 if (got_fileindex_entry_has_commit(ie))
9677 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9679 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9680 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9683 const struct got_error *
9684 got_worktree_path_info(struct got_worktree *worktree,
9685 struct got_pathlist_head *paths,
9686 got_worktree_path_info_cb info_cb, void *info_arg,
9687 got_cancel_cb cancel_cb, void *cancel_arg)
9690 const struct got_error *err = NULL, *unlockerr;
9691 struct got_fileindex *fileindex = NULL;
9692 char *fileindex_path = NULL;
9693 struct report_file_info_arg arg;
9695 err = lock_worktree(worktree, LOCK_SH);
9696 if (err)
9697 return err;
9699 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9700 if (err)
9701 goto done;
9703 arg.worktree = worktree;
9704 arg.info_cb = info_cb;
9705 arg.info_arg = info_arg;
9706 arg.paths = paths;
9707 arg.cancel_cb = cancel_cb;
9708 arg.cancel_arg = cancel_arg;
9709 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9710 &arg);
9711 done:
9712 free(fileindex_path);
9713 if (fileindex)
9714 got_fileindex_free(fileindex);
9715 unlockerr = lock_worktree(worktree, LOCK_UN);
9716 if (unlockerr && err == NULL)
9717 err = unlockerr;
9718 return err;
9721 static const struct got_error *
9722 patch_check_path(const char *p, char **path, unsigned char *status,
9723 unsigned char *staged_status, struct got_fileindex *fileindex,
9724 struct got_worktree *worktree, struct got_repository *repo)
9726 const struct got_error *err;
9727 struct got_fileindex_entry *ie;
9728 struct stat sb;
9729 char *ondisk_path = NULL;
9731 err = got_worktree_resolve_path(path, worktree, p);
9732 if (err)
9733 return err;
9735 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9736 *path[0] ? "/" : "", *path) == -1)
9737 return got_error_from_errno("asprintf");
9739 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9740 if (ie) {
9741 *staged_status = get_staged_status(ie);
9742 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9743 repo);
9744 if (err)
9745 goto done;
9746 } else {
9747 *staged_status = GOT_STATUS_NO_CHANGE;
9748 *status = GOT_STATUS_UNVERSIONED;
9749 if (lstat(ondisk_path, &sb) == -1) {
9750 if (errno != ENOENT) {
9751 err = got_error_from_errno2("lstat",
9752 ondisk_path);
9753 goto done;
9755 *status = GOT_STATUS_NONEXISTENT;
9759 done:
9760 free(ondisk_path);
9761 return err;
9764 static const struct got_error *
9765 patch_can_rm(const char *path, unsigned char status,
9766 unsigned char staged_status)
9768 if (status == GOT_STATUS_NONEXISTENT)
9769 return got_error_set_errno(ENOENT, path);
9770 if (status != GOT_STATUS_NO_CHANGE &&
9771 status != GOT_STATUS_ADD &&
9772 status != GOT_STATUS_MODIFY &&
9773 status != GOT_STATUS_MODE_CHANGE)
9774 return got_error_path(path, GOT_ERR_FILE_STATUS);
9775 if (staged_status == GOT_STATUS_DELETE)
9776 return got_error_path(path, GOT_ERR_FILE_STATUS);
9777 return NULL;
9780 static const struct got_error *
9781 patch_can_add(const char *path, unsigned char status)
9783 if (status != GOT_STATUS_NONEXISTENT)
9784 return got_error_path(path, GOT_ERR_FILE_STATUS);
9785 return NULL;
9788 static const struct got_error *
9789 patch_can_edit(const char *path, unsigned char status,
9790 unsigned char staged_status)
9792 if (status == GOT_STATUS_NONEXISTENT)
9793 return got_error_set_errno(ENOENT, path);
9794 if (status != GOT_STATUS_NO_CHANGE &&
9795 status != GOT_STATUS_ADD &&
9796 status != GOT_STATUS_MODIFY)
9797 return got_error_path(path, GOT_ERR_FILE_STATUS);
9798 if (staged_status == GOT_STATUS_DELETE)
9799 return got_error_path(path, GOT_ERR_FILE_STATUS);
9800 return NULL;
9803 const struct got_error *
9804 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9805 char **fileindex_path, struct got_worktree *worktree)
9807 return open_fileindex(fileindex, fileindex_path, worktree);
9810 const struct got_error *
9811 got_worktree_patch_check_path(const char *old, const char *new,
9812 char **oldpath, char **newpath, struct got_worktree *worktree,
9813 struct got_repository *repo, struct got_fileindex *fileindex)
9815 const struct got_error *err = NULL;
9816 int file_renamed = 0;
9817 unsigned char status_old, staged_status_old;
9818 unsigned char status_new, staged_status_new;
9820 *oldpath = NULL;
9821 *newpath = NULL;
9823 err = patch_check_path(old != NULL ? old : new, oldpath,
9824 &status_old, &staged_status_old, fileindex, worktree, repo);
9825 if (err)
9826 goto done;
9828 err = patch_check_path(new != NULL ? new : old, newpath,
9829 &status_new, &staged_status_new, fileindex, worktree, repo);
9830 if (err)
9831 goto done;
9833 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9834 file_renamed = 1;
9836 if (old != NULL && new == NULL)
9837 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9838 else if (file_renamed) {
9839 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9840 if (err == NULL)
9841 err = patch_can_add(*newpath, status_new);
9842 } else if (old == NULL)
9843 err = patch_can_add(*newpath, status_new);
9844 else
9845 err = patch_can_edit(*newpath, status_new, staged_status_new);
9847 done:
9848 if (err) {
9849 free(*oldpath);
9850 *oldpath = NULL;
9851 free(*newpath);
9852 *newpath = NULL;
9854 return err;
9857 const struct got_error *
9858 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9859 struct got_worktree *worktree, struct got_fileindex *fileindex,
9860 got_worktree_checkout_cb progress_cb, void *progress_arg)
9862 struct schedule_addition_args saa;
9864 memset(&saa, 0, sizeof(saa));
9865 saa.worktree = worktree;
9866 saa.fileindex = fileindex;
9867 saa.progress_cb = progress_cb;
9868 saa.progress_arg = progress_arg;
9869 saa.repo = repo;
9871 return worktree_status(worktree, path, fileindex, repo,
9872 schedule_addition, &saa, NULL, NULL, 1, 0);
9875 const struct got_error *
9876 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9877 struct got_worktree *worktree, struct got_fileindex *fileindex,
9878 got_worktree_delete_cb progress_cb, void *progress_arg)
9880 const struct got_error *err;
9881 struct schedule_deletion_args sda;
9882 char *ondisk_status_path;
9884 memset(&sda, 0, sizeof(sda));
9885 sda.worktree = worktree;
9886 sda.fileindex = fileindex;
9887 sda.progress_cb = progress_cb;
9888 sda.progress_arg = progress_arg;
9889 sda.repo = repo;
9890 sda.delete_local_mods = 0;
9891 sda.keep_on_disk = 0;
9892 sda.ignore_missing_paths = 0;
9893 sda.status_codes = NULL;
9894 if (asprintf(&ondisk_status_path, "%s/%s",
9895 got_worktree_get_root_path(worktree), path) == -1)
9896 return got_error_from_errno("asprintf");
9897 sda.status_path = ondisk_status_path;
9898 sda.status_path_len = strlen(ondisk_status_path);
9900 err = worktree_status(worktree, path, fileindex, repo,
9901 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9902 free(ondisk_status_path);
9903 return err;
9906 const struct got_error *
9907 got_worktree_patch_complete(struct got_fileindex *fileindex,
9908 const char *fileindex_path)
9910 const struct got_error *err = NULL;
9912 err = sync_fileindex(fileindex, fileindex_path);
9913 got_fileindex_free(fileindex);
9915 return err;