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;
1517 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 * conflict marker is found in newly added lines only.
1520 static const struct got_error *
1521 get_modified_file_content_status(unsigned char *status,
1522 struct got_blob_object *blob, const char *path, struct stat *sb,
1523 FILE *ondisk_file)
1525 const struct got_error *err, *free_err;
1526 const char *markers[3] = {
1527 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 GOT_DIFF_CONFLICT_MARKER_SEP,
1529 GOT_DIFF_CONFLICT_MARKER_END
1531 FILE *f1 = NULL;
1532 struct got_diffreg_result *diffreg_result = NULL;
1533 struct diff_result *r;
1534 int nchunks_parsed, n, i = 0, ln = 0;
1535 char *line = NULL;
1536 size_t linesize = 0;
1537 ssize_t linelen;
1539 if (*status != GOT_STATUS_MODIFY)
1540 return NULL;
1542 f1 = got_opentemp();
1543 if (f1 == NULL)
1544 return got_error_from_errno("got_opentemp");
1546 if (blob) {
1547 got_object_blob_rewind(blob);
1548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1549 if (err)
1550 goto done;
1553 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1555 if (err)
1556 goto done;
1558 r = diffreg_result->result;
1560 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 struct diff_chunk *c;
1562 struct diff_chunk_context cc = {};
1563 off_t pos;
1566 * We can optimise a little by advancing straight
1567 * to the next chunk if this one has no added lines.
1569 c = diff_chunk_get(r, n);
1571 if (diff_chunk_type(c) != CHUNK_PLUS) {
1572 nchunks_parsed = 1;
1573 continue; /* removed or unchanged lines */
1576 pos = diff_chunk_get_right_start_pos(c);
1577 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 err = got_ferror(ondisk_file, GOT_ERR_IO);
1579 goto done;
1582 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 ln = cc.right.start;
1585 while (ln < cc.right.end) {
1586 linelen = getline(&line, &linesize, ondisk_file);
1587 if (linelen == -1) {
1588 if (feof(ondisk_file))
1589 break;
1590 err = got_ferror(ondisk_file, GOT_ERR_IO);
1591 break;
1594 if (line && strncmp(line, markers[i],
1595 strlen(markers[i])) == 0) {
1596 if (strcmp(markers[i],
1597 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 *status = GOT_STATUS_CONFLICT;
1599 goto done;
1600 } else
1601 i++;
1603 ++ln;
1607 done:
1608 free(line);
1609 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 err = got_error_from_errno("fclose");
1611 free_err = got_diffreg_result_free(diffreg_result);
1612 if (err == NULL)
1613 err = free_err;
1615 return err;
1618 static int
1619 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1621 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1622 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1625 static int
1626 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1628 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1629 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1630 ie->mtime_sec == sb->st_mtim.tv_sec &&
1631 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1632 ie->size == (sb->st_size & 0xffffffff) &&
1633 !xbit_differs(ie, sb->st_mode));
1636 static unsigned char
1637 get_staged_status(struct got_fileindex_entry *ie)
1639 switch (got_fileindex_entry_stage_get(ie)) {
1640 case GOT_FILEIDX_STAGE_ADD:
1641 return GOT_STATUS_ADD;
1642 case GOT_FILEIDX_STAGE_DELETE:
1643 return GOT_STATUS_DELETE;
1644 case GOT_FILEIDX_STAGE_MODIFY:
1645 return GOT_STATUS_MODIFY;
1646 default:
1647 return GOT_STATUS_NO_CHANGE;
1651 static const struct got_error *
1652 get_symlink_modification_status(unsigned char *status,
1653 struct got_fileindex_entry *ie, const char *abspath,
1654 int dirfd, const char *de_name, struct got_blob_object *blob)
1656 const struct got_error *err = NULL;
1657 char target_path[PATH_MAX];
1658 char etarget[PATH_MAX];
1659 ssize_t elen;
1660 size_t len, target_len = 0;
1661 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1662 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1664 *status = GOT_STATUS_NO_CHANGE;
1666 /* Blob object content specifies the target path of the link. */
1667 do {
1668 err = got_object_blob_read_block(&len, blob);
1669 if (err)
1670 return err;
1671 if (len + target_len >= sizeof(target_path)) {
1673 * Should not happen. The blob contents were OK
1674 * when this symlink was installed.
1676 return got_error(GOT_ERR_NO_SPACE);
1678 if (len > 0) {
1679 /* Skip blob object header first time around. */
1680 memcpy(target_path + target_len, buf + hdrlen,
1681 len - hdrlen);
1682 target_len += len - hdrlen;
1683 hdrlen = 0;
1685 } while (len != 0);
1686 target_path[target_len] = '\0';
1688 if (dirfd != -1) {
1689 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1690 if (elen == -1)
1691 return got_error_from_errno2("readlinkat", abspath);
1692 } else {
1693 elen = readlink(abspath, etarget, sizeof(etarget));
1694 if (elen == -1)
1695 return got_error_from_errno2("readlink", abspath);
1698 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1699 *status = GOT_STATUS_MODIFY;
1701 return NULL;
1704 static const struct got_error *
1705 get_file_status(unsigned char *status, struct stat *sb,
1706 struct got_fileindex_entry *ie, const char *abspath,
1707 int dirfd, const char *de_name, struct got_repository *repo)
1709 const struct got_error *err = NULL;
1710 struct got_object_id id;
1711 size_t hdrlen;
1712 int fd = -1, fd1 = -1;
1713 FILE *f = NULL;
1714 uint8_t fbuf[8192];
1715 struct got_blob_object *blob = NULL;
1716 size_t flen, blen;
1717 unsigned char staged_status;
1719 staged_status = get_staged_status(ie);
1720 *status = GOT_STATUS_NO_CHANGE;
1721 memset(sb, 0, sizeof(*sb));
1724 * Whenever the caller provides a directory descriptor and a
1725 * directory entry name for the file, use them! This prevents
1726 * race conditions if filesystem paths change beneath our feet.
1728 if (dirfd != -1) {
1729 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 if (errno == ENOENT) {
1731 if (got_fileindex_entry_has_file_on_disk(ie))
1732 *status = GOT_STATUS_MISSING;
1733 else
1734 *status = GOT_STATUS_DELETE;
1735 goto done;
1737 err = got_error_from_errno2("fstatat", abspath);
1738 goto done;
1740 } else {
1741 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 if (fd == -1 && errno != ENOENT &&
1743 !got_err_open_nofollow_on_symlink())
1744 return got_error_from_errno2("open", abspath);
1745 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1746 if (lstat(abspath, sb) == -1)
1747 return got_error_from_errno2("lstat", abspath);
1748 } else if (fd == -1 || fstat(fd, sb) == -1) {
1749 if (errno == ENOENT) {
1750 if (got_fileindex_entry_has_file_on_disk(ie))
1751 *status = GOT_STATUS_MISSING;
1752 else
1753 *status = GOT_STATUS_DELETE;
1754 goto done;
1756 err = got_error_from_errno2("fstat", abspath);
1757 goto done;
1761 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1762 *status = GOT_STATUS_OBSTRUCTED;
1763 goto done;
1766 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1767 *status = GOT_STATUS_DELETE;
1768 goto done;
1769 } else if (!got_fileindex_entry_has_blob(ie) &&
1770 staged_status != GOT_STATUS_ADD) {
1771 *status = GOT_STATUS_ADD;
1772 goto done;
1775 if (!stat_info_differs(ie, sb))
1776 goto done;
1778 if (S_ISLNK(sb->st_mode) &&
1779 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1780 *status = GOT_STATUS_MODIFY;
1781 goto done;
1784 if (staged_status == GOT_STATUS_MODIFY ||
1785 staged_status == GOT_STATUS_ADD)
1786 got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 else
1788 got_fileindex_entry_get_blob_id(&id, ie);
1790 fd1 = got_opentempfd();
1791 if (fd1 == -1) {
1792 err = got_error_from_errno("got_opentempfd");
1793 goto done;
1795 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1796 if (err)
1797 goto done;
1799 if (S_ISLNK(sb->st_mode)) {
1800 err = get_symlink_modification_status(status, ie,
1801 abspath, dirfd, de_name, blob);
1802 goto done;
1805 if (dirfd != -1) {
1806 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1807 if (fd == -1) {
1808 err = got_error_from_errno2("openat", abspath);
1809 goto done;
1813 f = fdopen(fd, "r");
1814 if (f == NULL) {
1815 err = got_error_from_errno2("fdopen", abspath);
1816 goto done;
1818 fd = -1;
1819 hdrlen = got_object_blob_get_hdrlen(blob);
1820 for (;;) {
1821 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1822 err = got_object_blob_read_block(&blen, blob);
1823 if (err)
1824 goto done;
1825 /* Skip length of blob object header first time around. */
1826 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1827 if (flen == 0 && ferror(f)) {
1828 err = got_error_from_errno("fread");
1829 goto done;
1831 if (blen - hdrlen == 0) {
1832 if (flen != 0)
1833 *status = GOT_STATUS_MODIFY;
1834 break;
1835 } else if (flen == 0) {
1836 if (blen - hdrlen != 0)
1837 *status = GOT_STATUS_MODIFY;
1838 break;
1839 } else if (blen - hdrlen == flen) {
1840 /* Skip blob object header first time around. */
1841 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1842 *status = GOT_STATUS_MODIFY;
1843 break;
1845 } else {
1846 *status = GOT_STATUS_MODIFY;
1847 break;
1849 hdrlen = 0;
1852 if (*status == GOT_STATUS_MODIFY) {
1853 rewind(f);
1854 err = get_modified_file_content_status(status, blob, ie->path,
1855 sb, f);
1856 } else if (xbit_differs(ie, sb->st_mode))
1857 *status = GOT_STATUS_MODE_CHANGE;
1858 done:
1859 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 err = got_error_from_errno("close");
1861 if (blob)
1862 got_object_blob_close(blob);
1863 if (f != NULL && fclose(f) == EOF && err == NULL)
1864 err = got_error_from_errno2("fclose", abspath);
1865 if (fd != -1 && close(fd) == -1 && err == NULL)
1866 err = got_error_from_errno2("close", abspath);
1867 return err;
1871 * Update timestamps in the file index if a file is unmodified and
1872 * we had to run a full content comparison to find out.
1874 static const struct got_error *
1875 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 struct got_fileindex_entry *ie, struct stat *sb)
1878 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 return got_fileindex_entry_update(ie, wt_fd, path,
1880 ie->blob_sha1, ie->commit_sha1, 1);
1882 return NULL;
1885 static const struct got_error *
1886 update_blob(struct got_worktree *worktree,
1887 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 struct got_tree_entry *te, const char *path,
1889 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1890 void *progress_arg)
1892 const struct got_error *err = NULL;
1893 struct got_blob_object *blob = NULL;
1894 char *ondisk_path = NULL;
1895 unsigned char status = GOT_STATUS_NO_CHANGE;
1896 struct stat sb;
1897 int fd1 = -1, fd2 = -1;
1899 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1900 return got_error_from_errno("asprintf");
1902 if (ie) {
1903 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1904 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1905 goto done;
1907 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1908 repo);
1909 if (err)
1910 goto done;
1911 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1912 sb.st_mode = got_fileindex_perms_to_st(ie);
1913 } else {
1914 if (stat(ondisk_path, &sb) == -1) {
1915 if (errno != ENOENT) {
1916 err = got_error_from_errno2("stat",
1917 ondisk_path);
1918 goto done;
1920 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1921 status = GOT_STATUS_UNVERSIONED;
1922 } else {
1923 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1924 status = GOT_STATUS_UNVERSIONED;
1925 else
1926 status = GOT_STATUS_OBSTRUCTED;
1930 if (status == GOT_STATUS_OBSTRUCTED) {
1931 if (ie)
1932 got_fileindex_entry_mark_skipped(ie);
1933 err = (*progress_cb)(progress_arg, status, path);
1934 goto done;
1936 if (status == GOT_STATUS_CONFLICT) {
1937 if (ie)
1938 got_fileindex_entry_mark_skipped(ie);
1939 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1940 path);
1941 goto done;
1944 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1945 (S_ISLNK(te->mode) ||
1946 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1948 * This is a regular file or an installed bad symlink.
1949 * If the file index indicates that this file is already
1950 * up-to-date with respect to the repository we can skip
1951 * updating contents of this file.
1953 if (got_fileindex_entry_has_commit(ie) &&
1954 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1955 SHA1_DIGEST_LENGTH) == 0) {
1956 /* Same commit. */
1957 err = sync_timestamps(worktree->root_fd,
1958 path, status, ie, &sb);
1959 if (err)
1960 goto done;
1961 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1962 path);
1963 goto done;
1965 if (got_fileindex_entry_has_blob(ie) &&
1966 memcmp(ie->blob_sha1, te->id.sha1,
1967 SHA1_DIGEST_LENGTH) == 0) {
1968 /* Different commit but the same blob. */
1969 err = sync_timestamps(worktree->root_fd,
1970 path, status, ie, &sb);
1971 if (err)
1972 goto done;
1973 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1974 path);
1975 goto done;
1979 fd1 = got_opentempfd();
1980 if (fd1 == -1) {
1981 err = got_error_from_errno("got_opentempfd");
1982 goto done;
1984 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1985 if (err)
1986 goto done;
1988 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1989 int update_timestamps;
1990 struct got_blob_object *blob2 = NULL;
1991 char *label_orig = NULL;
1992 if (got_fileindex_entry_has_blob(ie)) {
1993 fd2 = got_opentempfd();
1994 if (fd2 == -1) {
1995 err = got_error_from_errno("got_opentempfd");
1996 goto done;
1998 struct got_object_id id2;
1999 got_fileindex_entry_get_blob_id(&id2, ie);
2000 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2001 fd2);
2002 if (err)
2003 goto done;
2005 if (got_fileindex_entry_has_commit(ie)) {
2006 char id_str[SHA1_DIGEST_STRING_LENGTH];
2007 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2008 sizeof(id_str)) == NULL) {
2009 err = got_error_path(id_str,
2010 GOT_ERR_BAD_OBJ_ID_STR);
2011 goto done;
2013 if (asprintf(&label_orig, "%s: commit %s",
2014 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2015 err = got_error_from_errno("asprintf");
2016 goto done;
2019 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2020 char *link_target;
2021 err = got_object_blob_read_to_str(&link_target, blob);
2022 if (err)
2023 goto done;
2024 err = merge_symlink(worktree, blob2, ondisk_path, path,
2025 label_orig, link_target, worktree->base_commit_id,
2026 repo, progress_cb, progress_arg);
2027 free(link_target);
2028 } else {
2029 err = merge_blob(&update_timestamps, worktree, blob2,
2030 ondisk_path, path, sb.st_mode, label_orig, blob,
2031 worktree->base_commit_id, repo,
2032 progress_cb, progress_arg);
2034 free(label_orig);
2035 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2036 err = got_error_from_errno("close");
2037 goto done;
2039 if (blob2)
2040 got_object_blob_close(blob2);
2041 if (err)
2042 goto done;
2044 * Do not update timestamps of files with local changes.
2045 * Otherwise, a future status walk would treat them as
2046 * unmodified files again.
2048 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2049 blob->id.sha1, worktree->base_commit_id->sha1,
2050 update_timestamps);
2051 } else if (status == GOT_STATUS_MODE_CHANGE) {
2052 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2054 } else if (status == GOT_STATUS_DELETE) {
2055 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2056 if (err)
2057 goto done;
2058 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2059 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2060 if (err)
2061 goto done;
2062 } else {
2063 int is_bad_symlink = 0;
2064 if (S_ISLNK(te->mode)) {
2065 err = install_symlink(&is_bad_symlink, worktree,
2066 ondisk_path, path, blob,
2067 status == GOT_STATUS_MISSING, 0,
2068 status == GOT_STATUS_UNVERSIONED, 0,
2069 repo, progress_cb, progress_arg);
2070 } else {
2071 err = install_blob(worktree, ondisk_path, path,
2072 te->mode, sb.st_mode, blob,
2073 status == GOT_STATUS_MISSING, 0, 0,
2074 status == GOT_STATUS_UNVERSIONED, repo,
2075 progress_cb, progress_arg);
2077 if (err)
2078 goto done;
2080 if (ie) {
2081 err = got_fileindex_entry_update(ie,
2082 worktree->root_fd, path, blob->id.sha1,
2083 worktree->base_commit_id->sha1, 1);
2084 } else {
2085 err = create_fileindex_entry(&ie, fileindex,
2086 worktree->base_commit_id, worktree->root_fd, path,
2087 &blob->id);
2089 if (err)
2090 goto done;
2092 if (is_bad_symlink) {
2093 got_fileindex_entry_filetype_set(ie,
2094 GOT_FILEIDX_MODE_BAD_SYMLINK);
2098 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2099 err = got_error_from_errno("close");
2100 goto done;
2102 got_object_blob_close(blob);
2103 done:
2104 free(ondisk_path);
2105 return err;
2108 static const struct got_error *
2109 remove_ondisk_file(const char *root_path, const char *path)
2111 const struct got_error *err = NULL;
2112 char *ondisk_path = NULL, *parent = NULL;
2114 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2115 return got_error_from_errno("asprintf");
2117 if (unlink(ondisk_path) == -1) {
2118 if (errno != ENOENT)
2119 err = got_error_from_errno2("unlink", ondisk_path);
2120 } else {
2121 size_t root_len = strlen(root_path);
2122 err = got_path_dirname(&parent, ondisk_path);
2123 if (err)
2124 goto done;
2125 while (got_path_cmp(parent, root_path,
2126 strlen(parent), root_len) != 0) {
2127 free(ondisk_path);
2128 ondisk_path = parent;
2129 parent = NULL;
2130 if (rmdir(ondisk_path) == -1) {
2131 if (errno != ENOTEMPTY)
2132 err = got_error_from_errno2("rmdir",
2133 ondisk_path);
2134 break;
2136 err = got_path_dirname(&parent, ondisk_path);
2137 if (err)
2138 break;
2141 done:
2142 free(ondisk_path);
2143 free(parent);
2144 return err;
2147 static const struct got_error *
2148 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2149 struct got_fileindex_entry *ie, struct got_repository *repo,
2150 got_worktree_checkout_cb progress_cb, void *progress_arg)
2152 const struct got_error *err = NULL;
2153 unsigned char status;
2154 struct stat sb;
2155 char *ondisk_path;
2157 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2158 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2160 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2161 == -1)
2162 return got_error_from_errno("asprintf");
2164 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2165 if (err)
2166 goto done;
2168 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2169 char ondisk_target[PATH_MAX];
2170 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2171 sizeof(ondisk_target));
2172 if (ondisk_len == -1) {
2173 err = got_error_from_errno2("readlink", ondisk_path);
2174 goto done;
2176 ondisk_target[ondisk_len] = '\0';
2177 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2178 NULL, NULL, /* XXX pass common ancestor info? */
2179 ondisk_target, ondisk_path);
2180 if (err)
2181 goto done;
2182 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2183 ie->path);
2184 goto done;
2187 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2188 status == GOT_STATUS_ADD) {
2189 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2190 if (err)
2191 goto done;
2193 * Preserve the working file and change the deleted blob's
2194 * entry into a schedule-add entry.
2196 err = got_fileindex_entry_update(ie, worktree->root_fd,
2197 ie->path, NULL, NULL, 0);
2198 } else {
2199 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2200 if (err)
2201 goto done;
2202 if (status == GOT_STATUS_NO_CHANGE) {
2203 err = remove_ondisk_file(worktree->root_path, ie->path);
2204 if (err)
2205 goto done;
2207 got_fileindex_entry_remove(fileindex, ie);
2209 done:
2210 free(ondisk_path);
2211 return err;
2214 struct diff_cb_arg {
2215 struct got_fileindex *fileindex;
2216 struct got_worktree *worktree;
2217 struct got_repository *repo;
2218 got_worktree_checkout_cb progress_cb;
2219 void *progress_arg;
2220 got_cancel_cb cancel_cb;
2221 void *cancel_arg;
2224 static const struct got_error *
2225 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2226 struct got_tree_entry *te, const char *parent_path)
2228 struct diff_cb_arg *a = arg;
2230 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2231 return got_error(GOT_ERR_CANCELLED);
2233 return update_blob(a->worktree, a->fileindex, ie, te,
2234 ie->path, a->repo, a->progress_cb, a->progress_arg);
2237 static const struct got_error *
2238 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2240 struct diff_cb_arg *a = arg;
2242 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2243 return got_error(GOT_ERR_CANCELLED);
2245 return delete_blob(a->worktree, a->fileindex, ie,
2246 a->repo, a->progress_cb, a->progress_arg);
2249 static const struct got_error *
2250 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2252 struct diff_cb_arg *a = arg;
2253 const struct got_error *err;
2254 char *path;
2256 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2257 return got_error(GOT_ERR_CANCELLED);
2259 if (got_object_tree_entry_is_submodule(te))
2260 return NULL;
2262 if (asprintf(&path, "%s%s%s", parent_path,
2263 parent_path[0] ? "/" : "", te->name)
2264 == -1)
2265 return got_error_from_errno("asprintf");
2267 if (S_ISDIR(te->mode))
2268 err = add_dir_on_disk(a->worktree, path);
2269 else
2270 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2271 a->repo, a->progress_cb, a->progress_arg);
2273 free(path);
2274 return err;
2277 const struct got_error *
2278 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2280 uint32_t uuid_status;
2282 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2283 if (uuid_status != uuid_s_ok) {
2284 *uuidstr = NULL;
2285 return got_error_uuid(uuid_status, "uuid_to_string");
2288 return NULL;
2291 static const struct got_error *
2292 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2294 const struct got_error *err = NULL;
2295 char *uuidstr = NULL;
2297 *refname = NULL;
2299 err = got_worktree_get_uuid(&uuidstr, worktree);
2300 if (err)
2301 return err;
2303 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2304 err = got_error_from_errno("asprintf");
2305 *refname = NULL;
2307 free(uuidstr);
2308 return err;
2311 const struct got_error *
2312 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2313 const char *prefix)
2315 return get_ref_name(refname, worktree, prefix);
2318 const struct got_error *
2319 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2321 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2324 static const struct got_error *
2325 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree,
2328 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2331 static const struct got_error *
2332 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2334 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2337 static const struct got_error *
2338 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2340 return get_ref_name(refname, worktree,
2341 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2344 static const struct got_error *
2345 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2347 return get_ref_name(refname, worktree,
2348 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2351 static const struct got_error *
2352 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2354 return get_ref_name(refname, worktree,
2355 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2358 static const struct got_error *
2359 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2361 return get_ref_name(refname, worktree,
2362 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2365 static const struct got_error *
2366 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2368 return get_ref_name(refname, worktree,
2369 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2372 static const struct got_error *
2373 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2375 return get_ref_name(refname, worktree,
2376 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2379 const struct got_error *
2380 got_worktree_get_histedit_script_path(char **path,
2381 struct got_worktree *worktree)
2383 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2384 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2385 *path = NULL;
2386 return got_error_from_errno("asprintf");
2388 return NULL;
2391 static const struct got_error *
2392 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2394 return get_ref_name(refname, worktree,
2395 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2398 static const struct got_error *
2399 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2401 return get_ref_name(refname, worktree,
2402 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2406 * Prevent Git's garbage collector from deleting our base commit by
2407 * setting a reference to our base commit's ID.
2409 static const struct got_error *
2410 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2412 const struct got_error *err = NULL;
2413 struct got_reference *ref = NULL;
2414 char *refname;
2416 err = got_worktree_get_base_ref_name(&refname, worktree);
2417 if (err)
2418 return err;
2420 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2421 if (err)
2422 goto done;
2424 err = got_ref_write(ref, repo);
2425 done:
2426 free(refname);
2427 if (ref)
2428 got_ref_close(ref);
2429 return err;
2432 static const struct got_error *
2433 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2435 const struct got_error *err = NULL;
2437 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2438 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2439 err = got_error_from_errno("asprintf");
2440 *fileindex_path = NULL;
2442 return err;
2446 static const struct got_error *
2447 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2448 struct got_worktree *worktree)
2450 const struct got_error *err = NULL;
2451 FILE *index = NULL;
2453 *fileindex_path = NULL;
2454 *fileindex = got_fileindex_alloc();
2455 if (*fileindex == NULL)
2456 return got_error_from_errno("got_fileindex_alloc");
2458 err = get_fileindex_path(fileindex_path, worktree);
2459 if (err)
2460 goto done;
2462 index = fopen(*fileindex_path, "rbe");
2463 if (index == NULL) {
2464 if (errno != ENOENT)
2465 err = got_error_from_errno2("fopen", *fileindex_path);
2466 } else {
2467 err = got_fileindex_read(*fileindex, index);
2468 if (fclose(index) == EOF && err == NULL)
2469 err = got_error_from_errno("fclose");
2471 done:
2472 if (err) {
2473 free(*fileindex_path);
2474 *fileindex_path = NULL;
2475 got_fileindex_free(*fileindex);
2476 *fileindex = NULL;
2478 return err;
2481 struct bump_base_commit_id_arg {
2482 struct got_object_id *base_commit_id;
2483 const char *path;
2484 size_t path_len;
2485 const char *entry_name;
2486 got_worktree_checkout_cb progress_cb;
2487 void *progress_arg;
2490 static const struct got_error *
2491 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2493 const struct got_error *err;
2494 struct bump_base_commit_id_arg *a = arg;
2496 if (a->entry_name) {
2497 if (strcmp(ie->path, a->path) != 0)
2498 return NULL;
2499 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2500 return NULL;
2502 if (got_fileindex_entry_was_skipped(ie))
2503 return NULL;
2505 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2506 SHA1_DIGEST_LENGTH) == 0)
2507 return NULL;
2509 if (a->progress_cb) {
2510 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2511 ie->path);
2512 if (err)
2513 return err;
2515 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2516 return NULL;
2519 /* Bump base commit ID of all files within an updated part of the work tree. */
2520 static const struct got_error *
2521 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2522 struct got_fileindex *fileindex,
2523 got_worktree_checkout_cb progress_cb, void *progress_arg)
2525 struct bump_base_commit_id_arg bbc_arg;
2527 bbc_arg.base_commit_id = worktree->base_commit_id;
2528 bbc_arg.entry_name = NULL;
2529 bbc_arg.path = "";
2530 bbc_arg.path_len = 0;
2531 bbc_arg.progress_cb = progress_cb;
2532 bbc_arg.progress_arg = progress_arg;
2534 return got_fileindex_for_each_entry_safe(fileindex,
2535 bump_base_commit_id, &bbc_arg);
2538 static const struct got_error *
2539 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2541 const struct got_error *err = NULL;
2542 char *new_fileindex_path = NULL;
2543 FILE *new_index = NULL;
2544 struct timespec timeout;
2546 err = got_opentemp_named(&new_fileindex_path, &new_index,
2547 fileindex_path, "");
2548 if (err)
2549 goto done;
2551 err = got_fileindex_write(fileindex, new_index);
2552 if (err)
2553 goto done;
2555 if (rename(new_fileindex_path, fileindex_path) != 0) {
2556 err = got_error_from_errno3("rename", new_fileindex_path,
2557 fileindex_path);
2558 unlink(new_fileindex_path);
2562 * Sleep for a short amount of time to ensure that files modified after
2563 * this program exits have a different time stamp from the one which
2564 * was recorded in the file index.
2566 timeout.tv_sec = 0;
2567 timeout.tv_nsec = 1;
2568 nanosleep(&timeout, NULL);
2569 done:
2570 if (new_index)
2571 fclose(new_index);
2572 free(new_fileindex_path);
2573 return err;
2576 static const struct got_error *
2577 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2578 struct got_object_id **tree_id, const char *wt_relpath,
2579 struct got_commit_object *base_commit, struct got_worktree *worktree,
2580 struct got_repository *repo)
2582 const struct got_error *err = NULL;
2583 struct got_object_id *id = NULL;
2584 char *in_repo_path = NULL;
2585 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2587 *entry_type = GOT_OBJ_TYPE_ANY;
2588 *tree_relpath = NULL;
2589 *tree_id = NULL;
2591 if (wt_relpath[0] == '\0') {
2592 /* Check out all files within the work tree. */
2593 *entry_type = GOT_OBJ_TYPE_TREE;
2594 *tree_relpath = strdup("");
2595 if (*tree_relpath == NULL) {
2596 err = got_error_from_errno("strdup");
2597 goto done;
2599 err = got_object_id_by_path(tree_id, repo, base_commit,
2600 worktree->path_prefix);
2601 if (err)
2602 goto done;
2603 return NULL;
2606 /* Check out a subset of files in the work tree. */
2608 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2609 is_root_wt ? "" : "/", wt_relpath) == -1) {
2610 err = got_error_from_errno("asprintf");
2611 goto done;
2614 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2615 if (err)
2616 goto done;
2618 free(in_repo_path);
2619 in_repo_path = NULL;
2621 err = got_object_get_type(entry_type, repo, id);
2622 if (err)
2623 goto done;
2625 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2626 /* Check out a single file. */
2627 if (strchr(wt_relpath, '/') == NULL) {
2628 /* Check out a single file in work tree's root dir. */
2629 in_repo_path = strdup(worktree->path_prefix);
2630 if (in_repo_path == NULL) {
2631 err = got_error_from_errno("strdup");
2632 goto done;
2634 *tree_relpath = strdup("");
2635 if (*tree_relpath == NULL) {
2636 err = got_error_from_errno("strdup");
2637 goto done;
2639 } else {
2640 /* Check out a single file in a subdirectory. */
2641 err = got_path_dirname(tree_relpath, wt_relpath);
2642 if (err)
2643 return err;
2644 if (asprintf(&in_repo_path, "%s%s%s",
2645 worktree->path_prefix, is_root_wt ? "" : "/",
2646 *tree_relpath) == -1) {
2647 err = got_error_from_errno("asprintf");
2648 goto done;
2651 err = got_object_id_by_path(tree_id, repo,
2652 base_commit, in_repo_path);
2653 } else {
2654 /* Check out all files within a subdirectory. */
2655 *tree_id = got_object_id_dup(id);
2656 if (*tree_id == NULL) {
2657 err = got_error_from_errno("got_object_id_dup");
2658 goto done;
2660 *tree_relpath = strdup(wt_relpath);
2661 if (*tree_relpath == NULL) {
2662 err = got_error_from_errno("strdup");
2663 goto done;
2666 done:
2667 free(id);
2668 free(in_repo_path);
2669 if (err) {
2670 *entry_type = GOT_OBJ_TYPE_ANY;
2671 free(*tree_relpath);
2672 *tree_relpath = NULL;
2673 free(*tree_id);
2674 *tree_id = NULL;
2676 return err;
2679 static const struct got_error *
2680 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2681 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2682 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2683 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2685 const struct got_error *err = NULL;
2686 struct got_commit_object *commit = NULL;
2687 struct got_tree_object *tree = NULL;
2688 struct got_fileindex_diff_tree_cb diff_cb;
2689 struct diff_cb_arg arg;
2691 err = ref_base_commit(worktree, repo);
2692 if (err) {
2693 if (!(err->code == GOT_ERR_ERRNO &&
2694 (errno == EACCES || errno == EROFS)))
2695 goto done;
2696 err = (*progress_cb)(progress_arg,
2697 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2698 if (err)
2699 return err;
2702 err = got_object_open_as_commit(&commit, repo,
2703 worktree->base_commit_id);
2704 if (err)
2705 goto done;
2707 err = got_object_open_as_tree(&tree, repo, tree_id);
2708 if (err)
2709 goto done;
2711 if (entry_name &&
2712 got_object_tree_find_entry(tree, entry_name) == NULL) {
2713 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2714 goto done;
2717 diff_cb.diff_old_new = diff_old_new;
2718 diff_cb.diff_old = diff_old;
2719 diff_cb.diff_new = diff_new;
2720 arg.fileindex = fileindex;
2721 arg.worktree = worktree;
2722 arg.repo = repo;
2723 arg.progress_cb = progress_cb;
2724 arg.progress_arg = progress_arg;
2725 arg.cancel_cb = cancel_cb;
2726 arg.cancel_arg = cancel_arg;
2727 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2728 entry_name, repo, &diff_cb, &arg);
2729 done:
2730 if (tree)
2731 got_object_tree_close(tree);
2732 if (commit)
2733 got_object_commit_close(commit);
2734 return err;
2737 const struct got_error *
2738 got_worktree_checkout_files(struct got_worktree *worktree,
2739 struct got_pathlist_head *paths, struct got_repository *repo,
2740 got_worktree_checkout_cb progress_cb, void *progress_arg,
2741 got_cancel_cb cancel_cb, void *cancel_arg)
2743 const struct got_error *err = NULL, *sync_err, *unlockerr;
2744 struct got_commit_object *commit = NULL;
2745 struct got_tree_object *tree = NULL;
2746 struct got_fileindex *fileindex = NULL;
2747 char *fileindex_path = NULL;
2748 struct got_pathlist_entry *pe;
2749 struct tree_path_data {
2750 STAILQ_ENTRY(tree_path_data) entry;
2751 struct got_object_id *tree_id;
2752 int entry_type;
2753 char *relpath;
2754 char *entry_name;
2755 } *tpd = NULL;
2756 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2758 STAILQ_INIT(&tree_paths);
2760 err = lock_worktree(worktree, LOCK_EX);
2761 if (err)
2762 return err;
2764 err = got_object_open_as_commit(&commit, repo,
2765 worktree->base_commit_id);
2766 if (err)
2767 goto done;
2769 /* Map all specified paths to in-repository trees. */
2770 TAILQ_FOREACH(pe, paths, entry) {
2771 tpd = malloc(sizeof(*tpd));
2772 if (tpd == NULL) {
2773 err = got_error_from_errno("malloc");
2774 goto done;
2777 err = find_tree_entry_for_checkout(&tpd->entry_type,
2778 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2779 worktree, repo);
2780 if (err) {
2781 free(tpd);
2782 goto done;
2785 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2786 err = got_path_basename(&tpd->entry_name, pe->path);
2787 if (err) {
2788 free(tpd->relpath);
2789 free(tpd->tree_id);
2790 free(tpd);
2791 goto done;
2793 } else
2794 tpd->entry_name = NULL;
2796 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2800 * Read the file index.
2801 * Checking out files is supposed to be an idempotent operation.
2802 * If the on-disk file index is incomplete we will try to complete it.
2804 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2805 if (err)
2806 goto done;
2808 tpd = STAILQ_FIRST(&tree_paths);
2809 TAILQ_FOREACH(pe, paths, entry) {
2810 struct bump_base_commit_id_arg bbc_arg;
2812 err = checkout_files(worktree, fileindex, tpd->relpath,
2813 tpd->tree_id, tpd->entry_name, repo,
2814 progress_cb, progress_arg, cancel_cb, cancel_arg);
2815 if (err)
2816 break;
2818 bbc_arg.base_commit_id = worktree->base_commit_id;
2819 bbc_arg.entry_name = tpd->entry_name;
2820 bbc_arg.path = pe->path;
2821 bbc_arg.path_len = pe->path_len;
2822 bbc_arg.progress_cb = progress_cb;
2823 bbc_arg.progress_arg = progress_arg;
2824 err = got_fileindex_for_each_entry_safe(fileindex,
2825 bump_base_commit_id, &bbc_arg);
2826 if (err)
2827 break;
2829 tpd = STAILQ_NEXT(tpd, entry);
2831 sync_err = sync_fileindex(fileindex, fileindex_path);
2832 if (sync_err && err == NULL)
2833 err = sync_err;
2834 done:
2835 free(fileindex_path);
2836 if (tree)
2837 got_object_tree_close(tree);
2838 if (commit)
2839 got_object_commit_close(commit);
2840 if (fileindex)
2841 got_fileindex_free(fileindex);
2842 while (!STAILQ_EMPTY(&tree_paths)) {
2843 tpd = STAILQ_FIRST(&tree_paths);
2844 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2845 free(tpd->relpath);
2846 free(tpd->tree_id);
2847 free(tpd);
2849 unlockerr = lock_worktree(worktree, LOCK_SH);
2850 if (unlockerr && err == NULL)
2851 err = unlockerr;
2852 return err;
2855 struct merge_file_cb_arg {
2856 struct got_worktree *worktree;
2857 struct got_fileindex *fileindex;
2858 got_worktree_checkout_cb progress_cb;
2859 void *progress_arg;
2860 got_cancel_cb cancel_cb;
2861 void *cancel_arg;
2862 const char *label_orig;
2863 struct got_object_id *commit_id2;
2864 int allow_bad_symlinks;
2867 static const struct got_error *
2868 merge_file_cb(void *arg, struct got_blob_object *blob1,
2869 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2870 struct got_object_id *id1, struct got_object_id *id2,
2871 const char *path1, const char *path2,
2872 mode_t mode1, mode_t mode2, struct got_repository *repo)
2874 static const struct got_error *err = NULL;
2875 struct merge_file_cb_arg *a = arg;
2876 struct got_fileindex_entry *ie;
2877 char *ondisk_path = NULL;
2878 struct stat sb;
2879 unsigned char status;
2880 int local_changes_subsumed;
2881 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2882 char *id_str = NULL, *label_deriv2 = NULL;
2884 if (blob1 && blob2) {
2885 ie = got_fileindex_entry_get(a->fileindex, path2,
2886 strlen(path2));
2887 if (ie == NULL)
2888 return (*a->progress_cb)(a->progress_arg,
2889 GOT_STATUS_MISSING, path2);
2891 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2892 path2) == -1)
2893 return got_error_from_errno("asprintf");
2895 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2896 repo);
2897 if (err)
2898 goto done;
2900 if (status == GOT_STATUS_DELETE) {
2901 err = (*a->progress_cb)(a->progress_arg,
2902 GOT_STATUS_MERGE, path2);
2903 goto done;
2905 if (status != GOT_STATUS_NO_CHANGE &&
2906 status != GOT_STATUS_MODIFY &&
2907 status != GOT_STATUS_CONFLICT &&
2908 status != GOT_STATUS_ADD) {
2909 err = (*a->progress_cb)(a->progress_arg, status, path2);
2910 goto done;
2913 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2914 char *link_target2;
2915 err = got_object_blob_read_to_str(&link_target2, blob2);
2916 if (err)
2917 goto done;
2918 err = merge_symlink(a->worktree, blob1, ondisk_path,
2919 path2, a->label_orig, link_target2, a->commit_id2,
2920 repo, a->progress_cb, a->progress_arg);
2921 free(link_target2);
2922 } else {
2923 int fd;
2925 f_orig = got_opentemp();
2926 if (f_orig == NULL) {
2927 err = got_error_from_errno("got_opentemp");
2928 goto done;
2930 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2931 f_orig, blob1);
2932 if (err)
2933 goto done;
2935 f_deriv2 = got_opentemp();
2936 if (f_deriv2 == NULL)
2937 goto done;
2938 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2939 f_deriv2, blob2);
2940 if (err)
2941 goto done;
2943 fd = open(ondisk_path,
2944 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2945 if (fd == -1) {
2946 err = got_error_from_errno2("open",
2947 ondisk_path);
2948 goto done;
2950 f_deriv = fdopen(fd, "r");
2951 if (f_deriv == NULL) {
2952 err = got_error_from_errno2("fdopen",
2953 ondisk_path);
2954 close(fd);
2955 goto done;
2957 err = got_object_id_str(&id_str, a->commit_id2);
2958 if (err)
2959 goto done;
2960 if (asprintf(&label_deriv2, "%s: commit %s",
2961 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2962 err = got_error_from_errno("asprintf");
2963 goto done;
2965 err = merge_file(&local_changes_subsumed, a->worktree,
2966 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2967 mode2, a->label_orig, NULL, label_deriv2,
2968 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2969 a->progress_cb, a->progress_arg);
2971 } else if (blob1) {
2972 ie = got_fileindex_entry_get(a->fileindex, path1,
2973 strlen(path1));
2974 if (ie == NULL)
2975 return (*a->progress_cb)(a->progress_arg,
2976 GOT_STATUS_MISSING, path1);
2978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2979 path1) == -1)
2980 return got_error_from_errno("asprintf");
2982 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2983 repo);
2984 if (err)
2985 goto done;
2987 switch (status) {
2988 case GOT_STATUS_NO_CHANGE:
2989 err = (*a->progress_cb)(a->progress_arg,
2990 GOT_STATUS_DELETE, path1);
2991 if (err)
2992 goto done;
2993 err = remove_ondisk_file(a->worktree->root_path, path1);
2994 if (err)
2995 goto done;
2996 if (ie)
2997 got_fileindex_entry_mark_deleted_from_disk(ie);
2998 break;
2999 case GOT_STATUS_DELETE:
3000 case GOT_STATUS_MISSING:
3001 err = (*a->progress_cb)(a->progress_arg,
3002 GOT_STATUS_DELETE, path1);
3003 if (err)
3004 goto done;
3005 if (ie)
3006 got_fileindex_entry_mark_deleted_from_disk(ie);
3007 break;
3008 case GOT_STATUS_ADD: {
3009 struct got_object_id *id;
3010 FILE *blob1_f;
3011 off_t blob1_size;
3013 * Delete the added file only if its content already
3014 * exists in the repository.
3016 err = got_object_blob_file_create(&id, &blob1_f,
3017 &blob1_size, path1);
3018 if (err)
3019 goto done;
3020 if (got_object_id_cmp(id, id1) == 0) {
3021 err = (*a->progress_cb)(a->progress_arg,
3022 GOT_STATUS_DELETE, path1);
3023 if (err)
3024 goto done;
3025 err = remove_ondisk_file(a->worktree->root_path,
3026 path1);
3027 if (err)
3028 goto done;
3029 if (ie)
3030 got_fileindex_entry_remove(a->fileindex,
3031 ie);
3032 } else {
3033 err = (*a->progress_cb)(a->progress_arg,
3034 GOT_STATUS_CANNOT_DELETE, path1);
3036 if (fclose(blob1_f) == EOF && err == NULL)
3037 err = got_error_from_errno("fclose");
3038 free(id);
3039 if (err)
3040 goto done;
3041 break;
3043 case GOT_STATUS_MODIFY:
3044 case GOT_STATUS_CONFLICT:
3045 err = (*a->progress_cb)(a->progress_arg,
3046 GOT_STATUS_CANNOT_DELETE, path1);
3047 if (err)
3048 goto done;
3049 break;
3050 case GOT_STATUS_OBSTRUCTED:
3051 err = (*a->progress_cb)(a->progress_arg, status, path1);
3052 if (err)
3053 goto done;
3054 break;
3055 default:
3056 break;
3058 } else if (blob2) {
3059 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3060 path2) == -1)
3061 return got_error_from_errno("asprintf");
3062 ie = got_fileindex_entry_get(a->fileindex, path2,
3063 strlen(path2));
3064 if (ie) {
3065 err = get_file_status(&status, &sb, ie, ondisk_path,
3066 -1, NULL, repo);
3067 if (err)
3068 goto done;
3069 if (status != GOT_STATUS_NO_CHANGE &&
3070 status != GOT_STATUS_MODIFY &&
3071 status != GOT_STATUS_CONFLICT &&
3072 status != GOT_STATUS_ADD) {
3073 err = (*a->progress_cb)(a->progress_arg,
3074 status, path2);
3075 goto done;
3077 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3078 char *link_target2;
3079 err = got_object_blob_read_to_str(&link_target2,
3080 blob2);
3081 if (err)
3082 goto done;
3083 err = merge_symlink(a->worktree, NULL,
3084 ondisk_path, path2, a->label_orig,
3085 link_target2, a->commit_id2, repo,
3086 a->progress_cb, a->progress_arg);
3087 free(link_target2);
3088 } else if (S_ISREG(sb.st_mode)) {
3089 err = merge_blob(&local_changes_subsumed,
3090 a->worktree, NULL, ondisk_path, path2,
3091 sb.st_mode, a->label_orig, blob2,
3092 a->commit_id2, repo, a->progress_cb,
3093 a->progress_arg);
3094 } else {
3095 err = got_error_path(ondisk_path,
3096 GOT_ERR_FILE_OBSTRUCTED);
3098 if (err)
3099 goto done;
3100 if (status == GOT_STATUS_DELETE) {
3101 err = got_fileindex_entry_update(ie,
3102 a->worktree->root_fd, path2, blob2->id.sha1,
3103 a->worktree->base_commit_id->sha1, 0);
3104 if (err)
3105 goto done;
3107 } else {
3108 int is_bad_symlink = 0;
3109 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3110 if (S_ISLNK(mode2)) {
3111 err = install_symlink(&is_bad_symlink,
3112 a->worktree, ondisk_path, path2, blob2, 0,
3113 0, 1, a->allow_bad_symlinks, repo,
3114 a->progress_cb, a->progress_arg);
3115 } else {
3116 err = install_blob(a->worktree, ondisk_path, path2,
3117 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3118 a->progress_cb, a->progress_arg);
3120 if (err)
3121 goto done;
3122 err = got_fileindex_entry_alloc(&ie, path2);
3123 if (err)
3124 goto done;
3125 err = got_fileindex_entry_update(ie,
3126 a->worktree->root_fd, path2, NULL, NULL, 1);
3127 if (err) {
3128 got_fileindex_entry_free(ie);
3129 goto done;
3131 err = got_fileindex_entry_add(a->fileindex, ie);
3132 if (err) {
3133 got_fileindex_entry_free(ie);
3134 goto done;
3136 if (is_bad_symlink) {
3137 got_fileindex_entry_filetype_set(ie,
3138 GOT_FILEIDX_MODE_BAD_SYMLINK);
3142 done:
3143 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3144 err = got_error_from_errno("fclose");
3145 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3146 err = got_error_from_errno("fclose");
3147 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3148 err = got_error_from_errno("fclose");
3149 free(id_str);
3150 free(label_deriv2);
3151 free(ondisk_path);
3152 return err;
3155 static const struct got_error *
3156 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3158 struct got_worktree *worktree = arg;
3160 /* Reject merges into a work tree with mixed base commits. */
3161 if (got_fileindex_entry_has_commit(ie) &&
3162 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3163 SHA1_DIGEST_LENGTH) != 0)
3164 return got_error(GOT_ERR_MIXED_COMMITS);
3166 return NULL;
3169 struct check_merge_conflicts_arg {
3170 struct got_worktree *worktree;
3171 struct got_fileindex *fileindex;
3172 struct got_repository *repo;
3175 static const struct got_error *
3176 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3177 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3178 struct got_object_id *id1, struct got_object_id *id2,
3179 const char *path1, const char *path2,
3180 mode_t mode1, mode_t mode2, struct got_repository *repo)
3182 const struct got_error *err = NULL;
3183 struct check_merge_conflicts_arg *a = arg;
3184 unsigned char status;
3185 struct stat sb;
3186 struct got_fileindex_entry *ie;
3187 const char *path = path2 ? path2 : path1;
3188 struct got_object_id *id = id2 ? id2 : id1;
3189 char *ondisk_path;
3191 if (id == NULL)
3192 return NULL;
3194 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3195 if (ie == NULL)
3196 return NULL;
3198 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3199 == -1)
3200 return got_error_from_errno("asprintf");
3202 /* Reject merges into a work tree with conflicted files. */
3203 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3204 free(ondisk_path);
3205 if (err)
3206 return err;
3207 if (status == GOT_STATUS_CONFLICT)
3208 return got_error(GOT_ERR_CONFLICTS);
3210 return NULL;
3213 static const struct got_error *
3214 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3215 const char *fileindex_path, struct got_object_id *commit_id1,
3216 struct got_object_id *commit_id2, struct got_repository *repo,
3217 got_worktree_checkout_cb progress_cb, void *progress_arg,
3218 got_cancel_cb cancel_cb, void *cancel_arg)
3220 const struct got_error *err = NULL, *sync_err;
3221 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3222 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3223 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3224 struct check_merge_conflicts_arg cmc_arg;
3225 struct merge_file_cb_arg arg;
3226 char *label_orig = NULL;
3227 FILE *f1 = NULL, *f2 = NULL;
3228 int fd1 = -1, fd2 = -1;
3230 if (commit_id1) {
3231 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3232 if (err)
3233 goto done;
3234 err = got_object_id_by_path(&tree_id1, repo, commit1,
3235 worktree->path_prefix);
3236 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3237 goto done;
3239 if (tree_id1) {
3240 char *id_str;
3242 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3243 if (err)
3244 goto done;
3246 err = got_object_id_str(&id_str, commit_id1);
3247 if (err)
3248 goto done;
3250 if (asprintf(&label_orig, "%s: commit %s",
3251 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3252 err = got_error_from_errno("asprintf");
3253 free(id_str);
3254 goto done;
3256 free(id_str);
3258 f1 = got_opentemp();
3259 if (f1 == NULL) {
3260 err = got_error_from_errno("got_opentemp");
3261 goto done;
3265 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3266 if (err)
3267 goto done;
3269 err = got_object_id_by_path(&tree_id2, repo, commit2,
3270 worktree->path_prefix);
3271 if (err)
3272 goto done;
3274 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3275 if (err)
3276 goto done;
3278 f2 = got_opentemp();
3279 if (f2 == NULL) {
3280 err = got_error_from_errno("got_opentemp");
3281 goto done;
3284 fd1 = got_opentempfd();
3285 if (fd1 == -1) {
3286 err = got_error_from_errno("got_opentempfd");
3287 goto done;
3290 fd2 = got_opentempfd();
3291 if (fd2 == -1) {
3292 err = got_error_from_errno("got_opentempfd");
3293 goto done;
3296 cmc_arg.worktree = worktree;
3297 cmc_arg.fileindex = fileindex;
3298 cmc_arg.repo = repo;
3299 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3300 check_merge_conflicts, &cmc_arg, 0);
3301 if (err)
3302 goto done;
3304 arg.worktree = worktree;
3305 arg.fileindex = fileindex;
3306 arg.progress_cb = progress_cb;
3307 arg.progress_arg = progress_arg;
3308 arg.cancel_cb = cancel_cb;
3309 arg.cancel_arg = cancel_arg;
3310 arg.label_orig = label_orig;
3311 arg.commit_id2 = commit_id2;
3312 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3313 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3314 merge_file_cb, &arg, 1);
3315 sync_err = sync_fileindex(fileindex, fileindex_path);
3316 if (sync_err && err == NULL)
3317 err = sync_err;
3318 done:
3319 if (commit1)
3320 got_object_commit_close(commit1);
3321 if (commit2)
3322 got_object_commit_close(commit2);
3323 if (tree1)
3324 got_object_tree_close(tree1);
3325 if (tree2)
3326 got_object_tree_close(tree2);
3327 if (f1 && fclose(f1) == EOF && err == NULL)
3328 err = got_error_from_errno("fclose");
3329 if (f2 && fclose(f2) == EOF && err == NULL)
3330 err = got_error_from_errno("fclose");
3331 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3332 err = got_error_from_errno("close");
3333 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3334 err = got_error_from_errno("close");
3335 free(label_orig);
3336 return err;
3339 const struct got_error *
3340 got_worktree_merge_files(struct got_worktree *worktree,
3341 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3342 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3343 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3345 const struct got_error *err, *unlockerr;
3346 char *fileindex_path = NULL;
3347 struct got_fileindex *fileindex = NULL;
3349 err = lock_worktree(worktree, LOCK_EX);
3350 if (err)
3351 return err;
3353 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3354 if (err)
3355 goto done;
3357 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3358 worktree);
3359 if (err)
3360 goto done;
3362 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3363 commit_id2, repo, progress_cb, progress_arg,
3364 cancel_cb, cancel_arg);
3365 done:
3366 if (fileindex)
3367 got_fileindex_free(fileindex);
3368 free(fileindex_path);
3369 unlockerr = lock_worktree(worktree, LOCK_SH);
3370 if (unlockerr && err == NULL)
3371 err = unlockerr;
3372 return err;
3375 struct diff_dir_cb_arg {
3376 struct got_fileindex *fileindex;
3377 struct got_worktree *worktree;
3378 const char *status_path;
3379 size_t status_path_len;
3380 struct got_repository *repo;
3381 got_worktree_status_cb status_cb;
3382 void *status_arg;
3383 got_cancel_cb cancel_cb;
3384 void *cancel_arg;
3385 /* A pathlist containing per-directory pathlists of ignore patterns. */
3386 struct got_pathlist_head *ignores;
3387 int report_unchanged;
3388 int no_ignores;
3391 static const struct got_error *
3392 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3393 int dirfd, const char *de_name,
3394 got_worktree_status_cb status_cb, void *status_arg,
3395 struct got_repository *repo, int report_unchanged)
3397 const struct got_error *err = NULL;
3398 unsigned char status = GOT_STATUS_NO_CHANGE;
3399 unsigned char staged_status;
3400 struct stat sb;
3401 struct got_object_id blob_id, commit_id, staged_blob_id;
3402 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3403 struct got_object_id *staged_blob_idp = NULL;
3405 staged_status = get_staged_status(ie);
3406 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3407 if (err)
3408 return err;
3410 if (status == GOT_STATUS_NO_CHANGE &&
3411 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3412 return NULL;
3414 if (got_fileindex_entry_has_blob(ie))
3415 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3416 if (got_fileindex_entry_has_commit(ie))
3417 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3418 if (staged_status == GOT_STATUS_ADD ||
3419 staged_status == GOT_STATUS_MODIFY) {
3420 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3421 &staged_blob_id, ie);
3424 return (*status_cb)(status_arg, status, staged_status,
3425 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3428 static const struct got_error *
3429 status_old_new(void *arg, struct got_fileindex_entry *ie,
3430 struct dirent *de, const char *parent_path, int dirfd)
3432 const struct got_error *err = NULL;
3433 struct diff_dir_cb_arg *a = arg;
3434 char *abspath;
3436 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3437 return got_error(GOT_ERR_CANCELLED);
3439 if (got_path_cmp(parent_path, a->status_path,
3440 strlen(parent_path), a->status_path_len) != 0 &&
3441 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3442 return NULL;
3444 if (parent_path[0]) {
3445 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3446 parent_path, de->d_name) == -1)
3447 return got_error_from_errno("asprintf");
3448 } else {
3449 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3450 de->d_name) == -1)
3451 return got_error_from_errno("asprintf");
3454 err = report_file_status(ie, abspath, dirfd, de->d_name,
3455 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3456 free(abspath);
3457 return err;
3460 static const struct got_error *
3461 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3463 struct diff_dir_cb_arg *a = arg;
3464 struct got_object_id blob_id, commit_id;
3465 unsigned char status;
3467 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3468 return got_error(GOT_ERR_CANCELLED);
3470 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3471 return NULL;
3473 got_fileindex_entry_get_blob_id(&blob_id, ie);
3474 got_fileindex_entry_get_commit_id(&commit_id, ie);
3475 if (got_fileindex_entry_has_file_on_disk(ie))
3476 status = GOT_STATUS_MISSING;
3477 else
3478 status = GOT_STATUS_DELETE;
3479 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3480 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3483 static void
3484 free_ignores(struct got_pathlist_head *ignores)
3486 struct got_pathlist_entry *pe;
3488 TAILQ_FOREACH(pe, ignores, entry) {
3489 struct got_pathlist_head *ignorelist = pe->data;
3491 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3493 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3496 static const struct got_error *
3497 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3499 const struct got_error *err = NULL;
3500 struct got_pathlist_entry *pe = NULL;
3501 struct got_pathlist_head *ignorelist;
3502 char *line = NULL, *pattern, *dirpath = NULL;
3503 size_t linesize = 0;
3504 ssize_t linelen;
3506 ignorelist = calloc(1, sizeof(*ignorelist));
3507 if (ignorelist == NULL)
3508 return got_error_from_errno("calloc");
3509 TAILQ_INIT(ignorelist);
3511 while ((linelen = getline(&line, &linesize, f)) != -1) {
3512 if (linelen > 0 && line[linelen - 1] == '\n')
3513 line[linelen - 1] = '\0';
3515 /* Git's ignores may contain comments. */
3516 if (line[0] == '#')
3517 continue;
3519 /* Git's negated patterns are not (yet?) supported. */
3520 if (line[0] == '!')
3521 continue;
3523 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3524 line) == -1) {
3525 err = got_error_from_errno("asprintf");
3526 goto done;
3528 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3529 if (err)
3530 goto done;
3532 if (ferror(f)) {
3533 err = got_error_from_errno("getline");
3534 goto done;
3537 dirpath = strdup(path);
3538 if (dirpath == NULL) {
3539 err = got_error_from_errno("strdup");
3540 goto done;
3542 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3543 done:
3544 free(line);
3545 if (err || pe == NULL) {
3546 free(dirpath);
3547 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3549 return err;
3552 static int
3553 match_path(const char *pattern, size_t pattern_len, const char *path,
3554 int flags)
3556 char buf[PATH_MAX];
3559 * Trailing slashes signify directories.
3560 * Append a * to make such patterns conform to fnmatch rules.
3562 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3563 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3564 return FNM_NOMATCH; /* XXX */
3566 return fnmatch(buf, path, flags);
3569 return fnmatch(pattern, path, flags);
3572 static int
3573 match_ignores(struct got_pathlist_head *ignores, const char *path)
3575 struct got_pathlist_entry *pe;
3577 /* Handle patterns which match in all directories. */
3578 TAILQ_FOREACH(pe, ignores, entry) {
3579 struct got_pathlist_head *ignorelist = pe->data;
3580 struct got_pathlist_entry *pi;
3582 TAILQ_FOREACH(pi, ignorelist, entry) {
3583 const char *p;
3585 if (pi->path_len < 3 ||
3586 strncmp(pi->path, "**/", 3) != 0)
3587 continue;
3588 p = path;
3589 while (*p) {
3590 if (match_path(pi->path + 3,
3591 pi->path_len - 3, p,
3592 FNM_PATHNAME | FNM_LEADING_DIR)) {
3593 /* Retry in next directory. */
3594 while (*p && *p != '/')
3595 p++;
3596 while (*p == '/')
3597 p++;
3598 continue;
3600 return 1;
3606 * The ignores pathlist contains ignore lists from children before
3607 * parents, so we can find the most specific ignorelist by walking
3608 * ignores backwards.
3610 pe = TAILQ_LAST(ignores, got_pathlist_head);
3611 while (pe) {
3612 if (got_path_is_child(path, pe->path, pe->path_len)) {
3613 struct got_pathlist_head *ignorelist = pe->data;
3614 struct got_pathlist_entry *pi;
3615 TAILQ_FOREACH(pi, ignorelist, entry) {
3616 int flags = FNM_LEADING_DIR;
3617 if (strstr(pi->path, "/**/") == NULL)
3618 flags |= FNM_PATHNAME;
3619 if (match_path(pi->path, pi->path_len,
3620 path, flags))
3621 continue;
3622 return 1;
3625 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3628 return 0;
3631 static const struct got_error *
3632 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3633 const char *path, int dirfd, const char *ignores_filename)
3635 const struct got_error *err = NULL;
3636 char *ignorespath;
3637 int fd = -1;
3638 FILE *ignoresfile = NULL;
3640 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3641 path[0] ? "/" : "", ignores_filename) == -1)
3642 return got_error_from_errno("asprintf");
3644 if (dirfd != -1) {
3645 fd = openat(dirfd, ignores_filename,
3646 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3647 if (fd == -1) {
3648 if (errno != ENOENT && errno != EACCES)
3649 err = got_error_from_errno2("openat",
3650 ignorespath);
3651 } else {
3652 ignoresfile = fdopen(fd, "r");
3653 if (ignoresfile == NULL)
3654 err = got_error_from_errno2("fdopen",
3655 ignorespath);
3656 else {
3657 fd = -1;
3658 err = read_ignores(ignores, path, ignoresfile);
3661 } else {
3662 ignoresfile = fopen(ignorespath, "re");
3663 if (ignoresfile == NULL) {
3664 if (errno != ENOENT && errno != EACCES)
3665 err = got_error_from_errno2("fopen",
3666 ignorespath);
3667 } else
3668 err = read_ignores(ignores, path, ignoresfile);
3671 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3672 err = got_error_from_errno2("fclose", path);
3673 if (fd != -1 && close(fd) == -1 && err == NULL)
3674 err = got_error_from_errno2("close", path);
3675 free(ignorespath);
3676 return err;
3679 static const struct got_error *
3680 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3681 int dirfd)
3683 const struct got_error *err = NULL;
3684 struct diff_dir_cb_arg *a = arg;
3685 char *path = NULL;
3687 if (ignore != NULL)
3688 *ignore = 0;
3690 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3691 return got_error(GOT_ERR_CANCELLED);
3693 if (parent_path[0]) {
3694 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3695 return got_error_from_errno("asprintf");
3696 } else {
3697 path = de->d_name;
3700 if (de->d_type == DT_DIR) {
3701 if (!a->no_ignores && ignore != NULL &&
3702 match_ignores(a->ignores, path))
3703 *ignore = 1;
3704 } else if (!match_ignores(a->ignores, path) &&
3705 got_path_is_child(path, a->status_path, a->status_path_len))
3706 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3707 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3708 if (parent_path[0])
3709 free(path);
3710 return err;
3713 static const struct got_error *
3714 status_traverse(void *arg, const char *path, int dirfd)
3716 const struct got_error *err = NULL;
3717 struct diff_dir_cb_arg *a = arg;
3719 if (a->no_ignores)
3720 return NULL;
3722 err = add_ignores(a->ignores, a->worktree->root_path,
3723 path, dirfd, ".cvsignore");
3724 if (err)
3725 return err;
3727 err = add_ignores(a->ignores, a->worktree->root_path, path,
3728 dirfd, ".gitignore");
3730 return err;
3733 static const struct got_error *
3734 report_single_file_status(const char *path, const char *ondisk_path,
3735 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3736 void *status_arg, struct got_repository *repo, int report_unchanged,
3737 struct got_pathlist_head *ignores, int no_ignores)
3739 struct got_fileindex_entry *ie;
3740 struct stat sb;
3742 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3743 if (ie)
3744 return report_file_status(ie, ondisk_path, -1, NULL,
3745 status_cb, status_arg, repo, report_unchanged);
3747 if (lstat(ondisk_path, &sb) == -1) {
3748 if (errno != ENOENT)
3749 return got_error_from_errno2("lstat", ondisk_path);
3750 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3751 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3754 if (!no_ignores && match_ignores(ignores, path))
3755 return NULL;
3757 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3758 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3759 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3761 return NULL;
3764 static const struct got_error *
3765 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3766 const char *root_path, const char *path)
3768 const struct got_error *err;
3769 char *parent_path, *next_parent_path = NULL;
3771 err = add_ignores(ignores, root_path, "", -1,
3772 ".cvsignore");
3773 if (err)
3774 return err;
3776 err = add_ignores(ignores, root_path, "", -1,
3777 ".gitignore");
3778 if (err)
3779 return err;
3781 err = got_path_dirname(&parent_path, path);
3782 if (err) {
3783 if (err->code == GOT_ERR_BAD_PATH)
3784 return NULL; /* cannot traverse parent */
3785 return err;
3787 for (;;) {
3788 err = add_ignores(ignores, root_path, parent_path, -1,
3789 ".cvsignore");
3790 if (err)
3791 break;
3792 err = add_ignores(ignores, root_path, parent_path, -1,
3793 ".gitignore");
3794 if (err)
3795 break;
3796 err = got_path_dirname(&next_parent_path, parent_path);
3797 if (err) {
3798 if (err->code == GOT_ERR_BAD_PATH)
3799 err = NULL; /* traversed everything */
3800 break;
3802 if (got_path_is_root_dir(parent_path))
3803 break;
3804 free(parent_path);
3805 parent_path = next_parent_path;
3806 next_parent_path = NULL;
3809 free(parent_path);
3810 free(next_parent_path);
3811 return err;
3814 static const struct got_error *
3815 worktree_status(struct got_worktree *worktree, const char *path,
3816 struct got_fileindex *fileindex, struct got_repository *repo,
3817 got_worktree_status_cb status_cb, void *status_arg,
3818 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3819 int report_unchanged)
3821 const struct got_error *err = NULL;
3822 int fd = -1;
3823 struct got_fileindex_diff_dir_cb fdiff_cb;
3824 struct diff_dir_cb_arg arg;
3825 char *ondisk_path = NULL;
3826 struct got_pathlist_head ignores;
3827 struct got_fileindex_entry *ie;
3829 TAILQ_INIT(&ignores);
3831 if (asprintf(&ondisk_path, "%s%s%s",
3832 worktree->root_path, path[0] ? "/" : "", path) == -1)
3833 return got_error_from_errno("asprintf");
3835 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3836 if (ie) {
3837 err = report_single_file_status(path, ondisk_path,
3838 fileindex, status_cb, status_arg, repo,
3839 report_unchanged, &ignores, no_ignores);
3840 goto done;
3843 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3844 if (fd == -1) {
3845 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3846 !got_err_open_nofollow_on_symlink())
3847 err = got_error_from_errno2("open", ondisk_path);
3848 else {
3849 if (!no_ignores) {
3850 err = add_ignores_from_parent_paths(&ignores,
3851 worktree->root_path, ondisk_path);
3852 if (err)
3853 goto done;
3855 err = report_single_file_status(path, ondisk_path,
3856 fileindex, status_cb, status_arg, repo,
3857 report_unchanged, &ignores, no_ignores);
3859 } else {
3860 fdiff_cb.diff_old_new = status_old_new;
3861 fdiff_cb.diff_old = status_old;
3862 fdiff_cb.diff_new = status_new;
3863 fdiff_cb.diff_traverse = status_traverse;
3864 arg.fileindex = fileindex;
3865 arg.worktree = worktree;
3866 arg.status_path = path;
3867 arg.status_path_len = strlen(path);
3868 arg.repo = repo;
3869 arg.status_cb = status_cb;
3870 arg.status_arg = status_arg;
3871 arg.cancel_cb = cancel_cb;
3872 arg.cancel_arg = cancel_arg;
3873 arg.report_unchanged = report_unchanged;
3874 arg.no_ignores = no_ignores;
3875 if (!no_ignores) {
3876 err = add_ignores_from_parent_paths(&ignores,
3877 worktree->root_path, path);
3878 if (err)
3879 goto done;
3881 arg.ignores = &ignores;
3882 err = got_fileindex_diff_dir(fileindex, fd,
3883 worktree->root_path, path, repo, &fdiff_cb, &arg);
3885 done:
3886 free_ignores(&ignores);
3887 if (fd != -1 && close(fd) == -1 && err == NULL)
3888 err = got_error_from_errno("close");
3889 free(ondisk_path);
3890 return err;
3893 const struct got_error *
3894 got_worktree_status(struct got_worktree *worktree,
3895 struct got_pathlist_head *paths, struct got_repository *repo,
3896 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3897 got_cancel_cb cancel_cb, void *cancel_arg)
3899 const struct got_error *err = NULL;
3900 char *fileindex_path = NULL;
3901 struct got_fileindex *fileindex = NULL;
3902 struct got_pathlist_entry *pe;
3904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3905 if (err)
3906 return err;
3908 TAILQ_FOREACH(pe, paths, entry) {
3909 err = worktree_status(worktree, pe->path, fileindex, repo,
3910 status_cb, status_arg, cancel_cb, cancel_arg,
3911 no_ignores, 0);
3912 if (err)
3913 break;
3915 free(fileindex_path);
3916 got_fileindex_free(fileindex);
3917 return err;
3920 const struct got_error *
3921 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3922 const char *arg)
3924 const struct got_error *err = NULL;
3925 char *resolved = NULL, *cwd = NULL, *path = NULL;
3926 size_t len;
3927 struct stat sb;
3928 char *abspath = NULL;
3929 char canonpath[PATH_MAX];
3931 *wt_path = NULL;
3933 cwd = getcwd(NULL, 0);
3934 if (cwd == NULL)
3935 return got_error_from_errno("getcwd");
3937 if (lstat(arg, &sb) == -1) {
3938 if (errno != ENOENT) {
3939 err = got_error_from_errno2("lstat", arg);
3940 goto done;
3942 sb.st_mode = 0;
3944 if (S_ISLNK(sb.st_mode)) {
3946 * We cannot use realpath(3) with symlinks since we want to
3947 * operate on the symlink itself.
3948 * But we can make the path absolute, assuming it is relative
3949 * to the current working directory, and then canonicalize it.
3951 if (!got_path_is_absolute(arg)) {
3952 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3953 err = got_error_from_errno("asprintf");
3954 goto done;
3958 err = got_canonpath(abspath ? abspath : arg, canonpath,
3959 sizeof(canonpath));
3960 if (err)
3961 goto done;
3962 resolved = strdup(canonpath);
3963 if (resolved == NULL) {
3964 err = got_error_from_errno("strdup");
3965 goto done;
3967 } else {
3968 resolved = realpath(arg, NULL);
3969 if (resolved == NULL) {
3970 if (errno != ENOENT) {
3971 err = got_error_from_errno2("realpath", arg);
3972 goto done;
3974 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3975 err = got_error_from_errno("asprintf");
3976 goto done;
3978 err = got_canonpath(abspath, canonpath,
3979 sizeof(canonpath));
3980 if (err)
3981 goto done;
3982 resolved = strdup(canonpath);
3983 if (resolved == NULL) {
3984 err = got_error_from_errno("strdup");
3985 goto done;
3990 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3991 strlen(got_worktree_get_root_path(worktree)))) {
3992 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3993 goto done;
3996 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3997 err = got_path_skip_common_ancestor(&path,
3998 got_worktree_get_root_path(worktree), resolved);
3999 if (err)
4000 goto done;
4001 } else {
4002 path = strdup("");
4003 if (path == NULL) {
4004 err = got_error_from_errno("strdup");
4005 goto done;
4009 /* XXX status walk can't deal with trailing slash! */
4010 len = strlen(path);
4011 while (len > 0 && path[len - 1] == '/') {
4012 path[len - 1] = '\0';
4013 len--;
4015 done:
4016 free(abspath);
4017 free(resolved);
4018 free(cwd);
4019 if (err == NULL)
4020 *wt_path = path;
4021 else
4022 free(path);
4023 return err;
4026 struct schedule_addition_args {
4027 struct got_worktree *worktree;
4028 struct got_fileindex *fileindex;
4029 got_worktree_checkout_cb progress_cb;
4030 void *progress_arg;
4031 struct got_repository *repo;
4034 static const struct got_error *
4035 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4036 const char *relpath, struct got_object_id *blob_id,
4037 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4038 int dirfd, const char *de_name)
4040 struct schedule_addition_args *a = arg;
4041 const struct got_error *err = NULL;
4042 struct got_fileindex_entry *ie;
4043 struct stat sb;
4044 char *ondisk_path;
4046 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4047 relpath) == -1)
4048 return got_error_from_errno("asprintf");
4050 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4051 if (ie) {
4052 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4053 de_name, a->repo);
4054 if (err)
4055 goto done;
4056 /* Re-adding an existing entry is a no-op. */
4057 if (status == GOT_STATUS_ADD)
4058 goto done;
4059 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4060 if (err)
4061 goto done;
4064 if (status != GOT_STATUS_UNVERSIONED) {
4065 if (status == GOT_STATUS_NONEXISTENT)
4066 err = got_error_set_errno(ENOENT, ondisk_path);
4067 else
4068 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4069 goto done;
4072 err = got_fileindex_entry_alloc(&ie, relpath);
4073 if (err)
4074 goto done;
4075 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4076 relpath, NULL, NULL, 1);
4077 if (err) {
4078 got_fileindex_entry_free(ie);
4079 goto done;
4081 err = got_fileindex_entry_add(a->fileindex, ie);
4082 if (err) {
4083 got_fileindex_entry_free(ie);
4084 goto done;
4086 done:
4087 free(ondisk_path);
4088 if (err)
4089 return err;
4090 if (status == GOT_STATUS_ADD)
4091 return NULL;
4092 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4095 const struct got_error *
4096 got_worktree_schedule_add(struct got_worktree *worktree,
4097 struct got_pathlist_head *paths,
4098 got_worktree_checkout_cb progress_cb, void *progress_arg,
4099 struct got_repository *repo, int no_ignores)
4101 struct got_fileindex *fileindex = NULL;
4102 char *fileindex_path = NULL;
4103 const struct got_error *err = NULL, *sync_err, *unlockerr;
4104 struct got_pathlist_entry *pe;
4105 struct schedule_addition_args saa;
4107 err = lock_worktree(worktree, LOCK_EX);
4108 if (err)
4109 return err;
4111 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4112 if (err)
4113 goto done;
4115 saa.worktree = worktree;
4116 saa.fileindex = fileindex;
4117 saa.progress_cb = progress_cb;
4118 saa.progress_arg = progress_arg;
4119 saa.repo = repo;
4121 TAILQ_FOREACH(pe, paths, entry) {
4122 err = worktree_status(worktree, pe->path, fileindex, repo,
4123 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4124 if (err)
4125 break;
4127 sync_err = sync_fileindex(fileindex, fileindex_path);
4128 if (sync_err && err == NULL)
4129 err = sync_err;
4130 done:
4131 free(fileindex_path);
4132 if (fileindex)
4133 got_fileindex_free(fileindex);
4134 unlockerr = lock_worktree(worktree, LOCK_SH);
4135 if (unlockerr && err == NULL)
4136 err = unlockerr;
4137 return err;
4140 struct schedule_deletion_args {
4141 struct got_worktree *worktree;
4142 struct got_fileindex *fileindex;
4143 got_worktree_delete_cb progress_cb;
4144 void *progress_arg;
4145 struct got_repository *repo;
4146 int delete_local_mods;
4147 int keep_on_disk;
4148 int ignore_missing_paths;
4149 const char *status_codes;
4152 static const struct got_error *
4153 schedule_for_deletion(void *arg, unsigned char status,
4154 unsigned char staged_status, const char *relpath,
4155 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4156 struct got_object_id *commit_id, int dirfd, const char *de_name)
4158 struct schedule_deletion_args *a = arg;
4159 const struct got_error *err = NULL;
4160 struct got_fileindex_entry *ie = NULL;
4161 struct stat sb;
4162 char *ondisk_path;
4164 if (status == GOT_STATUS_NONEXISTENT) {
4165 if (a->ignore_missing_paths)
4166 return NULL;
4167 return got_error_set_errno(ENOENT, relpath);
4170 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4171 if (ie == NULL)
4172 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4174 staged_status = get_staged_status(ie);
4175 if (staged_status != GOT_STATUS_NO_CHANGE) {
4176 if (staged_status == GOT_STATUS_DELETE)
4177 return NULL;
4178 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4181 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4182 relpath) == -1)
4183 return got_error_from_errno("asprintf");
4185 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4186 a->repo);
4187 if (err)
4188 goto done;
4190 if (a->status_codes) {
4191 size_t ncodes = strlen(a->status_codes);
4192 int i;
4193 for (i = 0; i < ncodes ; i++) {
4194 if (status == a->status_codes[i])
4195 break;
4197 if (i == ncodes) {
4198 /* Do not delete files in non-matching status. */
4199 free(ondisk_path);
4200 return NULL;
4202 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4203 a->status_codes[i] != GOT_STATUS_MISSING) {
4204 static char msg[64];
4205 snprintf(msg, sizeof(msg),
4206 "invalid status code '%c'", a->status_codes[i]);
4207 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4208 goto done;
4212 if (status != GOT_STATUS_NO_CHANGE) {
4213 if (status == GOT_STATUS_DELETE)
4214 goto done;
4215 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4216 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4217 goto done;
4219 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4220 err = got_error_set_errno(ENOENT, relpath);
4221 goto done;
4223 if (status != GOT_STATUS_MODIFY &&
4224 status != GOT_STATUS_MISSING) {
4225 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4226 goto done;
4230 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4231 size_t root_len;
4233 if (dirfd != -1) {
4234 if (unlinkat(dirfd, de_name, 0) == -1) {
4235 err = got_error_from_errno2("unlinkat",
4236 ondisk_path);
4237 goto done;
4239 } else if (unlink(ondisk_path) == -1) {
4240 err = got_error_from_errno2("unlink", ondisk_path);
4241 goto done;
4244 root_len = strlen(a->worktree->root_path);
4245 do {
4246 char *parent;
4247 err = got_path_dirname(&parent, ondisk_path);
4248 if (err)
4249 goto done;
4250 free(ondisk_path);
4251 ondisk_path = parent;
4252 if (rmdir(ondisk_path) == -1) {
4253 if (errno != ENOTEMPTY)
4254 err = got_error_from_errno2("rmdir",
4255 ondisk_path);
4256 break;
4258 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4259 strlen(ondisk_path), root_len) != 0);
4262 got_fileindex_entry_mark_deleted_from_disk(ie);
4263 done:
4264 free(ondisk_path);
4265 if (err)
4266 return err;
4267 if (status == GOT_STATUS_DELETE)
4268 return NULL;
4269 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4270 staged_status, relpath);
4273 const struct got_error *
4274 got_worktree_schedule_delete(struct got_worktree *worktree,
4275 struct got_pathlist_head *paths, int delete_local_mods,
4276 const char *status_codes,
4277 got_worktree_delete_cb progress_cb, void *progress_arg,
4278 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4280 struct got_fileindex *fileindex = NULL;
4281 char *fileindex_path = NULL;
4282 const struct got_error *err = NULL, *sync_err, *unlockerr;
4283 struct got_pathlist_entry *pe;
4284 struct schedule_deletion_args sda;
4286 err = lock_worktree(worktree, LOCK_EX);
4287 if (err)
4288 return err;
4290 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4291 if (err)
4292 goto done;
4294 sda.worktree = worktree;
4295 sda.fileindex = fileindex;
4296 sda.progress_cb = progress_cb;
4297 sda.progress_arg = progress_arg;
4298 sda.repo = repo;
4299 sda.delete_local_mods = delete_local_mods;
4300 sda.keep_on_disk = keep_on_disk;
4301 sda.ignore_missing_paths = ignore_missing_paths;
4302 sda.status_codes = status_codes;
4304 TAILQ_FOREACH(pe, paths, entry) {
4305 err = worktree_status(worktree, pe->path, fileindex, repo,
4306 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4307 if (err)
4308 break;
4310 sync_err = sync_fileindex(fileindex, fileindex_path);
4311 if (sync_err && err == NULL)
4312 err = sync_err;
4313 done:
4314 free(fileindex_path);
4315 if (fileindex)
4316 got_fileindex_free(fileindex);
4317 unlockerr = lock_worktree(worktree, LOCK_SH);
4318 if (unlockerr && err == NULL)
4319 err = unlockerr;
4320 return err;
4323 static const struct got_error *
4324 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4326 const struct got_error *err = NULL;
4327 char *line = NULL;
4328 size_t linesize = 0, n;
4329 ssize_t linelen;
4331 linelen = getline(&line, &linesize, infile);
4332 if (linelen == -1) {
4333 if (ferror(infile)) {
4334 err = got_error_from_errno("getline");
4335 goto done;
4337 return NULL;
4339 if (outfile) {
4340 n = fwrite(line, 1, linelen, outfile);
4341 if (n != linelen) {
4342 err = got_ferror(outfile, GOT_ERR_IO);
4343 goto done;
4346 if (rejectfile) {
4347 n = fwrite(line, 1, linelen, rejectfile);
4348 if (n != linelen)
4349 err = got_ferror(rejectfile, GOT_ERR_IO);
4351 done:
4352 free(line);
4353 return err;
4356 static const struct got_error *
4357 skip_one_line(FILE *f)
4359 char *line = NULL;
4360 size_t linesize = 0;
4361 ssize_t linelen;
4363 linelen = getline(&line, &linesize, f);
4364 if (linelen == -1) {
4365 if (ferror(f))
4366 return got_error_from_errno("getline");
4367 return NULL;
4369 free(line);
4370 return NULL;
4373 static const struct got_error *
4374 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4375 int start_old, int end_old, int start_new, int end_new,
4376 FILE *outfile, FILE *rejectfile)
4378 const struct got_error *err;
4380 /* Copy old file's lines leading up to patch. */
4381 while (!feof(f1) && *line_cur1 < start_old) {
4382 err = copy_one_line(f1, outfile, NULL);
4383 if (err)
4384 return err;
4385 (*line_cur1)++;
4387 /* Skip new file's lines leading up to patch. */
4388 while (!feof(f2) && *line_cur2 < start_new) {
4389 if (rejectfile)
4390 err = copy_one_line(f2, NULL, rejectfile);
4391 else
4392 err = skip_one_line(f2);
4393 if (err)
4394 return err;
4395 (*line_cur2)++;
4397 /* Copy patched lines. */
4398 while (!feof(f2) && *line_cur2 <= end_new) {
4399 err = copy_one_line(f2, outfile, NULL);
4400 if (err)
4401 return err;
4402 (*line_cur2)++;
4404 /* Skip over old file's replaced lines. */
4405 while (!feof(f1) && *line_cur1 <= end_old) {
4406 if (rejectfile)
4407 err = copy_one_line(f1, NULL, rejectfile);
4408 else
4409 err = skip_one_line(f1);
4410 if (err)
4411 return err;
4412 (*line_cur1)++;
4415 return NULL;
4418 static const struct got_error *
4419 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4420 FILE *outfile, FILE *rejectfile)
4422 const struct got_error *err;
4424 if (outfile) {
4425 /* Copy old file's lines until EOF. */
4426 while (!feof(f1)) {
4427 err = copy_one_line(f1, outfile, NULL);
4428 if (err)
4429 return err;
4430 (*line_cur1)++;
4433 if (rejectfile) {
4434 /* Copy new file's lines until EOF. */
4435 while (!feof(f2)) {
4436 err = copy_one_line(f2, NULL, rejectfile);
4437 if (err)
4438 return err;
4439 (*line_cur2)++;
4443 return NULL;
4446 static const struct got_error *
4447 apply_or_reject_change(int *choice, int *nchunks_used,
4448 struct diff_result *diff_result, int n,
4449 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4450 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4451 got_worktree_patch_cb patch_cb, void *patch_arg)
4453 const struct got_error *err = NULL;
4454 struct diff_chunk_context cc = {};
4455 int start_old, end_old, start_new, end_new;
4456 FILE *hunkfile;
4457 struct diff_output_unidiff_state *diff_state;
4458 struct diff_input_info diff_info;
4459 int rc;
4461 *choice = GOT_PATCH_CHOICE_NONE;
4463 /* Get changed line numbers without context lines for copy_change(). */
4464 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4465 start_old = cc.left.start;
4466 end_old = cc.left.end;
4467 start_new = cc.right.start;
4468 end_new = cc.right.end;
4470 /* Get the same change with context lines for display. */
4471 memset(&cc, 0, sizeof(cc));
4472 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4474 memset(&diff_info, 0, sizeof(diff_info));
4475 diff_info.left_path = relpath;
4476 diff_info.right_path = relpath;
4478 diff_state = diff_output_unidiff_state_alloc();
4479 if (diff_state == NULL)
4480 return got_error_set_errno(ENOMEM,
4481 "diff_output_unidiff_state_alloc");
4483 hunkfile = got_opentemp();
4484 if (hunkfile == NULL) {
4485 err = got_error_from_errno("got_opentemp");
4486 goto done;
4489 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4490 diff_result, &cc);
4491 if (rc != DIFF_RC_OK) {
4492 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4493 goto done;
4496 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4497 err = got_ferror(hunkfile, GOT_ERR_IO);
4498 goto done;
4501 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4502 hunkfile, changeno, nchanges);
4503 if (err)
4504 goto done;
4506 switch (*choice) {
4507 case GOT_PATCH_CHOICE_YES:
4508 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4509 end_old, start_new, end_new, outfile, rejectfile);
4510 break;
4511 case GOT_PATCH_CHOICE_NO:
4512 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4513 end_old, start_new, end_new, rejectfile, outfile);
4514 break;
4515 case GOT_PATCH_CHOICE_QUIT:
4516 break;
4517 default:
4518 err = got_error(GOT_ERR_PATCH_CHOICE);
4519 break;
4521 done:
4522 diff_output_unidiff_state_free(diff_state);
4523 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4524 err = got_error_from_errno("fclose");
4525 return err;
4528 struct revert_file_args {
4529 struct got_worktree *worktree;
4530 struct got_fileindex *fileindex;
4531 got_worktree_checkout_cb progress_cb;
4532 void *progress_arg;
4533 got_worktree_patch_cb patch_cb;
4534 void *patch_arg;
4535 struct got_repository *repo;
4536 int unlink_added_files;
4539 static const struct got_error *
4540 create_patched_content(char **path_outfile, int reverse_patch,
4541 struct got_object_id *blob_id, const char *path2,
4542 int dirfd2, const char *de_name2,
4543 const char *relpath, struct got_repository *repo,
4544 got_worktree_patch_cb patch_cb, void *patch_arg)
4546 const struct got_error *err, *free_err;
4547 struct got_blob_object *blob = NULL;
4548 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4549 int fd = -1, fd2 = -1;
4550 char link_target[PATH_MAX];
4551 ssize_t link_len = 0;
4552 char *path1 = NULL, *id_str = NULL;
4553 struct stat sb2;
4554 struct got_diffreg_result *diffreg_result = NULL;
4555 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4556 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4558 *path_outfile = NULL;
4560 err = got_object_id_str(&id_str, blob_id);
4561 if (err)
4562 return err;
4564 if (dirfd2 != -1) {
4565 fd2 = openat(dirfd2, de_name2,
4566 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4567 if (fd2 == -1) {
4568 if (!got_err_open_nofollow_on_symlink()) {
4569 err = got_error_from_errno2("openat", path2);
4570 goto done;
4572 link_len = readlinkat(dirfd2, de_name2,
4573 link_target, sizeof(link_target));
4574 if (link_len == -1) {
4575 return got_error_from_errno2("readlinkat",
4576 path2);
4578 sb2.st_mode = S_IFLNK;
4579 sb2.st_size = link_len;
4581 } else {
4582 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4583 if (fd2 == -1) {
4584 if (!got_err_open_nofollow_on_symlink()) {
4585 err = got_error_from_errno2("open", path2);
4586 goto done;
4588 link_len = readlink(path2, link_target,
4589 sizeof(link_target));
4590 if (link_len == -1)
4591 return got_error_from_errno2("readlink", path2);
4592 sb2.st_mode = S_IFLNK;
4593 sb2.st_size = link_len;
4596 if (fd2 != -1) {
4597 if (fstat(fd2, &sb2) == -1) {
4598 err = got_error_from_errno2("fstat", path2);
4599 goto done;
4602 f2 = fdopen(fd2, "r");
4603 if (f2 == NULL) {
4604 err = got_error_from_errno2("fdopen", path2);
4605 goto done;
4607 fd2 = -1;
4608 } else {
4609 size_t n;
4610 f2 = got_opentemp();
4611 if (f2 == NULL) {
4612 err = got_error_from_errno2("got_opentemp", path2);
4613 goto done;
4615 n = fwrite(link_target, 1, link_len, f2);
4616 if (n != link_len) {
4617 err = got_ferror(f2, GOT_ERR_IO);
4618 goto done;
4620 if (fflush(f2) == EOF) {
4621 err = got_error_from_errno("fflush");
4622 goto done;
4624 rewind(f2);
4627 fd = got_opentempfd();
4628 if (fd == -1) {
4629 err = got_error_from_errno("got_opentempfd");
4630 goto done;
4633 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4634 if (err)
4635 goto done;
4637 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4638 if (err)
4639 goto done;
4641 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4642 if (err)
4643 goto done;
4645 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4646 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4647 if (err)
4648 goto done;
4650 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4651 "");
4652 if (err)
4653 goto done;
4655 if (fseek(f1, 0L, SEEK_SET) == -1)
4656 return got_ferror(f1, GOT_ERR_IO);
4657 if (fseek(f2, 0L, SEEK_SET) == -1)
4658 return got_ferror(f2, GOT_ERR_IO);
4660 /* Count the number of actual changes in the diff result. */
4661 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4662 struct diff_chunk_context cc = {};
4663 diff_chunk_context_load_change(&cc, &nchunks_used,
4664 diffreg_result->result, n, 0);
4665 nchanges++;
4667 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4668 int choice;
4669 err = apply_or_reject_change(&choice, &nchunks_used,
4670 diffreg_result->result, n, relpath, f1, f2,
4671 &line_cur1, &line_cur2,
4672 reverse_patch ? NULL : outfile,
4673 reverse_patch ? outfile : NULL,
4674 ++i, nchanges, patch_cb, patch_arg);
4675 if (err)
4676 goto done;
4677 if (choice == GOT_PATCH_CHOICE_YES)
4678 have_content = 1;
4679 else if (choice == GOT_PATCH_CHOICE_QUIT)
4680 break;
4682 if (have_content) {
4683 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4684 reverse_patch ? NULL : outfile,
4685 reverse_patch ? outfile : NULL);
4686 if (err)
4687 goto done;
4689 if (!S_ISLNK(sb2.st_mode)) {
4690 mode_t mode;
4692 mode = apply_umask(sb2.st_mode);
4693 if (fchmod(fileno(outfile), mode) == -1) {
4694 err = got_error_from_errno2("fchmod", path2);
4695 goto done;
4699 done:
4700 free(id_str);
4701 if (fd != -1 && close(fd) == -1 && err == NULL)
4702 err = got_error_from_errno("close");
4703 if (blob)
4704 got_object_blob_close(blob);
4705 free_err = got_diffreg_result_free(diffreg_result);
4706 if (err == NULL)
4707 err = free_err;
4708 if (f1 && fclose(f1) == EOF && err == NULL)
4709 err = got_error_from_errno2("fclose", path1);
4710 if (f2 && fclose(f2) == EOF && err == NULL)
4711 err = got_error_from_errno2("fclose", path2);
4712 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4713 err = got_error_from_errno2("close", path2);
4714 if (outfile && fclose(outfile) == EOF && err == NULL)
4715 err = got_error_from_errno2("fclose", *path_outfile);
4716 if (path1 && unlink(path1) == -1 && err == NULL)
4717 err = got_error_from_errno2("unlink", path1);
4718 if (err || !have_content) {
4719 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4720 err = got_error_from_errno2("unlink", *path_outfile);
4721 free(*path_outfile);
4722 *path_outfile = NULL;
4724 free(path1);
4725 return err;
4728 static const struct got_error *
4729 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4730 const char *relpath, struct got_object_id *blob_id,
4731 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4732 int dirfd, const char *de_name)
4734 struct revert_file_args *a = arg;
4735 const struct got_error *err = NULL;
4736 char *parent_path = NULL;
4737 struct got_fileindex_entry *ie;
4738 struct got_commit_object *base_commit = NULL;
4739 struct got_tree_object *tree = NULL;
4740 struct got_object_id *tree_id = NULL;
4741 const struct got_tree_entry *te = NULL;
4742 char *tree_path = NULL, *te_name;
4743 char *ondisk_path = NULL, *path_content = NULL;
4744 struct got_blob_object *blob = NULL;
4745 int fd = -1;
4747 /* Reverting a staged deletion is a no-op. */
4748 if (status == GOT_STATUS_DELETE &&
4749 staged_status != GOT_STATUS_NO_CHANGE)
4750 return NULL;
4752 if (status == GOT_STATUS_UNVERSIONED)
4753 return (*a->progress_cb)(a->progress_arg,
4754 GOT_STATUS_UNVERSIONED, relpath);
4756 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4757 if (ie == NULL)
4758 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4760 /* Construct in-repository path of tree which contains this blob. */
4761 err = got_path_dirname(&parent_path, ie->path);
4762 if (err) {
4763 if (err->code != GOT_ERR_BAD_PATH)
4764 goto done;
4765 parent_path = strdup("/");
4766 if (parent_path == NULL) {
4767 err = got_error_from_errno("strdup");
4768 goto done;
4771 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4772 tree_path = strdup(parent_path);
4773 if (tree_path == NULL) {
4774 err = got_error_from_errno("strdup");
4775 goto done;
4777 } else {
4778 if (got_path_is_root_dir(parent_path)) {
4779 tree_path = strdup(a->worktree->path_prefix);
4780 if (tree_path == NULL) {
4781 err = got_error_from_errno("strdup");
4782 goto done;
4784 } else {
4785 if (asprintf(&tree_path, "%s/%s",
4786 a->worktree->path_prefix, parent_path) == -1) {
4787 err = got_error_from_errno("asprintf");
4788 goto done;
4793 err = got_object_open_as_commit(&base_commit, a->repo,
4794 a->worktree->base_commit_id);
4795 if (err)
4796 goto done;
4798 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4799 if (err) {
4800 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4801 (status == GOT_STATUS_ADD ||
4802 staged_status == GOT_STATUS_ADD)))
4803 goto done;
4804 } else {
4805 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4806 if (err)
4807 goto done;
4809 err = got_path_basename(&te_name, ie->path);
4810 if (err)
4811 goto done;
4813 te = got_object_tree_find_entry(tree, te_name);
4814 free(te_name);
4815 if (te == NULL && status != GOT_STATUS_ADD &&
4816 staged_status != GOT_STATUS_ADD) {
4817 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4818 goto done;
4822 switch (status) {
4823 case GOT_STATUS_ADD:
4824 if (a->patch_cb) {
4825 int choice = GOT_PATCH_CHOICE_NONE;
4826 err = (*a->patch_cb)(&choice, a->patch_arg,
4827 status, ie->path, NULL, 1, 1);
4828 if (err)
4829 goto done;
4830 if (choice != GOT_PATCH_CHOICE_YES)
4831 break;
4833 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4834 ie->path);
4835 if (err)
4836 goto done;
4837 got_fileindex_entry_remove(a->fileindex, ie);
4838 if (a->unlink_added_files) {
4839 if (asprintf(&ondisk_path, "%s/%s",
4840 got_worktree_get_root_path(a->worktree),
4841 relpath) == -1) {
4842 err = got_error_from_errno("asprintf");
4843 goto done;
4845 if (unlink(ondisk_path) == -1) {
4846 err = got_error_from_errno2("unlink",
4847 ondisk_path);
4848 break;
4851 break;
4852 case GOT_STATUS_DELETE:
4853 if (a->patch_cb) {
4854 int choice = GOT_PATCH_CHOICE_NONE;
4855 err = (*a->patch_cb)(&choice, a->patch_arg,
4856 status, ie->path, NULL, 1, 1);
4857 if (err)
4858 goto done;
4859 if (choice != GOT_PATCH_CHOICE_YES)
4860 break;
4862 /* fall through */
4863 case GOT_STATUS_MODIFY:
4864 case GOT_STATUS_MODE_CHANGE:
4865 case GOT_STATUS_CONFLICT:
4866 case GOT_STATUS_MISSING: {
4867 struct got_object_id id;
4868 if (staged_status == GOT_STATUS_ADD ||
4869 staged_status == GOT_STATUS_MODIFY)
4870 got_fileindex_entry_get_staged_blob_id(&id, ie);
4871 else
4872 got_fileindex_entry_get_blob_id(&id, ie);
4873 fd = got_opentempfd();
4874 if (fd == -1) {
4875 err = got_error_from_errno("got_opentempfd");
4876 goto done;
4879 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4880 if (err)
4881 goto done;
4883 if (asprintf(&ondisk_path, "%s/%s",
4884 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4885 err = got_error_from_errno("asprintf");
4886 goto done;
4889 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4890 status == GOT_STATUS_CONFLICT)) {
4891 int is_bad_symlink = 0;
4892 err = create_patched_content(&path_content, 1, &id,
4893 ondisk_path, dirfd, de_name, ie->path, a->repo,
4894 a->patch_cb, a->patch_arg);
4895 if (err || path_content == NULL)
4896 break;
4897 if (te && S_ISLNK(te->mode)) {
4898 if (unlink(path_content) == -1) {
4899 err = got_error_from_errno2("unlink",
4900 path_content);
4901 break;
4903 err = install_symlink(&is_bad_symlink,
4904 a->worktree, ondisk_path, ie->path,
4905 blob, 0, 1, 0, 0, a->repo,
4906 a->progress_cb, a->progress_arg);
4907 } else {
4908 if (rename(path_content, ondisk_path) == -1) {
4909 err = got_error_from_errno3("rename",
4910 path_content, ondisk_path);
4911 goto done;
4914 } else {
4915 int is_bad_symlink = 0;
4916 if (te && S_ISLNK(te->mode)) {
4917 err = install_symlink(&is_bad_symlink,
4918 a->worktree, ondisk_path, ie->path,
4919 blob, 0, 1, 0, 0, a->repo,
4920 a->progress_cb, a->progress_arg);
4921 } else {
4922 err = install_blob(a->worktree, ondisk_path,
4923 ie->path,
4924 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4925 got_fileindex_perms_to_st(ie), blob,
4926 0, 1, 0, 0, a->repo,
4927 a->progress_cb, a->progress_arg);
4929 if (err)
4930 goto done;
4931 if (status == GOT_STATUS_DELETE ||
4932 status == GOT_STATUS_MODE_CHANGE) {
4933 err = got_fileindex_entry_update(ie,
4934 a->worktree->root_fd, relpath,
4935 blob->id.sha1,
4936 a->worktree->base_commit_id->sha1, 1);
4937 if (err)
4938 goto done;
4940 if (is_bad_symlink) {
4941 got_fileindex_entry_filetype_set(ie,
4942 GOT_FILEIDX_MODE_BAD_SYMLINK);
4945 break;
4947 default:
4948 break;
4950 done:
4951 free(ondisk_path);
4952 free(path_content);
4953 free(parent_path);
4954 free(tree_path);
4955 if (fd != -1 && close(fd) == -1 && err == NULL)
4956 err = got_error_from_errno("close");
4957 if (blob)
4958 got_object_blob_close(blob);
4959 if (tree)
4960 got_object_tree_close(tree);
4961 free(tree_id);
4962 if (base_commit)
4963 got_object_commit_close(base_commit);
4964 return err;
4967 const struct got_error *
4968 got_worktree_revert(struct got_worktree *worktree,
4969 struct got_pathlist_head *paths,
4970 got_worktree_checkout_cb progress_cb, void *progress_arg,
4971 got_worktree_patch_cb patch_cb, void *patch_arg,
4972 struct got_repository *repo)
4974 struct got_fileindex *fileindex = NULL;
4975 char *fileindex_path = NULL;
4976 const struct got_error *err = NULL, *unlockerr = NULL;
4977 const struct got_error *sync_err = NULL;
4978 struct got_pathlist_entry *pe;
4979 struct revert_file_args rfa;
4981 err = lock_worktree(worktree, LOCK_EX);
4982 if (err)
4983 return err;
4985 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4986 if (err)
4987 goto done;
4989 rfa.worktree = worktree;
4990 rfa.fileindex = fileindex;
4991 rfa.progress_cb = progress_cb;
4992 rfa.progress_arg = progress_arg;
4993 rfa.patch_cb = patch_cb;
4994 rfa.patch_arg = patch_arg;
4995 rfa.repo = repo;
4996 rfa.unlink_added_files = 0;
4997 TAILQ_FOREACH(pe, paths, entry) {
4998 err = worktree_status(worktree, pe->path, fileindex, repo,
4999 revert_file, &rfa, NULL, NULL, 1, 0);
5000 if (err)
5001 break;
5003 sync_err = sync_fileindex(fileindex, fileindex_path);
5004 if (sync_err && err == NULL)
5005 err = sync_err;
5006 done:
5007 free(fileindex_path);
5008 if (fileindex)
5009 got_fileindex_free(fileindex);
5010 unlockerr = lock_worktree(worktree, LOCK_SH);
5011 if (unlockerr && err == NULL)
5012 err = unlockerr;
5013 return err;
5016 static void
5017 free_commitable(struct got_commitable *ct)
5019 free(ct->path);
5020 free(ct->in_repo_path);
5021 free(ct->ondisk_path);
5022 free(ct->blob_id);
5023 free(ct->base_blob_id);
5024 free(ct->staged_blob_id);
5025 free(ct->base_commit_id);
5026 free(ct);
5029 struct collect_commitables_arg {
5030 struct got_pathlist_head *commitable_paths;
5031 struct got_repository *repo;
5032 struct got_worktree *worktree;
5033 struct got_fileindex *fileindex;
5034 int have_staged_files;
5035 int allow_bad_symlinks;
5036 int diff_header_shown;
5037 int commit_conflicts;
5038 FILE *diff_outfile;
5039 FILE *f1;
5040 FILE *f2;
5044 * Create a file which contains the target path of a symlink so we can feed
5045 * it as content to the diff engine.
5047 static const struct got_error *
5048 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5049 const char *abspath)
5051 const struct got_error *err = NULL;
5052 char target_path[PATH_MAX];
5053 ssize_t target_len, outlen;
5055 *fd = -1;
5057 if (dirfd != -1) {
5058 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5059 if (target_len == -1)
5060 return got_error_from_errno2("readlinkat", abspath);
5061 } else {
5062 target_len = readlink(abspath, target_path, PATH_MAX);
5063 if (target_len == -1)
5064 return got_error_from_errno2("readlink", abspath);
5067 *fd = got_opentempfd();
5068 if (*fd == -1)
5069 return got_error_from_errno("got_opentempfd");
5071 outlen = write(*fd, target_path, target_len);
5072 if (outlen == -1) {
5073 err = got_error_from_errno("got_opentempfd");
5074 goto done;
5077 if (lseek(*fd, 0, SEEK_SET) == -1) {
5078 err = got_error_from_errno2("lseek", abspath);
5079 goto done;
5081 done:
5082 if (err) {
5083 close(*fd);
5084 *fd = -1;
5086 return err;
5089 static const struct got_error *
5090 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5091 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5092 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5094 const struct got_error *err = NULL;
5095 struct got_blob_object *blob1 = NULL;
5096 int fd = -1, fd1 = -1, fd2 = -1;
5097 FILE *ondisk_file = NULL;
5098 char *label1 = NULL;
5099 struct stat sb;
5100 off_t size1 = 0;
5101 int f2_exists = 0;
5102 char *id_str = NULL;
5104 memset(&sb, 0, sizeof(sb));
5106 if (diff_staged) {
5107 if (ct->staged_status != GOT_STATUS_MODIFY &&
5108 ct->staged_status != GOT_STATUS_ADD &&
5109 ct->staged_status != GOT_STATUS_DELETE)
5110 return NULL;
5111 } else {
5112 if (ct->status != GOT_STATUS_MODIFY &&
5113 ct->status != GOT_STATUS_ADD &&
5114 ct->status != GOT_STATUS_DELETE &&
5115 ct->status != GOT_STATUS_CONFLICT)
5116 return NULL;
5119 err = got_opentemp_truncate(f1);
5120 if (err)
5121 return got_error_from_errno("got_opentemp_truncate");
5122 err = got_opentemp_truncate(f2);
5123 if (err)
5124 return got_error_from_errno("got_opentemp_truncate");
5126 if (!*diff_header_shown) {
5127 err = got_object_id_str(&id_str, worktree->base_commit_id);
5128 if (err)
5129 return err;
5130 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5131 got_worktree_get_root_path(worktree));
5132 fprintf(diff_outfile, "commit - %s\n", id_str);
5133 fprintf(diff_outfile, "path + %s%s\n",
5134 got_worktree_get_root_path(worktree),
5135 diff_staged ? " (staged changes)" : "");
5136 *diff_header_shown = 1;
5139 if (diff_staged) {
5140 const char *label1 = NULL, *label2 = NULL;
5141 switch (ct->staged_status) {
5142 case GOT_STATUS_MODIFY:
5143 label1 = ct->path;
5144 label2 = ct->path;
5145 break;
5146 case GOT_STATUS_ADD:
5147 label2 = ct->path;
5148 break;
5149 case GOT_STATUS_DELETE:
5150 label1 = ct->path;
5151 break;
5152 default:
5153 return got_error(GOT_ERR_FILE_STATUS);
5155 fd1 = got_opentempfd();
5156 if (fd1 == -1) {
5157 err = got_error_from_errno("got_opentempfd");
5158 goto done;
5160 fd2 = got_opentempfd();
5161 if (fd2 == -1) {
5162 err = got_error_from_errno("got_opentempfd");
5163 goto done;
5165 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5166 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5167 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5168 NULL, repo, diff_outfile);
5169 goto done;
5172 fd1 = got_opentempfd();
5173 if (fd1 == -1) {
5174 err = got_error_from_errno("got_opentempfd");
5175 goto done;
5178 if (ct->status != GOT_STATUS_ADD) {
5179 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5180 8192, fd1);
5181 if (err)
5182 goto done;
5185 if (ct->status != GOT_STATUS_DELETE) {
5186 if (dirfd != -1) {
5187 fd = openat(dirfd, de_name,
5188 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5189 if (fd == -1) {
5190 if (!got_err_open_nofollow_on_symlink()) {
5191 err = got_error_from_errno2("openat",
5192 ct->ondisk_path);
5193 goto done;
5195 err = get_symlink_target_file(&fd, dirfd,
5196 de_name, ct->ondisk_path);
5197 if (err)
5198 goto done;
5200 } else {
5201 fd = open(ct->ondisk_path,
5202 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5203 if (fd == -1) {
5204 if (!got_err_open_nofollow_on_symlink()) {
5205 err = got_error_from_errno2("open",
5206 ct->ondisk_path);
5207 goto done;
5209 err = get_symlink_target_file(&fd, dirfd,
5210 de_name, ct->ondisk_path);
5211 if (err)
5212 goto done;
5215 if (fstatat(fd, ct->ondisk_path, &sb,
5216 AT_SYMLINK_NOFOLLOW) == -1) {
5217 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5218 goto done;
5220 ondisk_file = fdopen(fd, "r");
5221 if (ondisk_file == NULL) {
5222 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5223 goto done;
5225 fd = -1;
5226 f2_exists = 1;
5229 if (blob1) {
5230 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5231 f1, blob1);
5232 if (err)
5233 goto done;
5236 err = got_diff_blob_file(blob1, f1, size1, label1,
5237 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5238 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5239 done:
5240 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5241 err = got_error_from_errno("close");
5242 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5243 err = got_error_from_errno("close");
5244 if (blob1)
5245 got_object_blob_close(blob1);
5246 if (fd != -1 && close(fd) == -1 && err == NULL)
5247 err = got_error_from_errno("close");
5248 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5249 err = got_error_from_errno("fclose");
5250 return err;
5253 static const struct got_error *
5254 collect_commitables(void *arg, unsigned char status,
5255 unsigned char staged_status, const char *relpath,
5256 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5257 struct got_object_id *commit_id, int dirfd, const char *de_name)
5259 struct collect_commitables_arg *a = arg;
5260 const struct got_error *err = NULL;
5261 struct got_commitable *ct = NULL;
5262 struct got_pathlist_entry *new = NULL;
5263 char *parent_path = NULL, *path = NULL;
5264 struct stat sb;
5266 if (a->have_staged_files) {
5267 if (staged_status != GOT_STATUS_MODIFY &&
5268 staged_status != GOT_STATUS_ADD &&
5269 staged_status != GOT_STATUS_DELETE)
5270 return NULL;
5271 } else {
5272 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5273 printf("C %s\n", relpath);
5274 return got_error(GOT_ERR_COMMIT_CONFLICT);
5277 if (status != GOT_STATUS_MODIFY &&
5278 status != GOT_STATUS_MODE_CHANGE &&
5279 status != GOT_STATUS_ADD &&
5280 status != GOT_STATUS_DELETE &&
5281 status != GOT_STATUS_CONFLICT)
5282 return NULL;
5285 if (asprintf(&path, "/%s", relpath) == -1) {
5286 err = got_error_from_errno("asprintf");
5287 goto done;
5289 if (strcmp(path, "/") == 0) {
5290 parent_path = strdup("");
5291 if (parent_path == NULL)
5292 return got_error_from_errno("strdup");
5293 } else {
5294 err = got_path_dirname(&parent_path, path);
5295 if (err)
5296 return err;
5299 ct = calloc(1, sizeof(*ct));
5300 if (ct == NULL) {
5301 err = got_error_from_errno("calloc");
5302 goto done;
5305 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5306 relpath) == -1) {
5307 err = got_error_from_errno("asprintf");
5308 goto done;
5311 if (staged_status == GOT_STATUS_ADD ||
5312 staged_status == GOT_STATUS_MODIFY) {
5313 struct got_fileindex_entry *ie;
5314 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5315 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5316 case GOT_FILEIDX_MODE_REGULAR_FILE:
5317 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5318 ct->mode = S_IFREG;
5319 break;
5320 case GOT_FILEIDX_MODE_SYMLINK:
5321 ct->mode = S_IFLNK;
5322 break;
5323 default:
5324 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5325 goto done;
5327 ct->mode |= got_fileindex_entry_perms_get(ie);
5328 } else if (status != GOT_STATUS_DELETE &&
5329 staged_status != GOT_STATUS_DELETE) {
5330 if (dirfd != -1) {
5331 if (fstatat(dirfd, de_name, &sb,
5332 AT_SYMLINK_NOFOLLOW) == -1) {
5333 err = got_error_from_errno2("fstatat",
5334 ct->ondisk_path);
5335 goto done;
5337 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5338 err = got_error_from_errno2("lstat", ct->ondisk_path);
5339 goto done;
5341 ct->mode = sb.st_mode;
5344 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5345 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5346 relpath) == -1) {
5347 err = got_error_from_errno("asprintf");
5348 goto done;
5351 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5352 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5353 int is_bad_symlink;
5354 char target_path[PATH_MAX];
5355 ssize_t target_len;
5356 target_len = readlink(ct->ondisk_path, target_path,
5357 sizeof(target_path));
5358 if (target_len == -1) {
5359 err = got_error_from_errno2("readlink",
5360 ct->ondisk_path);
5361 goto done;
5363 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5364 target_len, ct->ondisk_path, a->worktree->root_path);
5365 if (err)
5366 goto done;
5367 if (is_bad_symlink) {
5368 err = got_error_path(ct->ondisk_path,
5369 GOT_ERR_BAD_SYMLINK);
5370 goto done;
5375 ct->status = status;
5376 ct->staged_status = staged_status;
5377 ct->blob_id = NULL; /* will be filled in when blob gets created */
5378 if (ct->status != GOT_STATUS_ADD &&
5379 ct->staged_status != GOT_STATUS_ADD) {
5380 ct->base_blob_id = got_object_id_dup(blob_id);
5381 if (ct->base_blob_id == NULL) {
5382 err = got_error_from_errno("got_object_id_dup");
5383 goto done;
5385 ct->base_commit_id = got_object_id_dup(commit_id);
5386 if (ct->base_commit_id == NULL) {
5387 err = got_error_from_errno("got_object_id_dup");
5388 goto done;
5391 if (ct->staged_status == GOT_STATUS_ADD ||
5392 ct->staged_status == GOT_STATUS_MODIFY) {
5393 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5394 if (ct->staged_blob_id == NULL) {
5395 err = got_error_from_errno("got_object_id_dup");
5396 goto done;
5399 ct->path = strdup(path);
5400 if (ct->path == NULL) {
5401 err = got_error_from_errno("strdup");
5402 goto done;
5404 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5405 if (err)
5406 goto done;
5408 if (a->diff_outfile && ct && new != NULL) {
5409 err = append_ct_diff(ct, &a->diff_header_shown,
5410 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5411 a->have_staged_files, a->repo, a->worktree);
5412 if (err)
5413 goto done;
5415 done:
5416 if (ct && (err || new == NULL))
5417 free_commitable(ct);
5418 free(parent_path);
5419 free(path);
5420 return err;
5423 static const struct got_error *write_tree(struct got_object_id **, int *,
5424 struct got_tree_object *, const char *, struct got_pathlist_head *,
5425 got_worktree_status_cb status_cb, void *status_arg,
5426 struct got_repository *);
5428 static const struct got_error *
5429 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5430 struct got_tree_entry *te, const char *parent_path,
5431 struct got_pathlist_head *commitable_paths,
5432 got_worktree_status_cb status_cb, void *status_arg,
5433 struct got_repository *repo)
5435 const struct got_error *err = NULL;
5436 struct got_tree_object *subtree;
5437 char *subpath;
5439 if (asprintf(&subpath, "%s%s%s", parent_path,
5440 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5441 return got_error_from_errno("asprintf");
5443 err = got_object_open_as_tree(&subtree, repo, &te->id);
5444 if (err)
5445 return err;
5447 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5448 commitable_paths, status_cb, status_arg, repo);
5449 got_object_tree_close(subtree);
5450 free(subpath);
5451 return err;
5454 static const struct got_error *
5455 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5457 const struct got_error *err = NULL;
5458 char *ct_parent_path = NULL;
5460 *match = 0;
5462 if (strchr(ct->in_repo_path, '/') == NULL) {
5463 *match = got_path_is_root_dir(path);
5464 return NULL;
5467 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5468 if (err)
5469 return err;
5470 *match = (strcmp(path, ct_parent_path) == 0);
5471 free(ct_parent_path);
5472 return err;
5475 static mode_t
5476 get_ct_file_mode(struct got_commitable *ct)
5478 if (S_ISLNK(ct->mode))
5479 return S_IFLNK;
5481 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5484 static const struct got_error *
5485 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5486 struct got_tree_entry *te, struct got_commitable *ct)
5488 const struct got_error *err = NULL;
5490 *new_te = NULL;
5492 err = got_object_tree_entry_dup(new_te, te);
5493 if (err)
5494 goto done;
5496 (*new_te)->mode = get_ct_file_mode(ct);
5498 if (ct->staged_status == GOT_STATUS_MODIFY)
5499 memcpy(&(*new_te)->id, ct->staged_blob_id,
5500 sizeof((*new_te)->id));
5501 else
5502 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5503 done:
5504 if (err && *new_te) {
5505 free(*new_te);
5506 *new_te = NULL;
5508 return err;
5511 static const struct got_error *
5512 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5513 struct got_commitable *ct)
5515 const struct got_error *err = NULL;
5516 char *ct_name = NULL;
5518 *new_te = NULL;
5520 *new_te = calloc(1, sizeof(**new_te));
5521 if (*new_te == NULL)
5522 return got_error_from_errno("calloc");
5524 err = got_path_basename(&ct_name, ct->path);
5525 if (err)
5526 goto done;
5527 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5528 sizeof((*new_te)->name)) {
5529 err = got_error(GOT_ERR_NO_SPACE);
5530 goto done;
5533 (*new_te)->mode = get_ct_file_mode(ct);
5535 if (ct->staged_status == GOT_STATUS_ADD)
5536 memcpy(&(*new_te)->id, ct->staged_blob_id,
5537 sizeof((*new_te)->id));
5538 else
5539 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5540 done:
5541 free(ct_name);
5542 if (err && *new_te) {
5543 free(*new_te);
5544 *new_te = NULL;
5546 return err;
5549 static const struct got_error *
5550 insert_tree_entry(struct got_tree_entry *new_te,
5551 struct got_pathlist_head *paths)
5553 const struct got_error *err = NULL;
5554 struct got_pathlist_entry *new_pe;
5556 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5557 if (err)
5558 return err;
5559 if (new_pe == NULL)
5560 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5561 return NULL;
5564 static const struct got_error *
5565 report_ct_status(struct got_commitable *ct,
5566 got_worktree_status_cb status_cb, void *status_arg)
5568 const char *ct_path = ct->path;
5569 unsigned char status;
5571 if (status_cb == NULL) /* no commit progress output desired */
5572 return NULL;
5574 while (ct_path[0] == '/')
5575 ct_path++;
5577 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5578 status = ct->staged_status;
5579 else
5580 status = ct->status;
5582 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5583 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5586 static const struct got_error *
5587 match_modified_subtree(int *modified, struct got_tree_entry *te,
5588 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5590 const struct got_error *err = NULL;
5591 struct got_pathlist_entry *pe;
5592 char *te_path;
5594 *modified = 0;
5596 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5597 got_path_is_root_dir(base_tree_path) ? "" : "/",
5598 te->name) == -1)
5599 return got_error_from_errno("asprintf");
5601 TAILQ_FOREACH(pe, commitable_paths, entry) {
5602 struct got_commitable *ct = pe->data;
5603 *modified = got_path_is_child(ct->in_repo_path, te_path,
5604 strlen(te_path));
5605 if (*modified)
5606 break;
5609 free(te_path);
5610 return err;
5613 static const struct got_error *
5614 match_deleted_or_modified_ct(struct got_commitable **ctp,
5615 struct got_tree_entry *te, const char *base_tree_path,
5616 struct got_pathlist_head *commitable_paths)
5618 const struct got_error *err = NULL;
5619 struct got_pathlist_entry *pe;
5621 *ctp = NULL;
5623 TAILQ_FOREACH(pe, commitable_paths, entry) {
5624 struct got_commitable *ct = pe->data;
5625 char *ct_name = NULL;
5626 int path_matches;
5628 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5629 if (ct->status != GOT_STATUS_MODIFY &&
5630 ct->status != GOT_STATUS_MODE_CHANGE &&
5631 ct->status != GOT_STATUS_DELETE &&
5632 ct->status != GOT_STATUS_CONFLICT)
5633 continue;
5634 } else {
5635 if (ct->staged_status != GOT_STATUS_MODIFY &&
5636 ct->staged_status != GOT_STATUS_DELETE)
5637 continue;
5640 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5641 continue;
5643 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5644 if (err)
5645 return err;
5646 if (!path_matches)
5647 continue;
5649 err = got_path_basename(&ct_name, pe->path);
5650 if (err)
5651 return err;
5653 if (strcmp(te->name, ct_name) != 0) {
5654 free(ct_name);
5655 continue;
5657 free(ct_name);
5659 *ctp = ct;
5660 break;
5663 return err;
5666 static const struct got_error *
5667 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5668 const char *child_path, const char *path_base_tree,
5669 struct got_pathlist_head *commitable_paths,
5670 got_worktree_status_cb status_cb, void *status_arg,
5671 struct got_repository *repo)
5673 const struct got_error *err = NULL;
5674 struct got_tree_entry *new_te;
5675 char *subtree_path;
5676 struct got_object_id *id = NULL;
5677 int nentries;
5679 *new_tep = NULL;
5681 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5682 got_path_is_root_dir(path_base_tree) ? "" : "/",
5683 child_path) == -1)
5684 return got_error_from_errno("asprintf");
5686 new_te = calloc(1, sizeof(*new_te));
5687 if (new_te == NULL)
5688 return got_error_from_errno("calloc");
5689 new_te->mode = S_IFDIR;
5691 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5692 sizeof(new_te->name)) {
5693 err = got_error(GOT_ERR_NO_SPACE);
5694 goto done;
5696 err = write_tree(&id, &nentries, NULL, subtree_path,
5697 commitable_paths, status_cb, status_arg, repo);
5698 if (err) {
5699 free(new_te);
5700 goto done;
5702 memcpy(&new_te->id, id, sizeof(new_te->id));
5703 done:
5704 free(id);
5705 free(subtree_path);
5706 if (err == NULL)
5707 *new_tep = new_te;
5708 return err;
5711 static const struct got_error *
5712 write_tree(struct got_object_id **new_tree_id, int *nentries,
5713 struct got_tree_object *base_tree, const char *path_base_tree,
5714 struct got_pathlist_head *commitable_paths,
5715 got_worktree_status_cb status_cb, void *status_arg,
5716 struct got_repository *repo)
5718 const struct got_error *err = NULL;
5719 struct got_pathlist_head paths;
5720 struct got_tree_entry *te, *new_te = NULL;
5721 struct got_pathlist_entry *pe;
5723 TAILQ_INIT(&paths);
5724 *nentries = 0;
5726 /* Insert, and recurse into, newly added entries first. */
5727 TAILQ_FOREACH(pe, commitable_paths, entry) {
5728 struct got_commitable *ct = pe->data;
5729 char *child_path = NULL, *slash;
5731 if ((ct->status != GOT_STATUS_ADD &&
5732 ct->staged_status != GOT_STATUS_ADD) ||
5733 (ct->flags & GOT_COMMITABLE_ADDED))
5734 continue;
5736 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5737 strlen(path_base_tree)))
5738 continue;
5740 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5741 ct->in_repo_path);
5742 if (err)
5743 goto done;
5745 slash = strchr(child_path, '/');
5746 if (slash == NULL) {
5747 err = alloc_added_blob_tree_entry(&new_te, ct);
5748 if (err)
5749 goto done;
5750 err = report_ct_status(ct, status_cb, status_arg);
5751 if (err)
5752 goto done;
5753 ct->flags |= GOT_COMMITABLE_ADDED;
5754 err = insert_tree_entry(new_te, &paths);
5755 if (err)
5756 goto done;
5757 (*nentries)++;
5758 } else {
5759 *slash = '\0'; /* trim trailing path components */
5760 if (base_tree == NULL ||
5761 got_object_tree_find_entry(base_tree, child_path)
5762 == NULL) {
5763 err = make_subtree_for_added_blob(&new_te,
5764 child_path, path_base_tree,
5765 commitable_paths, status_cb, status_arg,
5766 repo);
5767 if (err)
5768 goto done;
5769 err = insert_tree_entry(new_te, &paths);
5770 if (err)
5771 goto done;
5772 (*nentries)++;
5777 if (base_tree) {
5778 int i, nbase_entries;
5779 /* Handle modified and deleted entries. */
5780 nbase_entries = got_object_tree_get_nentries(base_tree);
5781 for (i = 0; i < nbase_entries; i++) {
5782 struct got_commitable *ct = NULL;
5784 te = got_object_tree_get_entry(base_tree, i);
5785 if (got_object_tree_entry_is_submodule(te)) {
5786 /* Entry is a submodule; just copy it. */
5787 err = got_object_tree_entry_dup(&new_te, te);
5788 if (err)
5789 goto done;
5790 err = insert_tree_entry(new_te, &paths);
5791 if (err)
5792 goto done;
5793 (*nentries)++;
5794 continue;
5797 if (S_ISDIR(te->mode)) {
5798 int modified;
5799 err = got_object_tree_entry_dup(&new_te, te);
5800 if (err)
5801 goto done;
5802 err = match_modified_subtree(&modified, te,
5803 path_base_tree, commitable_paths);
5804 if (err)
5805 goto done;
5806 /* Avoid recursion into unmodified subtrees. */
5807 if (modified) {
5808 struct got_object_id *new_id;
5809 int nsubentries;
5810 err = write_subtree(&new_id,
5811 &nsubentries, te,
5812 path_base_tree, commitable_paths,
5813 status_cb, status_arg, repo);
5814 if (err)
5815 goto done;
5816 if (nsubentries == 0) {
5817 /* All entries were deleted. */
5818 free(new_id);
5819 continue;
5821 memcpy(&new_te->id, new_id,
5822 sizeof(new_te->id));
5823 free(new_id);
5825 err = insert_tree_entry(new_te, &paths);
5826 if (err)
5827 goto done;
5828 (*nentries)++;
5829 continue;
5832 err = match_deleted_or_modified_ct(&ct, te,
5833 path_base_tree, commitable_paths);
5834 if (err)
5835 goto done;
5836 if (ct) {
5837 /* NB: Deleted entries get dropped here. */
5838 if (ct->status == GOT_STATUS_MODIFY ||
5839 ct->status == GOT_STATUS_MODE_CHANGE ||
5840 ct->status == GOT_STATUS_CONFLICT ||
5841 ct->staged_status == GOT_STATUS_MODIFY) {
5842 err = alloc_modified_blob_tree_entry(
5843 &new_te, te, ct);
5844 if (err)
5845 goto done;
5846 err = insert_tree_entry(new_te, &paths);
5847 if (err)
5848 goto done;
5849 (*nentries)++;
5851 err = report_ct_status(ct, status_cb,
5852 status_arg);
5853 if (err)
5854 goto done;
5855 } else {
5856 /* Entry is unchanged; just copy it. */
5857 err = got_object_tree_entry_dup(&new_te, te);
5858 if (err)
5859 goto done;
5860 err = insert_tree_entry(new_te, &paths);
5861 if (err)
5862 goto done;
5863 (*nentries)++;
5868 /* Write new list of entries; deleted entries have been dropped. */
5869 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5870 done:
5871 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5872 return err;
5875 static const struct got_error *
5876 update_fileindex_after_commit(struct got_worktree *worktree,
5877 struct got_pathlist_head *commitable_paths,
5878 struct got_object_id *new_base_commit_id,
5879 struct got_fileindex *fileindex, int have_staged_files)
5881 const struct got_error *err = NULL;
5882 struct got_pathlist_entry *pe;
5883 char *relpath = NULL;
5885 TAILQ_FOREACH(pe, commitable_paths, entry) {
5886 struct got_fileindex_entry *ie;
5887 struct got_commitable *ct = pe->data;
5889 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5891 err = got_path_skip_common_ancestor(&relpath,
5892 worktree->root_path, ct->ondisk_path);
5893 if (err)
5894 goto done;
5896 if (ie) {
5897 if (ct->status == GOT_STATUS_DELETE ||
5898 ct->staged_status == GOT_STATUS_DELETE) {
5899 got_fileindex_entry_remove(fileindex, ie);
5900 } else if (ct->staged_status == GOT_STATUS_ADD ||
5901 ct->staged_status == GOT_STATUS_MODIFY) {
5902 got_fileindex_entry_stage_set(ie,
5903 GOT_FILEIDX_STAGE_NONE);
5904 got_fileindex_entry_staged_filetype_set(ie, 0);
5906 err = got_fileindex_entry_update(ie,
5907 worktree->root_fd, relpath,
5908 ct->staged_blob_id->sha1,
5909 new_base_commit_id->sha1,
5910 !have_staged_files);
5911 } else
5912 err = got_fileindex_entry_update(ie,
5913 worktree->root_fd, relpath,
5914 ct->blob_id->sha1,
5915 new_base_commit_id->sha1,
5916 !have_staged_files);
5917 } else {
5918 err = got_fileindex_entry_alloc(&ie, pe->path);
5919 if (err)
5920 goto done;
5921 err = got_fileindex_entry_update(ie,
5922 worktree->root_fd, relpath, ct->blob_id->sha1,
5923 new_base_commit_id->sha1, 1);
5924 if (err) {
5925 got_fileindex_entry_free(ie);
5926 goto done;
5928 err = got_fileindex_entry_add(fileindex, ie);
5929 if (err) {
5930 got_fileindex_entry_free(ie);
5931 goto done;
5934 free(relpath);
5935 relpath = NULL;
5937 done:
5938 free(relpath);
5939 return err;
5943 static const struct got_error *
5944 check_out_of_date(const char *in_repo_path, unsigned char status,
5945 unsigned char staged_status, struct got_object_id *base_blob_id,
5946 struct got_object_id *base_commit_id,
5947 struct got_object_id *head_commit_id, struct got_repository *repo,
5948 int ood_errcode)
5950 const struct got_error *err = NULL;
5951 struct got_commit_object *commit = NULL;
5952 struct got_object_id *id = NULL;
5954 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5955 /* Trivial case: base commit == head commit */
5956 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5957 return NULL;
5959 * Ensure file content which local changes were based
5960 * on matches file content in the branch head.
5962 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5963 if (err)
5964 goto done;
5965 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5966 if (err) {
5967 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5968 err = got_error(ood_errcode);
5969 goto done;
5970 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5971 err = got_error(ood_errcode);
5972 } else {
5973 /* Require that added files don't exist in the branch head. */
5974 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5975 if (err)
5976 goto done;
5977 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5978 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5979 goto done;
5980 err = id ? got_error(ood_errcode) : NULL;
5982 done:
5983 free(id);
5984 if (commit)
5985 got_object_commit_close(commit);
5986 return err;
5989 static const struct got_error *
5990 commit_worktree(struct got_object_id **new_commit_id,
5991 struct got_pathlist_head *commitable_paths,
5992 struct got_object_id *head_commit_id,
5993 struct got_object_id *parent_id2,
5994 struct got_worktree *worktree,
5995 const char *author, const char *committer, char *diff_path,
5996 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5997 got_worktree_status_cb status_cb, void *status_arg,
5998 struct got_repository *repo)
6000 const struct got_error *err = NULL, *unlockerr = NULL;
6001 struct got_pathlist_entry *pe;
6002 const char *head_ref_name = NULL;
6003 struct got_commit_object *head_commit = NULL;
6004 struct got_reference *head_ref2 = NULL;
6005 struct got_object_id *head_commit_id2 = NULL;
6006 struct got_tree_object *head_tree = NULL;
6007 struct got_object_id *new_tree_id = NULL;
6008 int nentries, nparents = 0;
6009 struct got_object_id_queue parent_ids;
6010 struct got_object_qid *pid = NULL;
6011 char *logmsg = NULL;
6012 time_t timestamp;
6014 *new_commit_id = NULL;
6016 STAILQ_INIT(&parent_ids);
6018 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6019 if (err)
6020 goto done;
6022 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6023 if (err)
6024 goto done;
6026 if (commit_msg_cb != NULL) {
6027 err = commit_msg_cb(commitable_paths, diff_path,
6028 &logmsg, commit_arg);
6029 if (err)
6030 goto done;
6033 if (logmsg == NULL || strlen(logmsg) == 0) {
6034 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6035 goto done;
6038 /* Create blobs from added and modified files and record their IDs. */
6039 TAILQ_FOREACH(pe, commitable_paths, entry) {
6040 struct got_commitable *ct = pe->data;
6041 char *ondisk_path;
6043 /* Blobs for staged files already exist. */
6044 if (ct->staged_status == GOT_STATUS_ADD ||
6045 ct->staged_status == GOT_STATUS_MODIFY)
6046 continue;
6048 if (ct->status != GOT_STATUS_ADD &&
6049 ct->status != GOT_STATUS_MODIFY &&
6050 ct->status != GOT_STATUS_MODE_CHANGE &&
6051 ct->status != GOT_STATUS_CONFLICT)
6052 continue;
6054 if (asprintf(&ondisk_path, "%s/%s",
6055 worktree->root_path, pe->path) == -1) {
6056 err = got_error_from_errno("asprintf");
6057 goto done;
6059 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6060 free(ondisk_path);
6061 if (err)
6062 goto done;
6065 /* Recursively write new tree objects. */
6066 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6067 commitable_paths, status_cb, status_arg, repo);
6068 if (err)
6069 goto done;
6071 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6072 if (err)
6073 goto done;
6074 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6075 nparents++;
6076 if (parent_id2) {
6077 err = got_object_qid_alloc(&pid, parent_id2);
6078 if (err)
6079 goto done;
6080 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6081 nparents++;
6083 timestamp = time(NULL);
6084 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6085 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6086 if (logmsg != NULL)
6087 free(logmsg);
6088 if (err)
6089 goto done;
6091 /* Check if a concurrent commit to our branch has occurred. */
6092 head_ref_name = got_worktree_get_head_ref_name(worktree);
6093 if (head_ref_name == NULL) {
6094 err = got_error_from_errno("got_worktree_get_head_ref_name");
6095 goto done;
6097 /* Lock the reference here to prevent concurrent modification. */
6098 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6099 if (err)
6100 goto done;
6101 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6102 if (err)
6103 goto done;
6104 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6105 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6106 goto done;
6108 /* Update branch head in repository. */
6109 err = got_ref_change_ref(head_ref2, *new_commit_id);
6110 if (err)
6111 goto done;
6112 err = got_ref_write(head_ref2, repo);
6113 if (err)
6114 goto done;
6116 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6117 if (err)
6118 goto done;
6120 err = ref_base_commit(worktree, repo);
6121 if (err)
6122 goto done;
6123 done:
6124 got_object_id_queue_free(&parent_ids);
6125 if (head_tree)
6126 got_object_tree_close(head_tree);
6127 if (head_commit)
6128 got_object_commit_close(head_commit);
6129 free(head_commit_id2);
6130 if (head_ref2) {
6131 unlockerr = got_ref_unlock(head_ref2);
6132 if (unlockerr && err == NULL)
6133 err = unlockerr;
6134 got_ref_close(head_ref2);
6136 return err;
6139 static const struct got_error *
6140 check_path_is_commitable(const char *path,
6141 struct got_pathlist_head *commitable_paths)
6143 struct got_pathlist_entry *cpe = NULL;
6144 size_t path_len = strlen(path);
6146 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6147 struct got_commitable *ct = cpe->data;
6148 const char *ct_path = ct->path;
6150 while (ct_path[0] == '/')
6151 ct_path++;
6153 if (strcmp(path, ct_path) == 0 ||
6154 got_path_is_child(ct_path, path, path_len))
6155 break;
6158 if (cpe == NULL)
6159 return got_error_path(path, GOT_ERR_BAD_PATH);
6161 return NULL;
6164 static const struct got_error *
6165 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6167 int *have_staged_files = arg;
6169 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6170 *have_staged_files = 1;
6171 return got_error(GOT_ERR_CANCELLED);
6174 return NULL;
6177 static const struct got_error *
6178 check_non_staged_files(struct got_fileindex *fileindex,
6179 struct got_pathlist_head *paths)
6181 struct got_pathlist_entry *pe;
6182 struct got_fileindex_entry *ie;
6184 TAILQ_FOREACH(pe, paths, entry) {
6185 if (pe->path[0] == '\0')
6186 continue;
6187 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6188 if (ie == NULL)
6189 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6190 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6191 return got_error_path(pe->path,
6192 GOT_ERR_FILE_NOT_STAGED);
6195 return NULL;
6198 const struct got_error *
6199 got_worktree_commit(struct got_object_id **new_commit_id,
6200 struct got_worktree *worktree, struct got_pathlist_head *paths,
6201 const char *author, const char *committer, int allow_bad_symlinks,
6202 int show_diff, int commit_conflicts,
6203 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6204 got_worktree_status_cb status_cb, void *status_arg,
6205 struct got_repository *repo)
6207 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6208 struct got_fileindex *fileindex = NULL;
6209 char *fileindex_path = NULL;
6210 struct got_pathlist_head commitable_paths;
6211 struct collect_commitables_arg cc_arg;
6212 struct got_pathlist_entry *pe;
6213 struct got_reference *head_ref = NULL;
6214 struct got_object_id *head_commit_id = NULL;
6215 char *diff_path = NULL;
6216 int have_staged_files = 0;
6218 *new_commit_id = NULL;
6220 memset(&cc_arg, 0, sizeof(cc_arg));
6221 TAILQ_INIT(&commitable_paths);
6223 err = lock_worktree(worktree, LOCK_EX);
6224 if (err)
6225 goto done;
6227 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6228 if (err)
6229 goto done;
6231 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6232 if (err)
6233 goto done;
6235 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6236 if (err)
6237 goto done;
6239 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6240 &have_staged_files);
6241 if (err && err->code != GOT_ERR_CANCELLED)
6242 goto done;
6243 if (have_staged_files) {
6244 err = check_non_staged_files(fileindex, paths);
6245 if (err)
6246 goto done;
6249 cc_arg.commitable_paths = &commitable_paths;
6250 cc_arg.worktree = worktree;
6251 cc_arg.fileindex = fileindex;
6252 cc_arg.repo = repo;
6253 cc_arg.have_staged_files = have_staged_files;
6254 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6255 cc_arg.diff_header_shown = 0;
6256 cc_arg.commit_conflicts = commit_conflicts;
6257 if (show_diff) {
6258 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6259 GOT_TMPDIR_STR "/got", ".diff");
6260 if (err)
6261 goto done;
6262 cc_arg.f1 = got_opentemp();
6263 if (cc_arg.f1 == NULL) {
6264 err = got_error_from_errno("got_opentemp");
6265 goto done;
6267 cc_arg.f2 = got_opentemp();
6268 if (cc_arg.f2 == NULL) {
6269 err = got_error_from_errno("got_opentemp");
6270 goto done;
6274 TAILQ_FOREACH(pe, paths, entry) {
6275 err = worktree_status(worktree, pe->path, fileindex, repo,
6276 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6277 if (err)
6278 goto done;
6281 if (show_diff) {
6282 if (fflush(cc_arg.diff_outfile) == EOF) {
6283 err = got_error_from_errno("fflush");
6284 goto done;
6288 if (TAILQ_EMPTY(&commitable_paths)) {
6289 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6290 goto done;
6293 TAILQ_FOREACH(pe, paths, entry) {
6294 err = check_path_is_commitable(pe->path, &commitable_paths);
6295 if (err)
6296 goto done;
6299 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6300 struct got_commitable *ct = pe->data;
6301 const char *ct_path = ct->in_repo_path;
6303 while (ct_path[0] == '/')
6304 ct_path++;
6305 err = check_out_of_date(ct_path, ct->status,
6306 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6307 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6308 if (err)
6309 goto done;
6313 err = commit_worktree(new_commit_id, &commitable_paths,
6314 head_commit_id, NULL, worktree, author, committer,
6315 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6316 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6317 if (err)
6318 goto done;
6320 err = update_fileindex_after_commit(worktree, &commitable_paths,
6321 *new_commit_id, fileindex, have_staged_files);
6322 sync_err = sync_fileindex(fileindex, fileindex_path);
6323 if (sync_err && err == NULL)
6324 err = sync_err;
6325 done:
6326 if (fileindex)
6327 got_fileindex_free(fileindex);
6328 free(fileindex_path);
6329 unlockerr = lock_worktree(worktree, LOCK_SH);
6330 if (unlockerr && err == NULL)
6331 err = unlockerr;
6332 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6333 struct got_commitable *ct = pe->data;
6335 free_commitable(ct);
6337 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6338 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6339 err = got_error_from_errno2("unlink", diff_path);
6340 free(diff_path);
6341 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6342 err == NULL)
6343 err = got_error_from_errno("fclose");
6344 return err;
6347 const char *
6348 got_commitable_get_path(struct got_commitable *ct)
6350 return ct->path;
6353 unsigned int
6354 got_commitable_get_status(struct got_commitable *ct)
6356 return ct->status;
6359 struct check_rebase_ok_arg {
6360 struct got_worktree *worktree;
6361 struct got_repository *repo;
6364 static const struct got_error *
6365 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6367 const struct got_error *err = NULL;
6368 struct check_rebase_ok_arg *a = arg;
6369 unsigned char status;
6370 struct stat sb;
6371 char *ondisk_path;
6373 /* Reject rebase of a work tree with mixed base commits. */
6374 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6375 SHA1_DIGEST_LENGTH))
6376 return got_error(GOT_ERR_MIXED_COMMITS);
6378 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6379 == -1)
6380 return got_error_from_errno("asprintf");
6382 /* Reject rebase of a work tree with modified or staged files. */
6383 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6384 free(ondisk_path);
6385 if (err)
6386 return err;
6388 if (status != GOT_STATUS_NO_CHANGE)
6389 return got_error(GOT_ERR_MODIFIED);
6390 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6391 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6393 return NULL;
6396 const struct got_error *
6397 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6398 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6399 struct got_worktree *worktree, struct got_reference *branch,
6400 struct got_repository *repo)
6402 const struct got_error *err = NULL;
6403 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6404 char *branch_ref_name = NULL;
6405 char *fileindex_path = NULL;
6406 struct check_rebase_ok_arg ok_arg;
6407 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6408 struct got_object_id *wt_branch_tip = NULL;
6410 *new_base_branch_ref = NULL;
6411 *tmp_branch = NULL;
6412 *fileindex = NULL;
6414 err = lock_worktree(worktree, LOCK_EX);
6415 if (err)
6416 return err;
6418 err = open_fileindex(fileindex, &fileindex_path, worktree);
6419 if (err)
6420 goto done;
6422 ok_arg.worktree = worktree;
6423 ok_arg.repo = repo;
6424 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6425 &ok_arg);
6426 if (err)
6427 goto done;
6429 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6430 if (err)
6431 goto done;
6433 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6434 if (err)
6435 goto done;
6437 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6438 if (err)
6439 goto done;
6441 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6442 0);
6443 if (err)
6444 goto done;
6446 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6447 if (err)
6448 goto done;
6449 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6450 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6451 goto done;
6454 err = got_ref_alloc_symref(new_base_branch_ref,
6455 new_base_branch_ref_name, wt_branch);
6456 if (err)
6457 goto done;
6458 err = got_ref_write(*new_base_branch_ref, repo);
6459 if (err)
6460 goto done;
6462 /* TODO Lock original branch's ref while rebasing? */
6464 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6465 if (err)
6466 goto done;
6468 err = got_ref_write(branch_ref, repo);
6469 if (err)
6470 goto done;
6472 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6473 worktree->base_commit_id);
6474 if (err)
6475 goto done;
6476 err = got_ref_write(*tmp_branch, repo);
6477 if (err)
6478 goto done;
6480 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6481 if (err)
6482 goto done;
6483 done:
6484 free(fileindex_path);
6485 free(tmp_branch_name);
6486 free(new_base_branch_ref_name);
6487 free(branch_ref_name);
6488 if (branch_ref)
6489 got_ref_close(branch_ref);
6490 if (wt_branch)
6491 got_ref_close(wt_branch);
6492 free(wt_branch_tip);
6493 if (err) {
6494 if (*new_base_branch_ref) {
6495 got_ref_close(*new_base_branch_ref);
6496 *new_base_branch_ref = NULL;
6498 if (*tmp_branch) {
6499 got_ref_close(*tmp_branch);
6500 *tmp_branch = NULL;
6502 if (*fileindex) {
6503 got_fileindex_free(*fileindex);
6504 *fileindex = NULL;
6506 lock_worktree(worktree, LOCK_SH);
6508 return err;
6511 const struct got_error *
6512 got_worktree_rebase_continue(struct got_object_id **commit_id,
6513 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6514 struct got_reference **branch, struct got_fileindex **fileindex,
6515 struct got_worktree *worktree, struct got_repository *repo)
6517 const struct got_error *err;
6518 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6519 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6520 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6521 char *fileindex_path = NULL;
6522 int have_staged_files = 0;
6524 *commit_id = NULL;
6525 *new_base_branch = NULL;
6526 *tmp_branch = NULL;
6527 *branch = NULL;
6528 *fileindex = NULL;
6530 err = lock_worktree(worktree, LOCK_EX);
6531 if (err)
6532 return err;
6534 err = open_fileindex(fileindex, &fileindex_path, worktree);
6535 if (err)
6536 goto done;
6538 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6539 &have_staged_files);
6540 if (err && err->code != GOT_ERR_CANCELLED)
6541 goto done;
6542 if (have_staged_files) {
6543 err = got_error(GOT_ERR_STAGED_PATHS);
6544 goto done;
6547 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6548 if (err)
6549 goto done;
6551 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6552 if (err)
6553 goto done;
6555 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6556 if (err)
6557 goto done;
6559 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6560 if (err)
6561 goto done;
6563 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6564 if (err)
6565 goto done;
6567 err = got_ref_open(branch, repo,
6568 got_ref_get_symref_target(branch_ref), 0);
6569 if (err)
6570 goto done;
6572 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6573 if (err)
6574 goto done;
6576 err = got_ref_resolve(commit_id, repo, commit_ref);
6577 if (err)
6578 goto done;
6580 err = got_ref_open(new_base_branch, repo,
6581 new_base_branch_ref_name, 0);
6582 if (err)
6583 goto done;
6585 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6586 if (err)
6587 goto done;
6588 done:
6589 free(commit_ref_name);
6590 free(branch_ref_name);
6591 free(fileindex_path);
6592 if (commit_ref)
6593 got_ref_close(commit_ref);
6594 if (branch_ref)
6595 got_ref_close(branch_ref);
6596 if (err) {
6597 free(*commit_id);
6598 *commit_id = NULL;
6599 if (*tmp_branch) {
6600 got_ref_close(*tmp_branch);
6601 *tmp_branch = NULL;
6603 if (*new_base_branch) {
6604 got_ref_close(*new_base_branch);
6605 *new_base_branch = NULL;
6607 if (*branch) {
6608 got_ref_close(*branch);
6609 *branch = NULL;
6611 if (*fileindex) {
6612 got_fileindex_free(*fileindex);
6613 *fileindex = NULL;
6615 lock_worktree(worktree, LOCK_SH);
6617 return err;
6620 const struct got_error *
6621 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6623 const struct got_error *err;
6624 char *tmp_branch_name = NULL;
6626 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6627 if (err)
6628 return err;
6630 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6631 free(tmp_branch_name);
6632 return NULL;
6635 static const struct got_error *
6636 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6637 const char *diff_path, char **logmsg, void *arg)
6639 *logmsg = arg;
6640 return NULL;
6643 static const struct got_error *
6644 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6645 const char *path, struct got_object_id *blob_id,
6646 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6647 int dirfd, const char *de_name)
6649 return NULL;
6652 struct collect_merged_paths_arg {
6653 got_worktree_checkout_cb progress_cb;
6654 void *progress_arg;
6655 struct got_pathlist_head *merged_paths;
6658 static const struct got_error *
6659 collect_merged_paths(void *arg, unsigned char status, const char *path)
6661 const struct got_error *err;
6662 struct collect_merged_paths_arg *a = arg;
6663 char *p;
6664 struct got_pathlist_entry *new;
6666 err = (*a->progress_cb)(a->progress_arg, status, path);
6667 if (err)
6668 return err;
6670 if (status != GOT_STATUS_MERGE &&
6671 status != GOT_STATUS_ADD &&
6672 status != GOT_STATUS_DELETE &&
6673 status != GOT_STATUS_CONFLICT)
6674 return NULL;
6676 p = strdup(path);
6677 if (p == NULL)
6678 return got_error_from_errno("strdup");
6680 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6681 if (err || new == NULL)
6682 free(p);
6683 return err;
6686 static const struct got_error *
6687 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6688 int is_rebase, struct got_repository *repo)
6690 const struct got_error *err;
6691 struct got_reference *commit_ref = NULL;
6693 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6694 if (err) {
6695 if (err->code != GOT_ERR_NOT_REF)
6696 goto done;
6697 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6698 if (err)
6699 goto done;
6700 err = got_ref_write(commit_ref, repo);
6701 if (err)
6702 goto done;
6703 } else if (is_rebase) {
6704 struct got_object_id *stored_id;
6705 int cmp;
6707 err = got_ref_resolve(&stored_id, repo, commit_ref);
6708 if (err)
6709 goto done;
6710 cmp = got_object_id_cmp(commit_id, stored_id);
6711 free(stored_id);
6712 if (cmp != 0) {
6713 err = got_error(GOT_ERR_REBASE_COMMITID);
6714 goto done;
6717 done:
6718 if (commit_ref)
6719 got_ref_close(commit_ref);
6720 return err;
6723 static const struct got_error *
6724 rebase_merge_files(struct got_pathlist_head *merged_paths,
6725 const char *commit_ref_name, struct got_worktree *worktree,
6726 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6727 struct got_object_id *commit_id, struct got_repository *repo,
6728 got_worktree_checkout_cb progress_cb, void *progress_arg,
6729 got_cancel_cb cancel_cb, void *cancel_arg)
6731 const struct got_error *err;
6732 struct got_reference *commit_ref = NULL;
6733 struct collect_merged_paths_arg cmp_arg;
6734 char *fileindex_path;
6736 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6738 err = get_fileindex_path(&fileindex_path, worktree);
6739 if (err)
6740 return err;
6742 cmp_arg.progress_cb = progress_cb;
6743 cmp_arg.progress_arg = progress_arg;
6744 cmp_arg.merged_paths = merged_paths;
6745 err = merge_files(worktree, fileindex, fileindex_path,
6746 parent_commit_id, commit_id, repo, collect_merged_paths,
6747 &cmp_arg, cancel_cb, cancel_arg);
6748 if (commit_ref)
6749 got_ref_close(commit_ref);
6750 return err;
6753 const struct got_error *
6754 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6755 struct got_worktree *worktree, struct got_fileindex *fileindex,
6756 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6757 struct got_repository *repo,
6758 got_worktree_checkout_cb progress_cb, void *progress_arg,
6759 got_cancel_cb cancel_cb, void *cancel_arg)
6761 const struct got_error *err;
6762 char *commit_ref_name;
6764 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6765 if (err)
6766 return err;
6768 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6769 if (err)
6770 goto done;
6772 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6773 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6774 progress_arg, cancel_cb, cancel_arg);
6775 done:
6776 free(commit_ref_name);
6777 return err;
6780 const struct got_error *
6781 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6782 struct got_worktree *worktree, struct got_fileindex *fileindex,
6783 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6784 struct got_repository *repo,
6785 got_worktree_checkout_cb progress_cb, void *progress_arg,
6786 got_cancel_cb cancel_cb, void *cancel_arg)
6788 const struct got_error *err;
6789 char *commit_ref_name;
6791 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6792 if (err)
6793 return err;
6795 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6796 if (err)
6797 goto done;
6799 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6800 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6801 progress_arg, cancel_cb, cancel_arg);
6802 done:
6803 free(commit_ref_name);
6804 return err;
6807 static const struct got_error *
6808 rebase_commit(struct got_object_id **new_commit_id,
6809 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6810 struct got_worktree *worktree, struct got_fileindex *fileindex,
6811 struct got_reference *tmp_branch, const char *committer,
6812 struct got_commit_object *orig_commit, const char *new_logmsg,
6813 int allow_conflict, struct got_repository *repo)
6815 const struct got_error *err, *sync_err;
6816 struct got_pathlist_head commitable_paths;
6817 struct collect_commitables_arg cc_arg;
6818 char *fileindex_path = NULL;
6819 struct got_reference *head_ref = NULL;
6820 struct got_object_id *head_commit_id = NULL;
6821 char *logmsg = NULL;
6823 memset(&cc_arg, 0, sizeof(cc_arg));
6824 TAILQ_INIT(&commitable_paths);
6825 *new_commit_id = NULL;
6827 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6829 err = get_fileindex_path(&fileindex_path, worktree);
6830 if (err)
6831 return err;
6833 cc_arg.commitable_paths = &commitable_paths;
6834 cc_arg.worktree = worktree;
6835 cc_arg.repo = repo;
6836 cc_arg.have_staged_files = 0;
6837 cc_arg.commit_conflicts = allow_conflict;
6839 * If possible get the status of individual files directly to
6840 * avoid crawling the entire work tree once per rebased commit.
6842 * Ideally, merged_paths would contain a list of commitables
6843 * we could use so we could skip worktree_status() entirely.
6844 * However, we would then need carefully keep track of cumulative
6845 * effects of operations such as file additions and deletions
6846 * in 'got histedit -f' (folding multiple commits into one),
6847 * and this extra complexity is not really worth it.
6849 if (merged_paths) {
6850 struct got_pathlist_entry *pe;
6851 TAILQ_FOREACH(pe, merged_paths, entry) {
6852 err = worktree_status(worktree, pe->path, fileindex,
6853 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6854 0);
6855 if (err)
6856 goto done;
6858 } else {
6859 err = worktree_status(worktree, "", fileindex, repo,
6860 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6861 if (err)
6862 goto done;
6865 if (TAILQ_EMPTY(&commitable_paths)) {
6866 /* No-op change; commit will be elided. */
6867 err = got_ref_delete(commit_ref, repo);
6868 if (err)
6869 goto done;
6870 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6871 goto done;
6874 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6875 if (err)
6876 goto done;
6878 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6879 if (err)
6880 goto done;
6882 if (new_logmsg) {
6883 logmsg = strdup(new_logmsg);
6884 if (logmsg == NULL) {
6885 err = got_error_from_errno("strdup");
6886 goto done;
6888 } else {
6889 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6890 if (err)
6891 goto done;
6894 /* NB: commit_worktree will call free(logmsg) */
6895 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6896 NULL, worktree, got_object_commit_get_author(orig_commit),
6897 committer ? committer :
6898 got_object_commit_get_committer(orig_commit), NULL,
6899 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6900 if (err)
6901 goto done;
6903 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6904 if (err)
6905 goto done;
6907 err = got_ref_delete(commit_ref, repo);
6908 if (err)
6909 goto done;
6911 err = update_fileindex_after_commit(worktree, &commitable_paths,
6912 *new_commit_id, fileindex, 0);
6913 sync_err = sync_fileindex(fileindex, fileindex_path);
6914 if (sync_err && err == NULL)
6915 err = sync_err;
6916 done:
6917 free(fileindex_path);
6918 free(head_commit_id);
6919 if (head_ref)
6920 got_ref_close(head_ref);
6921 if (err) {
6922 free(*new_commit_id);
6923 *new_commit_id = NULL;
6925 return err;
6928 const struct got_error *
6929 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6930 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6931 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6932 const char *committer, struct got_commit_object *orig_commit,
6933 struct got_object_id *orig_commit_id, int allow_conflict,
6934 struct got_repository *repo)
6936 const struct got_error *err;
6937 char *commit_ref_name;
6938 struct got_reference *commit_ref = NULL;
6939 struct got_object_id *commit_id = NULL;
6941 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6942 if (err)
6943 return err;
6945 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6946 if (err)
6947 goto done;
6948 err = got_ref_resolve(&commit_id, repo, commit_ref);
6949 if (err)
6950 goto done;
6951 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6952 err = got_error(GOT_ERR_REBASE_COMMITID);
6953 goto done;
6956 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6957 worktree, fileindex, tmp_branch, committer, orig_commit,
6958 NULL, allow_conflict, repo);
6959 done:
6960 if (commit_ref)
6961 got_ref_close(commit_ref);
6962 free(commit_ref_name);
6963 free(commit_id);
6964 return err;
6967 const struct got_error *
6968 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6969 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6970 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6971 const char *committer, struct got_commit_object *orig_commit,
6972 struct got_object_id *orig_commit_id, const char *new_logmsg,
6973 int allow_conflict, struct got_repository *repo)
6975 const struct got_error *err;
6976 char *commit_ref_name;
6977 struct got_reference *commit_ref = NULL;
6979 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6980 if (err)
6981 return err;
6983 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6984 if (err)
6985 goto done;
6987 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6988 worktree, fileindex, tmp_branch, committer, orig_commit,
6989 new_logmsg, allow_conflict, repo);
6990 done:
6991 if (commit_ref)
6992 got_ref_close(commit_ref);
6993 free(commit_ref_name);
6994 return err;
6997 const struct got_error *
6998 got_worktree_rebase_postpone(struct got_worktree *worktree,
6999 struct got_fileindex *fileindex)
7001 if (fileindex)
7002 got_fileindex_free(fileindex);
7003 return lock_worktree(worktree, LOCK_SH);
7006 static const struct got_error *
7007 delete_ref(const char *name, struct got_repository *repo)
7009 const struct got_error *err;
7010 struct got_reference *ref;
7012 err = got_ref_open(&ref, repo, name, 0);
7013 if (err) {
7014 if (err->code == GOT_ERR_NOT_REF)
7015 return NULL;
7016 return err;
7019 err = got_ref_delete(ref, repo);
7020 got_ref_close(ref);
7021 return err;
7024 static const struct got_error *
7025 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7027 const struct got_error *err;
7028 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7029 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7031 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7032 if (err)
7033 goto done;
7034 err = delete_ref(tmp_branch_name, repo);
7035 if (err)
7036 goto done;
7038 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7039 if (err)
7040 goto done;
7041 err = delete_ref(new_base_branch_ref_name, repo);
7042 if (err)
7043 goto done;
7045 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7046 if (err)
7047 goto done;
7048 err = delete_ref(branch_ref_name, repo);
7049 if (err)
7050 goto done;
7052 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7053 if (err)
7054 goto done;
7055 err = delete_ref(commit_ref_name, repo);
7056 if (err)
7057 goto done;
7059 done:
7060 free(tmp_branch_name);
7061 free(new_base_branch_ref_name);
7062 free(branch_ref_name);
7063 free(commit_ref_name);
7064 return err;
7067 static const struct got_error *
7068 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7069 struct got_object_id *new_commit_id, struct got_repository *repo)
7071 const struct got_error *err;
7072 struct got_reference *ref = NULL;
7073 struct got_object_id *old_commit_id = NULL;
7074 const char *branch_name = NULL;
7075 char *new_id_str = NULL;
7076 char *refname = NULL;
7078 branch_name = got_ref_get_name(branch);
7079 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7080 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7081 branch_name += 11;
7083 err = got_object_id_str(&new_id_str, new_commit_id);
7084 if (err)
7085 return err;
7087 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7088 new_id_str) == -1) {
7089 err = got_error_from_errno("asprintf");
7090 goto done;
7093 err = got_ref_resolve(&old_commit_id, repo, branch);
7094 if (err)
7095 goto done;
7097 err = got_ref_alloc(&ref, refname, old_commit_id);
7098 if (err)
7099 goto done;
7101 err = got_ref_write(ref, repo);
7102 done:
7103 free(new_id_str);
7104 free(refname);
7105 free(old_commit_id);
7106 if (ref)
7107 got_ref_close(ref);
7108 return err;
7111 const struct got_error *
7112 got_worktree_rebase_complete(struct got_worktree *worktree,
7113 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7114 struct got_reference *rebased_branch, struct got_repository *repo,
7115 int create_backup)
7117 const struct got_error *err, *unlockerr, *sync_err;
7118 struct got_object_id *new_head_commit_id = NULL;
7119 char *fileindex_path = NULL;
7121 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7122 if (err)
7123 return err;
7125 if (create_backup) {
7126 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7127 rebased_branch, new_head_commit_id, repo);
7128 if (err)
7129 goto done;
7132 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7133 if (err)
7134 goto done;
7136 err = got_ref_write(rebased_branch, repo);
7137 if (err)
7138 goto done;
7140 err = got_worktree_set_head_ref(worktree, rebased_branch);
7141 if (err)
7142 goto done;
7144 err = delete_rebase_refs(worktree, repo);
7145 if (err)
7146 goto done;
7148 err = get_fileindex_path(&fileindex_path, worktree);
7149 if (err)
7150 goto done;
7151 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7152 sync_err = sync_fileindex(fileindex, fileindex_path);
7153 if (sync_err && err == NULL)
7154 err = sync_err;
7155 done:
7156 got_fileindex_free(fileindex);
7157 free(fileindex_path);
7158 free(new_head_commit_id);
7159 unlockerr = lock_worktree(worktree, LOCK_SH);
7160 if (unlockerr && err == NULL)
7161 err = unlockerr;
7162 return err;
7165 const struct got_error *
7166 got_worktree_rebase_abort(struct got_worktree *worktree,
7167 struct got_fileindex *fileindex, struct got_repository *repo,
7168 struct got_reference *new_base_branch,
7169 got_worktree_checkout_cb progress_cb, void *progress_arg)
7171 const struct got_error *err, *unlockerr, *sync_err;
7172 struct got_reference *resolved = NULL;
7173 struct got_object_id *commit_id = NULL;
7174 struct got_commit_object *commit = NULL;
7175 char *fileindex_path = NULL;
7176 struct revert_file_args rfa;
7177 struct got_object_id *tree_id = NULL;
7179 err = lock_worktree(worktree, LOCK_EX);
7180 if (err)
7181 return err;
7183 err = got_object_open_as_commit(&commit, repo,
7184 worktree->base_commit_id);
7185 if (err)
7186 goto done;
7188 err = got_ref_open(&resolved, repo,
7189 got_ref_get_symref_target(new_base_branch), 0);
7190 if (err)
7191 goto done;
7193 err = got_worktree_set_head_ref(worktree, resolved);
7194 if (err)
7195 goto done;
7198 * XXX commits to the base branch could have happened while
7199 * we were busy rebasing; should we store the original commit ID
7200 * when rebase begins and read it back here?
7202 err = got_ref_resolve(&commit_id, repo, resolved);
7203 if (err)
7204 goto done;
7206 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7207 if (err)
7208 goto done;
7210 err = got_object_id_by_path(&tree_id, repo, commit,
7211 worktree->path_prefix);
7212 if (err)
7213 goto done;
7215 err = delete_rebase_refs(worktree, repo);
7216 if (err)
7217 goto done;
7219 err = get_fileindex_path(&fileindex_path, worktree);
7220 if (err)
7221 goto done;
7223 rfa.worktree = worktree;
7224 rfa.fileindex = fileindex;
7225 rfa.progress_cb = progress_cb;
7226 rfa.progress_arg = progress_arg;
7227 rfa.patch_cb = NULL;
7228 rfa.patch_arg = NULL;
7229 rfa.repo = repo;
7230 rfa.unlink_added_files = 0;
7231 err = worktree_status(worktree, "", fileindex, repo,
7232 revert_file, &rfa, NULL, NULL, 1, 0);
7233 if (err)
7234 goto sync;
7236 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7237 repo, progress_cb, progress_arg, NULL, NULL);
7238 sync:
7239 sync_err = sync_fileindex(fileindex, fileindex_path);
7240 if (sync_err && err == NULL)
7241 err = sync_err;
7242 done:
7243 got_ref_close(resolved);
7244 free(tree_id);
7245 free(commit_id);
7246 if (commit)
7247 got_object_commit_close(commit);
7248 if (fileindex)
7249 got_fileindex_free(fileindex);
7250 free(fileindex_path);
7252 unlockerr = lock_worktree(worktree, LOCK_SH);
7253 if (unlockerr && err == NULL)
7254 err = unlockerr;
7255 return err;
7258 const struct got_error *
7259 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7260 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7261 struct got_fileindex **fileindex, struct got_worktree *worktree,
7262 struct got_repository *repo)
7264 const struct got_error *err = NULL;
7265 char *tmp_branch_name = NULL;
7266 char *branch_ref_name = NULL;
7267 char *base_commit_ref_name = NULL;
7268 char *fileindex_path = NULL;
7269 struct check_rebase_ok_arg ok_arg;
7270 struct got_reference *wt_branch = NULL;
7271 struct got_reference *base_commit_ref = NULL;
7273 *tmp_branch = NULL;
7274 *branch_ref = NULL;
7275 *base_commit_id = NULL;
7276 *fileindex = NULL;
7278 err = lock_worktree(worktree, LOCK_EX);
7279 if (err)
7280 return err;
7282 err = open_fileindex(fileindex, &fileindex_path, worktree);
7283 if (err)
7284 goto done;
7286 ok_arg.worktree = worktree;
7287 ok_arg.repo = repo;
7288 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7289 &ok_arg);
7290 if (err)
7291 goto done;
7293 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7294 if (err)
7295 goto done;
7297 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7298 if (err)
7299 goto done;
7301 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7302 worktree);
7303 if (err)
7304 goto done;
7306 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7307 0);
7308 if (err)
7309 goto done;
7311 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7312 if (err)
7313 goto done;
7315 err = got_ref_write(*branch_ref, repo);
7316 if (err)
7317 goto done;
7319 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7320 worktree->base_commit_id);
7321 if (err)
7322 goto done;
7323 err = got_ref_write(base_commit_ref, repo);
7324 if (err)
7325 goto done;
7326 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7327 if (*base_commit_id == NULL) {
7328 err = got_error_from_errno("got_object_id_dup");
7329 goto done;
7332 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7333 worktree->base_commit_id);
7334 if (err)
7335 goto done;
7336 err = got_ref_write(*tmp_branch, repo);
7337 if (err)
7338 goto done;
7340 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7341 if (err)
7342 goto done;
7343 done:
7344 free(fileindex_path);
7345 free(tmp_branch_name);
7346 free(branch_ref_name);
7347 free(base_commit_ref_name);
7348 if (wt_branch)
7349 got_ref_close(wt_branch);
7350 if (err) {
7351 if (*branch_ref) {
7352 got_ref_close(*branch_ref);
7353 *branch_ref = NULL;
7355 if (*tmp_branch) {
7356 got_ref_close(*tmp_branch);
7357 *tmp_branch = NULL;
7359 free(*base_commit_id);
7360 if (*fileindex) {
7361 got_fileindex_free(*fileindex);
7362 *fileindex = NULL;
7364 lock_worktree(worktree, LOCK_SH);
7366 return err;
7369 const struct got_error *
7370 got_worktree_histedit_postpone(struct got_worktree *worktree,
7371 struct got_fileindex *fileindex)
7373 if (fileindex)
7374 got_fileindex_free(fileindex);
7375 return lock_worktree(worktree, LOCK_SH);
7378 const struct got_error *
7379 got_worktree_histedit_in_progress(int *in_progress,
7380 struct got_worktree *worktree)
7382 const struct got_error *err;
7383 char *tmp_branch_name = NULL;
7385 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7386 if (err)
7387 return err;
7389 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7390 free(tmp_branch_name);
7391 return NULL;
7394 const struct got_error *
7395 got_worktree_histedit_continue(struct got_object_id **commit_id,
7396 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7397 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7398 struct got_worktree *worktree, struct got_repository *repo)
7400 const struct got_error *err;
7401 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7402 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7403 struct got_reference *commit_ref = NULL;
7404 struct got_reference *base_commit_ref = NULL;
7405 char *fileindex_path = NULL;
7406 int have_staged_files = 0;
7408 *commit_id = NULL;
7409 *tmp_branch = NULL;
7410 *base_commit_id = NULL;
7411 *fileindex = NULL;
7413 err = lock_worktree(worktree, LOCK_EX);
7414 if (err)
7415 return err;
7417 err = open_fileindex(fileindex, &fileindex_path, worktree);
7418 if (err)
7419 goto done;
7421 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7422 &have_staged_files);
7423 if (err && err->code != GOT_ERR_CANCELLED)
7424 goto done;
7425 if (have_staged_files) {
7426 err = got_error(GOT_ERR_STAGED_PATHS);
7427 goto done;
7430 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7431 if (err)
7432 goto done;
7434 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7435 if (err)
7436 goto done;
7438 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7439 if (err)
7440 goto done;
7442 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7443 worktree);
7444 if (err)
7445 goto done;
7447 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7448 if (err)
7449 goto done;
7451 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7452 if (err)
7453 goto done;
7454 err = got_ref_resolve(commit_id, repo, commit_ref);
7455 if (err)
7456 goto done;
7458 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7459 if (err)
7460 goto done;
7461 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7462 if (err)
7463 goto done;
7465 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7466 if (err)
7467 goto done;
7468 done:
7469 free(commit_ref_name);
7470 free(branch_ref_name);
7471 free(fileindex_path);
7472 if (commit_ref)
7473 got_ref_close(commit_ref);
7474 if (base_commit_ref)
7475 got_ref_close(base_commit_ref);
7476 if (err) {
7477 free(*commit_id);
7478 *commit_id = NULL;
7479 free(*base_commit_id);
7480 *base_commit_id = NULL;
7481 if (*tmp_branch) {
7482 got_ref_close(*tmp_branch);
7483 *tmp_branch = NULL;
7485 if (*fileindex) {
7486 got_fileindex_free(*fileindex);
7487 *fileindex = NULL;
7489 lock_worktree(worktree, LOCK_EX);
7491 return err;
7494 static const struct got_error *
7495 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7497 const struct got_error *err;
7498 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7499 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7501 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7502 if (err)
7503 goto done;
7504 err = delete_ref(tmp_branch_name, repo);
7505 if (err)
7506 goto done;
7508 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7509 worktree);
7510 if (err)
7511 goto done;
7512 err = delete_ref(base_commit_ref_name, repo);
7513 if (err)
7514 goto done;
7516 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7517 if (err)
7518 goto done;
7519 err = delete_ref(branch_ref_name, repo);
7520 if (err)
7521 goto done;
7523 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7524 if (err)
7525 goto done;
7526 err = delete_ref(commit_ref_name, repo);
7527 if (err)
7528 goto done;
7529 done:
7530 free(tmp_branch_name);
7531 free(base_commit_ref_name);
7532 free(branch_ref_name);
7533 free(commit_ref_name);
7534 return err;
7537 const struct got_error *
7538 got_worktree_histedit_abort(struct got_worktree *worktree,
7539 struct got_fileindex *fileindex, struct got_repository *repo,
7540 struct got_reference *branch, struct got_object_id *base_commit_id,
7541 got_worktree_checkout_cb progress_cb, void *progress_arg)
7543 const struct got_error *err, *unlockerr, *sync_err;
7544 struct got_reference *resolved = NULL;
7545 char *fileindex_path = NULL;
7546 struct got_commit_object *commit = NULL;
7547 struct got_object_id *tree_id = NULL;
7548 struct revert_file_args rfa;
7550 err = lock_worktree(worktree, LOCK_EX);
7551 if (err)
7552 return err;
7554 err = got_object_open_as_commit(&commit, repo,
7555 worktree->base_commit_id);
7556 if (err)
7557 goto done;
7559 err = got_ref_open(&resolved, repo,
7560 got_ref_get_symref_target(branch), 0);
7561 if (err)
7562 goto done;
7564 err = got_worktree_set_head_ref(worktree, resolved);
7565 if (err)
7566 goto done;
7568 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7569 if (err)
7570 goto done;
7572 err = got_object_id_by_path(&tree_id, repo, commit,
7573 worktree->path_prefix);
7574 if (err)
7575 goto done;
7577 err = delete_histedit_refs(worktree, repo);
7578 if (err)
7579 goto done;
7581 err = get_fileindex_path(&fileindex_path, worktree);
7582 if (err)
7583 goto done;
7585 rfa.worktree = worktree;
7586 rfa.fileindex = fileindex;
7587 rfa.progress_cb = progress_cb;
7588 rfa.progress_arg = progress_arg;
7589 rfa.patch_cb = NULL;
7590 rfa.patch_arg = NULL;
7591 rfa.repo = repo;
7592 rfa.unlink_added_files = 0;
7593 err = worktree_status(worktree, "", fileindex, repo,
7594 revert_file, &rfa, NULL, NULL, 1, 0);
7595 if (err)
7596 goto sync;
7598 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7599 repo, progress_cb, progress_arg, NULL, NULL);
7600 sync:
7601 sync_err = sync_fileindex(fileindex, fileindex_path);
7602 if (sync_err && err == NULL)
7603 err = sync_err;
7604 done:
7605 got_ref_close(resolved);
7606 free(tree_id);
7607 free(fileindex_path);
7609 unlockerr = lock_worktree(worktree, LOCK_SH);
7610 if (unlockerr && err == NULL)
7611 err = unlockerr;
7612 return err;
7615 const struct got_error *
7616 got_worktree_histedit_complete(struct got_worktree *worktree,
7617 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7618 struct got_reference *edited_branch, struct got_repository *repo)
7620 const struct got_error *err, *unlockerr, *sync_err;
7621 struct got_object_id *new_head_commit_id = NULL;
7622 struct got_reference *resolved = NULL;
7623 char *fileindex_path = NULL;
7625 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7626 if (err)
7627 return err;
7629 err = got_ref_open(&resolved, repo,
7630 got_ref_get_symref_target(edited_branch), 0);
7631 if (err)
7632 goto done;
7634 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7635 resolved, new_head_commit_id, repo);
7636 if (err)
7637 goto done;
7639 err = got_ref_change_ref(resolved, new_head_commit_id);
7640 if (err)
7641 goto done;
7643 err = got_ref_write(resolved, repo);
7644 if (err)
7645 goto done;
7647 err = got_worktree_set_head_ref(worktree, resolved);
7648 if (err)
7649 goto done;
7651 err = delete_histedit_refs(worktree, repo);
7652 if (err)
7653 goto done;
7655 err = get_fileindex_path(&fileindex_path, worktree);
7656 if (err)
7657 goto done;
7658 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7659 sync_err = sync_fileindex(fileindex, fileindex_path);
7660 if (sync_err && err == NULL)
7661 err = sync_err;
7662 done:
7663 got_fileindex_free(fileindex);
7664 free(fileindex_path);
7665 free(new_head_commit_id);
7666 unlockerr = lock_worktree(worktree, LOCK_SH);
7667 if (unlockerr && err == NULL)
7668 err = unlockerr;
7669 return err;
7672 const struct got_error *
7673 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7674 struct got_object_id *commit_id, struct got_repository *repo)
7676 const struct got_error *err;
7677 char *commit_ref_name;
7679 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7680 if (err)
7681 return err;
7683 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7684 if (err)
7685 goto done;
7687 err = delete_ref(commit_ref_name, repo);
7688 done:
7689 free(commit_ref_name);
7690 return err;
7693 const struct got_error *
7694 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7695 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7696 struct got_worktree *worktree, const char *refname,
7697 struct got_repository *repo)
7699 const struct got_error *err = NULL;
7700 char *fileindex_path = NULL;
7701 struct check_rebase_ok_arg ok_arg;
7703 *fileindex = NULL;
7704 *branch_ref = NULL;
7705 *base_branch_ref = NULL;
7707 err = lock_worktree(worktree, LOCK_EX);
7708 if (err)
7709 return err;
7711 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7712 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7713 "cannot integrate a branch into itself; "
7714 "update -b or different branch name required");
7715 goto done;
7718 err = open_fileindex(fileindex, &fileindex_path, worktree);
7719 if (err)
7720 goto done;
7722 /* Preconditions are the same as for rebase. */
7723 ok_arg.worktree = worktree;
7724 ok_arg.repo = repo;
7725 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7726 &ok_arg);
7727 if (err)
7728 goto done;
7730 err = got_ref_open(branch_ref, repo, refname, 1);
7731 if (err)
7732 goto done;
7734 err = got_ref_open(base_branch_ref, repo,
7735 got_worktree_get_head_ref_name(worktree), 1);
7736 done:
7737 if (err) {
7738 if (*branch_ref) {
7739 got_ref_close(*branch_ref);
7740 *branch_ref = NULL;
7742 if (*base_branch_ref) {
7743 got_ref_close(*base_branch_ref);
7744 *base_branch_ref = NULL;
7746 if (*fileindex) {
7747 got_fileindex_free(*fileindex);
7748 *fileindex = NULL;
7750 lock_worktree(worktree, LOCK_SH);
7752 return err;
7755 const struct got_error *
7756 got_worktree_integrate_continue(struct got_worktree *worktree,
7757 struct got_fileindex *fileindex, struct got_repository *repo,
7758 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7759 got_worktree_checkout_cb progress_cb, void *progress_arg,
7760 got_cancel_cb cancel_cb, void *cancel_arg)
7762 const struct got_error *err = NULL, *sync_err, *unlockerr;
7763 char *fileindex_path = NULL;
7764 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7765 struct got_commit_object *commit = NULL;
7767 err = get_fileindex_path(&fileindex_path, worktree);
7768 if (err)
7769 goto done;
7771 err = got_ref_resolve(&commit_id, repo, branch_ref);
7772 if (err)
7773 goto done;
7775 err = got_object_open_as_commit(&commit, repo, commit_id);
7776 if (err)
7777 goto done;
7779 err = got_object_id_by_path(&tree_id, repo, commit,
7780 worktree->path_prefix);
7781 if (err)
7782 goto done;
7784 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7785 if (err)
7786 goto done;
7788 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7789 progress_cb, progress_arg, cancel_cb, cancel_arg);
7790 if (err)
7791 goto sync;
7793 err = got_ref_change_ref(base_branch_ref, commit_id);
7794 if (err)
7795 goto sync;
7797 err = got_ref_write(base_branch_ref, repo);
7798 if (err)
7799 goto sync;
7801 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7802 sync:
7803 sync_err = sync_fileindex(fileindex, fileindex_path);
7804 if (sync_err && err == NULL)
7805 err = sync_err;
7807 done:
7808 unlockerr = got_ref_unlock(branch_ref);
7809 if (unlockerr && err == NULL)
7810 err = unlockerr;
7811 got_ref_close(branch_ref);
7813 unlockerr = got_ref_unlock(base_branch_ref);
7814 if (unlockerr && err == NULL)
7815 err = unlockerr;
7816 got_ref_close(base_branch_ref);
7818 got_fileindex_free(fileindex);
7819 free(fileindex_path);
7820 free(tree_id);
7821 if (commit)
7822 got_object_commit_close(commit);
7824 unlockerr = lock_worktree(worktree, LOCK_SH);
7825 if (unlockerr && err == NULL)
7826 err = unlockerr;
7827 return err;
7830 const struct got_error *
7831 got_worktree_integrate_abort(struct got_worktree *worktree,
7832 struct got_fileindex *fileindex, struct got_repository *repo,
7833 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7835 const struct got_error *err = NULL, *unlockerr = NULL;
7837 got_fileindex_free(fileindex);
7839 err = lock_worktree(worktree, LOCK_SH);
7841 unlockerr = got_ref_unlock(branch_ref);
7842 if (unlockerr && err == NULL)
7843 err = unlockerr;
7844 got_ref_close(branch_ref);
7846 unlockerr = got_ref_unlock(base_branch_ref);
7847 if (unlockerr && err == NULL)
7848 err = unlockerr;
7849 got_ref_close(base_branch_ref);
7851 return err;
7854 const struct got_error *
7855 got_worktree_merge_postpone(struct got_worktree *worktree,
7856 struct got_fileindex *fileindex)
7858 const struct got_error *err, *sync_err;
7859 char *fileindex_path = NULL;
7861 err = get_fileindex_path(&fileindex_path, worktree);
7862 if (err)
7863 goto done;
7865 sync_err = sync_fileindex(fileindex, fileindex_path);
7867 err = lock_worktree(worktree, LOCK_SH);
7868 if (sync_err && err == NULL)
7869 err = sync_err;
7870 done:
7871 got_fileindex_free(fileindex);
7872 free(fileindex_path);
7873 return err;
7876 static const struct got_error *
7877 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7879 const struct got_error *err;
7880 char *branch_refname = NULL, *commit_refname = NULL;
7882 err = get_merge_branch_ref_name(&branch_refname, worktree);
7883 if (err)
7884 goto done;
7885 err = delete_ref(branch_refname, repo);
7886 if (err)
7887 goto done;
7889 err = get_merge_commit_ref_name(&commit_refname, worktree);
7890 if (err)
7891 goto done;
7892 err = delete_ref(commit_refname, repo);
7893 if (err)
7894 goto done;
7896 done:
7897 free(branch_refname);
7898 free(commit_refname);
7899 return err;
7902 struct merge_commit_msg_arg {
7903 struct got_worktree *worktree;
7904 const char *branch_name;
7907 static const struct got_error *
7908 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7909 const char *diff_path, char **logmsg, void *arg)
7911 struct merge_commit_msg_arg *a = arg;
7913 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7914 got_worktree_get_head_ref_name(a->worktree)) == -1)
7915 return got_error_from_errno("asprintf");
7917 return NULL;
7921 const struct got_error *
7922 got_worktree_merge_branch(struct got_worktree *worktree,
7923 struct got_fileindex *fileindex,
7924 struct got_object_id *yca_commit_id,
7925 struct got_object_id *branch_tip,
7926 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7927 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7929 const struct got_error *err;
7930 char *fileindex_path = NULL;
7932 err = get_fileindex_path(&fileindex_path, worktree);
7933 if (err)
7934 goto done;
7936 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7937 worktree);
7938 if (err)
7939 goto done;
7941 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7942 branch_tip, repo, progress_cb, progress_arg,
7943 cancel_cb, cancel_arg);
7944 done:
7945 free(fileindex_path);
7946 return err;
7949 const struct got_error *
7950 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7951 struct got_worktree *worktree, struct got_fileindex *fileindex,
7952 const char *author, const char *committer, int allow_bad_symlinks,
7953 struct got_object_id *branch_tip, const char *branch_name,
7954 int allow_conflict, struct got_repository *repo,
7955 got_worktree_status_cb status_cb, void *status_arg)
7958 const struct got_error *err = NULL, *sync_err;
7959 struct got_pathlist_head commitable_paths;
7960 struct collect_commitables_arg cc_arg;
7961 struct got_pathlist_entry *pe;
7962 struct got_reference *head_ref = NULL;
7963 struct got_object_id *head_commit_id = NULL;
7964 int have_staged_files = 0;
7965 struct merge_commit_msg_arg mcm_arg;
7966 char *fileindex_path = NULL;
7968 memset(&cc_arg, 0, sizeof(cc_arg));
7969 *new_commit_id = NULL;
7971 TAILQ_INIT(&commitable_paths);
7973 err = get_fileindex_path(&fileindex_path, worktree);
7974 if (err)
7975 goto done;
7977 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7978 if (err)
7979 goto done;
7981 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7982 if (err)
7983 goto done;
7985 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7986 &have_staged_files);
7987 if (err && err->code != GOT_ERR_CANCELLED)
7988 goto done;
7989 if (have_staged_files) {
7990 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7991 goto done;
7994 cc_arg.commitable_paths = &commitable_paths;
7995 cc_arg.worktree = worktree;
7996 cc_arg.fileindex = fileindex;
7997 cc_arg.repo = repo;
7998 cc_arg.have_staged_files = have_staged_files;
7999 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8000 cc_arg.commit_conflicts = allow_conflict;
8001 err = worktree_status(worktree, "", fileindex, repo,
8002 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8003 if (err)
8004 goto done;
8006 if (TAILQ_EMPTY(&commitable_paths)) {
8007 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8008 "merge of %s cannot proceed", branch_name);
8009 goto done;
8012 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8013 struct got_commitable *ct = pe->data;
8014 const char *ct_path = ct->in_repo_path;
8016 while (ct_path[0] == '/')
8017 ct_path++;
8018 err = check_out_of_date(ct_path, ct->status,
8019 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8020 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8021 if (err)
8022 goto done;
8026 mcm_arg.worktree = worktree;
8027 mcm_arg.branch_name = branch_name;
8028 err = commit_worktree(new_commit_id, &commitable_paths,
8029 head_commit_id, branch_tip, worktree, author, committer, NULL,
8030 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8031 if (err)
8032 goto done;
8034 err = update_fileindex_after_commit(worktree, &commitable_paths,
8035 *new_commit_id, fileindex, have_staged_files);
8036 sync_err = sync_fileindex(fileindex, fileindex_path);
8037 if (sync_err && err == NULL)
8038 err = sync_err;
8039 done:
8040 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8041 struct got_commitable *ct = pe->data;
8043 free_commitable(ct);
8045 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8046 free(fileindex_path);
8047 return err;
8050 const struct got_error *
8051 got_worktree_merge_complete(struct got_worktree *worktree,
8052 struct got_fileindex *fileindex, struct got_repository *repo)
8054 const struct got_error *err, *unlockerr, *sync_err;
8055 char *fileindex_path = NULL;
8057 err = delete_merge_refs(worktree, repo);
8058 if (err)
8059 goto done;
8061 err = get_fileindex_path(&fileindex_path, worktree);
8062 if (err)
8063 goto done;
8064 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8065 sync_err = sync_fileindex(fileindex, fileindex_path);
8066 if (sync_err && err == NULL)
8067 err = sync_err;
8068 done:
8069 got_fileindex_free(fileindex);
8070 free(fileindex_path);
8071 unlockerr = lock_worktree(worktree, LOCK_SH);
8072 if (unlockerr && err == NULL)
8073 err = unlockerr;
8074 return err;
8077 const struct got_error *
8078 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8079 struct got_repository *repo)
8081 const struct got_error *err;
8082 char *branch_refname = NULL;
8083 struct got_reference *branch_ref = NULL;
8085 *in_progress = 0;
8087 err = get_merge_branch_ref_name(&branch_refname, worktree);
8088 if (err)
8089 return err;
8090 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8091 free(branch_refname);
8092 if (err) {
8093 if (err->code != GOT_ERR_NOT_REF)
8094 return err;
8095 } else
8096 *in_progress = 1;
8098 return NULL;
8101 const struct got_error *got_worktree_merge_prepare(
8102 struct got_fileindex **fileindex, struct got_worktree *worktree,
8103 struct got_reference *branch, struct got_repository *repo)
8105 const struct got_error *err = NULL;
8106 char *fileindex_path = NULL;
8107 char *branch_refname = NULL, *commit_refname = NULL;
8108 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8109 struct got_reference *commit_ref = NULL;
8110 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8111 struct check_rebase_ok_arg ok_arg;
8113 *fileindex = NULL;
8115 err = lock_worktree(worktree, LOCK_EX);
8116 if (err)
8117 return err;
8119 err = open_fileindex(fileindex, &fileindex_path, worktree);
8120 if (err)
8121 goto done;
8123 /* Preconditions are the same as for rebase. */
8124 ok_arg.worktree = worktree;
8125 ok_arg.repo = repo;
8126 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8127 &ok_arg);
8128 if (err)
8129 goto done;
8131 err = get_merge_branch_ref_name(&branch_refname, worktree);
8132 if (err)
8133 return err;
8135 err = get_merge_commit_ref_name(&commit_refname, worktree);
8136 if (err)
8137 return err;
8139 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8140 0);
8141 if (err)
8142 goto done;
8144 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8145 if (err)
8146 goto done;
8148 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8149 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8150 goto done;
8153 err = got_ref_resolve(&branch_tip, repo, branch);
8154 if (err)
8155 goto done;
8157 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8158 if (err)
8159 goto done;
8160 err = got_ref_write(branch_ref, repo);
8161 if (err)
8162 goto done;
8164 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8165 if (err)
8166 goto done;
8167 err = got_ref_write(commit_ref, repo);
8168 if (err)
8169 goto done;
8171 done:
8172 free(branch_refname);
8173 free(commit_refname);
8174 free(fileindex_path);
8175 if (branch_ref)
8176 got_ref_close(branch_ref);
8177 if (commit_ref)
8178 got_ref_close(commit_ref);
8179 if (wt_branch)
8180 got_ref_close(wt_branch);
8181 free(wt_branch_tip);
8182 if (err) {
8183 if (*fileindex) {
8184 got_fileindex_free(*fileindex);
8185 *fileindex = NULL;
8187 lock_worktree(worktree, LOCK_SH);
8189 return err;
8192 const struct got_error *
8193 got_worktree_merge_continue(char **branch_name,
8194 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8195 struct got_worktree *worktree, struct got_repository *repo)
8197 const struct got_error *err;
8198 char *commit_refname = NULL, *branch_refname = NULL;
8199 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8200 char *fileindex_path = NULL;
8201 int have_staged_files = 0;
8203 *branch_name = NULL;
8204 *branch_tip = NULL;
8205 *fileindex = NULL;
8207 err = lock_worktree(worktree, LOCK_EX);
8208 if (err)
8209 return err;
8211 err = open_fileindex(fileindex, &fileindex_path, worktree);
8212 if (err)
8213 goto done;
8215 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8216 &have_staged_files);
8217 if (err && err->code != GOT_ERR_CANCELLED)
8218 goto done;
8219 if (have_staged_files) {
8220 err = got_error(GOT_ERR_STAGED_PATHS);
8221 goto done;
8224 err = get_merge_branch_ref_name(&branch_refname, worktree);
8225 if (err)
8226 goto done;
8228 err = get_merge_commit_ref_name(&commit_refname, worktree);
8229 if (err)
8230 goto done;
8232 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8233 if (err)
8234 goto done;
8236 if (!got_ref_is_symbolic(branch_ref)) {
8237 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8238 "%s is not a symbolic reference",
8239 got_ref_get_name(branch_ref));
8240 goto done;
8242 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8243 if (*branch_name == NULL) {
8244 err = got_error_from_errno("strdup");
8245 goto done;
8248 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8249 if (err)
8250 goto done;
8252 err = got_ref_resolve(branch_tip, repo, commit_ref);
8253 if (err)
8254 goto done;
8255 done:
8256 free(commit_refname);
8257 free(branch_refname);
8258 free(fileindex_path);
8259 if (commit_ref)
8260 got_ref_close(commit_ref);
8261 if (branch_ref)
8262 got_ref_close(branch_ref);
8263 if (err) {
8264 if (*branch_name) {
8265 free(*branch_name);
8266 *branch_name = NULL;
8268 free(*branch_tip);
8269 *branch_tip = NULL;
8270 if (*fileindex) {
8271 got_fileindex_free(*fileindex);
8272 *fileindex = NULL;
8274 lock_worktree(worktree, LOCK_SH);
8276 return err;
8279 const struct got_error *
8280 got_worktree_merge_abort(struct got_worktree *worktree,
8281 struct got_fileindex *fileindex, struct got_repository *repo,
8282 got_worktree_checkout_cb progress_cb, void *progress_arg)
8284 const struct got_error *err, *unlockerr, *sync_err;
8285 struct got_object_id *commit_id = NULL;
8286 struct got_commit_object *commit = NULL;
8287 char *fileindex_path = NULL;
8288 struct revert_file_args rfa;
8289 struct got_object_id *tree_id = NULL;
8291 err = got_object_open_as_commit(&commit, repo,
8292 worktree->base_commit_id);
8293 if (err)
8294 goto done;
8296 err = got_object_id_by_path(&tree_id, repo, commit,
8297 worktree->path_prefix);
8298 if (err)
8299 goto done;
8301 err = delete_merge_refs(worktree, repo);
8302 if (err)
8303 goto done;
8305 err = get_fileindex_path(&fileindex_path, worktree);
8306 if (err)
8307 goto done;
8309 rfa.worktree = worktree;
8310 rfa.fileindex = fileindex;
8311 rfa.progress_cb = progress_cb;
8312 rfa.progress_arg = progress_arg;
8313 rfa.patch_cb = NULL;
8314 rfa.patch_arg = NULL;
8315 rfa.repo = repo;
8316 rfa.unlink_added_files = 1;
8317 err = worktree_status(worktree, "", fileindex, repo,
8318 revert_file, &rfa, NULL, NULL, 1, 0);
8319 if (err)
8320 goto sync;
8322 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8323 repo, progress_cb, progress_arg, NULL, NULL);
8324 sync:
8325 sync_err = sync_fileindex(fileindex, fileindex_path);
8326 if (sync_err && err == NULL)
8327 err = sync_err;
8328 done:
8329 free(tree_id);
8330 free(commit_id);
8331 if (commit)
8332 got_object_commit_close(commit);
8333 if (fileindex)
8334 got_fileindex_free(fileindex);
8335 free(fileindex_path);
8337 unlockerr = lock_worktree(worktree, LOCK_SH);
8338 if (unlockerr && err == NULL)
8339 err = unlockerr;
8340 return err;
8343 struct check_stage_ok_arg {
8344 struct got_object_id *head_commit_id;
8345 struct got_worktree *worktree;
8346 struct got_fileindex *fileindex;
8347 struct got_repository *repo;
8348 int have_changes;
8351 static const struct got_error *
8352 check_stage_ok(void *arg, unsigned char status,
8353 unsigned char staged_status, const char *relpath,
8354 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8355 struct got_object_id *commit_id, int dirfd, const char *de_name)
8357 struct check_stage_ok_arg *a = arg;
8358 const struct got_error *err = NULL;
8359 struct got_fileindex_entry *ie;
8360 struct got_object_id base_commit_id;
8361 struct got_object_id *base_commit_idp = NULL;
8362 char *in_repo_path = NULL, *p;
8364 if (status == GOT_STATUS_UNVERSIONED ||
8365 status == GOT_STATUS_NO_CHANGE)
8366 return NULL;
8367 if (status == GOT_STATUS_NONEXISTENT)
8368 return got_error_set_errno(ENOENT, relpath);
8370 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8371 if (ie == NULL)
8372 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8374 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8375 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8376 relpath) == -1)
8377 return got_error_from_errno("asprintf");
8379 if (got_fileindex_entry_has_commit(ie)) {
8380 base_commit_idp = got_fileindex_entry_get_commit_id(
8381 &base_commit_id, ie);
8384 if (status == GOT_STATUS_CONFLICT) {
8385 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8386 goto done;
8387 } else if (status != GOT_STATUS_ADD &&
8388 status != GOT_STATUS_MODIFY &&
8389 status != GOT_STATUS_DELETE) {
8390 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8391 goto done;
8394 a->have_changes = 1;
8396 p = in_repo_path;
8397 while (p[0] == '/')
8398 p++;
8399 err = check_out_of_date(p, status, staged_status,
8400 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8401 GOT_ERR_STAGE_OUT_OF_DATE);
8402 done:
8403 free(in_repo_path);
8404 return err;
8407 struct stage_path_arg {
8408 struct got_worktree *worktree;
8409 struct got_fileindex *fileindex;
8410 struct got_repository *repo;
8411 got_worktree_status_cb status_cb;
8412 void *status_arg;
8413 got_worktree_patch_cb patch_cb;
8414 void *patch_arg;
8415 int staged_something;
8416 int allow_bad_symlinks;
8419 static const struct got_error *
8420 stage_path(void *arg, unsigned char status,
8421 unsigned char staged_status, const char *relpath,
8422 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8423 struct got_object_id *commit_id, int dirfd, const char *de_name)
8425 struct stage_path_arg *a = arg;
8426 const struct got_error *err = NULL;
8427 struct got_fileindex_entry *ie;
8428 char *ondisk_path = NULL, *path_content = NULL;
8429 uint32_t stage;
8430 struct got_object_id *new_staged_blob_id = NULL;
8431 struct stat sb;
8433 if (status == GOT_STATUS_UNVERSIONED)
8434 return NULL;
8436 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8437 if (ie == NULL)
8438 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8440 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8441 relpath)== -1)
8442 return got_error_from_errno("asprintf");
8444 switch (status) {
8445 case GOT_STATUS_ADD:
8446 case GOT_STATUS_MODIFY:
8447 /* XXX could sb.st_mode be passed in by our caller? */
8448 if (lstat(ondisk_path, &sb) == -1) {
8449 err = got_error_from_errno2("lstat", ondisk_path);
8450 break;
8452 if (a->patch_cb) {
8453 if (status == GOT_STATUS_ADD) {
8454 int choice = GOT_PATCH_CHOICE_NONE;
8455 err = (*a->patch_cb)(&choice, a->patch_arg,
8456 status, ie->path, NULL, 1, 1);
8457 if (err)
8458 break;
8459 if (choice != GOT_PATCH_CHOICE_YES)
8460 break;
8461 } else {
8462 err = create_patched_content(&path_content, 0,
8463 staged_blob_id ? staged_blob_id : blob_id,
8464 ondisk_path, dirfd, de_name, ie->path,
8465 a->repo, a->patch_cb, a->patch_arg);
8466 if (err || path_content == NULL)
8467 break;
8470 err = got_object_blob_create(&new_staged_blob_id,
8471 path_content ? path_content : ondisk_path, a->repo);
8472 if (err)
8473 break;
8474 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8475 SHA1_DIGEST_LENGTH);
8476 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8477 stage = GOT_FILEIDX_STAGE_ADD;
8478 else
8479 stage = GOT_FILEIDX_STAGE_MODIFY;
8480 got_fileindex_entry_stage_set(ie, stage);
8481 if (S_ISLNK(sb.st_mode)) {
8482 int is_bad_symlink = 0;
8483 if (!a->allow_bad_symlinks) {
8484 char target_path[PATH_MAX];
8485 ssize_t target_len;
8486 target_len = readlink(ondisk_path, target_path,
8487 sizeof(target_path));
8488 if (target_len == -1) {
8489 err = got_error_from_errno2("readlink",
8490 ondisk_path);
8491 break;
8493 err = is_bad_symlink_target(&is_bad_symlink,
8494 target_path, target_len, ondisk_path,
8495 a->worktree->root_path);
8496 if (err)
8497 break;
8498 if (is_bad_symlink) {
8499 err = got_error_path(ondisk_path,
8500 GOT_ERR_BAD_SYMLINK);
8501 break;
8504 if (is_bad_symlink)
8505 got_fileindex_entry_staged_filetype_set(ie,
8506 GOT_FILEIDX_MODE_BAD_SYMLINK);
8507 else
8508 got_fileindex_entry_staged_filetype_set(ie,
8509 GOT_FILEIDX_MODE_SYMLINK);
8510 } else {
8511 got_fileindex_entry_staged_filetype_set(ie,
8512 GOT_FILEIDX_MODE_REGULAR_FILE);
8514 a->staged_something = 1;
8515 if (a->status_cb == NULL)
8516 break;
8517 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8518 get_staged_status(ie), relpath, blob_id,
8519 new_staged_blob_id, NULL, dirfd, de_name);
8520 if (err)
8521 break;
8523 * When staging the reverse of the staged diff,
8524 * implicitly unstage the file.
8526 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8527 sizeof(ie->blob_sha1)) == 0) {
8528 got_fileindex_entry_stage_set(ie,
8529 GOT_FILEIDX_STAGE_NONE);
8531 break;
8532 case GOT_STATUS_DELETE:
8533 if (staged_status == GOT_STATUS_DELETE)
8534 break;
8535 if (a->patch_cb) {
8536 int choice = GOT_PATCH_CHOICE_NONE;
8537 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8538 ie->path, NULL, 1, 1);
8539 if (err)
8540 break;
8541 if (choice == GOT_PATCH_CHOICE_NO)
8542 break;
8543 if (choice != GOT_PATCH_CHOICE_YES) {
8544 err = got_error(GOT_ERR_PATCH_CHOICE);
8545 break;
8548 stage = GOT_FILEIDX_STAGE_DELETE;
8549 got_fileindex_entry_stage_set(ie, stage);
8550 a->staged_something = 1;
8551 if (a->status_cb == NULL)
8552 break;
8553 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8554 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8555 de_name);
8556 break;
8557 case GOT_STATUS_NO_CHANGE:
8558 break;
8559 case GOT_STATUS_CONFLICT:
8560 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8561 break;
8562 case GOT_STATUS_NONEXISTENT:
8563 err = got_error_set_errno(ENOENT, relpath);
8564 break;
8565 default:
8566 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8567 break;
8570 if (path_content && unlink(path_content) == -1 && err == NULL)
8571 err = got_error_from_errno2("unlink", path_content);
8572 free(path_content);
8573 free(ondisk_path);
8574 free(new_staged_blob_id);
8575 return err;
8578 const struct got_error *
8579 got_worktree_stage(struct got_worktree *worktree,
8580 struct got_pathlist_head *paths,
8581 got_worktree_status_cb status_cb, void *status_arg,
8582 got_worktree_patch_cb patch_cb, void *patch_arg,
8583 int allow_bad_symlinks, struct got_repository *repo)
8585 const struct got_error *err = NULL, *sync_err, *unlockerr;
8586 struct got_pathlist_entry *pe;
8587 struct got_fileindex *fileindex = NULL;
8588 char *fileindex_path = NULL;
8589 struct got_reference *head_ref = NULL;
8590 struct got_object_id *head_commit_id = NULL;
8591 struct check_stage_ok_arg oka;
8592 struct stage_path_arg spa;
8594 err = lock_worktree(worktree, LOCK_EX);
8595 if (err)
8596 return err;
8598 err = got_ref_open(&head_ref, repo,
8599 got_worktree_get_head_ref_name(worktree), 0);
8600 if (err)
8601 goto done;
8602 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8603 if (err)
8604 goto done;
8605 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8606 if (err)
8607 goto done;
8609 /* Check pre-conditions before staging anything. */
8610 oka.head_commit_id = head_commit_id;
8611 oka.worktree = worktree;
8612 oka.fileindex = fileindex;
8613 oka.repo = repo;
8614 oka.have_changes = 0;
8615 TAILQ_FOREACH(pe, paths, entry) {
8616 err = worktree_status(worktree, pe->path, fileindex, repo,
8617 check_stage_ok, &oka, NULL, NULL, 1, 0);
8618 if (err)
8619 goto done;
8621 if (!oka.have_changes) {
8622 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8623 goto done;
8626 spa.worktree = worktree;
8627 spa.fileindex = fileindex;
8628 spa.repo = repo;
8629 spa.patch_cb = patch_cb;
8630 spa.patch_arg = patch_arg;
8631 spa.status_cb = status_cb;
8632 spa.status_arg = status_arg;
8633 spa.staged_something = 0;
8634 spa.allow_bad_symlinks = allow_bad_symlinks;
8635 TAILQ_FOREACH(pe, paths, entry) {
8636 err = worktree_status(worktree, pe->path, fileindex, repo,
8637 stage_path, &spa, NULL, NULL, 1, 0);
8638 if (err)
8639 goto done;
8641 if (!spa.staged_something) {
8642 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8643 goto done;
8646 sync_err = sync_fileindex(fileindex, fileindex_path);
8647 if (sync_err && err == NULL)
8648 err = sync_err;
8649 done:
8650 if (head_ref)
8651 got_ref_close(head_ref);
8652 free(head_commit_id);
8653 free(fileindex_path);
8654 if (fileindex)
8655 got_fileindex_free(fileindex);
8656 unlockerr = lock_worktree(worktree, LOCK_SH);
8657 if (unlockerr && err == NULL)
8658 err = unlockerr;
8659 return err;
8662 struct unstage_path_arg {
8663 struct got_worktree *worktree;
8664 struct got_fileindex *fileindex;
8665 struct got_repository *repo;
8666 got_worktree_checkout_cb progress_cb;
8667 void *progress_arg;
8668 got_worktree_patch_cb patch_cb;
8669 void *patch_arg;
8672 static const struct got_error *
8673 create_unstaged_content(char **path_unstaged_content,
8674 char **path_new_staged_content, struct got_object_id *blob_id,
8675 struct got_object_id *staged_blob_id, const char *relpath,
8676 struct got_repository *repo,
8677 got_worktree_patch_cb patch_cb, void *patch_arg)
8679 const struct got_error *err, *free_err;
8680 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8681 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8682 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8683 struct got_diffreg_result *diffreg_result = NULL;
8684 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8685 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8686 int fd1 = -1, fd2 = -1;
8688 *path_unstaged_content = NULL;
8689 *path_new_staged_content = NULL;
8691 err = got_object_id_str(&label1, blob_id);
8692 if (err)
8693 return err;
8695 fd1 = got_opentempfd();
8696 if (fd1 == -1) {
8697 err = got_error_from_errno("got_opentempfd");
8698 goto done;
8700 fd2 = got_opentempfd();
8701 if (fd2 == -1) {
8702 err = got_error_from_errno("got_opentempfd");
8703 goto done;
8706 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8707 if (err)
8708 goto done;
8710 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8711 if (err)
8712 goto done;
8714 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8715 if (err)
8716 goto done;
8718 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8719 fd2);
8720 if (err)
8721 goto done;
8723 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8724 if (err)
8725 goto done;
8727 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8728 if (err)
8729 goto done;
8731 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8732 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8733 if (err)
8734 goto done;
8736 err = got_opentemp_named(path_unstaged_content, &outfile,
8737 "got-unstaged-content", "");
8738 if (err)
8739 goto done;
8740 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8741 "got-new-staged-content", "");
8742 if (err)
8743 goto done;
8745 if (fseek(f1, 0L, SEEK_SET) == -1) {
8746 err = got_ferror(f1, GOT_ERR_IO);
8747 goto done;
8749 if (fseek(f2, 0L, SEEK_SET) == -1) {
8750 err = got_ferror(f2, GOT_ERR_IO);
8751 goto done;
8753 /* Count the number of actual changes in the diff result. */
8754 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8755 struct diff_chunk_context cc = {};
8756 diff_chunk_context_load_change(&cc, &nchunks_used,
8757 diffreg_result->result, n, 0);
8758 nchanges++;
8760 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8761 int choice;
8762 err = apply_or_reject_change(&choice, &nchunks_used,
8763 diffreg_result->result, n, relpath, f1, f2,
8764 &line_cur1, &line_cur2,
8765 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8766 if (err)
8767 goto done;
8768 if (choice == GOT_PATCH_CHOICE_YES)
8769 have_content = 1;
8770 else
8771 have_rejected_content = 1;
8772 if (choice == GOT_PATCH_CHOICE_QUIT)
8773 break;
8775 if (have_content || have_rejected_content)
8776 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8777 outfile, rejectfile);
8778 done:
8779 free(label1);
8780 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8781 err = got_error_from_errno("close");
8782 if (blob)
8783 got_object_blob_close(blob);
8784 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8785 err = got_error_from_errno("close");
8786 if (staged_blob)
8787 got_object_blob_close(staged_blob);
8788 free_err = got_diffreg_result_free(diffreg_result);
8789 if (free_err && err == NULL)
8790 err = free_err;
8791 if (f1 && fclose(f1) == EOF && err == NULL)
8792 err = got_error_from_errno2("fclose", path1);
8793 if (f2 && fclose(f2) == EOF && err == NULL)
8794 err = got_error_from_errno2("fclose", path2);
8795 if (outfile && fclose(outfile) == EOF && err == NULL)
8796 err = got_error_from_errno2("fclose", *path_unstaged_content);
8797 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8798 err = got_error_from_errno2("fclose", *path_new_staged_content);
8799 if (path1 && unlink(path1) == -1 && err == NULL)
8800 err = got_error_from_errno2("unlink", path1);
8801 if (path2 && unlink(path2) == -1 && err == NULL)
8802 err = got_error_from_errno2("unlink", path2);
8803 if (err || !have_content) {
8804 if (*path_unstaged_content &&
8805 unlink(*path_unstaged_content) == -1 && err == NULL)
8806 err = got_error_from_errno2("unlink",
8807 *path_unstaged_content);
8808 free(*path_unstaged_content);
8809 *path_unstaged_content = NULL;
8811 if (err || !have_content || !have_rejected_content) {
8812 if (*path_new_staged_content &&
8813 unlink(*path_new_staged_content) == -1 && err == NULL)
8814 err = got_error_from_errno2("unlink",
8815 *path_new_staged_content);
8816 free(*path_new_staged_content);
8817 *path_new_staged_content = NULL;
8819 free(path1);
8820 free(path2);
8821 return err;
8824 static const struct got_error *
8825 unstage_hunks(struct got_object_id *staged_blob_id,
8826 struct got_blob_object *blob_base,
8827 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8828 const char *ondisk_path, const char *label_orig,
8829 struct got_worktree *worktree, struct got_repository *repo,
8830 got_worktree_patch_cb patch_cb, void *patch_arg,
8831 got_worktree_checkout_cb progress_cb, void *progress_arg)
8833 const struct got_error *err = NULL;
8834 char *path_unstaged_content = NULL;
8835 char *path_new_staged_content = NULL;
8836 char *parent = NULL, *base_path = NULL;
8837 char *blob_base_path = NULL;
8838 struct got_object_id *new_staged_blob_id = NULL;
8839 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8840 struct stat sb;
8842 err = create_unstaged_content(&path_unstaged_content,
8843 &path_new_staged_content, blob_id, staged_blob_id,
8844 ie->path, repo, patch_cb, patch_arg);
8845 if (err)
8846 return err;
8848 if (path_unstaged_content == NULL)
8849 return NULL;
8851 if (path_new_staged_content) {
8852 err = got_object_blob_create(&new_staged_blob_id,
8853 path_new_staged_content, repo);
8854 if (err)
8855 goto done;
8858 f = fopen(path_unstaged_content, "re");
8859 if (f == NULL) {
8860 err = got_error_from_errno2("fopen",
8861 path_unstaged_content);
8862 goto done;
8864 if (fstat(fileno(f), &sb) == -1) {
8865 err = got_error_from_errno2("fstat", path_unstaged_content);
8866 goto done;
8868 if (got_fileindex_entry_staged_filetype_get(ie) ==
8869 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8870 char link_target[PATH_MAX];
8871 size_t r;
8872 r = fread(link_target, 1, sizeof(link_target), f);
8873 if (r == 0 && ferror(f)) {
8874 err = got_error_from_errno("fread");
8875 goto done;
8877 if (r >= sizeof(link_target)) { /* should not happen */
8878 err = got_error(GOT_ERR_NO_SPACE);
8879 goto done;
8881 link_target[r] = '\0';
8882 err = merge_symlink(worktree, blob_base,
8883 ondisk_path, ie->path, label_orig, link_target,
8884 worktree->base_commit_id, repo, progress_cb,
8885 progress_arg);
8886 } else {
8887 int local_changes_subsumed;
8889 err = got_path_dirname(&parent, ondisk_path);
8890 if (err)
8891 return err;
8893 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8894 parent) == -1) {
8895 err = got_error_from_errno("asprintf");
8896 base_path = NULL;
8897 goto done;
8900 err = got_opentemp_named(&blob_base_path, &f_base,
8901 base_path, "");
8902 if (err)
8903 goto done;
8904 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8905 blob_base);
8906 if (err)
8907 goto done;
8910 * In order the run a 3-way merge with a symlink we copy the symlink's
8911 * target path into a temporary file and use that file with diff3.
8913 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8914 err = dump_symlink_target_path_to_file(&f_deriv2,
8915 ondisk_path);
8916 if (err)
8917 goto done;
8918 } else {
8919 int fd;
8920 fd = open(ondisk_path,
8921 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8922 if (fd == -1) {
8923 err = got_error_from_errno2("open", ondisk_path);
8924 goto done;
8926 f_deriv2 = fdopen(fd, "r");
8927 if (f_deriv2 == NULL) {
8928 err = got_error_from_errno2("fdopen", ondisk_path);
8929 close(fd);
8930 goto done;
8934 err = merge_file(&local_changes_subsumed, worktree,
8935 f_base, f, f_deriv2, ondisk_path, ie->path,
8936 got_fileindex_perms_to_st(ie),
8937 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8938 repo, progress_cb, progress_arg);
8940 if (err)
8941 goto done;
8943 if (new_staged_blob_id) {
8944 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8945 SHA1_DIGEST_LENGTH);
8946 } else {
8947 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8948 got_fileindex_entry_staged_filetype_set(ie, 0);
8950 done:
8951 free(new_staged_blob_id);
8952 if (path_unstaged_content &&
8953 unlink(path_unstaged_content) == -1 && err == NULL)
8954 err = got_error_from_errno2("unlink", path_unstaged_content);
8955 if (path_new_staged_content &&
8956 unlink(path_new_staged_content) == -1 && err == NULL)
8957 err = got_error_from_errno2("unlink", path_new_staged_content);
8958 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8959 err = got_error_from_errno2("unlink", blob_base_path);
8960 if (f_base && fclose(f_base) == EOF && err == NULL)
8961 err = got_error_from_errno2("fclose", path_unstaged_content);
8962 if (f && fclose(f) == EOF && err == NULL)
8963 err = got_error_from_errno2("fclose", path_unstaged_content);
8964 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8965 err = got_error_from_errno2("fclose", ondisk_path);
8966 free(path_unstaged_content);
8967 free(path_new_staged_content);
8968 free(blob_base_path);
8969 free(parent);
8970 free(base_path);
8971 return err;
8974 static const struct got_error *
8975 unstage_path(void *arg, unsigned char status,
8976 unsigned char staged_status, const char *relpath,
8977 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8978 struct got_object_id *commit_id, int dirfd, const char *de_name)
8980 const struct got_error *err = NULL;
8981 struct unstage_path_arg *a = arg;
8982 struct got_fileindex_entry *ie;
8983 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8984 char *ondisk_path = NULL;
8985 char *id_str = NULL, *label_orig = NULL;
8986 int local_changes_subsumed;
8987 struct stat sb;
8988 int fd1 = -1, fd2 = -1;
8990 if (staged_status != GOT_STATUS_ADD &&
8991 staged_status != GOT_STATUS_MODIFY &&
8992 staged_status != GOT_STATUS_DELETE)
8993 return NULL;
8995 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8996 if (ie == NULL)
8997 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8999 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9000 == -1)
9001 return got_error_from_errno("asprintf");
9003 err = got_object_id_str(&id_str,
9004 commit_id ? commit_id : a->worktree->base_commit_id);
9005 if (err)
9006 goto done;
9007 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9008 id_str) == -1) {
9009 err = got_error_from_errno("asprintf");
9010 goto done;
9013 fd1 = got_opentempfd();
9014 if (fd1 == -1) {
9015 err = got_error_from_errno("got_opentempfd");
9016 goto done;
9018 fd2 = got_opentempfd();
9019 if (fd2 == -1) {
9020 err = got_error_from_errno("got_opentempfd");
9021 goto done;
9024 switch (staged_status) {
9025 case GOT_STATUS_MODIFY:
9026 err = got_object_open_as_blob(&blob_base, a->repo,
9027 blob_id, 8192, fd1);
9028 if (err)
9029 break;
9030 /* fall through */
9031 case GOT_STATUS_ADD:
9032 if (a->patch_cb) {
9033 if (staged_status == GOT_STATUS_ADD) {
9034 int choice = GOT_PATCH_CHOICE_NONE;
9035 err = (*a->patch_cb)(&choice, a->patch_arg,
9036 staged_status, ie->path, NULL, 1, 1);
9037 if (err)
9038 break;
9039 if (choice != GOT_PATCH_CHOICE_YES)
9040 break;
9041 } else {
9042 err = unstage_hunks(staged_blob_id,
9043 blob_base, blob_id, ie, ondisk_path,
9044 label_orig, a->worktree, a->repo,
9045 a->patch_cb, a->patch_arg,
9046 a->progress_cb, a->progress_arg);
9047 break; /* Done with this file. */
9050 err = got_object_open_as_blob(&blob_staged, a->repo,
9051 staged_blob_id, 8192, fd2);
9052 if (err)
9053 break;
9054 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9055 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9056 case GOT_FILEIDX_MODE_REGULAR_FILE:
9057 err = merge_blob(&local_changes_subsumed, a->worktree,
9058 blob_base, ondisk_path, relpath,
9059 got_fileindex_perms_to_st(ie), label_orig,
9060 blob_staged, commit_id ? commit_id :
9061 a->worktree->base_commit_id, a->repo,
9062 a->progress_cb, a->progress_arg);
9063 break;
9064 case GOT_FILEIDX_MODE_SYMLINK:
9065 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9066 char *staged_target;
9067 err = got_object_blob_read_to_str(
9068 &staged_target, blob_staged);
9069 if (err)
9070 goto done;
9071 err = merge_symlink(a->worktree, blob_base,
9072 ondisk_path, relpath, label_orig,
9073 staged_target, commit_id ? commit_id :
9074 a->worktree->base_commit_id,
9075 a->repo, a->progress_cb, a->progress_arg);
9076 free(staged_target);
9077 } else {
9078 err = merge_blob(&local_changes_subsumed,
9079 a->worktree, blob_base, ondisk_path,
9080 relpath, got_fileindex_perms_to_st(ie),
9081 label_orig, blob_staged,
9082 commit_id ? commit_id :
9083 a->worktree->base_commit_id, a->repo,
9084 a->progress_cb, a->progress_arg);
9086 break;
9087 default:
9088 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9089 break;
9091 if (err == NULL) {
9092 got_fileindex_entry_stage_set(ie,
9093 GOT_FILEIDX_STAGE_NONE);
9094 got_fileindex_entry_staged_filetype_set(ie, 0);
9096 break;
9097 case GOT_STATUS_DELETE:
9098 if (a->patch_cb) {
9099 int choice = GOT_PATCH_CHOICE_NONE;
9100 err = (*a->patch_cb)(&choice, a->patch_arg,
9101 staged_status, ie->path, NULL, 1, 1);
9102 if (err)
9103 break;
9104 if (choice == GOT_PATCH_CHOICE_NO)
9105 break;
9106 if (choice != GOT_PATCH_CHOICE_YES) {
9107 err = got_error(GOT_ERR_PATCH_CHOICE);
9108 break;
9111 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9112 got_fileindex_entry_staged_filetype_set(ie, 0);
9113 err = get_file_status(&status, &sb, ie, ondisk_path,
9114 dirfd, de_name, a->repo);
9115 if (err)
9116 break;
9117 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9118 break;
9120 done:
9121 free(ondisk_path);
9122 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9123 err = got_error_from_errno("close");
9124 if (blob_base)
9125 got_object_blob_close(blob_base);
9126 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9127 err = got_error_from_errno("close");
9128 if (blob_staged)
9129 got_object_blob_close(blob_staged);
9130 free(id_str);
9131 free(label_orig);
9132 return err;
9135 const struct got_error *
9136 got_worktree_unstage(struct got_worktree *worktree,
9137 struct got_pathlist_head *paths,
9138 got_worktree_checkout_cb progress_cb, void *progress_arg,
9139 got_worktree_patch_cb patch_cb, void *patch_arg,
9140 struct got_repository *repo)
9142 const struct got_error *err = NULL, *sync_err, *unlockerr;
9143 struct got_pathlist_entry *pe;
9144 struct got_fileindex *fileindex = NULL;
9145 char *fileindex_path = NULL;
9146 struct unstage_path_arg upa;
9148 err = lock_worktree(worktree, LOCK_EX);
9149 if (err)
9150 return err;
9152 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9153 if (err)
9154 goto done;
9156 upa.worktree = worktree;
9157 upa.fileindex = fileindex;
9158 upa.repo = repo;
9159 upa.progress_cb = progress_cb;
9160 upa.progress_arg = progress_arg;
9161 upa.patch_cb = patch_cb;
9162 upa.patch_arg = patch_arg;
9163 TAILQ_FOREACH(pe, paths, entry) {
9164 err = worktree_status(worktree, pe->path, fileindex, repo,
9165 unstage_path, &upa, NULL, NULL, 1, 0);
9166 if (err)
9167 goto done;
9170 sync_err = sync_fileindex(fileindex, fileindex_path);
9171 if (sync_err && err == NULL)
9172 err = sync_err;
9173 done:
9174 free(fileindex_path);
9175 if (fileindex)
9176 got_fileindex_free(fileindex);
9177 unlockerr = lock_worktree(worktree, LOCK_SH);
9178 if (unlockerr && err == NULL)
9179 err = unlockerr;
9180 return err;
9183 struct report_file_info_arg {
9184 struct got_worktree *worktree;
9185 got_worktree_path_info_cb info_cb;
9186 void *info_arg;
9187 struct got_pathlist_head *paths;
9188 got_cancel_cb cancel_cb;
9189 void *cancel_arg;
9192 static const struct got_error *
9193 report_file_info(void *arg, struct got_fileindex_entry *ie)
9195 struct report_file_info_arg *a = arg;
9196 struct got_pathlist_entry *pe;
9197 struct got_object_id blob_id, staged_blob_id, commit_id;
9198 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9199 struct got_object_id *commit_idp = NULL;
9200 int stage;
9202 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9203 return got_error(GOT_ERR_CANCELLED);
9205 TAILQ_FOREACH(pe, a->paths, entry) {
9206 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9207 got_path_is_child(ie->path, pe->path, pe->path_len))
9208 break;
9210 if (pe == NULL) /* not found */
9211 return NULL;
9213 if (got_fileindex_entry_has_blob(ie))
9214 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9215 stage = got_fileindex_entry_stage_get(ie);
9216 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9217 stage == GOT_FILEIDX_STAGE_ADD) {
9218 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9219 &staged_blob_id, ie);
9222 if (got_fileindex_entry_has_commit(ie))
9223 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9225 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9226 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9229 const struct got_error *
9230 got_worktree_path_info(struct got_worktree *worktree,
9231 struct got_pathlist_head *paths,
9232 got_worktree_path_info_cb info_cb, void *info_arg,
9233 got_cancel_cb cancel_cb, void *cancel_arg)
9236 const struct got_error *err = NULL, *unlockerr;
9237 struct got_fileindex *fileindex = NULL;
9238 char *fileindex_path = NULL;
9239 struct report_file_info_arg arg;
9241 err = lock_worktree(worktree, LOCK_SH);
9242 if (err)
9243 return err;
9245 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9246 if (err)
9247 goto done;
9249 arg.worktree = worktree;
9250 arg.info_cb = info_cb;
9251 arg.info_arg = info_arg;
9252 arg.paths = paths;
9253 arg.cancel_cb = cancel_cb;
9254 arg.cancel_arg = cancel_arg;
9255 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9256 &arg);
9257 done:
9258 free(fileindex_path);
9259 if (fileindex)
9260 got_fileindex_free(fileindex);
9261 unlockerr = lock_worktree(worktree, LOCK_UN);
9262 if (unlockerr && err == NULL)
9263 err = unlockerr;
9264 return err;
9267 static const struct got_error *
9268 patch_check_path(const char *p, char **path, unsigned char *status,
9269 unsigned char *staged_status, struct got_fileindex *fileindex,
9270 struct got_worktree *worktree, struct got_repository *repo)
9272 const struct got_error *err;
9273 struct got_fileindex_entry *ie;
9274 struct stat sb;
9275 char *ondisk_path = NULL;
9277 err = got_worktree_resolve_path(path, worktree, p);
9278 if (err)
9279 return err;
9281 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9282 *path[0] ? "/" : "", *path) == -1)
9283 return got_error_from_errno("asprintf");
9285 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9286 if (ie) {
9287 *staged_status = get_staged_status(ie);
9288 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9289 repo);
9290 if (err)
9291 goto done;
9292 } else {
9293 *staged_status = GOT_STATUS_NO_CHANGE;
9294 *status = GOT_STATUS_UNVERSIONED;
9295 if (lstat(ondisk_path, &sb) == -1) {
9296 if (errno != ENOENT) {
9297 err = got_error_from_errno2("lstat",
9298 ondisk_path);
9299 goto done;
9301 *status = GOT_STATUS_NONEXISTENT;
9305 done:
9306 free(ondisk_path);
9307 return err;
9310 static const struct got_error *
9311 patch_can_rm(const char *path, unsigned char status,
9312 unsigned char staged_status)
9314 if (status == GOT_STATUS_NONEXISTENT)
9315 return got_error_set_errno(ENOENT, path);
9316 if (status != GOT_STATUS_NO_CHANGE &&
9317 status != GOT_STATUS_ADD &&
9318 status != GOT_STATUS_MODIFY &&
9319 status != GOT_STATUS_MODE_CHANGE)
9320 return got_error_path(path, GOT_ERR_FILE_STATUS);
9321 if (staged_status == GOT_STATUS_DELETE)
9322 return got_error_path(path, GOT_ERR_FILE_STATUS);
9323 return NULL;
9326 static const struct got_error *
9327 patch_can_add(const char *path, unsigned char status)
9329 if (status != GOT_STATUS_NONEXISTENT)
9330 return got_error_path(path, GOT_ERR_FILE_STATUS);
9331 return NULL;
9334 static const struct got_error *
9335 patch_can_edit(const char *path, unsigned char status,
9336 unsigned char staged_status)
9338 if (status == GOT_STATUS_NONEXISTENT)
9339 return got_error_set_errno(ENOENT, path);
9340 if (status != GOT_STATUS_NO_CHANGE &&
9341 status != GOT_STATUS_ADD &&
9342 status != GOT_STATUS_MODIFY)
9343 return got_error_path(path, GOT_ERR_FILE_STATUS);
9344 if (staged_status == GOT_STATUS_DELETE)
9345 return got_error_path(path, GOT_ERR_FILE_STATUS);
9346 return NULL;
9349 const struct got_error *
9350 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9351 char **fileindex_path, struct got_worktree *worktree)
9353 return open_fileindex(fileindex, fileindex_path, worktree);
9356 const struct got_error *
9357 got_worktree_patch_check_path(const char *old, const char *new,
9358 char **oldpath, char **newpath, struct got_worktree *worktree,
9359 struct got_repository *repo, struct got_fileindex *fileindex)
9361 const struct got_error *err = NULL;
9362 int file_renamed = 0;
9363 unsigned char status_old, staged_status_old;
9364 unsigned char status_new, staged_status_new;
9366 *oldpath = NULL;
9367 *newpath = NULL;
9369 err = patch_check_path(old != NULL ? old : new, oldpath,
9370 &status_old, &staged_status_old, fileindex, worktree, repo);
9371 if (err)
9372 goto done;
9374 err = patch_check_path(new != NULL ? new : old, newpath,
9375 &status_new, &staged_status_new, fileindex, worktree, repo);
9376 if (err)
9377 goto done;
9379 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9380 file_renamed = 1;
9382 if (old != NULL && new == NULL)
9383 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9384 else if (file_renamed) {
9385 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9386 if (err == NULL)
9387 err = patch_can_add(*newpath, status_new);
9388 } else if (old == NULL)
9389 err = patch_can_add(*newpath, status_new);
9390 else
9391 err = patch_can_edit(*newpath, status_new, staged_status_new);
9393 done:
9394 if (err) {
9395 free(*oldpath);
9396 *oldpath = NULL;
9397 free(*newpath);
9398 *newpath = NULL;
9400 return err;
9403 const struct got_error *
9404 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9405 struct got_worktree *worktree, struct got_fileindex *fileindex,
9406 got_worktree_checkout_cb progress_cb, void *progress_arg)
9408 struct schedule_addition_args saa;
9410 memset(&saa, 0, sizeof(saa));
9411 saa.worktree = worktree;
9412 saa.fileindex = fileindex;
9413 saa.progress_cb = progress_cb;
9414 saa.progress_arg = progress_arg;
9415 saa.repo = repo;
9417 return worktree_status(worktree, path, fileindex, repo,
9418 schedule_addition, &saa, NULL, NULL, 1, 0);
9421 const struct got_error *
9422 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9423 struct got_worktree *worktree, struct got_fileindex *fileindex,
9424 got_worktree_delete_cb progress_cb, void *progress_arg)
9426 struct schedule_deletion_args sda;
9428 memset(&sda, 0, sizeof(sda));
9429 sda.worktree = worktree;
9430 sda.fileindex = fileindex;
9431 sda.progress_cb = progress_cb;
9432 sda.progress_arg = progress_arg;
9433 sda.repo = repo;
9434 sda.delete_local_mods = 0;
9435 sda.keep_on_disk = 0;
9436 sda.ignore_missing_paths = 0;
9437 sda.status_codes = NULL;
9439 return worktree_status(worktree, path, fileindex, repo,
9440 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9443 const struct got_error *
9444 got_worktree_patch_complete(struct got_fileindex *fileindex,
9445 const char *fileindex_path)
9447 const struct got_error *err = NULL;
9449 err = sync_fileindex(fileindex, fileindex_path);
9450 got_fileindex_free(fileindex);
9452 return err;