Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_diff.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t
69 apply_umask(mode_t mode)
70 {
71 mode_t um;
73 um = umask(000);
74 umask(um);
75 return mode & ~um;
76 }
78 static const struct got_error *
79 create_meta_file(const char *path_got, const char *name, const char *content)
80 {
81 const struct got_error *err = NULL;
82 char *path;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1)
85 return got_error_from_errno("asprintf");
87 err = got_path_create_file(path, content);
88 free(path);
89 return err;
90 }
92 static const struct got_error *
93 update_meta_file(const char *path_got, const char *name, const char *content)
94 {
95 const struct got_error *err = NULL;
96 FILE *tmpfile = NULL;
97 char *tmppath = NULL;
98 char *path = NULL;
100 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 err = got_error_from_errno("asprintf");
102 path = NULL;
103 goto done;
106 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
107 if (err)
108 goto done;
110 if (content) {
111 int len = fprintf(tmpfile, "%s\n", content);
112 if (len != strlen(content) + 1) {
113 err = got_error_from_errno2("fprintf", tmppath);
114 goto done;
118 if (rename(tmppath, path) != 0) {
119 err = got_error_from_errno3("rename", tmppath, path);
120 unlink(tmppath);
121 goto done;
124 done:
125 if (fclose(tmpfile) == EOF && err == NULL)
126 err = got_error_from_errno2("fclose", tmppath);
127 free(tmppath);
128 return err;
131 static const struct got_error *
132 write_head_ref(const char *path_got, struct got_reference *head_ref)
134 const struct got_error *err = NULL;
135 char *refstr = NULL;
137 if (got_ref_is_symbolic(head_ref)) {
138 refstr = got_ref_to_str(head_ref);
139 if (refstr == NULL)
140 return got_error_from_errno("got_ref_to_str");
141 } else {
142 refstr = strdup(got_ref_get_name(head_ref));
143 if (refstr == NULL)
144 return got_error_from_errno("strdup");
146 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
147 free(refstr);
148 return err;
151 const struct got_error *
152 got_worktree_init(const char *path, struct got_reference *head_ref,
153 const char *prefix, const char *meta_dir, struct got_repository *repo)
155 const struct got_error *err = NULL;
156 struct got_object_id *commit_id = NULL;
157 uuid_t uuid;
158 uint32_t uuid_status;
159 int obj_type;
160 char *path_got = NULL;
161 char *formatstr = NULL;
162 char *absprefix = NULL;
163 char *basestr = NULL;
164 char *uuidstr = NULL;
166 if (strcmp(path, got_repo_get_path(repo)) == 0) {
167 err = got_error(GOT_ERR_WORKTREE_REPO);
168 goto done;
171 err = got_ref_resolve(&commit_id, repo, head_ref);
172 if (err)
173 return err;
174 err = got_object_get_type(&obj_type, repo, commit_id);
175 if (err)
176 return err;
177 if (obj_type != GOT_OBJ_TYPE_COMMIT)
178 return got_error(GOT_ERR_OBJ_TYPE);
180 if (!got_path_is_absolute(prefix)) {
181 if (asprintf(&absprefix, "/%s", prefix) == -1)
182 return got_error_from_errno("asprintf");
185 /* Create top-level directory (may already exist). */
186 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 err = got_error_from_errno2("mkdir", path);
188 goto done;
191 /* Create .got/.cvg directory (may already exist). */
192 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
193 err = got_error_from_errno("asprintf");
194 goto done;
196 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
197 err = got_error_from_errno2("mkdir", path_got);
198 goto done;
201 /* Create an empty lock file. */
202 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 if (err)
204 goto done;
206 /* Create an empty file index. */
207 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 if (err)
209 goto done;
211 /* Write the HEAD reference. */
212 err = write_head_ref(path_got, head_ref);
213 if (err)
214 goto done;
216 /* Record our base commit. */
217 err = got_object_id_str(&basestr, commit_id);
218 if (err)
219 goto done;
220 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 if (err)
222 goto done;
224 /* Store path to repository. */
225 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
226 got_repo_get_path(repo));
227 if (err)
228 goto done;
230 /* Store in-repository path prefix. */
231 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
232 absprefix ? absprefix : prefix);
233 if (err)
234 goto done;
236 /* Generate UUID. */
237 uuid_create(&uuid, &uuid_status);
238 if (uuid_status != uuid_s_ok) {
239 err = got_error_uuid(uuid_status, "uuid_create");
240 goto done;
242 uuid_to_string(&uuid, &uuidstr, &uuid_status);
243 if (uuid_status != uuid_s_ok) {
244 err = got_error_uuid(uuid_status, "uuid_to_string");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 if (err)
249 goto done;
251 /* Stamp work tree with format file. */
252 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
253 err = got_error_from_errno("asprintf");
254 goto done;
256 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
257 if (err)
258 goto done;
260 done:
261 free(commit_id);
262 free(path_got);
263 free(formatstr);
264 free(absprefix);
265 free(basestr);
266 free(uuidstr);
267 return err;
270 const struct got_error *
271 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
272 const char *path_prefix)
274 char *absprefix = NULL;
276 if (!got_path_is_absolute(path_prefix)) {
277 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
278 return got_error_from_errno("asprintf");
280 *match = (strcmp(absprefix ? absprefix : path_prefix,
281 worktree->path_prefix) == 0);
282 free(absprefix);
283 return NULL;
286 const char *
287 got_worktree_get_head_ref_name(struct got_worktree *worktree)
289 return worktree->head_ref_name;
292 const struct got_error *
293 got_worktree_set_head_ref(struct got_worktree *worktree,
294 struct got_reference *head_ref)
296 const struct got_error *err = NULL;
297 char *path_got = NULL, *head_ref_name = NULL;
299 if (asprintf(&path_got, "%s/%s", worktree->root_path,
300 worktree->meta_dir) == -1) {
301 err = got_error_from_errno("asprintf");
302 path_got = NULL;
303 goto done;
306 head_ref_name = strdup(got_ref_get_name(head_ref));
307 if (head_ref_name == NULL) {
308 err = got_error_from_errno("strdup");
309 goto done;
312 err = write_head_ref(path_got, head_ref);
313 if (err)
314 goto done;
316 free(worktree->head_ref_name);
317 worktree->head_ref_name = head_ref_name;
318 done:
319 free(path_got);
320 if (err)
321 free(head_ref_name);
322 return err;
325 struct got_object_id *
326 got_worktree_get_base_commit_id(struct got_worktree *worktree)
328 return worktree->base_commit_id;
331 const struct got_error *
332 got_worktree_set_base_commit_id(struct got_worktree *worktree,
333 struct got_repository *repo, struct got_object_id *commit_id)
335 const struct got_error *err;
336 struct got_object *obj = NULL;
337 char *id_str = NULL;
338 char *path_got = NULL;
340 if (asprintf(&path_got, "%s/%s", worktree->root_path,
341 worktree->meta_dir) == -1) {
342 err = got_error_from_errno("asprintf");
343 path_got = NULL;
344 goto done;
347 err = got_object_open(&obj, repo, commit_id);
348 if (err)
349 return err;
351 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 err = got_error(GOT_ERR_OBJ_TYPE);
353 goto done;
356 /* Record our base commit. */
357 err = got_object_id_str(&id_str, commit_id);
358 if (err)
359 goto done;
360 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 if (err)
362 goto done;
364 free(worktree->base_commit_id);
365 worktree->base_commit_id = got_object_id_dup(commit_id);
366 if (worktree->base_commit_id == NULL) {
367 err = got_error_from_errno("got_object_id_dup");
368 goto done;
370 done:
371 if (obj)
372 got_object_close(obj);
373 free(id_str);
374 free(path_got);
375 return err;
378 const struct got_gotconfig *
379 got_worktree_get_gotconfig(struct got_worktree *worktree)
381 return worktree->gotconfig;
384 static const struct got_error *
385 lock_worktree(struct got_worktree *worktree, int operation)
387 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 : got_error_from_errno2("flock",
390 got_worktree_get_root_path(worktree)));
391 return NULL;
394 static const struct got_error *
395 add_dir_on_disk(struct got_worktree *worktree, const char *path)
397 const struct got_error *err = NULL;
398 char *abspath;
400 /* We only accept worktree-relative paths here. */
401 if (got_path_is_absolute(path)) {
402 return got_error_fmt(GOT_ERR_BAD_PATH,
403 "%s does not accept absolute paths: %s",
404 __func__, path);
407 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 return got_error_from_errno("asprintf");
410 err = got_path_mkdir(abspath);
411 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
412 struct stat sb;
413 err = NULL;
414 if (lstat(abspath, &sb) == -1) {
415 err = got_error_from_errno2("lstat", abspath);
416 } else if (!S_ISDIR(sb.st_mode)) {
417 /* TODO directory is obstructed; do something */
418 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
421 free(abspath);
422 return err;
425 static const struct got_error *
426 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
428 const struct got_error *err = NULL;
429 uint8_t fbuf1[8192];
430 uint8_t fbuf2[8192];
431 size_t flen1 = 0, flen2 = 0;
433 *same = 1;
435 for (;;) {
436 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 if (flen1 == 0 && ferror(f1)) {
438 err = got_error_from_errno("fread");
439 break;
441 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 if (flen2 == 0 && ferror(f2)) {
443 err = got_error_from_errno("fread");
444 break;
446 if (flen1 == 0) {
447 if (flen2 != 0)
448 *same = 0;
449 break;
450 } else if (flen2 == 0) {
451 if (flen1 != 0)
452 *same = 0;
453 break;
454 } else if (flen1 == flen2) {
455 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
456 *same = 0;
457 break;
459 } else {
460 *same = 0;
461 break;
465 return err;
468 static const struct got_error *
469 check_files_equal(int *same, FILE *f1, FILE *f2)
471 struct stat sb;
472 size_t size1, size2;
474 *same = 1;
476 if (fstat(fileno(f1), &sb) != 0)
477 return got_error_from_errno("fstat");
478 size1 = sb.st_size;
480 if (fstat(fileno(f2), &sb) != 0)
481 return got_error_from_errno("fstat");
482 size2 = sb.st_size;
484 if (size1 != size2) {
485 *same = 0;
486 return NULL;
489 if (fseek(f1, 0L, SEEK_SET) == -1)
490 return got_ferror(f1, GOT_ERR_IO);
491 if (fseek(f2, 0L, SEEK_SET) == -1)
492 return got_ferror(f2, GOT_ERR_IO);
494 return check_file_contents_equal(same, f1, f2);
497 static const struct got_error *
498 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
500 uint8_t fbuf[65536];
501 size_t flen;
502 ssize_t outlen;
504 *outsize = 0;
506 if (fseek(f, 0L, SEEK_SET) == -1)
507 return got_ferror(f, GOT_ERR_IO);
509 for (;;) {
510 flen = fread(fbuf, 1, sizeof(fbuf), f);
511 if (flen == 0) {
512 if (ferror(f))
513 return got_error_from_errno("fread");
514 if (feof(f))
515 break;
517 outlen = write(outfd, fbuf, flen);
518 if (outlen == -1)
519 return got_error_from_errno("write");
520 if (outlen != flen)
521 return got_error(GOT_ERR_IO);
522 *outsize += outlen;
525 return NULL;
528 static const struct got_error *
529 merge_binary_file(int *overlapcnt, int merged_fd,
530 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 const char *ondisk_path)
534 const struct got_error *err = NULL;
535 int same_content, changed_deriv, changed_deriv2;
536 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 char *base_path_orig = NULL, *base_path_deriv = NULL;
540 char *base_path_deriv2 = NULL;
542 *overlapcnt = 0;
544 err = check_files_equal(&same_content, f_deriv, f_deriv2);
545 if (err)
546 return err;
548 if (same_content)
549 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
551 err = check_files_equal(&same_content, f_deriv, f_orig);
552 if (err)
553 return err;
554 changed_deriv = !same_content;
555 err = check_files_equal(&same_content, f_deriv2, f_orig);
556 if (err)
557 return err;
558 changed_deriv2 = !same_content;
560 if (changed_deriv && changed_deriv2) {
561 *overlapcnt = 1;
562 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 err = got_error_from_errno("asprintf");
564 goto done;
566 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 err = got_error_from_errno("asprintf");
568 goto done;
570 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 err = got_error_from_errno("asprintf");
572 goto done;
574 err = got_opentemp_named_fd(&path_orig, &fd_orig,
575 base_path_orig, "");
576 if (err)
577 goto done;
578 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 base_path_deriv, "");
580 if (err)
581 goto done;
582 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 base_path_deriv2, "");
584 if (err)
585 goto done;
586 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
587 if (err)
588 goto done;
589 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
590 if (err)
591 goto done;
592 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
593 if (err)
594 goto done;
595 if (dprintf(merged_fd, "Binary files differ and cannot be "
596 "merged automatically:\n") < 0) {
597 err = got_error_from_errno("dprintf");
598 goto done;
600 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 label_deriv ? " " : "",
603 label_deriv ? label_deriv : "",
604 path_deriv) < 0) {
605 err = got_error_from_errno("dprintf");
606 goto done;
608 if (size_orig > 0) {
609 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 GOT_DIFF_CONFLICT_MARKER_ORIG,
611 label_orig ? " " : "",
612 label_orig ? label_orig : "",
613 path_orig) < 0) {
614 err = got_error_from_errno("dprintf");
615 goto done;
618 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 GOT_DIFF_CONFLICT_MARKER_SEP,
620 path_deriv2,
621 GOT_DIFF_CONFLICT_MARKER_END,
622 label_deriv2 ? " " : "",
623 label_deriv2 ? label_deriv2 : "") < 0) {
624 err = got_error_from_errno("dprintf");
625 goto done;
627 } else if (changed_deriv)
628 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 else if (changed_deriv2)
630 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
631 done:
632 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
633 err == NULL)
634 err = got_error_from_errno2("unlink", path_orig);
635 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 err = got_error_from_errno2("close", path_orig);
637 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 err = got_error_from_errno2("close", path_deriv);
639 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 err = got_error_from_errno2("close", path_deriv2);
641 free(path_orig);
642 free(path_deriv);
643 free(path_deriv2);
644 free(base_path_orig);
645 free(base_path_deriv);
646 free(base_path_deriv2);
647 return err;
650 /*
651 * Perform a 3-way merge where the file f_orig acts as the common
652 * ancestor, the file f_deriv acts as the first derived version,
653 * and the file f_deriv2 acts as the second derived version.
654 * The merge result will be written to a new file at ondisk_path; any
655 * existing file at this path will be replaced.
656 */
657 static const struct got_error *
658 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 const char *path, uint16_t st_mode,
661 const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 got_worktree_checkout_cb progress_cb, void *progress_arg)
665 const struct got_error *err = NULL;
666 int merged_fd = -1;
667 FILE *f_merged = NULL;
668 char *merged_path = NULL, *base_path = NULL;
669 int overlapcnt = 0;
670 char *parent = NULL;
672 *local_changes_subsumed = 0;
674 err = got_path_dirname(&parent, ondisk_path);
675 if (err)
676 return err;
678 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 err = got_error_from_errno("asprintf");
680 goto done;
683 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 if (err)
685 goto done;
687 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
689 if (err) {
690 if (err->code != GOT_ERR_FILE_BINARY)
691 goto done;
692 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
694 ondisk_path);
695 if (err)
696 goto done;
699 err = (*progress_cb)(progress_arg,
700 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 if (err)
702 goto done;
704 if (fsync(merged_fd) != 0) {
705 err = got_error_from_errno("fsync");
706 goto done;
709 f_merged = fdopen(merged_fd, "r");
710 if (f_merged == NULL) {
711 err = got_error_from_errno("fdopen");
712 goto done;
714 merged_fd = -1;
716 /* Check if a clean merge has subsumed all local changes. */
717 if (overlapcnt == 0) {
718 err = check_files_equal(local_changes_subsumed, f_deriv,
719 f_merged);
720 if (err)
721 goto done;
724 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 err = got_error_from_errno2("fchmod", merged_path);
726 goto done;
729 if (rename(merged_path, ondisk_path) != 0) {
730 err = got_error_from_errno3("rename", merged_path,
731 ondisk_path);
732 goto done;
734 done:
735 if (err) {
736 if (merged_path)
737 unlink(merged_path);
739 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 err = got_error_from_errno("close");
741 if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 err = got_error_from_errno("fclose");
743 free(merged_path);
744 free(base_path);
745 free(parent);
746 return err;
749 static const struct got_error *
750 update_symlink(const char *ondisk_path, const char *target_path,
751 size_t target_len)
753 /* This is not atomic but matches what 'ln -sf' does. */
754 if (unlink(ondisk_path) == -1)
755 return got_error_from_errno2("unlink", ondisk_path);
756 if (symlink(target_path, ondisk_path) == -1)
757 return got_error_from_errno3("symlink", target_path,
758 ondisk_path);
759 return NULL;
762 /*
763 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 * in the work tree with a file that contains conflict markers and the
765 * conflicting target paths of the original version, a "derived version"
766 * of a symlink from an incoming change, and a local version of the symlink.
768 * The original versions's target path can be NULL if it is not available,
769 * such as if both derived versions added a new symlink at the same path.
771 * The incoming derived symlink target is NULL in case the incoming change
772 * has deleted this symlink.
773 */
774 static const struct got_error *
775 install_symlink_conflict(const char *deriv_target,
776 struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 const char *label_orig, const char *local_target, const char *ondisk_path)
779 const struct got_error *err;
780 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
781 FILE *f = NULL;
783 err = got_object_id_str(&id_str, deriv_base_commit_id);
784 if (err)
785 return got_error_from_errno("asprintf");
787 if (asprintf(&label_deriv, "%s: commit %s",
788 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 err = got_error_from_errno("asprintf");
790 goto done;
793 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
794 if (err)
795 goto done;
797 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 err = got_error_from_errno2("fchmod", path);
799 goto done;
802 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 deriv_target ? deriv_target : "(symlink was deleted)",
805 orig_target ? label_orig : "",
806 orig_target ? "\n" : "",
807 orig_target ? orig_target : "",
808 orig_target ? "\n" : "",
809 GOT_DIFF_CONFLICT_MARKER_SEP,
810 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 err = got_error_from_errno2("fprintf", path);
812 goto done;
815 if (unlink(ondisk_path) == -1) {
816 err = got_error_from_errno2("unlink", ondisk_path);
817 goto done;
819 if (rename(path, ondisk_path) == -1) {
820 err = got_error_from_errno3("rename", path, ondisk_path);
821 goto done;
823 done:
824 if (f != NULL && fclose(f) == EOF && err == NULL)
825 err = got_error_from_errno2("fclose", path);
826 free(path);
827 free(id_str);
828 free(label_deriv);
829 return err;
832 /* forward declaration */
833 static const struct got_error *
834 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 const char *, const char *, uint16_t, const char *,
836 struct got_blob_object *, struct got_object_id *,
837 struct got_repository *, got_worktree_checkout_cb, void *);
839 /*
840 * Merge a symlink into the work tree, where blob_orig acts as the common
841 * ancestor, deriv_target is the link target of the first derived version,
842 * and the symlink on disk acts as the second derived version.
843 * Assume that contents of both blobs represent symlinks.
844 */
845 static const struct got_error *
846 merge_symlink(struct got_worktree *worktree,
847 struct got_blob_object *blob_orig, const char *ondisk_path,
848 const char *path, const char *label_orig, const char *deriv_target,
849 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 got_worktree_checkout_cb progress_cb, void *progress_arg)
852 const struct got_error *err = NULL;
853 char *ancestor_target = NULL;
854 struct stat sb;
855 ssize_t ondisk_len, deriv_len;
856 char ondisk_target[PATH_MAX];
857 int have_local_change = 0;
858 int have_incoming_change = 0;
860 if (lstat(ondisk_path, &sb) == -1)
861 return got_error_from_errno2("lstat", ondisk_path);
863 ondisk_len = readlink(ondisk_path, ondisk_target,
864 sizeof(ondisk_target));
865 if (ondisk_len == -1) {
866 err = got_error_from_errno2("readlink",
867 ondisk_path);
868 goto done;
870 ondisk_target[ondisk_len] = '\0';
872 if (blob_orig) {
873 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
874 if (err)
875 goto done;
878 if (ancestor_target == NULL ||
879 (ondisk_len != strlen(ancestor_target) ||
880 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 have_local_change = 1;
883 deriv_len = strlen(deriv_target);
884 if (ancestor_target == NULL ||
885 (deriv_len != strlen(ancestor_target) ||
886 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 have_incoming_change = 1;
889 if (!have_local_change && !have_incoming_change) {
890 if (ancestor_target) {
891 /* Both sides made the same change. */
892 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
893 path);
894 } else if (deriv_len == ondisk_len &&
895 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 /* Both sides added the same symlink. */
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
898 path);
899 } else {
900 /* Both sides added symlinks which don't match. */
901 err = install_symlink_conflict(deriv_target,
902 deriv_base_commit_id, ancestor_target,
903 label_orig, ondisk_target, ondisk_path);
904 if (err)
905 goto done;
906 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
907 path);
909 } else if (!have_local_change && have_incoming_change) {
910 /* Apply the incoming change. */
911 err = update_symlink(ondisk_path, deriv_target,
912 strlen(deriv_target));
913 if (err)
914 goto done;
915 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 } else if (have_local_change && have_incoming_change) {
917 if (deriv_len == ondisk_len &&
918 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 /* Both sides made the same change. */
920 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
921 path);
922 } else {
923 err = install_symlink_conflict(deriv_target,
924 deriv_base_commit_id, ancestor_target, label_orig,
925 ondisk_target, ondisk_path);
926 if (err)
927 goto done;
928 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
929 path);
933 done:
934 free(ancestor_target);
935 return err;
938 static const struct got_error *
939 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
941 const struct got_error *err = NULL;
942 char target_path[PATH_MAX];
943 ssize_t target_len;
944 size_t n;
945 FILE *f;
947 *outfile = NULL;
949 f = got_opentemp();
950 if (f == NULL)
951 return got_error_from_errno("got_opentemp");
952 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 if (target_len == -1) {
954 err = got_error_from_errno2("readlink", ondisk_path);
955 goto done;
957 n = fwrite(target_path, 1, target_len, f);
958 if (n != target_len) {
959 err = got_ferror(f, GOT_ERR_IO);
960 goto done;
962 if (fflush(f) == EOF) {
963 err = got_error_from_errno("fflush");
964 goto done;
966 if (fseek(f, 0L, SEEK_SET) == -1) {
967 err = got_ferror(f, GOT_ERR_IO);
968 goto done;
970 done:
971 if (err)
972 fclose(f);
973 else
974 *outfile = f;
975 return err;
978 /*
979 * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 * blob_deriv acts as the first derived version, and the file on disk
981 * acts as the second derived version.
982 */
983 static const struct got_error *
984 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 struct got_blob_object *blob_orig, const char *ondisk_path,
986 const char *path, uint16_t st_mode, const char *label_orig,
987 struct got_blob_object *blob_deriv,
988 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 got_worktree_checkout_cb progress_cb, void *progress_arg)
991 const struct got_error *err = NULL;
992 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 char *blob_orig_path = NULL;
994 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 char *label_deriv = NULL, *parent = NULL;
997 *local_changes_subsumed = 0;
999 err = got_path_dirname(&parent, ondisk_path);
1000 if (err)
1001 return err;
1003 if (blob_orig) {
1004 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1005 parent) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 base_path = NULL;
1008 goto done;
1011 err = got_opentemp_named(&blob_orig_path, &f_orig,
1012 base_path, "");
1013 if (err)
1014 goto done;
1015 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1016 blob_orig);
1017 if (err)
1018 goto done;
1019 free(base_path);
1020 } else {
1022 * No common ancestor exists. This is an "add vs add" conflict
1023 * and we simply use an empty ancestor file to make both files
1024 * appear in the merged result in their entirety.
1026 f_orig = got_opentemp();
1027 if (f_orig == NULL) {
1028 err = got_error_from_errno("got_opentemp");
1029 goto done;
1033 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 base_path = NULL;
1036 goto done;
1039 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1040 if (err)
1041 goto done;
1042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1043 blob_deriv);
1044 if (err)
1045 goto done;
1047 err = got_object_id_str(&id_str, deriv_base_commit_id);
1048 if (err)
1049 goto done;
1050 if (asprintf(&label_deriv, "%s: commit %s",
1051 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 err = got_error_from_errno("asprintf");
1053 goto done;
1057 * In order the run a 3-way merge with a symlink we copy the symlink's
1058 * target path into a temporary file and use that file with diff3.
1060 if (S_ISLNK(st_mode)) {
1061 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1062 if (err)
1063 goto done;
1064 } else {
1065 int fd;
1066 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1067 if (fd == -1) {
1068 err = got_error_from_errno2("open", ondisk_path);
1069 goto done;
1071 f_deriv2 = fdopen(fd, "r");
1072 if (f_deriv2 == NULL) {
1073 err = got_error_from_errno2("fdopen", ondisk_path);
1074 close(fd);
1075 goto done;
1079 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1082 done:
1083 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 err = got_error_from_errno("fclose");
1085 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 err = got_error_from_errno("fclose");
1087 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 err = got_error_from_errno("fclose");
1089 free(base_path);
1090 if (blob_orig_path) {
1091 unlink(blob_orig_path);
1092 free(blob_orig_path);
1094 if (blob_deriv_path) {
1095 unlink(blob_deriv_path);
1096 free(blob_deriv_path);
1098 free(id_str);
1099 free(label_deriv);
1100 free(parent);
1101 return err;
1104 static const struct got_error *
1105 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 int wt_fd, const char *path, struct got_object_id *blob_id,
1108 int update_timestamps)
1110 const struct got_error *err = NULL;
1111 struct got_fileindex_entry *new_ie;
1113 *new_iep = NULL;
1115 err = got_fileindex_entry_alloc(&new_ie, path);
1116 if (err)
1117 return err;
1119 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1120 blob_id->sha1, base_commit_id->sha1, update_timestamps);
1121 if (err)
1122 goto done;
1124 err = got_fileindex_entry_add(fileindex, new_ie);
1125 done:
1126 if (err)
1127 got_fileindex_entry_free(new_ie);
1128 else
1129 *new_iep = new_ie;
1130 return err;
1133 static mode_t
1134 get_ondisk_perms(int executable, mode_t st_mode)
1136 mode_t xbits = S_IXUSR;
1138 if (executable) {
1139 /* Map read bits to execute bits. */
1140 if (st_mode & S_IRGRP)
1141 xbits |= S_IXGRP;
1142 if (st_mode & S_IROTH)
1143 xbits |= S_IXOTH;
1144 return st_mode | xbits;
1147 return st_mode;
1150 /* forward declaration */
1151 static const struct got_error *
1152 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1153 const char *path, mode_t te_mode, mode_t st_mode,
1154 struct got_blob_object *blob, int restoring_missing_file,
1155 int reverting_versioned_file, int installing_bad_symlink,
1156 int path_is_unversioned, int *update_timestamps,
1157 struct got_repository *repo,
1158 got_worktree_checkout_cb progress_cb, void *progress_arg);
1161 * This function assumes that the provided symlink target points at a
1162 * safe location in the work tree!
1164 static const struct got_error *
1165 replace_existing_symlink(int *did_something, const char *ondisk_path,
1166 const char *target_path, size_t target_len)
1168 const struct got_error *err = NULL;
1169 ssize_t elen;
1170 char etarget[PATH_MAX];
1171 int fd;
1173 *did_something = 0;
1176 * "Bad" symlinks (those pointing outside the work tree or into the
1177 * .got directory) are installed in the work tree as a regular file
1178 * which contains the bad symlink target path.
1179 * The new symlink target has already been checked for safety by our
1180 * caller. If we can successfully open a regular file then we simply
1181 * replace this file with a symlink below.
1183 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1184 if (fd == -1) {
1185 if (!got_err_open_nofollow_on_symlink())
1186 return got_error_from_errno2("open", ondisk_path);
1188 /* We are updating an existing on-disk symlink. */
1189 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1190 if (elen == -1)
1191 return got_error_from_errno2("readlink", ondisk_path);
1193 if (elen == target_len &&
1194 memcmp(etarget, target_path, target_len) == 0)
1195 return NULL; /* nothing to do */
1198 *did_something = 1;
1199 err = update_symlink(ondisk_path, target_path, target_len);
1200 if (fd != -1 && close(fd) == -1 && err == NULL)
1201 err = got_error_from_errno2("close", ondisk_path);
1202 return err;
1205 static const struct got_error *
1206 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1207 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1208 const char *meta_dir)
1210 const struct got_error *err = NULL;
1211 char canonpath[PATH_MAX];
1212 char *path_got = NULL;
1214 *is_bad_symlink = 0;
1216 if (target_len >= sizeof(canonpath)) {
1217 *is_bad_symlink = 1;
1218 return NULL;
1222 * We do not use realpath(3) to resolve the symlink's target
1223 * path because we don't want to resolve symlinks recursively.
1224 * Instead we make the path absolute and then canonicalize it.
1225 * Relative symlink target lookup should begin at the directory
1226 * in which the blob object is being installed.
1228 if (!got_path_is_absolute(target_path)) {
1229 char *abspath, *parent;
1230 err = got_path_dirname(&parent, ondisk_path);
1231 if (err)
1232 return err;
1233 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1234 free(parent);
1235 return got_error_from_errno("asprintf");
1237 free(parent);
1238 if (strlen(abspath) >= sizeof(canonpath)) {
1239 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1240 free(abspath);
1241 return err;
1243 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1244 free(abspath);
1245 if (err)
1246 return err;
1247 } else {
1248 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1249 if (err)
1250 return err;
1253 /* Only allow symlinks pointing at paths within the work tree. */
1254 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1255 *is_bad_symlink = 1;
1256 return NULL;
1259 /* Do not allow symlinks pointing into the meta directory. */
1260 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1261 return got_error_from_errno("asprintf");
1262 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1263 *is_bad_symlink = 1;
1265 free(path_got);
1266 return NULL;
1269 static const struct got_error *
1270 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1271 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1272 int restoring_missing_file, int reverting_versioned_file,
1273 int path_is_unversioned, int allow_bad_symlinks,
1274 struct got_repository *repo,
1275 got_worktree_checkout_cb progress_cb, void *progress_arg)
1277 const struct got_error *err = NULL;
1278 char target_path[PATH_MAX];
1279 size_t len, target_len = 0;
1280 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1281 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1283 *is_bad_symlink = 0;
1286 * Blob object content specifies the target path of the link.
1287 * If a symbolic link cannot be installed we instead create
1288 * a regular file which contains the link target path stored
1289 * in the blob object.
1291 do {
1292 err = got_object_blob_read_block(&len, blob);
1293 if (err)
1294 return err;
1296 if (len + target_len >= sizeof(target_path)) {
1297 /* Path too long; install as a regular file. */
1298 *is_bad_symlink = 1;
1299 got_object_blob_rewind(blob);
1300 return install_blob(worktree, ondisk_path, path,
1301 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1302 restoring_missing_file, reverting_versioned_file,
1303 1, path_is_unversioned, NULL, repo, progress_cb,
1304 progress_arg);
1306 if (len > 0) {
1307 /* Skip blob object header first time around. */
1308 memcpy(target_path + target_len, buf + hdrlen,
1309 len - hdrlen);
1310 target_len += len - hdrlen;
1311 hdrlen = 0;
1313 } while (len != 0);
1314 target_path[target_len] = '\0';
1316 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1317 ondisk_path, worktree->root_path, worktree->meta_dir);
1318 if (err)
1319 return err;
1321 if (*is_bad_symlink && !allow_bad_symlinks) {
1322 /* install as a regular file */
1323 got_object_blob_rewind(blob);
1324 err = install_blob(worktree, ondisk_path, path,
1325 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1326 restoring_missing_file, reverting_versioned_file, 1,
1327 path_is_unversioned, NULL, repo,
1328 progress_cb, progress_arg);
1329 return err;
1332 if (symlink(target_path, ondisk_path) == -1) {
1333 if (errno == EEXIST) {
1334 int symlink_replaced;
1335 if (path_is_unversioned) {
1336 err = (*progress_cb)(progress_arg,
1337 GOT_STATUS_UNVERSIONED, path);
1338 return err;
1340 err = replace_existing_symlink(&symlink_replaced,
1341 ondisk_path, target_path, target_len);
1342 if (err)
1343 return err;
1344 if (progress_cb) {
1345 if (symlink_replaced) {
1346 err = (*progress_cb)(progress_arg,
1347 reverting_versioned_file ?
1348 GOT_STATUS_REVERT :
1349 GOT_STATUS_UPDATE, path);
1350 } else {
1351 err = (*progress_cb)(progress_arg,
1352 GOT_STATUS_EXISTS, path);
1355 return err; /* Nothing else to do. */
1358 if (errno == ENOENT) {
1359 char *parent;
1360 err = got_path_dirname(&parent, ondisk_path);
1361 if (err)
1362 return err;
1363 err = got_path_mkdir(parent);
1364 free(parent);
1365 if (err)
1366 return err;
1368 * Retry, and fall through to error handling
1369 * below if this second attempt fails.
1371 if (symlink(target_path, ondisk_path) != -1) {
1372 err = NULL; /* success */
1373 if (progress_cb) {
1374 err = (*progress_cb)(progress_arg,
1375 reverting_versioned_file ?
1376 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1377 path);
1379 return err;
1383 /* Handle errors from first or second creation attempt. */
1384 if (errno == ENAMETOOLONG) {
1385 /* bad target path; install as a regular file */
1386 *is_bad_symlink = 1;
1387 got_object_blob_rewind(blob);
1388 err = install_blob(worktree, ondisk_path, path,
1389 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1390 restoring_missing_file, reverting_versioned_file, 1,
1391 path_is_unversioned, NULL, repo,
1392 progress_cb, progress_arg);
1393 } else if (errno == ENOTDIR) {
1394 err = got_error_path(ondisk_path,
1395 GOT_ERR_FILE_OBSTRUCTED);
1396 } else {
1397 err = got_error_from_errno3("symlink",
1398 target_path, ondisk_path);
1400 } else if (progress_cb)
1401 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1402 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1403 return err;
1406 static const struct got_error *
1407 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1408 const char *path, mode_t te_mode, mode_t st_mode,
1409 struct got_blob_object *blob, int restoring_missing_file,
1410 int reverting_versioned_file, int installing_bad_symlink,
1411 int path_is_unversioned, int *update_timestamps,
1412 struct got_repository *repo,
1413 got_worktree_checkout_cb progress_cb, void *progress_arg)
1415 const struct got_error *err = NULL;
1416 int fd = -1;
1417 size_t len, hdrlen;
1418 int update = 0;
1419 char *tmppath = NULL;
1420 mode_t mode;
1422 if (update_timestamps)
1423 *update_timestamps = 1;
1425 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1426 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1427 O_CLOEXEC, mode);
1428 if (fd == -1) {
1429 if (errno == ENOENT || errno == ENOTDIR) {
1430 char *parent;
1431 err = got_path_dirname(&parent, path);
1432 if (err)
1433 return err;
1434 err = add_dir_on_disk(worktree, parent);
1435 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1436 err = got_error_path(path, err->code);
1437 free(parent);
1438 if (err)
1439 return err;
1440 fd = open(ondisk_path,
1441 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1442 mode);
1443 if (fd == -1)
1444 return got_error_from_errno2("open",
1445 ondisk_path);
1446 } else if (errno == EEXIST) {
1447 if (path_is_unversioned) {
1448 if (update_timestamps)
1449 *update_timestamps = 0;
1450 err = (*progress_cb)(progress_arg,
1451 GOT_STATUS_EXISTS, path);
1452 goto done;
1454 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1455 !S_ISREG(st_mode) && !installing_bad_symlink) {
1456 /* TODO file is obstructed; do something */
1457 err = got_error_path(ondisk_path,
1458 GOT_ERR_FILE_OBSTRUCTED);
1459 goto done;
1460 } else {
1461 err = got_opentemp_named_fd(&tmppath, &fd,
1462 ondisk_path, "");
1463 if (err)
1464 goto done;
1465 update = 1;
1467 if (fchmod(fd, apply_umask(mode)) == -1) {
1468 err = got_error_from_errno2("fchmod",
1469 tmppath);
1470 goto done;
1473 } else
1474 return got_error_from_errno2("open", ondisk_path);
1477 if (progress_cb) {
1478 if (restoring_missing_file)
1479 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1480 path);
1481 else if (reverting_versioned_file)
1482 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1483 path);
1484 else
1485 err = (*progress_cb)(progress_arg,
1486 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1487 if (err)
1488 goto done;
1491 hdrlen = got_object_blob_get_hdrlen(blob);
1492 do {
1493 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1494 err = got_object_blob_read_block(&len, blob);
1495 if (err)
1496 break;
1497 if (len > 0) {
1498 /* Skip blob object header first time around. */
1499 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1500 if (outlen == -1) {
1501 err = got_error_from_errno("write");
1502 goto done;
1503 } else if (outlen != len - hdrlen) {
1504 err = got_error(GOT_ERR_IO);
1505 goto done;
1507 hdrlen = 0;
1509 } while (len != 0);
1511 if (fsync(fd) != 0) {
1512 err = got_error_from_errno("fsync");
1513 goto done;
1516 if (update) {
1517 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1518 err = got_error_from_errno2("unlink", ondisk_path);
1519 goto done;
1521 if (rename(tmppath, ondisk_path) != 0) {
1522 err = got_error_from_errno3("rename", tmppath,
1523 ondisk_path);
1524 goto done;
1526 free(tmppath);
1527 tmppath = NULL;
1530 done:
1531 if (fd != -1 && close(fd) == -1 && err == NULL)
1532 err = got_error_from_errno("close");
1533 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1534 err = got_error_from_errno2("unlink", tmppath);
1535 free(tmppath);
1536 return err;
1540 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1541 * conflict marker is found in newly added lines only.
1543 static const struct got_error *
1544 get_modified_file_content_status(unsigned char *status,
1545 struct got_blob_object *blob, const char *path, struct stat *sb,
1546 FILE *ondisk_file)
1548 const struct got_error *err, *free_err;
1549 const char *markers[3] = {
1550 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1551 GOT_DIFF_CONFLICT_MARKER_SEP,
1552 GOT_DIFF_CONFLICT_MARKER_END
1554 FILE *f1 = NULL;
1555 struct got_diffreg_result *diffreg_result = NULL;
1556 struct diff_result *r;
1557 int nchunks_parsed, n, i = 0, ln = 0;
1558 char *line = NULL;
1559 size_t linesize = 0;
1560 ssize_t linelen;
1562 if (*status != GOT_STATUS_MODIFY)
1563 return NULL;
1565 f1 = got_opentemp();
1566 if (f1 == NULL)
1567 return got_error_from_errno("got_opentemp");
1569 if (blob) {
1570 got_object_blob_rewind(blob);
1571 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1572 if (err)
1573 goto done;
1576 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1577 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1578 if (err)
1579 goto done;
1581 r = diffreg_result->result;
1583 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1584 struct diff_chunk *c;
1585 struct diff_chunk_context cc = {};
1586 off_t pos;
1589 * We can optimise a little by advancing straight
1590 * to the next chunk if this one has no added lines.
1592 c = diff_chunk_get(r, n);
1594 if (diff_chunk_type(c) != CHUNK_PLUS) {
1595 nchunks_parsed = 1;
1596 continue; /* removed or unchanged lines */
1599 pos = diff_chunk_get_right_start_pos(c);
1600 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1601 err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 goto done;
1605 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1606 ln = cc.right.start;
1608 while (ln < cc.right.end) {
1609 linelen = getline(&line, &linesize, ondisk_file);
1610 if (linelen == -1) {
1611 if (feof(ondisk_file))
1612 break;
1613 err = got_ferror(ondisk_file, GOT_ERR_IO);
1614 break;
1617 if (line && strncmp(line, markers[i],
1618 strlen(markers[i])) == 0) {
1619 if (strcmp(markers[i],
1620 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1621 *status = GOT_STATUS_CONFLICT;
1622 goto done;
1623 } else
1624 i++;
1626 ++ln;
1630 done:
1631 free(line);
1632 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1633 err = got_error_from_errno("fclose");
1634 free_err = got_diffreg_result_free(diffreg_result);
1635 if (err == NULL)
1636 err = free_err;
1638 return err;
1641 static int
1642 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1644 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1645 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1648 static int
1649 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1651 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1652 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1653 ie->mtime_sec == sb->st_mtim.tv_sec &&
1654 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1655 ie->size == (sb->st_size & 0xffffffff) &&
1656 !xbit_differs(ie, sb->st_mode));
1659 static unsigned char
1660 get_staged_status(struct got_fileindex_entry *ie)
1662 switch (got_fileindex_entry_stage_get(ie)) {
1663 case GOT_FILEIDX_STAGE_ADD:
1664 return GOT_STATUS_ADD;
1665 case GOT_FILEIDX_STAGE_DELETE:
1666 return GOT_STATUS_DELETE;
1667 case GOT_FILEIDX_STAGE_MODIFY:
1668 return GOT_STATUS_MODIFY;
1669 default:
1670 return GOT_STATUS_NO_CHANGE;
1674 static const struct got_error *
1675 get_symlink_modification_status(unsigned char *status,
1676 struct got_fileindex_entry *ie, const char *abspath,
1677 int dirfd, const char *de_name, struct got_blob_object *blob)
1679 const struct got_error *err = NULL;
1680 char target_path[PATH_MAX];
1681 char etarget[PATH_MAX];
1682 ssize_t elen;
1683 size_t len, target_len = 0;
1684 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1685 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1687 *status = GOT_STATUS_NO_CHANGE;
1689 /* Blob object content specifies the target path of the link. */
1690 do {
1691 err = got_object_blob_read_block(&len, blob);
1692 if (err)
1693 return err;
1694 if (len + target_len >= sizeof(target_path)) {
1696 * Should not happen. The blob contents were OK
1697 * when this symlink was installed.
1699 return got_error(GOT_ERR_NO_SPACE);
1701 if (len > 0) {
1702 /* Skip blob object header first time around. */
1703 memcpy(target_path + target_len, buf + hdrlen,
1704 len - hdrlen);
1705 target_len += len - hdrlen;
1706 hdrlen = 0;
1708 } while (len != 0);
1709 target_path[target_len] = '\0';
1711 if (dirfd != -1) {
1712 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1713 if (elen == -1)
1714 return got_error_from_errno2("readlinkat", abspath);
1715 } else {
1716 elen = readlink(abspath, etarget, sizeof(etarget));
1717 if (elen == -1)
1718 return got_error_from_errno2("readlink", abspath);
1721 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1722 *status = GOT_STATUS_MODIFY;
1724 return NULL;
1727 static const struct got_error *
1728 get_file_status(unsigned char *status, struct stat *sb,
1729 struct got_fileindex_entry *ie, const char *abspath,
1730 int dirfd, const char *de_name, struct got_repository *repo)
1732 const struct got_error *err = NULL;
1733 struct got_object_id id;
1734 size_t hdrlen;
1735 int fd = -1, fd1 = -1;
1736 FILE *f = NULL;
1737 uint8_t fbuf[8192];
1738 struct got_blob_object *blob = NULL;
1739 size_t flen, blen;
1740 unsigned char staged_status;
1742 staged_status = get_staged_status(ie);
1743 *status = GOT_STATUS_NO_CHANGE;
1744 memset(sb, 0, sizeof(*sb));
1747 * Whenever the caller provides a directory descriptor and a
1748 * directory entry name for the file, use them! This prevents
1749 * race conditions if filesystem paths change beneath our feet.
1751 if (dirfd != -1) {
1752 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1753 if (errno == ENOENT) {
1754 if (got_fileindex_entry_has_file_on_disk(ie))
1755 *status = GOT_STATUS_MISSING;
1756 else
1757 *status = GOT_STATUS_DELETE;
1758 goto done;
1760 err = got_error_from_errno2("fstatat", abspath);
1761 goto done;
1763 } else {
1764 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1765 if (fd == -1 && errno != ENOENT &&
1766 !got_err_open_nofollow_on_symlink())
1767 return got_error_from_errno2("open", abspath);
1768 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1769 if (lstat(abspath, sb) == -1)
1770 return got_error_from_errno2("lstat", abspath);
1771 } else if (fd == -1 || fstat(fd, sb) == -1) {
1772 if (errno == ENOENT) {
1773 if (got_fileindex_entry_has_file_on_disk(ie))
1774 *status = GOT_STATUS_MISSING;
1775 else
1776 *status = GOT_STATUS_DELETE;
1777 goto done;
1779 err = got_error_from_errno2("fstat", abspath);
1780 goto done;
1784 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1785 *status = GOT_STATUS_OBSTRUCTED;
1786 goto done;
1789 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1790 *status = GOT_STATUS_DELETE;
1791 goto done;
1792 } else if (!got_fileindex_entry_has_blob(ie) &&
1793 staged_status != GOT_STATUS_ADD) {
1794 *status = GOT_STATUS_ADD;
1795 goto done;
1798 if (!stat_info_differs(ie, sb))
1799 goto done;
1801 if (S_ISLNK(sb->st_mode) &&
1802 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1803 *status = GOT_STATUS_MODIFY;
1804 goto done;
1807 if (staged_status == GOT_STATUS_MODIFY ||
1808 staged_status == GOT_STATUS_ADD)
1809 got_fileindex_entry_get_staged_blob_id(&id, ie);
1810 else
1811 got_fileindex_entry_get_blob_id(&id, ie);
1813 fd1 = got_opentempfd();
1814 if (fd1 == -1) {
1815 err = got_error_from_errno("got_opentempfd");
1816 goto done;
1818 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1819 if (err)
1820 goto done;
1822 if (S_ISLNK(sb->st_mode)) {
1823 err = get_symlink_modification_status(status, ie,
1824 abspath, dirfd, de_name, blob);
1825 goto done;
1828 if (dirfd != -1) {
1829 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1830 if (fd == -1) {
1831 err = got_error_from_errno2("openat", abspath);
1832 goto done;
1836 f = fdopen(fd, "r");
1837 if (f == NULL) {
1838 err = got_error_from_errno2("fdopen", abspath);
1839 goto done;
1841 fd = -1;
1842 hdrlen = got_object_blob_get_hdrlen(blob);
1843 for (;;) {
1844 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1845 err = got_object_blob_read_block(&blen, blob);
1846 if (err)
1847 goto done;
1848 /* Skip length of blob object header first time around. */
1849 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1850 if (flen == 0 && ferror(f)) {
1851 err = got_error_from_errno("fread");
1852 goto done;
1854 if (blen - hdrlen == 0) {
1855 if (flen != 0)
1856 *status = GOT_STATUS_MODIFY;
1857 break;
1858 } else if (flen == 0) {
1859 if (blen - hdrlen != 0)
1860 *status = GOT_STATUS_MODIFY;
1861 break;
1862 } else if (blen - hdrlen == flen) {
1863 /* Skip blob object header first time around. */
1864 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1865 *status = GOT_STATUS_MODIFY;
1866 break;
1868 } else {
1869 *status = GOT_STATUS_MODIFY;
1870 break;
1872 hdrlen = 0;
1875 if (*status == GOT_STATUS_MODIFY) {
1876 rewind(f);
1877 err = get_modified_file_content_status(status, blob, ie->path,
1878 sb, f);
1879 } else if (xbit_differs(ie, sb->st_mode))
1880 *status = GOT_STATUS_MODE_CHANGE;
1881 done:
1882 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1883 err = got_error_from_errno("close");
1884 if (blob)
1885 got_object_blob_close(blob);
1886 if (f != NULL && fclose(f) == EOF && err == NULL)
1887 err = got_error_from_errno2("fclose", abspath);
1888 if (fd != -1 && close(fd) == -1 && err == NULL)
1889 err = got_error_from_errno2("close", abspath);
1890 return err;
1894 * Update timestamps in the file index if a file is unmodified and
1895 * we had to run a full content comparison to find out.
1897 static const struct got_error *
1898 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1899 struct got_fileindex_entry *ie, struct stat *sb)
1901 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1902 return got_fileindex_entry_update(ie, wt_fd, path,
1903 ie->blob_sha1, ie->commit_sha1, 1);
1905 return NULL;
1908 static const struct got_error *remove_ondisk_file(const char *, const char *);
1910 static const struct got_error *
1911 update_blob(struct got_worktree *worktree,
1912 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1913 struct got_tree_entry *te, const char *path,
1914 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1915 void *progress_arg)
1917 const struct got_error *err = NULL;
1918 struct got_blob_object *blob = NULL;
1919 char *ondisk_path = NULL;
1920 unsigned char status = GOT_STATUS_NO_CHANGE;
1921 struct stat sb;
1922 int fd1 = -1, fd2 = -1;
1923 int update_timestamps = 0;
1925 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1926 return got_error_from_errno("asprintf");
1928 if (ie) {
1929 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1930 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1931 goto done;
1933 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1934 repo);
1935 if (err)
1936 goto done;
1937 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1938 sb.st_mode = got_fileindex_perms_to_st(ie);
1939 } else {
1940 if (stat(ondisk_path, &sb) == -1) {
1941 if (errno != ENOENT && errno != ENOTDIR) {
1942 err = got_error_from_errno2("stat",
1943 ondisk_path);
1944 goto done;
1946 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1947 status = GOT_STATUS_UNVERSIONED;
1948 } else {
1949 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1950 status = GOT_STATUS_UNVERSIONED;
1951 else
1952 status = GOT_STATUS_OBSTRUCTED;
1956 if (status == GOT_STATUS_OBSTRUCTED) {
1957 if (ie)
1958 got_fileindex_entry_mark_skipped(ie);
1959 err = (*progress_cb)(progress_arg, status, path);
1960 goto done;
1962 if (status == GOT_STATUS_CONFLICT) {
1963 if (ie)
1964 got_fileindex_entry_mark_skipped(ie);
1965 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1966 path);
1967 goto done;
1970 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1971 if (status == GOT_STATUS_UNVERSIONED) {
1972 err = (*progress_cb)(progress_arg, status, path);
1973 } else if (status != GOT_STATUS_NO_CHANGE &&
1974 status != GOT_STATUS_DELETE &&
1975 status != GOT_STATUS_NONEXISTENT &&
1976 status != GOT_STATUS_MISSING) {
1977 err = (*progress_cb)(progress_arg,
1978 GOT_STATUS_CANNOT_DELETE, path);
1979 } else if (ie) {
1980 if (status != GOT_STATUS_DELETE &&
1981 status != GOT_STATUS_NONEXISTENT &&
1982 status != GOT_STATUS_MISSING) {
1983 err = remove_ondisk_file(worktree->root_path,
1984 ie->path);
1985 if (err && !(err->code == GOT_ERR_ERRNO &&
1986 errno == ENOENT))
1987 goto done;
1989 got_fileindex_entry_remove(fileindex, ie);
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1991 ie->path);
1993 goto done; /* nothing else to do */
1996 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1997 (S_ISLNK(te->mode) ||
1998 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
2000 * This is a regular file or an installed bad symlink.
2001 * If the file index indicates that this file is already
2002 * up-to-date with respect to the repository we can skip
2003 * updating contents of this file.
2005 if (got_fileindex_entry_has_commit(ie) &&
2006 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2007 SHA1_DIGEST_LENGTH) == 0) {
2008 /* Same commit. */
2009 err = sync_timestamps(worktree->root_fd,
2010 path, status, ie, &sb);
2011 if (err)
2012 goto done;
2013 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2014 path);
2015 goto done;
2017 if (got_fileindex_entry_has_blob(ie) &&
2018 memcmp(ie->blob_sha1, te->id.sha1,
2019 SHA1_DIGEST_LENGTH) == 0) {
2020 /* Different commit but the same blob. */
2021 if (got_fileindex_entry_has_commit(ie)) {
2022 /* Update the base commit ID of this file. */
2023 memcpy(ie->commit_sha1,
2024 worktree->base_commit_id->sha1,
2025 sizeof(ie->commit_sha1));
2027 err = sync_timestamps(worktree->root_fd,
2028 path, status, ie, &sb);
2029 if (err)
2030 goto done;
2031 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2032 path);
2033 goto done;
2037 fd1 = got_opentempfd();
2038 if (fd1 == -1) {
2039 err = got_error_from_errno("got_opentempfd");
2040 goto done;
2042 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2043 if (err)
2044 goto done;
2046 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2047 struct got_blob_object *blob2 = NULL;
2048 char *label_orig = NULL;
2049 if (got_fileindex_entry_has_blob(ie)) {
2050 fd2 = got_opentempfd();
2051 if (fd2 == -1) {
2052 err = got_error_from_errno("got_opentempfd");
2053 goto done;
2055 struct got_object_id id2;
2056 got_fileindex_entry_get_blob_id(&id2, ie);
2057 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2058 fd2);
2059 if (err)
2060 goto done;
2062 if (got_fileindex_entry_has_commit(ie)) {
2063 char id_str[SHA1_DIGEST_STRING_LENGTH];
2064 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2065 sizeof(id_str)) == NULL) {
2066 err = got_error_path(id_str,
2067 GOT_ERR_BAD_OBJ_ID_STR);
2068 goto done;
2070 if (asprintf(&label_orig, "%s: commit %s",
2071 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2072 err = got_error_from_errno("asprintf");
2073 goto done;
2076 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2077 char *link_target;
2078 err = got_object_blob_read_to_str(&link_target, blob);
2079 if (err)
2080 goto done;
2081 err = merge_symlink(worktree, blob2, ondisk_path, path,
2082 label_orig, link_target, worktree->base_commit_id,
2083 repo, progress_cb, progress_arg);
2084 free(link_target);
2085 } else {
2086 err = merge_blob(&update_timestamps, worktree, blob2,
2087 ondisk_path, path, sb.st_mode, label_orig, blob,
2088 worktree->base_commit_id, repo,
2089 progress_cb, progress_arg);
2091 free(label_orig);
2092 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2093 err = got_error_from_errno("close");
2094 goto done;
2096 if (blob2)
2097 got_object_blob_close(blob2);
2098 if (err)
2099 goto done;
2101 * Do not update timestamps of files with local changes.
2102 * Otherwise, a future status walk would treat them as
2103 * unmodified files again.
2105 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2106 blob->id.sha1, worktree->base_commit_id->sha1,
2107 update_timestamps);
2108 } else if (status == GOT_STATUS_MODE_CHANGE) {
2109 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2110 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2111 } else if (status == GOT_STATUS_DELETE) {
2112 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2113 if (err)
2114 goto done;
2115 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2116 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2117 if (err)
2118 goto done;
2119 } else {
2120 int is_bad_symlink = 0;
2121 if (S_ISLNK(te->mode)) {
2122 err = install_symlink(&is_bad_symlink, worktree,
2123 ondisk_path, path, blob,
2124 status == GOT_STATUS_MISSING, 0,
2125 status == GOT_STATUS_UNVERSIONED, 0,
2126 repo, progress_cb, progress_arg);
2127 } else {
2128 err = install_blob(worktree, ondisk_path, path,
2129 te->mode, sb.st_mode, blob,
2130 status == GOT_STATUS_MISSING, 0, 0,
2131 status == GOT_STATUS_UNVERSIONED,
2132 &update_timestamps,
2133 repo, progress_cb, progress_arg);
2135 if (err)
2136 goto done;
2138 if (ie) {
2139 err = got_fileindex_entry_update(ie,
2140 worktree->root_fd, path, blob->id.sha1,
2141 worktree->base_commit_id->sha1, 1);
2142 } else {
2143 err = create_fileindex_entry(&ie, fileindex,
2144 worktree->base_commit_id, worktree->root_fd, path,
2145 &blob->id, update_timestamps);
2147 if (err)
2148 goto done;
2150 if (is_bad_symlink) {
2151 got_fileindex_entry_filetype_set(ie,
2152 GOT_FILEIDX_MODE_BAD_SYMLINK);
2156 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2157 err = got_error_from_errno("close");
2158 goto done;
2160 got_object_blob_close(blob);
2161 done:
2162 free(ondisk_path);
2163 return err;
2166 static const struct got_error *
2167 remove_ondisk_file(const char *root_path, const char *path)
2169 const struct got_error *err = NULL;
2170 char *ondisk_path = NULL, *parent = NULL;
2172 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2173 return got_error_from_errno("asprintf");
2175 if (unlink(ondisk_path) == -1) {
2176 if (errno != ENOENT)
2177 err = got_error_from_errno2("unlink", ondisk_path);
2178 } else {
2179 size_t root_len = strlen(root_path);
2180 err = got_path_dirname(&parent, ondisk_path);
2181 if (err)
2182 goto done;
2183 while (got_path_cmp(parent, root_path,
2184 strlen(parent), root_len) != 0) {
2185 free(ondisk_path);
2186 ondisk_path = parent;
2187 parent = NULL;
2188 if (rmdir(ondisk_path) == -1) {
2189 if (errno != ENOTEMPTY)
2190 err = got_error_from_errno2("rmdir",
2191 ondisk_path);
2192 break;
2194 err = got_path_dirname(&parent, ondisk_path);
2195 if (err)
2196 break;
2199 done:
2200 free(ondisk_path);
2201 free(parent);
2202 return err;
2205 static const struct got_error *
2206 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2207 struct got_fileindex_entry *ie, struct got_repository *repo,
2208 got_worktree_checkout_cb progress_cb, void *progress_arg)
2210 const struct got_error *err = NULL;
2211 unsigned char status;
2212 struct stat sb;
2213 char *ondisk_path;
2215 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2216 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2218 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2219 == -1)
2220 return got_error_from_errno("asprintf");
2222 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2223 if (err)
2224 goto done;
2226 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2227 char ondisk_target[PATH_MAX];
2228 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2229 sizeof(ondisk_target));
2230 if (ondisk_len == -1) {
2231 err = got_error_from_errno2("readlink", ondisk_path);
2232 goto done;
2234 ondisk_target[ondisk_len] = '\0';
2235 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2236 NULL, NULL, /* XXX pass common ancestor info? */
2237 ondisk_target, ondisk_path);
2238 if (err)
2239 goto done;
2240 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2241 ie->path);
2242 goto done;
2245 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2246 status == GOT_STATUS_ADD) {
2247 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2248 if (err)
2249 goto done;
2251 * Preserve the working file and change the deleted blob's
2252 * entry into a schedule-add entry.
2254 err = got_fileindex_entry_update(ie, worktree->root_fd,
2255 ie->path, NULL, NULL, 0);
2256 } else {
2257 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2258 if (err)
2259 goto done;
2260 if (status == GOT_STATUS_NO_CHANGE) {
2261 err = remove_ondisk_file(worktree->root_path, ie->path);
2262 if (err)
2263 goto done;
2265 got_fileindex_entry_remove(fileindex, ie);
2267 done:
2268 free(ondisk_path);
2269 return err;
2272 struct diff_cb_arg {
2273 struct got_fileindex *fileindex;
2274 struct got_worktree *worktree;
2275 struct got_repository *repo;
2276 got_worktree_checkout_cb progress_cb;
2277 void *progress_arg;
2278 got_cancel_cb cancel_cb;
2279 void *cancel_arg;
2282 static const struct got_error *
2283 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2284 struct got_tree_entry *te, const char *parent_path)
2286 const struct got_error *err = NULL;
2287 struct diff_cb_arg *a = arg;
2289 if (a->cancel_cb) {
2290 err = a->cancel_cb(a->cancel_arg);
2291 if (err)
2292 return err;
2295 return update_blob(a->worktree, a->fileindex, ie, te,
2296 ie->path, a->repo, a->progress_cb, a->progress_arg);
2299 static const struct got_error *
2300 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2302 const struct got_error *err = NULL;
2303 struct diff_cb_arg *a = arg;
2305 if (a->cancel_cb) {
2306 err = a->cancel_cb(a->cancel_arg);
2307 if (err)
2308 return err;
2311 return delete_blob(a->worktree, a->fileindex, ie,
2312 a->repo, a->progress_cb, a->progress_arg);
2315 static const struct got_error *
2316 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2318 const struct got_error *err = NULL;
2319 struct diff_cb_arg *a = arg;
2320 char *path;
2322 if (a->cancel_cb) {
2323 err = a->cancel_cb(a->cancel_arg);
2324 if (err)
2325 return err;
2328 if (got_object_tree_entry_is_submodule(te))
2329 return NULL;
2331 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2332 return NULL;
2334 if (asprintf(&path, "%s%s%s", parent_path,
2335 parent_path[0] ? "/" : "", te->name)
2336 == -1)
2337 return got_error_from_errno("asprintf");
2339 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2340 a->repo, a->progress_cb, a->progress_arg);
2342 free(path);
2343 return err;
2346 const struct got_error *
2347 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2349 uint32_t uuid_status;
2351 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2352 if (uuid_status != uuid_s_ok) {
2353 *uuidstr = NULL;
2354 return got_error_uuid(uuid_status, "uuid_to_string");
2357 return NULL;
2360 static const struct got_error *
2361 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2363 const struct got_error *err = NULL;
2364 char *uuidstr = NULL;
2366 *refname = NULL;
2368 err = got_worktree_get_uuid(&uuidstr, worktree);
2369 if (err)
2370 return err;
2372 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2373 err = got_error_from_errno("asprintf");
2374 *refname = NULL;
2376 free(uuidstr);
2377 return err;
2380 const struct got_error *
2381 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2382 const char *prefix)
2384 return get_ref_name(refname, worktree, prefix);
2387 static const struct got_error *
2388 get_base_ref_name(char **refname, struct got_worktree *worktree)
2390 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2393 static const struct got_error *
2394 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2396 return get_ref_name(refname, worktree,
2397 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2400 static const struct got_error *
2401 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2403 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2406 static const struct got_error *
2407 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2409 return get_ref_name(refname, worktree,
2410 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2413 static const struct got_error *
2414 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2416 return get_ref_name(refname, worktree,
2417 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2420 static const struct got_error *
2421 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2423 return get_ref_name(refname, worktree,
2424 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2427 static const struct got_error *
2428 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2430 return get_ref_name(refname, worktree,
2431 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2434 static const struct got_error *
2435 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2437 return get_ref_name(refname, worktree,
2438 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2441 static const struct got_error *
2442 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2444 return get_ref_name(refname, worktree,
2445 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2448 const struct got_error *
2449 got_worktree_get_histedit_script_path(char **path,
2450 struct got_worktree *worktree)
2452 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2453 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2454 *path = NULL;
2455 return got_error_from_errno("asprintf");
2457 return NULL;
2460 static const struct got_error *
2461 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2463 return get_ref_name(refname, worktree,
2464 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2467 static const struct got_error *
2468 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2470 return get_ref_name(refname, worktree,
2471 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2475 * Prevent Git's garbage collector from deleting our base commit by
2476 * setting a reference to our base commit's ID.
2478 static const struct got_error *
2479 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2481 const struct got_error *err = NULL;
2482 struct got_reference *ref = NULL;
2483 char *refname;
2485 err = get_base_ref_name(&refname, worktree);
2486 if (err)
2487 return err;
2489 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2490 if (err)
2491 goto done;
2493 err = got_ref_write(ref, repo);
2494 done:
2495 free(refname);
2496 if (ref)
2497 got_ref_close(ref);
2498 return err;
2501 static const struct got_error *
2502 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2504 const struct got_error *err = NULL;
2506 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2507 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2508 err = got_error_from_errno("asprintf");
2509 *fileindex_path = NULL;
2511 return err;
2515 static const struct got_error *
2516 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2517 struct got_worktree *worktree)
2519 const struct got_error *err = NULL;
2520 FILE *index = NULL;
2522 *fileindex_path = NULL;
2523 *fileindex = got_fileindex_alloc();
2524 if (*fileindex == NULL)
2525 return got_error_from_errno("got_fileindex_alloc");
2527 err = get_fileindex_path(fileindex_path, worktree);
2528 if (err)
2529 goto done;
2531 index = fopen(*fileindex_path, "rbe");
2532 if (index == NULL) {
2533 if (errno != ENOENT)
2534 err = got_error_from_errno2("fopen", *fileindex_path);
2535 } else {
2536 err = got_fileindex_read(*fileindex, index);
2537 if (fclose(index) == EOF && err == NULL)
2538 err = got_error_from_errno("fclose");
2540 done:
2541 if (err) {
2542 free(*fileindex_path);
2543 *fileindex_path = NULL;
2544 got_fileindex_free(*fileindex);
2545 *fileindex = NULL;
2547 return err;
2550 struct bump_base_commit_id_arg {
2551 struct got_object_id *base_commit_id;
2552 const char *path;
2553 size_t path_len;
2554 const char *entry_name;
2555 got_worktree_checkout_cb progress_cb;
2556 void *progress_arg;
2559 static const struct got_error *
2560 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2562 const struct got_error *err;
2563 struct bump_base_commit_id_arg *a = arg;
2565 if (a->entry_name) {
2566 if (strcmp(ie->path, a->path) != 0)
2567 return NULL;
2568 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2569 return NULL;
2571 if (got_fileindex_entry_was_skipped(ie))
2572 return NULL;
2574 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2575 SHA1_DIGEST_LENGTH) == 0)
2576 return NULL;
2578 if (a->progress_cb) {
2579 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2580 ie->path);
2581 if (err)
2582 return err;
2584 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2585 return NULL;
2588 /* Bump base commit ID of all files within an updated part of the work tree. */
2589 static const struct got_error *
2590 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2591 struct got_fileindex *fileindex,
2592 got_worktree_checkout_cb progress_cb, void *progress_arg)
2594 struct bump_base_commit_id_arg bbc_arg;
2596 bbc_arg.base_commit_id = worktree->base_commit_id;
2597 bbc_arg.entry_name = NULL;
2598 bbc_arg.path = "";
2599 bbc_arg.path_len = 0;
2600 bbc_arg.progress_cb = progress_cb;
2601 bbc_arg.progress_arg = progress_arg;
2603 return got_fileindex_for_each_entry_safe(fileindex,
2604 bump_base_commit_id, &bbc_arg);
2607 static const struct got_error *
2608 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2610 const struct got_error *err = NULL;
2611 char *new_fileindex_path = NULL;
2612 FILE *new_index = NULL;
2613 struct timespec timeout;
2615 err = got_opentemp_named(&new_fileindex_path, &new_index,
2616 fileindex_path, "");
2617 if (err)
2618 goto done;
2620 err = got_fileindex_write(fileindex, new_index);
2621 if (err)
2622 goto done;
2624 if (rename(new_fileindex_path, fileindex_path) != 0) {
2625 err = got_error_from_errno3("rename", new_fileindex_path,
2626 fileindex_path);
2627 unlink(new_fileindex_path);
2631 * Sleep for a short amount of time to ensure that files modified after
2632 * this program exits have a different time stamp from the one which
2633 * was recorded in the file index.
2635 timeout.tv_sec = 0;
2636 timeout.tv_nsec = 1;
2637 nanosleep(&timeout, NULL);
2638 done:
2639 if (new_index)
2640 fclose(new_index);
2641 free(new_fileindex_path);
2642 return err;
2645 static const struct got_error *
2646 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2647 struct got_object_id **tree_id, const char *wt_relpath,
2648 struct got_commit_object *base_commit, struct got_worktree *worktree,
2649 struct got_repository *repo)
2651 const struct got_error *err = NULL;
2652 struct got_object_id *id = NULL;
2653 char *in_repo_path = NULL;
2654 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2656 *entry_type = GOT_OBJ_TYPE_ANY;
2657 *tree_relpath = NULL;
2658 *tree_id = NULL;
2660 if (wt_relpath[0] == '\0') {
2661 /* Check out all files within the work tree. */
2662 *entry_type = GOT_OBJ_TYPE_TREE;
2663 *tree_relpath = strdup("");
2664 if (*tree_relpath == NULL) {
2665 err = got_error_from_errno("strdup");
2666 goto done;
2668 err = got_object_id_by_path(tree_id, repo, base_commit,
2669 worktree->path_prefix);
2670 if (err)
2671 goto done;
2672 return NULL;
2675 /* Check out a subset of files in the work tree. */
2677 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2678 is_root_wt ? "" : "/", wt_relpath) == -1) {
2679 err = got_error_from_errno("asprintf");
2680 goto done;
2683 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2684 if (err)
2685 goto done;
2687 free(in_repo_path);
2688 in_repo_path = NULL;
2690 err = got_object_get_type(entry_type, repo, id);
2691 if (err)
2692 goto done;
2694 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2695 /* Check out a single file. */
2696 if (strchr(wt_relpath, '/') == NULL) {
2697 /* Check out a single file in work tree's root dir. */
2698 in_repo_path = strdup(worktree->path_prefix);
2699 if (in_repo_path == NULL) {
2700 err = got_error_from_errno("strdup");
2701 goto done;
2703 *tree_relpath = strdup("");
2704 if (*tree_relpath == NULL) {
2705 err = got_error_from_errno("strdup");
2706 goto done;
2708 } else {
2709 /* Check out a single file in a subdirectory. */
2710 err = got_path_dirname(tree_relpath, wt_relpath);
2711 if (err)
2712 return err;
2713 if (asprintf(&in_repo_path, "%s%s%s",
2714 worktree->path_prefix, is_root_wt ? "" : "/",
2715 *tree_relpath) == -1) {
2716 err = got_error_from_errno("asprintf");
2717 goto done;
2720 err = got_object_id_by_path(tree_id, repo,
2721 base_commit, in_repo_path);
2722 } else {
2723 /* Check out all files within a subdirectory. */
2724 *tree_id = got_object_id_dup(id);
2725 if (*tree_id == NULL) {
2726 err = got_error_from_errno("got_object_id_dup");
2727 goto done;
2729 *tree_relpath = strdup(wt_relpath);
2730 if (*tree_relpath == NULL) {
2731 err = got_error_from_errno("strdup");
2732 goto done;
2735 done:
2736 free(id);
2737 free(in_repo_path);
2738 if (err) {
2739 *entry_type = GOT_OBJ_TYPE_ANY;
2740 free(*tree_relpath);
2741 *tree_relpath = NULL;
2742 free(*tree_id);
2743 *tree_id = NULL;
2745 return err;
2748 static const struct got_error *
2749 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2750 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2751 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2752 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2754 const struct got_error *err = NULL;
2755 struct got_commit_object *commit = NULL;
2756 struct got_tree_object *tree = NULL;
2757 struct got_fileindex_diff_tree_cb diff_cb;
2758 struct diff_cb_arg arg;
2760 err = ref_base_commit(worktree, repo);
2761 if (err) {
2762 if (!(err->code == GOT_ERR_ERRNO &&
2763 (errno == EACCES || errno == EROFS)))
2764 goto done;
2765 err = (*progress_cb)(progress_arg,
2766 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2767 if (err)
2768 return err;
2771 err = got_object_open_as_commit(&commit, repo,
2772 worktree->base_commit_id);
2773 if (err)
2774 goto done;
2776 err = got_object_open_as_tree(&tree, repo, tree_id);
2777 if (err)
2778 goto done;
2780 if (entry_name &&
2781 got_object_tree_find_entry(tree, entry_name) == NULL) {
2782 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2783 goto done;
2786 diff_cb.diff_old_new = diff_old_new;
2787 diff_cb.diff_old = diff_old;
2788 diff_cb.diff_new = diff_new;
2789 arg.fileindex = fileindex;
2790 arg.worktree = worktree;
2791 arg.repo = repo;
2792 arg.progress_cb = progress_cb;
2793 arg.progress_arg = progress_arg;
2794 arg.cancel_cb = cancel_cb;
2795 arg.cancel_arg = cancel_arg;
2796 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2797 entry_name, repo, &diff_cb, &arg);
2798 done:
2799 if (tree)
2800 got_object_tree_close(tree);
2801 if (commit)
2802 got_object_commit_close(commit);
2803 return err;
2806 const struct got_error *
2807 got_worktree_checkout_files(struct got_worktree *worktree,
2808 struct got_pathlist_head *paths, struct got_repository *repo,
2809 got_worktree_checkout_cb progress_cb, void *progress_arg,
2810 got_cancel_cb cancel_cb, void *cancel_arg)
2812 const struct got_error *err = NULL, *sync_err, *unlockerr;
2813 struct got_commit_object *commit = NULL;
2814 struct got_tree_object *tree = NULL;
2815 struct got_fileindex *fileindex = NULL;
2816 char *fileindex_path = NULL;
2817 struct got_pathlist_entry *pe;
2818 struct tree_path_data {
2819 STAILQ_ENTRY(tree_path_data) entry;
2820 struct got_object_id *tree_id;
2821 int entry_type;
2822 char *relpath;
2823 char *entry_name;
2824 } *tpd = NULL;
2825 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2827 STAILQ_INIT(&tree_paths);
2829 err = lock_worktree(worktree, LOCK_EX);
2830 if (err)
2831 return err;
2833 err = got_object_open_as_commit(&commit, repo,
2834 worktree->base_commit_id);
2835 if (err)
2836 goto done;
2838 /* Map all specified paths to in-repository trees. */
2839 TAILQ_FOREACH(pe, paths, entry) {
2840 tpd = malloc(sizeof(*tpd));
2841 if (tpd == NULL) {
2842 err = got_error_from_errno("malloc");
2843 goto done;
2846 err = find_tree_entry_for_checkout(&tpd->entry_type,
2847 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2848 worktree, repo);
2849 if (err) {
2850 free(tpd);
2851 goto done;
2854 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2855 err = got_path_basename(&tpd->entry_name, pe->path);
2856 if (err) {
2857 free(tpd->relpath);
2858 free(tpd->tree_id);
2859 free(tpd);
2860 goto done;
2862 } else
2863 tpd->entry_name = NULL;
2865 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2869 * Read the file index.
2870 * Checking out files is supposed to be an idempotent operation.
2871 * If the on-disk file index is incomplete we will try to complete it.
2873 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2874 if (err)
2875 goto done;
2877 tpd = STAILQ_FIRST(&tree_paths);
2878 TAILQ_FOREACH(pe, paths, entry) {
2879 struct bump_base_commit_id_arg bbc_arg;
2881 err = checkout_files(worktree, fileindex, tpd->relpath,
2882 tpd->tree_id, tpd->entry_name, repo,
2883 progress_cb, progress_arg, cancel_cb, cancel_arg);
2884 if (err)
2885 break;
2887 bbc_arg.base_commit_id = worktree->base_commit_id;
2888 bbc_arg.entry_name = tpd->entry_name;
2889 bbc_arg.path = pe->path;
2890 bbc_arg.path_len = pe->path_len;
2891 bbc_arg.progress_cb = progress_cb;
2892 bbc_arg.progress_arg = progress_arg;
2893 err = got_fileindex_for_each_entry_safe(fileindex,
2894 bump_base_commit_id, &bbc_arg);
2895 if (err)
2896 break;
2898 tpd = STAILQ_NEXT(tpd, entry);
2900 sync_err = sync_fileindex(fileindex, fileindex_path);
2901 if (sync_err && err == NULL)
2902 err = sync_err;
2903 done:
2904 free(fileindex_path);
2905 if (tree)
2906 got_object_tree_close(tree);
2907 if (commit)
2908 got_object_commit_close(commit);
2909 if (fileindex)
2910 got_fileindex_free(fileindex);
2911 while (!STAILQ_EMPTY(&tree_paths)) {
2912 tpd = STAILQ_FIRST(&tree_paths);
2913 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2914 free(tpd->relpath);
2915 free(tpd->tree_id);
2916 free(tpd);
2918 unlockerr = lock_worktree(worktree, LOCK_SH);
2919 if (unlockerr && err == NULL)
2920 err = unlockerr;
2921 return err;
2924 static const struct got_error *
2925 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2926 struct got_fileindex_entry *ie, const char *ondisk_path,
2927 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2928 int restoring_missing_file, int reverting_versioned_file,
2929 int path_is_unversioned, int allow_bad_symlinks,
2930 struct got_repository *repo,
2931 got_worktree_checkout_cb progress_cb, void *progress_arg)
2933 const struct got_error *err = NULL;
2934 int is_bad_symlink = 0;
2936 if (S_ISLNK(mode2)) {
2937 err = install_symlink(&is_bad_symlink,
2938 worktree, ondisk_path, path2, blob2,
2939 restoring_missing_file,
2940 reverting_versioned_file,
2941 path_is_unversioned, allow_bad_symlinks,
2942 repo, progress_cb, progress_arg);
2943 } else {
2944 err = install_blob(worktree, ondisk_path, path2,
2945 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2946 restoring_missing_file, reverting_versioned_file, 0,
2947 path_is_unversioned, NULL, repo,
2948 progress_cb, progress_arg);
2950 if (err)
2951 return err;
2952 if (ie == NULL) {
2953 /* Adding an unversioned file. */
2954 err = got_fileindex_entry_alloc(&ie, path2);
2955 if (err)
2956 return err;
2957 err = got_fileindex_entry_update(ie,
2958 worktree->root_fd, path2, NULL, NULL, 1);
2959 if (err) {
2960 got_fileindex_entry_free(ie);
2961 return err;
2963 err = got_fileindex_entry_add(fileindex, ie);
2964 if (err) {
2965 got_fileindex_entry_free(ie);
2966 return err;
2968 } else {
2969 /* Re-adding a locally deleted file. */
2970 err = got_fileindex_entry_update(ie,
2971 worktree->root_fd, path2, ie->blob_sha1,
2972 worktree->base_commit_id->sha1, 0);
2973 if (err)
2974 return err;
2977 if (is_bad_symlink) {
2978 got_fileindex_entry_filetype_set(ie,
2979 GOT_FILEIDX_MODE_BAD_SYMLINK);
2982 return NULL;
2985 struct merge_file_cb_arg {
2986 struct got_worktree *worktree;
2987 struct got_fileindex *fileindex;
2988 got_worktree_checkout_cb progress_cb;
2989 void *progress_arg;
2990 got_cancel_cb cancel_cb;
2991 void *cancel_arg;
2992 const char *label_orig;
2993 struct got_object_id *commit_id2;
2994 int allow_bad_symlinks;
2997 static const struct got_error *
2998 merge_file_cb(void *arg, struct got_blob_object *blob1,
2999 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3000 struct got_object_id *id1, struct got_object_id *id2,
3001 const char *path1, const char *path2,
3002 mode_t mode1, mode_t mode2, struct got_repository *repo)
3004 static const struct got_error *err = NULL;
3005 struct merge_file_cb_arg *a = arg;
3006 struct got_fileindex_entry *ie;
3007 char *ondisk_path = NULL;
3008 struct stat sb;
3009 unsigned char status;
3010 int local_changes_subsumed;
3011 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3012 char *id_str = NULL, *label_deriv2 = NULL;
3013 struct got_object_id *id = NULL;
3015 if (blob1 && blob2) {
3016 ie = got_fileindex_entry_get(a->fileindex, path2,
3017 strlen(path2));
3018 if (ie == NULL)
3019 return (*a->progress_cb)(a->progress_arg,
3020 GOT_STATUS_MISSING, path2);
3022 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3023 path2) == -1)
3024 return got_error_from_errno("asprintf");
3026 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3027 repo);
3028 if (err)
3029 goto done;
3031 if (status == GOT_STATUS_DELETE) {
3032 err = (*a->progress_cb)(a->progress_arg,
3033 GOT_STATUS_MERGE, path2);
3034 goto done;
3036 if (status != GOT_STATUS_NO_CHANGE &&
3037 status != GOT_STATUS_MODIFY &&
3038 status != GOT_STATUS_CONFLICT &&
3039 status != GOT_STATUS_ADD) {
3040 err = (*a->progress_cb)(a->progress_arg, status, path2);
3041 goto done;
3044 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3045 char *link_target2;
3046 err = got_object_blob_read_to_str(&link_target2, blob2);
3047 if (err)
3048 goto done;
3049 err = merge_symlink(a->worktree, blob1, ondisk_path,
3050 path2, a->label_orig, link_target2, a->commit_id2,
3051 repo, a->progress_cb, a->progress_arg);
3052 free(link_target2);
3053 } else {
3054 int fd;
3056 f_orig = got_opentemp();
3057 if (f_orig == NULL) {
3058 err = got_error_from_errno("got_opentemp");
3059 goto done;
3061 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3062 f_orig, blob1);
3063 if (err)
3064 goto done;
3066 f_deriv2 = got_opentemp();
3067 if (f_deriv2 == NULL)
3068 goto done;
3069 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3070 f_deriv2, blob2);
3071 if (err)
3072 goto done;
3074 fd = open(ondisk_path,
3075 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3076 if (fd == -1) {
3077 err = got_error_from_errno2("open",
3078 ondisk_path);
3079 goto done;
3081 f_deriv = fdopen(fd, "r");
3082 if (f_deriv == NULL) {
3083 err = got_error_from_errno2("fdopen",
3084 ondisk_path);
3085 close(fd);
3086 goto done;
3088 err = got_object_id_str(&id_str, a->commit_id2);
3089 if (err)
3090 goto done;
3091 if (asprintf(&label_deriv2, "%s: commit %s",
3092 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3093 err = got_error_from_errno("asprintf");
3094 goto done;
3096 err = merge_file(&local_changes_subsumed, a->worktree,
3097 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3098 mode2, a->label_orig, NULL, label_deriv2,
3099 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3100 a->progress_cb, a->progress_arg);
3102 } else if (blob1) {
3103 ie = got_fileindex_entry_get(a->fileindex, path1,
3104 strlen(path1));
3105 if (ie == NULL)
3106 return (*a->progress_cb)(a->progress_arg,
3107 GOT_STATUS_MISSING, path1);
3109 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3110 path1) == -1)
3111 return got_error_from_errno("asprintf");
3113 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3114 repo);
3115 if (err)
3116 goto done;
3118 switch (status) {
3119 case GOT_STATUS_NO_CHANGE:
3120 err = (*a->progress_cb)(a->progress_arg,
3121 GOT_STATUS_DELETE, path1);
3122 if (err)
3123 goto done;
3124 err = remove_ondisk_file(a->worktree->root_path, path1);
3125 if (err)
3126 goto done;
3127 if (ie)
3128 got_fileindex_entry_mark_deleted_from_disk(ie);
3129 break;
3130 case GOT_STATUS_DELETE:
3131 case GOT_STATUS_MISSING:
3132 err = (*a->progress_cb)(a->progress_arg,
3133 GOT_STATUS_DELETE, path1);
3134 if (err)
3135 goto done;
3136 if (ie)
3137 got_fileindex_entry_mark_deleted_from_disk(ie);
3138 break;
3139 case GOT_STATUS_MODIFY: {
3140 FILE *blob1_f;
3141 off_t blob1_size;
3142 int obj_type;
3144 * Delete the file only if its content already
3145 * exists in the repository.
3147 err = got_object_blob_file_create(&id, &blob1_f,
3148 &blob1_size, path1);
3149 if (err)
3150 goto done;
3151 if (fclose(blob1_f) == EOF) {
3152 err = got_error_from_errno("fclose");
3153 goto done;
3156 /* Implied existence check. */
3157 err = got_object_get_type(&obj_type, repo, id);
3158 if (err) {
3159 if (err->code != GOT_ERR_NO_OBJ)
3160 goto done;
3161 err = (*a->progress_cb)(a->progress_arg,
3162 GOT_STATUS_CANNOT_DELETE, path1);
3163 goto done;
3164 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3165 err = (*a->progress_cb)(a->progress_arg,
3166 GOT_STATUS_CANNOT_DELETE, path1);
3167 goto done;
3169 err = (*a->progress_cb)(a->progress_arg,
3170 GOT_STATUS_DELETE, path1);
3171 if (err)
3172 goto done;
3173 err = remove_ondisk_file(a->worktree->root_path,
3174 path1);
3175 if (err)
3176 goto done;
3177 if (ie)
3178 got_fileindex_entry_mark_deleted_from_disk(ie);
3179 break;
3181 case GOT_STATUS_ADD: {
3182 FILE *blob1_f;
3183 off_t blob1_size;
3185 * Delete the file only if its content already
3186 * exists in the repository.
3188 err = got_object_blob_file_create(&id, &blob1_f,
3189 &blob1_size, path1);
3190 if (err)
3191 goto done;
3192 if (fclose(blob1_f) == EOF) {
3193 err = got_error_from_errno("fclose");
3194 goto done;
3196 if (got_object_id_cmp(id, id1) == 0) {
3197 err = (*a->progress_cb)(a->progress_arg,
3198 GOT_STATUS_DELETE, path1);
3199 if (err)
3200 goto done;
3201 err = remove_ondisk_file(a->worktree->root_path,
3202 path1);
3203 if (err)
3204 goto done;
3205 if (ie)
3206 got_fileindex_entry_remove(a->fileindex,
3207 ie);
3208 } else {
3209 err = (*a->progress_cb)(a->progress_arg,
3210 GOT_STATUS_CANNOT_DELETE, path1);
3212 if (err)
3213 goto done;
3214 break;
3216 case GOT_STATUS_CONFLICT:
3217 err = (*a->progress_cb)(a->progress_arg,
3218 GOT_STATUS_CANNOT_DELETE, path1);
3219 if (err)
3220 goto done;
3221 break;
3222 case GOT_STATUS_OBSTRUCTED:
3223 err = (*a->progress_cb)(a->progress_arg, status, path1);
3224 if (err)
3225 goto done;
3226 break;
3227 default:
3228 break;
3230 } else if (blob2) {
3231 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3232 path2) == -1)
3233 return got_error_from_errno("asprintf");
3234 ie = got_fileindex_entry_get(a->fileindex, path2,
3235 strlen(path2));
3236 if (ie) {
3237 err = get_file_status(&status, &sb, ie, ondisk_path,
3238 -1, NULL, repo);
3239 if (err)
3240 goto done;
3241 if (status != GOT_STATUS_NO_CHANGE &&
3242 status != GOT_STATUS_MODIFY &&
3243 status != GOT_STATUS_CONFLICT &&
3244 status != GOT_STATUS_ADD &&
3245 status != GOT_STATUS_DELETE) {
3246 err = (*a->progress_cb)(a->progress_arg,
3247 status, path2);
3248 goto done;
3250 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3251 char *link_target2;
3252 err = got_object_blob_read_to_str(&link_target2,
3253 blob2);
3254 if (err)
3255 goto done;
3256 err = merge_symlink(a->worktree, NULL,
3257 ondisk_path, path2, a->label_orig,
3258 link_target2, a->commit_id2, repo,
3259 a->progress_cb, a->progress_arg);
3260 free(link_target2);
3261 } else if (S_ISREG(sb.st_mode)) {
3262 err = merge_blob(&local_changes_subsumed,
3263 a->worktree, NULL, ondisk_path, path2,
3264 sb.st_mode, a->label_orig, blob2,
3265 a->commit_id2, repo, a->progress_cb,
3266 a->progress_arg);
3267 } else if (status != GOT_STATUS_DELETE) {
3268 err = got_error_path(ondisk_path,
3269 GOT_ERR_FILE_OBSTRUCTED);
3271 if (err)
3272 goto done;
3273 if (status == GOT_STATUS_DELETE) {
3274 /* Re-add file with content from new blob. */
3275 err = add_file(a->worktree, a->fileindex, ie,
3276 ondisk_path, path2, blob2, mode2,
3277 0, 0, 0, a->allow_bad_symlinks,
3278 repo, a->progress_cb, a->progress_arg);
3279 if (err)
3280 goto done;
3282 } else {
3283 err = add_file(a->worktree, a->fileindex, NULL,
3284 ondisk_path, path2, blob2, mode2,
3285 0, 0, 1, a->allow_bad_symlinks,
3286 repo, a->progress_cb, a->progress_arg);
3287 if (err)
3288 goto done;
3291 done:
3292 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3293 err = got_error_from_errno("fclose");
3294 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3295 err = got_error_from_errno("fclose");
3296 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3297 err = got_error_from_errno("fclose");
3298 free(id_str);
3299 free(id);
3300 free(label_deriv2);
3301 free(ondisk_path);
3302 return err;
3305 struct check_mixed_commits_args {
3306 struct got_worktree *worktree;
3307 got_cancel_cb cancel_cb;
3308 void *cancel_arg;
3311 static const struct got_error *
3312 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3314 const struct got_error *err = NULL;
3315 struct check_mixed_commits_args *a = arg;
3317 if (a->cancel_cb) {
3318 err = a->cancel_cb(a->cancel_arg);
3319 if (err)
3320 return err;
3323 /* Reject merges into a work tree with mixed base commits. */
3324 if (got_fileindex_entry_has_commit(ie) &&
3325 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3326 SHA1_DIGEST_LENGTH) != 0)
3327 return got_error(GOT_ERR_MIXED_COMMITS);
3329 return NULL;
3332 const struct got_error *
3333 got_worktree_get_state(char *state, struct got_repository *repo,
3334 struct got_worktree *worktree,
3335 got_cancel_cb cancel_cb, void *cancel_arg)
3337 const struct got_error *err;
3338 struct got_object_id *base_id, *head_id = NULL;
3339 struct got_reference *head_ref;
3340 struct got_fileindex *fileindex = NULL;
3341 char *fileindex_path = NULL;
3342 struct check_mixed_commits_args cma;
3344 if (worktree == NULL)
3345 return got_error(GOT_ERR_NOT_WORKTREE);
3347 err = got_ref_open(&head_ref, repo,
3348 got_worktree_get_head_ref_name(worktree), 0);
3349 if (err)
3350 return err;
3352 err = got_ref_resolve(&head_id, repo, head_ref);
3353 if (err)
3354 goto done;
3356 *state = GOT_WORKTREE_STATE_UNKNOWN;
3357 base_id = got_worktree_get_base_commit_id(worktree);
3359 cma.worktree = worktree;
3360 cma.cancel_cb = cancel_cb;
3361 cma.cancel_arg = cancel_arg;
3363 if (got_object_id_cmp(base_id, head_id) == 0) {
3364 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3365 if (err)
3366 goto done;
3368 err = got_fileindex_for_each_entry_safe(fileindex,
3369 check_mixed_commits, &cma);
3370 if (err == NULL)
3371 *state = GOT_WORKTREE_STATE_UPTODATE;
3372 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3373 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3374 err = NULL;
3376 } else
3377 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3379 done:
3380 free(head_id);
3381 free(fileindex_path);
3382 got_ref_close(head_ref);
3383 if (fileindex != NULL)
3384 got_fileindex_free(fileindex);
3385 return err;
3388 struct check_merge_conflicts_arg {
3389 struct got_worktree *worktree;
3390 struct got_fileindex *fileindex;
3391 struct got_repository *repo;
3394 static const struct got_error *
3395 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3396 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3397 struct got_object_id *id1, struct got_object_id *id2,
3398 const char *path1, const char *path2,
3399 mode_t mode1, mode_t mode2, struct got_repository *repo)
3401 const struct got_error *err = NULL;
3402 struct check_merge_conflicts_arg *a = arg;
3403 unsigned char status;
3404 struct stat sb;
3405 struct got_fileindex_entry *ie;
3406 const char *path = path2 ? path2 : path1;
3407 struct got_object_id *id = id2 ? id2 : id1;
3408 char *ondisk_path;
3410 if (id == NULL)
3411 return NULL;
3413 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3414 if (ie == NULL)
3415 return NULL;
3417 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3418 == -1)
3419 return got_error_from_errno("asprintf");
3421 /* Reject merges into a work tree with conflicted files. */
3422 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3423 free(ondisk_path);
3424 if (err)
3425 return err;
3426 if (status == GOT_STATUS_CONFLICT)
3427 return got_error(GOT_ERR_CONFLICTS);
3429 return NULL;
3432 static const struct got_error *
3433 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3434 const char *fileindex_path, struct got_object_id *commit_id1,
3435 struct got_object_id *commit_id2, struct got_repository *repo,
3436 got_worktree_checkout_cb progress_cb, void *progress_arg,
3437 got_cancel_cb cancel_cb, void *cancel_arg)
3439 const struct got_error *err = NULL, *sync_err;
3440 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3441 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3442 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3443 struct check_merge_conflicts_arg cmc_arg;
3444 struct merge_file_cb_arg arg;
3445 char *label_orig = NULL;
3446 FILE *f1 = NULL, *f2 = NULL;
3447 int fd1 = -1, fd2 = -1;
3449 if (commit_id1) {
3450 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3451 if (err)
3452 goto done;
3453 err = got_object_id_by_path(&tree_id1, repo, commit1,
3454 worktree->path_prefix);
3455 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3456 goto done;
3458 if (tree_id1) {
3459 char *id_str;
3461 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3462 if (err)
3463 goto done;
3465 err = got_object_id_str(&id_str, commit_id1);
3466 if (err)
3467 goto done;
3469 if (asprintf(&label_orig, "%s: commit %s",
3470 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3471 err = got_error_from_errno("asprintf");
3472 free(id_str);
3473 goto done;
3475 free(id_str);
3477 f1 = got_opentemp();
3478 if (f1 == NULL) {
3479 err = got_error_from_errno("got_opentemp");
3480 goto done;
3484 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3485 if (err)
3486 goto done;
3488 err = got_object_id_by_path(&tree_id2, repo, commit2,
3489 worktree->path_prefix);
3490 if (err)
3491 goto done;
3493 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3494 if (err)
3495 goto done;
3497 f2 = got_opentemp();
3498 if (f2 == NULL) {
3499 err = got_error_from_errno("got_opentemp");
3500 goto done;
3503 fd1 = got_opentempfd();
3504 if (fd1 == -1) {
3505 err = got_error_from_errno("got_opentempfd");
3506 goto done;
3509 fd2 = got_opentempfd();
3510 if (fd2 == -1) {
3511 err = got_error_from_errno("got_opentempfd");
3512 goto done;
3515 cmc_arg.worktree = worktree;
3516 cmc_arg.fileindex = fileindex;
3517 cmc_arg.repo = repo;
3518 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3519 check_merge_conflicts, &cmc_arg, 0);
3520 if (err)
3521 goto done;
3523 arg.worktree = worktree;
3524 arg.fileindex = fileindex;
3525 arg.progress_cb = progress_cb;
3526 arg.progress_arg = progress_arg;
3527 arg.cancel_cb = cancel_cb;
3528 arg.cancel_arg = cancel_arg;
3529 arg.label_orig = label_orig;
3530 arg.commit_id2 = commit_id2;
3531 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3532 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3533 merge_file_cb, &arg, 1);
3534 sync_err = sync_fileindex(fileindex, fileindex_path);
3535 if (sync_err && err == NULL)
3536 err = sync_err;
3537 done:
3538 if (commit1)
3539 got_object_commit_close(commit1);
3540 if (commit2)
3541 got_object_commit_close(commit2);
3542 if (tree1)
3543 got_object_tree_close(tree1);
3544 if (tree2)
3545 got_object_tree_close(tree2);
3546 if (f1 && fclose(f1) == EOF && err == NULL)
3547 err = got_error_from_errno("fclose");
3548 if (f2 && fclose(f2) == EOF && err == NULL)
3549 err = got_error_from_errno("fclose");
3550 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3551 err = got_error_from_errno("close");
3552 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3553 err = got_error_from_errno("close");
3554 free(label_orig);
3555 return err;
3558 const struct got_error *
3559 got_worktree_merge_files(struct got_worktree *worktree,
3560 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3561 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3562 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3564 const struct got_error *err, *unlockerr;
3565 char *fileindex_path = NULL;
3566 struct got_fileindex *fileindex = NULL;
3567 struct check_mixed_commits_args cma;
3569 err = lock_worktree(worktree, LOCK_EX);
3570 if (err)
3571 return err;
3573 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3574 if (err)
3575 goto done;
3577 cma.worktree = worktree;
3578 cma.cancel_cb = cancel_cb;
3579 cma.cancel_arg = cancel_arg;
3581 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3582 &cma);
3583 if (err)
3584 goto done;
3586 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3587 commit_id2, repo, progress_cb, progress_arg,
3588 cancel_cb, cancel_arg);
3589 done:
3590 if (fileindex)
3591 got_fileindex_free(fileindex);
3592 free(fileindex_path);
3593 unlockerr = lock_worktree(worktree, LOCK_SH);
3594 if (unlockerr && err == NULL)
3595 err = unlockerr;
3596 return err;
3599 struct diff_dir_cb_arg {
3600 struct got_fileindex *fileindex;
3601 struct got_worktree *worktree;
3602 const char *status_path;
3603 size_t status_path_len;
3604 struct got_repository *repo;
3605 got_worktree_status_cb status_cb;
3606 void *status_arg;
3607 got_cancel_cb cancel_cb;
3608 void *cancel_arg;
3609 /* A pathlist containing per-directory pathlists of ignore patterns. */
3610 struct got_pathlist_head *ignores;
3611 int report_unchanged;
3612 int no_ignores;
3615 static const struct got_error *
3616 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3617 int dirfd, const char *de_name,
3618 got_worktree_status_cb status_cb, void *status_arg,
3619 struct got_repository *repo, int report_unchanged)
3621 const struct got_error *err = NULL;
3622 unsigned char status = GOT_STATUS_NO_CHANGE;
3623 unsigned char staged_status;
3624 struct stat sb;
3625 struct got_object_id blob_id, commit_id, staged_blob_id;
3626 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3627 struct got_object_id *staged_blob_idp = NULL;
3629 staged_status = get_staged_status(ie);
3630 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3631 if (err)
3632 return err;
3634 if (status == GOT_STATUS_NO_CHANGE &&
3635 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3636 return NULL;
3638 if (got_fileindex_entry_has_blob(ie))
3639 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3640 if (got_fileindex_entry_has_commit(ie))
3641 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3642 if (staged_status == GOT_STATUS_ADD ||
3643 staged_status == GOT_STATUS_MODIFY) {
3644 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3645 &staged_blob_id, ie);
3648 return (*status_cb)(status_arg, status, staged_status,
3649 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3652 static const struct got_error *
3653 status_old_new(void *arg, struct got_fileindex_entry *ie,
3654 struct dirent *de, const char *parent_path, int dirfd)
3656 const struct got_error *err = NULL;
3657 struct diff_dir_cb_arg *a = arg;
3658 char *abspath;
3660 if (a->cancel_cb) {
3661 err = a->cancel_cb(a->cancel_arg);
3662 if (err)
3663 return err;
3666 if (got_path_cmp(parent_path, a->status_path,
3667 strlen(parent_path), a->status_path_len) != 0 &&
3668 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3669 return NULL;
3671 if (parent_path[0]) {
3672 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3673 parent_path, de->d_name) == -1)
3674 return got_error_from_errno("asprintf");
3675 } else {
3676 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3677 de->d_name) == -1)
3678 return got_error_from_errno("asprintf");
3681 err = report_file_status(ie, abspath, dirfd, de->d_name,
3682 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3683 free(abspath);
3684 return err;
3687 static const struct got_error *
3688 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3690 const struct got_error *err;
3691 struct diff_dir_cb_arg *a = arg;
3692 struct got_object_id blob_id, commit_id;
3693 unsigned char status;
3695 if (a->cancel_cb) {
3696 err = a->cancel_cb(a->cancel_arg);
3697 if (err)
3698 return err;
3701 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3702 return NULL;
3704 got_fileindex_entry_get_blob_id(&blob_id, ie);
3705 got_fileindex_entry_get_commit_id(&commit_id, ie);
3706 if (got_fileindex_entry_has_file_on_disk(ie))
3707 status = GOT_STATUS_MISSING;
3708 else
3709 status = GOT_STATUS_DELETE;
3710 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3711 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3714 static void
3715 free_ignores(struct got_pathlist_head *ignores)
3717 struct got_pathlist_entry *pe;
3719 TAILQ_FOREACH(pe, ignores, entry) {
3720 struct got_pathlist_head *ignorelist = pe->data;
3722 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3724 got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3727 static const struct got_error *
3728 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3730 const struct got_error *err = NULL;
3731 struct got_pathlist_entry *pe = NULL;
3732 struct got_pathlist_head *ignorelist;
3733 char *line = NULL, *pattern, *dirpath = NULL;
3734 size_t linesize = 0;
3735 ssize_t linelen;
3737 ignorelist = calloc(1, sizeof(*ignorelist));
3738 if (ignorelist == NULL)
3739 return got_error_from_errno("calloc");
3740 TAILQ_INIT(ignorelist);
3742 while ((linelen = getline(&line, &linesize, f)) != -1) {
3743 if (linelen > 0 && line[linelen - 1] == '\n')
3744 line[linelen - 1] = '\0';
3746 /* Git's ignores may contain comments. */
3747 if (line[0] == '#')
3748 continue;
3750 /* Git's negated patterns are not (yet?) supported. */
3751 if (line[0] == '!')
3752 continue;
3754 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3755 line) == -1) {
3756 err = got_error_from_errno("asprintf");
3757 goto done;
3759 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3760 if (err)
3761 goto done;
3763 if (ferror(f)) {
3764 err = got_error_from_errno("getline");
3765 goto done;
3768 dirpath = strdup(path);
3769 if (dirpath == NULL) {
3770 err = got_error_from_errno("strdup");
3771 goto done;
3773 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3774 done:
3775 free(line);
3776 if (err || pe == NULL) {
3777 free(dirpath);
3778 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3779 free(ignorelist);
3781 return err;
3784 static int
3785 match_path(const char *pattern, size_t pattern_len, const char *path,
3786 int flags)
3788 char buf[PATH_MAX];
3791 * Trailing slashes signify directories.
3792 * Append a * to make such patterns conform to fnmatch rules.
3794 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3795 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3796 return FNM_NOMATCH; /* XXX */
3798 return fnmatch(buf, path, flags);
3801 return fnmatch(pattern, path, flags);
3804 static int
3805 match_ignores(struct got_pathlist_head *ignores, const char *path)
3807 struct got_pathlist_entry *pe;
3809 /* Handle patterns which match in all directories. */
3810 TAILQ_FOREACH(pe, ignores, entry) {
3811 struct got_pathlist_head *ignorelist = pe->data;
3812 struct got_pathlist_entry *pi;
3814 TAILQ_FOREACH(pi, ignorelist, entry) {
3815 const char *p;
3817 if (pi->path_len < 3 ||
3818 strncmp(pi->path, "**/", 3) != 0)
3819 continue;
3820 p = path;
3821 while (*p) {
3822 if (match_path(pi->path + 3,
3823 pi->path_len - 3, p,
3824 FNM_PATHNAME | FNM_LEADING_DIR)) {
3825 /* Retry in next directory. */
3826 while (*p && *p != '/')
3827 p++;
3828 while (*p == '/')
3829 p++;
3830 continue;
3832 return 1;
3838 * The ignores pathlist contains ignore lists from children before
3839 * parents, so we can find the most specific ignorelist by walking
3840 * ignores backwards.
3842 pe = TAILQ_LAST(ignores, got_pathlist_head);
3843 while (pe) {
3844 if (got_path_is_child(path, pe->path, pe->path_len)) {
3845 struct got_pathlist_head *ignorelist = pe->data;
3846 struct got_pathlist_entry *pi;
3847 TAILQ_FOREACH(pi, ignorelist, entry) {
3848 int flags = FNM_LEADING_DIR;
3849 if (strstr(pi->path, "/**/") == NULL)
3850 flags |= FNM_PATHNAME;
3851 if (match_path(pi->path, pi->path_len,
3852 path, flags))
3853 continue;
3854 return 1;
3857 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3860 return 0;
3863 static const struct got_error *
3864 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3865 const char *path, int dirfd, const char *ignores_filename)
3867 const struct got_error *err = NULL;
3868 char *ignorespath;
3869 int fd = -1;
3870 FILE *ignoresfile = NULL;
3872 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3873 path[0] ? "/" : "", ignores_filename) == -1)
3874 return got_error_from_errno("asprintf");
3876 if (dirfd != -1) {
3877 fd = openat(dirfd, ignores_filename,
3878 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3879 if (fd == -1) {
3880 if (errno != ENOENT && errno != EACCES)
3881 err = got_error_from_errno2("openat",
3882 ignorespath);
3883 } else {
3884 ignoresfile = fdopen(fd, "r");
3885 if (ignoresfile == NULL)
3886 err = got_error_from_errno2("fdopen",
3887 ignorespath);
3888 else {
3889 fd = -1;
3890 err = read_ignores(ignores, path, ignoresfile);
3893 } else {
3894 ignoresfile = fopen(ignorespath, "re");
3895 if (ignoresfile == NULL) {
3896 if (errno != ENOENT && errno != EACCES)
3897 err = got_error_from_errno2("fopen",
3898 ignorespath);
3899 } else
3900 err = read_ignores(ignores, path, ignoresfile);
3903 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3904 err = got_error_from_errno2("fclose", path);
3905 if (fd != -1 && close(fd) == -1 && err == NULL)
3906 err = got_error_from_errno2("close", path);
3907 free(ignorespath);
3908 return err;
3911 static const struct got_error *
3912 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3913 int dirfd)
3915 const struct got_error *err = NULL;
3916 struct diff_dir_cb_arg *a = arg;
3917 char *path = NULL;
3919 if (ignore != NULL)
3920 *ignore = 0;
3922 if (a->cancel_cb) {
3923 err = a->cancel_cb(a->cancel_arg);
3924 if (err)
3925 return err;
3928 if (parent_path[0]) {
3929 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3930 return got_error_from_errno("asprintf");
3931 } else {
3932 path = de->d_name;
3935 if (de->d_type == DT_DIR) {
3936 if (!a->no_ignores && ignore != NULL &&
3937 match_ignores(a->ignores, path))
3938 *ignore = 1;
3939 } else if (!match_ignores(a->ignores, path) &&
3940 got_path_is_child(path, a->status_path, a->status_path_len))
3941 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3942 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3943 if (parent_path[0])
3944 free(path);
3945 return err;
3948 static const struct got_error *
3949 status_traverse(void *arg, const char *path, int dirfd)
3951 const struct got_error *err = NULL;
3952 struct diff_dir_cb_arg *a = arg;
3954 if (a->no_ignores)
3955 return NULL;
3957 err = add_ignores(a->ignores, a->worktree->root_path,
3958 path, dirfd, ".cvsignore");
3959 if (err)
3960 return err;
3962 err = add_ignores(a->ignores, a->worktree->root_path, path,
3963 dirfd, ".gitignore");
3965 return err;
3968 static const struct got_error *
3969 report_single_file_status(const char *path, const char *ondisk_path,
3970 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3971 void *status_arg, struct got_repository *repo, int report_unchanged,
3972 struct got_pathlist_head *ignores, int no_ignores)
3974 struct got_fileindex_entry *ie;
3975 struct stat sb;
3977 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3978 if (ie)
3979 return report_file_status(ie, ondisk_path, -1, NULL,
3980 status_cb, status_arg, repo, report_unchanged);
3982 if (lstat(ondisk_path, &sb) == -1) {
3983 if (errno != ENOENT)
3984 return got_error_from_errno2("lstat", ondisk_path);
3985 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3986 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3989 if (!no_ignores && match_ignores(ignores, path))
3990 return NULL;
3992 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3993 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3994 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3996 return NULL;
3999 static const struct got_error *
4000 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
4001 const char *root_path, const char *path)
4003 const struct got_error *err;
4004 char *parent_path, *next_parent_path = NULL;
4006 err = add_ignores(ignores, root_path, "", -1,
4007 ".cvsignore");
4008 if (err)
4009 return err;
4011 err = add_ignores(ignores, root_path, "", -1,
4012 ".gitignore");
4013 if (err)
4014 return err;
4016 err = got_path_dirname(&parent_path, path);
4017 if (err) {
4018 if (err->code == GOT_ERR_BAD_PATH)
4019 return NULL; /* cannot traverse parent */
4020 return err;
4022 for (;;) {
4023 err = add_ignores(ignores, root_path, parent_path, -1,
4024 ".cvsignore");
4025 if (err)
4026 break;
4027 err = add_ignores(ignores, root_path, parent_path, -1,
4028 ".gitignore");
4029 if (err)
4030 break;
4031 err = got_path_dirname(&next_parent_path, parent_path);
4032 if (err) {
4033 if (err->code == GOT_ERR_BAD_PATH)
4034 err = NULL; /* traversed everything */
4035 break;
4037 if (got_path_is_root_dir(parent_path))
4038 break;
4039 free(parent_path);
4040 parent_path = next_parent_path;
4041 next_parent_path = NULL;
4044 free(parent_path);
4045 free(next_parent_path);
4046 return err;
4049 struct find_missing_children_args {
4050 const char *parent_path;
4051 size_t parent_len;
4052 struct got_pathlist_head *children;
4053 got_cancel_cb cancel_cb;
4054 void *cancel_arg;
4057 static const struct got_error *
4058 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4060 const struct got_error *err = NULL;
4061 struct find_missing_children_args *a = arg;
4063 if (a->cancel_cb) {
4064 err = a->cancel_cb(a->cancel_arg);
4065 if (err)
4066 return err;
4069 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4070 err = got_pathlist_append(a->children, ie->path, NULL);
4072 return err;
4075 static const struct got_error *
4076 report_children(struct got_pathlist_head *children,
4077 struct got_worktree *worktree, struct got_fileindex *fileindex,
4078 struct got_repository *repo, int is_root_dir, int report_unchanged,
4079 struct got_pathlist_head *ignores, int no_ignores,
4080 got_worktree_status_cb status_cb, void *status_arg,
4081 got_cancel_cb cancel_cb, void *cancel_arg)
4083 const struct got_error *err = NULL;
4084 struct got_pathlist_entry *pe;
4085 char *ondisk_path = NULL;
4087 TAILQ_FOREACH(pe, children, entry) {
4088 if (cancel_cb) {
4089 err = cancel_cb(cancel_arg);
4090 if (err)
4091 break;
4094 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4095 !is_root_dir ? "/" : "", pe->path) == -1) {
4096 err = got_error_from_errno("asprintf");
4097 ondisk_path = NULL;
4098 break;
4101 err = report_single_file_status(pe->path, ondisk_path,
4102 fileindex, status_cb, status_arg, repo, report_unchanged,
4103 ignores, no_ignores);
4104 if (err)
4105 break;
4107 free(ondisk_path);
4108 ondisk_path = NULL;
4111 free(ondisk_path);
4112 return err;
4115 static const struct got_error *
4116 worktree_status(struct got_worktree *worktree, const char *path,
4117 struct got_fileindex *fileindex, struct got_repository *repo,
4118 got_worktree_status_cb status_cb, void *status_arg,
4119 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4120 int report_unchanged)
4122 const struct got_error *err = NULL;
4123 int fd = -1;
4124 struct got_fileindex_diff_dir_cb fdiff_cb;
4125 struct diff_dir_cb_arg arg;
4126 char *ondisk_path = NULL;
4127 struct got_pathlist_head ignores, missing_children;
4128 struct got_fileindex_entry *ie;
4130 TAILQ_INIT(&ignores);
4131 TAILQ_INIT(&missing_children);
4133 if (asprintf(&ondisk_path, "%s%s%s",
4134 worktree->root_path, path[0] ? "/" : "", path) == -1)
4135 return got_error_from_errno("asprintf");
4137 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4138 if (ie) {
4139 err = report_single_file_status(path, ondisk_path,
4140 fileindex, status_cb, status_arg, repo,
4141 report_unchanged, &ignores, no_ignores);
4142 goto done;
4143 } else {
4144 struct find_missing_children_args fmca;
4145 fmca.parent_path = path;
4146 fmca.parent_len = strlen(path);
4147 fmca.children = &missing_children;
4148 fmca.cancel_cb = cancel_cb;
4149 fmca.cancel_arg = cancel_arg;
4150 err = got_fileindex_for_each_entry_safe(fileindex,
4151 find_missing_children, &fmca);
4152 if (err)
4153 goto done;
4156 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4157 if (fd == -1) {
4158 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4159 !got_err_open_nofollow_on_symlink())
4160 err = got_error_from_errno2("open", ondisk_path);
4161 else {
4162 if (!no_ignores) {
4163 err = add_ignores_from_parent_paths(&ignores,
4164 worktree->root_path, ondisk_path);
4165 if (err)
4166 goto done;
4168 if (TAILQ_EMPTY(&missing_children)) {
4169 err = report_single_file_status(path,
4170 ondisk_path, fileindex,
4171 status_cb, status_arg, repo,
4172 report_unchanged, &ignores, no_ignores);
4173 if (err)
4174 goto done;
4175 } else {
4176 err = report_children(&missing_children,
4177 worktree, fileindex, repo,
4178 (path[0] == '\0'), report_unchanged,
4179 &ignores, no_ignores,
4180 status_cb, status_arg,
4181 cancel_cb, cancel_arg);
4182 if (err)
4183 goto done;
4186 } else {
4187 fdiff_cb.diff_old_new = status_old_new;
4188 fdiff_cb.diff_old = status_old;
4189 fdiff_cb.diff_new = status_new;
4190 fdiff_cb.diff_traverse = status_traverse;
4191 arg.fileindex = fileindex;
4192 arg.worktree = worktree;
4193 arg.status_path = path;
4194 arg.status_path_len = strlen(path);
4195 arg.repo = repo;
4196 arg.status_cb = status_cb;
4197 arg.status_arg = status_arg;
4198 arg.cancel_cb = cancel_cb;
4199 arg.cancel_arg = cancel_arg;
4200 arg.report_unchanged = report_unchanged;
4201 arg.no_ignores = no_ignores;
4202 if (!no_ignores) {
4203 err = add_ignores_from_parent_paths(&ignores,
4204 worktree->root_path, path);
4205 if (err)
4206 goto done;
4208 arg.ignores = &ignores;
4209 err = got_fileindex_diff_dir(fileindex, fd,
4210 worktree->root_path, path, repo, &fdiff_cb, &arg);
4212 done:
4213 free_ignores(&ignores);
4214 got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4215 if (fd != -1 && close(fd) == -1 && err == NULL)
4216 err = got_error_from_errno("close");
4217 free(ondisk_path);
4218 return err;
4221 const struct got_error *
4222 got_worktree_status(struct got_worktree *worktree,
4223 struct got_pathlist_head *paths, struct got_repository *repo,
4224 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4225 got_cancel_cb cancel_cb, void *cancel_arg)
4227 const struct got_error *err = NULL;
4228 char *fileindex_path = NULL;
4229 struct got_fileindex *fileindex = NULL;
4230 struct got_pathlist_entry *pe;
4232 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4233 if (err)
4234 return err;
4236 TAILQ_FOREACH(pe, paths, entry) {
4237 err = worktree_status(worktree, pe->path, fileindex, repo,
4238 status_cb, status_arg, cancel_cb, cancel_arg,
4239 no_ignores, 0);
4240 if (err)
4241 break;
4243 free(fileindex_path);
4244 got_fileindex_free(fileindex);
4245 return err;
4248 const struct got_error *
4249 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4250 const char *arg)
4252 const struct got_error *err = NULL;
4253 char *resolved = NULL, *cwd = NULL, *path = NULL;
4254 size_t len;
4255 struct stat sb;
4256 char *abspath = NULL;
4257 char canonpath[PATH_MAX];
4259 *wt_path = NULL;
4261 cwd = getcwd(NULL, 0);
4262 if (cwd == NULL)
4263 return got_error_from_errno("getcwd");
4265 if (lstat(arg, &sb) == -1) {
4266 if (errno != ENOENT) {
4267 err = got_error_from_errno2("lstat", arg);
4268 goto done;
4270 sb.st_mode = 0;
4272 if (S_ISLNK(sb.st_mode)) {
4274 * We cannot use realpath(3) with symlinks since we want to
4275 * operate on the symlink itself.
4276 * But we can make the path absolute, assuming it is relative
4277 * to the current working directory, and then canonicalize it.
4279 if (!got_path_is_absolute(arg)) {
4280 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4281 err = got_error_from_errno("asprintf");
4282 goto done;
4286 err = got_canonpath(abspath ? abspath : arg, canonpath,
4287 sizeof(canonpath));
4288 if (err)
4289 goto done;
4290 resolved = strdup(canonpath);
4291 if (resolved == NULL) {
4292 err = got_error_from_errno("strdup");
4293 goto done;
4295 } else {
4296 resolved = realpath(arg, NULL);
4297 if (resolved == NULL) {
4298 if (errno != ENOENT) {
4299 err = got_error_from_errno2("realpath", arg);
4300 goto done;
4302 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4303 err = got_error_from_errno("asprintf");
4304 goto done;
4306 err = got_canonpath(abspath, canonpath,
4307 sizeof(canonpath));
4308 if (err)
4309 goto done;
4310 resolved = strdup(canonpath);
4311 if (resolved == NULL) {
4312 err = got_error_from_errno("strdup");
4313 goto done;
4318 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4319 strlen(got_worktree_get_root_path(worktree)))) {
4320 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4321 goto done;
4324 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4325 err = got_path_skip_common_ancestor(&path,
4326 got_worktree_get_root_path(worktree), resolved);
4327 if (err)
4328 goto done;
4329 } else {
4330 path = strdup("");
4331 if (path == NULL) {
4332 err = got_error_from_errno("strdup");
4333 goto done;
4337 /* XXX status walk can't deal with trailing slash! */
4338 len = strlen(path);
4339 while (len > 0 && path[len - 1] == '/') {
4340 path[len - 1] = '\0';
4341 len--;
4343 done:
4344 free(abspath);
4345 free(resolved);
4346 free(cwd);
4347 if (err == NULL)
4348 *wt_path = path;
4349 else
4350 free(path);
4351 return err;
4354 struct schedule_addition_args {
4355 struct got_worktree *worktree;
4356 struct got_fileindex *fileindex;
4357 got_worktree_checkout_cb progress_cb;
4358 void *progress_arg;
4359 struct got_repository *repo;
4362 static int
4363 add_noop_status(unsigned char status)
4365 return (status == GOT_STATUS_ADD ||
4366 status == GOT_STATUS_MODIFY ||
4367 status == GOT_STATUS_CONFLICT ||
4368 status == GOT_STATUS_MODE_CHANGE ||
4369 status == GOT_STATUS_NO_CHANGE);
4372 static const struct got_error *
4373 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4374 const char *relpath, struct got_object_id *blob_id,
4375 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4376 int dirfd, const char *de_name)
4378 struct schedule_addition_args *a = arg;
4379 const struct got_error *err = NULL;
4380 struct got_fileindex_entry *ie;
4381 struct stat sb;
4382 char *ondisk_path;
4384 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4385 relpath) == -1)
4386 return got_error_from_errno("asprintf");
4388 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4389 if (ie) {
4390 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4391 de_name, a->repo);
4392 if (err)
4393 goto done;
4394 /* Re-adding an existing entry is a no-op. */
4395 if (staged_status == GOT_STATUS_NO_CHANGE &&
4396 add_noop_status(status))
4397 goto done;
4398 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4399 if (err)
4400 goto done;
4403 if (status != GOT_STATUS_UNVERSIONED) {
4404 if (status == GOT_STATUS_NONEXISTENT)
4405 err = got_error_set_errno(ENOENT, ondisk_path);
4406 else
4407 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4408 goto done;
4411 err = got_fileindex_entry_alloc(&ie, relpath);
4412 if (err)
4413 goto done;
4414 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4415 relpath, NULL, NULL, 1);
4416 if (err) {
4417 got_fileindex_entry_free(ie);
4418 goto done;
4420 err = got_fileindex_entry_add(a->fileindex, ie);
4421 if (err) {
4422 got_fileindex_entry_free(ie);
4423 goto done;
4425 done:
4426 free(ondisk_path);
4427 if (err)
4428 return err;
4429 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4430 return NULL;
4431 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4434 const struct got_error *
4435 got_worktree_schedule_add(struct got_worktree *worktree,
4436 struct got_pathlist_head *paths,
4437 got_worktree_checkout_cb progress_cb, void *progress_arg,
4438 struct got_repository *repo, int no_ignores)
4440 struct got_fileindex *fileindex = NULL;
4441 char *fileindex_path = NULL;
4442 const struct got_error *err = NULL, *sync_err, *unlockerr;
4443 struct got_pathlist_entry *pe;
4444 struct schedule_addition_args saa;
4446 err = lock_worktree(worktree, LOCK_EX);
4447 if (err)
4448 return err;
4450 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4451 if (err)
4452 goto done;
4454 saa.worktree = worktree;
4455 saa.fileindex = fileindex;
4456 saa.progress_cb = progress_cb;
4457 saa.progress_arg = progress_arg;
4458 saa.repo = repo;
4460 TAILQ_FOREACH(pe, paths, entry) {
4461 err = worktree_status(worktree, pe->path, fileindex, repo,
4462 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4463 if (err)
4464 break;
4466 sync_err = sync_fileindex(fileindex, fileindex_path);
4467 if (sync_err && err == NULL)
4468 err = sync_err;
4469 done:
4470 free(fileindex_path);
4471 if (fileindex)
4472 got_fileindex_free(fileindex);
4473 unlockerr = lock_worktree(worktree, LOCK_SH);
4474 if (unlockerr && err == NULL)
4475 err = unlockerr;
4476 return err;
4479 struct schedule_deletion_args {
4480 struct got_worktree *worktree;
4481 struct got_fileindex *fileindex;
4482 got_worktree_delete_cb progress_cb;
4483 void *progress_arg;
4484 struct got_repository *repo;
4485 int delete_local_mods;
4486 int keep_on_disk;
4487 int ignore_missing_paths;
4488 const char *status_path;
4489 size_t status_path_len;
4490 const char *status_codes;
4493 static const struct got_error *
4494 schedule_for_deletion(void *arg, unsigned char status,
4495 unsigned char staged_status, const char *relpath,
4496 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4497 struct got_object_id *commit_id, int dirfd, const char *de_name)
4499 struct schedule_deletion_args *a = arg;
4500 const struct got_error *err = NULL;
4501 struct got_fileindex_entry *ie = NULL;
4502 struct stat sb;
4503 char *ondisk_path;
4505 if (status == GOT_STATUS_NONEXISTENT) {
4506 if (a->ignore_missing_paths)
4507 return NULL;
4508 return got_error_set_errno(ENOENT, relpath);
4511 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4512 if (ie == NULL)
4513 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4515 staged_status = get_staged_status(ie);
4516 if (staged_status != GOT_STATUS_NO_CHANGE) {
4517 if (staged_status == GOT_STATUS_DELETE)
4518 return NULL;
4519 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4522 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4523 relpath) == -1)
4524 return got_error_from_errno("asprintf");
4526 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4527 a->repo);
4528 if (err)
4529 goto done;
4531 if (a->status_codes) {
4532 size_t ncodes = strlen(a->status_codes);
4533 int i;
4534 for (i = 0; i < ncodes ; i++) {
4535 if (status == a->status_codes[i])
4536 break;
4538 if (i == ncodes) {
4539 /* Do not delete files in non-matching status. */
4540 free(ondisk_path);
4541 return NULL;
4543 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4544 a->status_codes[i] != GOT_STATUS_MISSING) {
4545 static char msg[64];
4546 snprintf(msg, sizeof(msg),
4547 "invalid status code '%c'", a->status_codes[i]);
4548 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4549 goto done;
4553 if (status != GOT_STATUS_NO_CHANGE) {
4554 if (status == GOT_STATUS_DELETE)
4555 goto done;
4556 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4557 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4558 goto done;
4560 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4561 err = got_error_set_errno(ENOENT, relpath);
4562 goto done;
4564 if (status != GOT_STATUS_MODIFY &&
4565 status != GOT_STATUS_MISSING) {
4566 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4567 goto done;
4571 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4572 size_t root_len;
4574 if (dirfd != -1) {
4575 if (unlinkat(dirfd, de_name, 0) == -1) {
4576 err = got_error_from_errno2("unlinkat",
4577 ondisk_path);
4578 goto done;
4580 } else if (unlink(ondisk_path) == -1) {
4581 err = got_error_from_errno2("unlink", ondisk_path);
4582 goto done;
4585 root_len = strlen(a->worktree->root_path);
4586 do {
4587 char *parent;
4589 err = got_path_dirname(&parent, ondisk_path);
4590 if (err)
4591 goto done;
4592 free(ondisk_path);
4593 ondisk_path = parent;
4594 if (got_path_cmp(ondisk_path, a->status_path,
4595 strlen(ondisk_path), a->status_path_len) != 0 &&
4596 !got_path_is_child(ondisk_path, a->status_path,
4597 a->status_path_len))
4598 break;
4599 if (rmdir(ondisk_path) == -1) {
4600 if (errno != ENOTEMPTY)
4601 err = got_error_from_errno2("rmdir",
4602 ondisk_path);
4603 break;
4605 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4606 strlen(ondisk_path), root_len) != 0);
4609 if (got_fileindex_entry_has_blob(ie))
4610 got_fileindex_entry_mark_deleted_from_disk(ie);
4611 else
4612 got_fileindex_entry_remove(a->fileindex, ie);
4613 done:
4614 free(ondisk_path);
4615 if (err)
4616 return err;
4617 if (status == GOT_STATUS_DELETE)
4618 return NULL;
4619 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4620 staged_status, relpath);
4623 const struct got_error *
4624 got_worktree_schedule_delete(struct got_worktree *worktree,
4625 struct got_pathlist_head *paths, int delete_local_mods,
4626 const char *status_codes,
4627 got_worktree_delete_cb progress_cb, void *progress_arg,
4628 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4630 struct got_fileindex *fileindex = NULL;
4631 char *fileindex_path = NULL;
4632 const struct got_error *err = NULL, *sync_err, *unlockerr;
4633 struct got_pathlist_entry *pe;
4634 struct schedule_deletion_args sda;
4636 err = lock_worktree(worktree, LOCK_EX);
4637 if (err)
4638 return err;
4640 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4641 if (err)
4642 goto done;
4644 sda.worktree = worktree;
4645 sda.fileindex = fileindex;
4646 sda.progress_cb = progress_cb;
4647 sda.progress_arg = progress_arg;
4648 sda.repo = repo;
4649 sda.delete_local_mods = delete_local_mods;
4650 sda.keep_on_disk = keep_on_disk;
4651 sda.ignore_missing_paths = ignore_missing_paths;
4652 sda.status_codes = status_codes;
4654 TAILQ_FOREACH(pe, paths, entry) {
4655 char *ondisk_status_path;
4657 if (asprintf(&ondisk_status_path, "%s%s%s",
4658 got_worktree_get_root_path(worktree),
4659 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4660 err = got_error_from_errno("asprintf");
4661 goto done;
4663 sda.status_path = ondisk_status_path;
4664 sda.status_path_len = strlen(ondisk_status_path);
4665 err = worktree_status(worktree, pe->path, fileindex, repo,
4666 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4667 free(ondisk_status_path);
4668 if (err)
4669 break;
4671 sync_err = sync_fileindex(fileindex, fileindex_path);
4672 if (sync_err && err == NULL)
4673 err = sync_err;
4674 done:
4675 free(fileindex_path);
4676 if (fileindex)
4677 got_fileindex_free(fileindex);
4678 unlockerr = lock_worktree(worktree, LOCK_SH);
4679 if (unlockerr && err == NULL)
4680 err = unlockerr;
4681 return err;
4684 static const struct got_error *
4685 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4687 const struct got_error *err = NULL;
4688 char *line = NULL;
4689 size_t linesize = 0, n;
4690 ssize_t linelen;
4692 linelen = getline(&line, &linesize, infile);
4693 if (linelen == -1) {
4694 if (ferror(infile)) {
4695 err = got_error_from_errno("getline");
4696 goto done;
4698 return NULL;
4700 if (outfile) {
4701 n = fwrite(line, 1, linelen, outfile);
4702 if (n != linelen) {
4703 err = got_ferror(outfile, GOT_ERR_IO);
4704 goto done;
4707 if (rejectfile) {
4708 n = fwrite(line, 1, linelen, rejectfile);
4709 if (n != linelen)
4710 err = got_ferror(rejectfile, GOT_ERR_IO);
4712 done:
4713 free(line);
4714 return err;
4717 static const struct got_error *
4718 skip_one_line(FILE *f)
4720 char *line = NULL;
4721 size_t linesize = 0;
4722 ssize_t linelen;
4724 linelen = getline(&line, &linesize, f);
4725 if (linelen == -1) {
4726 if (ferror(f))
4727 return got_error_from_errno("getline");
4728 return NULL;
4730 free(line);
4731 return NULL;
4734 static const struct got_error *
4735 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4736 int start_old, int end_old, int start_new, int end_new,
4737 FILE *outfile, FILE *rejectfile)
4739 const struct got_error *err;
4741 /* Copy old file's lines leading up to patch. */
4742 while (!feof(f1) && *line_cur1 < start_old) {
4743 err = copy_one_line(f1, outfile, NULL);
4744 if (err)
4745 return err;
4746 (*line_cur1)++;
4748 /* Skip new file's lines leading up to patch. */
4749 while (!feof(f2) && *line_cur2 < start_new) {
4750 if (rejectfile)
4751 err = copy_one_line(f2, NULL, rejectfile);
4752 else
4753 err = skip_one_line(f2);
4754 if (err)
4755 return err;
4756 (*line_cur2)++;
4758 /* Copy patched lines. */
4759 while (!feof(f2) && *line_cur2 <= end_new) {
4760 err = copy_one_line(f2, outfile, NULL);
4761 if (err)
4762 return err;
4763 (*line_cur2)++;
4765 /* Skip over old file's replaced lines. */
4766 while (!feof(f1) && *line_cur1 <= end_old) {
4767 if (rejectfile)
4768 err = copy_one_line(f1, NULL, rejectfile);
4769 else
4770 err = skip_one_line(f1);
4771 if (err)
4772 return err;
4773 (*line_cur1)++;
4776 return NULL;
4779 static const struct got_error *
4780 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4781 FILE *outfile, FILE *rejectfile)
4783 const struct got_error *err;
4785 if (outfile) {
4786 /* Copy old file's lines until EOF. */
4787 while (!feof(f1)) {
4788 err = copy_one_line(f1, outfile, NULL);
4789 if (err)
4790 return err;
4791 (*line_cur1)++;
4794 if (rejectfile) {
4795 /* Copy new file's lines until EOF. */
4796 while (!feof(f2)) {
4797 err = copy_one_line(f2, NULL, rejectfile);
4798 if (err)
4799 return err;
4800 (*line_cur2)++;
4804 return NULL;
4807 static const struct got_error *
4808 apply_or_reject_change(int *choice, int *nchunks_used,
4809 struct diff_result *diff_result, int n,
4810 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4811 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4812 got_worktree_patch_cb patch_cb, void *patch_arg)
4814 const struct got_error *err = NULL;
4815 struct diff_chunk_context cc = {};
4816 int start_old, end_old, start_new, end_new;
4817 FILE *hunkfile;
4818 struct diff_output_unidiff_state *diff_state;
4819 struct diff_input_info diff_info;
4820 int rc;
4822 *choice = GOT_PATCH_CHOICE_NONE;
4824 /* Get changed line numbers without context lines for copy_change(). */
4825 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4826 start_old = cc.left.start;
4827 end_old = cc.left.end;
4828 start_new = cc.right.start;
4829 end_new = cc.right.end;
4831 /* Get the same change with context lines for display. */
4832 memset(&cc, 0, sizeof(cc));
4833 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4835 memset(&diff_info, 0, sizeof(diff_info));
4836 diff_info.left_path = relpath;
4837 diff_info.right_path = relpath;
4839 diff_state = diff_output_unidiff_state_alloc();
4840 if (diff_state == NULL)
4841 return got_error_set_errno(ENOMEM,
4842 "diff_output_unidiff_state_alloc");
4844 hunkfile = got_opentemp();
4845 if (hunkfile == NULL) {
4846 err = got_error_from_errno("got_opentemp");
4847 goto done;
4850 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4851 diff_result, &cc);
4852 if (rc != DIFF_RC_OK) {
4853 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4854 goto done;
4857 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4858 err = got_ferror(hunkfile, GOT_ERR_IO);
4859 goto done;
4862 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4863 hunkfile, changeno, nchanges);
4864 if (err)
4865 goto done;
4867 switch (*choice) {
4868 case GOT_PATCH_CHOICE_YES:
4869 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4870 end_old, start_new, end_new, outfile, rejectfile);
4871 break;
4872 case GOT_PATCH_CHOICE_NO:
4873 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4874 end_old, start_new, end_new, rejectfile, outfile);
4875 break;
4876 case GOT_PATCH_CHOICE_QUIT:
4877 break;
4878 default:
4879 err = got_error(GOT_ERR_PATCH_CHOICE);
4880 break;
4882 done:
4883 diff_output_unidiff_state_free(diff_state);
4884 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4885 err = got_error_from_errno("fclose");
4886 return err;
4889 struct revert_file_args {
4890 struct got_worktree *worktree;
4891 struct got_fileindex *fileindex;
4892 got_worktree_checkout_cb progress_cb;
4893 void *progress_arg;
4894 got_worktree_patch_cb patch_cb;
4895 void *patch_arg;
4896 struct got_repository *repo;
4897 int unlink_added_files;
4898 struct got_pathlist_head *added_files_to_unlink;
4901 static const struct got_error *
4902 create_patched_content(char **path_outfile, int reverse_patch,
4903 struct got_object_id *blob_id, const char *path2,
4904 int dirfd2, const char *de_name2,
4905 const char *relpath, struct got_repository *repo,
4906 got_worktree_patch_cb patch_cb, void *patch_arg)
4908 const struct got_error *err, *free_err;
4909 struct got_blob_object *blob = NULL;
4910 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4911 int fd = -1, fd2 = -1;
4912 char link_target[PATH_MAX];
4913 ssize_t link_len = 0;
4914 char *path1 = NULL, *id_str = NULL;
4915 struct stat sb2;
4916 struct got_diffreg_result *diffreg_result = NULL;
4917 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4918 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4920 *path_outfile = NULL;
4922 err = got_object_id_str(&id_str, blob_id);
4923 if (err)
4924 return err;
4926 if (dirfd2 != -1) {
4927 fd2 = openat(dirfd2, de_name2,
4928 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4929 if (fd2 == -1) {
4930 if (!got_err_open_nofollow_on_symlink()) {
4931 err = got_error_from_errno2("openat", path2);
4932 goto done;
4934 link_len = readlinkat(dirfd2, de_name2,
4935 link_target, sizeof(link_target));
4936 if (link_len == -1) {
4937 return got_error_from_errno2("readlinkat",
4938 path2);
4940 sb2.st_mode = S_IFLNK;
4941 sb2.st_size = link_len;
4943 } else {
4944 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4945 if (fd2 == -1) {
4946 if (!got_err_open_nofollow_on_symlink()) {
4947 err = got_error_from_errno2("open", path2);
4948 goto done;
4950 link_len = readlink(path2, link_target,
4951 sizeof(link_target));
4952 if (link_len == -1)
4953 return got_error_from_errno2("readlink", path2);
4954 sb2.st_mode = S_IFLNK;
4955 sb2.st_size = link_len;
4958 if (fd2 != -1) {
4959 if (fstat(fd2, &sb2) == -1) {
4960 err = got_error_from_errno2("fstat", path2);
4961 goto done;
4964 f2 = fdopen(fd2, "r");
4965 if (f2 == NULL) {
4966 err = got_error_from_errno2("fdopen", path2);
4967 goto done;
4969 fd2 = -1;
4970 } else {
4971 size_t n;
4972 f2 = got_opentemp();
4973 if (f2 == NULL) {
4974 err = got_error_from_errno2("got_opentemp", path2);
4975 goto done;
4977 n = fwrite(link_target, 1, link_len, f2);
4978 if (n != link_len) {
4979 err = got_ferror(f2, GOT_ERR_IO);
4980 goto done;
4982 if (fflush(f2) == EOF) {
4983 err = got_error_from_errno("fflush");
4984 goto done;
4986 rewind(f2);
4989 fd = got_opentempfd();
4990 if (fd == -1) {
4991 err = got_error_from_errno("got_opentempfd");
4992 goto done;
4995 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4996 if (err)
4997 goto done;
4999 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
5000 if (err)
5001 goto done;
5003 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5004 if (err)
5005 goto done;
5007 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5008 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5009 if (err)
5010 goto done;
5012 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5013 "");
5014 if (err)
5015 goto done;
5017 if (fseek(f1, 0L, SEEK_SET) == -1)
5018 return got_ferror(f1, GOT_ERR_IO);
5019 if (fseek(f2, 0L, SEEK_SET) == -1)
5020 return got_ferror(f2, GOT_ERR_IO);
5022 /* Count the number of actual changes in the diff result. */
5023 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5024 struct diff_chunk_context cc = {};
5025 diff_chunk_context_load_change(&cc, &nchunks_used,
5026 diffreg_result->result, n, 0);
5027 nchanges++;
5029 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5030 int choice;
5031 err = apply_or_reject_change(&choice, &nchunks_used,
5032 diffreg_result->result, n, relpath, f1, f2,
5033 &line_cur1, &line_cur2,
5034 reverse_patch ? NULL : outfile,
5035 reverse_patch ? outfile : NULL,
5036 ++i, nchanges, patch_cb, patch_arg);
5037 if (err)
5038 goto done;
5039 if (choice == GOT_PATCH_CHOICE_YES)
5040 have_content = 1;
5041 else if (choice == GOT_PATCH_CHOICE_QUIT)
5042 break;
5044 if (have_content) {
5045 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5046 reverse_patch ? NULL : outfile,
5047 reverse_patch ? outfile : NULL);
5048 if (err)
5049 goto done;
5051 if (!S_ISLNK(sb2.st_mode)) {
5052 mode_t mode;
5054 mode = apply_umask(sb2.st_mode);
5055 if (fchmod(fileno(outfile), mode) == -1) {
5056 err = got_error_from_errno2("fchmod", path2);
5057 goto done;
5061 done:
5062 free(id_str);
5063 if (fd != -1 && close(fd) == -1 && err == NULL)
5064 err = got_error_from_errno("close");
5065 if (blob)
5066 got_object_blob_close(blob);
5067 free_err = got_diffreg_result_free(diffreg_result);
5068 if (err == NULL)
5069 err = free_err;
5070 if (f1 && fclose(f1) == EOF && err == NULL)
5071 err = got_error_from_errno2("fclose", path1);
5072 if (f2 && fclose(f2) == EOF && err == NULL)
5073 err = got_error_from_errno2("fclose", path2);
5074 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5075 err = got_error_from_errno2("close", path2);
5076 if (outfile && fclose(outfile) == EOF && err == NULL)
5077 err = got_error_from_errno2("fclose", *path_outfile);
5078 if (path1 && unlink(path1) == -1 && err == NULL)
5079 err = got_error_from_errno2("unlink", path1);
5080 if (err || !have_content) {
5081 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5082 err = got_error_from_errno2("unlink", *path_outfile);
5083 free(*path_outfile);
5084 *path_outfile = NULL;
5086 free(path1);
5087 return err;
5090 static const struct got_error *
5091 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5092 const char *relpath, struct got_object_id *blob_id,
5093 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5094 int dirfd, const char *de_name)
5096 struct revert_file_args *a = arg;
5097 const struct got_error *err = NULL;
5098 char *parent_path = NULL;
5099 struct got_fileindex_entry *ie;
5100 struct got_commit_object *base_commit = NULL;
5101 struct got_tree_object *tree = NULL;
5102 struct got_object_id *tree_id = NULL;
5103 const struct got_tree_entry *te = NULL;
5104 char *tree_path = NULL, *te_name;
5105 char *ondisk_path = NULL, *path_content = NULL;
5106 struct got_blob_object *blob = NULL;
5107 int fd = -1;
5109 /* Reverting a staged deletion is a no-op. */
5110 if (status == GOT_STATUS_DELETE &&
5111 staged_status != GOT_STATUS_NO_CHANGE)
5112 return NULL;
5114 if (status == GOT_STATUS_UNVERSIONED)
5115 return (*a->progress_cb)(a->progress_arg,
5116 GOT_STATUS_UNVERSIONED, relpath);
5118 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5119 if (ie == NULL)
5120 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5122 /* Construct in-repository path of tree which contains this blob. */
5123 err = got_path_dirname(&parent_path, ie->path);
5124 if (err) {
5125 if (err->code != GOT_ERR_BAD_PATH)
5126 goto done;
5127 parent_path = strdup("/");
5128 if (parent_path == NULL) {
5129 err = got_error_from_errno("strdup");
5130 goto done;
5133 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5134 tree_path = strdup(parent_path);
5135 if (tree_path == NULL) {
5136 err = got_error_from_errno("strdup");
5137 goto done;
5139 } else {
5140 if (got_path_is_root_dir(parent_path)) {
5141 tree_path = strdup(a->worktree->path_prefix);
5142 if (tree_path == NULL) {
5143 err = got_error_from_errno("strdup");
5144 goto done;
5146 } else {
5147 if (asprintf(&tree_path, "%s/%s",
5148 a->worktree->path_prefix, parent_path) == -1) {
5149 err = got_error_from_errno("asprintf");
5150 goto done;
5155 err = got_object_open_as_commit(&base_commit, a->repo,
5156 a->worktree->base_commit_id);
5157 if (err)
5158 goto done;
5160 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5161 if (err) {
5162 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5163 (status == GOT_STATUS_ADD ||
5164 staged_status == GOT_STATUS_ADD)))
5165 goto done;
5166 } else {
5167 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5168 if (err)
5169 goto done;
5171 err = got_path_basename(&te_name, ie->path);
5172 if (err)
5173 goto done;
5175 te = got_object_tree_find_entry(tree, te_name);
5176 free(te_name);
5177 if (te == NULL && status != GOT_STATUS_ADD &&
5178 staged_status != GOT_STATUS_ADD) {
5179 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5180 goto done;
5184 switch (status) {
5185 case GOT_STATUS_ADD:
5186 if (a->patch_cb) {
5187 int choice = GOT_PATCH_CHOICE_NONE;
5188 err = (*a->patch_cb)(&choice, a->patch_arg,
5189 status, ie->path, NULL, 1, 1);
5190 if (err)
5191 goto done;
5192 if (choice != GOT_PATCH_CHOICE_YES)
5193 break;
5195 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5196 ie->path);
5197 if (err)
5198 goto done;
5199 got_fileindex_entry_remove(a->fileindex, ie);
5200 if (a->unlink_added_files) {
5201 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5203 if (a->added_files_to_unlink) {
5204 struct got_pathlist_entry *pe;
5206 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5207 entry) {
5208 if (got_path_cmp(pe->path, relpath,
5209 pe->path_len, strlen(relpath)))
5210 continue;
5211 do_unlink = 1;
5212 break;
5216 if (do_unlink) {
5217 if (asprintf(&ondisk_path, "%s/%s",
5218 got_worktree_get_root_path(a->worktree),
5219 relpath) == -1) {
5220 err = got_error_from_errno("asprintf");
5221 goto done;
5223 if (unlink(ondisk_path) == -1) {
5224 err = got_error_from_errno2("unlink",
5225 ondisk_path);
5226 break;
5230 break;
5231 case GOT_STATUS_DELETE:
5232 if (a->patch_cb) {
5233 int choice = GOT_PATCH_CHOICE_NONE;
5234 err = (*a->patch_cb)(&choice, a->patch_arg,
5235 status, ie->path, NULL, 1, 1);
5236 if (err)
5237 goto done;
5238 if (choice != GOT_PATCH_CHOICE_YES)
5239 break;
5241 /* fall through */
5242 case GOT_STATUS_MODIFY:
5243 case GOT_STATUS_MODE_CHANGE:
5244 case GOT_STATUS_CONFLICT:
5245 case GOT_STATUS_MISSING: {
5246 struct got_object_id id;
5247 if (staged_status == GOT_STATUS_ADD ||
5248 staged_status == GOT_STATUS_MODIFY)
5249 got_fileindex_entry_get_staged_blob_id(&id, ie);
5250 else
5251 got_fileindex_entry_get_blob_id(&id, ie);
5252 fd = got_opentempfd();
5253 if (fd == -1) {
5254 err = got_error_from_errno("got_opentempfd");
5255 goto done;
5258 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5259 if (err)
5260 goto done;
5262 if (asprintf(&ondisk_path, "%s/%s",
5263 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5264 err = got_error_from_errno("asprintf");
5265 goto done;
5268 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5269 status == GOT_STATUS_CONFLICT)) {
5270 int is_bad_symlink = 0;
5271 err = create_patched_content(&path_content, 1, &id,
5272 ondisk_path, dirfd, de_name, ie->path, a->repo,
5273 a->patch_cb, a->patch_arg);
5274 if (err || path_content == NULL)
5275 break;
5276 if (te && S_ISLNK(te->mode)) {
5277 if (unlink(path_content) == -1) {
5278 err = got_error_from_errno2("unlink",
5279 path_content);
5280 break;
5282 err = install_symlink(&is_bad_symlink,
5283 a->worktree, ondisk_path, ie->path,
5284 blob, 0, 1, 0, 0, a->repo,
5285 a->progress_cb, a->progress_arg);
5286 } else {
5287 if (rename(path_content, ondisk_path) == -1) {
5288 err = got_error_from_errno3("rename",
5289 path_content, ondisk_path);
5290 goto done;
5293 } else {
5294 int is_bad_symlink = 0;
5295 if (te && S_ISLNK(te->mode)) {
5296 err = install_symlink(&is_bad_symlink,
5297 a->worktree, ondisk_path, ie->path,
5298 blob, 0, 1, 0, 0, a->repo,
5299 a->progress_cb, a->progress_arg);
5300 } else {
5301 err = install_blob(a->worktree, ondisk_path,
5302 ie->path,
5303 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5304 got_fileindex_perms_to_st(ie), blob,
5305 0, 1, 0, 0, NULL, a->repo,
5306 a->progress_cb, a->progress_arg);
5308 if (err)
5309 goto done;
5310 if (status == GOT_STATUS_DELETE ||
5311 status == GOT_STATUS_MODE_CHANGE) {
5312 err = got_fileindex_entry_update(ie,
5313 a->worktree->root_fd, relpath,
5314 blob->id.sha1,
5315 a->worktree->base_commit_id->sha1, 1);
5316 if (err)
5317 goto done;
5319 if (is_bad_symlink) {
5320 got_fileindex_entry_filetype_set(ie,
5321 GOT_FILEIDX_MODE_BAD_SYMLINK);
5324 break;
5326 default:
5327 break;
5329 done:
5330 free(ondisk_path);
5331 free(path_content);
5332 free(parent_path);
5333 free(tree_path);
5334 if (fd != -1 && close(fd) == -1 && err == NULL)
5335 err = got_error_from_errno("close");
5336 if (blob)
5337 got_object_blob_close(blob);
5338 if (tree)
5339 got_object_tree_close(tree);
5340 free(tree_id);
5341 if (base_commit)
5342 got_object_commit_close(base_commit);
5343 return err;
5346 const struct got_error *
5347 got_worktree_revert(struct got_worktree *worktree,
5348 struct got_pathlist_head *paths,
5349 got_worktree_checkout_cb progress_cb, void *progress_arg,
5350 got_worktree_patch_cb patch_cb, void *patch_arg,
5351 struct got_repository *repo)
5353 struct got_fileindex *fileindex = NULL;
5354 char *fileindex_path = NULL;
5355 const struct got_error *err = NULL, *unlockerr = NULL;
5356 const struct got_error *sync_err = NULL;
5357 struct got_pathlist_entry *pe;
5358 struct revert_file_args rfa;
5360 err = lock_worktree(worktree, LOCK_EX);
5361 if (err)
5362 return err;
5364 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5365 if (err)
5366 goto done;
5368 rfa.worktree = worktree;
5369 rfa.fileindex = fileindex;
5370 rfa.progress_cb = progress_cb;
5371 rfa.progress_arg = progress_arg;
5372 rfa.patch_cb = patch_cb;
5373 rfa.patch_arg = patch_arg;
5374 rfa.repo = repo;
5375 rfa.unlink_added_files = 0;
5376 TAILQ_FOREACH(pe, paths, entry) {
5377 err = worktree_status(worktree, pe->path, fileindex, repo,
5378 revert_file, &rfa, NULL, NULL, 1, 0);
5379 if (err)
5380 break;
5382 sync_err = sync_fileindex(fileindex, fileindex_path);
5383 if (sync_err && err == NULL)
5384 err = sync_err;
5385 done:
5386 free(fileindex_path);
5387 if (fileindex)
5388 got_fileindex_free(fileindex);
5389 unlockerr = lock_worktree(worktree, LOCK_SH);
5390 if (unlockerr && err == NULL)
5391 err = unlockerr;
5392 return err;
5395 static void
5396 free_commitable(struct got_commitable *ct)
5398 free(ct->path);
5399 free(ct->in_repo_path);
5400 free(ct->ondisk_path);
5401 free(ct->blob_id);
5402 free(ct->base_blob_id);
5403 free(ct->staged_blob_id);
5404 free(ct->base_commit_id);
5405 free(ct);
5408 struct collect_commitables_arg {
5409 struct got_pathlist_head *commitable_paths;
5410 struct got_repository *repo;
5411 struct got_worktree *worktree;
5412 struct got_fileindex *fileindex;
5413 int have_staged_files;
5414 int allow_bad_symlinks;
5415 int diff_header_shown;
5416 int commit_conflicts;
5417 FILE *diff_outfile;
5418 FILE *f1;
5419 FILE *f2;
5423 * Create a file which contains the target path of a symlink so we can feed
5424 * it as content to the diff engine.
5426 static const struct got_error *
5427 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5428 const char *abspath)
5430 const struct got_error *err = NULL;
5431 char target_path[PATH_MAX];
5432 ssize_t target_len, outlen;
5434 *fd = -1;
5436 if (dirfd != -1) {
5437 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5438 if (target_len == -1)
5439 return got_error_from_errno2("readlinkat", abspath);
5440 } else {
5441 target_len = readlink(abspath, target_path, PATH_MAX);
5442 if (target_len == -1)
5443 return got_error_from_errno2("readlink", abspath);
5446 *fd = got_opentempfd();
5447 if (*fd == -1)
5448 return got_error_from_errno("got_opentempfd");
5450 outlen = write(*fd, target_path, target_len);
5451 if (outlen == -1) {
5452 err = got_error_from_errno("got_opentempfd");
5453 goto done;
5456 if (lseek(*fd, 0, SEEK_SET) == -1) {
5457 err = got_error_from_errno2("lseek", abspath);
5458 goto done;
5460 done:
5461 if (err) {
5462 close(*fd);
5463 *fd = -1;
5465 return err;
5468 static const struct got_error *
5469 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5470 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5471 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5473 const struct got_error *err = NULL;
5474 struct got_blob_object *blob1 = NULL;
5475 int fd = -1, fd1 = -1, fd2 = -1;
5476 FILE *ondisk_file = NULL;
5477 char *label1 = NULL;
5478 struct stat sb;
5479 off_t size1 = 0;
5480 int f2_exists = 0;
5481 char *id_str = NULL;
5483 memset(&sb, 0, sizeof(sb));
5485 if (diff_staged) {
5486 if (ct->staged_status != GOT_STATUS_MODIFY &&
5487 ct->staged_status != GOT_STATUS_ADD &&
5488 ct->staged_status != GOT_STATUS_DELETE)
5489 return NULL;
5490 } else {
5491 if (ct->status != GOT_STATUS_MODIFY &&
5492 ct->status != GOT_STATUS_ADD &&
5493 ct->status != GOT_STATUS_DELETE &&
5494 ct->status != GOT_STATUS_CONFLICT)
5495 return NULL;
5498 err = got_opentemp_truncate(f1);
5499 if (err)
5500 return got_error_from_errno("got_opentemp_truncate");
5501 err = got_opentemp_truncate(f2);
5502 if (err)
5503 return got_error_from_errno("got_opentemp_truncate");
5505 if (!*diff_header_shown) {
5506 err = got_object_id_str(&id_str, worktree->base_commit_id);
5507 if (err)
5508 return err;
5509 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5510 got_worktree_get_root_path(worktree));
5511 fprintf(diff_outfile, "commit - %s\n", id_str);
5512 fprintf(diff_outfile, "path + %s%s\n",
5513 got_worktree_get_root_path(worktree),
5514 diff_staged ? " (staged changes)" : "");
5515 *diff_header_shown = 1;
5518 if (diff_staged) {
5519 const char *label1 = NULL, *label2 = NULL;
5520 switch (ct->staged_status) {
5521 case GOT_STATUS_MODIFY:
5522 label1 = ct->path;
5523 label2 = ct->path;
5524 break;
5525 case GOT_STATUS_ADD:
5526 label2 = ct->path;
5527 break;
5528 case GOT_STATUS_DELETE:
5529 label1 = ct->path;
5530 break;
5531 default:
5532 return got_error(GOT_ERR_FILE_STATUS);
5534 fd1 = got_opentempfd();
5535 if (fd1 == -1) {
5536 err = got_error_from_errno("got_opentempfd");
5537 goto done;
5539 fd2 = got_opentempfd();
5540 if (fd2 == -1) {
5541 err = got_error_from_errno("got_opentempfd");
5542 goto done;
5544 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5545 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5546 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5547 NULL, repo, diff_outfile);
5548 goto done;
5551 fd1 = got_opentempfd();
5552 if (fd1 == -1) {
5553 err = got_error_from_errno("got_opentempfd");
5554 goto done;
5557 if (ct->status != GOT_STATUS_ADD) {
5558 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5559 8192, fd1);
5560 if (err)
5561 goto done;
5564 if (ct->status != GOT_STATUS_DELETE) {
5565 if (dirfd != -1) {
5566 fd = openat(dirfd, de_name,
5567 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5568 if (fd == -1) {
5569 if (!got_err_open_nofollow_on_symlink()) {
5570 err = got_error_from_errno2("openat",
5571 ct->ondisk_path);
5572 goto done;
5574 err = get_symlink_target_file(&fd, dirfd,
5575 de_name, ct->ondisk_path);
5576 if (err)
5577 goto done;
5579 } else {
5580 fd = open(ct->ondisk_path,
5581 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5582 if (fd == -1) {
5583 if (!got_err_open_nofollow_on_symlink()) {
5584 err = got_error_from_errno2("open",
5585 ct->ondisk_path);
5586 goto done;
5588 err = get_symlink_target_file(&fd, dirfd,
5589 de_name, ct->ondisk_path);
5590 if (err)
5591 goto done;
5594 if (fstatat(fd, ct->ondisk_path, &sb,
5595 AT_SYMLINK_NOFOLLOW) == -1) {
5596 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5597 goto done;
5599 ondisk_file = fdopen(fd, "r");
5600 if (ondisk_file == NULL) {
5601 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5602 goto done;
5604 fd = -1;
5605 f2_exists = 1;
5608 if (blob1) {
5609 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5610 f1, blob1);
5611 if (err)
5612 goto done;
5615 err = got_diff_blob_file(blob1, f1, size1, label1,
5616 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5617 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5618 done:
5619 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5620 err = got_error_from_errno("close");
5621 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5622 err = got_error_from_errno("close");
5623 if (blob1)
5624 got_object_blob_close(blob1);
5625 if (fd != -1 && close(fd) == -1 && err == NULL)
5626 err = got_error_from_errno("close");
5627 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5628 err = got_error_from_errno("fclose");
5629 return err;
5632 static const struct got_error *
5633 collect_commitables(void *arg, unsigned char status,
5634 unsigned char staged_status, const char *relpath,
5635 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5636 struct got_object_id *commit_id, int dirfd, const char *de_name)
5638 struct collect_commitables_arg *a = arg;
5639 const struct got_error *err = NULL;
5640 struct got_commitable *ct = NULL;
5641 struct got_pathlist_entry *new = NULL;
5642 char *parent_path = NULL, *path = NULL;
5643 struct stat sb;
5645 if (a->have_staged_files) {
5646 if (staged_status != GOT_STATUS_MODIFY &&
5647 staged_status != GOT_STATUS_ADD &&
5648 staged_status != GOT_STATUS_DELETE)
5649 return NULL;
5650 } else {
5651 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5652 printf("C %s\n", relpath);
5653 return got_error(GOT_ERR_COMMIT_CONFLICT);
5656 if (status != GOT_STATUS_MODIFY &&
5657 status != GOT_STATUS_MODE_CHANGE &&
5658 status != GOT_STATUS_ADD &&
5659 status != GOT_STATUS_DELETE &&
5660 status != GOT_STATUS_CONFLICT)
5661 return NULL;
5664 if (asprintf(&path, "/%s", relpath) == -1) {
5665 err = got_error_from_errno("asprintf");
5666 goto done;
5668 if (strcmp(path, "/") == 0) {
5669 parent_path = strdup("");
5670 if (parent_path == NULL)
5671 return got_error_from_errno("strdup");
5672 } else {
5673 err = got_path_dirname(&parent_path, path);
5674 if (err)
5675 return err;
5678 ct = calloc(1, sizeof(*ct));
5679 if (ct == NULL) {
5680 err = got_error_from_errno("calloc");
5681 goto done;
5684 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5685 relpath) == -1) {
5686 err = got_error_from_errno("asprintf");
5687 goto done;
5690 if (staged_status == GOT_STATUS_ADD ||
5691 staged_status == GOT_STATUS_MODIFY) {
5692 struct got_fileindex_entry *ie;
5693 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5694 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5695 case GOT_FILEIDX_MODE_REGULAR_FILE:
5696 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5697 ct->mode = S_IFREG;
5698 break;
5699 case GOT_FILEIDX_MODE_SYMLINK:
5700 ct->mode = S_IFLNK;
5701 break;
5702 default:
5703 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5704 goto done;
5706 ct->mode |= got_fileindex_entry_perms_get(ie);
5707 } else if (status != GOT_STATUS_DELETE &&
5708 staged_status != GOT_STATUS_DELETE) {
5709 if (dirfd != -1) {
5710 if (fstatat(dirfd, de_name, &sb,
5711 AT_SYMLINK_NOFOLLOW) == -1) {
5712 err = got_error_from_errno2("fstatat",
5713 ct->ondisk_path);
5714 goto done;
5716 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5717 err = got_error_from_errno2("lstat", ct->ondisk_path);
5718 goto done;
5720 ct->mode = sb.st_mode;
5723 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5724 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5725 relpath) == -1) {
5726 err = got_error_from_errno("asprintf");
5727 goto done;
5730 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5731 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5732 int is_bad_symlink;
5733 char target_path[PATH_MAX];
5734 ssize_t target_len;
5735 target_len = readlink(ct->ondisk_path, target_path,
5736 sizeof(target_path));
5737 if (target_len == -1) {
5738 err = got_error_from_errno2("readlink",
5739 ct->ondisk_path);
5740 goto done;
5742 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5743 target_len, ct->ondisk_path, a->worktree->root_path,
5744 a->worktree->meta_dir);
5745 if (err)
5746 goto done;
5747 if (is_bad_symlink) {
5748 err = got_error_path(ct->ondisk_path,
5749 GOT_ERR_BAD_SYMLINK);
5750 goto done;
5755 ct->status = status;
5756 ct->staged_status = staged_status;
5757 ct->blob_id = NULL; /* will be filled in when blob gets created */
5758 if (ct->status != GOT_STATUS_ADD &&
5759 ct->staged_status != GOT_STATUS_ADD) {
5760 ct->base_blob_id = got_object_id_dup(blob_id);
5761 if (ct->base_blob_id == NULL) {
5762 err = got_error_from_errno("got_object_id_dup");
5763 goto done;
5765 ct->base_commit_id = got_object_id_dup(commit_id);
5766 if (ct->base_commit_id == NULL) {
5767 err = got_error_from_errno("got_object_id_dup");
5768 goto done;
5771 if (ct->staged_status == GOT_STATUS_ADD ||
5772 ct->staged_status == GOT_STATUS_MODIFY) {
5773 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5774 if (ct->staged_blob_id == NULL) {
5775 err = got_error_from_errno("got_object_id_dup");
5776 goto done;
5779 ct->path = strdup(path);
5780 if (ct->path == NULL) {
5781 err = got_error_from_errno("strdup");
5782 goto done;
5785 if (a->diff_outfile) {
5786 err = append_ct_diff(ct, &a->diff_header_shown,
5787 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5788 a->have_staged_files, a->repo, a->worktree);
5789 if (err)
5790 goto done;
5793 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5794 done:
5795 if (ct && (err || new == NULL))
5796 free_commitable(ct);
5797 free(parent_path);
5798 free(path);
5799 return err;
5802 static const struct got_error *write_tree(struct got_object_id **, int *,
5803 struct got_tree_object *, const char *, struct got_pathlist_head *,
5804 got_worktree_status_cb status_cb, void *status_arg,
5805 struct got_repository *);
5807 static const struct got_error *
5808 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5809 struct got_tree_entry *te, const char *parent_path,
5810 struct got_pathlist_head *commitable_paths,
5811 got_worktree_status_cb status_cb, void *status_arg,
5812 struct got_repository *repo)
5814 const struct got_error *err = NULL;
5815 struct got_tree_object *subtree;
5816 char *subpath;
5818 if (asprintf(&subpath, "%s%s%s", parent_path,
5819 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5820 return got_error_from_errno("asprintf");
5822 err = got_object_open_as_tree(&subtree, repo, &te->id);
5823 if (err)
5824 return err;
5826 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5827 commitable_paths, status_cb, status_arg, repo);
5828 got_object_tree_close(subtree);
5829 free(subpath);
5830 return err;
5833 static const struct got_error *
5834 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5836 const struct got_error *err = NULL;
5837 char *ct_parent_path = NULL;
5839 *match = 0;
5841 if (strchr(ct->in_repo_path, '/') == NULL) {
5842 *match = got_path_is_root_dir(path);
5843 return NULL;
5846 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5847 if (err)
5848 return err;
5849 *match = (strcmp(path, ct_parent_path) == 0);
5850 free(ct_parent_path);
5851 return err;
5854 static mode_t
5855 get_ct_file_mode(struct got_commitable *ct)
5857 if (S_ISLNK(ct->mode))
5858 return S_IFLNK;
5860 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5863 static const struct got_error *
5864 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5865 struct got_tree_entry *te, struct got_commitable *ct)
5867 const struct got_error *err = NULL;
5869 *new_te = NULL;
5871 err = got_object_tree_entry_dup(new_te, te);
5872 if (err)
5873 goto done;
5875 (*new_te)->mode = get_ct_file_mode(ct);
5877 if (ct->staged_status == GOT_STATUS_MODIFY)
5878 memcpy(&(*new_te)->id, ct->staged_blob_id,
5879 sizeof((*new_te)->id));
5880 else
5881 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5882 done:
5883 if (err && *new_te) {
5884 free(*new_te);
5885 *new_te = NULL;
5887 return err;
5890 static const struct got_error *
5891 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5892 struct got_commitable *ct)
5894 const struct got_error *err = NULL;
5895 char *ct_name = NULL;
5897 *new_te = NULL;
5899 *new_te = calloc(1, sizeof(**new_te));
5900 if (*new_te == NULL)
5901 return got_error_from_errno("calloc");
5903 err = got_path_basename(&ct_name, ct->path);
5904 if (err)
5905 goto done;
5906 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5907 sizeof((*new_te)->name)) {
5908 err = got_error(GOT_ERR_NO_SPACE);
5909 goto done;
5912 (*new_te)->mode = get_ct_file_mode(ct);
5914 if (ct->staged_status == GOT_STATUS_ADD)
5915 memcpy(&(*new_te)->id, ct->staged_blob_id,
5916 sizeof((*new_te)->id));
5917 else
5918 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5919 done:
5920 free(ct_name);
5921 if (err && *new_te) {
5922 free(*new_te);
5923 *new_te = NULL;
5925 return err;
5928 static const struct got_error *
5929 insert_tree_entry(struct got_tree_entry *new_te,
5930 struct got_pathlist_head *paths)
5932 const struct got_error *err = NULL;
5933 struct got_pathlist_entry *new_pe;
5935 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5936 if (err)
5937 return err;
5938 if (new_pe == NULL)
5939 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5940 return NULL;
5943 static const struct got_error *
5944 report_ct_status(struct got_commitable *ct,
5945 got_worktree_status_cb status_cb, void *status_arg)
5947 const char *ct_path = ct->path;
5948 unsigned char status;
5950 if (status_cb == NULL) /* no commit progress output desired */
5951 return NULL;
5953 while (ct_path[0] == '/')
5954 ct_path++;
5956 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5957 status = ct->staged_status;
5958 else
5959 status = ct->status;
5961 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5962 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5965 static const struct got_error *
5966 match_modified_subtree(int *modified, struct got_tree_entry *te,
5967 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5969 const struct got_error *err = NULL;
5970 struct got_pathlist_entry *pe;
5971 char *te_path;
5973 *modified = 0;
5975 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5976 got_path_is_root_dir(base_tree_path) ? "" : "/",
5977 te->name) == -1)
5978 return got_error_from_errno("asprintf");
5980 TAILQ_FOREACH(pe, commitable_paths, entry) {
5981 struct got_commitable *ct = pe->data;
5982 *modified = got_path_is_child(ct->in_repo_path, te_path,
5983 strlen(te_path));
5984 if (*modified)
5985 break;
5988 free(te_path);
5989 return err;
5992 static const struct got_error *
5993 match_deleted_or_modified_ct(struct got_commitable **ctp,
5994 struct got_tree_entry *te, const char *base_tree_path,
5995 struct got_pathlist_head *commitable_paths)
5997 const struct got_error *err = NULL;
5998 struct got_pathlist_entry *pe;
6000 *ctp = NULL;
6002 TAILQ_FOREACH(pe, commitable_paths, entry) {
6003 struct got_commitable *ct = pe->data;
6004 char *ct_name = NULL;
6005 int path_matches;
6007 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6008 if (ct->status != GOT_STATUS_MODIFY &&
6009 ct->status != GOT_STATUS_MODE_CHANGE &&
6010 ct->status != GOT_STATUS_DELETE &&
6011 ct->status != GOT_STATUS_CONFLICT)
6012 continue;
6013 } else {
6014 if (ct->staged_status != GOT_STATUS_MODIFY &&
6015 ct->staged_status != GOT_STATUS_DELETE)
6016 continue;
6019 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6020 continue;
6022 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6023 if (err)
6024 return err;
6025 if (!path_matches)
6026 continue;
6028 err = got_path_basename(&ct_name, pe->path);
6029 if (err)
6030 return err;
6032 if (strcmp(te->name, ct_name) != 0) {
6033 free(ct_name);
6034 continue;
6036 free(ct_name);
6038 *ctp = ct;
6039 break;
6042 return err;
6045 static const struct got_error *
6046 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6047 const char *child_path, const char *path_base_tree,
6048 struct got_pathlist_head *commitable_paths,
6049 got_worktree_status_cb status_cb, void *status_arg,
6050 struct got_repository *repo)
6052 const struct got_error *err = NULL;
6053 struct got_tree_entry *new_te;
6054 char *subtree_path;
6055 struct got_object_id *id = NULL;
6056 int nentries;
6058 *new_tep = NULL;
6060 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6061 got_path_is_root_dir(path_base_tree) ? "" : "/",
6062 child_path) == -1)
6063 return got_error_from_errno("asprintf");
6065 new_te = calloc(1, sizeof(*new_te));
6066 if (new_te == NULL)
6067 return got_error_from_errno("calloc");
6068 new_te->mode = S_IFDIR;
6070 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6071 sizeof(new_te->name)) {
6072 err = got_error(GOT_ERR_NO_SPACE);
6073 goto done;
6075 err = write_tree(&id, &nentries, NULL, subtree_path,
6076 commitable_paths, status_cb, status_arg, repo);
6077 if (err) {
6078 free(new_te);
6079 goto done;
6081 memcpy(&new_te->id, id, sizeof(new_te->id));
6082 done:
6083 free(id);
6084 free(subtree_path);
6085 if (err == NULL)
6086 *new_tep = new_te;
6087 return err;
6090 static const struct got_error *
6091 write_tree(struct got_object_id **new_tree_id, int *nentries,
6092 struct got_tree_object *base_tree, const char *path_base_tree,
6093 struct got_pathlist_head *commitable_paths,
6094 got_worktree_status_cb status_cb, void *status_arg,
6095 struct got_repository *repo)
6097 const struct got_error *err = NULL;
6098 struct got_pathlist_head paths;
6099 struct got_tree_entry *te, *new_te = NULL;
6100 struct got_pathlist_entry *pe;
6102 TAILQ_INIT(&paths);
6103 *nentries = 0;
6105 /* Insert, and recurse into, newly added entries first. */
6106 TAILQ_FOREACH(pe, commitable_paths, entry) {
6107 struct got_commitable *ct = pe->data;
6108 char *child_path = NULL, *slash;
6110 if ((ct->status != GOT_STATUS_ADD &&
6111 ct->staged_status != GOT_STATUS_ADD) ||
6112 (ct->flags & GOT_COMMITABLE_ADDED))
6113 continue;
6115 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6116 strlen(path_base_tree)))
6117 continue;
6119 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6120 ct->in_repo_path);
6121 if (err)
6122 goto done;
6124 slash = strchr(child_path, '/');
6125 if (slash == NULL) {
6126 err = alloc_added_blob_tree_entry(&new_te, ct);
6127 if (err)
6128 goto done;
6129 err = report_ct_status(ct, status_cb, status_arg);
6130 if (err)
6131 goto done;
6132 ct->flags |= GOT_COMMITABLE_ADDED;
6133 err = insert_tree_entry(new_te, &paths);
6134 if (err)
6135 goto done;
6136 (*nentries)++;
6137 } else {
6138 *slash = '\0'; /* trim trailing path components */
6139 if (base_tree == NULL ||
6140 got_object_tree_find_entry(base_tree, child_path)
6141 == NULL) {
6142 err = make_subtree_for_added_blob(&new_te,
6143 child_path, path_base_tree,
6144 commitable_paths, status_cb, status_arg,
6145 repo);
6146 if (err)
6147 goto done;
6148 err = insert_tree_entry(new_te, &paths);
6149 if (err)
6150 goto done;
6151 (*nentries)++;
6156 if (base_tree) {
6157 int i, nbase_entries;
6158 /* Handle modified and deleted entries. */
6159 nbase_entries = got_object_tree_get_nentries(base_tree);
6160 for (i = 0; i < nbase_entries; i++) {
6161 struct got_commitable *ct = NULL;
6163 te = got_object_tree_get_entry(base_tree, i);
6164 if (got_object_tree_entry_is_submodule(te)) {
6165 /* Entry is a submodule; just copy it. */
6166 err = got_object_tree_entry_dup(&new_te, te);
6167 if (err)
6168 goto done;
6169 err = insert_tree_entry(new_te, &paths);
6170 if (err)
6171 goto done;
6172 (*nentries)++;
6173 continue;
6176 if (S_ISDIR(te->mode)) {
6177 int modified;
6178 err = got_object_tree_entry_dup(&new_te, te);
6179 if (err)
6180 goto done;
6181 err = match_modified_subtree(&modified, te,
6182 path_base_tree, commitable_paths);
6183 if (err)
6184 goto done;
6185 /* Avoid recursion into unmodified subtrees. */
6186 if (modified) {
6187 struct got_object_id *new_id;
6188 int nsubentries;
6189 err = write_subtree(&new_id,
6190 &nsubentries, te,
6191 path_base_tree, commitable_paths,
6192 status_cb, status_arg, repo);
6193 if (err)
6194 goto done;
6195 if (nsubentries == 0) {
6196 /* All entries were deleted. */
6197 free(new_id);
6198 continue;
6200 memcpy(&new_te->id, new_id,
6201 sizeof(new_te->id));
6202 free(new_id);
6204 err = insert_tree_entry(new_te, &paths);
6205 if (err)
6206 goto done;
6207 (*nentries)++;
6208 continue;
6211 err = match_deleted_or_modified_ct(&ct, te,
6212 path_base_tree, commitable_paths);
6213 if (err)
6214 goto done;
6215 if (ct) {
6216 /* NB: Deleted entries get dropped here. */
6217 if (ct->status == GOT_STATUS_MODIFY ||
6218 ct->status == GOT_STATUS_MODE_CHANGE ||
6219 ct->status == GOT_STATUS_CONFLICT ||
6220 ct->staged_status == GOT_STATUS_MODIFY) {
6221 err = alloc_modified_blob_tree_entry(
6222 &new_te, te, ct);
6223 if (err)
6224 goto done;
6225 err = insert_tree_entry(new_te, &paths);
6226 if (err)
6227 goto done;
6228 (*nentries)++;
6230 err = report_ct_status(ct, status_cb,
6231 status_arg);
6232 if (err)
6233 goto done;
6234 } else {
6235 /* Entry is unchanged; just copy it. */
6236 err = got_object_tree_entry_dup(&new_te, te);
6237 if (err)
6238 goto done;
6239 err = insert_tree_entry(new_te, &paths);
6240 if (err)
6241 goto done;
6242 (*nentries)++;
6247 /* Write new list of entries; deleted entries have been dropped. */
6248 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6249 done:
6250 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6251 return err;
6254 static const struct got_error *
6255 update_fileindex_after_commit(struct got_worktree *worktree,
6256 struct got_pathlist_head *commitable_paths,
6257 struct got_object_id *new_base_commit_id,
6258 struct got_fileindex *fileindex, int have_staged_files)
6260 const struct got_error *err = NULL;
6261 struct got_pathlist_entry *pe;
6262 char *relpath = NULL;
6264 TAILQ_FOREACH(pe, commitable_paths, entry) {
6265 struct got_fileindex_entry *ie;
6266 struct got_commitable *ct = pe->data;
6268 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6270 err = got_path_skip_common_ancestor(&relpath,
6271 worktree->root_path, ct->ondisk_path);
6272 if (err)
6273 goto done;
6275 if (ie) {
6276 if (ct->status == GOT_STATUS_DELETE ||
6277 ct->staged_status == GOT_STATUS_DELETE) {
6278 got_fileindex_entry_remove(fileindex, ie);
6279 } else if (ct->staged_status == GOT_STATUS_ADD ||
6280 ct->staged_status == GOT_STATUS_MODIFY) {
6281 got_fileindex_entry_stage_set(ie,
6282 GOT_FILEIDX_STAGE_NONE);
6283 got_fileindex_entry_staged_filetype_set(ie, 0);
6285 err = got_fileindex_entry_update(ie,
6286 worktree->root_fd, relpath,
6287 ct->staged_blob_id->sha1,
6288 new_base_commit_id->sha1,
6289 !have_staged_files);
6290 } else
6291 err = got_fileindex_entry_update(ie,
6292 worktree->root_fd, relpath,
6293 ct->blob_id->sha1,
6294 new_base_commit_id->sha1,
6295 !have_staged_files);
6296 } else {
6297 err = got_fileindex_entry_alloc(&ie, pe->path);
6298 if (err)
6299 goto done;
6300 err = got_fileindex_entry_update(ie,
6301 worktree->root_fd, relpath, ct->blob_id->sha1,
6302 new_base_commit_id->sha1, 1);
6303 if (err) {
6304 got_fileindex_entry_free(ie);
6305 goto done;
6307 err = got_fileindex_entry_add(fileindex, ie);
6308 if (err) {
6309 got_fileindex_entry_free(ie);
6310 goto done;
6313 free(relpath);
6314 relpath = NULL;
6316 done:
6317 free(relpath);
6318 return err;
6322 static const struct got_error *
6323 check_out_of_date(const char *in_repo_path, unsigned char status,
6324 unsigned char staged_status, struct got_object_id *base_blob_id,
6325 struct got_object_id *base_commit_id,
6326 struct got_object_id *head_commit_id, struct got_repository *repo,
6327 int ood_errcode)
6329 const struct got_error *err = NULL;
6330 struct got_commit_object *commit = NULL;
6331 struct got_object_id *id = NULL;
6333 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6334 /* Trivial case: base commit == head commit */
6335 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6336 return NULL;
6338 * Ensure file content which local changes were based
6339 * on matches file content in the branch head.
6341 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6342 if (err)
6343 goto done;
6344 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6345 if (err) {
6346 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6347 err = got_error(ood_errcode);
6348 goto done;
6349 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6350 err = got_error(ood_errcode);
6351 } else {
6352 /* Require that added files don't exist in the branch head. */
6353 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6354 if (err)
6355 goto done;
6356 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6357 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6358 goto done;
6359 err = id ? got_error(ood_errcode) : NULL;
6361 done:
6362 free(id);
6363 if (commit)
6364 got_object_commit_close(commit);
6365 return err;
6368 static const struct got_error *
6369 commit_worktree(struct got_object_id **new_commit_id,
6370 struct got_pathlist_head *commitable_paths,
6371 struct got_object_id *head_commit_id,
6372 struct got_object_id *parent_id2,
6373 struct got_worktree *worktree,
6374 const char *author, const char *committer, char *diff_path,
6375 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6376 got_worktree_status_cb status_cb, void *status_arg,
6377 struct got_repository *repo)
6379 const struct got_error *err = NULL, *unlockerr = NULL;
6380 struct got_pathlist_entry *pe;
6381 const char *head_ref_name = NULL;
6382 struct got_commit_object *head_commit = NULL;
6383 struct got_reference *head_ref2 = NULL;
6384 struct got_object_id *head_commit_id2 = NULL;
6385 struct got_tree_object *head_tree = NULL;
6386 struct got_object_id *new_tree_id = NULL;
6387 int nentries, nparents = 0;
6388 struct got_object_id_queue parent_ids;
6389 struct got_object_qid *pid = NULL;
6390 char *logmsg = NULL;
6391 time_t timestamp;
6393 *new_commit_id = NULL;
6395 STAILQ_INIT(&parent_ids);
6397 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6398 if (err)
6399 goto done;
6401 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6402 if (err)
6403 goto done;
6405 if (commit_msg_cb != NULL) {
6406 err = commit_msg_cb(commitable_paths, diff_path,
6407 &logmsg, commit_arg);
6408 if (err)
6409 goto done;
6412 if (logmsg == NULL || strlen(logmsg) == 0) {
6413 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6414 goto done;
6417 /* Create blobs from added and modified files and record their IDs. */
6418 TAILQ_FOREACH(pe, commitable_paths, entry) {
6419 struct got_commitable *ct = pe->data;
6420 char *ondisk_path;
6422 /* Blobs for staged files already exist. */
6423 if (ct->staged_status == GOT_STATUS_ADD ||
6424 ct->staged_status == GOT_STATUS_MODIFY)
6425 continue;
6427 if (ct->status != GOT_STATUS_ADD &&
6428 ct->status != GOT_STATUS_MODIFY &&
6429 ct->status != GOT_STATUS_MODE_CHANGE &&
6430 ct->status != GOT_STATUS_CONFLICT)
6431 continue;
6433 if (asprintf(&ondisk_path, "%s/%s",
6434 worktree->root_path, pe->path) == -1) {
6435 err = got_error_from_errno("asprintf");
6436 goto done;
6438 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6439 free(ondisk_path);
6440 if (err)
6441 goto done;
6444 /* Recursively write new tree objects. */
6445 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6446 commitable_paths, status_cb, status_arg, repo);
6447 if (err)
6448 goto done;
6450 err = got_object_qid_alloc(&pid, head_commit_id);
6451 if (err)
6452 goto done;
6453 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6454 nparents++;
6455 if (parent_id2) {
6456 err = got_object_qid_alloc(&pid, parent_id2);
6457 if (err)
6458 goto done;
6459 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6460 nparents++;
6462 timestamp = time(NULL);
6463 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6464 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6465 if (logmsg != NULL)
6466 free(logmsg);
6467 if (err)
6468 goto done;
6470 /* Check if a concurrent commit to our branch has occurred. */
6471 head_ref_name = got_worktree_get_head_ref_name(worktree);
6472 if (head_ref_name == NULL) {
6473 err = got_error_from_errno("got_worktree_get_head_ref_name");
6474 goto done;
6476 /* Lock the reference here to prevent concurrent modification. */
6477 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6478 if (err)
6479 goto done;
6480 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6481 if (err)
6482 goto done;
6483 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6484 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6485 goto done;
6487 /* Update branch head in repository. */
6488 err = got_ref_change_ref(head_ref2, *new_commit_id);
6489 if (err)
6490 goto done;
6491 err = got_ref_write(head_ref2, repo);
6492 if (err)
6493 goto done;
6495 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6496 if (err)
6497 goto done;
6499 err = ref_base_commit(worktree, repo);
6500 if (err)
6501 goto done;
6502 done:
6503 got_object_id_queue_free(&parent_ids);
6504 if (head_tree)
6505 got_object_tree_close(head_tree);
6506 if (head_commit)
6507 got_object_commit_close(head_commit);
6508 free(head_commit_id2);
6509 if (head_ref2) {
6510 unlockerr = got_ref_unlock(head_ref2);
6511 if (unlockerr && err == NULL)
6512 err = unlockerr;
6513 got_ref_close(head_ref2);
6515 return err;
6518 static const struct got_error *
6519 check_path_is_commitable(const char *path,
6520 struct got_pathlist_head *commitable_paths)
6522 struct got_pathlist_entry *cpe = NULL;
6523 size_t path_len = strlen(path);
6525 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6526 struct got_commitable *ct = cpe->data;
6527 const char *ct_path = ct->path;
6529 while (ct_path[0] == '/')
6530 ct_path++;
6532 if (strcmp(path, ct_path) == 0 ||
6533 got_path_is_child(ct_path, path, path_len))
6534 break;
6537 if (cpe == NULL)
6538 return got_error_path(path, GOT_ERR_BAD_PATH);
6540 return NULL;
6543 static const struct got_error *
6544 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6546 int *have_staged_files = arg;
6548 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6549 *have_staged_files = 1;
6550 return got_error(GOT_ERR_CANCELLED);
6553 return NULL;
6556 static const struct got_error *
6557 check_non_staged_files(struct got_fileindex *fileindex,
6558 struct got_pathlist_head *paths)
6560 struct got_pathlist_entry *pe;
6561 struct got_fileindex_entry *ie;
6563 TAILQ_FOREACH(pe, paths, entry) {
6564 if (pe->path[0] == '\0')
6565 continue;
6566 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6567 if (ie == NULL)
6568 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6569 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6570 return got_error_path(pe->path,
6571 GOT_ERR_FILE_NOT_STAGED);
6574 return NULL;
6577 const struct got_error *
6578 got_worktree_commit(struct got_object_id **new_commit_id,
6579 struct got_worktree *worktree, struct got_pathlist_head *paths,
6580 const char *author, const char *committer, int allow_bad_symlinks,
6581 int show_diff, int commit_conflicts,
6582 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6583 got_worktree_status_cb status_cb, void *status_arg,
6584 struct got_repository *repo)
6586 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6587 struct got_fileindex *fileindex = NULL;
6588 char *fileindex_path = NULL;
6589 struct got_pathlist_head commitable_paths;
6590 struct collect_commitables_arg cc_arg;
6591 struct got_pathlist_entry *pe;
6592 struct got_reference *head_ref = NULL;
6593 struct got_object_id *head_commit_id = NULL;
6594 char *diff_path = NULL;
6595 int have_staged_files = 0;
6597 *new_commit_id = NULL;
6599 memset(&cc_arg, 0, sizeof(cc_arg));
6600 TAILQ_INIT(&commitable_paths);
6602 err = lock_worktree(worktree, LOCK_EX);
6603 if (err)
6604 goto done;
6606 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6607 if (err)
6608 goto done;
6610 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6611 if (err)
6612 goto done;
6614 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6615 if (err)
6616 goto done;
6618 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6619 &have_staged_files);
6620 if (err && err->code != GOT_ERR_CANCELLED)
6621 goto done;
6622 if (have_staged_files) {
6623 err = check_non_staged_files(fileindex, paths);
6624 if (err)
6625 goto done;
6628 cc_arg.commitable_paths = &commitable_paths;
6629 cc_arg.worktree = worktree;
6630 cc_arg.fileindex = fileindex;
6631 cc_arg.repo = repo;
6632 cc_arg.have_staged_files = have_staged_files;
6633 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6634 cc_arg.diff_header_shown = 0;
6635 cc_arg.commit_conflicts = commit_conflicts;
6636 if (show_diff) {
6637 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6638 GOT_TMPDIR_STR "/got", ".diff");
6639 if (err)
6640 goto done;
6641 cc_arg.f1 = got_opentemp();
6642 if (cc_arg.f1 == NULL) {
6643 err = got_error_from_errno("got_opentemp");
6644 goto done;
6646 cc_arg.f2 = got_opentemp();
6647 if (cc_arg.f2 == NULL) {
6648 err = got_error_from_errno("got_opentemp");
6649 goto done;
6653 TAILQ_FOREACH(pe, paths, entry) {
6654 err = worktree_status(worktree, pe->path, fileindex, repo,
6655 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6656 if (err)
6657 goto done;
6660 if (show_diff) {
6661 if (fflush(cc_arg.diff_outfile) == EOF) {
6662 err = got_error_from_errno("fflush");
6663 goto done;
6667 if (TAILQ_EMPTY(&commitable_paths)) {
6668 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6669 goto done;
6672 TAILQ_FOREACH(pe, paths, entry) {
6673 err = check_path_is_commitable(pe->path, &commitable_paths);
6674 if (err)
6675 goto done;
6678 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6679 struct got_commitable *ct = pe->data;
6680 const char *ct_path = ct->in_repo_path;
6682 while (ct_path[0] == '/')
6683 ct_path++;
6684 err = check_out_of_date(ct_path, ct->status,
6685 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6686 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6687 if (err)
6688 goto done;
6692 err = commit_worktree(new_commit_id, &commitable_paths,
6693 head_commit_id, NULL, worktree, author, committer,
6694 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6695 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6696 if (err)
6697 goto done;
6699 err = update_fileindex_after_commit(worktree, &commitable_paths,
6700 *new_commit_id, fileindex, have_staged_files);
6701 sync_err = sync_fileindex(fileindex, fileindex_path);
6702 if (sync_err && err == NULL)
6703 err = sync_err;
6704 done:
6705 if (fileindex)
6706 got_fileindex_free(fileindex);
6707 free(fileindex_path);
6708 unlockerr = lock_worktree(worktree, LOCK_SH);
6709 if (unlockerr && err == NULL)
6710 err = unlockerr;
6711 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6712 struct got_commitable *ct = pe->data;
6714 free_commitable(ct);
6716 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6717 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6718 err = got_error_from_errno2("unlink", diff_path);
6719 free(diff_path);
6720 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6721 err == NULL)
6722 err = got_error_from_errno("fclose");
6723 return err;
6726 const char *
6727 got_commitable_get_path(struct got_commitable *ct)
6729 return ct->path;
6732 unsigned int
6733 got_commitable_get_status(struct got_commitable *ct)
6735 return ct->status;
6738 struct check_rebase_ok_arg {
6739 struct got_worktree *worktree;
6740 struct got_repository *repo;
6743 static const struct got_error *
6744 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6746 const struct got_error *err = NULL;
6747 struct check_rebase_ok_arg *a = arg;
6748 unsigned char status;
6749 struct stat sb;
6750 char *ondisk_path;
6752 /* Reject rebase of a work tree with mixed base commits. */
6753 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6754 SHA1_DIGEST_LENGTH))
6755 return got_error(GOT_ERR_MIXED_COMMITS);
6757 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6758 == -1)
6759 return got_error_from_errno("asprintf");
6761 /* Reject rebase of a work tree with modified or staged files. */
6762 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6763 free(ondisk_path);
6764 if (err)
6765 return err;
6767 if (status != GOT_STATUS_NO_CHANGE)
6768 return got_error(GOT_ERR_MODIFIED);
6769 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6770 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6772 return NULL;
6775 const struct got_error *
6776 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6777 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6778 struct got_worktree *worktree, struct got_reference *branch,
6779 struct got_repository *repo)
6781 const struct got_error *err = NULL;
6782 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6783 char *branch_ref_name = NULL;
6784 char *fileindex_path = NULL;
6785 struct check_rebase_ok_arg ok_arg;
6786 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6787 struct got_object_id *wt_branch_tip = NULL;
6789 *new_base_branch_ref = NULL;
6790 *tmp_branch = NULL;
6791 *fileindex = NULL;
6793 err = lock_worktree(worktree, LOCK_EX);
6794 if (err)
6795 return err;
6797 err = open_fileindex(fileindex, &fileindex_path, worktree);
6798 if (err)
6799 goto done;
6801 ok_arg.worktree = worktree;
6802 ok_arg.repo = repo;
6803 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6804 &ok_arg);
6805 if (err)
6806 goto done;
6808 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6809 if (err)
6810 goto done;
6812 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6813 if (err)
6814 goto done;
6816 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6817 if (err)
6818 goto done;
6820 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6821 0);
6822 if (err)
6823 goto done;
6825 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6826 if (err)
6827 goto done;
6828 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6829 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6830 goto done;
6833 err = got_ref_alloc_symref(new_base_branch_ref,
6834 new_base_branch_ref_name, wt_branch);
6835 if (err)
6836 goto done;
6837 err = got_ref_write(*new_base_branch_ref, repo);
6838 if (err)
6839 goto done;
6841 /* TODO Lock original branch's ref while rebasing? */
6843 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6844 if (err)
6845 goto done;
6847 err = got_ref_write(branch_ref, repo);
6848 if (err)
6849 goto done;
6851 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6852 worktree->base_commit_id);
6853 if (err)
6854 goto done;
6855 err = got_ref_write(*tmp_branch, repo);
6856 if (err)
6857 goto done;
6859 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6860 if (err)
6861 goto done;
6862 done:
6863 free(fileindex_path);
6864 free(tmp_branch_name);
6865 free(new_base_branch_ref_name);
6866 free(branch_ref_name);
6867 if (branch_ref)
6868 got_ref_close(branch_ref);
6869 if (wt_branch)
6870 got_ref_close(wt_branch);
6871 free(wt_branch_tip);
6872 if (err) {
6873 if (*new_base_branch_ref) {
6874 got_ref_close(*new_base_branch_ref);
6875 *new_base_branch_ref = NULL;
6877 if (*tmp_branch) {
6878 got_ref_close(*tmp_branch);
6879 *tmp_branch = NULL;
6881 if (*fileindex) {
6882 got_fileindex_free(*fileindex);
6883 *fileindex = NULL;
6885 lock_worktree(worktree, LOCK_SH);
6887 return err;
6890 const struct got_error *
6891 got_worktree_rebase_continue(struct got_object_id **commit_id,
6892 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6893 struct got_reference **branch, struct got_fileindex **fileindex,
6894 struct got_worktree *worktree, struct got_repository *repo)
6896 const struct got_error *err;
6897 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6898 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6899 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6900 char *fileindex_path = NULL;
6901 int have_staged_files = 0;
6903 *commit_id = NULL;
6904 *new_base_branch = NULL;
6905 *tmp_branch = NULL;
6906 *branch = NULL;
6907 *fileindex = NULL;
6909 err = lock_worktree(worktree, LOCK_EX);
6910 if (err)
6911 return err;
6913 err = open_fileindex(fileindex, &fileindex_path, worktree);
6914 if (err)
6915 goto done;
6917 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6918 &have_staged_files);
6919 if (err && err->code != GOT_ERR_CANCELLED)
6920 goto done;
6921 if (have_staged_files) {
6922 err = got_error(GOT_ERR_STAGED_PATHS);
6923 goto done;
6926 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6927 if (err)
6928 goto done;
6930 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6931 if (err)
6932 goto done;
6934 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6935 if (err)
6936 goto done;
6938 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6939 if (err)
6940 goto done;
6942 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6943 if (err)
6944 goto done;
6946 err = got_ref_open(branch, repo,
6947 got_ref_get_symref_target(branch_ref), 0);
6948 if (err)
6949 goto done;
6951 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6952 if (err)
6953 goto done;
6955 err = got_ref_resolve(commit_id, repo, commit_ref);
6956 if (err)
6957 goto done;
6959 err = got_ref_open(new_base_branch, repo,
6960 new_base_branch_ref_name, 0);
6961 if (err)
6962 goto done;
6964 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6965 if (err)
6966 goto done;
6967 done:
6968 free(commit_ref_name);
6969 free(branch_ref_name);
6970 free(fileindex_path);
6971 if (commit_ref)
6972 got_ref_close(commit_ref);
6973 if (branch_ref)
6974 got_ref_close(branch_ref);
6975 if (err) {
6976 free(*commit_id);
6977 *commit_id = NULL;
6978 if (*tmp_branch) {
6979 got_ref_close(*tmp_branch);
6980 *tmp_branch = NULL;
6982 if (*new_base_branch) {
6983 got_ref_close(*new_base_branch);
6984 *new_base_branch = NULL;
6986 if (*branch) {
6987 got_ref_close(*branch);
6988 *branch = NULL;
6990 if (*fileindex) {
6991 got_fileindex_free(*fileindex);
6992 *fileindex = NULL;
6994 lock_worktree(worktree, LOCK_SH);
6996 return err;
6999 const struct got_error *
7000 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7002 const struct got_error *err;
7003 char *tmp_branch_name = NULL;
7005 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7006 if (err)
7007 return err;
7009 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7010 free(tmp_branch_name);
7011 return NULL;
7014 const struct got_error *
7015 got_worktree_rebase_info(char **new_base_branch_name, char **branch_name,
7016 struct got_worktree *worktree, struct got_repository *repo)
7018 const struct got_error *err;
7019 char *new_base_branch_ref_name = NULL;
7020 char *branch_ref_name = NULL;
7021 struct got_reference *branch_ref = NULL, *branch = NULL;
7022 struct got_reference *new_base_branch = NULL;
7024 *new_base_branch_name = NULL;
7025 *branch_name = NULL;
7027 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7028 if (err)
7029 goto done;
7031 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7032 if (err)
7033 goto done;
7035 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
7036 if (err)
7037 goto done;
7039 err = got_ref_open(&branch, repo,
7040 got_ref_get_symref_target(branch_ref), 0);
7041 if (err)
7042 goto done;
7044 err = got_ref_open(&new_base_branch, repo,
7045 new_base_branch_ref_name, 0);
7046 if (err)
7047 goto done;
7049 if (!got_ref_is_symbolic(new_base_branch)) {
7050 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7051 "%s is not a symbolic reference",
7052 got_ref_get_name(branch_ref));
7053 goto done;
7056 *new_base_branch_name = strdup(got_ref_get_symref_target(
7057 new_base_branch));
7058 if (*new_base_branch_name == NULL) {
7059 err = got_error_from_errno("strdup");
7060 goto done;
7062 *branch_name = strdup(got_ref_get_name(branch));
7063 if (*branch_name == NULL) {
7064 err = got_error_from_errno("strdup");
7065 goto done;
7067 done:
7068 free(branch_ref_name);
7069 if (branch_ref)
7070 got_ref_close(branch_ref);
7071 if (new_base_branch)
7072 got_ref_close(new_base_branch);
7073 if (branch)
7074 got_ref_close(branch);
7075 return err;
7078 static const struct got_error *
7079 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7080 const char *diff_path, char **logmsg, void *arg)
7082 *logmsg = arg;
7083 return NULL;
7086 static const struct got_error *
7087 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7088 const char *path, struct got_object_id *blob_id,
7089 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7090 int dirfd, const char *de_name)
7092 return NULL;
7095 struct collect_merged_paths_arg {
7096 got_worktree_checkout_cb progress_cb;
7097 void *progress_arg;
7098 struct got_pathlist_head *merged_paths;
7101 static const struct got_error *
7102 collect_merged_paths(void *arg, unsigned char status, const char *path)
7104 const struct got_error *err;
7105 struct collect_merged_paths_arg *a = arg;
7106 char *p;
7107 struct got_pathlist_entry *new;
7109 err = (*a->progress_cb)(a->progress_arg, status, path);
7110 if (err)
7111 return err;
7113 if (status != GOT_STATUS_MERGE &&
7114 status != GOT_STATUS_ADD &&
7115 status != GOT_STATUS_DELETE &&
7116 status != GOT_STATUS_CONFLICT)
7117 return NULL;
7119 p = strdup(path);
7120 if (p == NULL)
7121 return got_error_from_errno("strdup");
7123 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7124 if (err || new == NULL)
7125 free(p);
7126 return err;
7129 static const struct got_error *
7130 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7131 int is_rebase, struct got_repository *repo)
7133 const struct got_error *err;
7134 struct got_reference *commit_ref = NULL;
7136 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7137 if (err) {
7138 if (err->code != GOT_ERR_NOT_REF)
7139 goto done;
7140 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7141 if (err)
7142 goto done;
7143 err = got_ref_write(commit_ref, repo);
7144 if (err)
7145 goto done;
7146 } else if (is_rebase) {
7147 struct got_object_id *stored_id;
7148 int cmp;
7150 err = got_ref_resolve(&stored_id, repo, commit_ref);
7151 if (err)
7152 goto done;
7153 cmp = got_object_id_cmp(commit_id, stored_id);
7154 free(stored_id);
7155 if (cmp != 0) {
7156 err = got_error(GOT_ERR_REBASE_COMMITID);
7157 goto done;
7160 done:
7161 if (commit_ref)
7162 got_ref_close(commit_ref);
7163 return err;
7166 static const struct got_error *
7167 rebase_merge_files(struct got_pathlist_head *merged_paths,
7168 const char *commit_ref_name, struct got_worktree *worktree,
7169 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7170 struct got_object_id *commit_id, struct got_repository *repo,
7171 got_worktree_checkout_cb progress_cb, void *progress_arg,
7172 got_cancel_cb cancel_cb, void *cancel_arg)
7174 const struct got_error *err;
7175 struct got_reference *commit_ref = NULL;
7176 struct collect_merged_paths_arg cmp_arg;
7177 char *fileindex_path;
7179 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7181 err = get_fileindex_path(&fileindex_path, worktree);
7182 if (err)
7183 return err;
7185 cmp_arg.progress_cb = progress_cb;
7186 cmp_arg.progress_arg = progress_arg;
7187 cmp_arg.merged_paths = merged_paths;
7188 err = merge_files(worktree, fileindex, fileindex_path,
7189 parent_commit_id, commit_id, repo, collect_merged_paths,
7190 &cmp_arg, cancel_cb, cancel_arg);
7191 if (commit_ref)
7192 got_ref_close(commit_ref);
7193 return err;
7196 const struct got_error *
7197 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7198 struct got_worktree *worktree, struct got_fileindex *fileindex,
7199 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7200 struct got_repository *repo,
7201 got_worktree_checkout_cb progress_cb, void *progress_arg,
7202 got_cancel_cb cancel_cb, void *cancel_arg)
7204 const struct got_error *err;
7205 char *commit_ref_name;
7207 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7208 if (err)
7209 return err;
7211 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7212 if (err)
7213 goto done;
7215 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7216 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7217 progress_arg, cancel_cb, cancel_arg);
7218 done:
7219 free(commit_ref_name);
7220 return err;
7223 const struct got_error *
7224 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7225 struct got_worktree *worktree, struct got_fileindex *fileindex,
7226 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7227 struct got_repository *repo,
7228 got_worktree_checkout_cb progress_cb, void *progress_arg,
7229 got_cancel_cb cancel_cb, void *cancel_arg)
7231 const struct got_error *err;
7232 char *commit_ref_name;
7234 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7235 if (err)
7236 return err;
7238 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7239 if (err)
7240 goto done;
7242 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7243 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7244 progress_arg, cancel_cb, cancel_arg);
7245 done:
7246 free(commit_ref_name);
7247 return err;
7250 static const struct got_error *
7251 rebase_commit(struct got_object_id **new_commit_id,
7252 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7253 struct got_worktree *worktree, struct got_fileindex *fileindex,
7254 struct got_reference *tmp_branch, const char *committer,
7255 struct got_commit_object *orig_commit, const char *new_logmsg,
7256 int allow_conflict, struct got_repository *repo)
7258 const struct got_error *err, *sync_err;
7259 struct got_pathlist_head commitable_paths;
7260 struct collect_commitables_arg cc_arg;
7261 char *fileindex_path = NULL;
7262 struct got_reference *head_ref = NULL;
7263 struct got_object_id *head_commit_id = NULL;
7264 char *logmsg = NULL;
7266 memset(&cc_arg, 0, sizeof(cc_arg));
7267 TAILQ_INIT(&commitable_paths);
7268 *new_commit_id = NULL;
7270 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7272 err = get_fileindex_path(&fileindex_path, worktree);
7273 if (err)
7274 return err;
7276 cc_arg.commitable_paths = &commitable_paths;
7277 cc_arg.worktree = worktree;
7278 cc_arg.repo = repo;
7279 cc_arg.have_staged_files = 0;
7280 cc_arg.commit_conflicts = allow_conflict;
7282 * If possible get the status of individual files directly to
7283 * avoid crawling the entire work tree once per rebased commit.
7285 * Ideally, merged_paths would contain a list of commitables
7286 * we could use so we could skip worktree_status() entirely.
7287 * However, we would then need carefully keep track of cumulative
7288 * effects of operations such as file additions and deletions
7289 * in 'got histedit -f' (folding multiple commits into one),
7290 * and this extra complexity is not really worth it.
7292 if (merged_paths) {
7293 struct got_pathlist_entry *pe;
7294 TAILQ_FOREACH(pe, merged_paths, entry) {
7295 err = worktree_status(worktree, pe->path, fileindex,
7296 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7297 0);
7298 if (err)
7299 goto done;
7301 } else {
7302 err = worktree_status(worktree, "", fileindex, repo,
7303 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7304 if (err)
7305 goto done;
7308 if (TAILQ_EMPTY(&commitable_paths)) {
7309 /* No-op change; commit will be elided. */
7310 err = got_ref_delete(commit_ref, repo);
7311 if (err)
7312 goto done;
7313 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7314 goto done;
7317 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7318 if (err)
7319 goto done;
7321 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7322 if (err)
7323 goto done;
7325 if (new_logmsg) {
7326 logmsg = strdup(new_logmsg);
7327 if (logmsg == NULL) {
7328 err = got_error_from_errno("strdup");
7329 goto done;
7331 } else {
7332 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7333 if (err)
7334 goto done;
7337 /* NB: commit_worktree will call free(logmsg) */
7338 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7339 NULL, worktree, got_object_commit_get_author(orig_commit),
7340 committer ? committer :
7341 got_object_commit_get_committer(orig_commit), NULL,
7342 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7343 if (err)
7344 goto done;
7346 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7347 if (err)
7348 goto done;
7350 err = got_ref_delete(commit_ref, repo);
7351 if (err)
7352 goto done;
7354 err = update_fileindex_after_commit(worktree, &commitable_paths,
7355 *new_commit_id, fileindex, 0);
7356 sync_err = sync_fileindex(fileindex, fileindex_path);
7357 if (sync_err && err == NULL)
7358 err = sync_err;
7359 done:
7360 free(fileindex_path);
7361 free(head_commit_id);
7362 if (head_ref)
7363 got_ref_close(head_ref);
7364 if (err) {
7365 free(*new_commit_id);
7366 *new_commit_id = NULL;
7368 return err;
7371 const struct got_error *
7372 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7373 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7374 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7375 const char *committer, struct got_commit_object *orig_commit,
7376 struct got_object_id *orig_commit_id, int allow_conflict,
7377 struct got_repository *repo)
7379 const struct got_error *err;
7380 char *commit_ref_name;
7381 struct got_reference *commit_ref = NULL;
7382 struct got_object_id *commit_id = NULL;
7384 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7385 if (err)
7386 return err;
7388 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7389 if (err)
7390 goto done;
7391 err = got_ref_resolve(&commit_id, repo, commit_ref);
7392 if (err)
7393 goto done;
7394 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7395 err = got_error(GOT_ERR_REBASE_COMMITID);
7396 goto done;
7399 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7400 worktree, fileindex, tmp_branch, committer, orig_commit,
7401 NULL, allow_conflict, repo);
7402 done:
7403 if (commit_ref)
7404 got_ref_close(commit_ref);
7405 free(commit_ref_name);
7406 free(commit_id);
7407 return err;
7410 const struct got_error *
7411 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7412 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7413 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7414 const char *committer, struct got_commit_object *orig_commit,
7415 struct got_object_id *orig_commit_id, const char *new_logmsg,
7416 int allow_conflict, struct got_repository *repo)
7418 const struct got_error *err;
7419 char *commit_ref_name;
7420 struct got_reference *commit_ref = NULL;
7422 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7423 if (err)
7424 return err;
7426 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7427 if (err)
7428 goto done;
7430 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7431 worktree, fileindex, tmp_branch, committer, orig_commit,
7432 new_logmsg, allow_conflict, repo);
7433 done:
7434 if (commit_ref)
7435 got_ref_close(commit_ref);
7436 free(commit_ref_name);
7437 return err;
7440 const struct got_error *
7441 got_worktree_rebase_postpone(struct got_worktree *worktree,
7442 struct got_fileindex *fileindex)
7444 if (fileindex)
7445 got_fileindex_free(fileindex);
7446 return lock_worktree(worktree, LOCK_SH);
7449 static const struct got_error *
7450 delete_ref(const char *name, struct got_repository *repo)
7452 const struct got_error *err;
7453 struct got_reference *ref;
7455 err = got_ref_open(&ref, repo, name, 0);
7456 if (err) {
7457 if (err->code == GOT_ERR_NOT_REF)
7458 return NULL;
7459 return err;
7462 err = got_ref_delete(ref, repo);
7463 got_ref_close(ref);
7464 return err;
7467 static const struct got_error *
7468 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7470 const struct got_error *err;
7471 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7472 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7474 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7475 if (err)
7476 goto done;
7477 err = delete_ref(tmp_branch_name, repo);
7478 if (err)
7479 goto done;
7481 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7482 if (err)
7483 goto done;
7484 err = delete_ref(new_base_branch_ref_name, repo);
7485 if (err)
7486 goto done;
7488 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7489 if (err)
7490 goto done;
7491 err = delete_ref(branch_ref_name, repo);
7492 if (err)
7493 goto done;
7495 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7496 if (err)
7497 goto done;
7498 err = delete_ref(commit_ref_name, repo);
7499 if (err)
7500 goto done;
7502 done:
7503 free(tmp_branch_name);
7504 free(new_base_branch_ref_name);
7505 free(branch_ref_name);
7506 free(commit_ref_name);
7507 return err;
7510 static const struct got_error *
7511 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7512 struct got_object_id *new_commit_id, struct got_repository *repo)
7514 const struct got_error *err;
7515 struct got_reference *ref = NULL;
7516 struct got_object_id *old_commit_id = NULL;
7517 const char *branch_name = NULL;
7518 char *new_id_str = NULL;
7519 char *refname = NULL;
7521 branch_name = got_ref_get_name(branch);
7522 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7523 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7524 branch_name += 11;
7526 err = got_object_id_str(&new_id_str, new_commit_id);
7527 if (err)
7528 return err;
7530 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7531 new_id_str) == -1) {
7532 err = got_error_from_errno("asprintf");
7533 goto done;
7536 err = got_ref_resolve(&old_commit_id, repo, branch);
7537 if (err)
7538 goto done;
7540 err = got_ref_alloc(&ref, refname, old_commit_id);
7541 if (err)
7542 goto done;
7544 err = got_ref_write(ref, repo);
7545 done:
7546 free(new_id_str);
7547 free(refname);
7548 free(old_commit_id);
7549 if (ref)
7550 got_ref_close(ref);
7551 return err;
7554 const struct got_error *
7555 got_worktree_rebase_complete(struct got_worktree *worktree,
7556 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7557 struct got_reference *rebased_branch, struct got_repository *repo,
7558 int create_backup)
7560 const struct got_error *err, *unlockerr, *sync_err;
7561 struct got_object_id *new_head_commit_id = NULL;
7562 char *fileindex_path = NULL;
7564 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7565 if (err)
7566 return err;
7568 if (create_backup) {
7569 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7570 rebased_branch, new_head_commit_id, repo);
7571 if (err)
7572 goto done;
7575 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7576 if (err)
7577 goto done;
7579 err = got_ref_write(rebased_branch, repo);
7580 if (err)
7581 goto done;
7583 err = got_worktree_set_head_ref(worktree, rebased_branch);
7584 if (err)
7585 goto done;
7587 err = delete_rebase_refs(worktree, repo);
7588 if (err)
7589 goto done;
7591 err = get_fileindex_path(&fileindex_path, worktree);
7592 if (err)
7593 goto done;
7594 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7595 sync_err = sync_fileindex(fileindex, fileindex_path);
7596 if (sync_err && err == NULL)
7597 err = sync_err;
7598 done:
7599 got_fileindex_free(fileindex);
7600 free(fileindex_path);
7601 free(new_head_commit_id);
7602 unlockerr = lock_worktree(worktree, LOCK_SH);
7603 if (unlockerr && err == NULL)
7604 err = unlockerr;
7605 return err;
7608 static const struct got_error *
7609 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7610 struct got_object_id *id1, struct got_object_id *id2,
7611 struct got_repository *repo)
7613 const struct got_error *err;
7614 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7615 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7617 if (id1) {
7618 err = got_object_open_as_commit(&commit1, repo, id1);
7619 if (err)
7620 goto done;
7622 err = got_object_open_as_tree(&tree1, repo,
7623 got_object_commit_get_tree_id(commit1));
7624 if (err)
7625 goto done;
7628 if (id2) {
7629 err = got_object_open_as_commit(&commit2, repo, id2);
7630 if (err)
7631 goto done;
7633 err = got_object_open_as_tree(&tree2, repo,
7634 got_object_commit_get_tree_id(commit2));
7635 if (err)
7636 goto done;
7639 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7640 got_diff_tree_collect_changed_paths, paths, 0);
7641 if (err)
7642 goto done;
7643 done:
7644 if (commit1)
7645 got_object_commit_close(commit1);
7646 if (commit2)
7647 got_object_commit_close(commit2);
7648 if (tree1)
7649 got_object_tree_close(tree1);
7650 if (tree2)
7651 got_object_tree_close(tree2);
7652 return err;
7655 static const struct got_error *
7656 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7657 struct got_object_id *id1, struct got_object_id *id2,
7658 const char *path_prefix, struct got_repository *repo)
7660 const struct got_error *err;
7661 struct got_pathlist_head merged_paths;
7662 struct got_pathlist_entry *pe;
7663 char *abspath = NULL, *wt_path = NULL;
7665 TAILQ_INIT(&merged_paths);
7667 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7668 if (err)
7669 goto done;
7671 TAILQ_FOREACH(pe, &merged_paths, entry) {
7672 struct got_diff_changed_path *change = pe->data;
7674 if (change->status != GOT_STATUS_ADD)
7675 continue;
7677 if (got_path_is_root_dir(path_prefix)) {
7678 wt_path = strdup(pe->path);
7679 if (wt_path == NULL) {
7680 err = got_error_from_errno("strdup");
7681 goto done;
7683 } else {
7684 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7685 err = got_error_from_errno("asprintf");
7686 goto done;
7689 err = got_path_skip_common_ancestor(&wt_path,
7690 path_prefix, abspath);
7691 if (err)
7692 goto done;
7693 free(abspath);
7694 abspath = NULL;
7697 err = got_pathlist_append(added_paths, wt_path, NULL);
7698 if (err)
7699 goto done;
7700 wt_path = NULL;
7703 done:
7704 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7705 free(abspath);
7706 free(wt_path);
7707 return err;
7710 static const struct got_error *
7711 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7712 struct got_object_id *id, const char *path_prefix,
7713 struct got_repository *repo)
7715 const struct got_error *err;
7716 struct got_commit_object *commit = NULL;
7717 struct got_object_qid *pid;
7719 err = got_object_open_as_commit(&commit, repo, id);
7720 if (err)
7721 goto done;
7723 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7725 err = get_paths_added_between_commits(added_paths,
7726 pid ? &pid->id : NULL, id, path_prefix, repo);
7727 if (err)
7728 goto done;
7729 done:
7730 if (commit)
7731 got_object_commit_close(commit);
7732 return err;
7735 const struct got_error *
7736 got_worktree_rebase_abort(struct got_worktree *worktree,
7737 struct got_fileindex *fileindex, struct got_repository *repo,
7738 struct got_reference *new_base_branch,
7739 got_worktree_checkout_cb progress_cb, void *progress_arg)
7741 const struct got_error *err, *unlockerr, *sync_err;
7742 struct got_reference *resolved = NULL;
7743 struct got_object_id *commit_id = NULL;
7744 struct got_object_id *merged_commit_id = NULL;
7745 struct got_commit_object *commit = NULL;
7746 char *fileindex_path = NULL;
7747 char *commit_ref_name = NULL;
7748 struct got_reference *commit_ref = NULL;
7749 struct revert_file_args rfa;
7750 struct got_object_id *tree_id = NULL;
7751 struct got_pathlist_head added_paths;
7753 TAILQ_INIT(&added_paths);
7755 err = lock_worktree(worktree, LOCK_EX);
7756 if (err)
7757 return err;
7759 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7760 if (err)
7761 goto done;
7763 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7764 if (err)
7765 goto done;
7767 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7768 if (err)
7769 goto done;
7772 * Determine which files in added status can be safely removed
7773 * from disk while reverting changes in the work tree.
7774 * We want to avoid deleting unrelated files which were added by
7775 * the user for conflict resolution purposes.
7777 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7778 got_worktree_get_path_prefix(worktree), repo);
7779 if (err)
7780 goto done;
7782 err = got_ref_open(&resolved, repo,
7783 got_ref_get_symref_target(new_base_branch), 0);
7784 if (err)
7785 goto done;
7787 err = got_worktree_set_head_ref(worktree, resolved);
7788 if (err)
7789 goto done;
7792 * XXX commits to the base branch could have happened while
7793 * we were busy rebasing; should we store the original commit ID
7794 * when rebase begins and read it back here?
7796 err = got_ref_resolve(&commit_id, repo, resolved);
7797 if (err)
7798 goto done;
7800 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7801 if (err)
7802 goto done;
7804 err = got_object_open_as_commit(&commit, repo,
7805 worktree->base_commit_id);
7806 if (err)
7807 goto done;
7809 err = got_object_id_by_path(&tree_id, repo, commit,
7810 worktree->path_prefix);
7811 if (err)
7812 goto done;
7814 err = delete_rebase_refs(worktree, repo);
7815 if (err)
7816 goto done;
7818 err = get_fileindex_path(&fileindex_path, worktree);
7819 if (err)
7820 goto done;
7822 rfa.worktree = worktree;
7823 rfa.fileindex = fileindex;
7824 rfa.progress_cb = progress_cb;
7825 rfa.progress_arg = progress_arg;
7826 rfa.patch_cb = NULL;
7827 rfa.patch_arg = NULL;
7828 rfa.repo = repo;
7829 rfa.unlink_added_files = 1;
7830 rfa.added_files_to_unlink = &added_paths;
7831 err = worktree_status(worktree, "", fileindex, repo,
7832 revert_file, &rfa, NULL, NULL, 1, 0);
7833 if (err)
7834 goto sync;
7836 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7837 repo, progress_cb, progress_arg, NULL, NULL);
7838 sync:
7839 sync_err = sync_fileindex(fileindex, fileindex_path);
7840 if (sync_err && err == NULL)
7841 err = sync_err;
7842 done:
7843 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7844 got_ref_close(resolved);
7845 free(tree_id);
7846 free(commit_id);
7847 free(merged_commit_id);
7848 if (commit)
7849 got_object_commit_close(commit);
7850 if (fileindex)
7851 got_fileindex_free(fileindex);
7852 free(fileindex_path);
7853 free(commit_ref_name);
7854 if (commit_ref)
7855 got_ref_close(commit_ref);
7857 unlockerr = lock_worktree(worktree, LOCK_SH);
7858 if (unlockerr && err == NULL)
7859 err = unlockerr;
7860 return err;
7863 const struct got_error *
7864 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7865 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7866 struct got_fileindex **fileindex, struct got_worktree *worktree,
7867 struct got_repository *repo)
7869 const struct got_error *err = NULL;
7870 char *tmp_branch_name = NULL;
7871 char *branch_ref_name = NULL;
7872 char *base_commit_ref_name = NULL;
7873 char *fileindex_path = NULL;
7874 struct check_rebase_ok_arg ok_arg;
7875 struct got_reference *wt_branch = NULL;
7876 struct got_reference *base_commit_ref = NULL;
7878 *tmp_branch = NULL;
7879 *branch_ref = NULL;
7880 *base_commit_id = NULL;
7881 *fileindex = NULL;
7883 err = lock_worktree(worktree, LOCK_EX);
7884 if (err)
7885 return err;
7887 err = open_fileindex(fileindex, &fileindex_path, worktree);
7888 if (err)
7889 goto done;
7891 ok_arg.worktree = worktree;
7892 ok_arg.repo = repo;
7893 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7894 &ok_arg);
7895 if (err)
7896 goto done;
7898 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7899 if (err)
7900 goto done;
7902 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7903 if (err)
7904 goto done;
7906 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7907 worktree);
7908 if (err)
7909 goto done;
7911 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7912 0);
7913 if (err)
7914 goto done;
7916 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7917 if (err)
7918 goto done;
7920 err = got_ref_write(*branch_ref, repo);
7921 if (err)
7922 goto done;
7924 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7925 worktree->base_commit_id);
7926 if (err)
7927 goto done;
7928 err = got_ref_write(base_commit_ref, repo);
7929 if (err)
7930 goto done;
7931 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7932 if (*base_commit_id == NULL) {
7933 err = got_error_from_errno("got_object_id_dup");
7934 goto done;
7937 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7938 worktree->base_commit_id);
7939 if (err)
7940 goto done;
7941 err = got_ref_write(*tmp_branch, repo);
7942 if (err)
7943 goto done;
7945 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7946 if (err)
7947 goto done;
7948 done:
7949 free(fileindex_path);
7950 free(tmp_branch_name);
7951 free(branch_ref_name);
7952 free(base_commit_ref_name);
7953 if (wt_branch)
7954 got_ref_close(wt_branch);
7955 if (err) {
7956 if (*branch_ref) {
7957 got_ref_close(*branch_ref);
7958 *branch_ref = NULL;
7960 if (*tmp_branch) {
7961 got_ref_close(*tmp_branch);
7962 *tmp_branch = NULL;
7964 free(*base_commit_id);
7965 if (*fileindex) {
7966 got_fileindex_free(*fileindex);
7967 *fileindex = NULL;
7969 lock_worktree(worktree, LOCK_SH);
7971 return err;
7974 const struct got_error *
7975 got_worktree_histedit_postpone(struct got_worktree *worktree,
7976 struct got_fileindex *fileindex)
7978 if (fileindex)
7979 got_fileindex_free(fileindex);
7980 return lock_worktree(worktree, LOCK_SH);
7983 const struct got_error *
7984 got_worktree_histedit_in_progress(int *in_progress,
7985 struct got_worktree *worktree)
7987 const struct got_error *err;
7988 char *tmp_branch_name = NULL;
7990 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7991 if (err)
7992 return err;
7994 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7995 free(tmp_branch_name);
7996 return NULL;
7999 const struct got_error *
8000 got_worktree_histedit_continue(struct got_object_id **commit_id,
8001 struct got_reference **tmp_branch, struct got_reference **branch_ref,
8002 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
8003 struct got_worktree *worktree, struct got_repository *repo)
8005 const struct got_error *err;
8006 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
8007 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
8008 struct got_reference *commit_ref = NULL;
8009 struct got_reference *base_commit_ref = NULL;
8010 char *fileindex_path = NULL;
8011 int have_staged_files = 0;
8013 *commit_id = NULL;
8014 *tmp_branch = NULL;
8015 *branch_ref = NULL;
8016 *base_commit_id = NULL;
8017 *fileindex = NULL;
8019 err = lock_worktree(worktree, LOCK_EX);
8020 if (err)
8021 return err;
8023 err = open_fileindex(fileindex, &fileindex_path, worktree);
8024 if (err)
8025 goto done;
8027 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8028 &have_staged_files);
8029 if (err && err->code != GOT_ERR_CANCELLED)
8030 goto done;
8031 if (have_staged_files) {
8032 err = got_error(GOT_ERR_STAGED_PATHS);
8033 goto done;
8036 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8037 if (err)
8038 goto done;
8040 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8041 if (err)
8042 goto done;
8044 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8045 if (err)
8046 goto done;
8048 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8049 worktree);
8050 if (err)
8051 goto done;
8053 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
8054 if (err)
8055 goto done;
8057 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8058 if (err)
8059 goto done;
8060 err = got_ref_resolve(commit_id, repo, commit_ref);
8061 if (err)
8062 goto done;
8064 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8065 if (err)
8066 goto done;
8067 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8068 if (err)
8069 goto done;
8071 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8072 if (err)
8073 goto done;
8074 done:
8075 free(commit_ref_name);
8076 free(branch_ref_name);
8077 free(fileindex_path);
8078 if (commit_ref)
8079 got_ref_close(commit_ref);
8080 if (base_commit_ref)
8081 got_ref_close(base_commit_ref);
8082 if (err) {
8083 free(*commit_id);
8084 *commit_id = NULL;
8085 free(*base_commit_id);
8086 *base_commit_id = NULL;
8087 if (*tmp_branch) {
8088 got_ref_close(*tmp_branch);
8089 *tmp_branch = NULL;
8091 if (*branch_ref) {
8092 got_ref_close(*branch_ref);
8093 *branch_ref = NULL;
8095 if (*fileindex) {
8096 got_fileindex_free(*fileindex);
8097 *fileindex = NULL;
8099 lock_worktree(worktree, LOCK_EX);
8101 return err;
8104 const struct got_error *
8105 got_worktree_histedit_info(char **branch_name,
8106 struct got_worktree *worktree, struct got_repository *repo)
8108 const struct got_error *err;
8109 struct got_reference *branch_ref = NULL;
8110 char *branch_ref_name = NULL;
8112 *branch_name = NULL;
8114 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8115 if (err)
8116 goto done;
8118 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
8119 if (err)
8120 goto done;
8122 if (!got_ref_is_symbolic(branch_ref)) {
8123 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8124 "%s is not a symbolic reference",
8125 got_ref_get_name(branch_ref));
8126 goto done;
8129 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8130 if (*branch_name == NULL) {
8131 err = got_error_from_errno("strdup");
8132 goto done;
8134 done:
8135 free(branch_ref_name);
8136 if (branch_ref)
8137 got_ref_close(branch_ref);
8138 if (err) {
8139 free(*branch_name);
8140 *branch_name = NULL;
8142 return err;
8145 static const struct got_error *
8146 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8148 const struct got_error *err;
8149 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8150 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8152 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8153 if (err)
8154 goto done;
8155 err = delete_ref(tmp_branch_name, repo);
8156 if (err)
8157 goto done;
8159 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8160 worktree);
8161 if (err)
8162 goto done;
8163 err = delete_ref(base_commit_ref_name, repo);
8164 if (err)
8165 goto done;
8167 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8168 if (err)
8169 goto done;
8170 err = delete_ref(branch_ref_name, repo);
8171 if (err)
8172 goto done;
8174 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8175 if (err)
8176 goto done;
8177 err = delete_ref(commit_ref_name, repo);
8178 if (err)
8179 goto done;
8180 done:
8181 free(tmp_branch_name);
8182 free(base_commit_ref_name);
8183 free(branch_ref_name);
8184 free(commit_ref_name);
8185 return err;
8188 const struct got_error *
8189 got_worktree_histedit_abort(struct got_worktree *worktree,
8190 struct got_fileindex *fileindex, struct got_repository *repo,
8191 struct got_reference *branch, struct got_object_id *base_commit_id,
8192 got_worktree_checkout_cb progress_cb, void *progress_arg)
8194 const struct got_error *err, *unlockerr, *sync_err;
8195 struct got_reference *resolved = NULL;
8196 char *fileindex_path = NULL;
8197 struct got_object_id *merged_commit_id = NULL;
8198 struct got_commit_object *commit = NULL;
8199 char *commit_ref_name = NULL;
8200 struct got_reference *commit_ref = NULL;
8201 struct got_object_id *tree_id = NULL;
8202 struct revert_file_args rfa;
8203 struct got_pathlist_head added_paths;
8205 TAILQ_INIT(&added_paths);
8207 err = lock_worktree(worktree, LOCK_EX);
8208 if (err)
8209 return err;
8211 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8212 if (err)
8213 goto done;
8215 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8216 if (err) {
8217 if (err->code != GOT_ERR_NOT_REF)
8218 goto done;
8219 /* Can happen on early abort due to invalid histedit script. */
8220 commit_ref = NULL;
8223 if (commit_ref) {
8224 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8225 if (err)
8226 goto done;
8229 * Determine which files in added status can be safely removed
8230 * from disk while reverting changes in the work tree.
8231 * We want to avoid deleting unrelated files added by the
8232 * user during conflict resolution or during histedit -e.
8234 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8235 got_worktree_get_path_prefix(worktree), repo);
8236 if (err)
8237 goto done;
8240 err = got_ref_open(&resolved, repo,
8241 got_ref_get_symref_target(branch), 0);
8242 if (err)
8243 goto done;
8245 err = got_worktree_set_head_ref(worktree, resolved);
8246 if (err)
8247 goto done;
8249 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8250 if (err)
8251 goto done;
8253 err = got_object_open_as_commit(&commit, repo,
8254 worktree->base_commit_id);
8255 if (err)
8256 goto done;
8258 err = got_object_id_by_path(&tree_id, repo, commit,
8259 worktree->path_prefix);
8260 if (err)
8261 goto done;
8263 err = delete_histedit_refs(worktree, repo);
8264 if (err)
8265 goto done;
8267 err = get_fileindex_path(&fileindex_path, worktree);
8268 if (err)
8269 goto done;
8271 rfa.worktree = worktree;
8272 rfa.fileindex = fileindex;
8273 rfa.progress_cb = progress_cb;
8274 rfa.progress_arg = progress_arg;
8275 rfa.patch_cb = NULL;
8276 rfa.patch_arg = NULL;
8277 rfa.repo = repo;
8278 rfa.unlink_added_files = 1;
8279 rfa.added_files_to_unlink = &added_paths;
8280 err = worktree_status(worktree, "", fileindex, repo,
8281 revert_file, &rfa, NULL, NULL, 1, 0);
8282 if (err)
8283 goto sync;
8285 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8286 repo, progress_cb, progress_arg, NULL, NULL);
8287 sync:
8288 sync_err = sync_fileindex(fileindex, fileindex_path);
8289 if (sync_err && err == NULL)
8290 err = sync_err;
8291 done:
8292 if (resolved)
8293 got_ref_close(resolved);
8294 if (commit_ref)
8295 got_ref_close(commit_ref);
8296 free(merged_commit_id);
8297 free(tree_id);
8298 free(fileindex_path);
8299 free(commit_ref_name);
8301 unlockerr = lock_worktree(worktree, LOCK_SH);
8302 if (unlockerr && err == NULL)
8303 err = unlockerr;
8304 return err;
8307 const struct got_error *
8308 got_worktree_histedit_complete(struct got_worktree *worktree,
8309 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8310 struct got_reference *edited_branch, struct got_repository *repo)
8312 const struct got_error *err, *unlockerr, *sync_err;
8313 struct got_object_id *new_head_commit_id = NULL;
8314 struct got_reference *resolved = NULL;
8315 char *fileindex_path = NULL;
8317 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8318 if (err)
8319 return err;
8321 err = got_ref_open(&resolved, repo,
8322 got_ref_get_symref_target(edited_branch), 0);
8323 if (err)
8324 goto done;
8326 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8327 resolved, new_head_commit_id, repo);
8328 if (err)
8329 goto done;
8331 err = got_ref_change_ref(resolved, new_head_commit_id);
8332 if (err)
8333 goto done;
8335 err = got_ref_write(resolved, repo);
8336 if (err)
8337 goto done;
8339 err = got_worktree_set_head_ref(worktree, resolved);
8340 if (err)
8341 goto done;
8343 err = delete_histedit_refs(worktree, repo);
8344 if (err)
8345 goto done;
8347 err = get_fileindex_path(&fileindex_path, worktree);
8348 if (err)
8349 goto done;
8350 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8351 sync_err = sync_fileindex(fileindex, fileindex_path);
8352 if (sync_err && err == NULL)
8353 err = sync_err;
8354 done:
8355 got_fileindex_free(fileindex);
8356 free(fileindex_path);
8357 free(new_head_commit_id);
8358 unlockerr = lock_worktree(worktree, LOCK_SH);
8359 if (unlockerr && err == NULL)
8360 err = unlockerr;
8361 return err;
8364 const struct got_error *
8365 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8366 struct got_object_id *commit_id, struct got_repository *repo)
8368 const struct got_error *err;
8369 char *commit_ref_name;
8371 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8372 if (err)
8373 return err;
8375 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8376 if (err)
8377 goto done;
8379 err = delete_ref(commit_ref_name, repo);
8380 done:
8381 free(commit_ref_name);
8382 return err;
8385 const struct got_error *
8386 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8387 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8388 struct got_worktree *worktree, const char *refname,
8389 struct got_repository *repo)
8391 const struct got_error *err = NULL;
8392 char *fileindex_path = NULL;
8393 struct check_rebase_ok_arg ok_arg;
8395 *fileindex = NULL;
8396 *branch_ref = NULL;
8397 *base_branch_ref = NULL;
8399 err = lock_worktree(worktree, LOCK_EX);
8400 if (err)
8401 return err;
8403 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8404 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8405 "cannot integrate a branch into itself; "
8406 "update -b or different branch name required");
8407 goto done;
8410 err = open_fileindex(fileindex, &fileindex_path, worktree);
8411 if (err)
8412 goto done;
8414 /* Preconditions are the same as for rebase. */
8415 ok_arg.worktree = worktree;
8416 ok_arg.repo = repo;
8417 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8418 &ok_arg);
8419 if (err)
8420 goto done;
8422 err = got_ref_open(branch_ref, repo, refname, 1);
8423 if (err)
8424 goto done;
8426 err = got_ref_open(base_branch_ref, repo,
8427 got_worktree_get_head_ref_name(worktree), 1);
8428 done:
8429 if (err) {
8430 if (*branch_ref) {
8431 got_ref_close(*branch_ref);
8432 *branch_ref = NULL;
8434 if (*base_branch_ref) {
8435 got_ref_close(*base_branch_ref);
8436 *base_branch_ref = NULL;
8438 if (*fileindex) {
8439 got_fileindex_free(*fileindex);
8440 *fileindex = NULL;
8442 lock_worktree(worktree, LOCK_SH);
8444 return err;
8447 const struct got_error *
8448 got_worktree_integrate_continue(struct got_worktree *worktree,
8449 struct got_fileindex *fileindex, struct got_repository *repo,
8450 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8451 got_worktree_checkout_cb progress_cb, void *progress_arg,
8452 got_cancel_cb cancel_cb, void *cancel_arg)
8454 const struct got_error *err = NULL, *sync_err, *unlockerr;
8455 char *fileindex_path = NULL;
8456 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8457 struct got_commit_object *commit = NULL;
8459 err = get_fileindex_path(&fileindex_path, worktree);
8460 if (err)
8461 goto done;
8463 err = got_ref_resolve(&commit_id, repo, branch_ref);
8464 if (err)
8465 goto done;
8467 err = got_object_open_as_commit(&commit, repo, commit_id);
8468 if (err)
8469 goto done;
8471 err = got_object_id_by_path(&tree_id, repo, commit,
8472 worktree->path_prefix);
8473 if (err)
8474 goto done;
8476 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8477 if (err)
8478 goto done;
8480 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8481 progress_cb, progress_arg, cancel_cb, cancel_arg);
8482 if (err)
8483 goto sync;
8485 err = got_ref_change_ref(base_branch_ref, commit_id);
8486 if (err)
8487 goto sync;
8489 err = got_ref_write(base_branch_ref, repo);
8490 if (err)
8491 goto sync;
8493 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8494 sync:
8495 sync_err = sync_fileindex(fileindex, fileindex_path);
8496 if (sync_err && err == NULL)
8497 err = sync_err;
8499 done:
8500 unlockerr = got_ref_unlock(branch_ref);
8501 if (unlockerr && err == NULL)
8502 err = unlockerr;
8503 got_ref_close(branch_ref);
8505 unlockerr = got_ref_unlock(base_branch_ref);
8506 if (unlockerr && err == NULL)
8507 err = unlockerr;
8508 got_ref_close(base_branch_ref);
8510 got_fileindex_free(fileindex);
8511 free(fileindex_path);
8512 free(tree_id);
8513 if (commit)
8514 got_object_commit_close(commit);
8516 unlockerr = lock_worktree(worktree, LOCK_SH);
8517 if (unlockerr && err == NULL)
8518 err = unlockerr;
8519 return err;
8522 const struct got_error *
8523 got_worktree_integrate_abort(struct got_worktree *worktree,
8524 struct got_fileindex *fileindex, struct got_repository *repo,
8525 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8527 const struct got_error *err = NULL, *unlockerr = NULL;
8529 got_fileindex_free(fileindex);
8531 err = lock_worktree(worktree, LOCK_SH);
8533 unlockerr = got_ref_unlock(branch_ref);
8534 if (unlockerr && err == NULL)
8535 err = unlockerr;
8536 got_ref_close(branch_ref);
8538 unlockerr = got_ref_unlock(base_branch_ref);
8539 if (unlockerr && err == NULL)
8540 err = unlockerr;
8541 got_ref_close(base_branch_ref);
8543 return err;
8546 const struct got_error *
8547 got_worktree_merge_postpone(struct got_worktree *worktree,
8548 struct got_fileindex *fileindex)
8550 const struct got_error *err, *sync_err;
8551 char *fileindex_path = NULL;
8553 err = get_fileindex_path(&fileindex_path, worktree);
8554 if (err)
8555 goto done;
8557 sync_err = sync_fileindex(fileindex, fileindex_path);
8559 err = lock_worktree(worktree, LOCK_SH);
8560 if (sync_err && err == NULL)
8561 err = sync_err;
8562 done:
8563 got_fileindex_free(fileindex);
8564 free(fileindex_path);
8565 return err;
8568 static const struct got_error *
8569 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8571 const struct got_error *err;
8572 char *branch_refname = NULL, *commit_refname = NULL;
8574 err = get_merge_branch_ref_name(&branch_refname, worktree);
8575 if (err)
8576 goto done;
8577 err = delete_ref(branch_refname, repo);
8578 if (err)
8579 goto done;
8581 err = get_merge_commit_ref_name(&commit_refname, worktree);
8582 if (err)
8583 goto done;
8584 err = delete_ref(commit_refname, repo);
8585 if (err)
8586 goto done;
8588 done:
8589 free(branch_refname);
8590 free(commit_refname);
8591 return err;
8594 struct merge_commit_msg_arg {
8595 struct got_worktree *worktree;
8596 const char *branch_name;
8599 static const struct got_error *
8600 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8601 const char *diff_path, char **logmsg, void *arg)
8603 struct merge_commit_msg_arg *a = arg;
8605 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8606 got_worktree_get_head_ref_name(a->worktree)) == -1)
8607 return got_error_from_errno("asprintf");
8609 return NULL;
8613 const struct got_error *
8614 got_worktree_merge_branch(struct got_worktree *worktree,
8615 struct got_fileindex *fileindex,
8616 struct got_object_id *yca_commit_id,
8617 struct got_object_id *branch_tip,
8618 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8619 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8621 const struct got_error *err;
8622 char *fileindex_path = NULL;
8623 struct check_mixed_commits_args cma;
8625 err = get_fileindex_path(&fileindex_path, worktree);
8626 if (err)
8627 goto done;
8629 cma.worktree = worktree;
8630 cma.cancel_cb = cancel_cb;
8631 cma.cancel_arg = cancel_arg;
8633 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8634 &cma);
8635 if (err)
8636 goto done;
8638 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8639 branch_tip, repo, progress_cb, progress_arg,
8640 cancel_cb, cancel_arg);
8641 done:
8642 free(fileindex_path);
8643 return err;
8646 const struct got_error *
8647 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8648 struct got_worktree *worktree, struct got_fileindex *fileindex,
8649 const char *author, const char *committer, int allow_bad_symlinks,
8650 struct got_object_id *branch_tip, const char *branch_name,
8651 int allow_conflict, struct got_repository *repo,
8652 got_worktree_status_cb status_cb, void *status_arg)
8655 const struct got_error *err = NULL, *sync_err;
8656 struct got_pathlist_head commitable_paths;
8657 struct collect_commitables_arg cc_arg;
8658 struct got_pathlist_entry *pe;
8659 struct got_reference *head_ref = NULL;
8660 struct got_object_id *head_commit_id = NULL;
8661 int have_staged_files = 0;
8662 struct merge_commit_msg_arg mcm_arg;
8663 char *fileindex_path = NULL;
8665 memset(&cc_arg, 0, sizeof(cc_arg));
8666 *new_commit_id = NULL;
8668 TAILQ_INIT(&commitable_paths);
8670 err = get_fileindex_path(&fileindex_path, worktree);
8671 if (err)
8672 goto done;
8674 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8675 if (err)
8676 goto done;
8678 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8679 if (err)
8680 goto done;
8682 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8683 &have_staged_files);
8684 if (err && err->code != GOT_ERR_CANCELLED)
8685 goto done;
8686 if (have_staged_files) {
8687 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8688 goto done;
8691 cc_arg.commitable_paths = &commitable_paths;
8692 cc_arg.worktree = worktree;
8693 cc_arg.fileindex = fileindex;
8694 cc_arg.repo = repo;
8695 cc_arg.have_staged_files = have_staged_files;
8696 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8697 cc_arg.commit_conflicts = allow_conflict;
8698 err = worktree_status(worktree, "", fileindex, repo,
8699 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8700 if (err)
8701 goto done;
8703 mcm_arg.worktree = worktree;
8704 mcm_arg.branch_name = branch_name;
8705 err = commit_worktree(new_commit_id, &commitable_paths,
8706 head_commit_id, branch_tip, worktree, author, committer, NULL,
8707 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8708 if (err)
8709 goto done;
8711 err = update_fileindex_after_commit(worktree, &commitable_paths,
8712 *new_commit_id, fileindex, have_staged_files);
8713 sync_err = sync_fileindex(fileindex, fileindex_path);
8714 if (sync_err && err == NULL)
8715 err = sync_err;
8716 done:
8717 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8718 struct got_commitable *ct = pe->data;
8720 free_commitable(ct);
8722 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8723 free(fileindex_path);
8724 return err;
8727 const struct got_error *
8728 got_worktree_merge_complete(struct got_worktree *worktree,
8729 struct got_fileindex *fileindex, struct got_repository *repo)
8731 const struct got_error *err, *unlockerr, *sync_err;
8732 char *fileindex_path = NULL;
8734 err = delete_merge_refs(worktree, repo);
8735 if (err)
8736 goto done;
8738 err = get_fileindex_path(&fileindex_path, worktree);
8739 if (err)
8740 goto done;
8741 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8742 sync_err = sync_fileindex(fileindex, fileindex_path);
8743 if (sync_err && err == NULL)
8744 err = sync_err;
8745 done:
8746 got_fileindex_free(fileindex);
8747 free(fileindex_path);
8748 unlockerr = lock_worktree(worktree, LOCK_SH);
8749 if (unlockerr && err == NULL)
8750 err = unlockerr;
8751 return err;
8754 const struct got_error *
8755 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8756 struct got_repository *repo)
8758 const struct got_error *err;
8759 char *branch_refname = NULL;
8760 struct got_reference *branch_ref = NULL;
8762 *in_progress = 0;
8764 err = get_merge_branch_ref_name(&branch_refname, worktree);
8765 if (err)
8766 return err;
8767 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8768 free(branch_refname);
8769 if (err) {
8770 if (err->code != GOT_ERR_NOT_REF)
8771 return err;
8772 } else
8773 *in_progress = 1;
8775 return NULL;
8778 const struct got_error *got_worktree_merge_prepare(
8779 struct got_fileindex **fileindex, struct got_worktree *worktree,
8780 struct got_repository *repo)
8782 const struct got_error *err = NULL;
8783 char *fileindex_path = NULL;
8784 struct got_reference *wt_branch = NULL;
8785 struct got_object_id *wt_branch_tip = NULL;
8786 struct check_rebase_ok_arg ok_arg;
8788 *fileindex = NULL;
8790 err = lock_worktree(worktree, LOCK_EX);
8791 if (err)
8792 return err;
8794 err = open_fileindex(fileindex, &fileindex_path, worktree);
8795 if (err)
8796 goto done;
8798 /* Preconditions are the same as for rebase. */
8799 ok_arg.worktree = worktree;
8800 ok_arg.repo = repo;
8801 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8802 &ok_arg);
8803 if (err)
8804 goto done;
8806 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8807 0);
8808 if (err)
8809 goto done;
8811 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8812 if (err)
8813 goto done;
8815 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8816 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8817 goto done;
8820 done:
8821 free(fileindex_path);
8822 if (wt_branch)
8823 got_ref_close(wt_branch);
8824 free(wt_branch_tip);
8825 if (err) {
8826 if (*fileindex) {
8827 got_fileindex_free(*fileindex);
8828 *fileindex = NULL;
8830 lock_worktree(worktree, LOCK_SH);
8832 return err;
8835 const struct got_error *got_worktree_merge_write_refs(
8836 struct got_worktree *worktree, struct got_reference *branch,
8837 struct got_repository *repo)
8839 const struct got_error *err = NULL;
8840 char *branch_refname = NULL, *commit_refname = NULL;
8841 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8842 struct got_object_id *branch_tip = NULL;
8844 err = get_merge_branch_ref_name(&branch_refname, worktree);
8845 if (err)
8846 return err;
8848 err = get_merge_commit_ref_name(&commit_refname, worktree);
8849 if (err)
8850 return err;
8852 err = got_ref_resolve(&branch_tip, repo, branch);
8853 if (err)
8854 goto done;
8856 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8857 if (err)
8858 goto done;
8859 err = got_ref_write(branch_ref, repo);
8860 if (err)
8861 goto done;
8863 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8864 if (err)
8865 goto done;
8866 err = got_ref_write(commit_ref, repo);
8867 if (err)
8868 goto done;
8870 done:
8871 free(branch_refname);
8872 free(commit_refname);
8873 if (branch_ref)
8874 got_ref_close(branch_ref);
8875 if (commit_ref)
8876 got_ref_close(commit_ref);
8877 free(branch_tip);
8878 return err;
8881 const struct got_error *
8882 got_worktree_merge_continue(char **branch_name,
8883 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8884 struct got_worktree *worktree, struct got_repository *repo)
8886 const struct got_error *err;
8887 char *commit_refname = NULL, *branch_refname = NULL;
8888 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8889 char *fileindex_path = NULL;
8890 int have_staged_files = 0;
8892 *branch_name = NULL;
8893 *branch_tip = NULL;
8894 *fileindex = NULL;
8896 err = lock_worktree(worktree, LOCK_EX);
8897 if (err)
8898 return err;
8900 err = open_fileindex(fileindex, &fileindex_path, worktree);
8901 if (err)
8902 goto done;
8904 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8905 &have_staged_files);
8906 if (err && err->code != GOT_ERR_CANCELLED)
8907 goto done;
8908 if (have_staged_files) {
8909 err = got_error(GOT_ERR_STAGED_PATHS);
8910 goto done;
8913 err = get_merge_branch_ref_name(&branch_refname, worktree);
8914 if (err)
8915 goto done;
8917 err = get_merge_commit_ref_name(&commit_refname, worktree);
8918 if (err)
8919 goto done;
8921 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8922 if (err)
8923 goto done;
8925 if (!got_ref_is_symbolic(branch_ref)) {
8926 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8927 "%s is not a symbolic reference",
8928 got_ref_get_name(branch_ref));
8929 goto done;
8931 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8932 if (*branch_name == NULL) {
8933 err = got_error_from_errno("strdup");
8934 goto done;
8937 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8938 if (err)
8939 goto done;
8941 err = got_ref_resolve(branch_tip, repo, commit_ref);
8942 if (err)
8943 goto done;
8944 done:
8945 free(commit_refname);
8946 free(branch_refname);
8947 free(fileindex_path);
8948 if (commit_ref)
8949 got_ref_close(commit_ref);
8950 if (branch_ref)
8951 got_ref_close(branch_ref);
8952 if (err) {
8953 if (*branch_name) {
8954 free(*branch_name);
8955 *branch_name = NULL;
8957 free(*branch_tip);
8958 *branch_tip = NULL;
8959 if (*fileindex) {
8960 got_fileindex_free(*fileindex);
8961 *fileindex = NULL;
8963 lock_worktree(worktree, LOCK_SH);
8965 return err;
8968 const struct got_error *
8969 got_worktree_merge_info(char **branch_name, struct got_worktree *worktree,
8970 struct got_repository *repo)
8972 const struct got_error *err;
8973 char *branch_refname = NULL;
8974 struct got_reference *branch_ref = NULL;
8976 *branch_name = NULL;
8978 err = get_merge_branch_ref_name(&branch_refname, worktree);
8979 if (err)
8980 goto done;
8982 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8983 if (err)
8984 goto done;
8986 if (!got_ref_is_symbolic(branch_ref)) {
8987 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8988 "%s is not a symbolic reference",
8989 got_ref_get_name(branch_ref));
8990 goto done;
8992 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8993 if (*branch_name == NULL) {
8994 err = got_error_from_errno("strdup");
8995 goto done;
8998 done:
8999 free(branch_refname);
9000 if (branch_ref)
9001 got_ref_close(branch_ref);
9002 if (err) {
9003 if (*branch_name) {
9004 free(*branch_name);
9005 *branch_name = NULL;
9008 return err;
9011 const struct got_error *
9012 got_worktree_merge_abort(struct got_worktree *worktree,
9013 struct got_fileindex *fileindex, struct got_repository *repo,
9014 got_worktree_checkout_cb progress_cb, void *progress_arg)
9016 const struct got_error *err, *unlockerr, *sync_err;
9017 struct got_commit_object *commit = NULL;
9018 char *fileindex_path = NULL;
9019 struct revert_file_args rfa;
9020 char *commit_ref_name = NULL;
9021 struct got_reference *commit_ref = NULL;
9022 struct got_object_id *merged_commit_id = NULL;
9023 struct got_object_id *tree_id = NULL;
9024 struct got_pathlist_head added_paths;
9026 TAILQ_INIT(&added_paths);
9028 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
9029 if (err)
9030 goto done;
9032 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
9033 if (err)
9034 goto done;
9036 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
9037 if (err)
9038 goto done;
9041 * Determine which files in added status can be safely removed
9042 * from disk while reverting changes in the work tree.
9043 * We want to avoid deleting unrelated files which were added by
9044 * the user for conflict resolution purposes.
9046 err = get_paths_added_between_commits(&added_paths,
9047 got_worktree_get_base_commit_id(worktree), merged_commit_id,
9048 got_worktree_get_path_prefix(worktree), repo);
9049 if (err)
9050 goto done;
9053 err = got_object_open_as_commit(&commit, repo,
9054 worktree->base_commit_id);
9055 if (err)
9056 goto done;
9058 err = got_object_id_by_path(&tree_id, repo, commit,
9059 worktree->path_prefix);
9060 if (err)
9061 goto done;
9063 err = delete_merge_refs(worktree, repo);
9064 if (err)
9065 goto done;
9067 err = get_fileindex_path(&fileindex_path, worktree);
9068 if (err)
9069 goto done;
9071 rfa.worktree = worktree;
9072 rfa.fileindex = fileindex;
9073 rfa.progress_cb = progress_cb;
9074 rfa.progress_arg = progress_arg;
9075 rfa.patch_cb = NULL;
9076 rfa.patch_arg = NULL;
9077 rfa.repo = repo;
9078 rfa.unlink_added_files = 1;
9079 rfa.added_files_to_unlink = &added_paths;
9080 err = worktree_status(worktree, "", fileindex, repo,
9081 revert_file, &rfa, NULL, NULL, 1, 0);
9082 if (err)
9083 goto sync;
9085 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
9086 repo, progress_cb, progress_arg, NULL, NULL);
9087 sync:
9088 sync_err = sync_fileindex(fileindex, fileindex_path);
9089 if (sync_err && err == NULL)
9090 err = sync_err;
9091 done:
9092 free(tree_id);
9093 free(merged_commit_id);
9094 if (commit)
9095 got_object_commit_close(commit);
9096 if (fileindex)
9097 got_fileindex_free(fileindex);
9098 free(fileindex_path);
9099 if (commit_ref)
9100 got_ref_close(commit_ref);
9101 free(commit_ref_name);
9103 unlockerr = lock_worktree(worktree, LOCK_SH);
9104 if (unlockerr && err == NULL)
9105 err = unlockerr;
9106 return err;
9109 struct check_stage_ok_arg {
9110 struct got_object_id *head_commit_id;
9111 struct got_worktree *worktree;
9112 struct got_fileindex *fileindex;
9113 struct got_repository *repo;
9114 int have_changes;
9117 static const struct got_error *
9118 check_stage_ok(void *arg, unsigned char status,
9119 unsigned char staged_status, const char *relpath,
9120 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9121 struct got_object_id *commit_id, int dirfd, const char *de_name)
9123 struct check_stage_ok_arg *a = arg;
9124 const struct got_error *err = NULL;
9125 struct got_fileindex_entry *ie;
9126 struct got_object_id base_commit_id;
9127 struct got_object_id *base_commit_idp = NULL;
9128 char *in_repo_path = NULL, *p;
9130 if (status == GOT_STATUS_UNVERSIONED ||
9131 status == GOT_STATUS_NO_CHANGE)
9132 return NULL;
9133 if (status == GOT_STATUS_NONEXISTENT)
9134 return got_error_set_errno(ENOENT, relpath);
9136 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9137 if (ie == NULL)
9138 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9140 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
9141 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
9142 relpath) == -1)
9143 return got_error_from_errno("asprintf");
9145 if (got_fileindex_entry_has_commit(ie)) {
9146 base_commit_idp = got_fileindex_entry_get_commit_id(
9147 &base_commit_id, ie);
9150 if (status == GOT_STATUS_CONFLICT) {
9151 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9152 goto done;
9153 } else if (status != GOT_STATUS_ADD &&
9154 status != GOT_STATUS_MODIFY &&
9155 status != GOT_STATUS_DELETE) {
9156 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9157 goto done;
9160 a->have_changes = 1;
9162 p = in_repo_path;
9163 while (p[0] == '/')
9164 p++;
9165 err = check_out_of_date(p, status, staged_status,
9166 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9167 GOT_ERR_STAGE_OUT_OF_DATE);
9168 done:
9169 free(in_repo_path);
9170 return err;
9173 struct stage_path_arg {
9174 struct got_worktree *worktree;
9175 struct got_fileindex *fileindex;
9176 struct got_repository *repo;
9177 got_worktree_status_cb status_cb;
9178 void *status_arg;
9179 got_worktree_patch_cb patch_cb;
9180 void *patch_arg;
9181 int staged_something;
9182 int allow_bad_symlinks;
9185 static const struct got_error *
9186 stage_path(void *arg, unsigned char status,
9187 unsigned char staged_status, const char *relpath,
9188 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9189 struct got_object_id *commit_id, int dirfd, const char *de_name)
9191 struct stage_path_arg *a = arg;
9192 const struct got_error *err = NULL;
9193 struct got_fileindex_entry *ie;
9194 char *ondisk_path = NULL, *path_content = NULL;
9195 uint32_t stage;
9196 struct got_object_id *new_staged_blob_id = NULL;
9197 struct stat sb;
9199 if (status == GOT_STATUS_UNVERSIONED)
9200 return NULL;
9202 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9203 if (ie == NULL)
9204 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9206 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9207 relpath)== -1)
9208 return got_error_from_errno("asprintf");
9210 switch (status) {
9211 case GOT_STATUS_ADD:
9212 case GOT_STATUS_MODIFY:
9213 /* XXX could sb.st_mode be passed in by our caller? */
9214 if (lstat(ondisk_path, &sb) == -1) {
9215 err = got_error_from_errno2("lstat", ondisk_path);
9216 break;
9218 if (a->patch_cb) {
9219 if (status == GOT_STATUS_ADD) {
9220 int choice = GOT_PATCH_CHOICE_NONE;
9221 err = (*a->patch_cb)(&choice, a->patch_arg,
9222 status, ie->path, NULL, 1, 1);
9223 if (err)
9224 break;
9225 if (choice != GOT_PATCH_CHOICE_YES)
9226 break;
9227 } else {
9228 err = create_patched_content(&path_content, 0,
9229 staged_blob_id ? staged_blob_id : blob_id,
9230 ondisk_path, dirfd, de_name, ie->path,
9231 a->repo, a->patch_cb, a->patch_arg);
9232 if (err || path_content == NULL)
9233 break;
9236 err = got_object_blob_create(&new_staged_blob_id,
9237 path_content ? path_content : ondisk_path, a->repo);
9238 if (err)
9239 break;
9240 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9241 SHA1_DIGEST_LENGTH);
9242 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9243 stage = GOT_FILEIDX_STAGE_ADD;
9244 else
9245 stage = GOT_FILEIDX_STAGE_MODIFY;
9246 got_fileindex_entry_stage_set(ie, stage);
9247 if (S_ISLNK(sb.st_mode)) {
9248 int is_bad_symlink = 0;
9249 if (!a->allow_bad_symlinks) {
9250 char target_path[PATH_MAX];
9251 ssize_t target_len;
9252 target_len = readlink(ondisk_path, target_path,
9253 sizeof(target_path));
9254 if (target_len == -1) {
9255 err = got_error_from_errno2("readlink",
9256 ondisk_path);
9257 break;
9259 err = is_bad_symlink_target(&is_bad_symlink,
9260 target_path, target_len, ondisk_path,
9261 a->worktree->root_path,
9262 a->worktree->meta_dir);
9263 if (err)
9264 break;
9265 if (is_bad_symlink) {
9266 err = got_error_path(ondisk_path,
9267 GOT_ERR_BAD_SYMLINK);
9268 break;
9271 if (is_bad_symlink)
9272 got_fileindex_entry_staged_filetype_set(ie,
9273 GOT_FILEIDX_MODE_BAD_SYMLINK);
9274 else
9275 got_fileindex_entry_staged_filetype_set(ie,
9276 GOT_FILEIDX_MODE_SYMLINK);
9277 } else {
9278 got_fileindex_entry_staged_filetype_set(ie,
9279 GOT_FILEIDX_MODE_REGULAR_FILE);
9281 a->staged_something = 1;
9282 if (a->status_cb == NULL)
9283 break;
9284 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9285 get_staged_status(ie), relpath, blob_id,
9286 new_staged_blob_id, NULL, dirfd, de_name);
9287 if (err)
9288 break;
9290 * When staging the reverse of the staged diff,
9291 * implicitly unstage the file.
9293 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9294 sizeof(ie->blob_sha1)) == 0) {
9295 got_fileindex_entry_stage_set(ie,
9296 GOT_FILEIDX_STAGE_NONE);
9298 break;
9299 case GOT_STATUS_DELETE:
9300 if (staged_status == GOT_STATUS_DELETE)
9301 break;
9302 if (a->patch_cb) {
9303 int choice = GOT_PATCH_CHOICE_NONE;
9304 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9305 ie->path, NULL, 1, 1);
9306 if (err)
9307 break;
9308 if (choice == GOT_PATCH_CHOICE_NO)
9309 break;
9310 if (choice != GOT_PATCH_CHOICE_YES) {
9311 err = got_error(GOT_ERR_PATCH_CHOICE);
9312 break;
9315 stage = GOT_FILEIDX_STAGE_DELETE;
9316 got_fileindex_entry_stage_set(ie, stage);
9317 a->staged_something = 1;
9318 if (a->status_cb == NULL)
9319 break;
9320 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9321 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9322 de_name);
9323 break;
9324 case GOT_STATUS_NO_CHANGE:
9325 break;
9326 case GOT_STATUS_CONFLICT:
9327 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9328 break;
9329 case GOT_STATUS_NONEXISTENT:
9330 err = got_error_set_errno(ENOENT, relpath);
9331 break;
9332 default:
9333 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9334 break;
9337 if (path_content && unlink(path_content) == -1 && err == NULL)
9338 err = got_error_from_errno2("unlink", path_content);
9339 free(path_content);
9340 free(ondisk_path);
9341 free(new_staged_blob_id);
9342 return err;
9345 const struct got_error *
9346 got_worktree_stage(struct got_worktree *worktree,
9347 struct got_pathlist_head *paths,
9348 got_worktree_status_cb status_cb, void *status_arg,
9349 got_worktree_patch_cb patch_cb, void *patch_arg,
9350 int allow_bad_symlinks, struct got_repository *repo)
9352 const struct got_error *err = NULL, *sync_err, *unlockerr;
9353 struct got_pathlist_entry *pe;
9354 struct got_fileindex *fileindex = NULL;
9355 char *fileindex_path = NULL;
9356 struct got_reference *head_ref = NULL;
9357 struct got_object_id *head_commit_id = NULL;
9358 struct check_stage_ok_arg oka;
9359 struct stage_path_arg spa;
9361 err = lock_worktree(worktree, LOCK_EX);
9362 if (err)
9363 return err;
9365 err = got_ref_open(&head_ref, repo,
9366 got_worktree_get_head_ref_name(worktree), 0);
9367 if (err)
9368 goto done;
9369 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9370 if (err)
9371 goto done;
9372 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9373 if (err)
9374 goto done;
9376 /* Check pre-conditions before staging anything. */
9377 oka.head_commit_id = head_commit_id;
9378 oka.worktree = worktree;
9379 oka.fileindex = fileindex;
9380 oka.repo = repo;
9381 oka.have_changes = 0;
9382 TAILQ_FOREACH(pe, paths, entry) {
9383 err = worktree_status(worktree, pe->path, fileindex, repo,
9384 check_stage_ok, &oka, NULL, NULL, 1, 0);
9385 if (err)
9386 goto done;
9388 if (!oka.have_changes) {
9389 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9390 goto done;
9393 spa.worktree = worktree;
9394 spa.fileindex = fileindex;
9395 spa.repo = repo;
9396 spa.patch_cb = patch_cb;
9397 spa.patch_arg = patch_arg;
9398 spa.status_cb = status_cb;
9399 spa.status_arg = status_arg;
9400 spa.staged_something = 0;
9401 spa.allow_bad_symlinks = allow_bad_symlinks;
9402 TAILQ_FOREACH(pe, paths, entry) {
9403 err = worktree_status(worktree, pe->path, fileindex, repo,
9404 stage_path, &spa, NULL, NULL, 1, 0);
9405 if (err)
9406 goto done;
9408 if (!spa.staged_something) {
9409 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9410 goto done;
9413 sync_err = sync_fileindex(fileindex, fileindex_path);
9414 if (sync_err && err == NULL)
9415 err = sync_err;
9416 done:
9417 if (head_ref)
9418 got_ref_close(head_ref);
9419 free(head_commit_id);
9420 free(fileindex_path);
9421 if (fileindex)
9422 got_fileindex_free(fileindex);
9423 unlockerr = lock_worktree(worktree, LOCK_SH);
9424 if (unlockerr && err == NULL)
9425 err = unlockerr;
9426 return err;
9429 struct unstage_path_arg {
9430 struct got_worktree *worktree;
9431 struct got_fileindex *fileindex;
9432 struct got_repository *repo;
9433 got_worktree_checkout_cb progress_cb;
9434 void *progress_arg;
9435 got_worktree_patch_cb patch_cb;
9436 void *patch_arg;
9439 static const struct got_error *
9440 create_unstaged_content(char **path_unstaged_content,
9441 char **path_new_staged_content, struct got_object_id *blob_id,
9442 struct got_object_id *staged_blob_id, const char *relpath,
9443 struct got_repository *repo,
9444 got_worktree_patch_cb patch_cb, void *patch_arg)
9446 const struct got_error *err, *free_err;
9447 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9448 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9449 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9450 struct got_diffreg_result *diffreg_result = NULL;
9451 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9452 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9453 int fd1 = -1, fd2 = -1;
9455 *path_unstaged_content = NULL;
9456 *path_new_staged_content = NULL;
9458 err = got_object_id_str(&label1, blob_id);
9459 if (err)
9460 return err;
9462 fd1 = got_opentempfd();
9463 if (fd1 == -1) {
9464 err = got_error_from_errno("got_opentempfd");
9465 goto done;
9467 fd2 = got_opentempfd();
9468 if (fd2 == -1) {
9469 err = got_error_from_errno("got_opentempfd");
9470 goto done;
9473 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9474 if (err)
9475 goto done;
9477 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9478 if (err)
9479 goto done;
9481 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9482 if (err)
9483 goto done;
9485 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9486 fd2);
9487 if (err)
9488 goto done;
9490 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9491 if (err)
9492 goto done;
9494 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9495 if (err)
9496 goto done;
9498 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9499 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9500 if (err)
9501 goto done;
9503 err = got_opentemp_named(path_unstaged_content, &outfile,
9504 "got-unstaged-content", "");
9505 if (err)
9506 goto done;
9507 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9508 "got-new-staged-content", "");
9509 if (err)
9510 goto done;
9512 if (fseek(f1, 0L, SEEK_SET) == -1) {
9513 err = got_ferror(f1, GOT_ERR_IO);
9514 goto done;
9516 if (fseek(f2, 0L, SEEK_SET) == -1) {
9517 err = got_ferror(f2, GOT_ERR_IO);
9518 goto done;
9520 /* Count the number of actual changes in the diff result. */
9521 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9522 struct diff_chunk_context cc = {};
9523 diff_chunk_context_load_change(&cc, &nchunks_used,
9524 diffreg_result->result, n, 0);
9525 nchanges++;
9527 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9528 int choice;
9529 err = apply_or_reject_change(&choice, &nchunks_used,
9530 diffreg_result->result, n, relpath, f1, f2,
9531 &line_cur1, &line_cur2,
9532 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9533 if (err)
9534 goto done;
9535 if (choice == GOT_PATCH_CHOICE_YES)
9536 have_content = 1;
9537 else
9538 have_rejected_content = 1;
9539 if (choice == GOT_PATCH_CHOICE_QUIT)
9540 break;
9542 if (have_content || have_rejected_content)
9543 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9544 outfile, rejectfile);
9545 done:
9546 free(label1);
9547 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9548 err = got_error_from_errno("close");
9549 if (blob)
9550 got_object_blob_close(blob);
9551 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9552 err = got_error_from_errno("close");
9553 if (staged_blob)
9554 got_object_blob_close(staged_blob);
9555 free_err = got_diffreg_result_free(diffreg_result);
9556 if (free_err && err == NULL)
9557 err = free_err;
9558 if (f1 && fclose(f1) == EOF && err == NULL)
9559 err = got_error_from_errno2("fclose", path1);
9560 if (f2 && fclose(f2) == EOF && err == NULL)
9561 err = got_error_from_errno2("fclose", path2);
9562 if (outfile && fclose(outfile) == EOF && err == NULL)
9563 err = got_error_from_errno2("fclose", *path_unstaged_content);
9564 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9565 err = got_error_from_errno2("fclose", *path_new_staged_content);
9566 if (path1 && unlink(path1) == -1 && err == NULL)
9567 err = got_error_from_errno2("unlink", path1);
9568 if (path2 && unlink(path2) == -1 && err == NULL)
9569 err = got_error_from_errno2("unlink", path2);
9570 if (err || !have_content) {
9571 if (*path_unstaged_content &&
9572 unlink(*path_unstaged_content) == -1 && err == NULL)
9573 err = got_error_from_errno2("unlink",
9574 *path_unstaged_content);
9575 free(*path_unstaged_content);
9576 *path_unstaged_content = NULL;
9578 if (err || !have_content || !have_rejected_content) {
9579 if (*path_new_staged_content &&
9580 unlink(*path_new_staged_content) == -1 && err == NULL)
9581 err = got_error_from_errno2("unlink",
9582 *path_new_staged_content);
9583 free(*path_new_staged_content);
9584 *path_new_staged_content = NULL;
9586 free(path1);
9587 free(path2);
9588 return err;
9591 static const struct got_error *
9592 unstage_hunks(struct got_object_id *staged_blob_id,
9593 struct got_blob_object *blob_base,
9594 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9595 const char *ondisk_path, const char *label_orig,
9596 struct got_worktree *worktree, struct got_repository *repo,
9597 got_worktree_patch_cb patch_cb, void *patch_arg,
9598 got_worktree_checkout_cb progress_cb, void *progress_arg)
9600 const struct got_error *err = NULL;
9601 char *path_unstaged_content = NULL;
9602 char *path_new_staged_content = NULL;
9603 char *parent = NULL, *base_path = NULL;
9604 char *blob_base_path = NULL;
9605 struct got_object_id *new_staged_blob_id = NULL;
9606 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9607 struct stat sb;
9609 err = create_unstaged_content(&path_unstaged_content,
9610 &path_new_staged_content, blob_id, staged_blob_id,
9611 ie->path, repo, patch_cb, patch_arg);
9612 if (err)
9613 return err;
9615 if (path_unstaged_content == NULL)
9616 return NULL;
9618 if (path_new_staged_content) {
9619 err = got_object_blob_create(&new_staged_blob_id,
9620 path_new_staged_content, repo);
9621 if (err)
9622 goto done;
9625 f = fopen(path_unstaged_content, "re");
9626 if (f == NULL) {
9627 err = got_error_from_errno2("fopen",
9628 path_unstaged_content);
9629 goto done;
9631 if (fstat(fileno(f), &sb) == -1) {
9632 err = got_error_from_errno2("fstat", path_unstaged_content);
9633 goto done;
9635 if (got_fileindex_entry_staged_filetype_get(ie) ==
9636 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9637 char link_target[PATH_MAX];
9638 size_t r;
9639 r = fread(link_target, 1, sizeof(link_target), f);
9640 if (r == 0 && ferror(f)) {
9641 err = got_error_from_errno("fread");
9642 goto done;
9644 if (r >= sizeof(link_target)) { /* should not happen */
9645 err = got_error(GOT_ERR_NO_SPACE);
9646 goto done;
9648 link_target[r] = '\0';
9649 err = merge_symlink(worktree, blob_base,
9650 ondisk_path, ie->path, label_orig, link_target,
9651 worktree->base_commit_id, repo, progress_cb,
9652 progress_arg);
9653 } else {
9654 int local_changes_subsumed;
9656 err = got_path_dirname(&parent, ondisk_path);
9657 if (err)
9658 return err;
9660 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9661 parent) == -1) {
9662 err = got_error_from_errno("asprintf");
9663 base_path = NULL;
9664 goto done;
9667 err = got_opentemp_named(&blob_base_path, &f_base,
9668 base_path, "");
9669 if (err)
9670 goto done;
9671 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9672 blob_base);
9673 if (err)
9674 goto done;
9677 * In order the run a 3-way merge with a symlink we copy the symlink's
9678 * target path into a temporary file and use that file with diff3.
9680 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9681 err = dump_symlink_target_path_to_file(&f_deriv2,
9682 ondisk_path);
9683 if (err)
9684 goto done;
9685 } else {
9686 int fd;
9687 fd = open(ondisk_path,
9688 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9689 if (fd == -1) {
9690 err = got_error_from_errno2("open", ondisk_path);
9691 goto done;
9693 f_deriv2 = fdopen(fd, "r");
9694 if (f_deriv2 == NULL) {
9695 err = got_error_from_errno2("fdopen", ondisk_path);
9696 close(fd);
9697 goto done;
9701 err = merge_file(&local_changes_subsumed, worktree,
9702 f_base, f, f_deriv2, ondisk_path, ie->path,
9703 got_fileindex_perms_to_st(ie),
9704 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9705 repo, progress_cb, progress_arg);
9707 if (err)
9708 goto done;
9710 if (new_staged_blob_id) {
9711 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9712 SHA1_DIGEST_LENGTH);
9713 } else {
9714 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9715 got_fileindex_entry_staged_filetype_set(ie, 0);
9717 done:
9718 free(new_staged_blob_id);
9719 if (path_unstaged_content &&
9720 unlink(path_unstaged_content) == -1 && err == NULL)
9721 err = got_error_from_errno2("unlink", path_unstaged_content);
9722 if (path_new_staged_content &&
9723 unlink(path_new_staged_content) == -1 && err == NULL)
9724 err = got_error_from_errno2("unlink", path_new_staged_content);
9725 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9726 err = got_error_from_errno2("unlink", blob_base_path);
9727 if (f_base && fclose(f_base) == EOF && err == NULL)
9728 err = got_error_from_errno2("fclose", path_unstaged_content);
9729 if (f && fclose(f) == EOF && err == NULL)
9730 err = got_error_from_errno2("fclose", path_unstaged_content);
9731 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9732 err = got_error_from_errno2("fclose", ondisk_path);
9733 free(path_unstaged_content);
9734 free(path_new_staged_content);
9735 free(blob_base_path);
9736 free(parent);
9737 free(base_path);
9738 return err;
9741 static const struct got_error *
9742 unstage_path(void *arg, unsigned char status,
9743 unsigned char staged_status, const char *relpath,
9744 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9745 struct got_object_id *commit_id, int dirfd, const char *de_name)
9747 const struct got_error *err = NULL;
9748 struct unstage_path_arg *a = arg;
9749 struct got_fileindex_entry *ie;
9750 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9751 char *ondisk_path = NULL;
9752 char *id_str = NULL, *label_orig = NULL;
9753 int local_changes_subsumed;
9754 struct stat sb;
9755 int fd1 = -1, fd2 = -1;
9757 if (staged_status != GOT_STATUS_ADD &&
9758 staged_status != GOT_STATUS_MODIFY &&
9759 staged_status != GOT_STATUS_DELETE)
9760 return NULL;
9762 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9763 if (ie == NULL)
9764 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9766 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9767 == -1)
9768 return got_error_from_errno("asprintf");
9770 err = got_object_id_str(&id_str,
9771 commit_id ? commit_id : a->worktree->base_commit_id);
9772 if (err)
9773 goto done;
9774 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9775 id_str) == -1) {
9776 err = got_error_from_errno("asprintf");
9777 goto done;
9780 fd1 = got_opentempfd();
9781 if (fd1 == -1) {
9782 err = got_error_from_errno("got_opentempfd");
9783 goto done;
9785 fd2 = got_opentempfd();
9786 if (fd2 == -1) {
9787 err = got_error_from_errno("got_opentempfd");
9788 goto done;
9791 switch (staged_status) {
9792 case GOT_STATUS_MODIFY:
9793 err = got_object_open_as_blob(&blob_base, a->repo,
9794 blob_id, 8192, fd1);
9795 if (err)
9796 break;
9797 /* fall through */
9798 case GOT_STATUS_ADD:
9799 if (a->patch_cb) {
9800 if (staged_status == GOT_STATUS_ADD) {
9801 int choice = GOT_PATCH_CHOICE_NONE;
9802 err = (*a->patch_cb)(&choice, a->patch_arg,
9803 staged_status, ie->path, NULL, 1, 1);
9804 if (err)
9805 break;
9806 if (choice != GOT_PATCH_CHOICE_YES)
9807 break;
9808 } else {
9809 err = unstage_hunks(staged_blob_id,
9810 blob_base, blob_id, ie, ondisk_path,
9811 label_orig, a->worktree, a->repo,
9812 a->patch_cb, a->patch_arg,
9813 a->progress_cb, a->progress_arg);
9814 break; /* Done with this file. */
9817 err = got_object_open_as_blob(&blob_staged, a->repo,
9818 staged_blob_id, 8192, fd2);
9819 if (err)
9820 break;
9821 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9822 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9823 case GOT_FILEIDX_MODE_REGULAR_FILE:
9824 err = merge_blob(&local_changes_subsumed, a->worktree,
9825 blob_base, ondisk_path, relpath,
9826 got_fileindex_perms_to_st(ie), label_orig,
9827 blob_staged, commit_id ? commit_id :
9828 a->worktree->base_commit_id, a->repo,
9829 a->progress_cb, a->progress_arg);
9830 break;
9831 case GOT_FILEIDX_MODE_SYMLINK:
9832 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9833 char *staged_target;
9834 err = got_object_blob_read_to_str(
9835 &staged_target, blob_staged);
9836 if (err)
9837 goto done;
9838 err = merge_symlink(a->worktree, blob_base,
9839 ondisk_path, relpath, label_orig,
9840 staged_target, commit_id ? commit_id :
9841 a->worktree->base_commit_id,
9842 a->repo, a->progress_cb, a->progress_arg);
9843 free(staged_target);
9844 } else {
9845 err = merge_blob(&local_changes_subsumed,
9846 a->worktree, blob_base, ondisk_path,
9847 relpath, got_fileindex_perms_to_st(ie),
9848 label_orig, blob_staged,
9849 commit_id ? commit_id :
9850 a->worktree->base_commit_id, a->repo,
9851 a->progress_cb, a->progress_arg);
9853 break;
9854 default:
9855 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9856 break;
9858 if (err == NULL) {
9859 got_fileindex_entry_stage_set(ie,
9860 GOT_FILEIDX_STAGE_NONE);
9861 got_fileindex_entry_staged_filetype_set(ie, 0);
9863 break;
9864 case GOT_STATUS_DELETE:
9865 if (a->patch_cb) {
9866 int choice = GOT_PATCH_CHOICE_NONE;
9867 err = (*a->patch_cb)(&choice, a->patch_arg,
9868 staged_status, ie->path, NULL, 1, 1);
9869 if (err)
9870 break;
9871 if (choice == GOT_PATCH_CHOICE_NO)
9872 break;
9873 if (choice != GOT_PATCH_CHOICE_YES) {
9874 err = got_error(GOT_ERR_PATCH_CHOICE);
9875 break;
9878 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9879 got_fileindex_entry_staged_filetype_set(ie, 0);
9880 err = get_file_status(&status, &sb, ie, ondisk_path,
9881 dirfd, de_name, a->repo);
9882 if (err)
9883 break;
9884 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9885 break;
9887 done:
9888 free(ondisk_path);
9889 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9890 err = got_error_from_errno("close");
9891 if (blob_base)
9892 got_object_blob_close(blob_base);
9893 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9894 err = got_error_from_errno("close");
9895 if (blob_staged)
9896 got_object_blob_close(blob_staged);
9897 free(id_str);
9898 free(label_orig);
9899 return err;
9902 const struct got_error *
9903 got_worktree_unstage(struct got_worktree *worktree,
9904 struct got_pathlist_head *paths,
9905 got_worktree_checkout_cb progress_cb, void *progress_arg,
9906 got_worktree_patch_cb patch_cb, void *patch_arg,
9907 struct got_repository *repo)
9909 const struct got_error *err = NULL, *sync_err, *unlockerr;
9910 struct got_pathlist_entry *pe;
9911 struct got_fileindex *fileindex = NULL;
9912 char *fileindex_path = NULL;
9913 struct unstage_path_arg upa;
9915 err = lock_worktree(worktree, LOCK_EX);
9916 if (err)
9917 return err;
9919 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9920 if (err)
9921 goto done;
9923 upa.worktree = worktree;
9924 upa.fileindex = fileindex;
9925 upa.repo = repo;
9926 upa.progress_cb = progress_cb;
9927 upa.progress_arg = progress_arg;
9928 upa.patch_cb = patch_cb;
9929 upa.patch_arg = patch_arg;
9930 TAILQ_FOREACH(pe, paths, entry) {
9931 err = worktree_status(worktree, pe->path, fileindex, repo,
9932 unstage_path, &upa, NULL, NULL, 1, 0);
9933 if (err)
9934 goto done;
9937 sync_err = sync_fileindex(fileindex, fileindex_path);
9938 if (sync_err && err == NULL)
9939 err = sync_err;
9940 done:
9941 free(fileindex_path);
9942 if (fileindex)
9943 got_fileindex_free(fileindex);
9944 unlockerr = lock_worktree(worktree, LOCK_SH);
9945 if (unlockerr && err == NULL)
9946 err = unlockerr;
9947 return err;
9950 struct report_file_info_arg {
9951 struct got_worktree *worktree;
9952 got_worktree_path_info_cb info_cb;
9953 void *info_arg;
9954 struct got_pathlist_head *paths;
9955 got_cancel_cb cancel_cb;
9956 void *cancel_arg;
9959 static const struct got_error *
9960 report_file_info(void *arg, struct got_fileindex_entry *ie)
9962 const struct got_error *err;
9963 struct report_file_info_arg *a = arg;
9964 struct got_pathlist_entry *pe;
9965 struct got_object_id blob_id, staged_blob_id, commit_id;
9966 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9967 struct got_object_id *commit_idp = NULL;
9968 int stage;
9970 if (a->cancel_cb) {
9971 err = a->cancel_cb(a->cancel_arg);
9972 if (err)
9973 return err;
9976 TAILQ_FOREACH(pe, a->paths, entry) {
9977 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9978 got_path_is_child(ie->path, pe->path, pe->path_len))
9979 break;
9981 if (pe == NULL) /* not found */
9982 return NULL;
9984 if (got_fileindex_entry_has_blob(ie))
9985 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9986 stage = got_fileindex_entry_stage_get(ie);
9987 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9988 stage == GOT_FILEIDX_STAGE_ADD) {
9989 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9990 &staged_blob_id, ie);
9993 if (got_fileindex_entry_has_commit(ie))
9994 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9996 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9997 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
10000 const struct got_error *
10001 got_worktree_path_info(struct got_worktree *worktree,
10002 struct got_pathlist_head *paths,
10003 got_worktree_path_info_cb info_cb, void *info_arg,
10004 got_cancel_cb cancel_cb, void *cancel_arg)
10007 const struct got_error *err = NULL, *unlockerr;
10008 struct got_fileindex *fileindex = NULL;
10009 char *fileindex_path = NULL;
10010 struct report_file_info_arg arg;
10012 err = lock_worktree(worktree, LOCK_SH);
10013 if (err)
10014 return err;
10016 err = open_fileindex(&fileindex, &fileindex_path, worktree);
10017 if (err)
10018 goto done;
10020 arg.worktree = worktree;
10021 arg.info_cb = info_cb;
10022 arg.info_arg = info_arg;
10023 arg.paths = paths;
10024 arg.cancel_cb = cancel_cb;
10025 arg.cancel_arg = cancel_arg;
10026 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
10027 &arg);
10028 done:
10029 free(fileindex_path);
10030 if (fileindex)
10031 got_fileindex_free(fileindex);
10032 unlockerr = lock_worktree(worktree, LOCK_UN);
10033 if (unlockerr && err == NULL)
10034 err = unlockerr;
10035 return err;
10038 static const struct got_error *
10039 patch_check_path(const char *p, char **path, unsigned char *status,
10040 unsigned char *staged_status, struct got_fileindex *fileindex,
10041 struct got_worktree *worktree, struct got_repository *repo)
10043 const struct got_error *err;
10044 struct got_fileindex_entry *ie;
10045 struct stat sb;
10046 char *ondisk_path = NULL;
10048 err = got_worktree_resolve_path(path, worktree, p);
10049 if (err)
10050 return err;
10052 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
10053 *path[0] ? "/" : "", *path) == -1)
10054 return got_error_from_errno("asprintf");
10056 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
10057 if (ie) {
10058 *staged_status = get_staged_status(ie);
10059 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
10060 repo);
10061 if (err)
10062 goto done;
10063 } else {
10064 *staged_status = GOT_STATUS_NO_CHANGE;
10065 *status = GOT_STATUS_UNVERSIONED;
10066 if (lstat(ondisk_path, &sb) == -1) {
10067 if (errno != ENOENT) {
10068 err = got_error_from_errno2("lstat",
10069 ondisk_path);
10070 goto done;
10072 *status = GOT_STATUS_NONEXISTENT;
10076 done:
10077 free(ondisk_path);
10078 return err;
10081 static const struct got_error *
10082 patch_can_rm(const char *path, unsigned char status,
10083 unsigned char staged_status)
10085 if (status == GOT_STATUS_NONEXISTENT)
10086 return got_error_set_errno(ENOENT, path);
10087 if (status != GOT_STATUS_NO_CHANGE &&
10088 status != GOT_STATUS_ADD &&
10089 status != GOT_STATUS_MODIFY &&
10090 status != GOT_STATUS_MODE_CHANGE)
10091 return got_error_path(path, GOT_ERR_FILE_STATUS);
10092 if (staged_status == GOT_STATUS_DELETE)
10093 return got_error_path(path, GOT_ERR_FILE_STATUS);
10094 return NULL;
10097 static const struct got_error *
10098 patch_can_add(const char *path, unsigned char status)
10100 if (status != GOT_STATUS_NONEXISTENT)
10101 return got_error_path(path, GOT_ERR_FILE_STATUS);
10102 return NULL;
10105 static const struct got_error *
10106 patch_can_edit(const char *path, unsigned char status,
10107 unsigned char staged_status)
10109 if (status == GOT_STATUS_NONEXISTENT)
10110 return got_error_set_errno(ENOENT, path);
10111 if (status != GOT_STATUS_NO_CHANGE &&
10112 status != GOT_STATUS_ADD &&
10113 status != GOT_STATUS_MODIFY)
10114 return got_error_path(path, GOT_ERR_FILE_STATUS);
10115 if (staged_status == GOT_STATUS_DELETE)
10116 return got_error_path(path, GOT_ERR_FILE_STATUS);
10117 return NULL;
10120 const struct got_error *
10121 got_worktree_patch_prepare(struct got_fileindex **fileindex,
10122 char **fileindex_path, struct got_worktree *worktree)
10124 return open_fileindex(fileindex, fileindex_path, worktree);
10127 const struct got_error *
10128 got_worktree_patch_check_path(const char *old, const char *new,
10129 char **oldpath, char **newpath, struct got_worktree *worktree,
10130 struct got_repository *repo, struct got_fileindex *fileindex)
10132 const struct got_error *err = NULL;
10133 int file_renamed = 0;
10134 unsigned char status_old, staged_status_old;
10135 unsigned char status_new, staged_status_new;
10137 *oldpath = NULL;
10138 *newpath = NULL;
10140 err = patch_check_path(old != NULL ? old : new, oldpath,
10141 &status_old, &staged_status_old, fileindex, worktree, repo);
10142 if (err)
10143 goto done;
10145 err = patch_check_path(new != NULL ? new : old, newpath,
10146 &status_new, &staged_status_new, fileindex, worktree, repo);
10147 if (err)
10148 goto done;
10150 if (old != NULL && new != NULL && strcmp(old, new) != 0)
10151 file_renamed = 1;
10153 if (old != NULL && new == NULL)
10154 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10155 else if (file_renamed) {
10156 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10157 if (err == NULL)
10158 err = patch_can_add(*newpath, status_new);
10159 } else if (old == NULL)
10160 err = patch_can_add(*newpath, status_new);
10161 else
10162 err = patch_can_edit(*newpath, status_new, staged_status_new);
10164 done:
10165 if (err) {
10166 free(*oldpath);
10167 *oldpath = NULL;
10168 free(*newpath);
10169 *newpath = NULL;
10171 return err;
10174 const struct got_error *
10175 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10176 struct got_worktree *worktree, struct got_fileindex *fileindex,
10177 got_worktree_checkout_cb progress_cb, void *progress_arg)
10179 struct schedule_addition_args saa;
10181 memset(&saa, 0, sizeof(saa));
10182 saa.worktree = worktree;
10183 saa.fileindex = fileindex;
10184 saa.progress_cb = progress_cb;
10185 saa.progress_arg = progress_arg;
10186 saa.repo = repo;
10188 return worktree_status(worktree, path, fileindex, repo,
10189 schedule_addition, &saa, NULL, NULL, 1, 0);
10192 const struct got_error *
10193 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10194 struct got_worktree *worktree, struct got_fileindex *fileindex,
10195 got_worktree_delete_cb progress_cb, void *progress_arg)
10197 const struct got_error *err;
10198 struct schedule_deletion_args sda;
10199 char *ondisk_status_path;
10201 memset(&sda, 0, sizeof(sda));
10202 sda.worktree = worktree;
10203 sda.fileindex = fileindex;
10204 sda.progress_cb = progress_cb;
10205 sda.progress_arg = progress_arg;
10206 sda.repo = repo;
10207 sda.delete_local_mods = 0;
10208 sda.keep_on_disk = 0;
10209 sda.ignore_missing_paths = 0;
10210 sda.status_codes = NULL;
10211 if (asprintf(&ondisk_status_path, "%s/%s",
10212 got_worktree_get_root_path(worktree), path) == -1)
10213 return got_error_from_errno("asprintf");
10214 sda.status_path = ondisk_status_path;
10215 sda.status_path_len = strlen(ondisk_status_path);
10217 err = worktree_status(worktree, path, fileindex, repo,
10218 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10219 free(ondisk_status_path);
10220 return err;
10223 const struct got_error *
10224 got_worktree_patch_complete(struct got_fileindex *fileindex,
10225 const char *fileindex_path)
10227 const struct got_error *err = NULL;
10229 err = sync_fileindex(fileindex, fileindex_path);
10230 got_fileindex_free(fileindex);
10232 return err;