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 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
401 return got_error_from_errno("asprintf");
403 err = got_path_mkdir(abspath);
404 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
405 struct stat sb;
406 err = NULL;
407 if (lstat(abspath, &sb) == -1) {
408 err = got_error_from_errno2("lstat", abspath);
409 } else if (!S_ISDIR(sb.st_mode)) {
410 /* TODO directory is obstructed; do something */
411 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
414 free(abspath);
415 return err;
418 static const struct got_error *
419 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
421 const struct got_error *err = NULL;
422 uint8_t fbuf1[8192];
423 uint8_t fbuf2[8192];
424 size_t flen1 = 0, flen2 = 0;
426 *same = 1;
428 for (;;) {
429 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
430 if (flen1 == 0 && ferror(f1)) {
431 err = got_error_from_errno("fread");
432 break;
434 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
435 if (flen2 == 0 && ferror(f2)) {
436 err = got_error_from_errno("fread");
437 break;
439 if (flen1 == 0) {
440 if (flen2 != 0)
441 *same = 0;
442 break;
443 } else if (flen2 == 0) {
444 if (flen1 != 0)
445 *same = 0;
446 break;
447 } else if (flen1 == flen2) {
448 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
449 *same = 0;
450 break;
452 } else {
453 *same = 0;
454 break;
458 return err;
461 static const struct got_error *
462 check_files_equal(int *same, FILE *f1, FILE *f2)
464 struct stat sb;
465 size_t size1, size2;
467 *same = 1;
469 if (fstat(fileno(f1), &sb) != 0)
470 return got_error_from_errno("fstat");
471 size1 = sb.st_size;
473 if (fstat(fileno(f2), &sb) != 0)
474 return got_error_from_errno("fstat");
475 size2 = sb.st_size;
477 if (size1 != size2) {
478 *same = 0;
479 return NULL;
482 if (fseek(f1, 0L, SEEK_SET) == -1)
483 return got_ferror(f1, GOT_ERR_IO);
484 if (fseek(f2, 0L, SEEK_SET) == -1)
485 return got_ferror(f2, GOT_ERR_IO);
487 return check_file_contents_equal(same, f1, f2);
490 static const struct got_error *
491 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
493 uint8_t fbuf[65536];
494 size_t flen;
495 ssize_t outlen;
497 *outsize = 0;
499 if (fseek(f, 0L, SEEK_SET) == -1)
500 return got_ferror(f, GOT_ERR_IO);
502 for (;;) {
503 flen = fread(fbuf, 1, sizeof(fbuf), f);
504 if (flen == 0) {
505 if (ferror(f))
506 return got_error_from_errno("fread");
507 if (feof(f))
508 break;
510 outlen = write(outfd, fbuf, flen);
511 if (outlen == -1)
512 return got_error_from_errno("write");
513 if (outlen != flen)
514 return got_error(GOT_ERR_IO);
515 *outsize += outlen;
518 return NULL;
521 static const struct got_error *
522 merge_binary_file(int *overlapcnt, int merged_fd,
523 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
524 const char *label_deriv, const char *label_orig, const char *label_deriv2,
525 const char *ondisk_path)
527 const struct got_error *err = NULL;
528 int same_content, changed_deriv, changed_deriv2;
529 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
530 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
531 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
532 char *base_path_orig = NULL, *base_path_deriv = NULL;
533 char *base_path_deriv2 = NULL;
535 *overlapcnt = 0;
537 err = check_files_equal(&same_content, f_deriv, f_deriv2);
538 if (err)
539 return err;
541 if (same_content)
542 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
544 err = check_files_equal(&same_content, f_deriv, f_orig);
545 if (err)
546 return err;
547 changed_deriv = !same_content;
548 err = check_files_equal(&same_content, f_deriv2, f_orig);
549 if (err)
550 return err;
551 changed_deriv2 = !same_content;
553 if (changed_deriv && changed_deriv2) {
554 *overlapcnt = 1;
555 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
556 err = got_error_from_errno("asprintf");
557 goto done;
559 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
560 err = got_error_from_errno("asprintf");
561 goto done;
563 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
564 err = got_error_from_errno("asprintf");
565 goto done;
567 err = got_opentemp_named_fd(&path_orig, &fd_orig,
568 base_path_orig, "");
569 if (err)
570 goto done;
571 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
572 base_path_deriv, "");
573 if (err)
574 goto done;
575 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
576 base_path_deriv2, "");
577 if (err)
578 goto done;
579 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
580 if (err)
581 goto done;
582 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
583 if (err)
584 goto done;
585 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
586 if (err)
587 goto done;
588 if (dprintf(merged_fd, "Binary files differ and cannot be "
589 "merged automatically:\n") < 0) {
590 err = got_error_from_errno("dprintf");
591 goto done;
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_BEGIN,
595 label_deriv ? " " : "",
596 label_deriv ? label_deriv : "",
597 path_deriv) < 0) {
598 err = got_error_from_errno("dprintf");
599 goto done;
601 if (size_orig > 0) {
602 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
603 GOT_DIFF_CONFLICT_MARKER_ORIG,
604 label_orig ? " " : "",
605 label_orig ? label_orig : "",
606 path_orig) < 0) {
607 err = got_error_from_errno("dprintf");
608 goto done;
611 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
612 GOT_DIFF_CONFLICT_MARKER_SEP,
613 path_deriv2,
614 GOT_DIFF_CONFLICT_MARKER_END,
615 label_deriv2 ? " " : "",
616 label_deriv2 ? label_deriv2 : "") < 0) {
617 err = got_error_from_errno("dprintf");
618 goto done;
620 } else if (changed_deriv)
621 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
622 else if (changed_deriv2)
623 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
624 done:
625 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
626 err == NULL)
627 err = got_error_from_errno2("unlink", path_orig);
628 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
629 err = got_error_from_errno2("close", path_orig);
630 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
631 err = got_error_from_errno2("close", path_deriv);
632 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
633 err = got_error_from_errno2("close", path_deriv2);
634 free(path_orig);
635 free(path_deriv);
636 free(path_deriv2);
637 free(base_path_orig);
638 free(base_path_deriv);
639 free(base_path_deriv2);
640 return err;
643 /*
644 * Perform a 3-way merge where the file f_orig acts as the common
645 * ancestor, the file f_deriv acts as the first derived version,
646 * and the file f_deriv2 acts as the second derived version.
647 * The merge result will be written to a new file at ondisk_path; any
648 * existing file at this path will be replaced.
649 */
650 static const struct got_error *
651 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
652 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
653 const char *path, uint16_t st_mode,
654 const char *label_orig, const char *label_deriv, const char *label_deriv2,
655 enum got_diff_algorithm diff_algo, struct got_repository *repo,
656 got_worktree_checkout_cb progress_cb, void *progress_arg)
658 const struct got_error *err = NULL;
659 int merged_fd = -1;
660 FILE *f_merged = NULL;
661 char *merged_path = NULL, *base_path = NULL;
662 int overlapcnt = 0;
663 char *parent = NULL;
665 *local_changes_subsumed = 0;
667 err = got_path_dirname(&parent, ondisk_path);
668 if (err)
669 return err;
671 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
672 err = got_error_from_errno("asprintf");
673 goto done;
676 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
677 if (err)
678 goto done;
680 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
681 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
682 if (err) {
683 if (err->code != GOT_ERR_FILE_BINARY)
684 goto done;
685 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
686 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
687 ondisk_path);
688 if (err)
689 goto done;
692 err = (*progress_cb)(progress_arg,
693 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
694 if (err)
695 goto done;
697 if (fsync(merged_fd) != 0) {
698 err = got_error_from_errno("fsync");
699 goto done;
702 f_merged = fdopen(merged_fd, "r");
703 if (f_merged == NULL) {
704 err = got_error_from_errno("fdopen");
705 goto done;
707 merged_fd = -1;
709 /* Check if a clean merge has subsumed all local changes. */
710 if (overlapcnt == 0) {
711 err = check_files_equal(local_changes_subsumed, f_deriv,
712 f_merged);
713 if (err)
714 goto done;
717 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
718 err = got_error_from_errno2("fchmod", merged_path);
719 goto done;
722 if (rename(merged_path, ondisk_path) != 0) {
723 err = got_error_from_errno3("rename", merged_path,
724 ondisk_path);
725 goto done;
727 done:
728 if (err) {
729 if (merged_path)
730 unlink(merged_path);
732 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
733 err = got_error_from_errno("close");
734 if (f_merged && fclose(f_merged) == EOF && err == NULL)
735 err = got_error_from_errno("fclose");
736 free(merged_path);
737 free(base_path);
738 free(parent);
739 return err;
742 static const struct got_error *
743 update_symlink(const char *ondisk_path, const char *target_path,
744 size_t target_len)
746 /* This is not atomic but matches what 'ln -sf' does. */
747 if (unlink(ondisk_path) == -1)
748 return got_error_from_errno2("unlink", ondisk_path);
749 if (symlink(target_path, ondisk_path) == -1)
750 return got_error_from_errno3("symlink", target_path,
751 ondisk_path);
752 return NULL;
755 /*
756 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
757 * in the work tree with a file that contains conflict markers and the
758 * conflicting target paths of the original version, a "derived version"
759 * of a symlink from an incoming change, and a local version of the symlink.
761 * The original versions's target path can be NULL if it is not available,
762 * such as if both derived versions added a new symlink at the same path.
764 * The incoming derived symlink target is NULL in case the incoming change
765 * has deleted this symlink.
766 */
767 static const struct got_error *
768 install_symlink_conflict(const char *deriv_target,
769 struct got_object_id *deriv_base_commit_id, const char *orig_target,
770 const char *label_orig, const char *local_target, const char *ondisk_path)
772 const struct got_error *err;
773 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
774 FILE *f = NULL;
776 err = got_object_id_str(&id_str, deriv_base_commit_id);
777 if (err)
778 return got_error_from_errno("asprintf");
780 if (asprintf(&label_deriv, "%s: commit %s",
781 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
782 err = got_error_from_errno("asprintf");
783 goto done;
786 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
787 if (err)
788 goto done;
790 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
791 err = got_error_from_errno2("fchmod", path);
792 goto done;
795 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
796 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
797 deriv_target ? deriv_target : "(symlink was deleted)",
798 orig_target ? label_orig : "",
799 orig_target ? "\n" : "",
800 orig_target ? orig_target : "",
801 orig_target ? "\n" : "",
802 GOT_DIFF_CONFLICT_MARKER_SEP,
803 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
804 err = got_error_from_errno2("fprintf", path);
805 goto done;
808 if (unlink(ondisk_path) == -1) {
809 err = got_error_from_errno2("unlink", ondisk_path);
810 goto done;
812 if (rename(path, ondisk_path) == -1) {
813 err = got_error_from_errno3("rename", path, ondisk_path);
814 goto done;
816 done:
817 if (f != NULL && fclose(f) == EOF && err == NULL)
818 err = got_error_from_errno2("fclose", path);
819 free(path);
820 free(id_str);
821 free(label_deriv);
822 return err;
825 /* forward declaration */
826 static const struct got_error *
827 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
828 const char *, const char *, uint16_t, const char *,
829 struct got_blob_object *, struct got_object_id *,
830 struct got_repository *, got_worktree_checkout_cb, void *);
832 /*
833 * Merge a symlink into the work tree, where blob_orig acts as the common
834 * ancestor, deriv_target is the link target of the first derived version,
835 * and the symlink on disk acts as the second derived version.
836 * Assume that contents of both blobs represent symlinks.
837 */
838 static const struct got_error *
839 merge_symlink(struct got_worktree *worktree,
840 struct got_blob_object *blob_orig, const char *ondisk_path,
841 const char *path, const char *label_orig, const char *deriv_target,
842 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
843 got_worktree_checkout_cb progress_cb, void *progress_arg)
845 const struct got_error *err = NULL;
846 char *ancestor_target = NULL;
847 struct stat sb;
848 ssize_t ondisk_len, deriv_len;
849 char ondisk_target[PATH_MAX];
850 int have_local_change = 0;
851 int have_incoming_change = 0;
853 if (lstat(ondisk_path, &sb) == -1)
854 return got_error_from_errno2("lstat", ondisk_path);
856 ondisk_len = readlink(ondisk_path, ondisk_target,
857 sizeof(ondisk_target));
858 if (ondisk_len == -1) {
859 err = got_error_from_errno2("readlink",
860 ondisk_path);
861 goto done;
863 ondisk_target[ondisk_len] = '\0';
865 if (blob_orig) {
866 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
867 if (err)
868 goto done;
871 if (ancestor_target == NULL ||
872 (ondisk_len != strlen(ancestor_target) ||
873 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
874 have_local_change = 1;
876 deriv_len = strlen(deriv_target);
877 if (ancestor_target == NULL ||
878 (deriv_len != strlen(ancestor_target) ||
879 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
880 have_incoming_change = 1;
882 if (!have_local_change && !have_incoming_change) {
883 if (ancestor_target) {
884 /* Both sides made the same change. */
885 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
886 path);
887 } else if (deriv_len == ondisk_len &&
888 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
889 /* Both sides added the same symlink. */
890 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
891 path);
892 } else {
893 /* Both sides added symlinks which don't match. */
894 err = install_symlink_conflict(deriv_target,
895 deriv_base_commit_id, ancestor_target,
896 label_orig, ondisk_target, ondisk_path);
897 if (err)
898 goto done;
899 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
900 path);
902 } else if (!have_local_change && have_incoming_change) {
903 /* Apply the incoming change. */
904 err = update_symlink(ondisk_path, deriv_target,
905 strlen(deriv_target));
906 if (err)
907 goto done;
908 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
909 } else if (have_local_change && have_incoming_change) {
910 if (deriv_len == ondisk_len &&
911 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
912 /* Both sides made the same change. */
913 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
914 path);
915 } else {
916 err = install_symlink_conflict(deriv_target,
917 deriv_base_commit_id, ancestor_target, label_orig,
918 ondisk_target, ondisk_path);
919 if (err)
920 goto done;
921 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
922 path);
926 done:
927 free(ancestor_target);
928 return err;
931 static const struct got_error *
932 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
934 const struct got_error *err = NULL;
935 char target_path[PATH_MAX];
936 ssize_t target_len;
937 size_t n;
938 FILE *f;
940 *outfile = NULL;
942 f = got_opentemp();
943 if (f == NULL)
944 return got_error_from_errno("got_opentemp");
945 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
946 if (target_len == -1) {
947 err = got_error_from_errno2("readlink", ondisk_path);
948 goto done;
950 n = fwrite(target_path, 1, target_len, f);
951 if (n != target_len) {
952 err = got_ferror(f, GOT_ERR_IO);
953 goto done;
955 if (fflush(f) == EOF) {
956 err = got_error_from_errno("fflush");
957 goto done;
959 if (fseek(f, 0L, SEEK_SET) == -1) {
960 err = got_ferror(f, GOT_ERR_IO);
961 goto done;
963 done:
964 if (err)
965 fclose(f);
966 else
967 *outfile = f;
968 return err;
971 /*
972 * Perform a 3-way merge where blob_orig acts as the common ancestor,
973 * blob_deriv acts as the first derived version, and the file on disk
974 * acts as the second derived version.
975 */
976 static const struct got_error *
977 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
978 struct got_blob_object *blob_orig, const char *ondisk_path,
979 const char *path, uint16_t st_mode, const char *label_orig,
980 struct got_blob_object *blob_deriv,
981 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
982 got_worktree_checkout_cb progress_cb, void *progress_arg)
984 const struct got_error *err = NULL;
985 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
986 char *blob_orig_path = NULL;
987 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
988 char *label_deriv = NULL, *parent = NULL;
990 *local_changes_subsumed = 0;
992 err = got_path_dirname(&parent, ondisk_path);
993 if (err)
994 return err;
996 if (blob_orig) {
997 if (asprintf(&base_path, "%s/got-merge-blob-orig",
998 parent) == -1) {
999 err = got_error_from_errno("asprintf");
1000 base_path = NULL;
1001 goto done;
1004 err = got_opentemp_named(&blob_orig_path, &f_orig,
1005 base_path, "");
1006 if (err)
1007 goto done;
1008 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1009 blob_orig);
1010 if (err)
1011 goto done;
1012 free(base_path);
1013 } else {
1015 * No common ancestor exists. This is an "add vs add" conflict
1016 * and we simply use an empty ancestor file to make both files
1017 * appear in the merged result in their entirety.
1019 f_orig = got_opentemp();
1020 if (f_orig == NULL) {
1021 err = got_error_from_errno("got_opentemp");
1022 goto done;
1026 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1027 err = got_error_from_errno("asprintf");
1028 base_path = NULL;
1029 goto done;
1032 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1033 if (err)
1034 goto done;
1035 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1036 blob_deriv);
1037 if (err)
1038 goto done;
1040 err = got_object_id_str(&id_str, deriv_base_commit_id);
1041 if (err)
1042 goto done;
1043 if (asprintf(&label_deriv, "%s: commit %s",
1044 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1045 err = got_error_from_errno("asprintf");
1046 goto done;
1050 * In order the run a 3-way merge with a symlink we copy the symlink's
1051 * target path into a temporary file and use that file with diff3.
1053 if (S_ISLNK(st_mode)) {
1054 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1055 if (err)
1056 goto done;
1057 } else {
1058 int fd;
1059 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1060 if (fd == -1) {
1061 err = got_error_from_errno2("open", ondisk_path);
1062 goto done;
1064 f_deriv2 = fdopen(fd, "r");
1065 if (f_deriv2 == NULL) {
1066 err = got_error_from_errno2("fdopen", ondisk_path);
1067 close(fd);
1068 goto done;
1072 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1073 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1074 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1075 done:
1076 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1077 err = got_error_from_errno("fclose");
1078 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1079 err = got_error_from_errno("fclose");
1080 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1081 err = got_error_from_errno("fclose");
1082 free(base_path);
1083 if (blob_orig_path) {
1084 unlink(blob_orig_path);
1085 free(blob_orig_path);
1087 if (blob_deriv_path) {
1088 unlink(blob_deriv_path);
1089 free(blob_deriv_path);
1091 free(id_str);
1092 free(label_deriv);
1093 free(parent);
1094 return err;
1097 static const struct got_error *
1098 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1099 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1100 int wt_fd, const char *path, struct got_object_id *blob_id)
1102 const struct got_error *err = NULL;
1103 struct got_fileindex_entry *new_ie;
1105 *new_iep = NULL;
1107 err = got_fileindex_entry_alloc(&new_ie, path);
1108 if (err)
1109 return err;
1111 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1112 blob_id->sha1, base_commit_id->sha1, 1);
1113 if (err)
1114 goto done;
1116 err = got_fileindex_entry_add(fileindex, new_ie);
1117 done:
1118 if (err)
1119 got_fileindex_entry_free(new_ie);
1120 else
1121 *new_iep = new_ie;
1122 return err;
1125 static mode_t
1126 get_ondisk_perms(int executable, mode_t st_mode)
1128 mode_t xbits = S_IXUSR;
1130 if (executable) {
1131 /* Map read bits to execute bits. */
1132 if (st_mode & S_IRGRP)
1133 xbits |= S_IXGRP;
1134 if (st_mode & S_IROTH)
1135 xbits |= S_IXOTH;
1136 return st_mode | xbits;
1139 return st_mode;
1142 /* forward declaration */
1143 static const struct got_error *
1144 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 const char *path, mode_t te_mode, mode_t st_mode,
1146 struct got_blob_object *blob, int restoring_missing_file,
1147 int reverting_versioned_file, int installing_bad_symlink,
1148 int path_is_unversioned, struct got_repository *repo,
1149 got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 * This function assumes that the provided symlink target points at a
1153 * safe location in the work tree!
1155 static const struct got_error *
1156 replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 const char *target_path, size_t target_len)
1159 const struct got_error *err = NULL;
1160 ssize_t elen;
1161 char etarget[PATH_MAX];
1162 int fd;
1164 *did_something = 0;
1167 * "Bad" symlinks (those pointing outside the work tree or into the
1168 * .got directory) are installed in the work tree as a regular file
1169 * which contains the bad symlink target path.
1170 * The new symlink target has already been checked for safety by our
1171 * caller. If we can successfully open a regular file then we simply
1172 * replace this file with a symlink below.
1174 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 if (fd == -1) {
1176 if (!got_err_open_nofollow_on_symlink())
1177 return got_error_from_errno2("open", ondisk_path);
1179 /* We are updating an existing on-disk symlink. */
1180 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 if (elen == -1)
1182 return got_error_from_errno2("readlink", ondisk_path);
1184 if (elen == target_len &&
1185 memcmp(etarget, target_path, target_len) == 0)
1186 return NULL; /* nothing to do */
1189 *did_something = 1;
1190 err = update_symlink(ondisk_path, target_path, target_len);
1191 if (fd != -1 && close(fd) == -1 && err == NULL)
1192 err = got_error_from_errno2("close", ondisk_path);
1193 return err;
1196 static const struct got_error *
1197 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 const struct got_error *err = NULL;
1201 char canonpath[PATH_MAX];
1202 char *path_got = NULL;
1204 *is_bad_symlink = 0;
1206 if (target_len >= sizeof(canonpath)) {
1207 *is_bad_symlink = 1;
1208 return NULL;
1212 * We do not use realpath(3) to resolve the symlink's target
1213 * path because we don't want to resolve symlinks recursively.
1214 * Instead we make the path absolute and then canonicalize it.
1215 * Relative symlink target lookup should begin at the directory
1216 * in which the blob object is being installed.
1218 if (!got_path_is_absolute(target_path)) {
1219 char *abspath, *parent;
1220 err = got_path_dirname(&parent, ondisk_path);
1221 if (err)
1222 return err;
1223 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 free(parent);
1225 return got_error_from_errno("asprintf");
1227 free(parent);
1228 if (strlen(abspath) >= sizeof(canonpath)) {
1229 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1230 free(abspath);
1231 return err;
1233 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1234 free(abspath);
1235 if (err)
1236 return err;
1237 } else {
1238 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1239 if (err)
1240 return err;
1243 /* Only allow symlinks pointing at paths within the work tree. */
1244 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 *is_bad_symlink = 1;
1246 return NULL;
1249 /* Do not allow symlinks pointing into the .got directory. */
1250 if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 GOT_WORKTREE_GOT_DIR) == -1)
1252 return got_error_from_errno("asprintf");
1253 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 *is_bad_symlink = 1;
1256 free(path_got);
1257 return NULL;
1260 static const struct got_error *
1261 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 int restoring_missing_file, int reverting_versioned_file,
1264 int path_is_unversioned, int allow_bad_symlinks,
1265 struct got_repository *repo,
1266 got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 const struct got_error *err = NULL;
1269 char target_path[PATH_MAX];
1270 size_t len, target_len = 0;
1271 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 *is_bad_symlink = 0;
1277 * Blob object content specifies the target path of the link.
1278 * If a symbolic link cannot be installed we instead create
1279 * a regular file which contains the link target path stored
1280 * in the blob object.
1282 do {
1283 err = got_object_blob_read_block(&len, blob);
1284 if (err)
1285 return err;
1287 if (len + target_len >= sizeof(target_path)) {
1288 /* Path too long; install as a regular file. */
1289 *is_bad_symlink = 1;
1290 got_object_blob_rewind(blob);
1291 return install_blob(worktree, ondisk_path, path,
1292 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 restoring_missing_file, reverting_versioned_file,
1294 1, path_is_unversioned, repo, progress_cb,
1295 progress_arg);
1297 if (len > 0) {
1298 /* Skip blob object header first time around. */
1299 memcpy(target_path + target_len, buf + hdrlen,
1300 len - hdrlen);
1301 target_len += len - hdrlen;
1302 hdrlen = 0;
1304 } while (len != 0);
1305 target_path[target_len] = '\0';
1307 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 ondisk_path, worktree->root_path);
1309 if (err)
1310 return err;
1312 if (*is_bad_symlink && !allow_bad_symlinks) {
1313 /* install as a regular file */
1314 got_object_blob_rewind(blob);
1315 err = install_blob(worktree, ondisk_path, path,
1316 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 restoring_missing_file, reverting_versioned_file, 1,
1318 path_is_unversioned, repo, progress_cb, progress_arg);
1319 return err;
1322 if (symlink(target_path, ondisk_path) == -1) {
1323 if (errno == EEXIST) {
1324 int symlink_replaced;
1325 if (path_is_unversioned) {
1326 err = (*progress_cb)(progress_arg,
1327 GOT_STATUS_UNVERSIONED, path);
1328 return err;
1330 err = replace_existing_symlink(&symlink_replaced,
1331 ondisk_path, target_path, target_len);
1332 if (err)
1333 return err;
1334 if (progress_cb) {
1335 if (symlink_replaced) {
1336 err = (*progress_cb)(progress_arg,
1337 reverting_versioned_file ?
1338 GOT_STATUS_REVERT :
1339 GOT_STATUS_UPDATE, path);
1340 } else {
1341 err = (*progress_cb)(progress_arg,
1342 GOT_STATUS_EXISTS, path);
1345 return err; /* Nothing else to do. */
1348 if (errno == ENOENT) {
1349 char *parent;
1350 err = got_path_dirname(&parent, ondisk_path);
1351 if (err)
1352 return err;
1353 err = add_dir_on_disk(worktree, parent);
1354 free(parent);
1355 if (err)
1356 return err;
1358 * Retry, and fall through to error handling
1359 * below if this second attempt fails.
1361 if (symlink(target_path, ondisk_path) != -1) {
1362 err = NULL; /* success */
1363 return err;
1367 /* Handle errors from first or second creation attempt. */
1368 if (errno == ENAMETOOLONG) {
1369 /* bad target path; install as a regular file */
1370 *is_bad_symlink = 1;
1371 got_object_blob_rewind(blob);
1372 err = install_blob(worktree, ondisk_path, path,
1373 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 restoring_missing_file, reverting_versioned_file, 1,
1375 path_is_unversioned, repo,
1376 progress_cb, progress_arg);
1377 } else if (errno == ENOTDIR) {
1378 err = got_error_path(ondisk_path,
1379 GOT_ERR_FILE_OBSTRUCTED);
1380 } else {
1381 err = got_error_from_errno3("symlink",
1382 target_path, ondisk_path);
1384 } else if (progress_cb)
1385 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1387 return err;
1390 static const struct got_error *
1391 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 const char *path, mode_t te_mode, mode_t st_mode,
1393 struct got_blob_object *blob, int restoring_missing_file,
1394 int reverting_versioned_file, int installing_bad_symlink,
1395 int path_is_unversioned, struct got_repository *repo,
1396 got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 const struct got_error *err = NULL;
1399 int fd = -1;
1400 size_t len, hdrlen;
1401 int update = 0;
1402 char *tmppath = NULL;
1403 mode_t mode;
1405 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1407 O_CLOEXEC, mode);
1408 if (fd == -1) {
1409 if (errno == ENOENT || errno == ENOTDIR) {
1410 char *parent;
1411 err = got_path_dirname(&parent, path);
1412 if (err)
1413 return err;
1414 err = add_dir_on_disk(worktree, parent);
1415 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1416 err = got_error_path(path, err->code);
1417 free(parent);
1418 if (err)
1419 return err;
1420 fd = open(ondisk_path,
1421 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 mode);
1423 if (fd == -1)
1424 return got_error_from_errno2("open",
1425 ondisk_path);
1426 } else if (errno == EEXIST) {
1427 if (path_is_unversioned) {
1428 err = (*progress_cb)(progress_arg,
1429 GOT_STATUS_UNVERSIONED, path);
1430 goto done;
1432 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1433 !S_ISREG(st_mode) && !installing_bad_symlink) {
1434 /* TODO file is obstructed; do something */
1435 err = got_error_path(ondisk_path,
1436 GOT_ERR_FILE_OBSTRUCTED);
1437 goto done;
1438 } else {
1439 err = got_opentemp_named_fd(&tmppath, &fd,
1440 ondisk_path, "");
1441 if (err)
1442 goto done;
1443 update = 1;
1445 if (fchmod(fd, apply_umask(mode)) == -1) {
1446 err = got_error_from_errno2("fchmod",
1447 tmppath);
1448 goto done;
1451 } else
1452 return got_error_from_errno2("open", ondisk_path);
1455 if (progress_cb) {
1456 if (restoring_missing_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 path);
1459 else if (reverting_versioned_file)
1460 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 path);
1462 else
1463 err = (*progress_cb)(progress_arg,
1464 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1465 if (err)
1466 goto done;
1469 hdrlen = got_object_blob_get_hdrlen(blob);
1470 do {
1471 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1472 err = got_object_blob_read_block(&len, blob);
1473 if (err)
1474 break;
1475 if (len > 0) {
1476 /* Skip blob object header first time around. */
1477 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 if (outlen == -1) {
1479 err = got_error_from_errno("write");
1480 goto done;
1481 } else if (outlen != len - hdrlen) {
1482 err = got_error(GOT_ERR_IO);
1483 goto done;
1485 hdrlen = 0;
1487 } while (len != 0);
1489 if (fsync(fd) != 0) {
1490 err = got_error_from_errno("fsync");
1491 goto done;
1494 if (update) {
1495 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1496 err = got_error_from_errno2("unlink", ondisk_path);
1497 goto done;
1499 if (rename(tmppath, ondisk_path) != 0) {
1500 err = got_error_from_errno3("rename", tmppath,
1501 ondisk_path);
1502 goto done;
1504 free(tmppath);
1505 tmppath = NULL;
1508 done:
1509 if (fd != -1 && close(fd) == -1 && err == NULL)
1510 err = got_error_from_errno("close");
1511 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1512 err = got_error_from_errno2("unlink", tmppath);
1513 free(tmppath);
1514 return err;
1518 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1519 * conflict marker is found in newly added lines only.
1521 static const struct got_error *
1522 get_modified_file_content_status(unsigned char *status,
1523 struct got_blob_object *blob, const char *path, struct stat *sb,
1524 FILE *ondisk_file)
1526 const struct got_error *err, *free_err;
1527 const char *markers[3] = {
1528 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1529 GOT_DIFF_CONFLICT_MARKER_SEP,
1530 GOT_DIFF_CONFLICT_MARKER_END
1532 FILE *f1 = NULL;
1533 struct got_diffreg_result *diffreg_result = NULL;
1534 struct diff_result *r;
1535 int nchunks_parsed, n, i = 0, ln = 0;
1536 char *line = NULL;
1537 size_t linesize = 0;
1538 ssize_t linelen;
1540 if (*status != GOT_STATUS_MODIFY)
1541 return NULL;
1543 f1 = got_opentemp();
1544 if (f1 == NULL)
1545 return got_error_from_errno("got_opentemp");
1547 if (blob) {
1548 got_object_blob_rewind(blob);
1549 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1550 if (err)
1551 goto done;
1554 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1555 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1556 if (err)
1557 goto done;
1559 r = diffreg_result->result;
1561 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1562 struct diff_chunk *c;
1563 struct diff_chunk_context cc = {};
1564 off_t pos;
1567 * We can optimise a little by advancing straight
1568 * to the next chunk if this one has no added lines.
1570 c = diff_chunk_get(r, n);
1572 if (diff_chunk_type(c) != CHUNK_PLUS) {
1573 nchunks_parsed = 1;
1574 continue; /* removed or unchanged lines */
1577 pos = diff_chunk_get_right_start_pos(c);
1578 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1579 err = got_ferror(ondisk_file, GOT_ERR_IO);
1580 goto done;
1583 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1584 ln = cc.right.start;
1586 while (ln < cc.right.end) {
1587 linelen = getline(&line, &linesize, ondisk_file);
1588 if (linelen == -1) {
1589 if (feof(ondisk_file))
1590 break;
1591 err = got_ferror(ondisk_file, GOT_ERR_IO);
1592 break;
1595 if (line && strncmp(line, markers[i],
1596 strlen(markers[i])) == 0) {
1597 if (strcmp(markers[i],
1598 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1599 *status = GOT_STATUS_CONFLICT;
1600 goto done;
1601 } else
1602 i++;
1604 ++ln;
1608 done:
1609 free(line);
1610 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1611 err = got_error_from_errno("fclose");
1612 free_err = got_diffreg_result_free(diffreg_result);
1613 if (err == NULL)
1614 err = free_err;
1616 return err;
1619 static int
1620 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1622 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1623 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1626 static int
1627 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1629 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1630 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1631 ie->mtime_sec == sb->st_mtim.tv_sec &&
1632 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1633 ie->size == (sb->st_size & 0xffffffff) &&
1634 !xbit_differs(ie, sb->st_mode));
1637 static unsigned char
1638 get_staged_status(struct got_fileindex_entry *ie)
1640 switch (got_fileindex_entry_stage_get(ie)) {
1641 case GOT_FILEIDX_STAGE_ADD:
1642 return GOT_STATUS_ADD;
1643 case GOT_FILEIDX_STAGE_DELETE:
1644 return GOT_STATUS_DELETE;
1645 case GOT_FILEIDX_STAGE_MODIFY:
1646 return GOT_STATUS_MODIFY;
1647 default:
1648 return GOT_STATUS_NO_CHANGE;
1652 static const struct got_error *
1653 get_symlink_modification_status(unsigned char *status,
1654 struct got_fileindex_entry *ie, const char *abspath,
1655 int dirfd, const char *de_name, struct got_blob_object *blob)
1657 const struct got_error *err = NULL;
1658 char target_path[PATH_MAX];
1659 char etarget[PATH_MAX];
1660 ssize_t elen;
1661 size_t len, target_len = 0;
1662 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1663 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1665 *status = GOT_STATUS_NO_CHANGE;
1667 /* Blob object content specifies the target path of the link. */
1668 do {
1669 err = got_object_blob_read_block(&len, blob);
1670 if (err)
1671 return err;
1672 if (len + target_len >= sizeof(target_path)) {
1674 * Should not happen. The blob contents were OK
1675 * when this symlink was installed.
1677 return got_error(GOT_ERR_NO_SPACE);
1679 if (len > 0) {
1680 /* Skip blob object header first time around. */
1681 memcpy(target_path + target_len, buf + hdrlen,
1682 len - hdrlen);
1683 target_len += len - hdrlen;
1684 hdrlen = 0;
1686 } while (len != 0);
1687 target_path[target_len] = '\0';
1689 if (dirfd != -1) {
1690 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1691 if (elen == -1)
1692 return got_error_from_errno2("readlinkat", abspath);
1693 } else {
1694 elen = readlink(abspath, etarget, sizeof(etarget));
1695 if (elen == -1)
1696 return got_error_from_errno2("readlink", abspath);
1699 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1700 *status = GOT_STATUS_MODIFY;
1702 return NULL;
1705 static const struct got_error *
1706 get_file_status(unsigned char *status, struct stat *sb,
1707 struct got_fileindex_entry *ie, const char *abspath,
1708 int dirfd, const char *de_name, struct got_repository *repo)
1710 const struct got_error *err = NULL;
1711 struct got_object_id id;
1712 size_t hdrlen;
1713 int fd = -1, fd1 = -1;
1714 FILE *f = NULL;
1715 uint8_t fbuf[8192];
1716 struct got_blob_object *blob = NULL;
1717 size_t flen, blen;
1718 unsigned char staged_status;
1720 staged_status = get_staged_status(ie);
1721 *status = GOT_STATUS_NO_CHANGE;
1722 memset(sb, 0, sizeof(*sb));
1725 * Whenever the caller provides a directory descriptor and a
1726 * directory entry name for the file, use them! This prevents
1727 * race conditions if filesystem paths change beneath our feet.
1729 if (dirfd != -1) {
1730 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1731 if (errno == ENOENT) {
1732 if (got_fileindex_entry_has_file_on_disk(ie))
1733 *status = GOT_STATUS_MISSING;
1734 else
1735 *status = GOT_STATUS_DELETE;
1736 goto done;
1738 err = got_error_from_errno2("fstatat", abspath);
1739 goto done;
1741 } else {
1742 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1743 if (fd == -1 && errno != ENOENT &&
1744 !got_err_open_nofollow_on_symlink())
1745 return got_error_from_errno2("open", abspath);
1746 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1747 if (lstat(abspath, sb) == -1)
1748 return got_error_from_errno2("lstat", abspath);
1749 } else if (fd == -1 || fstat(fd, sb) == -1) {
1750 if (errno == ENOENT) {
1751 if (got_fileindex_entry_has_file_on_disk(ie))
1752 *status = GOT_STATUS_MISSING;
1753 else
1754 *status = GOT_STATUS_DELETE;
1755 goto done;
1757 err = got_error_from_errno2("fstat", abspath);
1758 goto done;
1762 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1763 *status = GOT_STATUS_OBSTRUCTED;
1764 goto done;
1767 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1768 *status = GOT_STATUS_DELETE;
1769 goto done;
1770 } else if (!got_fileindex_entry_has_blob(ie) &&
1771 staged_status != GOT_STATUS_ADD) {
1772 *status = GOT_STATUS_ADD;
1773 goto done;
1776 if (!stat_info_differs(ie, sb))
1777 goto done;
1779 if (S_ISLNK(sb->st_mode) &&
1780 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1781 *status = GOT_STATUS_MODIFY;
1782 goto done;
1785 if (staged_status == GOT_STATUS_MODIFY ||
1786 staged_status == GOT_STATUS_ADD)
1787 got_fileindex_entry_get_staged_blob_id(&id, ie);
1788 else
1789 got_fileindex_entry_get_blob_id(&id, ie);
1791 fd1 = got_opentempfd();
1792 if (fd1 == -1) {
1793 err = got_error_from_errno("got_opentempfd");
1794 goto done;
1796 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1797 if (err)
1798 goto done;
1800 if (S_ISLNK(sb->st_mode)) {
1801 err = get_symlink_modification_status(status, ie,
1802 abspath, dirfd, de_name, blob);
1803 goto done;
1806 if (dirfd != -1) {
1807 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1808 if (fd == -1) {
1809 err = got_error_from_errno2("openat", abspath);
1810 goto done;
1814 f = fdopen(fd, "r");
1815 if (f == NULL) {
1816 err = got_error_from_errno2("fdopen", abspath);
1817 goto done;
1819 fd = -1;
1820 hdrlen = got_object_blob_get_hdrlen(blob);
1821 for (;;) {
1822 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1823 err = got_object_blob_read_block(&blen, blob);
1824 if (err)
1825 goto done;
1826 /* Skip length of blob object header first time around. */
1827 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1828 if (flen == 0 && ferror(f)) {
1829 err = got_error_from_errno("fread");
1830 goto done;
1832 if (blen - hdrlen == 0) {
1833 if (flen != 0)
1834 *status = GOT_STATUS_MODIFY;
1835 break;
1836 } else if (flen == 0) {
1837 if (blen - hdrlen != 0)
1838 *status = GOT_STATUS_MODIFY;
1839 break;
1840 } else if (blen - hdrlen == flen) {
1841 /* Skip blob object header first time around. */
1842 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1843 *status = GOT_STATUS_MODIFY;
1844 break;
1846 } else {
1847 *status = GOT_STATUS_MODIFY;
1848 break;
1850 hdrlen = 0;
1853 if (*status == GOT_STATUS_MODIFY) {
1854 rewind(f);
1855 err = get_modified_file_content_status(status, blob, ie->path,
1856 sb, f);
1857 } else if (xbit_differs(ie, sb->st_mode))
1858 *status = GOT_STATUS_MODE_CHANGE;
1859 done:
1860 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1861 err = got_error_from_errno("close");
1862 if (blob)
1863 got_object_blob_close(blob);
1864 if (f != NULL && fclose(f) == EOF && err == NULL)
1865 err = got_error_from_errno2("fclose", abspath);
1866 if (fd != -1 && close(fd) == -1 && err == NULL)
1867 err = got_error_from_errno2("close", abspath);
1868 return err;
1872 * Update timestamps in the file index if a file is unmodified and
1873 * we had to run a full content comparison to find out.
1875 static const struct got_error *
1876 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1877 struct got_fileindex_entry *ie, struct stat *sb)
1879 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1880 return got_fileindex_entry_update(ie, wt_fd, path,
1881 ie->blob_sha1, ie->commit_sha1, 1);
1883 return NULL;
1886 static const struct got_error *remove_ondisk_file(const char *, const char *);
1888 static const struct got_error *
1889 update_blob(struct got_worktree *worktree,
1890 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1891 struct got_tree_entry *te, const char *path,
1892 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1893 void *progress_arg)
1895 const struct got_error *err = NULL;
1896 struct got_blob_object *blob = NULL;
1897 char *ondisk_path = NULL;
1898 unsigned char status = GOT_STATUS_NO_CHANGE;
1899 struct stat sb;
1900 int fd1 = -1, fd2 = -1;
1902 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 return got_error_from_errno("asprintf");
1905 if (ie) {
1906 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1908 goto done;
1910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1911 repo);
1912 if (err)
1913 goto done;
1914 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 sb.st_mode = got_fileindex_perms_to_st(ie);
1916 } else {
1917 if (stat(ondisk_path, &sb) == -1) {
1918 if (errno != ENOENT && errno != ENOTDIR) {
1919 err = got_error_from_errno2("stat",
1920 ondisk_path);
1921 goto done;
1923 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1924 status = GOT_STATUS_UNVERSIONED;
1925 } else {
1926 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1927 status = GOT_STATUS_UNVERSIONED;
1928 else
1929 status = GOT_STATUS_OBSTRUCTED;
1933 if (status == GOT_STATUS_OBSTRUCTED) {
1934 if (ie)
1935 got_fileindex_entry_mark_skipped(ie);
1936 err = (*progress_cb)(progress_arg, status, path);
1937 goto done;
1939 if (status == GOT_STATUS_CONFLICT) {
1940 if (ie)
1941 got_fileindex_entry_mark_skipped(ie);
1942 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1943 path);
1944 goto done;
1947 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1948 if (status == GOT_STATUS_UNVERSIONED) {
1949 err = (*progress_cb)(progress_arg, status, path);
1950 } else if (status != GOT_STATUS_NO_CHANGE &&
1951 status != GOT_STATUS_DELETE &&
1952 status != GOT_STATUS_NONEXISTENT &&
1953 status != GOT_STATUS_MISSING) {
1954 err = (*progress_cb)(progress_arg,
1955 GOT_STATUS_CANNOT_DELETE, path);
1956 } else if (ie) {
1957 if (status != GOT_STATUS_DELETE &&
1958 status != GOT_STATUS_NONEXISTENT &&
1959 status != GOT_STATUS_MISSING) {
1960 err = remove_ondisk_file(worktree->root_path,
1961 ie->path);
1962 if (err && !(err->code == GOT_ERR_ERRNO &&
1963 errno == ENOENT))
1964 goto done;
1966 got_fileindex_entry_remove(fileindex, ie);
1967 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1968 ie->path);
1970 goto done; /* nothing else to do */
1973 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1974 (S_ISLNK(te->mode) ||
1975 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1977 * This is a regular file or an installed bad symlink.
1978 * If the file index indicates that this file is already
1979 * up-to-date with respect to the repository we can skip
1980 * updating contents of this file.
1982 if (got_fileindex_entry_has_commit(ie) &&
1983 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1984 SHA1_DIGEST_LENGTH) == 0) {
1985 /* Same commit. */
1986 err = sync_timestamps(worktree->root_fd,
1987 path, status, ie, &sb);
1988 if (err)
1989 goto done;
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1991 path);
1992 goto done;
1994 if (got_fileindex_entry_has_blob(ie) &&
1995 memcmp(ie->blob_sha1, te->id.sha1,
1996 SHA1_DIGEST_LENGTH) == 0) {
1997 /* Different commit but the same blob. */
1998 if (got_fileindex_entry_has_commit(ie)) {
1999 /* Update the base commit ID of this file. */
2000 memcpy(ie->commit_sha1,
2001 worktree->base_commit_id->sha1,
2002 sizeof(ie->commit_sha1));
2004 err = sync_timestamps(worktree->root_fd,
2005 path, status, ie, &sb);
2006 if (err)
2007 goto done;
2008 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2009 path);
2010 goto done;
2014 fd1 = got_opentempfd();
2015 if (fd1 == -1) {
2016 err = got_error_from_errno("got_opentempfd");
2017 goto done;
2019 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2020 if (err)
2021 goto done;
2023 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2024 int update_timestamps;
2025 struct got_blob_object *blob2 = NULL;
2026 char *label_orig = NULL;
2027 if (got_fileindex_entry_has_blob(ie)) {
2028 fd2 = got_opentempfd();
2029 if (fd2 == -1) {
2030 err = got_error_from_errno("got_opentempfd");
2031 goto done;
2033 struct got_object_id id2;
2034 got_fileindex_entry_get_blob_id(&id2, ie);
2035 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2036 fd2);
2037 if (err)
2038 goto done;
2040 if (got_fileindex_entry_has_commit(ie)) {
2041 char id_str[SHA1_DIGEST_STRING_LENGTH];
2042 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2043 sizeof(id_str)) == NULL) {
2044 err = got_error_path(id_str,
2045 GOT_ERR_BAD_OBJ_ID_STR);
2046 goto done;
2048 if (asprintf(&label_orig, "%s: commit %s",
2049 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2050 err = got_error_from_errno("asprintf");
2051 goto done;
2054 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2055 char *link_target;
2056 err = got_object_blob_read_to_str(&link_target, blob);
2057 if (err)
2058 goto done;
2059 err = merge_symlink(worktree, blob2, ondisk_path, path,
2060 label_orig, link_target, worktree->base_commit_id,
2061 repo, progress_cb, progress_arg);
2062 free(link_target);
2063 } else {
2064 err = merge_blob(&update_timestamps, worktree, blob2,
2065 ondisk_path, path, sb.st_mode, label_orig, blob,
2066 worktree->base_commit_id, repo,
2067 progress_cb, progress_arg);
2069 free(label_orig);
2070 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2071 err = got_error_from_errno("close");
2072 goto done;
2074 if (blob2)
2075 got_object_blob_close(blob2);
2076 if (err)
2077 goto done;
2079 * Do not update timestamps of files with local changes.
2080 * Otherwise, a future status walk would treat them as
2081 * unmodified files again.
2083 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2084 blob->id.sha1, worktree->base_commit_id->sha1,
2085 update_timestamps);
2086 } else if (status == GOT_STATUS_MODE_CHANGE) {
2087 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2088 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2089 } else if (status == GOT_STATUS_DELETE) {
2090 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2091 if (err)
2092 goto done;
2093 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2094 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2095 if (err)
2096 goto done;
2097 } else {
2098 int is_bad_symlink = 0;
2099 if (S_ISLNK(te->mode)) {
2100 err = install_symlink(&is_bad_symlink, worktree,
2101 ondisk_path, path, blob,
2102 status == GOT_STATUS_MISSING, 0,
2103 status == GOT_STATUS_UNVERSIONED, 0,
2104 repo, progress_cb, progress_arg);
2105 } else {
2106 err = install_blob(worktree, ondisk_path, path,
2107 te->mode, sb.st_mode, blob,
2108 status == GOT_STATUS_MISSING, 0, 0,
2109 status == GOT_STATUS_UNVERSIONED, repo,
2110 progress_cb, progress_arg);
2112 if (err)
2113 goto done;
2115 if (ie) {
2116 err = got_fileindex_entry_update(ie,
2117 worktree->root_fd, path, blob->id.sha1,
2118 worktree->base_commit_id->sha1, 1);
2119 } else {
2120 err = create_fileindex_entry(&ie, fileindex,
2121 worktree->base_commit_id, worktree->root_fd, path,
2122 &blob->id);
2124 if (err)
2125 goto done;
2127 if (is_bad_symlink) {
2128 got_fileindex_entry_filetype_set(ie,
2129 GOT_FILEIDX_MODE_BAD_SYMLINK);
2133 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2134 err = got_error_from_errno("close");
2135 goto done;
2137 got_object_blob_close(blob);
2138 done:
2139 free(ondisk_path);
2140 return err;
2143 static const struct got_error *
2144 remove_ondisk_file(const char *root_path, const char *path)
2146 const struct got_error *err = NULL;
2147 char *ondisk_path = NULL, *parent = NULL;
2149 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2150 return got_error_from_errno("asprintf");
2152 if (unlink(ondisk_path) == -1) {
2153 if (errno != ENOENT)
2154 err = got_error_from_errno2("unlink", ondisk_path);
2155 } else {
2156 size_t root_len = strlen(root_path);
2157 err = got_path_dirname(&parent, ondisk_path);
2158 if (err)
2159 goto done;
2160 while (got_path_cmp(parent, root_path,
2161 strlen(parent), root_len) != 0) {
2162 free(ondisk_path);
2163 ondisk_path = parent;
2164 parent = NULL;
2165 if (rmdir(ondisk_path) == -1) {
2166 if (errno != ENOTEMPTY)
2167 err = got_error_from_errno2("rmdir",
2168 ondisk_path);
2169 break;
2171 err = got_path_dirname(&parent, ondisk_path);
2172 if (err)
2173 break;
2176 done:
2177 free(ondisk_path);
2178 free(parent);
2179 return err;
2182 static const struct got_error *
2183 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2184 struct got_fileindex_entry *ie, struct got_repository *repo,
2185 got_worktree_checkout_cb progress_cb, void *progress_arg)
2187 const struct got_error *err = NULL;
2188 unsigned char status;
2189 struct stat sb;
2190 char *ondisk_path;
2192 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2193 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2195 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2196 == -1)
2197 return got_error_from_errno("asprintf");
2199 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2200 if (err)
2201 goto done;
2203 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2204 char ondisk_target[PATH_MAX];
2205 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2206 sizeof(ondisk_target));
2207 if (ondisk_len == -1) {
2208 err = got_error_from_errno2("readlink", ondisk_path);
2209 goto done;
2211 ondisk_target[ondisk_len] = '\0';
2212 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2213 NULL, NULL, /* XXX pass common ancestor info? */
2214 ondisk_target, ondisk_path);
2215 if (err)
2216 goto done;
2217 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2218 ie->path);
2219 goto done;
2222 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2223 status == GOT_STATUS_ADD) {
2224 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2225 if (err)
2226 goto done;
2228 * Preserve the working file and change the deleted blob's
2229 * entry into a schedule-add entry.
2231 err = got_fileindex_entry_update(ie, worktree->root_fd,
2232 ie->path, NULL, NULL, 0);
2233 } else {
2234 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2235 if (err)
2236 goto done;
2237 if (status == GOT_STATUS_NO_CHANGE) {
2238 err = remove_ondisk_file(worktree->root_path, ie->path);
2239 if (err)
2240 goto done;
2242 got_fileindex_entry_remove(fileindex, ie);
2244 done:
2245 free(ondisk_path);
2246 return err;
2249 struct diff_cb_arg {
2250 struct got_fileindex *fileindex;
2251 struct got_worktree *worktree;
2252 struct got_repository *repo;
2253 got_worktree_checkout_cb progress_cb;
2254 void *progress_arg;
2255 got_cancel_cb cancel_cb;
2256 void *cancel_arg;
2259 static const struct got_error *
2260 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2261 struct got_tree_entry *te, const char *parent_path)
2263 struct diff_cb_arg *a = arg;
2265 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2266 return got_error(GOT_ERR_CANCELLED);
2268 return update_blob(a->worktree, a->fileindex, ie, te,
2269 ie->path, a->repo, a->progress_cb, a->progress_arg);
2272 static const struct got_error *
2273 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2275 struct diff_cb_arg *a = arg;
2277 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2278 return got_error(GOT_ERR_CANCELLED);
2280 return delete_blob(a->worktree, a->fileindex, ie,
2281 a->repo, a->progress_cb, a->progress_arg);
2284 static const struct got_error *
2285 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2287 struct diff_cb_arg *a = arg;
2288 const struct got_error *err;
2289 char *path;
2291 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2292 return got_error(GOT_ERR_CANCELLED);
2294 if (got_object_tree_entry_is_submodule(te))
2295 return NULL;
2297 if (asprintf(&path, "%s%s%s", parent_path,
2298 parent_path[0] ? "/" : "", te->name)
2299 == -1)
2300 return got_error_from_errno("asprintf");
2302 if (S_ISDIR(te->mode))
2303 err = add_dir_on_disk(a->worktree, path);
2304 else
2305 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2306 a->repo, a->progress_cb, a->progress_arg);
2308 free(path);
2309 return err;
2312 const struct got_error *
2313 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2315 uint32_t uuid_status;
2317 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2318 if (uuid_status != uuid_s_ok) {
2319 *uuidstr = NULL;
2320 return got_error_uuid(uuid_status, "uuid_to_string");
2323 return NULL;
2326 static const struct got_error *
2327 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2329 const struct got_error *err = NULL;
2330 char *uuidstr = NULL;
2332 *refname = NULL;
2334 err = got_worktree_get_uuid(&uuidstr, worktree);
2335 if (err)
2336 return err;
2338 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2339 err = got_error_from_errno("asprintf");
2340 *refname = NULL;
2342 free(uuidstr);
2343 return err;
2346 const struct got_error *
2347 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2348 const char *prefix)
2350 return get_ref_name(refname, worktree, prefix);
2353 static const struct got_error *
2354 get_base_ref_name(char **refname, struct got_worktree *worktree)
2356 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2359 static const struct got_error *
2360 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2362 return get_ref_name(refname, worktree,
2363 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2366 static const struct got_error *
2367 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2369 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2372 static const struct got_error *
2373 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2375 return get_ref_name(refname, worktree,
2376 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2379 static const struct got_error *
2380 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2382 return get_ref_name(refname, worktree,
2383 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2386 static const struct got_error *
2387 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2389 return get_ref_name(refname, worktree,
2390 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2393 static const struct got_error *
2394 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2396 return get_ref_name(refname, worktree,
2397 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2400 static const struct got_error *
2401 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2403 return get_ref_name(refname, worktree,
2404 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2407 static const struct got_error *
2408 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2410 return get_ref_name(refname, worktree,
2411 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2414 const struct got_error *
2415 got_worktree_get_histedit_script_path(char **path,
2416 struct got_worktree *worktree)
2418 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2419 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2420 *path = NULL;
2421 return got_error_from_errno("asprintf");
2423 return NULL;
2426 static const struct got_error *
2427 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2429 return get_ref_name(refname, worktree,
2430 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2433 static const struct got_error *
2434 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2436 return get_ref_name(refname, worktree,
2437 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2441 * Prevent Git's garbage collector from deleting our base commit by
2442 * setting a reference to our base commit's ID.
2444 static const struct got_error *
2445 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2447 const struct got_error *err = NULL;
2448 struct got_reference *ref = NULL;
2449 char *refname;
2451 err = get_base_ref_name(&refname, worktree);
2452 if (err)
2453 return err;
2455 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2456 if (err)
2457 goto done;
2459 err = got_ref_write(ref, repo);
2460 done:
2461 free(refname);
2462 if (ref)
2463 got_ref_close(ref);
2464 return err;
2467 static const struct got_error *
2468 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2470 const struct got_error *err = NULL;
2472 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2473 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2474 err = got_error_from_errno("asprintf");
2475 *fileindex_path = NULL;
2477 return err;
2481 static const struct got_error *
2482 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2483 struct got_worktree *worktree)
2485 const struct got_error *err = NULL;
2486 FILE *index = NULL;
2488 *fileindex_path = NULL;
2489 *fileindex = got_fileindex_alloc();
2490 if (*fileindex == NULL)
2491 return got_error_from_errno("got_fileindex_alloc");
2493 err = get_fileindex_path(fileindex_path, worktree);
2494 if (err)
2495 goto done;
2497 index = fopen(*fileindex_path, "rbe");
2498 if (index == NULL) {
2499 if (errno != ENOENT)
2500 err = got_error_from_errno2("fopen", *fileindex_path);
2501 } else {
2502 err = got_fileindex_read(*fileindex, index);
2503 if (fclose(index) == EOF && err == NULL)
2504 err = got_error_from_errno("fclose");
2506 done:
2507 if (err) {
2508 free(*fileindex_path);
2509 *fileindex_path = NULL;
2510 got_fileindex_free(*fileindex);
2511 *fileindex = NULL;
2513 return err;
2516 struct bump_base_commit_id_arg {
2517 struct got_object_id *base_commit_id;
2518 const char *path;
2519 size_t path_len;
2520 const char *entry_name;
2521 got_worktree_checkout_cb progress_cb;
2522 void *progress_arg;
2525 static const struct got_error *
2526 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2528 const struct got_error *err;
2529 struct bump_base_commit_id_arg *a = arg;
2531 if (a->entry_name) {
2532 if (strcmp(ie->path, a->path) != 0)
2533 return NULL;
2534 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2535 return NULL;
2537 if (got_fileindex_entry_was_skipped(ie))
2538 return NULL;
2540 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2541 SHA1_DIGEST_LENGTH) == 0)
2542 return NULL;
2544 if (a->progress_cb) {
2545 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2546 ie->path);
2547 if (err)
2548 return err;
2550 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2551 return NULL;
2554 /* Bump base commit ID of all files within an updated part of the work tree. */
2555 static const struct got_error *
2556 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2557 struct got_fileindex *fileindex,
2558 got_worktree_checkout_cb progress_cb, void *progress_arg)
2560 struct bump_base_commit_id_arg bbc_arg;
2562 bbc_arg.base_commit_id = worktree->base_commit_id;
2563 bbc_arg.entry_name = NULL;
2564 bbc_arg.path = "";
2565 bbc_arg.path_len = 0;
2566 bbc_arg.progress_cb = progress_cb;
2567 bbc_arg.progress_arg = progress_arg;
2569 return got_fileindex_for_each_entry_safe(fileindex,
2570 bump_base_commit_id, &bbc_arg);
2573 static const struct got_error *
2574 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2576 const struct got_error *err = NULL;
2577 char *new_fileindex_path = NULL;
2578 FILE *new_index = NULL;
2579 struct timespec timeout;
2581 err = got_opentemp_named(&new_fileindex_path, &new_index,
2582 fileindex_path, "");
2583 if (err)
2584 goto done;
2586 err = got_fileindex_write(fileindex, new_index);
2587 if (err)
2588 goto done;
2590 if (rename(new_fileindex_path, fileindex_path) != 0) {
2591 err = got_error_from_errno3("rename", new_fileindex_path,
2592 fileindex_path);
2593 unlink(new_fileindex_path);
2597 * Sleep for a short amount of time to ensure that files modified after
2598 * this program exits have a different time stamp from the one which
2599 * was recorded in the file index.
2601 timeout.tv_sec = 0;
2602 timeout.tv_nsec = 1;
2603 nanosleep(&timeout, NULL);
2604 done:
2605 if (new_index)
2606 fclose(new_index);
2607 free(new_fileindex_path);
2608 return err;
2611 static const struct got_error *
2612 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2613 struct got_object_id **tree_id, const char *wt_relpath,
2614 struct got_commit_object *base_commit, struct got_worktree *worktree,
2615 struct got_repository *repo)
2617 const struct got_error *err = NULL;
2618 struct got_object_id *id = NULL;
2619 char *in_repo_path = NULL;
2620 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2622 *entry_type = GOT_OBJ_TYPE_ANY;
2623 *tree_relpath = NULL;
2624 *tree_id = NULL;
2626 if (wt_relpath[0] == '\0') {
2627 /* Check out all files within the work tree. */
2628 *entry_type = GOT_OBJ_TYPE_TREE;
2629 *tree_relpath = strdup("");
2630 if (*tree_relpath == NULL) {
2631 err = got_error_from_errno("strdup");
2632 goto done;
2634 err = got_object_id_by_path(tree_id, repo, base_commit,
2635 worktree->path_prefix);
2636 if (err)
2637 goto done;
2638 return NULL;
2641 /* Check out a subset of files in the work tree. */
2643 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2644 is_root_wt ? "" : "/", wt_relpath) == -1) {
2645 err = got_error_from_errno("asprintf");
2646 goto done;
2649 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2650 if (err)
2651 goto done;
2653 free(in_repo_path);
2654 in_repo_path = NULL;
2656 err = got_object_get_type(entry_type, repo, id);
2657 if (err)
2658 goto done;
2660 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2661 /* Check out a single file. */
2662 if (strchr(wt_relpath, '/') == NULL) {
2663 /* Check out a single file in work tree's root dir. */
2664 in_repo_path = strdup(worktree->path_prefix);
2665 if (in_repo_path == NULL) {
2666 err = got_error_from_errno("strdup");
2667 goto done;
2669 *tree_relpath = strdup("");
2670 if (*tree_relpath == NULL) {
2671 err = got_error_from_errno("strdup");
2672 goto done;
2674 } else {
2675 /* Check out a single file in a subdirectory. */
2676 err = got_path_dirname(tree_relpath, wt_relpath);
2677 if (err)
2678 return err;
2679 if (asprintf(&in_repo_path, "%s%s%s",
2680 worktree->path_prefix, is_root_wt ? "" : "/",
2681 *tree_relpath) == -1) {
2682 err = got_error_from_errno("asprintf");
2683 goto done;
2686 err = got_object_id_by_path(tree_id, repo,
2687 base_commit, in_repo_path);
2688 } else {
2689 /* Check out all files within a subdirectory. */
2690 *tree_id = got_object_id_dup(id);
2691 if (*tree_id == NULL) {
2692 err = got_error_from_errno("got_object_id_dup");
2693 goto done;
2695 *tree_relpath = strdup(wt_relpath);
2696 if (*tree_relpath == NULL) {
2697 err = got_error_from_errno("strdup");
2698 goto done;
2701 done:
2702 free(id);
2703 free(in_repo_path);
2704 if (err) {
2705 *entry_type = GOT_OBJ_TYPE_ANY;
2706 free(*tree_relpath);
2707 *tree_relpath = NULL;
2708 free(*tree_id);
2709 *tree_id = NULL;
2711 return err;
2714 static const struct got_error *
2715 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2716 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2717 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2718 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2720 const struct got_error *err = NULL;
2721 struct got_commit_object *commit = NULL;
2722 struct got_tree_object *tree = NULL;
2723 struct got_fileindex_diff_tree_cb diff_cb;
2724 struct diff_cb_arg arg;
2726 err = ref_base_commit(worktree, repo);
2727 if (err) {
2728 if (!(err->code == GOT_ERR_ERRNO &&
2729 (errno == EACCES || errno == EROFS)))
2730 goto done;
2731 err = (*progress_cb)(progress_arg,
2732 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2733 if (err)
2734 return err;
2737 err = got_object_open_as_commit(&commit, repo,
2738 worktree->base_commit_id);
2739 if (err)
2740 goto done;
2742 err = got_object_open_as_tree(&tree, repo, tree_id);
2743 if (err)
2744 goto done;
2746 if (entry_name &&
2747 got_object_tree_find_entry(tree, entry_name) == NULL) {
2748 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2749 goto done;
2752 diff_cb.diff_old_new = diff_old_new;
2753 diff_cb.diff_old = diff_old;
2754 diff_cb.diff_new = diff_new;
2755 arg.fileindex = fileindex;
2756 arg.worktree = worktree;
2757 arg.repo = repo;
2758 arg.progress_cb = progress_cb;
2759 arg.progress_arg = progress_arg;
2760 arg.cancel_cb = cancel_cb;
2761 arg.cancel_arg = cancel_arg;
2762 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2763 entry_name, repo, &diff_cb, &arg);
2764 done:
2765 if (tree)
2766 got_object_tree_close(tree);
2767 if (commit)
2768 got_object_commit_close(commit);
2769 return err;
2772 const struct got_error *
2773 got_worktree_checkout_files(struct got_worktree *worktree,
2774 struct got_pathlist_head *paths, struct got_repository *repo,
2775 got_worktree_checkout_cb progress_cb, void *progress_arg,
2776 got_cancel_cb cancel_cb, void *cancel_arg)
2778 const struct got_error *err = NULL, *sync_err, *unlockerr;
2779 struct got_commit_object *commit = NULL;
2780 struct got_tree_object *tree = NULL;
2781 struct got_fileindex *fileindex = NULL;
2782 char *fileindex_path = NULL;
2783 struct got_pathlist_entry *pe;
2784 struct tree_path_data {
2785 STAILQ_ENTRY(tree_path_data) entry;
2786 struct got_object_id *tree_id;
2787 int entry_type;
2788 char *relpath;
2789 char *entry_name;
2790 } *tpd = NULL;
2791 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2793 STAILQ_INIT(&tree_paths);
2795 err = lock_worktree(worktree, LOCK_EX);
2796 if (err)
2797 return err;
2799 err = got_object_open_as_commit(&commit, repo,
2800 worktree->base_commit_id);
2801 if (err)
2802 goto done;
2804 /* Map all specified paths to in-repository trees. */
2805 TAILQ_FOREACH(pe, paths, entry) {
2806 tpd = malloc(sizeof(*tpd));
2807 if (tpd == NULL) {
2808 err = got_error_from_errno("malloc");
2809 goto done;
2812 err = find_tree_entry_for_checkout(&tpd->entry_type,
2813 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2814 worktree, repo);
2815 if (err) {
2816 free(tpd);
2817 goto done;
2820 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2821 err = got_path_basename(&tpd->entry_name, pe->path);
2822 if (err) {
2823 free(tpd->relpath);
2824 free(tpd->tree_id);
2825 free(tpd);
2826 goto done;
2828 } else
2829 tpd->entry_name = NULL;
2831 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2835 * Read the file index.
2836 * Checking out files is supposed to be an idempotent operation.
2837 * If the on-disk file index is incomplete we will try to complete it.
2839 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2840 if (err)
2841 goto done;
2843 tpd = STAILQ_FIRST(&tree_paths);
2844 TAILQ_FOREACH(pe, paths, entry) {
2845 struct bump_base_commit_id_arg bbc_arg;
2847 err = checkout_files(worktree, fileindex, tpd->relpath,
2848 tpd->tree_id, tpd->entry_name, repo,
2849 progress_cb, progress_arg, cancel_cb, cancel_arg);
2850 if (err)
2851 break;
2853 bbc_arg.base_commit_id = worktree->base_commit_id;
2854 bbc_arg.entry_name = tpd->entry_name;
2855 bbc_arg.path = pe->path;
2856 bbc_arg.path_len = pe->path_len;
2857 bbc_arg.progress_cb = progress_cb;
2858 bbc_arg.progress_arg = progress_arg;
2859 err = got_fileindex_for_each_entry_safe(fileindex,
2860 bump_base_commit_id, &bbc_arg);
2861 if (err)
2862 break;
2864 tpd = STAILQ_NEXT(tpd, entry);
2866 sync_err = sync_fileindex(fileindex, fileindex_path);
2867 if (sync_err && err == NULL)
2868 err = sync_err;
2869 done:
2870 free(fileindex_path);
2871 if (tree)
2872 got_object_tree_close(tree);
2873 if (commit)
2874 got_object_commit_close(commit);
2875 if (fileindex)
2876 got_fileindex_free(fileindex);
2877 while (!STAILQ_EMPTY(&tree_paths)) {
2878 tpd = STAILQ_FIRST(&tree_paths);
2879 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2880 free(tpd->relpath);
2881 free(tpd->tree_id);
2882 free(tpd);
2884 unlockerr = lock_worktree(worktree, LOCK_SH);
2885 if (unlockerr && err == NULL)
2886 err = unlockerr;
2887 return err;
2890 static const struct got_error *
2891 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2892 struct got_fileindex_entry *ie, const char *ondisk_path,
2893 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2894 int restoring_missing_file, int reverting_versioned_file,
2895 int path_is_unversioned, int allow_bad_symlinks,
2896 struct got_repository *repo,
2897 got_worktree_checkout_cb progress_cb, void *progress_arg)
2899 const struct got_error *err = NULL;
2900 int is_bad_symlink = 0;
2902 if (S_ISLNK(mode2)) {
2903 err = install_symlink(&is_bad_symlink,
2904 worktree, ondisk_path, path2, blob2,
2905 restoring_missing_file,
2906 reverting_versioned_file,
2907 path_is_unversioned, allow_bad_symlinks,
2908 repo, progress_cb, progress_arg);
2909 } else {
2910 err = install_blob(worktree, ondisk_path, path2,
2911 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2912 restoring_missing_file, reverting_versioned_file, 0,
2913 path_is_unversioned, repo, progress_cb, progress_arg);
2915 if (err)
2916 return err;
2917 if (ie == NULL) {
2918 /* Adding an unversioned file. */
2919 err = got_fileindex_entry_alloc(&ie, path2);
2920 if (err)
2921 return err;
2922 err = got_fileindex_entry_update(ie,
2923 worktree->root_fd, path2, NULL, NULL, 1);
2924 if (err) {
2925 got_fileindex_entry_free(ie);
2926 return err;
2928 err = got_fileindex_entry_add(fileindex, ie);
2929 if (err) {
2930 got_fileindex_entry_free(ie);
2931 return err;
2933 } else {
2934 /* Re-adding a locally deleted file. */
2935 err = got_fileindex_entry_update(ie,
2936 worktree->root_fd, path2, ie->blob_sha1,
2937 worktree->base_commit_id->sha1, 0);
2938 if (err)
2939 return err;
2942 if (is_bad_symlink) {
2943 got_fileindex_entry_filetype_set(ie,
2944 GOT_FILEIDX_MODE_BAD_SYMLINK);
2947 return NULL;
2950 struct merge_file_cb_arg {
2951 struct got_worktree *worktree;
2952 struct got_fileindex *fileindex;
2953 got_worktree_checkout_cb progress_cb;
2954 void *progress_arg;
2955 got_cancel_cb cancel_cb;
2956 void *cancel_arg;
2957 const char *label_orig;
2958 struct got_object_id *commit_id2;
2959 int allow_bad_symlinks;
2962 static const struct got_error *
2963 merge_file_cb(void *arg, struct got_blob_object *blob1,
2964 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2965 struct got_object_id *id1, struct got_object_id *id2,
2966 const char *path1, const char *path2,
2967 mode_t mode1, mode_t mode2, struct got_repository *repo)
2969 static const struct got_error *err = NULL;
2970 struct merge_file_cb_arg *a = arg;
2971 struct got_fileindex_entry *ie;
2972 char *ondisk_path = NULL;
2973 struct stat sb;
2974 unsigned char status;
2975 int local_changes_subsumed;
2976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2977 char *id_str = NULL, *label_deriv2 = NULL;
2979 if (blob1 && blob2) {
2980 ie = got_fileindex_entry_get(a->fileindex, path2,
2981 strlen(path2));
2982 if (ie == NULL)
2983 return (*a->progress_cb)(a->progress_arg,
2984 GOT_STATUS_MISSING, path2);
2986 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2987 path2) == -1)
2988 return got_error_from_errno("asprintf");
2990 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2991 repo);
2992 if (err)
2993 goto done;
2995 if (status == GOT_STATUS_DELETE) {
2996 err = (*a->progress_cb)(a->progress_arg,
2997 GOT_STATUS_MERGE, path2);
2998 goto done;
3000 if (status != GOT_STATUS_NO_CHANGE &&
3001 status != GOT_STATUS_MODIFY &&
3002 status != GOT_STATUS_CONFLICT &&
3003 status != GOT_STATUS_ADD) {
3004 err = (*a->progress_cb)(a->progress_arg, status, path2);
3005 goto done;
3008 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3009 char *link_target2;
3010 err = got_object_blob_read_to_str(&link_target2, blob2);
3011 if (err)
3012 goto done;
3013 err = merge_symlink(a->worktree, blob1, ondisk_path,
3014 path2, a->label_orig, link_target2, a->commit_id2,
3015 repo, a->progress_cb, a->progress_arg);
3016 free(link_target2);
3017 } else {
3018 int fd;
3020 f_orig = got_opentemp();
3021 if (f_orig == NULL) {
3022 err = got_error_from_errno("got_opentemp");
3023 goto done;
3025 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3026 f_orig, blob1);
3027 if (err)
3028 goto done;
3030 f_deriv2 = got_opentemp();
3031 if (f_deriv2 == NULL)
3032 goto done;
3033 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3034 f_deriv2, blob2);
3035 if (err)
3036 goto done;
3038 fd = open(ondisk_path,
3039 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3040 if (fd == -1) {
3041 err = got_error_from_errno2("open",
3042 ondisk_path);
3043 goto done;
3045 f_deriv = fdopen(fd, "r");
3046 if (f_deriv == NULL) {
3047 err = got_error_from_errno2("fdopen",
3048 ondisk_path);
3049 close(fd);
3050 goto done;
3052 err = got_object_id_str(&id_str, a->commit_id2);
3053 if (err)
3054 goto done;
3055 if (asprintf(&label_deriv2, "%s: commit %s",
3056 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3057 err = got_error_from_errno("asprintf");
3058 goto done;
3060 err = merge_file(&local_changes_subsumed, a->worktree,
3061 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3062 mode2, a->label_orig, NULL, label_deriv2,
3063 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3064 a->progress_cb, a->progress_arg);
3066 } else if (blob1) {
3067 ie = got_fileindex_entry_get(a->fileindex, path1,
3068 strlen(path1));
3069 if (ie == NULL)
3070 return (*a->progress_cb)(a->progress_arg,
3071 GOT_STATUS_MISSING, path1);
3073 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3074 path1) == -1)
3075 return got_error_from_errno("asprintf");
3077 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3078 repo);
3079 if (err)
3080 goto done;
3082 switch (status) {
3083 case GOT_STATUS_NO_CHANGE:
3084 err = (*a->progress_cb)(a->progress_arg,
3085 GOT_STATUS_DELETE, path1);
3086 if (err)
3087 goto done;
3088 err = remove_ondisk_file(a->worktree->root_path, path1);
3089 if (err)
3090 goto done;
3091 if (ie)
3092 got_fileindex_entry_mark_deleted_from_disk(ie);
3093 break;
3094 case GOT_STATUS_DELETE:
3095 case GOT_STATUS_MISSING:
3096 err = (*a->progress_cb)(a->progress_arg,
3097 GOT_STATUS_DELETE, path1);
3098 if (err)
3099 goto done;
3100 if (ie)
3101 got_fileindex_entry_mark_deleted_from_disk(ie);
3102 break;
3103 case GOT_STATUS_ADD: {
3104 struct got_object_id *id;
3105 FILE *blob1_f;
3106 off_t blob1_size;
3108 * Delete the added file only if its content already
3109 * exists in the repository.
3111 err = got_object_blob_file_create(&id, &blob1_f,
3112 &blob1_size, path1);
3113 if (err)
3114 goto done;
3115 if (got_object_id_cmp(id, id1) == 0) {
3116 err = (*a->progress_cb)(a->progress_arg,
3117 GOT_STATUS_DELETE, path1);
3118 if (err)
3119 goto done;
3120 err = remove_ondisk_file(a->worktree->root_path,
3121 path1);
3122 if (err)
3123 goto done;
3124 if (ie)
3125 got_fileindex_entry_remove(a->fileindex,
3126 ie);
3127 } else {
3128 err = (*a->progress_cb)(a->progress_arg,
3129 GOT_STATUS_CANNOT_DELETE, path1);
3131 if (fclose(blob1_f) == EOF && err == NULL)
3132 err = got_error_from_errno("fclose");
3133 free(id);
3134 if (err)
3135 goto done;
3136 break;
3138 case GOT_STATUS_MODIFY:
3139 case GOT_STATUS_CONFLICT:
3140 err = (*a->progress_cb)(a->progress_arg,
3141 GOT_STATUS_CANNOT_DELETE, path1);
3142 if (err)
3143 goto done;
3144 break;
3145 case GOT_STATUS_OBSTRUCTED:
3146 err = (*a->progress_cb)(a->progress_arg, status, path1);
3147 if (err)
3148 goto done;
3149 break;
3150 default:
3151 break;
3153 } else if (blob2) {
3154 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3155 path2) == -1)
3156 return got_error_from_errno("asprintf");
3157 ie = got_fileindex_entry_get(a->fileindex, path2,
3158 strlen(path2));
3159 if (ie) {
3160 err = get_file_status(&status, &sb, ie, ondisk_path,
3161 -1, NULL, repo);
3162 if (err)
3163 goto done;
3164 if (status != GOT_STATUS_NO_CHANGE &&
3165 status != GOT_STATUS_MODIFY &&
3166 status != GOT_STATUS_CONFLICT &&
3167 status != GOT_STATUS_ADD &&
3168 status != GOT_STATUS_DELETE) {
3169 err = (*a->progress_cb)(a->progress_arg,
3170 status, path2);
3171 goto done;
3173 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3174 char *link_target2;
3175 err = got_object_blob_read_to_str(&link_target2,
3176 blob2);
3177 if (err)
3178 goto done;
3179 err = merge_symlink(a->worktree, NULL,
3180 ondisk_path, path2, a->label_orig,
3181 link_target2, a->commit_id2, repo,
3182 a->progress_cb, a->progress_arg);
3183 free(link_target2);
3184 } else if (S_ISREG(sb.st_mode)) {
3185 err = merge_blob(&local_changes_subsumed,
3186 a->worktree, NULL, ondisk_path, path2,
3187 sb.st_mode, a->label_orig, blob2,
3188 a->commit_id2, repo, a->progress_cb,
3189 a->progress_arg);
3190 } else if (status != GOT_STATUS_DELETE) {
3191 err = got_error_path(ondisk_path,
3192 GOT_ERR_FILE_OBSTRUCTED);
3194 if (err)
3195 goto done;
3196 if (status == GOT_STATUS_DELETE) {
3197 /* Re-add file with content from new blob. */
3198 err = add_file(a->worktree, a->fileindex, ie,
3199 ondisk_path, path2, blob2, mode2,
3200 0, 0, 0, a->allow_bad_symlinks,
3201 repo, a->progress_cb, a->progress_arg);
3202 if (err)
3203 goto done;
3205 } else {
3206 err = add_file(a->worktree, a->fileindex, NULL,
3207 ondisk_path, path2, blob2, mode2,
3208 0, 0, 1, a->allow_bad_symlinks,
3209 repo, a->progress_cb, a->progress_arg);
3210 if (err)
3211 goto done;
3214 done:
3215 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3216 err = got_error_from_errno("fclose");
3217 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3218 err = got_error_from_errno("fclose");
3219 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3220 err = got_error_from_errno("fclose");
3221 free(id_str);
3222 free(label_deriv2);
3223 free(ondisk_path);
3224 return err;
3227 static const struct got_error *
3228 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3230 struct got_worktree *worktree = arg;
3232 /* Reject merges into a work tree with mixed base commits. */
3233 if (got_fileindex_entry_has_commit(ie) &&
3234 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3235 SHA1_DIGEST_LENGTH) != 0)
3236 return got_error(GOT_ERR_MIXED_COMMITS);
3238 return NULL;
3241 struct check_merge_conflicts_arg {
3242 struct got_worktree *worktree;
3243 struct got_fileindex *fileindex;
3244 struct got_repository *repo;
3247 static const struct got_error *
3248 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3249 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3250 struct got_object_id *id1, struct got_object_id *id2,
3251 const char *path1, const char *path2,
3252 mode_t mode1, mode_t mode2, struct got_repository *repo)
3254 const struct got_error *err = NULL;
3255 struct check_merge_conflicts_arg *a = arg;
3256 unsigned char status;
3257 struct stat sb;
3258 struct got_fileindex_entry *ie;
3259 const char *path = path2 ? path2 : path1;
3260 struct got_object_id *id = id2 ? id2 : id1;
3261 char *ondisk_path;
3263 if (id == NULL)
3264 return NULL;
3266 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3267 if (ie == NULL)
3268 return NULL;
3270 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3271 == -1)
3272 return got_error_from_errno("asprintf");
3274 /* Reject merges into a work tree with conflicted files. */
3275 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3276 free(ondisk_path);
3277 if (err)
3278 return err;
3279 if (status == GOT_STATUS_CONFLICT)
3280 return got_error(GOT_ERR_CONFLICTS);
3282 return NULL;
3285 static const struct got_error *
3286 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3287 const char *fileindex_path, struct got_object_id *commit_id1,
3288 struct got_object_id *commit_id2, struct got_repository *repo,
3289 got_worktree_checkout_cb progress_cb, void *progress_arg,
3290 got_cancel_cb cancel_cb, void *cancel_arg)
3292 const struct got_error *err = NULL, *sync_err;
3293 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3294 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3295 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3296 struct check_merge_conflicts_arg cmc_arg;
3297 struct merge_file_cb_arg arg;
3298 char *label_orig = NULL;
3299 FILE *f1 = NULL, *f2 = NULL;
3300 int fd1 = -1, fd2 = -1;
3302 if (commit_id1) {
3303 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3304 if (err)
3305 goto done;
3306 err = got_object_id_by_path(&tree_id1, repo, commit1,
3307 worktree->path_prefix);
3308 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3309 goto done;
3311 if (tree_id1) {
3312 char *id_str;
3314 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3315 if (err)
3316 goto done;
3318 err = got_object_id_str(&id_str, commit_id1);
3319 if (err)
3320 goto done;
3322 if (asprintf(&label_orig, "%s: commit %s",
3323 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3324 err = got_error_from_errno("asprintf");
3325 free(id_str);
3326 goto done;
3328 free(id_str);
3330 f1 = got_opentemp();
3331 if (f1 == NULL) {
3332 err = got_error_from_errno("got_opentemp");
3333 goto done;
3337 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3338 if (err)
3339 goto done;
3341 err = got_object_id_by_path(&tree_id2, repo, commit2,
3342 worktree->path_prefix);
3343 if (err)
3344 goto done;
3346 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3347 if (err)
3348 goto done;
3350 f2 = got_opentemp();
3351 if (f2 == NULL) {
3352 err = got_error_from_errno("got_opentemp");
3353 goto done;
3356 fd1 = got_opentempfd();
3357 if (fd1 == -1) {
3358 err = got_error_from_errno("got_opentempfd");
3359 goto done;
3362 fd2 = got_opentempfd();
3363 if (fd2 == -1) {
3364 err = got_error_from_errno("got_opentempfd");
3365 goto done;
3368 cmc_arg.worktree = worktree;
3369 cmc_arg.fileindex = fileindex;
3370 cmc_arg.repo = repo;
3371 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3372 check_merge_conflicts, &cmc_arg, 0);
3373 if (err)
3374 goto done;
3376 arg.worktree = worktree;
3377 arg.fileindex = fileindex;
3378 arg.progress_cb = progress_cb;
3379 arg.progress_arg = progress_arg;
3380 arg.cancel_cb = cancel_cb;
3381 arg.cancel_arg = cancel_arg;
3382 arg.label_orig = label_orig;
3383 arg.commit_id2 = commit_id2;
3384 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3385 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3386 merge_file_cb, &arg, 1);
3387 sync_err = sync_fileindex(fileindex, fileindex_path);
3388 if (sync_err && err == NULL)
3389 err = sync_err;
3390 done:
3391 if (commit1)
3392 got_object_commit_close(commit1);
3393 if (commit2)
3394 got_object_commit_close(commit2);
3395 if (tree1)
3396 got_object_tree_close(tree1);
3397 if (tree2)
3398 got_object_tree_close(tree2);
3399 if (f1 && fclose(f1) == EOF && err == NULL)
3400 err = got_error_from_errno("fclose");
3401 if (f2 && fclose(f2) == EOF && err == NULL)
3402 err = got_error_from_errno("fclose");
3403 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3404 err = got_error_from_errno("close");
3405 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3406 err = got_error_from_errno("close");
3407 free(label_orig);
3408 return err;
3411 const struct got_error *
3412 got_worktree_merge_files(struct got_worktree *worktree,
3413 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3414 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3415 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3417 const struct got_error *err, *unlockerr;
3418 char *fileindex_path = NULL;
3419 struct got_fileindex *fileindex = NULL;
3421 err = lock_worktree(worktree, LOCK_EX);
3422 if (err)
3423 return err;
3425 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3426 if (err)
3427 goto done;
3429 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3430 worktree);
3431 if (err)
3432 goto done;
3434 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3435 commit_id2, repo, progress_cb, progress_arg,
3436 cancel_cb, cancel_arg);
3437 done:
3438 if (fileindex)
3439 got_fileindex_free(fileindex);
3440 free(fileindex_path);
3441 unlockerr = lock_worktree(worktree, LOCK_SH);
3442 if (unlockerr && err == NULL)
3443 err = unlockerr;
3444 return err;
3447 struct diff_dir_cb_arg {
3448 struct got_fileindex *fileindex;
3449 struct got_worktree *worktree;
3450 const char *status_path;
3451 size_t status_path_len;
3452 struct got_repository *repo;
3453 got_worktree_status_cb status_cb;
3454 void *status_arg;
3455 got_cancel_cb cancel_cb;
3456 void *cancel_arg;
3457 /* A pathlist containing per-directory pathlists of ignore patterns. */
3458 struct got_pathlist_head *ignores;
3459 int report_unchanged;
3460 int no_ignores;
3463 static const struct got_error *
3464 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3465 int dirfd, const char *de_name,
3466 got_worktree_status_cb status_cb, void *status_arg,
3467 struct got_repository *repo, int report_unchanged)
3469 const struct got_error *err = NULL;
3470 unsigned char status = GOT_STATUS_NO_CHANGE;
3471 unsigned char staged_status;
3472 struct stat sb;
3473 struct got_object_id blob_id, commit_id, staged_blob_id;
3474 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3475 struct got_object_id *staged_blob_idp = NULL;
3477 staged_status = get_staged_status(ie);
3478 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3479 if (err)
3480 return err;
3482 if (status == GOT_STATUS_NO_CHANGE &&
3483 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3484 return NULL;
3486 if (got_fileindex_entry_has_blob(ie))
3487 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3488 if (got_fileindex_entry_has_commit(ie))
3489 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3490 if (staged_status == GOT_STATUS_ADD ||
3491 staged_status == GOT_STATUS_MODIFY) {
3492 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3493 &staged_blob_id, ie);
3496 return (*status_cb)(status_arg, status, staged_status,
3497 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3500 static const struct got_error *
3501 status_old_new(void *arg, struct got_fileindex_entry *ie,
3502 struct dirent *de, const char *parent_path, int dirfd)
3504 const struct got_error *err = NULL;
3505 struct diff_dir_cb_arg *a = arg;
3506 char *abspath;
3508 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3509 return got_error(GOT_ERR_CANCELLED);
3511 if (got_path_cmp(parent_path, a->status_path,
3512 strlen(parent_path), a->status_path_len) != 0 &&
3513 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3514 return NULL;
3516 if (parent_path[0]) {
3517 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3518 parent_path, de->d_name) == -1)
3519 return got_error_from_errno("asprintf");
3520 } else {
3521 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3522 de->d_name) == -1)
3523 return got_error_from_errno("asprintf");
3526 err = report_file_status(ie, abspath, dirfd, de->d_name,
3527 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3528 free(abspath);
3529 return err;
3532 static const struct got_error *
3533 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3535 struct diff_dir_cb_arg *a = arg;
3536 struct got_object_id blob_id, commit_id;
3537 unsigned char status;
3539 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3540 return got_error(GOT_ERR_CANCELLED);
3542 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3543 return NULL;
3545 got_fileindex_entry_get_blob_id(&blob_id, ie);
3546 got_fileindex_entry_get_commit_id(&commit_id, ie);
3547 if (got_fileindex_entry_has_file_on_disk(ie))
3548 status = GOT_STATUS_MISSING;
3549 else
3550 status = GOT_STATUS_DELETE;
3551 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3552 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3555 static void
3556 free_ignores(struct got_pathlist_head *ignores)
3558 struct got_pathlist_entry *pe;
3560 TAILQ_FOREACH(pe, ignores, entry) {
3561 struct got_pathlist_head *ignorelist = pe->data;
3563 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3565 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3568 static const struct got_error *
3569 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3571 const struct got_error *err = NULL;
3572 struct got_pathlist_entry *pe = NULL;
3573 struct got_pathlist_head *ignorelist;
3574 char *line = NULL, *pattern, *dirpath = NULL;
3575 size_t linesize = 0;
3576 ssize_t linelen;
3578 ignorelist = calloc(1, sizeof(*ignorelist));
3579 if (ignorelist == NULL)
3580 return got_error_from_errno("calloc");
3581 TAILQ_INIT(ignorelist);
3583 while ((linelen = getline(&line, &linesize, f)) != -1) {
3584 if (linelen > 0 && line[linelen - 1] == '\n')
3585 line[linelen - 1] = '\0';
3587 /* Git's ignores may contain comments. */
3588 if (line[0] == '#')
3589 continue;
3591 /* Git's negated patterns are not (yet?) supported. */
3592 if (line[0] == '!')
3593 continue;
3595 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3596 line) == -1) {
3597 err = got_error_from_errno("asprintf");
3598 goto done;
3600 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3601 if (err)
3602 goto done;
3604 if (ferror(f)) {
3605 err = got_error_from_errno("getline");
3606 goto done;
3609 dirpath = strdup(path);
3610 if (dirpath == NULL) {
3611 err = got_error_from_errno("strdup");
3612 goto done;
3614 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3615 done:
3616 free(line);
3617 if (err || pe == NULL) {
3618 free(dirpath);
3619 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3621 return err;
3624 static int
3625 match_path(const char *pattern, size_t pattern_len, const char *path,
3626 int flags)
3628 char buf[PATH_MAX];
3631 * Trailing slashes signify directories.
3632 * Append a * to make such patterns conform to fnmatch rules.
3634 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3635 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3636 return FNM_NOMATCH; /* XXX */
3638 return fnmatch(buf, path, flags);
3641 return fnmatch(pattern, path, flags);
3644 static int
3645 match_ignores(struct got_pathlist_head *ignores, const char *path)
3647 struct got_pathlist_entry *pe;
3649 /* Handle patterns which match in all directories. */
3650 TAILQ_FOREACH(pe, ignores, entry) {
3651 struct got_pathlist_head *ignorelist = pe->data;
3652 struct got_pathlist_entry *pi;
3654 TAILQ_FOREACH(pi, ignorelist, entry) {
3655 const char *p;
3657 if (pi->path_len < 3 ||
3658 strncmp(pi->path, "**/", 3) != 0)
3659 continue;
3660 p = path;
3661 while (*p) {
3662 if (match_path(pi->path + 3,
3663 pi->path_len - 3, p,
3664 FNM_PATHNAME | FNM_LEADING_DIR)) {
3665 /* Retry in next directory. */
3666 while (*p && *p != '/')
3667 p++;
3668 while (*p == '/')
3669 p++;
3670 continue;
3672 return 1;
3678 * The ignores pathlist contains ignore lists from children before
3679 * parents, so we can find the most specific ignorelist by walking
3680 * ignores backwards.
3682 pe = TAILQ_LAST(ignores, got_pathlist_head);
3683 while (pe) {
3684 if (got_path_is_child(path, pe->path, pe->path_len)) {
3685 struct got_pathlist_head *ignorelist = pe->data;
3686 struct got_pathlist_entry *pi;
3687 TAILQ_FOREACH(pi, ignorelist, entry) {
3688 int flags = FNM_LEADING_DIR;
3689 if (strstr(pi->path, "/**/") == NULL)
3690 flags |= FNM_PATHNAME;
3691 if (match_path(pi->path, pi->path_len,
3692 path, flags))
3693 continue;
3694 return 1;
3697 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3700 return 0;
3703 static const struct got_error *
3704 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3705 const char *path, int dirfd, const char *ignores_filename)
3707 const struct got_error *err = NULL;
3708 char *ignorespath;
3709 int fd = -1;
3710 FILE *ignoresfile = NULL;
3712 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3713 path[0] ? "/" : "", ignores_filename) == -1)
3714 return got_error_from_errno("asprintf");
3716 if (dirfd != -1) {
3717 fd = openat(dirfd, ignores_filename,
3718 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3719 if (fd == -1) {
3720 if (errno != ENOENT && errno != EACCES)
3721 err = got_error_from_errno2("openat",
3722 ignorespath);
3723 } else {
3724 ignoresfile = fdopen(fd, "r");
3725 if (ignoresfile == NULL)
3726 err = got_error_from_errno2("fdopen",
3727 ignorespath);
3728 else {
3729 fd = -1;
3730 err = read_ignores(ignores, path, ignoresfile);
3733 } else {
3734 ignoresfile = fopen(ignorespath, "re");
3735 if (ignoresfile == NULL) {
3736 if (errno != ENOENT && errno != EACCES)
3737 err = got_error_from_errno2("fopen",
3738 ignorespath);
3739 } else
3740 err = read_ignores(ignores, path, ignoresfile);
3743 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3744 err = got_error_from_errno2("fclose", path);
3745 if (fd != -1 && close(fd) == -1 && err == NULL)
3746 err = got_error_from_errno2("close", path);
3747 free(ignorespath);
3748 return err;
3751 static const struct got_error *
3752 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3753 int dirfd)
3755 const struct got_error *err = NULL;
3756 struct diff_dir_cb_arg *a = arg;
3757 char *path = NULL;
3759 if (ignore != NULL)
3760 *ignore = 0;
3762 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3763 return got_error(GOT_ERR_CANCELLED);
3765 if (parent_path[0]) {
3766 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3767 return got_error_from_errno("asprintf");
3768 } else {
3769 path = de->d_name;
3772 if (de->d_type == DT_DIR) {
3773 if (!a->no_ignores && ignore != NULL &&
3774 match_ignores(a->ignores, path))
3775 *ignore = 1;
3776 } else if (!match_ignores(a->ignores, path) &&
3777 got_path_is_child(path, a->status_path, a->status_path_len))
3778 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3779 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3780 if (parent_path[0])
3781 free(path);
3782 return err;
3785 static const struct got_error *
3786 status_traverse(void *arg, const char *path, int dirfd)
3788 const struct got_error *err = NULL;
3789 struct diff_dir_cb_arg *a = arg;
3791 if (a->no_ignores)
3792 return NULL;
3794 err = add_ignores(a->ignores, a->worktree->root_path,
3795 path, dirfd, ".cvsignore");
3796 if (err)
3797 return err;
3799 err = add_ignores(a->ignores, a->worktree->root_path, path,
3800 dirfd, ".gitignore");
3802 return err;
3805 static const struct got_error *
3806 report_single_file_status(const char *path, const char *ondisk_path,
3807 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3808 void *status_arg, struct got_repository *repo, int report_unchanged,
3809 struct got_pathlist_head *ignores, int no_ignores)
3811 struct got_fileindex_entry *ie;
3812 struct stat sb;
3814 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3815 if (ie)
3816 return report_file_status(ie, ondisk_path, -1, NULL,
3817 status_cb, status_arg, repo, report_unchanged);
3819 if (lstat(ondisk_path, &sb) == -1) {
3820 if (errno != ENOENT)
3821 return got_error_from_errno2("lstat", ondisk_path);
3822 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3823 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3826 if (!no_ignores && match_ignores(ignores, path))
3827 return NULL;
3829 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3830 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3831 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3833 return NULL;
3836 static const struct got_error *
3837 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3838 const char *root_path, const char *path)
3840 const struct got_error *err;
3841 char *parent_path, *next_parent_path = NULL;
3843 err = add_ignores(ignores, root_path, "", -1,
3844 ".cvsignore");
3845 if (err)
3846 return err;
3848 err = add_ignores(ignores, root_path, "", -1,
3849 ".gitignore");
3850 if (err)
3851 return err;
3853 err = got_path_dirname(&parent_path, path);
3854 if (err) {
3855 if (err->code == GOT_ERR_BAD_PATH)
3856 return NULL; /* cannot traverse parent */
3857 return err;
3859 for (;;) {
3860 err = add_ignores(ignores, root_path, parent_path, -1,
3861 ".cvsignore");
3862 if (err)
3863 break;
3864 err = add_ignores(ignores, root_path, parent_path, -1,
3865 ".gitignore");
3866 if (err)
3867 break;
3868 err = got_path_dirname(&next_parent_path, parent_path);
3869 if (err) {
3870 if (err->code == GOT_ERR_BAD_PATH)
3871 err = NULL; /* traversed everything */
3872 break;
3874 if (got_path_is_root_dir(parent_path))
3875 break;
3876 free(parent_path);
3877 parent_path = next_parent_path;
3878 next_parent_path = NULL;
3881 free(parent_path);
3882 free(next_parent_path);
3883 return err;
3886 struct find_missing_children_args {
3887 const char *parent_path;
3888 size_t parent_len;
3889 struct got_pathlist_head *children;
3890 got_cancel_cb cancel_cb;
3891 void *cancel_arg;
3894 static const struct got_error *
3895 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3897 const struct got_error *err = NULL;
3898 struct find_missing_children_args *a = arg;
3900 if (a->cancel_cb) {
3901 err = a->cancel_cb(a->cancel_arg);
3902 if (err)
3903 return err;
3906 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3907 err = got_pathlist_append(a->children, ie->path, NULL);
3909 return err;
3912 static const struct got_error *
3913 report_children(struct got_pathlist_head *children,
3914 struct got_worktree *worktree, struct got_fileindex *fileindex,
3915 struct got_repository *repo, int is_root_dir, int report_unchanged,
3916 struct got_pathlist_head *ignores, int no_ignores,
3917 got_worktree_status_cb status_cb, void *status_arg,
3918 got_cancel_cb cancel_cb, void *cancel_arg)
3920 const struct got_error *err = NULL;
3921 struct got_pathlist_entry *pe;
3922 char *ondisk_path = NULL;
3924 TAILQ_FOREACH(pe, children, entry) {
3925 if (cancel_cb) {
3926 err = cancel_cb(cancel_arg);
3927 if (err)
3928 break;
3931 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3932 !is_root_dir ? "/" : "", pe->path) == -1) {
3933 err = got_error_from_errno("asprintf");
3934 ondisk_path = NULL;
3935 break;
3938 err = report_single_file_status(pe->path, ondisk_path,
3939 fileindex, status_cb, status_arg, repo, report_unchanged,
3940 ignores, no_ignores);
3941 if (err)
3942 break;
3944 free(ondisk_path);
3945 ondisk_path = NULL;
3948 free(ondisk_path);
3949 return err;
3952 static const struct got_error *
3953 worktree_status(struct got_worktree *worktree, const char *path,
3954 struct got_fileindex *fileindex, struct got_repository *repo,
3955 got_worktree_status_cb status_cb, void *status_arg,
3956 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3957 int report_unchanged)
3959 const struct got_error *err = NULL;
3960 int fd = -1;
3961 struct got_fileindex_diff_dir_cb fdiff_cb;
3962 struct diff_dir_cb_arg arg;
3963 char *ondisk_path = NULL;
3964 struct got_pathlist_head ignores, missing_children;
3965 struct got_fileindex_entry *ie;
3967 TAILQ_INIT(&ignores);
3968 TAILQ_INIT(&missing_children);
3970 if (asprintf(&ondisk_path, "%s%s%s",
3971 worktree->root_path, path[0] ? "/" : "", path) == -1)
3972 return got_error_from_errno("asprintf");
3974 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3975 if (ie) {
3976 err = report_single_file_status(path, ondisk_path,
3977 fileindex, status_cb, status_arg, repo,
3978 report_unchanged, &ignores, no_ignores);
3979 goto done;
3980 } else {
3981 struct find_missing_children_args fmca;
3982 fmca.parent_path = path;
3983 fmca.parent_len = strlen(path);
3984 fmca.children = &missing_children;
3985 fmca.cancel_cb = cancel_cb;
3986 fmca.cancel_arg = cancel_arg;
3987 err = got_fileindex_for_each_entry_safe(fileindex,
3988 find_missing_children, &fmca);
3989 if (err)
3990 goto done;
3993 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3994 if (fd == -1) {
3995 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3996 !got_err_open_nofollow_on_symlink())
3997 err = got_error_from_errno2("open", ondisk_path);
3998 else {
3999 if (!no_ignores) {
4000 err = add_ignores_from_parent_paths(&ignores,
4001 worktree->root_path, ondisk_path);
4002 if (err)
4003 goto done;
4005 if (TAILQ_EMPTY(&missing_children)) {
4006 err = report_single_file_status(path,
4007 ondisk_path, fileindex,
4008 status_cb, status_arg, repo,
4009 report_unchanged, &ignores, no_ignores);
4010 if (err)
4011 goto done;
4012 } else {
4013 err = report_children(&missing_children,
4014 worktree, fileindex, repo,
4015 (path[0] == '\0'), report_unchanged,
4016 &ignores, no_ignores,
4017 status_cb, status_arg,
4018 cancel_cb, cancel_arg);
4019 if (err)
4020 goto done;
4023 } else {
4024 fdiff_cb.diff_old_new = status_old_new;
4025 fdiff_cb.diff_old = status_old;
4026 fdiff_cb.diff_new = status_new;
4027 fdiff_cb.diff_traverse = status_traverse;
4028 arg.fileindex = fileindex;
4029 arg.worktree = worktree;
4030 arg.status_path = path;
4031 arg.status_path_len = strlen(path);
4032 arg.repo = repo;
4033 arg.status_cb = status_cb;
4034 arg.status_arg = status_arg;
4035 arg.cancel_cb = cancel_cb;
4036 arg.cancel_arg = cancel_arg;
4037 arg.report_unchanged = report_unchanged;
4038 arg.no_ignores = no_ignores;
4039 if (!no_ignores) {
4040 err = add_ignores_from_parent_paths(&ignores,
4041 worktree->root_path, path);
4042 if (err)
4043 goto done;
4045 arg.ignores = &ignores;
4046 err = got_fileindex_diff_dir(fileindex, fd,
4047 worktree->root_path, path, repo, &fdiff_cb, &arg);
4049 done:
4050 free_ignores(&ignores);
4051 if (fd != -1 && close(fd) == -1 && err == NULL)
4052 err = got_error_from_errno("close");
4053 free(ondisk_path);
4054 return err;
4057 const struct got_error *
4058 got_worktree_status(struct got_worktree *worktree,
4059 struct got_pathlist_head *paths, struct got_repository *repo,
4060 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4061 got_cancel_cb cancel_cb, void *cancel_arg)
4063 const struct got_error *err = NULL;
4064 char *fileindex_path = NULL;
4065 struct got_fileindex *fileindex = NULL;
4066 struct got_pathlist_entry *pe;
4068 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4069 if (err)
4070 return err;
4072 TAILQ_FOREACH(pe, paths, entry) {
4073 err = worktree_status(worktree, pe->path, fileindex, repo,
4074 status_cb, status_arg, cancel_cb, cancel_arg,
4075 no_ignores, 0);
4076 if (err)
4077 break;
4079 free(fileindex_path);
4080 got_fileindex_free(fileindex);
4081 return err;
4084 const struct got_error *
4085 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4086 const char *arg)
4088 const struct got_error *err = NULL;
4089 char *resolved = NULL, *cwd = NULL, *path = NULL;
4090 size_t len;
4091 struct stat sb;
4092 char *abspath = NULL;
4093 char canonpath[PATH_MAX];
4095 *wt_path = NULL;
4097 cwd = getcwd(NULL, 0);
4098 if (cwd == NULL)
4099 return got_error_from_errno("getcwd");
4101 if (lstat(arg, &sb) == -1) {
4102 if (errno != ENOENT) {
4103 err = got_error_from_errno2("lstat", arg);
4104 goto done;
4106 sb.st_mode = 0;
4108 if (S_ISLNK(sb.st_mode)) {
4110 * We cannot use realpath(3) with symlinks since we want to
4111 * operate on the symlink itself.
4112 * But we can make the path absolute, assuming it is relative
4113 * to the current working directory, and then canonicalize it.
4115 if (!got_path_is_absolute(arg)) {
4116 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4117 err = got_error_from_errno("asprintf");
4118 goto done;
4122 err = got_canonpath(abspath ? abspath : arg, canonpath,
4123 sizeof(canonpath));
4124 if (err)
4125 goto done;
4126 resolved = strdup(canonpath);
4127 if (resolved == NULL) {
4128 err = got_error_from_errno("strdup");
4129 goto done;
4131 } else {
4132 resolved = realpath(arg, NULL);
4133 if (resolved == NULL) {
4134 if (errno != ENOENT) {
4135 err = got_error_from_errno2("realpath", arg);
4136 goto done;
4138 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4139 err = got_error_from_errno("asprintf");
4140 goto done;
4142 err = got_canonpath(abspath, canonpath,
4143 sizeof(canonpath));
4144 if (err)
4145 goto done;
4146 resolved = strdup(canonpath);
4147 if (resolved == NULL) {
4148 err = got_error_from_errno("strdup");
4149 goto done;
4154 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4155 strlen(got_worktree_get_root_path(worktree)))) {
4156 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4157 goto done;
4160 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4161 err = got_path_skip_common_ancestor(&path,
4162 got_worktree_get_root_path(worktree), resolved);
4163 if (err)
4164 goto done;
4165 } else {
4166 path = strdup("");
4167 if (path == NULL) {
4168 err = got_error_from_errno("strdup");
4169 goto done;
4173 /* XXX status walk can't deal with trailing slash! */
4174 len = strlen(path);
4175 while (len > 0 && path[len - 1] == '/') {
4176 path[len - 1] = '\0';
4177 len--;
4179 done:
4180 free(abspath);
4181 free(resolved);
4182 free(cwd);
4183 if (err == NULL)
4184 *wt_path = path;
4185 else
4186 free(path);
4187 return err;
4190 struct schedule_addition_args {
4191 struct got_worktree *worktree;
4192 struct got_fileindex *fileindex;
4193 got_worktree_checkout_cb progress_cb;
4194 void *progress_arg;
4195 struct got_repository *repo;
4198 static int
4199 add_noop_status(unsigned char status)
4201 return (status == GOT_STATUS_ADD ||
4202 status == GOT_STATUS_MODIFY ||
4203 status == GOT_STATUS_CONFLICT ||
4204 status == GOT_STATUS_MODE_CHANGE ||
4205 status == GOT_STATUS_NO_CHANGE);
4208 static const struct got_error *
4209 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4210 const char *relpath, struct got_object_id *blob_id,
4211 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4212 int dirfd, const char *de_name)
4214 struct schedule_addition_args *a = arg;
4215 const struct got_error *err = NULL;
4216 struct got_fileindex_entry *ie;
4217 struct stat sb;
4218 char *ondisk_path;
4220 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4221 relpath) == -1)
4222 return got_error_from_errno("asprintf");
4224 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4225 if (ie) {
4226 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4227 de_name, a->repo);
4228 if (err)
4229 goto done;
4230 /* Re-adding an existing entry is a no-op. */
4231 if (staged_status == GOT_STATUS_NO_CHANGE &&
4232 add_noop_status(status))
4233 goto done;
4234 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4235 if (err)
4236 goto done;
4239 if (status != GOT_STATUS_UNVERSIONED) {
4240 if (status == GOT_STATUS_NONEXISTENT)
4241 err = got_error_set_errno(ENOENT, ondisk_path);
4242 else
4243 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4244 goto done;
4247 err = got_fileindex_entry_alloc(&ie, relpath);
4248 if (err)
4249 goto done;
4250 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4251 relpath, NULL, NULL, 1);
4252 if (err) {
4253 got_fileindex_entry_free(ie);
4254 goto done;
4256 err = got_fileindex_entry_add(a->fileindex, ie);
4257 if (err) {
4258 got_fileindex_entry_free(ie);
4259 goto done;
4261 done:
4262 free(ondisk_path);
4263 if (err)
4264 return err;
4265 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4266 return NULL;
4267 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4270 const struct got_error *
4271 got_worktree_schedule_add(struct got_worktree *worktree,
4272 struct got_pathlist_head *paths,
4273 got_worktree_checkout_cb progress_cb, void *progress_arg,
4274 struct got_repository *repo, int no_ignores)
4276 struct got_fileindex *fileindex = NULL;
4277 char *fileindex_path = NULL;
4278 const struct got_error *err = NULL, *sync_err, *unlockerr;
4279 struct got_pathlist_entry *pe;
4280 struct schedule_addition_args saa;
4282 err = lock_worktree(worktree, LOCK_EX);
4283 if (err)
4284 return err;
4286 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4287 if (err)
4288 goto done;
4290 saa.worktree = worktree;
4291 saa.fileindex = fileindex;
4292 saa.progress_cb = progress_cb;
4293 saa.progress_arg = progress_arg;
4294 saa.repo = repo;
4296 TAILQ_FOREACH(pe, paths, entry) {
4297 err = worktree_status(worktree, pe->path, fileindex, repo,
4298 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4299 if (err)
4300 break;
4302 sync_err = sync_fileindex(fileindex, fileindex_path);
4303 if (sync_err && err == NULL)
4304 err = sync_err;
4305 done:
4306 free(fileindex_path);
4307 if (fileindex)
4308 got_fileindex_free(fileindex);
4309 unlockerr = lock_worktree(worktree, LOCK_SH);
4310 if (unlockerr && err == NULL)
4311 err = unlockerr;
4312 return err;
4315 struct schedule_deletion_args {
4316 struct got_worktree *worktree;
4317 struct got_fileindex *fileindex;
4318 got_worktree_delete_cb progress_cb;
4319 void *progress_arg;
4320 struct got_repository *repo;
4321 int delete_local_mods;
4322 int keep_on_disk;
4323 int ignore_missing_paths;
4324 const char *status_path;
4325 size_t status_path_len;
4326 const char *status_codes;
4329 static const struct got_error *
4330 schedule_for_deletion(void *arg, unsigned char status,
4331 unsigned char staged_status, const char *relpath,
4332 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4333 struct got_object_id *commit_id, int dirfd, const char *de_name)
4335 struct schedule_deletion_args *a = arg;
4336 const struct got_error *err = NULL;
4337 struct got_fileindex_entry *ie = NULL;
4338 struct stat sb;
4339 char *ondisk_path;
4341 if (status == GOT_STATUS_NONEXISTENT) {
4342 if (a->ignore_missing_paths)
4343 return NULL;
4344 return got_error_set_errno(ENOENT, relpath);
4347 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4348 if (ie == NULL)
4349 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4351 staged_status = get_staged_status(ie);
4352 if (staged_status != GOT_STATUS_NO_CHANGE) {
4353 if (staged_status == GOT_STATUS_DELETE)
4354 return NULL;
4355 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4358 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4359 relpath) == -1)
4360 return got_error_from_errno("asprintf");
4362 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4363 a->repo);
4364 if (err)
4365 goto done;
4367 if (a->status_codes) {
4368 size_t ncodes = strlen(a->status_codes);
4369 int i;
4370 for (i = 0; i < ncodes ; i++) {
4371 if (status == a->status_codes[i])
4372 break;
4374 if (i == ncodes) {
4375 /* Do not delete files in non-matching status. */
4376 free(ondisk_path);
4377 return NULL;
4379 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4380 a->status_codes[i] != GOT_STATUS_MISSING) {
4381 static char msg[64];
4382 snprintf(msg, sizeof(msg),
4383 "invalid status code '%c'", a->status_codes[i]);
4384 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4385 goto done;
4389 if (status != GOT_STATUS_NO_CHANGE) {
4390 if (status == GOT_STATUS_DELETE)
4391 goto done;
4392 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4393 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4394 goto done;
4396 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4397 err = got_error_set_errno(ENOENT, relpath);
4398 goto done;
4400 if (status != GOT_STATUS_MODIFY &&
4401 status != GOT_STATUS_MISSING) {
4402 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4403 goto done;
4407 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4408 size_t root_len;
4410 if (dirfd != -1) {
4411 if (unlinkat(dirfd, de_name, 0) == -1) {
4412 err = got_error_from_errno2("unlinkat",
4413 ondisk_path);
4414 goto done;
4416 } else if (unlink(ondisk_path) == -1) {
4417 err = got_error_from_errno2("unlink", ondisk_path);
4418 goto done;
4421 root_len = strlen(a->worktree->root_path);
4422 do {
4423 char *parent;
4425 err = got_path_dirname(&parent, ondisk_path);
4426 if (err)
4427 goto done;
4428 free(ondisk_path);
4429 ondisk_path = parent;
4430 if (got_path_cmp(ondisk_path, a->status_path,
4431 strlen(ondisk_path), a->status_path_len) != 0 &&
4432 !got_path_is_child(ondisk_path, a->status_path,
4433 a->status_path_len))
4434 break;
4435 if (rmdir(ondisk_path) == -1) {
4436 if (errno != ENOTEMPTY)
4437 err = got_error_from_errno2("rmdir",
4438 ondisk_path);
4439 break;
4441 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4442 strlen(ondisk_path), root_len) != 0);
4445 got_fileindex_entry_mark_deleted_from_disk(ie);
4446 done:
4447 free(ondisk_path);
4448 if (err)
4449 return err;
4450 if (status == GOT_STATUS_DELETE)
4451 return NULL;
4452 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4453 staged_status, relpath);
4456 const struct got_error *
4457 got_worktree_schedule_delete(struct got_worktree *worktree,
4458 struct got_pathlist_head *paths, int delete_local_mods,
4459 const char *status_codes,
4460 got_worktree_delete_cb progress_cb, void *progress_arg,
4461 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4463 struct got_fileindex *fileindex = NULL;
4464 char *fileindex_path = NULL;
4465 const struct got_error *err = NULL, *sync_err, *unlockerr;
4466 struct got_pathlist_entry *pe;
4467 struct schedule_deletion_args sda;
4469 err = lock_worktree(worktree, LOCK_EX);
4470 if (err)
4471 return err;
4473 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4474 if (err)
4475 goto done;
4477 sda.worktree = worktree;
4478 sda.fileindex = fileindex;
4479 sda.progress_cb = progress_cb;
4480 sda.progress_arg = progress_arg;
4481 sda.repo = repo;
4482 sda.delete_local_mods = delete_local_mods;
4483 sda.keep_on_disk = keep_on_disk;
4484 sda.ignore_missing_paths = ignore_missing_paths;
4485 sda.status_codes = status_codes;
4487 TAILQ_FOREACH(pe, paths, entry) {
4488 char *ondisk_status_path;
4490 if (asprintf(&ondisk_status_path, "%s%s%s",
4491 got_worktree_get_root_path(worktree),
4492 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4493 err = got_error_from_errno("asprintf");
4494 goto done;
4496 sda.status_path = ondisk_status_path;
4497 sda.status_path_len = strlen(ondisk_status_path);
4498 err = worktree_status(worktree, pe->path, fileindex, repo,
4499 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4500 free(ondisk_status_path);
4501 if (err)
4502 break;
4504 sync_err = sync_fileindex(fileindex, fileindex_path);
4505 if (sync_err && err == NULL)
4506 err = sync_err;
4507 done:
4508 free(fileindex_path);
4509 if (fileindex)
4510 got_fileindex_free(fileindex);
4511 unlockerr = lock_worktree(worktree, LOCK_SH);
4512 if (unlockerr && err == NULL)
4513 err = unlockerr;
4514 return err;
4517 static const struct got_error *
4518 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4520 const struct got_error *err = NULL;
4521 char *line = NULL;
4522 size_t linesize = 0, n;
4523 ssize_t linelen;
4525 linelen = getline(&line, &linesize, infile);
4526 if (linelen == -1) {
4527 if (ferror(infile)) {
4528 err = got_error_from_errno("getline");
4529 goto done;
4531 return NULL;
4533 if (outfile) {
4534 n = fwrite(line, 1, linelen, outfile);
4535 if (n != linelen) {
4536 err = got_ferror(outfile, GOT_ERR_IO);
4537 goto done;
4540 if (rejectfile) {
4541 n = fwrite(line, 1, linelen, rejectfile);
4542 if (n != linelen)
4543 err = got_ferror(rejectfile, GOT_ERR_IO);
4545 done:
4546 free(line);
4547 return err;
4550 static const struct got_error *
4551 skip_one_line(FILE *f)
4553 char *line = NULL;
4554 size_t linesize = 0;
4555 ssize_t linelen;
4557 linelen = getline(&line, &linesize, f);
4558 if (linelen == -1) {
4559 if (ferror(f))
4560 return got_error_from_errno("getline");
4561 return NULL;
4563 free(line);
4564 return NULL;
4567 static const struct got_error *
4568 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4569 int start_old, int end_old, int start_new, int end_new,
4570 FILE *outfile, FILE *rejectfile)
4572 const struct got_error *err;
4574 /* Copy old file's lines leading up to patch. */
4575 while (!feof(f1) && *line_cur1 < start_old) {
4576 err = copy_one_line(f1, outfile, NULL);
4577 if (err)
4578 return err;
4579 (*line_cur1)++;
4581 /* Skip new file's lines leading up to patch. */
4582 while (!feof(f2) && *line_cur2 < start_new) {
4583 if (rejectfile)
4584 err = copy_one_line(f2, NULL, rejectfile);
4585 else
4586 err = skip_one_line(f2);
4587 if (err)
4588 return err;
4589 (*line_cur2)++;
4591 /* Copy patched lines. */
4592 while (!feof(f2) && *line_cur2 <= end_new) {
4593 err = copy_one_line(f2, outfile, NULL);
4594 if (err)
4595 return err;
4596 (*line_cur2)++;
4598 /* Skip over old file's replaced lines. */
4599 while (!feof(f1) && *line_cur1 <= end_old) {
4600 if (rejectfile)
4601 err = copy_one_line(f1, NULL, rejectfile);
4602 else
4603 err = skip_one_line(f1);
4604 if (err)
4605 return err;
4606 (*line_cur1)++;
4609 return NULL;
4612 static const struct got_error *
4613 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4614 FILE *outfile, FILE *rejectfile)
4616 const struct got_error *err;
4618 if (outfile) {
4619 /* Copy old file's lines until EOF. */
4620 while (!feof(f1)) {
4621 err = copy_one_line(f1, outfile, NULL);
4622 if (err)
4623 return err;
4624 (*line_cur1)++;
4627 if (rejectfile) {
4628 /* Copy new file's lines until EOF. */
4629 while (!feof(f2)) {
4630 err = copy_one_line(f2, NULL, rejectfile);
4631 if (err)
4632 return err;
4633 (*line_cur2)++;
4637 return NULL;
4640 static const struct got_error *
4641 apply_or_reject_change(int *choice, int *nchunks_used,
4642 struct diff_result *diff_result, int n,
4643 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4644 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4645 got_worktree_patch_cb patch_cb, void *patch_arg)
4647 const struct got_error *err = NULL;
4648 struct diff_chunk_context cc = {};
4649 int start_old, end_old, start_new, end_new;
4650 FILE *hunkfile;
4651 struct diff_output_unidiff_state *diff_state;
4652 struct diff_input_info diff_info;
4653 int rc;
4655 *choice = GOT_PATCH_CHOICE_NONE;
4657 /* Get changed line numbers without context lines for copy_change(). */
4658 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4659 start_old = cc.left.start;
4660 end_old = cc.left.end;
4661 start_new = cc.right.start;
4662 end_new = cc.right.end;
4664 /* Get the same change with context lines for display. */
4665 memset(&cc, 0, sizeof(cc));
4666 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4668 memset(&diff_info, 0, sizeof(diff_info));
4669 diff_info.left_path = relpath;
4670 diff_info.right_path = relpath;
4672 diff_state = diff_output_unidiff_state_alloc();
4673 if (diff_state == NULL)
4674 return got_error_set_errno(ENOMEM,
4675 "diff_output_unidiff_state_alloc");
4677 hunkfile = got_opentemp();
4678 if (hunkfile == NULL) {
4679 err = got_error_from_errno("got_opentemp");
4680 goto done;
4683 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4684 diff_result, &cc);
4685 if (rc != DIFF_RC_OK) {
4686 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4687 goto done;
4690 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4691 err = got_ferror(hunkfile, GOT_ERR_IO);
4692 goto done;
4695 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4696 hunkfile, changeno, nchanges);
4697 if (err)
4698 goto done;
4700 switch (*choice) {
4701 case GOT_PATCH_CHOICE_YES:
4702 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4703 end_old, start_new, end_new, outfile, rejectfile);
4704 break;
4705 case GOT_PATCH_CHOICE_NO:
4706 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4707 end_old, start_new, end_new, rejectfile, outfile);
4708 break;
4709 case GOT_PATCH_CHOICE_QUIT:
4710 break;
4711 default:
4712 err = got_error(GOT_ERR_PATCH_CHOICE);
4713 break;
4715 done:
4716 diff_output_unidiff_state_free(diff_state);
4717 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4718 err = got_error_from_errno("fclose");
4719 return err;
4722 struct revert_file_args {
4723 struct got_worktree *worktree;
4724 struct got_fileindex *fileindex;
4725 got_worktree_checkout_cb progress_cb;
4726 void *progress_arg;
4727 got_worktree_patch_cb patch_cb;
4728 void *patch_arg;
4729 struct got_repository *repo;
4730 int unlink_added_files;
4731 struct got_pathlist_head *added_files_to_unlink;
4734 static const struct got_error *
4735 create_patched_content(char **path_outfile, int reverse_patch,
4736 struct got_object_id *blob_id, const char *path2,
4737 int dirfd2, const char *de_name2,
4738 const char *relpath, struct got_repository *repo,
4739 got_worktree_patch_cb patch_cb, void *patch_arg)
4741 const struct got_error *err, *free_err;
4742 struct got_blob_object *blob = NULL;
4743 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4744 int fd = -1, fd2 = -1;
4745 char link_target[PATH_MAX];
4746 ssize_t link_len = 0;
4747 char *path1 = NULL, *id_str = NULL;
4748 struct stat sb2;
4749 struct got_diffreg_result *diffreg_result = NULL;
4750 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4751 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4753 *path_outfile = NULL;
4755 err = got_object_id_str(&id_str, blob_id);
4756 if (err)
4757 return err;
4759 if (dirfd2 != -1) {
4760 fd2 = openat(dirfd2, de_name2,
4761 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4762 if (fd2 == -1) {
4763 if (!got_err_open_nofollow_on_symlink()) {
4764 err = got_error_from_errno2("openat", path2);
4765 goto done;
4767 link_len = readlinkat(dirfd2, de_name2,
4768 link_target, sizeof(link_target));
4769 if (link_len == -1) {
4770 return got_error_from_errno2("readlinkat",
4771 path2);
4773 sb2.st_mode = S_IFLNK;
4774 sb2.st_size = link_len;
4776 } else {
4777 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4778 if (fd2 == -1) {
4779 if (!got_err_open_nofollow_on_symlink()) {
4780 err = got_error_from_errno2("open", path2);
4781 goto done;
4783 link_len = readlink(path2, link_target,
4784 sizeof(link_target));
4785 if (link_len == -1)
4786 return got_error_from_errno2("readlink", path2);
4787 sb2.st_mode = S_IFLNK;
4788 sb2.st_size = link_len;
4791 if (fd2 != -1) {
4792 if (fstat(fd2, &sb2) == -1) {
4793 err = got_error_from_errno2("fstat", path2);
4794 goto done;
4797 f2 = fdopen(fd2, "r");
4798 if (f2 == NULL) {
4799 err = got_error_from_errno2("fdopen", path2);
4800 goto done;
4802 fd2 = -1;
4803 } else {
4804 size_t n;
4805 f2 = got_opentemp();
4806 if (f2 == NULL) {
4807 err = got_error_from_errno2("got_opentemp", path2);
4808 goto done;
4810 n = fwrite(link_target, 1, link_len, f2);
4811 if (n != link_len) {
4812 err = got_ferror(f2, GOT_ERR_IO);
4813 goto done;
4815 if (fflush(f2) == EOF) {
4816 err = got_error_from_errno("fflush");
4817 goto done;
4819 rewind(f2);
4822 fd = got_opentempfd();
4823 if (fd == -1) {
4824 err = got_error_from_errno("got_opentempfd");
4825 goto done;
4828 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4829 if (err)
4830 goto done;
4832 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4833 if (err)
4834 goto done;
4836 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4837 if (err)
4838 goto done;
4840 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4841 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4842 if (err)
4843 goto done;
4845 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4846 "");
4847 if (err)
4848 goto done;
4850 if (fseek(f1, 0L, SEEK_SET) == -1)
4851 return got_ferror(f1, GOT_ERR_IO);
4852 if (fseek(f2, 0L, SEEK_SET) == -1)
4853 return got_ferror(f2, GOT_ERR_IO);
4855 /* Count the number of actual changes in the diff result. */
4856 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4857 struct diff_chunk_context cc = {};
4858 diff_chunk_context_load_change(&cc, &nchunks_used,
4859 diffreg_result->result, n, 0);
4860 nchanges++;
4862 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4863 int choice;
4864 err = apply_or_reject_change(&choice, &nchunks_used,
4865 diffreg_result->result, n, relpath, f1, f2,
4866 &line_cur1, &line_cur2,
4867 reverse_patch ? NULL : outfile,
4868 reverse_patch ? outfile : NULL,
4869 ++i, nchanges, patch_cb, patch_arg);
4870 if (err)
4871 goto done;
4872 if (choice == GOT_PATCH_CHOICE_YES)
4873 have_content = 1;
4874 else if (choice == GOT_PATCH_CHOICE_QUIT)
4875 break;
4877 if (have_content) {
4878 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4879 reverse_patch ? NULL : outfile,
4880 reverse_patch ? outfile : NULL);
4881 if (err)
4882 goto done;
4884 if (!S_ISLNK(sb2.st_mode)) {
4885 mode_t mode;
4887 mode = apply_umask(sb2.st_mode);
4888 if (fchmod(fileno(outfile), mode) == -1) {
4889 err = got_error_from_errno2("fchmod", path2);
4890 goto done;
4894 done:
4895 free(id_str);
4896 if (fd != -1 && close(fd) == -1 && err == NULL)
4897 err = got_error_from_errno("close");
4898 if (blob)
4899 got_object_blob_close(blob);
4900 free_err = got_diffreg_result_free(diffreg_result);
4901 if (err == NULL)
4902 err = free_err;
4903 if (f1 && fclose(f1) == EOF && err == NULL)
4904 err = got_error_from_errno2("fclose", path1);
4905 if (f2 && fclose(f2) == EOF && err == NULL)
4906 err = got_error_from_errno2("fclose", path2);
4907 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4908 err = got_error_from_errno2("close", path2);
4909 if (outfile && fclose(outfile) == EOF && err == NULL)
4910 err = got_error_from_errno2("fclose", *path_outfile);
4911 if (path1 && unlink(path1) == -1 && err == NULL)
4912 err = got_error_from_errno2("unlink", path1);
4913 if (err || !have_content) {
4914 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4915 err = got_error_from_errno2("unlink", *path_outfile);
4916 free(*path_outfile);
4917 *path_outfile = NULL;
4919 free(path1);
4920 return err;
4923 static const struct got_error *
4924 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4925 const char *relpath, struct got_object_id *blob_id,
4926 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4927 int dirfd, const char *de_name)
4929 struct revert_file_args *a = arg;
4930 const struct got_error *err = NULL;
4931 char *parent_path = NULL;
4932 struct got_fileindex_entry *ie;
4933 struct got_commit_object *base_commit = NULL;
4934 struct got_tree_object *tree = NULL;
4935 struct got_object_id *tree_id = NULL;
4936 const struct got_tree_entry *te = NULL;
4937 char *tree_path = NULL, *te_name;
4938 char *ondisk_path = NULL, *path_content = NULL;
4939 struct got_blob_object *blob = NULL;
4940 int fd = -1;
4942 /* Reverting a staged deletion is a no-op. */
4943 if (status == GOT_STATUS_DELETE &&
4944 staged_status != GOT_STATUS_NO_CHANGE)
4945 return NULL;
4947 if (status == GOT_STATUS_UNVERSIONED)
4948 return (*a->progress_cb)(a->progress_arg,
4949 GOT_STATUS_UNVERSIONED, relpath);
4951 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4952 if (ie == NULL)
4953 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4955 /* Construct in-repository path of tree which contains this blob. */
4956 err = got_path_dirname(&parent_path, ie->path);
4957 if (err) {
4958 if (err->code != GOT_ERR_BAD_PATH)
4959 goto done;
4960 parent_path = strdup("/");
4961 if (parent_path == NULL) {
4962 err = got_error_from_errno("strdup");
4963 goto done;
4966 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4967 tree_path = strdup(parent_path);
4968 if (tree_path == NULL) {
4969 err = got_error_from_errno("strdup");
4970 goto done;
4972 } else {
4973 if (got_path_is_root_dir(parent_path)) {
4974 tree_path = strdup(a->worktree->path_prefix);
4975 if (tree_path == NULL) {
4976 err = got_error_from_errno("strdup");
4977 goto done;
4979 } else {
4980 if (asprintf(&tree_path, "%s/%s",
4981 a->worktree->path_prefix, parent_path) == -1) {
4982 err = got_error_from_errno("asprintf");
4983 goto done;
4988 err = got_object_open_as_commit(&base_commit, a->repo,
4989 a->worktree->base_commit_id);
4990 if (err)
4991 goto done;
4993 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4994 if (err) {
4995 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4996 (status == GOT_STATUS_ADD ||
4997 staged_status == GOT_STATUS_ADD)))
4998 goto done;
4999 } else {
5000 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5001 if (err)
5002 goto done;
5004 err = got_path_basename(&te_name, ie->path);
5005 if (err)
5006 goto done;
5008 te = got_object_tree_find_entry(tree, te_name);
5009 free(te_name);
5010 if (te == NULL && status != GOT_STATUS_ADD &&
5011 staged_status != GOT_STATUS_ADD) {
5012 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5013 goto done;
5017 switch (status) {
5018 case GOT_STATUS_ADD:
5019 if (a->patch_cb) {
5020 int choice = GOT_PATCH_CHOICE_NONE;
5021 err = (*a->patch_cb)(&choice, a->patch_arg,
5022 status, ie->path, NULL, 1, 1);
5023 if (err)
5024 goto done;
5025 if (choice != GOT_PATCH_CHOICE_YES)
5026 break;
5028 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5029 ie->path);
5030 if (err)
5031 goto done;
5032 got_fileindex_entry_remove(a->fileindex, ie);
5033 if (a->unlink_added_files) {
5034 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5036 if (a->added_files_to_unlink) {
5037 struct got_pathlist_entry *pe;
5039 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5040 entry) {
5041 if (got_path_cmp(pe->path, relpath,
5042 pe->path_len, strlen(relpath)))
5043 continue;
5044 do_unlink = 1;
5045 break;
5049 if (do_unlink) {
5050 if (asprintf(&ondisk_path, "%s/%s",
5051 got_worktree_get_root_path(a->worktree),
5052 relpath) == -1) {
5053 err = got_error_from_errno("asprintf");
5054 goto done;
5056 if (unlink(ondisk_path) == -1) {
5057 err = got_error_from_errno2("unlink",
5058 ondisk_path);
5059 break;
5063 break;
5064 case GOT_STATUS_DELETE:
5065 if (a->patch_cb) {
5066 int choice = GOT_PATCH_CHOICE_NONE;
5067 err = (*a->patch_cb)(&choice, a->patch_arg,
5068 status, ie->path, NULL, 1, 1);
5069 if (err)
5070 goto done;
5071 if (choice != GOT_PATCH_CHOICE_YES)
5072 break;
5074 /* fall through */
5075 case GOT_STATUS_MODIFY:
5076 case GOT_STATUS_MODE_CHANGE:
5077 case GOT_STATUS_CONFLICT:
5078 case GOT_STATUS_MISSING: {
5079 struct got_object_id id;
5080 if (staged_status == GOT_STATUS_ADD ||
5081 staged_status == GOT_STATUS_MODIFY)
5082 got_fileindex_entry_get_staged_blob_id(&id, ie);
5083 else
5084 got_fileindex_entry_get_blob_id(&id, ie);
5085 fd = got_opentempfd();
5086 if (fd == -1) {
5087 err = got_error_from_errno("got_opentempfd");
5088 goto done;
5091 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5092 if (err)
5093 goto done;
5095 if (asprintf(&ondisk_path, "%s/%s",
5096 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5097 err = got_error_from_errno("asprintf");
5098 goto done;
5101 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5102 status == GOT_STATUS_CONFLICT)) {
5103 int is_bad_symlink = 0;
5104 err = create_patched_content(&path_content, 1, &id,
5105 ondisk_path, dirfd, de_name, ie->path, a->repo,
5106 a->patch_cb, a->patch_arg);
5107 if (err || path_content == NULL)
5108 break;
5109 if (te && S_ISLNK(te->mode)) {
5110 if (unlink(path_content) == -1) {
5111 err = got_error_from_errno2("unlink",
5112 path_content);
5113 break;
5115 err = install_symlink(&is_bad_symlink,
5116 a->worktree, ondisk_path, ie->path,
5117 blob, 0, 1, 0, 0, a->repo,
5118 a->progress_cb, a->progress_arg);
5119 } else {
5120 if (rename(path_content, ondisk_path) == -1) {
5121 err = got_error_from_errno3("rename",
5122 path_content, ondisk_path);
5123 goto done;
5126 } else {
5127 int is_bad_symlink = 0;
5128 if (te && S_ISLNK(te->mode)) {
5129 err = install_symlink(&is_bad_symlink,
5130 a->worktree, ondisk_path, ie->path,
5131 blob, 0, 1, 0, 0, a->repo,
5132 a->progress_cb, a->progress_arg);
5133 } else {
5134 err = install_blob(a->worktree, ondisk_path,
5135 ie->path,
5136 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5137 got_fileindex_perms_to_st(ie), blob,
5138 0, 1, 0, 0, a->repo,
5139 a->progress_cb, a->progress_arg);
5141 if (err)
5142 goto done;
5143 if (status == GOT_STATUS_DELETE ||
5144 status == GOT_STATUS_MODE_CHANGE) {
5145 err = got_fileindex_entry_update(ie,
5146 a->worktree->root_fd, relpath,
5147 blob->id.sha1,
5148 a->worktree->base_commit_id->sha1, 1);
5149 if (err)
5150 goto done;
5152 if (is_bad_symlink) {
5153 got_fileindex_entry_filetype_set(ie,
5154 GOT_FILEIDX_MODE_BAD_SYMLINK);
5157 break;
5159 default:
5160 break;
5162 done:
5163 free(ondisk_path);
5164 free(path_content);
5165 free(parent_path);
5166 free(tree_path);
5167 if (fd != -1 && close(fd) == -1 && err == NULL)
5168 err = got_error_from_errno("close");
5169 if (blob)
5170 got_object_blob_close(blob);
5171 if (tree)
5172 got_object_tree_close(tree);
5173 free(tree_id);
5174 if (base_commit)
5175 got_object_commit_close(base_commit);
5176 return err;
5179 const struct got_error *
5180 got_worktree_revert(struct got_worktree *worktree,
5181 struct got_pathlist_head *paths,
5182 got_worktree_checkout_cb progress_cb, void *progress_arg,
5183 got_worktree_patch_cb patch_cb, void *patch_arg,
5184 struct got_repository *repo)
5186 struct got_fileindex *fileindex = NULL;
5187 char *fileindex_path = NULL;
5188 const struct got_error *err = NULL, *unlockerr = NULL;
5189 const struct got_error *sync_err = NULL;
5190 struct got_pathlist_entry *pe;
5191 struct revert_file_args rfa;
5193 err = lock_worktree(worktree, LOCK_EX);
5194 if (err)
5195 return err;
5197 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5198 if (err)
5199 goto done;
5201 rfa.worktree = worktree;
5202 rfa.fileindex = fileindex;
5203 rfa.progress_cb = progress_cb;
5204 rfa.progress_arg = progress_arg;
5205 rfa.patch_cb = patch_cb;
5206 rfa.patch_arg = patch_arg;
5207 rfa.repo = repo;
5208 rfa.unlink_added_files = 0;
5209 TAILQ_FOREACH(pe, paths, entry) {
5210 err = worktree_status(worktree, pe->path, fileindex, repo,
5211 revert_file, &rfa, NULL, NULL, 1, 0);
5212 if (err)
5213 break;
5215 sync_err = sync_fileindex(fileindex, fileindex_path);
5216 if (sync_err && err == NULL)
5217 err = sync_err;
5218 done:
5219 free(fileindex_path);
5220 if (fileindex)
5221 got_fileindex_free(fileindex);
5222 unlockerr = lock_worktree(worktree, LOCK_SH);
5223 if (unlockerr && err == NULL)
5224 err = unlockerr;
5225 return err;
5228 static void
5229 free_commitable(struct got_commitable *ct)
5231 free(ct->path);
5232 free(ct->in_repo_path);
5233 free(ct->ondisk_path);
5234 free(ct->blob_id);
5235 free(ct->base_blob_id);
5236 free(ct->staged_blob_id);
5237 free(ct->base_commit_id);
5238 free(ct);
5241 struct collect_commitables_arg {
5242 struct got_pathlist_head *commitable_paths;
5243 struct got_repository *repo;
5244 struct got_worktree *worktree;
5245 struct got_fileindex *fileindex;
5246 int have_staged_files;
5247 int allow_bad_symlinks;
5248 int diff_header_shown;
5249 int commit_conflicts;
5250 FILE *diff_outfile;
5251 FILE *f1;
5252 FILE *f2;
5256 * Create a file which contains the target path of a symlink so we can feed
5257 * it as content to the diff engine.
5259 static const struct got_error *
5260 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5261 const char *abspath)
5263 const struct got_error *err = NULL;
5264 char target_path[PATH_MAX];
5265 ssize_t target_len, outlen;
5267 *fd = -1;
5269 if (dirfd != -1) {
5270 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5271 if (target_len == -1)
5272 return got_error_from_errno2("readlinkat", abspath);
5273 } else {
5274 target_len = readlink(abspath, target_path, PATH_MAX);
5275 if (target_len == -1)
5276 return got_error_from_errno2("readlink", abspath);
5279 *fd = got_opentempfd();
5280 if (*fd == -1)
5281 return got_error_from_errno("got_opentempfd");
5283 outlen = write(*fd, target_path, target_len);
5284 if (outlen == -1) {
5285 err = got_error_from_errno("got_opentempfd");
5286 goto done;
5289 if (lseek(*fd, 0, SEEK_SET) == -1) {
5290 err = got_error_from_errno2("lseek", abspath);
5291 goto done;
5293 done:
5294 if (err) {
5295 close(*fd);
5296 *fd = -1;
5298 return err;
5301 static const struct got_error *
5302 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5303 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5304 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5306 const struct got_error *err = NULL;
5307 struct got_blob_object *blob1 = NULL;
5308 int fd = -1, fd1 = -1, fd2 = -1;
5309 FILE *ondisk_file = NULL;
5310 char *label1 = NULL;
5311 struct stat sb;
5312 off_t size1 = 0;
5313 int f2_exists = 0;
5314 char *id_str = NULL;
5316 memset(&sb, 0, sizeof(sb));
5318 if (diff_staged) {
5319 if (ct->staged_status != GOT_STATUS_MODIFY &&
5320 ct->staged_status != GOT_STATUS_ADD &&
5321 ct->staged_status != GOT_STATUS_DELETE)
5322 return NULL;
5323 } else {
5324 if (ct->status != GOT_STATUS_MODIFY &&
5325 ct->status != GOT_STATUS_ADD &&
5326 ct->status != GOT_STATUS_DELETE &&
5327 ct->status != GOT_STATUS_CONFLICT)
5328 return NULL;
5331 err = got_opentemp_truncate(f1);
5332 if (err)
5333 return got_error_from_errno("got_opentemp_truncate");
5334 err = got_opentemp_truncate(f2);
5335 if (err)
5336 return got_error_from_errno("got_opentemp_truncate");
5338 if (!*diff_header_shown) {
5339 err = got_object_id_str(&id_str, worktree->base_commit_id);
5340 if (err)
5341 return err;
5342 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5343 got_worktree_get_root_path(worktree));
5344 fprintf(diff_outfile, "commit - %s\n", id_str);
5345 fprintf(diff_outfile, "path + %s%s\n",
5346 got_worktree_get_root_path(worktree),
5347 diff_staged ? " (staged changes)" : "");
5348 *diff_header_shown = 1;
5351 if (diff_staged) {
5352 const char *label1 = NULL, *label2 = NULL;
5353 switch (ct->staged_status) {
5354 case GOT_STATUS_MODIFY:
5355 label1 = ct->path;
5356 label2 = ct->path;
5357 break;
5358 case GOT_STATUS_ADD:
5359 label2 = ct->path;
5360 break;
5361 case GOT_STATUS_DELETE:
5362 label1 = ct->path;
5363 break;
5364 default:
5365 return got_error(GOT_ERR_FILE_STATUS);
5367 fd1 = got_opentempfd();
5368 if (fd1 == -1) {
5369 err = got_error_from_errno("got_opentempfd");
5370 goto done;
5372 fd2 = got_opentempfd();
5373 if (fd2 == -1) {
5374 err = got_error_from_errno("got_opentempfd");
5375 goto done;
5377 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5378 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5379 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5380 NULL, repo, diff_outfile);
5381 goto done;
5384 fd1 = got_opentempfd();
5385 if (fd1 == -1) {
5386 err = got_error_from_errno("got_opentempfd");
5387 goto done;
5390 if (ct->status != GOT_STATUS_ADD) {
5391 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5392 8192, fd1);
5393 if (err)
5394 goto done;
5397 if (ct->status != GOT_STATUS_DELETE) {
5398 if (dirfd != -1) {
5399 fd = openat(dirfd, de_name,
5400 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5401 if (fd == -1) {
5402 if (!got_err_open_nofollow_on_symlink()) {
5403 err = got_error_from_errno2("openat",
5404 ct->ondisk_path);
5405 goto done;
5407 err = get_symlink_target_file(&fd, dirfd,
5408 de_name, ct->ondisk_path);
5409 if (err)
5410 goto done;
5412 } else {
5413 fd = open(ct->ondisk_path,
5414 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5415 if (fd == -1) {
5416 if (!got_err_open_nofollow_on_symlink()) {
5417 err = got_error_from_errno2("open",
5418 ct->ondisk_path);
5419 goto done;
5421 err = get_symlink_target_file(&fd, dirfd,
5422 de_name, ct->ondisk_path);
5423 if (err)
5424 goto done;
5427 if (fstatat(fd, ct->ondisk_path, &sb,
5428 AT_SYMLINK_NOFOLLOW) == -1) {
5429 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5430 goto done;
5432 ondisk_file = fdopen(fd, "r");
5433 if (ondisk_file == NULL) {
5434 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5435 goto done;
5437 fd = -1;
5438 f2_exists = 1;
5441 if (blob1) {
5442 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5443 f1, blob1);
5444 if (err)
5445 goto done;
5448 err = got_diff_blob_file(blob1, f1, size1, label1,
5449 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5450 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5451 done:
5452 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5453 err = got_error_from_errno("close");
5454 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5455 err = got_error_from_errno("close");
5456 if (blob1)
5457 got_object_blob_close(blob1);
5458 if (fd != -1 && close(fd) == -1 && err == NULL)
5459 err = got_error_from_errno("close");
5460 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5461 err = got_error_from_errno("fclose");
5462 return err;
5465 static const struct got_error *
5466 collect_commitables(void *arg, unsigned char status,
5467 unsigned char staged_status, const char *relpath,
5468 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5469 struct got_object_id *commit_id, int dirfd, const char *de_name)
5471 struct collect_commitables_arg *a = arg;
5472 const struct got_error *err = NULL;
5473 struct got_commitable *ct = NULL;
5474 struct got_pathlist_entry *new = NULL;
5475 char *parent_path = NULL, *path = NULL;
5476 struct stat sb;
5478 if (a->have_staged_files) {
5479 if (staged_status != GOT_STATUS_MODIFY &&
5480 staged_status != GOT_STATUS_ADD &&
5481 staged_status != GOT_STATUS_DELETE)
5482 return NULL;
5483 } else {
5484 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5485 printf("C %s\n", relpath);
5486 return got_error(GOT_ERR_COMMIT_CONFLICT);
5489 if (status != GOT_STATUS_MODIFY &&
5490 status != GOT_STATUS_MODE_CHANGE &&
5491 status != GOT_STATUS_ADD &&
5492 status != GOT_STATUS_DELETE &&
5493 status != GOT_STATUS_CONFLICT)
5494 return NULL;
5497 if (asprintf(&path, "/%s", relpath) == -1) {
5498 err = got_error_from_errno("asprintf");
5499 goto done;
5501 if (strcmp(path, "/") == 0) {
5502 parent_path = strdup("");
5503 if (parent_path == NULL)
5504 return got_error_from_errno("strdup");
5505 } else {
5506 err = got_path_dirname(&parent_path, path);
5507 if (err)
5508 return err;
5511 ct = calloc(1, sizeof(*ct));
5512 if (ct == NULL) {
5513 err = got_error_from_errno("calloc");
5514 goto done;
5517 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5518 relpath) == -1) {
5519 err = got_error_from_errno("asprintf");
5520 goto done;
5523 if (staged_status == GOT_STATUS_ADD ||
5524 staged_status == GOT_STATUS_MODIFY) {
5525 struct got_fileindex_entry *ie;
5526 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5527 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5528 case GOT_FILEIDX_MODE_REGULAR_FILE:
5529 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5530 ct->mode = S_IFREG;
5531 break;
5532 case GOT_FILEIDX_MODE_SYMLINK:
5533 ct->mode = S_IFLNK;
5534 break;
5535 default:
5536 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5537 goto done;
5539 ct->mode |= got_fileindex_entry_perms_get(ie);
5540 } else if (status != GOT_STATUS_DELETE &&
5541 staged_status != GOT_STATUS_DELETE) {
5542 if (dirfd != -1) {
5543 if (fstatat(dirfd, de_name, &sb,
5544 AT_SYMLINK_NOFOLLOW) == -1) {
5545 err = got_error_from_errno2("fstatat",
5546 ct->ondisk_path);
5547 goto done;
5549 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5550 err = got_error_from_errno2("lstat", ct->ondisk_path);
5551 goto done;
5553 ct->mode = sb.st_mode;
5556 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5557 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5558 relpath) == -1) {
5559 err = got_error_from_errno("asprintf");
5560 goto done;
5563 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5564 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5565 int is_bad_symlink;
5566 char target_path[PATH_MAX];
5567 ssize_t target_len;
5568 target_len = readlink(ct->ondisk_path, target_path,
5569 sizeof(target_path));
5570 if (target_len == -1) {
5571 err = got_error_from_errno2("readlink",
5572 ct->ondisk_path);
5573 goto done;
5575 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5576 target_len, ct->ondisk_path, a->worktree->root_path);
5577 if (err)
5578 goto done;
5579 if (is_bad_symlink) {
5580 err = got_error_path(ct->ondisk_path,
5581 GOT_ERR_BAD_SYMLINK);
5582 goto done;
5587 ct->status = status;
5588 ct->staged_status = staged_status;
5589 ct->blob_id = NULL; /* will be filled in when blob gets created */
5590 if (ct->status != GOT_STATUS_ADD &&
5591 ct->staged_status != GOT_STATUS_ADD) {
5592 ct->base_blob_id = got_object_id_dup(blob_id);
5593 if (ct->base_blob_id == NULL) {
5594 err = got_error_from_errno("got_object_id_dup");
5595 goto done;
5597 ct->base_commit_id = got_object_id_dup(commit_id);
5598 if (ct->base_commit_id == NULL) {
5599 err = got_error_from_errno("got_object_id_dup");
5600 goto done;
5603 if (ct->staged_status == GOT_STATUS_ADD ||
5604 ct->staged_status == GOT_STATUS_MODIFY) {
5605 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5606 if (ct->staged_blob_id == NULL) {
5607 err = got_error_from_errno("got_object_id_dup");
5608 goto done;
5611 ct->path = strdup(path);
5612 if (ct->path == NULL) {
5613 err = got_error_from_errno("strdup");
5614 goto done;
5616 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5617 if (err)
5618 goto done;
5620 if (a->diff_outfile && ct && new != NULL) {
5621 err = append_ct_diff(ct, &a->diff_header_shown,
5622 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5623 a->have_staged_files, a->repo, a->worktree);
5624 if (err)
5625 goto done;
5627 done:
5628 if (ct && (err || new == NULL))
5629 free_commitable(ct);
5630 free(parent_path);
5631 free(path);
5632 return err;
5635 static const struct got_error *write_tree(struct got_object_id **, int *,
5636 struct got_tree_object *, const char *, struct got_pathlist_head *,
5637 got_worktree_status_cb status_cb, void *status_arg,
5638 struct got_repository *);
5640 static const struct got_error *
5641 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5642 struct got_tree_entry *te, const char *parent_path,
5643 struct got_pathlist_head *commitable_paths,
5644 got_worktree_status_cb status_cb, void *status_arg,
5645 struct got_repository *repo)
5647 const struct got_error *err = NULL;
5648 struct got_tree_object *subtree;
5649 char *subpath;
5651 if (asprintf(&subpath, "%s%s%s", parent_path,
5652 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5653 return got_error_from_errno("asprintf");
5655 err = got_object_open_as_tree(&subtree, repo, &te->id);
5656 if (err)
5657 return err;
5659 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5660 commitable_paths, status_cb, status_arg, repo);
5661 got_object_tree_close(subtree);
5662 free(subpath);
5663 return err;
5666 static const struct got_error *
5667 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5669 const struct got_error *err = NULL;
5670 char *ct_parent_path = NULL;
5672 *match = 0;
5674 if (strchr(ct->in_repo_path, '/') == NULL) {
5675 *match = got_path_is_root_dir(path);
5676 return NULL;
5679 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5680 if (err)
5681 return err;
5682 *match = (strcmp(path, ct_parent_path) == 0);
5683 free(ct_parent_path);
5684 return err;
5687 static mode_t
5688 get_ct_file_mode(struct got_commitable *ct)
5690 if (S_ISLNK(ct->mode))
5691 return S_IFLNK;
5693 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5696 static const struct got_error *
5697 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5698 struct got_tree_entry *te, struct got_commitable *ct)
5700 const struct got_error *err = NULL;
5702 *new_te = NULL;
5704 err = got_object_tree_entry_dup(new_te, te);
5705 if (err)
5706 goto done;
5708 (*new_te)->mode = get_ct_file_mode(ct);
5710 if (ct->staged_status == GOT_STATUS_MODIFY)
5711 memcpy(&(*new_te)->id, ct->staged_blob_id,
5712 sizeof((*new_te)->id));
5713 else
5714 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5715 done:
5716 if (err && *new_te) {
5717 free(*new_te);
5718 *new_te = NULL;
5720 return err;
5723 static const struct got_error *
5724 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5725 struct got_commitable *ct)
5727 const struct got_error *err = NULL;
5728 char *ct_name = NULL;
5730 *new_te = NULL;
5732 *new_te = calloc(1, sizeof(**new_te));
5733 if (*new_te == NULL)
5734 return got_error_from_errno("calloc");
5736 err = got_path_basename(&ct_name, ct->path);
5737 if (err)
5738 goto done;
5739 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5740 sizeof((*new_te)->name)) {
5741 err = got_error(GOT_ERR_NO_SPACE);
5742 goto done;
5745 (*new_te)->mode = get_ct_file_mode(ct);
5747 if (ct->staged_status == GOT_STATUS_ADD)
5748 memcpy(&(*new_te)->id, ct->staged_blob_id,
5749 sizeof((*new_te)->id));
5750 else
5751 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5752 done:
5753 free(ct_name);
5754 if (err && *new_te) {
5755 free(*new_te);
5756 *new_te = NULL;
5758 return err;
5761 static const struct got_error *
5762 insert_tree_entry(struct got_tree_entry *new_te,
5763 struct got_pathlist_head *paths)
5765 const struct got_error *err = NULL;
5766 struct got_pathlist_entry *new_pe;
5768 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5769 if (err)
5770 return err;
5771 if (new_pe == NULL)
5772 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5773 return NULL;
5776 static const struct got_error *
5777 report_ct_status(struct got_commitable *ct,
5778 got_worktree_status_cb status_cb, void *status_arg)
5780 const char *ct_path = ct->path;
5781 unsigned char status;
5783 if (status_cb == NULL) /* no commit progress output desired */
5784 return NULL;
5786 while (ct_path[0] == '/')
5787 ct_path++;
5789 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5790 status = ct->staged_status;
5791 else
5792 status = ct->status;
5794 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5795 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5798 static const struct got_error *
5799 match_modified_subtree(int *modified, struct got_tree_entry *te,
5800 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5802 const struct got_error *err = NULL;
5803 struct got_pathlist_entry *pe;
5804 char *te_path;
5806 *modified = 0;
5808 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5809 got_path_is_root_dir(base_tree_path) ? "" : "/",
5810 te->name) == -1)
5811 return got_error_from_errno("asprintf");
5813 TAILQ_FOREACH(pe, commitable_paths, entry) {
5814 struct got_commitable *ct = pe->data;
5815 *modified = got_path_is_child(ct->in_repo_path, te_path,
5816 strlen(te_path));
5817 if (*modified)
5818 break;
5821 free(te_path);
5822 return err;
5825 static const struct got_error *
5826 match_deleted_or_modified_ct(struct got_commitable **ctp,
5827 struct got_tree_entry *te, const char *base_tree_path,
5828 struct got_pathlist_head *commitable_paths)
5830 const struct got_error *err = NULL;
5831 struct got_pathlist_entry *pe;
5833 *ctp = NULL;
5835 TAILQ_FOREACH(pe, commitable_paths, entry) {
5836 struct got_commitable *ct = pe->data;
5837 char *ct_name = NULL;
5838 int path_matches;
5840 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5841 if (ct->status != GOT_STATUS_MODIFY &&
5842 ct->status != GOT_STATUS_MODE_CHANGE &&
5843 ct->status != GOT_STATUS_DELETE &&
5844 ct->status != GOT_STATUS_CONFLICT)
5845 continue;
5846 } else {
5847 if (ct->staged_status != GOT_STATUS_MODIFY &&
5848 ct->staged_status != GOT_STATUS_DELETE)
5849 continue;
5852 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5853 continue;
5855 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5856 if (err)
5857 return err;
5858 if (!path_matches)
5859 continue;
5861 err = got_path_basename(&ct_name, pe->path);
5862 if (err)
5863 return err;
5865 if (strcmp(te->name, ct_name) != 0) {
5866 free(ct_name);
5867 continue;
5869 free(ct_name);
5871 *ctp = ct;
5872 break;
5875 return err;
5878 static const struct got_error *
5879 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5880 const char *child_path, const char *path_base_tree,
5881 struct got_pathlist_head *commitable_paths,
5882 got_worktree_status_cb status_cb, void *status_arg,
5883 struct got_repository *repo)
5885 const struct got_error *err = NULL;
5886 struct got_tree_entry *new_te;
5887 char *subtree_path;
5888 struct got_object_id *id = NULL;
5889 int nentries;
5891 *new_tep = NULL;
5893 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5894 got_path_is_root_dir(path_base_tree) ? "" : "/",
5895 child_path) == -1)
5896 return got_error_from_errno("asprintf");
5898 new_te = calloc(1, sizeof(*new_te));
5899 if (new_te == NULL)
5900 return got_error_from_errno("calloc");
5901 new_te->mode = S_IFDIR;
5903 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5904 sizeof(new_te->name)) {
5905 err = got_error(GOT_ERR_NO_SPACE);
5906 goto done;
5908 err = write_tree(&id, &nentries, NULL, subtree_path,
5909 commitable_paths, status_cb, status_arg, repo);
5910 if (err) {
5911 free(new_te);
5912 goto done;
5914 memcpy(&new_te->id, id, sizeof(new_te->id));
5915 done:
5916 free(id);
5917 free(subtree_path);
5918 if (err == NULL)
5919 *new_tep = new_te;
5920 return err;
5923 static const struct got_error *
5924 write_tree(struct got_object_id **new_tree_id, int *nentries,
5925 struct got_tree_object *base_tree, const char *path_base_tree,
5926 struct got_pathlist_head *commitable_paths,
5927 got_worktree_status_cb status_cb, void *status_arg,
5928 struct got_repository *repo)
5930 const struct got_error *err = NULL;
5931 struct got_pathlist_head paths;
5932 struct got_tree_entry *te, *new_te = NULL;
5933 struct got_pathlist_entry *pe;
5935 TAILQ_INIT(&paths);
5936 *nentries = 0;
5938 /* Insert, and recurse into, newly added entries first. */
5939 TAILQ_FOREACH(pe, commitable_paths, entry) {
5940 struct got_commitable *ct = pe->data;
5941 char *child_path = NULL, *slash;
5943 if ((ct->status != GOT_STATUS_ADD &&
5944 ct->staged_status != GOT_STATUS_ADD) ||
5945 (ct->flags & GOT_COMMITABLE_ADDED))
5946 continue;
5948 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5949 strlen(path_base_tree)))
5950 continue;
5952 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5953 ct->in_repo_path);
5954 if (err)
5955 goto done;
5957 slash = strchr(child_path, '/');
5958 if (slash == NULL) {
5959 err = alloc_added_blob_tree_entry(&new_te, ct);
5960 if (err)
5961 goto done;
5962 err = report_ct_status(ct, status_cb, status_arg);
5963 if (err)
5964 goto done;
5965 ct->flags |= GOT_COMMITABLE_ADDED;
5966 err = insert_tree_entry(new_te, &paths);
5967 if (err)
5968 goto done;
5969 (*nentries)++;
5970 } else {
5971 *slash = '\0'; /* trim trailing path components */
5972 if (base_tree == NULL ||
5973 got_object_tree_find_entry(base_tree, child_path)
5974 == NULL) {
5975 err = make_subtree_for_added_blob(&new_te,
5976 child_path, path_base_tree,
5977 commitable_paths, status_cb, status_arg,
5978 repo);
5979 if (err)
5980 goto done;
5981 err = insert_tree_entry(new_te, &paths);
5982 if (err)
5983 goto done;
5984 (*nentries)++;
5989 if (base_tree) {
5990 int i, nbase_entries;
5991 /* Handle modified and deleted entries. */
5992 nbase_entries = got_object_tree_get_nentries(base_tree);
5993 for (i = 0; i < nbase_entries; i++) {
5994 struct got_commitable *ct = NULL;
5996 te = got_object_tree_get_entry(base_tree, i);
5997 if (got_object_tree_entry_is_submodule(te)) {
5998 /* Entry is a submodule; just copy it. */
5999 err = got_object_tree_entry_dup(&new_te, te);
6000 if (err)
6001 goto done;
6002 err = insert_tree_entry(new_te, &paths);
6003 if (err)
6004 goto done;
6005 (*nentries)++;
6006 continue;
6009 if (S_ISDIR(te->mode)) {
6010 int modified;
6011 err = got_object_tree_entry_dup(&new_te, te);
6012 if (err)
6013 goto done;
6014 err = match_modified_subtree(&modified, te,
6015 path_base_tree, commitable_paths);
6016 if (err)
6017 goto done;
6018 /* Avoid recursion into unmodified subtrees. */
6019 if (modified) {
6020 struct got_object_id *new_id;
6021 int nsubentries;
6022 err = write_subtree(&new_id,
6023 &nsubentries, te,
6024 path_base_tree, commitable_paths,
6025 status_cb, status_arg, repo);
6026 if (err)
6027 goto done;
6028 if (nsubentries == 0) {
6029 /* All entries were deleted. */
6030 free(new_id);
6031 continue;
6033 memcpy(&new_te->id, new_id,
6034 sizeof(new_te->id));
6035 free(new_id);
6037 err = insert_tree_entry(new_te, &paths);
6038 if (err)
6039 goto done;
6040 (*nentries)++;
6041 continue;
6044 err = match_deleted_or_modified_ct(&ct, te,
6045 path_base_tree, commitable_paths);
6046 if (err)
6047 goto done;
6048 if (ct) {
6049 /* NB: Deleted entries get dropped here. */
6050 if (ct->status == GOT_STATUS_MODIFY ||
6051 ct->status == GOT_STATUS_MODE_CHANGE ||
6052 ct->status == GOT_STATUS_CONFLICT ||
6053 ct->staged_status == GOT_STATUS_MODIFY) {
6054 err = alloc_modified_blob_tree_entry(
6055 &new_te, te, ct);
6056 if (err)
6057 goto done;
6058 err = insert_tree_entry(new_te, &paths);
6059 if (err)
6060 goto done;
6061 (*nentries)++;
6063 err = report_ct_status(ct, status_cb,
6064 status_arg);
6065 if (err)
6066 goto done;
6067 } else {
6068 /* Entry is unchanged; just copy it. */
6069 err = got_object_tree_entry_dup(&new_te, te);
6070 if (err)
6071 goto done;
6072 err = insert_tree_entry(new_te, &paths);
6073 if (err)
6074 goto done;
6075 (*nentries)++;
6080 /* Write new list of entries; deleted entries have been dropped. */
6081 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6082 done:
6083 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6084 return err;
6087 static const struct got_error *
6088 update_fileindex_after_commit(struct got_worktree *worktree,
6089 struct got_pathlist_head *commitable_paths,
6090 struct got_object_id *new_base_commit_id,
6091 struct got_fileindex *fileindex, int have_staged_files)
6093 const struct got_error *err = NULL;
6094 struct got_pathlist_entry *pe;
6095 char *relpath = NULL;
6097 TAILQ_FOREACH(pe, commitable_paths, entry) {
6098 struct got_fileindex_entry *ie;
6099 struct got_commitable *ct = pe->data;
6101 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6103 err = got_path_skip_common_ancestor(&relpath,
6104 worktree->root_path, ct->ondisk_path);
6105 if (err)
6106 goto done;
6108 if (ie) {
6109 if (ct->status == GOT_STATUS_DELETE ||
6110 ct->staged_status == GOT_STATUS_DELETE) {
6111 got_fileindex_entry_remove(fileindex, ie);
6112 } else if (ct->staged_status == GOT_STATUS_ADD ||
6113 ct->staged_status == GOT_STATUS_MODIFY) {
6114 got_fileindex_entry_stage_set(ie,
6115 GOT_FILEIDX_STAGE_NONE);
6116 got_fileindex_entry_staged_filetype_set(ie, 0);
6118 err = got_fileindex_entry_update(ie,
6119 worktree->root_fd, relpath,
6120 ct->staged_blob_id->sha1,
6121 new_base_commit_id->sha1,
6122 !have_staged_files);
6123 } else
6124 err = got_fileindex_entry_update(ie,
6125 worktree->root_fd, relpath,
6126 ct->blob_id->sha1,
6127 new_base_commit_id->sha1,
6128 !have_staged_files);
6129 } else {
6130 err = got_fileindex_entry_alloc(&ie, pe->path);
6131 if (err)
6132 goto done;
6133 err = got_fileindex_entry_update(ie,
6134 worktree->root_fd, relpath, ct->blob_id->sha1,
6135 new_base_commit_id->sha1, 1);
6136 if (err) {
6137 got_fileindex_entry_free(ie);
6138 goto done;
6140 err = got_fileindex_entry_add(fileindex, ie);
6141 if (err) {
6142 got_fileindex_entry_free(ie);
6143 goto done;
6146 free(relpath);
6147 relpath = NULL;
6149 done:
6150 free(relpath);
6151 return err;
6155 static const struct got_error *
6156 check_out_of_date(const char *in_repo_path, unsigned char status,
6157 unsigned char staged_status, struct got_object_id *base_blob_id,
6158 struct got_object_id *base_commit_id,
6159 struct got_object_id *head_commit_id, struct got_repository *repo,
6160 int ood_errcode)
6162 const struct got_error *err = NULL;
6163 struct got_commit_object *commit = NULL;
6164 struct got_object_id *id = NULL;
6166 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6167 /* Trivial case: base commit == head commit */
6168 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6169 return NULL;
6171 * Ensure file content which local changes were based
6172 * on matches file content in the branch head.
6174 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6175 if (err)
6176 goto done;
6177 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6178 if (err) {
6179 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6180 err = got_error(ood_errcode);
6181 goto done;
6182 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6183 err = got_error(ood_errcode);
6184 } else {
6185 /* Require that added files don't exist in the branch head. */
6186 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6187 if (err)
6188 goto done;
6189 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6190 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6191 goto done;
6192 err = id ? got_error(ood_errcode) : NULL;
6194 done:
6195 free(id);
6196 if (commit)
6197 got_object_commit_close(commit);
6198 return err;
6201 static const struct got_error *
6202 commit_worktree(struct got_object_id **new_commit_id,
6203 struct got_pathlist_head *commitable_paths,
6204 struct got_object_id *head_commit_id,
6205 struct got_object_id *parent_id2,
6206 struct got_worktree *worktree,
6207 const char *author, const char *committer, char *diff_path,
6208 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6209 got_worktree_status_cb status_cb, void *status_arg,
6210 struct got_repository *repo)
6212 const struct got_error *err = NULL, *unlockerr = NULL;
6213 struct got_pathlist_entry *pe;
6214 const char *head_ref_name = NULL;
6215 struct got_commit_object *head_commit = NULL;
6216 struct got_reference *head_ref2 = NULL;
6217 struct got_object_id *head_commit_id2 = NULL;
6218 struct got_tree_object *head_tree = NULL;
6219 struct got_object_id *new_tree_id = NULL;
6220 int nentries, nparents = 0;
6221 struct got_object_id_queue parent_ids;
6222 struct got_object_qid *pid = NULL;
6223 char *logmsg = NULL;
6224 time_t timestamp;
6226 *new_commit_id = NULL;
6228 STAILQ_INIT(&parent_ids);
6230 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6231 if (err)
6232 goto done;
6234 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6235 if (err)
6236 goto done;
6238 if (commit_msg_cb != NULL) {
6239 err = commit_msg_cb(commitable_paths, diff_path,
6240 &logmsg, commit_arg);
6241 if (err)
6242 goto done;
6245 if (logmsg == NULL || strlen(logmsg) == 0) {
6246 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6247 goto done;
6250 /* Create blobs from added and modified files and record their IDs. */
6251 TAILQ_FOREACH(pe, commitable_paths, entry) {
6252 struct got_commitable *ct = pe->data;
6253 char *ondisk_path;
6255 /* Blobs for staged files already exist. */
6256 if (ct->staged_status == GOT_STATUS_ADD ||
6257 ct->staged_status == GOT_STATUS_MODIFY)
6258 continue;
6260 if (ct->status != GOT_STATUS_ADD &&
6261 ct->status != GOT_STATUS_MODIFY &&
6262 ct->status != GOT_STATUS_MODE_CHANGE &&
6263 ct->status != GOT_STATUS_CONFLICT)
6264 continue;
6266 if (asprintf(&ondisk_path, "%s/%s",
6267 worktree->root_path, pe->path) == -1) {
6268 err = got_error_from_errno("asprintf");
6269 goto done;
6271 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6272 free(ondisk_path);
6273 if (err)
6274 goto done;
6277 /* Recursively write new tree objects. */
6278 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6279 commitable_paths, status_cb, status_arg, repo);
6280 if (err)
6281 goto done;
6283 err = got_object_qid_alloc(&pid, head_commit_id);
6284 if (err)
6285 goto done;
6286 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6287 nparents++;
6288 if (parent_id2) {
6289 err = got_object_qid_alloc(&pid, parent_id2);
6290 if (err)
6291 goto done;
6292 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6293 nparents++;
6295 timestamp = time(NULL);
6296 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6297 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6298 if (logmsg != NULL)
6299 free(logmsg);
6300 if (err)
6301 goto done;
6303 /* Check if a concurrent commit to our branch has occurred. */
6304 head_ref_name = got_worktree_get_head_ref_name(worktree);
6305 if (head_ref_name == NULL) {
6306 err = got_error_from_errno("got_worktree_get_head_ref_name");
6307 goto done;
6309 /* Lock the reference here to prevent concurrent modification. */
6310 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6311 if (err)
6312 goto done;
6313 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6314 if (err)
6315 goto done;
6316 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6317 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6318 goto done;
6320 /* Update branch head in repository. */
6321 err = got_ref_change_ref(head_ref2, *new_commit_id);
6322 if (err)
6323 goto done;
6324 err = got_ref_write(head_ref2, repo);
6325 if (err)
6326 goto done;
6328 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6329 if (err)
6330 goto done;
6332 err = ref_base_commit(worktree, repo);
6333 if (err)
6334 goto done;
6335 done:
6336 got_object_id_queue_free(&parent_ids);
6337 if (head_tree)
6338 got_object_tree_close(head_tree);
6339 if (head_commit)
6340 got_object_commit_close(head_commit);
6341 free(head_commit_id2);
6342 if (head_ref2) {
6343 unlockerr = got_ref_unlock(head_ref2);
6344 if (unlockerr && err == NULL)
6345 err = unlockerr;
6346 got_ref_close(head_ref2);
6348 return err;
6351 static const struct got_error *
6352 check_path_is_commitable(const char *path,
6353 struct got_pathlist_head *commitable_paths)
6355 struct got_pathlist_entry *cpe = NULL;
6356 size_t path_len = strlen(path);
6358 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6359 struct got_commitable *ct = cpe->data;
6360 const char *ct_path = ct->path;
6362 while (ct_path[0] == '/')
6363 ct_path++;
6365 if (strcmp(path, ct_path) == 0 ||
6366 got_path_is_child(ct_path, path, path_len))
6367 break;
6370 if (cpe == NULL)
6371 return got_error_path(path, GOT_ERR_BAD_PATH);
6373 return NULL;
6376 static const struct got_error *
6377 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6379 int *have_staged_files = arg;
6381 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6382 *have_staged_files = 1;
6383 return got_error(GOT_ERR_CANCELLED);
6386 return NULL;
6389 static const struct got_error *
6390 check_non_staged_files(struct got_fileindex *fileindex,
6391 struct got_pathlist_head *paths)
6393 struct got_pathlist_entry *pe;
6394 struct got_fileindex_entry *ie;
6396 TAILQ_FOREACH(pe, paths, entry) {
6397 if (pe->path[0] == '\0')
6398 continue;
6399 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6400 if (ie == NULL)
6401 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6402 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6403 return got_error_path(pe->path,
6404 GOT_ERR_FILE_NOT_STAGED);
6407 return NULL;
6410 const struct got_error *
6411 got_worktree_commit(struct got_object_id **new_commit_id,
6412 struct got_worktree *worktree, struct got_pathlist_head *paths,
6413 const char *author, const char *committer, int allow_bad_symlinks,
6414 int show_diff, int commit_conflicts,
6415 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6416 got_worktree_status_cb status_cb, void *status_arg,
6417 struct got_repository *repo)
6419 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6420 struct got_fileindex *fileindex = NULL;
6421 char *fileindex_path = NULL;
6422 struct got_pathlist_head commitable_paths;
6423 struct collect_commitables_arg cc_arg;
6424 struct got_pathlist_entry *pe;
6425 struct got_reference *head_ref = NULL;
6426 struct got_object_id *head_commit_id = NULL;
6427 char *diff_path = NULL;
6428 int have_staged_files = 0;
6430 *new_commit_id = NULL;
6432 memset(&cc_arg, 0, sizeof(cc_arg));
6433 TAILQ_INIT(&commitable_paths);
6435 err = lock_worktree(worktree, LOCK_EX);
6436 if (err)
6437 goto done;
6439 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6440 if (err)
6441 goto done;
6443 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6444 if (err)
6445 goto done;
6447 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6448 if (err)
6449 goto done;
6451 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6452 &have_staged_files);
6453 if (err && err->code != GOT_ERR_CANCELLED)
6454 goto done;
6455 if (have_staged_files) {
6456 err = check_non_staged_files(fileindex, paths);
6457 if (err)
6458 goto done;
6461 cc_arg.commitable_paths = &commitable_paths;
6462 cc_arg.worktree = worktree;
6463 cc_arg.fileindex = fileindex;
6464 cc_arg.repo = repo;
6465 cc_arg.have_staged_files = have_staged_files;
6466 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6467 cc_arg.diff_header_shown = 0;
6468 cc_arg.commit_conflicts = commit_conflicts;
6469 if (show_diff) {
6470 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6471 GOT_TMPDIR_STR "/got", ".diff");
6472 if (err)
6473 goto done;
6474 cc_arg.f1 = got_opentemp();
6475 if (cc_arg.f1 == NULL) {
6476 err = got_error_from_errno("got_opentemp");
6477 goto done;
6479 cc_arg.f2 = got_opentemp();
6480 if (cc_arg.f2 == NULL) {
6481 err = got_error_from_errno("got_opentemp");
6482 goto done;
6486 TAILQ_FOREACH(pe, paths, entry) {
6487 err = worktree_status(worktree, pe->path, fileindex, repo,
6488 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6489 if (err)
6490 goto done;
6493 if (show_diff) {
6494 if (fflush(cc_arg.diff_outfile) == EOF) {
6495 err = got_error_from_errno("fflush");
6496 goto done;
6500 if (TAILQ_EMPTY(&commitable_paths)) {
6501 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6502 goto done;
6505 TAILQ_FOREACH(pe, paths, entry) {
6506 err = check_path_is_commitable(pe->path, &commitable_paths);
6507 if (err)
6508 goto done;
6511 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6512 struct got_commitable *ct = pe->data;
6513 const char *ct_path = ct->in_repo_path;
6515 while (ct_path[0] == '/')
6516 ct_path++;
6517 err = check_out_of_date(ct_path, ct->status,
6518 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6519 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6520 if (err)
6521 goto done;
6525 err = commit_worktree(new_commit_id, &commitable_paths,
6526 head_commit_id, NULL, worktree, author, committer,
6527 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6528 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6529 if (err)
6530 goto done;
6532 err = update_fileindex_after_commit(worktree, &commitable_paths,
6533 *new_commit_id, fileindex, have_staged_files);
6534 sync_err = sync_fileindex(fileindex, fileindex_path);
6535 if (sync_err && err == NULL)
6536 err = sync_err;
6537 done:
6538 if (fileindex)
6539 got_fileindex_free(fileindex);
6540 free(fileindex_path);
6541 unlockerr = lock_worktree(worktree, LOCK_SH);
6542 if (unlockerr && err == NULL)
6543 err = unlockerr;
6544 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6545 struct got_commitable *ct = pe->data;
6547 free_commitable(ct);
6549 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6550 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6551 err = got_error_from_errno2("unlink", diff_path);
6552 free(diff_path);
6553 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6554 err == NULL)
6555 err = got_error_from_errno("fclose");
6556 return err;
6559 const char *
6560 got_commitable_get_path(struct got_commitable *ct)
6562 return ct->path;
6565 unsigned int
6566 got_commitable_get_status(struct got_commitable *ct)
6568 return ct->status;
6571 struct check_rebase_ok_arg {
6572 struct got_worktree *worktree;
6573 struct got_repository *repo;
6576 static const struct got_error *
6577 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6579 const struct got_error *err = NULL;
6580 struct check_rebase_ok_arg *a = arg;
6581 unsigned char status;
6582 struct stat sb;
6583 char *ondisk_path;
6585 /* Reject rebase of a work tree with mixed base commits. */
6586 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6587 SHA1_DIGEST_LENGTH))
6588 return got_error(GOT_ERR_MIXED_COMMITS);
6590 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6591 == -1)
6592 return got_error_from_errno("asprintf");
6594 /* Reject rebase of a work tree with modified or staged files. */
6595 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6596 free(ondisk_path);
6597 if (err)
6598 return err;
6600 if (status != GOT_STATUS_NO_CHANGE)
6601 return got_error(GOT_ERR_MODIFIED);
6602 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6603 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6605 return NULL;
6608 const struct got_error *
6609 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6610 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6611 struct got_worktree *worktree, struct got_reference *branch,
6612 struct got_repository *repo)
6614 const struct got_error *err = NULL;
6615 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6616 char *branch_ref_name = NULL;
6617 char *fileindex_path = NULL;
6618 struct check_rebase_ok_arg ok_arg;
6619 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6620 struct got_object_id *wt_branch_tip = NULL;
6622 *new_base_branch_ref = NULL;
6623 *tmp_branch = NULL;
6624 *fileindex = NULL;
6626 err = lock_worktree(worktree, LOCK_EX);
6627 if (err)
6628 return err;
6630 err = open_fileindex(fileindex, &fileindex_path, worktree);
6631 if (err)
6632 goto done;
6634 ok_arg.worktree = worktree;
6635 ok_arg.repo = repo;
6636 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6637 &ok_arg);
6638 if (err)
6639 goto done;
6641 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6642 if (err)
6643 goto done;
6645 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6646 if (err)
6647 goto done;
6649 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6650 if (err)
6651 goto done;
6653 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6654 0);
6655 if (err)
6656 goto done;
6658 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6659 if (err)
6660 goto done;
6661 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6662 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6663 goto done;
6666 err = got_ref_alloc_symref(new_base_branch_ref,
6667 new_base_branch_ref_name, wt_branch);
6668 if (err)
6669 goto done;
6670 err = got_ref_write(*new_base_branch_ref, repo);
6671 if (err)
6672 goto done;
6674 /* TODO Lock original branch's ref while rebasing? */
6676 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6677 if (err)
6678 goto done;
6680 err = got_ref_write(branch_ref, repo);
6681 if (err)
6682 goto done;
6684 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6685 worktree->base_commit_id);
6686 if (err)
6687 goto done;
6688 err = got_ref_write(*tmp_branch, repo);
6689 if (err)
6690 goto done;
6692 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6693 if (err)
6694 goto done;
6695 done:
6696 free(fileindex_path);
6697 free(tmp_branch_name);
6698 free(new_base_branch_ref_name);
6699 free(branch_ref_name);
6700 if (branch_ref)
6701 got_ref_close(branch_ref);
6702 if (wt_branch)
6703 got_ref_close(wt_branch);
6704 free(wt_branch_tip);
6705 if (err) {
6706 if (*new_base_branch_ref) {
6707 got_ref_close(*new_base_branch_ref);
6708 *new_base_branch_ref = NULL;
6710 if (*tmp_branch) {
6711 got_ref_close(*tmp_branch);
6712 *tmp_branch = NULL;
6714 if (*fileindex) {
6715 got_fileindex_free(*fileindex);
6716 *fileindex = NULL;
6718 lock_worktree(worktree, LOCK_SH);
6720 return err;
6723 const struct got_error *
6724 got_worktree_rebase_continue(struct got_object_id **commit_id,
6725 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6726 struct got_reference **branch, struct got_fileindex **fileindex,
6727 struct got_worktree *worktree, struct got_repository *repo)
6729 const struct got_error *err;
6730 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6731 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6732 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6733 char *fileindex_path = NULL;
6734 int have_staged_files = 0;
6736 *commit_id = NULL;
6737 *new_base_branch = NULL;
6738 *tmp_branch = NULL;
6739 *branch = NULL;
6740 *fileindex = NULL;
6742 err = lock_worktree(worktree, LOCK_EX);
6743 if (err)
6744 return err;
6746 err = open_fileindex(fileindex, &fileindex_path, worktree);
6747 if (err)
6748 goto done;
6750 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6751 &have_staged_files);
6752 if (err && err->code != GOT_ERR_CANCELLED)
6753 goto done;
6754 if (have_staged_files) {
6755 err = got_error(GOT_ERR_STAGED_PATHS);
6756 goto done;
6759 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6760 if (err)
6761 goto done;
6763 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6764 if (err)
6765 goto done;
6767 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6768 if (err)
6769 goto done;
6771 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6772 if (err)
6773 goto done;
6775 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6776 if (err)
6777 goto done;
6779 err = got_ref_open(branch, repo,
6780 got_ref_get_symref_target(branch_ref), 0);
6781 if (err)
6782 goto done;
6784 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6785 if (err)
6786 goto done;
6788 err = got_ref_resolve(commit_id, repo, commit_ref);
6789 if (err)
6790 goto done;
6792 err = got_ref_open(new_base_branch, repo,
6793 new_base_branch_ref_name, 0);
6794 if (err)
6795 goto done;
6797 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6798 if (err)
6799 goto done;
6800 done:
6801 free(commit_ref_name);
6802 free(branch_ref_name);
6803 free(fileindex_path);
6804 if (commit_ref)
6805 got_ref_close(commit_ref);
6806 if (branch_ref)
6807 got_ref_close(branch_ref);
6808 if (err) {
6809 free(*commit_id);
6810 *commit_id = NULL;
6811 if (*tmp_branch) {
6812 got_ref_close(*tmp_branch);
6813 *tmp_branch = NULL;
6815 if (*new_base_branch) {
6816 got_ref_close(*new_base_branch);
6817 *new_base_branch = NULL;
6819 if (*branch) {
6820 got_ref_close(*branch);
6821 *branch = NULL;
6823 if (*fileindex) {
6824 got_fileindex_free(*fileindex);
6825 *fileindex = NULL;
6827 lock_worktree(worktree, LOCK_SH);
6829 return err;
6832 const struct got_error *
6833 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6835 const struct got_error *err;
6836 char *tmp_branch_name = NULL;
6838 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6839 if (err)
6840 return err;
6842 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6843 free(tmp_branch_name);
6844 return NULL;
6847 static const struct got_error *
6848 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6849 const char *diff_path, char **logmsg, void *arg)
6851 *logmsg = arg;
6852 return NULL;
6855 static const struct got_error *
6856 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6857 const char *path, struct got_object_id *blob_id,
6858 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6859 int dirfd, const char *de_name)
6861 return NULL;
6864 struct collect_merged_paths_arg {
6865 got_worktree_checkout_cb progress_cb;
6866 void *progress_arg;
6867 struct got_pathlist_head *merged_paths;
6870 static const struct got_error *
6871 collect_merged_paths(void *arg, unsigned char status, const char *path)
6873 const struct got_error *err;
6874 struct collect_merged_paths_arg *a = arg;
6875 char *p;
6876 struct got_pathlist_entry *new;
6878 err = (*a->progress_cb)(a->progress_arg, status, path);
6879 if (err)
6880 return err;
6882 if (status != GOT_STATUS_MERGE &&
6883 status != GOT_STATUS_ADD &&
6884 status != GOT_STATUS_DELETE &&
6885 status != GOT_STATUS_CONFLICT)
6886 return NULL;
6888 p = strdup(path);
6889 if (p == NULL)
6890 return got_error_from_errno("strdup");
6892 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6893 if (err || new == NULL)
6894 free(p);
6895 return err;
6898 static const struct got_error *
6899 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6900 int is_rebase, struct got_repository *repo)
6902 const struct got_error *err;
6903 struct got_reference *commit_ref = NULL;
6905 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6906 if (err) {
6907 if (err->code != GOT_ERR_NOT_REF)
6908 goto done;
6909 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6910 if (err)
6911 goto done;
6912 err = got_ref_write(commit_ref, repo);
6913 if (err)
6914 goto done;
6915 } else if (is_rebase) {
6916 struct got_object_id *stored_id;
6917 int cmp;
6919 err = got_ref_resolve(&stored_id, repo, commit_ref);
6920 if (err)
6921 goto done;
6922 cmp = got_object_id_cmp(commit_id, stored_id);
6923 free(stored_id);
6924 if (cmp != 0) {
6925 err = got_error(GOT_ERR_REBASE_COMMITID);
6926 goto done;
6929 done:
6930 if (commit_ref)
6931 got_ref_close(commit_ref);
6932 return err;
6935 static const struct got_error *
6936 rebase_merge_files(struct got_pathlist_head *merged_paths,
6937 const char *commit_ref_name, struct got_worktree *worktree,
6938 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6939 struct got_object_id *commit_id, struct got_repository *repo,
6940 got_worktree_checkout_cb progress_cb, void *progress_arg,
6941 got_cancel_cb cancel_cb, void *cancel_arg)
6943 const struct got_error *err;
6944 struct got_reference *commit_ref = NULL;
6945 struct collect_merged_paths_arg cmp_arg;
6946 char *fileindex_path;
6948 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6950 err = get_fileindex_path(&fileindex_path, worktree);
6951 if (err)
6952 return err;
6954 cmp_arg.progress_cb = progress_cb;
6955 cmp_arg.progress_arg = progress_arg;
6956 cmp_arg.merged_paths = merged_paths;
6957 err = merge_files(worktree, fileindex, fileindex_path,
6958 parent_commit_id, commit_id, repo, collect_merged_paths,
6959 &cmp_arg, cancel_cb, cancel_arg);
6960 if (commit_ref)
6961 got_ref_close(commit_ref);
6962 return err;
6965 const struct got_error *
6966 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6967 struct got_worktree *worktree, struct got_fileindex *fileindex,
6968 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6969 struct got_repository *repo,
6970 got_worktree_checkout_cb progress_cb, void *progress_arg,
6971 got_cancel_cb cancel_cb, void *cancel_arg)
6973 const struct got_error *err;
6974 char *commit_ref_name;
6976 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6977 if (err)
6978 return err;
6980 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6981 if (err)
6982 goto done;
6984 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6985 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6986 progress_arg, cancel_cb, cancel_arg);
6987 done:
6988 free(commit_ref_name);
6989 return err;
6992 const struct got_error *
6993 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6994 struct got_worktree *worktree, struct got_fileindex *fileindex,
6995 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6996 struct got_repository *repo,
6997 got_worktree_checkout_cb progress_cb, void *progress_arg,
6998 got_cancel_cb cancel_cb, void *cancel_arg)
7000 const struct got_error *err;
7001 char *commit_ref_name;
7003 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7004 if (err)
7005 return err;
7007 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7008 if (err)
7009 goto done;
7011 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7012 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7013 progress_arg, cancel_cb, cancel_arg);
7014 done:
7015 free(commit_ref_name);
7016 return err;
7019 static const struct got_error *
7020 rebase_commit(struct got_object_id **new_commit_id,
7021 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7022 struct got_worktree *worktree, struct got_fileindex *fileindex,
7023 struct got_reference *tmp_branch, const char *committer,
7024 struct got_commit_object *orig_commit, const char *new_logmsg,
7025 int allow_conflict, struct got_repository *repo)
7027 const struct got_error *err, *sync_err;
7028 struct got_pathlist_head commitable_paths;
7029 struct collect_commitables_arg cc_arg;
7030 char *fileindex_path = NULL;
7031 struct got_reference *head_ref = NULL;
7032 struct got_object_id *head_commit_id = NULL;
7033 char *logmsg = NULL;
7035 memset(&cc_arg, 0, sizeof(cc_arg));
7036 TAILQ_INIT(&commitable_paths);
7037 *new_commit_id = NULL;
7039 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7041 err = get_fileindex_path(&fileindex_path, worktree);
7042 if (err)
7043 return err;
7045 cc_arg.commitable_paths = &commitable_paths;
7046 cc_arg.worktree = worktree;
7047 cc_arg.repo = repo;
7048 cc_arg.have_staged_files = 0;
7049 cc_arg.commit_conflicts = allow_conflict;
7051 * If possible get the status of individual files directly to
7052 * avoid crawling the entire work tree once per rebased commit.
7054 * Ideally, merged_paths would contain a list of commitables
7055 * we could use so we could skip worktree_status() entirely.
7056 * However, we would then need carefully keep track of cumulative
7057 * effects of operations such as file additions and deletions
7058 * in 'got histedit -f' (folding multiple commits into one),
7059 * and this extra complexity is not really worth it.
7061 if (merged_paths) {
7062 struct got_pathlist_entry *pe;
7063 TAILQ_FOREACH(pe, merged_paths, entry) {
7064 err = worktree_status(worktree, pe->path, fileindex,
7065 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7066 0);
7067 if (err)
7068 goto done;
7070 } else {
7071 err = worktree_status(worktree, "", fileindex, repo,
7072 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7073 if (err)
7074 goto done;
7077 if (TAILQ_EMPTY(&commitable_paths)) {
7078 /* No-op change; commit will be elided. */
7079 err = got_ref_delete(commit_ref, repo);
7080 if (err)
7081 goto done;
7082 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7083 goto done;
7086 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7087 if (err)
7088 goto done;
7090 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7091 if (err)
7092 goto done;
7094 if (new_logmsg) {
7095 logmsg = strdup(new_logmsg);
7096 if (logmsg == NULL) {
7097 err = got_error_from_errno("strdup");
7098 goto done;
7100 } else {
7101 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7102 if (err)
7103 goto done;
7106 /* NB: commit_worktree will call free(logmsg) */
7107 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7108 NULL, worktree, got_object_commit_get_author(orig_commit),
7109 committer ? committer :
7110 got_object_commit_get_committer(orig_commit), NULL,
7111 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7112 if (err)
7113 goto done;
7115 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7116 if (err)
7117 goto done;
7119 err = got_ref_delete(commit_ref, repo);
7120 if (err)
7121 goto done;
7123 err = update_fileindex_after_commit(worktree, &commitable_paths,
7124 *new_commit_id, fileindex, 0);
7125 sync_err = sync_fileindex(fileindex, fileindex_path);
7126 if (sync_err && err == NULL)
7127 err = sync_err;
7128 done:
7129 free(fileindex_path);
7130 free(head_commit_id);
7131 if (head_ref)
7132 got_ref_close(head_ref);
7133 if (err) {
7134 free(*new_commit_id);
7135 *new_commit_id = NULL;
7137 return err;
7140 const struct got_error *
7141 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7142 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7143 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7144 const char *committer, struct got_commit_object *orig_commit,
7145 struct got_object_id *orig_commit_id, int allow_conflict,
7146 struct got_repository *repo)
7148 const struct got_error *err;
7149 char *commit_ref_name;
7150 struct got_reference *commit_ref = NULL;
7151 struct got_object_id *commit_id = NULL;
7153 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7154 if (err)
7155 return err;
7157 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7158 if (err)
7159 goto done;
7160 err = got_ref_resolve(&commit_id, repo, commit_ref);
7161 if (err)
7162 goto done;
7163 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7164 err = got_error(GOT_ERR_REBASE_COMMITID);
7165 goto done;
7168 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7169 worktree, fileindex, tmp_branch, committer, orig_commit,
7170 NULL, allow_conflict, repo);
7171 done:
7172 if (commit_ref)
7173 got_ref_close(commit_ref);
7174 free(commit_ref_name);
7175 free(commit_id);
7176 return err;
7179 const struct got_error *
7180 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7181 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7182 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7183 const char *committer, struct got_commit_object *orig_commit,
7184 struct got_object_id *orig_commit_id, const char *new_logmsg,
7185 int allow_conflict, struct got_repository *repo)
7187 const struct got_error *err;
7188 char *commit_ref_name;
7189 struct got_reference *commit_ref = NULL;
7191 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7192 if (err)
7193 return err;
7195 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7196 if (err)
7197 goto done;
7199 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7200 worktree, fileindex, tmp_branch, committer, orig_commit,
7201 new_logmsg, allow_conflict, repo);
7202 done:
7203 if (commit_ref)
7204 got_ref_close(commit_ref);
7205 free(commit_ref_name);
7206 return err;
7209 const struct got_error *
7210 got_worktree_rebase_postpone(struct got_worktree *worktree,
7211 struct got_fileindex *fileindex)
7213 if (fileindex)
7214 got_fileindex_free(fileindex);
7215 return lock_worktree(worktree, LOCK_SH);
7218 static const struct got_error *
7219 delete_ref(const char *name, struct got_repository *repo)
7221 const struct got_error *err;
7222 struct got_reference *ref;
7224 err = got_ref_open(&ref, repo, name, 0);
7225 if (err) {
7226 if (err->code == GOT_ERR_NOT_REF)
7227 return NULL;
7228 return err;
7231 err = got_ref_delete(ref, repo);
7232 got_ref_close(ref);
7233 return err;
7236 static const struct got_error *
7237 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7239 const struct got_error *err;
7240 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7241 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7243 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7244 if (err)
7245 goto done;
7246 err = delete_ref(tmp_branch_name, repo);
7247 if (err)
7248 goto done;
7250 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7251 if (err)
7252 goto done;
7253 err = delete_ref(new_base_branch_ref_name, repo);
7254 if (err)
7255 goto done;
7257 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7258 if (err)
7259 goto done;
7260 err = delete_ref(branch_ref_name, repo);
7261 if (err)
7262 goto done;
7264 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7265 if (err)
7266 goto done;
7267 err = delete_ref(commit_ref_name, repo);
7268 if (err)
7269 goto done;
7271 done:
7272 free(tmp_branch_name);
7273 free(new_base_branch_ref_name);
7274 free(branch_ref_name);
7275 free(commit_ref_name);
7276 return err;
7279 static const struct got_error *
7280 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7281 struct got_object_id *new_commit_id, struct got_repository *repo)
7283 const struct got_error *err;
7284 struct got_reference *ref = NULL;
7285 struct got_object_id *old_commit_id = NULL;
7286 const char *branch_name = NULL;
7287 char *new_id_str = NULL;
7288 char *refname = NULL;
7290 branch_name = got_ref_get_name(branch);
7291 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7292 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7293 branch_name += 11;
7295 err = got_object_id_str(&new_id_str, new_commit_id);
7296 if (err)
7297 return err;
7299 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7300 new_id_str) == -1) {
7301 err = got_error_from_errno("asprintf");
7302 goto done;
7305 err = got_ref_resolve(&old_commit_id, repo, branch);
7306 if (err)
7307 goto done;
7309 err = got_ref_alloc(&ref, refname, old_commit_id);
7310 if (err)
7311 goto done;
7313 err = got_ref_write(ref, repo);
7314 done:
7315 free(new_id_str);
7316 free(refname);
7317 free(old_commit_id);
7318 if (ref)
7319 got_ref_close(ref);
7320 return err;
7323 const struct got_error *
7324 got_worktree_rebase_complete(struct got_worktree *worktree,
7325 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7326 struct got_reference *rebased_branch, struct got_repository *repo,
7327 int create_backup)
7329 const struct got_error *err, *unlockerr, *sync_err;
7330 struct got_object_id *new_head_commit_id = NULL;
7331 char *fileindex_path = NULL;
7333 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7334 if (err)
7335 return err;
7337 if (create_backup) {
7338 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7339 rebased_branch, new_head_commit_id, repo);
7340 if (err)
7341 goto done;
7344 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7345 if (err)
7346 goto done;
7348 err = got_ref_write(rebased_branch, repo);
7349 if (err)
7350 goto done;
7352 err = got_worktree_set_head_ref(worktree, rebased_branch);
7353 if (err)
7354 goto done;
7356 err = delete_rebase_refs(worktree, repo);
7357 if (err)
7358 goto done;
7360 err = get_fileindex_path(&fileindex_path, worktree);
7361 if (err)
7362 goto done;
7363 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7364 sync_err = sync_fileindex(fileindex, fileindex_path);
7365 if (sync_err && err == NULL)
7366 err = sync_err;
7367 done:
7368 got_fileindex_free(fileindex);
7369 free(fileindex_path);
7370 free(new_head_commit_id);
7371 unlockerr = lock_worktree(worktree, LOCK_SH);
7372 if (unlockerr && err == NULL)
7373 err = unlockerr;
7374 return err;
7377 static const struct got_error *
7378 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7379 struct got_object_id *id1, struct got_object_id *id2,
7380 struct got_repository *repo)
7382 const struct got_error *err;
7383 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7384 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7386 if (id1) {
7387 err = got_object_open_as_commit(&commit1, repo, id1);
7388 if (err)
7389 goto done;
7391 err = got_object_open_as_tree(&tree1, repo,
7392 got_object_commit_get_tree_id(commit1));
7393 if (err)
7394 goto done;
7397 if (id2) {
7398 err = got_object_open_as_commit(&commit2, repo, id2);
7399 if (err)
7400 goto done;
7402 err = got_object_open_as_tree(&tree2, repo,
7403 got_object_commit_get_tree_id(commit2));
7404 if (err)
7405 goto done;
7408 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7409 got_diff_tree_collect_changed_paths, paths, 0);
7410 if (err)
7411 goto done;
7412 done:
7413 if (commit1)
7414 got_object_commit_close(commit1);
7415 if (commit2)
7416 got_object_commit_close(commit2);
7417 if (tree1)
7418 got_object_tree_close(tree1);
7419 if (tree2)
7420 got_object_tree_close(tree2);
7421 return err;
7424 static const struct got_error *
7425 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7426 struct got_object_id *id1, struct got_object_id *id2,
7427 const char *path_prefix, struct got_repository *repo)
7429 const struct got_error *err;
7430 struct got_pathlist_head merged_paths;
7431 struct got_pathlist_entry *pe;
7432 char *abspath = NULL, *wt_path = NULL;
7434 TAILQ_INIT(&merged_paths);
7436 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7437 if (err)
7438 goto done;
7440 TAILQ_FOREACH(pe, &merged_paths, entry) {
7441 struct got_diff_changed_path *change = pe->data;
7443 if (change->status != GOT_STATUS_ADD)
7444 continue;
7446 if (got_path_is_root_dir(path_prefix)) {
7447 wt_path = strdup(pe->path);
7448 if (wt_path == NULL) {
7449 err = got_error_from_errno("strdup");
7450 goto done;
7452 } else {
7453 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7454 err = got_error_from_errno("asprintf");
7455 goto done;
7458 err = got_path_skip_common_ancestor(&wt_path,
7459 path_prefix, abspath);
7460 if (err)
7461 goto done;
7462 free(abspath);
7463 abspath = NULL;
7466 err = got_pathlist_append(added_paths, wt_path, NULL);
7467 if (err)
7468 goto done;
7469 wt_path = NULL;
7472 done:
7473 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7474 free(abspath);
7475 free(wt_path);
7476 return err;
7479 static const struct got_error *
7480 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7481 struct got_object_id *id, const char *path_prefix,
7482 struct got_repository *repo)
7484 const struct got_error *err;
7485 struct got_commit_object *commit = NULL;
7486 struct got_object_qid *pid;
7488 err = got_object_open_as_commit(&commit, repo, id);
7489 if (err)
7490 goto done;
7492 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7494 err = get_paths_added_between_commits(added_paths,
7495 pid ? &pid->id : NULL, id, path_prefix, repo);
7496 if (err)
7497 goto done;
7498 done:
7499 if (commit)
7500 got_object_commit_close(commit);
7501 return err;
7504 const struct got_error *
7505 got_worktree_rebase_abort(struct got_worktree *worktree,
7506 struct got_fileindex *fileindex, struct got_repository *repo,
7507 struct got_reference *new_base_branch,
7508 got_worktree_checkout_cb progress_cb, void *progress_arg)
7510 const struct got_error *err, *unlockerr, *sync_err;
7511 struct got_reference *resolved = NULL;
7512 struct got_object_id *commit_id = NULL;
7513 struct got_object_id *merged_commit_id = NULL;
7514 struct got_commit_object *commit = NULL;
7515 char *fileindex_path = NULL;
7516 char *commit_ref_name = NULL;
7517 struct got_reference *commit_ref = NULL;
7518 struct revert_file_args rfa;
7519 struct got_object_id *tree_id = NULL;
7520 struct got_pathlist_head added_paths;
7522 TAILQ_INIT(&added_paths);
7524 err = lock_worktree(worktree, LOCK_EX);
7525 if (err)
7526 return err;
7528 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7529 if (err)
7530 goto done;
7532 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7533 if (err)
7534 goto done;
7536 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7537 if (err)
7538 goto done;
7541 * Determine which files in added status can be safely removed
7542 * from disk while reverting changes in the work tree.
7543 * We want to avoid deleting unrelated files which were added by
7544 * the user for conflict resolution purposes.
7546 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7547 got_worktree_get_path_prefix(worktree), repo);
7548 if (err)
7549 goto done;
7551 err = got_ref_open(&resolved, repo,
7552 got_ref_get_symref_target(new_base_branch), 0);
7553 if (err)
7554 goto done;
7556 err = got_worktree_set_head_ref(worktree, resolved);
7557 if (err)
7558 goto done;
7561 * XXX commits to the base branch could have happened while
7562 * we were busy rebasing; should we store the original commit ID
7563 * when rebase begins and read it back here?
7565 err = got_ref_resolve(&commit_id, repo, resolved);
7566 if (err)
7567 goto done;
7569 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7570 if (err)
7571 goto done;
7573 err = got_object_open_as_commit(&commit, repo,
7574 worktree->base_commit_id);
7575 if (err)
7576 goto done;
7578 err = got_object_id_by_path(&tree_id, repo, commit,
7579 worktree->path_prefix);
7580 if (err)
7581 goto done;
7583 err = delete_rebase_refs(worktree, repo);
7584 if (err)
7585 goto done;
7587 err = get_fileindex_path(&fileindex_path, worktree);
7588 if (err)
7589 goto done;
7591 rfa.worktree = worktree;
7592 rfa.fileindex = fileindex;
7593 rfa.progress_cb = progress_cb;
7594 rfa.progress_arg = progress_arg;
7595 rfa.patch_cb = NULL;
7596 rfa.patch_arg = NULL;
7597 rfa.repo = repo;
7598 rfa.unlink_added_files = 1;
7599 rfa.added_files_to_unlink = &added_paths;
7600 err = worktree_status(worktree, "", fileindex, repo,
7601 revert_file, &rfa, NULL, NULL, 1, 0);
7602 if (err)
7603 goto sync;
7605 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7606 repo, progress_cb, progress_arg, NULL, NULL);
7607 sync:
7608 sync_err = sync_fileindex(fileindex, fileindex_path);
7609 if (sync_err && err == NULL)
7610 err = sync_err;
7611 done:
7612 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7613 got_ref_close(resolved);
7614 free(tree_id);
7615 free(commit_id);
7616 free(merged_commit_id);
7617 if (commit)
7618 got_object_commit_close(commit);
7619 if (fileindex)
7620 got_fileindex_free(fileindex);
7621 free(fileindex_path);
7622 free(commit_ref_name);
7623 if (commit_ref)
7624 got_ref_close(commit_ref);
7626 unlockerr = lock_worktree(worktree, LOCK_SH);
7627 if (unlockerr && err == NULL)
7628 err = unlockerr;
7629 return err;
7632 const struct got_error *
7633 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7634 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7635 struct got_fileindex **fileindex, struct got_worktree *worktree,
7636 struct got_repository *repo)
7638 const struct got_error *err = NULL;
7639 char *tmp_branch_name = NULL;
7640 char *branch_ref_name = NULL;
7641 char *base_commit_ref_name = NULL;
7642 char *fileindex_path = NULL;
7643 struct check_rebase_ok_arg ok_arg;
7644 struct got_reference *wt_branch = NULL;
7645 struct got_reference *base_commit_ref = NULL;
7647 *tmp_branch = NULL;
7648 *branch_ref = NULL;
7649 *base_commit_id = NULL;
7650 *fileindex = NULL;
7652 err = lock_worktree(worktree, LOCK_EX);
7653 if (err)
7654 return err;
7656 err = open_fileindex(fileindex, &fileindex_path, worktree);
7657 if (err)
7658 goto done;
7660 ok_arg.worktree = worktree;
7661 ok_arg.repo = repo;
7662 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7663 &ok_arg);
7664 if (err)
7665 goto done;
7667 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7668 if (err)
7669 goto done;
7671 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7672 if (err)
7673 goto done;
7675 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7676 worktree);
7677 if (err)
7678 goto done;
7680 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7681 0);
7682 if (err)
7683 goto done;
7685 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7686 if (err)
7687 goto done;
7689 err = got_ref_write(*branch_ref, repo);
7690 if (err)
7691 goto done;
7693 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7694 worktree->base_commit_id);
7695 if (err)
7696 goto done;
7697 err = got_ref_write(base_commit_ref, repo);
7698 if (err)
7699 goto done;
7700 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7701 if (*base_commit_id == NULL) {
7702 err = got_error_from_errno("got_object_id_dup");
7703 goto done;
7706 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7707 worktree->base_commit_id);
7708 if (err)
7709 goto done;
7710 err = got_ref_write(*tmp_branch, repo);
7711 if (err)
7712 goto done;
7714 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7715 if (err)
7716 goto done;
7717 done:
7718 free(fileindex_path);
7719 free(tmp_branch_name);
7720 free(branch_ref_name);
7721 free(base_commit_ref_name);
7722 if (wt_branch)
7723 got_ref_close(wt_branch);
7724 if (err) {
7725 if (*branch_ref) {
7726 got_ref_close(*branch_ref);
7727 *branch_ref = NULL;
7729 if (*tmp_branch) {
7730 got_ref_close(*tmp_branch);
7731 *tmp_branch = NULL;
7733 free(*base_commit_id);
7734 if (*fileindex) {
7735 got_fileindex_free(*fileindex);
7736 *fileindex = NULL;
7738 lock_worktree(worktree, LOCK_SH);
7740 return err;
7743 const struct got_error *
7744 got_worktree_histedit_postpone(struct got_worktree *worktree,
7745 struct got_fileindex *fileindex)
7747 if (fileindex)
7748 got_fileindex_free(fileindex);
7749 return lock_worktree(worktree, LOCK_SH);
7752 const struct got_error *
7753 got_worktree_histedit_in_progress(int *in_progress,
7754 struct got_worktree *worktree)
7756 const struct got_error *err;
7757 char *tmp_branch_name = NULL;
7759 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7760 if (err)
7761 return err;
7763 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7764 free(tmp_branch_name);
7765 return NULL;
7768 const struct got_error *
7769 got_worktree_histedit_continue(struct got_object_id **commit_id,
7770 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7771 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7772 struct got_worktree *worktree, struct got_repository *repo)
7774 const struct got_error *err;
7775 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7776 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7777 struct got_reference *commit_ref = NULL;
7778 struct got_reference *base_commit_ref = NULL;
7779 char *fileindex_path = NULL;
7780 int have_staged_files = 0;
7782 *commit_id = NULL;
7783 *tmp_branch = NULL;
7784 *base_commit_id = NULL;
7785 *fileindex = NULL;
7787 err = lock_worktree(worktree, LOCK_EX);
7788 if (err)
7789 return err;
7791 err = open_fileindex(fileindex, &fileindex_path, worktree);
7792 if (err)
7793 goto done;
7795 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7796 &have_staged_files);
7797 if (err && err->code != GOT_ERR_CANCELLED)
7798 goto done;
7799 if (have_staged_files) {
7800 err = got_error(GOT_ERR_STAGED_PATHS);
7801 goto done;
7804 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7805 if (err)
7806 goto done;
7808 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7809 if (err)
7810 goto done;
7812 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7813 if (err)
7814 goto done;
7816 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7817 worktree);
7818 if (err)
7819 goto done;
7821 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7822 if (err)
7823 goto done;
7825 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7826 if (err)
7827 goto done;
7828 err = got_ref_resolve(commit_id, repo, commit_ref);
7829 if (err)
7830 goto done;
7832 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7833 if (err)
7834 goto done;
7835 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7836 if (err)
7837 goto done;
7839 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7840 if (err)
7841 goto done;
7842 done:
7843 free(commit_ref_name);
7844 free(branch_ref_name);
7845 free(fileindex_path);
7846 if (commit_ref)
7847 got_ref_close(commit_ref);
7848 if (base_commit_ref)
7849 got_ref_close(base_commit_ref);
7850 if (err) {
7851 free(*commit_id);
7852 *commit_id = NULL;
7853 free(*base_commit_id);
7854 *base_commit_id = NULL;
7855 if (*tmp_branch) {
7856 got_ref_close(*tmp_branch);
7857 *tmp_branch = NULL;
7859 if (*fileindex) {
7860 got_fileindex_free(*fileindex);
7861 *fileindex = NULL;
7863 lock_worktree(worktree, LOCK_EX);
7865 return err;
7868 static const struct got_error *
7869 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7871 const struct got_error *err;
7872 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7873 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7875 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7876 if (err)
7877 goto done;
7878 err = delete_ref(tmp_branch_name, repo);
7879 if (err)
7880 goto done;
7882 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7883 worktree);
7884 if (err)
7885 goto done;
7886 err = delete_ref(base_commit_ref_name, repo);
7887 if (err)
7888 goto done;
7890 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7891 if (err)
7892 goto done;
7893 err = delete_ref(branch_ref_name, repo);
7894 if (err)
7895 goto done;
7897 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7898 if (err)
7899 goto done;
7900 err = delete_ref(commit_ref_name, repo);
7901 if (err)
7902 goto done;
7903 done:
7904 free(tmp_branch_name);
7905 free(base_commit_ref_name);
7906 free(branch_ref_name);
7907 free(commit_ref_name);
7908 return err;
7911 const struct got_error *
7912 got_worktree_histedit_abort(struct got_worktree *worktree,
7913 struct got_fileindex *fileindex, struct got_repository *repo,
7914 struct got_reference *branch, struct got_object_id *base_commit_id,
7915 got_worktree_checkout_cb progress_cb, void *progress_arg)
7917 const struct got_error *err, *unlockerr, *sync_err;
7918 struct got_reference *resolved = NULL;
7919 char *fileindex_path = NULL;
7920 struct got_object_id *merged_commit_id = NULL;
7921 struct got_commit_object *commit = NULL;
7922 char *commit_ref_name = NULL;
7923 struct got_reference *commit_ref = NULL;
7924 struct got_object_id *tree_id = NULL;
7925 struct revert_file_args rfa;
7926 struct got_pathlist_head added_paths;
7928 TAILQ_INIT(&added_paths);
7930 err = lock_worktree(worktree, LOCK_EX);
7931 if (err)
7932 return err;
7934 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7935 if (err)
7936 goto done;
7938 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7939 if (err) {
7940 if (err->code != GOT_ERR_NOT_REF)
7941 goto done;
7942 /* Can happen on early abort due to invalid histedit script. */
7943 commit_ref = NULL;
7946 if (commit_ref) {
7947 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7948 if (err)
7949 goto done;
7952 * Determine which files in added status can be safely removed
7953 * from disk while reverting changes in the work tree.
7954 * We want to avoid deleting unrelated files added by the
7955 * user during conflict resolution or during histedit -e.
7957 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7958 got_worktree_get_path_prefix(worktree), repo);
7959 if (err)
7960 goto done;
7963 err = got_ref_open(&resolved, repo,
7964 got_ref_get_symref_target(branch), 0);
7965 if (err)
7966 goto done;
7968 err = got_worktree_set_head_ref(worktree, resolved);
7969 if (err)
7970 goto done;
7972 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7973 if (err)
7974 goto done;
7976 err = got_object_open_as_commit(&commit, repo,
7977 worktree->base_commit_id);
7978 if (err)
7979 goto done;
7981 err = got_object_id_by_path(&tree_id, repo, commit,
7982 worktree->path_prefix);
7983 if (err)
7984 goto done;
7986 err = delete_histedit_refs(worktree, repo);
7987 if (err)
7988 goto done;
7990 err = get_fileindex_path(&fileindex_path, worktree);
7991 if (err)
7992 goto done;
7994 rfa.worktree = worktree;
7995 rfa.fileindex = fileindex;
7996 rfa.progress_cb = progress_cb;
7997 rfa.progress_arg = progress_arg;
7998 rfa.patch_cb = NULL;
7999 rfa.patch_arg = NULL;
8000 rfa.repo = repo;
8001 rfa.unlink_added_files = 1;
8002 rfa.added_files_to_unlink = &added_paths;
8003 err = worktree_status(worktree, "", fileindex, repo,
8004 revert_file, &rfa, NULL, NULL, 1, 0);
8005 if (err)
8006 goto sync;
8008 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8009 repo, progress_cb, progress_arg, NULL, NULL);
8010 sync:
8011 sync_err = sync_fileindex(fileindex, fileindex_path);
8012 if (sync_err && err == NULL)
8013 err = sync_err;
8014 done:
8015 if (resolved)
8016 got_ref_close(resolved);
8017 if (commit_ref)
8018 got_ref_close(commit_ref);
8019 free(merged_commit_id);
8020 free(tree_id);
8021 free(fileindex_path);
8022 free(commit_ref_name);
8024 unlockerr = lock_worktree(worktree, LOCK_SH);
8025 if (unlockerr && err == NULL)
8026 err = unlockerr;
8027 return err;
8030 const struct got_error *
8031 got_worktree_histedit_complete(struct got_worktree *worktree,
8032 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8033 struct got_reference *edited_branch, struct got_repository *repo)
8035 const struct got_error *err, *unlockerr, *sync_err;
8036 struct got_object_id *new_head_commit_id = NULL;
8037 struct got_reference *resolved = NULL;
8038 char *fileindex_path = NULL;
8040 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8041 if (err)
8042 return err;
8044 err = got_ref_open(&resolved, repo,
8045 got_ref_get_symref_target(edited_branch), 0);
8046 if (err)
8047 goto done;
8049 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8050 resolved, new_head_commit_id, repo);
8051 if (err)
8052 goto done;
8054 err = got_ref_change_ref(resolved, new_head_commit_id);
8055 if (err)
8056 goto done;
8058 err = got_ref_write(resolved, repo);
8059 if (err)
8060 goto done;
8062 err = got_worktree_set_head_ref(worktree, resolved);
8063 if (err)
8064 goto done;
8066 err = delete_histedit_refs(worktree, repo);
8067 if (err)
8068 goto done;
8070 err = get_fileindex_path(&fileindex_path, worktree);
8071 if (err)
8072 goto done;
8073 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8074 sync_err = sync_fileindex(fileindex, fileindex_path);
8075 if (sync_err && err == NULL)
8076 err = sync_err;
8077 done:
8078 got_fileindex_free(fileindex);
8079 free(fileindex_path);
8080 free(new_head_commit_id);
8081 unlockerr = lock_worktree(worktree, LOCK_SH);
8082 if (unlockerr && err == NULL)
8083 err = unlockerr;
8084 return err;
8087 const struct got_error *
8088 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8089 struct got_object_id *commit_id, struct got_repository *repo)
8091 const struct got_error *err;
8092 char *commit_ref_name;
8094 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8095 if (err)
8096 return err;
8098 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8099 if (err)
8100 goto done;
8102 err = delete_ref(commit_ref_name, repo);
8103 done:
8104 free(commit_ref_name);
8105 return err;
8108 const struct got_error *
8109 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8110 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8111 struct got_worktree *worktree, const char *refname,
8112 struct got_repository *repo)
8114 const struct got_error *err = NULL;
8115 char *fileindex_path = NULL;
8116 struct check_rebase_ok_arg ok_arg;
8118 *fileindex = NULL;
8119 *branch_ref = NULL;
8120 *base_branch_ref = NULL;
8122 err = lock_worktree(worktree, LOCK_EX);
8123 if (err)
8124 return err;
8126 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8127 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8128 "cannot integrate a branch into itself; "
8129 "update -b or different branch name required");
8130 goto done;
8133 err = open_fileindex(fileindex, &fileindex_path, worktree);
8134 if (err)
8135 goto done;
8137 /* Preconditions are the same as for rebase. */
8138 ok_arg.worktree = worktree;
8139 ok_arg.repo = repo;
8140 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8141 &ok_arg);
8142 if (err)
8143 goto done;
8145 err = got_ref_open(branch_ref, repo, refname, 1);
8146 if (err)
8147 goto done;
8149 err = got_ref_open(base_branch_ref, repo,
8150 got_worktree_get_head_ref_name(worktree), 1);
8151 done:
8152 if (err) {
8153 if (*branch_ref) {
8154 got_ref_close(*branch_ref);
8155 *branch_ref = NULL;
8157 if (*base_branch_ref) {
8158 got_ref_close(*base_branch_ref);
8159 *base_branch_ref = NULL;
8161 if (*fileindex) {
8162 got_fileindex_free(*fileindex);
8163 *fileindex = NULL;
8165 lock_worktree(worktree, LOCK_SH);
8167 return err;
8170 const struct got_error *
8171 got_worktree_integrate_continue(struct got_worktree *worktree,
8172 struct got_fileindex *fileindex, struct got_repository *repo,
8173 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8174 got_worktree_checkout_cb progress_cb, void *progress_arg,
8175 got_cancel_cb cancel_cb, void *cancel_arg)
8177 const struct got_error *err = NULL, *sync_err, *unlockerr;
8178 char *fileindex_path = NULL;
8179 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8180 struct got_commit_object *commit = NULL;
8182 err = get_fileindex_path(&fileindex_path, worktree);
8183 if (err)
8184 goto done;
8186 err = got_ref_resolve(&commit_id, repo, branch_ref);
8187 if (err)
8188 goto done;
8190 err = got_object_open_as_commit(&commit, repo, commit_id);
8191 if (err)
8192 goto done;
8194 err = got_object_id_by_path(&tree_id, repo, commit,
8195 worktree->path_prefix);
8196 if (err)
8197 goto done;
8199 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8200 if (err)
8201 goto done;
8203 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8204 progress_cb, progress_arg, cancel_cb, cancel_arg);
8205 if (err)
8206 goto sync;
8208 err = got_ref_change_ref(base_branch_ref, commit_id);
8209 if (err)
8210 goto sync;
8212 err = got_ref_write(base_branch_ref, repo);
8213 if (err)
8214 goto sync;
8216 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8217 sync:
8218 sync_err = sync_fileindex(fileindex, fileindex_path);
8219 if (sync_err && err == NULL)
8220 err = sync_err;
8222 done:
8223 unlockerr = got_ref_unlock(branch_ref);
8224 if (unlockerr && err == NULL)
8225 err = unlockerr;
8226 got_ref_close(branch_ref);
8228 unlockerr = got_ref_unlock(base_branch_ref);
8229 if (unlockerr && err == NULL)
8230 err = unlockerr;
8231 got_ref_close(base_branch_ref);
8233 got_fileindex_free(fileindex);
8234 free(fileindex_path);
8235 free(tree_id);
8236 if (commit)
8237 got_object_commit_close(commit);
8239 unlockerr = lock_worktree(worktree, LOCK_SH);
8240 if (unlockerr && err == NULL)
8241 err = unlockerr;
8242 return err;
8245 const struct got_error *
8246 got_worktree_integrate_abort(struct got_worktree *worktree,
8247 struct got_fileindex *fileindex, struct got_repository *repo,
8248 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8250 const struct got_error *err = NULL, *unlockerr = NULL;
8252 got_fileindex_free(fileindex);
8254 err = lock_worktree(worktree, LOCK_SH);
8256 unlockerr = got_ref_unlock(branch_ref);
8257 if (unlockerr && err == NULL)
8258 err = unlockerr;
8259 got_ref_close(branch_ref);
8261 unlockerr = got_ref_unlock(base_branch_ref);
8262 if (unlockerr && err == NULL)
8263 err = unlockerr;
8264 got_ref_close(base_branch_ref);
8266 return err;
8269 const struct got_error *
8270 got_worktree_merge_postpone(struct got_worktree *worktree,
8271 struct got_fileindex *fileindex)
8273 const struct got_error *err, *sync_err;
8274 char *fileindex_path = NULL;
8276 err = get_fileindex_path(&fileindex_path, worktree);
8277 if (err)
8278 goto done;
8280 sync_err = sync_fileindex(fileindex, fileindex_path);
8282 err = lock_worktree(worktree, LOCK_SH);
8283 if (sync_err && err == NULL)
8284 err = sync_err;
8285 done:
8286 got_fileindex_free(fileindex);
8287 free(fileindex_path);
8288 return err;
8291 static const struct got_error *
8292 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8294 const struct got_error *err;
8295 char *branch_refname = NULL, *commit_refname = NULL;
8297 err = get_merge_branch_ref_name(&branch_refname, worktree);
8298 if (err)
8299 goto done;
8300 err = delete_ref(branch_refname, repo);
8301 if (err)
8302 goto done;
8304 err = get_merge_commit_ref_name(&commit_refname, worktree);
8305 if (err)
8306 goto done;
8307 err = delete_ref(commit_refname, repo);
8308 if (err)
8309 goto done;
8311 done:
8312 free(branch_refname);
8313 free(commit_refname);
8314 return err;
8317 struct merge_commit_msg_arg {
8318 struct got_worktree *worktree;
8319 const char *branch_name;
8322 static const struct got_error *
8323 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8324 const char *diff_path, char **logmsg, void *arg)
8326 struct merge_commit_msg_arg *a = arg;
8328 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8329 got_worktree_get_head_ref_name(a->worktree)) == -1)
8330 return got_error_from_errno("asprintf");
8332 return NULL;
8336 const struct got_error *
8337 got_worktree_merge_branch(struct got_worktree *worktree,
8338 struct got_fileindex *fileindex,
8339 struct got_object_id *yca_commit_id,
8340 struct got_object_id *branch_tip,
8341 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8342 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8344 const struct got_error *err;
8345 char *fileindex_path = NULL;
8347 err = get_fileindex_path(&fileindex_path, worktree);
8348 if (err)
8349 goto done;
8351 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8352 worktree);
8353 if (err)
8354 goto done;
8356 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8357 branch_tip, repo, progress_cb, progress_arg,
8358 cancel_cb, cancel_arg);
8359 done:
8360 free(fileindex_path);
8361 return err;
8364 const struct got_error *
8365 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8366 struct got_worktree *worktree, struct got_fileindex *fileindex,
8367 const char *author, const char *committer, int allow_bad_symlinks,
8368 struct got_object_id *branch_tip, const char *branch_name,
8369 int allow_conflict, struct got_repository *repo,
8370 got_worktree_status_cb status_cb, void *status_arg)
8373 const struct got_error *err = NULL, *sync_err;
8374 struct got_pathlist_head commitable_paths;
8375 struct collect_commitables_arg cc_arg;
8376 struct got_pathlist_entry *pe;
8377 struct got_reference *head_ref = NULL;
8378 struct got_object_id *head_commit_id = NULL;
8379 int have_staged_files = 0;
8380 struct merge_commit_msg_arg mcm_arg;
8381 char *fileindex_path = NULL;
8383 memset(&cc_arg, 0, sizeof(cc_arg));
8384 *new_commit_id = NULL;
8386 TAILQ_INIT(&commitable_paths);
8388 err = get_fileindex_path(&fileindex_path, worktree);
8389 if (err)
8390 goto done;
8392 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8393 if (err)
8394 goto done;
8396 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8397 if (err)
8398 goto done;
8400 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8401 &have_staged_files);
8402 if (err && err->code != GOT_ERR_CANCELLED)
8403 goto done;
8404 if (have_staged_files) {
8405 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8406 goto done;
8409 cc_arg.commitable_paths = &commitable_paths;
8410 cc_arg.worktree = worktree;
8411 cc_arg.fileindex = fileindex;
8412 cc_arg.repo = repo;
8413 cc_arg.have_staged_files = have_staged_files;
8414 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8415 cc_arg.commit_conflicts = allow_conflict;
8416 err = worktree_status(worktree, "", fileindex, repo,
8417 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8418 if (err)
8419 goto done;
8421 mcm_arg.worktree = worktree;
8422 mcm_arg.branch_name = branch_name;
8423 err = commit_worktree(new_commit_id, &commitable_paths,
8424 head_commit_id, branch_tip, worktree, author, committer, NULL,
8425 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8426 if (err)
8427 goto done;
8429 err = update_fileindex_after_commit(worktree, &commitable_paths,
8430 *new_commit_id, fileindex, have_staged_files);
8431 sync_err = sync_fileindex(fileindex, fileindex_path);
8432 if (sync_err && err == NULL)
8433 err = sync_err;
8434 done:
8435 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8436 struct got_commitable *ct = pe->data;
8438 free_commitable(ct);
8440 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8441 free(fileindex_path);
8442 return err;
8445 const struct got_error *
8446 got_worktree_merge_complete(struct got_worktree *worktree,
8447 struct got_fileindex *fileindex, struct got_repository *repo)
8449 const struct got_error *err, *unlockerr, *sync_err;
8450 char *fileindex_path = NULL;
8452 err = delete_merge_refs(worktree, repo);
8453 if (err)
8454 goto done;
8456 err = get_fileindex_path(&fileindex_path, worktree);
8457 if (err)
8458 goto done;
8459 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8460 sync_err = sync_fileindex(fileindex, fileindex_path);
8461 if (sync_err && err == NULL)
8462 err = sync_err;
8463 done:
8464 got_fileindex_free(fileindex);
8465 free(fileindex_path);
8466 unlockerr = lock_worktree(worktree, LOCK_SH);
8467 if (unlockerr && err == NULL)
8468 err = unlockerr;
8469 return err;
8472 const struct got_error *
8473 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8474 struct got_repository *repo)
8476 const struct got_error *err;
8477 char *branch_refname = NULL;
8478 struct got_reference *branch_ref = NULL;
8480 *in_progress = 0;
8482 err = get_merge_branch_ref_name(&branch_refname, worktree);
8483 if (err)
8484 return err;
8485 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8486 free(branch_refname);
8487 if (err) {
8488 if (err->code != GOT_ERR_NOT_REF)
8489 return err;
8490 } else
8491 *in_progress = 1;
8493 return NULL;
8496 const struct got_error *got_worktree_merge_prepare(
8497 struct got_fileindex **fileindex, struct got_worktree *worktree,
8498 struct got_repository *repo)
8500 const struct got_error *err = NULL;
8501 char *fileindex_path = NULL;
8502 struct got_reference *wt_branch = NULL;
8503 struct got_object_id *wt_branch_tip = NULL;
8504 struct check_rebase_ok_arg ok_arg;
8506 *fileindex = NULL;
8508 err = lock_worktree(worktree, LOCK_EX);
8509 if (err)
8510 return err;
8512 err = open_fileindex(fileindex, &fileindex_path, worktree);
8513 if (err)
8514 goto done;
8516 /* Preconditions are the same as for rebase. */
8517 ok_arg.worktree = worktree;
8518 ok_arg.repo = repo;
8519 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8520 &ok_arg);
8521 if (err)
8522 goto done;
8524 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8525 0);
8526 if (err)
8527 goto done;
8529 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8530 if (err)
8531 goto done;
8533 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8534 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8535 goto done;
8538 done:
8539 free(fileindex_path);
8540 if (wt_branch)
8541 got_ref_close(wt_branch);
8542 free(wt_branch_tip);
8543 if (err) {
8544 if (*fileindex) {
8545 got_fileindex_free(*fileindex);
8546 *fileindex = NULL;
8548 lock_worktree(worktree, LOCK_SH);
8550 return err;
8553 const struct got_error *got_worktree_merge_write_refs(
8554 struct got_worktree *worktree, struct got_reference *branch,
8555 struct got_repository *repo)
8557 const struct got_error *err = NULL;
8558 char *branch_refname = NULL, *commit_refname = NULL;
8559 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8560 struct got_object_id *branch_tip = NULL;
8562 err = get_merge_branch_ref_name(&branch_refname, worktree);
8563 if (err)
8564 return err;
8566 err = get_merge_commit_ref_name(&commit_refname, worktree);
8567 if (err)
8568 return err;
8570 err = got_ref_resolve(&branch_tip, repo, branch);
8571 if (err)
8572 goto done;
8574 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8575 if (err)
8576 goto done;
8577 err = got_ref_write(branch_ref, repo);
8578 if (err)
8579 goto done;
8581 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8582 if (err)
8583 goto done;
8584 err = got_ref_write(commit_ref, repo);
8585 if (err)
8586 goto done;
8588 done:
8589 free(branch_refname);
8590 free(commit_refname);
8591 if (branch_ref)
8592 got_ref_close(branch_ref);
8593 if (commit_ref)
8594 got_ref_close(commit_ref);
8595 free(branch_tip);
8596 return err;
8599 const struct got_error *
8600 got_worktree_merge_continue(char **branch_name,
8601 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8602 struct got_worktree *worktree, struct got_repository *repo)
8604 const struct got_error *err;
8605 char *commit_refname = NULL, *branch_refname = NULL;
8606 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8607 char *fileindex_path = NULL;
8608 int have_staged_files = 0;
8610 *branch_name = NULL;
8611 *branch_tip = NULL;
8612 *fileindex = NULL;
8614 err = lock_worktree(worktree, LOCK_EX);
8615 if (err)
8616 return err;
8618 err = open_fileindex(fileindex, &fileindex_path, worktree);
8619 if (err)
8620 goto done;
8622 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8623 &have_staged_files);
8624 if (err && err->code != GOT_ERR_CANCELLED)
8625 goto done;
8626 if (have_staged_files) {
8627 err = got_error(GOT_ERR_STAGED_PATHS);
8628 goto done;
8631 err = get_merge_branch_ref_name(&branch_refname, worktree);
8632 if (err)
8633 goto done;
8635 err = get_merge_commit_ref_name(&commit_refname, worktree);
8636 if (err)
8637 goto done;
8639 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8640 if (err)
8641 goto done;
8643 if (!got_ref_is_symbolic(branch_ref)) {
8644 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8645 "%s is not a symbolic reference",
8646 got_ref_get_name(branch_ref));
8647 goto done;
8649 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8650 if (*branch_name == NULL) {
8651 err = got_error_from_errno("strdup");
8652 goto done;
8655 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8656 if (err)
8657 goto done;
8659 err = got_ref_resolve(branch_tip, repo, commit_ref);
8660 if (err)
8661 goto done;
8662 done:
8663 free(commit_refname);
8664 free(branch_refname);
8665 free(fileindex_path);
8666 if (commit_ref)
8667 got_ref_close(commit_ref);
8668 if (branch_ref)
8669 got_ref_close(branch_ref);
8670 if (err) {
8671 if (*branch_name) {
8672 free(*branch_name);
8673 *branch_name = NULL;
8675 free(*branch_tip);
8676 *branch_tip = NULL;
8677 if (*fileindex) {
8678 got_fileindex_free(*fileindex);
8679 *fileindex = NULL;
8681 lock_worktree(worktree, LOCK_SH);
8683 return err;
8686 const struct got_error *
8687 got_worktree_merge_abort(struct got_worktree *worktree,
8688 struct got_fileindex *fileindex, struct got_repository *repo,
8689 got_worktree_checkout_cb progress_cb, void *progress_arg)
8691 const struct got_error *err, *unlockerr, *sync_err;
8692 struct got_commit_object *commit = NULL;
8693 char *fileindex_path = NULL;
8694 struct revert_file_args rfa;
8695 char *commit_ref_name = NULL;
8696 struct got_reference *commit_ref = NULL;
8697 struct got_object_id *merged_commit_id = NULL;
8698 struct got_object_id *tree_id = NULL;
8699 struct got_pathlist_head added_paths;
8701 TAILQ_INIT(&added_paths);
8703 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8704 if (err)
8705 goto done;
8707 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8708 if (err)
8709 goto done;
8711 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8712 if (err)
8713 goto done;
8716 * Determine which files in added status can be safely removed
8717 * from disk while reverting changes in the work tree.
8718 * We want to avoid deleting unrelated files which were added by
8719 * the user for conflict resolution purposes.
8721 err = get_paths_added_between_commits(&added_paths,
8722 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8723 got_worktree_get_path_prefix(worktree), repo);
8724 if (err)
8725 goto done;
8728 err = got_object_open_as_commit(&commit, repo,
8729 worktree->base_commit_id);
8730 if (err)
8731 goto done;
8733 err = got_object_id_by_path(&tree_id, repo, commit,
8734 worktree->path_prefix);
8735 if (err)
8736 goto done;
8738 err = delete_merge_refs(worktree, repo);
8739 if (err)
8740 goto done;
8742 err = get_fileindex_path(&fileindex_path, worktree);
8743 if (err)
8744 goto done;
8746 rfa.worktree = worktree;
8747 rfa.fileindex = fileindex;
8748 rfa.progress_cb = progress_cb;
8749 rfa.progress_arg = progress_arg;
8750 rfa.patch_cb = NULL;
8751 rfa.patch_arg = NULL;
8752 rfa.repo = repo;
8753 rfa.unlink_added_files = 1;
8754 rfa.added_files_to_unlink = &added_paths;
8755 err = worktree_status(worktree, "", fileindex, repo,
8756 revert_file, &rfa, NULL, NULL, 1, 0);
8757 if (err)
8758 goto sync;
8760 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8761 repo, progress_cb, progress_arg, NULL, NULL);
8762 sync:
8763 sync_err = sync_fileindex(fileindex, fileindex_path);
8764 if (sync_err && err == NULL)
8765 err = sync_err;
8766 done:
8767 free(tree_id);
8768 free(merged_commit_id);
8769 if (commit)
8770 got_object_commit_close(commit);
8771 if (fileindex)
8772 got_fileindex_free(fileindex);
8773 free(fileindex_path);
8774 if (commit_ref)
8775 got_ref_close(commit_ref);
8776 free(commit_ref_name);
8778 unlockerr = lock_worktree(worktree, LOCK_SH);
8779 if (unlockerr && err == NULL)
8780 err = unlockerr;
8781 return err;
8784 struct check_stage_ok_arg {
8785 struct got_object_id *head_commit_id;
8786 struct got_worktree *worktree;
8787 struct got_fileindex *fileindex;
8788 struct got_repository *repo;
8789 int have_changes;
8792 static const struct got_error *
8793 check_stage_ok(void *arg, unsigned char status,
8794 unsigned char staged_status, const char *relpath,
8795 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8796 struct got_object_id *commit_id, int dirfd, const char *de_name)
8798 struct check_stage_ok_arg *a = arg;
8799 const struct got_error *err = NULL;
8800 struct got_fileindex_entry *ie;
8801 struct got_object_id base_commit_id;
8802 struct got_object_id *base_commit_idp = NULL;
8803 char *in_repo_path = NULL, *p;
8805 if (status == GOT_STATUS_UNVERSIONED ||
8806 status == GOT_STATUS_NO_CHANGE)
8807 return NULL;
8808 if (status == GOT_STATUS_NONEXISTENT)
8809 return got_error_set_errno(ENOENT, relpath);
8811 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8812 if (ie == NULL)
8813 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8815 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8816 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8817 relpath) == -1)
8818 return got_error_from_errno("asprintf");
8820 if (got_fileindex_entry_has_commit(ie)) {
8821 base_commit_idp = got_fileindex_entry_get_commit_id(
8822 &base_commit_id, ie);
8825 if (status == GOT_STATUS_CONFLICT) {
8826 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8827 goto done;
8828 } else if (status != GOT_STATUS_ADD &&
8829 status != GOT_STATUS_MODIFY &&
8830 status != GOT_STATUS_DELETE) {
8831 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8832 goto done;
8835 a->have_changes = 1;
8837 p = in_repo_path;
8838 while (p[0] == '/')
8839 p++;
8840 err = check_out_of_date(p, status, staged_status,
8841 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8842 GOT_ERR_STAGE_OUT_OF_DATE);
8843 done:
8844 free(in_repo_path);
8845 return err;
8848 struct stage_path_arg {
8849 struct got_worktree *worktree;
8850 struct got_fileindex *fileindex;
8851 struct got_repository *repo;
8852 got_worktree_status_cb status_cb;
8853 void *status_arg;
8854 got_worktree_patch_cb patch_cb;
8855 void *patch_arg;
8856 int staged_something;
8857 int allow_bad_symlinks;
8860 static const struct got_error *
8861 stage_path(void *arg, unsigned char status,
8862 unsigned char staged_status, const char *relpath,
8863 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8864 struct got_object_id *commit_id, int dirfd, const char *de_name)
8866 struct stage_path_arg *a = arg;
8867 const struct got_error *err = NULL;
8868 struct got_fileindex_entry *ie;
8869 char *ondisk_path = NULL, *path_content = NULL;
8870 uint32_t stage;
8871 struct got_object_id *new_staged_blob_id = NULL;
8872 struct stat sb;
8874 if (status == GOT_STATUS_UNVERSIONED)
8875 return NULL;
8877 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8878 if (ie == NULL)
8879 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8881 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8882 relpath)== -1)
8883 return got_error_from_errno("asprintf");
8885 switch (status) {
8886 case GOT_STATUS_ADD:
8887 case GOT_STATUS_MODIFY:
8888 /* XXX could sb.st_mode be passed in by our caller? */
8889 if (lstat(ondisk_path, &sb) == -1) {
8890 err = got_error_from_errno2("lstat", ondisk_path);
8891 break;
8893 if (a->patch_cb) {
8894 if (status == GOT_STATUS_ADD) {
8895 int choice = GOT_PATCH_CHOICE_NONE;
8896 err = (*a->patch_cb)(&choice, a->patch_arg,
8897 status, ie->path, NULL, 1, 1);
8898 if (err)
8899 break;
8900 if (choice != GOT_PATCH_CHOICE_YES)
8901 break;
8902 } else {
8903 err = create_patched_content(&path_content, 0,
8904 staged_blob_id ? staged_blob_id : blob_id,
8905 ondisk_path, dirfd, de_name, ie->path,
8906 a->repo, a->patch_cb, a->patch_arg);
8907 if (err || path_content == NULL)
8908 break;
8911 err = got_object_blob_create(&new_staged_blob_id,
8912 path_content ? path_content : ondisk_path, a->repo);
8913 if (err)
8914 break;
8915 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8916 SHA1_DIGEST_LENGTH);
8917 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8918 stage = GOT_FILEIDX_STAGE_ADD;
8919 else
8920 stage = GOT_FILEIDX_STAGE_MODIFY;
8921 got_fileindex_entry_stage_set(ie, stage);
8922 if (S_ISLNK(sb.st_mode)) {
8923 int is_bad_symlink = 0;
8924 if (!a->allow_bad_symlinks) {
8925 char target_path[PATH_MAX];
8926 ssize_t target_len;
8927 target_len = readlink(ondisk_path, target_path,
8928 sizeof(target_path));
8929 if (target_len == -1) {
8930 err = got_error_from_errno2("readlink",
8931 ondisk_path);
8932 break;
8934 err = is_bad_symlink_target(&is_bad_symlink,
8935 target_path, target_len, ondisk_path,
8936 a->worktree->root_path);
8937 if (err)
8938 break;
8939 if (is_bad_symlink) {
8940 err = got_error_path(ondisk_path,
8941 GOT_ERR_BAD_SYMLINK);
8942 break;
8945 if (is_bad_symlink)
8946 got_fileindex_entry_staged_filetype_set(ie,
8947 GOT_FILEIDX_MODE_BAD_SYMLINK);
8948 else
8949 got_fileindex_entry_staged_filetype_set(ie,
8950 GOT_FILEIDX_MODE_SYMLINK);
8951 } else {
8952 got_fileindex_entry_staged_filetype_set(ie,
8953 GOT_FILEIDX_MODE_REGULAR_FILE);
8955 a->staged_something = 1;
8956 if (a->status_cb == NULL)
8957 break;
8958 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8959 get_staged_status(ie), relpath, blob_id,
8960 new_staged_blob_id, NULL, dirfd, de_name);
8961 if (err)
8962 break;
8964 * When staging the reverse of the staged diff,
8965 * implicitly unstage the file.
8967 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8968 sizeof(ie->blob_sha1)) == 0) {
8969 got_fileindex_entry_stage_set(ie,
8970 GOT_FILEIDX_STAGE_NONE);
8972 break;
8973 case GOT_STATUS_DELETE:
8974 if (staged_status == GOT_STATUS_DELETE)
8975 break;
8976 if (a->patch_cb) {
8977 int choice = GOT_PATCH_CHOICE_NONE;
8978 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8979 ie->path, NULL, 1, 1);
8980 if (err)
8981 break;
8982 if (choice == GOT_PATCH_CHOICE_NO)
8983 break;
8984 if (choice != GOT_PATCH_CHOICE_YES) {
8985 err = got_error(GOT_ERR_PATCH_CHOICE);
8986 break;
8989 stage = GOT_FILEIDX_STAGE_DELETE;
8990 got_fileindex_entry_stage_set(ie, stage);
8991 a->staged_something = 1;
8992 if (a->status_cb == NULL)
8993 break;
8994 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8995 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8996 de_name);
8997 break;
8998 case GOT_STATUS_NO_CHANGE:
8999 break;
9000 case GOT_STATUS_CONFLICT:
9001 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9002 break;
9003 case GOT_STATUS_NONEXISTENT:
9004 err = got_error_set_errno(ENOENT, relpath);
9005 break;
9006 default:
9007 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9008 break;
9011 if (path_content && unlink(path_content) == -1 && err == NULL)
9012 err = got_error_from_errno2("unlink", path_content);
9013 free(path_content);
9014 free(ondisk_path);
9015 free(new_staged_blob_id);
9016 return err;
9019 const struct got_error *
9020 got_worktree_stage(struct got_worktree *worktree,
9021 struct got_pathlist_head *paths,
9022 got_worktree_status_cb status_cb, void *status_arg,
9023 got_worktree_patch_cb patch_cb, void *patch_arg,
9024 int allow_bad_symlinks, struct got_repository *repo)
9026 const struct got_error *err = NULL, *sync_err, *unlockerr;
9027 struct got_pathlist_entry *pe;
9028 struct got_fileindex *fileindex = NULL;
9029 char *fileindex_path = NULL;
9030 struct got_reference *head_ref = NULL;
9031 struct got_object_id *head_commit_id = NULL;
9032 struct check_stage_ok_arg oka;
9033 struct stage_path_arg spa;
9035 err = lock_worktree(worktree, LOCK_EX);
9036 if (err)
9037 return err;
9039 err = got_ref_open(&head_ref, repo,
9040 got_worktree_get_head_ref_name(worktree), 0);
9041 if (err)
9042 goto done;
9043 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9044 if (err)
9045 goto done;
9046 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9047 if (err)
9048 goto done;
9050 /* Check pre-conditions before staging anything. */
9051 oka.head_commit_id = head_commit_id;
9052 oka.worktree = worktree;
9053 oka.fileindex = fileindex;
9054 oka.repo = repo;
9055 oka.have_changes = 0;
9056 TAILQ_FOREACH(pe, paths, entry) {
9057 err = worktree_status(worktree, pe->path, fileindex, repo,
9058 check_stage_ok, &oka, NULL, NULL, 1, 0);
9059 if (err)
9060 goto done;
9062 if (!oka.have_changes) {
9063 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9064 goto done;
9067 spa.worktree = worktree;
9068 spa.fileindex = fileindex;
9069 spa.repo = repo;
9070 spa.patch_cb = patch_cb;
9071 spa.patch_arg = patch_arg;
9072 spa.status_cb = status_cb;
9073 spa.status_arg = status_arg;
9074 spa.staged_something = 0;
9075 spa.allow_bad_symlinks = allow_bad_symlinks;
9076 TAILQ_FOREACH(pe, paths, entry) {
9077 err = worktree_status(worktree, pe->path, fileindex, repo,
9078 stage_path, &spa, NULL, NULL, 1, 0);
9079 if (err)
9080 goto done;
9082 if (!spa.staged_something) {
9083 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9084 goto done;
9087 sync_err = sync_fileindex(fileindex, fileindex_path);
9088 if (sync_err && err == NULL)
9089 err = sync_err;
9090 done:
9091 if (head_ref)
9092 got_ref_close(head_ref);
9093 free(head_commit_id);
9094 free(fileindex_path);
9095 if (fileindex)
9096 got_fileindex_free(fileindex);
9097 unlockerr = lock_worktree(worktree, LOCK_SH);
9098 if (unlockerr && err == NULL)
9099 err = unlockerr;
9100 return err;
9103 struct unstage_path_arg {
9104 struct got_worktree *worktree;
9105 struct got_fileindex *fileindex;
9106 struct got_repository *repo;
9107 got_worktree_checkout_cb progress_cb;
9108 void *progress_arg;
9109 got_worktree_patch_cb patch_cb;
9110 void *patch_arg;
9113 static const struct got_error *
9114 create_unstaged_content(char **path_unstaged_content,
9115 char **path_new_staged_content, struct got_object_id *blob_id,
9116 struct got_object_id *staged_blob_id, const char *relpath,
9117 struct got_repository *repo,
9118 got_worktree_patch_cb patch_cb, void *patch_arg)
9120 const struct got_error *err, *free_err;
9121 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9122 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9123 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9124 struct got_diffreg_result *diffreg_result = NULL;
9125 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9126 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9127 int fd1 = -1, fd2 = -1;
9129 *path_unstaged_content = NULL;
9130 *path_new_staged_content = NULL;
9132 err = got_object_id_str(&label1, blob_id);
9133 if (err)
9134 return err;
9136 fd1 = got_opentempfd();
9137 if (fd1 == -1) {
9138 err = got_error_from_errno("got_opentempfd");
9139 goto done;
9141 fd2 = got_opentempfd();
9142 if (fd2 == -1) {
9143 err = got_error_from_errno("got_opentempfd");
9144 goto done;
9147 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9148 if (err)
9149 goto done;
9151 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9152 if (err)
9153 goto done;
9155 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9156 if (err)
9157 goto done;
9159 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9160 fd2);
9161 if (err)
9162 goto done;
9164 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9165 if (err)
9166 goto done;
9168 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9169 if (err)
9170 goto done;
9172 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9173 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9174 if (err)
9175 goto done;
9177 err = got_opentemp_named(path_unstaged_content, &outfile,
9178 "got-unstaged-content", "");
9179 if (err)
9180 goto done;
9181 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9182 "got-new-staged-content", "");
9183 if (err)
9184 goto done;
9186 if (fseek(f1, 0L, SEEK_SET) == -1) {
9187 err = got_ferror(f1, GOT_ERR_IO);
9188 goto done;
9190 if (fseek(f2, 0L, SEEK_SET) == -1) {
9191 err = got_ferror(f2, GOT_ERR_IO);
9192 goto done;
9194 /* Count the number of actual changes in the diff result. */
9195 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9196 struct diff_chunk_context cc = {};
9197 diff_chunk_context_load_change(&cc, &nchunks_used,
9198 diffreg_result->result, n, 0);
9199 nchanges++;
9201 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9202 int choice;
9203 err = apply_or_reject_change(&choice, &nchunks_used,
9204 diffreg_result->result, n, relpath, f1, f2,
9205 &line_cur1, &line_cur2,
9206 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9207 if (err)
9208 goto done;
9209 if (choice == GOT_PATCH_CHOICE_YES)
9210 have_content = 1;
9211 else
9212 have_rejected_content = 1;
9213 if (choice == GOT_PATCH_CHOICE_QUIT)
9214 break;
9216 if (have_content || have_rejected_content)
9217 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9218 outfile, rejectfile);
9219 done:
9220 free(label1);
9221 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9222 err = got_error_from_errno("close");
9223 if (blob)
9224 got_object_blob_close(blob);
9225 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9226 err = got_error_from_errno("close");
9227 if (staged_blob)
9228 got_object_blob_close(staged_blob);
9229 free_err = got_diffreg_result_free(diffreg_result);
9230 if (free_err && err == NULL)
9231 err = free_err;
9232 if (f1 && fclose(f1) == EOF && err == NULL)
9233 err = got_error_from_errno2("fclose", path1);
9234 if (f2 && fclose(f2) == EOF && err == NULL)
9235 err = got_error_from_errno2("fclose", path2);
9236 if (outfile && fclose(outfile) == EOF && err == NULL)
9237 err = got_error_from_errno2("fclose", *path_unstaged_content);
9238 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9239 err = got_error_from_errno2("fclose", *path_new_staged_content);
9240 if (path1 && unlink(path1) == -1 && err == NULL)
9241 err = got_error_from_errno2("unlink", path1);
9242 if (path2 && unlink(path2) == -1 && err == NULL)
9243 err = got_error_from_errno2("unlink", path2);
9244 if (err || !have_content) {
9245 if (*path_unstaged_content &&
9246 unlink(*path_unstaged_content) == -1 && err == NULL)
9247 err = got_error_from_errno2("unlink",
9248 *path_unstaged_content);
9249 free(*path_unstaged_content);
9250 *path_unstaged_content = NULL;
9252 if (err || !have_content || !have_rejected_content) {
9253 if (*path_new_staged_content &&
9254 unlink(*path_new_staged_content) == -1 && err == NULL)
9255 err = got_error_from_errno2("unlink",
9256 *path_new_staged_content);
9257 free(*path_new_staged_content);
9258 *path_new_staged_content = NULL;
9260 free(path1);
9261 free(path2);
9262 return err;
9265 static const struct got_error *
9266 unstage_hunks(struct got_object_id *staged_blob_id,
9267 struct got_blob_object *blob_base,
9268 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9269 const char *ondisk_path, const char *label_orig,
9270 struct got_worktree *worktree, struct got_repository *repo,
9271 got_worktree_patch_cb patch_cb, void *patch_arg,
9272 got_worktree_checkout_cb progress_cb, void *progress_arg)
9274 const struct got_error *err = NULL;
9275 char *path_unstaged_content = NULL;
9276 char *path_new_staged_content = NULL;
9277 char *parent = NULL, *base_path = NULL;
9278 char *blob_base_path = NULL;
9279 struct got_object_id *new_staged_blob_id = NULL;
9280 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9281 struct stat sb;
9283 err = create_unstaged_content(&path_unstaged_content,
9284 &path_new_staged_content, blob_id, staged_blob_id,
9285 ie->path, repo, patch_cb, patch_arg);
9286 if (err)
9287 return err;
9289 if (path_unstaged_content == NULL)
9290 return NULL;
9292 if (path_new_staged_content) {
9293 err = got_object_blob_create(&new_staged_blob_id,
9294 path_new_staged_content, repo);
9295 if (err)
9296 goto done;
9299 f = fopen(path_unstaged_content, "re");
9300 if (f == NULL) {
9301 err = got_error_from_errno2("fopen",
9302 path_unstaged_content);
9303 goto done;
9305 if (fstat(fileno(f), &sb) == -1) {
9306 err = got_error_from_errno2("fstat", path_unstaged_content);
9307 goto done;
9309 if (got_fileindex_entry_staged_filetype_get(ie) ==
9310 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9311 char link_target[PATH_MAX];
9312 size_t r;
9313 r = fread(link_target, 1, sizeof(link_target), f);
9314 if (r == 0 && ferror(f)) {
9315 err = got_error_from_errno("fread");
9316 goto done;
9318 if (r >= sizeof(link_target)) { /* should not happen */
9319 err = got_error(GOT_ERR_NO_SPACE);
9320 goto done;
9322 link_target[r] = '\0';
9323 err = merge_symlink(worktree, blob_base,
9324 ondisk_path, ie->path, label_orig, link_target,
9325 worktree->base_commit_id, repo, progress_cb,
9326 progress_arg);
9327 } else {
9328 int local_changes_subsumed;
9330 err = got_path_dirname(&parent, ondisk_path);
9331 if (err)
9332 return err;
9334 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9335 parent) == -1) {
9336 err = got_error_from_errno("asprintf");
9337 base_path = NULL;
9338 goto done;
9341 err = got_opentemp_named(&blob_base_path, &f_base,
9342 base_path, "");
9343 if (err)
9344 goto done;
9345 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9346 blob_base);
9347 if (err)
9348 goto done;
9351 * In order the run a 3-way merge with a symlink we copy the symlink's
9352 * target path into a temporary file and use that file with diff3.
9354 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9355 err = dump_symlink_target_path_to_file(&f_deriv2,
9356 ondisk_path);
9357 if (err)
9358 goto done;
9359 } else {
9360 int fd;
9361 fd = open(ondisk_path,
9362 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9363 if (fd == -1) {
9364 err = got_error_from_errno2("open", ondisk_path);
9365 goto done;
9367 f_deriv2 = fdopen(fd, "r");
9368 if (f_deriv2 == NULL) {
9369 err = got_error_from_errno2("fdopen", ondisk_path);
9370 close(fd);
9371 goto done;
9375 err = merge_file(&local_changes_subsumed, worktree,
9376 f_base, f, f_deriv2, ondisk_path, ie->path,
9377 got_fileindex_perms_to_st(ie),
9378 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9379 repo, progress_cb, progress_arg);
9381 if (err)
9382 goto done;
9384 if (new_staged_blob_id) {
9385 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9386 SHA1_DIGEST_LENGTH);
9387 } else {
9388 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9389 got_fileindex_entry_staged_filetype_set(ie, 0);
9391 done:
9392 free(new_staged_blob_id);
9393 if (path_unstaged_content &&
9394 unlink(path_unstaged_content) == -1 && err == NULL)
9395 err = got_error_from_errno2("unlink", path_unstaged_content);
9396 if (path_new_staged_content &&
9397 unlink(path_new_staged_content) == -1 && err == NULL)
9398 err = got_error_from_errno2("unlink", path_new_staged_content);
9399 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9400 err = got_error_from_errno2("unlink", blob_base_path);
9401 if (f_base && fclose(f_base) == EOF && err == NULL)
9402 err = got_error_from_errno2("fclose", path_unstaged_content);
9403 if (f && fclose(f) == EOF && err == NULL)
9404 err = got_error_from_errno2("fclose", path_unstaged_content);
9405 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9406 err = got_error_from_errno2("fclose", ondisk_path);
9407 free(path_unstaged_content);
9408 free(path_new_staged_content);
9409 free(blob_base_path);
9410 free(parent);
9411 free(base_path);
9412 return err;
9415 static const struct got_error *
9416 unstage_path(void *arg, unsigned char status,
9417 unsigned char staged_status, const char *relpath,
9418 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9419 struct got_object_id *commit_id, int dirfd, const char *de_name)
9421 const struct got_error *err = NULL;
9422 struct unstage_path_arg *a = arg;
9423 struct got_fileindex_entry *ie;
9424 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9425 char *ondisk_path = NULL;
9426 char *id_str = NULL, *label_orig = NULL;
9427 int local_changes_subsumed;
9428 struct stat sb;
9429 int fd1 = -1, fd2 = -1;
9431 if (staged_status != GOT_STATUS_ADD &&
9432 staged_status != GOT_STATUS_MODIFY &&
9433 staged_status != GOT_STATUS_DELETE)
9434 return NULL;
9436 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9437 if (ie == NULL)
9438 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9440 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9441 == -1)
9442 return got_error_from_errno("asprintf");
9444 err = got_object_id_str(&id_str,
9445 commit_id ? commit_id : a->worktree->base_commit_id);
9446 if (err)
9447 goto done;
9448 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9449 id_str) == -1) {
9450 err = got_error_from_errno("asprintf");
9451 goto done;
9454 fd1 = got_opentempfd();
9455 if (fd1 == -1) {
9456 err = got_error_from_errno("got_opentempfd");
9457 goto done;
9459 fd2 = got_opentempfd();
9460 if (fd2 == -1) {
9461 err = got_error_from_errno("got_opentempfd");
9462 goto done;
9465 switch (staged_status) {
9466 case GOT_STATUS_MODIFY:
9467 err = got_object_open_as_blob(&blob_base, a->repo,
9468 blob_id, 8192, fd1);
9469 if (err)
9470 break;
9471 /* fall through */
9472 case GOT_STATUS_ADD:
9473 if (a->patch_cb) {
9474 if (staged_status == GOT_STATUS_ADD) {
9475 int choice = GOT_PATCH_CHOICE_NONE;
9476 err = (*a->patch_cb)(&choice, a->patch_arg,
9477 staged_status, ie->path, NULL, 1, 1);
9478 if (err)
9479 break;
9480 if (choice != GOT_PATCH_CHOICE_YES)
9481 break;
9482 } else {
9483 err = unstage_hunks(staged_blob_id,
9484 blob_base, blob_id, ie, ondisk_path,
9485 label_orig, a->worktree, a->repo,
9486 a->patch_cb, a->patch_arg,
9487 a->progress_cb, a->progress_arg);
9488 break; /* Done with this file. */
9491 err = got_object_open_as_blob(&blob_staged, a->repo,
9492 staged_blob_id, 8192, fd2);
9493 if (err)
9494 break;
9495 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9496 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9497 case GOT_FILEIDX_MODE_REGULAR_FILE:
9498 err = merge_blob(&local_changes_subsumed, a->worktree,
9499 blob_base, ondisk_path, relpath,
9500 got_fileindex_perms_to_st(ie), label_orig,
9501 blob_staged, commit_id ? commit_id :
9502 a->worktree->base_commit_id, a->repo,
9503 a->progress_cb, a->progress_arg);
9504 break;
9505 case GOT_FILEIDX_MODE_SYMLINK:
9506 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9507 char *staged_target;
9508 err = got_object_blob_read_to_str(
9509 &staged_target, blob_staged);
9510 if (err)
9511 goto done;
9512 err = merge_symlink(a->worktree, blob_base,
9513 ondisk_path, relpath, label_orig,
9514 staged_target, commit_id ? commit_id :
9515 a->worktree->base_commit_id,
9516 a->repo, a->progress_cb, a->progress_arg);
9517 free(staged_target);
9518 } else {
9519 err = merge_blob(&local_changes_subsumed,
9520 a->worktree, blob_base, ondisk_path,
9521 relpath, got_fileindex_perms_to_st(ie),
9522 label_orig, blob_staged,
9523 commit_id ? commit_id :
9524 a->worktree->base_commit_id, a->repo,
9525 a->progress_cb, a->progress_arg);
9527 break;
9528 default:
9529 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9530 break;
9532 if (err == NULL) {
9533 got_fileindex_entry_stage_set(ie,
9534 GOT_FILEIDX_STAGE_NONE);
9535 got_fileindex_entry_staged_filetype_set(ie, 0);
9537 break;
9538 case GOT_STATUS_DELETE:
9539 if (a->patch_cb) {
9540 int choice = GOT_PATCH_CHOICE_NONE;
9541 err = (*a->patch_cb)(&choice, a->patch_arg,
9542 staged_status, ie->path, NULL, 1, 1);
9543 if (err)
9544 break;
9545 if (choice == GOT_PATCH_CHOICE_NO)
9546 break;
9547 if (choice != GOT_PATCH_CHOICE_YES) {
9548 err = got_error(GOT_ERR_PATCH_CHOICE);
9549 break;
9552 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9553 got_fileindex_entry_staged_filetype_set(ie, 0);
9554 err = get_file_status(&status, &sb, ie, ondisk_path,
9555 dirfd, de_name, a->repo);
9556 if (err)
9557 break;
9558 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9559 break;
9561 done:
9562 free(ondisk_path);
9563 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9564 err = got_error_from_errno("close");
9565 if (blob_base)
9566 got_object_blob_close(blob_base);
9567 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9568 err = got_error_from_errno("close");
9569 if (blob_staged)
9570 got_object_blob_close(blob_staged);
9571 free(id_str);
9572 free(label_orig);
9573 return err;
9576 const struct got_error *
9577 got_worktree_unstage(struct got_worktree *worktree,
9578 struct got_pathlist_head *paths,
9579 got_worktree_checkout_cb progress_cb, void *progress_arg,
9580 got_worktree_patch_cb patch_cb, void *patch_arg,
9581 struct got_repository *repo)
9583 const struct got_error *err = NULL, *sync_err, *unlockerr;
9584 struct got_pathlist_entry *pe;
9585 struct got_fileindex *fileindex = NULL;
9586 char *fileindex_path = NULL;
9587 struct unstage_path_arg upa;
9589 err = lock_worktree(worktree, LOCK_EX);
9590 if (err)
9591 return err;
9593 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9594 if (err)
9595 goto done;
9597 upa.worktree = worktree;
9598 upa.fileindex = fileindex;
9599 upa.repo = repo;
9600 upa.progress_cb = progress_cb;
9601 upa.progress_arg = progress_arg;
9602 upa.patch_cb = patch_cb;
9603 upa.patch_arg = patch_arg;
9604 TAILQ_FOREACH(pe, paths, entry) {
9605 err = worktree_status(worktree, pe->path, fileindex, repo,
9606 unstage_path, &upa, NULL, NULL, 1, 0);
9607 if (err)
9608 goto done;
9611 sync_err = sync_fileindex(fileindex, fileindex_path);
9612 if (sync_err && err == NULL)
9613 err = sync_err;
9614 done:
9615 free(fileindex_path);
9616 if (fileindex)
9617 got_fileindex_free(fileindex);
9618 unlockerr = lock_worktree(worktree, LOCK_SH);
9619 if (unlockerr && err == NULL)
9620 err = unlockerr;
9621 return err;
9624 struct report_file_info_arg {
9625 struct got_worktree *worktree;
9626 got_worktree_path_info_cb info_cb;
9627 void *info_arg;
9628 struct got_pathlist_head *paths;
9629 got_cancel_cb cancel_cb;
9630 void *cancel_arg;
9633 static const struct got_error *
9634 report_file_info(void *arg, struct got_fileindex_entry *ie)
9636 struct report_file_info_arg *a = arg;
9637 struct got_pathlist_entry *pe;
9638 struct got_object_id blob_id, staged_blob_id, commit_id;
9639 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9640 struct got_object_id *commit_idp = NULL;
9641 int stage;
9643 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9644 return got_error(GOT_ERR_CANCELLED);
9646 TAILQ_FOREACH(pe, a->paths, entry) {
9647 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9648 got_path_is_child(ie->path, pe->path, pe->path_len))
9649 break;
9651 if (pe == NULL) /* not found */
9652 return NULL;
9654 if (got_fileindex_entry_has_blob(ie))
9655 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9656 stage = got_fileindex_entry_stage_get(ie);
9657 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9658 stage == GOT_FILEIDX_STAGE_ADD) {
9659 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9660 &staged_blob_id, ie);
9663 if (got_fileindex_entry_has_commit(ie))
9664 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9666 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9667 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9670 const struct got_error *
9671 got_worktree_path_info(struct got_worktree *worktree,
9672 struct got_pathlist_head *paths,
9673 got_worktree_path_info_cb info_cb, void *info_arg,
9674 got_cancel_cb cancel_cb, void *cancel_arg)
9677 const struct got_error *err = NULL, *unlockerr;
9678 struct got_fileindex *fileindex = NULL;
9679 char *fileindex_path = NULL;
9680 struct report_file_info_arg arg;
9682 err = lock_worktree(worktree, LOCK_SH);
9683 if (err)
9684 return err;
9686 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9687 if (err)
9688 goto done;
9690 arg.worktree = worktree;
9691 arg.info_cb = info_cb;
9692 arg.info_arg = info_arg;
9693 arg.paths = paths;
9694 arg.cancel_cb = cancel_cb;
9695 arg.cancel_arg = cancel_arg;
9696 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9697 &arg);
9698 done:
9699 free(fileindex_path);
9700 if (fileindex)
9701 got_fileindex_free(fileindex);
9702 unlockerr = lock_worktree(worktree, LOCK_UN);
9703 if (unlockerr && err == NULL)
9704 err = unlockerr;
9705 return err;
9708 static const struct got_error *
9709 patch_check_path(const char *p, char **path, unsigned char *status,
9710 unsigned char *staged_status, struct got_fileindex *fileindex,
9711 struct got_worktree *worktree, struct got_repository *repo)
9713 const struct got_error *err;
9714 struct got_fileindex_entry *ie;
9715 struct stat sb;
9716 char *ondisk_path = NULL;
9718 err = got_worktree_resolve_path(path, worktree, p);
9719 if (err)
9720 return err;
9722 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9723 *path[0] ? "/" : "", *path) == -1)
9724 return got_error_from_errno("asprintf");
9726 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9727 if (ie) {
9728 *staged_status = get_staged_status(ie);
9729 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9730 repo);
9731 if (err)
9732 goto done;
9733 } else {
9734 *staged_status = GOT_STATUS_NO_CHANGE;
9735 *status = GOT_STATUS_UNVERSIONED;
9736 if (lstat(ondisk_path, &sb) == -1) {
9737 if (errno != ENOENT) {
9738 err = got_error_from_errno2("lstat",
9739 ondisk_path);
9740 goto done;
9742 *status = GOT_STATUS_NONEXISTENT;
9746 done:
9747 free(ondisk_path);
9748 return err;
9751 static const struct got_error *
9752 patch_can_rm(const char *path, unsigned char status,
9753 unsigned char staged_status)
9755 if (status == GOT_STATUS_NONEXISTENT)
9756 return got_error_set_errno(ENOENT, path);
9757 if (status != GOT_STATUS_NO_CHANGE &&
9758 status != GOT_STATUS_ADD &&
9759 status != GOT_STATUS_MODIFY &&
9760 status != GOT_STATUS_MODE_CHANGE)
9761 return got_error_path(path, GOT_ERR_FILE_STATUS);
9762 if (staged_status == GOT_STATUS_DELETE)
9763 return got_error_path(path, GOT_ERR_FILE_STATUS);
9764 return NULL;
9767 static const struct got_error *
9768 patch_can_add(const char *path, unsigned char status)
9770 if (status != GOT_STATUS_NONEXISTENT)
9771 return got_error_path(path, GOT_ERR_FILE_STATUS);
9772 return NULL;
9775 static const struct got_error *
9776 patch_can_edit(const char *path, unsigned char status,
9777 unsigned char staged_status)
9779 if (status == GOT_STATUS_NONEXISTENT)
9780 return got_error_set_errno(ENOENT, path);
9781 if (status != GOT_STATUS_NO_CHANGE &&
9782 status != GOT_STATUS_ADD &&
9783 status != GOT_STATUS_MODIFY)
9784 return got_error_path(path, GOT_ERR_FILE_STATUS);
9785 if (staged_status == GOT_STATUS_DELETE)
9786 return got_error_path(path, GOT_ERR_FILE_STATUS);
9787 return NULL;
9790 const struct got_error *
9791 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9792 char **fileindex_path, struct got_worktree *worktree)
9794 return open_fileindex(fileindex, fileindex_path, worktree);
9797 const struct got_error *
9798 got_worktree_patch_check_path(const char *old, const char *new,
9799 char **oldpath, char **newpath, struct got_worktree *worktree,
9800 struct got_repository *repo, struct got_fileindex *fileindex)
9802 const struct got_error *err = NULL;
9803 int file_renamed = 0;
9804 unsigned char status_old, staged_status_old;
9805 unsigned char status_new, staged_status_new;
9807 *oldpath = NULL;
9808 *newpath = NULL;
9810 err = patch_check_path(old != NULL ? old : new, oldpath,
9811 &status_old, &staged_status_old, fileindex, worktree, repo);
9812 if (err)
9813 goto done;
9815 err = patch_check_path(new != NULL ? new : old, newpath,
9816 &status_new, &staged_status_new, fileindex, worktree, repo);
9817 if (err)
9818 goto done;
9820 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9821 file_renamed = 1;
9823 if (old != NULL && new == NULL)
9824 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9825 else if (file_renamed) {
9826 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9827 if (err == NULL)
9828 err = patch_can_add(*newpath, status_new);
9829 } else if (old == NULL)
9830 err = patch_can_add(*newpath, status_new);
9831 else
9832 err = patch_can_edit(*newpath, status_new, staged_status_new);
9834 done:
9835 if (err) {
9836 free(*oldpath);
9837 *oldpath = NULL;
9838 free(*newpath);
9839 *newpath = NULL;
9841 return err;
9844 const struct got_error *
9845 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9846 struct got_worktree *worktree, struct got_fileindex *fileindex,
9847 got_worktree_checkout_cb progress_cb, void *progress_arg)
9849 struct schedule_addition_args saa;
9851 memset(&saa, 0, sizeof(saa));
9852 saa.worktree = worktree;
9853 saa.fileindex = fileindex;
9854 saa.progress_cb = progress_cb;
9855 saa.progress_arg = progress_arg;
9856 saa.repo = repo;
9858 return worktree_status(worktree, path, fileindex, repo,
9859 schedule_addition, &saa, NULL, NULL, 1, 0);
9862 const struct got_error *
9863 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9864 struct got_worktree *worktree, struct got_fileindex *fileindex,
9865 got_worktree_delete_cb progress_cb, void *progress_arg)
9867 const struct got_error *err;
9868 struct schedule_deletion_args sda;
9869 char *ondisk_status_path;
9871 memset(&sda, 0, sizeof(sda));
9872 sda.worktree = worktree;
9873 sda.fileindex = fileindex;
9874 sda.progress_cb = progress_cb;
9875 sda.progress_arg = progress_arg;
9876 sda.repo = repo;
9877 sda.delete_local_mods = 0;
9878 sda.keep_on_disk = 0;
9879 sda.ignore_missing_paths = 0;
9880 sda.status_codes = NULL;
9881 if (asprintf(&ondisk_status_path, "%s/%s",
9882 got_worktree_get_root_path(worktree), path) == -1)
9883 return got_error_from_errno("asprintf");
9884 sda.status_path = ondisk_status_path;
9885 sda.status_path_len = strlen(ondisk_status_path);
9887 err = worktree_status(worktree, path, fileindex, repo,
9888 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9889 free(ondisk_status_path);
9890 return err;
9893 const struct got_error *
9894 got_worktree_patch_complete(struct got_fileindex *fileindex,
9895 const char *fileindex_path)
9897 const struct got_error *err = NULL;
9899 err = sync_fileindex(fileindex, fileindex_path);
9900 got_fileindex_free(fileindex);
9902 return err;