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 <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
71 {
72 const struct got_error *err = NULL;
73 char *path;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
79 free(path);
80 return err;
81 }
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
85 {
86 const struct got_error *err = NULL;
87 FILE *tmpfile = NULL;
88 char *tmppath = NULL;
89 char *path = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
93 path = NULL;
94 goto done;
95 }
97 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
98 if (err)
99 goto done;
101 if (content) {
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
105 goto done;
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
111 unlink(tmppath);
112 goto done;
115 done:
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
118 free(tmppath);
119 return err;
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
126 char *refstr = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
130 if (refstr == NULL)
131 return got_error_from_errno("got_ref_to_str");
132 } else {
133 refstr = strdup(got_ref_get_name(head_ref));
134 if (refstr == NULL)
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 free(refstr);
139 return err;
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
148 uuid_t uuid;
149 uint32_t uuid_status;
150 int obj_type;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
159 goto done;
162 err = got_ref_resolve(&commit_id, repo, head_ref);
163 if (err)
164 return err;
165 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (err)
167 return err;
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
179 goto done;
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
185 goto done;
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
189 goto done;
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
194 if (err)
195 goto done;
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
199 if (err)
200 goto done;
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
204 if (err)
205 goto done;
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
209 if (err)
210 goto done;
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 if (err)
213 goto done;
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
218 if (err)
219 goto done;
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
224 if (err)
225 goto done;
227 /* Generate UUID. */
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
231 goto done;
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
236 goto done;
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
239 if (err)
240 goto done;
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 if (err)
249 goto done;
251 done:
252 free(commit_id);
253 free(path_got);
254 free(formatstr);
255 free(absprefix);
256 free(basestr);
257 free(uuidstr);
258 return err;
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
273 free(absprefix);
274 return NULL;
277 const char *
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
293 path_got = NULL;
294 goto done;
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
300 goto done;
303 err = write_head_ref(path_got, head_ref);
304 if (err)
305 goto done;
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
309 done:
310 free(path_got);
311 if (err)
312 free(head_ref_name);
313 return err;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
328 char *id_str = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
334 path_got = NULL;
335 goto done;
338 err = got_object_open(&obj, repo, commit_id);
339 if (err)
340 return err;
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
344 goto done;
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
349 if (err)
350 goto done;
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
352 if (err)
353 goto done;
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
359 goto done;
361 done:
362 if (obj)
363 got_object_close(obj);
364 free(id_str);
365 free(path_got);
366 return err;
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
382 return NULL;
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
389 char *abspath;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 struct stat sb;
397 err = NULL;
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
405 free(abspath);
406 return err;
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
413 uint8_t fbuf1[8192];
414 uint8_t fbuf2[8192];
415 size_t flen1 = 0, flen2 = 0;
417 *same = 1;
419 for (;;) {
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
423 break;
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
428 break;
430 if (flen1 == 0) {
431 if (flen2 != 0)
432 *same = 0;
433 break;
434 } else if (flen2 == 0) {
435 if (flen1 != 0)
436 *same = 0;
437 break;
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
440 *same = 0;
441 break;
443 } else {
444 *same = 0;
445 break;
449 return err;
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
455 struct stat sb;
456 size_t size1, size2;
458 *same = 1;
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
462 size1 = sb.st_size;
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
466 size2 = sb.st_size;
468 if (size1 != size2) {
469 *same = 0;
470 return NULL;
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
484 uint8_t fbuf[65536];
485 size_t flen;
486 ssize_t outlen;
488 *outsize = 0;
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
493 for (;;) {
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 if (flen == 0) {
496 if (ferror(f))
497 return got_error_from_errno("fread");
498 if (feof(f))
499 break;
501 outlen = write(outfd, fbuf, flen);
502 if (outlen == -1)
503 return got_error_from_errno("write");
504 if (outlen != flen)
505 return got_error(GOT_ERR_IO);
506 *outsize += outlen;
509 return NULL;
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
526 *overlapcnt = 0;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 if (err)
530 return err;
532 if (same_content)
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
536 if (err)
537 return err;
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 if (err)
541 return err;
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
545 *overlapcnt = 1;
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
552 goto done;
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
556 goto done;
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 base_path_orig, "");
560 if (err)
561 goto done;
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv, "");
564 if (err)
565 goto done;
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2, "");
568 if (err)
569 goto done;
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 if (err)
572 goto done;
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 if (err)
575 goto done;
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (err)
578 goto done;
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
582 goto done;
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
588 path_deriv) < 0) {
589 err = got_error_from_errno("dprintf");
590 goto done;
592 if (size_orig > 0) {
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
597 path_orig) < 0) {
598 err = got_error_from_errno("dprintf");
599 goto done;
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
604 path_deriv2,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
609 goto done;
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 done:
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 err == NULL)
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
625 free(path_orig);
626 free(path_deriv);
627 free(path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
631 return err;
634 /*
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
640 */
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
650 int merged_fd = -1;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
653 int overlapcnt = 0;
654 char *parent = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
659 if (err)
660 return err;
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
664 goto done;
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
668 if (err)
669 goto done;
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 if (err) {
674 if (err->code != GOT_ERR_FILE_BINARY)
675 goto done;
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 ondisk_path);
679 if (err)
680 goto done;
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
685 if (err)
686 goto done;
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
690 goto done;
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
696 goto done;
698 merged_fd = -1;
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
703 f_merged);
704 if (err)
705 goto done;
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
710 goto done;
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
715 ondisk_path);
716 goto done;
718 done:
719 if (err) {
720 if (merged_path)
721 unlink(merged_path);
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
727 free(merged_path);
728 free(base_path);
729 free(parent);
730 return err;
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
735 size_t target_len)
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
742 ondisk_path);
743 return NULL;
746 /*
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
757 */
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 FILE *f = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
768 if (err)
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
774 goto done;
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
778 if (err)
779 goto done;
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
783 goto done;
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
796 goto done;
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
801 goto done;
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
805 goto done;
807 done:
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
810 free(path);
811 free(id_str);
812 free(label_deriv);
813 return err;
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
823 /*
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
828 */
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
838 struct stat sb;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
851 ondisk_path);
852 goto done;
854 ondisk_target[ondisk_len] = '\0';
856 if (blob_orig) {
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 if (err)
859 goto done;
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 path);
883 } else {
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
888 if (err)
889 goto done;
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 path);
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
897 if (err)
898 goto done;
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 path);
906 } else {
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 path);
917 done:
918 free(ancestor_target);
919 return err;
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
927 ssize_t target_len;
928 size_t n;
929 FILE *f;
931 *outfile = NULL;
933 f = got_opentemp();
934 if (f == NULL)
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
939 goto done;
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
944 goto done;
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
948 goto done;
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
952 goto done;
954 done:
955 if (err)
956 fclose(f);
957 else
958 *outfile = f;
959 return err;
962 /*
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
966 */
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
984 if (err)
985 return err;
987 if (blob_orig) {
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 parent) == -1) {
990 err = got_error_from_errno("asprintf");
991 base_path = NULL;
992 goto done;
995 err = got_opentemp_named(&blob_orig_path, &f_orig,
996 base_path, "");
997 if (err)
998 goto done;
999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 blob_orig);
1001 if (err)
1002 goto done;
1003 free(base_path);
1004 } else {
1006 * No common ancestor exists. This is an "add vs add" conflict
1007 * and we simply use an empty ancestor file to make both files
1008 * appear in the merged result in their entirety.
1010 f_orig = got_opentemp();
1011 if (f_orig == NULL) {
1012 err = got_error_from_errno("got_opentemp");
1013 goto done;
1017 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 err = got_error_from_errno("asprintf");
1019 base_path = NULL;
1020 goto done;
1023 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 if (err)
1025 goto done;
1026 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1027 blob_deriv);
1028 if (err)
1029 goto done;
1031 err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 if (err)
1033 goto done;
1034 if (asprintf(&label_deriv, "%s: commit %s",
1035 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 err = got_error_from_errno("asprintf");
1037 goto done;
1041 * In order the run a 3-way merge with a symlink we copy the symlink's
1042 * target path into a temporary file and use that file with diff3.
1044 if (S_ISLNK(st_mode)) {
1045 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1046 if (err)
1047 goto done;
1048 } else {
1049 int fd;
1050 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 if (fd == -1) {
1052 err = got_error_from_errno2("open", ondisk_path);
1053 goto done;
1055 f_deriv2 = fdopen(fd, "r");
1056 if (f_deriv2 == NULL) {
1057 err = got_error_from_errno2("fdopen", ondisk_path);
1058 close(fd);
1059 goto done;
1063 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 done:
1067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 err = got_error_from_errno("fclose");
1073 free(base_path);
1074 if (blob_orig_path) {
1075 unlink(blob_orig_path);
1076 free(blob_orig_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1082 free(id_str);
1083 free(label_deriv);
1084 free(parent);
1085 return err;
1088 static const struct got_error *
1089 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 int wt_fd, const char *path, struct got_object_id *blob_id)
1093 const struct got_error *err = NULL;
1094 struct got_fileindex_entry *new_ie;
1096 *new_iep = NULL;
1098 err = got_fileindex_entry_alloc(&new_ie, path);
1099 if (err)
1100 return err;
1102 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 blob_id->sha1, base_commit_id->sha1, 1);
1104 if (err)
1105 goto done;
1107 err = got_fileindex_entry_add(fileindex, new_ie);
1108 done:
1109 if (err)
1110 got_fileindex_entry_free(new_ie);
1111 else
1112 *new_iep = new_ie;
1113 return err;
1116 static mode_t
1117 get_ondisk_perms(int executable, mode_t st_mode)
1119 mode_t xbits = S_IXUSR;
1121 if (executable) {
1122 /* Map read bits to execute bits. */
1123 if (st_mode & S_IRGRP)
1124 xbits |= S_IXGRP;
1125 if (st_mode & S_IROTH)
1126 xbits |= S_IXOTH;
1127 return st_mode | xbits;
1130 return st_mode;
1133 static mode_t
1134 apply_umask(mode_t mode)
1136 mode_t um;
1138 um = umask(000);
1139 umask(um);
1140 return mode & ~um;
1143 /* forward declaration */
1144 static const struct got_error *
1145 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 const char *path, mode_t te_mode, mode_t st_mode,
1147 struct got_blob_object *blob, int restoring_missing_file,
1148 int reverting_versioned_file, int installing_bad_symlink,
1149 int path_is_unversioned, struct got_repository *repo,
1150 got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 * This function assumes that the provided symlink target points at a
1154 * safe location in the work tree!
1156 static const struct got_error *
1157 replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 const char *target_path, size_t target_len)
1160 const struct got_error *err = NULL;
1161 ssize_t elen;
1162 char etarget[PATH_MAX];
1163 int fd;
1165 *did_something = 0;
1168 * "Bad" symlinks (those pointing outside the work tree or into the
1169 * .got directory) are installed in the work tree as a regular file
1170 * which contains the bad symlink target path.
1171 * The new symlink target has already been checked for safety by our
1172 * caller. If we can successfully open a regular file then we simply
1173 * replace this file with a symlink below.
1175 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 if (fd == -1) {
1177 if (!got_err_open_nofollow_on_symlink())
1178 return got_error_from_errno2("open", ondisk_path);
1180 /* We are updating an existing on-disk symlink. */
1181 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 if (elen == -1)
1183 return got_error_from_errno2("readlink", ondisk_path);
1185 if (elen == target_len &&
1186 memcmp(etarget, target_path, target_len) == 0)
1187 return NULL; /* nothing to do */
1190 *did_something = 1;
1191 err = update_symlink(ondisk_path, target_path, target_len);
1192 if (fd != -1 && close(fd) == -1 && err == NULL)
1193 err = got_error_from_errno2("close", ondisk_path);
1194 return err;
1197 static const struct got_error *
1198 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 const struct got_error *err = NULL;
1202 char canonpath[PATH_MAX];
1203 char *path_got = NULL;
1205 *is_bad_symlink = 0;
1207 if (target_len >= sizeof(canonpath)) {
1208 *is_bad_symlink = 1;
1209 return NULL;
1213 * We do not use realpath(3) to resolve the symlink's target
1214 * path because we don't want to resolve symlinks recursively.
1215 * Instead we make the path absolute and then canonicalize it.
1216 * Relative symlink target lookup should begin at the directory
1217 * in which the blob object is being installed.
1219 if (!got_path_is_absolute(target_path)) {
1220 char *abspath, *parent;
1221 err = got_path_dirname(&parent, ondisk_path);
1222 if (err)
1223 return err;
1224 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 free(parent);
1226 return got_error_from_errno("asprintf");
1228 free(parent);
1229 if (strlen(abspath) >= sizeof(canonpath)) {
1230 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 free(abspath);
1232 return err;
1234 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 free(abspath);
1236 if (err)
1237 return err;
1238 } else {
1239 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 if (err)
1241 return err;
1244 /* Only allow symlinks pointing at paths within the work tree. */
1245 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 *is_bad_symlink = 1;
1247 return NULL;
1250 /* Do not allow symlinks pointing into the .got directory. */
1251 if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 GOT_WORKTREE_GOT_DIR) == -1)
1253 return got_error_from_errno("asprintf");
1254 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 *is_bad_symlink = 1;
1257 free(path_got);
1258 return NULL;
1261 static const struct got_error *
1262 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 int restoring_missing_file, int reverting_versioned_file,
1265 int path_is_unversioned, int allow_bad_symlinks,
1266 struct got_repository *repo,
1267 got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 const struct got_error *err = NULL;
1270 char target_path[PATH_MAX];
1271 size_t len, target_len = 0;
1272 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 *is_bad_symlink = 0;
1278 * Blob object content specifies the target path of the link.
1279 * If a symbolic link cannot be installed we instead create
1280 * a regular file which contains the link target path stored
1281 * in the blob object.
1283 do {
1284 err = got_object_blob_read_block(&len, blob);
1285 if (err)
1286 return err;
1288 if (len + target_len >= sizeof(target_path)) {
1289 /* Path too long; install as a regular file. */
1290 *is_bad_symlink = 1;
1291 got_object_blob_rewind(blob);
1292 return install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 restoring_missing_file, reverting_versioned_file,
1295 1, path_is_unversioned, repo, progress_cb,
1296 progress_arg);
1298 if (len > 0) {
1299 /* Skip blob object header first time around. */
1300 memcpy(target_path + target_len, buf + hdrlen,
1301 len - hdrlen);
1302 target_len += len - hdrlen;
1303 hdrlen = 0;
1305 } while (len != 0);
1306 target_path[target_len] = '\0';
1308 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 ondisk_path, worktree->root_path);
1310 if (err)
1311 return err;
1313 if (*is_bad_symlink && !allow_bad_symlinks) {
1314 /* install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 path_is_unversioned, repo, progress_cb, progress_arg);
1320 return err;
1323 if (symlink(target_path, ondisk_path) == -1) {
1324 if (errno == EEXIST) {
1325 int symlink_replaced;
1326 if (path_is_unversioned) {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_UNVERSIONED, path);
1329 return err;
1331 err = replace_existing_symlink(&symlink_replaced,
1332 ondisk_path, target_path, target_len);
1333 if (err)
1334 return err;
1335 if (progress_cb) {
1336 if (symlink_replaced) {
1337 err = (*progress_cb)(progress_arg,
1338 reverting_versioned_file ?
1339 GOT_STATUS_REVERT :
1340 GOT_STATUS_UPDATE, path);
1341 } else {
1342 err = (*progress_cb)(progress_arg,
1343 GOT_STATUS_EXISTS, path);
1346 return err; /* Nothing else to do. */
1349 if (errno == ENOENT) {
1350 char *parent;
1351 err = got_path_dirname(&parent, ondisk_path);
1352 if (err)
1353 return err;
1354 err = add_dir_on_disk(worktree, parent);
1355 free(parent);
1356 if (err)
1357 return err;
1359 * Retry, and fall through to error handling
1360 * below if this second attempt fails.
1362 if (symlink(target_path, ondisk_path) != -1) {
1363 err = NULL; /* success */
1364 return err;
1368 /* Handle errors from first or second creation attempt. */
1369 if (errno == ENAMETOOLONG) {
1370 /* bad target path; install as a regular file */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 err = install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file, 1,
1376 path_is_unversioned, repo,
1377 progress_cb, progress_arg);
1378 } else if (errno == ENOTDIR) {
1379 err = got_error_path(ondisk_path,
1380 GOT_ERR_FILE_OBSTRUCTED);
1381 } else {
1382 err = got_error_from_errno3("symlink",
1383 target_path, ondisk_path);
1385 } else if (progress_cb)
1386 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 return err;
1391 static const struct got_error *
1392 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 const char *path, mode_t te_mode, mode_t st_mode,
1394 struct got_blob_object *blob, int restoring_missing_file,
1395 int reverting_versioned_file, int installing_bad_symlink,
1396 int path_is_unversioned, struct got_repository *repo,
1397 got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 const struct got_error *err = NULL;
1400 int fd = -1;
1401 size_t len, hdrlen;
1402 int update = 0;
1403 char *tmppath = NULL;
1404 mode_t mode;
1406 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 O_CLOEXEC, mode);
1409 if (fd == -1) {
1410 if (errno == ENOENT) {
1411 char *parent;
1412 err = got_path_dirname(&parent, path);
1413 if (err)
1414 return err;
1415 err = add_dir_on_disk(worktree, parent);
1416 free(parent);
1417 if (err)
1418 return err;
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 mode);
1422 if (fd == -1)
1423 return got_error_from_errno2("open",
1424 ondisk_path);
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1429 goto done;
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1436 goto done;
1437 } else {
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1439 ondisk_path, "");
1440 if (err)
1441 goto done;
1442 update = 1;
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1446 tmppath);
1447 goto done;
1450 } else
1451 return got_error_from_errno2("open", ondisk_path);
1454 if (progress_cb) {
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 path);
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 path);
1461 else
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 if (err)
1465 goto done;
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1469 do {
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1472 if (err)
1473 break;
1474 if (len > 0) {
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 if (outlen == -1) {
1478 err = got_error_from_errno("write");
1479 goto done;
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1482 goto done;
1484 hdrlen = 0;
1486 } while (len != 0);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1490 goto done;
1493 if (update) {
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1496 goto done;
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1500 ondisk_path);
1501 goto done;
1503 free(tmppath);
1504 tmppath = NULL;
1507 done:
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1512 free(tmppath);
1513 return err;
1516 static const struct got_error *skip_one_line(FILE *);
1519 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1520 * conflict marker is found in newly added lines only.
1522 static const struct got_error *
1523 get_modified_file_content_status(unsigned char *status,
1524 struct got_blob_object *blob, const char *path, struct stat *sb,
1525 FILE *ondisk_file)
1527 const struct got_error *err, *free_err;
1528 const char *markers[3] = {
1529 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1530 GOT_DIFF_CONFLICT_MARKER_SEP,
1531 GOT_DIFF_CONFLICT_MARKER_END
1533 FILE *f1 = NULL;
1534 struct got_diffreg_result *diffreg_result = NULL;
1535 struct diff_result *r;
1536 int nchunks_parsed, n, i = 0, ln = 0;
1537 char *line = NULL;
1538 size_t linesize = 0;
1539 ssize_t linelen;
1541 if (*status != GOT_STATUS_MODIFY)
1542 return NULL;
1544 f1 = got_opentemp();
1545 if (f1 == NULL)
1546 return got_error_from_errno("got_opentemp");
1548 if (blob) {
1549 got_object_blob_rewind(blob);
1550 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1551 if (err)
1552 goto done;
1555 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1556 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1557 if (err)
1558 goto done;
1560 r = diffreg_result->result;
1562 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1563 struct diff_chunk *c;
1564 struct diff_chunk_context cc = {};
1565 off_t pos;
1566 int clc, crc;
1569 * We can optimise a little by advancing straight
1570 * to the next chunk if this one has no added lines.
1572 c = diff_chunk_get(r, n);
1573 clc = diff_chunk_get_left_count(c);
1574 crc = diff_chunk_get_right_count(c);
1576 if (!crc || crc == clc) {
1577 nchunks_parsed = 1;
1578 continue; /* removed or unchanged lines */
1581 pos = diff_chunk_get_right_start_pos(c);
1582 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1583 err = got_ferror(ondisk_file, GOT_ERR_IO);
1584 goto done;
1587 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1588 ln = cc.right.start;
1590 while (ln < cc.right.end) {
1591 linelen = getline(&line, &linesize, ondisk_file);
1592 if (linelen == -1) {
1593 if (feof(ondisk_file))
1594 break;
1595 err = got_ferror(ondisk_file, GOT_ERR_IO);
1596 break;
1599 if (line && strncmp(line, markers[i],
1600 strlen(markers[i])) == 0) {
1601 if (strcmp(markers[i],
1602 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1603 *status = GOT_STATUS_CONFLICT;
1604 goto done;
1605 } else
1606 i++;
1608 ++ln;
1612 done:
1613 free(line);
1614 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1615 err = got_error_from_errno("fclose");
1616 free_err = got_diffreg_result_free(diffreg_result);
1617 if (err == NULL)
1618 err = free_err;
1620 return err;
1623 static int
1624 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1626 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1627 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1630 static int
1631 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1633 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1634 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1635 ie->mtime_sec == sb->st_mtim.tv_sec &&
1636 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1637 ie->size == (sb->st_size & 0xffffffff) &&
1638 !xbit_differs(ie, sb->st_mode));
1641 static unsigned char
1642 get_staged_status(struct got_fileindex_entry *ie)
1644 switch (got_fileindex_entry_stage_get(ie)) {
1645 case GOT_FILEIDX_STAGE_ADD:
1646 return GOT_STATUS_ADD;
1647 case GOT_FILEIDX_STAGE_DELETE:
1648 return GOT_STATUS_DELETE;
1649 case GOT_FILEIDX_STAGE_MODIFY:
1650 return GOT_STATUS_MODIFY;
1651 default:
1652 return GOT_STATUS_NO_CHANGE;
1656 static const struct got_error *
1657 get_symlink_modification_status(unsigned char *status,
1658 struct got_fileindex_entry *ie, const char *abspath,
1659 int dirfd, const char *de_name, struct got_blob_object *blob)
1661 const struct got_error *err = NULL;
1662 char target_path[PATH_MAX];
1663 char etarget[PATH_MAX];
1664 ssize_t elen;
1665 size_t len, target_len = 0;
1666 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1667 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1669 *status = GOT_STATUS_NO_CHANGE;
1671 /* Blob object content specifies the target path of the link. */
1672 do {
1673 err = got_object_blob_read_block(&len, blob);
1674 if (err)
1675 return err;
1676 if (len + target_len >= sizeof(target_path)) {
1678 * Should not happen. The blob contents were OK
1679 * when this symlink was installed.
1681 return got_error(GOT_ERR_NO_SPACE);
1683 if (len > 0) {
1684 /* Skip blob object header first time around. */
1685 memcpy(target_path + target_len, buf + hdrlen,
1686 len - hdrlen);
1687 target_len += len - hdrlen;
1688 hdrlen = 0;
1690 } while (len != 0);
1691 target_path[target_len] = '\0';
1693 if (dirfd != -1) {
1694 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1695 if (elen == -1)
1696 return got_error_from_errno2("readlinkat", abspath);
1697 } else {
1698 elen = readlink(abspath, etarget, sizeof(etarget));
1699 if (elen == -1)
1700 return got_error_from_errno2("readlink", abspath);
1703 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1704 *status = GOT_STATUS_MODIFY;
1706 return NULL;
1709 static const struct got_error *
1710 get_file_status(unsigned char *status, struct stat *sb,
1711 struct got_fileindex_entry *ie, const char *abspath,
1712 int dirfd, const char *de_name, struct got_repository *repo)
1714 const struct got_error *err = NULL;
1715 struct got_object_id id;
1716 size_t hdrlen;
1717 int fd = -1, fd1 = -1;
1718 FILE *f = NULL;
1719 uint8_t fbuf[8192];
1720 struct got_blob_object *blob = NULL;
1721 size_t flen, blen;
1722 unsigned char staged_status;
1724 staged_status = get_staged_status(ie);
1725 *status = GOT_STATUS_NO_CHANGE;
1726 memset(sb, 0, sizeof(*sb));
1729 * Whenever the caller provides a directory descriptor and a
1730 * directory entry name for the file, use them! This prevents
1731 * race conditions if filesystem paths change beneath our feet.
1733 if (dirfd != -1) {
1734 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1735 if (errno == ENOENT) {
1736 if (got_fileindex_entry_has_file_on_disk(ie))
1737 *status = GOT_STATUS_MISSING;
1738 else
1739 *status = GOT_STATUS_DELETE;
1740 goto done;
1742 err = got_error_from_errno2("fstatat", abspath);
1743 goto done;
1745 } else {
1746 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1747 if (fd == -1 && errno != ENOENT &&
1748 !got_err_open_nofollow_on_symlink())
1749 return got_error_from_errno2("open", abspath);
1750 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1751 if (lstat(abspath, sb) == -1)
1752 return got_error_from_errno2("lstat", abspath);
1753 } else if (fd == -1 || fstat(fd, sb) == -1) {
1754 if (errno == ENOENT) {
1755 if (got_fileindex_entry_has_file_on_disk(ie))
1756 *status = GOT_STATUS_MISSING;
1757 else
1758 *status = GOT_STATUS_DELETE;
1759 goto done;
1761 err = got_error_from_errno2("fstat", abspath);
1762 goto done;
1766 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1767 *status = GOT_STATUS_OBSTRUCTED;
1768 goto done;
1771 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1772 *status = GOT_STATUS_DELETE;
1773 goto done;
1774 } else if (!got_fileindex_entry_has_blob(ie) &&
1775 staged_status != GOT_STATUS_ADD) {
1776 *status = GOT_STATUS_ADD;
1777 goto done;
1780 if (!stat_info_differs(ie, sb))
1781 goto done;
1783 if (S_ISLNK(sb->st_mode) &&
1784 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1785 *status = GOT_STATUS_MODIFY;
1786 goto done;
1789 if (staged_status == GOT_STATUS_MODIFY ||
1790 staged_status == GOT_STATUS_ADD)
1791 got_fileindex_entry_get_staged_blob_id(&id, ie);
1792 else
1793 got_fileindex_entry_get_blob_id(&id, ie);
1795 fd1 = got_opentempfd();
1796 if (fd1 == -1) {
1797 err = got_error_from_errno("got_opentempfd");
1798 goto done;
1800 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1801 if (err)
1802 goto done;
1804 if (S_ISLNK(sb->st_mode)) {
1805 err = get_symlink_modification_status(status, ie,
1806 abspath, dirfd, de_name, blob);
1807 goto done;
1810 if (dirfd != -1) {
1811 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1812 if (fd == -1) {
1813 err = got_error_from_errno2("openat", abspath);
1814 goto done;
1818 f = fdopen(fd, "r");
1819 if (f == NULL) {
1820 err = got_error_from_errno2("fdopen", abspath);
1821 goto done;
1823 fd = -1;
1824 hdrlen = got_object_blob_get_hdrlen(blob);
1825 for (;;) {
1826 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1827 err = got_object_blob_read_block(&blen, blob);
1828 if (err)
1829 goto done;
1830 /* Skip length of blob object header first time around. */
1831 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1832 if (flen == 0 && ferror(f)) {
1833 err = got_error_from_errno("fread");
1834 goto done;
1836 if (blen - hdrlen == 0) {
1837 if (flen != 0)
1838 *status = GOT_STATUS_MODIFY;
1839 break;
1840 } else if (flen == 0) {
1841 if (blen - hdrlen != 0)
1842 *status = GOT_STATUS_MODIFY;
1843 break;
1844 } else if (blen - hdrlen == flen) {
1845 /* Skip blob object header first time around. */
1846 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1847 *status = GOT_STATUS_MODIFY;
1848 break;
1850 } else {
1851 *status = GOT_STATUS_MODIFY;
1852 break;
1854 hdrlen = 0;
1857 if (*status == GOT_STATUS_MODIFY) {
1858 rewind(f);
1859 err = get_modified_file_content_status(status, blob, ie->path,
1860 sb, f);
1861 } else if (xbit_differs(ie, sb->st_mode))
1862 *status = GOT_STATUS_MODE_CHANGE;
1863 done:
1864 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1865 err = got_error_from_errno("close");
1866 if (blob)
1867 got_object_blob_close(blob);
1868 if (f != NULL && fclose(f) == EOF && err == NULL)
1869 err = got_error_from_errno2("fclose", abspath);
1870 if (fd != -1 && close(fd) == -1 && err == NULL)
1871 err = got_error_from_errno2("close", abspath);
1872 return err;
1876 * Update timestamps in the file index if a file is unmodified and
1877 * we had to run a full content comparison to find out.
1879 static const struct got_error *
1880 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1881 struct got_fileindex_entry *ie, struct stat *sb)
1883 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1884 return got_fileindex_entry_update(ie, wt_fd, path,
1885 ie->blob_sha1, ie->commit_sha1, 1);
1887 return NULL;
1890 static const struct got_error *
1891 update_blob(struct got_worktree *worktree,
1892 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1893 struct got_tree_entry *te, const char *path,
1894 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1895 void *progress_arg)
1897 const struct got_error *err = NULL;
1898 struct got_blob_object *blob = NULL;
1899 char *ondisk_path = NULL;
1900 unsigned char status = GOT_STATUS_NO_CHANGE;
1901 struct stat sb;
1902 int fd1 = -1, fd2 = -1;
1904 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1905 return got_error_from_errno("asprintf");
1907 if (ie) {
1908 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1909 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1910 goto done;
1912 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1913 repo);
1914 if (err)
1915 goto done;
1916 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1917 sb.st_mode = got_fileindex_perms_to_st(ie);
1918 } else {
1919 if (stat(ondisk_path, &sb) == -1) {
1920 if (errno != ENOENT) {
1921 err = got_error_from_errno2("stat",
1922 ondisk_path);
1923 goto done;
1925 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1926 status = GOT_STATUS_UNVERSIONED;
1927 } else {
1928 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1929 status = GOT_STATUS_UNVERSIONED;
1930 else
1931 status = GOT_STATUS_OBSTRUCTED;
1935 if (status == GOT_STATUS_OBSTRUCTED) {
1936 if (ie)
1937 got_fileindex_entry_mark_skipped(ie);
1938 err = (*progress_cb)(progress_arg, status, path);
1939 goto done;
1941 if (status == GOT_STATUS_CONFLICT) {
1942 if (ie)
1943 got_fileindex_entry_mark_skipped(ie);
1944 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1945 path);
1946 goto done;
1949 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1950 (S_ISLNK(te->mode) ||
1951 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1953 * This is a regular file or an installed bad symlink.
1954 * If the file index indicates that this file is already
1955 * up-to-date with respect to the repository we can skip
1956 * updating contents of this file.
1958 if (got_fileindex_entry_has_commit(ie) &&
1959 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1960 SHA1_DIGEST_LENGTH) == 0) {
1961 /* Same commit. */
1962 err = sync_timestamps(worktree->root_fd,
1963 path, status, ie, &sb);
1964 if (err)
1965 goto done;
1966 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1967 path);
1968 goto done;
1970 if (got_fileindex_entry_has_blob(ie) &&
1971 memcmp(ie->blob_sha1, te->id.sha1,
1972 SHA1_DIGEST_LENGTH) == 0) {
1973 /* Different commit but the same blob. */
1974 err = sync_timestamps(worktree->root_fd,
1975 path, status, ie, &sb);
1976 if (err)
1977 goto done;
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1979 path);
1980 goto done;
1984 fd1 = got_opentempfd();
1985 if (fd1 == -1) {
1986 err = got_error_from_errno("got_opentempfd");
1987 goto done;
1989 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1990 if (err)
1991 goto done;
1993 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1994 int update_timestamps;
1995 struct got_blob_object *blob2 = NULL;
1996 char *label_orig = NULL;
1997 if (got_fileindex_entry_has_blob(ie)) {
1998 fd2 = got_opentempfd();
1999 if (fd2 == -1) {
2000 err = got_error_from_errno("got_opentempfd");
2001 goto done;
2003 struct got_object_id id2;
2004 got_fileindex_entry_get_blob_id(&id2, ie);
2005 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2006 fd2);
2007 if (err)
2008 goto done;
2010 if (got_fileindex_entry_has_commit(ie)) {
2011 char id_str[SHA1_DIGEST_STRING_LENGTH];
2012 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2013 sizeof(id_str)) == NULL) {
2014 err = got_error_path(id_str,
2015 GOT_ERR_BAD_OBJ_ID_STR);
2016 goto done;
2018 if (asprintf(&label_orig, "%s: commit %s",
2019 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2020 err = got_error_from_errno("asprintf");
2021 goto done;
2024 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2025 char *link_target;
2026 err = got_object_blob_read_to_str(&link_target, blob);
2027 if (err)
2028 goto done;
2029 err = merge_symlink(worktree, blob2, ondisk_path, path,
2030 label_orig, link_target, worktree->base_commit_id,
2031 repo, progress_cb, progress_arg);
2032 free(link_target);
2033 } else {
2034 err = merge_blob(&update_timestamps, worktree, blob2,
2035 ondisk_path, path, sb.st_mode, label_orig, blob,
2036 worktree->base_commit_id, repo,
2037 progress_cb, progress_arg);
2039 free(label_orig);
2040 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2041 err = got_error_from_errno("close");
2042 goto done;
2044 if (blob2)
2045 got_object_blob_close(blob2);
2046 if (err)
2047 goto done;
2049 * Do not update timestamps of files with local changes.
2050 * Otherwise, a future status walk would treat them as
2051 * unmodified files again.
2053 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2054 blob->id.sha1, worktree->base_commit_id->sha1,
2055 update_timestamps);
2056 } else if (status == GOT_STATUS_MODE_CHANGE) {
2057 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2058 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2059 } else if (status == GOT_STATUS_DELETE) {
2060 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2061 if (err)
2062 goto done;
2063 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2064 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2065 if (err)
2066 goto done;
2067 } else {
2068 int is_bad_symlink = 0;
2069 if (S_ISLNK(te->mode)) {
2070 err = install_symlink(&is_bad_symlink, worktree,
2071 ondisk_path, path, blob,
2072 status == GOT_STATUS_MISSING, 0,
2073 status == GOT_STATUS_UNVERSIONED, 0,
2074 repo, progress_cb, progress_arg);
2075 } else {
2076 err = install_blob(worktree, ondisk_path, path,
2077 te->mode, sb.st_mode, blob,
2078 status == GOT_STATUS_MISSING, 0, 0,
2079 status == GOT_STATUS_UNVERSIONED, repo,
2080 progress_cb, progress_arg);
2082 if (err)
2083 goto done;
2085 if (ie) {
2086 err = got_fileindex_entry_update(ie,
2087 worktree->root_fd, path, blob->id.sha1,
2088 worktree->base_commit_id->sha1, 1);
2089 } else {
2090 err = create_fileindex_entry(&ie, fileindex,
2091 worktree->base_commit_id, worktree->root_fd, path,
2092 &blob->id);
2094 if (err)
2095 goto done;
2097 if (is_bad_symlink) {
2098 got_fileindex_entry_filetype_set(ie,
2099 GOT_FILEIDX_MODE_BAD_SYMLINK);
2103 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2104 err = got_error_from_errno("close");
2105 goto done;
2107 got_object_blob_close(blob);
2108 done:
2109 free(ondisk_path);
2110 return err;
2113 static const struct got_error *
2114 remove_ondisk_file(const char *root_path, const char *path)
2116 const struct got_error *err = NULL;
2117 char *ondisk_path = NULL, *parent = NULL;
2119 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2120 return got_error_from_errno("asprintf");
2122 if (unlink(ondisk_path) == -1) {
2123 if (errno != ENOENT)
2124 err = got_error_from_errno2("unlink", ondisk_path);
2125 } else {
2126 size_t root_len = strlen(root_path);
2127 err = got_path_dirname(&parent, ondisk_path);
2128 if (err)
2129 goto done;
2130 while (got_path_cmp(parent, root_path,
2131 strlen(parent), root_len) != 0) {
2132 free(ondisk_path);
2133 ondisk_path = parent;
2134 parent = NULL;
2135 if (rmdir(ondisk_path) == -1) {
2136 if (errno != ENOTEMPTY)
2137 err = got_error_from_errno2("rmdir",
2138 ondisk_path);
2139 break;
2141 err = got_path_dirname(&parent, ondisk_path);
2142 if (err)
2143 break;
2146 done:
2147 free(ondisk_path);
2148 free(parent);
2149 return err;
2152 static const struct got_error *
2153 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2154 struct got_fileindex_entry *ie, struct got_repository *repo,
2155 got_worktree_checkout_cb progress_cb, void *progress_arg)
2157 const struct got_error *err = NULL;
2158 unsigned char status;
2159 struct stat sb;
2160 char *ondisk_path;
2162 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2163 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2165 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2166 == -1)
2167 return got_error_from_errno("asprintf");
2169 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2170 if (err)
2171 goto done;
2173 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2174 char ondisk_target[PATH_MAX];
2175 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2176 sizeof(ondisk_target));
2177 if (ondisk_len == -1) {
2178 err = got_error_from_errno2("readlink", ondisk_path);
2179 goto done;
2181 ondisk_target[ondisk_len] = '\0';
2182 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2183 NULL, NULL, /* XXX pass common ancestor info? */
2184 ondisk_target, ondisk_path);
2185 if (err)
2186 goto done;
2187 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2188 ie->path);
2189 goto done;
2192 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2193 status == GOT_STATUS_ADD) {
2194 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2195 if (err)
2196 goto done;
2198 * Preserve the working file and change the deleted blob's
2199 * entry into a schedule-add entry.
2201 err = got_fileindex_entry_update(ie, worktree->root_fd,
2202 ie->path, NULL, NULL, 0);
2203 } else {
2204 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2205 if (err)
2206 goto done;
2207 if (status == GOT_STATUS_NO_CHANGE) {
2208 err = remove_ondisk_file(worktree->root_path, ie->path);
2209 if (err)
2210 goto done;
2212 got_fileindex_entry_remove(fileindex, ie);
2214 done:
2215 free(ondisk_path);
2216 return err;
2219 struct diff_cb_arg {
2220 struct got_fileindex *fileindex;
2221 struct got_worktree *worktree;
2222 struct got_repository *repo;
2223 got_worktree_checkout_cb progress_cb;
2224 void *progress_arg;
2225 got_cancel_cb cancel_cb;
2226 void *cancel_arg;
2229 static const struct got_error *
2230 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2231 struct got_tree_entry *te, const char *parent_path)
2233 struct diff_cb_arg *a = arg;
2235 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2236 return got_error(GOT_ERR_CANCELLED);
2238 return update_blob(a->worktree, a->fileindex, ie, te,
2239 ie->path, a->repo, a->progress_cb, a->progress_arg);
2242 static const struct got_error *
2243 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2245 struct diff_cb_arg *a = arg;
2247 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2248 return got_error(GOT_ERR_CANCELLED);
2250 return delete_blob(a->worktree, a->fileindex, ie,
2251 a->repo, a->progress_cb, a->progress_arg);
2254 static const struct got_error *
2255 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2257 struct diff_cb_arg *a = arg;
2258 const struct got_error *err;
2259 char *path;
2261 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2262 return got_error(GOT_ERR_CANCELLED);
2264 if (got_object_tree_entry_is_submodule(te))
2265 return NULL;
2267 if (asprintf(&path, "%s%s%s", parent_path,
2268 parent_path[0] ? "/" : "", te->name)
2269 == -1)
2270 return got_error_from_errno("asprintf");
2272 if (S_ISDIR(te->mode))
2273 err = add_dir_on_disk(a->worktree, path);
2274 else
2275 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2276 a->repo, a->progress_cb, a->progress_arg);
2278 free(path);
2279 return err;
2282 const struct got_error *
2283 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2285 uint32_t uuid_status;
2287 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2288 if (uuid_status != uuid_s_ok) {
2289 *uuidstr = NULL;
2290 return got_error_uuid(uuid_status, "uuid_to_string");
2293 return NULL;
2296 static const struct got_error *
2297 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2299 const struct got_error *err = NULL;
2300 char *uuidstr = NULL;
2302 *refname = NULL;
2304 err = got_worktree_get_uuid(&uuidstr, worktree);
2305 if (err)
2306 return err;
2308 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2309 err = got_error_from_errno("asprintf");
2310 *refname = NULL;
2312 free(uuidstr);
2313 return err;
2316 const struct got_error *
2317 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2318 const char *prefix)
2320 return get_ref_name(refname, worktree, prefix);
2323 const struct got_error *
2324 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2329 static const struct got_error *
2330 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree,
2333 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2336 static const struct got_error *
2337 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2342 static const struct got_error *
2343 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2345 return get_ref_name(refname, worktree,
2346 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2349 static const struct got_error *
2350 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2352 return get_ref_name(refname, worktree,
2353 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2356 static const struct got_error *
2357 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2359 return get_ref_name(refname, worktree,
2360 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2363 static const struct got_error *
2364 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree,
2367 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2370 static const struct got_error *
2371 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2373 return get_ref_name(refname, worktree,
2374 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2377 static const struct got_error *
2378 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2380 return get_ref_name(refname, worktree,
2381 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2384 const struct got_error *
2385 got_worktree_get_histedit_script_path(char **path,
2386 struct got_worktree *worktree)
2388 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2389 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2390 *path = NULL;
2391 return got_error_from_errno("asprintf");
2393 return NULL;
2396 static const struct got_error *
2397 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2403 static const struct got_error *
2404 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2411 * Prevent Git's garbage collector from deleting our base commit by
2412 * setting a reference to our base commit's ID.
2414 static const struct got_error *
2415 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2417 const struct got_error *err = NULL;
2418 struct got_reference *ref = NULL;
2419 char *refname;
2421 err = got_worktree_get_base_ref_name(&refname, worktree);
2422 if (err)
2423 return err;
2425 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2426 if (err)
2427 goto done;
2429 err = got_ref_write(ref, repo);
2430 done:
2431 free(refname);
2432 if (ref)
2433 got_ref_close(ref);
2434 return err;
2437 static const struct got_error *
2438 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2440 const struct got_error *err = NULL;
2442 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2443 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2444 err = got_error_from_errno("asprintf");
2445 *fileindex_path = NULL;
2447 return err;
2451 static const struct got_error *
2452 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2453 struct got_worktree *worktree)
2455 const struct got_error *err = NULL;
2456 FILE *index = NULL;
2458 *fileindex_path = NULL;
2459 *fileindex = got_fileindex_alloc();
2460 if (*fileindex == NULL)
2461 return got_error_from_errno("got_fileindex_alloc");
2463 err = get_fileindex_path(fileindex_path, worktree);
2464 if (err)
2465 goto done;
2467 index = fopen(*fileindex_path, "rbe");
2468 if (index == NULL) {
2469 if (errno != ENOENT)
2470 err = got_error_from_errno2("fopen", *fileindex_path);
2471 } else {
2472 err = got_fileindex_read(*fileindex, index);
2473 if (fclose(index) == EOF && err == NULL)
2474 err = got_error_from_errno("fclose");
2476 done:
2477 if (err) {
2478 free(*fileindex_path);
2479 *fileindex_path = NULL;
2480 got_fileindex_free(*fileindex);
2481 *fileindex = NULL;
2483 return err;
2486 struct bump_base_commit_id_arg {
2487 struct got_object_id *base_commit_id;
2488 const char *path;
2489 size_t path_len;
2490 const char *entry_name;
2491 got_worktree_checkout_cb progress_cb;
2492 void *progress_arg;
2495 static const struct got_error *
2496 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2498 const struct got_error *err;
2499 struct bump_base_commit_id_arg *a = arg;
2501 if (a->entry_name) {
2502 if (strcmp(ie->path, a->path) != 0)
2503 return NULL;
2504 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2505 return NULL;
2507 if (got_fileindex_entry_was_skipped(ie))
2508 return NULL;
2510 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2511 SHA1_DIGEST_LENGTH) == 0)
2512 return NULL;
2514 if (a->progress_cb) {
2515 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2516 ie->path);
2517 if (err)
2518 return err;
2520 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2521 return NULL;
2524 /* Bump base commit ID of all files within an updated part of the work tree. */
2525 static const struct got_error *
2526 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2527 struct got_fileindex *fileindex,
2528 got_worktree_checkout_cb progress_cb, void *progress_arg)
2530 struct bump_base_commit_id_arg bbc_arg;
2532 bbc_arg.base_commit_id = worktree->base_commit_id;
2533 bbc_arg.entry_name = NULL;
2534 bbc_arg.path = "";
2535 bbc_arg.path_len = 0;
2536 bbc_arg.progress_cb = progress_cb;
2537 bbc_arg.progress_arg = progress_arg;
2539 return got_fileindex_for_each_entry_safe(fileindex,
2540 bump_base_commit_id, &bbc_arg);
2543 static const struct got_error *
2544 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2546 const struct got_error *err = NULL;
2547 char *new_fileindex_path = NULL;
2548 FILE *new_index = NULL;
2549 struct timespec timeout;
2551 err = got_opentemp_named(&new_fileindex_path, &new_index,
2552 fileindex_path, "");
2553 if (err)
2554 goto done;
2556 err = got_fileindex_write(fileindex, new_index);
2557 if (err)
2558 goto done;
2560 if (rename(new_fileindex_path, fileindex_path) != 0) {
2561 err = got_error_from_errno3("rename", new_fileindex_path,
2562 fileindex_path);
2563 unlink(new_fileindex_path);
2567 * Sleep for a short amount of time to ensure that files modified after
2568 * this program exits have a different time stamp from the one which
2569 * was recorded in the file index.
2571 timeout.tv_sec = 0;
2572 timeout.tv_nsec = 1;
2573 nanosleep(&timeout, NULL);
2574 done:
2575 if (new_index)
2576 fclose(new_index);
2577 free(new_fileindex_path);
2578 return err;
2581 static const struct got_error *
2582 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2583 struct got_object_id **tree_id, const char *wt_relpath,
2584 struct got_commit_object *base_commit, struct got_worktree *worktree,
2585 struct got_repository *repo)
2587 const struct got_error *err = NULL;
2588 struct got_object_id *id = NULL;
2589 char *in_repo_path = NULL;
2590 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2592 *entry_type = GOT_OBJ_TYPE_ANY;
2593 *tree_relpath = NULL;
2594 *tree_id = NULL;
2596 if (wt_relpath[0] == '\0') {
2597 /* Check out all files within the work tree. */
2598 *entry_type = GOT_OBJ_TYPE_TREE;
2599 *tree_relpath = strdup("");
2600 if (*tree_relpath == NULL) {
2601 err = got_error_from_errno("strdup");
2602 goto done;
2604 err = got_object_id_by_path(tree_id, repo, base_commit,
2605 worktree->path_prefix);
2606 if (err)
2607 goto done;
2608 return NULL;
2611 /* Check out a subset of files in the work tree. */
2613 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2614 is_root_wt ? "" : "/", wt_relpath) == -1) {
2615 err = got_error_from_errno("asprintf");
2616 goto done;
2619 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2620 if (err)
2621 goto done;
2623 free(in_repo_path);
2624 in_repo_path = NULL;
2626 err = got_object_get_type(entry_type, repo, id);
2627 if (err)
2628 goto done;
2630 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2631 /* Check out a single file. */
2632 if (strchr(wt_relpath, '/') == NULL) {
2633 /* Check out a single file in work tree's root dir. */
2634 in_repo_path = strdup(worktree->path_prefix);
2635 if (in_repo_path == NULL) {
2636 err = got_error_from_errno("strdup");
2637 goto done;
2639 *tree_relpath = strdup("");
2640 if (*tree_relpath == NULL) {
2641 err = got_error_from_errno("strdup");
2642 goto done;
2644 } else {
2645 /* Check out a single file in a subdirectory. */
2646 err = got_path_dirname(tree_relpath, wt_relpath);
2647 if (err)
2648 return err;
2649 if (asprintf(&in_repo_path, "%s%s%s",
2650 worktree->path_prefix, is_root_wt ? "" : "/",
2651 *tree_relpath) == -1) {
2652 err = got_error_from_errno("asprintf");
2653 goto done;
2656 err = got_object_id_by_path(tree_id, repo,
2657 base_commit, in_repo_path);
2658 } else {
2659 /* Check out all files within a subdirectory. */
2660 *tree_id = got_object_id_dup(id);
2661 if (*tree_id == NULL) {
2662 err = got_error_from_errno("got_object_id_dup");
2663 goto done;
2665 *tree_relpath = strdup(wt_relpath);
2666 if (*tree_relpath == NULL) {
2667 err = got_error_from_errno("strdup");
2668 goto done;
2671 done:
2672 free(id);
2673 free(in_repo_path);
2674 if (err) {
2675 *entry_type = GOT_OBJ_TYPE_ANY;
2676 free(*tree_relpath);
2677 *tree_relpath = NULL;
2678 free(*tree_id);
2679 *tree_id = NULL;
2681 return err;
2684 static const struct got_error *
2685 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2686 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2687 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2688 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2690 const struct got_error *err = NULL;
2691 struct got_commit_object *commit = NULL;
2692 struct got_tree_object *tree = NULL;
2693 struct got_fileindex_diff_tree_cb diff_cb;
2694 struct diff_cb_arg arg;
2696 err = ref_base_commit(worktree, repo);
2697 if (err) {
2698 if (!(err->code == GOT_ERR_ERRNO &&
2699 (errno == EACCES || errno == EROFS)))
2700 goto done;
2701 err = (*progress_cb)(progress_arg,
2702 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2703 if (err)
2704 return err;
2707 err = got_object_open_as_commit(&commit, repo,
2708 worktree->base_commit_id);
2709 if (err)
2710 goto done;
2712 err = got_object_open_as_tree(&tree, repo, tree_id);
2713 if (err)
2714 goto done;
2716 if (entry_name &&
2717 got_object_tree_find_entry(tree, entry_name) == NULL) {
2718 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2719 goto done;
2722 diff_cb.diff_old_new = diff_old_new;
2723 diff_cb.diff_old = diff_old;
2724 diff_cb.diff_new = diff_new;
2725 arg.fileindex = fileindex;
2726 arg.worktree = worktree;
2727 arg.repo = repo;
2728 arg.progress_cb = progress_cb;
2729 arg.progress_arg = progress_arg;
2730 arg.cancel_cb = cancel_cb;
2731 arg.cancel_arg = cancel_arg;
2732 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2733 entry_name, repo, &diff_cb, &arg);
2734 done:
2735 if (tree)
2736 got_object_tree_close(tree);
2737 if (commit)
2738 got_object_commit_close(commit);
2739 return err;
2742 const struct got_error *
2743 got_worktree_checkout_files(struct got_worktree *worktree,
2744 struct got_pathlist_head *paths, struct got_repository *repo,
2745 got_worktree_checkout_cb progress_cb, void *progress_arg,
2746 got_cancel_cb cancel_cb, void *cancel_arg)
2748 const struct got_error *err = NULL, *sync_err, *unlockerr;
2749 struct got_commit_object *commit = NULL;
2750 struct got_tree_object *tree = NULL;
2751 struct got_fileindex *fileindex = NULL;
2752 char *fileindex_path = NULL;
2753 struct got_pathlist_entry *pe;
2754 struct tree_path_data {
2755 STAILQ_ENTRY(tree_path_data) entry;
2756 struct got_object_id *tree_id;
2757 int entry_type;
2758 char *relpath;
2759 char *entry_name;
2760 } *tpd = NULL;
2761 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2763 STAILQ_INIT(&tree_paths);
2765 err = lock_worktree(worktree, LOCK_EX);
2766 if (err)
2767 return err;
2769 err = got_object_open_as_commit(&commit, repo,
2770 worktree->base_commit_id);
2771 if (err)
2772 goto done;
2774 /* Map all specified paths to in-repository trees. */
2775 TAILQ_FOREACH(pe, paths, entry) {
2776 tpd = malloc(sizeof(*tpd));
2777 if (tpd == NULL) {
2778 err = got_error_from_errno("malloc");
2779 goto done;
2782 err = find_tree_entry_for_checkout(&tpd->entry_type,
2783 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2784 worktree, repo);
2785 if (err) {
2786 free(tpd);
2787 goto done;
2790 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2791 err = got_path_basename(&tpd->entry_name, pe->path);
2792 if (err) {
2793 free(tpd->relpath);
2794 free(tpd->tree_id);
2795 free(tpd);
2796 goto done;
2798 } else
2799 tpd->entry_name = NULL;
2801 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2805 * Read the file index.
2806 * Checking out files is supposed to be an idempotent operation.
2807 * If the on-disk file index is incomplete we will try to complete it.
2809 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2810 if (err)
2811 goto done;
2813 tpd = STAILQ_FIRST(&tree_paths);
2814 TAILQ_FOREACH(pe, paths, entry) {
2815 struct bump_base_commit_id_arg bbc_arg;
2817 err = checkout_files(worktree, fileindex, tpd->relpath,
2818 tpd->tree_id, tpd->entry_name, repo,
2819 progress_cb, progress_arg, cancel_cb, cancel_arg);
2820 if (err)
2821 break;
2823 bbc_arg.base_commit_id = worktree->base_commit_id;
2824 bbc_arg.entry_name = tpd->entry_name;
2825 bbc_arg.path = pe->path;
2826 bbc_arg.path_len = pe->path_len;
2827 bbc_arg.progress_cb = progress_cb;
2828 bbc_arg.progress_arg = progress_arg;
2829 err = got_fileindex_for_each_entry_safe(fileindex,
2830 bump_base_commit_id, &bbc_arg);
2831 if (err)
2832 break;
2834 tpd = STAILQ_NEXT(tpd, entry);
2836 sync_err = sync_fileindex(fileindex, fileindex_path);
2837 if (sync_err && err == NULL)
2838 err = sync_err;
2839 done:
2840 free(fileindex_path);
2841 if (tree)
2842 got_object_tree_close(tree);
2843 if (commit)
2844 got_object_commit_close(commit);
2845 if (fileindex)
2846 got_fileindex_free(fileindex);
2847 while (!STAILQ_EMPTY(&tree_paths)) {
2848 tpd = STAILQ_FIRST(&tree_paths);
2849 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2850 free(tpd->relpath);
2851 free(tpd->tree_id);
2852 free(tpd);
2854 unlockerr = lock_worktree(worktree, LOCK_SH);
2855 if (unlockerr && err == NULL)
2856 err = unlockerr;
2857 return err;
2860 struct merge_file_cb_arg {
2861 struct got_worktree *worktree;
2862 struct got_fileindex *fileindex;
2863 got_worktree_checkout_cb progress_cb;
2864 void *progress_arg;
2865 got_cancel_cb cancel_cb;
2866 void *cancel_arg;
2867 const char *label_orig;
2868 struct got_object_id *commit_id2;
2869 int allow_bad_symlinks;
2872 static const struct got_error *
2873 merge_file_cb(void *arg, struct got_blob_object *blob1,
2874 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2875 struct got_object_id *id1, struct got_object_id *id2,
2876 const char *path1, const char *path2,
2877 mode_t mode1, mode_t mode2, struct got_repository *repo)
2879 static const struct got_error *err = NULL;
2880 struct merge_file_cb_arg *a = arg;
2881 struct got_fileindex_entry *ie;
2882 char *ondisk_path = NULL;
2883 struct stat sb;
2884 unsigned char status;
2885 int local_changes_subsumed;
2886 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2887 char *id_str = NULL, *label_deriv2 = NULL;
2889 if (blob1 && blob2) {
2890 ie = got_fileindex_entry_get(a->fileindex, path2,
2891 strlen(path2));
2892 if (ie == NULL)
2893 return (*a->progress_cb)(a->progress_arg,
2894 GOT_STATUS_MISSING, path2);
2896 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2897 path2) == -1)
2898 return got_error_from_errno("asprintf");
2900 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2901 repo);
2902 if (err)
2903 goto done;
2905 if (status == GOT_STATUS_DELETE) {
2906 err = (*a->progress_cb)(a->progress_arg,
2907 GOT_STATUS_MERGE, path2);
2908 goto done;
2910 if (status != GOT_STATUS_NO_CHANGE &&
2911 status != GOT_STATUS_MODIFY &&
2912 status != GOT_STATUS_CONFLICT &&
2913 status != GOT_STATUS_ADD) {
2914 err = (*a->progress_cb)(a->progress_arg, status, path2);
2915 goto done;
2918 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2919 char *link_target2;
2920 err = got_object_blob_read_to_str(&link_target2, blob2);
2921 if (err)
2922 goto done;
2923 err = merge_symlink(a->worktree, blob1, ondisk_path,
2924 path2, a->label_orig, link_target2, a->commit_id2,
2925 repo, a->progress_cb, a->progress_arg);
2926 free(link_target2);
2927 } else {
2928 int fd;
2930 f_orig = got_opentemp();
2931 if (f_orig == NULL) {
2932 err = got_error_from_errno("got_opentemp");
2933 goto done;
2935 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2936 f_orig, blob1);
2937 if (err)
2938 goto done;
2940 f_deriv2 = got_opentemp();
2941 if (f_deriv2 == NULL)
2942 goto done;
2943 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2944 f_deriv2, blob2);
2945 if (err)
2946 goto done;
2948 fd = open(ondisk_path,
2949 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2950 if (fd == -1) {
2951 err = got_error_from_errno2("open",
2952 ondisk_path);
2953 goto done;
2955 f_deriv = fdopen(fd, "r");
2956 if (f_deriv == NULL) {
2957 err = got_error_from_errno2("fdopen",
2958 ondisk_path);
2959 close(fd);
2960 goto done;
2962 err = got_object_id_str(&id_str, a->commit_id2);
2963 if (err)
2964 goto done;
2965 if (asprintf(&label_deriv2, "%s: commit %s",
2966 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2967 err = got_error_from_errno("asprintf");
2968 goto done;
2970 err = merge_file(&local_changes_subsumed, a->worktree,
2971 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2972 mode2, a->label_orig, NULL, label_deriv2,
2973 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2974 a->progress_cb, a->progress_arg);
2976 } else if (blob1) {
2977 ie = got_fileindex_entry_get(a->fileindex, path1,
2978 strlen(path1));
2979 if (ie == NULL)
2980 return (*a->progress_cb)(a->progress_arg,
2981 GOT_STATUS_MISSING, path1);
2983 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2984 path1) == -1)
2985 return got_error_from_errno("asprintf");
2987 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2988 repo);
2989 if (err)
2990 goto done;
2992 switch (status) {
2993 case GOT_STATUS_NO_CHANGE:
2994 err = (*a->progress_cb)(a->progress_arg,
2995 GOT_STATUS_DELETE, path1);
2996 if (err)
2997 goto done;
2998 err = remove_ondisk_file(a->worktree->root_path, path1);
2999 if (err)
3000 goto done;
3001 if (ie)
3002 got_fileindex_entry_mark_deleted_from_disk(ie);
3003 break;
3004 case GOT_STATUS_DELETE:
3005 case GOT_STATUS_MISSING:
3006 err = (*a->progress_cb)(a->progress_arg,
3007 GOT_STATUS_DELETE, path1);
3008 if (err)
3009 goto done;
3010 if (ie)
3011 got_fileindex_entry_mark_deleted_from_disk(ie);
3012 break;
3013 case GOT_STATUS_ADD: {
3014 struct got_object_id *id;
3015 FILE *blob1_f;
3016 off_t blob1_size;
3018 * Delete the added file only if its content already
3019 * exists in the repository.
3021 err = got_object_blob_file_create(&id, &blob1_f,
3022 &blob1_size, path1);
3023 if (err)
3024 goto done;
3025 if (got_object_id_cmp(id, id1) == 0) {
3026 err = (*a->progress_cb)(a->progress_arg,
3027 GOT_STATUS_DELETE, path1);
3028 if (err)
3029 goto done;
3030 err = remove_ondisk_file(a->worktree->root_path,
3031 path1);
3032 if (err)
3033 goto done;
3034 if (ie)
3035 got_fileindex_entry_remove(a->fileindex,
3036 ie);
3037 } else {
3038 err = (*a->progress_cb)(a->progress_arg,
3039 GOT_STATUS_CANNOT_DELETE, path1);
3041 if (fclose(blob1_f) == EOF && err == NULL)
3042 err = got_error_from_errno("fclose");
3043 free(id);
3044 if (err)
3045 goto done;
3046 break;
3048 case GOT_STATUS_MODIFY:
3049 case GOT_STATUS_CONFLICT:
3050 err = (*a->progress_cb)(a->progress_arg,
3051 GOT_STATUS_CANNOT_DELETE, path1);
3052 if (err)
3053 goto done;
3054 break;
3055 case GOT_STATUS_OBSTRUCTED:
3056 err = (*a->progress_cb)(a->progress_arg, status, path1);
3057 if (err)
3058 goto done;
3059 break;
3060 default:
3061 break;
3063 } else if (blob2) {
3064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3065 path2) == -1)
3066 return got_error_from_errno("asprintf");
3067 ie = got_fileindex_entry_get(a->fileindex, path2,
3068 strlen(path2));
3069 if (ie) {
3070 err = get_file_status(&status, &sb, ie, ondisk_path,
3071 -1, NULL, repo);
3072 if (err)
3073 goto done;
3074 if (status != GOT_STATUS_NO_CHANGE &&
3075 status != GOT_STATUS_MODIFY &&
3076 status != GOT_STATUS_CONFLICT &&
3077 status != GOT_STATUS_ADD) {
3078 err = (*a->progress_cb)(a->progress_arg,
3079 status, path2);
3080 goto done;
3082 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3083 char *link_target2;
3084 err = got_object_blob_read_to_str(&link_target2,
3085 blob2);
3086 if (err)
3087 goto done;
3088 err = merge_symlink(a->worktree, NULL,
3089 ondisk_path, path2, a->label_orig,
3090 link_target2, a->commit_id2, repo,
3091 a->progress_cb, a->progress_arg);
3092 free(link_target2);
3093 } else if (S_ISREG(sb.st_mode)) {
3094 err = merge_blob(&local_changes_subsumed,
3095 a->worktree, NULL, ondisk_path, path2,
3096 sb.st_mode, a->label_orig, blob2,
3097 a->commit_id2, repo, a->progress_cb,
3098 a->progress_arg);
3099 } else {
3100 err = got_error_path(ondisk_path,
3101 GOT_ERR_FILE_OBSTRUCTED);
3103 if (err)
3104 goto done;
3105 if (status == GOT_STATUS_DELETE) {
3106 err = got_fileindex_entry_update(ie,
3107 a->worktree->root_fd, path2, blob2->id.sha1,
3108 a->worktree->base_commit_id->sha1, 0);
3109 if (err)
3110 goto done;
3112 } else {
3113 int is_bad_symlink = 0;
3114 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3115 if (S_ISLNK(mode2)) {
3116 err = install_symlink(&is_bad_symlink,
3117 a->worktree, ondisk_path, path2, blob2, 0,
3118 0, 1, a->allow_bad_symlinks, repo,
3119 a->progress_cb, a->progress_arg);
3120 } else {
3121 err = install_blob(a->worktree, ondisk_path, path2,
3122 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3123 a->progress_cb, a->progress_arg);
3125 if (err)
3126 goto done;
3127 err = got_fileindex_entry_alloc(&ie, path2);
3128 if (err)
3129 goto done;
3130 err = got_fileindex_entry_update(ie,
3131 a->worktree->root_fd, path2, NULL, NULL, 1);
3132 if (err) {
3133 got_fileindex_entry_free(ie);
3134 goto done;
3136 err = got_fileindex_entry_add(a->fileindex, ie);
3137 if (err) {
3138 got_fileindex_entry_free(ie);
3139 goto done;
3141 if (is_bad_symlink) {
3142 got_fileindex_entry_filetype_set(ie,
3143 GOT_FILEIDX_MODE_BAD_SYMLINK);
3147 done:
3148 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3149 err = got_error_from_errno("fclose");
3150 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3151 err = got_error_from_errno("fclose");
3152 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3153 err = got_error_from_errno("fclose");
3154 free(id_str);
3155 free(label_deriv2);
3156 free(ondisk_path);
3157 return err;
3160 static const struct got_error *
3161 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3163 struct got_worktree *worktree = arg;
3165 /* Reject merges into a work tree with mixed base commits. */
3166 if (got_fileindex_entry_has_commit(ie) &&
3167 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3168 SHA1_DIGEST_LENGTH) != 0)
3169 return got_error(GOT_ERR_MIXED_COMMITS);
3171 return NULL;
3174 struct check_merge_conflicts_arg {
3175 struct got_worktree *worktree;
3176 struct got_fileindex *fileindex;
3177 struct got_repository *repo;
3180 static const struct got_error *
3181 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3182 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3183 struct got_object_id *id1, struct got_object_id *id2,
3184 const char *path1, const char *path2,
3185 mode_t mode1, mode_t mode2, struct got_repository *repo)
3187 const struct got_error *err = NULL;
3188 struct check_merge_conflicts_arg *a = arg;
3189 unsigned char status;
3190 struct stat sb;
3191 struct got_fileindex_entry *ie;
3192 const char *path = path2 ? path2 : path1;
3193 struct got_object_id *id = id2 ? id2 : id1;
3194 char *ondisk_path;
3196 if (id == NULL)
3197 return NULL;
3199 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3200 if (ie == NULL)
3201 return NULL;
3203 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3204 == -1)
3205 return got_error_from_errno("asprintf");
3207 /* Reject merges into a work tree with conflicted files. */
3208 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3209 free(ondisk_path);
3210 if (err)
3211 return err;
3212 if (status == GOT_STATUS_CONFLICT)
3213 return got_error(GOT_ERR_CONFLICTS);
3215 return NULL;
3218 static const struct got_error *
3219 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3220 const char *fileindex_path, struct got_object_id *commit_id1,
3221 struct got_object_id *commit_id2, struct got_repository *repo,
3222 got_worktree_checkout_cb progress_cb, void *progress_arg,
3223 got_cancel_cb cancel_cb, void *cancel_arg)
3225 const struct got_error *err = NULL, *sync_err;
3226 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3227 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3228 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3229 struct check_merge_conflicts_arg cmc_arg;
3230 struct merge_file_cb_arg arg;
3231 char *label_orig = NULL;
3232 FILE *f1 = NULL, *f2 = NULL;
3233 int fd1 = -1, fd2 = -1;
3235 if (commit_id1) {
3236 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3237 if (err)
3238 goto done;
3239 err = got_object_id_by_path(&tree_id1, repo, commit1,
3240 worktree->path_prefix);
3241 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3242 goto done;
3244 if (tree_id1) {
3245 char *id_str;
3247 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3248 if (err)
3249 goto done;
3251 err = got_object_id_str(&id_str, commit_id1);
3252 if (err)
3253 goto done;
3255 if (asprintf(&label_orig, "%s: commit %s",
3256 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3257 err = got_error_from_errno("asprintf");
3258 free(id_str);
3259 goto done;
3261 free(id_str);
3263 f1 = got_opentemp();
3264 if (f1 == NULL) {
3265 err = got_error_from_errno("got_opentemp");
3266 goto done;
3270 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3271 if (err)
3272 goto done;
3274 err = got_object_id_by_path(&tree_id2, repo, commit2,
3275 worktree->path_prefix);
3276 if (err)
3277 goto done;
3279 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3280 if (err)
3281 goto done;
3283 f2 = got_opentemp();
3284 if (f2 == NULL) {
3285 err = got_error_from_errno("got_opentemp");
3286 goto done;
3289 fd1 = got_opentempfd();
3290 if (fd1 == -1) {
3291 err = got_error_from_errno("got_opentempfd");
3292 goto done;
3295 fd2 = got_opentempfd();
3296 if (fd2 == -1) {
3297 err = got_error_from_errno("got_opentempfd");
3298 goto done;
3301 cmc_arg.worktree = worktree;
3302 cmc_arg.fileindex = fileindex;
3303 cmc_arg.repo = repo;
3304 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3305 check_merge_conflicts, &cmc_arg, 0);
3306 if (err)
3307 goto done;
3309 arg.worktree = worktree;
3310 arg.fileindex = fileindex;
3311 arg.progress_cb = progress_cb;
3312 arg.progress_arg = progress_arg;
3313 arg.cancel_cb = cancel_cb;
3314 arg.cancel_arg = cancel_arg;
3315 arg.label_orig = label_orig;
3316 arg.commit_id2 = commit_id2;
3317 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3318 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3319 merge_file_cb, &arg, 1);
3320 sync_err = sync_fileindex(fileindex, fileindex_path);
3321 if (sync_err && err == NULL)
3322 err = sync_err;
3323 done:
3324 if (commit1)
3325 got_object_commit_close(commit1);
3326 if (commit2)
3327 got_object_commit_close(commit2);
3328 if (tree1)
3329 got_object_tree_close(tree1);
3330 if (tree2)
3331 got_object_tree_close(tree2);
3332 if (f1 && fclose(f1) == EOF && err == NULL)
3333 err = got_error_from_errno("fclose");
3334 if (f2 && fclose(f2) == EOF && err == NULL)
3335 err = got_error_from_errno("fclose");
3336 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3337 err = got_error_from_errno("close");
3338 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3339 err = got_error_from_errno("close");
3340 free(label_orig);
3341 return err;
3344 const struct got_error *
3345 got_worktree_merge_files(struct got_worktree *worktree,
3346 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3347 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3348 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3350 const struct got_error *err, *unlockerr;
3351 char *fileindex_path = NULL;
3352 struct got_fileindex *fileindex = NULL;
3354 err = lock_worktree(worktree, LOCK_EX);
3355 if (err)
3356 return err;
3358 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3359 if (err)
3360 goto done;
3362 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3363 worktree);
3364 if (err)
3365 goto done;
3367 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3368 commit_id2, repo, progress_cb, progress_arg,
3369 cancel_cb, cancel_arg);
3370 done:
3371 if (fileindex)
3372 got_fileindex_free(fileindex);
3373 free(fileindex_path);
3374 unlockerr = lock_worktree(worktree, LOCK_SH);
3375 if (unlockerr && err == NULL)
3376 err = unlockerr;
3377 return err;
3380 struct diff_dir_cb_arg {
3381 struct got_fileindex *fileindex;
3382 struct got_worktree *worktree;
3383 const char *status_path;
3384 size_t status_path_len;
3385 struct got_repository *repo;
3386 got_worktree_status_cb status_cb;
3387 void *status_arg;
3388 got_cancel_cb cancel_cb;
3389 void *cancel_arg;
3390 /* A pathlist containing per-directory pathlists of ignore patterns. */
3391 struct got_pathlist_head *ignores;
3392 int report_unchanged;
3393 int no_ignores;
3396 static const struct got_error *
3397 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3398 int dirfd, const char *de_name,
3399 got_worktree_status_cb status_cb, void *status_arg,
3400 struct got_repository *repo, int report_unchanged)
3402 const struct got_error *err = NULL;
3403 unsigned char status = GOT_STATUS_NO_CHANGE;
3404 unsigned char staged_status;
3405 struct stat sb;
3406 struct got_object_id blob_id, commit_id, staged_blob_id;
3407 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3408 struct got_object_id *staged_blob_idp = NULL;
3410 staged_status = get_staged_status(ie);
3411 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3412 if (err)
3413 return err;
3415 if (status == GOT_STATUS_NO_CHANGE &&
3416 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3417 return NULL;
3419 if (got_fileindex_entry_has_blob(ie))
3420 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3421 if (got_fileindex_entry_has_commit(ie))
3422 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3423 if (staged_status == GOT_STATUS_ADD ||
3424 staged_status == GOT_STATUS_MODIFY) {
3425 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3426 &staged_blob_id, ie);
3429 return (*status_cb)(status_arg, status, staged_status,
3430 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3433 static const struct got_error *
3434 status_old_new(void *arg, struct got_fileindex_entry *ie,
3435 struct dirent *de, const char *parent_path, int dirfd)
3437 const struct got_error *err = NULL;
3438 struct diff_dir_cb_arg *a = arg;
3439 char *abspath;
3441 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3442 return got_error(GOT_ERR_CANCELLED);
3444 if (got_path_cmp(parent_path, a->status_path,
3445 strlen(parent_path), a->status_path_len) != 0 &&
3446 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3447 return NULL;
3449 if (parent_path[0]) {
3450 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3451 parent_path, de->d_name) == -1)
3452 return got_error_from_errno("asprintf");
3453 } else {
3454 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3455 de->d_name) == -1)
3456 return got_error_from_errno("asprintf");
3459 err = report_file_status(ie, abspath, dirfd, de->d_name,
3460 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3461 free(abspath);
3462 return err;
3465 static const struct got_error *
3466 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3468 struct diff_dir_cb_arg *a = arg;
3469 struct got_object_id blob_id, commit_id;
3470 unsigned char status;
3472 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3473 return got_error(GOT_ERR_CANCELLED);
3475 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3476 return NULL;
3478 got_fileindex_entry_get_blob_id(&blob_id, ie);
3479 got_fileindex_entry_get_commit_id(&commit_id, ie);
3480 if (got_fileindex_entry_has_file_on_disk(ie))
3481 status = GOT_STATUS_MISSING;
3482 else
3483 status = GOT_STATUS_DELETE;
3484 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3485 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3488 static void
3489 free_ignores(struct got_pathlist_head *ignores)
3491 struct got_pathlist_entry *pe;
3493 TAILQ_FOREACH(pe, ignores, entry) {
3494 struct got_pathlist_head *ignorelist = pe->data;
3496 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3498 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3501 static const struct got_error *
3502 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3504 const struct got_error *err = NULL;
3505 struct got_pathlist_entry *pe = NULL;
3506 struct got_pathlist_head *ignorelist;
3507 char *line = NULL, *pattern, *dirpath = NULL;
3508 size_t linesize = 0;
3509 ssize_t linelen;
3511 ignorelist = calloc(1, sizeof(*ignorelist));
3512 if (ignorelist == NULL)
3513 return got_error_from_errno("calloc");
3514 TAILQ_INIT(ignorelist);
3516 while ((linelen = getline(&line, &linesize, f)) != -1) {
3517 if (linelen > 0 && line[linelen - 1] == '\n')
3518 line[linelen - 1] = '\0';
3520 /* Git's ignores may contain comments. */
3521 if (line[0] == '#')
3522 continue;
3524 /* Git's negated patterns are not (yet?) supported. */
3525 if (line[0] == '!')
3526 continue;
3528 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3529 line) == -1) {
3530 err = got_error_from_errno("asprintf");
3531 goto done;
3533 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3534 if (err)
3535 goto done;
3537 if (ferror(f)) {
3538 err = got_error_from_errno("getline");
3539 goto done;
3542 dirpath = strdup(path);
3543 if (dirpath == NULL) {
3544 err = got_error_from_errno("strdup");
3545 goto done;
3547 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3548 done:
3549 free(line);
3550 if (err || pe == NULL) {
3551 free(dirpath);
3552 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3554 return err;
3557 static int
3558 match_ignores(struct got_pathlist_head *ignores, const char *path)
3560 struct got_pathlist_entry *pe;
3562 /* Handle patterns which match in all directories. */
3563 TAILQ_FOREACH(pe, ignores, entry) {
3564 struct got_pathlist_head *ignorelist = pe->data;
3565 struct got_pathlist_entry *pi;
3567 TAILQ_FOREACH(pi, ignorelist, entry) {
3568 const char *p, *pattern = pi->path;
3570 if (strncmp(pattern, "**/", 3) != 0)
3571 continue;
3572 pattern += 3;
3573 p = path;
3574 while (*p) {
3575 if (fnmatch(pattern, p,
3576 FNM_PATHNAME | FNM_LEADING_DIR)) {
3577 /* Retry in next directory. */
3578 while (*p && *p != '/')
3579 p++;
3580 while (*p == '/')
3581 p++;
3582 continue;
3584 return 1;
3590 * The ignores pathlist contains ignore lists from children before
3591 * parents, so we can find the most specific ignorelist by walking
3592 * ignores backwards.
3594 pe = TAILQ_LAST(ignores, got_pathlist_head);
3595 while (pe) {
3596 if (got_path_is_child(path, pe->path, pe->path_len)) {
3597 struct got_pathlist_head *ignorelist = pe->data;
3598 struct got_pathlist_entry *pi;
3599 TAILQ_FOREACH(pi, ignorelist, entry) {
3600 const char *pattern = pi->path;
3601 int flags = FNM_LEADING_DIR;
3602 if (strstr(pattern, "/**/") == NULL)
3603 flags |= FNM_PATHNAME;
3604 if (fnmatch(pattern, path, flags))
3605 continue;
3606 return 1;
3609 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3612 return 0;
3615 static const struct got_error *
3616 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3617 const char *path, int dirfd, const char *ignores_filename)
3619 const struct got_error *err = NULL;
3620 char *ignorespath;
3621 int fd = -1;
3622 FILE *ignoresfile = NULL;
3624 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3625 path[0] ? "/" : "", ignores_filename) == -1)
3626 return got_error_from_errno("asprintf");
3628 if (dirfd != -1) {
3629 fd = openat(dirfd, ignores_filename,
3630 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3631 if (fd == -1) {
3632 if (errno != ENOENT && errno != EACCES)
3633 err = got_error_from_errno2("openat",
3634 ignorespath);
3635 } else {
3636 ignoresfile = fdopen(fd, "r");
3637 if (ignoresfile == NULL)
3638 err = got_error_from_errno2("fdopen",
3639 ignorespath);
3640 else {
3641 fd = -1;
3642 err = read_ignores(ignores, path, ignoresfile);
3645 } else {
3646 ignoresfile = fopen(ignorespath, "re");
3647 if (ignoresfile == NULL) {
3648 if (errno != ENOENT && errno != EACCES)
3649 err = got_error_from_errno2("fopen",
3650 ignorespath);
3651 } else
3652 err = read_ignores(ignores, path, ignoresfile);
3655 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3656 err = got_error_from_errno2("fclose", path);
3657 if (fd != -1 && close(fd) == -1 && err == NULL)
3658 err = got_error_from_errno2("close", path);
3659 free(ignorespath);
3660 return err;
3663 static const struct got_error *
3664 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3665 int dirfd)
3667 const struct got_error *err = NULL;
3668 struct diff_dir_cb_arg *a = arg;
3669 char *path = NULL;
3671 if (ignore != NULL)
3672 *ignore = 0;
3674 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3675 return got_error(GOT_ERR_CANCELLED);
3677 if (parent_path[0]) {
3678 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3679 return got_error_from_errno("asprintf");
3680 } else {
3681 path = de->d_name;
3684 if (de->d_type == DT_DIR) {
3685 if (!a->no_ignores && ignore != NULL &&
3686 match_ignores(a->ignores, path))
3687 *ignore = 1;
3688 } else if (!match_ignores(a->ignores, path) &&
3689 got_path_is_child(path, a->status_path, a->status_path_len))
3690 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3691 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3692 if (parent_path[0])
3693 free(path);
3694 return err;
3697 static const struct got_error *
3698 status_traverse(void *arg, const char *path, int dirfd)
3700 const struct got_error *err = NULL;
3701 struct diff_dir_cb_arg *a = arg;
3703 if (a->no_ignores)
3704 return NULL;
3706 err = add_ignores(a->ignores, a->worktree->root_path,
3707 path, dirfd, ".cvsignore");
3708 if (err)
3709 return err;
3711 err = add_ignores(a->ignores, a->worktree->root_path, path,
3712 dirfd, ".gitignore");
3714 return err;
3717 static const struct got_error *
3718 report_single_file_status(const char *path, const char *ondisk_path,
3719 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3720 void *status_arg, struct got_repository *repo, int report_unchanged,
3721 struct got_pathlist_head *ignores, int no_ignores)
3723 struct got_fileindex_entry *ie;
3724 struct stat sb;
3726 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3727 if (ie)
3728 return report_file_status(ie, ondisk_path, -1, NULL,
3729 status_cb, status_arg, repo, report_unchanged);
3731 if (lstat(ondisk_path, &sb) == -1) {
3732 if (errno != ENOENT)
3733 return got_error_from_errno2("lstat", ondisk_path);
3734 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3735 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3738 if (!no_ignores && match_ignores(ignores, path))
3739 return NULL;
3741 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3742 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3743 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3745 return NULL;
3748 static const struct got_error *
3749 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3750 const char *root_path, const char *path)
3752 const struct got_error *err;
3753 char *parent_path, *next_parent_path = NULL;
3755 err = add_ignores(ignores, root_path, "", -1,
3756 ".cvsignore");
3757 if (err)
3758 return err;
3760 err = add_ignores(ignores, root_path, "", -1,
3761 ".gitignore");
3762 if (err)
3763 return err;
3765 err = got_path_dirname(&parent_path, path);
3766 if (err) {
3767 if (err->code == GOT_ERR_BAD_PATH)
3768 return NULL; /* cannot traverse parent */
3769 return err;
3771 for (;;) {
3772 err = add_ignores(ignores, root_path, parent_path, -1,
3773 ".cvsignore");
3774 if (err)
3775 break;
3776 err = add_ignores(ignores, root_path, parent_path, -1,
3777 ".gitignore");
3778 if (err)
3779 break;
3780 err = got_path_dirname(&next_parent_path, parent_path);
3781 if (err) {
3782 if (err->code == GOT_ERR_BAD_PATH)
3783 err = NULL; /* traversed everything */
3784 break;
3786 if (got_path_is_root_dir(parent_path))
3787 break;
3788 free(parent_path);
3789 parent_path = next_parent_path;
3790 next_parent_path = NULL;
3793 free(parent_path);
3794 free(next_parent_path);
3795 return err;
3798 static const struct got_error *
3799 worktree_status(struct got_worktree *worktree, const char *path,
3800 struct got_fileindex *fileindex, struct got_repository *repo,
3801 got_worktree_status_cb status_cb, void *status_arg,
3802 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3803 int report_unchanged)
3805 const struct got_error *err = NULL;
3806 int fd = -1;
3807 struct got_fileindex_diff_dir_cb fdiff_cb;
3808 struct diff_dir_cb_arg arg;
3809 char *ondisk_path = NULL;
3810 struct got_pathlist_head ignores;
3811 struct got_fileindex_entry *ie;
3813 TAILQ_INIT(&ignores);
3815 if (asprintf(&ondisk_path, "%s%s%s",
3816 worktree->root_path, path[0] ? "/" : "", path) == -1)
3817 return got_error_from_errno("asprintf");
3819 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3820 if (ie) {
3821 err = report_single_file_status(path, ondisk_path,
3822 fileindex, status_cb, status_arg, repo,
3823 report_unchanged, &ignores, no_ignores);
3824 goto done;
3827 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3828 if (fd == -1) {
3829 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3830 !got_err_open_nofollow_on_symlink())
3831 err = got_error_from_errno2("open", ondisk_path);
3832 else {
3833 if (!no_ignores) {
3834 err = add_ignores_from_parent_paths(&ignores,
3835 worktree->root_path, ondisk_path);
3836 if (err)
3837 goto done;
3839 err = report_single_file_status(path, ondisk_path,
3840 fileindex, status_cb, status_arg, repo,
3841 report_unchanged, &ignores, no_ignores);
3843 } else {
3844 fdiff_cb.diff_old_new = status_old_new;
3845 fdiff_cb.diff_old = status_old;
3846 fdiff_cb.diff_new = status_new;
3847 fdiff_cb.diff_traverse = status_traverse;
3848 arg.fileindex = fileindex;
3849 arg.worktree = worktree;
3850 arg.status_path = path;
3851 arg.status_path_len = strlen(path);
3852 arg.repo = repo;
3853 arg.status_cb = status_cb;
3854 arg.status_arg = status_arg;
3855 arg.cancel_cb = cancel_cb;
3856 arg.cancel_arg = cancel_arg;
3857 arg.report_unchanged = report_unchanged;
3858 arg.no_ignores = no_ignores;
3859 if (!no_ignores) {
3860 err = add_ignores_from_parent_paths(&ignores,
3861 worktree->root_path, path);
3862 if (err)
3863 goto done;
3865 arg.ignores = &ignores;
3866 err = got_fileindex_diff_dir(fileindex, fd,
3867 worktree->root_path, path, repo, &fdiff_cb, &arg);
3869 done:
3870 free_ignores(&ignores);
3871 if (fd != -1 && close(fd) == -1 && err == NULL)
3872 err = got_error_from_errno("close");
3873 free(ondisk_path);
3874 return err;
3877 const struct got_error *
3878 got_worktree_status(struct got_worktree *worktree,
3879 struct got_pathlist_head *paths, struct got_repository *repo,
3880 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3881 got_cancel_cb cancel_cb, void *cancel_arg)
3883 const struct got_error *err = NULL;
3884 char *fileindex_path = NULL;
3885 struct got_fileindex *fileindex = NULL;
3886 struct got_pathlist_entry *pe;
3888 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3889 if (err)
3890 return err;
3892 TAILQ_FOREACH(pe, paths, entry) {
3893 err = worktree_status(worktree, pe->path, fileindex, repo,
3894 status_cb, status_arg, cancel_cb, cancel_arg,
3895 no_ignores, 0);
3896 if (err)
3897 break;
3899 free(fileindex_path);
3900 got_fileindex_free(fileindex);
3901 return err;
3904 const struct got_error *
3905 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3906 const char *arg)
3908 const struct got_error *err = NULL;
3909 char *resolved = NULL, *cwd = NULL, *path = NULL;
3910 size_t len;
3911 struct stat sb;
3912 char *abspath = NULL;
3913 char canonpath[PATH_MAX];
3915 *wt_path = NULL;
3917 cwd = getcwd(NULL, 0);
3918 if (cwd == NULL)
3919 return got_error_from_errno("getcwd");
3921 if (lstat(arg, &sb) == -1) {
3922 if (errno != ENOENT) {
3923 err = got_error_from_errno2("lstat", arg);
3924 goto done;
3926 sb.st_mode = 0;
3928 if (S_ISLNK(sb.st_mode)) {
3930 * We cannot use realpath(3) with symlinks since we want to
3931 * operate on the symlink itself.
3932 * But we can make the path absolute, assuming it is relative
3933 * to the current working directory, and then canonicalize it.
3935 if (!got_path_is_absolute(arg)) {
3936 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3937 err = got_error_from_errno("asprintf");
3938 goto done;
3942 err = got_canonpath(abspath ? abspath : arg, canonpath,
3943 sizeof(canonpath));
3944 if (err)
3945 goto done;
3946 resolved = strdup(canonpath);
3947 if (resolved == NULL) {
3948 err = got_error_from_errno("strdup");
3949 goto done;
3951 } else {
3952 resolved = realpath(arg, NULL);
3953 if (resolved == NULL) {
3954 if (errno != ENOENT) {
3955 err = got_error_from_errno2("realpath", arg);
3956 goto done;
3958 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3959 err = got_error_from_errno("asprintf");
3960 goto done;
3962 err = got_canonpath(abspath, canonpath,
3963 sizeof(canonpath));
3964 if (err)
3965 goto done;
3966 resolved = strdup(canonpath);
3967 if (resolved == NULL) {
3968 err = got_error_from_errno("strdup");
3969 goto done;
3974 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3975 strlen(got_worktree_get_root_path(worktree)))) {
3976 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3977 goto done;
3980 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3981 err = got_path_skip_common_ancestor(&path,
3982 got_worktree_get_root_path(worktree), resolved);
3983 if (err)
3984 goto done;
3985 } else {
3986 path = strdup("");
3987 if (path == NULL) {
3988 err = got_error_from_errno("strdup");
3989 goto done;
3993 /* XXX status walk can't deal with trailing slash! */
3994 len = strlen(path);
3995 while (len > 0 && path[len - 1] == '/') {
3996 path[len - 1] = '\0';
3997 len--;
3999 done:
4000 free(abspath);
4001 free(resolved);
4002 free(cwd);
4003 if (err == NULL)
4004 *wt_path = path;
4005 else
4006 free(path);
4007 return err;
4010 struct schedule_addition_args {
4011 struct got_worktree *worktree;
4012 struct got_fileindex *fileindex;
4013 got_worktree_checkout_cb progress_cb;
4014 void *progress_arg;
4015 struct got_repository *repo;
4018 static const struct got_error *
4019 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4020 const char *relpath, struct got_object_id *blob_id,
4021 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4022 int dirfd, const char *de_name)
4024 struct schedule_addition_args *a = arg;
4025 const struct got_error *err = NULL;
4026 struct got_fileindex_entry *ie;
4027 struct stat sb;
4028 char *ondisk_path;
4030 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4031 relpath) == -1)
4032 return got_error_from_errno("asprintf");
4034 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4035 if (ie) {
4036 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4037 de_name, a->repo);
4038 if (err)
4039 goto done;
4040 /* Re-adding an existing entry is a no-op. */
4041 if (status == GOT_STATUS_ADD)
4042 goto done;
4043 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4044 if (err)
4045 goto done;
4048 if (status != GOT_STATUS_UNVERSIONED) {
4049 if (status == GOT_STATUS_NONEXISTENT)
4050 err = got_error_set_errno(ENOENT, ondisk_path);
4051 else
4052 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4053 goto done;
4056 err = got_fileindex_entry_alloc(&ie, relpath);
4057 if (err)
4058 goto done;
4059 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4060 relpath, NULL, NULL, 1);
4061 if (err) {
4062 got_fileindex_entry_free(ie);
4063 goto done;
4065 err = got_fileindex_entry_add(a->fileindex, ie);
4066 if (err) {
4067 got_fileindex_entry_free(ie);
4068 goto done;
4070 done:
4071 free(ondisk_path);
4072 if (err)
4073 return err;
4074 if (status == GOT_STATUS_ADD)
4075 return NULL;
4076 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4079 const struct got_error *
4080 got_worktree_schedule_add(struct got_worktree *worktree,
4081 struct got_pathlist_head *paths,
4082 got_worktree_checkout_cb progress_cb, void *progress_arg,
4083 struct got_repository *repo, int no_ignores)
4085 struct got_fileindex *fileindex = NULL;
4086 char *fileindex_path = NULL;
4087 const struct got_error *err = NULL, *sync_err, *unlockerr;
4088 struct got_pathlist_entry *pe;
4089 struct schedule_addition_args saa;
4091 err = lock_worktree(worktree, LOCK_EX);
4092 if (err)
4093 return err;
4095 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4096 if (err)
4097 goto done;
4099 saa.worktree = worktree;
4100 saa.fileindex = fileindex;
4101 saa.progress_cb = progress_cb;
4102 saa.progress_arg = progress_arg;
4103 saa.repo = repo;
4105 TAILQ_FOREACH(pe, paths, entry) {
4106 err = worktree_status(worktree, pe->path, fileindex, repo,
4107 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4108 if (err)
4109 break;
4111 sync_err = sync_fileindex(fileindex, fileindex_path);
4112 if (sync_err && err == NULL)
4113 err = sync_err;
4114 done:
4115 free(fileindex_path);
4116 if (fileindex)
4117 got_fileindex_free(fileindex);
4118 unlockerr = lock_worktree(worktree, LOCK_SH);
4119 if (unlockerr && err == NULL)
4120 err = unlockerr;
4121 return err;
4124 struct schedule_deletion_args {
4125 struct got_worktree *worktree;
4126 struct got_fileindex *fileindex;
4127 got_worktree_delete_cb progress_cb;
4128 void *progress_arg;
4129 struct got_repository *repo;
4130 int delete_local_mods;
4131 int keep_on_disk;
4132 int ignore_missing_paths;
4133 const char *status_codes;
4136 static const struct got_error *
4137 schedule_for_deletion(void *arg, unsigned char status,
4138 unsigned char staged_status, const char *relpath,
4139 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4140 struct got_object_id *commit_id, int dirfd, const char *de_name)
4142 struct schedule_deletion_args *a = arg;
4143 const struct got_error *err = NULL;
4144 struct got_fileindex_entry *ie = NULL;
4145 struct stat sb;
4146 char *ondisk_path;
4148 if (status == GOT_STATUS_NONEXISTENT) {
4149 if (a->ignore_missing_paths)
4150 return NULL;
4151 return got_error_set_errno(ENOENT, relpath);
4154 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4155 if (ie == NULL)
4156 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4158 staged_status = get_staged_status(ie);
4159 if (staged_status != GOT_STATUS_NO_CHANGE) {
4160 if (staged_status == GOT_STATUS_DELETE)
4161 return NULL;
4162 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4165 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4166 relpath) == -1)
4167 return got_error_from_errno("asprintf");
4169 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4170 a->repo);
4171 if (err)
4172 goto done;
4174 if (a->status_codes) {
4175 size_t ncodes = strlen(a->status_codes);
4176 int i;
4177 for (i = 0; i < ncodes ; i++) {
4178 if (status == a->status_codes[i])
4179 break;
4181 if (i == ncodes) {
4182 /* Do not delete files in non-matching status. */
4183 free(ondisk_path);
4184 return NULL;
4186 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4187 a->status_codes[i] != GOT_STATUS_MISSING) {
4188 static char msg[64];
4189 snprintf(msg, sizeof(msg),
4190 "invalid status code '%c'", a->status_codes[i]);
4191 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4192 goto done;
4196 if (status != GOT_STATUS_NO_CHANGE) {
4197 if (status == GOT_STATUS_DELETE)
4198 goto done;
4199 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4200 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4201 goto done;
4203 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4204 err = got_error_set_errno(ENOENT, relpath);
4205 goto done;
4207 if (status != GOT_STATUS_MODIFY &&
4208 status != GOT_STATUS_MISSING) {
4209 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4210 goto done;
4214 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4215 size_t root_len;
4217 if (dirfd != -1) {
4218 if (unlinkat(dirfd, de_name, 0) == -1) {
4219 err = got_error_from_errno2("unlinkat",
4220 ondisk_path);
4221 goto done;
4223 } else if (unlink(ondisk_path) == -1) {
4224 err = got_error_from_errno2("unlink", ondisk_path);
4225 goto done;
4228 root_len = strlen(a->worktree->root_path);
4229 do {
4230 char *parent;
4231 err = got_path_dirname(&parent, ondisk_path);
4232 if (err)
4233 goto done;
4234 free(ondisk_path);
4235 ondisk_path = parent;
4236 if (rmdir(ondisk_path) == -1) {
4237 if (errno != ENOTEMPTY)
4238 err = got_error_from_errno2("rmdir",
4239 ondisk_path);
4240 break;
4242 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4243 strlen(ondisk_path), root_len) != 0);
4246 got_fileindex_entry_mark_deleted_from_disk(ie);
4247 done:
4248 free(ondisk_path);
4249 if (err)
4250 return err;
4251 if (status == GOT_STATUS_DELETE)
4252 return NULL;
4253 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4254 staged_status, relpath);
4257 const struct got_error *
4258 got_worktree_schedule_delete(struct got_worktree *worktree,
4259 struct got_pathlist_head *paths, int delete_local_mods,
4260 const char *status_codes,
4261 got_worktree_delete_cb progress_cb, void *progress_arg,
4262 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4264 struct got_fileindex *fileindex = NULL;
4265 char *fileindex_path = NULL;
4266 const struct got_error *err = NULL, *sync_err, *unlockerr;
4267 struct got_pathlist_entry *pe;
4268 struct schedule_deletion_args sda;
4270 err = lock_worktree(worktree, LOCK_EX);
4271 if (err)
4272 return err;
4274 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4275 if (err)
4276 goto done;
4278 sda.worktree = worktree;
4279 sda.fileindex = fileindex;
4280 sda.progress_cb = progress_cb;
4281 sda.progress_arg = progress_arg;
4282 sda.repo = repo;
4283 sda.delete_local_mods = delete_local_mods;
4284 sda.keep_on_disk = keep_on_disk;
4285 sda.ignore_missing_paths = ignore_missing_paths;
4286 sda.status_codes = status_codes;
4288 TAILQ_FOREACH(pe, paths, entry) {
4289 err = worktree_status(worktree, pe->path, fileindex, repo,
4290 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4291 if (err)
4292 break;
4294 sync_err = sync_fileindex(fileindex, fileindex_path);
4295 if (sync_err && err == NULL)
4296 err = sync_err;
4297 done:
4298 free(fileindex_path);
4299 if (fileindex)
4300 got_fileindex_free(fileindex);
4301 unlockerr = lock_worktree(worktree, LOCK_SH);
4302 if (unlockerr && err == NULL)
4303 err = unlockerr;
4304 return err;
4307 static const struct got_error *
4308 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4310 const struct got_error *err = NULL;
4311 char *line = NULL;
4312 size_t linesize = 0, n;
4313 ssize_t linelen;
4315 linelen = getline(&line, &linesize, infile);
4316 if (linelen == -1) {
4317 if (ferror(infile)) {
4318 err = got_error_from_errno("getline");
4319 goto done;
4321 return NULL;
4323 if (outfile) {
4324 n = fwrite(line, 1, linelen, outfile);
4325 if (n != linelen) {
4326 err = got_ferror(outfile, GOT_ERR_IO);
4327 goto done;
4330 if (rejectfile) {
4331 n = fwrite(line, 1, linelen, rejectfile);
4332 if (n != linelen)
4333 err = got_ferror(rejectfile, GOT_ERR_IO);
4335 done:
4336 free(line);
4337 return err;
4340 static const struct got_error *
4341 skip_one_line(FILE *f)
4343 char *line = NULL;
4344 size_t linesize = 0;
4345 ssize_t linelen;
4347 linelen = getline(&line, &linesize, f);
4348 if (linelen == -1) {
4349 if (ferror(f))
4350 return got_error_from_errno("getline");
4351 return NULL;
4353 free(line);
4354 return NULL;
4357 static const struct got_error *
4358 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4359 int start_old, int end_old, int start_new, int end_new,
4360 FILE *outfile, FILE *rejectfile)
4362 const struct got_error *err;
4364 /* Copy old file's lines leading up to patch. */
4365 while (!feof(f1) && *line_cur1 < start_old) {
4366 err = copy_one_line(f1, outfile, NULL);
4367 if (err)
4368 return err;
4369 (*line_cur1)++;
4371 /* Skip new file's lines leading up to patch. */
4372 while (!feof(f2) && *line_cur2 < start_new) {
4373 if (rejectfile)
4374 err = copy_one_line(f2, NULL, rejectfile);
4375 else
4376 err = skip_one_line(f2);
4377 if (err)
4378 return err;
4379 (*line_cur2)++;
4381 /* Copy patched lines. */
4382 while (!feof(f2) && *line_cur2 <= end_new) {
4383 err = copy_one_line(f2, outfile, NULL);
4384 if (err)
4385 return err;
4386 (*line_cur2)++;
4388 /* Skip over old file's replaced lines. */
4389 while (!feof(f1) && *line_cur1 <= end_old) {
4390 if (rejectfile)
4391 err = copy_one_line(f1, NULL, rejectfile);
4392 else
4393 err = skip_one_line(f1);
4394 if (err)
4395 return err;
4396 (*line_cur1)++;
4399 return NULL;
4402 static const struct got_error *
4403 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4404 FILE *outfile, FILE *rejectfile)
4406 const struct got_error *err;
4408 if (outfile) {
4409 /* Copy old file's lines until EOF. */
4410 while (!feof(f1)) {
4411 err = copy_one_line(f1, outfile, NULL);
4412 if (err)
4413 return err;
4414 (*line_cur1)++;
4417 if (rejectfile) {
4418 /* Copy new file's lines until EOF. */
4419 while (!feof(f2)) {
4420 err = copy_one_line(f2, NULL, rejectfile);
4421 if (err)
4422 return err;
4423 (*line_cur2)++;
4427 return NULL;
4430 static const struct got_error *
4431 apply_or_reject_change(int *choice, int *nchunks_used,
4432 struct diff_result *diff_result, int n,
4433 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4434 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4435 got_worktree_patch_cb patch_cb, void *patch_arg)
4437 const struct got_error *err = NULL;
4438 struct diff_chunk_context cc = {};
4439 int start_old, end_old, start_new, end_new;
4440 FILE *hunkfile;
4441 struct diff_output_unidiff_state *diff_state;
4442 struct diff_input_info diff_info;
4443 int rc;
4445 *choice = GOT_PATCH_CHOICE_NONE;
4447 /* Get changed line numbers without context lines for copy_change(). */
4448 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4449 start_old = cc.left.start;
4450 end_old = cc.left.end;
4451 start_new = cc.right.start;
4452 end_new = cc.right.end;
4454 /* Get the same change with context lines for display. */
4455 memset(&cc, 0, sizeof(cc));
4456 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4458 memset(&diff_info, 0, sizeof(diff_info));
4459 diff_info.left_path = relpath;
4460 diff_info.right_path = relpath;
4462 diff_state = diff_output_unidiff_state_alloc();
4463 if (diff_state == NULL)
4464 return got_error_set_errno(ENOMEM,
4465 "diff_output_unidiff_state_alloc");
4467 hunkfile = got_opentemp();
4468 if (hunkfile == NULL) {
4469 err = got_error_from_errno("got_opentemp");
4470 goto done;
4473 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4474 diff_result, &cc);
4475 if (rc != DIFF_RC_OK) {
4476 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4477 goto done;
4480 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4481 err = got_ferror(hunkfile, GOT_ERR_IO);
4482 goto done;
4485 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4486 hunkfile, changeno, nchanges);
4487 if (err)
4488 goto done;
4490 switch (*choice) {
4491 case GOT_PATCH_CHOICE_YES:
4492 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4493 end_old, start_new, end_new, outfile, rejectfile);
4494 break;
4495 case GOT_PATCH_CHOICE_NO:
4496 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4497 end_old, start_new, end_new, rejectfile, outfile);
4498 break;
4499 case GOT_PATCH_CHOICE_QUIT:
4500 break;
4501 default:
4502 err = got_error(GOT_ERR_PATCH_CHOICE);
4503 break;
4505 done:
4506 diff_output_unidiff_state_free(diff_state);
4507 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4508 err = got_error_from_errno("fclose");
4509 return err;
4512 struct revert_file_args {
4513 struct got_worktree *worktree;
4514 struct got_fileindex *fileindex;
4515 got_worktree_checkout_cb progress_cb;
4516 void *progress_arg;
4517 got_worktree_patch_cb patch_cb;
4518 void *patch_arg;
4519 struct got_repository *repo;
4520 int unlink_added_files;
4523 static const struct got_error *
4524 create_patched_content(char **path_outfile, int reverse_patch,
4525 struct got_object_id *blob_id, const char *path2,
4526 int dirfd2, const char *de_name2,
4527 const char *relpath, struct got_repository *repo,
4528 got_worktree_patch_cb patch_cb, void *patch_arg)
4530 const struct got_error *err, *free_err;
4531 struct got_blob_object *blob = NULL;
4532 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4533 int fd = -1, fd2 = -1;
4534 char link_target[PATH_MAX];
4535 ssize_t link_len = 0;
4536 char *path1 = NULL, *id_str = NULL;
4537 struct stat sb2;
4538 struct got_diffreg_result *diffreg_result = NULL;
4539 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4540 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4542 *path_outfile = NULL;
4544 err = got_object_id_str(&id_str, blob_id);
4545 if (err)
4546 return err;
4548 if (dirfd2 != -1) {
4549 fd2 = openat(dirfd2, de_name2,
4550 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4551 if (fd2 == -1) {
4552 if (!got_err_open_nofollow_on_symlink()) {
4553 err = got_error_from_errno2("openat", path2);
4554 goto done;
4556 link_len = readlinkat(dirfd2, de_name2,
4557 link_target, sizeof(link_target));
4558 if (link_len == -1) {
4559 return got_error_from_errno2("readlinkat",
4560 path2);
4562 sb2.st_mode = S_IFLNK;
4563 sb2.st_size = link_len;
4565 } else {
4566 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4567 if (fd2 == -1) {
4568 if (!got_err_open_nofollow_on_symlink()) {
4569 err = got_error_from_errno2("open", path2);
4570 goto done;
4572 link_len = readlink(path2, link_target,
4573 sizeof(link_target));
4574 if (link_len == -1)
4575 return got_error_from_errno2("readlink", path2);
4576 sb2.st_mode = S_IFLNK;
4577 sb2.st_size = link_len;
4580 if (fd2 != -1) {
4581 if (fstat(fd2, &sb2) == -1) {
4582 err = got_error_from_errno2("fstat", path2);
4583 goto done;
4586 f2 = fdopen(fd2, "r");
4587 if (f2 == NULL) {
4588 err = got_error_from_errno2("fdopen", path2);
4589 goto done;
4591 fd2 = -1;
4592 } else {
4593 size_t n;
4594 f2 = got_opentemp();
4595 if (f2 == NULL) {
4596 err = got_error_from_errno2("got_opentemp", path2);
4597 goto done;
4599 n = fwrite(link_target, 1, link_len, f2);
4600 if (n != link_len) {
4601 err = got_ferror(f2, GOT_ERR_IO);
4602 goto done;
4604 if (fflush(f2) == EOF) {
4605 err = got_error_from_errno("fflush");
4606 goto done;
4608 rewind(f2);
4611 fd = got_opentempfd();
4612 if (fd == -1) {
4613 err = got_error_from_errno("got_opentempfd");
4614 goto done;
4617 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4618 if (err)
4619 goto done;
4621 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4622 if (err)
4623 goto done;
4625 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4626 if (err)
4627 goto done;
4629 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4630 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4631 if (err)
4632 goto done;
4634 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4635 "");
4636 if (err)
4637 goto done;
4639 if (fseek(f1, 0L, SEEK_SET) == -1)
4640 return got_ferror(f1, GOT_ERR_IO);
4641 if (fseek(f2, 0L, SEEK_SET) == -1)
4642 return got_ferror(f2, GOT_ERR_IO);
4644 /* Count the number of actual changes in the diff result. */
4645 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4646 struct diff_chunk_context cc = {};
4647 diff_chunk_context_load_change(&cc, &nchunks_used,
4648 diffreg_result->result, n, 0);
4649 nchanges++;
4651 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4652 int choice;
4653 err = apply_or_reject_change(&choice, &nchunks_used,
4654 diffreg_result->result, n, relpath, f1, f2,
4655 &line_cur1, &line_cur2,
4656 reverse_patch ? NULL : outfile,
4657 reverse_patch ? outfile : NULL,
4658 ++i, nchanges, patch_cb, patch_arg);
4659 if (err)
4660 goto done;
4661 if (choice == GOT_PATCH_CHOICE_YES)
4662 have_content = 1;
4663 else if (choice == GOT_PATCH_CHOICE_QUIT)
4664 break;
4666 if (have_content) {
4667 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4668 reverse_patch ? NULL : outfile,
4669 reverse_patch ? outfile : NULL);
4670 if (err)
4671 goto done;
4673 if (!S_ISLNK(sb2.st_mode)) {
4674 mode_t mode;
4676 mode = apply_umask(sb2.st_mode);
4677 if (fchmod(fileno(outfile), mode) == -1) {
4678 err = got_error_from_errno2("fchmod", path2);
4679 goto done;
4683 done:
4684 free(id_str);
4685 if (fd != -1 && close(fd) == -1 && err == NULL)
4686 err = got_error_from_errno("close");
4687 if (blob)
4688 got_object_blob_close(blob);
4689 free_err = got_diffreg_result_free(diffreg_result);
4690 if (err == NULL)
4691 err = free_err;
4692 if (f1 && fclose(f1) == EOF && err == NULL)
4693 err = got_error_from_errno2("fclose", path1);
4694 if (f2 && fclose(f2) == EOF && err == NULL)
4695 err = got_error_from_errno2("fclose", path2);
4696 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4697 err = got_error_from_errno2("close", path2);
4698 if (outfile && fclose(outfile) == EOF && err == NULL)
4699 err = got_error_from_errno2("fclose", *path_outfile);
4700 if (path1 && unlink(path1) == -1 && err == NULL)
4701 err = got_error_from_errno2("unlink", path1);
4702 if (err || !have_content) {
4703 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4704 err = got_error_from_errno2("unlink", *path_outfile);
4705 free(*path_outfile);
4706 *path_outfile = NULL;
4708 free(path1);
4709 return err;
4712 static const struct got_error *
4713 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4714 const char *relpath, struct got_object_id *blob_id,
4715 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4716 int dirfd, const char *de_name)
4718 struct revert_file_args *a = arg;
4719 const struct got_error *err = NULL;
4720 char *parent_path = NULL;
4721 struct got_fileindex_entry *ie;
4722 struct got_commit_object *base_commit = NULL;
4723 struct got_tree_object *tree = NULL;
4724 struct got_object_id *tree_id = NULL;
4725 const struct got_tree_entry *te = NULL;
4726 char *tree_path = NULL, *te_name;
4727 char *ondisk_path = NULL, *path_content = NULL;
4728 struct got_blob_object *blob = NULL;
4729 int fd = -1;
4731 /* Reverting a staged deletion is a no-op. */
4732 if (status == GOT_STATUS_DELETE &&
4733 staged_status != GOT_STATUS_NO_CHANGE)
4734 return NULL;
4736 if (status == GOT_STATUS_UNVERSIONED)
4737 return (*a->progress_cb)(a->progress_arg,
4738 GOT_STATUS_UNVERSIONED, relpath);
4740 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4741 if (ie == NULL)
4742 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4744 /* Construct in-repository path of tree which contains this blob. */
4745 err = got_path_dirname(&parent_path, ie->path);
4746 if (err) {
4747 if (err->code != GOT_ERR_BAD_PATH)
4748 goto done;
4749 parent_path = strdup("/");
4750 if (parent_path == NULL) {
4751 err = got_error_from_errno("strdup");
4752 goto done;
4755 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4756 tree_path = strdup(parent_path);
4757 if (tree_path == NULL) {
4758 err = got_error_from_errno("strdup");
4759 goto done;
4761 } else {
4762 if (got_path_is_root_dir(parent_path)) {
4763 tree_path = strdup(a->worktree->path_prefix);
4764 if (tree_path == NULL) {
4765 err = got_error_from_errno("strdup");
4766 goto done;
4768 } else {
4769 if (asprintf(&tree_path, "%s/%s",
4770 a->worktree->path_prefix, parent_path) == -1) {
4771 err = got_error_from_errno("asprintf");
4772 goto done;
4777 err = got_object_open_as_commit(&base_commit, a->repo,
4778 a->worktree->base_commit_id);
4779 if (err)
4780 goto done;
4782 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4783 if (err) {
4784 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4785 (status == GOT_STATUS_ADD ||
4786 staged_status == GOT_STATUS_ADD)))
4787 goto done;
4788 } else {
4789 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4790 if (err)
4791 goto done;
4793 err = got_path_basename(&te_name, ie->path);
4794 if (err)
4795 goto done;
4797 te = got_object_tree_find_entry(tree, te_name);
4798 free(te_name);
4799 if (te == NULL && status != GOT_STATUS_ADD &&
4800 staged_status != GOT_STATUS_ADD) {
4801 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4802 goto done;
4806 switch (status) {
4807 case GOT_STATUS_ADD:
4808 if (a->patch_cb) {
4809 int choice = GOT_PATCH_CHOICE_NONE;
4810 err = (*a->patch_cb)(&choice, a->patch_arg,
4811 status, ie->path, NULL, 1, 1);
4812 if (err)
4813 goto done;
4814 if (choice != GOT_PATCH_CHOICE_YES)
4815 break;
4817 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4818 ie->path);
4819 if (err)
4820 goto done;
4821 got_fileindex_entry_remove(a->fileindex, ie);
4822 if (a->unlink_added_files) {
4823 if (asprintf(&ondisk_path, "%s/%s",
4824 got_worktree_get_root_path(a->worktree),
4825 relpath) == -1) {
4826 err = got_error_from_errno("asprintf");
4827 goto done;
4829 if (unlink(ondisk_path) == -1) {
4830 err = got_error_from_errno2("unlink",
4831 ondisk_path);
4832 break;
4835 break;
4836 case GOT_STATUS_DELETE:
4837 if (a->patch_cb) {
4838 int choice = GOT_PATCH_CHOICE_NONE;
4839 err = (*a->patch_cb)(&choice, a->patch_arg,
4840 status, ie->path, NULL, 1, 1);
4841 if (err)
4842 goto done;
4843 if (choice != GOT_PATCH_CHOICE_YES)
4844 break;
4846 /* fall through */
4847 case GOT_STATUS_MODIFY:
4848 case GOT_STATUS_MODE_CHANGE:
4849 case GOT_STATUS_CONFLICT:
4850 case GOT_STATUS_MISSING: {
4851 struct got_object_id id;
4852 if (staged_status == GOT_STATUS_ADD ||
4853 staged_status == GOT_STATUS_MODIFY)
4854 got_fileindex_entry_get_staged_blob_id(&id, ie);
4855 else
4856 got_fileindex_entry_get_blob_id(&id, ie);
4857 fd = got_opentempfd();
4858 if (fd == -1) {
4859 err = got_error_from_errno("got_opentempfd");
4860 goto done;
4863 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4864 if (err)
4865 goto done;
4867 if (asprintf(&ondisk_path, "%s/%s",
4868 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4869 err = got_error_from_errno("asprintf");
4870 goto done;
4873 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4874 status == GOT_STATUS_CONFLICT)) {
4875 int is_bad_symlink = 0;
4876 err = create_patched_content(&path_content, 1, &id,
4877 ondisk_path, dirfd, de_name, ie->path, a->repo,
4878 a->patch_cb, a->patch_arg);
4879 if (err || path_content == NULL)
4880 break;
4881 if (te && S_ISLNK(te->mode)) {
4882 if (unlink(path_content) == -1) {
4883 err = got_error_from_errno2("unlink",
4884 path_content);
4885 break;
4887 err = install_symlink(&is_bad_symlink,
4888 a->worktree, ondisk_path, ie->path,
4889 blob, 0, 1, 0, 0, a->repo,
4890 a->progress_cb, a->progress_arg);
4891 } else {
4892 if (rename(path_content, ondisk_path) == -1) {
4893 err = got_error_from_errno3("rename",
4894 path_content, ondisk_path);
4895 goto done;
4898 } else {
4899 int is_bad_symlink = 0;
4900 if (te && S_ISLNK(te->mode)) {
4901 err = install_symlink(&is_bad_symlink,
4902 a->worktree, ondisk_path, ie->path,
4903 blob, 0, 1, 0, 0, a->repo,
4904 a->progress_cb, a->progress_arg);
4905 } else {
4906 err = install_blob(a->worktree, ondisk_path,
4907 ie->path,
4908 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4909 got_fileindex_perms_to_st(ie), blob,
4910 0, 1, 0, 0, a->repo,
4911 a->progress_cb, a->progress_arg);
4913 if (err)
4914 goto done;
4915 if (status == GOT_STATUS_DELETE ||
4916 status == GOT_STATUS_MODE_CHANGE) {
4917 err = got_fileindex_entry_update(ie,
4918 a->worktree->root_fd, relpath,
4919 blob->id.sha1,
4920 a->worktree->base_commit_id->sha1, 1);
4921 if (err)
4922 goto done;
4924 if (is_bad_symlink) {
4925 got_fileindex_entry_filetype_set(ie,
4926 GOT_FILEIDX_MODE_BAD_SYMLINK);
4929 break;
4931 default:
4932 break;
4934 done:
4935 free(ondisk_path);
4936 free(path_content);
4937 free(parent_path);
4938 free(tree_path);
4939 if (fd != -1 && close(fd) == -1 && err == NULL)
4940 err = got_error_from_errno("close");
4941 if (blob)
4942 got_object_blob_close(blob);
4943 if (tree)
4944 got_object_tree_close(tree);
4945 free(tree_id);
4946 if (base_commit)
4947 got_object_commit_close(base_commit);
4948 return err;
4951 const struct got_error *
4952 got_worktree_revert(struct got_worktree *worktree,
4953 struct got_pathlist_head *paths,
4954 got_worktree_checkout_cb progress_cb, void *progress_arg,
4955 got_worktree_patch_cb patch_cb, void *patch_arg,
4956 struct got_repository *repo)
4958 struct got_fileindex *fileindex = NULL;
4959 char *fileindex_path = NULL;
4960 const struct got_error *err = NULL, *unlockerr = NULL;
4961 const struct got_error *sync_err = NULL;
4962 struct got_pathlist_entry *pe;
4963 struct revert_file_args rfa;
4965 err = lock_worktree(worktree, LOCK_EX);
4966 if (err)
4967 return err;
4969 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4970 if (err)
4971 goto done;
4973 rfa.worktree = worktree;
4974 rfa.fileindex = fileindex;
4975 rfa.progress_cb = progress_cb;
4976 rfa.progress_arg = progress_arg;
4977 rfa.patch_cb = patch_cb;
4978 rfa.patch_arg = patch_arg;
4979 rfa.repo = repo;
4980 rfa.unlink_added_files = 0;
4981 TAILQ_FOREACH(pe, paths, entry) {
4982 err = worktree_status(worktree, pe->path, fileindex, repo,
4983 revert_file, &rfa, NULL, NULL, 1, 0);
4984 if (err)
4985 break;
4987 sync_err = sync_fileindex(fileindex, fileindex_path);
4988 if (sync_err && err == NULL)
4989 err = sync_err;
4990 done:
4991 free(fileindex_path);
4992 if (fileindex)
4993 got_fileindex_free(fileindex);
4994 unlockerr = lock_worktree(worktree, LOCK_SH);
4995 if (unlockerr && err == NULL)
4996 err = unlockerr;
4997 return err;
5000 static void
5001 free_commitable(struct got_commitable *ct)
5003 free(ct->path);
5004 free(ct->in_repo_path);
5005 free(ct->ondisk_path);
5006 free(ct->blob_id);
5007 free(ct->base_blob_id);
5008 free(ct->staged_blob_id);
5009 free(ct->base_commit_id);
5010 free(ct);
5013 struct collect_commitables_arg {
5014 struct got_pathlist_head *commitable_paths;
5015 struct got_repository *repo;
5016 struct got_worktree *worktree;
5017 struct got_fileindex *fileindex;
5018 int have_staged_files;
5019 int allow_bad_symlinks;
5020 int diff_header_shown;
5021 int commit_conflicts;
5022 FILE *diff_outfile;
5023 FILE *f1;
5024 FILE *f2;
5028 * Create a file which contains the target path of a symlink so we can feed
5029 * it as content to the diff engine.
5031 static const struct got_error *
5032 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5033 const char *abspath)
5035 const struct got_error *err = NULL;
5036 char target_path[PATH_MAX];
5037 ssize_t target_len, outlen;
5039 *fd = -1;
5041 if (dirfd != -1) {
5042 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5043 if (target_len == -1)
5044 return got_error_from_errno2("readlinkat", abspath);
5045 } else {
5046 target_len = readlink(abspath, target_path, PATH_MAX);
5047 if (target_len == -1)
5048 return got_error_from_errno2("readlink", abspath);
5051 *fd = got_opentempfd();
5052 if (*fd == -1)
5053 return got_error_from_errno("got_opentempfd");
5055 outlen = write(*fd, target_path, target_len);
5056 if (outlen == -1) {
5057 err = got_error_from_errno("got_opentempfd");
5058 goto done;
5061 if (lseek(*fd, 0, SEEK_SET) == -1) {
5062 err = got_error_from_errno2("lseek", abspath);
5063 goto done;
5065 done:
5066 if (err) {
5067 close(*fd);
5068 *fd = -1;
5070 return err;
5073 static const struct got_error *
5074 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5075 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5076 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5078 const struct got_error *err = NULL;
5079 struct got_blob_object *blob1 = NULL;
5080 int fd = -1, fd1 = -1, fd2 = -1;
5081 FILE *ondisk_file = NULL;
5082 char *label1 = NULL;
5083 struct stat sb;
5084 off_t size1 = 0;
5085 int f2_exists = 0;
5086 char *id_str = NULL;
5088 memset(&sb, 0, sizeof(sb));
5090 if (diff_staged) {
5091 if (ct->staged_status != GOT_STATUS_MODIFY &&
5092 ct->staged_status != GOT_STATUS_ADD &&
5093 ct->staged_status != GOT_STATUS_DELETE)
5094 return NULL;
5095 } else {
5096 if (ct->status != GOT_STATUS_MODIFY &&
5097 ct->status != GOT_STATUS_ADD &&
5098 ct->status != GOT_STATUS_DELETE &&
5099 ct->status != GOT_STATUS_CONFLICT)
5100 return NULL;
5103 err = got_opentemp_truncate(f1);
5104 if (err)
5105 return got_error_from_errno("got_opentemp_truncate");
5106 err = got_opentemp_truncate(f2);
5107 if (err)
5108 return got_error_from_errno("got_opentemp_truncate");
5110 if (!*diff_header_shown) {
5111 err = got_object_id_str(&id_str, worktree->base_commit_id);
5112 if (err)
5113 return err;
5114 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5115 got_worktree_get_root_path(worktree));
5116 fprintf(diff_outfile, "commit - %s\n", id_str);
5117 fprintf(diff_outfile, "path + %s%s\n",
5118 got_worktree_get_root_path(worktree),
5119 diff_staged ? " (staged changes)" : "");
5120 *diff_header_shown = 1;
5123 if (diff_staged) {
5124 const char *label1 = NULL, *label2 = NULL;
5125 switch (ct->staged_status) {
5126 case GOT_STATUS_MODIFY:
5127 label1 = ct->path;
5128 label2 = ct->path;
5129 break;
5130 case GOT_STATUS_ADD:
5131 label2 = ct->path;
5132 break;
5133 case GOT_STATUS_DELETE:
5134 label1 = ct->path;
5135 break;
5136 default:
5137 return got_error(GOT_ERR_FILE_STATUS);
5139 fd1 = got_opentempfd();
5140 if (fd1 == -1) {
5141 err = got_error_from_errno("got_opentempfd");
5142 goto done;
5144 fd2 = got_opentempfd();
5145 if (fd2 == -1) {
5146 err = got_error_from_errno("got_opentempfd");
5147 goto done;
5149 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5150 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5151 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5152 NULL, repo, diff_outfile);
5153 goto done;
5156 fd1 = got_opentempfd();
5157 if (fd1 == -1) {
5158 err = got_error_from_errno("got_opentempfd");
5159 goto done;
5162 if (ct->status != GOT_STATUS_ADD) {
5163 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5164 8192, fd1);
5165 if (err)
5166 goto done;
5169 if (ct->status != GOT_STATUS_DELETE) {
5170 if (dirfd != -1) {
5171 fd = openat(dirfd, de_name,
5172 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5173 if (fd == -1) {
5174 if (!got_err_open_nofollow_on_symlink()) {
5175 err = got_error_from_errno2("openat",
5176 ct->ondisk_path);
5177 goto done;
5179 err = get_symlink_target_file(&fd, dirfd,
5180 de_name, ct->ondisk_path);
5181 if (err)
5182 goto done;
5184 } else {
5185 fd = open(ct->ondisk_path,
5186 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5187 if (fd == -1) {
5188 if (!got_err_open_nofollow_on_symlink()) {
5189 err = got_error_from_errno2("open",
5190 ct->ondisk_path);
5191 goto done;
5193 err = get_symlink_target_file(&fd, dirfd,
5194 de_name, ct->ondisk_path);
5195 if (err)
5196 goto done;
5199 if (fstatat(fd, ct->ondisk_path, &sb,
5200 AT_SYMLINK_NOFOLLOW) == -1) {
5201 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5202 goto done;
5204 ondisk_file = fdopen(fd, "r");
5205 if (ondisk_file == NULL) {
5206 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5207 goto done;
5209 fd = -1;
5210 f2_exists = 1;
5213 if (blob1) {
5214 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5215 f1, blob1);
5216 if (err)
5217 goto done;
5220 err = got_diff_blob_file(blob1, f1, size1, label1,
5221 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5222 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5223 done:
5224 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5225 err = got_error_from_errno("close");
5226 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5227 err = got_error_from_errno("close");
5228 if (blob1)
5229 got_object_blob_close(blob1);
5230 if (fd != -1 && close(fd) == -1 && err == NULL)
5231 err = got_error_from_errno("close");
5232 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5233 err = got_error_from_errno("fclose");
5234 return err;
5237 static const struct got_error *
5238 collect_commitables(void *arg, unsigned char status,
5239 unsigned char staged_status, const char *relpath,
5240 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5241 struct got_object_id *commit_id, int dirfd, const char *de_name)
5243 struct collect_commitables_arg *a = arg;
5244 const struct got_error *err = NULL;
5245 struct got_commitable *ct = NULL;
5246 struct got_pathlist_entry *new = NULL;
5247 char *parent_path = NULL, *path = NULL;
5248 struct stat sb;
5250 if (a->have_staged_files) {
5251 if (staged_status != GOT_STATUS_MODIFY &&
5252 staged_status != GOT_STATUS_ADD &&
5253 staged_status != GOT_STATUS_DELETE)
5254 return NULL;
5255 } else {
5256 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5257 printf("C %s\n", relpath);
5258 return got_error(GOT_ERR_COMMIT_CONFLICT);
5261 if (status != GOT_STATUS_MODIFY &&
5262 status != GOT_STATUS_MODE_CHANGE &&
5263 status != GOT_STATUS_ADD &&
5264 status != GOT_STATUS_DELETE &&
5265 status != GOT_STATUS_CONFLICT)
5266 return NULL;
5269 if (asprintf(&path, "/%s", relpath) == -1) {
5270 err = got_error_from_errno("asprintf");
5271 goto done;
5273 if (strcmp(path, "/") == 0) {
5274 parent_path = strdup("");
5275 if (parent_path == NULL)
5276 return got_error_from_errno("strdup");
5277 } else {
5278 err = got_path_dirname(&parent_path, path);
5279 if (err)
5280 return err;
5283 ct = calloc(1, sizeof(*ct));
5284 if (ct == NULL) {
5285 err = got_error_from_errno("calloc");
5286 goto done;
5289 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5290 relpath) == -1) {
5291 err = got_error_from_errno("asprintf");
5292 goto done;
5295 if (staged_status == GOT_STATUS_ADD ||
5296 staged_status == GOT_STATUS_MODIFY) {
5297 struct got_fileindex_entry *ie;
5298 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5299 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5300 case GOT_FILEIDX_MODE_REGULAR_FILE:
5301 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5302 ct->mode = S_IFREG;
5303 break;
5304 case GOT_FILEIDX_MODE_SYMLINK:
5305 ct->mode = S_IFLNK;
5306 break;
5307 default:
5308 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5309 goto done;
5311 ct->mode |= got_fileindex_entry_perms_get(ie);
5312 } else if (status != GOT_STATUS_DELETE &&
5313 staged_status != GOT_STATUS_DELETE) {
5314 if (dirfd != -1) {
5315 if (fstatat(dirfd, de_name, &sb,
5316 AT_SYMLINK_NOFOLLOW) == -1) {
5317 err = got_error_from_errno2("fstatat",
5318 ct->ondisk_path);
5319 goto done;
5321 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5322 err = got_error_from_errno2("lstat", ct->ondisk_path);
5323 goto done;
5325 ct->mode = sb.st_mode;
5328 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5329 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5330 relpath) == -1) {
5331 err = got_error_from_errno("asprintf");
5332 goto done;
5335 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5336 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5337 int is_bad_symlink;
5338 char target_path[PATH_MAX];
5339 ssize_t target_len;
5340 target_len = readlink(ct->ondisk_path, target_path,
5341 sizeof(target_path));
5342 if (target_len == -1) {
5343 err = got_error_from_errno2("readlink",
5344 ct->ondisk_path);
5345 goto done;
5347 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5348 target_len, ct->ondisk_path, a->worktree->root_path);
5349 if (err)
5350 goto done;
5351 if (is_bad_symlink) {
5352 err = got_error_path(ct->ondisk_path,
5353 GOT_ERR_BAD_SYMLINK);
5354 goto done;
5359 ct->status = status;
5360 ct->staged_status = staged_status;
5361 ct->blob_id = NULL; /* will be filled in when blob gets created */
5362 if (ct->status != GOT_STATUS_ADD &&
5363 ct->staged_status != GOT_STATUS_ADD) {
5364 ct->base_blob_id = got_object_id_dup(blob_id);
5365 if (ct->base_blob_id == NULL) {
5366 err = got_error_from_errno("got_object_id_dup");
5367 goto done;
5369 ct->base_commit_id = got_object_id_dup(commit_id);
5370 if (ct->base_commit_id == NULL) {
5371 err = got_error_from_errno("got_object_id_dup");
5372 goto done;
5375 if (ct->staged_status == GOT_STATUS_ADD ||
5376 ct->staged_status == GOT_STATUS_MODIFY) {
5377 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5378 if (ct->staged_blob_id == NULL) {
5379 err = got_error_from_errno("got_object_id_dup");
5380 goto done;
5383 ct->path = strdup(path);
5384 if (ct->path == NULL) {
5385 err = got_error_from_errno("strdup");
5386 goto done;
5388 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5389 if (err)
5390 goto done;
5392 if (a->diff_outfile && ct && new != NULL) {
5393 err = append_ct_diff(ct, &a->diff_header_shown,
5394 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5395 a->have_staged_files, a->repo, a->worktree);
5396 if (err)
5397 goto done;
5399 done:
5400 if (ct && (err || new == NULL))
5401 free_commitable(ct);
5402 free(parent_path);
5403 free(path);
5404 return err;
5407 static const struct got_error *write_tree(struct got_object_id **, int *,
5408 struct got_tree_object *, const char *, struct got_pathlist_head *,
5409 got_worktree_status_cb status_cb, void *status_arg,
5410 struct got_repository *);
5412 static const struct got_error *
5413 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5414 struct got_tree_entry *te, const char *parent_path,
5415 struct got_pathlist_head *commitable_paths,
5416 got_worktree_status_cb status_cb, void *status_arg,
5417 struct got_repository *repo)
5419 const struct got_error *err = NULL;
5420 struct got_tree_object *subtree;
5421 char *subpath;
5423 if (asprintf(&subpath, "%s%s%s", parent_path,
5424 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5425 return got_error_from_errno("asprintf");
5427 err = got_object_open_as_tree(&subtree, repo, &te->id);
5428 if (err)
5429 return err;
5431 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5432 commitable_paths, status_cb, status_arg, repo);
5433 got_object_tree_close(subtree);
5434 free(subpath);
5435 return err;
5438 static const struct got_error *
5439 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5441 const struct got_error *err = NULL;
5442 char *ct_parent_path = NULL;
5444 *match = 0;
5446 if (strchr(ct->in_repo_path, '/') == NULL) {
5447 *match = got_path_is_root_dir(path);
5448 return NULL;
5451 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5452 if (err)
5453 return err;
5454 *match = (strcmp(path, ct_parent_path) == 0);
5455 free(ct_parent_path);
5456 return err;
5459 static mode_t
5460 get_ct_file_mode(struct got_commitable *ct)
5462 if (S_ISLNK(ct->mode))
5463 return S_IFLNK;
5465 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5468 static const struct got_error *
5469 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5470 struct got_tree_entry *te, struct got_commitable *ct)
5472 const struct got_error *err = NULL;
5474 *new_te = NULL;
5476 err = got_object_tree_entry_dup(new_te, te);
5477 if (err)
5478 goto done;
5480 (*new_te)->mode = get_ct_file_mode(ct);
5482 if (ct->staged_status == GOT_STATUS_MODIFY)
5483 memcpy(&(*new_te)->id, ct->staged_blob_id,
5484 sizeof((*new_te)->id));
5485 else
5486 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5487 done:
5488 if (err && *new_te) {
5489 free(*new_te);
5490 *new_te = NULL;
5492 return err;
5495 static const struct got_error *
5496 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5497 struct got_commitable *ct)
5499 const struct got_error *err = NULL;
5500 char *ct_name = NULL;
5502 *new_te = NULL;
5504 *new_te = calloc(1, sizeof(**new_te));
5505 if (*new_te == NULL)
5506 return got_error_from_errno("calloc");
5508 err = got_path_basename(&ct_name, ct->path);
5509 if (err)
5510 goto done;
5511 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5512 sizeof((*new_te)->name)) {
5513 err = got_error(GOT_ERR_NO_SPACE);
5514 goto done;
5517 (*new_te)->mode = get_ct_file_mode(ct);
5519 if (ct->staged_status == GOT_STATUS_ADD)
5520 memcpy(&(*new_te)->id, ct->staged_blob_id,
5521 sizeof((*new_te)->id));
5522 else
5523 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5524 done:
5525 free(ct_name);
5526 if (err && *new_te) {
5527 free(*new_te);
5528 *new_te = NULL;
5530 return err;
5533 static const struct got_error *
5534 insert_tree_entry(struct got_tree_entry *new_te,
5535 struct got_pathlist_head *paths)
5537 const struct got_error *err = NULL;
5538 struct got_pathlist_entry *new_pe;
5540 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5541 if (err)
5542 return err;
5543 if (new_pe == NULL)
5544 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5545 return NULL;
5548 static const struct got_error *
5549 report_ct_status(struct got_commitable *ct,
5550 got_worktree_status_cb status_cb, void *status_arg)
5552 const char *ct_path = ct->path;
5553 unsigned char status;
5555 if (status_cb == NULL) /* no commit progress output desired */
5556 return NULL;
5558 while (ct_path[0] == '/')
5559 ct_path++;
5561 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5562 status = ct->staged_status;
5563 else
5564 status = ct->status;
5566 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5567 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5570 static const struct got_error *
5571 match_modified_subtree(int *modified, struct got_tree_entry *te,
5572 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5574 const struct got_error *err = NULL;
5575 struct got_pathlist_entry *pe;
5576 char *te_path;
5578 *modified = 0;
5580 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5581 got_path_is_root_dir(base_tree_path) ? "" : "/",
5582 te->name) == -1)
5583 return got_error_from_errno("asprintf");
5585 TAILQ_FOREACH(pe, commitable_paths, entry) {
5586 struct got_commitable *ct = pe->data;
5587 *modified = got_path_is_child(ct->in_repo_path, te_path,
5588 strlen(te_path));
5589 if (*modified)
5590 break;
5593 free(te_path);
5594 return err;
5597 static const struct got_error *
5598 match_deleted_or_modified_ct(struct got_commitable **ctp,
5599 struct got_tree_entry *te, const char *base_tree_path,
5600 struct got_pathlist_head *commitable_paths)
5602 const struct got_error *err = NULL;
5603 struct got_pathlist_entry *pe;
5605 *ctp = NULL;
5607 TAILQ_FOREACH(pe, commitable_paths, entry) {
5608 struct got_commitable *ct = pe->data;
5609 char *ct_name = NULL;
5610 int path_matches;
5612 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5613 if (ct->status != GOT_STATUS_MODIFY &&
5614 ct->status != GOT_STATUS_MODE_CHANGE &&
5615 ct->status != GOT_STATUS_DELETE &&
5616 ct->status != GOT_STATUS_CONFLICT)
5617 continue;
5618 } else {
5619 if (ct->staged_status != GOT_STATUS_MODIFY &&
5620 ct->staged_status != GOT_STATUS_DELETE)
5621 continue;
5624 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5625 continue;
5627 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5628 if (err)
5629 return err;
5630 if (!path_matches)
5631 continue;
5633 err = got_path_basename(&ct_name, pe->path);
5634 if (err)
5635 return err;
5637 if (strcmp(te->name, ct_name) != 0) {
5638 free(ct_name);
5639 continue;
5641 free(ct_name);
5643 *ctp = ct;
5644 break;
5647 return err;
5650 static const struct got_error *
5651 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5652 const char *child_path, const char *path_base_tree,
5653 struct got_pathlist_head *commitable_paths,
5654 got_worktree_status_cb status_cb, void *status_arg,
5655 struct got_repository *repo)
5657 const struct got_error *err = NULL;
5658 struct got_tree_entry *new_te;
5659 char *subtree_path;
5660 struct got_object_id *id = NULL;
5661 int nentries;
5663 *new_tep = NULL;
5665 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5666 got_path_is_root_dir(path_base_tree) ? "" : "/",
5667 child_path) == -1)
5668 return got_error_from_errno("asprintf");
5670 new_te = calloc(1, sizeof(*new_te));
5671 if (new_te == NULL)
5672 return got_error_from_errno("calloc");
5673 new_te->mode = S_IFDIR;
5675 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5676 sizeof(new_te->name)) {
5677 err = got_error(GOT_ERR_NO_SPACE);
5678 goto done;
5680 err = write_tree(&id, &nentries, NULL, subtree_path,
5681 commitable_paths, status_cb, status_arg, repo);
5682 if (err) {
5683 free(new_te);
5684 goto done;
5686 memcpy(&new_te->id, id, sizeof(new_te->id));
5687 done:
5688 free(id);
5689 free(subtree_path);
5690 if (err == NULL)
5691 *new_tep = new_te;
5692 return err;
5695 static const struct got_error *
5696 write_tree(struct got_object_id **new_tree_id, int *nentries,
5697 struct got_tree_object *base_tree, const char *path_base_tree,
5698 struct got_pathlist_head *commitable_paths,
5699 got_worktree_status_cb status_cb, void *status_arg,
5700 struct got_repository *repo)
5702 const struct got_error *err = NULL;
5703 struct got_pathlist_head paths;
5704 struct got_tree_entry *te, *new_te = NULL;
5705 struct got_pathlist_entry *pe;
5707 TAILQ_INIT(&paths);
5708 *nentries = 0;
5710 /* Insert, and recurse into, newly added entries first. */
5711 TAILQ_FOREACH(pe, commitable_paths, entry) {
5712 struct got_commitable *ct = pe->data;
5713 char *child_path = NULL, *slash;
5715 if ((ct->status != GOT_STATUS_ADD &&
5716 ct->staged_status != GOT_STATUS_ADD) ||
5717 (ct->flags & GOT_COMMITABLE_ADDED))
5718 continue;
5720 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5721 strlen(path_base_tree)))
5722 continue;
5724 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5725 ct->in_repo_path);
5726 if (err)
5727 goto done;
5729 slash = strchr(child_path, '/');
5730 if (slash == NULL) {
5731 err = alloc_added_blob_tree_entry(&new_te, ct);
5732 if (err)
5733 goto done;
5734 err = report_ct_status(ct, status_cb, status_arg);
5735 if (err)
5736 goto done;
5737 ct->flags |= GOT_COMMITABLE_ADDED;
5738 err = insert_tree_entry(new_te, &paths);
5739 if (err)
5740 goto done;
5741 (*nentries)++;
5742 } else {
5743 *slash = '\0'; /* trim trailing path components */
5744 if (base_tree == NULL ||
5745 got_object_tree_find_entry(base_tree, child_path)
5746 == NULL) {
5747 err = make_subtree_for_added_blob(&new_te,
5748 child_path, path_base_tree,
5749 commitable_paths, status_cb, status_arg,
5750 repo);
5751 if (err)
5752 goto done;
5753 err = insert_tree_entry(new_te, &paths);
5754 if (err)
5755 goto done;
5756 (*nentries)++;
5761 if (base_tree) {
5762 int i, nbase_entries;
5763 /* Handle modified and deleted entries. */
5764 nbase_entries = got_object_tree_get_nentries(base_tree);
5765 for (i = 0; i < nbase_entries; i++) {
5766 struct got_commitable *ct = NULL;
5768 te = got_object_tree_get_entry(base_tree, i);
5769 if (got_object_tree_entry_is_submodule(te)) {
5770 /* Entry is a submodule; just copy it. */
5771 err = got_object_tree_entry_dup(&new_te, te);
5772 if (err)
5773 goto done;
5774 err = insert_tree_entry(new_te, &paths);
5775 if (err)
5776 goto done;
5777 (*nentries)++;
5778 continue;
5781 if (S_ISDIR(te->mode)) {
5782 int modified;
5783 err = got_object_tree_entry_dup(&new_te, te);
5784 if (err)
5785 goto done;
5786 err = match_modified_subtree(&modified, te,
5787 path_base_tree, commitable_paths);
5788 if (err)
5789 goto done;
5790 /* Avoid recursion into unmodified subtrees. */
5791 if (modified) {
5792 struct got_object_id *new_id;
5793 int nsubentries;
5794 err = write_subtree(&new_id,
5795 &nsubentries, te,
5796 path_base_tree, commitable_paths,
5797 status_cb, status_arg, repo);
5798 if (err)
5799 goto done;
5800 if (nsubentries == 0) {
5801 /* All entries were deleted. */
5802 free(new_id);
5803 continue;
5805 memcpy(&new_te->id, new_id,
5806 sizeof(new_te->id));
5807 free(new_id);
5809 err = insert_tree_entry(new_te, &paths);
5810 if (err)
5811 goto done;
5812 (*nentries)++;
5813 continue;
5816 err = match_deleted_or_modified_ct(&ct, te,
5817 path_base_tree, commitable_paths);
5818 if (err)
5819 goto done;
5820 if (ct) {
5821 /* NB: Deleted entries get dropped here. */
5822 if (ct->status == GOT_STATUS_MODIFY ||
5823 ct->status == GOT_STATUS_MODE_CHANGE ||
5824 ct->status == GOT_STATUS_CONFLICT ||
5825 ct->staged_status == GOT_STATUS_MODIFY) {
5826 err = alloc_modified_blob_tree_entry(
5827 &new_te, te, ct);
5828 if (err)
5829 goto done;
5830 err = insert_tree_entry(new_te, &paths);
5831 if (err)
5832 goto done;
5833 (*nentries)++;
5835 err = report_ct_status(ct, status_cb,
5836 status_arg);
5837 if (err)
5838 goto done;
5839 } else {
5840 /* Entry is unchanged; just copy it. */
5841 err = got_object_tree_entry_dup(&new_te, te);
5842 if (err)
5843 goto done;
5844 err = insert_tree_entry(new_te, &paths);
5845 if (err)
5846 goto done;
5847 (*nentries)++;
5852 /* Write new list of entries; deleted entries have been dropped. */
5853 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5854 done:
5855 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5856 return err;
5859 static const struct got_error *
5860 update_fileindex_after_commit(struct got_worktree *worktree,
5861 struct got_pathlist_head *commitable_paths,
5862 struct got_object_id *new_base_commit_id,
5863 struct got_fileindex *fileindex, int have_staged_files)
5865 const struct got_error *err = NULL;
5866 struct got_pathlist_entry *pe;
5867 char *relpath = NULL;
5869 TAILQ_FOREACH(pe, commitable_paths, entry) {
5870 struct got_fileindex_entry *ie;
5871 struct got_commitable *ct = pe->data;
5873 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5875 err = got_path_skip_common_ancestor(&relpath,
5876 worktree->root_path, ct->ondisk_path);
5877 if (err)
5878 goto done;
5880 if (ie) {
5881 if (ct->status == GOT_STATUS_DELETE ||
5882 ct->staged_status == GOT_STATUS_DELETE) {
5883 got_fileindex_entry_remove(fileindex, ie);
5884 } else if (ct->staged_status == GOT_STATUS_ADD ||
5885 ct->staged_status == GOT_STATUS_MODIFY) {
5886 got_fileindex_entry_stage_set(ie,
5887 GOT_FILEIDX_STAGE_NONE);
5888 got_fileindex_entry_staged_filetype_set(ie, 0);
5890 err = got_fileindex_entry_update(ie,
5891 worktree->root_fd, relpath,
5892 ct->staged_blob_id->sha1,
5893 new_base_commit_id->sha1,
5894 !have_staged_files);
5895 } else
5896 err = got_fileindex_entry_update(ie,
5897 worktree->root_fd, relpath,
5898 ct->blob_id->sha1,
5899 new_base_commit_id->sha1,
5900 !have_staged_files);
5901 } else {
5902 err = got_fileindex_entry_alloc(&ie, pe->path);
5903 if (err)
5904 goto done;
5905 err = got_fileindex_entry_update(ie,
5906 worktree->root_fd, relpath, ct->blob_id->sha1,
5907 new_base_commit_id->sha1, 1);
5908 if (err) {
5909 got_fileindex_entry_free(ie);
5910 goto done;
5912 err = got_fileindex_entry_add(fileindex, ie);
5913 if (err) {
5914 got_fileindex_entry_free(ie);
5915 goto done;
5918 free(relpath);
5919 relpath = NULL;
5921 done:
5922 free(relpath);
5923 return err;
5927 static const struct got_error *
5928 check_out_of_date(const char *in_repo_path, unsigned char status,
5929 unsigned char staged_status, struct got_object_id *base_blob_id,
5930 struct got_object_id *base_commit_id,
5931 struct got_object_id *head_commit_id, struct got_repository *repo,
5932 int ood_errcode)
5934 const struct got_error *err = NULL;
5935 struct got_commit_object *commit = NULL;
5936 struct got_object_id *id = NULL;
5938 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5939 /* Trivial case: base commit == head commit */
5940 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5941 return NULL;
5943 * Ensure file content which local changes were based
5944 * on matches file content in the branch head.
5946 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5947 if (err)
5948 goto done;
5949 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5950 if (err) {
5951 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5952 err = got_error(ood_errcode);
5953 goto done;
5954 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5955 err = got_error(ood_errcode);
5956 } else {
5957 /* Require that added files don't exist in the branch head. */
5958 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5959 if (err)
5960 goto done;
5961 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5962 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5963 goto done;
5964 err = id ? got_error(ood_errcode) : NULL;
5966 done:
5967 free(id);
5968 if (commit)
5969 got_object_commit_close(commit);
5970 return err;
5973 static const struct got_error *
5974 commit_worktree(struct got_object_id **new_commit_id,
5975 struct got_pathlist_head *commitable_paths,
5976 struct got_object_id *head_commit_id,
5977 struct got_object_id *parent_id2,
5978 struct got_worktree *worktree,
5979 const char *author, const char *committer, char *diff_path,
5980 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5981 got_worktree_status_cb status_cb, void *status_arg,
5982 struct got_repository *repo)
5984 const struct got_error *err = NULL, *unlockerr = NULL;
5985 struct got_pathlist_entry *pe;
5986 const char *head_ref_name = NULL;
5987 struct got_commit_object *head_commit = NULL;
5988 struct got_reference *head_ref2 = NULL;
5989 struct got_object_id *head_commit_id2 = NULL;
5990 struct got_tree_object *head_tree = NULL;
5991 struct got_object_id *new_tree_id = NULL;
5992 int nentries, nparents = 0;
5993 struct got_object_id_queue parent_ids;
5994 struct got_object_qid *pid = NULL;
5995 char *logmsg = NULL;
5996 time_t timestamp;
5998 *new_commit_id = NULL;
6000 STAILQ_INIT(&parent_ids);
6002 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6003 if (err)
6004 goto done;
6006 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6007 if (err)
6008 goto done;
6010 if (commit_msg_cb != NULL) {
6011 err = commit_msg_cb(commitable_paths, diff_path,
6012 &logmsg, commit_arg);
6013 if (err)
6014 goto done;
6017 if (logmsg == NULL || strlen(logmsg) == 0) {
6018 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6019 goto done;
6022 /* Create blobs from added and modified files and record their IDs. */
6023 TAILQ_FOREACH(pe, commitable_paths, entry) {
6024 struct got_commitable *ct = pe->data;
6025 char *ondisk_path;
6027 /* Blobs for staged files already exist. */
6028 if (ct->staged_status == GOT_STATUS_ADD ||
6029 ct->staged_status == GOT_STATUS_MODIFY)
6030 continue;
6032 if (ct->status != GOT_STATUS_ADD &&
6033 ct->status != GOT_STATUS_MODIFY &&
6034 ct->status != GOT_STATUS_MODE_CHANGE &&
6035 ct->status != GOT_STATUS_CONFLICT)
6036 continue;
6038 if (asprintf(&ondisk_path, "%s/%s",
6039 worktree->root_path, pe->path) == -1) {
6040 err = got_error_from_errno("asprintf");
6041 goto done;
6043 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6044 free(ondisk_path);
6045 if (err)
6046 goto done;
6049 /* Recursively write new tree objects. */
6050 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6051 commitable_paths, status_cb, status_arg, repo);
6052 if (err)
6053 goto done;
6055 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6056 if (err)
6057 goto done;
6058 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6059 nparents++;
6060 if (parent_id2) {
6061 err = got_object_qid_alloc(&pid, parent_id2);
6062 if (err)
6063 goto done;
6064 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6065 nparents++;
6067 timestamp = time(NULL);
6068 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6069 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6070 if (logmsg != NULL)
6071 free(logmsg);
6072 if (err)
6073 goto done;
6075 /* Check if a concurrent commit to our branch has occurred. */
6076 head_ref_name = got_worktree_get_head_ref_name(worktree);
6077 if (head_ref_name == NULL) {
6078 err = got_error_from_errno("got_worktree_get_head_ref_name");
6079 goto done;
6081 /* Lock the reference here to prevent concurrent modification. */
6082 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6083 if (err)
6084 goto done;
6085 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6086 if (err)
6087 goto done;
6088 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6089 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6090 goto done;
6092 /* Update branch head in repository. */
6093 err = got_ref_change_ref(head_ref2, *new_commit_id);
6094 if (err)
6095 goto done;
6096 err = got_ref_write(head_ref2, repo);
6097 if (err)
6098 goto done;
6100 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6101 if (err)
6102 goto done;
6104 err = ref_base_commit(worktree, repo);
6105 if (err)
6106 goto done;
6107 done:
6108 got_object_id_queue_free(&parent_ids);
6109 if (head_tree)
6110 got_object_tree_close(head_tree);
6111 if (head_commit)
6112 got_object_commit_close(head_commit);
6113 free(head_commit_id2);
6114 if (head_ref2) {
6115 unlockerr = got_ref_unlock(head_ref2);
6116 if (unlockerr && err == NULL)
6117 err = unlockerr;
6118 got_ref_close(head_ref2);
6120 return err;
6123 static const struct got_error *
6124 check_path_is_commitable(const char *path,
6125 struct got_pathlist_head *commitable_paths)
6127 struct got_pathlist_entry *cpe = NULL;
6128 size_t path_len = strlen(path);
6130 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6131 struct got_commitable *ct = cpe->data;
6132 const char *ct_path = ct->path;
6134 while (ct_path[0] == '/')
6135 ct_path++;
6137 if (strcmp(path, ct_path) == 0 ||
6138 got_path_is_child(ct_path, path, path_len))
6139 break;
6142 if (cpe == NULL)
6143 return got_error_path(path, GOT_ERR_BAD_PATH);
6145 return NULL;
6148 static const struct got_error *
6149 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6151 int *have_staged_files = arg;
6153 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6154 *have_staged_files = 1;
6155 return got_error(GOT_ERR_CANCELLED);
6158 return NULL;
6161 static const struct got_error *
6162 check_non_staged_files(struct got_fileindex *fileindex,
6163 struct got_pathlist_head *paths)
6165 struct got_pathlist_entry *pe;
6166 struct got_fileindex_entry *ie;
6168 TAILQ_FOREACH(pe, paths, entry) {
6169 if (pe->path[0] == '\0')
6170 continue;
6171 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6172 if (ie == NULL)
6173 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6174 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6175 return got_error_path(pe->path,
6176 GOT_ERR_FILE_NOT_STAGED);
6179 return NULL;
6182 const struct got_error *
6183 got_worktree_commit(struct got_object_id **new_commit_id,
6184 struct got_worktree *worktree, struct got_pathlist_head *paths,
6185 const char *author, const char *committer, int allow_bad_symlinks,
6186 int show_diff, int commit_conflicts,
6187 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6188 got_worktree_status_cb status_cb, void *status_arg,
6189 struct got_repository *repo)
6191 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6192 struct got_fileindex *fileindex = NULL;
6193 char *fileindex_path = NULL;
6194 struct got_pathlist_head commitable_paths;
6195 struct collect_commitables_arg cc_arg;
6196 struct got_pathlist_entry *pe;
6197 struct got_reference *head_ref = NULL;
6198 struct got_object_id *head_commit_id = NULL;
6199 char *diff_path = NULL;
6200 int have_staged_files = 0;
6202 *new_commit_id = NULL;
6204 memset(&cc_arg, 0, sizeof(cc_arg));
6205 TAILQ_INIT(&commitable_paths);
6207 err = lock_worktree(worktree, LOCK_EX);
6208 if (err)
6209 goto done;
6211 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6212 if (err)
6213 goto done;
6215 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6216 if (err)
6217 goto done;
6219 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6220 if (err)
6221 goto done;
6223 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6224 &have_staged_files);
6225 if (err && err->code != GOT_ERR_CANCELLED)
6226 goto done;
6227 if (have_staged_files) {
6228 err = check_non_staged_files(fileindex, paths);
6229 if (err)
6230 goto done;
6233 cc_arg.commitable_paths = &commitable_paths;
6234 cc_arg.worktree = worktree;
6235 cc_arg.fileindex = fileindex;
6236 cc_arg.repo = repo;
6237 cc_arg.have_staged_files = have_staged_files;
6238 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6239 cc_arg.diff_header_shown = 0;
6240 cc_arg.commit_conflicts = commit_conflicts;
6241 if (show_diff) {
6242 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6243 GOT_TMPDIR_STR "/got", ".diff");
6244 if (err)
6245 goto done;
6246 cc_arg.f1 = got_opentemp();
6247 if (cc_arg.f1 == NULL) {
6248 err = got_error_from_errno("got_opentemp");
6249 goto done;
6251 cc_arg.f2 = got_opentemp();
6252 if (cc_arg.f2 == NULL) {
6253 err = got_error_from_errno("got_opentemp");
6254 goto done;
6258 TAILQ_FOREACH(pe, paths, entry) {
6259 err = worktree_status(worktree, pe->path, fileindex, repo,
6260 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6261 if (err)
6262 goto done;
6265 if (show_diff) {
6266 if (fflush(cc_arg.diff_outfile) == EOF) {
6267 err = got_error_from_errno("fflush");
6268 goto done;
6272 if (TAILQ_EMPTY(&commitable_paths)) {
6273 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6274 goto done;
6277 TAILQ_FOREACH(pe, paths, entry) {
6278 err = check_path_is_commitable(pe->path, &commitable_paths);
6279 if (err)
6280 goto done;
6283 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6284 struct got_commitable *ct = pe->data;
6285 const char *ct_path = ct->in_repo_path;
6287 while (ct_path[0] == '/')
6288 ct_path++;
6289 err = check_out_of_date(ct_path, ct->status,
6290 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6291 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6292 if (err)
6293 goto done;
6297 err = commit_worktree(new_commit_id, &commitable_paths,
6298 head_commit_id, NULL, worktree, author, committer,
6299 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6300 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6301 if (err)
6302 goto done;
6304 err = update_fileindex_after_commit(worktree, &commitable_paths,
6305 *new_commit_id, fileindex, have_staged_files);
6306 sync_err = sync_fileindex(fileindex, fileindex_path);
6307 if (sync_err && err == NULL)
6308 err = sync_err;
6309 done:
6310 if (fileindex)
6311 got_fileindex_free(fileindex);
6312 free(fileindex_path);
6313 unlockerr = lock_worktree(worktree, LOCK_SH);
6314 if (unlockerr && err == NULL)
6315 err = unlockerr;
6316 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6317 struct got_commitable *ct = pe->data;
6319 free_commitable(ct);
6321 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6322 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6323 err = got_error_from_errno2("unlink", diff_path);
6324 free(diff_path);
6325 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6326 err == NULL)
6327 err = got_error_from_errno("fclose");
6328 return err;
6331 const char *
6332 got_commitable_get_path(struct got_commitable *ct)
6334 return ct->path;
6337 unsigned int
6338 got_commitable_get_status(struct got_commitable *ct)
6340 return ct->status;
6343 struct check_rebase_ok_arg {
6344 struct got_worktree *worktree;
6345 struct got_repository *repo;
6348 static const struct got_error *
6349 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6351 const struct got_error *err = NULL;
6352 struct check_rebase_ok_arg *a = arg;
6353 unsigned char status;
6354 struct stat sb;
6355 char *ondisk_path;
6357 /* Reject rebase of a work tree with mixed base commits. */
6358 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6359 SHA1_DIGEST_LENGTH))
6360 return got_error(GOT_ERR_MIXED_COMMITS);
6362 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6363 == -1)
6364 return got_error_from_errno("asprintf");
6366 /* Reject rebase of a work tree with modified or staged files. */
6367 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6368 free(ondisk_path);
6369 if (err)
6370 return err;
6372 if (status != GOT_STATUS_NO_CHANGE)
6373 return got_error(GOT_ERR_MODIFIED);
6374 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6375 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6377 return NULL;
6380 const struct got_error *
6381 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6382 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6383 struct got_worktree *worktree, struct got_reference *branch,
6384 struct got_repository *repo)
6386 const struct got_error *err = NULL;
6387 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6388 char *branch_ref_name = NULL;
6389 char *fileindex_path = NULL;
6390 struct check_rebase_ok_arg ok_arg;
6391 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6392 struct got_object_id *wt_branch_tip = NULL;
6394 *new_base_branch_ref = NULL;
6395 *tmp_branch = NULL;
6396 *fileindex = NULL;
6398 err = lock_worktree(worktree, LOCK_EX);
6399 if (err)
6400 return err;
6402 err = open_fileindex(fileindex, &fileindex_path, worktree);
6403 if (err)
6404 goto done;
6406 ok_arg.worktree = worktree;
6407 ok_arg.repo = repo;
6408 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6409 &ok_arg);
6410 if (err)
6411 goto done;
6413 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6414 if (err)
6415 goto done;
6417 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6418 if (err)
6419 goto done;
6421 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6422 if (err)
6423 goto done;
6425 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6426 0);
6427 if (err)
6428 goto done;
6430 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6431 if (err)
6432 goto done;
6433 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6434 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6435 goto done;
6438 err = got_ref_alloc_symref(new_base_branch_ref,
6439 new_base_branch_ref_name, wt_branch);
6440 if (err)
6441 goto done;
6442 err = got_ref_write(*new_base_branch_ref, repo);
6443 if (err)
6444 goto done;
6446 /* TODO Lock original branch's ref while rebasing? */
6448 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6449 if (err)
6450 goto done;
6452 err = got_ref_write(branch_ref, repo);
6453 if (err)
6454 goto done;
6456 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6457 worktree->base_commit_id);
6458 if (err)
6459 goto done;
6460 err = got_ref_write(*tmp_branch, repo);
6461 if (err)
6462 goto done;
6464 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6465 if (err)
6466 goto done;
6467 done:
6468 free(fileindex_path);
6469 free(tmp_branch_name);
6470 free(new_base_branch_ref_name);
6471 free(branch_ref_name);
6472 if (branch_ref)
6473 got_ref_close(branch_ref);
6474 if (wt_branch)
6475 got_ref_close(wt_branch);
6476 free(wt_branch_tip);
6477 if (err) {
6478 if (*new_base_branch_ref) {
6479 got_ref_close(*new_base_branch_ref);
6480 *new_base_branch_ref = NULL;
6482 if (*tmp_branch) {
6483 got_ref_close(*tmp_branch);
6484 *tmp_branch = NULL;
6486 if (*fileindex) {
6487 got_fileindex_free(*fileindex);
6488 *fileindex = NULL;
6490 lock_worktree(worktree, LOCK_SH);
6492 return err;
6495 const struct got_error *
6496 got_worktree_rebase_continue(struct got_object_id **commit_id,
6497 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6498 struct got_reference **branch, struct got_fileindex **fileindex,
6499 struct got_worktree *worktree, struct got_repository *repo)
6501 const struct got_error *err;
6502 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6503 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6504 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6505 char *fileindex_path = NULL;
6506 int have_staged_files = 0;
6508 *commit_id = NULL;
6509 *new_base_branch = NULL;
6510 *tmp_branch = NULL;
6511 *branch = NULL;
6512 *fileindex = NULL;
6514 err = lock_worktree(worktree, LOCK_EX);
6515 if (err)
6516 return err;
6518 err = open_fileindex(fileindex, &fileindex_path, worktree);
6519 if (err)
6520 goto done;
6522 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6523 &have_staged_files);
6524 if (err && err->code != GOT_ERR_CANCELLED)
6525 goto done;
6526 if (have_staged_files) {
6527 err = got_error(GOT_ERR_STAGED_PATHS);
6528 goto done;
6531 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6532 if (err)
6533 goto done;
6535 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6536 if (err)
6537 goto done;
6539 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6540 if (err)
6541 goto done;
6543 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6544 if (err)
6545 goto done;
6547 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6548 if (err)
6549 goto done;
6551 err = got_ref_open(branch, repo,
6552 got_ref_get_symref_target(branch_ref), 0);
6553 if (err)
6554 goto done;
6556 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6557 if (err)
6558 goto done;
6560 err = got_ref_resolve(commit_id, repo, commit_ref);
6561 if (err)
6562 goto done;
6564 err = got_ref_open(new_base_branch, repo,
6565 new_base_branch_ref_name, 0);
6566 if (err)
6567 goto done;
6569 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6570 if (err)
6571 goto done;
6572 done:
6573 free(commit_ref_name);
6574 free(branch_ref_name);
6575 free(fileindex_path);
6576 if (commit_ref)
6577 got_ref_close(commit_ref);
6578 if (branch_ref)
6579 got_ref_close(branch_ref);
6580 if (err) {
6581 free(*commit_id);
6582 *commit_id = NULL;
6583 if (*tmp_branch) {
6584 got_ref_close(*tmp_branch);
6585 *tmp_branch = NULL;
6587 if (*new_base_branch) {
6588 got_ref_close(*new_base_branch);
6589 *new_base_branch = NULL;
6591 if (*branch) {
6592 got_ref_close(*branch);
6593 *branch = NULL;
6595 if (*fileindex) {
6596 got_fileindex_free(*fileindex);
6597 *fileindex = NULL;
6599 lock_worktree(worktree, LOCK_SH);
6601 return err;
6604 const struct got_error *
6605 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6607 const struct got_error *err;
6608 char *tmp_branch_name = NULL;
6610 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6611 if (err)
6612 return err;
6614 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6615 free(tmp_branch_name);
6616 return NULL;
6619 static const struct got_error *
6620 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6621 const char *diff_path, char **logmsg, void *arg)
6623 *logmsg = arg;
6624 return NULL;
6627 static const struct got_error *
6628 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6629 const char *path, struct got_object_id *blob_id,
6630 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6631 int dirfd, const char *de_name)
6633 return NULL;
6636 struct collect_merged_paths_arg {
6637 got_worktree_checkout_cb progress_cb;
6638 void *progress_arg;
6639 struct got_pathlist_head *merged_paths;
6642 static const struct got_error *
6643 collect_merged_paths(void *arg, unsigned char status, const char *path)
6645 const struct got_error *err;
6646 struct collect_merged_paths_arg *a = arg;
6647 char *p;
6648 struct got_pathlist_entry *new;
6650 err = (*a->progress_cb)(a->progress_arg, status, path);
6651 if (err)
6652 return err;
6654 if (status != GOT_STATUS_MERGE &&
6655 status != GOT_STATUS_ADD &&
6656 status != GOT_STATUS_DELETE &&
6657 status != GOT_STATUS_CONFLICT)
6658 return NULL;
6660 p = strdup(path);
6661 if (p == NULL)
6662 return got_error_from_errno("strdup");
6664 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6665 if (err || new == NULL)
6666 free(p);
6667 return err;
6670 static const struct got_error *
6671 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6672 int is_rebase, struct got_repository *repo)
6674 const struct got_error *err;
6675 struct got_reference *commit_ref = NULL;
6677 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6678 if (err) {
6679 if (err->code != GOT_ERR_NOT_REF)
6680 goto done;
6681 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6682 if (err)
6683 goto done;
6684 err = got_ref_write(commit_ref, repo);
6685 if (err)
6686 goto done;
6687 } else if (is_rebase) {
6688 struct got_object_id *stored_id;
6689 int cmp;
6691 err = got_ref_resolve(&stored_id, repo, commit_ref);
6692 if (err)
6693 goto done;
6694 cmp = got_object_id_cmp(commit_id, stored_id);
6695 free(stored_id);
6696 if (cmp != 0) {
6697 err = got_error(GOT_ERR_REBASE_COMMITID);
6698 goto done;
6701 done:
6702 if (commit_ref)
6703 got_ref_close(commit_ref);
6704 return err;
6707 static const struct got_error *
6708 rebase_merge_files(struct got_pathlist_head *merged_paths,
6709 const char *commit_ref_name, struct got_worktree *worktree,
6710 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6711 struct got_object_id *commit_id, struct got_repository *repo,
6712 got_worktree_checkout_cb progress_cb, void *progress_arg,
6713 got_cancel_cb cancel_cb, void *cancel_arg)
6715 const struct got_error *err;
6716 struct got_reference *commit_ref = NULL;
6717 struct collect_merged_paths_arg cmp_arg;
6718 char *fileindex_path;
6720 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6722 err = get_fileindex_path(&fileindex_path, worktree);
6723 if (err)
6724 return err;
6726 cmp_arg.progress_cb = progress_cb;
6727 cmp_arg.progress_arg = progress_arg;
6728 cmp_arg.merged_paths = merged_paths;
6729 err = merge_files(worktree, fileindex, fileindex_path,
6730 parent_commit_id, commit_id, repo, collect_merged_paths,
6731 &cmp_arg, cancel_cb, cancel_arg);
6732 if (commit_ref)
6733 got_ref_close(commit_ref);
6734 return err;
6737 const struct got_error *
6738 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6739 struct got_worktree *worktree, struct got_fileindex *fileindex,
6740 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6741 struct got_repository *repo,
6742 got_worktree_checkout_cb progress_cb, void *progress_arg,
6743 got_cancel_cb cancel_cb, void *cancel_arg)
6745 const struct got_error *err;
6746 char *commit_ref_name;
6748 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6749 if (err)
6750 return err;
6752 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6753 if (err)
6754 goto done;
6756 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6757 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6758 progress_arg, cancel_cb, cancel_arg);
6759 done:
6760 free(commit_ref_name);
6761 return err;
6764 const struct got_error *
6765 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6766 struct got_worktree *worktree, struct got_fileindex *fileindex,
6767 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6768 struct got_repository *repo,
6769 got_worktree_checkout_cb progress_cb, void *progress_arg,
6770 got_cancel_cb cancel_cb, void *cancel_arg)
6772 const struct got_error *err;
6773 char *commit_ref_name;
6775 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6776 if (err)
6777 return err;
6779 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6780 if (err)
6781 goto done;
6783 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6784 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6785 progress_arg, cancel_cb, cancel_arg);
6786 done:
6787 free(commit_ref_name);
6788 return err;
6791 static const struct got_error *
6792 rebase_commit(struct got_object_id **new_commit_id,
6793 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6794 struct got_worktree *worktree, struct got_fileindex *fileindex,
6795 struct got_reference *tmp_branch, const char *committer,
6796 struct got_commit_object *orig_commit, const char *new_logmsg,
6797 int allow_conflict, struct got_repository *repo)
6799 const struct got_error *err, *sync_err;
6800 struct got_pathlist_head commitable_paths;
6801 struct collect_commitables_arg cc_arg;
6802 char *fileindex_path = NULL;
6803 struct got_reference *head_ref = NULL;
6804 struct got_object_id *head_commit_id = NULL;
6805 char *logmsg = NULL;
6807 memset(&cc_arg, 0, sizeof(cc_arg));
6808 TAILQ_INIT(&commitable_paths);
6809 *new_commit_id = NULL;
6811 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6813 err = get_fileindex_path(&fileindex_path, worktree);
6814 if (err)
6815 return err;
6817 cc_arg.commitable_paths = &commitable_paths;
6818 cc_arg.worktree = worktree;
6819 cc_arg.repo = repo;
6820 cc_arg.have_staged_files = 0;
6821 cc_arg.commit_conflicts = allow_conflict;
6823 * If possible get the status of individual files directly to
6824 * avoid crawling the entire work tree once per rebased commit.
6826 * Ideally, merged_paths would contain a list of commitables
6827 * we could use so we could skip worktree_status() entirely.
6828 * However, we would then need carefully keep track of cumulative
6829 * effects of operations such as file additions and deletions
6830 * in 'got histedit -f' (folding multiple commits into one),
6831 * and this extra complexity is not really worth it.
6833 if (merged_paths) {
6834 struct got_pathlist_entry *pe;
6835 TAILQ_FOREACH(pe, merged_paths, entry) {
6836 err = worktree_status(worktree, pe->path, fileindex,
6837 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6838 0);
6839 if (err)
6840 goto done;
6842 } else {
6843 err = worktree_status(worktree, "", fileindex, repo,
6844 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6845 if (err)
6846 goto done;
6849 if (TAILQ_EMPTY(&commitable_paths)) {
6850 /* No-op change; commit will be elided. */
6851 err = got_ref_delete(commit_ref, repo);
6852 if (err)
6853 goto done;
6854 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6855 goto done;
6858 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6859 if (err)
6860 goto done;
6862 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6863 if (err)
6864 goto done;
6866 if (new_logmsg) {
6867 logmsg = strdup(new_logmsg);
6868 if (logmsg == NULL) {
6869 err = got_error_from_errno("strdup");
6870 goto done;
6872 } else {
6873 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6874 if (err)
6875 goto done;
6878 /* NB: commit_worktree will call free(logmsg) */
6879 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6880 NULL, worktree, got_object_commit_get_author(orig_commit),
6881 committer ? committer :
6882 got_object_commit_get_committer(orig_commit), NULL,
6883 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6884 if (err)
6885 goto done;
6887 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6888 if (err)
6889 goto done;
6891 err = got_ref_delete(commit_ref, repo);
6892 if (err)
6893 goto done;
6895 err = update_fileindex_after_commit(worktree, &commitable_paths,
6896 *new_commit_id, fileindex, 0);
6897 sync_err = sync_fileindex(fileindex, fileindex_path);
6898 if (sync_err && err == NULL)
6899 err = sync_err;
6900 done:
6901 free(fileindex_path);
6902 free(head_commit_id);
6903 if (head_ref)
6904 got_ref_close(head_ref);
6905 if (err) {
6906 free(*new_commit_id);
6907 *new_commit_id = NULL;
6909 return err;
6912 const struct got_error *
6913 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6914 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6915 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6916 const char *committer, struct got_commit_object *orig_commit,
6917 struct got_object_id *orig_commit_id, int allow_conflict,
6918 struct got_repository *repo)
6920 const struct got_error *err;
6921 char *commit_ref_name;
6922 struct got_reference *commit_ref = NULL;
6923 struct got_object_id *commit_id = NULL;
6925 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6926 if (err)
6927 return err;
6929 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6930 if (err)
6931 goto done;
6932 err = got_ref_resolve(&commit_id, repo, commit_ref);
6933 if (err)
6934 goto done;
6935 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6936 err = got_error(GOT_ERR_REBASE_COMMITID);
6937 goto done;
6940 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6941 worktree, fileindex, tmp_branch, committer, orig_commit,
6942 NULL, allow_conflict, repo);
6943 done:
6944 if (commit_ref)
6945 got_ref_close(commit_ref);
6946 free(commit_ref_name);
6947 free(commit_id);
6948 return err;
6951 const struct got_error *
6952 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6953 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6954 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6955 const char *committer, struct got_commit_object *orig_commit,
6956 struct got_object_id *orig_commit_id, const char *new_logmsg,
6957 int allow_conflict, struct got_repository *repo)
6959 const struct got_error *err;
6960 char *commit_ref_name;
6961 struct got_reference *commit_ref = NULL;
6963 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6964 if (err)
6965 return err;
6967 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6968 if (err)
6969 goto done;
6971 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6972 worktree, fileindex, tmp_branch, committer, orig_commit,
6973 new_logmsg, allow_conflict, repo);
6974 done:
6975 if (commit_ref)
6976 got_ref_close(commit_ref);
6977 free(commit_ref_name);
6978 return err;
6981 const struct got_error *
6982 got_worktree_rebase_postpone(struct got_worktree *worktree,
6983 struct got_fileindex *fileindex)
6985 if (fileindex)
6986 got_fileindex_free(fileindex);
6987 return lock_worktree(worktree, LOCK_SH);
6990 static const struct got_error *
6991 delete_ref(const char *name, struct got_repository *repo)
6993 const struct got_error *err;
6994 struct got_reference *ref;
6996 err = got_ref_open(&ref, repo, name, 0);
6997 if (err) {
6998 if (err->code == GOT_ERR_NOT_REF)
6999 return NULL;
7000 return err;
7003 err = got_ref_delete(ref, repo);
7004 got_ref_close(ref);
7005 return err;
7008 static const struct got_error *
7009 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7011 const struct got_error *err;
7012 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7013 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7015 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7016 if (err)
7017 goto done;
7018 err = delete_ref(tmp_branch_name, repo);
7019 if (err)
7020 goto done;
7022 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7023 if (err)
7024 goto done;
7025 err = delete_ref(new_base_branch_ref_name, repo);
7026 if (err)
7027 goto done;
7029 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7030 if (err)
7031 goto done;
7032 err = delete_ref(branch_ref_name, repo);
7033 if (err)
7034 goto done;
7036 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7037 if (err)
7038 goto done;
7039 err = delete_ref(commit_ref_name, repo);
7040 if (err)
7041 goto done;
7043 done:
7044 free(tmp_branch_name);
7045 free(new_base_branch_ref_name);
7046 free(branch_ref_name);
7047 free(commit_ref_name);
7048 return err;
7051 static const struct got_error *
7052 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7053 struct got_object_id *new_commit_id, struct got_repository *repo)
7055 const struct got_error *err;
7056 struct got_reference *ref = NULL;
7057 struct got_object_id *old_commit_id = NULL;
7058 const char *branch_name = NULL;
7059 char *new_id_str = NULL;
7060 char *refname = NULL;
7062 branch_name = got_ref_get_name(branch);
7063 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7064 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7065 branch_name += 11;
7067 err = got_object_id_str(&new_id_str, new_commit_id);
7068 if (err)
7069 return err;
7071 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7072 new_id_str) == -1) {
7073 err = got_error_from_errno("asprintf");
7074 goto done;
7077 err = got_ref_resolve(&old_commit_id, repo, branch);
7078 if (err)
7079 goto done;
7081 err = got_ref_alloc(&ref, refname, old_commit_id);
7082 if (err)
7083 goto done;
7085 err = got_ref_write(ref, repo);
7086 done:
7087 free(new_id_str);
7088 free(refname);
7089 free(old_commit_id);
7090 if (ref)
7091 got_ref_close(ref);
7092 return err;
7095 const struct got_error *
7096 got_worktree_rebase_complete(struct got_worktree *worktree,
7097 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7098 struct got_reference *rebased_branch, struct got_repository *repo,
7099 int create_backup)
7101 const struct got_error *err, *unlockerr, *sync_err;
7102 struct got_object_id *new_head_commit_id = NULL;
7103 char *fileindex_path = NULL;
7105 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7106 if (err)
7107 return err;
7109 if (create_backup) {
7110 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7111 rebased_branch, new_head_commit_id, repo);
7112 if (err)
7113 goto done;
7116 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7117 if (err)
7118 goto done;
7120 err = got_ref_write(rebased_branch, repo);
7121 if (err)
7122 goto done;
7124 err = got_worktree_set_head_ref(worktree, rebased_branch);
7125 if (err)
7126 goto done;
7128 err = delete_rebase_refs(worktree, repo);
7129 if (err)
7130 goto done;
7132 err = get_fileindex_path(&fileindex_path, worktree);
7133 if (err)
7134 goto done;
7135 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7136 sync_err = sync_fileindex(fileindex, fileindex_path);
7137 if (sync_err && err == NULL)
7138 err = sync_err;
7139 done:
7140 got_fileindex_free(fileindex);
7141 free(fileindex_path);
7142 free(new_head_commit_id);
7143 unlockerr = lock_worktree(worktree, LOCK_SH);
7144 if (unlockerr && err == NULL)
7145 err = unlockerr;
7146 return err;
7149 const struct got_error *
7150 got_worktree_rebase_abort(struct got_worktree *worktree,
7151 struct got_fileindex *fileindex, struct got_repository *repo,
7152 struct got_reference *new_base_branch,
7153 got_worktree_checkout_cb progress_cb, void *progress_arg)
7155 const struct got_error *err, *unlockerr, *sync_err;
7156 struct got_reference *resolved = NULL;
7157 struct got_object_id *commit_id = NULL;
7158 struct got_commit_object *commit = NULL;
7159 char *fileindex_path = NULL;
7160 struct revert_file_args rfa;
7161 struct got_object_id *tree_id = NULL;
7163 err = lock_worktree(worktree, LOCK_EX);
7164 if (err)
7165 return err;
7167 err = got_object_open_as_commit(&commit, repo,
7168 worktree->base_commit_id);
7169 if (err)
7170 goto done;
7172 err = got_ref_open(&resolved, repo,
7173 got_ref_get_symref_target(new_base_branch), 0);
7174 if (err)
7175 goto done;
7177 err = got_worktree_set_head_ref(worktree, resolved);
7178 if (err)
7179 goto done;
7182 * XXX commits to the base branch could have happened while
7183 * we were busy rebasing; should we store the original commit ID
7184 * when rebase begins and read it back here?
7186 err = got_ref_resolve(&commit_id, repo, resolved);
7187 if (err)
7188 goto done;
7190 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7191 if (err)
7192 goto done;
7194 err = got_object_id_by_path(&tree_id, repo, commit,
7195 worktree->path_prefix);
7196 if (err)
7197 goto done;
7199 err = delete_rebase_refs(worktree, repo);
7200 if (err)
7201 goto done;
7203 err = get_fileindex_path(&fileindex_path, worktree);
7204 if (err)
7205 goto done;
7207 rfa.worktree = worktree;
7208 rfa.fileindex = fileindex;
7209 rfa.progress_cb = progress_cb;
7210 rfa.progress_arg = progress_arg;
7211 rfa.patch_cb = NULL;
7212 rfa.patch_arg = NULL;
7213 rfa.repo = repo;
7214 rfa.unlink_added_files = 0;
7215 err = worktree_status(worktree, "", fileindex, repo,
7216 revert_file, &rfa, NULL, NULL, 1, 0);
7217 if (err)
7218 goto sync;
7220 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7221 repo, progress_cb, progress_arg, NULL, NULL);
7222 sync:
7223 sync_err = sync_fileindex(fileindex, fileindex_path);
7224 if (sync_err && err == NULL)
7225 err = sync_err;
7226 done:
7227 got_ref_close(resolved);
7228 free(tree_id);
7229 free(commit_id);
7230 if (commit)
7231 got_object_commit_close(commit);
7232 if (fileindex)
7233 got_fileindex_free(fileindex);
7234 free(fileindex_path);
7236 unlockerr = lock_worktree(worktree, LOCK_SH);
7237 if (unlockerr && err == NULL)
7238 err = unlockerr;
7239 return err;
7242 const struct got_error *
7243 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7244 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7245 struct got_fileindex **fileindex, struct got_worktree *worktree,
7246 struct got_repository *repo)
7248 const struct got_error *err = NULL;
7249 char *tmp_branch_name = NULL;
7250 char *branch_ref_name = NULL;
7251 char *base_commit_ref_name = NULL;
7252 char *fileindex_path = NULL;
7253 struct check_rebase_ok_arg ok_arg;
7254 struct got_reference *wt_branch = NULL;
7255 struct got_reference *base_commit_ref = NULL;
7257 *tmp_branch = NULL;
7258 *branch_ref = NULL;
7259 *base_commit_id = NULL;
7260 *fileindex = NULL;
7262 err = lock_worktree(worktree, LOCK_EX);
7263 if (err)
7264 return err;
7266 err = open_fileindex(fileindex, &fileindex_path, worktree);
7267 if (err)
7268 goto done;
7270 ok_arg.worktree = worktree;
7271 ok_arg.repo = repo;
7272 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7273 &ok_arg);
7274 if (err)
7275 goto done;
7277 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7278 if (err)
7279 goto done;
7281 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7282 if (err)
7283 goto done;
7285 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7286 worktree);
7287 if (err)
7288 goto done;
7290 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7291 0);
7292 if (err)
7293 goto done;
7295 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7296 if (err)
7297 goto done;
7299 err = got_ref_write(*branch_ref, repo);
7300 if (err)
7301 goto done;
7303 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7304 worktree->base_commit_id);
7305 if (err)
7306 goto done;
7307 err = got_ref_write(base_commit_ref, repo);
7308 if (err)
7309 goto done;
7310 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7311 if (*base_commit_id == NULL) {
7312 err = got_error_from_errno("got_object_id_dup");
7313 goto done;
7316 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7317 worktree->base_commit_id);
7318 if (err)
7319 goto done;
7320 err = got_ref_write(*tmp_branch, repo);
7321 if (err)
7322 goto done;
7324 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7325 if (err)
7326 goto done;
7327 done:
7328 free(fileindex_path);
7329 free(tmp_branch_name);
7330 free(branch_ref_name);
7331 free(base_commit_ref_name);
7332 if (wt_branch)
7333 got_ref_close(wt_branch);
7334 if (err) {
7335 if (*branch_ref) {
7336 got_ref_close(*branch_ref);
7337 *branch_ref = NULL;
7339 if (*tmp_branch) {
7340 got_ref_close(*tmp_branch);
7341 *tmp_branch = NULL;
7343 free(*base_commit_id);
7344 if (*fileindex) {
7345 got_fileindex_free(*fileindex);
7346 *fileindex = NULL;
7348 lock_worktree(worktree, LOCK_SH);
7350 return err;
7353 const struct got_error *
7354 got_worktree_histedit_postpone(struct got_worktree *worktree,
7355 struct got_fileindex *fileindex)
7357 if (fileindex)
7358 got_fileindex_free(fileindex);
7359 return lock_worktree(worktree, LOCK_SH);
7362 const struct got_error *
7363 got_worktree_histedit_in_progress(int *in_progress,
7364 struct got_worktree *worktree)
7366 const struct got_error *err;
7367 char *tmp_branch_name = NULL;
7369 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7370 if (err)
7371 return err;
7373 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7374 free(tmp_branch_name);
7375 return NULL;
7378 const struct got_error *
7379 got_worktree_histedit_continue(struct got_object_id **commit_id,
7380 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7381 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7382 struct got_worktree *worktree, struct got_repository *repo)
7384 const struct got_error *err;
7385 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7386 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7387 struct got_reference *commit_ref = NULL;
7388 struct got_reference *base_commit_ref = NULL;
7389 char *fileindex_path = NULL;
7390 int have_staged_files = 0;
7392 *commit_id = NULL;
7393 *tmp_branch = NULL;
7394 *base_commit_id = NULL;
7395 *fileindex = NULL;
7397 err = lock_worktree(worktree, LOCK_EX);
7398 if (err)
7399 return err;
7401 err = open_fileindex(fileindex, &fileindex_path, worktree);
7402 if (err)
7403 goto done;
7405 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7406 &have_staged_files);
7407 if (err && err->code != GOT_ERR_CANCELLED)
7408 goto done;
7409 if (have_staged_files) {
7410 err = got_error(GOT_ERR_STAGED_PATHS);
7411 goto done;
7414 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7415 if (err)
7416 goto done;
7418 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7419 if (err)
7420 goto done;
7422 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7423 if (err)
7424 goto done;
7426 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7427 worktree);
7428 if (err)
7429 goto done;
7431 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7432 if (err)
7433 goto done;
7435 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7436 if (err)
7437 goto done;
7438 err = got_ref_resolve(commit_id, repo, commit_ref);
7439 if (err)
7440 goto done;
7442 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7443 if (err)
7444 goto done;
7445 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7446 if (err)
7447 goto done;
7449 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7450 if (err)
7451 goto done;
7452 done:
7453 free(commit_ref_name);
7454 free(branch_ref_name);
7455 free(fileindex_path);
7456 if (commit_ref)
7457 got_ref_close(commit_ref);
7458 if (base_commit_ref)
7459 got_ref_close(base_commit_ref);
7460 if (err) {
7461 free(*commit_id);
7462 *commit_id = NULL;
7463 free(*base_commit_id);
7464 *base_commit_id = NULL;
7465 if (*tmp_branch) {
7466 got_ref_close(*tmp_branch);
7467 *tmp_branch = NULL;
7469 if (*fileindex) {
7470 got_fileindex_free(*fileindex);
7471 *fileindex = NULL;
7473 lock_worktree(worktree, LOCK_EX);
7475 return err;
7478 static const struct got_error *
7479 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7481 const struct got_error *err;
7482 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7483 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7485 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7486 if (err)
7487 goto done;
7488 err = delete_ref(tmp_branch_name, repo);
7489 if (err)
7490 goto done;
7492 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7493 worktree);
7494 if (err)
7495 goto done;
7496 err = delete_ref(base_commit_ref_name, repo);
7497 if (err)
7498 goto done;
7500 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7501 if (err)
7502 goto done;
7503 err = delete_ref(branch_ref_name, repo);
7504 if (err)
7505 goto done;
7507 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7508 if (err)
7509 goto done;
7510 err = delete_ref(commit_ref_name, repo);
7511 if (err)
7512 goto done;
7513 done:
7514 free(tmp_branch_name);
7515 free(base_commit_ref_name);
7516 free(branch_ref_name);
7517 free(commit_ref_name);
7518 return err;
7521 const struct got_error *
7522 got_worktree_histedit_abort(struct got_worktree *worktree,
7523 struct got_fileindex *fileindex, struct got_repository *repo,
7524 struct got_reference *branch, struct got_object_id *base_commit_id,
7525 got_worktree_checkout_cb progress_cb, void *progress_arg)
7527 const struct got_error *err, *unlockerr, *sync_err;
7528 struct got_reference *resolved = NULL;
7529 char *fileindex_path = NULL;
7530 struct got_commit_object *commit = NULL;
7531 struct got_object_id *tree_id = NULL;
7532 struct revert_file_args rfa;
7534 err = lock_worktree(worktree, LOCK_EX);
7535 if (err)
7536 return err;
7538 err = got_object_open_as_commit(&commit, repo,
7539 worktree->base_commit_id);
7540 if (err)
7541 goto done;
7543 err = got_ref_open(&resolved, repo,
7544 got_ref_get_symref_target(branch), 0);
7545 if (err)
7546 goto done;
7548 err = got_worktree_set_head_ref(worktree, resolved);
7549 if (err)
7550 goto done;
7552 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7553 if (err)
7554 goto done;
7556 err = got_object_id_by_path(&tree_id, repo, commit,
7557 worktree->path_prefix);
7558 if (err)
7559 goto done;
7561 err = delete_histedit_refs(worktree, repo);
7562 if (err)
7563 goto done;
7565 err = get_fileindex_path(&fileindex_path, worktree);
7566 if (err)
7567 goto done;
7569 rfa.worktree = worktree;
7570 rfa.fileindex = fileindex;
7571 rfa.progress_cb = progress_cb;
7572 rfa.progress_arg = progress_arg;
7573 rfa.patch_cb = NULL;
7574 rfa.patch_arg = NULL;
7575 rfa.repo = repo;
7576 rfa.unlink_added_files = 0;
7577 err = worktree_status(worktree, "", fileindex, repo,
7578 revert_file, &rfa, NULL, NULL, 1, 0);
7579 if (err)
7580 goto sync;
7582 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7583 repo, progress_cb, progress_arg, NULL, NULL);
7584 sync:
7585 sync_err = sync_fileindex(fileindex, fileindex_path);
7586 if (sync_err && err == NULL)
7587 err = sync_err;
7588 done:
7589 got_ref_close(resolved);
7590 free(tree_id);
7591 free(fileindex_path);
7593 unlockerr = lock_worktree(worktree, LOCK_SH);
7594 if (unlockerr && err == NULL)
7595 err = unlockerr;
7596 return err;
7599 const struct got_error *
7600 got_worktree_histedit_complete(struct got_worktree *worktree,
7601 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7602 struct got_reference *edited_branch, struct got_repository *repo)
7604 const struct got_error *err, *unlockerr, *sync_err;
7605 struct got_object_id *new_head_commit_id = NULL;
7606 struct got_reference *resolved = NULL;
7607 char *fileindex_path = NULL;
7609 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7610 if (err)
7611 return err;
7613 err = got_ref_open(&resolved, repo,
7614 got_ref_get_symref_target(edited_branch), 0);
7615 if (err)
7616 goto done;
7618 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7619 resolved, new_head_commit_id, repo);
7620 if (err)
7621 goto done;
7623 err = got_ref_change_ref(resolved, new_head_commit_id);
7624 if (err)
7625 goto done;
7627 err = got_ref_write(resolved, repo);
7628 if (err)
7629 goto done;
7631 err = got_worktree_set_head_ref(worktree, resolved);
7632 if (err)
7633 goto done;
7635 err = delete_histedit_refs(worktree, repo);
7636 if (err)
7637 goto done;
7639 err = get_fileindex_path(&fileindex_path, worktree);
7640 if (err)
7641 goto done;
7642 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7643 sync_err = sync_fileindex(fileindex, fileindex_path);
7644 if (sync_err && err == NULL)
7645 err = sync_err;
7646 done:
7647 got_fileindex_free(fileindex);
7648 free(fileindex_path);
7649 free(new_head_commit_id);
7650 unlockerr = lock_worktree(worktree, LOCK_SH);
7651 if (unlockerr && err == NULL)
7652 err = unlockerr;
7653 return err;
7656 const struct got_error *
7657 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7658 struct got_object_id *commit_id, struct got_repository *repo)
7660 const struct got_error *err;
7661 char *commit_ref_name;
7663 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7664 if (err)
7665 return err;
7667 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7668 if (err)
7669 goto done;
7671 err = delete_ref(commit_ref_name, repo);
7672 done:
7673 free(commit_ref_name);
7674 return err;
7677 const struct got_error *
7678 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7679 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7680 struct got_worktree *worktree, const char *refname,
7681 struct got_repository *repo)
7683 const struct got_error *err = NULL;
7684 char *fileindex_path = NULL;
7685 struct check_rebase_ok_arg ok_arg;
7687 *fileindex = NULL;
7688 *branch_ref = NULL;
7689 *base_branch_ref = NULL;
7691 err = lock_worktree(worktree, LOCK_EX);
7692 if (err)
7693 return err;
7695 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7696 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7697 "cannot integrate a branch into itself; "
7698 "update -b or different branch name required");
7699 goto done;
7702 err = open_fileindex(fileindex, &fileindex_path, worktree);
7703 if (err)
7704 goto done;
7706 /* Preconditions are the same as for rebase. */
7707 ok_arg.worktree = worktree;
7708 ok_arg.repo = repo;
7709 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7710 &ok_arg);
7711 if (err)
7712 goto done;
7714 err = got_ref_open(branch_ref, repo, refname, 1);
7715 if (err)
7716 goto done;
7718 err = got_ref_open(base_branch_ref, repo,
7719 got_worktree_get_head_ref_name(worktree), 1);
7720 done:
7721 if (err) {
7722 if (*branch_ref) {
7723 got_ref_close(*branch_ref);
7724 *branch_ref = NULL;
7726 if (*base_branch_ref) {
7727 got_ref_close(*base_branch_ref);
7728 *base_branch_ref = NULL;
7730 if (*fileindex) {
7731 got_fileindex_free(*fileindex);
7732 *fileindex = NULL;
7734 lock_worktree(worktree, LOCK_SH);
7736 return err;
7739 const struct got_error *
7740 got_worktree_integrate_continue(struct got_worktree *worktree,
7741 struct got_fileindex *fileindex, struct got_repository *repo,
7742 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7743 got_worktree_checkout_cb progress_cb, void *progress_arg,
7744 got_cancel_cb cancel_cb, void *cancel_arg)
7746 const struct got_error *err = NULL, *sync_err, *unlockerr;
7747 char *fileindex_path = NULL;
7748 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7749 struct got_commit_object *commit = NULL;
7751 err = get_fileindex_path(&fileindex_path, worktree);
7752 if (err)
7753 goto done;
7755 err = got_ref_resolve(&commit_id, repo, branch_ref);
7756 if (err)
7757 goto done;
7759 err = got_object_open_as_commit(&commit, repo, commit_id);
7760 if (err)
7761 goto done;
7763 err = got_object_id_by_path(&tree_id, repo, commit,
7764 worktree->path_prefix);
7765 if (err)
7766 goto done;
7768 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7769 if (err)
7770 goto done;
7772 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7773 progress_cb, progress_arg, cancel_cb, cancel_arg);
7774 if (err)
7775 goto sync;
7777 err = got_ref_change_ref(base_branch_ref, commit_id);
7778 if (err)
7779 goto sync;
7781 err = got_ref_write(base_branch_ref, repo);
7782 if (err)
7783 goto sync;
7785 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7786 sync:
7787 sync_err = sync_fileindex(fileindex, fileindex_path);
7788 if (sync_err && err == NULL)
7789 err = sync_err;
7791 done:
7792 unlockerr = got_ref_unlock(branch_ref);
7793 if (unlockerr && err == NULL)
7794 err = unlockerr;
7795 got_ref_close(branch_ref);
7797 unlockerr = got_ref_unlock(base_branch_ref);
7798 if (unlockerr && err == NULL)
7799 err = unlockerr;
7800 got_ref_close(base_branch_ref);
7802 got_fileindex_free(fileindex);
7803 free(fileindex_path);
7804 free(tree_id);
7805 if (commit)
7806 got_object_commit_close(commit);
7808 unlockerr = lock_worktree(worktree, LOCK_SH);
7809 if (unlockerr && err == NULL)
7810 err = unlockerr;
7811 return err;
7814 const struct got_error *
7815 got_worktree_integrate_abort(struct got_worktree *worktree,
7816 struct got_fileindex *fileindex, struct got_repository *repo,
7817 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7819 const struct got_error *err = NULL, *unlockerr = NULL;
7821 got_fileindex_free(fileindex);
7823 err = lock_worktree(worktree, LOCK_SH);
7825 unlockerr = got_ref_unlock(branch_ref);
7826 if (unlockerr && err == NULL)
7827 err = unlockerr;
7828 got_ref_close(branch_ref);
7830 unlockerr = got_ref_unlock(base_branch_ref);
7831 if (unlockerr && err == NULL)
7832 err = unlockerr;
7833 got_ref_close(base_branch_ref);
7835 return err;
7838 const struct got_error *
7839 got_worktree_merge_postpone(struct got_worktree *worktree,
7840 struct got_fileindex *fileindex)
7842 const struct got_error *err, *sync_err;
7843 char *fileindex_path = NULL;
7845 err = get_fileindex_path(&fileindex_path, worktree);
7846 if (err)
7847 goto done;
7849 sync_err = sync_fileindex(fileindex, fileindex_path);
7851 err = lock_worktree(worktree, LOCK_SH);
7852 if (sync_err && err == NULL)
7853 err = sync_err;
7854 done:
7855 got_fileindex_free(fileindex);
7856 free(fileindex_path);
7857 return err;
7860 static const struct got_error *
7861 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7863 const struct got_error *err;
7864 char *branch_refname = NULL, *commit_refname = NULL;
7866 err = get_merge_branch_ref_name(&branch_refname, worktree);
7867 if (err)
7868 goto done;
7869 err = delete_ref(branch_refname, repo);
7870 if (err)
7871 goto done;
7873 err = get_merge_commit_ref_name(&commit_refname, worktree);
7874 if (err)
7875 goto done;
7876 err = delete_ref(commit_refname, repo);
7877 if (err)
7878 goto done;
7880 done:
7881 free(branch_refname);
7882 free(commit_refname);
7883 return err;
7886 struct merge_commit_msg_arg {
7887 struct got_worktree *worktree;
7888 const char *branch_name;
7891 static const struct got_error *
7892 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7893 const char *diff_path, char **logmsg, void *arg)
7895 struct merge_commit_msg_arg *a = arg;
7897 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7898 got_worktree_get_head_ref_name(a->worktree)) == -1)
7899 return got_error_from_errno("asprintf");
7901 return NULL;
7905 const struct got_error *
7906 got_worktree_merge_branch(struct got_worktree *worktree,
7907 struct got_fileindex *fileindex,
7908 struct got_object_id *yca_commit_id,
7909 struct got_object_id *branch_tip,
7910 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7911 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7913 const struct got_error *err;
7914 char *fileindex_path = NULL;
7916 err = get_fileindex_path(&fileindex_path, worktree);
7917 if (err)
7918 goto done;
7920 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7921 worktree);
7922 if (err)
7923 goto done;
7925 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7926 branch_tip, repo, progress_cb, progress_arg,
7927 cancel_cb, cancel_arg);
7928 done:
7929 free(fileindex_path);
7930 return err;
7933 const struct got_error *
7934 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7935 struct got_worktree *worktree, struct got_fileindex *fileindex,
7936 const char *author, const char *committer, int allow_bad_symlinks,
7937 struct got_object_id *branch_tip, const char *branch_name,
7938 int allow_conflict, struct got_repository *repo,
7939 got_worktree_status_cb status_cb, void *status_arg)
7942 const struct got_error *err = NULL, *sync_err;
7943 struct got_pathlist_head commitable_paths;
7944 struct collect_commitables_arg cc_arg;
7945 struct got_pathlist_entry *pe;
7946 struct got_reference *head_ref = NULL;
7947 struct got_object_id *head_commit_id = NULL;
7948 int have_staged_files = 0;
7949 struct merge_commit_msg_arg mcm_arg;
7950 char *fileindex_path = NULL;
7952 memset(&cc_arg, 0, sizeof(cc_arg));
7953 *new_commit_id = NULL;
7955 TAILQ_INIT(&commitable_paths);
7957 err = get_fileindex_path(&fileindex_path, worktree);
7958 if (err)
7959 goto done;
7961 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7962 if (err)
7963 goto done;
7965 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7966 if (err)
7967 goto done;
7969 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7970 &have_staged_files);
7971 if (err && err->code != GOT_ERR_CANCELLED)
7972 goto done;
7973 if (have_staged_files) {
7974 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7975 goto done;
7978 cc_arg.commitable_paths = &commitable_paths;
7979 cc_arg.worktree = worktree;
7980 cc_arg.fileindex = fileindex;
7981 cc_arg.repo = repo;
7982 cc_arg.have_staged_files = have_staged_files;
7983 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7984 cc_arg.commit_conflicts = allow_conflict;
7985 err = worktree_status(worktree, "", fileindex, repo,
7986 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7987 if (err)
7988 goto done;
7990 if (TAILQ_EMPTY(&commitable_paths)) {
7991 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7992 "merge of %s cannot proceed", branch_name);
7993 goto done;
7996 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7997 struct got_commitable *ct = pe->data;
7998 const char *ct_path = ct->in_repo_path;
8000 while (ct_path[0] == '/')
8001 ct_path++;
8002 err = check_out_of_date(ct_path, ct->status,
8003 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8004 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8005 if (err)
8006 goto done;
8010 mcm_arg.worktree = worktree;
8011 mcm_arg.branch_name = branch_name;
8012 err = commit_worktree(new_commit_id, &commitable_paths,
8013 head_commit_id, branch_tip, worktree, author, committer, NULL,
8014 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8015 if (err)
8016 goto done;
8018 err = update_fileindex_after_commit(worktree, &commitable_paths,
8019 *new_commit_id, fileindex, have_staged_files);
8020 sync_err = sync_fileindex(fileindex, fileindex_path);
8021 if (sync_err && err == NULL)
8022 err = sync_err;
8023 done:
8024 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8025 struct got_commitable *ct = pe->data;
8027 free_commitable(ct);
8029 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8030 free(fileindex_path);
8031 return err;
8034 const struct got_error *
8035 got_worktree_merge_complete(struct got_worktree *worktree,
8036 struct got_fileindex *fileindex, struct got_repository *repo)
8038 const struct got_error *err, *unlockerr, *sync_err;
8039 char *fileindex_path = NULL;
8041 err = delete_merge_refs(worktree, repo);
8042 if (err)
8043 goto done;
8045 err = get_fileindex_path(&fileindex_path, worktree);
8046 if (err)
8047 goto done;
8048 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8049 sync_err = sync_fileindex(fileindex, fileindex_path);
8050 if (sync_err && err == NULL)
8051 err = sync_err;
8052 done:
8053 got_fileindex_free(fileindex);
8054 free(fileindex_path);
8055 unlockerr = lock_worktree(worktree, LOCK_SH);
8056 if (unlockerr && err == NULL)
8057 err = unlockerr;
8058 return err;
8061 const struct got_error *
8062 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8063 struct got_repository *repo)
8065 const struct got_error *err;
8066 char *branch_refname = NULL;
8067 struct got_reference *branch_ref = NULL;
8069 *in_progress = 0;
8071 err = get_merge_branch_ref_name(&branch_refname, worktree);
8072 if (err)
8073 return err;
8074 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8075 free(branch_refname);
8076 if (err) {
8077 if (err->code != GOT_ERR_NOT_REF)
8078 return err;
8079 } else
8080 *in_progress = 1;
8082 return NULL;
8085 const struct got_error *got_worktree_merge_prepare(
8086 struct got_fileindex **fileindex, struct got_worktree *worktree,
8087 struct got_reference *branch, struct got_repository *repo)
8089 const struct got_error *err = NULL;
8090 char *fileindex_path = NULL;
8091 char *branch_refname = NULL, *commit_refname = NULL;
8092 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8093 struct got_reference *commit_ref = NULL;
8094 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8095 struct check_rebase_ok_arg ok_arg;
8097 *fileindex = NULL;
8099 err = lock_worktree(worktree, LOCK_EX);
8100 if (err)
8101 return err;
8103 err = open_fileindex(fileindex, &fileindex_path, worktree);
8104 if (err)
8105 goto done;
8107 /* Preconditions are the same as for rebase. */
8108 ok_arg.worktree = worktree;
8109 ok_arg.repo = repo;
8110 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8111 &ok_arg);
8112 if (err)
8113 goto done;
8115 err = get_merge_branch_ref_name(&branch_refname, worktree);
8116 if (err)
8117 return err;
8119 err = get_merge_commit_ref_name(&commit_refname, worktree);
8120 if (err)
8121 return err;
8123 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8124 0);
8125 if (err)
8126 goto done;
8128 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8129 if (err)
8130 goto done;
8132 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8133 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8134 goto done;
8137 err = got_ref_resolve(&branch_tip, repo, branch);
8138 if (err)
8139 goto done;
8141 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8142 if (err)
8143 goto done;
8144 err = got_ref_write(branch_ref, repo);
8145 if (err)
8146 goto done;
8148 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8149 if (err)
8150 goto done;
8151 err = got_ref_write(commit_ref, repo);
8152 if (err)
8153 goto done;
8155 done:
8156 free(branch_refname);
8157 free(commit_refname);
8158 free(fileindex_path);
8159 if (branch_ref)
8160 got_ref_close(branch_ref);
8161 if (commit_ref)
8162 got_ref_close(commit_ref);
8163 if (wt_branch)
8164 got_ref_close(wt_branch);
8165 free(wt_branch_tip);
8166 if (err) {
8167 if (*fileindex) {
8168 got_fileindex_free(*fileindex);
8169 *fileindex = NULL;
8171 lock_worktree(worktree, LOCK_SH);
8173 return err;
8176 const struct got_error *
8177 got_worktree_merge_continue(char **branch_name,
8178 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8179 struct got_worktree *worktree, struct got_repository *repo)
8181 const struct got_error *err;
8182 char *commit_refname = NULL, *branch_refname = NULL;
8183 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8184 char *fileindex_path = NULL;
8185 int have_staged_files = 0;
8187 *branch_name = NULL;
8188 *branch_tip = NULL;
8189 *fileindex = NULL;
8191 err = lock_worktree(worktree, LOCK_EX);
8192 if (err)
8193 return err;
8195 err = open_fileindex(fileindex, &fileindex_path, worktree);
8196 if (err)
8197 goto done;
8199 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8200 &have_staged_files);
8201 if (err && err->code != GOT_ERR_CANCELLED)
8202 goto done;
8203 if (have_staged_files) {
8204 err = got_error(GOT_ERR_STAGED_PATHS);
8205 goto done;
8208 err = get_merge_branch_ref_name(&branch_refname, worktree);
8209 if (err)
8210 goto done;
8212 err = get_merge_commit_ref_name(&commit_refname, worktree);
8213 if (err)
8214 goto done;
8216 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8217 if (err)
8218 goto done;
8220 if (!got_ref_is_symbolic(branch_ref)) {
8221 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8222 "%s is not a symbolic reference",
8223 got_ref_get_name(branch_ref));
8224 goto done;
8226 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8227 if (*branch_name == NULL) {
8228 err = got_error_from_errno("strdup");
8229 goto done;
8232 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8233 if (err)
8234 goto done;
8236 err = got_ref_resolve(branch_tip, repo, commit_ref);
8237 if (err)
8238 goto done;
8239 done:
8240 free(commit_refname);
8241 free(branch_refname);
8242 free(fileindex_path);
8243 if (commit_ref)
8244 got_ref_close(commit_ref);
8245 if (branch_ref)
8246 got_ref_close(branch_ref);
8247 if (err) {
8248 if (*branch_name) {
8249 free(*branch_name);
8250 *branch_name = NULL;
8252 free(*branch_tip);
8253 *branch_tip = NULL;
8254 if (*fileindex) {
8255 got_fileindex_free(*fileindex);
8256 *fileindex = NULL;
8258 lock_worktree(worktree, LOCK_SH);
8260 return err;
8263 const struct got_error *
8264 got_worktree_merge_abort(struct got_worktree *worktree,
8265 struct got_fileindex *fileindex, struct got_repository *repo,
8266 got_worktree_checkout_cb progress_cb, void *progress_arg)
8268 const struct got_error *err, *unlockerr, *sync_err;
8269 struct got_object_id *commit_id = NULL;
8270 struct got_commit_object *commit = NULL;
8271 char *fileindex_path = NULL;
8272 struct revert_file_args rfa;
8273 struct got_object_id *tree_id = NULL;
8275 err = got_object_open_as_commit(&commit, repo,
8276 worktree->base_commit_id);
8277 if (err)
8278 goto done;
8280 err = got_object_id_by_path(&tree_id, repo, commit,
8281 worktree->path_prefix);
8282 if (err)
8283 goto done;
8285 err = delete_merge_refs(worktree, repo);
8286 if (err)
8287 goto done;
8289 err = get_fileindex_path(&fileindex_path, worktree);
8290 if (err)
8291 goto done;
8293 rfa.worktree = worktree;
8294 rfa.fileindex = fileindex;
8295 rfa.progress_cb = progress_cb;
8296 rfa.progress_arg = progress_arg;
8297 rfa.patch_cb = NULL;
8298 rfa.patch_arg = NULL;
8299 rfa.repo = repo;
8300 rfa.unlink_added_files = 1;
8301 err = worktree_status(worktree, "", fileindex, repo,
8302 revert_file, &rfa, NULL, NULL, 1, 0);
8303 if (err)
8304 goto sync;
8306 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8307 repo, progress_cb, progress_arg, NULL, NULL);
8308 sync:
8309 sync_err = sync_fileindex(fileindex, fileindex_path);
8310 if (sync_err && err == NULL)
8311 err = sync_err;
8312 done:
8313 free(tree_id);
8314 free(commit_id);
8315 if (commit)
8316 got_object_commit_close(commit);
8317 if (fileindex)
8318 got_fileindex_free(fileindex);
8319 free(fileindex_path);
8321 unlockerr = lock_worktree(worktree, LOCK_SH);
8322 if (unlockerr && err == NULL)
8323 err = unlockerr;
8324 return err;
8327 struct check_stage_ok_arg {
8328 struct got_object_id *head_commit_id;
8329 struct got_worktree *worktree;
8330 struct got_fileindex *fileindex;
8331 struct got_repository *repo;
8332 int have_changes;
8335 static const struct got_error *
8336 check_stage_ok(void *arg, unsigned char status,
8337 unsigned char staged_status, const char *relpath,
8338 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8339 struct got_object_id *commit_id, int dirfd, const char *de_name)
8341 struct check_stage_ok_arg *a = arg;
8342 const struct got_error *err = NULL;
8343 struct got_fileindex_entry *ie;
8344 struct got_object_id base_commit_id;
8345 struct got_object_id *base_commit_idp = NULL;
8346 char *in_repo_path = NULL, *p;
8348 if (status == GOT_STATUS_UNVERSIONED ||
8349 status == GOT_STATUS_NO_CHANGE)
8350 return NULL;
8351 if (status == GOT_STATUS_NONEXISTENT)
8352 return got_error_set_errno(ENOENT, relpath);
8354 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8355 if (ie == NULL)
8356 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8358 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8359 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8360 relpath) == -1)
8361 return got_error_from_errno("asprintf");
8363 if (got_fileindex_entry_has_commit(ie)) {
8364 base_commit_idp = got_fileindex_entry_get_commit_id(
8365 &base_commit_id, ie);
8368 if (status == GOT_STATUS_CONFLICT) {
8369 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8370 goto done;
8371 } else if (status != GOT_STATUS_ADD &&
8372 status != GOT_STATUS_MODIFY &&
8373 status != GOT_STATUS_DELETE) {
8374 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8375 goto done;
8378 a->have_changes = 1;
8380 p = in_repo_path;
8381 while (p[0] == '/')
8382 p++;
8383 err = check_out_of_date(p, status, staged_status,
8384 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8385 GOT_ERR_STAGE_OUT_OF_DATE);
8386 done:
8387 free(in_repo_path);
8388 return err;
8391 struct stage_path_arg {
8392 struct got_worktree *worktree;
8393 struct got_fileindex *fileindex;
8394 struct got_repository *repo;
8395 got_worktree_status_cb status_cb;
8396 void *status_arg;
8397 got_worktree_patch_cb patch_cb;
8398 void *patch_arg;
8399 int staged_something;
8400 int allow_bad_symlinks;
8403 static const struct got_error *
8404 stage_path(void *arg, unsigned char status,
8405 unsigned char staged_status, const char *relpath,
8406 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8407 struct got_object_id *commit_id, int dirfd, const char *de_name)
8409 struct stage_path_arg *a = arg;
8410 const struct got_error *err = NULL;
8411 struct got_fileindex_entry *ie;
8412 char *ondisk_path = NULL, *path_content = NULL;
8413 uint32_t stage;
8414 struct got_object_id *new_staged_blob_id = NULL;
8415 struct stat sb;
8417 if (status == GOT_STATUS_UNVERSIONED)
8418 return NULL;
8420 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8421 if (ie == NULL)
8422 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8424 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8425 relpath)== -1)
8426 return got_error_from_errno("asprintf");
8428 switch (status) {
8429 case GOT_STATUS_ADD:
8430 case GOT_STATUS_MODIFY:
8431 /* XXX could sb.st_mode be passed in by our caller? */
8432 if (lstat(ondisk_path, &sb) == -1) {
8433 err = got_error_from_errno2("lstat", ondisk_path);
8434 break;
8436 if (a->patch_cb) {
8437 if (status == GOT_STATUS_ADD) {
8438 int choice = GOT_PATCH_CHOICE_NONE;
8439 err = (*a->patch_cb)(&choice, a->patch_arg,
8440 status, ie->path, NULL, 1, 1);
8441 if (err)
8442 break;
8443 if (choice != GOT_PATCH_CHOICE_YES)
8444 break;
8445 } else {
8446 err = create_patched_content(&path_content, 0,
8447 staged_blob_id ? staged_blob_id : blob_id,
8448 ondisk_path, dirfd, de_name, ie->path,
8449 a->repo, a->patch_cb, a->patch_arg);
8450 if (err || path_content == NULL)
8451 break;
8454 err = got_object_blob_create(&new_staged_blob_id,
8455 path_content ? path_content : ondisk_path, a->repo);
8456 if (err)
8457 break;
8458 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8459 SHA1_DIGEST_LENGTH);
8460 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8461 stage = GOT_FILEIDX_STAGE_ADD;
8462 else
8463 stage = GOT_FILEIDX_STAGE_MODIFY;
8464 got_fileindex_entry_stage_set(ie, stage);
8465 if (S_ISLNK(sb.st_mode)) {
8466 int is_bad_symlink = 0;
8467 if (!a->allow_bad_symlinks) {
8468 char target_path[PATH_MAX];
8469 ssize_t target_len;
8470 target_len = readlink(ondisk_path, target_path,
8471 sizeof(target_path));
8472 if (target_len == -1) {
8473 err = got_error_from_errno2("readlink",
8474 ondisk_path);
8475 break;
8477 err = is_bad_symlink_target(&is_bad_symlink,
8478 target_path, target_len, ondisk_path,
8479 a->worktree->root_path);
8480 if (err)
8481 break;
8482 if (is_bad_symlink) {
8483 err = got_error_path(ondisk_path,
8484 GOT_ERR_BAD_SYMLINK);
8485 break;
8488 if (is_bad_symlink)
8489 got_fileindex_entry_staged_filetype_set(ie,
8490 GOT_FILEIDX_MODE_BAD_SYMLINK);
8491 else
8492 got_fileindex_entry_staged_filetype_set(ie,
8493 GOT_FILEIDX_MODE_SYMLINK);
8494 } else {
8495 got_fileindex_entry_staged_filetype_set(ie,
8496 GOT_FILEIDX_MODE_REGULAR_FILE);
8498 a->staged_something = 1;
8499 if (a->status_cb == NULL)
8500 break;
8501 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8502 get_staged_status(ie), relpath, blob_id,
8503 new_staged_blob_id, NULL, dirfd, de_name);
8504 if (err)
8505 break;
8507 * When staging the reverse of the staged diff,
8508 * implicitly unstage the file.
8510 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8511 sizeof(ie->blob_sha1)) == 0) {
8512 got_fileindex_entry_stage_set(ie,
8513 GOT_FILEIDX_STAGE_NONE);
8515 break;
8516 case GOT_STATUS_DELETE:
8517 if (staged_status == GOT_STATUS_DELETE)
8518 break;
8519 if (a->patch_cb) {
8520 int choice = GOT_PATCH_CHOICE_NONE;
8521 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8522 ie->path, NULL, 1, 1);
8523 if (err)
8524 break;
8525 if (choice == GOT_PATCH_CHOICE_NO)
8526 break;
8527 if (choice != GOT_PATCH_CHOICE_YES) {
8528 err = got_error(GOT_ERR_PATCH_CHOICE);
8529 break;
8532 stage = GOT_FILEIDX_STAGE_DELETE;
8533 got_fileindex_entry_stage_set(ie, stage);
8534 a->staged_something = 1;
8535 if (a->status_cb == NULL)
8536 break;
8537 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8538 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8539 de_name);
8540 break;
8541 case GOT_STATUS_NO_CHANGE:
8542 break;
8543 case GOT_STATUS_CONFLICT:
8544 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8545 break;
8546 case GOT_STATUS_NONEXISTENT:
8547 err = got_error_set_errno(ENOENT, relpath);
8548 break;
8549 default:
8550 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8551 break;
8554 if (path_content && unlink(path_content) == -1 && err == NULL)
8555 err = got_error_from_errno2("unlink", path_content);
8556 free(path_content);
8557 free(ondisk_path);
8558 free(new_staged_blob_id);
8559 return err;
8562 const struct got_error *
8563 got_worktree_stage(struct got_worktree *worktree,
8564 struct got_pathlist_head *paths,
8565 got_worktree_status_cb status_cb, void *status_arg,
8566 got_worktree_patch_cb patch_cb, void *patch_arg,
8567 int allow_bad_symlinks, struct got_repository *repo)
8569 const struct got_error *err = NULL, *sync_err, *unlockerr;
8570 struct got_pathlist_entry *pe;
8571 struct got_fileindex *fileindex = NULL;
8572 char *fileindex_path = NULL;
8573 struct got_reference *head_ref = NULL;
8574 struct got_object_id *head_commit_id = NULL;
8575 struct check_stage_ok_arg oka;
8576 struct stage_path_arg spa;
8578 err = lock_worktree(worktree, LOCK_EX);
8579 if (err)
8580 return err;
8582 err = got_ref_open(&head_ref, repo,
8583 got_worktree_get_head_ref_name(worktree), 0);
8584 if (err)
8585 goto done;
8586 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8587 if (err)
8588 goto done;
8589 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8590 if (err)
8591 goto done;
8593 /* Check pre-conditions before staging anything. */
8594 oka.head_commit_id = head_commit_id;
8595 oka.worktree = worktree;
8596 oka.fileindex = fileindex;
8597 oka.repo = repo;
8598 oka.have_changes = 0;
8599 TAILQ_FOREACH(pe, paths, entry) {
8600 err = worktree_status(worktree, pe->path, fileindex, repo,
8601 check_stage_ok, &oka, NULL, NULL, 1, 0);
8602 if (err)
8603 goto done;
8605 if (!oka.have_changes) {
8606 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8607 goto done;
8610 spa.worktree = worktree;
8611 spa.fileindex = fileindex;
8612 spa.repo = repo;
8613 spa.patch_cb = patch_cb;
8614 spa.patch_arg = patch_arg;
8615 spa.status_cb = status_cb;
8616 spa.status_arg = status_arg;
8617 spa.staged_something = 0;
8618 spa.allow_bad_symlinks = allow_bad_symlinks;
8619 TAILQ_FOREACH(pe, paths, entry) {
8620 err = worktree_status(worktree, pe->path, fileindex, repo,
8621 stage_path, &spa, NULL, NULL, 1, 0);
8622 if (err)
8623 goto done;
8625 if (!spa.staged_something) {
8626 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8627 goto done;
8630 sync_err = sync_fileindex(fileindex, fileindex_path);
8631 if (sync_err && err == NULL)
8632 err = sync_err;
8633 done:
8634 if (head_ref)
8635 got_ref_close(head_ref);
8636 free(head_commit_id);
8637 free(fileindex_path);
8638 if (fileindex)
8639 got_fileindex_free(fileindex);
8640 unlockerr = lock_worktree(worktree, LOCK_SH);
8641 if (unlockerr && err == NULL)
8642 err = unlockerr;
8643 return err;
8646 struct unstage_path_arg {
8647 struct got_worktree *worktree;
8648 struct got_fileindex *fileindex;
8649 struct got_repository *repo;
8650 got_worktree_checkout_cb progress_cb;
8651 void *progress_arg;
8652 got_worktree_patch_cb patch_cb;
8653 void *patch_arg;
8656 static const struct got_error *
8657 create_unstaged_content(char **path_unstaged_content,
8658 char **path_new_staged_content, struct got_object_id *blob_id,
8659 struct got_object_id *staged_blob_id, const char *relpath,
8660 struct got_repository *repo,
8661 got_worktree_patch_cb patch_cb, void *patch_arg)
8663 const struct got_error *err, *free_err;
8664 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8665 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8666 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8667 struct got_diffreg_result *diffreg_result = NULL;
8668 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8669 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8670 int fd1 = -1, fd2 = -1;
8672 *path_unstaged_content = NULL;
8673 *path_new_staged_content = NULL;
8675 err = got_object_id_str(&label1, blob_id);
8676 if (err)
8677 return err;
8679 fd1 = got_opentempfd();
8680 if (fd1 == -1) {
8681 err = got_error_from_errno("got_opentempfd");
8682 goto done;
8684 fd2 = got_opentempfd();
8685 if (fd2 == -1) {
8686 err = got_error_from_errno("got_opentempfd");
8687 goto done;
8690 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8691 if (err)
8692 goto done;
8694 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8695 if (err)
8696 goto done;
8698 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8699 if (err)
8700 goto done;
8702 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8703 fd2);
8704 if (err)
8705 goto done;
8707 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8708 if (err)
8709 goto done;
8711 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8712 if (err)
8713 goto done;
8715 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8716 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8717 if (err)
8718 goto done;
8720 err = got_opentemp_named(path_unstaged_content, &outfile,
8721 "got-unstaged-content", "");
8722 if (err)
8723 goto done;
8724 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8725 "got-new-staged-content", "");
8726 if (err)
8727 goto done;
8729 if (fseek(f1, 0L, SEEK_SET) == -1) {
8730 err = got_ferror(f1, GOT_ERR_IO);
8731 goto done;
8733 if (fseek(f2, 0L, SEEK_SET) == -1) {
8734 err = got_ferror(f2, GOT_ERR_IO);
8735 goto done;
8737 /* Count the number of actual changes in the diff result. */
8738 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8739 struct diff_chunk_context cc = {};
8740 diff_chunk_context_load_change(&cc, &nchunks_used,
8741 diffreg_result->result, n, 0);
8742 nchanges++;
8744 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8745 int choice;
8746 err = apply_or_reject_change(&choice, &nchunks_used,
8747 diffreg_result->result, n, relpath, f1, f2,
8748 &line_cur1, &line_cur2,
8749 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8750 if (err)
8751 goto done;
8752 if (choice == GOT_PATCH_CHOICE_YES)
8753 have_content = 1;
8754 else
8755 have_rejected_content = 1;
8756 if (choice == GOT_PATCH_CHOICE_QUIT)
8757 break;
8759 if (have_content || have_rejected_content)
8760 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8761 outfile, rejectfile);
8762 done:
8763 free(label1);
8764 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8765 err = got_error_from_errno("close");
8766 if (blob)
8767 got_object_blob_close(blob);
8768 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8769 err = got_error_from_errno("close");
8770 if (staged_blob)
8771 got_object_blob_close(staged_blob);
8772 free_err = got_diffreg_result_free(diffreg_result);
8773 if (free_err && err == NULL)
8774 err = free_err;
8775 if (f1 && fclose(f1) == EOF && err == NULL)
8776 err = got_error_from_errno2("fclose", path1);
8777 if (f2 && fclose(f2) == EOF && err == NULL)
8778 err = got_error_from_errno2("fclose", path2);
8779 if (outfile && fclose(outfile) == EOF && err == NULL)
8780 err = got_error_from_errno2("fclose", *path_unstaged_content);
8781 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8782 err = got_error_from_errno2("fclose", *path_new_staged_content);
8783 if (path1 && unlink(path1) == -1 && err == NULL)
8784 err = got_error_from_errno2("unlink", path1);
8785 if (path2 && unlink(path2) == -1 && err == NULL)
8786 err = got_error_from_errno2("unlink", path2);
8787 if (err || !have_content) {
8788 if (*path_unstaged_content &&
8789 unlink(*path_unstaged_content) == -1 && err == NULL)
8790 err = got_error_from_errno2("unlink",
8791 *path_unstaged_content);
8792 free(*path_unstaged_content);
8793 *path_unstaged_content = NULL;
8795 if (err || !have_content || !have_rejected_content) {
8796 if (*path_new_staged_content &&
8797 unlink(*path_new_staged_content) == -1 && err == NULL)
8798 err = got_error_from_errno2("unlink",
8799 *path_new_staged_content);
8800 free(*path_new_staged_content);
8801 *path_new_staged_content = NULL;
8803 free(path1);
8804 free(path2);
8805 return err;
8808 static const struct got_error *
8809 unstage_hunks(struct got_object_id *staged_blob_id,
8810 struct got_blob_object *blob_base,
8811 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8812 const char *ondisk_path, const char *label_orig,
8813 struct got_worktree *worktree, struct got_repository *repo,
8814 got_worktree_patch_cb patch_cb, void *patch_arg,
8815 got_worktree_checkout_cb progress_cb, void *progress_arg)
8817 const struct got_error *err = NULL;
8818 char *path_unstaged_content = NULL;
8819 char *path_new_staged_content = NULL;
8820 char *parent = NULL, *base_path = NULL;
8821 char *blob_base_path = NULL;
8822 struct got_object_id *new_staged_blob_id = NULL;
8823 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8824 struct stat sb;
8826 err = create_unstaged_content(&path_unstaged_content,
8827 &path_new_staged_content, blob_id, staged_blob_id,
8828 ie->path, repo, patch_cb, patch_arg);
8829 if (err)
8830 return err;
8832 if (path_unstaged_content == NULL)
8833 return NULL;
8835 if (path_new_staged_content) {
8836 err = got_object_blob_create(&new_staged_blob_id,
8837 path_new_staged_content, repo);
8838 if (err)
8839 goto done;
8842 f = fopen(path_unstaged_content, "re");
8843 if (f == NULL) {
8844 err = got_error_from_errno2("fopen",
8845 path_unstaged_content);
8846 goto done;
8848 if (fstat(fileno(f), &sb) == -1) {
8849 err = got_error_from_errno2("fstat", path_unstaged_content);
8850 goto done;
8852 if (got_fileindex_entry_staged_filetype_get(ie) ==
8853 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8854 char link_target[PATH_MAX];
8855 size_t r;
8856 r = fread(link_target, 1, sizeof(link_target), f);
8857 if (r == 0 && ferror(f)) {
8858 err = got_error_from_errno("fread");
8859 goto done;
8861 if (r >= sizeof(link_target)) { /* should not happen */
8862 err = got_error(GOT_ERR_NO_SPACE);
8863 goto done;
8865 link_target[r] = '\0';
8866 err = merge_symlink(worktree, blob_base,
8867 ondisk_path, ie->path, label_orig, link_target,
8868 worktree->base_commit_id, repo, progress_cb,
8869 progress_arg);
8870 } else {
8871 int local_changes_subsumed;
8873 err = got_path_dirname(&parent, ondisk_path);
8874 if (err)
8875 return err;
8877 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8878 parent) == -1) {
8879 err = got_error_from_errno("asprintf");
8880 base_path = NULL;
8881 goto done;
8884 err = got_opentemp_named(&blob_base_path, &f_base,
8885 base_path, "");
8886 if (err)
8887 goto done;
8888 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8889 blob_base);
8890 if (err)
8891 goto done;
8894 * In order the run a 3-way merge with a symlink we copy the symlink's
8895 * target path into a temporary file and use that file with diff3.
8897 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8898 err = dump_symlink_target_path_to_file(&f_deriv2,
8899 ondisk_path);
8900 if (err)
8901 goto done;
8902 } else {
8903 int fd;
8904 fd = open(ondisk_path,
8905 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8906 if (fd == -1) {
8907 err = got_error_from_errno2("open", ondisk_path);
8908 goto done;
8910 f_deriv2 = fdopen(fd, "r");
8911 if (f_deriv2 == NULL) {
8912 err = got_error_from_errno2("fdopen", ondisk_path);
8913 close(fd);
8914 goto done;
8918 err = merge_file(&local_changes_subsumed, worktree,
8919 f_base, f, f_deriv2, ondisk_path, ie->path,
8920 got_fileindex_perms_to_st(ie),
8921 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8922 repo, progress_cb, progress_arg);
8924 if (err)
8925 goto done;
8927 if (new_staged_blob_id) {
8928 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8929 SHA1_DIGEST_LENGTH);
8930 } else {
8931 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8932 got_fileindex_entry_staged_filetype_set(ie, 0);
8934 done:
8935 free(new_staged_blob_id);
8936 if (path_unstaged_content &&
8937 unlink(path_unstaged_content) == -1 && err == NULL)
8938 err = got_error_from_errno2("unlink", path_unstaged_content);
8939 if (path_new_staged_content &&
8940 unlink(path_new_staged_content) == -1 && err == NULL)
8941 err = got_error_from_errno2("unlink", path_new_staged_content);
8942 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8943 err = got_error_from_errno2("unlink", blob_base_path);
8944 if (f_base && fclose(f_base) == EOF && err == NULL)
8945 err = got_error_from_errno2("fclose", path_unstaged_content);
8946 if (f && fclose(f) == EOF && err == NULL)
8947 err = got_error_from_errno2("fclose", path_unstaged_content);
8948 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8949 err = got_error_from_errno2("fclose", ondisk_path);
8950 free(path_unstaged_content);
8951 free(path_new_staged_content);
8952 free(blob_base_path);
8953 free(parent);
8954 free(base_path);
8955 return err;
8958 static const struct got_error *
8959 unstage_path(void *arg, unsigned char status,
8960 unsigned char staged_status, const char *relpath,
8961 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8962 struct got_object_id *commit_id, int dirfd, const char *de_name)
8964 const struct got_error *err = NULL;
8965 struct unstage_path_arg *a = arg;
8966 struct got_fileindex_entry *ie;
8967 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8968 char *ondisk_path = NULL;
8969 char *id_str = NULL, *label_orig = NULL;
8970 int local_changes_subsumed;
8971 struct stat sb;
8972 int fd1 = -1, fd2 = -1;
8974 if (staged_status != GOT_STATUS_ADD &&
8975 staged_status != GOT_STATUS_MODIFY &&
8976 staged_status != GOT_STATUS_DELETE)
8977 return NULL;
8979 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8980 if (ie == NULL)
8981 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8983 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8984 == -1)
8985 return got_error_from_errno("asprintf");
8987 err = got_object_id_str(&id_str,
8988 commit_id ? commit_id : a->worktree->base_commit_id);
8989 if (err)
8990 goto done;
8991 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8992 id_str) == -1) {
8993 err = got_error_from_errno("asprintf");
8994 goto done;
8997 fd1 = got_opentempfd();
8998 if (fd1 == -1) {
8999 err = got_error_from_errno("got_opentempfd");
9000 goto done;
9002 fd2 = got_opentempfd();
9003 if (fd2 == -1) {
9004 err = got_error_from_errno("got_opentempfd");
9005 goto done;
9008 switch (staged_status) {
9009 case GOT_STATUS_MODIFY:
9010 err = got_object_open_as_blob(&blob_base, a->repo,
9011 blob_id, 8192, fd1);
9012 if (err)
9013 break;
9014 /* fall through */
9015 case GOT_STATUS_ADD:
9016 if (a->patch_cb) {
9017 if (staged_status == GOT_STATUS_ADD) {
9018 int choice = GOT_PATCH_CHOICE_NONE;
9019 err = (*a->patch_cb)(&choice, a->patch_arg,
9020 staged_status, ie->path, NULL, 1, 1);
9021 if (err)
9022 break;
9023 if (choice != GOT_PATCH_CHOICE_YES)
9024 break;
9025 } else {
9026 err = unstage_hunks(staged_blob_id,
9027 blob_base, blob_id, ie, ondisk_path,
9028 label_orig, a->worktree, a->repo,
9029 a->patch_cb, a->patch_arg,
9030 a->progress_cb, a->progress_arg);
9031 break; /* Done with this file. */
9034 err = got_object_open_as_blob(&blob_staged, a->repo,
9035 staged_blob_id, 8192, fd2);
9036 if (err)
9037 break;
9038 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9039 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9040 case GOT_FILEIDX_MODE_REGULAR_FILE:
9041 err = merge_blob(&local_changes_subsumed, a->worktree,
9042 blob_base, ondisk_path, relpath,
9043 got_fileindex_perms_to_st(ie), label_orig,
9044 blob_staged, commit_id ? commit_id :
9045 a->worktree->base_commit_id, a->repo,
9046 a->progress_cb, a->progress_arg);
9047 break;
9048 case GOT_FILEIDX_MODE_SYMLINK:
9049 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9050 char *staged_target;
9051 err = got_object_blob_read_to_str(
9052 &staged_target, blob_staged);
9053 if (err)
9054 goto done;
9055 err = merge_symlink(a->worktree, blob_base,
9056 ondisk_path, relpath, label_orig,
9057 staged_target, commit_id ? commit_id :
9058 a->worktree->base_commit_id,
9059 a->repo, a->progress_cb, a->progress_arg);
9060 free(staged_target);
9061 } else {
9062 err = merge_blob(&local_changes_subsumed,
9063 a->worktree, blob_base, ondisk_path,
9064 relpath, got_fileindex_perms_to_st(ie),
9065 label_orig, blob_staged,
9066 commit_id ? commit_id :
9067 a->worktree->base_commit_id, a->repo,
9068 a->progress_cb, a->progress_arg);
9070 break;
9071 default:
9072 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9073 break;
9075 if (err == NULL) {
9076 got_fileindex_entry_stage_set(ie,
9077 GOT_FILEIDX_STAGE_NONE);
9078 got_fileindex_entry_staged_filetype_set(ie, 0);
9080 break;
9081 case GOT_STATUS_DELETE:
9082 if (a->patch_cb) {
9083 int choice = GOT_PATCH_CHOICE_NONE;
9084 err = (*a->patch_cb)(&choice, a->patch_arg,
9085 staged_status, ie->path, NULL, 1, 1);
9086 if (err)
9087 break;
9088 if (choice == GOT_PATCH_CHOICE_NO)
9089 break;
9090 if (choice != GOT_PATCH_CHOICE_YES) {
9091 err = got_error(GOT_ERR_PATCH_CHOICE);
9092 break;
9095 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9096 got_fileindex_entry_staged_filetype_set(ie, 0);
9097 err = get_file_status(&status, &sb, ie, ondisk_path,
9098 dirfd, de_name, a->repo);
9099 if (err)
9100 break;
9101 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9102 break;
9104 done:
9105 free(ondisk_path);
9106 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9107 err = got_error_from_errno("close");
9108 if (blob_base)
9109 got_object_blob_close(blob_base);
9110 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9111 err = got_error_from_errno("close");
9112 if (blob_staged)
9113 got_object_blob_close(blob_staged);
9114 free(id_str);
9115 free(label_orig);
9116 return err;
9119 const struct got_error *
9120 got_worktree_unstage(struct got_worktree *worktree,
9121 struct got_pathlist_head *paths,
9122 got_worktree_checkout_cb progress_cb, void *progress_arg,
9123 got_worktree_patch_cb patch_cb, void *patch_arg,
9124 struct got_repository *repo)
9126 const struct got_error *err = NULL, *sync_err, *unlockerr;
9127 struct got_pathlist_entry *pe;
9128 struct got_fileindex *fileindex = NULL;
9129 char *fileindex_path = NULL;
9130 struct unstage_path_arg upa;
9132 err = lock_worktree(worktree, LOCK_EX);
9133 if (err)
9134 return err;
9136 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9137 if (err)
9138 goto done;
9140 upa.worktree = worktree;
9141 upa.fileindex = fileindex;
9142 upa.repo = repo;
9143 upa.progress_cb = progress_cb;
9144 upa.progress_arg = progress_arg;
9145 upa.patch_cb = patch_cb;
9146 upa.patch_arg = patch_arg;
9147 TAILQ_FOREACH(pe, paths, entry) {
9148 err = worktree_status(worktree, pe->path, fileindex, repo,
9149 unstage_path, &upa, NULL, NULL, 1, 0);
9150 if (err)
9151 goto done;
9154 sync_err = sync_fileindex(fileindex, fileindex_path);
9155 if (sync_err && err == NULL)
9156 err = sync_err;
9157 done:
9158 free(fileindex_path);
9159 if (fileindex)
9160 got_fileindex_free(fileindex);
9161 unlockerr = lock_worktree(worktree, LOCK_SH);
9162 if (unlockerr && err == NULL)
9163 err = unlockerr;
9164 return err;
9167 struct report_file_info_arg {
9168 struct got_worktree *worktree;
9169 got_worktree_path_info_cb info_cb;
9170 void *info_arg;
9171 struct got_pathlist_head *paths;
9172 got_cancel_cb cancel_cb;
9173 void *cancel_arg;
9176 static const struct got_error *
9177 report_file_info(void *arg, struct got_fileindex_entry *ie)
9179 struct report_file_info_arg *a = arg;
9180 struct got_pathlist_entry *pe;
9181 struct got_object_id blob_id, staged_blob_id, commit_id;
9182 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9183 struct got_object_id *commit_idp = NULL;
9184 int stage;
9186 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9187 return got_error(GOT_ERR_CANCELLED);
9189 TAILQ_FOREACH(pe, a->paths, entry) {
9190 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9191 got_path_is_child(ie->path, pe->path, pe->path_len))
9192 break;
9194 if (pe == NULL) /* not found */
9195 return NULL;
9197 if (got_fileindex_entry_has_blob(ie))
9198 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9199 stage = got_fileindex_entry_stage_get(ie);
9200 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9201 stage == GOT_FILEIDX_STAGE_ADD) {
9202 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9203 &staged_blob_id, ie);
9206 if (got_fileindex_entry_has_commit(ie))
9207 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9209 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9210 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9213 const struct got_error *
9214 got_worktree_path_info(struct got_worktree *worktree,
9215 struct got_pathlist_head *paths,
9216 got_worktree_path_info_cb info_cb, void *info_arg,
9217 got_cancel_cb cancel_cb, void *cancel_arg)
9220 const struct got_error *err = NULL, *unlockerr;
9221 struct got_fileindex *fileindex = NULL;
9222 char *fileindex_path = NULL;
9223 struct report_file_info_arg arg;
9225 err = lock_worktree(worktree, LOCK_SH);
9226 if (err)
9227 return err;
9229 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9230 if (err)
9231 goto done;
9233 arg.worktree = worktree;
9234 arg.info_cb = info_cb;
9235 arg.info_arg = info_arg;
9236 arg.paths = paths;
9237 arg.cancel_cb = cancel_cb;
9238 arg.cancel_arg = cancel_arg;
9239 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9240 &arg);
9241 done:
9242 free(fileindex_path);
9243 if (fileindex)
9244 got_fileindex_free(fileindex);
9245 unlockerr = lock_worktree(worktree, LOCK_UN);
9246 if (unlockerr && err == NULL)
9247 err = unlockerr;
9248 return err;
9251 static const struct got_error *
9252 patch_check_path(const char *p, char **path, unsigned char *status,
9253 unsigned char *staged_status, struct got_fileindex *fileindex,
9254 struct got_worktree *worktree, struct got_repository *repo)
9256 const struct got_error *err;
9257 struct got_fileindex_entry *ie;
9258 struct stat sb;
9259 char *ondisk_path = NULL;
9261 err = got_worktree_resolve_path(path, worktree, p);
9262 if (err)
9263 return err;
9265 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9266 *path[0] ? "/" : "", *path) == -1)
9267 return got_error_from_errno("asprintf");
9269 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9270 if (ie) {
9271 *staged_status = get_staged_status(ie);
9272 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9273 repo);
9274 if (err)
9275 goto done;
9276 } else {
9277 *staged_status = GOT_STATUS_NO_CHANGE;
9278 *status = GOT_STATUS_UNVERSIONED;
9279 if (lstat(ondisk_path, &sb) == -1) {
9280 if (errno != ENOENT) {
9281 err = got_error_from_errno2("lstat",
9282 ondisk_path);
9283 goto done;
9285 *status = GOT_STATUS_NONEXISTENT;
9289 done:
9290 free(ondisk_path);
9291 return err;
9294 static const struct got_error *
9295 patch_can_rm(const char *path, unsigned char status,
9296 unsigned char staged_status)
9298 if (status == GOT_STATUS_NONEXISTENT)
9299 return got_error_set_errno(ENOENT, path);
9300 if (status != GOT_STATUS_NO_CHANGE &&
9301 status != GOT_STATUS_ADD &&
9302 status != GOT_STATUS_MODIFY &&
9303 status != GOT_STATUS_MODE_CHANGE)
9304 return got_error_path(path, GOT_ERR_FILE_STATUS);
9305 if (staged_status == GOT_STATUS_DELETE)
9306 return got_error_path(path, GOT_ERR_FILE_STATUS);
9307 return NULL;
9310 static const struct got_error *
9311 patch_can_add(const char *path, unsigned char status)
9313 if (status != GOT_STATUS_NONEXISTENT)
9314 return got_error_path(path, GOT_ERR_FILE_STATUS);
9315 return NULL;
9318 static const struct got_error *
9319 patch_can_edit(const char *path, unsigned char status,
9320 unsigned char staged_status)
9322 if (status == GOT_STATUS_NONEXISTENT)
9323 return got_error_set_errno(ENOENT, path);
9324 if (status != GOT_STATUS_NO_CHANGE &&
9325 status != GOT_STATUS_ADD &&
9326 status != GOT_STATUS_MODIFY)
9327 return got_error_path(path, GOT_ERR_FILE_STATUS);
9328 if (staged_status == GOT_STATUS_DELETE)
9329 return got_error_path(path, GOT_ERR_FILE_STATUS);
9330 return NULL;
9333 const struct got_error *
9334 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9335 char **fileindex_path, struct got_worktree *worktree)
9337 return open_fileindex(fileindex, fileindex_path, worktree);
9340 const struct got_error *
9341 got_worktree_patch_check_path(const char *old, const char *new,
9342 char **oldpath, char **newpath, struct got_worktree *worktree,
9343 struct got_repository *repo, struct got_fileindex *fileindex)
9345 const struct got_error *err = NULL;
9346 int file_renamed = 0;
9347 unsigned char status_old, staged_status_old;
9348 unsigned char status_new, staged_status_new;
9350 *oldpath = NULL;
9351 *newpath = NULL;
9353 err = patch_check_path(old != NULL ? old : new, oldpath,
9354 &status_old, &staged_status_old, fileindex, worktree, repo);
9355 if (err)
9356 goto done;
9358 err = patch_check_path(new != NULL ? new : old, newpath,
9359 &status_new, &staged_status_new, fileindex, worktree, repo);
9360 if (err)
9361 goto done;
9363 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9364 file_renamed = 1;
9366 if (old != NULL && new == NULL)
9367 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9368 else if (file_renamed) {
9369 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9370 if (err == NULL)
9371 err = patch_can_add(*newpath, status_new);
9372 } else if (old == NULL)
9373 err = patch_can_add(*newpath, status_new);
9374 else
9375 err = patch_can_edit(*newpath, status_new, staged_status_new);
9377 done:
9378 if (err) {
9379 free(*oldpath);
9380 *oldpath = NULL;
9381 free(*newpath);
9382 *newpath = NULL;
9384 return err;
9387 const struct got_error *
9388 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9389 struct got_worktree *worktree, struct got_fileindex *fileindex,
9390 got_worktree_checkout_cb progress_cb, void *progress_arg)
9392 struct schedule_addition_args saa;
9394 memset(&saa, 0, sizeof(saa));
9395 saa.worktree = worktree;
9396 saa.fileindex = fileindex;
9397 saa.progress_cb = progress_cb;
9398 saa.progress_arg = progress_arg;
9399 saa.repo = repo;
9401 return worktree_status(worktree, path, fileindex, repo,
9402 schedule_addition, &saa, NULL, NULL, 1, 0);
9405 const struct got_error *
9406 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9407 struct got_worktree *worktree, struct got_fileindex *fileindex,
9408 got_worktree_delete_cb progress_cb, void *progress_arg)
9410 struct schedule_deletion_args sda;
9412 memset(&sda, 0, sizeof(sda));
9413 sda.worktree = worktree;
9414 sda.fileindex = fileindex;
9415 sda.progress_cb = progress_cb;
9416 sda.progress_arg = progress_arg;
9417 sda.repo = repo;
9418 sda.delete_local_mods = 0;
9419 sda.keep_on_disk = 0;
9420 sda.ignore_missing_paths = 0;
9421 sda.status_codes = NULL;
9423 return worktree_status(worktree, path, fileindex, repo,
9424 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9427 const struct got_error *
9428 got_worktree_patch_complete(struct got_fileindex *fileindex,
9429 const char *fileindex_path)
9431 const struct got_error *err = NULL;
9433 err = sync_fileindex(fileindex, fileindex_path);
9434 got_fileindex_free(fileindex);
9436 return err;