Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_diff.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t
69 apply_umask(mode_t mode)
70 {
71 mode_t um;
73 um = umask(000);
74 umask(um);
75 return mode & ~um;
76 }
78 static const struct got_error *
79 create_meta_file(const char *path_got, const char *name, const char *content)
80 {
81 const struct got_error *err = NULL;
82 char *path;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1)
85 return got_error_from_errno("asprintf");
87 err = got_path_create_file(path, content);
88 free(path);
89 return err;
90 }
92 static const struct got_error *
93 update_meta_file(const char *path_got, const char *name, const char *content)
94 {
95 const struct got_error *err = NULL;
96 FILE *tmpfile = NULL;
97 char *tmppath = NULL;
98 char *path = NULL;
100 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 err = got_error_from_errno("asprintf");
102 path = NULL;
103 goto done;
106 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
107 if (err)
108 goto done;
110 if (content) {
111 int len = fprintf(tmpfile, "%s\n", content);
112 if (len != strlen(content) + 1) {
113 err = got_error_from_errno2("fprintf", tmppath);
114 goto done;
118 if (rename(tmppath, path) != 0) {
119 err = got_error_from_errno3("rename", tmppath, path);
120 unlink(tmppath);
121 goto done;
124 done:
125 if (fclose(tmpfile) == EOF && err == NULL)
126 err = got_error_from_errno2("fclose", tmppath);
127 free(tmppath);
128 return err;
131 static const struct got_error *
132 write_head_ref(const char *path_got, struct got_reference *head_ref)
134 const struct got_error *err = NULL;
135 char *refstr = NULL;
137 if (got_ref_is_symbolic(head_ref)) {
138 refstr = got_ref_to_str(head_ref);
139 if (refstr == NULL)
140 return got_error_from_errno("got_ref_to_str");
141 } else {
142 refstr = strdup(got_ref_get_name(head_ref));
143 if (refstr == NULL)
144 return got_error_from_errno("strdup");
146 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
147 free(refstr);
148 return err;
151 const struct got_error *
152 got_worktree_init(const char *path, struct got_reference *head_ref,
153 const char *prefix, const char *meta_dir, struct got_repository *repo)
155 const struct got_error *err = NULL;
156 struct got_object_id *commit_id = NULL;
157 uuid_t uuid;
158 uint32_t uuid_status;
159 int obj_type;
160 char *path_got = NULL;
161 char *formatstr = NULL;
162 char *absprefix = NULL;
163 char *basestr = NULL;
164 char *uuidstr = NULL;
166 if (strcmp(path, got_repo_get_path(repo)) == 0) {
167 err = got_error(GOT_ERR_WORKTREE_REPO);
168 goto done;
171 err = got_ref_resolve(&commit_id, repo, head_ref);
172 if (err)
173 return err;
174 err = got_object_get_type(&obj_type, repo, commit_id);
175 if (err)
176 return err;
177 if (obj_type != GOT_OBJ_TYPE_COMMIT)
178 return got_error(GOT_ERR_OBJ_TYPE);
180 if (!got_path_is_absolute(prefix)) {
181 if (asprintf(&absprefix, "/%s", prefix) == -1)
182 return got_error_from_errno("asprintf");
185 /* Create top-level directory (may already exist). */
186 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 err = got_error_from_errno2("mkdir", path);
188 goto done;
191 /* Create .got/.cvg directory (may already exist). */
192 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
193 err = got_error_from_errno("asprintf");
194 goto done;
196 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
197 err = got_error_from_errno2("mkdir", path_got);
198 goto done;
201 /* Create an empty lock file. */
202 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 if (err)
204 goto done;
206 /* Create an empty file index. */
207 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 if (err)
209 goto done;
211 /* Write the HEAD reference. */
212 err = write_head_ref(path_got, head_ref);
213 if (err)
214 goto done;
216 /* Record our base commit. */
217 err = got_object_id_str(&basestr, commit_id);
218 if (err)
219 goto done;
220 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 if (err)
222 goto done;
224 /* Store path to repository. */
225 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
226 got_repo_get_path(repo));
227 if (err)
228 goto done;
230 /* Store in-repository path prefix. */
231 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
232 absprefix ? absprefix : prefix);
233 if (err)
234 goto done;
236 /* Generate UUID. */
237 uuid_create(&uuid, &uuid_status);
238 if (uuid_status != uuid_s_ok) {
239 err = got_error_uuid(uuid_status, "uuid_create");
240 goto done;
242 uuid_to_string(&uuid, &uuidstr, &uuid_status);
243 if (uuid_status != uuid_s_ok) {
244 err = got_error_uuid(uuid_status, "uuid_to_string");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 if (err)
249 goto done;
251 /* Stamp work tree with format file. */
252 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
253 err = got_error_from_errno("asprintf");
254 goto done;
256 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
257 if (err)
258 goto done;
260 done:
261 free(commit_id);
262 free(path_got);
263 free(formatstr);
264 free(absprefix);
265 free(basestr);
266 free(uuidstr);
267 return err;
270 const struct got_error *
271 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
272 const char *path_prefix)
274 char *absprefix = NULL;
276 if (!got_path_is_absolute(path_prefix)) {
277 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
278 return got_error_from_errno("asprintf");
280 *match = (strcmp(absprefix ? absprefix : path_prefix,
281 worktree->path_prefix) == 0);
282 free(absprefix);
283 return NULL;
286 const char *
287 got_worktree_get_head_ref_name(struct got_worktree *worktree)
289 return worktree->head_ref_name;
292 const struct got_error *
293 got_worktree_set_head_ref(struct got_worktree *worktree,
294 struct got_reference *head_ref)
296 const struct got_error *err = NULL;
297 char *path_got = NULL, *head_ref_name = NULL;
299 if (asprintf(&path_got, "%s/%s", worktree->root_path,
300 worktree->meta_dir) == -1) {
301 err = got_error_from_errno("asprintf");
302 path_got = NULL;
303 goto done;
306 head_ref_name = strdup(got_ref_get_name(head_ref));
307 if (head_ref_name == NULL) {
308 err = got_error_from_errno("strdup");
309 goto done;
312 err = write_head_ref(path_got, head_ref);
313 if (err)
314 goto done;
316 free(worktree->head_ref_name);
317 worktree->head_ref_name = head_ref_name;
318 done:
319 free(path_got);
320 if (err)
321 free(head_ref_name);
322 return err;
325 struct got_object_id *
326 got_worktree_get_base_commit_id(struct got_worktree *worktree)
328 return worktree->base_commit_id;
331 const struct got_error *
332 got_worktree_set_base_commit_id(struct got_worktree *worktree,
333 struct got_repository *repo, struct got_object_id *commit_id)
335 const struct got_error *err;
336 struct got_object *obj = NULL;
337 char *id_str = NULL;
338 char *path_got = NULL;
340 if (asprintf(&path_got, "%s/%s", worktree->root_path,
341 worktree->meta_dir) == -1) {
342 err = got_error_from_errno("asprintf");
343 path_got = NULL;
344 goto done;
347 err = got_object_open(&obj, repo, commit_id);
348 if (err)
349 return err;
351 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 err = got_error(GOT_ERR_OBJ_TYPE);
353 goto done;
356 /* Record our base commit. */
357 err = got_object_id_str(&id_str, commit_id);
358 if (err)
359 goto done;
360 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 if (err)
362 goto done;
364 free(worktree->base_commit_id);
365 worktree->base_commit_id = got_object_id_dup(commit_id);
366 if (worktree->base_commit_id == NULL) {
367 err = got_error_from_errno("got_object_id_dup");
368 goto done;
370 done:
371 if (obj)
372 got_object_close(obj);
373 free(id_str);
374 free(path_got);
375 return err;
378 const struct got_gotconfig *
379 got_worktree_get_gotconfig(struct got_worktree *worktree)
381 return worktree->gotconfig;
384 static const struct got_error *
385 lock_worktree(struct got_worktree *worktree, int operation)
387 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 : got_error_from_errno2("flock",
390 got_worktree_get_root_path(worktree)));
391 return NULL;
394 static const struct got_error *
395 add_dir_on_disk(struct got_worktree *worktree, const char *path)
397 const struct got_error *err = NULL;
398 char *abspath;
400 /* We only accept worktree-relative paths here. */
401 if (got_path_is_absolute(path)) {
402 return got_error_fmt(GOT_ERR_BAD_PATH,
403 "%s does not accept absolute paths: %s",
404 __func__, path);
407 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 return got_error_from_errno("asprintf");
410 err = got_path_mkdir(abspath);
411 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
412 struct stat sb;
413 err = NULL;
414 if (lstat(abspath, &sb) == -1) {
415 err = got_error_from_errno2("lstat", abspath);
416 } else if (!S_ISDIR(sb.st_mode)) {
417 /* TODO directory is obstructed; do something */
418 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
421 free(abspath);
422 return err;
425 static const struct got_error *
426 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
428 const struct got_error *err = NULL;
429 uint8_t fbuf1[8192];
430 uint8_t fbuf2[8192];
431 size_t flen1 = 0, flen2 = 0;
433 *same = 1;
435 for (;;) {
436 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 if (flen1 == 0 && ferror(f1)) {
438 err = got_error_from_errno("fread");
439 break;
441 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 if (flen2 == 0 && ferror(f2)) {
443 err = got_error_from_errno("fread");
444 break;
446 if (flen1 == 0) {
447 if (flen2 != 0)
448 *same = 0;
449 break;
450 } else if (flen2 == 0) {
451 if (flen1 != 0)
452 *same = 0;
453 break;
454 } else if (flen1 == flen2) {
455 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
456 *same = 0;
457 break;
459 } else {
460 *same = 0;
461 break;
465 return err;
468 static const struct got_error *
469 check_files_equal(int *same, FILE *f1, FILE *f2)
471 struct stat sb;
472 size_t size1, size2;
474 *same = 1;
476 if (fstat(fileno(f1), &sb) != 0)
477 return got_error_from_errno("fstat");
478 size1 = sb.st_size;
480 if (fstat(fileno(f2), &sb) != 0)
481 return got_error_from_errno("fstat");
482 size2 = sb.st_size;
484 if (size1 != size2) {
485 *same = 0;
486 return NULL;
489 if (fseek(f1, 0L, SEEK_SET) == -1)
490 return got_ferror(f1, GOT_ERR_IO);
491 if (fseek(f2, 0L, SEEK_SET) == -1)
492 return got_ferror(f2, GOT_ERR_IO);
494 return check_file_contents_equal(same, f1, f2);
497 static const struct got_error *
498 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
500 uint8_t fbuf[65536];
501 size_t flen;
502 ssize_t outlen;
504 *outsize = 0;
506 if (fseek(f, 0L, SEEK_SET) == -1)
507 return got_ferror(f, GOT_ERR_IO);
509 for (;;) {
510 flen = fread(fbuf, 1, sizeof(fbuf), f);
511 if (flen == 0) {
512 if (ferror(f))
513 return got_error_from_errno("fread");
514 if (feof(f))
515 break;
517 outlen = write(outfd, fbuf, flen);
518 if (outlen == -1)
519 return got_error_from_errno("write");
520 if (outlen != flen)
521 return got_error(GOT_ERR_IO);
522 *outsize += outlen;
525 return NULL;
528 static const struct got_error *
529 merge_binary_file(int *overlapcnt, int merged_fd,
530 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 const char *ondisk_path)
534 const struct got_error *err = NULL;
535 int same_content, changed_deriv, changed_deriv2;
536 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 char *base_path_orig = NULL, *base_path_deriv = NULL;
540 char *base_path_deriv2 = NULL;
542 *overlapcnt = 0;
544 err = check_files_equal(&same_content, f_deriv, f_deriv2);
545 if (err)
546 return err;
548 if (same_content)
549 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
551 err = check_files_equal(&same_content, f_deriv, f_orig);
552 if (err)
553 return err;
554 changed_deriv = !same_content;
555 err = check_files_equal(&same_content, f_deriv2, f_orig);
556 if (err)
557 return err;
558 changed_deriv2 = !same_content;
560 if (changed_deriv && changed_deriv2) {
561 *overlapcnt = 1;
562 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 err = got_error_from_errno("asprintf");
564 goto done;
566 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 err = got_error_from_errno("asprintf");
568 goto done;
570 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 err = got_error_from_errno("asprintf");
572 goto done;
574 err = got_opentemp_named_fd(&path_orig, &fd_orig,
575 base_path_orig, "");
576 if (err)
577 goto done;
578 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 base_path_deriv, "");
580 if (err)
581 goto done;
582 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 base_path_deriv2, "");
584 if (err)
585 goto done;
586 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
587 if (err)
588 goto done;
589 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
590 if (err)
591 goto done;
592 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
593 if (err)
594 goto done;
595 if (dprintf(merged_fd, "Binary files differ and cannot be "
596 "merged automatically:\n") < 0) {
597 err = got_error_from_errno("dprintf");
598 goto done;
600 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 label_deriv ? " " : "",
603 label_deriv ? label_deriv : "",
604 path_deriv) < 0) {
605 err = got_error_from_errno("dprintf");
606 goto done;
608 if (size_orig > 0) {
609 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 GOT_DIFF_CONFLICT_MARKER_ORIG,
611 label_orig ? " " : "",
612 label_orig ? label_orig : "",
613 path_orig) < 0) {
614 err = got_error_from_errno("dprintf");
615 goto done;
618 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 GOT_DIFF_CONFLICT_MARKER_SEP,
620 path_deriv2,
621 GOT_DIFF_CONFLICT_MARKER_END,
622 label_deriv2 ? " " : "",
623 label_deriv2 ? label_deriv2 : "") < 0) {
624 err = got_error_from_errno("dprintf");
625 goto done;
627 } else if (changed_deriv)
628 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 else if (changed_deriv2)
630 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
631 done:
632 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
633 err == NULL)
634 err = got_error_from_errno2("unlink", path_orig);
635 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 err = got_error_from_errno2("close", path_orig);
637 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 err = got_error_from_errno2("close", path_deriv);
639 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 err = got_error_from_errno2("close", path_deriv2);
641 free(path_orig);
642 free(path_deriv);
643 free(path_deriv2);
644 free(base_path_orig);
645 free(base_path_deriv);
646 free(base_path_deriv2);
647 return err;
650 /*
651 * Perform a 3-way merge where the file f_orig acts as the common
652 * ancestor, the file f_deriv acts as the first derived version,
653 * and the file f_deriv2 acts as the second derived version.
654 * The merge result will be written to a new file at ondisk_path; any
655 * existing file at this path will be replaced.
656 */
657 static const struct got_error *
658 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 const char *path, uint16_t st_mode,
661 const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 got_worktree_checkout_cb progress_cb, void *progress_arg)
665 const struct got_error *err = NULL;
666 int merged_fd = -1;
667 FILE *f_merged = NULL;
668 char *merged_path = NULL, *base_path = NULL;
669 int overlapcnt = 0;
670 char *parent = NULL;
672 *local_changes_subsumed = 0;
674 err = got_path_dirname(&parent, ondisk_path);
675 if (err)
676 return err;
678 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 err = got_error_from_errno("asprintf");
680 goto done;
683 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 if (err)
685 goto done;
687 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
689 if (err) {
690 if (err->code != GOT_ERR_FILE_BINARY)
691 goto done;
692 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
694 ondisk_path);
695 if (err)
696 goto done;
699 err = (*progress_cb)(progress_arg,
700 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 if (err)
702 goto done;
704 if (fsync(merged_fd) != 0) {
705 err = got_error_from_errno("fsync");
706 goto done;
709 f_merged = fdopen(merged_fd, "r");
710 if (f_merged == NULL) {
711 err = got_error_from_errno("fdopen");
712 goto done;
714 merged_fd = -1;
716 /* Check if a clean merge has subsumed all local changes. */
717 if (overlapcnt == 0) {
718 err = check_files_equal(local_changes_subsumed, f_deriv,
719 f_merged);
720 if (err)
721 goto done;
724 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 err = got_error_from_errno2("fchmod", merged_path);
726 goto done;
729 if (rename(merged_path, ondisk_path) != 0) {
730 err = got_error_from_errno3("rename", merged_path,
731 ondisk_path);
732 goto done;
734 done:
735 if (err) {
736 if (merged_path)
737 unlink(merged_path);
739 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 err = got_error_from_errno("close");
741 if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 err = got_error_from_errno("fclose");
743 free(merged_path);
744 free(base_path);
745 free(parent);
746 return err;
749 static const struct got_error *
750 update_symlink(const char *ondisk_path, const char *target_path,
751 size_t target_len)
753 /* This is not atomic but matches what 'ln -sf' does. */
754 if (unlink(ondisk_path) == -1)
755 return got_error_from_errno2("unlink", ondisk_path);
756 if (symlink(target_path, ondisk_path) == -1)
757 return got_error_from_errno3("symlink", target_path,
758 ondisk_path);
759 return NULL;
762 /*
763 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 * in the work tree with a file that contains conflict markers and the
765 * conflicting target paths of the original version, a "derived version"
766 * of a symlink from an incoming change, and a local version of the symlink.
768 * The original versions's target path can be NULL if it is not available,
769 * such as if both derived versions added a new symlink at the same path.
771 * The incoming derived symlink target is NULL in case the incoming change
772 * has deleted this symlink.
773 */
774 static const struct got_error *
775 install_symlink_conflict(const char *deriv_target,
776 struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 const char *label_orig, const char *local_target, const char *ondisk_path)
779 const struct got_error *err;
780 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
781 FILE *f = NULL;
783 err = got_object_id_str(&id_str, deriv_base_commit_id);
784 if (err)
785 return got_error_from_errno("asprintf");
787 if (asprintf(&label_deriv, "%s: commit %s",
788 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 err = got_error_from_errno("asprintf");
790 goto done;
793 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
794 if (err)
795 goto done;
797 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 err = got_error_from_errno2("fchmod", path);
799 goto done;
802 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 deriv_target ? deriv_target : "(symlink was deleted)",
805 orig_target ? label_orig : "",
806 orig_target ? "\n" : "",
807 orig_target ? orig_target : "",
808 orig_target ? "\n" : "",
809 GOT_DIFF_CONFLICT_MARKER_SEP,
810 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 err = got_error_from_errno2("fprintf", path);
812 goto done;
815 if (unlink(ondisk_path) == -1) {
816 err = got_error_from_errno2("unlink", ondisk_path);
817 goto done;
819 if (rename(path, ondisk_path) == -1) {
820 err = got_error_from_errno3("rename", path, ondisk_path);
821 goto done;
823 done:
824 if (f != NULL && fclose(f) == EOF && err == NULL)
825 err = got_error_from_errno2("fclose", path);
826 free(path);
827 free(id_str);
828 free(label_deriv);
829 return err;
832 /* forward declaration */
833 static const struct got_error *
834 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 const char *, const char *, uint16_t, const char *,
836 struct got_blob_object *, struct got_object_id *,
837 struct got_repository *, got_worktree_checkout_cb, void *);
839 /*
840 * Merge a symlink into the work tree, where blob_orig acts as the common
841 * ancestor, deriv_target is the link target of the first derived version,
842 * and the symlink on disk acts as the second derived version.
843 * Assume that contents of both blobs represent symlinks.
844 */
845 static const struct got_error *
846 merge_symlink(struct got_worktree *worktree,
847 struct got_blob_object *blob_orig, const char *ondisk_path,
848 const char *path, const char *label_orig, const char *deriv_target,
849 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 got_worktree_checkout_cb progress_cb, void *progress_arg)
852 const struct got_error *err = NULL;
853 char *ancestor_target = NULL;
854 struct stat sb;
855 ssize_t ondisk_len, deriv_len;
856 char ondisk_target[PATH_MAX];
857 int have_local_change = 0;
858 int have_incoming_change = 0;
860 if (lstat(ondisk_path, &sb) == -1)
861 return got_error_from_errno2("lstat", ondisk_path);
863 ondisk_len = readlink(ondisk_path, ondisk_target,
864 sizeof(ondisk_target));
865 if (ondisk_len == -1) {
866 err = got_error_from_errno2("readlink",
867 ondisk_path);
868 goto done;
870 ondisk_target[ondisk_len] = '\0';
872 if (blob_orig) {
873 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
874 if (err)
875 goto done;
878 if (ancestor_target == NULL ||
879 (ondisk_len != strlen(ancestor_target) ||
880 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 have_local_change = 1;
883 deriv_len = strlen(deriv_target);
884 if (ancestor_target == NULL ||
885 (deriv_len != strlen(ancestor_target) ||
886 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 have_incoming_change = 1;
889 if (!have_local_change && !have_incoming_change) {
890 if (ancestor_target) {
891 /* Both sides made the same change. */
892 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
893 path);
894 } else if (deriv_len == ondisk_len &&
895 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 /* Both sides added the same symlink. */
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
898 path);
899 } else {
900 /* Both sides added symlinks which don't match. */
901 err = install_symlink_conflict(deriv_target,
902 deriv_base_commit_id, ancestor_target,
903 label_orig, ondisk_target, ondisk_path);
904 if (err)
905 goto done;
906 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
907 path);
909 } else if (!have_local_change && have_incoming_change) {
910 /* Apply the incoming change. */
911 err = update_symlink(ondisk_path, deriv_target,
912 strlen(deriv_target));
913 if (err)
914 goto done;
915 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 } else if (have_local_change && have_incoming_change) {
917 if (deriv_len == ondisk_len &&
918 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 /* Both sides made the same change. */
920 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
921 path);
922 } else {
923 err = install_symlink_conflict(deriv_target,
924 deriv_base_commit_id, ancestor_target, label_orig,
925 ondisk_target, ondisk_path);
926 if (err)
927 goto done;
928 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
929 path);
933 done:
934 free(ancestor_target);
935 return err;
938 static const struct got_error *
939 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
941 const struct got_error *err = NULL;
942 char target_path[PATH_MAX];
943 ssize_t target_len;
944 size_t n;
945 FILE *f;
947 *outfile = NULL;
949 f = got_opentemp();
950 if (f == NULL)
951 return got_error_from_errno("got_opentemp");
952 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 if (target_len == -1) {
954 err = got_error_from_errno2("readlink", ondisk_path);
955 goto done;
957 n = fwrite(target_path, 1, target_len, f);
958 if (n != target_len) {
959 err = got_ferror(f, GOT_ERR_IO);
960 goto done;
962 if (fflush(f) == EOF) {
963 err = got_error_from_errno("fflush");
964 goto done;
966 if (fseek(f, 0L, SEEK_SET) == -1) {
967 err = got_ferror(f, GOT_ERR_IO);
968 goto done;
970 done:
971 if (err)
972 fclose(f);
973 else
974 *outfile = f;
975 return err;
978 /*
979 * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 * blob_deriv acts as the first derived version, and the file on disk
981 * acts as the second derived version.
982 */
983 static const struct got_error *
984 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 struct got_blob_object *blob_orig, const char *ondisk_path,
986 const char *path, uint16_t st_mode, const char *label_orig,
987 struct got_blob_object *blob_deriv,
988 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 got_worktree_checkout_cb progress_cb, void *progress_arg)
991 const struct got_error *err = NULL;
992 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 char *blob_orig_path = NULL;
994 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 char *label_deriv = NULL, *parent = NULL;
997 *local_changes_subsumed = 0;
999 err = got_path_dirname(&parent, ondisk_path);
1000 if (err)
1001 return err;
1003 if (blob_orig) {
1004 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1005 parent) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 base_path = NULL;
1008 goto done;
1011 err = got_opentemp_named(&blob_orig_path, &f_orig,
1012 base_path, "");
1013 if (err)
1014 goto done;
1015 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1016 blob_orig);
1017 if (err)
1018 goto done;
1019 free(base_path);
1020 } else {
1022 * No common ancestor exists. This is an "add vs add" conflict
1023 * and we simply use an empty ancestor file to make both files
1024 * appear in the merged result in their entirety.
1026 f_orig = got_opentemp();
1027 if (f_orig == NULL) {
1028 err = got_error_from_errno("got_opentemp");
1029 goto done;
1033 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 base_path = NULL;
1036 goto done;
1039 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1040 if (err)
1041 goto done;
1042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1043 blob_deriv);
1044 if (err)
1045 goto done;
1047 err = got_object_id_str(&id_str, deriv_base_commit_id);
1048 if (err)
1049 goto done;
1050 if (asprintf(&label_deriv, "%s: commit %s",
1051 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 err = got_error_from_errno("asprintf");
1053 goto done;
1057 * In order the run a 3-way merge with a symlink we copy the symlink's
1058 * target path into a temporary file and use that file with diff3.
1060 if (S_ISLNK(st_mode)) {
1061 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1062 if (err)
1063 goto done;
1064 } else {
1065 int fd;
1066 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1067 if (fd == -1) {
1068 err = got_error_from_errno2("open", ondisk_path);
1069 goto done;
1071 f_deriv2 = fdopen(fd, "r");
1072 if (f_deriv2 == NULL) {
1073 err = got_error_from_errno2("fdopen", ondisk_path);
1074 close(fd);
1075 goto done;
1079 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1082 done:
1083 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 err = got_error_from_errno("fclose");
1085 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 err = got_error_from_errno("fclose");
1087 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 err = got_error_from_errno("fclose");
1089 free(base_path);
1090 if (blob_orig_path) {
1091 unlink(blob_orig_path);
1092 free(blob_orig_path);
1094 if (blob_deriv_path) {
1095 unlink(blob_deriv_path);
1096 free(blob_deriv_path);
1098 free(id_str);
1099 free(label_deriv);
1100 free(parent);
1101 return err;
1104 static const struct got_error *
1105 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 int wt_fd, const char *path, struct got_object_id *blob_id,
1108 int update_timestamps)
1110 const struct got_error *err = NULL;
1111 struct got_fileindex_entry *new_ie;
1113 *new_iep = NULL;
1115 err = got_fileindex_entry_alloc(&new_ie, path);
1116 if (err)
1117 return err;
1119 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1120 blob_id->sha1, base_commit_id->sha1, update_timestamps);
1121 if (err)
1122 goto done;
1124 err = got_fileindex_entry_add(fileindex, new_ie);
1125 done:
1126 if (err)
1127 got_fileindex_entry_free(new_ie);
1128 else
1129 *new_iep = new_ie;
1130 return err;
1133 static mode_t
1134 get_ondisk_perms(int executable, mode_t st_mode)
1136 mode_t xbits = S_IXUSR;
1138 if (executable) {
1139 /* Map read bits to execute bits. */
1140 if (st_mode & S_IRGRP)
1141 xbits |= S_IXGRP;
1142 if (st_mode & S_IROTH)
1143 xbits |= S_IXOTH;
1144 return st_mode | xbits;
1147 return st_mode;
1150 /* forward declaration */
1151 static const struct got_error *
1152 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1153 const char *path, mode_t te_mode, mode_t st_mode,
1154 struct got_blob_object *blob, int restoring_missing_file,
1155 int reverting_versioned_file, int installing_bad_symlink,
1156 int path_is_unversioned, int *update_timestamps,
1157 struct got_repository *repo,
1158 got_worktree_checkout_cb progress_cb, void *progress_arg);
1161 * This function assumes that the provided symlink target points at a
1162 * safe location in the work tree!
1164 static const struct got_error *
1165 replace_existing_symlink(int *did_something, const char *ondisk_path,
1166 const char *target_path, size_t target_len)
1168 const struct got_error *err = NULL;
1169 ssize_t elen;
1170 char etarget[PATH_MAX];
1171 int fd;
1173 *did_something = 0;
1176 * "Bad" symlinks (those pointing outside the work tree or into the
1177 * .got directory) are installed in the work tree as a regular file
1178 * which contains the bad symlink target path.
1179 * The new symlink target has already been checked for safety by our
1180 * caller. If we can successfully open a regular file then we simply
1181 * replace this file with a symlink below.
1183 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1184 if (fd == -1) {
1185 if (!got_err_open_nofollow_on_symlink())
1186 return got_error_from_errno2("open", ondisk_path);
1188 /* We are updating an existing on-disk symlink. */
1189 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1190 if (elen == -1)
1191 return got_error_from_errno2("readlink", ondisk_path);
1193 if (elen == target_len &&
1194 memcmp(etarget, target_path, target_len) == 0)
1195 return NULL; /* nothing to do */
1198 *did_something = 1;
1199 err = update_symlink(ondisk_path, target_path, target_len);
1200 if (fd != -1 && close(fd) == -1 && err == NULL)
1201 err = got_error_from_errno2("close", ondisk_path);
1202 return err;
1205 static const struct got_error *
1206 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1207 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1208 const char *meta_dir)
1210 const struct got_error *err = NULL;
1211 char canonpath[PATH_MAX];
1212 char *path_got = NULL;
1214 *is_bad_symlink = 0;
1216 if (target_len >= sizeof(canonpath)) {
1217 *is_bad_symlink = 1;
1218 return NULL;
1222 * We do not use realpath(3) to resolve the symlink's target
1223 * path because we don't want to resolve symlinks recursively.
1224 * Instead we make the path absolute and then canonicalize it.
1225 * Relative symlink target lookup should begin at the directory
1226 * in which the blob object is being installed.
1228 if (!got_path_is_absolute(target_path)) {
1229 char *abspath, *parent;
1230 err = got_path_dirname(&parent, ondisk_path);
1231 if (err)
1232 return err;
1233 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1234 free(parent);
1235 return got_error_from_errno("asprintf");
1237 free(parent);
1238 if (strlen(abspath) >= sizeof(canonpath)) {
1239 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1240 free(abspath);
1241 return err;
1243 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1244 free(abspath);
1245 if (err)
1246 return err;
1247 } else {
1248 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1249 if (err)
1250 return err;
1253 /* Only allow symlinks pointing at paths within the work tree. */
1254 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1255 *is_bad_symlink = 1;
1256 return NULL;
1259 /* Do not allow symlinks pointing into the meta directory. */
1260 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1261 return got_error_from_errno("asprintf");
1262 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1263 *is_bad_symlink = 1;
1265 free(path_got);
1266 return NULL;
1269 static const struct got_error *
1270 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1271 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1272 int restoring_missing_file, int reverting_versioned_file,
1273 int path_is_unversioned, int allow_bad_symlinks,
1274 struct got_repository *repo,
1275 got_worktree_checkout_cb progress_cb, void *progress_arg)
1277 const struct got_error *err = NULL;
1278 char target_path[PATH_MAX];
1279 size_t len, target_len = 0;
1280 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1281 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1283 *is_bad_symlink = 0;
1286 * Blob object content specifies the target path of the link.
1287 * If a symbolic link cannot be installed we instead create
1288 * a regular file which contains the link target path stored
1289 * in the blob object.
1291 do {
1292 err = got_object_blob_read_block(&len, blob);
1293 if (err)
1294 return err;
1296 if (len + target_len >= sizeof(target_path)) {
1297 /* Path too long; install as a regular file. */
1298 *is_bad_symlink = 1;
1299 got_object_blob_rewind(blob);
1300 return install_blob(worktree, ondisk_path, path,
1301 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1302 restoring_missing_file, reverting_versioned_file,
1303 1, path_is_unversioned, NULL, repo, progress_cb,
1304 progress_arg);
1306 if (len > 0) {
1307 /* Skip blob object header first time around. */
1308 memcpy(target_path + target_len, buf + hdrlen,
1309 len - hdrlen);
1310 target_len += len - hdrlen;
1311 hdrlen = 0;
1313 } while (len != 0);
1314 target_path[target_len] = '\0';
1316 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1317 ondisk_path, worktree->root_path, worktree->meta_dir);
1318 if (err)
1319 return err;
1321 if (*is_bad_symlink && !allow_bad_symlinks) {
1322 /* install as a regular file */
1323 got_object_blob_rewind(blob);
1324 err = install_blob(worktree, ondisk_path, path,
1325 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1326 restoring_missing_file, reverting_versioned_file, 1,
1327 path_is_unversioned, NULL, repo,
1328 progress_cb, progress_arg);
1329 return err;
1332 if (symlink(target_path, ondisk_path) == -1) {
1333 if (errno == EEXIST) {
1334 int symlink_replaced;
1335 if (path_is_unversioned) {
1336 err = (*progress_cb)(progress_arg,
1337 GOT_STATUS_UNVERSIONED, path);
1338 return err;
1340 err = replace_existing_symlink(&symlink_replaced,
1341 ondisk_path, target_path, target_len);
1342 if (err)
1343 return err;
1344 if (progress_cb) {
1345 if (symlink_replaced) {
1346 err = (*progress_cb)(progress_arg,
1347 reverting_versioned_file ?
1348 GOT_STATUS_REVERT :
1349 GOT_STATUS_UPDATE, path);
1350 } else {
1351 err = (*progress_cb)(progress_arg,
1352 GOT_STATUS_EXISTS, path);
1355 return err; /* Nothing else to do. */
1358 if (errno == ENOENT) {
1359 char *parent;
1360 err = got_path_dirname(&parent, ondisk_path);
1361 if (err)
1362 return err;
1363 err = got_path_mkdir(parent);
1364 free(parent);
1365 if (err)
1366 return err;
1368 * Retry, and fall through to error handling
1369 * below if this second attempt fails.
1371 if (symlink(target_path, ondisk_path) != -1) {
1372 err = NULL; /* success */
1373 if (progress_cb) {
1374 err = (*progress_cb)(progress_arg,
1375 reverting_versioned_file ?
1376 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1377 path);
1379 return err;
1383 /* Handle errors from first or second creation attempt. */
1384 if (errno == ENAMETOOLONG) {
1385 /* bad target path; install as a regular file */
1386 *is_bad_symlink = 1;
1387 got_object_blob_rewind(blob);
1388 err = install_blob(worktree, ondisk_path, path,
1389 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1390 restoring_missing_file, reverting_versioned_file, 1,
1391 path_is_unversioned, NULL, repo,
1392 progress_cb, progress_arg);
1393 } else if (errno == ENOTDIR) {
1394 err = got_error_path(ondisk_path,
1395 GOT_ERR_FILE_OBSTRUCTED);
1396 } else {
1397 err = got_error_from_errno3("symlink",
1398 target_path, ondisk_path);
1400 } else if (progress_cb)
1401 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1402 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1403 return err;
1406 static const struct got_error *
1407 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1408 const char *path, mode_t te_mode, mode_t st_mode,
1409 struct got_blob_object *blob, int restoring_missing_file,
1410 int reverting_versioned_file, int installing_bad_symlink,
1411 int path_is_unversioned, int *update_timestamps,
1412 struct got_repository *repo,
1413 got_worktree_checkout_cb progress_cb, void *progress_arg)
1415 const struct got_error *err = NULL;
1416 int fd = -1;
1417 size_t len, hdrlen;
1418 int update = 0;
1419 char *tmppath = NULL;
1420 mode_t mode;
1422 if (update_timestamps)
1423 *update_timestamps = 1;
1425 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1426 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1427 O_CLOEXEC, mode);
1428 if (fd == -1) {
1429 if (errno == ENOENT || errno == ENOTDIR) {
1430 char *parent;
1431 err = got_path_dirname(&parent, path);
1432 if (err)
1433 return err;
1434 err = add_dir_on_disk(worktree, parent);
1435 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1436 err = got_error_path(path, err->code);
1437 free(parent);
1438 if (err)
1439 return err;
1440 fd = open(ondisk_path,
1441 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1442 mode);
1443 if (fd == -1)
1444 return got_error_from_errno2("open",
1445 ondisk_path);
1446 } else if (errno == EEXIST) {
1447 if (path_is_unversioned) {
1448 if (update_timestamps)
1449 *update_timestamps = 0;
1450 err = (*progress_cb)(progress_arg,
1451 GOT_STATUS_EXISTS, path);
1452 goto done;
1454 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1455 !S_ISREG(st_mode) && !installing_bad_symlink) {
1456 /* TODO file is obstructed; do something */
1457 err = got_error_path(ondisk_path,
1458 GOT_ERR_FILE_OBSTRUCTED);
1459 goto done;
1460 } else {
1461 err = got_opentemp_named_fd(&tmppath, &fd,
1462 ondisk_path, "");
1463 if (err)
1464 goto done;
1465 update = 1;
1467 if (fchmod(fd, apply_umask(mode)) == -1) {
1468 err = got_error_from_errno2("fchmod",
1469 tmppath);
1470 goto done;
1473 } else
1474 return got_error_from_errno2("open", ondisk_path);
1477 if (progress_cb) {
1478 if (restoring_missing_file)
1479 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1480 path);
1481 else if (reverting_versioned_file)
1482 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1483 path);
1484 else
1485 err = (*progress_cb)(progress_arg,
1486 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1487 if (err)
1488 goto done;
1491 hdrlen = got_object_blob_get_hdrlen(blob);
1492 do {
1493 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1494 err = got_object_blob_read_block(&len, blob);
1495 if (err)
1496 break;
1497 if (len > 0) {
1498 /* Skip blob object header first time around. */
1499 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1500 if (outlen == -1) {
1501 err = got_error_from_errno("write");
1502 goto done;
1503 } else if (outlen != len - hdrlen) {
1504 err = got_error(GOT_ERR_IO);
1505 goto done;
1507 hdrlen = 0;
1509 } while (len != 0);
1511 if (fsync(fd) != 0) {
1512 err = got_error_from_errno("fsync");
1513 goto done;
1516 if (update) {
1517 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1518 err = got_error_from_errno2("unlink", ondisk_path);
1519 goto done;
1521 if (rename(tmppath, ondisk_path) != 0) {
1522 err = got_error_from_errno3("rename", tmppath,
1523 ondisk_path);
1524 goto done;
1526 free(tmppath);
1527 tmppath = NULL;
1530 done:
1531 if (fd != -1 && close(fd) == -1 && err == NULL)
1532 err = got_error_from_errno("close");
1533 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1534 err = got_error_from_errno2("unlink", tmppath);
1535 free(tmppath);
1536 return err;
1540 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1541 * conflict marker is found in newly added lines only.
1543 static const struct got_error *
1544 get_modified_file_content_status(unsigned char *status,
1545 struct got_blob_object *blob, const char *path, struct stat *sb,
1546 FILE *ondisk_file)
1548 const struct got_error *err, *free_err;
1549 const char *markers[3] = {
1550 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1551 GOT_DIFF_CONFLICT_MARKER_SEP,
1552 GOT_DIFF_CONFLICT_MARKER_END
1554 FILE *f1 = NULL;
1555 struct got_diffreg_result *diffreg_result = NULL;
1556 struct diff_result *r;
1557 int nchunks_parsed, n, i = 0, ln = 0;
1558 char *line = NULL;
1559 size_t linesize = 0;
1560 ssize_t linelen;
1562 if (*status != GOT_STATUS_MODIFY)
1563 return NULL;
1565 f1 = got_opentemp();
1566 if (f1 == NULL)
1567 return got_error_from_errno("got_opentemp");
1569 if (blob) {
1570 got_object_blob_rewind(blob);
1571 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1572 if (err)
1573 goto done;
1576 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1577 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1578 if (err)
1579 goto done;
1581 r = diffreg_result->result;
1583 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1584 struct diff_chunk *c;
1585 struct diff_chunk_context cc = {};
1586 off_t pos;
1589 * We can optimise a little by advancing straight
1590 * to the next chunk if this one has no added lines.
1592 c = diff_chunk_get(r, n);
1594 if (diff_chunk_type(c) != CHUNK_PLUS) {
1595 nchunks_parsed = 1;
1596 continue; /* removed or unchanged lines */
1599 pos = diff_chunk_get_right_start_pos(c);
1600 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1601 err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 goto done;
1605 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1606 ln = cc.right.start;
1608 while (ln < cc.right.end) {
1609 linelen = getline(&line, &linesize, ondisk_file);
1610 if (linelen == -1) {
1611 if (feof(ondisk_file))
1612 break;
1613 err = got_ferror(ondisk_file, GOT_ERR_IO);
1614 break;
1617 if (line && strncmp(line, markers[i],
1618 strlen(markers[i])) == 0) {
1619 if (strcmp(markers[i],
1620 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1621 *status = GOT_STATUS_CONFLICT;
1622 goto done;
1623 } else
1624 i++;
1626 ++ln;
1630 done:
1631 free(line);
1632 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1633 err = got_error_from_errno("fclose");
1634 free_err = got_diffreg_result_free(diffreg_result);
1635 if (err == NULL)
1636 err = free_err;
1638 return err;
1641 static int
1642 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1644 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1645 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1648 static int
1649 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1651 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1652 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1653 ie->mtime_sec == sb->st_mtim.tv_sec &&
1654 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1655 ie->size == (sb->st_size & 0xffffffff) &&
1656 !xbit_differs(ie, sb->st_mode));
1659 static unsigned char
1660 get_staged_status(struct got_fileindex_entry *ie)
1662 switch (got_fileindex_entry_stage_get(ie)) {
1663 case GOT_FILEIDX_STAGE_ADD:
1664 return GOT_STATUS_ADD;
1665 case GOT_FILEIDX_STAGE_DELETE:
1666 return GOT_STATUS_DELETE;
1667 case GOT_FILEIDX_STAGE_MODIFY:
1668 return GOT_STATUS_MODIFY;
1669 default:
1670 return GOT_STATUS_NO_CHANGE;
1674 static const struct got_error *
1675 get_symlink_modification_status(unsigned char *status,
1676 struct got_fileindex_entry *ie, const char *abspath,
1677 int dirfd, const char *de_name, struct got_blob_object *blob)
1679 const struct got_error *err = NULL;
1680 char target_path[PATH_MAX];
1681 char etarget[PATH_MAX];
1682 ssize_t elen;
1683 size_t len, target_len = 0;
1684 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1685 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1687 *status = GOT_STATUS_NO_CHANGE;
1689 /* Blob object content specifies the target path of the link. */
1690 do {
1691 err = got_object_blob_read_block(&len, blob);
1692 if (err)
1693 return err;
1694 if (len + target_len >= sizeof(target_path)) {
1696 * Should not happen. The blob contents were OK
1697 * when this symlink was installed.
1699 return got_error(GOT_ERR_NO_SPACE);
1701 if (len > 0) {
1702 /* Skip blob object header first time around. */
1703 memcpy(target_path + target_len, buf + hdrlen,
1704 len - hdrlen);
1705 target_len += len - hdrlen;
1706 hdrlen = 0;
1708 } while (len != 0);
1709 target_path[target_len] = '\0';
1711 if (dirfd != -1) {
1712 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1713 if (elen == -1)
1714 return got_error_from_errno2("readlinkat", abspath);
1715 } else {
1716 elen = readlink(abspath, etarget, sizeof(etarget));
1717 if (elen == -1)
1718 return got_error_from_errno2("readlink", abspath);
1721 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1722 *status = GOT_STATUS_MODIFY;
1724 return NULL;
1727 static const struct got_error *
1728 get_file_status(unsigned char *status, struct stat *sb,
1729 struct got_fileindex_entry *ie, const char *abspath,
1730 int dirfd, const char *de_name, struct got_repository *repo)
1732 const struct got_error *err = NULL;
1733 struct got_object_id id;
1734 size_t hdrlen;
1735 int fd = -1, fd1 = -1;
1736 FILE *f = NULL;
1737 uint8_t fbuf[8192];
1738 struct got_blob_object *blob = NULL;
1739 size_t flen, blen;
1740 unsigned char staged_status;
1742 staged_status = get_staged_status(ie);
1743 *status = GOT_STATUS_NO_CHANGE;
1744 memset(sb, 0, sizeof(*sb));
1747 * Whenever the caller provides a directory descriptor and a
1748 * directory entry name for the file, use them! This prevents
1749 * race conditions if filesystem paths change beneath our feet.
1751 if (dirfd != -1) {
1752 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1753 if (errno == ENOENT) {
1754 if (got_fileindex_entry_has_file_on_disk(ie))
1755 *status = GOT_STATUS_MISSING;
1756 else
1757 *status = GOT_STATUS_DELETE;
1758 goto done;
1760 err = got_error_from_errno2("fstatat", abspath);
1761 goto done;
1763 } else {
1764 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1765 if (fd == -1 && errno != ENOENT &&
1766 !got_err_open_nofollow_on_symlink())
1767 return got_error_from_errno2("open", abspath);
1768 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1769 if (lstat(abspath, sb) == -1)
1770 return got_error_from_errno2("lstat", abspath);
1771 } else if (fd == -1 || fstat(fd, sb) == -1) {
1772 if (errno == ENOENT) {
1773 if (got_fileindex_entry_has_file_on_disk(ie))
1774 *status = GOT_STATUS_MISSING;
1775 else
1776 *status = GOT_STATUS_DELETE;
1777 goto done;
1779 err = got_error_from_errno2("fstat", abspath);
1780 goto done;
1784 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1785 *status = GOT_STATUS_OBSTRUCTED;
1786 goto done;
1789 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1790 *status = GOT_STATUS_DELETE;
1791 goto done;
1792 } else if (!got_fileindex_entry_has_blob(ie) &&
1793 staged_status != GOT_STATUS_ADD) {
1794 *status = GOT_STATUS_ADD;
1795 goto done;
1798 if (!stat_info_differs(ie, sb))
1799 goto done;
1801 if (S_ISLNK(sb->st_mode) &&
1802 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1803 *status = GOT_STATUS_MODIFY;
1804 goto done;
1807 if (staged_status == GOT_STATUS_MODIFY ||
1808 staged_status == GOT_STATUS_ADD)
1809 got_fileindex_entry_get_staged_blob_id(&id, ie);
1810 else
1811 got_fileindex_entry_get_blob_id(&id, ie);
1813 fd1 = got_opentempfd();
1814 if (fd1 == -1) {
1815 err = got_error_from_errno("got_opentempfd");
1816 goto done;
1818 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1819 if (err)
1820 goto done;
1822 if (S_ISLNK(sb->st_mode)) {
1823 err = get_symlink_modification_status(status, ie,
1824 abspath, dirfd, de_name, blob);
1825 goto done;
1828 if (dirfd != -1) {
1829 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1830 if (fd == -1) {
1831 err = got_error_from_errno2("openat", abspath);
1832 goto done;
1836 f = fdopen(fd, "r");
1837 if (f == NULL) {
1838 err = got_error_from_errno2("fdopen", abspath);
1839 goto done;
1841 fd = -1;
1842 hdrlen = got_object_blob_get_hdrlen(blob);
1843 for (;;) {
1844 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1845 err = got_object_blob_read_block(&blen, blob);
1846 if (err)
1847 goto done;
1848 /* Skip length of blob object header first time around. */
1849 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1850 if (flen == 0 && ferror(f)) {
1851 err = got_error_from_errno("fread");
1852 goto done;
1854 if (blen - hdrlen == 0) {
1855 if (flen != 0)
1856 *status = GOT_STATUS_MODIFY;
1857 break;
1858 } else if (flen == 0) {
1859 if (blen - hdrlen != 0)
1860 *status = GOT_STATUS_MODIFY;
1861 break;
1862 } else if (blen - hdrlen == flen) {
1863 /* Skip blob object header first time around. */
1864 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1865 *status = GOT_STATUS_MODIFY;
1866 break;
1868 } else {
1869 *status = GOT_STATUS_MODIFY;
1870 break;
1872 hdrlen = 0;
1875 if (*status == GOT_STATUS_MODIFY) {
1876 rewind(f);
1877 err = get_modified_file_content_status(status, blob, ie->path,
1878 sb, f);
1879 } else if (xbit_differs(ie, sb->st_mode))
1880 *status = GOT_STATUS_MODE_CHANGE;
1881 done:
1882 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1883 err = got_error_from_errno("close");
1884 if (blob)
1885 got_object_blob_close(blob);
1886 if (f != NULL && fclose(f) == EOF && err == NULL)
1887 err = got_error_from_errno2("fclose", abspath);
1888 if (fd != -1 && close(fd) == -1 && err == NULL)
1889 err = got_error_from_errno2("close", abspath);
1890 return err;
1894 * Update timestamps in the file index if a file is unmodified and
1895 * we had to run a full content comparison to find out.
1897 static const struct got_error *
1898 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1899 struct got_fileindex_entry *ie, struct stat *sb)
1901 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1902 return got_fileindex_entry_update(ie, wt_fd, path,
1903 ie->blob_sha1, ie->commit_sha1, 1);
1905 return NULL;
1908 static const struct got_error *remove_ondisk_file(const char *, const char *);
1910 static const struct got_error *
1911 update_blob(struct got_worktree *worktree,
1912 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1913 struct got_tree_entry *te, const char *path,
1914 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1915 void *progress_arg)
1917 const struct got_error *err = NULL;
1918 struct got_blob_object *blob = NULL;
1919 char *ondisk_path = NULL;
1920 unsigned char status = GOT_STATUS_NO_CHANGE;
1921 struct stat sb;
1922 int fd1 = -1, fd2 = -1;
1923 int update_timestamps;
1925 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1926 return got_error_from_errno("asprintf");
1928 if (ie) {
1929 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1930 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1931 goto done;
1933 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1934 repo);
1935 if (err)
1936 goto done;
1937 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1938 sb.st_mode = got_fileindex_perms_to_st(ie);
1939 } else {
1940 if (stat(ondisk_path, &sb) == -1) {
1941 if (errno != ENOENT && errno != ENOTDIR) {
1942 err = got_error_from_errno2("stat",
1943 ondisk_path);
1944 goto done;
1946 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1947 status = GOT_STATUS_UNVERSIONED;
1948 } else {
1949 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1950 status = GOT_STATUS_UNVERSIONED;
1951 else
1952 status = GOT_STATUS_OBSTRUCTED;
1956 if (status == GOT_STATUS_OBSTRUCTED) {
1957 if (ie)
1958 got_fileindex_entry_mark_skipped(ie);
1959 err = (*progress_cb)(progress_arg, status, path);
1960 goto done;
1962 if (status == GOT_STATUS_CONFLICT) {
1963 if (ie)
1964 got_fileindex_entry_mark_skipped(ie);
1965 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1966 path);
1967 goto done;
1970 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1971 if (status == GOT_STATUS_UNVERSIONED) {
1972 err = (*progress_cb)(progress_arg, status, path);
1973 } else if (status != GOT_STATUS_NO_CHANGE &&
1974 status != GOT_STATUS_DELETE &&
1975 status != GOT_STATUS_NONEXISTENT &&
1976 status != GOT_STATUS_MISSING) {
1977 err = (*progress_cb)(progress_arg,
1978 GOT_STATUS_CANNOT_DELETE, path);
1979 } else if (ie) {
1980 if (status != GOT_STATUS_DELETE &&
1981 status != GOT_STATUS_NONEXISTENT &&
1982 status != GOT_STATUS_MISSING) {
1983 err = remove_ondisk_file(worktree->root_path,
1984 ie->path);
1985 if (err && !(err->code == GOT_ERR_ERRNO &&
1986 errno == ENOENT))
1987 goto done;
1989 got_fileindex_entry_remove(fileindex, ie);
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1991 ie->path);
1993 goto done; /* nothing else to do */
1996 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1997 (S_ISLNK(te->mode) ||
1998 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
2000 * This is a regular file or an installed bad symlink.
2001 * If the file index indicates that this file is already
2002 * up-to-date with respect to the repository we can skip
2003 * updating contents of this file.
2005 if (got_fileindex_entry_has_commit(ie) &&
2006 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2007 SHA1_DIGEST_LENGTH) == 0) {
2008 /* Same commit. */
2009 err = sync_timestamps(worktree->root_fd,
2010 path, status, ie, &sb);
2011 if (err)
2012 goto done;
2013 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2014 path);
2015 goto done;
2017 if (got_fileindex_entry_has_blob(ie) &&
2018 memcmp(ie->blob_sha1, te->id.sha1,
2019 SHA1_DIGEST_LENGTH) == 0) {
2020 /* Different commit but the same blob. */
2021 if (got_fileindex_entry_has_commit(ie)) {
2022 /* Update the base commit ID of this file. */
2023 memcpy(ie->commit_sha1,
2024 worktree->base_commit_id->sha1,
2025 sizeof(ie->commit_sha1));
2027 err = sync_timestamps(worktree->root_fd,
2028 path, status, ie, &sb);
2029 if (err)
2030 goto done;
2031 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2032 path);
2033 goto done;
2037 fd1 = got_opentempfd();
2038 if (fd1 == -1) {
2039 err = got_error_from_errno("got_opentempfd");
2040 goto done;
2042 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2043 if (err)
2044 goto done;
2046 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2047 int update_timestamps;
2048 struct got_blob_object *blob2 = NULL;
2049 char *label_orig = NULL;
2050 if (got_fileindex_entry_has_blob(ie)) {
2051 fd2 = got_opentempfd();
2052 if (fd2 == -1) {
2053 err = got_error_from_errno("got_opentempfd");
2054 goto done;
2056 struct got_object_id id2;
2057 got_fileindex_entry_get_blob_id(&id2, ie);
2058 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2059 fd2);
2060 if (err)
2061 goto done;
2063 if (got_fileindex_entry_has_commit(ie)) {
2064 char id_str[SHA1_DIGEST_STRING_LENGTH];
2065 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2066 sizeof(id_str)) == NULL) {
2067 err = got_error_path(id_str,
2068 GOT_ERR_BAD_OBJ_ID_STR);
2069 goto done;
2071 if (asprintf(&label_orig, "%s: commit %s",
2072 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2073 err = got_error_from_errno("asprintf");
2074 goto done;
2077 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2078 char *link_target;
2079 err = got_object_blob_read_to_str(&link_target, blob);
2080 if (err)
2081 goto done;
2082 err = merge_symlink(worktree, blob2, ondisk_path, path,
2083 label_orig, link_target, worktree->base_commit_id,
2084 repo, progress_cb, progress_arg);
2085 free(link_target);
2086 } else {
2087 err = merge_blob(&update_timestamps, worktree, blob2,
2088 ondisk_path, path, sb.st_mode, label_orig, blob,
2089 worktree->base_commit_id, repo,
2090 progress_cb, progress_arg);
2092 free(label_orig);
2093 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2094 err = got_error_from_errno("close");
2095 goto done;
2097 if (blob2)
2098 got_object_blob_close(blob2);
2099 if (err)
2100 goto done;
2102 * Do not update timestamps of files with local changes.
2103 * Otherwise, a future status walk would treat them as
2104 * unmodified files again.
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1,
2108 update_timestamps);
2109 } else if (status == GOT_STATUS_MODE_CHANGE) {
2110 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2111 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2112 } else if (status == GOT_STATUS_DELETE) {
2113 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2114 if (err)
2115 goto done;
2116 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2117 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2118 if (err)
2119 goto done;
2120 } else {
2121 int is_bad_symlink = 0;
2122 if (S_ISLNK(te->mode)) {
2123 err = install_symlink(&is_bad_symlink, worktree,
2124 ondisk_path, path, blob,
2125 status == GOT_STATUS_MISSING, 0,
2126 status == GOT_STATUS_UNVERSIONED, 0,
2127 repo, progress_cb, progress_arg);
2128 } else {
2129 err = install_blob(worktree, ondisk_path, path,
2130 te->mode, sb.st_mode, blob,
2131 status == GOT_STATUS_MISSING, 0, 0,
2132 status == GOT_STATUS_UNVERSIONED,
2133 &update_timestamps,
2134 repo, progress_cb, progress_arg);
2136 if (err)
2137 goto done;
2139 if (ie) {
2140 err = got_fileindex_entry_update(ie,
2141 worktree->root_fd, path, blob->id.sha1,
2142 worktree->base_commit_id->sha1, 1);
2143 } else {
2144 err = create_fileindex_entry(&ie, fileindex,
2145 worktree->base_commit_id, worktree->root_fd, path,
2146 &blob->id, update_timestamps);
2148 if (err)
2149 goto done;
2151 if (is_bad_symlink) {
2152 got_fileindex_entry_filetype_set(ie,
2153 GOT_FILEIDX_MODE_BAD_SYMLINK);
2157 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2158 err = got_error_from_errno("close");
2159 goto done;
2161 got_object_blob_close(blob);
2162 done:
2163 free(ondisk_path);
2164 return err;
2167 static const struct got_error *
2168 remove_ondisk_file(const char *root_path, const char *path)
2170 const struct got_error *err = NULL;
2171 char *ondisk_path = NULL, *parent = NULL;
2173 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2174 return got_error_from_errno("asprintf");
2176 if (unlink(ondisk_path) == -1) {
2177 if (errno != ENOENT)
2178 err = got_error_from_errno2("unlink", ondisk_path);
2179 } else {
2180 size_t root_len = strlen(root_path);
2181 err = got_path_dirname(&parent, ondisk_path);
2182 if (err)
2183 goto done;
2184 while (got_path_cmp(parent, root_path,
2185 strlen(parent), root_len) != 0) {
2186 free(ondisk_path);
2187 ondisk_path = parent;
2188 parent = NULL;
2189 if (rmdir(ondisk_path) == -1) {
2190 if (errno != ENOTEMPTY)
2191 err = got_error_from_errno2("rmdir",
2192 ondisk_path);
2193 break;
2195 err = got_path_dirname(&parent, ondisk_path);
2196 if (err)
2197 break;
2200 done:
2201 free(ondisk_path);
2202 free(parent);
2203 return err;
2206 static const struct got_error *
2207 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2208 struct got_fileindex_entry *ie, struct got_repository *repo,
2209 got_worktree_checkout_cb progress_cb, void *progress_arg)
2211 const struct got_error *err = NULL;
2212 unsigned char status;
2213 struct stat sb;
2214 char *ondisk_path;
2216 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2217 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2219 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2220 == -1)
2221 return got_error_from_errno("asprintf");
2223 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2224 if (err)
2225 goto done;
2227 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2228 char ondisk_target[PATH_MAX];
2229 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2230 sizeof(ondisk_target));
2231 if (ondisk_len == -1) {
2232 err = got_error_from_errno2("readlink", ondisk_path);
2233 goto done;
2235 ondisk_target[ondisk_len] = '\0';
2236 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2237 NULL, NULL, /* XXX pass common ancestor info? */
2238 ondisk_target, ondisk_path);
2239 if (err)
2240 goto done;
2241 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2242 ie->path);
2243 goto done;
2246 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2247 status == GOT_STATUS_ADD) {
2248 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2249 if (err)
2250 goto done;
2252 * Preserve the working file and change the deleted blob's
2253 * entry into a schedule-add entry.
2255 err = got_fileindex_entry_update(ie, worktree->root_fd,
2256 ie->path, NULL, NULL, 0);
2257 } else {
2258 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2259 if (err)
2260 goto done;
2261 if (status == GOT_STATUS_NO_CHANGE) {
2262 err = remove_ondisk_file(worktree->root_path, ie->path);
2263 if (err)
2264 goto done;
2266 got_fileindex_entry_remove(fileindex, ie);
2268 done:
2269 free(ondisk_path);
2270 return err;
2273 struct diff_cb_arg {
2274 struct got_fileindex *fileindex;
2275 struct got_worktree *worktree;
2276 struct got_repository *repo;
2277 got_worktree_checkout_cb progress_cb;
2278 void *progress_arg;
2279 got_cancel_cb cancel_cb;
2280 void *cancel_arg;
2283 static const struct got_error *
2284 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2285 struct got_tree_entry *te, const char *parent_path)
2287 const struct got_error *err = NULL;
2288 struct diff_cb_arg *a = arg;
2290 if (a->cancel_cb) {
2291 err = a->cancel_cb(a->cancel_arg);
2292 if (err)
2293 return err;
2296 return update_blob(a->worktree, a->fileindex, ie, te,
2297 ie->path, a->repo, a->progress_cb, a->progress_arg);
2300 static const struct got_error *
2301 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2303 const struct got_error *err = NULL;
2304 struct diff_cb_arg *a = arg;
2306 if (a->cancel_cb) {
2307 err = a->cancel_cb(a->cancel_arg);
2308 if (err)
2309 return err;
2312 return delete_blob(a->worktree, a->fileindex, ie,
2313 a->repo, a->progress_cb, a->progress_arg);
2316 static const struct got_error *
2317 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2319 const struct got_error *err = NULL;
2320 struct diff_cb_arg *a = arg;
2321 char *path;
2323 if (a->cancel_cb) {
2324 err = a->cancel_cb(a->cancel_arg);
2325 if (err)
2326 return err;
2329 if (got_object_tree_entry_is_submodule(te))
2330 return NULL;
2332 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2333 return NULL;
2335 if (asprintf(&path, "%s%s%s", parent_path,
2336 parent_path[0] ? "/" : "", te->name)
2337 == -1)
2338 return got_error_from_errno("asprintf");
2340 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2341 a->repo, a->progress_cb, a->progress_arg);
2343 free(path);
2344 return err;
2347 const struct got_error *
2348 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2350 uint32_t uuid_status;
2352 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2353 if (uuid_status != uuid_s_ok) {
2354 *uuidstr = NULL;
2355 return got_error_uuid(uuid_status, "uuid_to_string");
2358 return NULL;
2361 static const struct got_error *
2362 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2364 const struct got_error *err = NULL;
2365 char *uuidstr = NULL;
2367 *refname = NULL;
2369 err = got_worktree_get_uuid(&uuidstr, worktree);
2370 if (err)
2371 return err;
2373 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2374 err = got_error_from_errno("asprintf");
2375 *refname = NULL;
2377 free(uuidstr);
2378 return err;
2381 const struct got_error *
2382 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2383 const char *prefix)
2385 return get_ref_name(refname, worktree, prefix);
2388 static const struct got_error *
2389 get_base_ref_name(char **refname, struct got_worktree *worktree)
2391 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2394 static const struct got_error *
2395 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2397 return get_ref_name(refname, worktree,
2398 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2401 static const struct got_error *
2402 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2404 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2407 static const struct got_error *
2408 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2410 return get_ref_name(refname, worktree,
2411 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2414 static const struct got_error *
2415 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2417 return get_ref_name(refname, worktree,
2418 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2421 static const struct got_error *
2422 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2424 return get_ref_name(refname, worktree,
2425 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2428 static const struct got_error *
2429 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2431 return get_ref_name(refname, worktree,
2432 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2435 static const struct got_error *
2436 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2438 return get_ref_name(refname, worktree,
2439 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2442 static const struct got_error *
2443 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2445 return get_ref_name(refname, worktree,
2446 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2449 const struct got_error *
2450 got_worktree_get_histedit_script_path(char **path,
2451 struct got_worktree *worktree)
2453 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2454 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2455 *path = NULL;
2456 return got_error_from_errno("asprintf");
2458 return NULL;
2461 static const struct got_error *
2462 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2464 return get_ref_name(refname, worktree,
2465 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2468 static const struct got_error *
2469 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2471 return get_ref_name(refname, worktree,
2472 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2476 * Prevent Git's garbage collector from deleting our base commit by
2477 * setting a reference to our base commit's ID.
2479 static const struct got_error *
2480 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2482 const struct got_error *err = NULL;
2483 struct got_reference *ref = NULL;
2484 char *refname;
2486 err = get_base_ref_name(&refname, worktree);
2487 if (err)
2488 return err;
2490 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2491 if (err)
2492 goto done;
2494 err = got_ref_write(ref, repo);
2495 done:
2496 free(refname);
2497 if (ref)
2498 got_ref_close(ref);
2499 return err;
2502 static const struct got_error *
2503 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2505 const struct got_error *err = NULL;
2507 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2508 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2509 err = got_error_from_errno("asprintf");
2510 *fileindex_path = NULL;
2512 return err;
2516 static const struct got_error *
2517 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2518 struct got_worktree *worktree)
2520 const struct got_error *err = NULL;
2521 FILE *index = NULL;
2523 *fileindex_path = NULL;
2524 *fileindex = got_fileindex_alloc();
2525 if (*fileindex == NULL)
2526 return got_error_from_errno("got_fileindex_alloc");
2528 err = get_fileindex_path(fileindex_path, worktree);
2529 if (err)
2530 goto done;
2532 index = fopen(*fileindex_path, "rbe");
2533 if (index == NULL) {
2534 if (errno != ENOENT)
2535 err = got_error_from_errno2("fopen", *fileindex_path);
2536 } else {
2537 err = got_fileindex_read(*fileindex, index);
2538 if (fclose(index) == EOF && err == NULL)
2539 err = got_error_from_errno("fclose");
2541 done:
2542 if (err) {
2543 free(*fileindex_path);
2544 *fileindex_path = NULL;
2545 got_fileindex_free(*fileindex);
2546 *fileindex = NULL;
2548 return err;
2551 struct bump_base_commit_id_arg {
2552 struct got_object_id *base_commit_id;
2553 const char *path;
2554 size_t path_len;
2555 const char *entry_name;
2556 got_worktree_checkout_cb progress_cb;
2557 void *progress_arg;
2560 static const struct got_error *
2561 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2563 const struct got_error *err;
2564 struct bump_base_commit_id_arg *a = arg;
2566 if (a->entry_name) {
2567 if (strcmp(ie->path, a->path) != 0)
2568 return NULL;
2569 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2570 return NULL;
2572 if (got_fileindex_entry_was_skipped(ie))
2573 return NULL;
2575 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2576 SHA1_DIGEST_LENGTH) == 0)
2577 return NULL;
2579 if (a->progress_cb) {
2580 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2581 ie->path);
2582 if (err)
2583 return err;
2585 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2586 return NULL;
2589 /* Bump base commit ID of all files within an updated part of the work tree. */
2590 static const struct got_error *
2591 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2592 struct got_fileindex *fileindex,
2593 got_worktree_checkout_cb progress_cb, void *progress_arg)
2595 struct bump_base_commit_id_arg bbc_arg;
2597 bbc_arg.base_commit_id = worktree->base_commit_id;
2598 bbc_arg.entry_name = NULL;
2599 bbc_arg.path = "";
2600 bbc_arg.path_len = 0;
2601 bbc_arg.progress_cb = progress_cb;
2602 bbc_arg.progress_arg = progress_arg;
2604 return got_fileindex_for_each_entry_safe(fileindex,
2605 bump_base_commit_id, &bbc_arg);
2608 static const struct got_error *
2609 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2611 const struct got_error *err = NULL;
2612 char *new_fileindex_path = NULL;
2613 FILE *new_index = NULL;
2614 struct timespec timeout;
2616 err = got_opentemp_named(&new_fileindex_path, &new_index,
2617 fileindex_path, "");
2618 if (err)
2619 goto done;
2621 err = got_fileindex_write(fileindex, new_index);
2622 if (err)
2623 goto done;
2625 if (rename(new_fileindex_path, fileindex_path) != 0) {
2626 err = got_error_from_errno3("rename", new_fileindex_path,
2627 fileindex_path);
2628 unlink(new_fileindex_path);
2632 * Sleep for a short amount of time to ensure that files modified after
2633 * this program exits have a different time stamp from the one which
2634 * was recorded in the file index.
2636 timeout.tv_sec = 0;
2637 timeout.tv_nsec = 1;
2638 nanosleep(&timeout, NULL);
2639 done:
2640 if (new_index)
2641 fclose(new_index);
2642 free(new_fileindex_path);
2643 return err;
2646 static const struct got_error *
2647 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2648 struct got_object_id **tree_id, const char *wt_relpath,
2649 struct got_commit_object *base_commit, struct got_worktree *worktree,
2650 struct got_repository *repo)
2652 const struct got_error *err = NULL;
2653 struct got_object_id *id = NULL;
2654 char *in_repo_path = NULL;
2655 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2657 *entry_type = GOT_OBJ_TYPE_ANY;
2658 *tree_relpath = NULL;
2659 *tree_id = NULL;
2661 if (wt_relpath[0] == '\0') {
2662 /* Check out all files within the work tree. */
2663 *entry_type = GOT_OBJ_TYPE_TREE;
2664 *tree_relpath = strdup("");
2665 if (*tree_relpath == NULL) {
2666 err = got_error_from_errno("strdup");
2667 goto done;
2669 err = got_object_id_by_path(tree_id, repo, base_commit,
2670 worktree->path_prefix);
2671 if (err)
2672 goto done;
2673 return NULL;
2676 /* Check out a subset of files in the work tree. */
2678 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2679 is_root_wt ? "" : "/", wt_relpath) == -1) {
2680 err = got_error_from_errno("asprintf");
2681 goto done;
2684 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2685 if (err)
2686 goto done;
2688 free(in_repo_path);
2689 in_repo_path = NULL;
2691 err = got_object_get_type(entry_type, repo, id);
2692 if (err)
2693 goto done;
2695 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2696 /* Check out a single file. */
2697 if (strchr(wt_relpath, '/') == NULL) {
2698 /* Check out a single file in work tree's root dir. */
2699 in_repo_path = strdup(worktree->path_prefix);
2700 if (in_repo_path == NULL) {
2701 err = got_error_from_errno("strdup");
2702 goto done;
2704 *tree_relpath = strdup("");
2705 if (*tree_relpath == NULL) {
2706 err = got_error_from_errno("strdup");
2707 goto done;
2709 } else {
2710 /* Check out a single file in a subdirectory. */
2711 err = got_path_dirname(tree_relpath, wt_relpath);
2712 if (err)
2713 return err;
2714 if (asprintf(&in_repo_path, "%s%s%s",
2715 worktree->path_prefix, is_root_wt ? "" : "/",
2716 *tree_relpath) == -1) {
2717 err = got_error_from_errno("asprintf");
2718 goto done;
2721 err = got_object_id_by_path(tree_id, repo,
2722 base_commit, in_repo_path);
2723 } else {
2724 /* Check out all files within a subdirectory. */
2725 *tree_id = got_object_id_dup(id);
2726 if (*tree_id == NULL) {
2727 err = got_error_from_errno("got_object_id_dup");
2728 goto done;
2730 *tree_relpath = strdup(wt_relpath);
2731 if (*tree_relpath == NULL) {
2732 err = got_error_from_errno("strdup");
2733 goto done;
2736 done:
2737 free(id);
2738 free(in_repo_path);
2739 if (err) {
2740 *entry_type = GOT_OBJ_TYPE_ANY;
2741 free(*tree_relpath);
2742 *tree_relpath = NULL;
2743 free(*tree_id);
2744 *tree_id = NULL;
2746 return err;
2749 static const struct got_error *
2750 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2751 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2752 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2753 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2755 const struct got_error *err = NULL;
2756 struct got_commit_object *commit = NULL;
2757 struct got_tree_object *tree = NULL;
2758 struct got_fileindex_diff_tree_cb diff_cb;
2759 struct diff_cb_arg arg;
2761 err = ref_base_commit(worktree, repo);
2762 if (err) {
2763 if (!(err->code == GOT_ERR_ERRNO &&
2764 (errno == EACCES || errno == EROFS)))
2765 goto done;
2766 err = (*progress_cb)(progress_arg,
2767 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2768 if (err)
2769 return err;
2772 err = got_object_open_as_commit(&commit, repo,
2773 worktree->base_commit_id);
2774 if (err)
2775 goto done;
2777 err = got_object_open_as_tree(&tree, repo, tree_id);
2778 if (err)
2779 goto done;
2781 if (entry_name &&
2782 got_object_tree_find_entry(tree, entry_name) == NULL) {
2783 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2784 goto done;
2787 diff_cb.diff_old_new = diff_old_new;
2788 diff_cb.diff_old = diff_old;
2789 diff_cb.diff_new = diff_new;
2790 arg.fileindex = fileindex;
2791 arg.worktree = worktree;
2792 arg.repo = repo;
2793 arg.progress_cb = progress_cb;
2794 arg.progress_arg = progress_arg;
2795 arg.cancel_cb = cancel_cb;
2796 arg.cancel_arg = cancel_arg;
2797 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2798 entry_name, repo, &diff_cb, &arg);
2799 done:
2800 if (tree)
2801 got_object_tree_close(tree);
2802 if (commit)
2803 got_object_commit_close(commit);
2804 return err;
2807 const struct got_error *
2808 got_worktree_checkout_files(struct got_worktree *worktree,
2809 struct got_pathlist_head *paths, struct got_repository *repo,
2810 got_worktree_checkout_cb progress_cb, void *progress_arg,
2811 got_cancel_cb cancel_cb, void *cancel_arg)
2813 const struct got_error *err = NULL, *sync_err, *unlockerr;
2814 struct got_commit_object *commit = NULL;
2815 struct got_tree_object *tree = NULL;
2816 struct got_fileindex *fileindex = NULL;
2817 char *fileindex_path = NULL;
2818 struct got_pathlist_entry *pe;
2819 struct tree_path_data {
2820 STAILQ_ENTRY(tree_path_data) entry;
2821 struct got_object_id *tree_id;
2822 int entry_type;
2823 char *relpath;
2824 char *entry_name;
2825 } *tpd = NULL;
2826 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2828 STAILQ_INIT(&tree_paths);
2830 err = lock_worktree(worktree, LOCK_EX);
2831 if (err)
2832 return err;
2834 err = got_object_open_as_commit(&commit, repo,
2835 worktree->base_commit_id);
2836 if (err)
2837 goto done;
2839 /* Map all specified paths to in-repository trees. */
2840 TAILQ_FOREACH(pe, paths, entry) {
2841 tpd = malloc(sizeof(*tpd));
2842 if (tpd == NULL) {
2843 err = got_error_from_errno("malloc");
2844 goto done;
2847 err = find_tree_entry_for_checkout(&tpd->entry_type,
2848 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2849 worktree, repo);
2850 if (err) {
2851 free(tpd);
2852 goto done;
2855 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2856 err = got_path_basename(&tpd->entry_name, pe->path);
2857 if (err) {
2858 free(tpd->relpath);
2859 free(tpd->tree_id);
2860 free(tpd);
2861 goto done;
2863 } else
2864 tpd->entry_name = NULL;
2866 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2870 * Read the file index.
2871 * Checking out files is supposed to be an idempotent operation.
2872 * If the on-disk file index is incomplete we will try to complete it.
2874 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2875 if (err)
2876 goto done;
2878 tpd = STAILQ_FIRST(&tree_paths);
2879 TAILQ_FOREACH(pe, paths, entry) {
2880 struct bump_base_commit_id_arg bbc_arg;
2882 err = checkout_files(worktree, fileindex, tpd->relpath,
2883 tpd->tree_id, tpd->entry_name, repo,
2884 progress_cb, progress_arg, cancel_cb, cancel_arg);
2885 if (err)
2886 break;
2888 bbc_arg.base_commit_id = worktree->base_commit_id;
2889 bbc_arg.entry_name = tpd->entry_name;
2890 bbc_arg.path = pe->path;
2891 bbc_arg.path_len = pe->path_len;
2892 bbc_arg.progress_cb = progress_cb;
2893 bbc_arg.progress_arg = progress_arg;
2894 err = got_fileindex_for_each_entry_safe(fileindex,
2895 bump_base_commit_id, &bbc_arg);
2896 if (err)
2897 break;
2899 tpd = STAILQ_NEXT(tpd, entry);
2901 sync_err = sync_fileindex(fileindex, fileindex_path);
2902 if (sync_err && err == NULL)
2903 err = sync_err;
2904 done:
2905 free(fileindex_path);
2906 if (tree)
2907 got_object_tree_close(tree);
2908 if (commit)
2909 got_object_commit_close(commit);
2910 if (fileindex)
2911 got_fileindex_free(fileindex);
2912 while (!STAILQ_EMPTY(&tree_paths)) {
2913 tpd = STAILQ_FIRST(&tree_paths);
2914 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2915 free(tpd->relpath);
2916 free(tpd->tree_id);
2917 free(tpd);
2919 unlockerr = lock_worktree(worktree, LOCK_SH);
2920 if (unlockerr && err == NULL)
2921 err = unlockerr;
2922 return err;
2925 static const struct got_error *
2926 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2927 struct got_fileindex_entry *ie, const char *ondisk_path,
2928 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2929 int restoring_missing_file, int reverting_versioned_file,
2930 int path_is_unversioned, int allow_bad_symlinks,
2931 struct got_repository *repo,
2932 got_worktree_checkout_cb progress_cb, void *progress_arg)
2934 const struct got_error *err = NULL;
2935 int is_bad_symlink = 0;
2937 if (S_ISLNK(mode2)) {
2938 err = install_symlink(&is_bad_symlink,
2939 worktree, ondisk_path, path2, blob2,
2940 restoring_missing_file,
2941 reverting_versioned_file,
2942 path_is_unversioned, allow_bad_symlinks,
2943 repo, progress_cb, progress_arg);
2944 } else {
2945 err = install_blob(worktree, ondisk_path, path2,
2946 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2947 restoring_missing_file, reverting_versioned_file, 0,
2948 path_is_unversioned, NULL, repo,
2949 progress_cb, progress_arg);
2951 if (err)
2952 return err;
2953 if (ie == NULL) {
2954 /* Adding an unversioned file. */
2955 err = got_fileindex_entry_alloc(&ie, path2);
2956 if (err)
2957 return err;
2958 err = got_fileindex_entry_update(ie,
2959 worktree->root_fd, path2, NULL, NULL, 1);
2960 if (err) {
2961 got_fileindex_entry_free(ie);
2962 return err;
2964 err = got_fileindex_entry_add(fileindex, ie);
2965 if (err) {
2966 got_fileindex_entry_free(ie);
2967 return err;
2969 } else {
2970 /* Re-adding a locally deleted file. */
2971 err = got_fileindex_entry_update(ie,
2972 worktree->root_fd, path2, ie->blob_sha1,
2973 worktree->base_commit_id->sha1, 0);
2974 if (err)
2975 return err;
2978 if (is_bad_symlink) {
2979 got_fileindex_entry_filetype_set(ie,
2980 GOT_FILEIDX_MODE_BAD_SYMLINK);
2983 return NULL;
2986 struct merge_file_cb_arg {
2987 struct got_worktree *worktree;
2988 struct got_fileindex *fileindex;
2989 got_worktree_checkout_cb progress_cb;
2990 void *progress_arg;
2991 got_cancel_cb cancel_cb;
2992 void *cancel_arg;
2993 const char *label_orig;
2994 struct got_object_id *commit_id2;
2995 int allow_bad_symlinks;
2998 static const struct got_error *
2999 merge_file_cb(void *arg, struct got_blob_object *blob1,
3000 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3001 struct got_object_id *id1, struct got_object_id *id2,
3002 const char *path1, const char *path2,
3003 mode_t mode1, mode_t mode2, struct got_repository *repo)
3005 static const struct got_error *err = NULL;
3006 struct merge_file_cb_arg *a = arg;
3007 struct got_fileindex_entry *ie;
3008 char *ondisk_path = NULL;
3009 struct stat sb;
3010 unsigned char status;
3011 int local_changes_subsumed;
3012 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3013 char *id_str = NULL, *label_deriv2 = NULL;
3014 struct got_object_id *id = NULL;
3016 if (blob1 && blob2) {
3017 ie = got_fileindex_entry_get(a->fileindex, path2,
3018 strlen(path2));
3019 if (ie == NULL)
3020 return (*a->progress_cb)(a->progress_arg,
3021 GOT_STATUS_MISSING, path2);
3023 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3024 path2) == -1)
3025 return got_error_from_errno("asprintf");
3027 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3028 repo);
3029 if (err)
3030 goto done;
3032 if (status == GOT_STATUS_DELETE) {
3033 err = (*a->progress_cb)(a->progress_arg,
3034 GOT_STATUS_MERGE, path2);
3035 goto done;
3037 if (status != GOT_STATUS_NO_CHANGE &&
3038 status != GOT_STATUS_MODIFY &&
3039 status != GOT_STATUS_CONFLICT &&
3040 status != GOT_STATUS_ADD) {
3041 err = (*a->progress_cb)(a->progress_arg, status, path2);
3042 goto done;
3045 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3046 char *link_target2;
3047 err = got_object_blob_read_to_str(&link_target2, blob2);
3048 if (err)
3049 goto done;
3050 err = merge_symlink(a->worktree, blob1, ondisk_path,
3051 path2, a->label_orig, link_target2, a->commit_id2,
3052 repo, a->progress_cb, a->progress_arg);
3053 free(link_target2);
3054 } else {
3055 int fd;
3057 f_orig = got_opentemp();
3058 if (f_orig == NULL) {
3059 err = got_error_from_errno("got_opentemp");
3060 goto done;
3062 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3063 f_orig, blob1);
3064 if (err)
3065 goto done;
3067 f_deriv2 = got_opentemp();
3068 if (f_deriv2 == NULL)
3069 goto done;
3070 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3071 f_deriv2, blob2);
3072 if (err)
3073 goto done;
3075 fd = open(ondisk_path,
3076 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3077 if (fd == -1) {
3078 err = got_error_from_errno2("open",
3079 ondisk_path);
3080 goto done;
3082 f_deriv = fdopen(fd, "r");
3083 if (f_deriv == NULL) {
3084 err = got_error_from_errno2("fdopen",
3085 ondisk_path);
3086 close(fd);
3087 goto done;
3089 err = got_object_id_str(&id_str, a->commit_id2);
3090 if (err)
3091 goto done;
3092 if (asprintf(&label_deriv2, "%s: commit %s",
3093 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3094 err = got_error_from_errno("asprintf");
3095 goto done;
3097 err = merge_file(&local_changes_subsumed, a->worktree,
3098 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3099 mode2, a->label_orig, NULL, label_deriv2,
3100 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3101 a->progress_cb, a->progress_arg);
3103 } else if (blob1) {
3104 ie = got_fileindex_entry_get(a->fileindex, path1,
3105 strlen(path1));
3106 if (ie == NULL)
3107 return (*a->progress_cb)(a->progress_arg,
3108 GOT_STATUS_MISSING, path1);
3110 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3111 path1) == -1)
3112 return got_error_from_errno("asprintf");
3114 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3115 repo);
3116 if (err)
3117 goto done;
3119 switch (status) {
3120 case GOT_STATUS_NO_CHANGE:
3121 err = (*a->progress_cb)(a->progress_arg,
3122 GOT_STATUS_DELETE, path1);
3123 if (err)
3124 goto done;
3125 err = remove_ondisk_file(a->worktree->root_path, path1);
3126 if (err)
3127 goto done;
3128 if (ie)
3129 got_fileindex_entry_mark_deleted_from_disk(ie);
3130 break;
3131 case GOT_STATUS_DELETE:
3132 case GOT_STATUS_MISSING:
3133 err = (*a->progress_cb)(a->progress_arg,
3134 GOT_STATUS_DELETE, path1);
3135 if (err)
3136 goto done;
3137 if (ie)
3138 got_fileindex_entry_mark_deleted_from_disk(ie);
3139 break;
3140 case GOT_STATUS_MODIFY: {
3141 FILE *blob1_f;
3142 off_t blob1_size;
3143 int obj_type;
3145 * Delete the file only if its content already
3146 * exists in the repository.
3148 err = got_object_blob_file_create(&id, &blob1_f,
3149 &blob1_size, path1);
3150 if (err)
3151 goto done;
3152 if (fclose(blob1_f) == EOF) {
3153 err = got_error_from_errno("fclose");
3154 goto done;
3157 /* Implied existence check. */
3158 err = got_object_get_type(&obj_type, repo, id);
3159 if (err) {
3160 if (err->code != GOT_ERR_NO_OBJ)
3161 goto done;
3162 err = (*a->progress_cb)(a->progress_arg,
3163 GOT_STATUS_CANNOT_DELETE, path1);
3164 goto done;
3165 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3166 err = (*a->progress_cb)(a->progress_arg,
3167 GOT_STATUS_CANNOT_DELETE, path1);
3168 goto done;
3170 err = (*a->progress_cb)(a->progress_arg,
3171 GOT_STATUS_DELETE, path1);
3172 if (err)
3173 goto done;
3174 err = remove_ondisk_file(a->worktree->root_path,
3175 path1);
3176 if (err)
3177 goto done;
3178 if (ie)
3179 got_fileindex_entry_mark_deleted_from_disk(ie);
3180 break;
3182 case GOT_STATUS_ADD: {
3183 FILE *blob1_f;
3184 off_t blob1_size;
3186 * Delete the file only if its content already
3187 * exists in the repository.
3189 err = got_object_blob_file_create(&id, &blob1_f,
3190 &blob1_size, path1);
3191 if (err)
3192 goto done;
3193 if (fclose(blob1_f) == EOF) {
3194 err = got_error_from_errno("fclose");
3195 goto done;
3197 if (got_object_id_cmp(id, id1) == 0) {
3198 err = (*a->progress_cb)(a->progress_arg,
3199 GOT_STATUS_DELETE, path1);
3200 if (err)
3201 goto done;
3202 err = remove_ondisk_file(a->worktree->root_path,
3203 path1);
3204 if (err)
3205 goto done;
3206 if (ie)
3207 got_fileindex_entry_remove(a->fileindex,
3208 ie);
3209 } else {
3210 err = (*a->progress_cb)(a->progress_arg,
3211 GOT_STATUS_CANNOT_DELETE, path1);
3213 if (err)
3214 goto done;
3215 break;
3217 case GOT_STATUS_CONFLICT:
3218 err = (*a->progress_cb)(a->progress_arg,
3219 GOT_STATUS_CANNOT_DELETE, path1);
3220 if (err)
3221 goto done;
3222 break;
3223 case GOT_STATUS_OBSTRUCTED:
3224 err = (*a->progress_cb)(a->progress_arg, status, path1);
3225 if (err)
3226 goto done;
3227 break;
3228 default:
3229 break;
3231 } else if (blob2) {
3232 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3233 path2) == -1)
3234 return got_error_from_errno("asprintf");
3235 ie = got_fileindex_entry_get(a->fileindex, path2,
3236 strlen(path2));
3237 if (ie) {
3238 err = get_file_status(&status, &sb, ie, ondisk_path,
3239 -1, NULL, repo);
3240 if (err)
3241 goto done;
3242 if (status != GOT_STATUS_NO_CHANGE &&
3243 status != GOT_STATUS_MODIFY &&
3244 status != GOT_STATUS_CONFLICT &&
3245 status != GOT_STATUS_ADD &&
3246 status != GOT_STATUS_DELETE) {
3247 err = (*a->progress_cb)(a->progress_arg,
3248 status, path2);
3249 goto done;
3251 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3252 char *link_target2;
3253 err = got_object_blob_read_to_str(&link_target2,
3254 blob2);
3255 if (err)
3256 goto done;
3257 err = merge_symlink(a->worktree, NULL,
3258 ondisk_path, path2, a->label_orig,
3259 link_target2, a->commit_id2, repo,
3260 a->progress_cb, a->progress_arg);
3261 free(link_target2);
3262 } else if (S_ISREG(sb.st_mode)) {
3263 err = merge_blob(&local_changes_subsumed,
3264 a->worktree, NULL, ondisk_path, path2,
3265 sb.st_mode, a->label_orig, blob2,
3266 a->commit_id2, repo, a->progress_cb,
3267 a->progress_arg);
3268 } else if (status != GOT_STATUS_DELETE) {
3269 err = got_error_path(ondisk_path,
3270 GOT_ERR_FILE_OBSTRUCTED);
3272 if (err)
3273 goto done;
3274 if (status == GOT_STATUS_DELETE) {
3275 /* Re-add file with content from new blob. */
3276 err = add_file(a->worktree, a->fileindex, ie,
3277 ondisk_path, path2, blob2, mode2,
3278 0, 0, 0, a->allow_bad_symlinks,
3279 repo, a->progress_cb, a->progress_arg);
3280 if (err)
3281 goto done;
3283 } else {
3284 err = add_file(a->worktree, a->fileindex, NULL,
3285 ondisk_path, path2, blob2, mode2,
3286 0, 0, 1, a->allow_bad_symlinks,
3287 repo, a->progress_cb, a->progress_arg);
3288 if (err)
3289 goto done;
3292 done:
3293 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3294 err = got_error_from_errno("fclose");
3295 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3296 err = got_error_from_errno("fclose");
3297 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3298 err = got_error_from_errno("fclose");
3299 free(id_str);
3300 free(id);
3301 free(label_deriv2);
3302 free(ondisk_path);
3303 return err;
3306 struct check_mixed_commits_args {
3307 struct got_worktree *worktree;
3308 got_cancel_cb cancel_cb;
3309 void *cancel_arg;
3312 static const struct got_error *
3313 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3315 const struct got_error *err = NULL;
3316 struct check_mixed_commits_args *a = arg;
3318 if (a->cancel_cb) {
3319 err = a->cancel_cb(a->cancel_arg);
3320 if (err)
3321 return err;
3324 /* Reject merges into a work tree with mixed base commits. */
3325 if (got_fileindex_entry_has_commit(ie) &&
3326 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3327 SHA1_DIGEST_LENGTH) != 0)
3328 return got_error(GOT_ERR_MIXED_COMMITS);
3330 return NULL;
3333 const struct got_error *
3334 got_worktree_get_state(char *state, struct got_repository *repo,
3335 struct got_worktree *worktree,
3336 got_cancel_cb cancel_cb, void *cancel_arg)
3338 const struct got_error *err;
3339 struct got_object_id *base_id, *head_id = NULL;
3340 struct got_reference *head_ref;
3341 struct got_fileindex *fileindex = NULL;
3342 char *fileindex_path = NULL;
3343 struct check_mixed_commits_args cma;
3345 if (worktree == NULL)
3346 return got_error(GOT_ERR_NOT_WORKTREE);
3348 err = got_ref_open(&head_ref, repo,
3349 got_worktree_get_head_ref_name(worktree), 0);
3350 if (err)
3351 return err;
3353 err = got_ref_resolve(&head_id, repo, head_ref);
3354 if (err)
3355 goto done;
3357 *state = GOT_WORKTREE_STATE_UNKNOWN;
3358 base_id = got_worktree_get_base_commit_id(worktree);
3360 cma.worktree = worktree;
3361 cma.cancel_cb = cancel_cb;
3362 cma.cancel_arg = cancel_arg;
3364 if (got_object_id_cmp(base_id, head_id) == 0) {
3365 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3366 if (err)
3367 goto done;
3369 err = got_fileindex_for_each_entry_safe(fileindex,
3370 check_mixed_commits, &cma);
3371 if (err == NULL)
3372 *state = GOT_WORKTREE_STATE_UPTODATE;
3373 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3374 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3375 err = NULL;
3377 } else
3378 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3380 done:
3381 free(head_id);
3382 free(fileindex_path);
3383 got_ref_close(head_ref);
3384 if (fileindex != NULL)
3385 got_fileindex_free(fileindex);
3386 return err;
3389 struct check_merge_conflicts_arg {
3390 struct got_worktree *worktree;
3391 struct got_fileindex *fileindex;
3392 struct got_repository *repo;
3395 static const struct got_error *
3396 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3397 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3398 struct got_object_id *id1, struct got_object_id *id2,
3399 const char *path1, const char *path2,
3400 mode_t mode1, mode_t mode2, struct got_repository *repo)
3402 const struct got_error *err = NULL;
3403 struct check_merge_conflicts_arg *a = arg;
3404 unsigned char status;
3405 struct stat sb;
3406 struct got_fileindex_entry *ie;
3407 const char *path = path2 ? path2 : path1;
3408 struct got_object_id *id = id2 ? id2 : id1;
3409 char *ondisk_path;
3411 if (id == NULL)
3412 return NULL;
3414 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3415 if (ie == NULL)
3416 return NULL;
3418 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3419 == -1)
3420 return got_error_from_errno("asprintf");
3422 /* Reject merges into a work tree with conflicted files. */
3423 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3424 free(ondisk_path);
3425 if (err)
3426 return err;
3427 if (status == GOT_STATUS_CONFLICT)
3428 return got_error(GOT_ERR_CONFLICTS);
3430 return NULL;
3433 static const struct got_error *
3434 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3435 const char *fileindex_path, struct got_object_id *commit_id1,
3436 struct got_object_id *commit_id2, struct got_repository *repo,
3437 got_worktree_checkout_cb progress_cb, void *progress_arg,
3438 got_cancel_cb cancel_cb, void *cancel_arg)
3440 const struct got_error *err = NULL, *sync_err;
3441 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3442 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3443 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3444 struct check_merge_conflicts_arg cmc_arg;
3445 struct merge_file_cb_arg arg;
3446 char *label_orig = NULL;
3447 FILE *f1 = NULL, *f2 = NULL;
3448 int fd1 = -1, fd2 = -1;
3450 if (commit_id1) {
3451 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3452 if (err)
3453 goto done;
3454 err = got_object_id_by_path(&tree_id1, repo, commit1,
3455 worktree->path_prefix);
3456 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3457 goto done;
3459 if (tree_id1) {
3460 char *id_str;
3462 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3463 if (err)
3464 goto done;
3466 err = got_object_id_str(&id_str, commit_id1);
3467 if (err)
3468 goto done;
3470 if (asprintf(&label_orig, "%s: commit %s",
3471 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3472 err = got_error_from_errno("asprintf");
3473 free(id_str);
3474 goto done;
3476 free(id_str);
3478 f1 = got_opentemp();
3479 if (f1 == NULL) {
3480 err = got_error_from_errno("got_opentemp");
3481 goto done;
3485 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3486 if (err)
3487 goto done;
3489 err = got_object_id_by_path(&tree_id2, repo, commit2,
3490 worktree->path_prefix);
3491 if (err)
3492 goto done;
3494 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3495 if (err)
3496 goto done;
3498 f2 = got_opentemp();
3499 if (f2 == NULL) {
3500 err = got_error_from_errno("got_opentemp");
3501 goto done;
3504 fd1 = got_opentempfd();
3505 if (fd1 == -1) {
3506 err = got_error_from_errno("got_opentempfd");
3507 goto done;
3510 fd2 = got_opentempfd();
3511 if (fd2 == -1) {
3512 err = got_error_from_errno("got_opentempfd");
3513 goto done;
3516 cmc_arg.worktree = worktree;
3517 cmc_arg.fileindex = fileindex;
3518 cmc_arg.repo = repo;
3519 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3520 check_merge_conflicts, &cmc_arg, 0);
3521 if (err)
3522 goto done;
3524 arg.worktree = worktree;
3525 arg.fileindex = fileindex;
3526 arg.progress_cb = progress_cb;
3527 arg.progress_arg = progress_arg;
3528 arg.cancel_cb = cancel_cb;
3529 arg.cancel_arg = cancel_arg;
3530 arg.label_orig = label_orig;
3531 arg.commit_id2 = commit_id2;
3532 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3533 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3534 merge_file_cb, &arg, 1);
3535 sync_err = sync_fileindex(fileindex, fileindex_path);
3536 if (sync_err && err == NULL)
3537 err = sync_err;
3538 done:
3539 if (commit1)
3540 got_object_commit_close(commit1);
3541 if (commit2)
3542 got_object_commit_close(commit2);
3543 if (tree1)
3544 got_object_tree_close(tree1);
3545 if (tree2)
3546 got_object_tree_close(tree2);
3547 if (f1 && fclose(f1) == EOF && err == NULL)
3548 err = got_error_from_errno("fclose");
3549 if (f2 && fclose(f2) == EOF && err == NULL)
3550 err = got_error_from_errno("fclose");
3551 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3552 err = got_error_from_errno("close");
3553 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3554 err = got_error_from_errno("close");
3555 free(label_orig);
3556 return err;
3559 const struct got_error *
3560 got_worktree_merge_files(struct got_worktree *worktree,
3561 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3562 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3563 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3565 const struct got_error *err, *unlockerr;
3566 char *fileindex_path = NULL;
3567 struct got_fileindex *fileindex = NULL;
3568 struct check_mixed_commits_args cma;
3570 err = lock_worktree(worktree, LOCK_EX);
3571 if (err)
3572 return err;
3574 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3575 if (err)
3576 goto done;
3578 cma.worktree = worktree;
3579 cma.cancel_cb = cancel_cb;
3580 cma.cancel_arg = cancel_arg;
3582 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3583 &cma);
3584 if (err)
3585 goto done;
3587 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3588 commit_id2, repo, progress_cb, progress_arg,
3589 cancel_cb, cancel_arg);
3590 done:
3591 if (fileindex)
3592 got_fileindex_free(fileindex);
3593 free(fileindex_path);
3594 unlockerr = lock_worktree(worktree, LOCK_SH);
3595 if (unlockerr && err == NULL)
3596 err = unlockerr;
3597 return err;
3600 struct diff_dir_cb_arg {
3601 struct got_fileindex *fileindex;
3602 struct got_worktree *worktree;
3603 const char *status_path;
3604 size_t status_path_len;
3605 struct got_repository *repo;
3606 got_worktree_status_cb status_cb;
3607 void *status_arg;
3608 got_cancel_cb cancel_cb;
3609 void *cancel_arg;
3610 /* A pathlist containing per-directory pathlists of ignore patterns. */
3611 struct got_pathlist_head *ignores;
3612 int report_unchanged;
3613 int no_ignores;
3616 static const struct got_error *
3617 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3618 int dirfd, const char *de_name,
3619 got_worktree_status_cb status_cb, void *status_arg,
3620 struct got_repository *repo, int report_unchanged)
3622 const struct got_error *err = NULL;
3623 unsigned char status = GOT_STATUS_NO_CHANGE;
3624 unsigned char staged_status;
3625 struct stat sb;
3626 struct got_object_id blob_id, commit_id, staged_blob_id;
3627 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3628 struct got_object_id *staged_blob_idp = NULL;
3630 staged_status = get_staged_status(ie);
3631 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3632 if (err)
3633 return err;
3635 if (status == GOT_STATUS_NO_CHANGE &&
3636 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3637 return NULL;
3639 if (got_fileindex_entry_has_blob(ie))
3640 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3641 if (got_fileindex_entry_has_commit(ie))
3642 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3643 if (staged_status == GOT_STATUS_ADD ||
3644 staged_status == GOT_STATUS_MODIFY) {
3645 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3646 &staged_blob_id, ie);
3649 return (*status_cb)(status_arg, status, staged_status,
3650 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3653 static const struct got_error *
3654 status_old_new(void *arg, struct got_fileindex_entry *ie,
3655 struct dirent *de, const char *parent_path, int dirfd)
3657 const struct got_error *err = NULL;
3658 struct diff_dir_cb_arg *a = arg;
3659 char *abspath;
3661 if (a->cancel_cb) {
3662 err = a->cancel_cb(a->cancel_arg);
3663 if (err)
3664 return err;
3667 if (got_path_cmp(parent_path, a->status_path,
3668 strlen(parent_path), a->status_path_len) != 0 &&
3669 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3670 return NULL;
3672 if (parent_path[0]) {
3673 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3674 parent_path, de->d_name) == -1)
3675 return got_error_from_errno("asprintf");
3676 } else {
3677 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3678 de->d_name) == -1)
3679 return got_error_from_errno("asprintf");
3682 err = report_file_status(ie, abspath, dirfd, de->d_name,
3683 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3684 free(abspath);
3685 return err;
3688 static const struct got_error *
3689 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3691 const struct got_error *err;
3692 struct diff_dir_cb_arg *a = arg;
3693 struct got_object_id blob_id, commit_id;
3694 unsigned char status;
3696 if (a->cancel_cb) {
3697 err = a->cancel_cb(a->cancel_arg);
3698 if (err)
3699 return err;
3702 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3703 return NULL;
3705 got_fileindex_entry_get_blob_id(&blob_id, ie);
3706 got_fileindex_entry_get_commit_id(&commit_id, ie);
3707 if (got_fileindex_entry_has_file_on_disk(ie))
3708 status = GOT_STATUS_MISSING;
3709 else
3710 status = GOT_STATUS_DELETE;
3711 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3712 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3715 static void
3716 free_ignores(struct got_pathlist_head *ignores)
3718 struct got_pathlist_entry *pe;
3720 TAILQ_FOREACH(pe, ignores, entry) {
3721 struct got_pathlist_head *ignorelist = pe->data;
3723 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3725 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3728 static const struct got_error *
3729 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3731 const struct got_error *err = NULL;
3732 struct got_pathlist_entry *pe = NULL;
3733 struct got_pathlist_head *ignorelist;
3734 char *line = NULL, *pattern, *dirpath = NULL;
3735 size_t linesize = 0;
3736 ssize_t linelen;
3738 ignorelist = calloc(1, sizeof(*ignorelist));
3739 if (ignorelist == NULL)
3740 return got_error_from_errno("calloc");
3741 TAILQ_INIT(ignorelist);
3743 while ((linelen = getline(&line, &linesize, f)) != -1) {
3744 if (linelen > 0 && line[linelen - 1] == '\n')
3745 line[linelen - 1] = '\0';
3747 /* Git's ignores may contain comments. */
3748 if (line[0] == '#')
3749 continue;
3751 /* Git's negated patterns are not (yet?) supported. */
3752 if (line[0] == '!')
3753 continue;
3755 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3756 line) == -1) {
3757 err = got_error_from_errno("asprintf");
3758 goto done;
3760 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3761 if (err)
3762 goto done;
3764 if (ferror(f)) {
3765 err = got_error_from_errno("getline");
3766 goto done;
3769 dirpath = strdup(path);
3770 if (dirpath == NULL) {
3771 err = got_error_from_errno("strdup");
3772 goto done;
3774 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3775 done:
3776 free(line);
3777 if (err || pe == NULL) {
3778 free(dirpath);
3779 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
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 if (fd != -1 && close(fd) == -1 && err == NULL)
4215 err = got_error_from_errno("close");
4216 free(ondisk_path);
4217 return err;
4220 const struct got_error *
4221 got_worktree_status(struct got_worktree *worktree,
4222 struct got_pathlist_head *paths, struct got_repository *repo,
4223 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4224 got_cancel_cb cancel_cb, void *cancel_arg)
4226 const struct got_error *err = NULL;
4227 char *fileindex_path = NULL;
4228 struct got_fileindex *fileindex = NULL;
4229 struct got_pathlist_entry *pe;
4231 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4232 if (err)
4233 return err;
4235 TAILQ_FOREACH(pe, paths, entry) {
4236 err = worktree_status(worktree, pe->path, fileindex, repo,
4237 status_cb, status_arg, cancel_cb, cancel_arg,
4238 no_ignores, 0);
4239 if (err)
4240 break;
4242 free(fileindex_path);
4243 got_fileindex_free(fileindex);
4244 return err;
4247 const struct got_error *
4248 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4249 const char *arg)
4251 const struct got_error *err = NULL;
4252 char *resolved = NULL, *cwd = NULL, *path = NULL;
4253 size_t len;
4254 struct stat sb;
4255 char *abspath = NULL;
4256 char canonpath[PATH_MAX];
4258 *wt_path = NULL;
4260 cwd = getcwd(NULL, 0);
4261 if (cwd == NULL)
4262 return got_error_from_errno("getcwd");
4264 if (lstat(arg, &sb) == -1) {
4265 if (errno != ENOENT) {
4266 err = got_error_from_errno2("lstat", arg);
4267 goto done;
4269 sb.st_mode = 0;
4271 if (S_ISLNK(sb.st_mode)) {
4273 * We cannot use realpath(3) with symlinks since we want to
4274 * operate on the symlink itself.
4275 * But we can make the path absolute, assuming it is relative
4276 * to the current working directory, and then canonicalize it.
4278 if (!got_path_is_absolute(arg)) {
4279 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4280 err = got_error_from_errno("asprintf");
4281 goto done;
4285 err = got_canonpath(abspath ? abspath : arg, canonpath,
4286 sizeof(canonpath));
4287 if (err)
4288 goto done;
4289 resolved = strdup(canonpath);
4290 if (resolved == NULL) {
4291 err = got_error_from_errno("strdup");
4292 goto done;
4294 } else {
4295 resolved = realpath(arg, NULL);
4296 if (resolved == NULL) {
4297 if (errno != ENOENT) {
4298 err = got_error_from_errno2("realpath", arg);
4299 goto done;
4301 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4302 err = got_error_from_errno("asprintf");
4303 goto done;
4305 err = got_canonpath(abspath, canonpath,
4306 sizeof(canonpath));
4307 if (err)
4308 goto done;
4309 resolved = strdup(canonpath);
4310 if (resolved == NULL) {
4311 err = got_error_from_errno("strdup");
4312 goto done;
4317 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4318 strlen(got_worktree_get_root_path(worktree)))) {
4319 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4320 goto done;
4323 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4324 err = got_path_skip_common_ancestor(&path,
4325 got_worktree_get_root_path(worktree), resolved);
4326 if (err)
4327 goto done;
4328 } else {
4329 path = strdup("");
4330 if (path == NULL) {
4331 err = got_error_from_errno("strdup");
4332 goto done;
4336 /* XXX status walk can't deal with trailing slash! */
4337 len = strlen(path);
4338 while (len > 0 && path[len - 1] == '/') {
4339 path[len - 1] = '\0';
4340 len--;
4342 done:
4343 free(abspath);
4344 free(resolved);
4345 free(cwd);
4346 if (err == NULL)
4347 *wt_path = path;
4348 else
4349 free(path);
4350 return err;
4353 struct schedule_addition_args {
4354 struct got_worktree *worktree;
4355 struct got_fileindex *fileindex;
4356 got_worktree_checkout_cb progress_cb;
4357 void *progress_arg;
4358 struct got_repository *repo;
4361 static int
4362 add_noop_status(unsigned char status)
4364 return (status == GOT_STATUS_ADD ||
4365 status == GOT_STATUS_MODIFY ||
4366 status == GOT_STATUS_CONFLICT ||
4367 status == GOT_STATUS_MODE_CHANGE ||
4368 status == GOT_STATUS_NO_CHANGE);
4371 static const struct got_error *
4372 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4373 const char *relpath, struct got_object_id *blob_id,
4374 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4375 int dirfd, const char *de_name)
4377 struct schedule_addition_args *a = arg;
4378 const struct got_error *err = NULL;
4379 struct got_fileindex_entry *ie;
4380 struct stat sb;
4381 char *ondisk_path;
4383 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4384 relpath) == -1)
4385 return got_error_from_errno("asprintf");
4387 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4388 if (ie) {
4389 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4390 de_name, a->repo);
4391 if (err)
4392 goto done;
4393 /* Re-adding an existing entry is a no-op. */
4394 if (staged_status == GOT_STATUS_NO_CHANGE &&
4395 add_noop_status(status))
4396 goto done;
4397 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4398 if (err)
4399 goto done;
4402 if (status != GOT_STATUS_UNVERSIONED) {
4403 if (status == GOT_STATUS_NONEXISTENT)
4404 err = got_error_set_errno(ENOENT, ondisk_path);
4405 else
4406 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4407 goto done;
4410 err = got_fileindex_entry_alloc(&ie, relpath);
4411 if (err)
4412 goto done;
4413 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4414 relpath, NULL, NULL, 1);
4415 if (err) {
4416 got_fileindex_entry_free(ie);
4417 goto done;
4419 err = got_fileindex_entry_add(a->fileindex, ie);
4420 if (err) {
4421 got_fileindex_entry_free(ie);
4422 goto done;
4424 done:
4425 free(ondisk_path);
4426 if (err)
4427 return err;
4428 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4429 return NULL;
4430 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4433 const struct got_error *
4434 got_worktree_schedule_add(struct got_worktree *worktree,
4435 struct got_pathlist_head *paths,
4436 got_worktree_checkout_cb progress_cb, void *progress_arg,
4437 struct got_repository *repo, int no_ignores)
4439 struct got_fileindex *fileindex = NULL;
4440 char *fileindex_path = NULL;
4441 const struct got_error *err = NULL, *sync_err, *unlockerr;
4442 struct got_pathlist_entry *pe;
4443 struct schedule_addition_args saa;
4445 err = lock_worktree(worktree, LOCK_EX);
4446 if (err)
4447 return err;
4449 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4450 if (err)
4451 goto done;
4453 saa.worktree = worktree;
4454 saa.fileindex = fileindex;
4455 saa.progress_cb = progress_cb;
4456 saa.progress_arg = progress_arg;
4457 saa.repo = repo;
4459 TAILQ_FOREACH(pe, paths, entry) {
4460 err = worktree_status(worktree, pe->path, fileindex, repo,
4461 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4462 if (err)
4463 break;
4465 sync_err = sync_fileindex(fileindex, fileindex_path);
4466 if (sync_err && err == NULL)
4467 err = sync_err;
4468 done:
4469 free(fileindex_path);
4470 if (fileindex)
4471 got_fileindex_free(fileindex);
4472 unlockerr = lock_worktree(worktree, LOCK_SH);
4473 if (unlockerr && err == NULL)
4474 err = unlockerr;
4475 return err;
4478 struct schedule_deletion_args {
4479 struct got_worktree *worktree;
4480 struct got_fileindex *fileindex;
4481 got_worktree_delete_cb progress_cb;
4482 void *progress_arg;
4483 struct got_repository *repo;
4484 int delete_local_mods;
4485 int keep_on_disk;
4486 int ignore_missing_paths;
4487 const char *status_path;
4488 size_t status_path_len;
4489 const char *status_codes;
4492 static const struct got_error *
4493 schedule_for_deletion(void *arg, unsigned char status,
4494 unsigned char staged_status, const char *relpath,
4495 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4496 struct got_object_id *commit_id, int dirfd, const char *de_name)
4498 struct schedule_deletion_args *a = arg;
4499 const struct got_error *err = NULL;
4500 struct got_fileindex_entry *ie = NULL;
4501 struct stat sb;
4502 char *ondisk_path;
4504 if (status == GOT_STATUS_NONEXISTENT) {
4505 if (a->ignore_missing_paths)
4506 return NULL;
4507 return got_error_set_errno(ENOENT, relpath);
4510 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4511 if (ie == NULL)
4512 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4514 staged_status = get_staged_status(ie);
4515 if (staged_status != GOT_STATUS_NO_CHANGE) {
4516 if (staged_status == GOT_STATUS_DELETE)
4517 return NULL;
4518 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4521 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4522 relpath) == -1)
4523 return got_error_from_errno("asprintf");
4525 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4526 a->repo);
4527 if (err)
4528 goto done;
4530 if (a->status_codes) {
4531 size_t ncodes = strlen(a->status_codes);
4532 int i;
4533 for (i = 0; i < ncodes ; i++) {
4534 if (status == a->status_codes[i])
4535 break;
4537 if (i == ncodes) {
4538 /* Do not delete files in non-matching status. */
4539 free(ondisk_path);
4540 return NULL;
4542 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4543 a->status_codes[i] != GOT_STATUS_MISSING) {
4544 static char msg[64];
4545 snprintf(msg, sizeof(msg),
4546 "invalid status code '%c'", a->status_codes[i]);
4547 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4548 goto done;
4552 if (status != GOT_STATUS_NO_CHANGE) {
4553 if (status == GOT_STATUS_DELETE)
4554 goto done;
4555 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4556 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4557 goto done;
4559 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4560 err = got_error_set_errno(ENOENT, relpath);
4561 goto done;
4563 if (status != GOT_STATUS_MODIFY &&
4564 status != GOT_STATUS_MISSING) {
4565 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4566 goto done;
4570 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4571 size_t root_len;
4573 if (dirfd != -1) {
4574 if (unlinkat(dirfd, de_name, 0) == -1) {
4575 err = got_error_from_errno2("unlinkat",
4576 ondisk_path);
4577 goto done;
4579 } else if (unlink(ondisk_path) == -1) {
4580 err = got_error_from_errno2("unlink", ondisk_path);
4581 goto done;
4584 root_len = strlen(a->worktree->root_path);
4585 do {
4586 char *parent;
4588 err = got_path_dirname(&parent, ondisk_path);
4589 if (err)
4590 goto done;
4591 free(ondisk_path);
4592 ondisk_path = parent;
4593 if (got_path_cmp(ondisk_path, a->status_path,
4594 strlen(ondisk_path), a->status_path_len) != 0 &&
4595 !got_path_is_child(ondisk_path, a->status_path,
4596 a->status_path_len))
4597 break;
4598 if (rmdir(ondisk_path) == -1) {
4599 if (errno != ENOTEMPTY)
4600 err = got_error_from_errno2("rmdir",
4601 ondisk_path);
4602 break;
4604 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4605 strlen(ondisk_path), root_len) != 0);
4608 if (got_fileindex_entry_has_blob(ie))
4609 got_fileindex_entry_mark_deleted_from_disk(ie);
4610 else
4611 got_fileindex_entry_remove(a->fileindex, ie);
4612 done:
4613 free(ondisk_path);
4614 if (err)
4615 return err;
4616 if (status == GOT_STATUS_DELETE)
4617 return NULL;
4618 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4619 staged_status, relpath);
4622 const struct got_error *
4623 got_worktree_schedule_delete(struct got_worktree *worktree,
4624 struct got_pathlist_head *paths, int delete_local_mods,
4625 const char *status_codes,
4626 got_worktree_delete_cb progress_cb, void *progress_arg,
4627 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4629 struct got_fileindex *fileindex = NULL;
4630 char *fileindex_path = NULL;
4631 const struct got_error *err = NULL, *sync_err, *unlockerr;
4632 struct got_pathlist_entry *pe;
4633 struct schedule_deletion_args sda;
4635 err = lock_worktree(worktree, LOCK_EX);
4636 if (err)
4637 return err;
4639 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4640 if (err)
4641 goto done;
4643 sda.worktree = worktree;
4644 sda.fileindex = fileindex;
4645 sda.progress_cb = progress_cb;
4646 sda.progress_arg = progress_arg;
4647 sda.repo = repo;
4648 sda.delete_local_mods = delete_local_mods;
4649 sda.keep_on_disk = keep_on_disk;
4650 sda.ignore_missing_paths = ignore_missing_paths;
4651 sda.status_codes = status_codes;
4653 TAILQ_FOREACH(pe, paths, entry) {
4654 char *ondisk_status_path;
4656 if (asprintf(&ondisk_status_path, "%s%s%s",
4657 got_worktree_get_root_path(worktree),
4658 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4659 err = got_error_from_errno("asprintf");
4660 goto done;
4662 sda.status_path = ondisk_status_path;
4663 sda.status_path_len = strlen(ondisk_status_path);
4664 err = worktree_status(worktree, pe->path, fileindex, repo,
4665 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4666 free(ondisk_status_path);
4667 if (err)
4668 break;
4670 sync_err = sync_fileindex(fileindex, fileindex_path);
4671 if (sync_err && err == NULL)
4672 err = sync_err;
4673 done:
4674 free(fileindex_path);
4675 if (fileindex)
4676 got_fileindex_free(fileindex);
4677 unlockerr = lock_worktree(worktree, LOCK_SH);
4678 if (unlockerr && err == NULL)
4679 err = unlockerr;
4680 return err;
4683 static const struct got_error *
4684 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4686 const struct got_error *err = NULL;
4687 char *line = NULL;
4688 size_t linesize = 0, n;
4689 ssize_t linelen;
4691 linelen = getline(&line, &linesize, infile);
4692 if (linelen == -1) {
4693 if (ferror(infile)) {
4694 err = got_error_from_errno("getline");
4695 goto done;
4697 return NULL;
4699 if (outfile) {
4700 n = fwrite(line, 1, linelen, outfile);
4701 if (n != linelen) {
4702 err = got_ferror(outfile, GOT_ERR_IO);
4703 goto done;
4706 if (rejectfile) {
4707 n = fwrite(line, 1, linelen, rejectfile);
4708 if (n != linelen)
4709 err = got_ferror(rejectfile, GOT_ERR_IO);
4711 done:
4712 free(line);
4713 return err;
4716 static const struct got_error *
4717 skip_one_line(FILE *f)
4719 char *line = NULL;
4720 size_t linesize = 0;
4721 ssize_t linelen;
4723 linelen = getline(&line, &linesize, f);
4724 if (linelen == -1) {
4725 if (ferror(f))
4726 return got_error_from_errno("getline");
4727 return NULL;
4729 free(line);
4730 return NULL;
4733 static const struct got_error *
4734 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4735 int start_old, int end_old, int start_new, int end_new,
4736 FILE *outfile, FILE *rejectfile)
4738 const struct got_error *err;
4740 /* Copy old file's lines leading up to patch. */
4741 while (!feof(f1) && *line_cur1 < start_old) {
4742 err = copy_one_line(f1, outfile, NULL);
4743 if (err)
4744 return err;
4745 (*line_cur1)++;
4747 /* Skip new file's lines leading up to patch. */
4748 while (!feof(f2) && *line_cur2 < start_new) {
4749 if (rejectfile)
4750 err = copy_one_line(f2, NULL, rejectfile);
4751 else
4752 err = skip_one_line(f2);
4753 if (err)
4754 return err;
4755 (*line_cur2)++;
4757 /* Copy patched lines. */
4758 while (!feof(f2) && *line_cur2 <= end_new) {
4759 err = copy_one_line(f2, outfile, NULL);
4760 if (err)
4761 return err;
4762 (*line_cur2)++;
4764 /* Skip over old file's replaced lines. */
4765 while (!feof(f1) && *line_cur1 <= end_old) {
4766 if (rejectfile)
4767 err = copy_one_line(f1, NULL, rejectfile);
4768 else
4769 err = skip_one_line(f1);
4770 if (err)
4771 return err;
4772 (*line_cur1)++;
4775 return NULL;
4778 static const struct got_error *
4779 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4780 FILE *outfile, FILE *rejectfile)
4782 const struct got_error *err;
4784 if (outfile) {
4785 /* Copy old file's lines until EOF. */
4786 while (!feof(f1)) {
4787 err = copy_one_line(f1, outfile, NULL);
4788 if (err)
4789 return err;
4790 (*line_cur1)++;
4793 if (rejectfile) {
4794 /* Copy new file's lines until EOF. */
4795 while (!feof(f2)) {
4796 err = copy_one_line(f2, NULL, rejectfile);
4797 if (err)
4798 return err;
4799 (*line_cur2)++;
4803 return NULL;
4806 static const struct got_error *
4807 apply_or_reject_change(int *choice, int *nchunks_used,
4808 struct diff_result *diff_result, int n,
4809 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4810 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4811 got_worktree_patch_cb patch_cb, void *patch_arg)
4813 const struct got_error *err = NULL;
4814 struct diff_chunk_context cc = {};
4815 int start_old, end_old, start_new, end_new;
4816 FILE *hunkfile;
4817 struct diff_output_unidiff_state *diff_state;
4818 struct diff_input_info diff_info;
4819 int rc;
4821 *choice = GOT_PATCH_CHOICE_NONE;
4823 /* Get changed line numbers without context lines for copy_change(). */
4824 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4825 start_old = cc.left.start;
4826 end_old = cc.left.end;
4827 start_new = cc.right.start;
4828 end_new = cc.right.end;
4830 /* Get the same change with context lines for display. */
4831 memset(&cc, 0, sizeof(cc));
4832 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4834 memset(&diff_info, 0, sizeof(diff_info));
4835 diff_info.left_path = relpath;
4836 diff_info.right_path = relpath;
4838 diff_state = diff_output_unidiff_state_alloc();
4839 if (diff_state == NULL)
4840 return got_error_set_errno(ENOMEM,
4841 "diff_output_unidiff_state_alloc");
4843 hunkfile = got_opentemp();
4844 if (hunkfile == NULL) {
4845 err = got_error_from_errno("got_opentemp");
4846 goto done;
4849 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4850 diff_result, &cc);
4851 if (rc != DIFF_RC_OK) {
4852 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4853 goto done;
4856 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4857 err = got_ferror(hunkfile, GOT_ERR_IO);
4858 goto done;
4861 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4862 hunkfile, changeno, nchanges);
4863 if (err)
4864 goto done;
4866 switch (*choice) {
4867 case GOT_PATCH_CHOICE_YES:
4868 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4869 end_old, start_new, end_new, outfile, rejectfile);
4870 break;
4871 case GOT_PATCH_CHOICE_NO:
4872 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4873 end_old, start_new, end_new, rejectfile, outfile);
4874 break;
4875 case GOT_PATCH_CHOICE_QUIT:
4876 break;
4877 default:
4878 err = got_error(GOT_ERR_PATCH_CHOICE);
4879 break;
4881 done:
4882 diff_output_unidiff_state_free(diff_state);
4883 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4884 err = got_error_from_errno("fclose");
4885 return err;
4888 struct revert_file_args {
4889 struct got_worktree *worktree;
4890 struct got_fileindex *fileindex;
4891 got_worktree_checkout_cb progress_cb;
4892 void *progress_arg;
4893 got_worktree_patch_cb patch_cb;
4894 void *patch_arg;
4895 struct got_repository *repo;
4896 int unlink_added_files;
4897 struct got_pathlist_head *added_files_to_unlink;
4900 static const struct got_error *
4901 create_patched_content(char **path_outfile, int reverse_patch,
4902 struct got_object_id *blob_id, const char *path2,
4903 int dirfd2, const char *de_name2,
4904 const char *relpath, struct got_repository *repo,
4905 got_worktree_patch_cb patch_cb, void *patch_arg)
4907 const struct got_error *err, *free_err;
4908 struct got_blob_object *blob = NULL;
4909 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4910 int fd = -1, fd2 = -1;
4911 char link_target[PATH_MAX];
4912 ssize_t link_len = 0;
4913 char *path1 = NULL, *id_str = NULL;
4914 struct stat sb2;
4915 struct got_diffreg_result *diffreg_result = NULL;
4916 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4917 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4919 *path_outfile = NULL;
4921 err = got_object_id_str(&id_str, blob_id);
4922 if (err)
4923 return err;
4925 if (dirfd2 != -1) {
4926 fd2 = openat(dirfd2, de_name2,
4927 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4928 if (fd2 == -1) {
4929 if (!got_err_open_nofollow_on_symlink()) {
4930 err = got_error_from_errno2("openat", path2);
4931 goto done;
4933 link_len = readlinkat(dirfd2, de_name2,
4934 link_target, sizeof(link_target));
4935 if (link_len == -1) {
4936 return got_error_from_errno2("readlinkat",
4937 path2);
4939 sb2.st_mode = S_IFLNK;
4940 sb2.st_size = link_len;
4942 } else {
4943 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4944 if (fd2 == -1) {
4945 if (!got_err_open_nofollow_on_symlink()) {
4946 err = got_error_from_errno2("open", path2);
4947 goto done;
4949 link_len = readlink(path2, link_target,
4950 sizeof(link_target));
4951 if (link_len == -1)
4952 return got_error_from_errno2("readlink", path2);
4953 sb2.st_mode = S_IFLNK;
4954 sb2.st_size = link_len;
4957 if (fd2 != -1) {
4958 if (fstat(fd2, &sb2) == -1) {
4959 err = got_error_from_errno2("fstat", path2);
4960 goto done;
4963 f2 = fdopen(fd2, "r");
4964 if (f2 == NULL) {
4965 err = got_error_from_errno2("fdopen", path2);
4966 goto done;
4968 fd2 = -1;
4969 } else {
4970 size_t n;
4971 f2 = got_opentemp();
4972 if (f2 == NULL) {
4973 err = got_error_from_errno2("got_opentemp", path2);
4974 goto done;
4976 n = fwrite(link_target, 1, link_len, f2);
4977 if (n != link_len) {
4978 err = got_ferror(f2, GOT_ERR_IO);
4979 goto done;
4981 if (fflush(f2) == EOF) {
4982 err = got_error_from_errno("fflush");
4983 goto done;
4985 rewind(f2);
4988 fd = got_opentempfd();
4989 if (fd == -1) {
4990 err = got_error_from_errno("got_opentempfd");
4991 goto done;
4994 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4995 if (err)
4996 goto done;
4998 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4999 if (err)
5000 goto done;
5002 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5003 if (err)
5004 goto done;
5006 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5007 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5008 if (err)
5009 goto done;
5011 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5012 "");
5013 if (err)
5014 goto done;
5016 if (fseek(f1, 0L, SEEK_SET) == -1)
5017 return got_ferror(f1, GOT_ERR_IO);
5018 if (fseek(f2, 0L, SEEK_SET) == -1)
5019 return got_ferror(f2, GOT_ERR_IO);
5021 /* Count the number of actual changes in the diff result. */
5022 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5023 struct diff_chunk_context cc = {};
5024 diff_chunk_context_load_change(&cc, &nchunks_used,
5025 diffreg_result->result, n, 0);
5026 nchanges++;
5028 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5029 int choice;
5030 err = apply_or_reject_change(&choice, &nchunks_used,
5031 diffreg_result->result, n, relpath, f1, f2,
5032 &line_cur1, &line_cur2,
5033 reverse_patch ? NULL : outfile,
5034 reverse_patch ? outfile : NULL,
5035 ++i, nchanges, patch_cb, patch_arg);
5036 if (err)
5037 goto done;
5038 if (choice == GOT_PATCH_CHOICE_YES)
5039 have_content = 1;
5040 else if (choice == GOT_PATCH_CHOICE_QUIT)
5041 break;
5043 if (have_content) {
5044 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5045 reverse_patch ? NULL : outfile,
5046 reverse_patch ? outfile : NULL);
5047 if (err)
5048 goto done;
5050 if (!S_ISLNK(sb2.st_mode)) {
5051 mode_t mode;
5053 mode = apply_umask(sb2.st_mode);
5054 if (fchmod(fileno(outfile), mode) == -1) {
5055 err = got_error_from_errno2("fchmod", path2);
5056 goto done;
5060 done:
5061 free(id_str);
5062 if (fd != -1 && close(fd) == -1 && err == NULL)
5063 err = got_error_from_errno("close");
5064 if (blob)
5065 got_object_blob_close(blob);
5066 free_err = got_diffreg_result_free(diffreg_result);
5067 if (err == NULL)
5068 err = free_err;
5069 if (f1 && fclose(f1) == EOF && err == NULL)
5070 err = got_error_from_errno2("fclose", path1);
5071 if (f2 && fclose(f2) == EOF && err == NULL)
5072 err = got_error_from_errno2("fclose", path2);
5073 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5074 err = got_error_from_errno2("close", path2);
5075 if (outfile && fclose(outfile) == EOF && err == NULL)
5076 err = got_error_from_errno2("fclose", *path_outfile);
5077 if (path1 && unlink(path1) == -1 && err == NULL)
5078 err = got_error_from_errno2("unlink", path1);
5079 if (err || !have_content) {
5080 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5081 err = got_error_from_errno2("unlink", *path_outfile);
5082 free(*path_outfile);
5083 *path_outfile = NULL;
5085 free(path1);
5086 return err;
5089 static const struct got_error *
5090 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5091 const char *relpath, struct got_object_id *blob_id,
5092 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5093 int dirfd, const char *de_name)
5095 struct revert_file_args *a = arg;
5096 const struct got_error *err = NULL;
5097 char *parent_path = NULL;
5098 struct got_fileindex_entry *ie;
5099 struct got_commit_object *base_commit = NULL;
5100 struct got_tree_object *tree = NULL;
5101 struct got_object_id *tree_id = NULL;
5102 const struct got_tree_entry *te = NULL;
5103 char *tree_path = NULL, *te_name;
5104 char *ondisk_path = NULL, *path_content = NULL;
5105 struct got_blob_object *blob = NULL;
5106 int fd = -1;
5108 /* Reverting a staged deletion is a no-op. */
5109 if (status == GOT_STATUS_DELETE &&
5110 staged_status != GOT_STATUS_NO_CHANGE)
5111 return NULL;
5113 if (status == GOT_STATUS_UNVERSIONED)
5114 return (*a->progress_cb)(a->progress_arg,
5115 GOT_STATUS_UNVERSIONED, relpath);
5117 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5118 if (ie == NULL)
5119 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5121 /* Construct in-repository path of tree which contains this blob. */
5122 err = got_path_dirname(&parent_path, ie->path);
5123 if (err) {
5124 if (err->code != GOT_ERR_BAD_PATH)
5125 goto done;
5126 parent_path = strdup("/");
5127 if (parent_path == NULL) {
5128 err = got_error_from_errno("strdup");
5129 goto done;
5132 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5133 tree_path = strdup(parent_path);
5134 if (tree_path == NULL) {
5135 err = got_error_from_errno("strdup");
5136 goto done;
5138 } else {
5139 if (got_path_is_root_dir(parent_path)) {
5140 tree_path = strdup(a->worktree->path_prefix);
5141 if (tree_path == NULL) {
5142 err = got_error_from_errno("strdup");
5143 goto done;
5145 } else {
5146 if (asprintf(&tree_path, "%s/%s",
5147 a->worktree->path_prefix, parent_path) == -1) {
5148 err = got_error_from_errno("asprintf");
5149 goto done;
5154 err = got_object_open_as_commit(&base_commit, a->repo,
5155 a->worktree->base_commit_id);
5156 if (err)
5157 goto done;
5159 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5160 if (err) {
5161 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5162 (status == GOT_STATUS_ADD ||
5163 staged_status == GOT_STATUS_ADD)))
5164 goto done;
5165 } else {
5166 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5167 if (err)
5168 goto done;
5170 err = got_path_basename(&te_name, ie->path);
5171 if (err)
5172 goto done;
5174 te = got_object_tree_find_entry(tree, te_name);
5175 free(te_name);
5176 if (te == NULL && status != GOT_STATUS_ADD &&
5177 staged_status != GOT_STATUS_ADD) {
5178 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5179 goto done;
5183 switch (status) {
5184 case GOT_STATUS_ADD:
5185 if (a->patch_cb) {
5186 int choice = GOT_PATCH_CHOICE_NONE;
5187 err = (*a->patch_cb)(&choice, a->patch_arg,
5188 status, ie->path, NULL, 1, 1);
5189 if (err)
5190 goto done;
5191 if (choice != GOT_PATCH_CHOICE_YES)
5192 break;
5194 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5195 ie->path);
5196 if (err)
5197 goto done;
5198 got_fileindex_entry_remove(a->fileindex, ie);
5199 if (a->unlink_added_files) {
5200 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5202 if (a->added_files_to_unlink) {
5203 struct got_pathlist_entry *pe;
5205 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5206 entry) {
5207 if (got_path_cmp(pe->path, relpath,
5208 pe->path_len, strlen(relpath)))
5209 continue;
5210 do_unlink = 1;
5211 break;
5215 if (do_unlink) {
5216 if (asprintf(&ondisk_path, "%s/%s",
5217 got_worktree_get_root_path(a->worktree),
5218 relpath) == -1) {
5219 err = got_error_from_errno("asprintf");
5220 goto done;
5222 if (unlink(ondisk_path) == -1) {
5223 err = got_error_from_errno2("unlink",
5224 ondisk_path);
5225 break;
5229 break;
5230 case GOT_STATUS_DELETE:
5231 if (a->patch_cb) {
5232 int choice = GOT_PATCH_CHOICE_NONE;
5233 err = (*a->patch_cb)(&choice, a->patch_arg,
5234 status, ie->path, NULL, 1, 1);
5235 if (err)
5236 goto done;
5237 if (choice != GOT_PATCH_CHOICE_YES)
5238 break;
5240 /* fall through */
5241 case GOT_STATUS_MODIFY:
5242 case GOT_STATUS_MODE_CHANGE:
5243 case GOT_STATUS_CONFLICT:
5244 case GOT_STATUS_MISSING: {
5245 struct got_object_id id;
5246 if (staged_status == GOT_STATUS_ADD ||
5247 staged_status == GOT_STATUS_MODIFY)
5248 got_fileindex_entry_get_staged_blob_id(&id, ie);
5249 else
5250 got_fileindex_entry_get_blob_id(&id, ie);
5251 fd = got_opentempfd();
5252 if (fd == -1) {
5253 err = got_error_from_errno("got_opentempfd");
5254 goto done;
5257 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5258 if (err)
5259 goto done;
5261 if (asprintf(&ondisk_path, "%s/%s",
5262 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5263 err = got_error_from_errno("asprintf");
5264 goto done;
5267 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5268 status == GOT_STATUS_CONFLICT)) {
5269 int is_bad_symlink = 0;
5270 err = create_patched_content(&path_content, 1, &id,
5271 ondisk_path, dirfd, de_name, ie->path, a->repo,
5272 a->patch_cb, a->patch_arg);
5273 if (err || path_content == NULL)
5274 break;
5275 if (te && S_ISLNK(te->mode)) {
5276 if (unlink(path_content) == -1) {
5277 err = got_error_from_errno2("unlink",
5278 path_content);
5279 break;
5281 err = install_symlink(&is_bad_symlink,
5282 a->worktree, ondisk_path, ie->path,
5283 blob, 0, 1, 0, 0, a->repo,
5284 a->progress_cb, a->progress_arg);
5285 } else {
5286 if (rename(path_content, ondisk_path) == -1) {
5287 err = got_error_from_errno3("rename",
5288 path_content, ondisk_path);
5289 goto done;
5292 } else {
5293 int is_bad_symlink = 0;
5294 if (te && S_ISLNK(te->mode)) {
5295 err = install_symlink(&is_bad_symlink,
5296 a->worktree, ondisk_path, ie->path,
5297 blob, 0, 1, 0, 0, a->repo,
5298 a->progress_cb, a->progress_arg);
5299 } else {
5300 err = install_blob(a->worktree, ondisk_path,
5301 ie->path,
5302 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5303 got_fileindex_perms_to_st(ie), blob,
5304 0, 1, 0, 0, NULL, a->repo,
5305 a->progress_cb, a->progress_arg);
5307 if (err)
5308 goto done;
5309 if (status == GOT_STATUS_DELETE ||
5310 status == GOT_STATUS_MODE_CHANGE) {
5311 err = got_fileindex_entry_update(ie,
5312 a->worktree->root_fd, relpath,
5313 blob->id.sha1,
5314 a->worktree->base_commit_id->sha1, 1);
5315 if (err)
5316 goto done;
5318 if (is_bad_symlink) {
5319 got_fileindex_entry_filetype_set(ie,
5320 GOT_FILEIDX_MODE_BAD_SYMLINK);
5323 break;
5325 default:
5326 break;
5328 done:
5329 free(ondisk_path);
5330 free(path_content);
5331 free(parent_path);
5332 free(tree_path);
5333 if (fd != -1 && close(fd) == -1 && err == NULL)
5334 err = got_error_from_errno("close");
5335 if (blob)
5336 got_object_blob_close(blob);
5337 if (tree)
5338 got_object_tree_close(tree);
5339 free(tree_id);
5340 if (base_commit)
5341 got_object_commit_close(base_commit);
5342 return err;
5345 const struct got_error *
5346 got_worktree_revert(struct got_worktree *worktree,
5347 struct got_pathlist_head *paths,
5348 got_worktree_checkout_cb progress_cb, void *progress_arg,
5349 got_worktree_patch_cb patch_cb, void *patch_arg,
5350 struct got_repository *repo)
5352 struct got_fileindex *fileindex = NULL;
5353 char *fileindex_path = NULL;
5354 const struct got_error *err = NULL, *unlockerr = NULL;
5355 const struct got_error *sync_err = NULL;
5356 struct got_pathlist_entry *pe;
5357 struct revert_file_args rfa;
5359 err = lock_worktree(worktree, LOCK_EX);
5360 if (err)
5361 return err;
5363 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5364 if (err)
5365 goto done;
5367 rfa.worktree = worktree;
5368 rfa.fileindex = fileindex;
5369 rfa.progress_cb = progress_cb;
5370 rfa.progress_arg = progress_arg;
5371 rfa.patch_cb = patch_cb;
5372 rfa.patch_arg = patch_arg;
5373 rfa.repo = repo;
5374 rfa.unlink_added_files = 0;
5375 TAILQ_FOREACH(pe, paths, entry) {
5376 err = worktree_status(worktree, pe->path, fileindex, repo,
5377 revert_file, &rfa, NULL, NULL, 1, 0);
5378 if (err)
5379 break;
5381 sync_err = sync_fileindex(fileindex, fileindex_path);
5382 if (sync_err && err == NULL)
5383 err = sync_err;
5384 done:
5385 free(fileindex_path);
5386 if (fileindex)
5387 got_fileindex_free(fileindex);
5388 unlockerr = lock_worktree(worktree, LOCK_SH);
5389 if (unlockerr && err == NULL)
5390 err = unlockerr;
5391 return err;
5394 static void
5395 free_commitable(struct got_commitable *ct)
5397 free(ct->path);
5398 free(ct->in_repo_path);
5399 free(ct->ondisk_path);
5400 free(ct->blob_id);
5401 free(ct->base_blob_id);
5402 free(ct->staged_blob_id);
5403 free(ct->base_commit_id);
5404 free(ct);
5407 struct collect_commitables_arg {
5408 struct got_pathlist_head *commitable_paths;
5409 struct got_repository *repo;
5410 struct got_worktree *worktree;
5411 struct got_fileindex *fileindex;
5412 int have_staged_files;
5413 int allow_bad_symlinks;
5414 int diff_header_shown;
5415 int commit_conflicts;
5416 FILE *diff_outfile;
5417 FILE *f1;
5418 FILE *f2;
5422 * Create a file which contains the target path of a symlink so we can feed
5423 * it as content to the diff engine.
5425 static const struct got_error *
5426 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5427 const char *abspath)
5429 const struct got_error *err = NULL;
5430 char target_path[PATH_MAX];
5431 ssize_t target_len, outlen;
5433 *fd = -1;
5435 if (dirfd != -1) {
5436 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5437 if (target_len == -1)
5438 return got_error_from_errno2("readlinkat", abspath);
5439 } else {
5440 target_len = readlink(abspath, target_path, PATH_MAX);
5441 if (target_len == -1)
5442 return got_error_from_errno2("readlink", abspath);
5445 *fd = got_opentempfd();
5446 if (*fd == -1)
5447 return got_error_from_errno("got_opentempfd");
5449 outlen = write(*fd, target_path, target_len);
5450 if (outlen == -1) {
5451 err = got_error_from_errno("got_opentempfd");
5452 goto done;
5455 if (lseek(*fd, 0, SEEK_SET) == -1) {
5456 err = got_error_from_errno2("lseek", abspath);
5457 goto done;
5459 done:
5460 if (err) {
5461 close(*fd);
5462 *fd = -1;
5464 return err;
5467 static const struct got_error *
5468 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5469 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5470 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5472 const struct got_error *err = NULL;
5473 struct got_blob_object *blob1 = NULL;
5474 int fd = -1, fd1 = -1, fd2 = -1;
5475 FILE *ondisk_file = NULL;
5476 char *label1 = NULL;
5477 struct stat sb;
5478 off_t size1 = 0;
5479 int f2_exists = 0;
5480 char *id_str = NULL;
5482 memset(&sb, 0, sizeof(sb));
5484 if (diff_staged) {
5485 if (ct->staged_status != GOT_STATUS_MODIFY &&
5486 ct->staged_status != GOT_STATUS_ADD &&
5487 ct->staged_status != GOT_STATUS_DELETE)
5488 return NULL;
5489 } else {
5490 if (ct->status != GOT_STATUS_MODIFY &&
5491 ct->status != GOT_STATUS_ADD &&
5492 ct->status != GOT_STATUS_DELETE &&
5493 ct->status != GOT_STATUS_CONFLICT)
5494 return NULL;
5497 err = got_opentemp_truncate(f1);
5498 if (err)
5499 return got_error_from_errno("got_opentemp_truncate");
5500 err = got_opentemp_truncate(f2);
5501 if (err)
5502 return got_error_from_errno("got_opentemp_truncate");
5504 if (!*diff_header_shown) {
5505 err = got_object_id_str(&id_str, worktree->base_commit_id);
5506 if (err)
5507 return err;
5508 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5509 got_worktree_get_root_path(worktree));
5510 fprintf(diff_outfile, "commit - %s\n", id_str);
5511 fprintf(diff_outfile, "path + %s%s\n",
5512 got_worktree_get_root_path(worktree),
5513 diff_staged ? " (staged changes)" : "");
5514 *diff_header_shown = 1;
5517 if (diff_staged) {
5518 const char *label1 = NULL, *label2 = NULL;
5519 switch (ct->staged_status) {
5520 case GOT_STATUS_MODIFY:
5521 label1 = ct->path;
5522 label2 = ct->path;
5523 break;
5524 case GOT_STATUS_ADD:
5525 label2 = ct->path;
5526 break;
5527 case GOT_STATUS_DELETE:
5528 label1 = ct->path;
5529 break;
5530 default:
5531 return got_error(GOT_ERR_FILE_STATUS);
5533 fd1 = got_opentempfd();
5534 if (fd1 == -1) {
5535 err = got_error_from_errno("got_opentempfd");
5536 goto done;
5538 fd2 = got_opentempfd();
5539 if (fd2 == -1) {
5540 err = got_error_from_errno("got_opentempfd");
5541 goto done;
5543 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5544 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5545 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5546 NULL, repo, diff_outfile);
5547 goto done;
5550 fd1 = got_opentempfd();
5551 if (fd1 == -1) {
5552 err = got_error_from_errno("got_opentempfd");
5553 goto done;
5556 if (ct->status != GOT_STATUS_ADD) {
5557 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5558 8192, fd1);
5559 if (err)
5560 goto done;
5563 if (ct->status != GOT_STATUS_DELETE) {
5564 if (dirfd != -1) {
5565 fd = openat(dirfd, de_name,
5566 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5567 if (fd == -1) {
5568 if (!got_err_open_nofollow_on_symlink()) {
5569 err = got_error_from_errno2("openat",
5570 ct->ondisk_path);
5571 goto done;
5573 err = get_symlink_target_file(&fd, dirfd,
5574 de_name, ct->ondisk_path);
5575 if (err)
5576 goto done;
5578 } else {
5579 fd = open(ct->ondisk_path,
5580 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5581 if (fd == -1) {
5582 if (!got_err_open_nofollow_on_symlink()) {
5583 err = got_error_from_errno2("open",
5584 ct->ondisk_path);
5585 goto done;
5587 err = get_symlink_target_file(&fd, dirfd,
5588 de_name, ct->ondisk_path);
5589 if (err)
5590 goto done;
5593 if (fstatat(fd, ct->ondisk_path, &sb,
5594 AT_SYMLINK_NOFOLLOW) == -1) {
5595 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5596 goto done;
5598 ondisk_file = fdopen(fd, "r");
5599 if (ondisk_file == NULL) {
5600 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5601 goto done;
5603 fd = -1;
5604 f2_exists = 1;
5607 if (blob1) {
5608 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5609 f1, blob1);
5610 if (err)
5611 goto done;
5614 err = got_diff_blob_file(blob1, f1, size1, label1,
5615 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5616 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5617 done:
5618 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5619 err = got_error_from_errno("close");
5620 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5621 err = got_error_from_errno("close");
5622 if (blob1)
5623 got_object_blob_close(blob1);
5624 if (fd != -1 && close(fd) == -1 && err == NULL)
5625 err = got_error_from_errno("close");
5626 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5627 err = got_error_from_errno("fclose");
5628 return err;
5631 static const struct got_error *
5632 collect_commitables(void *arg, unsigned char status,
5633 unsigned char staged_status, const char *relpath,
5634 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5635 struct got_object_id *commit_id, int dirfd, const char *de_name)
5637 struct collect_commitables_arg *a = arg;
5638 const struct got_error *err = NULL;
5639 struct got_commitable *ct = NULL;
5640 struct got_pathlist_entry *new = NULL;
5641 char *parent_path = NULL, *path = NULL;
5642 struct stat sb;
5644 if (a->have_staged_files) {
5645 if (staged_status != GOT_STATUS_MODIFY &&
5646 staged_status != GOT_STATUS_ADD &&
5647 staged_status != GOT_STATUS_DELETE)
5648 return NULL;
5649 } else {
5650 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5651 printf("C %s\n", relpath);
5652 return got_error(GOT_ERR_COMMIT_CONFLICT);
5655 if (status != GOT_STATUS_MODIFY &&
5656 status != GOT_STATUS_MODE_CHANGE &&
5657 status != GOT_STATUS_ADD &&
5658 status != GOT_STATUS_DELETE &&
5659 status != GOT_STATUS_CONFLICT)
5660 return NULL;
5663 if (asprintf(&path, "/%s", relpath) == -1) {
5664 err = got_error_from_errno("asprintf");
5665 goto done;
5667 if (strcmp(path, "/") == 0) {
5668 parent_path = strdup("");
5669 if (parent_path == NULL)
5670 return got_error_from_errno("strdup");
5671 } else {
5672 err = got_path_dirname(&parent_path, path);
5673 if (err)
5674 return err;
5677 ct = calloc(1, sizeof(*ct));
5678 if (ct == NULL) {
5679 err = got_error_from_errno("calloc");
5680 goto done;
5683 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5684 relpath) == -1) {
5685 err = got_error_from_errno("asprintf");
5686 goto done;
5689 if (staged_status == GOT_STATUS_ADD ||
5690 staged_status == GOT_STATUS_MODIFY) {
5691 struct got_fileindex_entry *ie;
5692 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5693 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5694 case GOT_FILEIDX_MODE_REGULAR_FILE:
5695 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5696 ct->mode = S_IFREG;
5697 break;
5698 case GOT_FILEIDX_MODE_SYMLINK:
5699 ct->mode = S_IFLNK;
5700 break;
5701 default:
5702 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5703 goto done;
5705 ct->mode |= got_fileindex_entry_perms_get(ie);
5706 } else if (status != GOT_STATUS_DELETE &&
5707 staged_status != GOT_STATUS_DELETE) {
5708 if (dirfd != -1) {
5709 if (fstatat(dirfd, de_name, &sb,
5710 AT_SYMLINK_NOFOLLOW) == -1) {
5711 err = got_error_from_errno2("fstatat",
5712 ct->ondisk_path);
5713 goto done;
5715 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5716 err = got_error_from_errno2("lstat", ct->ondisk_path);
5717 goto done;
5719 ct->mode = sb.st_mode;
5722 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5723 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5724 relpath) == -1) {
5725 err = got_error_from_errno("asprintf");
5726 goto done;
5729 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5730 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5731 int is_bad_symlink;
5732 char target_path[PATH_MAX];
5733 ssize_t target_len;
5734 target_len = readlink(ct->ondisk_path, target_path,
5735 sizeof(target_path));
5736 if (target_len == -1) {
5737 err = got_error_from_errno2("readlink",
5738 ct->ondisk_path);
5739 goto done;
5741 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5742 target_len, ct->ondisk_path, a->worktree->root_path,
5743 a->worktree->meta_dir);
5744 if (err)
5745 goto done;
5746 if (is_bad_symlink) {
5747 err = got_error_path(ct->ondisk_path,
5748 GOT_ERR_BAD_SYMLINK);
5749 goto done;
5754 ct->status = status;
5755 ct->staged_status = staged_status;
5756 ct->blob_id = NULL; /* will be filled in when blob gets created */
5757 if (ct->status != GOT_STATUS_ADD &&
5758 ct->staged_status != GOT_STATUS_ADD) {
5759 ct->base_blob_id = got_object_id_dup(blob_id);
5760 if (ct->base_blob_id == NULL) {
5761 err = got_error_from_errno("got_object_id_dup");
5762 goto done;
5764 ct->base_commit_id = got_object_id_dup(commit_id);
5765 if (ct->base_commit_id == NULL) {
5766 err = got_error_from_errno("got_object_id_dup");
5767 goto done;
5770 if (ct->staged_status == GOT_STATUS_ADD ||
5771 ct->staged_status == GOT_STATUS_MODIFY) {
5772 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5773 if (ct->staged_blob_id == NULL) {
5774 err = got_error_from_errno("got_object_id_dup");
5775 goto done;
5778 ct->path = strdup(path);
5779 if (ct->path == NULL) {
5780 err = got_error_from_errno("strdup");
5781 goto done;
5784 if (a->diff_outfile) {
5785 err = append_ct_diff(ct, &a->diff_header_shown,
5786 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5787 a->have_staged_files, a->repo, a->worktree);
5788 if (err)
5789 goto done;
5792 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5793 done:
5794 if (ct && (err || new == NULL))
5795 free_commitable(ct);
5796 free(parent_path);
5797 free(path);
5798 return err;
5801 static const struct got_error *write_tree(struct got_object_id **, int *,
5802 struct got_tree_object *, const char *, struct got_pathlist_head *,
5803 got_worktree_status_cb status_cb, void *status_arg,
5804 struct got_repository *);
5806 static const struct got_error *
5807 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5808 struct got_tree_entry *te, const char *parent_path,
5809 struct got_pathlist_head *commitable_paths,
5810 got_worktree_status_cb status_cb, void *status_arg,
5811 struct got_repository *repo)
5813 const struct got_error *err = NULL;
5814 struct got_tree_object *subtree;
5815 char *subpath;
5817 if (asprintf(&subpath, "%s%s%s", parent_path,
5818 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5819 return got_error_from_errno("asprintf");
5821 err = got_object_open_as_tree(&subtree, repo, &te->id);
5822 if (err)
5823 return err;
5825 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5826 commitable_paths, status_cb, status_arg, repo);
5827 got_object_tree_close(subtree);
5828 free(subpath);
5829 return err;
5832 static const struct got_error *
5833 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5835 const struct got_error *err = NULL;
5836 char *ct_parent_path = NULL;
5838 *match = 0;
5840 if (strchr(ct->in_repo_path, '/') == NULL) {
5841 *match = got_path_is_root_dir(path);
5842 return NULL;
5845 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5846 if (err)
5847 return err;
5848 *match = (strcmp(path, ct_parent_path) == 0);
5849 free(ct_parent_path);
5850 return err;
5853 static mode_t
5854 get_ct_file_mode(struct got_commitable *ct)
5856 if (S_ISLNK(ct->mode))
5857 return S_IFLNK;
5859 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5862 static const struct got_error *
5863 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5864 struct got_tree_entry *te, struct got_commitable *ct)
5866 const struct got_error *err = NULL;
5868 *new_te = NULL;
5870 err = got_object_tree_entry_dup(new_te, te);
5871 if (err)
5872 goto done;
5874 (*new_te)->mode = get_ct_file_mode(ct);
5876 if (ct->staged_status == GOT_STATUS_MODIFY)
5877 memcpy(&(*new_te)->id, ct->staged_blob_id,
5878 sizeof((*new_te)->id));
5879 else
5880 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5881 done:
5882 if (err && *new_te) {
5883 free(*new_te);
5884 *new_te = NULL;
5886 return err;
5889 static const struct got_error *
5890 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5891 struct got_commitable *ct)
5893 const struct got_error *err = NULL;
5894 char *ct_name = NULL;
5896 *new_te = NULL;
5898 *new_te = calloc(1, sizeof(**new_te));
5899 if (*new_te == NULL)
5900 return got_error_from_errno("calloc");
5902 err = got_path_basename(&ct_name, ct->path);
5903 if (err)
5904 goto done;
5905 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5906 sizeof((*new_te)->name)) {
5907 err = got_error(GOT_ERR_NO_SPACE);
5908 goto done;
5911 (*new_te)->mode = get_ct_file_mode(ct);
5913 if (ct->staged_status == GOT_STATUS_ADD)
5914 memcpy(&(*new_te)->id, ct->staged_blob_id,
5915 sizeof((*new_te)->id));
5916 else
5917 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5918 done:
5919 free(ct_name);
5920 if (err && *new_te) {
5921 free(*new_te);
5922 *new_te = NULL;
5924 return err;
5927 static const struct got_error *
5928 insert_tree_entry(struct got_tree_entry *new_te,
5929 struct got_pathlist_head *paths)
5931 const struct got_error *err = NULL;
5932 struct got_pathlist_entry *new_pe;
5934 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5935 if (err)
5936 return err;
5937 if (new_pe == NULL)
5938 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5939 return NULL;
5942 static const struct got_error *
5943 report_ct_status(struct got_commitable *ct,
5944 got_worktree_status_cb status_cb, void *status_arg)
5946 const char *ct_path = ct->path;
5947 unsigned char status;
5949 if (status_cb == NULL) /* no commit progress output desired */
5950 return NULL;
5952 while (ct_path[0] == '/')
5953 ct_path++;
5955 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5956 status = ct->staged_status;
5957 else
5958 status = ct->status;
5960 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5961 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5964 static const struct got_error *
5965 match_modified_subtree(int *modified, struct got_tree_entry *te,
5966 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5968 const struct got_error *err = NULL;
5969 struct got_pathlist_entry *pe;
5970 char *te_path;
5972 *modified = 0;
5974 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5975 got_path_is_root_dir(base_tree_path) ? "" : "/",
5976 te->name) == -1)
5977 return got_error_from_errno("asprintf");
5979 TAILQ_FOREACH(pe, commitable_paths, entry) {
5980 struct got_commitable *ct = pe->data;
5981 *modified = got_path_is_child(ct->in_repo_path, te_path,
5982 strlen(te_path));
5983 if (*modified)
5984 break;
5987 free(te_path);
5988 return err;
5991 static const struct got_error *
5992 match_deleted_or_modified_ct(struct got_commitable **ctp,
5993 struct got_tree_entry *te, const char *base_tree_path,
5994 struct got_pathlist_head *commitable_paths)
5996 const struct got_error *err = NULL;
5997 struct got_pathlist_entry *pe;
5999 *ctp = NULL;
6001 TAILQ_FOREACH(pe, commitable_paths, entry) {
6002 struct got_commitable *ct = pe->data;
6003 char *ct_name = NULL;
6004 int path_matches;
6006 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6007 if (ct->status != GOT_STATUS_MODIFY &&
6008 ct->status != GOT_STATUS_MODE_CHANGE &&
6009 ct->status != GOT_STATUS_DELETE &&
6010 ct->status != GOT_STATUS_CONFLICT)
6011 continue;
6012 } else {
6013 if (ct->staged_status != GOT_STATUS_MODIFY &&
6014 ct->staged_status != GOT_STATUS_DELETE)
6015 continue;
6018 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6019 continue;
6021 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6022 if (err)
6023 return err;
6024 if (!path_matches)
6025 continue;
6027 err = got_path_basename(&ct_name, pe->path);
6028 if (err)
6029 return err;
6031 if (strcmp(te->name, ct_name) != 0) {
6032 free(ct_name);
6033 continue;
6035 free(ct_name);
6037 *ctp = ct;
6038 break;
6041 return err;
6044 static const struct got_error *
6045 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6046 const char *child_path, const char *path_base_tree,
6047 struct got_pathlist_head *commitable_paths,
6048 got_worktree_status_cb status_cb, void *status_arg,
6049 struct got_repository *repo)
6051 const struct got_error *err = NULL;
6052 struct got_tree_entry *new_te;
6053 char *subtree_path;
6054 struct got_object_id *id = NULL;
6055 int nentries;
6057 *new_tep = NULL;
6059 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6060 got_path_is_root_dir(path_base_tree) ? "" : "/",
6061 child_path) == -1)
6062 return got_error_from_errno("asprintf");
6064 new_te = calloc(1, sizeof(*new_te));
6065 if (new_te == NULL)
6066 return got_error_from_errno("calloc");
6067 new_te->mode = S_IFDIR;
6069 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6070 sizeof(new_te->name)) {
6071 err = got_error(GOT_ERR_NO_SPACE);
6072 goto done;
6074 err = write_tree(&id, &nentries, NULL, subtree_path,
6075 commitable_paths, status_cb, status_arg, repo);
6076 if (err) {
6077 free(new_te);
6078 goto done;
6080 memcpy(&new_te->id, id, sizeof(new_te->id));
6081 done:
6082 free(id);
6083 free(subtree_path);
6084 if (err == NULL)
6085 *new_tep = new_te;
6086 return err;
6089 static const struct got_error *
6090 write_tree(struct got_object_id **new_tree_id, int *nentries,
6091 struct got_tree_object *base_tree, const char *path_base_tree,
6092 struct got_pathlist_head *commitable_paths,
6093 got_worktree_status_cb status_cb, void *status_arg,
6094 struct got_repository *repo)
6096 const struct got_error *err = NULL;
6097 struct got_pathlist_head paths;
6098 struct got_tree_entry *te, *new_te = NULL;
6099 struct got_pathlist_entry *pe;
6101 TAILQ_INIT(&paths);
6102 *nentries = 0;
6104 /* Insert, and recurse into, newly added entries first. */
6105 TAILQ_FOREACH(pe, commitable_paths, entry) {
6106 struct got_commitable *ct = pe->data;
6107 char *child_path = NULL, *slash;
6109 if ((ct->status != GOT_STATUS_ADD &&
6110 ct->staged_status != GOT_STATUS_ADD) ||
6111 (ct->flags & GOT_COMMITABLE_ADDED))
6112 continue;
6114 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6115 strlen(path_base_tree)))
6116 continue;
6118 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6119 ct->in_repo_path);
6120 if (err)
6121 goto done;
6123 slash = strchr(child_path, '/');
6124 if (slash == NULL) {
6125 err = alloc_added_blob_tree_entry(&new_te, ct);
6126 if (err)
6127 goto done;
6128 err = report_ct_status(ct, status_cb, status_arg);
6129 if (err)
6130 goto done;
6131 ct->flags |= GOT_COMMITABLE_ADDED;
6132 err = insert_tree_entry(new_te, &paths);
6133 if (err)
6134 goto done;
6135 (*nentries)++;
6136 } else {
6137 *slash = '\0'; /* trim trailing path components */
6138 if (base_tree == NULL ||
6139 got_object_tree_find_entry(base_tree, child_path)
6140 == NULL) {
6141 err = make_subtree_for_added_blob(&new_te,
6142 child_path, path_base_tree,
6143 commitable_paths, status_cb, status_arg,
6144 repo);
6145 if (err)
6146 goto done;
6147 err = insert_tree_entry(new_te, &paths);
6148 if (err)
6149 goto done;
6150 (*nentries)++;
6155 if (base_tree) {
6156 int i, nbase_entries;
6157 /* Handle modified and deleted entries. */
6158 nbase_entries = got_object_tree_get_nentries(base_tree);
6159 for (i = 0; i < nbase_entries; i++) {
6160 struct got_commitable *ct = NULL;
6162 te = got_object_tree_get_entry(base_tree, i);
6163 if (got_object_tree_entry_is_submodule(te)) {
6164 /* Entry is a submodule; just copy it. */
6165 err = got_object_tree_entry_dup(&new_te, te);
6166 if (err)
6167 goto done;
6168 err = insert_tree_entry(new_te, &paths);
6169 if (err)
6170 goto done;
6171 (*nentries)++;
6172 continue;
6175 if (S_ISDIR(te->mode)) {
6176 int modified;
6177 err = got_object_tree_entry_dup(&new_te, te);
6178 if (err)
6179 goto done;
6180 err = match_modified_subtree(&modified, te,
6181 path_base_tree, commitable_paths);
6182 if (err)
6183 goto done;
6184 /* Avoid recursion into unmodified subtrees. */
6185 if (modified) {
6186 struct got_object_id *new_id;
6187 int nsubentries;
6188 err = write_subtree(&new_id,
6189 &nsubentries, te,
6190 path_base_tree, commitable_paths,
6191 status_cb, status_arg, repo);
6192 if (err)
6193 goto done;
6194 if (nsubentries == 0) {
6195 /* All entries were deleted. */
6196 free(new_id);
6197 continue;
6199 memcpy(&new_te->id, new_id,
6200 sizeof(new_te->id));
6201 free(new_id);
6203 err = insert_tree_entry(new_te, &paths);
6204 if (err)
6205 goto done;
6206 (*nentries)++;
6207 continue;
6210 err = match_deleted_or_modified_ct(&ct, te,
6211 path_base_tree, commitable_paths);
6212 if (err)
6213 goto done;
6214 if (ct) {
6215 /* NB: Deleted entries get dropped here. */
6216 if (ct->status == GOT_STATUS_MODIFY ||
6217 ct->status == GOT_STATUS_MODE_CHANGE ||
6218 ct->status == GOT_STATUS_CONFLICT ||
6219 ct->staged_status == GOT_STATUS_MODIFY) {
6220 err = alloc_modified_blob_tree_entry(
6221 &new_te, te, ct);
6222 if (err)
6223 goto done;
6224 err = insert_tree_entry(new_te, &paths);
6225 if (err)
6226 goto done;
6227 (*nentries)++;
6229 err = report_ct_status(ct, status_cb,
6230 status_arg);
6231 if (err)
6232 goto done;
6233 } else {
6234 /* Entry is unchanged; just copy it. */
6235 err = got_object_tree_entry_dup(&new_te, te);
6236 if (err)
6237 goto done;
6238 err = insert_tree_entry(new_te, &paths);
6239 if (err)
6240 goto done;
6241 (*nentries)++;
6246 /* Write new list of entries; deleted entries have been dropped. */
6247 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6248 done:
6249 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6250 return err;
6253 static const struct got_error *
6254 update_fileindex_after_commit(struct got_worktree *worktree,
6255 struct got_pathlist_head *commitable_paths,
6256 struct got_object_id *new_base_commit_id,
6257 struct got_fileindex *fileindex, int have_staged_files)
6259 const struct got_error *err = NULL;
6260 struct got_pathlist_entry *pe;
6261 char *relpath = NULL;
6263 TAILQ_FOREACH(pe, commitable_paths, entry) {
6264 struct got_fileindex_entry *ie;
6265 struct got_commitable *ct = pe->data;
6267 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6269 err = got_path_skip_common_ancestor(&relpath,
6270 worktree->root_path, ct->ondisk_path);
6271 if (err)
6272 goto done;
6274 if (ie) {
6275 if (ct->status == GOT_STATUS_DELETE ||
6276 ct->staged_status == GOT_STATUS_DELETE) {
6277 got_fileindex_entry_remove(fileindex, ie);
6278 } else if (ct->staged_status == GOT_STATUS_ADD ||
6279 ct->staged_status == GOT_STATUS_MODIFY) {
6280 got_fileindex_entry_stage_set(ie,
6281 GOT_FILEIDX_STAGE_NONE);
6282 got_fileindex_entry_staged_filetype_set(ie, 0);
6284 err = got_fileindex_entry_update(ie,
6285 worktree->root_fd, relpath,
6286 ct->staged_blob_id->sha1,
6287 new_base_commit_id->sha1,
6288 !have_staged_files);
6289 } else
6290 err = got_fileindex_entry_update(ie,
6291 worktree->root_fd, relpath,
6292 ct->blob_id->sha1,
6293 new_base_commit_id->sha1,
6294 !have_staged_files);
6295 } else {
6296 err = got_fileindex_entry_alloc(&ie, pe->path);
6297 if (err)
6298 goto done;
6299 err = got_fileindex_entry_update(ie,
6300 worktree->root_fd, relpath, ct->blob_id->sha1,
6301 new_base_commit_id->sha1, 1);
6302 if (err) {
6303 got_fileindex_entry_free(ie);
6304 goto done;
6306 err = got_fileindex_entry_add(fileindex, ie);
6307 if (err) {
6308 got_fileindex_entry_free(ie);
6309 goto done;
6312 free(relpath);
6313 relpath = NULL;
6315 done:
6316 free(relpath);
6317 return err;
6321 static const struct got_error *
6322 check_out_of_date(const char *in_repo_path, unsigned char status,
6323 unsigned char staged_status, struct got_object_id *base_blob_id,
6324 struct got_object_id *base_commit_id,
6325 struct got_object_id *head_commit_id, struct got_repository *repo,
6326 int ood_errcode)
6328 const struct got_error *err = NULL;
6329 struct got_commit_object *commit = NULL;
6330 struct got_object_id *id = NULL;
6332 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6333 /* Trivial case: base commit == head commit */
6334 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6335 return NULL;
6337 * Ensure file content which local changes were based
6338 * on matches file content in the branch head.
6340 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6341 if (err)
6342 goto done;
6343 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6344 if (err) {
6345 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6346 err = got_error(ood_errcode);
6347 goto done;
6348 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6349 err = got_error(ood_errcode);
6350 } else {
6351 /* Require that added files don't exist in the branch head. */
6352 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6353 if (err)
6354 goto done;
6355 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6356 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6357 goto done;
6358 err = id ? got_error(ood_errcode) : NULL;
6360 done:
6361 free(id);
6362 if (commit)
6363 got_object_commit_close(commit);
6364 return err;
6367 static const struct got_error *
6368 commit_worktree(struct got_object_id **new_commit_id,
6369 struct got_pathlist_head *commitable_paths,
6370 struct got_object_id *head_commit_id,
6371 struct got_object_id *parent_id2,
6372 struct got_worktree *worktree,
6373 const char *author, const char *committer, char *diff_path,
6374 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6375 got_worktree_status_cb status_cb, void *status_arg,
6376 struct got_repository *repo)
6378 const struct got_error *err = NULL, *unlockerr = NULL;
6379 struct got_pathlist_entry *pe;
6380 const char *head_ref_name = NULL;
6381 struct got_commit_object *head_commit = NULL;
6382 struct got_reference *head_ref2 = NULL;
6383 struct got_object_id *head_commit_id2 = NULL;
6384 struct got_tree_object *head_tree = NULL;
6385 struct got_object_id *new_tree_id = NULL;
6386 int nentries, nparents = 0;
6387 struct got_object_id_queue parent_ids;
6388 struct got_object_qid *pid = NULL;
6389 char *logmsg = NULL;
6390 time_t timestamp;
6392 *new_commit_id = NULL;
6394 STAILQ_INIT(&parent_ids);
6396 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6397 if (err)
6398 goto done;
6400 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6401 if (err)
6402 goto done;
6404 if (commit_msg_cb != NULL) {
6405 err = commit_msg_cb(commitable_paths, diff_path,
6406 &logmsg, commit_arg);
6407 if (err)
6408 goto done;
6411 if (logmsg == NULL || strlen(logmsg) == 0) {
6412 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6413 goto done;
6416 /* Create blobs from added and modified files and record their IDs. */
6417 TAILQ_FOREACH(pe, commitable_paths, entry) {
6418 struct got_commitable *ct = pe->data;
6419 char *ondisk_path;
6421 /* Blobs for staged files already exist. */
6422 if (ct->staged_status == GOT_STATUS_ADD ||
6423 ct->staged_status == GOT_STATUS_MODIFY)
6424 continue;
6426 if (ct->status != GOT_STATUS_ADD &&
6427 ct->status != GOT_STATUS_MODIFY &&
6428 ct->status != GOT_STATUS_MODE_CHANGE &&
6429 ct->status != GOT_STATUS_CONFLICT)
6430 continue;
6432 if (asprintf(&ondisk_path, "%s/%s",
6433 worktree->root_path, pe->path) == -1) {
6434 err = got_error_from_errno("asprintf");
6435 goto done;
6437 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6438 free(ondisk_path);
6439 if (err)
6440 goto done;
6443 /* Recursively write new tree objects. */
6444 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6445 commitable_paths, status_cb, status_arg, repo);
6446 if (err)
6447 goto done;
6449 err = got_object_qid_alloc(&pid, head_commit_id);
6450 if (err)
6451 goto done;
6452 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6453 nparents++;
6454 if (parent_id2) {
6455 err = got_object_qid_alloc(&pid, parent_id2);
6456 if (err)
6457 goto done;
6458 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6459 nparents++;
6461 timestamp = time(NULL);
6462 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6463 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6464 if (logmsg != NULL)
6465 free(logmsg);
6466 if (err)
6467 goto done;
6469 /* Check if a concurrent commit to our branch has occurred. */
6470 head_ref_name = got_worktree_get_head_ref_name(worktree);
6471 if (head_ref_name == NULL) {
6472 err = got_error_from_errno("got_worktree_get_head_ref_name");
6473 goto done;
6475 /* Lock the reference here to prevent concurrent modification. */
6476 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6477 if (err)
6478 goto done;
6479 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6480 if (err)
6481 goto done;
6482 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6483 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6484 goto done;
6486 /* Update branch head in repository. */
6487 err = got_ref_change_ref(head_ref2, *new_commit_id);
6488 if (err)
6489 goto done;
6490 err = got_ref_write(head_ref2, repo);
6491 if (err)
6492 goto done;
6494 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6495 if (err)
6496 goto done;
6498 err = ref_base_commit(worktree, repo);
6499 if (err)
6500 goto done;
6501 done:
6502 got_object_id_queue_free(&parent_ids);
6503 if (head_tree)
6504 got_object_tree_close(head_tree);
6505 if (head_commit)
6506 got_object_commit_close(head_commit);
6507 free(head_commit_id2);
6508 if (head_ref2) {
6509 unlockerr = got_ref_unlock(head_ref2);
6510 if (unlockerr && err == NULL)
6511 err = unlockerr;
6512 got_ref_close(head_ref2);
6514 return err;
6517 static const struct got_error *
6518 check_path_is_commitable(const char *path,
6519 struct got_pathlist_head *commitable_paths)
6521 struct got_pathlist_entry *cpe = NULL;
6522 size_t path_len = strlen(path);
6524 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6525 struct got_commitable *ct = cpe->data;
6526 const char *ct_path = ct->path;
6528 while (ct_path[0] == '/')
6529 ct_path++;
6531 if (strcmp(path, ct_path) == 0 ||
6532 got_path_is_child(ct_path, path, path_len))
6533 break;
6536 if (cpe == NULL)
6537 return got_error_path(path, GOT_ERR_BAD_PATH);
6539 return NULL;
6542 static const struct got_error *
6543 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6545 int *have_staged_files = arg;
6547 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6548 *have_staged_files = 1;
6549 return got_error(GOT_ERR_CANCELLED);
6552 return NULL;
6555 static const struct got_error *
6556 check_non_staged_files(struct got_fileindex *fileindex,
6557 struct got_pathlist_head *paths)
6559 struct got_pathlist_entry *pe;
6560 struct got_fileindex_entry *ie;
6562 TAILQ_FOREACH(pe, paths, entry) {
6563 if (pe->path[0] == '\0')
6564 continue;
6565 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6566 if (ie == NULL)
6567 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6568 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6569 return got_error_path(pe->path,
6570 GOT_ERR_FILE_NOT_STAGED);
6573 return NULL;
6576 const struct got_error *
6577 got_worktree_commit(struct got_object_id **new_commit_id,
6578 struct got_worktree *worktree, struct got_pathlist_head *paths,
6579 const char *author, const char *committer, int allow_bad_symlinks,
6580 int show_diff, int commit_conflicts,
6581 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6582 got_worktree_status_cb status_cb, void *status_arg,
6583 struct got_repository *repo)
6585 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6586 struct got_fileindex *fileindex = NULL;
6587 char *fileindex_path = NULL;
6588 struct got_pathlist_head commitable_paths;
6589 struct collect_commitables_arg cc_arg;
6590 struct got_pathlist_entry *pe;
6591 struct got_reference *head_ref = NULL;
6592 struct got_object_id *head_commit_id = NULL;
6593 char *diff_path = NULL;
6594 int have_staged_files = 0;
6596 *new_commit_id = NULL;
6598 memset(&cc_arg, 0, sizeof(cc_arg));
6599 TAILQ_INIT(&commitable_paths);
6601 err = lock_worktree(worktree, LOCK_EX);
6602 if (err)
6603 goto done;
6605 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6606 if (err)
6607 goto done;
6609 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6610 if (err)
6611 goto done;
6613 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6614 if (err)
6615 goto done;
6617 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6618 &have_staged_files);
6619 if (err && err->code != GOT_ERR_CANCELLED)
6620 goto done;
6621 if (have_staged_files) {
6622 err = check_non_staged_files(fileindex, paths);
6623 if (err)
6624 goto done;
6627 cc_arg.commitable_paths = &commitable_paths;
6628 cc_arg.worktree = worktree;
6629 cc_arg.fileindex = fileindex;
6630 cc_arg.repo = repo;
6631 cc_arg.have_staged_files = have_staged_files;
6632 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6633 cc_arg.diff_header_shown = 0;
6634 cc_arg.commit_conflicts = commit_conflicts;
6635 if (show_diff) {
6636 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6637 GOT_TMPDIR_STR "/got", ".diff");
6638 if (err)
6639 goto done;
6640 cc_arg.f1 = got_opentemp();
6641 if (cc_arg.f1 == NULL) {
6642 err = got_error_from_errno("got_opentemp");
6643 goto done;
6645 cc_arg.f2 = got_opentemp();
6646 if (cc_arg.f2 == NULL) {
6647 err = got_error_from_errno("got_opentemp");
6648 goto done;
6652 TAILQ_FOREACH(pe, paths, entry) {
6653 err = worktree_status(worktree, pe->path, fileindex, repo,
6654 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6655 if (err)
6656 goto done;
6659 if (show_diff) {
6660 if (fflush(cc_arg.diff_outfile) == EOF) {
6661 err = got_error_from_errno("fflush");
6662 goto done;
6666 if (TAILQ_EMPTY(&commitable_paths)) {
6667 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6668 goto done;
6671 TAILQ_FOREACH(pe, paths, entry) {
6672 err = check_path_is_commitable(pe->path, &commitable_paths);
6673 if (err)
6674 goto done;
6677 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6678 struct got_commitable *ct = pe->data;
6679 const char *ct_path = ct->in_repo_path;
6681 while (ct_path[0] == '/')
6682 ct_path++;
6683 err = check_out_of_date(ct_path, ct->status,
6684 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6685 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6686 if (err)
6687 goto done;
6691 err = commit_worktree(new_commit_id, &commitable_paths,
6692 head_commit_id, NULL, worktree, author, committer,
6693 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6694 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6695 if (err)
6696 goto done;
6698 err = update_fileindex_after_commit(worktree, &commitable_paths,
6699 *new_commit_id, fileindex, have_staged_files);
6700 sync_err = sync_fileindex(fileindex, fileindex_path);
6701 if (sync_err && err == NULL)
6702 err = sync_err;
6703 done:
6704 if (fileindex)
6705 got_fileindex_free(fileindex);
6706 free(fileindex_path);
6707 unlockerr = lock_worktree(worktree, LOCK_SH);
6708 if (unlockerr && err == NULL)
6709 err = unlockerr;
6710 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6711 struct got_commitable *ct = pe->data;
6713 free_commitable(ct);
6715 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6716 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6717 err = got_error_from_errno2("unlink", diff_path);
6718 free(diff_path);
6719 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6720 err == NULL)
6721 err = got_error_from_errno("fclose");
6722 return err;
6725 const char *
6726 got_commitable_get_path(struct got_commitable *ct)
6728 return ct->path;
6731 unsigned int
6732 got_commitable_get_status(struct got_commitable *ct)
6734 return ct->status;
6737 struct check_rebase_ok_arg {
6738 struct got_worktree *worktree;
6739 struct got_repository *repo;
6742 static const struct got_error *
6743 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6745 const struct got_error *err = NULL;
6746 struct check_rebase_ok_arg *a = arg;
6747 unsigned char status;
6748 struct stat sb;
6749 char *ondisk_path;
6751 /* Reject rebase of a work tree with mixed base commits. */
6752 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6753 SHA1_DIGEST_LENGTH))
6754 return got_error(GOT_ERR_MIXED_COMMITS);
6756 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6757 == -1)
6758 return got_error_from_errno("asprintf");
6760 /* Reject rebase of a work tree with modified or staged files. */
6761 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6762 free(ondisk_path);
6763 if (err)
6764 return err;
6766 if (status != GOT_STATUS_NO_CHANGE)
6767 return got_error(GOT_ERR_MODIFIED);
6768 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6769 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6771 return NULL;
6774 const struct got_error *
6775 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6776 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6777 struct got_worktree *worktree, struct got_reference *branch,
6778 struct got_repository *repo)
6780 const struct got_error *err = NULL;
6781 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6782 char *branch_ref_name = NULL;
6783 char *fileindex_path = NULL;
6784 struct check_rebase_ok_arg ok_arg;
6785 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6786 struct got_object_id *wt_branch_tip = NULL;
6788 *new_base_branch_ref = NULL;
6789 *tmp_branch = NULL;
6790 *fileindex = NULL;
6792 err = lock_worktree(worktree, LOCK_EX);
6793 if (err)
6794 return err;
6796 err = open_fileindex(fileindex, &fileindex_path, worktree);
6797 if (err)
6798 goto done;
6800 ok_arg.worktree = worktree;
6801 ok_arg.repo = repo;
6802 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6803 &ok_arg);
6804 if (err)
6805 goto done;
6807 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6808 if (err)
6809 goto done;
6811 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6812 if (err)
6813 goto done;
6815 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6816 if (err)
6817 goto done;
6819 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6820 0);
6821 if (err)
6822 goto done;
6824 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6825 if (err)
6826 goto done;
6827 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6828 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6829 goto done;
6832 err = got_ref_alloc_symref(new_base_branch_ref,
6833 new_base_branch_ref_name, wt_branch);
6834 if (err)
6835 goto done;
6836 err = got_ref_write(*new_base_branch_ref, repo);
6837 if (err)
6838 goto done;
6840 /* TODO Lock original branch's ref while rebasing? */
6842 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6843 if (err)
6844 goto done;
6846 err = got_ref_write(branch_ref, repo);
6847 if (err)
6848 goto done;
6850 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6851 worktree->base_commit_id);
6852 if (err)
6853 goto done;
6854 err = got_ref_write(*tmp_branch, repo);
6855 if (err)
6856 goto done;
6858 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6859 if (err)
6860 goto done;
6861 done:
6862 free(fileindex_path);
6863 free(tmp_branch_name);
6864 free(new_base_branch_ref_name);
6865 free(branch_ref_name);
6866 if (branch_ref)
6867 got_ref_close(branch_ref);
6868 if (wt_branch)
6869 got_ref_close(wt_branch);
6870 free(wt_branch_tip);
6871 if (err) {
6872 if (*new_base_branch_ref) {
6873 got_ref_close(*new_base_branch_ref);
6874 *new_base_branch_ref = NULL;
6876 if (*tmp_branch) {
6877 got_ref_close(*tmp_branch);
6878 *tmp_branch = NULL;
6880 if (*fileindex) {
6881 got_fileindex_free(*fileindex);
6882 *fileindex = NULL;
6884 lock_worktree(worktree, LOCK_SH);
6886 return err;
6889 const struct got_error *
6890 got_worktree_rebase_continue(struct got_object_id **commit_id,
6891 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6892 struct got_reference **branch, struct got_fileindex **fileindex,
6893 struct got_worktree *worktree, struct got_repository *repo)
6895 const struct got_error *err;
6896 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6897 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6898 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6899 char *fileindex_path = NULL;
6900 int have_staged_files = 0;
6902 *commit_id = NULL;
6903 *new_base_branch = NULL;
6904 *tmp_branch = NULL;
6905 *branch = NULL;
6906 *fileindex = NULL;
6908 err = lock_worktree(worktree, LOCK_EX);
6909 if (err)
6910 return err;
6912 err = open_fileindex(fileindex, &fileindex_path, worktree);
6913 if (err)
6914 goto done;
6916 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6917 &have_staged_files);
6918 if (err && err->code != GOT_ERR_CANCELLED)
6919 goto done;
6920 if (have_staged_files) {
6921 err = got_error(GOT_ERR_STAGED_PATHS);
6922 goto done;
6925 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6926 if (err)
6927 goto done;
6929 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6930 if (err)
6931 goto done;
6933 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6934 if (err)
6935 goto done;
6937 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6938 if (err)
6939 goto done;
6941 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6942 if (err)
6943 goto done;
6945 err = got_ref_open(branch, repo,
6946 got_ref_get_symref_target(branch_ref), 0);
6947 if (err)
6948 goto done;
6950 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6951 if (err)
6952 goto done;
6954 err = got_ref_resolve(commit_id, repo, commit_ref);
6955 if (err)
6956 goto done;
6958 err = got_ref_open(new_base_branch, repo,
6959 new_base_branch_ref_name, 0);
6960 if (err)
6961 goto done;
6963 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6964 if (err)
6965 goto done;
6966 done:
6967 free(commit_ref_name);
6968 free(branch_ref_name);
6969 free(fileindex_path);
6970 if (commit_ref)
6971 got_ref_close(commit_ref);
6972 if (branch_ref)
6973 got_ref_close(branch_ref);
6974 if (err) {
6975 free(*commit_id);
6976 *commit_id = NULL;
6977 if (*tmp_branch) {
6978 got_ref_close(*tmp_branch);
6979 *tmp_branch = NULL;
6981 if (*new_base_branch) {
6982 got_ref_close(*new_base_branch);
6983 *new_base_branch = NULL;
6985 if (*branch) {
6986 got_ref_close(*branch);
6987 *branch = NULL;
6989 if (*fileindex) {
6990 got_fileindex_free(*fileindex);
6991 *fileindex = NULL;
6993 lock_worktree(worktree, LOCK_SH);
6995 return err;
6998 const struct got_error *
6999 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7001 const struct got_error *err;
7002 char *tmp_branch_name = NULL;
7004 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7005 if (err)
7006 return err;
7008 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7009 free(tmp_branch_name);
7010 return NULL;
7013 static const struct got_error *
7014 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7015 const char *diff_path, char **logmsg, void *arg)
7017 *logmsg = arg;
7018 return NULL;
7021 static const struct got_error *
7022 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7023 const char *path, struct got_object_id *blob_id,
7024 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7025 int dirfd, const char *de_name)
7027 return NULL;
7030 struct collect_merged_paths_arg {
7031 got_worktree_checkout_cb progress_cb;
7032 void *progress_arg;
7033 struct got_pathlist_head *merged_paths;
7036 static const struct got_error *
7037 collect_merged_paths(void *arg, unsigned char status, const char *path)
7039 const struct got_error *err;
7040 struct collect_merged_paths_arg *a = arg;
7041 char *p;
7042 struct got_pathlist_entry *new;
7044 err = (*a->progress_cb)(a->progress_arg, status, path);
7045 if (err)
7046 return err;
7048 if (status != GOT_STATUS_MERGE &&
7049 status != GOT_STATUS_ADD &&
7050 status != GOT_STATUS_DELETE &&
7051 status != GOT_STATUS_CONFLICT)
7052 return NULL;
7054 p = strdup(path);
7055 if (p == NULL)
7056 return got_error_from_errno("strdup");
7058 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7059 if (err || new == NULL)
7060 free(p);
7061 return err;
7064 static const struct got_error *
7065 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7066 int is_rebase, struct got_repository *repo)
7068 const struct got_error *err;
7069 struct got_reference *commit_ref = NULL;
7071 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7072 if (err) {
7073 if (err->code != GOT_ERR_NOT_REF)
7074 goto done;
7075 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7076 if (err)
7077 goto done;
7078 err = got_ref_write(commit_ref, repo);
7079 if (err)
7080 goto done;
7081 } else if (is_rebase) {
7082 struct got_object_id *stored_id;
7083 int cmp;
7085 err = got_ref_resolve(&stored_id, repo, commit_ref);
7086 if (err)
7087 goto done;
7088 cmp = got_object_id_cmp(commit_id, stored_id);
7089 free(stored_id);
7090 if (cmp != 0) {
7091 err = got_error(GOT_ERR_REBASE_COMMITID);
7092 goto done;
7095 done:
7096 if (commit_ref)
7097 got_ref_close(commit_ref);
7098 return err;
7101 static const struct got_error *
7102 rebase_merge_files(struct got_pathlist_head *merged_paths,
7103 const char *commit_ref_name, struct got_worktree *worktree,
7104 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7105 struct got_object_id *commit_id, struct got_repository *repo,
7106 got_worktree_checkout_cb progress_cb, void *progress_arg,
7107 got_cancel_cb cancel_cb, void *cancel_arg)
7109 const struct got_error *err;
7110 struct got_reference *commit_ref = NULL;
7111 struct collect_merged_paths_arg cmp_arg;
7112 char *fileindex_path;
7114 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7116 err = get_fileindex_path(&fileindex_path, worktree);
7117 if (err)
7118 return err;
7120 cmp_arg.progress_cb = progress_cb;
7121 cmp_arg.progress_arg = progress_arg;
7122 cmp_arg.merged_paths = merged_paths;
7123 err = merge_files(worktree, fileindex, fileindex_path,
7124 parent_commit_id, commit_id, repo, collect_merged_paths,
7125 &cmp_arg, cancel_cb, cancel_arg);
7126 if (commit_ref)
7127 got_ref_close(commit_ref);
7128 return err;
7131 const struct got_error *
7132 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7133 struct got_worktree *worktree, struct got_fileindex *fileindex,
7134 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7135 struct got_repository *repo,
7136 got_worktree_checkout_cb progress_cb, void *progress_arg,
7137 got_cancel_cb cancel_cb, void *cancel_arg)
7139 const struct got_error *err;
7140 char *commit_ref_name;
7142 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7143 if (err)
7144 return err;
7146 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7147 if (err)
7148 goto done;
7150 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7151 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7152 progress_arg, cancel_cb, cancel_arg);
7153 done:
7154 free(commit_ref_name);
7155 return err;
7158 const struct got_error *
7159 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7160 struct got_worktree *worktree, struct got_fileindex *fileindex,
7161 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7162 struct got_repository *repo,
7163 got_worktree_checkout_cb progress_cb, void *progress_arg,
7164 got_cancel_cb cancel_cb, void *cancel_arg)
7166 const struct got_error *err;
7167 char *commit_ref_name;
7169 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7170 if (err)
7171 return err;
7173 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7174 if (err)
7175 goto done;
7177 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7178 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7179 progress_arg, cancel_cb, cancel_arg);
7180 done:
7181 free(commit_ref_name);
7182 return err;
7185 static const struct got_error *
7186 rebase_commit(struct got_object_id **new_commit_id,
7187 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7188 struct got_worktree *worktree, struct got_fileindex *fileindex,
7189 struct got_reference *tmp_branch, const char *committer,
7190 struct got_commit_object *orig_commit, const char *new_logmsg,
7191 int allow_conflict, struct got_repository *repo)
7193 const struct got_error *err, *sync_err;
7194 struct got_pathlist_head commitable_paths;
7195 struct collect_commitables_arg cc_arg;
7196 char *fileindex_path = NULL;
7197 struct got_reference *head_ref = NULL;
7198 struct got_object_id *head_commit_id = NULL;
7199 char *logmsg = NULL;
7201 memset(&cc_arg, 0, sizeof(cc_arg));
7202 TAILQ_INIT(&commitable_paths);
7203 *new_commit_id = NULL;
7205 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7207 err = get_fileindex_path(&fileindex_path, worktree);
7208 if (err)
7209 return err;
7211 cc_arg.commitable_paths = &commitable_paths;
7212 cc_arg.worktree = worktree;
7213 cc_arg.repo = repo;
7214 cc_arg.have_staged_files = 0;
7215 cc_arg.commit_conflicts = allow_conflict;
7217 * If possible get the status of individual files directly to
7218 * avoid crawling the entire work tree once per rebased commit.
7220 * Ideally, merged_paths would contain a list of commitables
7221 * we could use so we could skip worktree_status() entirely.
7222 * However, we would then need carefully keep track of cumulative
7223 * effects of operations such as file additions and deletions
7224 * in 'got histedit -f' (folding multiple commits into one),
7225 * and this extra complexity is not really worth it.
7227 if (merged_paths) {
7228 struct got_pathlist_entry *pe;
7229 TAILQ_FOREACH(pe, merged_paths, entry) {
7230 err = worktree_status(worktree, pe->path, fileindex,
7231 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7232 0);
7233 if (err)
7234 goto done;
7236 } else {
7237 err = worktree_status(worktree, "", fileindex, repo,
7238 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7239 if (err)
7240 goto done;
7243 if (TAILQ_EMPTY(&commitable_paths)) {
7244 /* No-op change; commit will be elided. */
7245 err = got_ref_delete(commit_ref, repo);
7246 if (err)
7247 goto done;
7248 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7249 goto done;
7252 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7253 if (err)
7254 goto done;
7256 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7257 if (err)
7258 goto done;
7260 if (new_logmsg) {
7261 logmsg = strdup(new_logmsg);
7262 if (logmsg == NULL) {
7263 err = got_error_from_errno("strdup");
7264 goto done;
7266 } else {
7267 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7268 if (err)
7269 goto done;
7272 /* NB: commit_worktree will call free(logmsg) */
7273 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7274 NULL, worktree, got_object_commit_get_author(orig_commit),
7275 committer ? committer :
7276 got_object_commit_get_committer(orig_commit), NULL,
7277 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7278 if (err)
7279 goto done;
7281 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7282 if (err)
7283 goto done;
7285 err = got_ref_delete(commit_ref, repo);
7286 if (err)
7287 goto done;
7289 err = update_fileindex_after_commit(worktree, &commitable_paths,
7290 *new_commit_id, fileindex, 0);
7291 sync_err = sync_fileindex(fileindex, fileindex_path);
7292 if (sync_err && err == NULL)
7293 err = sync_err;
7294 done:
7295 free(fileindex_path);
7296 free(head_commit_id);
7297 if (head_ref)
7298 got_ref_close(head_ref);
7299 if (err) {
7300 free(*new_commit_id);
7301 *new_commit_id = NULL;
7303 return err;
7306 const struct got_error *
7307 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7308 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7309 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7310 const char *committer, struct got_commit_object *orig_commit,
7311 struct got_object_id *orig_commit_id, int allow_conflict,
7312 struct got_repository *repo)
7314 const struct got_error *err;
7315 char *commit_ref_name;
7316 struct got_reference *commit_ref = NULL;
7317 struct got_object_id *commit_id = NULL;
7319 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7320 if (err)
7321 return err;
7323 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7324 if (err)
7325 goto done;
7326 err = got_ref_resolve(&commit_id, repo, commit_ref);
7327 if (err)
7328 goto done;
7329 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7330 err = got_error(GOT_ERR_REBASE_COMMITID);
7331 goto done;
7334 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7335 worktree, fileindex, tmp_branch, committer, orig_commit,
7336 NULL, allow_conflict, repo);
7337 done:
7338 if (commit_ref)
7339 got_ref_close(commit_ref);
7340 free(commit_ref_name);
7341 free(commit_id);
7342 return err;
7345 const struct got_error *
7346 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7347 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7348 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7349 const char *committer, struct got_commit_object *orig_commit,
7350 struct got_object_id *orig_commit_id, const char *new_logmsg,
7351 int allow_conflict, struct got_repository *repo)
7353 const struct got_error *err;
7354 char *commit_ref_name;
7355 struct got_reference *commit_ref = NULL;
7357 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7358 if (err)
7359 return err;
7361 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7362 if (err)
7363 goto done;
7365 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7366 worktree, fileindex, tmp_branch, committer, orig_commit,
7367 new_logmsg, allow_conflict, repo);
7368 done:
7369 if (commit_ref)
7370 got_ref_close(commit_ref);
7371 free(commit_ref_name);
7372 return err;
7375 const struct got_error *
7376 got_worktree_rebase_postpone(struct got_worktree *worktree,
7377 struct got_fileindex *fileindex)
7379 if (fileindex)
7380 got_fileindex_free(fileindex);
7381 return lock_worktree(worktree, LOCK_SH);
7384 static const struct got_error *
7385 delete_ref(const char *name, struct got_repository *repo)
7387 const struct got_error *err;
7388 struct got_reference *ref;
7390 err = got_ref_open(&ref, repo, name, 0);
7391 if (err) {
7392 if (err->code == GOT_ERR_NOT_REF)
7393 return NULL;
7394 return err;
7397 err = got_ref_delete(ref, repo);
7398 got_ref_close(ref);
7399 return err;
7402 static const struct got_error *
7403 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7405 const struct got_error *err;
7406 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7407 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7409 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7410 if (err)
7411 goto done;
7412 err = delete_ref(tmp_branch_name, repo);
7413 if (err)
7414 goto done;
7416 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7417 if (err)
7418 goto done;
7419 err = delete_ref(new_base_branch_ref_name, repo);
7420 if (err)
7421 goto done;
7423 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7424 if (err)
7425 goto done;
7426 err = delete_ref(branch_ref_name, repo);
7427 if (err)
7428 goto done;
7430 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7431 if (err)
7432 goto done;
7433 err = delete_ref(commit_ref_name, repo);
7434 if (err)
7435 goto done;
7437 done:
7438 free(tmp_branch_name);
7439 free(new_base_branch_ref_name);
7440 free(branch_ref_name);
7441 free(commit_ref_name);
7442 return err;
7445 static const struct got_error *
7446 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7447 struct got_object_id *new_commit_id, struct got_repository *repo)
7449 const struct got_error *err;
7450 struct got_reference *ref = NULL;
7451 struct got_object_id *old_commit_id = NULL;
7452 const char *branch_name = NULL;
7453 char *new_id_str = NULL;
7454 char *refname = NULL;
7456 branch_name = got_ref_get_name(branch);
7457 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7458 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7459 branch_name += 11;
7461 err = got_object_id_str(&new_id_str, new_commit_id);
7462 if (err)
7463 return err;
7465 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7466 new_id_str) == -1) {
7467 err = got_error_from_errno("asprintf");
7468 goto done;
7471 err = got_ref_resolve(&old_commit_id, repo, branch);
7472 if (err)
7473 goto done;
7475 err = got_ref_alloc(&ref, refname, old_commit_id);
7476 if (err)
7477 goto done;
7479 err = got_ref_write(ref, repo);
7480 done:
7481 free(new_id_str);
7482 free(refname);
7483 free(old_commit_id);
7484 if (ref)
7485 got_ref_close(ref);
7486 return err;
7489 const struct got_error *
7490 got_worktree_rebase_complete(struct got_worktree *worktree,
7491 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7492 struct got_reference *rebased_branch, struct got_repository *repo,
7493 int create_backup)
7495 const struct got_error *err, *unlockerr, *sync_err;
7496 struct got_object_id *new_head_commit_id = NULL;
7497 char *fileindex_path = NULL;
7499 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7500 if (err)
7501 return err;
7503 if (create_backup) {
7504 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7505 rebased_branch, new_head_commit_id, repo);
7506 if (err)
7507 goto done;
7510 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7511 if (err)
7512 goto done;
7514 err = got_ref_write(rebased_branch, repo);
7515 if (err)
7516 goto done;
7518 err = got_worktree_set_head_ref(worktree, rebased_branch);
7519 if (err)
7520 goto done;
7522 err = delete_rebase_refs(worktree, repo);
7523 if (err)
7524 goto done;
7526 err = get_fileindex_path(&fileindex_path, worktree);
7527 if (err)
7528 goto done;
7529 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7530 sync_err = sync_fileindex(fileindex, fileindex_path);
7531 if (sync_err && err == NULL)
7532 err = sync_err;
7533 done:
7534 got_fileindex_free(fileindex);
7535 free(fileindex_path);
7536 free(new_head_commit_id);
7537 unlockerr = lock_worktree(worktree, LOCK_SH);
7538 if (unlockerr && err == NULL)
7539 err = unlockerr;
7540 return err;
7543 static const struct got_error *
7544 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7545 struct got_object_id *id1, struct got_object_id *id2,
7546 struct got_repository *repo)
7548 const struct got_error *err;
7549 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7550 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7552 if (id1) {
7553 err = got_object_open_as_commit(&commit1, repo, id1);
7554 if (err)
7555 goto done;
7557 err = got_object_open_as_tree(&tree1, repo,
7558 got_object_commit_get_tree_id(commit1));
7559 if (err)
7560 goto done;
7563 if (id2) {
7564 err = got_object_open_as_commit(&commit2, repo, id2);
7565 if (err)
7566 goto done;
7568 err = got_object_open_as_tree(&tree2, repo,
7569 got_object_commit_get_tree_id(commit2));
7570 if (err)
7571 goto done;
7574 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7575 got_diff_tree_collect_changed_paths, paths, 0);
7576 if (err)
7577 goto done;
7578 done:
7579 if (commit1)
7580 got_object_commit_close(commit1);
7581 if (commit2)
7582 got_object_commit_close(commit2);
7583 if (tree1)
7584 got_object_tree_close(tree1);
7585 if (tree2)
7586 got_object_tree_close(tree2);
7587 return err;
7590 static const struct got_error *
7591 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7592 struct got_object_id *id1, struct got_object_id *id2,
7593 const char *path_prefix, struct got_repository *repo)
7595 const struct got_error *err;
7596 struct got_pathlist_head merged_paths;
7597 struct got_pathlist_entry *pe;
7598 char *abspath = NULL, *wt_path = NULL;
7600 TAILQ_INIT(&merged_paths);
7602 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7603 if (err)
7604 goto done;
7606 TAILQ_FOREACH(pe, &merged_paths, entry) {
7607 struct got_diff_changed_path *change = pe->data;
7609 if (change->status != GOT_STATUS_ADD)
7610 continue;
7612 if (got_path_is_root_dir(path_prefix)) {
7613 wt_path = strdup(pe->path);
7614 if (wt_path == NULL) {
7615 err = got_error_from_errno("strdup");
7616 goto done;
7618 } else {
7619 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7620 err = got_error_from_errno("asprintf");
7621 goto done;
7624 err = got_path_skip_common_ancestor(&wt_path,
7625 path_prefix, abspath);
7626 if (err)
7627 goto done;
7628 free(abspath);
7629 abspath = NULL;
7632 err = got_pathlist_append(added_paths, wt_path, NULL);
7633 if (err)
7634 goto done;
7635 wt_path = NULL;
7638 done:
7639 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7640 free(abspath);
7641 free(wt_path);
7642 return err;
7645 static const struct got_error *
7646 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7647 struct got_object_id *id, const char *path_prefix,
7648 struct got_repository *repo)
7650 const struct got_error *err;
7651 struct got_commit_object *commit = NULL;
7652 struct got_object_qid *pid;
7654 err = got_object_open_as_commit(&commit, repo, id);
7655 if (err)
7656 goto done;
7658 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7660 err = get_paths_added_between_commits(added_paths,
7661 pid ? &pid->id : NULL, id, path_prefix, repo);
7662 if (err)
7663 goto done;
7664 done:
7665 if (commit)
7666 got_object_commit_close(commit);
7667 return err;
7670 const struct got_error *
7671 got_worktree_rebase_abort(struct got_worktree *worktree,
7672 struct got_fileindex *fileindex, struct got_repository *repo,
7673 struct got_reference *new_base_branch,
7674 got_worktree_checkout_cb progress_cb, void *progress_arg)
7676 const struct got_error *err, *unlockerr, *sync_err;
7677 struct got_reference *resolved = NULL;
7678 struct got_object_id *commit_id = NULL;
7679 struct got_object_id *merged_commit_id = NULL;
7680 struct got_commit_object *commit = NULL;
7681 char *fileindex_path = NULL;
7682 char *commit_ref_name = NULL;
7683 struct got_reference *commit_ref = NULL;
7684 struct revert_file_args rfa;
7685 struct got_object_id *tree_id = NULL;
7686 struct got_pathlist_head added_paths;
7688 TAILQ_INIT(&added_paths);
7690 err = lock_worktree(worktree, LOCK_EX);
7691 if (err)
7692 return err;
7694 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7695 if (err)
7696 goto done;
7698 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7699 if (err)
7700 goto done;
7702 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7703 if (err)
7704 goto done;
7707 * Determine which files in added status can be safely removed
7708 * from disk while reverting changes in the work tree.
7709 * We want to avoid deleting unrelated files which were added by
7710 * the user for conflict resolution purposes.
7712 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7713 got_worktree_get_path_prefix(worktree), repo);
7714 if (err)
7715 goto done;
7717 err = got_ref_open(&resolved, repo,
7718 got_ref_get_symref_target(new_base_branch), 0);
7719 if (err)
7720 goto done;
7722 err = got_worktree_set_head_ref(worktree, resolved);
7723 if (err)
7724 goto done;
7727 * XXX commits to the base branch could have happened while
7728 * we were busy rebasing; should we store the original commit ID
7729 * when rebase begins and read it back here?
7731 err = got_ref_resolve(&commit_id, repo, resolved);
7732 if (err)
7733 goto done;
7735 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7736 if (err)
7737 goto done;
7739 err = got_object_open_as_commit(&commit, repo,
7740 worktree->base_commit_id);
7741 if (err)
7742 goto done;
7744 err = got_object_id_by_path(&tree_id, repo, commit,
7745 worktree->path_prefix);
7746 if (err)
7747 goto done;
7749 err = delete_rebase_refs(worktree, repo);
7750 if (err)
7751 goto done;
7753 err = get_fileindex_path(&fileindex_path, worktree);
7754 if (err)
7755 goto done;
7757 rfa.worktree = worktree;
7758 rfa.fileindex = fileindex;
7759 rfa.progress_cb = progress_cb;
7760 rfa.progress_arg = progress_arg;
7761 rfa.patch_cb = NULL;
7762 rfa.patch_arg = NULL;
7763 rfa.repo = repo;
7764 rfa.unlink_added_files = 1;
7765 rfa.added_files_to_unlink = &added_paths;
7766 err = worktree_status(worktree, "", fileindex, repo,
7767 revert_file, &rfa, NULL, NULL, 1, 0);
7768 if (err)
7769 goto sync;
7771 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7772 repo, progress_cb, progress_arg, NULL, NULL);
7773 sync:
7774 sync_err = sync_fileindex(fileindex, fileindex_path);
7775 if (sync_err && err == NULL)
7776 err = sync_err;
7777 done:
7778 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7779 got_ref_close(resolved);
7780 free(tree_id);
7781 free(commit_id);
7782 free(merged_commit_id);
7783 if (commit)
7784 got_object_commit_close(commit);
7785 if (fileindex)
7786 got_fileindex_free(fileindex);
7787 free(fileindex_path);
7788 free(commit_ref_name);
7789 if (commit_ref)
7790 got_ref_close(commit_ref);
7792 unlockerr = lock_worktree(worktree, LOCK_SH);
7793 if (unlockerr && err == NULL)
7794 err = unlockerr;
7795 return err;
7798 const struct got_error *
7799 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7800 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7801 struct got_fileindex **fileindex, struct got_worktree *worktree,
7802 struct got_repository *repo)
7804 const struct got_error *err = NULL;
7805 char *tmp_branch_name = NULL;
7806 char *branch_ref_name = NULL;
7807 char *base_commit_ref_name = NULL;
7808 char *fileindex_path = NULL;
7809 struct check_rebase_ok_arg ok_arg;
7810 struct got_reference *wt_branch = NULL;
7811 struct got_reference *base_commit_ref = NULL;
7813 *tmp_branch = NULL;
7814 *branch_ref = NULL;
7815 *base_commit_id = NULL;
7816 *fileindex = NULL;
7818 err = lock_worktree(worktree, LOCK_EX);
7819 if (err)
7820 return err;
7822 err = open_fileindex(fileindex, &fileindex_path, worktree);
7823 if (err)
7824 goto done;
7826 ok_arg.worktree = worktree;
7827 ok_arg.repo = repo;
7828 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7829 &ok_arg);
7830 if (err)
7831 goto done;
7833 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7834 if (err)
7835 goto done;
7837 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7838 if (err)
7839 goto done;
7841 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7842 worktree);
7843 if (err)
7844 goto done;
7846 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7847 0);
7848 if (err)
7849 goto done;
7851 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7852 if (err)
7853 goto done;
7855 err = got_ref_write(*branch_ref, repo);
7856 if (err)
7857 goto done;
7859 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7860 worktree->base_commit_id);
7861 if (err)
7862 goto done;
7863 err = got_ref_write(base_commit_ref, repo);
7864 if (err)
7865 goto done;
7866 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7867 if (*base_commit_id == NULL) {
7868 err = got_error_from_errno("got_object_id_dup");
7869 goto done;
7872 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7873 worktree->base_commit_id);
7874 if (err)
7875 goto done;
7876 err = got_ref_write(*tmp_branch, repo);
7877 if (err)
7878 goto done;
7880 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7881 if (err)
7882 goto done;
7883 done:
7884 free(fileindex_path);
7885 free(tmp_branch_name);
7886 free(branch_ref_name);
7887 free(base_commit_ref_name);
7888 if (wt_branch)
7889 got_ref_close(wt_branch);
7890 if (err) {
7891 if (*branch_ref) {
7892 got_ref_close(*branch_ref);
7893 *branch_ref = NULL;
7895 if (*tmp_branch) {
7896 got_ref_close(*tmp_branch);
7897 *tmp_branch = NULL;
7899 free(*base_commit_id);
7900 if (*fileindex) {
7901 got_fileindex_free(*fileindex);
7902 *fileindex = NULL;
7904 lock_worktree(worktree, LOCK_SH);
7906 return err;
7909 const struct got_error *
7910 got_worktree_histedit_postpone(struct got_worktree *worktree,
7911 struct got_fileindex *fileindex)
7913 if (fileindex)
7914 got_fileindex_free(fileindex);
7915 return lock_worktree(worktree, LOCK_SH);
7918 const struct got_error *
7919 got_worktree_histedit_in_progress(int *in_progress,
7920 struct got_worktree *worktree)
7922 const struct got_error *err;
7923 char *tmp_branch_name = NULL;
7925 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7926 if (err)
7927 return err;
7929 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7930 free(tmp_branch_name);
7931 return NULL;
7934 const struct got_error *
7935 got_worktree_histedit_continue(struct got_object_id **commit_id,
7936 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7937 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7938 struct got_worktree *worktree, struct got_repository *repo)
7940 const struct got_error *err;
7941 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7942 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7943 struct got_reference *commit_ref = NULL;
7944 struct got_reference *base_commit_ref = NULL;
7945 char *fileindex_path = NULL;
7946 int have_staged_files = 0;
7948 *commit_id = NULL;
7949 *tmp_branch = NULL;
7950 *base_commit_id = NULL;
7951 *fileindex = NULL;
7953 err = lock_worktree(worktree, LOCK_EX);
7954 if (err)
7955 return err;
7957 err = open_fileindex(fileindex, &fileindex_path, worktree);
7958 if (err)
7959 goto done;
7961 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7962 &have_staged_files);
7963 if (err && err->code != GOT_ERR_CANCELLED)
7964 goto done;
7965 if (have_staged_files) {
7966 err = got_error(GOT_ERR_STAGED_PATHS);
7967 goto done;
7970 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7971 if (err)
7972 goto done;
7974 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7975 if (err)
7976 goto done;
7978 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7979 if (err)
7980 goto done;
7982 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7983 worktree);
7984 if (err)
7985 goto done;
7987 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7988 if (err)
7989 goto done;
7991 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7992 if (err)
7993 goto done;
7994 err = got_ref_resolve(commit_id, repo, commit_ref);
7995 if (err)
7996 goto done;
7998 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7999 if (err)
8000 goto done;
8001 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8002 if (err)
8003 goto done;
8005 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8006 if (err)
8007 goto done;
8008 done:
8009 free(commit_ref_name);
8010 free(branch_ref_name);
8011 free(fileindex_path);
8012 if (commit_ref)
8013 got_ref_close(commit_ref);
8014 if (base_commit_ref)
8015 got_ref_close(base_commit_ref);
8016 if (err) {
8017 free(*commit_id);
8018 *commit_id = NULL;
8019 free(*base_commit_id);
8020 *base_commit_id = NULL;
8021 if (*tmp_branch) {
8022 got_ref_close(*tmp_branch);
8023 *tmp_branch = NULL;
8025 if (*fileindex) {
8026 got_fileindex_free(*fileindex);
8027 *fileindex = NULL;
8029 lock_worktree(worktree, LOCK_EX);
8031 return err;
8034 static const struct got_error *
8035 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8037 const struct got_error *err;
8038 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8039 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8041 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8042 if (err)
8043 goto done;
8044 err = delete_ref(tmp_branch_name, repo);
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;
8052 err = delete_ref(base_commit_ref_name, repo);
8053 if (err)
8054 goto done;
8056 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8057 if (err)
8058 goto done;
8059 err = delete_ref(branch_ref_name, repo);
8060 if (err)
8061 goto done;
8063 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8064 if (err)
8065 goto done;
8066 err = delete_ref(commit_ref_name, repo);
8067 if (err)
8068 goto done;
8069 done:
8070 free(tmp_branch_name);
8071 free(base_commit_ref_name);
8072 free(branch_ref_name);
8073 free(commit_ref_name);
8074 return err;
8077 const struct got_error *
8078 got_worktree_histedit_abort(struct got_worktree *worktree,
8079 struct got_fileindex *fileindex, struct got_repository *repo,
8080 struct got_reference *branch, struct got_object_id *base_commit_id,
8081 got_worktree_checkout_cb progress_cb, void *progress_arg)
8083 const struct got_error *err, *unlockerr, *sync_err;
8084 struct got_reference *resolved = NULL;
8085 char *fileindex_path = NULL;
8086 struct got_object_id *merged_commit_id = NULL;
8087 struct got_commit_object *commit = NULL;
8088 char *commit_ref_name = NULL;
8089 struct got_reference *commit_ref = NULL;
8090 struct got_object_id *tree_id = NULL;
8091 struct revert_file_args rfa;
8092 struct got_pathlist_head added_paths;
8094 TAILQ_INIT(&added_paths);
8096 err = lock_worktree(worktree, LOCK_EX);
8097 if (err)
8098 return err;
8100 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8101 if (err)
8102 goto done;
8104 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8105 if (err) {
8106 if (err->code != GOT_ERR_NOT_REF)
8107 goto done;
8108 /* Can happen on early abort due to invalid histedit script. */
8109 commit_ref = NULL;
8112 if (commit_ref) {
8113 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8114 if (err)
8115 goto done;
8118 * Determine which files in added status can be safely removed
8119 * from disk while reverting changes in the work tree.
8120 * We want to avoid deleting unrelated files added by the
8121 * user during conflict resolution or during histedit -e.
8123 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8124 got_worktree_get_path_prefix(worktree), repo);
8125 if (err)
8126 goto done;
8129 err = got_ref_open(&resolved, repo,
8130 got_ref_get_symref_target(branch), 0);
8131 if (err)
8132 goto done;
8134 err = got_worktree_set_head_ref(worktree, resolved);
8135 if (err)
8136 goto done;
8138 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8139 if (err)
8140 goto done;
8142 err = got_object_open_as_commit(&commit, repo,
8143 worktree->base_commit_id);
8144 if (err)
8145 goto done;
8147 err = got_object_id_by_path(&tree_id, repo, commit,
8148 worktree->path_prefix);
8149 if (err)
8150 goto done;
8152 err = delete_histedit_refs(worktree, repo);
8153 if (err)
8154 goto done;
8156 err = get_fileindex_path(&fileindex_path, worktree);
8157 if (err)
8158 goto done;
8160 rfa.worktree = worktree;
8161 rfa.fileindex = fileindex;
8162 rfa.progress_cb = progress_cb;
8163 rfa.progress_arg = progress_arg;
8164 rfa.patch_cb = NULL;
8165 rfa.patch_arg = NULL;
8166 rfa.repo = repo;
8167 rfa.unlink_added_files = 1;
8168 rfa.added_files_to_unlink = &added_paths;
8169 err = worktree_status(worktree, "", fileindex, repo,
8170 revert_file, &rfa, NULL, NULL, 1, 0);
8171 if (err)
8172 goto sync;
8174 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8175 repo, progress_cb, progress_arg, NULL, NULL);
8176 sync:
8177 sync_err = sync_fileindex(fileindex, fileindex_path);
8178 if (sync_err && err == NULL)
8179 err = sync_err;
8180 done:
8181 if (resolved)
8182 got_ref_close(resolved);
8183 if (commit_ref)
8184 got_ref_close(commit_ref);
8185 free(merged_commit_id);
8186 free(tree_id);
8187 free(fileindex_path);
8188 free(commit_ref_name);
8190 unlockerr = lock_worktree(worktree, LOCK_SH);
8191 if (unlockerr && err == NULL)
8192 err = unlockerr;
8193 return err;
8196 const struct got_error *
8197 got_worktree_histedit_complete(struct got_worktree *worktree,
8198 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8199 struct got_reference *edited_branch, struct got_repository *repo)
8201 const struct got_error *err, *unlockerr, *sync_err;
8202 struct got_object_id *new_head_commit_id = NULL;
8203 struct got_reference *resolved = NULL;
8204 char *fileindex_path = NULL;
8206 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8207 if (err)
8208 return err;
8210 err = got_ref_open(&resolved, repo,
8211 got_ref_get_symref_target(edited_branch), 0);
8212 if (err)
8213 goto done;
8215 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8216 resolved, new_head_commit_id, repo);
8217 if (err)
8218 goto done;
8220 err = got_ref_change_ref(resolved, new_head_commit_id);
8221 if (err)
8222 goto done;
8224 err = got_ref_write(resolved, repo);
8225 if (err)
8226 goto done;
8228 err = got_worktree_set_head_ref(worktree, resolved);
8229 if (err)
8230 goto done;
8232 err = delete_histedit_refs(worktree, repo);
8233 if (err)
8234 goto done;
8236 err = get_fileindex_path(&fileindex_path, worktree);
8237 if (err)
8238 goto done;
8239 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8240 sync_err = sync_fileindex(fileindex, fileindex_path);
8241 if (sync_err && err == NULL)
8242 err = sync_err;
8243 done:
8244 got_fileindex_free(fileindex);
8245 free(fileindex_path);
8246 free(new_head_commit_id);
8247 unlockerr = lock_worktree(worktree, LOCK_SH);
8248 if (unlockerr && err == NULL)
8249 err = unlockerr;
8250 return err;
8253 const struct got_error *
8254 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8255 struct got_object_id *commit_id, struct got_repository *repo)
8257 const struct got_error *err;
8258 char *commit_ref_name;
8260 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8261 if (err)
8262 return err;
8264 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8265 if (err)
8266 goto done;
8268 err = delete_ref(commit_ref_name, repo);
8269 done:
8270 free(commit_ref_name);
8271 return err;
8274 const struct got_error *
8275 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8276 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8277 struct got_worktree *worktree, const char *refname,
8278 struct got_repository *repo)
8280 const struct got_error *err = NULL;
8281 char *fileindex_path = NULL;
8282 struct check_rebase_ok_arg ok_arg;
8284 *fileindex = NULL;
8285 *branch_ref = NULL;
8286 *base_branch_ref = NULL;
8288 err = lock_worktree(worktree, LOCK_EX);
8289 if (err)
8290 return err;
8292 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8293 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8294 "cannot integrate a branch into itself; "
8295 "update -b or different branch name required");
8296 goto done;
8299 err = open_fileindex(fileindex, &fileindex_path, worktree);
8300 if (err)
8301 goto done;
8303 /* Preconditions are the same as for rebase. */
8304 ok_arg.worktree = worktree;
8305 ok_arg.repo = repo;
8306 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8307 &ok_arg);
8308 if (err)
8309 goto done;
8311 err = got_ref_open(branch_ref, repo, refname, 1);
8312 if (err)
8313 goto done;
8315 err = got_ref_open(base_branch_ref, repo,
8316 got_worktree_get_head_ref_name(worktree), 1);
8317 done:
8318 if (err) {
8319 if (*branch_ref) {
8320 got_ref_close(*branch_ref);
8321 *branch_ref = NULL;
8323 if (*base_branch_ref) {
8324 got_ref_close(*base_branch_ref);
8325 *base_branch_ref = NULL;
8327 if (*fileindex) {
8328 got_fileindex_free(*fileindex);
8329 *fileindex = NULL;
8331 lock_worktree(worktree, LOCK_SH);
8333 return err;
8336 const struct got_error *
8337 got_worktree_integrate_continue(struct got_worktree *worktree,
8338 struct got_fileindex *fileindex, struct got_repository *repo,
8339 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8340 got_worktree_checkout_cb progress_cb, void *progress_arg,
8341 got_cancel_cb cancel_cb, void *cancel_arg)
8343 const struct got_error *err = NULL, *sync_err, *unlockerr;
8344 char *fileindex_path = NULL;
8345 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8346 struct got_commit_object *commit = NULL;
8348 err = get_fileindex_path(&fileindex_path, worktree);
8349 if (err)
8350 goto done;
8352 err = got_ref_resolve(&commit_id, repo, branch_ref);
8353 if (err)
8354 goto done;
8356 err = got_object_open_as_commit(&commit, repo, commit_id);
8357 if (err)
8358 goto done;
8360 err = got_object_id_by_path(&tree_id, repo, commit,
8361 worktree->path_prefix);
8362 if (err)
8363 goto done;
8365 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8366 if (err)
8367 goto done;
8369 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8370 progress_cb, progress_arg, cancel_cb, cancel_arg);
8371 if (err)
8372 goto sync;
8374 err = got_ref_change_ref(base_branch_ref, commit_id);
8375 if (err)
8376 goto sync;
8378 err = got_ref_write(base_branch_ref, repo);
8379 if (err)
8380 goto sync;
8382 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8383 sync:
8384 sync_err = sync_fileindex(fileindex, fileindex_path);
8385 if (sync_err && err == NULL)
8386 err = sync_err;
8388 done:
8389 unlockerr = got_ref_unlock(branch_ref);
8390 if (unlockerr && err == NULL)
8391 err = unlockerr;
8392 got_ref_close(branch_ref);
8394 unlockerr = got_ref_unlock(base_branch_ref);
8395 if (unlockerr && err == NULL)
8396 err = unlockerr;
8397 got_ref_close(base_branch_ref);
8399 got_fileindex_free(fileindex);
8400 free(fileindex_path);
8401 free(tree_id);
8402 if (commit)
8403 got_object_commit_close(commit);
8405 unlockerr = lock_worktree(worktree, LOCK_SH);
8406 if (unlockerr && err == NULL)
8407 err = unlockerr;
8408 return err;
8411 const struct got_error *
8412 got_worktree_integrate_abort(struct got_worktree *worktree,
8413 struct got_fileindex *fileindex, struct got_repository *repo,
8414 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8416 const struct got_error *err = NULL, *unlockerr = NULL;
8418 got_fileindex_free(fileindex);
8420 err = lock_worktree(worktree, LOCK_SH);
8422 unlockerr = got_ref_unlock(branch_ref);
8423 if (unlockerr && err == NULL)
8424 err = unlockerr;
8425 got_ref_close(branch_ref);
8427 unlockerr = got_ref_unlock(base_branch_ref);
8428 if (unlockerr && err == NULL)
8429 err = unlockerr;
8430 got_ref_close(base_branch_ref);
8432 return err;
8435 const struct got_error *
8436 got_worktree_merge_postpone(struct got_worktree *worktree,
8437 struct got_fileindex *fileindex)
8439 const struct got_error *err, *sync_err;
8440 char *fileindex_path = NULL;
8442 err = get_fileindex_path(&fileindex_path, worktree);
8443 if (err)
8444 goto done;
8446 sync_err = sync_fileindex(fileindex, fileindex_path);
8448 err = lock_worktree(worktree, LOCK_SH);
8449 if (sync_err && err == NULL)
8450 err = sync_err;
8451 done:
8452 got_fileindex_free(fileindex);
8453 free(fileindex_path);
8454 return err;
8457 static const struct got_error *
8458 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8460 const struct got_error *err;
8461 char *branch_refname = NULL, *commit_refname = NULL;
8463 err = get_merge_branch_ref_name(&branch_refname, worktree);
8464 if (err)
8465 goto done;
8466 err = delete_ref(branch_refname, repo);
8467 if (err)
8468 goto done;
8470 err = get_merge_commit_ref_name(&commit_refname, worktree);
8471 if (err)
8472 goto done;
8473 err = delete_ref(commit_refname, repo);
8474 if (err)
8475 goto done;
8477 done:
8478 free(branch_refname);
8479 free(commit_refname);
8480 return err;
8483 struct merge_commit_msg_arg {
8484 struct got_worktree *worktree;
8485 const char *branch_name;
8488 static const struct got_error *
8489 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8490 const char *diff_path, char **logmsg, void *arg)
8492 struct merge_commit_msg_arg *a = arg;
8494 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8495 got_worktree_get_head_ref_name(a->worktree)) == -1)
8496 return got_error_from_errno("asprintf");
8498 return NULL;
8502 const struct got_error *
8503 got_worktree_merge_branch(struct got_worktree *worktree,
8504 struct got_fileindex *fileindex,
8505 struct got_object_id *yca_commit_id,
8506 struct got_object_id *branch_tip,
8507 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8508 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8510 const struct got_error *err;
8511 char *fileindex_path = NULL;
8512 struct check_mixed_commits_args cma;
8514 err = get_fileindex_path(&fileindex_path, worktree);
8515 if (err)
8516 goto done;
8518 cma.worktree = worktree;
8519 cma.cancel_cb = cancel_cb;
8520 cma.cancel_arg = cancel_arg;
8522 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8523 &cma);
8524 if (err)
8525 goto done;
8527 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8528 branch_tip, repo, progress_cb, progress_arg,
8529 cancel_cb, cancel_arg);
8530 done:
8531 free(fileindex_path);
8532 return err;
8535 const struct got_error *
8536 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8537 struct got_worktree *worktree, struct got_fileindex *fileindex,
8538 const char *author, const char *committer, int allow_bad_symlinks,
8539 struct got_object_id *branch_tip, const char *branch_name,
8540 int allow_conflict, struct got_repository *repo,
8541 got_worktree_status_cb status_cb, void *status_arg)
8544 const struct got_error *err = NULL, *sync_err;
8545 struct got_pathlist_head commitable_paths;
8546 struct collect_commitables_arg cc_arg;
8547 struct got_pathlist_entry *pe;
8548 struct got_reference *head_ref = NULL;
8549 struct got_object_id *head_commit_id = NULL;
8550 int have_staged_files = 0;
8551 struct merge_commit_msg_arg mcm_arg;
8552 char *fileindex_path = NULL;
8554 memset(&cc_arg, 0, sizeof(cc_arg));
8555 *new_commit_id = NULL;
8557 TAILQ_INIT(&commitable_paths);
8559 err = get_fileindex_path(&fileindex_path, worktree);
8560 if (err)
8561 goto done;
8563 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8564 if (err)
8565 goto done;
8567 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8568 if (err)
8569 goto done;
8571 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8572 &have_staged_files);
8573 if (err && err->code != GOT_ERR_CANCELLED)
8574 goto done;
8575 if (have_staged_files) {
8576 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8577 goto done;
8580 cc_arg.commitable_paths = &commitable_paths;
8581 cc_arg.worktree = worktree;
8582 cc_arg.fileindex = fileindex;
8583 cc_arg.repo = repo;
8584 cc_arg.have_staged_files = have_staged_files;
8585 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8586 cc_arg.commit_conflicts = allow_conflict;
8587 err = worktree_status(worktree, "", fileindex, repo,
8588 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8589 if (err)
8590 goto done;
8592 mcm_arg.worktree = worktree;
8593 mcm_arg.branch_name = branch_name;
8594 err = commit_worktree(new_commit_id, &commitable_paths,
8595 head_commit_id, branch_tip, worktree, author, committer, NULL,
8596 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8597 if (err)
8598 goto done;
8600 err = update_fileindex_after_commit(worktree, &commitable_paths,
8601 *new_commit_id, fileindex, have_staged_files);
8602 sync_err = sync_fileindex(fileindex, fileindex_path);
8603 if (sync_err && err == NULL)
8604 err = sync_err;
8605 done:
8606 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8607 struct got_commitable *ct = pe->data;
8609 free_commitable(ct);
8611 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8612 free(fileindex_path);
8613 return err;
8616 const struct got_error *
8617 got_worktree_merge_complete(struct got_worktree *worktree,
8618 struct got_fileindex *fileindex, struct got_repository *repo)
8620 const struct got_error *err, *unlockerr, *sync_err;
8621 char *fileindex_path = NULL;
8623 err = delete_merge_refs(worktree, repo);
8624 if (err)
8625 goto done;
8627 err = get_fileindex_path(&fileindex_path, worktree);
8628 if (err)
8629 goto done;
8630 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8631 sync_err = sync_fileindex(fileindex, fileindex_path);
8632 if (sync_err && err == NULL)
8633 err = sync_err;
8634 done:
8635 got_fileindex_free(fileindex);
8636 free(fileindex_path);
8637 unlockerr = lock_worktree(worktree, LOCK_SH);
8638 if (unlockerr && err == NULL)
8639 err = unlockerr;
8640 return err;
8643 const struct got_error *
8644 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8645 struct got_repository *repo)
8647 const struct got_error *err;
8648 char *branch_refname = NULL;
8649 struct got_reference *branch_ref = NULL;
8651 *in_progress = 0;
8653 err = get_merge_branch_ref_name(&branch_refname, worktree);
8654 if (err)
8655 return err;
8656 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8657 free(branch_refname);
8658 if (err) {
8659 if (err->code != GOT_ERR_NOT_REF)
8660 return err;
8661 } else
8662 *in_progress = 1;
8664 return NULL;
8667 const struct got_error *got_worktree_merge_prepare(
8668 struct got_fileindex **fileindex, struct got_worktree *worktree,
8669 struct got_repository *repo)
8671 const struct got_error *err = NULL;
8672 char *fileindex_path = NULL;
8673 struct got_reference *wt_branch = NULL;
8674 struct got_object_id *wt_branch_tip = NULL;
8675 struct check_rebase_ok_arg ok_arg;
8677 *fileindex = NULL;
8679 err = lock_worktree(worktree, LOCK_EX);
8680 if (err)
8681 return err;
8683 err = open_fileindex(fileindex, &fileindex_path, worktree);
8684 if (err)
8685 goto done;
8687 /* Preconditions are the same as for rebase. */
8688 ok_arg.worktree = worktree;
8689 ok_arg.repo = repo;
8690 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8691 &ok_arg);
8692 if (err)
8693 goto done;
8695 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8696 0);
8697 if (err)
8698 goto done;
8700 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8701 if (err)
8702 goto done;
8704 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8705 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8706 goto done;
8709 done:
8710 free(fileindex_path);
8711 if (wt_branch)
8712 got_ref_close(wt_branch);
8713 free(wt_branch_tip);
8714 if (err) {
8715 if (*fileindex) {
8716 got_fileindex_free(*fileindex);
8717 *fileindex = NULL;
8719 lock_worktree(worktree, LOCK_SH);
8721 return err;
8724 const struct got_error *got_worktree_merge_write_refs(
8725 struct got_worktree *worktree, struct got_reference *branch,
8726 struct got_repository *repo)
8728 const struct got_error *err = NULL;
8729 char *branch_refname = NULL, *commit_refname = NULL;
8730 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8731 struct got_object_id *branch_tip = NULL;
8733 err = get_merge_branch_ref_name(&branch_refname, worktree);
8734 if (err)
8735 return err;
8737 err = get_merge_commit_ref_name(&commit_refname, worktree);
8738 if (err)
8739 return err;
8741 err = got_ref_resolve(&branch_tip, repo, branch);
8742 if (err)
8743 goto done;
8745 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8746 if (err)
8747 goto done;
8748 err = got_ref_write(branch_ref, repo);
8749 if (err)
8750 goto done;
8752 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8753 if (err)
8754 goto done;
8755 err = got_ref_write(commit_ref, repo);
8756 if (err)
8757 goto done;
8759 done:
8760 free(branch_refname);
8761 free(commit_refname);
8762 if (branch_ref)
8763 got_ref_close(branch_ref);
8764 if (commit_ref)
8765 got_ref_close(commit_ref);
8766 free(branch_tip);
8767 return err;
8770 const struct got_error *
8771 got_worktree_merge_continue(char **branch_name,
8772 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8773 struct got_worktree *worktree, struct got_repository *repo)
8775 const struct got_error *err;
8776 char *commit_refname = NULL, *branch_refname = NULL;
8777 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8778 char *fileindex_path = NULL;
8779 int have_staged_files = 0;
8781 *branch_name = NULL;
8782 *branch_tip = NULL;
8783 *fileindex = NULL;
8785 err = lock_worktree(worktree, LOCK_EX);
8786 if (err)
8787 return err;
8789 err = open_fileindex(fileindex, &fileindex_path, worktree);
8790 if (err)
8791 goto done;
8793 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8794 &have_staged_files);
8795 if (err && err->code != GOT_ERR_CANCELLED)
8796 goto done;
8797 if (have_staged_files) {
8798 err = got_error(GOT_ERR_STAGED_PATHS);
8799 goto done;
8802 err = get_merge_branch_ref_name(&branch_refname, worktree);
8803 if (err)
8804 goto done;
8806 err = get_merge_commit_ref_name(&commit_refname, worktree);
8807 if (err)
8808 goto done;
8810 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8811 if (err)
8812 goto done;
8814 if (!got_ref_is_symbolic(branch_ref)) {
8815 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8816 "%s is not a symbolic reference",
8817 got_ref_get_name(branch_ref));
8818 goto done;
8820 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8821 if (*branch_name == NULL) {
8822 err = got_error_from_errno("strdup");
8823 goto done;
8826 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8827 if (err)
8828 goto done;
8830 err = got_ref_resolve(branch_tip, repo, commit_ref);
8831 if (err)
8832 goto done;
8833 done:
8834 free(commit_refname);
8835 free(branch_refname);
8836 free(fileindex_path);
8837 if (commit_ref)
8838 got_ref_close(commit_ref);
8839 if (branch_ref)
8840 got_ref_close(branch_ref);
8841 if (err) {
8842 if (*branch_name) {
8843 free(*branch_name);
8844 *branch_name = NULL;
8846 free(*branch_tip);
8847 *branch_tip = NULL;
8848 if (*fileindex) {
8849 got_fileindex_free(*fileindex);
8850 *fileindex = NULL;
8852 lock_worktree(worktree, LOCK_SH);
8854 return err;
8857 const struct got_error *
8858 got_worktree_merge_abort(struct got_worktree *worktree,
8859 struct got_fileindex *fileindex, struct got_repository *repo,
8860 got_worktree_checkout_cb progress_cb, void *progress_arg)
8862 const struct got_error *err, *unlockerr, *sync_err;
8863 struct got_commit_object *commit = NULL;
8864 char *fileindex_path = NULL;
8865 struct revert_file_args rfa;
8866 char *commit_ref_name = NULL;
8867 struct got_reference *commit_ref = NULL;
8868 struct got_object_id *merged_commit_id = NULL;
8869 struct got_object_id *tree_id = NULL;
8870 struct got_pathlist_head added_paths;
8872 TAILQ_INIT(&added_paths);
8874 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8875 if (err)
8876 goto done;
8878 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8879 if (err)
8880 goto done;
8882 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8883 if (err)
8884 goto done;
8887 * Determine which files in added status can be safely removed
8888 * from disk while reverting changes in the work tree.
8889 * We want to avoid deleting unrelated files which were added by
8890 * the user for conflict resolution purposes.
8892 err = get_paths_added_between_commits(&added_paths,
8893 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8894 got_worktree_get_path_prefix(worktree), repo);
8895 if (err)
8896 goto done;
8899 err = got_object_open_as_commit(&commit, repo,
8900 worktree->base_commit_id);
8901 if (err)
8902 goto done;
8904 err = got_object_id_by_path(&tree_id, repo, commit,
8905 worktree->path_prefix);
8906 if (err)
8907 goto done;
8909 err = delete_merge_refs(worktree, repo);
8910 if (err)
8911 goto done;
8913 err = get_fileindex_path(&fileindex_path, worktree);
8914 if (err)
8915 goto done;
8917 rfa.worktree = worktree;
8918 rfa.fileindex = fileindex;
8919 rfa.progress_cb = progress_cb;
8920 rfa.progress_arg = progress_arg;
8921 rfa.patch_cb = NULL;
8922 rfa.patch_arg = NULL;
8923 rfa.repo = repo;
8924 rfa.unlink_added_files = 1;
8925 rfa.added_files_to_unlink = &added_paths;
8926 err = worktree_status(worktree, "", fileindex, repo,
8927 revert_file, &rfa, NULL, NULL, 1, 0);
8928 if (err)
8929 goto sync;
8931 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8932 repo, progress_cb, progress_arg, NULL, NULL);
8933 sync:
8934 sync_err = sync_fileindex(fileindex, fileindex_path);
8935 if (sync_err && err == NULL)
8936 err = sync_err;
8937 done:
8938 free(tree_id);
8939 free(merged_commit_id);
8940 if (commit)
8941 got_object_commit_close(commit);
8942 if (fileindex)
8943 got_fileindex_free(fileindex);
8944 free(fileindex_path);
8945 if (commit_ref)
8946 got_ref_close(commit_ref);
8947 free(commit_ref_name);
8949 unlockerr = lock_worktree(worktree, LOCK_SH);
8950 if (unlockerr && err == NULL)
8951 err = unlockerr;
8952 return err;
8955 struct check_stage_ok_arg {
8956 struct got_object_id *head_commit_id;
8957 struct got_worktree *worktree;
8958 struct got_fileindex *fileindex;
8959 struct got_repository *repo;
8960 int have_changes;
8963 static const struct got_error *
8964 check_stage_ok(void *arg, unsigned char status,
8965 unsigned char staged_status, const char *relpath,
8966 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8967 struct got_object_id *commit_id, int dirfd, const char *de_name)
8969 struct check_stage_ok_arg *a = arg;
8970 const struct got_error *err = NULL;
8971 struct got_fileindex_entry *ie;
8972 struct got_object_id base_commit_id;
8973 struct got_object_id *base_commit_idp = NULL;
8974 char *in_repo_path = NULL, *p;
8976 if (status == GOT_STATUS_UNVERSIONED ||
8977 status == GOT_STATUS_NO_CHANGE)
8978 return NULL;
8979 if (status == GOT_STATUS_NONEXISTENT)
8980 return got_error_set_errno(ENOENT, relpath);
8982 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8983 if (ie == NULL)
8984 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8986 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8987 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8988 relpath) == -1)
8989 return got_error_from_errno("asprintf");
8991 if (got_fileindex_entry_has_commit(ie)) {
8992 base_commit_idp = got_fileindex_entry_get_commit_id(
8993 &base_commit_id, ie);
8996 if (status == GOT_STATUS_CONFLICT) {
8997 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8998 goto done;
8999 } else if (status != GOT_STATUS_ADD &&
9000 status != GOT_STATUS_MODIFY &&
9001 status != GOT_STATUS_DELETE) {
9002 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9003 goto done;
9006 a->have_changes = 1;
9008 p = in_repo_path;
9009 while (p[0] == '/')
9010 p++;
9011 err = check_out_of_date(p, status, staged_status,
9012 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9013 GOT_ERR_STAGE_OUT_OF_DATE);
9014 done:
9015 free(in_repo_path);
9016 return err;
9019 struct stage_path_arg {
9020 struct got_worktree *worktree;
9021 struct got_fileindex *fileindex;
9022 struct got_repository *repo;
9023 got_worktree_status_cb status_cb;
9024 void *status_arg;
9025 got_worktree_patch_cb patch_cb;
9026 void *patch_arg;
9027 int staged_something;
9028 int allow_bad_symlinks;
9031 static const struct got_error *
9032 stage_path(void *arg, unsigned char status,
9033 unsigned char staged_status, const char *relpath,
9034 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9035 struct got_object_id *commit_id, int dirfd, const char *de_name)
9037 struct stage_path_arg *a = arg;
9038 const struct got_error *err = NULL;
9039 struct got_fileindex_entry *ie;
9040 char *ondisk_path = NULL, *path_content = NULL;
9041 uint32_t stage;
9042 struct got_object_id *new_staged_blob_id = NULL;
9043 struct stat sb;
9045 if (status == GOT_STATUS_UNVERSIONED)
9046 return NULL;
9048 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9049 if (ie == NULL)
9050 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9052 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9053 relpath)== -1)
9054 return got_error_from_errno("asprintf");
9056 switch (status) {
9057 case GOT_STATUS_ADD:
9058 case GOT_STATUS_MODIFY:
9059 /* XXX could sb.st_mode be passed in by our caller? */
9060 if (lstat(ondisk_path, &sb) == -1) {
9061 err = got_error_from_errno2("lstat", ondisk_path);
9062 break;
9064 if (a->patch_cb) {
9065 if (status == GOT_STATUS_ADD) {
9066 int choice = GOT_PATCH_CHOICE_NONE;
9067 err = (*a->patch_cb)(&choice, a->patch_arg,
9068 status, ie->path, NULL, 1, 1);
9069 if (err)
9070 break;
9071 if (choice != GOT_PATCH_CHOICE_YES)
9072 break;
9073 } else {
9074 err = create_patched_content(&path_content, 0,
9075 staged_blob_id ? staged_blob_id : blob_id,
9076 ondisk_path, dirfd, de_name, ie->path,
9077 a->repo, a->patch_cb, a->patch_arg);
9078 if (err || path_content == NULL)
9079 break;
9082 err = got_object_blob_create(&new_staged_blob_id,
9083 path_content ? path_content : ondisk_path, a->repo);
9084 if (err)
9085 break;
9086 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9087 SHA1_DIGEST_LENGTH);
9088 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9089 stage = GOT_FILEIDX_STAGE_ADD;
9090 else
9091 stage = GOT_FILEIDX_STAGE_MODIFY;
9092 got_fileindex_entry_stage_set(ie, stage);
9093 if (S_ISLNK(sb.st_mode)) {
9094 int is_bad_symlink = 0;
9095 if (!a->allow_bad_symlinks) {
9096 char target_path[PATH_MAX];
9097 ssize_t target_len;
9098 target_len = readlink(ondisk_path, target_path,
9099 sizeof(target_path));
9100 if (target_len == -1) {
9101 err = got_error_from_errno2("readlink",
9102 ondisk_path);
9103 break;
9105 err = is_bad_symlink_target(&is_bad_symlink,
9106 target_path, target_len, ondisk_path,
9107 a->worktree->root_path,
9108 a->worktree->meta_dir);
9109 if (err)
9110 break;
9111 if (is_bad_symlink) {
9112 err = got_error_path(ondisk_path,
9113 GOT_ERR_BAD_SYMLINK);
9114 break;
9117 if (is_bad_symlink)
9118 got_fileindex_entry_staged_filetype_set(ie,
9119 GOT_FILEIDX_MODE_BAD_SYMLINK);
9120 else
9121 got_fileindex_entry_staged_filetype_set(ie,
9122 GOT_FILEIDX_MODE_SYMLINK);
9123 } else {
9124 got_fileindex_entry_staged_filetype_set(ie,
9125 GOT_FILEIDX_MODE_REGULAR_FILE);
9127 a->staged_something = 1;
9128 if (a->status_cb == NULL)
9129 break;
9130 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9131 get_staged_status(ie), relpath, blob_id,
9132 new_staged_blob_id, NULL, dirfd, de_name);
9133 if (err)
9134 break;
9136 * When staging the reverse of the staged diff,
9137 * implicitly unstage the file.
9139 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9140 sizeof(ie->blob_sha1)) == 0) {
9141 got_fileindex_entry_stage_set(ie,
9142 GOT_FILEIDX_STAGE_NONE);
9144 break;
9145 case GOT_STATUS_DELETE:
9146 if (staged_status == GOT_STATUS_DELETE)
9147 break;
9148 if (a->patch_cb) {
9149 int choice = GOT_PATCH_CHOICE_NONE;
9150 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9151 ie->path, NULL, 1, 1);
9152 if (err)
9153 break;
9154 if (choice == GOT_PATCH_CHOICE_NO)
9155 break;
9156 if (choice != GOT_PATCH_CHOICE_YES) {
9157 err = got_error(GOT_ERR_PATCH_CHOICE);
9158 break;
9161 stage = GOT_FILEIDX_STAGE_DELETE;
9162 got_fileindex_entry_stage_set(ie, stage);
9163 a->staged_something = 1;
9164 if (a->status_cb == NULL)
9165 break;
9166 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9167 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9168 de_name);
9169 break;
9170 case GOT_STATUS_NO_CHANGE:
9171 break;
9172 case GOT_STATUS_CONFLICT:
9173 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9174 break;
9175 case GOT_STATUS_NONEXISTENT:
9176 err = got_error_set_errno(ENOENT, relpath);
9177 break;
9178 default:
9179 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9180 break;
9183 if (path_content && unlink(path_content) == -1 && err == NULL)
9184 err = got_error_from_errno2("unlink", path_content);
9185 free(path_content);
9186 free(ondisk_path);
9187 free(new_staged_blob_id);
9188 return err;
9191 const struct got_error *
9192 got_worktree_stage(struct got_worktree *worktree,
9193 struct got_pathlist_head *paths,
9194 got_worktree_status_cb status_cb, void *status_arg,
9195 got_worktree_patch_cb patch_cb, void *patch_arg,
9196 int allow_bad_symlinks, struct got_repository *repo)
9198 const struct got_error *err = NULL, *sync_err, *unlockerr;
9199 struct got_pathlist_entry *pe;
9200 struct got_fileindex *fileindex = NULL;
9201 char *fileindex_path = NULL;
9202 struct got_reference *head_ref = NULL;
9203 struct got_object_id *head_commit_id = NULL;
9204 struct check_stage_ok_arg oka;
9205 struct stage_path_arg spa;
9207 err = lock_worktree(worktree, LOCK_EX);
9208 if (err)
9209 return err;
9211 err = got_ref_open(&head_ref, repo,
9212 got_worktree_get_head_ref_name(worktree), 0);
9213 if (err)
9214 goto done;
9215 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9216 if (err)
9217 goto done;
9218 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9219 if (err)
9220 goto done;
9222 /* Check pre-conditions before staging anything. */
9223 oka.head_commit_id = head_commit_id;
9224 oka.worktree = worktree;
9225 oka.fileindex = fileindex;
9226 oka.repo = repo;
9227 oka.have_changes = 0;
9228 TAILQ_FOREACH(pe, paths, entry) {
9229 err = worktree_status(worktree, pe->path, fileindex, repo,
9230 check_stage_ok, &oka, NULL, NULL, 1, 0);
9231 if (err)
9232 goto done;
9234 if (!oka.have_changes) {
9235 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9236 goto done;
9239 spa.worktree = worktree;
9240 spa.fileindex = fileindex;
9241 spa.repo = repo;
9242 spa.patch_cb = patch_cb;
9243 spa.patch_arg = patch_arg;
9244 spa.status_cb = status_cb;
9245 spa.status_arg = status_arg;
9246 spa.staged_something = 0;
9247 spa.allow_bad_symlinks = allow_bad_symlinks;
9248 TAILQ_FOREACH(pe, paths, entry) {
9249 err = worktree_status(worktree, pe->path, fileindex, repo,
9250 stage_path, &spa, NULL, NULL, 1, 0);
9251 if (err)
9252 goto done;
9254 if (!spa.staged_something) {
9255 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9256 goto done;
9259 sync_err = sync_fileindex(fileindex, fileindex_path);
9260 if (sync_err && err == NULL)
9261 err = sync_err;
9262 done:
9263 if (head_ref)
9264 got_ref_close(head_ref);
9265 free(head_commit_id);
9266 free(fileindex_path);
9267 if (fileindex)
9268 got_fileindex_free(fileindex);
9269 unlockerr = lock_worktree(worktree, LOCK_SH);
9270 if (unlockerr && err == NULL)
9271 err = unlockerr;
9272 return err;
9275 struct unstage_path_arg {
9276 struct got_worktree *worktree;
9277 struct got_fileindex *fileindex;
9278 struct got_repository *repo;
9279 got_worktree_checkout_cb progress_cb;
9280 void *progress_arg;
9281 got_worktree_patch_cb patch_cb;
9282 void *patch_arg;
9285 static const struct got_error *
9286 create_unstaged_content(char **path_unstaged_content,
9287 char **path_new_staged_content, struct got_object_id *blob_id,
9288 struct got_object_id *staged_blob_id, const char *relpath,
9289 struct got_repository *repo,
9290 got_worktree_patch_cb patch_cb, void *patch_arg)
9292 const struct got_error *err, *free_err;
9293 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9294 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9295 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9296 struct got_diffreg_result *diffreg_result = NULL;
9297 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9298 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9299 int fd1 = -1, fd2 = -1;
9301 *path_unstaged_content = NULL;
9302 *path_new_staged_content = NULL;
9304 err = got_object_id_str(&label1, blob_id);
9305 if (err)
9306 return err;
9308 fd1 = got_opentempfd();
9309 if (fd1 == -1) {
9310 err = got_error_from_errno("got_opentempfd");
9311 goto done;
9313 fd2 = got_opentempfd();
9314 if (fd2 == -1) {
9315 err = got_error_from_errno("got_opentempfd");
9316 goto done;
9319 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9320 if (err)
9321 goto done;
9323 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9324 if (err)
9325 goto done;
9327 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9328 if (err)
9329 goto done;
9331 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9332 fd2);
9333 if (err)
9334 goto done;
9336 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9337 if (err)
9338 goto done;
9340 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9341 if (err)
9342 goto done;
9344 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9345 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9346 if (err)
9347 goto done;
9349 err = got_opentemp_named(path_unstaged_content, &outfile,
9350 "got-unstaged-content", "");
9351 if (err)
9352 goto done;
9353 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9354 "got-new-staged-content", "");
9355 if (err)
9356 goto done;
9358 if (fseek(f1, 0L, SEEK_SET) == -1) {
9359 err = got_ferror(f1, GOT_ERR_IO);
9360 goto done;
9362 if (fseek(f2, 0L, SEEK_SET) == -1) {
9363 err = got_ferror(f2, GOT_ERR_IO);
9364 goto done;
9366 /* Count the number of actual changes in the diff result. */
9367 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9368 struct diff_chunk_context cc = {};
9369 diff_chunk_context_load_change(&cc, &nchunks_used,
9370 diffreg_result->result, n, 0);
9371 nchanges++;
9373 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9374 int choice;
9375 err = apply_or_reject_change(&choice, &nchunks_used,
9376 diffreg_result->result, n, relpath, f1, f2,
9377 &line_cur1, &line_cur2,
9378 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9379 if (err)
9380 goto done;
9381 if (choice == GOT_PATCH_CHOICE_YES)
9382 have_content = 1;
9383 else
9384 have_rejected_content = 1;
9385 if (choice == GOT_PATCH_CHOICE_QUIT)
9386 break;
9388 if (have_content || have_rejected_content)
9389 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9390 outfile, rejectfile);
9391 done:
9392 free(label1);
9393 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9394 err = got_error_from_errno("close");
9395 if (blob)
9396 got_object_blob_close(blob);
9397 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9398 err = got_error_from_errno("close");
9399 if (staged_blob)
9400 got_object_blob_close(staged_blob);
9401 free_err = got_diffreg_result_free(diffreg_result);
9402 if (free_err && err == NULL)
9403 err = free_err;
9404 if (f1 && fclose(f1) == EOF && err == NULL)
9405 err = got_error_from_errno2("fclose", path1);
9406 if (f2 && fclose(f2) == EOF && err == NULL)
9407 err = got_error_from_errno2("fclose", path2);
9408 if (outfile && fclose(outfile) == EOF && err == NULL)
9409 err = got_error_from_errno2("fclose", *path_unstaged_content);
9410 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9411 err = got_error_from_errno2("fclose", *path_new_staged_content);
9412 if (path1 && unlink(path1) == -1 && err == NULL)
9413 err = got_error_from_errno2("unlink", path1);
9414 if (path2 && unlink(path2) == -1 && err == NULL)
9415 err = got_error_from_errno2("unlink", path2);
9416 if (err || !have_content) {
9417 if (*path_unstaged_content &&
9418 unlink(*path_unstaged_content) == -1 && err == NULL)
9419 err = got_error_from_errno2("unlink",
9420 *path_unstaged_content);
9421 free(*path_unstaged_content);
9422 *path_unstaged_content = NULL;
9424 if (err || !have_content || !have_rejected_content) {
9425 if (*path_new_staged_content &&
9426 unlink(*path_new_staged_content) == -1 && err == NULL)
9427 err = got_error_from_errno2("unlink",
9428 *path_new_staged_content);
9429 free(*path_new_staged_content);
9430 *path_new_staged_content = NULL;
9432 free(path1);
9433 free(path2);
9434 return err;
9437 static const struct got_error *
9438 unstage_hunks(struct got_object_id *staged_blob_id,
9439 struct got_blob_object *blob_base,
9440 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9441 const char *ondisk_path, const char *label_orig,
9442 struct got_worktree *worktree, struct got_repository *repo,
9443 got_worktree_patch_cb patch_cb, void *patch_arg,
9444 got_worktree_checkout_cb progress_cb, void *progress_arg)
9446 const struct got_error *err = NULL;
9447 char *path_unstaged_content = NULL;
9448 char *path_new_staged_content = NULL;
9449 char *parent = NULL, *base_path = NULL;
9450 char *blob_base_path = NULL;
9451 struct got_object_id *new_staged_blob_id = NULL;
9452 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9453 struct stat sb;
9455 err = create_unstaged_content(&path_unstaged_content,
9456 &path_new_staged_content, blob_id, staged_blob_id,
9457 ie->path, repo, patch_cb, patch_arg);
9458 if (err)
9459 return err;
9461 if (path_unstaged_content == NULL)
9462 return NULL;
9464 if (path_new_staged_content) {
9465 err = got_object_blob_create(&new_staged_blob_id,
9466 path_new_staged_content, repo);
9467 if (err)
9468 goto done;
9471 f = fopen(path_unstaged_content, "re");
9472 if (f == NULL) {
9473 err = got_error_from_errno2("fopen",
9474 path_unstaged_content);
9475 goto done;
9477 if (fstat(fileno(f), &sb) == -1) {
9478 err = got_error_from_errno2("fstat", path_unstaged_content);
9479 goto done;
9481 if (got_fileindex_entry_staged_filetype_get(ie) ==
9482 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9483 char link_target[PATH_MAX];
9484 size_t r;
9485 r = fread(link_target, 1, sizeof(link_target), f);
9486 if (r == 0 && ferror(f)) {
9487 err = got_error_from_errno("fread");
9488 goto done;
9490 if (r >= sizeof(link_target)) { /* should not happen */
9491 err = got_error(GOT_ERR_NO_SPACE);
9492 goto done;
9494 link_target[r] = '\0';
9495 err = merge_symlink(worktree, blob_base,
9496 ondisk_path, ie->path, label_orig, link_target,
9497 worktree->base_commit_id, repo, progress_cb,
9498 progress_arg);
9499 } else {
9500 int local_changes_subsumed;
9502 err = got_path_dirname(&parent, ondisk_path);
9503 if (err)
9504 return err;
9506 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9507 parent) == -1) {
9508 err = got_error_from_errno("asprintf");
9509 base_path = NULL;
9510 goto done;
9513 err = got_opentemp_named(&blob_base_path, &f_base,
9514 base_path, "");
9515 if (err)
9516 goto done;
9517 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9518 blob_base);
9519 if (err)
9520 goto done;
9523 * In order the run a 3-way merge with a symlink we copy the symlink's
9524 * target path into a temporary file and use that file with diff3.
9526 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9527 err = dump_symlink_target_path_to_file(&f_deriv2,
9528 ondisk_path);
9529 if (err)
9530 goto done;
9531 } else {
9532 int fd;
9533 fd = open(ondisk_path,
9534 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9535 if (fd == -1) {
9536 err = got_error_from_errno2("open", ondisk_path);
9537 goto done;
9539 f_deriv2 = fdopen(fd, "r");
9540 if (f_deriv2 == NULL) {
9541 err = got_error_from_errno2("fdopen", ondisk_path);
9542 close(fd);
9543 goto done;
9547 err = merge_file(&local_changes_subsumed, worktree,
9548 f_base, f, f_deriv2, ondisk_path, ie->path,
9549 got_fileindex_perms_to_st(ie),
9550 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9551 repo, progress_cb, progress_arg);
9553 if (err)
9554 goto done;
9556 if (new_staged_blob_id) {
9557 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9558 SHA1_DIGEST_LENGTH);
9559 } else {
9560 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9561 got_fileindex_entry_staged_filetype_set(ie, 0);
9563 done:
9564 free(new_staged_blob_id);
9565 if (path_unstaged_content &&
9566 unlink(path_unstaged_content) == -1 && err == NULL)
9567 err = got_error_from_errno2("unlink", path_unstaged_content);
9568 if (path_new_staged_content &&
9569 unlink(path_new_staged_content) == -1 && err == NULL)
9570 err = got_error_from_errno2("unlink", path_new_staged_content);
9571 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9572 err = got_error_from_errno2("unlink", blob_base_path);
9573 if (f_base && fclose(f_base) == EOF && err == NULL)
9574 err = got_error_from_errno2("fclose", path_unstaged_content);
9575 if (f && fclose(f) == EOF && err == NULL)
9576 err = got_error_from_errno2("fclose", path_unstaged_content);
9577 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9578 err = got_error_from_errno2("fclose", ondisk_path);
9579 free(path_unstaged_content);
9580 free(path_new_staged_content);
9581 free(blob_base_path);
9582 free(parent);
9583 free(base_path);
9584 return err;
9587 static const struct got_error *
9588 unstage_path(void *arg, unsigned char status,
9589 unsigned char staged_status, const char *relpath,
9590 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9591 struct got_object_id *commit_id, int dirfd, const char *de_name)
9593 const struct got_error *err = NULL;
9594 struct unstage_path_arg *a = arg;
9595 struct got_fileindex_entry *ie;
9596 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9597 char *ondisk_path = NULL;
9598 char *id_str = NULL, *label_orig = NULL;
9599 int local_changes_subsumed;
9600 struct stat sb;
9601 int fd1 = -1, fd2 = -1;
9603 if (staged_status != GOT_STATUS_ADD &&
9604 staged_status != GOT_STATUS_MODIFY &&
9605 staged_status != GOT_STATUS_DELETE)
9606 return NULL;
9608 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9609 if (ie == NULL)
9610 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9612 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9613 == -1)
9614 return got_error_from_errno("asprintf");
9616 err = got_object_id_str(&id_str,
9617 commit_id ? commit_id : a->worktree->base_commit_id);
9618 if (err)
9619 goto done;
9620 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9621 id_str) == -1) {
9622 err = got_error_from_errno("asprintf");
9623 goto done;
9626 fd1 = got_opentempfd();
9627 if (fd1 == -1) {
9628 err = got_error_from_errno("got_opentempfd");
9629 goto done;
9631 fd2 = got_opentempfd();
9632 if (fd2 == -1) {
9633 err = got_error_from_errno("got_opentempfd");
9634 goto done;
9637 switch (staged_status) {
9638 case GOT_STATUS_MODIFY:
9639 err = got_object_open_as_blob(&blob_base, a->repo,
9640 blob_id, 8192, fd1);
9641 if (err)
9642 break;
9643 /* fall through */
9644 case GOT_STATUS_ADD:
9645 if (a->patch_cb) {
9646 if (staged_status == GOT_STATUS_ADD) {
9647 int choice = GOT_PATCH_CHOICE_NONE;
9648 err = (*a->patch_cb)(&choice, a->patch_arg,
9649 staged_status, ie->path, NULL, 1, 1);
9650 if (err)
9651 break;
9652 if (choice != GOT_PATCH_CHOICE_YES)
9653 break;
9654 } else {
9655 err = unstage_hunks(staged_blob_id,
9656 blob_base, blob_id, ie, ondisk_path,
9657 label_orig, a->worktree, a->repo,
9658 a->patch_cb, a->patch_arg,
9659 a->progress_cb, a->progress_arg);
9660 break; /* Done with this file. */
9663 err = got_object_open_as_blob(&blob_staged, a->repo,
9664 staged_blob_id, 8192, fd2);
9665 if (err)
9666 break;
9667 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9668 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9669 case GOT_FILEIDX_MODE_REGULAR_FILE:
9670 err = merge_blob(&local_changes_subsumed, a->worktree,
9671 blob_base, ondisk_path, relpath,
9672 got_fileindex_perms_to_st(ie), label_orig,
9673 blob_staged, commit_id ? commit_id :
9674 a->worktree->base_commit_id, a->repo,
9675 a->progress_cb, a->progress_arg);
9676 break;
9677 case GOT_FILEIDX_MODE_SYMLINK:
9678 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9679 char *staged_target;
9680 err = got_object_blob_read_to_str(
9681 &staged_target, blob_staged);
9682 if (err)
9683 goto done;
9684 err = merge_symlink(a->worktree, blob_base,
9685 ondisk_path, relpath, label_orig,
9686 staged_target, commit_id ? commit_id :
9687 a->worktree->base_commit_id,
9688 a->repo, a->progress_cb, a->progress_arg);
9689 free(staged_target);
9690 } else {
9691 err = merge_blob(&local_changes_subsumed,
9692 a->worktree, blob_base, ondisk_path,
9693 relpath, got_fileindex_perms_to_st(ie),
9694 label_orig, blob_staged,
9695 commit_id ? commit_id :
9696 a->worktree->base_commit_id, a->repo,
9697 a->progress_cb, a->progress_arg);
9699 break;
9700 default:
9701 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9702 break;
9704 if (err == NULL) {
9705 got_fileindex_entry_stage_set(ie,
9706 GOT_FILEIDX_STAGE_NONE);
9707 got_fileindex_entry_staged_filetype_set(ie, 0);
9709 break;
9710 case GOT_STATUS_DELETE:
9711 if (a->patch_cb) {
9712 int choice = GOT_PATCH_CHOICE_NONE;
9713 err = (*a->patch_cb)(&choice, a->patch_arg,
9714 staged_status, ie->path, NULL, 1, 1);
9715 if (err)
9716 break;
9717 if (choice == GOT_PATCH_CHOICE_NO)
9718 break;
9719 if (choice != GOT_PATCH_CHOICE_YES) {
9720 err = got_error(GOT_ERR_PATCH_CHOICE);
9721 break;
9724 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9725 got_fileindex_entry_staged_filetype_set(ie, 0);
9726 err = get_file_status(&status, &sb, ie, ondisk_path,
9727 dirfd, de_name, a->repo);
9728 if (err)
9729 break;
9730 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9731 break;
9733 done:
9734 free(ondisk_path);
9735 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9736 err = got_error_from_errno("close");
9737 if (blob_base)
9738 got_object_blob_close(blob_base);
9739 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9740 err = got_error_from_errno("close");
9741 if (blob_staged)
9742 got_object_blob_close(blob_staged);
9743 free(id_str);
9744 free(label_orig);
9745 return err;
9748 const struct got_error *
9749 got_worktree_unstage(struct got_worktree *worktree,
9750 struct got_pathlist_head *paths,
9751 got_worktree_checkout_cb progress_cb, void *progress_arg,
9752 got_worktree_patch_cb patch_cb, void *patch_arg,
9753 struct got_repository *repo)
9755 const struct got_error *err = NULL, *sync_err, *unlockerr;
9756 struct got_pathlist_entry *pe;
9757 struct got_fileindex *fileindex = NULL;
9758 char *fileindex_path = NULL;
9759 struct unstage_path_arg upa;
9761 err = lock_worktree(worktree, LOCK_EX);
9762 if (err)
9763 return err;
9765 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9766 if (err)
9767 goto done;
9769 upa.worktree = worktree;
9770 upa.fileindex = fileindex;
9771 upa.repo = repo;
9772 upa.progress_cb = progress_cb;
9773 upa.progress_arg = progress_arg;
9774 upa.patch_cb = patch_cb;
9775 upa.patch_arg = patch_arg;
9776 TAILQ_FOREACH(pe, paths, entry) {
9777 err = worktree_status(worktree, pe->path, fileindex, repo,
9778 unstage_path, &upa, NULL, NULL, 1, 0);
9779 if (err)
9780 goto done;
9783 sync_err = sync_fileindex(fileindex, fileindex_path);
9784 if (sync_err && err == NULL)
9785 err = sync_err;
9786 done:
9787 free(fileindex_path);
9788 if (fileindex)
9789 got_fileindex_free(fileindex);
9790 unlockerr = lock_worktree(worktree, LOCK_SH);
9791 if (unlockerr && err == NULL)
9792 err = unlockerr;
9793 return err;
9796 struct report_file_info_arg {
9797 struct got_worktree *worktree;
9798 got_worktree_path_info_cb info_cb;
9799 void *info_arg;
9800 struct got_pathlist_head *paths;
9801 got_cancel_cb cancel_cb;
9802 void *cancel_arg;
9805 static const struct got_error *
9806 report_file_info(void *arg, struct got_fileindex_entry *ie)
9808 const struct got_error *err;
9809 struct report_file_info_arg *a = arg;
9810 struct got_pathlist_entry *pe;
9811 struct got_object_id blob_id, staged_blob_id, commit_id;
9812 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9813 struct got_object_id *commit_idp = NULL;
9814 int stage;
9816 if (a->cancel_cb) {
9817 err = a->cancel_cb(a->cancel_arg);
9818 if (err)
9819 return err;
9822 TAILQ_FOREACH(pe, a->paths, entry) {
9823 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9824 got_path_is_child(ie->path, pe->path, pe->path_len))
9825 break;
9827 if (pe == NULL) /* not found */
9828 return NULL;
9830 if (got_fileindex_entry_has_blob(ie))
9831 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9832 stage = got_fileindex_entry_stage_get(ie);
9833 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9834 stage == GOT_FILEIDX_STAGE_ADD) {
9835 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9836 &staged_blob_id, ie);
9839 if (got_fileindex_entry_has_commit(ie))
9840 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9842 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9843 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9846 const struct got_error *
9847 got_worktree_path_info(struct got_worktree *worktree,
9848 struct got_pathlist_head *paths,
9849 got_worktree_path_info_cb info_cb, void *info_arg,
9850 got_cancel_cb cancel_cb, void *cancel_arg)
9853 const struct got_error *err = NULL, *unlockerr;
9854 struct got_fileindex *fileindex = NULL;
9855 char *fileindex_path = NULL;
9856 struct report_file_info_arg arg;
9858 err = lock_worktree(worktree, LOCK_SH);
9859 if (err)
9860 return err;
9862 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9863 if (err)
9864 goto done;
9866 arg.worktree = worktree;
9867 arg.info_cb = info_cb;
9868 arg.info_arg = info_arg;
9869 arg.paths = paths;
9870 arg.cancel_cb = cancel_cb;
9871 arg.cancel_arg = cancel_arg;
9872 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9873 &arg);
9874 done:
9875 free(fileindex_path);
9876 if (fileindex)
9877 got_fileindex_free(fileindex);
9878 unlockerr = lock_worktree(worktree, LOCK_UN);
9879 if (unlockerr && err == NULL)
9880 err = unlockerr;
9881 return err;
9884 static const struct got_error *
9885 patch_check_path(const char *p, char **path, unsigned char *status,
9886 unsigned char *staged_status, struct got_fileindex *fileindex,
9887 struct got_worktree *worktree, struct got_repository *repo)
9889 const struct got_error *err;
9890 struct got_fileindex_entry *ie;
9891 struct stat sb;
9892 char *ondisk_path = NULL;
9894 err = got_worktree_resolve_path(path, worktree, p);
9895 if (err)
9896 return err;
9898 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9899 *path[0] ? "/" : "", *path) == -1)
9900 return got_error_from_errno("asprintf");
9902 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9903 if (ie) {
9904 *staged_status = get_staged_status(ie);
9905 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9906 repo);
9907 if (err)
9908 goto done;
9909 } else {
9910 *staged_status = GOT_STATUS_NO_CHANGE;
9911 *status = GOT_STATUS_UNVERSIONED;
9912 if (lstat(ondisk_path, &sb) == -1) {
9913 if (errno != ENOENT) {
9914 err = got_error_from_errno2("lstat",
9915 ondisk_path);
9916 goto done;
9918 *status = GOT_STATUS_NONEXISTENT;
9922 done:
9923 free(ondisk_path);
9924 return err;
9927 static const struct got_error *
9928 patch_can_rm(const char *path, unsigned char status,
9929 unsigned char staged_status)
9931 if (status == GOT_STATUS_NONEXISTENT)
9932 return got_error_set_errno(ENOENT, path);
9933 if (status != GOT_STATUS_NO_CHANGE &&
9934 status != GOT_STATUS_ADD &&
9935 status != GOT_STATUS_MODIFY &&
9936 status != GOT_STATUS_MODE_CHANGE)
9937 return got_error_path(path, GOT_ERR_FILE_STATUS);
9938 if (staged_status == GOT_STATUS_DELETE)
9939 return got_error_path(path, GOT_ERR_FILE_STATUS);
9940 return NULL;
9943 static const struct got_error *
9944 patch_can_add(const char *path, unsigned char status)
9946 if (status != GOT_STATUS_NONEXISTENT)
9947 return got_error_path(path, GOT_ERR_FILE_STATUS);
9948 return NULL;
9951 static const struct got_error *
9952 patch_can_edit(const char *path, unsigned char status,
9953 unsigned char staged_status)
9955 if (status == GOT_STATUS_NONEXISTENT)
9956 return got_error_set_errno(ENOENT, path);
9957 if (status != GOT_STATUS_NO_CHANGE &&
9958 status != GOT_STATUS_ADD &&
9959 status != GOT_STATUS_MODIFY)
9960 return got_error_path(path, GOT_ERR_FILE_STATUS);
9961 if (staged_status == GOT_STATUS_DELETE)
9962 return got_error_path(path, GOT_ERR_FILE_STATUS);
9963 return NULL;
9966 const struct got_error *
9967 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9968 char **fileindex_path, struct got_worktree *worktree)
9970 return open_fileindex(fileindex, fileindex_path, worktree);
9973 const struct got_error *
9974 got_worktree_patch_check_path(const char *old, const char *new,
9975 char **oldpath, char **newpath, struct got_worktree *worktree,
9976 struct got_repository *repo, struct got_fileindex *fileindex)
9978 const struct got_error *err = NULL;
9979 int file_renamed = 0;
9980 unsigned char status_old, staged_status_old;
9981 unsigned char status_new, staged_status_new;
9983 *oldpath = NULL;
9984 *newpath = NULL;
9986 err = patch_check_path(old != NULL ? old : new, oldpath,
9987 &status_old, &staged_status_old, fileindex, worktree, repo);
9988 if (err)
9989 goto done;
9991 err = patch_check_path(new != NULL ? new : old, newpath,
9992 &status_new, &staged_status_new, fileindex, worktree, repo);
9993 if (err)
9994 goto done;
9996 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9997 file_renamed = 1;
9999 if (old != NULL && new == NULL)
10000 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10001 else if (file_renamed) {
10002 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10003 if (err == NULL)
10004 err = patch_can_add(*newpath, status_new);
10005 } else if (old == NULL)
10006 err = patch_can_add(*newpath, status_new);
10007 else
10008 err = patch_can_edit(*newpath, status_new, staged_status_new);
10010 done:
10011 if (err) {
10012 free(*oldpath);
10013 *oldpath = NULL;
10014 free(*newpath);
10015 *newpath = NULL;
10017 return err;
10020 const struct got_error *
10021 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10022 struct got_worktree *worktree, struct got_fileindex *fileindex,
10023 got_worktree_checkout_cb progress_cb, void *progress_arg)
10025 struct schedule_addition_args saa;
10027 memset(&saa, 0, sizeof(saa));
10028 saa.worktree = worktree;
10029 saa.fileindex = fileindex;
10030 saa.progress_cb = progress_cb;
10031 saa.progress_arg = progress_arg;
10032 saa.repo = repo;
10034 return worktree_status(worktree, path, fileindex, repo,
10035 schedule_addition, &saa, NULL, NULL, 1, 0);
10038 const struct got_error *
10039 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10040 struct got_worktree *worktree, struct got_fileindex *fileindex,
10041 got_worktree_delete_cb progress_cb, void *progress_arg)
10043 const struct got_error *err;
10044 struct schedule_deletion_args sda;
10045 char *ondisk_status_path;
10047 memset(&sda, 0, sizeof(sda));
10048 sda.worktree = worktree;
10049 sda.fileindex = fileindex;
10050 sda.progress_cb = progress_cb;
10051 sda.progress_arg = progress_arg;
10052 sda.repo = repo;
10053 sda.delete_local_mods = 0;
10054 sda.keep_on_disk = 0;
10055 sda.ignore_missing_paths = 0;
10056 sda.status_codes = NULL;
10057 if (asprintf(&ondisk_status_path, "%s/%s",
10058 got_worktree_get_root_path(worktree), path) == -1)
10059 return got_error_from_errno("asprintf");
10060 sda.status_path = ondisk_status_path;
10061 sda.status_path_len = strlen(ondisk_status_path);
10063 err = worktree_status(worktree, path, fileindex, repo,
10064 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10065 free(ondisk_status_path);
10066 return err;
10069 const struct got_error *
10070 got_worktree_patch_complete(struct got_fileindex *fileindex,
10071 const char *fileindex_path)
10073 const struct got_error *err = NULL;
10075 err = sync_fileindex(fileindex, fileindex_path);
10076 got_fileindex_free(fileindex);
10078 return err;