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;
1564 int clc, crc;
1567 * We can optimise a little by advancing straight
1568 * to the next chunk if this one has no added lines.
1570 c = diff_chunk_get(r, n);
1571 clc = diff_chunk_get_left_count(c);
1572 crc = diff_chunk_get_right_count(c);
1574 if (!crc || crc == clc) {
1575 nchunks_parsed = 1;
1576 continue; /* removed or unchanged lines */
1579 pos = diff_chunk_get_right_start_pos(c);
1580 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 err = got_ferror(ondisk_file, GOT_ERR_IO);
1582 goto done;
1585 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 ln = cc.right.start;
1588 while (ln < cc.right.end) {
1589 linelen = getline(&line, &linesize, ondisk_file);
1590 if (linelen == -1) {
1591 if (feof(ondisk_file))
1592 break;
1593 err = got_ferror(ondisk_file, GOT_ERR_IO);
1594 break;
1597 if (line && strncmp(line, markers[i],
1598 strlen(markers[i])) == 0) {
1599 if (strcmp(markers[i],
1600 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 *status = GOT_STATUS_CONFLICT;
1602 goto done;
1603 } else
1604 i++;
1606 ++ln;
1610 done:
1611 free(line);
1612 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 err = got_error_from_errno("fclose");
1614 free_err = got_diffreg_result_free(diffreg_result);
1615 if (err == NULL)
1616 err = free_err;
1618 return err;
1621 static int
1622 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1624 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1628 static int
1629 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1631 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 ie->size == (sb->st_size & 0xffffffff) &&
1636 !xbit_differs(ie, sb->st_mode));
1639 static unsigned char
1640 get_staged_status(struct got_fileindex_entry *ie)
1642 switch (got_fileindex_entry_stage_get(ie)) {
1643 case GOT_FILEIDX_STAGE_ADD:
1644 return GOT_STATUS_ADD;
1645 case GOT_FILEIDX_STAGE_DELETE:
1646 return GOT_STATUS_DELETE;
1647 case GOT_FILEIDX_STAGE_MODIFY:
1648 return GOT_STATUS_MODIFY;
1649 default:
1650 return GOT_STATUS_NO_CHANGE;
1654 static const struct got_error *
1655 get_symlink_modification_status(unsigned char *status,
1656 struct got_fileindex_entry *ie, const char *abspath,
1657 int dirfd, const char *de_name, struct got_blob_object *blob)
1659 const struct got_error *err = NULL;
1660 char target_path[PATH_MAX];
1661 char etarget[PATH_MAX];
1662 ssize_t elen;
1663 size_t len, target_len = 0;
1664 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1667 *status = GOT_STATUS_NO_CHANGE;
1669 /* Blob object content specifies the target path of the link. */
1670 do {
1671 err = got_object_blob_read_block(&len, blob);
1672 if (err)
1673 return err;
1674 if (len + target_len >= sizeof(target_path)) {
1676 * Should not happen. The blob contents were OK
1677 * when this symlink was installed.
1679 return got_error(GOT_ERR_NO_SPACE);
1681 if (len > 0) {
1682 /* Skip blob object header first time around. */
1683 memcpy(target_path + target_len, buf + hdrlen,
1684 len - hdrlen);
1685 target_len += len - hdrlen;
1686 hdrlen = 0;
1688 } while (len != 0);
1689 target_path[target_len] = '\0';
1691 if (dirfd != -1) {
1692 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1693 if (elen == -1)
1694 return got_error_from_errno2("readlinkat", abspath);
1695 } else {
1696 elen = readlink(abspath, etarget, sizeof(etarget));
1697 if (elen == -1)
1698 return got_error_from_errno2("readlink", abspath);
1701 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 *status = GOT_STATUS_MODIFY;
1704 return NULL;
1707 static const struct got_error *
1708 get_file_status(unsigned char *status, struct stat *sb,
1709 struct got_fileindex_entry *ie, const char *abspath,
1710 int dirfd, const char *de_name, struct got_repository *repo)
1712 const struct got_error *err = NULL;
1713 struct got_object_id id;
1714 size_t hdrlen;
1715 int fd = -1, fd1 = -1;
1716 FILE *f = NULL;
1717 uint8_t fbuf[8192];
1718 struct got_blob_object *blob = NULL;
1719 size_t flen, blen;
1720 unsigned char staged_status;
1722 staged_status = get_staged_status(ie);
1723 *status = GOT_STATUS_NO_CHANGE;
1724 memset(sb, 0, sizeof(*sb));
1727 * Whenever the caller provides a directory descriptor and a
1728 * directory entry name for the file, use them! This prevents
1729 * race conditions if filesystem paths change beneath our feet.
1731 if (dirfd != -1) {
1732 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 if (errno == ENOENT) {
1734 if (got_fileindex_entry_has_file_on_disk(ie))
1735 *status = GOT_STATUS_MISSING;
1736 else
1737 *status = GOT_STATUS_DELETE;
1738 goto done;
1740 err = got_error_from_errno2("fstatat", abspath);
1741 goto done;
1743 } else {
1744 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 if (fd == -1 && errno != ENOENT &&
1746 !got_err_open_nofollow_on_symlink())
1747 return got_error_from_errno2("open", abspath);
1748 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 if (lstat(abspath, sb) == -1)
1750 return got_error_from_errno2("lstat", abspath);
1751 } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 if (errno == ENOENT) {
1753 if (got_fileindex_entry_has_file_on_disk(ie))
1754 *status = GOT_STATUS_MISSING;
1755 else
1756 *status = GOT_STATUS_DELETE;
1757 goto done;
1759 err = got_error_from_errno2("fstat", abspath);
1760 goto done;
1764 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 *status = GOT_STATUS_OBSTRUCTED;
1766 goto done;
1769 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 *status = GOT_STATUS_DELETE;
1771 goto done;
1772 } else if (!got_fileindex_entry_has_blob(ie) &&
1773 staged_status != GOT_STATUS_ADD) {
1774 *status = GOT_STATUS_ADD;
1775 goto done;
1778 if (!stat_info_differs(ie, sb))
1779 goto done;
1781 if (S_ISLNK(sb->st_mode) &&
1782 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 *status = GOT_STATUS_MODIFY;
1784 goto done;
1787 if (staged_status == GOT_STATUS_MODIFY ||
1788 staged_status == GOT_STATUS_ADD)
1789 got_fileindex_entry_get_staged_blob_id(&id, ie);
1790 else
1791 got_fileindex_entry_get_blob_id(&id, ie);
1793 fd1 = got_opentempfd();
1794 if (fd1 == -1) {
1795 err = got_error_from_errno("got_opentempfd");
1796 goto done;
1798 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1799 if (err)
1800 goto done;
1802 if (S_ISLNK(sb->st_mode)) {
1803 err = get_symlink_modification_status(status, ie,
1804 abspath, dirfd, de_name, blob);
1805 goto done;
1808 if (dirfd != -1) {
1809 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1810 if (fd == -1) {
1811 err = got_error_from_errno2("openat", abspath);
1812 goto done;
1816 f = fdopen(fd, "r");
1817 if (f == NULL) {
1818 err = got_error_from_errno2("fdopen", abspath);
1819 goto done;
1821 fd = -1;
1822 hdrlen = got_object_blob_get_hdrlen(blob);
1823 for (;;) {
1824 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 err = got_object_blob_read_block(&blen, blob);
1826 if (err)
1827 goto done;
1828 /* Skip length of blob object header first time around. */
1829 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 if (flen == 0 && ferror(f)) {
1831 err = got_error_from_errno("fread");
1832 goto done;
1834 if (blen - hdrlen == 0) {
1835 if (flen != 0)
1836 *status = GOT_STATUS_MODIFY;
1837 break;
1838 } else if (flen == 0) {
1839 if (blen - hdrlen != 0)
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1842 } else if (blen - hdrlen == flen) {
1843 /* Skip blob object header first time around. */
1844 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 *status = GOT_STATUS_MODIFY;
1846 break;
1848 } else {
1849 *status = GOT_STATUS_MODIFY;
1850 break;
1852 hdrlen = 0;
1855 if (*status == GOT_STATUS_MODIFY) {
1856 rewind(f);
1857 err = get_modified_file_content_status(status, blob, ie->path,
1858 sb, f);
1859 } else if (xbit_differs(ie, sb->st_mode))
1860 *status = GOT_STATUS_MODE_CHANGE;
1861 done:
1862 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 err = got_error_from_errno("close");
1864 if (blob)
1865 got_object_blob_close(blob);
1866 if (f != NULL && fclose(f) == EOF && err == NULL)
1867 err = got_error_from_errno2("fclose", abspath);
1868 if (fd != -1 && close(fd) == -1 && err == NULL)
1869 err = got_error_from_errno2("close", abspath);
1870 return err;
1874 * Update timestamps in the file index if a file is unmodified and
1875 * we had to run a full content comparison to find out.
1877 static const struct got_error *
1878 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 struct got_fileindex_entry *ie, struct stat *sb)
1881 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 return got_fileindex_entry_update(ie, wt_fd, path,
1883 ie->blob_sha1, ie->commit_sha1, 1);
1885 return NULL;
1888 static const struct got_error *
1889 update_blob(struct got_worktree *worktree,
1890 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1891 struct got_tree_entry *te, const char *path,
1892 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1893 void *progress_arg)
1895 const struct got_error *err = NULL;
1896 struct got_blob_object *blob = NULL;
1897 char *ondisk_path = NULL;
1898 unsigned char status = GOT_STATUS_NO_CHANGE;
1899 struct stat sb;
1900 int fd1 = -1, fd2 = -1;
1902 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 return got_error_from_errno("asprintf");
1905 if (ie) {
1906 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1908 goto done;
1910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1911 repo);
1912 if (err)
1913 goto done;
1914 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 sb.st_mode = got_fileindex_perms_to_st(ie);
1916 } else {
1917 if (stat(ondisk_path, &sb) == -1) {
1918 if (errno != ENOENT) {
1919 err = got_error_from_errno2("stat",
1920 ondisk_path);
1921 goto done;
1923 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1924 status = GOT_STATUS_UNVERSIONED;
1925 } else {
1926 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1927 status = GOT_STATUS_UNVERSIONED;
1928 else
1929 status = GOT_STATUS_OBSTRUCTED;
1933 if (status == GOT_STATUS_OBSTRUCTED) {
1934 if (ie)
1935 got_fileindex_entry_mark_skipped(ie);
1936 err = (*progress_cb)(progress_arg, status, path);
1937 goto done;
1939 if (status == GOT_STATUS_CONFLICT) {
1940 if (ie)
1941 got_fileindex_entry_mark_skipped(ie);
1942 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1943 path);
1944 goto done;
1947 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1948 (S_ISLNK(te->mode) ||
1949 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1951 * This is a regular file or an installed bad symlink.
1952 * If the file index indicates that this file is already
1953 * up-to-date with respect to the repository we can skip
1954 * updating contents of this file.
1956 if (got_fileindex_entry_has_commit(ie) &&
1957 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1958 SHA1_DIGEST_LENGTH) == 0) {
1959 /* Same commit. */
1960 err = sync_timestamps(worktree->root_fd,
1961 path, status, ie, &sb);
1962 if (err)
1963 goto done;
1964 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1965 path);
1966 goto done;
1968 if (got_fileindex_entry_has_blob(ie) &&
1969 memcmp(ie->blob_sha1, te->id.sha1,
1970 SHA1_DIGEST_LENGTH) == 0) {
1971 /* Different commit but the same blob. */
1972 err = sync_timestamps(worktree->root_fd,
1973 path, status, ie, &sb);
1974 if (err)
1975 goto done;
1976 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1977 path);
1978 goto done;
1982 fd1 = got_opentempfd();
1983 if (fd1 == -1) {
1984 err = got_error_from_errno("got_opentempfd");
1985 goto done;
1987 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1988 if (err)
1989 goto done;
1991 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1992 int update_timestamps;
1993 struct got_blob_object *blob2 = NULL;
1994 char *label_orig = NULL;
1995 if (got_fileindex_entry_has_blob(ie)) {
1996 fd2 = got_opentempfd();
1997 if (fd2 == -1) {
1998 err = got_error_from_errno("got_opentempfd");
1999 goto done;
2001 struct got_object_id id2;
2002 got_fileindex_entry_get_blob_id(&id2, ie);
2003 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2004 fd2);
2005 if (err)
2006 goto done;
2008 if (got_fileindex_entry_has_commit(ie)) {
2009 char id_str[SHA1_DIGEST_STRING_LENGTH];
2010 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2011 sizeof(id_str)) == NULL) {
2012 err = got_error_path(id_str,
2013 GOT_ERR_BAD_OBJ_ID_STR);
2014 goto done;
2016 if (asprintf(&label_orig, "%s: commit %s",
2017 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2018 err = got_error_from_errno("asprintf");
2019 goto done;
2022 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2023 char *link_target;
2024 err = got_object_blob_read_to_str(&link_target, blob);
2025 if (err)
2026 goto done;
2027 err = merge_symlink(worktree, blob2, ondisk_path, path,
2028 label_orig, link_target, worktree->base_commit_id,
2029 repo, progress_cb, progress_arg);
2030 free(link_target);
2031 } else {
2032 err = merge_blob(&update_timestamps, worktree, blob2,
2033 ondisk_path, path, sb.st_mode, label_orig, blob,
2034 worktree->base_commit_id, repo,
2035 progress_cb, progress_arg);
2037 free(label_orig);
2038 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2039 err = got_error_from_errno("close");
2040 goto done;
2042 if (blob2)
2043 got_object_blob_close(blob2);
2044 if (err)
2045 goto done;
2047 * Do not update timestamps of files with local changes.
2048 * Otherwise, a future status walk would treat them as
2049 * unmodified files again.
2051 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2052 blob->id.sha1, worktree->base_commit_id->sha1,
2053 update_timestamps);
2054 } else if (status == GOT_STATUS_MODE_CHANGE) {
2055 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2056 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2057 } else if (status == GOT_STATUS_DELETE) {
2058 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2059 if (err)
2060 goto done;
2061 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2062 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2063 if (err)
2064 goto done;
2065 } else {
2066 int is_bad_symlink = 0;
2067 if (S_ISLNK(te->mode)) {
2068 err = install_symlink(&is_bad_symlink, worktree,
2069 ondisk_path, path, blob,
2070 status == GOT_STATUS_MISSING, 0,
2071 status == GOT_STATUS_UNVERSIONED, 0,
2072 repo, progress_cb, progress_arg);
2073 } else {
2074 err = install_blob(worktree, ondisk_path, path,
2075 te->mode, sb.st_mode, blob,
2076 status == GOT_STATUS_MISSING, 0, 0,
2077 status == GOT_STATUS_UNVERSIONED, repo,
2078 progress_cb, progress_arg);
2080 if (err)
2081 goto done;
2083 if (ie) {
2084 err = got_fileindex_entry_update(ie,
2085 worktree->root_fd, path, blob->id.sha1,
2086 worktree->base_commit_id->sha1, 1);
2087 } else {
2088 err = create_fileindex_entry(&ie, fileindex,
2089 worktree->base_commit_id, worktree->root_fd, path,
2090 &blob->id);
2092 if (err)
2093 goto done;
2095 if (is_bad_symlink) {
2096 got_fileindex_entry_filetype_set(ie,
2097 GOT_FILEIDX_MODE_BAD_SYMLINK);
2101 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2102 err = got_error_from_errno("close");
2103 goto done;
2105 got_object_blob_close(blob);
2106 done:
2107 free(ondisk_path);
2108 return err;
2111 static const struct got_error *
2112 remove_ondisk_file(const char *root_path, const char *path)
2114 const struct got_error *err = NULL;
2115 char *ondisk_path = NULL, *parent = NULL;
2117 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2118 return got_error_from_errno("asprintf");
2120 if (unlink(ondisk_path) == -1) {
2121 if (errno != ENOENT)
2122 err = got_error_from_errno2("unlink", ondisk_path);
2123 } else {
2124 size_t root_len = strlen(root_path);
2125 err = got_path_dirname(&parent, ondisk_path);
2126 if (err)
2127 goto done;
2128 while (got_path_cmp(parent, root_path,
2129 strlen(parent), root_len) != 0) {
2130 free(ondisk_path);
2131 ondisk_path = parent;
2132 parent = NULL;
2133 if (rmdir(ondisk_path) == -1) {
2134 if (errno != ENOTEMPTY)
2135 err = got_error_from_errno2("rmdir",
2136 ondisk_path);
2137 break;
2139 err = got_path_dirname(&parent, ondisk_path);
2140 if (err)
2141 break;
2144 done:
2145 free(ondisk_path);
2146 free(parent);
2147 return err;
2150 static const struct got_error *
2151 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2152 struct got_fileindex_entry *ie, struct got_repository *repo,
2153 got_worktree_checkout_cb progress_cb, void *progress_arg)
2155 const struct got_error *err = NULL;
2156 unsigned char status;
2157 struct stat sb;
2158 char *ondisk_path;
2160 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2161 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2163 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2164 == -1)
2165 return got_error_from_errno("asprintf");
2167 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2168 if (err)
2169 goto done;
2171 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2172 char ondisk_target[PATH_MAX];
2173 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2174 sizeof(ondisk_target));
2175 if (ondisk_len == -1) {
2176 err = got_error_from_errno2("readlink", ondisk_path);
2177 goto done;
2179 ondisk_target[ondisk_len] = '\0';
2180 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2181 NULL, NULL, /* XXX pass common ancestor info? */
2182 ondisk_target, ondisk_path);
2183 if (err)
2184 goto done;
2185 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2186 ie->path);
2187 goto done;
2190 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2191 status == GOT_STATUS_ADD) {
2192 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2193 if (err)
2194 goto done;
2196 * Preserve the working file and change the deleted blob's
2197 * entry into a schedule-add entry.
2199 err = got_fileindex_entry_update(ie, worktree->root_fd,
2200 ie->path, NULL, NULL, 0);
2201 } else {
2202 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2203 if (err)
2204 goto done;
2205 if (status == GOT_STATUS_NO_CHANGE) {
2206 err = remove_ondisk_file(worktree->root_path, ie->path);
2207 if (err)
2208 goto done;
2210 got_fileindex_entry_remove(fileindex, ie);
2212 done:
2213 free(ondisk_path);
2214 return err;
2217 struct diff_cb_arg {
2218 struct got_fileindex *fileindex;
2219 struct got_worktree *worktree;
2220 struct got_repository *repo;
2221 got_worktree_checkout_cb progress_cb;
2222 void *progress_arg;
2223 got_cancel_cb cancel_cb;
2224 void *cancel_arg;
2227 static const struct got_error *
2228 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2229 struct got_tree_entry *te, const char *parent_path)
2231 struct diff_cb_arg *a = arg;
2233 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2234 return got_error(GOT_ERR_CANCELLED);
2236 return update_blob(a->worktree, a->fileindex, ie, te,
2237 ie->path, a->repo, a->progress_cb, a->progress_arg);
2240 static const struct got_error *
2241 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2243 struct diff_cb_arg *a = arg;
2245 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2246 return got_error(GOT_ERR_CANCELLED);
2248 return delete_blob(a->worktree, a->fileindex, ie,
2249 a->repo, a->progress_cb, a->progress_arg);
2252 static const struct got_error *
2253 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2255 struct diff_cb_arg *a = arg;
2256 const struct got_error *err;
2257 char *path;
2259 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2260 return got_error(GOT_ERR_CANCELLED);
2262 if (got_object_tree_entry_is_submodule(te))
2263 return NULL;
2265 if (asprintf(&path, "%s%s%s", parent_path,
2266 parent_path[0] ? "/" : "", te->name)
2267 == -1)
2268 return got_error_from_errno("asprintf");
2270 if (S_ISDIR(te->mode))
2271 err = add_dir_on_disk(a->worktree, path);
2272 else
2273 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2274 a->repo, a->progress_cb, a->progress_arg);
2276 free(path);
2277 return err;
2280 const struct got_error *
2281 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2283 uint32_t uuid_status;
2285 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2286 if (uuid_status != uuid_s_ok) {
2287 *uuidstr = NULL;
2288 return got_error_uuid(uuid_status, "uuid_to_string");
2291 return NULL;
2294 static const struct got_error *
2295 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2297 const struct got_error *err = NULL;
2298 char *uuidstr = NULL;
2300 *refname = NULL;
2302 err = got_worktree_get_uuid(&uuidstr, worktree);
2303 if (err)
2304 return err;
2306 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2307 err = got_error_from_errno("asprintf");
2308 *refname = NULL;
2310 free(uuidstr);
2311 return err;
2314 const struct got_error *
2315 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2316 const char *prefix)
2318 return get_ref_name(refname, worktree, prefix);
2321 const struct got_error *
2322 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2334 static const struct got_error *
2335 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2340 static const struct got_error *
2341 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2343 return get_ref_name(refname, worktree,
2344 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2347 static const struct got_error *
2348 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2350 return get_ref_name(refname, worktree,
2351 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2354 static const struct got_error *
2355 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2357 return get_ref_name(refname, worktree,
2358 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2361 static const struct got_error *
2362 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2364 return get_ref_name(refname, worktree,
2365 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2368 static const struct got_error *
2369 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree,
2372 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2375 static const struct got_error *
2376 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 return get_ref_name(refname, worktree,
2379 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2382 const struct got_error *
2383 got_worktree_get_histedit_script_path(char **path,
2384 struct got_worktree *worktree)
2386 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2387 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2388 *path = NULL;
2389 return got_error_from_errno("asprintf");
2391 return NULL;
2394 static const struct got_error *
2395 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2397 return get_ref_name(refname, worktree,
2398 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2401 static const struct got_error *
2402 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 return get_ref_name(refname, worktree,
2405 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2409 * Prevent Git's garbage collector from deleting our base commit by
2410 * setting a reference to our base commit's ID.
2412 static const struct got_error *
2413 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2415 const struct got_error *err = NULL;
2416 struct got_reference *ref = NULL;
2417 char *refname;
2419 err = got_worktree_get_base_ref_name(&refname, worktree);
2420 if (err)
2421 return err;
2423 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2424 if (err)
2425 goto done;
2427 err = got_ref_write(ref, repo);
2428 done:
2429 free(refname);
2430 if (ref)
2431 got_ref_close(ref);
2432 return err;
2435 static const struct got_error *
2436 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2438 const struct got_error *err = NULL;
2440 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2441 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2442 err = got_error_from_errno("asprintf");
2443 *fileindex_path = NULL;
2445 return err;
2449 static const struct got_error *
2450 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2451 struct got_worktree *worktree)
2453 const struct got_error *err = NULL;
2454 FILE *index = NULL;
2456 *fileindex_path = NULL;
2457 *fileindex = got_fileindex_alloc();
2458 if (*fileindex == NULL)
2459 return got_error_from_errno("got_fileindex_alloc");
2461 err = get_fileindex_path(fileindex_path, worktree);
2462 if (err)
2463 goto done;
2465 index = fopen(*fileindex_path, "rbe");
2466 if (index == NULL) {
2467 if (errno != ENOENT)
2468 err = got_error_from_errno2("fopen", *fileindex_path);
2469 } else {
2470 err = got_fileindex_read(*fileindex, index);
2471 if (fclose(index) == EOF && err == NULL)
2472 err = got_error_from_errno("fclose");
2474 done:
2475 if (err) {
2476 free(*fileindex_path);
2477 *fileindex_path = NULL;
2478 got_fileindex_free(*fileindex);
2479 *fileindex = NULL;
2481 return err;
2484 struct bump_base_commit_id_arg {
2485 struct got_object_id *base_commit_id;
2486 const char *path;
2487 size_t path_len;
2488 const char *entry_name;
2489 got_worktree_checkout_cb progress_cb;
2490 void *progress_arg;
2493 static const struct got_error *
2494 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2496 const struct got_error *err;
2497 struct bump_base_commit_id_arg *a = arg;
2499 if (a->entry_name) {
2500 if (strcmp(ie->path, a->path) != 0)
2501 return NULL;
2502 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2503 return NULL;
2505 if (got_fileindex_entry_was_skipped(ie))
2506 return NULL;
2508 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2509 SHA1_DIGEST_LENGTH) == 0)
2510 return NULL;
2512 if (a->progress_cb) {
2513 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2514 ie->path);
2515 if (err)
2516 return err;
2518 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2519 return NULL;
2522 /* Bump base commit ID of all files within an updated part of the work tree. */
2523 static const struct got_error *
2524 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2525 struct got_fileindex *fileindex,
2526 got_worktree_checkout_cb progress_cb, void *progress_arg)
2528 struct bump_base_commit_id_arg bbc_arg;
2530 bbc_arg.base_commit_id = worktree->base_commit_id;
2531 bbc_arg.entry_name = NULL;
2532 bbc_arg.path = "";
2533 bbc_arg.path_len = 0;
2534 bbc_arg.progress_cb = progress_cb;
2535 bbc_arg.progress_arg = progress_arg;
2537 return got_fileindex_for_each_entry_safe(fileindex,
2538 bump_base_commit_id, &bbc_arg);
2541 static const struct got_error *
2542 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2544 const struct got_error *err = NULL;
2545 char *new_fileindex_path = NULL;
2546 FILE *new_index = NULL;
2547 struct timespec timeout;
2549 err = got_opentemp_named(&new_fileindex_path, &new_index,
2550 fileindex_path, "");
2551 if (err)
2552 goto done;
2554 err = got_fileindex_write(fileindex, new_index);
2555 if (err)
2556 goto done;
2558 if (rename(new_fileindex_path, fileindex_path) != 0) {
2559 err = got_error_from_errno3("rename", new_fileindex_path,
2560 fileindex_path);
2561 unlink(new_fileindex_path);
2565 * Sleep for a short amount of time to ensure that files modified after
2566 * this program exits have a different time stamp from the one which
2567 * was recorded in the file index.
2569 timeout.tv_sec = 0;
2570 timeout.tv_nsec = 1;
2571 nanosleep(&timeout, NULL);
2572 done:
2573 if (new_index)
2574 fclose(new_index);
2575 free(new_fileindex_path);
2576 return err;
2579 static const struct got_error *
2580 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2581 struct got_object_id **tree_id, const char *wt_relpath,
2582 struct got_commit_object *base_commit, struct got_worktree *worktree,
2583 struct got_repository *repo)
2585 const struct got_error *err = NULL;
2586 struct got_object_id *id = NULL;
2587 char *in_repo_path = NULL;
2588 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2590 *entry_type = GOT_OBJ_TYPE_ANY;
2591 *tree_relpath = NULL;
2592 *tree_id = NULL;
2594 if (wt_relpath[0] == '\0') {
2595 /* Check out all files within the work tree. */
2596 *entry_type = GOT_OBJ_TYPE_TREE;
2597 *tree_relpath = strdup("");
2598 if (*tree_relpath == NULL) {
2599 err = got_error_from_errno("strdup");
2600 goto done;
2602 err = got_object_id_by_path(tree_id, repo, base_commit,
2603 worktree->path_prefix);
2604 if (err)
2605 goto done;
2606 return NULL;
2609 /* Check out a subset of files in the work tree. */
2611 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2612 is_root_wt ? "" : "/", wt_relpath) == -1) {
2613 err = got_error_from_errno("asprintf");
2614 goto done;
2617 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2618 if (err)
2619 goto done;
2621 free(in_repo_path);
2622 in_repo_path = NULL;
2624 err = got_object_get_type(entry_type, repo, id);
2625 if (err)
2626 goto done;
2628 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2629 /* Check out a single file. */
2630 if (strchr(wt_relpath, '/') == NULL) {
2631 /* Check out a single file in work tree's root dir. */
2632 in_repo_path = strdup(worktree->path_prefix);
2633 if (in_repo_path == NULL) {
2634 err = got_error_from_errno("strdup");
2635 goto done;
2637 *tree_relpath = strdup("");
2638 if (*tree_relpath == NULL) {
2639 err = got_error_from_errno("strdup");
2640 goto done;
2642 } else {
2643 /* Check out a single file in a subdirectory. */
2644 err = got_path_dirname(tree_relpath, wt_relpath);
2645 if (err)
2646 return err;
2647 if (asprintf(&in_repo_path, "%s%s%s",
2648 worktree->path_prefix, is_root_wt ? "" : "/",
2649 *tree_relpath) == -1) {
2650 err = got_error_from_errno("asprintf");
2651 goto done;
2654 err = got_object_id_by_path(tree_id, repo,
2655 base_commit, in_repo_path);
2656 } else {
2657 /* Check out all files within a subdirectory. */
2658 *tree_id = got_object_id_dup(id);
2659 if (*tree_id == NULL) {
2660 err = got_error_from_errno("got_object_id_dup");
2661 goto done;
2663 *tree_relpath = strdup(wt_relpath);
2664 if (*tree_relpath == NULL) {
2665 err = got_error_from_errno("strdup");
2666 goto done;
2669 done:
2670 free(id);
2671 free(in_repo_path);
2672 if (err) {
2673 *entry_type = GOT_OBJ_TYPE_ANY;
2674 free(*tree_relpath);
2675 *tree_relpath = NULL;
2676 free(*tree_id);
2677 *tree_id = NULL;
2679 return err;
2682 static const struct got_error *
2683 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2684 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2685 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2686 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2688 const struct got_error *err = NULL;
2689 struct got_commit_object *commit = NULL;
2690 struct got_tree_object *tree = NULL;
2691 struct got_fileindex_diff_tree_cb diff_cb;
2692 struct diff_cb_arg arg;
2694 err = ref_base_commit(worktree, repo);
2695 if (err) {
2696 if (!(err->code == GOT_ERR_ERRNO &&
2697 (errno == EACCES || errno == EROFS)))
2698 goto done;
2699 err = (*progress_cb)(progress_arg,
2700 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2701 if (err)
2702 return err;
2705 err = got_object_open_as_commit(&commit, repo,
2706 worktree->base_commit_id);
2707 if (err)
2708 goto done;
2710 err = got_object_open_as_tree(&tree, repo, tree_id);
2711 if (err)
2712 goto done;
2714 if (entry_name &&
2715 got_object_tree_find_entry(tree, entry_name) == NULL) {
2716 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2717 goto done;
2720 diff_cb.diff_old_new = diff_old_new;
2721 diff_cb.diff_old = diff_old;
2722 diff_cb.diff_new = diff_new;
2723 arg.fileindex = fileindex;
2724 arg.worktree = worktree;
2725 arg.repo = repo;
2726 arg.progress_cb = progress_cb;
2727 arg.progress_arg = progress_arg;
2728 arg.cancel_cb = cancel_cb;
2729 arg.cancel_arg = cancel_arg;
2730 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2731 entry_name, repo, &diff_cb, &arg);
2732 done:
2733 if (tree)
2734 got_object_tree_close(tree);
2735 if (commit)
2736 got_object_commit_close(commit);
2737 return err;
2740 const struct got_error *
2741 got_worktree_checkout_files(struct got_worktree *worktree,
2742 struct got_pathlist_head *paths, struct got_repository *repo,
2743 got_worktree_checkout_cb progress_cb, void *progress_arg,
2744 got_cancel_cb cancel_cb, void *cancel_arg)
2746 const struct got_error *err = NULL, *sync_err, *unlockerr;
2747 struct got_commit_object *commit = NULL;
2748 struct got_tree_object *tree = NULL;
2749 struct got_fileindex *fileindex = NULL;
2750 char *fileindex_path = NULL;
2751 struct got_pathlist_entry *pe;
2752 struct tree_path_data {
2753 STAILQ_ENTRY(tree_path_data) entry;
2754 struct got_object_id *tree_id;
2755 int entry_type;
2756 char *relpath;
2757 char *entry_name;
2758 } *tpd = NULL;
2759 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2761 STAILQ_INIT(&tree_paths);
2763 err = lock_worktree(worktree, LOCK_EX);
2764 if (err)
2765 return err;
2767 err = got_object_open_as_commit(&commit, repo,
2768 worktree->base_commit_id);
2769 if (err)
2770 goto done;
2772 /* Map all specified paths to in-repository trees. */
2773 TAILQ_FOREACH(pe, paths, entry) {
2774 tpd = malloc(sizeof(*tpd));
2775 if (tpd == NULL) {
2776 err = got_error_from_errno("malloc");
2777 goto done;
2780 err = find_tree_entry_for_checkout(&tpd->entry_type,
2781 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2782 worktree, repo);
2783 if (err) {
2784 free(tpd);
2785 goto done;
2788 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2789 err = got_path_basename(&tpd->entry_name, pe->path);
2790 if (err) {
2791 free(tpd->relpath);
2792 free(tpd->tree_id);
2793 free(tpd);
2794 goto done;
2796 } else
2797 tpd->entry_name = NULL;
2799 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2803 * Read the file index.
2804 * Checking out files is supposed to be an idempotent operation.
2805 * If the on-disk file index is incomplete we will try to complete it.
2807 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2808 if (err)
2809 goto done;
2811 tpd = STAILQ_FIRST(&tree_paths);
2812 TAILQ_FOREACH(pe, paths, entry) {
2813 struct bump_base_commit_id_arg bbc_arg;
2815 err = checkout_files(worktree, fileindex, tpd->relpath,
2816 tpd->tree_id, tpd->entry_name, repo,
2817 progress_cb, progress_arg, cancel_cb, cancel_arg);
2818 if (err)
2819 break;
2821 bbc_arg.base_commit_id = worktree->base_commit_id;
2822 bbc_arg.entry_name = tpd->entry_name;
2823 bbc_arg.path = pe->path;
2824 bbc_arg.path_len = pe->path_len;
2825 bbc_arg.progress_cb = progress_cb;
2826 bbc_arg.progress_arg = progress_arg;
2827 err = got_fileindex_for_each_entry_safe(fileindex,
2828 bump_base_commit_id, &bbc_arg);
2829 if (err)
2830 break;
2832 tpd = STAILQ_NEXT(tpd, entry);
2834 sync_err = sync_fileindex(fileindex, fileindex_path);
2835 if (sync_err && err == NULL)
2836 err = sync_err;
2837 done:
2838 free(fileindex_path);
2839 if (tree)
2840 got_object_tree_close(tree);
2841 if (commit)
2842 got_object_commit_close(commit);
2843 if (fileindex)
2844 got_fileindex_free(fileindex);
2845 while (!STAILQ_EMPTY(&tree_paths)) {
2846 tpd = STAILQ_FIRST(&tree_paths);
2847 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2848 free(tpd->relpath);
2849 free(tpd->tree_id);
2850 free(tpd);
2852 unlockerr = lock_worktree(worktree, LOCK_SH);
2853 if (unlockerr && err == NULL)
2854 err = unlockerr;
2855 return err;
2858 struct merge_file_cb_arg {
2859 struct got_worktree *worktree;
2860 struct got_fileindex *fileindex;
2861 got_worktree_checkout_cb progress_cb;
2862 void *progress_arg;
2863 got_cancel_cb cancel_cb;
2864 void *cancel_arg;
2865 const char *label_orig;
2866 struct got_object_id *commit_id2;
2867 int allow_bad_symlinks;
2870 static const struct got_error *
2871 merge_file_cb(void *arg, struct got_blob_object *blob1,
2872 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2873 struct got_object_id *id1, struct got_object_id *id2,
2874 const char *path1, const char *path2,
2875 mode_t mode1, mode_t mode2, struct got_repository *repo)
2877 static const struct got_error *err = NULL;
2878 struct merge_file_cb_arg *a = arg;
2879 struct got_fileindex_entry *ie;
2880 char *ondisk_path = NULL;
2881 struct stat sb;
2882 unsigned char status;
2883 int local_changes_subsumed;
2884 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2885 char *id_str = NULL, *label_deriv2 = NULL;
2887 if (blob1 && blob2) {
2888 ie = got_fileindex_entry_get(a->fileindex, path2,
2889 strlen(path2));
2890 if (ie == NULL)
2891 return (*a->progress_cb)(a->progress_arg,
2892 GOT_STATUS_MISSING, path2);
2894 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2895 path2) == -1)
2896 return got_error_from_errno("asprintf");
2898 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2899 repo);
2900 if (err)
2901 goto done;
2903 if (status == GOT_STATUS_DELETE) {
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_MERGE, path2);
2906 goto done;
2908 if (status != GOT_STATUS_NO_CHANGE &&
2909 status != GOT_STATUS_MODIFY &&
2910 status != GOT_STATUS_CONFLICT &&
2911 status != GOT_STATUS_ADD) {
2912 err = (*a->progress_cb)(a->progress_arg, status, path2);
2913 goto done;
2916 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2917 char *link_target2;
2918 err = got_object_blob_read_to_str(&link_target2, blob2);
2919 if (err)
2920 goto done;
2921 err = merge_symlink(a->worktree, blob1, ondisk_path,
2922 path2, a->label_orig, link_target2, a->commit_id2,
2923 repo, a->progress_cb, a->progress_arg);
2924 free(link_target2);
2925 } else {
2926 int fd;
2928 f_orig = got_opentemp();
2929 if (f_orig == NULL) {
2930 err = got_error_from_errno("got_opentemp");
2931 goto done;
2933 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2934 f_orig, blob1);
2935 if (err)
2936 goto done;
2938 f_deriv2 = got_opentemp();
2939 if (f_deriv2 == NULL)
2940 goto done;
2941 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2942 f_deriv2, blob2);
2943 if (err)
2944 goto done;
2946 fd = open(ondisk_path,
2947 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2948 if (fd == -1) {
2949 err = got_error_from_errno2("open",
2950 ondisk_path);
2951 goto done;
2953 f_deriv = fdopen(fd, "r");
2954 if (f_deriv == NULL) {
2955 err = got_error_from_errno2("fdopen",
2956 ondisk_path);
2957 close(fd);
2958 goto done;
2960 err = got_object_id_str(&id_str, a->commit_id2);
2961 if (err)
2962 goto done;
2963 if (asprintf(&label_deriv2, "%s: commit %s",
2964 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2965 err = got_error_from_errno("asprintf");
2966 goto done;
2968 err = merge_file(&local_changes_subsumed, a->worktree,
2969 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2970 mode2, a->label_orig, NULL, label_deriv2,
2971 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2972 a->progress_cb, a->progress_arg);
2974 } else if (blob1) {
2975 ie = got_fileindex_entry_get(a->fileindex, path1,
2976 strlen(path1));
2977 if (ie == NULL)
2978 return (*a->progress_cb)(a->progress_arg,
2979 GOT_STATUS_MISSING, path1);
2981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2982 path1) == -1)
2983 return got_error_from_errno("asprintf");
2985 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2986 repo);
2987 if (err)
2988 goto done;
2990 switch (status) {
2991 case GOT_STATUS_NO_CHANGE:
2992 err = (*a->progress_cb)(a->progress_arg,
2993 GOT_STATUS_DELETE, path1);
2994 if (err)
2995 goto done;
2996 err = remove_ondisk_file(a->worktree->root_path, path1);
2997 if (err)
2998 goto done;
2999 if (ie)
3000 got_fileindex_entry_mark_deleted_from_disk(ie);
3001 break;
3002 case GOT_STATUS_DELETE:
3003 case GOT_STATUS_MISSING:
3004 err = (*a->progress_cb)(a->progress_arg,
3005 GOT_STATUS_DELETE, path1);
3006 if (err)
3007 goto done;
3008 if (ie)
3009 got_fileindex_entry_mark_deleted_from_disk(ie);
3010 break;
3011 case GOT_STATUS_ADD: {
3012 struct got_object_id *id;
3013 FILE *blob1_f;
3014 off_t blob1_size;
3016 * Delete the added file only if its content already
3017 * exists in the repository.
3019 err = got_object_blob_file_create(&id, &blob1_f,
3020 &blob1_size, path1);
3021 if (err)
3022 goto done;
3023 if (got_object_id_cmp(id, id1) == 0) {
3024 err = (*a->progress_cb)(a->progress_arg,
3025 GOT_STATUS_DELETE, path1);
3026 if (err)
3027 goto done;
3028 err = remove_ondisk_file(a->worktree->root_path,
3029 path1);
3030 if (err)
3031 goto done;
3032 if (ie)
3033 got_fileindex_entry_remove(a->fileindex,
3034 ie);
3035 } else {
3036 err = (*a->progress_cb)(a->progress_arg,
3037 GOT_STATUS_CANNOT_DELETE, path1);
3039 if (fclose(blob1_f) == EOF && err == NULL)
3040 err = got_error_from_errno("fclose");
3041 free(id);
3042 if (err)
3043 goto done;
3044 break;
3046 case GOT_STATUS_MODIFY:
3047 case GOT_STATUS_CONFLICT:
3048 err = (*a->progress_cb)(a->progress_arg,
3049 GOT_STATUS_CANNOT_DELETE, path1);
3050 if (err)
3051 goto done;
3052 break;
3053 case GOT_STATUS_OBSTRUCTED:
3054 err = (*a->progress_cb)(a->progress_arg, status, path1);
3055 if (err)
3056 goto done;
3057 break;
3058 default:
3059 break;
3061 } else if (blob2) {
3062 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3063 path2) == -1)
3064 return got_error_from_errno("asprintf");
3065 ie = got_fileindex_entry_get(a->fileindex, path2,
3066 strlen(path2));
3067 if (ie) {
3068 err = get_file_status(&status, &sb, ie, ondisk_path,
3069 -1, NULL, repo);
3070 if (err)
3071 goto done;
3072 if (status != GOT_STATUS_NO_CHANGE &&
3073 status != GOT_STATUS_MODIFY &&
3074 status != GOT_STATUS_CONFLICT &&
3075 status != GOT_STATUS_ADD) {
3076 err = (*a->progress_cb)(a->progress_arg,
3077 status, path2);
3078 goto done;
3080 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3081 char *link_target2;
3082 err = got_object_blob_read_to_str(&link_target2,
3083 blob2);
3084 if (err)
3085 goto done;
3086 err = merge_symlink(a->worktree, NULL,
3087 ondisk_path, path2, a->label_orig,
3088 link_target2, a->commit_id2, repo,
3089 a->progress_cb, a->progress_arg);
3090 free(link_target2);
3091 } else if (S_ISREG(sb.st_mode)) {
3092 err = merge_blob(&local_changes_subsumed,
3093 a->worktree, NULL, ondisk_path, path2,
3094 sb.st_mode, a->label_orig, blob2,
3095 a->commit_id2, repo, a->progress_cb,
3096 a->progress_arg);
3097 } else {
3098 err = got_error_path(ondisk_path,
3099 GOT_ERR_FILE_OBSTRUCTED);
3101 if (err)
3102 goto done;
3103 if (status == GOT_STATUS_DELETE) {
3104 err = got_fileindex_entry_update(ie,
3105 a->worktree->root_fd, path2, blob2->id.sha1,
3106 a->worktree->base_commit_id->sha1, 0);
3107 if (err)
3108 goto done;
3110 } else {
3111 int is_bad_symlink = 0;
3112 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3113 if (S_ISLNK(mode2)) {
3114 err = install_symlink(&is_bad_symlink,
3115 a->worktree, ondisk_path, path2, blob2, 0,
3116 0, 1, a->allow_bad_symlinks, repo,
3117 a->progress_cb, a->progress_arg);
3118 } else {
3119 err = install_blob(a->worktree, ondisk_path, path2,
3120 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3121 a->progress_cb, a->progress_arg);
3123 if (err)
3124 goto done;
3125 err = got_fileindex_entry_alloc(&ie, path2);
3126 if (err)
3127 goto done;
3128 err = got_fileindex_entry_update(ie,
3129 a->worktree->root_fd, path2, NULL, NULL, 1);
3130 if (err) {
3131 got_fileindex_entry_free(ie);
3132 goto done;
3134 err = got_fileindex_entry_add(a->fileindex, ie);
3135 if (err) {
3136 got_fileindex_entry_free(ie);
3137 goto done;
3139 if (is_bad_symlink) {
3140 got_fileindex_entry_filetype_set(ie,
3141 GOT_FILEIDX_MODE_BAD_SYMLINK);
3145 done:
3146 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3147 err = got_error_from_errno("fclose");
3148 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3149 err = got_error_from_errno("fclose");
3150 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3151 err = got_error_from_errno("fclose");
3152 free(id_str);
3153 free(label_deriv2);
3154 free(ondisk_path);
3155 return err;
3158 static const struct got_error *
3159 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3161 struct got_worktree *worktree = arg;
3163 /* Reject merges into a work tree with mixed base commits. */
3164 if (got_fileindex_entry_has_commit(ie) &&
3165 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3166 SHA1_DIGEST_LENGTH) != 0)
3167 return got_error(GOT_ERR_MIXED_COMMITS);
3169 return NULL;
3172 struct check_merge_conflicts_arg {
3173 struct got_worktree *worktree;
3174 struct got_fileindex *fileindex;
3175 struct got_repository *repo;
3178 static const struct got_error *
3179 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3180 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3181 struct got_object_id *id1, struct got_object_id *id2,
3182 const char *path1, const char *path2,
3183 mode_t mode1, mode_t mode2, struct got_repository *repo)
3185 const struct got_error *err = NULL;
3186 struct check_merge_conflicts_arg *a = arg;
3187 unsigned char status;
3188 struct stat sb;
3189 struct got_fileindex_entry *ie;
3190 const char *path = path2 ? path2 : path1;
3191 struct got_object_id *id = id2 ? id2 : id1;
3192 char *ondisk_path;
3194 if (id == NULL)
3195 return NULL;
3197 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3198 if (ie == NULL)
3199 return NULL;
3201 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3202 == -1)
3203 return got_error_from_errno("asprintf");
3205 /* Reject merges into a work tree with conflicted files. */
3206 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3207 free(ondisk_path);
3208 if (err)
3209 return err;
3210 if (status == GOT_STATUS_CONFLICT)
3211 return got_error(GOT_ERR_CONFLICTS);
3213 return NULL;
3216 static const struct got_error *
3217 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3218 const char *fileindex_path, struct got_object_id *commit_id1,
3219 struct got_object_id *commit_id2, struct got_repository *repo,
3220 got_worktree_checkout_cb progress_cb, void *progress_arg,
3221 got_cancel_cb cancel_cb, void *cancel_arg)
3223 const struct got_error *err = NULL, *sync_err;
3224 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3225 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3226 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3227 struct check_merge_conflicts_arg cmc_arg;
3228 struct merge_file_cb_arg arg;
3229 char *label_orig = NULL;
3230 FILE *f1 = NULL, *f2 = NULL;
3231 int fd1 = -1, fd2 = -1;
3233 if (commit_id1) {
3234 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3235 if (err)
3236 goto done;
3237 err = got_object_id_by_path(&tree_id1, repo, commit1,
3238 worktree->path_prefix);
3239 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3240 goto done;
3242 if (tree_id1) {
3243 char *id_str;
3245 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3246 if (err)
3247 goto done;
3249 err = got_object_id_str(&id_str, commit_id1);
3250 if (err)
3251 goto done;
3253 if (asprintf(&label_orig, "%s: commit %s",
3254 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3255 err = got_error_from_errno("asprintf");
3256 free(id_str);
3257 goto done;
3259 free(id_str);
3261 f1 = got_opentemp();
3262 if (f1 == NULL) {
3263 err = got_error_from_errno("got_opentemp");
3264 goto done;
3268 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3269 if (err)
3270 goto done;
3272 err = got_object_id_by_path(&tree_id2, repo, commit2,
3273 worktree->path_prefix);
3274 if (err)
3275 goto done;
3277 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3278 if (err)
3279 goto done;
3281 f2 = got_opentemp();
3282 if (f2 == NULL) {
3283 err = got_error_from_errno("got_opentemp");
3284 goto done;
3287 fd1 = got_opentempfd();
3288 if (fd1 == -1) {
3289 err = got_error_from_errno("got_opentempfd");
3290 goto done;
3293 fd2 = got_opentempfd();
3294 if (fd2 == -1) {
3295 err = got_error_from_errno("got_opentempfd");
3296 goto done;
3299 cmc_arg.worktree = worktree;
3300 cmc_arg.fileindex = fileindex;
3301 cmc_arg.repo = repo;
3302 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3303 check_merge_conflicts, &cmc_arg, 0);
3304 if (err)
3305 goto done;
3307 arg.worktree = worktree;
3308 arg.fileindex = fileindex;
3309 arg.progress_cb = progress_cb;
3310 arg.progress_arg = progress_arg;
3311 arg.cancel_cb = cancel_cb;
3312 arg.cancel_arg = cancel_arg;
3313 arg.label_orig = label_orig;
3314 arg.commit_id2 = commit_id2;
3315 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3316 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3317 merge_file_cb, &arg, 1);
3318 sync_err = sync_fileindex(fileindex, fileindex_path);
3319 if (sync_err && err == NULL)
3320 err = sync_err;
3321 done:
3322 if (commit1)
3323 got_object_commit_close(commit1);
3324 if (commit2)
3325 got_object_commit_close(commit2);
3326 if (tree1)
3327 got_object_tree_close(tree1);
3328 if (tree2)
3329 got_object_tree_close(tree2);
3330 if (f1 && fclose(f1) == EOF && err == NULL)
3331 err = got_error_from_errno("fclose");
3332 if (f2 && fclose(f2) == EOF && err == NULL)
3333 err = got_error_from_errno("fclose");
3334 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3335 err = got_error_from_errno("close");
3336 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3337 err = got_error_from_errno("close");
3338 free(label_orig);
3339 return err;
3342 const struct got_error *
3343 got_worktree_merge_files(struct got_worktree *worktree,
3344 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3345 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3346 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3348 const struct got_error *err, *unlockerr;
3349 char *fileindex_path = NULL;
3350 struct got_fileindex *fileindex = NULL;
3352 err = lock_worktree(worktree, LOCK_EX);
3353 if (err)
3354 return err;
3356 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3357 if (err)
3358 goto done;
3360 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3361 worktree);
3362 if (err)
3363 goto done;
3365 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3366 commit_id2, repo, progress_cb, progress_arg,
3367 cancel_cb, cancel_arg);
3368 done:
3369 if (fileindex)
3370 got_fileindex_free(fileindex);
3371 free(fileindex_path);
3372 unlockerr = lock_worktree(worktree, LOCK_SH);
3373 if (unlockerr && err == NULL)
3374 err = unlockerr;
3375 return err;
3378 struct diff_dir_cb_arg {
3379 struct got_fileindex *fileindex;
3380 struct got_worktree *worktree;
3381 const char *status_path;
3382 size_t status_path_len;
3383 struct got_repository *repo;
3384 got_worktree_status_cb status_cb;
3385 void *status_arg;
3386 got_cancel_cb cancel_cb;
3387 void *cancel_arg;
3388 /* A pathlist containing per-directory pathlists of ignore patterns. */
3389 struct got_pathlist_head *ignores;
3390 int report_unchanged;
3391 int no_ignores;
3394 static const struct got_error *
3395 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3396 int dirfd, const char *de_name,
3397 got_worktree_status_cb status_cb, void *status_arg,
3398 struct got_repository *repo, int report_unchanged)
3400 const struct got_error *err = NULL;
3401 unsigned char status = GOT_STATUS_NO_CHANGE;
3402 unsigned char staged_status;
3403 struct stat sb;
3404 struct got_object_id blob_id, commit_id, staged_blob_id;
3405 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3406 struct got_object_id *staged_blob_idp = NULL;
3408 staged_status = get_staged_status(ie);
3409 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3410 if (err)
3411 return err;
3413 if (status == GOT_STATUS_NO_CHANGE &&
3414 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3415 return NULL;
3417 if (got_fileindex_entry_has_blob(ie))
3418 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3419 if (got_fileindex_entry_has_commit(ie))
3420 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3421 if (staged_status == GOT_STATUS_ADD ||
3422 staged_status == GOT_STATUS_MODIFY) {
3423 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3424 &staged_blob_id, ie);
3427 return (*status_cb)(status_arg, status, staged_status,
3428 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3431 static const struct got_error *
3432 status_old_new(void *arg, struct got_fileindex_entry *ie,
3433 struct dirent *de, const char *parent_path, int dirfd)
3435 const struct got_error *err = NULL;
3436 struct diff_dir_cb_arg *a = arg;
3437 char *abspath;
3439 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3440 return got_error(GOT_ERR_CANCELLED);
3442 if (got_path_cmp(parent_path, a->status_path,
3443 strlen(parent_path), a->status_path_len) != 0 &&
3444 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3445 return NULL;
3447 if (parent_path[0]) {
3448 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3449 parent_path, de->d_name) == -1)
3450 return got_error_from_errno("asprintf");
3451 } else {
3452 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3453 de->d_name) == -1)
3454 return got_error_from_errno("asprintf");
3457 err = report_file_status(ie, abspath, dirfd, de->d_name,
3458 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3459 free(abspath);
3460 return err;
3463 static const struct got_error *
3464 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3466 struct diff_dir_cb_arg *a = arg;
3467 struct got_object_id blob_id, commit_id;
3468 unsigned char status;
3470 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3471 return got_error(GOT_ERR_CANCELLED);
3473 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3474 return NULL;
3476 got_fileindex_entry_get_blob_id(&blob_id, ie);
3477 got_fileindex_entry_get_commit_id(&commit_id, ie);
3478 if (got_fileindex_entry_has_file_on_disk(ie))
3479 status = GOT_STATUS_MISSING;
3480 else
3481 status = GOT_STATUS_DELETE;
3482 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3483 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3486 static void
3487 free_ignores(struct got_pathlist_head *ignores)
3489 struct got_pathlist_entry *pe;
3491 TAILQ_FOREACH(pe, ignores, entry) {
3492 struct got_pathlist_head *ignorelist = pe->data;
3494 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3496 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3499 static const struct got_error *
3500 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3502 const struct got_error *err = NULL;
3503 struct got_pathlist_entry *pe = NULL;
3504 struct got_pathlist_head *ignorelist;
3505 char *line = NULL, *pattern, *dirpath = NULL;
3506 size_t linesize = 0;
3507 ssize_t linelen;
3509 ignorelist = calloc(1, sizeof(*ignorelist));
3510 if (ignorelist == NULL)
3511 return got_error_from_errno("calloc");
3512 TAILQ_INIT(ignorelist);
3514 while ((linelen = getline(&line, &linesize, f)) != -1) {
3515 if (linelen > 0 && line[linelen - 1] == '\n')
3516 line[linelen - 1] = '\0';
3518 /* Git's ignores may contain comments. */
3519 if (line[0] == '#')
3520 continue;
3522 /* Git's negated patterns are not (yet?) supported. */
3523 if (line[0] == '!')
3524 continue;
3526 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3527 line) == -1) {
3528 err = got_error_from_errno("asprintf");
3529 goto done;
3531 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3532 if (err)
3533 goto done;
3535 if (ferror(f)) {
3536 err = got_error_from_errno("getline");
3537 goto done;
3540 dirpath = strdup(path);
3541 if (dirpath == NULL) {
3542 err = got_error_from_errno("strdup");
3543 goto done;
3545 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3546 done:
3547 free(line);
3548 if (err || pe == NULL) {
3549 free(dirpath);
3550 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3552 return err;
3555 static int
3556 match_ignores(struct got_pathlist_head *ignores, const char *path)
3558 struct got_pathlist_entry *pe;
3560 /* Handle patterns which match in all directories. */
3561 TAILQ_FOREACH(pe, ignores, entry) {
3562 struct got_pathlist_head *ignorelist = pe->data;
3563 struct got_pathlist_entry *pi;
3565 TAILQ_FOREACH(pi, ignorelist, entry) {
3566 const char *p, *pattern = pi->path;
3568 if (strncmp(pattern, "**/", 3) != 0)
3569 continue;
3570 pattern += 3;
3571 p = path;
3572 while (*p) {
3573 if (fnmatch(pattern, p,
3574 FNM_PATHNAME | FNM_LEADING_DIR)) {
3575 /* Retry in next directory. */
3576 while (*p && *p != '/')
3577 p++;
3578 while (*p == '/')
3579 p++;
3580 continue;
3582 return 1;
3588 * The ignores pathlist contains ignore lists from children before
3589 * parents, so we can find the most specific ignorelist by walking
3590 * ignores backwards.
3592 pe = TAILQ_LAST(ignores, got_pathlist_head);
3593 while (pe) {
3594 if (got_path_is_child(path, pe->path, pe->path_len)) {
3595 struct got_pathlist_head *ignorelist = pe->data;
3596 struct got_pathlist_entry *pi;
3597 TAILQ_FOREACH(pi, ignorelist, entry) {
3598 const char *pattern = pi->path;
3599 int flags = FNM_LEADING_DIR;
3600 if (strstr(pattern, "/**/") == NULL)
3601 flags |= FNM_PATHNAME;
3602 if (fnmatch(pattern, path, flags))
3603 continue;
3604 return 1;
3607 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3610 return 0;
3613 static const struct got_error *
3614 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3615 const char *path, int dirfd, const char *ignores_filename)
3617 const struct got_error *err = NULL;
3618 char *ignorespath;
3619 int fd = -1;
3620 FILE *ignoresfile = NULL;
3622 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3623 path[0] ? "/" : "", ignores_filename) == -1)
3624 return got_error_from_errno("asprintf");
3626 if (dirfd != -1) {
3627 fd = openat(dirfd, ignores_filename,
3628 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3629 if (fd == -1) {
3630 if (errno != ENOENT && errno != EACCES)
3631 err = got_error_from_errno2("openat",
3632 ignorespath);
3633 } else {
3634 ignoresfile = fdopen(fd, "r");
3635 if (ignoresfile == NULL)
3636 err = got_error_from_errno2("fdopen",
3637 ignorespath);
3638 else {
3639 fd = -1;
3640 err = read_ignores(ignores, path, ignoresfile);
3643 } else {
3644 ignoresfile = fopen(ignorespath, "re");
3645 if (ignoresfile == NULL) {
3646 if (errno != ENOENT && errno != EACCES)
3647 err = got_error_from_errno2("fopen",
3648 ignorespath);
3649 } else
3650 err = read_ignores(ignores, path, ignoresfile);
3653 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3654 err = got_error_from_errno2("fclose", path);
3655 if (fd != -1 && close(fd) == -1 && err == NULL)
3656 err = got_error_from_errno2("close", path);
3657 free(ignorespath);
3658 return err;
3661 static const struct got_error *
3662 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3663 int dirfd)
3665 const struct got_error *err = NULL;
3666 struct diff_dir_cb_arg *a = arg;
3667 char *path = NULL;
3669 if (ignore != NULL)
3670 *ignore = 0;
3672 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3673 return got_error(GOT_ERR_CANCELLED);
3675 if (parent_path[0]) {
3676 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3677 return got_error_from_errno("asprintf");
3678 } else {
3679 path = de->d_name;
3682 if (de->d_type == DT_DIR) {
3683 if (!a->no_ignores && ignore != NULL &&
3684 match_ignores(a->ignores, path))
3685 *ignore = 1;
3686 } else if (!match_ignores(a->ignores, path) &&
3687 got_path_is_child(path, a->status_path, a->status_path_len))
3688 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3689 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3690 if (parent_path[0])
3691 free(path);
3692 return err;
3695 static const struct got_error *
3696 status_traverse(void *arg, const char *path, int dirfd)
3698 const struct got_error *err = NULL;
3699 struct diff_dir_cb_arg *a = arg;
3701 if (a->no_ignores)
3702 return NULL;
3704 err = add_ignores(a->ignores, a->worktree->root_path,
3705 path, dirfd, ".cvsignore");
3706 if (err)
3707 return err;
3709 err = add_ignores(a->ignores, a->worktree->root_path, path,
3710 dirfd, ".gitignore");
3712 return err;
3715 static const struct got_error *
3716 report_single_file_status(const char *path, const char *ondisk_path,
3717 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3718 void *status_arg, struct got_repository *repo, int report_unchanged,
3719 struct got_pathlist_head *ignores, int no_ignores)
3721 struct got_fileindex_entry *ie;
3722 struct stat sb;
3724 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3725 if (ie)
3726 return report_file_status(ie, ondisk_path, -1, NULL,
3727 status_cb, status_arg, repo, report_unchanged);
3729 if (lstat(ondisk_path, &sb) == -1) {
3730 if (errno != ENOENT)
3731 return got_error_from_errno2("lstat", ondisk_path);
3732 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3733 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3736 if (!no_ignores && match_ignores(ignores, path))
3737 return NULL;
3739 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3740 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3741 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3743 return NULL;
3746 static const struct got_error *
3747 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3748 const char *root_path, const char *path)
3750 const struct got_error *err;
3751 char *parent_path, *next_parent_path = NULL;
3753 err = add_ignores(ignores, root_path, "", -1,
3754 ".cvsignore");
3755 if (err)
3756 return err;
3758 err = add_ignores(ignores, root_path, "", -1,
3759 ".gitignore");
3760 if (err)
3761 return err;
3763 err = got_path_dirname(&parent_path, path);
3764 if (err) {
3765 if (err->code == GOT_ERR_BAD_PATH)
3766 return NULL; /* cannot traverse parent */
3767 return err;
3769 for (;;) {
3770 err = add_ignores(ignores, root_path, parent_path, -1,
3771 ".cvsignore");
3772 if (err)
3773 break;
3774 err = add_ignores(ignores, root_path, parent_path, -1,
3775 ".gitignore");
3776 if (err)
3777 break;
3778 err = got_path_dirname(&next_parent_path, parent_path);
3779 if (err) {
3780 if (err->code == GOT_ERR_BAD_PATH)
3781 err = NULL; /* traversed everything */
3782 break;
3784 if (got_path_is_root_dir(parent_path))
3785 break;
3786 free(parent_path);
3787 parent_path = next_parent_path;
3788 next_parent_path = NULL;
3791 free(parent_path);
3792 free(next_parent_path);
3793 return err;
3796 static const struct got_error *
3797 worktree_status(struct got_worktree *worktree, const char *path,
3798 struct got_fileindex *fileindex, struct got_repository *repo,
3799 got_worktree_status_cb status_cb, void *status_arg,
3800 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3801 int report_unchanged)
3803 const struct got_error *err = NULL;
3804 int fd = -1;
3805 struct got_fileindex_diff_dir_cb fdiff_cb;
3806 struct diff_dir_cb_arg arg;
3807 char *ondisk_path = NULL;
3808 struct got_pathlist_head ignores;
3809 struct got_fileindex_entry *ie;
3811 TAILQ_INIT(&ignores);
3813 if (asprintf(&ondisk_path, "%s%s%s",
3814 worktree->root_path, path[0] ? "/" : "", path) == -1)
3815 return got_error_from_errno("asprintf");
3817 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3818 if (ie) {
3819 err = report_single_file_status(path, ondisk_path,
3820 fileindex, status_cb, status_arg, repo,
3821 report_unchanged, &ignores, no_ignores);
3822 goto done;
3825 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3826 if (fd == -1) {
3827 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3828 !got_err_open_nofollow_on_symlink())
3829 err = got_error_from_errno2("open", ondisk_path);
3830 else {
3831 if (!no_ignores) {
3832 err = add_ignores_from_parent_paths(&ignores,
3833 worktree->root_path, ondisk_path);
3834 if (err)
3835 goto done;
3837 err = report_single_file_status(path, ondisk_path,
3838 fileindex, status_cb, status_arg, repo,
3839 report_unchanged, &ignores, no_ignores);
3841 } else {
3842 fdiff_cb.diff_old_new = status_old_new;
3843 fdiff_cb.diff_old = status_old;
3844 fdiff_cb.diff_new = status_new;
3845 fdiff_cb.diff_traverse = status_traverse;
3846 arg.fileindex = fileindex;
3847 arg.worktree = worktree;
3848 arg.status_path = path;
3849 arg.status_path_len = strlen(path);
3850 arg.repo = repo;
3851 arg.status_cb = status_cb;
3852 arg.status_arg = status_arg;
3853 arg.cancel_cb = cancel_cb;
3854 arg.cancel_arg = cancel_arg;
3855 arg.report_unchanged = report_unchanged;
3856 arg.no_ignores = no_ignores;
3857 if (!no_ignores) {
3858 err = add_ignores_from_parent_paths(&ignores,
3859 worktree->root_path, path);
3860 if (err)
3861 goto done;
3863 arg.ignores = &ignores;
3864 err = got_fileindex_diff_dir(fileindex, fd,
3865 worktree->root_path, path, repo, &fdiff_cb, &arg);
3867 done:
3868 free_ignores(&ignores);
3869 if (fd != -1 && close(fd) == -1 && err == NULL)
3870 err = got_error_from_errno("close");
3871 free(ondisk_path);
3872 return err;
3875 const struct got_error *
3876 got_worktree_status(struct got_worktree *worktree,
3877 struct got_pathlist_head *paths, struct got_repository *repo,
3878 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3879 got_cancel_cb cancel_cb, void *cancel_arg)
3881 const struct got_error *err = NULL;
3882 char *fileindex_path = NULL;
3883 struct got_fileindex *fileindex = NULL;
3884 struct got_pathlist_entry *pe;
3886 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3887 if (err)
3888 return err;
3890 TAILQ_FOREACH(pe, paths, entry) {
3891 err = worktree_status(worktree, pe->path, fileindex, repo,
3892 status_cb, status_arg, cancel_cb, cancel_arg,
3893 no_ignores, 0);
3894 if (err)
3895 break;
3897 free(fileindex_path);
3898 got_fileindex_free(fileindex);
3899 return err;
3902 const struct got_error *
3903 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3904 const char *arg)
3906 const struct got_error *err = NULL;
3907 char *resolved = NULL, *cwd = NULL, *path = NULL;
3908 size_t len;
3909 struct stat sb;
3910 char *abspath = NULL;
3911 char canonpath[PATH_MAX];
3913 *wt_path = NULL;
3915 cwd = getcwd(NULL, 0);
3916 if (cwd == NULL)
3917 return got_error_from_errno("getcwd");
3919 if (lstat(arg, &sb) == -1) {
3920 if (errno != ENOENT) {
3921 err = got_error_from_errno2("lstat", arg);
3922 goto done;
3924 sb.st_mode = 0;
3926 if (S_ISLNK(sb.st_mode)) {
3928 * We cannot use realpath(3) with symlinks since we want to
3929 * operate on the symlink itself.
3930 * But we can make the path absolute, assuming it is relative
3931 * to the current working directory, and then canonicalize it.
3933 if (!got_path_is_absolute(arg)) {
3934 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3935 err = got_error_from_errno("asprintf");
3936 goto done;
3940 err = got_canonpath(abspath ? abspath : arg, canonpath,
3941 sizeof(canonpath));
3942 if (err)
3943 goto done;
3944 resolved = strdup(canonpath);
3945 if (resolved == NULL) {
3946 err = got_error_from_errno("strdup");
3947 goto done;
3949 } else {
3950 resolved = realpath(arg, NULL);
3951 if (resolved == NULL) {
3952 if (errno != ENOENT) {
3953 err = got_error_from_errno2("realpath", arg);
3954 goto done;
3956 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3957 err = got_error_from_errno("asprintf");
3958 goto done;
3960 err = got_canonpath(abspath, canonpath,
3961 sizeof(canonpath));
3962 if (err)
3963 goto done;
3964 resolved = strdup(canonpath);
3965 if (resolved == NULL) {
3966 err = got_error_from_errno("strdup");
3967 goto done;
3972 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3973 strlen(got_worktree_get_root_path(worktree)))) {
3974 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3975 goto done;
3978 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3979 err = got_path_skip_common_ancestor(&path,
3980 got_worktree_get_root_path(worktree), resolved);
3981 if (err)
3982 goto done;
3983 } else {
3984 path = strdup("");
3985 if (path == NULL) {
3986 err = got_error_from_errno("strdup");
3987 goto done;
3991 /* XXX status walk can't deal with trailing slash! */
3992 len = strlen(path);
3993 while (len > 0 && path[len - 1] == '/') {
3994 path[len - 1] = '\0';
3995 len--;
3997 done:
3998 free(abspath);
3999 free(resolved);
4000 free(cwd);
4001 if (err == NULL)
4002 *wt_path = path;
4003 else
4004 free(path);
4005 return err;
4008 struct schedule_addition_args {
4009 struct got_worktree *worktree;
4010 struct got_fileindex *fileindex;
4011 got_worktree_checkout_cb progress_cb;
4012 void *progress_arg;
4013 struct got_repository *repo;
4016 static const struct got_error *
4017 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4018 const char *relpath, struct got_object_id *blob_id,
4019 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4020 int dirfd, const char *de_name)
4022 struct schedule_addition_args *a = arg;
4023 const struct got_error *err = NULL;
4024 struct got_fileindex_entry *ie;
4025 struct stat sb;
4026 char *ondisk_path;
4028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4029 relpath) == -1)
4030 return got_error_from_errno("asprintf");
4032 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4033 if (ie) {
4034 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4035 de_name, a->repo);
4036 if (err)
4037 goto done;
4038 /* Re-adding an existing entry is a no-op. */
4039 if (status == GOT_STATUS_ADD)
4040 goto done;
4041 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4042 if (err)
4043 goto done;
4046 if (status != GOT_STATUS_UNVERSIONED) {
4047 if (status == GOT_STATUS_NONEXISTENT)
4048 err = got_error_set_errno(ENOENT, ondisk_path);
4049 else
4050 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4051 goto done;
4054 err = got_fileindex_entry_alloc(&ie, relpath);
4055 if (err)
4056 goto done;
4057 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4058 relpath, NULL, NULL, 1);
4059 if (err) {
4060 got_fileindex_entry_free(ie);
4061 goto done;
4063 err = got_fileindex_entry_add(a->fileindex, ie);
4064 if (err) {
4065 got_fileindex_entry_free(ie);
4066 goto done;
4068 done:
4069 free(ondisk_path);
4070 if (err)
4071 return err;
4072 if (status == GOT_STATUS_ADD)
4073 return NULL;
4074 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4077 const struct got_error *
4078 got_worktree_schedule_add(struct got_worktree *worktree,
4079 struct got_pathlist_head *paths,
4080 got_worktree_checkout_cb progress_cb, void *progress_arg,
4081 struct got_repository *repo, int no_ignores)
4083 struct got_fileindex *fileindex = NULL;
4084 char *fileindex_path = NULL;
4085 const struct got_error *err = NULL, *sync_err, *unlockerr;
4086 struct got_pathlist_entry *pe;
4087 struct schedule_addition_args saa;
4089 err = lock_worktree(worktree, LOCK_EX);
4090 if (err)
4091 return err;
4093 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4094 if (err)
4095 goto done;
4097 saa.worktree = worktree;
4098 saa.fileindex = fileindex;
4099 saa.progress_cb = progress_cb;
4100 saa.progress_arg = progress_arg;
4101 saa.repo = repo;
4103 TAILQ_FOREACH(pe, paths, entry) {
4104 err = worktree_status(worktree, pe->path, fileindex, repo,
4105 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4106 if (err)
4107 break;
4109 sync_err = sync_fileindex(fileindex, fileindex_path);
4110 if (sync_err && err == NULL)
4111 err = sync_err;
4112 done:
4113 free(fileindex_path);
4114 if (fileindex)
4115 got_fileindex_free(fileindex);
4116 unlockerr = lock_worktree(worktree, LOCK_SH);
4117 if (unlockerr && err == NULL)
4118 err = unlockerr;
4119 return err;
4122 struct schedule_deletion_args {
4123 struct got_worktree *worktree;
4124 struct got_fileindex *fileindex;
4125 got_worktree_delete_cb progress_cb;
4126 void *progress_arg;
4127 struct got_repository *repo;
4128 int delete_local_mods;
4129 int keep_on_disk;
4130 int ignore_missing_paths;
4131 const char *status_codes;
4134 static const struct got_error *
4135 schedule_for_deletion(void *arg, unsigned char status,
4136 unsigned char staged_status, const char *relpath,
4137 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4138 struct got_object_id *commit_id, int dirfd, const char *de_name)
4140 struct schedule_deletion_args *a = arg;
4141 const struct got_error *err = NULL;
4142 struct got_fileindex_entry *ie = NULL;
4143 struct stat sb;
4144 char *ondisk_path;
4146 if (status == GOT_STATUS_NONEXISTENT) {
4147 if (a->ignore_missing_paths)
4148 return NULL;
4149 return got_error_set_errno(ENOENT, relpath);
4152 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4153 if (ie == NULL)
4154 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4156 staged_status = get_staged_status(ie);
4157 if (staged_status != GOT_STATUS_NO_CHANGE) {
4158 if (staged_status == GOT_STATUS_DELETE)
4159 return NULL;
4160 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4163 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4164 relpath) == -1)
4165 return got_error_from_errno("asprintf");
4167 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4168 a->repo);
4169 if (err)
4170 goto done;
4172 if (a->status_codes) {
4173 size_t ncodes = strlen(a->status_codes);
4174 int i;
4175 for (i = 0; i < ncodes ; i++) {
4176 if (status == a->status_codes[i])
4177 break;
4179 if (i == ncodes) {
4180 /* Do not delete files in non-matching status. */
4181 free(ondisk_path);
4182 return NULL;
4184 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4185 a->status_codes[i] != GOT_STATUS_MISSING) {
4186 static char msg[64];
4187 snprintf(msg, sizeof(msg),
4188 "invalid status code '%c'", a->status_codes[i]);
4189 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4190 goto done;
4194 if (status != GOT_STATUS_NO_CHANGE) {
4195 if (status == GOT_STATUS_DELETE)
4196 goto done;
4197 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4198 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4199 goto done;
4201 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4202 err = got_error_set_errno(ENOENT, relpath);
4203 goto done;
4205 if (status != GOT_STATUS_MODIFY &&
4206 status != GOT_STATUS_MISSING) {
4207 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4208 goto done;
4212 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4213 size_t root_len;
4215 if (dirfd != -1) {
4216 if (unlinkat(dirfd, de_name, 0) == -1) {
4217 err = got_error_from_errno2("unlinkat",
4218 ondisk_path);
4219 goto done;
4221 } else if (unlink(ondisk_path) == -1) {
4222 err = got_error_from_errno2("unlink", ondisk_path);
4223 goto done;
4226 root_len = strlen(a->worktree->root_path);
4227 do {
4228 char *parent;
4229 err = got_path_dirname(&parent, ondisk_path);
4230 if (err)
4231 goto done;
4232 free(ondisk_path);
4233 ondisk_path = parent;
4234 if (rmdir(ondisk_path) == -1) {
4235 if (errno != ENOTEMPTY)
4236 err = got_error_from_errno2("rmdir",
4237 ondisk_path);
4238 break;
4240 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4241 strlen(ondisk_path), root_len) != 0);
4244 got_fileindex_entry_mark_deleted_from_disk(ie);
4245 done:
4246 free(ondisk_path);
4247 if (err)
4248 return err;
4249 if (status == GOT_STATUS_DELETE)
4250 return NULL;
4251 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4252 staged_status, relpath);
4255 const struct got_error *
4256 got_worktree_schedule_delete(struct got_worktree *worktree,
4257 struct got_pathlist_head *paths, int delete_local_mods,
4258 const char *status_codes,
4259 got_worktree_delete_cb progress_cb, void *progress_arg,
4260 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4262 struct got_fileindex *fileindex = NULL;
4263 char *fileindex_path = NULL;
4264 const struct got_error *err = NULL, *sync_err, *unlockerr;
4265 struct got_pathlist_entry *pe;
4266 struct schedule_deletion_args sda;
4268 err = lock_worktree(worktree, LOCK_EX);
4269 if (err)
4270 return err;
4272 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4273 if (err)
4274 goto done;
4276 sda.worktree = worktree;
4277 sda.fileindex = fileindex;
4278 sda.progress_cb = progress_cb;
4279 sda.progress_arg = progress_arg;
4280 sda.repo = repo;
4281 sda.delete_local_mods = delete_local_mods;
4282 sda.keep_on_disk = keep_on_disk;
4283 sda.ignore_missing_paths = ignore_missing_paths;
4284 sda.status_codes = status_codes;
4286 TAILQ_FOREACH(pe, paths, entry) {
4287 err = worktree_status(worktree, pe->path, fileindex, repo,
4288 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4289 if (err)
4290 break;
4292 sync_err = sync_fileindex(fileindex, fileindex_path);
4293 if (sync_err && err == NULL)
4294 err = sync_err;
4295 done:
4296 free(fileindex_path);
4297 if (fileindex)
4298 got_fileindex_free(fileindex);
4299 unlockerr = lock_worktree(worktree, LOCK_SH);
4300 if (unlockerr && err == NULL)
4301 err = unlockerr;
4302 return err;
4305 static const struct got_error *
4306 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4308 const struct got_error *err = NULL;
4309 char *line = NULL;
4310 size_t linesize = 0, n;
4311 ssize_t linelen;
4313 linelen = getline(&line, &linesize, infile);
4314 if (linelen == -1) {
4315 if (ferror(infile)) {
4316 err = got_error_from_errno("getline");
4317 goto done;
4319 return NULL;
4321 if (outfile) {
4322 n = fwrite(line, 1, linelen, outfile);
4323 if (n != linelen) {
4324 err = got_ferror(outfile, GOT_ERR_IO);
4325 goto done;
4328 if (rejectfile) {
4329 n = fwrite(line, 1, linelen, rejectfile);
4330 if (n != linelen)
4331 err = got_ferror(rejectfile, GOT_ERR_IO);
4333 done:
4334 free(line);
4335 return err;
4338 static const struct got_error *
4339 skip_one_line(FILE *f)
4341 char *line = NULL;
4342 size_t linesize = 0;
4343 ssize_t linelen;
4345 linelen = getline(&line, &linesize, f);
4346 if (linelen == -1) {
4347 if (ferror(f))
4348 return got_error_from_errno("getline");
4349 return NULL;
4351 free(line);
4352 return NULL;
4355 static const struct got_error *
4356 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4357 int start_old, int end_old, int start_new, int end_new,
4358 FILE *outfile, FILE *rejectfile)
4360 const struct got_error *err;
4362 /* Copy old file's lines leading up to patch. */
4363 while (!feof(f1) && *line_cur1 < start_old) {
4364 err = copy_one_line(f1, outfile, NULL);
4365 if (err)
4366 return err;
4367 (*line_cur1)++;
4369 /* Skip new file's lines leading up to patch. */
4370 while (!feof(f2) && *line_cur2 < start_new) {
4371 if (rejectfile)
4372 err = copy_one_line(f2, NULL, rejectfile);
4373 else
4374 err = skip_one_line(f2);
4375 if (err)
4376 return err;
4377 (*line_cur2)++;
4379 /* Copy patched lines. */
4380 while (!feof(f2) && *line_cur2 <= end_new) {
4381 err = copy_one_line(f2, outfile, NULL);
4382 if (err)
4383 return err;
4384 (*line_cur2)++;
4386 /* Skip over old file's replaced lines. */
4387 while (!feof(f1) && *line_cur1 <= end_old) {
4388 if (rejectfile)
4389 err = copy_one_line(f1, NULL, rejectfile);
4390 else
4391 err = skip_one_line(f1);
4392 if (err)
4393 return err;
4394 (*line_cur1)++;
4397 return NULL;
4400 static const struct got_error *
4401 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4402 FILE *outfile, FILE *rejectfile)
4404 const struct got_error *err;
4406 if (outfile) {
4407 /* Copy old file's lines until EOF. */
4408 while (!feof(f1)) {
4409 err = copy_one_line(f1, outfile, NULL);
4410 if (err)
4411 return err;
4412 (*line_cur1)++;
4415 if (rejectfile) {
4416 /* Copy new file's lines until EOF. */
4417 while (!feof(f2)) {
4418 err = copy_one_line(f2, NULL, rejectfile);
4419 if (err)
4420 return err;
4421 (*line_cur2)++;
4425 return NULL;
4428 static const struct got_error *
4429 apply_or_reject_change(int *choice, int *nchunks_used,
4430 struct diff_result *diff_result, int n,
4431 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4432 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4433 got_worktree_patch_cb patch_cb, void *patch_arg)
4435 const struct got_error *err = NULL;
4436 struct diff_chunk_context cc = {};
4437 int start_old, end_old, start_new, end_new;
4438 FILE *hunkfile;
4439 struct diff_output_unidiff_state *diff_state;
4440 struct diff_input_info diff_info;
4441 int rc;
4443 *choice = GOT_PATCH_CHOICE_NONE;
4445 /* Get changed line numbers without context lines for copy_change(). */
4446 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4447 start_old = cc.left.start;
4448 end_old = cc.left.end;
4449 start_new = cc.right.start;
4450 end_new = cc.right.end;
4452 /* Get the same change with context lines for display. */
4453 memset(&cc, 0, sizeof(cc));
4454 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4456 memset(&diff_info, 0, sizeof(diff_info));
4457 diff_info.left_path = relpath;
4458 diff_info.right_path = relpath;
4460 diff_state = diff_output_unidiff_state_alloc();
4461 if (diff_state == NULL)
4462 return got_error_set_errno(ENOMEM,
4463 "diff_output_unidiff_state_alloc");
4465 hunkfile = got_opentemp();
4466 if (hunkfile == NULL) {
4467 err = got_error_from_errno("got_opentemp");
4468 goto done;
4471 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4472 diff_result, &cc);
4473 if (rc != DIFF_RC_OK) {
4474 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4475 goto done;
4478 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4479 err = got_ferror(hunkfile, GOT_ERR_IO);
4480 goto done;
4483 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4484 hunkfile, changeno, nchanges);
4485 if (err)
4486 goto done;
4488 switch (*choice) {
4489 case GOT_PATCH_CHOICE_YES:
4490 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4491 end_old, start_new, end_new, outfile, rejectfile);
4492 break;
4493 case GOT_PATCH_CHOICE_NO:
4494 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4495 end_old, start_new, end_new, rejectfile, outfile);
4496 break;
4497 case GOT_PATCH_CHOICE_QUIT:
4498 break;
4499 default:
4500 err = got_error(GOT_ERR_PATCH_CHOICE);
4501 break;
4503 done:
4504 diff_output_unidiff_state_free(diff_state);
4505 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4506 err = got_error_from_errno("fclose");
4507 return err;
4510 struct revert_file_args {
4511 struct got_worktree *worktree;
4512 struct got_fileindex *fileindex;
4513 got_worktree_checkout_cb progress_cb;
4514 void *progress_arg;
4515 got_worktree_patch_cb patch_cb;
4516 void *patch_arg;
4517 struct got_repository *repo;
4518 int unlink_added_files;
4521 static const struct got_error *
4522 create_patched_content(char **path_outfile, int reverse_patch,
4523 struct got_object_id *blob_id, const char *path2,
4524 int dirfd2, const char *de_name2,
4525 const char *relpath, struct got_repository *repo,
4526 got_worktree_patch_cb patch_cb, void *patch_arg)
4528 const struct got_error *err, *free_err;
4529 struct got_blob_object *blob = NULL;
4530 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4531 int fd = -1, fd2 = -1;
4532 char link_target[PATH_MAX];
4533 ssize_t link_len = 0;
4534 char *path1 = NULL, *id_str = NULL;
4535 struct stat sb2;
4536 struct got_diffreg_result *diffreg_result = NULL;
4537 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4538 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4540 *path_outfile = NULL;
4542 err = got_object_id_str(&id_str, blob_id);
4543 if (err)
4544 return err;
4546 if (dirfd2 != -1) {
4547 fd2 = openat(dirfd2, de_name2,
4548 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4549 if (fd2 == -1) {
4550 if (!got_err_open_nofollow_on_symlink()) {
4551 err = got_error_from_errno2("openat", path2);
4552 goto done;
4554 link_len = readlinkat(dirfd2, de_name2,
4555 link_target, sizeof(link_target));
4556 if (link_len == -1) {
4557 return got_error_from_errno2("readlinkat",
4558 path2);
4560 sb2.st_mode = S_IFLNK;
4561 sb2.st_size = link_len;
4563 } else {
4564 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4565 if (fd2 == -1) {
4566 if (!got_err_open_nofollow_on_symlink()) {
4567 err = got_error_from_errno2("open", path2);
4568 goto done;
4570 link_len = readlink(path2, link_target,
4571 sizeof(link_target));
4572 if (link_len == -1)
4573 return got_error_from_errno2("readlink", path2);
4574 sb2.st_mode = S_IFLNK;
4575 sb2.st_size = link_len;
4578 if (fd2 != -1) {
4579 if (fstat(fd2, &sb2) == -1) {
4580 err = got_error_from_errno2("fstat", path2);
4581 goto done;
4584 f2 = fdopen(fd2, "r");
4585 if (f2 == NULL) {
4586 err = got_error_from_errno2("fdopen", path2);
4587 goto done;
4589 fd2 = -1;
4590 } else {
4591 size_t n;
4592 f2 = got_opentemp();
4593 if (f2 == NULL) {
4594 err = got_error_from_errno2("got_opentemp", path2);
4595 goto done;
4597 n = fwrite(link_target, 1, link_len, f2);
4598 if (n != link_len) {
4599 err = got_ferror(f2, GOT_ERR_IO);
4600 goto done;
4602 if (fflush(f2) == EOF) {
4603 err = got_error_from_errno("fflush");
4604 goto done;
4606 rewind(f2);
4609 fd = got_opentempfd();
4610 if (fd == -1) {
4611 err = got_error_from_errno("got_opentempfd");
4612 goto done;
4615 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4616 if (err)
4617 goto done;
4619 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4620 if (err)
4621 goto done;
4623 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4624 if (err)
4625 goto done;
4627 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4628 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4629 if (err)
4630 goto done;
4632 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4633 "");
4634 if (err)
4635 goto done;
4637 if (fseek(f1, 0L, SEEK_SET) == -1)
4638 return got_ferror(f1, GOT_ERR_IO);
4639 if (fseek(f2, 0L, SEEK_SET) == -1)
4640 return got_ferror(f2, GOT_ERR_IO);
4642 /* Count the number of actual changes in the diff result. */
4643 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4644 struct diff_chunk_context cc = {};
4645 diff_chunk_context_load_change(&cc, &nchunks_used,
4646 diffreg_result->result, n, 0);
4647 nchanges++;
4649 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4650 int choice;
4651 err = apply_or_reject_change(&choice, &nchunks_used,
4652 diffreg_result->result, n, relpath, f1, f2,
4653 &line_cur1, &line_cur2,
4654 reverse_patch ? NULL : outfile,
4655 reverse_patch ? outfile : NULL,
4656 ++i, nchanges, patch_cb, patch_arg);
4657 if (err)
4658 goto done;
4659 if (choice == GOT_PATCH_CHOICE_YES)
4660 have_content = 1;
4661 else if (choice == GOT_PATCH_CHOICE_QUIT)
4662 break;
4664 if (have_content) {
4665 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4666 reverse_patch ? NULL : outfile,
4667 reverse_patch ? outfile : NULL);
4668 if (err)
4669 goto done;
4671 if (!S_ISLNK(sb2.st_mode)) {
4672 mode_t mode;
4674 mode = apply_umask(sb2.st_mode);
4675 if (fchmod(fileno(outfile), mode) == -1) {
4676 err = got_error_from_errno2("fchmod", path2);
4677 goto done;
4681 done:
4682 free(id_str);
4683 if (fd != -1 && close(fd) == -1 && err == NULL)
4684 err = got_error_from_errno("close");
4685 if (blob)
4686 got_object_blob_close(blob);
4687 free_err = got_diffreg_result_free(diffreg_result);
4688 if (err == NULL)
4689 err = free_err;
4690 if (f1 && fclose(f1) == EOF && err == NULL)
4691 err = got_error_from_errno2("fclose", path1);
4692 if (f2 && fclose(f2) == EOF && err == NULL)
4693 err = got_error_from_errno2("fclose", path2);
4694 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4695 err = got_error_from_errno2("close", path2);
4696 if (outfile && fclose(outfile) == EOF && err == NULL)
4697 err = got_error_from_errno2("fclose", *path_outfile);
4698 if (path1 && unlink(path1) == -1 && err == NULL)
4699 err = got_error_from_errno2("unlink", path1);
4700 if (err || !have_content) {
4701 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4702 err = got_error_from_errno2("unlink", *path_outfile);
4703 free(*path_outfile);
4704 *path_outfile = NULL;
4706 free(path1);
4707 return err;
4710 static const struct got_error *
4711 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4712 const char *relpath, struct got_object_id *blob_id,
4713 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4714 int dirfd, const char *de_name)
4716 struct revert_file_args *a = arg;
4717 const struct got_error *err = NULL;
4718 char *parent_path = NULL;
4719 struct got_fileindex_entry *ie;
4720 struct got_commit_object *base_commit = NULL;
4721 struct got_tree_object *tree = NULL;
4722 struct got_object_id *tree_id = NULL;
4723 const struct got_tree_entry *te = NULL;
4724 char *tree_path = NULL, *te_name;
4725 char *ondisk_path = NULL, *path_content = NULL;
4726 struct got_blob_object *blob = NULL;
4727 int fd = -1;
4729 /* Reverting a staged deletion is a no-op. */
4730 if (status == GOT_STATUS_DELETE &&
4731 staged_status != GOT_STATUS_NO_CHANGE)
4732 return NULL;
4734 if (status == GOT_STATUS_UNVERSIONED)
4735 return (*a->progress_cb)(a->progress_arg,
4736 GOT_STATUS_UNVERSIONED, relpath);
4738 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4739 if (ie == NULL)
4740 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4742 /* Construct in-repository path of tree which contains this blob. */
4743 err = got_path_dirname(&parent_path, ie->path);
4744 if (err) {
4745 if (err->code != GOT_ERR_BAD_PATH)
4746 goto done;
4747 parent_path = strdup("/");
4748 if (parent_path == NULL) {
4749 err = got_error_from_errno("strdup");
4750 goto done;
4753 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4754 tree_path = strdup(parent_path);
4755 if (tree_path == NULL) {
4756 err = got_error_from_errno("strdup");
4757 goto done;
4759 } else {
4760 if (got_path_is_root_dir(parent_path)) {
4761 tree_path = strdup(a->worktree->path_prefix);
4762 if (tree_path == NULL) {
4763 err = got_error_from_errno("strdup");
4764 goto done;
4766 } else {
4767 if (asprintf(&tree_path, "%s/%s",
4768 a->worktree->path_prefix, parent_path) == -1) {
4769 err = got_error_from_errno("asprintf");
4770 goto done;
4775 err = got_object_open_as_commit(&base_commit, a->repo,
4776 a->worktree->base_commit_id);
4777 if (err)
4778 goto done;
4780 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4781 if (err) {
4782 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4783 (status == GOT_STATUS_ADD ||
4784 staged_status == GOT_STATUS_ADD)))
4785 goto done;
4786 } else {
4787 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4788 if (err)
4789 goto done;
4791 err = got_path_basename(&te_name, ie->path);
4792 if (err)
4793 goto done;
4795 te = got_object_tree_find_entry(tree, te_name);
4796 free(te_name);
4797 if (te == NULL && status != GOT_STATUS_ADD &&
4798 staged_status != GOT_STATUS_ADD) {
4799 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4800 goto done;
4804 switch (status) {
4805 case GOT_STATUS_ADD:
4806 if (a->patch_cb) {
4807 int choice = GOT_PATCH_CHOICE_NONE;
4808 err = (*a->patch_cb)(&choice, a->patch_arg,
4809 status, ie->path, NULL, 1, 1);
4810 if (err)
4811 goto done;
4812 if (choice != GOT_PATCH_CHOICE_YES)
4813 break;
4815 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4816 ie->path);
4817 if (err)
4818 goto done;
4819 got_fileindex_entry_remove(a->fileindex, ie);
4820 if (a->unlink_added_files) {
4821 if (asprintf(&ondisk_path, "%s/%s",
4822 got_worktree_get_root_path(a->worktree),
4823 relpath) == -1) {
4824 err = got_error_from_errno("asprintf");
4825 goto done;
4827 if (unlink(ondisk_path) == -1) {
4828 err = got_error_from_errno2("unlink",
4829 ondisk_path);
4830 break;
4833 break;
4834 case GOT_STATUS_DELETE:
4835 if (a->patch_cb) {
4836 int choice = GOT_PATCH_CHOICE_NONE;
4837 err = (*a->patch_cb)(&choice, a->patch_arg,
4838 status, ie->path, NULL, 1, 1);
4839 if (err)
4840 goto done;
4841 if (choice != GOT_PATCH_CHOICE_YES)
4842 break;
4844 /* fall through */
4845 case GOT_STATUS_MODIFY:
4846 case GOT_STATUS_MODE_CHANGE:
4847 case GOT_STATUS_CONFLICT:
4848 case GOT_STATUS_MISSING: {
4849 struct got_object_id id;
4850 if (staged_status == GOT_STATUS_ADD ||
4851 staged_status == GOT_STATUS_MODIFY)
4852 got_fileindex_entry_get_staged_blob_id(&id, ie);
4853 else
4854 got_fileindex_entry_get_blob_id(&id, ie);
4855 fd = got_opentempfd();
4856 if (fd == -1) {
4857 err = got_error_from_errno("got_opentempfd");
4858 goto done;
4861 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4862 if (err)
4863 goto done;
4865 if (asprintf(&ondisk_path, "%s/%s",
4866 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4867 err = got_error_from_errno("asprintf");
4868 goto done;
4871 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4872 status == GOT_STATUS_CONFLICT)) {
4873 int is_bad_symlink = 0;
4874 err = create_patched_content(&path_content, 1, &id,
4875 ondisk_path, dirfd, de_name, ie->path, a->repo,
4876 a->patch_cb, a->patch_arg);
4877 if (err || path_content == NULL)
4878 break;
4879 if (te && S_ISLNK(te->mode)) {
4880 if (unlink(path_content) == -1) {
4881 err = got_error_from_errno2("unlink",
4882 path_content);
4883 break;
4885 err = install_symlink(&is_bad_symlink,
4886 a->worktree, ondisk_path, ie->path,
4887 blob, 0, 1, 0, 0, a->repo,
4888 a->progress_cb, a->progress_arg);
4889 } else {
4890 if (rename(path_content, ondisk_path) == -1) {
4891 err = got_error_from_errno3("rename",
4892 path_content, ondisk_path);
4893 goto done;
4896 } else {
4897 int is_bad_symlink = 0;
4898 if (te && S_ISLNK(te->mode)) {
4899 err = install_symlink(&is_bad_symlink,
4900 a->worktree, ondisk_path, ie->path,
4901 blob, 0, 1, 0, 0, a->repo,
4902 a->progress_cb, a->progress_arg);
4903 } else {
4904 err = install_blob(a->worktree, ondisk_path,
4905 ie->path,
4906 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4907 got_fileindex_perms_to_st(ie), blob,
4908 0, 1, 0, 0, a->repo,
4909 a->progress_cb, a->progress_arg);
4911 if (err)
4912 goto done;
4913 if (status == GOT_STATUS_DELETE ||
4914 status == GOT_STATUS_MODE_CHANGE) {
4915 err = got_fileindex_entry_update(ie,
4916 a->worktree->root_fd, relpath,
4917 blob->id.sha1,
4918 a->worktree->base_commit_id->sha1, 1);
4919 if (err)
4920 goto done;
4922 if (is_bad_symlink) {
4923 got_fileindex_entry_filetype_set(ie,
4924 GOT_FILEIDX_MODE_BAD_SYMLINK);
4927 break;
4929 default:
4930 break;
4932 done:
4933 free(ondisk_path);
4934 free(path_content);
4935 free(parent_path);
4936 free(tree_path);
4937 if (fd != -1 && close(fd) == -1 && err == NULL)
4938 err = got_error_from_errno("close");
4939 if (blob)
4940 got_object_blob_close(blob);
4941 if (tree)
4942 got_object_tree_close(tree);
4943 free(tree_id);
4944 if (base_commit)
4945 got_object_commit_close(base_commit);
4946 return err;
4949 const struct got_error *
4950 got_worktree_revert(struct got_worktree *worktree,
4951 struct got_pathlist_head *paths,
4952 got_worktree_checkout_cb progress_cb, void *progress_arg,
4953 got_worktree_patch_cb patch_cb, void *patch_arg,
4954 struct got_repository *repo)
4956 struct got_fileindex *fileindex = NULL;
4957 char *fileindex_path = NULL;
4958 const struct got_error *err = NULL, *unlockerr = NULL;
4959 const struct got_error *sync_err = NULL;
4960 struct got_pathlist_entry *pe;
4961 struct revert_file_args rfa;
4963 err = lock_worktree(worktree, LOCK_EX);
4964 if (err)
4965 return err;
4967 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4968 if (err)
4969 goto done;
4971 rfa.worktree = worktree;
4972 rfa.fileindex = fileindex;
4973 rfa.progress_cb = progress_cb;
4974 rfa.progress_arg = progress_arg;
4975 rfa.patch_cb = patch_cb;
4976 rfa.patch_arg = patch_arg;
4977 rfa.repo = repo;
4978 rfa.unlink_added_files = 0;
4979 TAILQ_FOREACH(pe, paths, entry) {
4980 err = worktree_status(worktree, pe->path, fileindex, repo,
4981 revert_file, &rfa, NULL, NULL, 1, 0);
4982 if (err)
4983 break;
4985 sync_err = sync_fileindex(fileindex, fileindex_path);
4986 if (sync_err && err == NULL)
4987 err = sync_err;
4988 done:
4989 free(fileindex_path);
4990 if (fileindex)
4991 got_fileindex_free(fileindex);
4992 unlockerr = lock_worktree(worktree, LOCK_SH);
4993 if (unlockerr && err == NULL)
4994 err = unlockerr;
4995 return err;
4998 static void
4999 free_commitable(struct got_commitable *ct)
5001 free(ct->path);
5002 free(ct->in_repo_path);
5003 free(ct->ondisk_path);
5004 free(ct->blob_id);
5005 free(ct->base_blob_id);
5006 free(ct->staged_blob_id);
5007 free(ct->base_commit_id);
5008 free(ct);
5011 struct collect_commitables_arg {
5012 struct got_pathlist_head *commitable_paths;
5013 struct got_repository *repo;
5014 struct got_worktree *worktree;
5015 struct got_fileindex *fileindex;
5016 int have_staged_files;
5017 int allow_bad_symlinks;
5018 int diff_header_shown;
5019 int commit_conflicts;
5020 FILE *diff_outfile;
5021 FILE *f1;
5022 FILE *f2;
5026 * Create a file which contains the target path of a symlink so we can feed
5027 * it as content to the diff engine.
5029 static const struct got_error *
5030 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5031 const char *abspath)
5033 const struct got_error *err = NULL;
5034 char target_path[PATH_MAX];
5035 ssize_t target_len, outlen;
5037 *fd = -1;
5039 if (dirfd != -1) {
5040 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5041 if (target_len == -1)
5042 return got_error_from_errno2("readlinkat", abspath);
5043 } else {
5044 target_len = readlink(abspath, target_path, PATH_MAX);
5045 if (target_len == -1)
5046 return got_error_from_errno2("readlink", abspath);
5049 *fd = got_opentempfd();
5050 if (*fd == -1)
5051 return got_error_from_errno("got_opentempfd");
5053 outlen = write(*fd, target_path, target_len);
5054 if (outlen == -1) {
5055 err = got_error_from_errno("got_opentempfd");
5056 goto done;
5059 if (lseek(*fd, 0, SEEK_SET) == -1) {
5060 err = got_error_from_errno2("lseek", abspath);
5061 goto done;
5063 done:
5064 if (err) {
5065 close(*fd);
5066 *fd = -1;
5068 return err;
5071 static const struct got_error *
5072 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5073 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5074 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5076 const struct got_error *err = NULL;
5077 struct got_blob_object *blob1 = NULL;
5078 int fd = -1, fd1 = -1, fd2 = -1;
5079 FILE *ondisk_file = NULL;
5080 char *label1 = NULL;
5081 struct stat sb;
5082 off_t size1 = 0;
5083 int f2_exists = 0;
5084 char *id_str = NULL;
5086 memset(&sb, 0, sizeof(sb));
5088 if (diff_staged) {
5089 if (ct->staged_status != GOT_STATUS_MODIFY &&
5090 ct->staged_status != GOT_STATUS_ADD &&
5091 ct->staged_status != GOT_STATUS_DELETE)
5092 return NULL;
5093 } else {
5094 if (ct->status != GOT_STATUS_MODIFY &&
5095 ct->status != GOT_STATUS_ADD &&
5096 ct->status != GOT_STATUS_DELETE &&
5097 ct->status != GOT_STATUS_CONFLICT)
5098 return NULL;
5101 err = got_opentemp_truncate(f1);
5102 if (err)
5103 return got_error_from_errno("got_opentemp_truncate");
5104 err = got_opentemp_truncate(f2);
5105 if (err)
5106 return got_error_from_errno("got_opentemp_truncate");
5108 if (!*diff_header_shown) {
5109 err = got_object_id_str(&id_str, worktree->base_commit_id);
5110 if (err)
5111 return err;
5112 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5113 got_worktree_get_root_path(worktree));
5114 fprintf(diff_outfile, "commit - %s\n", id_str);
5115 fprintf(diff_outfile, "path + %s%s\n",
5116 got_worktree_get_root_path(worktree),
5117 diff_staged ? " (staged changes)" : "");
5118 *diff_header_shown = 1;
5121 if (diff_staged) {
5122 const char *label1 = NULL, *label2 = NULL;
5123 switch (ct->staged_status) {
5124 case GOT_STATUS_MODIFY:
5125 label1 = ct->path;
5126 label2 = ct->path;
5127 break;
5128 case GOT_STATUS_ADD:
5129 label2 = ct->path;
5130 break;
5131 case GOT_STATUS_DELETE:
5132 label1 = ct->path;
5133 break;
5134 default:
5135 return got_error(GOT_ERR_FILE_STATUS);
5137 fd1 = got_opentempfd();
5138 if (fd1 == -1) {
5139 err = got_error_from_errno("got_opentempfd");
5140 goto done;
5142 fd2 = got_opentempfd();
5143 if (fd2 == -1) {
5144 err = got_error_from_errno("got_opentempfd");
5145 goto done;
5147 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5148 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5149 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5150 NULL, repo, diff_outfile);
5151 goto done;
5154 fd1 = got_opentempfd();
5155 if (fd1 == -1) {
5156 err = got_error_from_errno("got_opentempfd");
5157 goto done;
5160 if (ct->status != GOT_STATUS_ADD) {
5161 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5162 8192, fd1);
5163 if (err)
5164 goto done;
5167 if (ct->status != GOT_STATUS_DELETE) {
5168 if (dirfd != -1) {
5169 fd = openat(dirfd, de_name,
5170 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5171 if (fd == -1) {
5172 if (!got_err_open_nofollow_on_symlink()) {
5173 err = got_error_from_errno2("openat",
5174 ct->ondisk_path);
5175 goto done;
5177 err = get_symlink_target_file(&fd, dirfd,
5178 de_name, ct->ondisk_path);
5179 if (err)
5180 goto done;
5182 } else {
5183 fd = open(ct->ondisk_path,
5184 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5185 if (fd == -1) {
5186 if (!got_err_open_nofollow_on_symlink()) {
5187 err = got_error_from_errno2("open",
5188 ct->ondisk_path);
5189 goto done;
5191 err = get_symlink_target_file(&fd, dirfd,
5192 de_name, ct->ondisk_path);
5193 if (err)
5194 goto done;
5197 if (fstatat(fd, ct->ondisk_path, &sb,
5198 AT_SYMLINK_NOFOLLOW) == -1) {
5199 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5200 goto done;
5202 ondisk_file = fdopen(fd, "r");
5203 if (ondisk_file == NULL) {
5204 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5205 goto done;
5207 fd = -1;
5208 f2_exists = 1;
5211 if (blob1) {
5212 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5213 f1, blob1);
5214 if (err)
5215 goto done;
5218 err = got_diff_blob_file(blob1, f1, size1, label1,
5219 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5220 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5221 done:
5222 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5223 err = got_error_from_errno("close");
5224 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5225 err = got_error_from_errno("close");
5226 if (blob1)
5227 got_object_blob_close(blob1);
5228 if (fd != -1 && close(fd) == -1 && err == NULL)
5229 err = got_error_from_errno("close");
5230 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5231 err = got_error_from_errno("fclose");
5232 return err;
5235 static const struct got_error *
5236 collect_commitables(void *arg, unsigned char status,
5237 unsigned char staged_status, const char *relpath,
5238 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5239 struct got_object_id *commit_id, int dirfd, const char *de_name)
5241 struct collect_commitables_arg *a = arg;
5242 const struct got_error *err = NULL;
5243 struct got_commitable *ct = NULL;
5244 struct got_pathlist_entry *new = NULL;
5245 char *parent_path = NULL, *path = NULL;
5246 struct stat sb;
5248 if (a->have_staged_files) {
5249 if (staged_status != GOT_STATUS_MODIFY &&
5250 staged_status != GOT_STATUS_ADD &&
5251 staged_status != GOT_STATUS_DELETE)
5252 return NULL;
5253 } else {
5254 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5255 printf("C %s\n", relpath);
5256 return got_error(GOT_ERR_COMMIT_CONFLICT);
5259 if (status != GOT_STATUS_MODIFY &&
5260 status != GOT_STATUS_MODE_CHANGE &&
5261 status != GOT_STATUS_ADD &&
5262 status != GOT_STATUS_DELETE &&
5263 status != GOT_STATUS_CONFLICT)
5264 return NULL;
5267 if (asprintf(&path, "/%s", relpath) == -1) {
5268 err = got_error_from_errno("asprintf");
5269 goto done;
5271 if (strcmp(path, "/") == 0) {
5272 parent_path = strdup("");
5273 if (parent_path == NULL)
5274 return got_error_from_errno("strdup");
5275 } else {
5276 err = got_path_dirname(&parent_path, path);
5277 if (err)
5278 return err;
5281 ct = calloc(1, sizeof(*ct));
5282 if (ct == NULL) {
5283 err = got_error_from_errno("calloc");
5284 goto done;
5287 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5288 relpath) == -1) {
5289 err = got_error_from_errno("asprintf");
5290 goto done;
5293 if (staged_status == GOT_STATUS_ADD ||
5294 staged_status == GOT_STATUS_MODIFY) {
5295 struct got_fileindex_entry *ie;
5296 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5297 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5298 case GOT_FILEIDX_MODE_REGULAR_FILE:
5299 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5300 ct->mode = S_IFREG;
5301 break;
5302 case GOT_FILEIDX_MODE_SYMLINK:
5303 ct->mode = S_IFLNK;
5304 break;
5305 default:
5306 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5307 goto done;
5309 ct->mode |= got_fileindex_entry_perms_get(ie);
5310 } else if (status != GOT_STATUS_DELETE &&
5311 staged_status != GOT_STATUS_DELETE) {
5312 if (dirfd != -1) {
5313 if (fstatat(dirfd, de_name, &sb,
5314 AT_SYMLINK_NOFOLLOW) == -1) {
5315 err = got_error_from_errno2("fstatat",
5316 ct->ondisk_path);
5317 goto done;
5319 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5320 err = got_error_from_errno2("lstat", ct->ondisk_path);
5321 goto done;
5323 ct->mode = sb.st_mode;
5326 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5327 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5328 relpath) == -1) {
5329 err = got_error_from_errno("asprintf");
5330 goto done;
5333 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5334 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5335 int is_bad_symlink;
5336 char target_path[PATH_MAX];
5337 ssize_t target_len;
5338 target_len = readlink(ct->ondisk_path, target_path,
5339 sizeof(target_path));
5340 if (target_len == -1) {
5341 err = got_error_from_errno2("readlink",
5342 ct->ondisk_path);
5343 goto done;
5345 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5346 target_len, ct->ondisk_path, a->worktree->root_path);
5347 if (err)
5348 goto done;
5349 if (is_bad_symlink) {
5350 err = got_error_path(ct->ondisk_path,
5351 GOT_ERR_BAD_SYMLINK);
5352 goto done;
5357 ct->status = status;
5358 ct->staged_status = staged_status;
5359 ct->blob_id = NULL; /* will be filled in when blob gets created */
5360 if (ct->status != GOT_STATUS_ADD &&
5361 ct->staged_status != GOT_STATUS_ADD) {
5362 ct->base_blob_id = got_object_id_dup(blob_id);
5363 if (ct->base_blob_id == NULL) {
5364 err = got_error_from_errno("got_object_id_dup");
5365 goto done;
5367 ct->base_commit_id = got_object_id_dup(commit_id);
5368 if (ct->base_commit_id == NULL) {
5369 err = got_error_from_errno("got_object_id_dup");
5370 goto done;
5373 if (ct->staged_status == GOT_STATUS_ADD ||
5374 ct->staged_status == GOT_STATUS_MODIFY) {
5375 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5376 if (ct->staged_blob_id == NULL) {
5377 err = got_error_from_errno("got_object_id_dup");
5378 goto done;
5381 ct->path = strdup(path);
5382 if (ct->path == NULL) {
5383 err = got_error_from_errno("strdup");
5384 goto done;
5386 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5387 if (err)
5388 goto done;
5390 if (a->diff_outfile && ct && new != NULL) {
5391 err = append_ct_diff(ct, &a->diff_header_shown,
5392 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5393 a->have_staged_files, a->repo, a->worktree);
5394 if (err)
5395 goto done;
5397 done:
5398 if (ct && (err || new == NULL))
5399 free_commitable(ct);
5400 free(parent_path);
5401 free(path);
5402 return err;
5405 static const struct got_error *write_tree(struct got_object_id **, int *,
5406 struct got_tree_object *, const char *, struct got_pathlist_head *,
5407 got_worktree_status_cb status_cb, void *status_arg,
5408 struct got_repository *);
5410 static const struct got_error *
5411 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5412 struct got_tree_entry *te, const char *parent_path,
5413 struct got_pathlist_head *commitable_paths,
5414 got_worktree_status_cb status_cb, void *status_arg,
5415 struct got_repository *repo)
5417 const struct got_error *err = NULL;
5418 struct got_tree_object *subtree;
5419 char *subpath;
5421 if (asprintf(&subpath, "%s%s%s", parent_path,
5422 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5423 return got_error_from_errno("asprintf");
5425 err = got_object_open_as_tree(&subtree, repo, &te->id);
5426 if (err)
5427 return err;
5429 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5430 commitable_paths, status_cb, status_arg, repo);
5431 got_object_tree_close(subtree);
5432 free(subpath);
5433 return err;
5436 static const struct got_error *
5437 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5439 const struct got_error *err = NULL;
5440 char *ct_parent_path = NULL;
5442 *match = 0;
5444 if (strchr(ct->in_repo_path, '/') == NULL) {
5445 *match = got_path_is_root_dir(path);
5446 return NULL;
5449 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5450 if (err)
5451 return err;
5452 *match = (strcmp(path, ct_parent_path) == 0);
5453 free(ct_parent_path);
5454 return err;
5457 static mode_t
5458 get_ct_file_mode(struct got_commitable *ct)
5460 if (S_ISLNK(ct->mode))
5461 return S_IFLNK;
5463 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5466 static const struct got_error *
5467 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5468 struct got_tree_entry *te, struct got_commitable *ct)
5470 const struct got_error *err = NULL;
5472 *new_te = NULL;
5474 err = got_object_tree_entry_dup(new_te, te);
5475 if (err)
5476 goto done;
5478 (*new_te)->mode = get_ct_file_mode(ct);
5480 if (ct->staged_status == GOT_STATUS_MODIFY)
5481 memcpy(&(*new_te)->id, ct->staged_blob_id,
5482 sizeof((*new_te)->id));
5483 else
5484 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5485 done:
5486 if (err && *new_te) {
5487 free(*new_te);
5488 *new_te = NULL;
5490 return err;
5493 static const struct got_error *
5494 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5495 struct got_commitable *ct)
5497 const struct got_error *err = NULL;
5498 char *ct_name = NULL;
5500 *new_te = NULL;
5502 *new_te = calloc(1, sizeof(**new_te));
5503 if (*new_te == NULL)
5504 return got_error_from_errno("calloc");
5506 err = got_path_basename(&ct_name, ct->path);
5507 if (err)
5508 goto done;
5509 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5510 sizeof((*new_te)->name)) {
5511 err = got_error(GOT_ERR_NO_SPACE);
5512 goto done;
5515 (*new_te)->mode = get_ct_file_mode(ct);
5517 if (ct->staged_status == GOT_STATUS_ADD)
5518 memcpy(&(*new_te)->id, ct->staged_blob_id,
5519 sizeof((*new_te)->id));
5520 else
5521 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5522 done:
5523 free(ct_name);
5524 if (err && *new_te) {
5525 free(*new_te);
5526 *new_te = NULL;
5528 return err;
5531 static const struct got_error *
5532 insert_tree_entry(struct got_tree_entry *new_te,
5533 struct got_pathlist_head *paths)
5535 const struct got_error *err = NULL;
5536 struct got_pathlist_entry *new_pe;
5538 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5539 if (err)
5540 return err;
5541 if (new_pe == NULL)
5542 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5543 return NULL;
5546 static const struct got_error *
5547 report_ct_status(struct got_commitable *ct,
5548 got_worktree_status_cb status_cb, void *status_arg)
5550 const char *ct_path = ct->path;
5551 unsigned char status;
5553 if (status_cb == NULL) /* no commit progress output desired */
5554 return NULL;
5556 while (ct_path[0] == '/')
5557 ct_path++;
5559 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5560 status = ct->staged_status;
5561 else
5562 status = ct->status;
5564 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5565 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5568 static const struct got_error *
5569 match_modified_subtree(int *modified, struct got_tree_entry *te,
5570 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5572 const struct got_error *err = NULL;
5573 struct got_pathlist_entry *pe;
5574 char *te_path;
5576 *modified = 0;
5578 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5579 got_path_is_root_dir(base_tree_path) ? "" : "/",
5580 te->name) == -1)
5581 return got_error_from_errno("asprintf");
5583 TAILQ_FOREACH(pe, commitable_paths, entry) {
5584 struct got_commitable *ct = pe->data;
5585 *modified = got_path_is_child(ct->in_repo_path, te_path,
5586 strlen(te_path));
5587 if (*modified)
5588 break;
5591 free(te_path);
5592 return err;
5595 static const struct got_error *
5596 match_deleted_or_modified_ct(struct got_commitable **ctp,
5597 struct got_tree_entry *te, const char *base_tree_path,
5598 struct got_pathlist_head *commitable_paths)
5600 const struct got_error *err = NULL;
5601 struct got_pathlist_entry *pe;
5603 *ctp = NULL;
5605 TAILQ_FOREACH(pe, commitable_paths, entry) {
5606 struct got_commitable *ct = pe->data;
5607 char *ct_name = NULL;
5608 int path_matches;
5610 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5611 if (ct->status != GOT_STATUS_MODIFY &&
5612 ct->status != GOT_STATUS_MODE_CHANGE &&
5613 ct->status != GOT_STATUS_DELETE &&
5614 ct->status != GOT_STATUS_CONFLICT)
5615 continue;
5616 } else {
5617 if (ct->staged_status != GOT_STATUS_MODIFY &&
5618 ct->staged_status != GOT_STATUS_DELETE)
5619 continue;
5622 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5623 continue;
5625 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5626 if (err)
5627 return err;
5628 if (!path_matches)
5629 continue;
5631 err = got_path_basename(&ct_name, pe->path);
5632 if (err)
5633 return err;
5635 if (strcmp(te->name, ct_name) != 0) {
5636 free(ct_name);
5637 continue;
5639 free(ct_name);
5641 *ctp = ct;
5642 break;
5645 return err;
5648 static const struct got_error *
5649 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5650 const char *child_path, const char *path_base_tree,
5651 struct got_pathlist_head *commitable_paths,
5652 got_worktree_status_cb status_cb, void *status_arg,
5653 struct got_repository *repo)
5655 const struct got_error *err = NULL;
5656 struct got_tree_entry *new_te;
5657 char *subtree_path;
5658 struct got_object_id *id = NULL;
5659 int nentries;
5661 *new_tep = NULL;
5663 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5664 got_path_is_root_dir(path_base_tree) ? "" : "/",
5665 child_path) == -1)
5666 return got_error_from_errno("asprintf");
5668 new_te = calloc(1, sizeof(*new_te));
5669 if (new_te == NULL)
5670 return got_error_from_errno("calloc");
5671 new_te->mode = S_IFDIR;
5673 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5674 sizeof(new_te->name)) {
5675 err = got_error(GOT_ERR_NO_SPACE);
5676 goto done;
5678 err = write_tree(&id, &nentries, NULL, subtree_path,
5679 commitable_paths, status_cb, status_arg, repo);
5680 if (err) {
5681 free(new_te);
5682 goto done;
5684 memcpy(&new_te->id, id, sizeof(new_te->id));
5685 done:
5686 free(id);
5687 free(subtree_path);
5688 if (err == NULL)
5689 *new_tep = new_te;
5690 return err;
5693 static const struct got_error *
5694 write_tree(struct got_object_id **new_tree_id, int *nentries,
5695 struct got_tree_object *base_tree, const char *path_base_tree,
5696 struct got_pathlist_head *commitable_paths,
5697 got_worktree_status_cb status_cb, void *status_arg,
5698 struct got_repository *repo)
5700 const struct got_error *err = NULL;
5701 struct got_pathlist_head paths;
5702 struct got_tree_entry *te, *new_te = NULL;
5703 struct got_pathlist_entry *pe;
5705 TAILQ_INIT(&paths);
5706 *nentries = 0;
5708 /* Insert, and recurse into, newly added entries first. */
5709 TAILQ_FOREACH(pe, commitable_paths, entry) {
5710 struct got_commitable *ct = pe->data;
5711 char *child_path = NULL, *slash;
5713 if ((ct->status != GOT_STATUS_ADD &&
5714 ct->staged_status != GOT_STATUS_ADD) ||
5715 (ct->flags & GOT_COMMITABLE_ADDED))
5716 continue;
5718 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5719 strlen(path_base_tree)))
5720 continue;
5722 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5723 ct->in_repo_path);
5724 if (err)
5725 goto done;
5727 slash = strchr(child_path, '/');
5728 if (slash == NULL) {
5729 err = alloc_added_blob_tree_entry(&new_te, ct);
5730 if (err)
5731 goto done;
5732 err = report_ct_status(ct, status_cb, status_arg);
5733 if (err)
5734 goto done;
5735 ct->flags |= GOT_COMMITABLE_ADDED;
5736 err = insert_tree_entry(new_te, &paths);
5737 if (err)
5738 goto done;
5739 (*nentries)++;
5740 } else {
5741 *slash = '\0'; /* trim trailing path components */
5742 if (base_tree == NULL ||
5743 got_object_tree_find_entry(base_tree, child_path)
5744 == NULL) {
5745 err = make_subtree_for_added_blob(&new_te,
5746 child_path, path_base_tree,
5747 commitable_paths, status_cb, status_arg,
5748 repo);
5749 if (err)
5750 goto done;
5751 err = insert_tree_entry(new_te, &paths);
5752 if (err)
5753 goto done;
5754 (*nentries)++;
5759 if (base_tree) {
5760 int i, nbase_entries;
5761 /* Handle modified and deleted entries. */
5762 nbase_entries = got_object_tree_get_nentries(base_tree);
5763 for (i = 0; i < nbase_entries; i++) {
5764 struct got_commitable *ct = NULL;
5766 te = got_object_tree_get_entry(base_tree, i);
5767 if (got_object_tree_entry_is_submodule(te)) {
5768 /* Entry is a submodule; just copy it. */
5769 err = got_object_tree_entry_dup(&new_te, te);
5770 if (err)
5771 goto done;
5772 err = insert_tree_entry(new_te, &paths);
5773 if (err)
5774 goto done;
5775 (*nentries)++;
5776 continue;
5779 if (S_ISDIR(te->mode)) {
5780 int modified;
5781 err = got_object_tree_entry_dup(&new_te, te);
5782 if (err)
5783 goto done;
5784 err = match_modified_subtree(&modified, te,
5785 path_base_tree, commitable_paths);
5786 if (err)
5787 goto done;
5788 /* Avoid recursion into unmodified subtrees. */
5789 if (modified) {
5790 struct got_object_id *new_id;
5791 int nsubentries;
5792 err = write_subtree(&new_id,
5793 &nsubentries, te,
5794 path_base_tree, commitable_paths,
5795 status_cb, status_arg, repo);
5796 if (err)
5797 goto done;
5798 if (nsubentries == 0) {
5799 /* All entries were deleted. */
5800 free(new_id);
5801 continue;
5803 memcpy(&new_te->id, new_id,
5804 sizeof(new_te->id));
5805 free(new_id);
5807 err = insert_tree_entry(new_te, &paths);
5808 if (err)
5809 goto done;
5810 (*nentries)++;
5811 continue;
5814 err = match_deleted_or_modified_ct(&ct, te,
5815 path_base_tree, commitable_paths);
5816 if (err)
5817 goto done;
5818 if (ct) {
5819 /* NB: Deleted entries get dropped here. */
5820 if (ct->status == GOT_STATUS_MODIFY ||
5821 ct->status == GOT_STATUS_MODE_CHANGE ||
5822 ct->status == GOT_STATUS_CONFLICT ||
5823 ct->staged_status == GOT_STATUS_MODIFY) {
5824 err = alloc_modified_blob_tree_entry(
5825 &new_te, te, ct);
5826 if (err)
5827 goto done;
5828 err = insert_tree_entry(new_te, &paths);
5829 if (err)
5830 goto done;
5831 (*nentries)++;
5833 err = report_ct_status(ct, status_cb,
5834 status_arg);
5835 if (err)
5836 goto done;
5837 } else {
5838 /* Entry is unchanged; just copy it. */
5839 err = got_object_tree_entry_dup(&new_te, te);
5840 if (err)
5841 goto done;
5842 err = insert_tree_entry(new_te, &paths);
5843 if (err)
5844 goto done;
5845 (*nentries)++;
5850 /* Write new list of entries; deleted entries have been dropped. */
5851 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5852 done:
5853 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5854 return err;
5857 static const struct got_error *
5858 update_fileindex_after_commit(struct got_worktree *worktree,
5859 struct got_pathlist_head *commitable_paths,
5860 struct got_object_id *new_base_commit_id,
5861 struct got_fileindex *fileindex, int have_staged_files)
5863 const struct got_error *err = NULL;
5864 struct got_pathlist_entry *pe;
5865 char *relpath = NULL;
5867 TAILQ_FOREACH(pe, commitable_paths, entry) {
5868 struct got_fileindex_entry *ie;
5869 struct got_commitable *ct = pe->data;
5871 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5873 err = got_path_skip_common_ancestor(&relpath,
5874 worktree->root_path, ct->ondisk_path);
5875 if (err)
5876 goto done;
5878 if (ie) {
5879 if (ct->status == GOT_STATUS_DELETE ||
5880 ct->staged_status == GOT_STATUS_DELETE) {
5881 got_fileindex_entry_remove(fileindex, ie);
5882 } else if (ct->staged_status == GOT_STATUS_ADD ||
5883 ct->staged_status == GOT_STATUS_MODIFY) {
5884 got_fileindex_entry_stage_set(ie,
5885 GOT_FILEIDX_STAGE_NONE);
5886 got_fileindex_entry_staged_filetype_set(ie, 0);
5888 err = got_fileindex_entry_update(ie,
5889 worktree->root_fd, relpath,
5890 ct->staged_blob_id->sha1,
5891 new_base_commit_id->sha1,
5892 !have_staged_files);
5893 } else
5894 err = got_fileindex_entry_update(ie,
5895 worktree->root_fd, relpath,
5896 ct->blob_id->sha1,
5897 new_base_commit_id->sha1,
5898 !have_staged_files);
5899 } else {
5900 err = got_fileindex_entry_alloc(&ie, pe->path);
5901 if (err)
5902 goto done;
5903 err = got_fileindex_entry_update(ie,
5904 worktree->root_fd, relpath, ct->blob_id->sha1,
5905 new_base_commit_id->sha1, 1);
5906 if (err) {
5907 got_fileindex_entry_free(ie);
5908 goto done;
5910 err = got_fileindex_entry_add(fileindex, ie);
5911 if (err) {
5912 got_fileindex_entry_free(ie);
5913 goto done;
5916 free(relpath);
5917 relpath = NULL;
5919 done:
5920 free(relpath);
5921 return err;
5925 static const struct got_error *
5926 check_out_of_date(const char *in_repo_path, unsigned char status,
5927 unsigned char staged_status, struct got_object_id *base_blob_id,
5928 struct got_object_id *base_commit_id,
5929 struct got_object_id *head_commit_id, struct got_repository *repo,
5930 int ood_errcode)
5932 const struct got_error *err = NULL;
5933 struct got_commit_object *commit = NULL;
5934 struct got_object_id *id = NULL;
5936 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5937 /* Trivial case: base commit == head commit */
5938 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5939 return NULL;
5941 * Ensure file content which local changes were based
5942 * on matches file content in the branch head.
5944 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5945 if (err)
5946 goto done;
5947 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5948 if (err) {
5949 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5950 err = got_error(ood_errcode);
5951 goto done;
5952 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5953 err = got_error(ood_errcode);
5954 } else {
5955 /* Require that added files don't exist in the branch head. */
5956 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5957 if (err)
5958 goto done;
5959 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5960 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5961 goto done;
5962 err = id ? got_error(ood_errcode) : NULL;
5964 done:
5965 free(id);
5966 if (commit)
5967 got_object_commit_close(commit);
5968 return err;
5971 static const struct got_error *
5972 commit_worktree(struct got_object_id **new_commit_id,
5973 struct got_pathlist_head *commitable_paths,
5974 struct got_object_id *head_commit_id,
5975 struct got_object_id *parent_id2,
5976 struct got_worktree *worktree,
5977 const char *author, const char *committer, char *diff_path,
5978 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5979 got_worktree_status_cb status_cb, void *status_arg,
5980 struct got_repository *repo)
5982 const struct got_error *err = NULL, *unlockerr = NULL;
5983 struct got_pathlist_entry *pe;
5984 const char *head_ref_name = NULL;
5985 struct got_commit_object *head_commit = NULL;
5986 struct got_reference *head_ref2 = NULL;
5987 struct got_object_id *head_commit_id2 = NULL;
5988 struct got_tree_object *head_tree = NULL;
5989 struct got_object_id *new_tree_id = NULL;
5990 int nentries, nparents = 0;
5991 struct got_object_id_queue parent_ids;
5992 struct got_object_qid *pid = NULL;
5993 char *logmsg = NULL;
5994 time_t timestamp;
5996 *new_commit_id = NULL;
5998 STAILQ_INIT(&parent_ids);
6000 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6001 if (err)
6002 goto done;
6004 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6005 if (err)
6006 goto done;
6008 if (commit_msg_cb != NULL) {
6009 err = commit_msg_cb(commitable_paths, diff_path,
6010 &logmsg, commit_arg);
6011 if (err)
6012 goto done;
6015 if (logmsg == NULL || strlen(logmsg) == 0) {
6016 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6017 goto done;
6020 /* Create blobs from added and modified files and record their IDs. */
6021 TAILQ_FOREACH(pe, commitable_paths, entry) {
6022 struct got_commitable *ct = pe->data;
6023 char *ondisk_path;
6025 /* Blobs for staged files already exist. */
6026 if (ct->staged_status == GOT_STATUS_ADD ||
6027 ct->staged_status == GOT_STATUS_MODIFY)
6028 continue;
6030 if (ct->status != GOT_STATUS_ADD &&
6031 ct->status != GOT_STATUS_MODIFY &&
6032 ct->status != GOT_STATUS_MODE_CHANGE &&
6033 ct->status != GOT_STATUS_CONFLICT)
6034 continue;
6036 if (asprintf(&ondisk_path, "%s/%s",
6037 worktree->root_path, pe->path) == -1) {
6038 err = got_error_from_errno("asprintf");
6039 goto done;
6041 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6042 free(ondisk_path);
6043 if (err)
6044 goto done;
6047 /* Recursively write new tree objects. */
6048 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6049 commitable_paths, status_cb, status_arg, repo);
6050 if (err)
6051 goto done;
6053 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6054 if (err)
6055 goto done;
6056 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6057 nparents++;
6058 if (parent_id2) {
6059 err = got_object_qid_alloc(&pid, parent_id2);
6060 if (err)
6061 goto done;
6062 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6063 nparents++;
6065 timestamp = time(NULL);
6066 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6067 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6068 if (logmsg != NULL)
6069 free(logmsg);
6070 if (err)
6071 goto done;
6073 /* Check if a concurrent commit to our branch has occurred. */
6074 head_ref_name = got_worktree_get_head_ref_name(worktree);
6075 if (head_ref_name == NULL) {
6076 err = got_error_from_errno("got_worktree_get_head_ref_name");
6077 goto done;
6079 /* Lock the reference here to prevent concurrent modification. */
6080 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6081 if (err)
6082 goto done;
6083 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6084 if (err)
6085 goto done;
6086 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6087 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6088 goto done;
6090 /* Update branch head in repository. */
6091 err = got_ref_change_ref(head_ref2, *new_commit_id);
6092 if (err)
6093 goto done;
6094 err = got_ref_write(head_ref2, repo);
6095 if (err)
6096 goto done;
6098 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6099 if (err)
6100 goto done;
6102 err = ref_base_commit(worktree, repo);
6103 if (err)
6104 goto done;
6105 done:
6106 got_object_id_queue_free(&parent_ids);
6107 if (head_tree)
6108 got_object_tree_close(head_tree);
6109 if (head_commit)
6110 got_object_commit_close(head_commit);
6111 free(head_commit_id2);
6112 if (head_ref2) {
6113 unlockerr = got_ref_unlock(head_ref2);
6114 if (unlockerr && err == NULL)
6115 err = unlockerr;
6116 got_ref_close(head_ref2);
6118 return err;
6121 static const struct got_error *
6122 check_path_is_commitable(const char *path,
6123 struct got_pathlist_head *commitable_paths)
6125 struct got_pathlist_entry *cpe = NULL;
6126 size_t path_len = strlen(path);
6128 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6129 struct got_commitable *ct = cpe->data;
6130 const char *ct_path = ct->path;
6132 while (ct_path[0] == '/')
6133 ct_path++;
6135 if (strcmp(path, ct_path) == 0 ||
6136 got_path_is_child(ct_path, path, path_len))
6137 break;
6140 if (cpe == NULL)
6141 return got_error_path(path, GOT_ERR_BAD_PATH);
6143 return NULL;
6146 static const struct got_error *
6147 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6149 int *have_staged_files = arg;
6151 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6152 *have_staged_files = 1;
6153 return got_error(GOT_ERR_CANCELLED);
6156 return NULL;
6159 static const struct got_error *
6160 check_non_staged_files(struct got_fileindex *fileindex,
6161 struct got_pathlist_head *paths)
6163 struct got_pathlist_entry *pe;
6164 struct got_fileindex_entry *ie;
6166 TAILQ_FOREACH(pe, paths, entry) {
6167 if (pe->path[0] == '\0')
6168 continue;
6169 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6170 if (ie == NULL)
6171 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6172 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6173 return got_error_path(pe->path,
6174 GOT_ERR_FILE_NOT_STAGED);
6177 return NULL;
6180 const struct got_error *
6181 got_worktree_commit(struct got_object_id **new_commit_id,
6182 struct got_worktree *worktree, struct got_pathlist_head *paths,
6183 const char *author, const char *committer, int allow_bad_symlinks,
6184 int show_diff, int commit_conflicts,
6185 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6186 got_worktree_status_cb status_cb, void *status_arg,
6187 struct got_repository *repo)
6189 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6190 struct got_fileindex *fileindex = NULL;
6191 char *fileindex_path = NULL;
6192 struct got_pathlist_head commitable_paths;
6193 struct collect_commitables_arg cc_arg;
6194 struct got_pathlist_entry *pe;
6195 struct got_reference *head_ref = NULL;
6196 struct got_object_id *head_commit_id = NULL;
6197 char *diff_path = NULL;
6198 int have_staged_files = 0;
6200 *new_commit_id = NULL;
6202 memset(&cc_arg, 0, sizeof(cc_arg));
6203 TAILQ_INIT(&commitable_paths);
6205 err = lock_worktree(worktree, LOCK_EX);
6206 if (err)
6207 goto done;
6209 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6210 if (err)
6211 goto done;
6213 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6214 if (err)
6215 goto done;
6217 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6218 if (err)
6219 goto done;
6221 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6222 &have_staged_files);
6223 if (err && err->code != GOT_ERR_CANCELLED)
6224 goto done;
6225 if (have_staged_files) {
6226 err = check_non_staged_files(fileindex, paths);
6227 if (err)
6228 goto done;
6231 cc_arg.commitable_paths = &commitable_paths;
6232 cc_arg.worktree = worktree;
6233 cc_arg.fileindex = fileindex;
6234 cc_arg.repo = repo;
6235 cc_arg.have_staged_files = have_staged_files;
6236 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6237 cc_arg.diff_header_shown = 0;
6238 cc_arg.commit_conflicts = commit_conflicts;
6239 if (show_diff) {
6240 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6241 GOT_TMPDIR_STR "/got", ".diff");
6242 if (err)
6243 goto done;
6244 cc_arg.f1 = got_opentemp();
6245 if (cc_arg.f1 == NULL) {
6246 err = got_error_from_errno("got_opentemp");
6247 goto done;
6249 cc_arg.f2 = got_opentemp();
6250 if (cc_arg.f2 == NULL) {
6251 err = got_error_from_errno("got_opentemp");
6252 goto done;
6256 TAILQ_FOREACH(pe, paths, entry) {
6257 err = worktree_status(worktree, pe->path, fileindex, repo,
6258 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6259 if (err)
6260 goto done;
6263 if (show_diff) {
6264 if (fflush(cc_arg.diff_outfile) == EOF) {
6265 err = got_error_from_errno("fflush");
6266 goto done;
6270 if (TAILQ_EMPTY(&commitable_paths)) {
6271 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6272 goto done;
6275 TAILQ_FOREACH(pe, paths, entry) {
6276 err = check_path_is_commitable(pe->path, &commitable_paths);
6277 if (err)
6278 goto done;
6281 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6282 struct got_commitable *ct = pe->data;
6283 const char *ct_path = ct->in_repo_path;
6285 while (ct_path[0] == '/')
6286 ct_path++;
6287 err = check_out_of_date(ct_path, ct->status,
6288 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6289 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6290 if (err)
6291 goto done;
6295 err = commit_worktree(new_commit_id, &commitable_paths,
6296 head_commit_id, NULL, worktree, author, committer,
6297 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6298 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6299 if (err)
6300 goto done;
6302 err = update_fileindex_after_commit(worktree, &commitable_paths,
6303 *new_commit_id, fileindex, have_staged_files);
6304 sync_err = sync_fileindex(fileindex, fileindex_path);
6305 if (sync_err && err == NULL)
6306 err = sync_err;
6307 done:
6308 if (fileindex)
6309 got_fileindex_free(fileindex);
6310 free(fileindex_path);
6311 unlockerr = lock_worktree(worktree, LOCK_SH);
6312 if (unlockerr && err == NULL)
6313 err = unlockerr;
6314 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6315 struct got_commitable *ct = pe->data;
6317 free_commitable(ct);
6319 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6320 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6321 err = got_error_from_errno2("unlink", diff_path);
6322 free(diff_path);
6323 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6324 err == NULL)
6325 err = got_error_from_errno("fclose");
6326 return err;
6329 const char *
6330 got_commitable_get_path(struct got_commitable *ct)
6332 return ct->path;
6335 unsigned int
6336 got_commitable_get_status(struct got_commitable *ct)
6338 return ct->status;
6341 struct check_rebase_ok_arg {
6342 struct got_worktree *worktree;
6343 struct got_repository *repo;
6346 static const struct got_error *
6347 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6349 const struct got_error *err = NULL;
6350 struct check_rebase_ok_arg *a = arg;
6351 unsigned char status;
6352 struct stat sb;
6353 char *ondisk_path;
6355 /* Reject rebase of a work tree with mixed base commits. */
6356 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6357 SHA1_DIGEST_LENGTH))
6358 return got_error(GOT_ERR_MIXED_COMMITS);
6360 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6361 == -1)
6362 return got_error_from_errno("asprintf");
6364 /* Reject rebase of a work tree with modified or staged files. */
6365 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6366 free(ondisk_path);
6367 if (err)
6368 return err;
6370 if (status != GOT_STATUS_NO_CHANGE)
6371 return got_error(GOT_ERR_MODIFIED);
6372 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6373 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6375 return NULL;
6378 const struct got_error *
6379 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6380 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6381 struct got_worktree *worktree, struct got_reference *branch,
6382 struct got_repository *repo)
6384 const struct got_error *err = NULL;
6385 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6386 char *branch_ref_name = NULL;
6387 char *fileindex_path = NULL;
6388 struct check_rebase_ok_arg ok_arg;
6389 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6390 struct got_object_id *wt_branch_tip = NULL;
6392 *new_base_branch_ref = NULL;
6393 *tmp_branch = NULL;
6394 *fileindex = NULL;
6396 err = lock_worktree(worktree, LOCK_EX);
6397 if (err)
6398 return err;
6400 err = open_fileindex(fileindex, &fileindex_path, worktree);
6401 if (err)
6402 goto done;
6404 ok_arg.worktree = worktree;
6405 ok_arg.repo = repo;
6406 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6407 &ok_arg);
6408 if (err)
6409 goto done;
6411 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6412 if (err)
6413 goto done;
6415 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6416 if (err)
6417 goto done;
6419 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6420 if (err)
6421 goto done;
6423 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6424 0);
6425 if (err)
6426 goto done;
6428 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6429 if (err)
6430 goto done;
6431 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6432 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6433 goto done;
6436 err = got_ref_alloc_symref(new_base_branch_ref,
6437 new_base_branch_ref_name, wt_branch);
6438 if (err)
6439 goto done;
6440 err = got_ref_write(*new_base_branch_ref, repo);
6441 if (err)
6442 goto done;
6444 /* TODO Lock original branch's ref while rebasing? */
6446 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6447 if (err)
6448 goto done;
6450 err = got_ref_write(branch_ref, repo);
6451 if (err)
6452 goto done;
6454 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6455 worktree->base_commit_id);
6456 if (err)
6457 goto done;
6458 err = got_ref_write(*tmp_branch, repo);
6459 if (err)
6460 goto done;
6462 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6463 if (err)
6464 goto done;
6465 done:
6466 free(fileindex_path);
6467 free(tmp_branch_name);
6468 free(new_base_branch_ref_name);
6469 free(branch_ref_name);
6470 if (branch_ref)
6471 got_ref_close(branch_ref);
6472 if (wt_branch)
6473 got_ref_close(wt_branch);
6474 free(wt_branch_tip);
6475 if (err) {
6476 if (*new_base_branch_ref) {
6477 got_ref_close(*new_base_branch_ref);
6478 *new_base_branch_ref = NULL;
6480 if (*tmp_branch) {
6481 got_ref_close(*tmp_branch);
6482 *tmp_branch = NULL;
6484 if (*fileindex) {
6485 got_fileindex_free(*fileindex);
6486 *fileindex = NULL;
6488 lock_worktree(worktree, LOCK_SH);
6490 return err;
6493 const struct got_error *
6494 got_worktree_rebase_continue(struct got_object_id **commit_id,
6495 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6496 struct got_reference **branch, struct got_fileindex **fileindex,
6497 struct got_worktree *worktree, struct got_repository *repo)
6499 const struct got_error *err;
6500 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6501 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6502 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6503 char *fileindex_path = NULL;
6504 int have_staged_files = 0;
6506 *commit_id = NULL;
6507 *new_base_branch = NULL;
6508 *tmp_branch = NULL;
6509 *branch = NULL;
6510 *fileindex = NULL;
6512 err = lock_worktree(worktree, LOCK_EX);
6513 if (err)
6514 return err;
6516 err = open_fileindex(fileindex, &fileindex_path, worktree);
6517 if (err)
6518 goto done;
6520 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6521 &have_staged_files);
6522 if (err && err->code != GOT_ERR_CANCELLED)
6523 goto done;
6524 if (have_staged_files) {
6525 err = got_error(GOT_ERR_STAGED_PATHS);
6526 goto done;
6529 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6530 if (err)
6531 goto done;
6533 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6534 if (err)
6535 goto done;
6537 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6538 if (err)
6539 goto done;
6541 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6542 if (err)
6543 goto done;
6545 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6546 if (err)
6547 goto done;
6549 err = got_ref_open(branch, repo,
6550 got_ref_get_symref_target(branch_ref), 0);
6551 if (err)
6552 goto done;
6554 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6555 if (err)
6556 goto done;
6558 err = got_ref_resolve(commit_id, repo, commit_ref);
6559 if (err)
6560 goto done;
6562 err = got_ref_open(new_base_branch, repo,
6563 new_base_branch_ref_name, 0);
6564 if (err)
6565 goto done;
6567 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6568 if (err)
6569 goto done;
6570 done:
6571 free(commit_ref_name);
6572 free(branch_ref_name);
6573 free(fileindex_path);
6574 if (commit_ref)
6575 got_ref_close(commit_ref);
6576 if (branch_ref)
6577 got_ref_close(branch_ref);
6578 if (err) {
6579 free(*commit_id);
6580 *commit_id = NULL;
6581 if (*tmp_branch) {
6582 got_ref_close(*tmp_branch);
6583 *tmp_branch = NULL;
6585 if (*new_base_branch) {
6586 got_ref_close(*new_base_branch);
6587 *new_base_branch = NULL;
6589 if (*branch) {
6590 got_ref_close(*branch);
6591 *branch = NULL;
6593 if (*fileindex) {
6594 got_fileindex_free(*fileindex);
6595 *fileindex = NULL;
6597 lock_worktree(worktree, LOCK_SH);
6599 return err;
6602 const struct got_error *
6603 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6605 const struct got_error *err;
6606 char *tmp_branch_name = NULL;
6608 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6609 if (err)
6610 return err;
6612 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6613 free(tmp_branch_name);
6614 return NULL;
6617 static const struct got_error *
6618 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6619 const char *diff_path, char **logmsg, void *arg)
6621 *logmsg = arg;
6622 return NULL;
6625 static const struct got_error *
6626 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6627 const char *path, struct got_object_id *blob_id,
6628 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6629 int dirfd, const char *de_name)
6631 return NULL;
6634 struct collect_merged_paths_arg {
6635 got_worktree_checkout_cb progress_cb;
6636 void *progress_arg;
6637 struct got_pathlist_head *merged_paths;
6640 static const struct got_error *
6641 collect_merged_paths(void *arg, unsigned char status, const char *path)
6643 const struct got_error *err;
6644 struct collect_merged_paths_arg *a = arg;
6645 char *p;
6646 struct got_pathlist_entry *new;
6648 err = (*a->progress_cb)(a->progress_arg, status, path);
6649 if (err)
6650 return err;
6652 if (status != GOT_STATUS_MERGE &&
6653 status != GOT_STATUS_ADD &&
6654 status != GOT_STATUS_DELETE &&
6655 status != GOT_STATUS_CONFLICT)
6656 return NULL;
6658 p = strdup(path);
6659 if (p == NULL)
6660 return got_error_from_errno("strdup");
6662 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6663 if (err || new == NULL)
6664 free(p);
6665 return err;
6668 static const struct got_error *
6669 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6670 int is_rebase, struct got_repository *repo)
6672 const struct got_error *err;
6673 struct got_reference *commit_ref = NULL;
6675 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6676 if (err) {
6677 if (err->code != GOT_ERR_NOT_REF)
6678 goto done;
6679 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6680 if (err)
6681 goto done;
6682 err = got_ref_write(commit_ref, repo);
6683 if (err)
6684 goto done;
6685 } else if (is_rebase) {
6686 struct got_object_id *stored_id;
6687 int cmp;
6689 err = got_ref_resolve(&stored_id, repo, commit_ref);
6690 if (err)
6691 goto done;
6692 cmp = got_object_id_cmp(commit_id, stored_id);
6693 free(stored_id);
6694 if (cmp != 0) {
6695 err = got_error(GOT_ERR_REBASE_COMMITID);
6696 goto done;
6699 done:
6700 if (commit_ref)
6701 got_ref_close(commit_ref);
6702 return err;
6705 static const struct got_error *
6706 rebase_merge_files(struct got_pathlist_head *merged_paths,
6707 const char *commit_ref_name, struct got_worktree *worktree,
6708 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6709 struct got_object_id *commit_id, struct got_repository *repo,
6710 got_worktree_checkout_cb progress_cb, void *progress_arg,
6711 got_cancel_cb cancel_cb, void *cancel_arg)
6713 const struct got_error *err;
6714 struct got_reference *commit_ref = NULL;
6715 struct collect_merged_paths_arg cmp_arg;
6716 char *fileindex_path;
6718 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6720 err = get_fileindex_path(&fileindex_path, worktree);
6721 if (err)
6722 return err;
6724 cmp_arg.progress_cb = progress_cb;
6725 cmp_arg.progress_arg = progress_arg;
6726 cmp_arg.merged_paths = merged_paths;
6727 err = merge_files(worktree, fileindex, fileindex_path,
6728 parent_commit_id, commit_id, repo, collect_merged_paths,
6729 &cmp_arg, cancel_cb, cancel_arg);
6730 if (commit_ref)
6731 got_ref_close(commit_ref);
6732 return err;
6735 const struct got_error *
6736 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6737 struct got_worktree *worktree, struct got_fileindex *fileindex,
6738 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6739 struct got_repository *repo,
6740 got_worktree_checkout_cb progress_cb, void *progress_arg,
6741 got_cancel_cb cancel_cb, void *cancel_arg)
6743 const struct got_error *err;
6744 char *commit_ref_name;
6746 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6747 if (err)
6748 return err;
6750 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6751 if (err)
6752 goto done;
6754 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6755 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6756 progress_arg, cancel_cb, cancel_arg);
6757 done:
6758 free(commit_ref_name);
6759 return err;
6762 const struct got_error *
6763 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6764 struct got_worktree *worktree, struct got_fileindex *fileindex,
6765 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6766 struct got_repository *repo,
6767 got_worktree_checkout_cb progress_cb, void *progress_arg,
6768 got_cancel_cb cancel_cb, void *cancel_arg)
6770 const struct got_error *err;
6771 char *commit_ref_name;
6773 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6774 if (err)
6775 return err;
6777 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6778 if (err)
6779 goto done;
6781 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6782 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6783 progress_arg, cancel_cb, cancel_arg);
6784 done:
6785 free(commit_ref_name);
6786 return err;
6789 static const struct got_error *
6790 rebase_commit(struct got_object_id **new_commit_id,
6791 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6792 struct got_worktree *worktree, struct got_fileindex *fileindex,
6793 struct got_reference *tmp_branch, const char *committer,
6794 struct got_commit_object *orig_commit, const char *new_logmsg,
6795 int allow_conflict, struct got_repository *repo)
6797 const struct got_error *err, *sync_err;
6798 struct got_pathlist_head commitable_paths;
6799 struct collect_commitables_arg cc_arg;
6800 char *fileindex_path = NULL;
6801 struct got_reference *head_ref = NULL;
6802 struct got_object_id *head_commit_id = NULL;
6803 char *logmsg = NULL;
6805 memset(&cc_arg, 0, sizeof(cc_arg));
6806 TAILQ_INIT(&commitable_paths);
6807 *new_commit_id = NULL;
6809 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6811 err = get_fileindex_path(&fileindex_path, worktree);
6812 if (err)
6813 return err;
6815 cc_arg.commitable_paths = &commitable_paths;
6816 cc_arg.worktree = worktree;
6817 cc_arg.repo = repo;
6818 cc_arg.have_staged_files = 0;
6819 cc_arg.commit_conflicts = allow_conflict;
6821 * If possible get the status of individual files directly to
6822 * avoid crawling the entire work tree once per rebased commit.
6824 * Ideally, merged_paths would contain a list of commitables
6825 * we could use so we could skip worktree_status() entirely.
6826 * However, we would then need carefully keep track of cumulative
6827 * effects of operations such as file additions and deletions
6828 * in 'got histedit -f' (folding multiple commits into one),
6829 * and this extra complexity is not really worth it.
6831 if (merged_paths) {
6832 struct got_pathlist_entry *pe;
6833 TAILQ_FOREACH(pe, merged_paths, entry) {
6834 err = worktree_status(worktree, pe->path, fileindex,
6835 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6836 0);
6837 if (err)
6838 goto done;
6840 } else {
6841 err = worktree_status(worktree, "", fileindex, repo,
6842 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6843 if (err)
6844 goto done;
6847 if (TAILQ_EMPTY(&commitable_paths)) {
6848 /* No-op change; commit will be elided. */
6849 err = got_ref_delete(commit_ref, repo);
6850 if (err)
6851 goto done;
6852 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6853 goto done;
6856 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6857 if (err)
6858 goto done;
6860 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6861 if (err)
6862 goto done;
6864 if (new_logmsg) {
6865 logmsg = strdup(new_logmsg);
6866 if (logmsg == NULL) {
6867 err = got_error_from_errno("strdup");
6868 goto done;
6870 } else {
6871 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6872 if (err)
6873 goto done;
6876 /* NB: commit_worktree will call free(logmsg) */
6877 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6878 NULL, worktree, got_object_commit_get_author(orig_commit),
6879 committer ? committer :
6880 got_object_commit_get_committer(orig_commit), NULL,
6881 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6882 if (err)
6883 goto done;
6885 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6886 if (err)
6887 goto done;
6889 err = got_ref_delete(commit_ref, repo);
6890 if (err)
6891 goto done;
6893 err = update_fileindex_after_commit(worktree, &commitable_paths,
6894 *new_commit_id, fileindex, 0);
6895 sync_err = sync_fileindex(fileindex, fileindex_path);
6896 if (sync_err && err == NULL)
6897 err = sync_err;
6898 done:
6899 free(fileindex_path);
6900 free(head_commit_id);
6901 if (head_ref)
6902 got_ref_close(head_ref);
6903 if (err) {
6904 free(*new_commit_id);
6905 *new_commit_id = NULL;
6907 return err;
6910 const struct got_error *
6911 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6912 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6913 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6914 const char *committer, struct got_commit_object *orig_commit,
6915 struct got_object_id *orig_commit_id, int allow_conflict,
6916 struct got_repository *repo)
6918 const struct got_error *err;
6919 char *commit_ref_name;
6920 struct got_reference *commit_ref = NULL;
6921 struct got_object_id *commit_id = NULL;
6923 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6924 if (err)
6925 return err;
6927 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6928 if (err)
6929 goto done;
6930 err = got_ref_resolve(&commit_id, repo, commit_ref);
6931 if (err)
6932 goto done;
6933 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6934 err = got_error(GOT_ERR_REBASE_COMMITID);
6935 goto done;
6938 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6939 worktree, fileindex, tmp_branch, committer, orig_commit,
6940 NULL, allow_conflict, repo);
6941 done:
6942 if (commit_ref)
6943 got_ref_close(commit_ref);
6944 free(commit_ref_name);
6945 free(commit_id);
6946 return err;
6949 const struct got_error *
6950 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6951 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6952 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6953 const char *committer, struct got_commit_object *orig_commit,
6954 struct got_object_id *orig_commit_id, const char *new_logmsg,
6955 int allow_conflict, struct got_repository *repo)
6957 const struct got_error *err;
6958 char *commit_ref_name;
6959 struct got_reference *commit_ref = NULL;
6961 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6962 if (err)
6963 return err;
6965 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6966 if (err)
6967 goto done;
6969 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6970 worktree, fileindex, tmp_branch, committer, orig_commit,
6971 new_logmsg, allow_conflict, repo);
6972 done:
6973 if (commit_ref)
6974 got_ref_close(commit_ref);
6975 free(commit_ref_name);
6976 return err;
6979 const struct got_error *
6980 got_worktree_rebase_postpone(struct got_worktree *worktree,
6981 struct got_fileindex *fileindex)
6983 if (fileindex)
6984 got_fileindex_free(fileindex);
6985 return lock_worktree(worktree, LOCK_SH);
6988 static const struct got_error *
6989 delete_ref(const char *name, struct got_repository *repo)
6991 const struct got_error *err;
6992 struct got_reference *ref;
6994 err = got_ref_open(&ref, repo, name, 0);
6995 if (err) {
6996 if (err->code == GOT_ERR_NOT_REF)
6997 return NULL;
6998 return err;
7001 err = got_ref_delete(ref, repo);
7002 got_ref_close(ref);
7003 return err;
7006 static const struct got_error *
7007 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7009 const struct got_error *err;
7010 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7011 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7013 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7014 if (err)
7015 goto done;
7016 err = delete_ref(tmp_branch_name, repo);
7017 if (err)
7018 goto done;
7020 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7021 if (err)
7022 goto done;
7023 err = delete_ref(new_base_branch_ref_name, repo);
7024 if (err)
7025 goto done;
7027 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7028 if (err)
7029 goto done;
7030 err = delete_ref(branch_ref_name, repo);
7031 if (err)
7032 goto done;
7034 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7035 if (err)
7036 goto done;
7037 err = delete_ref(commit_ref_name, repo);
7038 if (err)
7039 goto done;
7041 done:
7042 free(tmp_branch_name);
7043 free(new_base_branch_ref_name);
7044 free(branch_ref_name);
7045 free(commit_ref_name);
7046 return err;
7049 static const struct got_error *
7050 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7051 struct got_object_id *new_commit_id, struct got_repository *repo)
7053 const struct got_error *err;
7054 struct got_reference *ref = NULL;
7055 struct got_object_id *old_commit_id = NULL;
7056 const char *branch_name = NULL;
7057 char *new_id_str = NULL;
7058 char *refname = NULL;
7060 branch_name = got_ref_get_name(branch);
7061 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7062 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7063 branch_name += 11;
7065 err = got_object_id_str(&new_id_str, new_commit_id);
7066 if (err)
7067 return err;
7069 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7070 new_id_str) == -1) {
7071 err = got_error_from_errno("asprintf");
7072 goto done;
7075 err = got_ref_resolve(&old_commit_id, repo, branch);
7076 if (err)
7077 goto done;
7079 err = got_ref_alloc(&ref, refname, old_commit_id);
7080 if (err)
7081 goto done;
7083 err = got_ref_write(ref, repo);
7084 done:
7085 free(new_id_str);
7086 free(refname);
7087 free(old_commit_id);
7088 if (ref)
7089 got_ref_close(ref);
7090 return err;
7093 const struct got_error *
7094 got_worktree_rebase_complete(struct got_worktree *worktree,
7095 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7096 struct got_reference *rebased_branch, struct got_repository *repo,
7097 int create_backup)
7099 const struct got_error *err, *unlockerr, *sync_err;
7100 struct got_object_id *new_head_commit_id = NULL;
7101 char *fileindex_path = NULL;
7103 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7104 if (err)
7105 return err;
7107 if (create_backup) {
7108 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7109 rebased_branch, new_head_commit_id, repo);
7110 if (err)
7111 goto done;
7114 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7115 if (err)
7116 goto done;
7118 err = got_ref_write(rebased_branch, repo);
7119 if (err)
7120 goto done;
7122 err = got_worktree_set_head_ref(worktree, rebased_branch);
7123 if (err)
7124 goto done;
7126 err = delete_rebase_refs(worktree, repo);
7127 if (err)
7128 goto done;
7130 err = get_fileindex_path(&fileindex_path, worktree);
7131 if (err)
7132 goto done;
7133 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7134 sync_err = sync_fileindex(fileindex, fileindex_path);
7135 if (sync_err && err == NULL)
7136 err = sync_err;
7137 done:
7138 got_fileindex_free(fileindex);
7139 free(fileindex_path);
7140 free(new_head_commit_id);
7141 unlockerr = lock_worktree(worktree, LOCK_SH);
7142 if (unlockerr && err == NULL)
7143 err = unlockerr;
7144 return err;
7147 const struct got_error *
7148 got_worktree_rebase_abort(struct got_worktree *worktree,
7149 struct got_fileindex *fileindex, struct got_repository *repo,
7150 struct got_reference *new_base_branch,
7151 got_worktree_checkout_cb progress_cb, void *progress_arg)
7153 const struct got_error *err, *unlockerr, *sync_err;
7154 struct got_reference *resolved = NULL;
7155 struct got_object_id *commit_id = NULL;
7156 struct got_commit_object *commit = NULL;
7157 char *fileindex_path = NULL;
7158 struct revert_file_args rfa;
7159 struct got_object_id *tree_id = NULL;
7161 err = lock_worktree(worktree, LOCK_EX);
7162 if (err)
7163 return err;
7165 err = got_object_open_as_commit(&commit, repo,
7166 worktree->base_commit_id);
7167 if (err)
7168 goto done;
7170 err = got_ref_open(&resolved, repo,
7171 got_ref_get_symref_target(new_base_branch), 0);
7172 if (err)
7173 goto done;
7175 err = got_worktree_set_head_ref(worktree, resolved);
7176 if (err)
7177 goto done;
7180 * XXX commits to the base branch could have happened while
7181 * we were busy rebasing; should we store the original commit ID
7182 * when rebase begins and read it back here?
7184 err = got_ref_resolve(&commit_id, repo, resolved);
7185 if (err)
7186 goto done;
7188 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7189 if (err)
7190 goto done;
7192 err = got_object_id_by_path(&tree_id, repo, commit,
7193 worktree->path_prefix);
7194 if (err)
7195 goto done;
7197 err = delete_rebase_refs(worktree, repo);
7198 if (err)
7199 goto done;
7201 err = get_fileindex_path(&fileindex_path, worktree);
7202 if (err)
7203 goto done;
7205 rfa.worktree = worktree;
7206 rfa.fileindex = fileindex;
7207 rfa.progress_cb = progress_cb;
7208 rfa.progress_arg = progress_arg;
7209 rfa.patch_cb = NULL;
7210 rfa.patch_arg = NULL;
7211 rfa.repo = repo;
7212 rfa.unlink_added_files = 0;
7213 err = worktree_status(worktree, "", fileindex, repo,
7214 revert_file, &rfa, NULL, NULL, 1, 0);
7215 if (err)
7216 goto sync;
7218 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7219 repo, progress_cb, progress_arg, NULL, NULL);
7220 sync:
7221 sync_err = sync_fileindex(fileindex, fileindex_path);
7222 if (sync_err && err == NULL)
7223 err = sync_err;
7224 done:
7225 got_ref_close(resolved);
7226 free(tree_id);
7227 free(commit_id);
7228 if (commit)
7229 got_object_commit_close(commit);
7230 if (fileindex)
7231 got_fileindex_free(fileindex);
7232 free(fileindex_path);
7234 unlockerr = lock_worktree(worktree, LOCK_SH);
7235 if (unlockerr && err == NULL)
7236 err = unlockerr;
7237 return err;
7240 const struct got_error *
7241 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7242 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7243 struct got_fileindex **fileindex, struct got_worktree *worktree,
7244 struct got_repository *repo)
7246 const struct got_error *err = NULL;
7247 char *tmp_branch_name = NULL;
7248 char *branch_ref_name = NULL;
7249 char *base_commit_ref_name = NULL;
7250 char *fileindex_path = NULL;
7251 struct check_rebase_ok_arg ok_arg;
7252 struct got_reference *wt_branch = NULL;
7253 struct got_reference *base_commit_ref = NULL;
7255 *tmp_branch = NULL;
7256 *branch_ref = NULL;
7257 *base_commit_id = NULL;
7258 *fileindex = NULL;
7260 err = lock_worktree(worktree, LOCK_EX);
7261 if (err)
7262 return err;
7264 err = open_fileindex(fileindex, &fileindex_path, worktree);
7265 if (err)
7266 goto done;
7268 ok_arg.worktree = worktree;
7269 ok_arg.repo = repo;
7270 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7271 &ok_arg);
7272 if (err)
7273 goto done;
7275 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7276 if (err)
7277 goto done;
7279 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7280 if (err)
7281 goto done;
7283 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7284 worktree);
7285 if (err)
7286 goto done;
7288 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7289 0);
7290 if (err)
7291 goto done;
7293 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7294 if (err)
7295 goto done;
7297 err = got_ref_write(*branch_ref, repo);
7298 if (err)
7299 goto done;
7301 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7302 worktree->base_commit_id);
7303 if (err)
7304 goto done;
7305 err = got_ref_write(base_commit_ref, repo);
7306 if (err)
7307 goto done;
7308 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7309 if (*base_commit_id == NULL) {
7310 err = got_error_from_errno("got_object_id_dup");
7311 goto done;
7314 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7315 worktree->base_commit_id);
7316 if (err)
7317 goto done;
7318 err = got_ref_write(*tmp_branch, repo);
7319 if (err)
7320 goto done;
7322 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7323 if (err)
7324 goto done;
7325 done:
7326 free(fileindex_path);
7327 free(tmp_branch_name);
7328 free(branch_ref_name);
7329 free(base_commit_ref_name);
7330 if (wt_branch)
7331 got_ref_close(wt_branch);
7332 if (err) {
7333 if (*branch_ref) {
7334 got_ref_close(*branch_ref);
7335 *branch_ref = NULL;
7337 if (*tmp_branch) {
7338 got_ref_close(*tmp_branch);
7339 *tmp_branch = NULL;
7341 free(*base_commit_id);
7342 if (*fileindex) {
7343 got_fileindex_free(*fileindex);
7344 *fileindex = NULL;
7346 lock_worktree(worktree, LOCK_SH);
7348 return err;
7351 const struct got_error *
7352 got_worktree_histedit_postpone(struct got_worktree *worktree,
7353 struct got_fileindex *fileindex)
7355 if (fileindex)
7356 got_fileindex_free(fileindex);
7357 return lock_worktree(worktree, LOCK_SH);
7360 const struct got_error *
7361 got_worktree_histedit_in_progress(int *in_progress,
7362 struct got_worktree *worktree)
7364 const struct got_error *err;
7365 char *tmp_branch_name = NULL;
7367 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7368 if (err)
7369 return err;
7371 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7372 free(tmp_branch_name);
7373 return NULL;
7376 const struct got_error *
7377 got_worktree_histedit_continue(struct got_object_id **commit_id,
7378 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7379 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7380 struct got_worktree *worktree, struct got_repository *repo)
7382 const struct got_error *err;
7383 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7384 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7385 struct got_reference *commit_ref = NULL;
7386 struct got_reference *base_commit_ref = NULL;
7387 char *fileindex_path = NULL;
7388 int have_staged_files = 0;
7390 *commit_id = NULL;
7391 *tmp_branch = NULL;
7392 *base_commit_id = NULL;
7393 *fileindex = NULL;
7395 err = lock_worktree(worktree, LOCK_EX);
7396 if (err)
7397 return err;
7399 err = open_fileindex(fileindex, &fileindex_path, worktree);
7400 if (err)
7401 goto done;
7403 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7404 &have_staged_files);
7405 if (err && err->code != GOT_ERR_CANCELLED)
7406 goto done;
7407 if (have_staged_files) {
7408 err = got_error(GOT_ERR_STAGED_PATHS);
7409 goto done;
7412 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7413 if (err)
7414 goto done;
7416 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7417 if (err)
7418 goto done;
7420 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7421 if (err)
7422 goto done;
7424 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7425 worktree);
7426 if (err)
7427 goto done;
7429 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7430 if (err)
7431 goto done;
7433 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7434 if (err)
7435 goto done;
7436 err = got_ref_resolve(commit_id, repo, commit_ref);
7437 if (err)
7438 goto done;
7440 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7441 if (err)
7442 goto done;
7443 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7444 if (err)
7445 goto done;
7447 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7448 if (err)
7449 goto done;
7450 done:
7451 free(commit_ref_name);
7452 free(branch_ref_name);
7453 free(fileindex_path);
7454 if (commit_ref)
7455 got_ref_close(commit_ref);
7456 if (base_commit_ref)
7457 got_ref_close(base_commit_ref);
7458 if (err) {
7459 free(*commit_id);
7460 *commit_id = NULL;
7461 free(*base_commit_id);
7462 *base_commit_id = NULL;
7463 if (*tmp_branch) {
7464 got_ref_close(*tmp_branch);
7465 *tmp_branch = NULL;
7467 if (*fileindex) {
7468 got_fileindex_free(*fileindex);
7469 *fileindex = NULL;
7471 lock_worktree(worktree, LOCK_EX);
7473 return err;
7476 static const struct got_error *
7477 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7479 const struct got_error *err;
7480 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7481 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7483 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7484 if (err)
7485 goto done;
7486 err = delete_ref(tmp_branch_name, repo);
7487 if (err)
7488 goto done;
7490 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7491 worktree);
7492 if (err)
7493 goto done;
7494 err = delete_ref(base_commit_ref_name, repo);
7495 if (err)
7496 goto done;
7498 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7499 if (err)
7500 goto done;
7501 err = delete_ref(branch_ref_name, repo);
7502 if (err)
7503 goto done;
7505 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7506 if (err)
7507 goto done;
7508 err = delete_ref(commit_ref_name, repo);
7509 if (err)
7510 goto done;
7511 done:
7512 free(tmp_branch_name);
7513 free(base_commit_ref_name);
7514 free(branch_ref_name);
7515 free(commit_ref_name);
7516 return err;
7519 const struct got_error *
7520 got_worktree_histedit_abort(struct got_worktree *worktree,
7521 struct got_fileindex *fileindex, struct got_repository *repo,
7522 struct got_reference *branch, struct got_object_id *base_commit_id,
7523 got_worktree_checkout_cb progress_cb, void *progress_arg)
7525 const struct got_error *err, *unlockerr, *sync_err;
7526 struct got_reference *resolved = NULL;
7527 char *fileindex_path = NULL;
7528 struct got_commit_object *commit = NULL;
7529 struct got_object_id *tree_id = NULL;
7530 struct revert_file_args rfa;
7532 err = lock_worktree(worktree, LOCK_EX);
7533 if (err)
7534 return err;
7536 err = got_object_open_as_commit(&commit, repo,
7537 worktree->base_commit_id);
7538 if (err)
7539 goto done;
7541 err = got_ref_open(&resolved, repo,
7542 got_ref_get_symref_target(branch), 0);
7543 if (err)
7544 goto done;
7546 err = got_worktree_set_head_ref(worktree, resolved);
7547 if (err)
7548 goto done;
7550 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7551 if (err)
7552 goto done;
7554 err = got_object_id_by_path(&tree_id, repo, commit,
7555 worktree->path_prefix);
7556 if (err)
7557 goto done;
7559 err = delete_histedit_refs(worktree, repo);
7560 if (err)
7561 goto done;
7563 err = get_fileindex_path(&fileindex_path, worktree);
7564 if (err)
7565 goto done;
7567 rfa.worktree = worktree;
7568 rfa.fileindex = fileindex;
7569 rfa.progress_cb = progress_cb;
7570 rfa.progress_arg = progress_arg;
7571 rfa.patch_cb = NULL;
7572 rfa.patch_arg = NULL;
7573 rfa.repo = repo;
7574 rfa.unlink_added_files = 0;
7575 err = worktree_status(worktree, "", fileindex, repo,
7576 revert_file, &rfa, NULL, NULL, 1, 0);
7577 if (err)
7578 goto sync;
7580 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7581 repo, progress_cb, progress_arg, NULL, NULL);
7582 sync:
7583 sync_err = sync_fileindex(fileindex, fileindex_path);
7584 if (sync_err && err == NULL)
7585 err = sync_err;
7586 done:
7587 got_ref_close(resolved);
7588 free(tree_id);
7589 free(fileindex_path);
7591 unlockerr = lock_worktree(worktree, LOCK_SH);
7592 if (unlockerr && err == NULL)
7593 err = unlockerr;
7594 return err;
7597 const struct got_error *
7598 got_worktree_histedit_complete(struct got_worktree *worktree,
7599 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7600 struct got_reference *edited_branch, struct got_repository *repo)
7602 const struct got_error *err, *unlockerr, *sync_err;
7603 struct got_object_id *new_head_commit_id = NULL;
7604 struct got_reference *resolved = NULL;
7605 char *fileindex_path = NULL;
7607 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7608 if (err)
7609 return err;
7611 err = got_ref_open(&resolved, repo,
7612 got_ref_get_symref_target(edited_branch), 0);
7613 if (err)
7614 goto done;
7616 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7617 resolved, new_head_commit_id, repo);
7618 if (err)
7619 goto done;
7621 err = got_ref_change_ref(resolved, new_head_commit_id);
7622 if (err)
7623 goto done;
7625 err = got_ref_write(resolved, repo);
7626 if (err)
7627 goto done;
7629 err = got_worktree_set_head_ref(worktree, resolved);
7630 if (err)
7631 goto done;
7633 err = delete_histedit_refs(worktree, repo);
7634 if (err)
7635 goto done;
7637 err = get_fileindex_path(&fileindex_path, worktree);
7638 if (err)
7639 goto done;
7640 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7641 sync_err = sync_fileindex(fileindex, fileindex_path);
7642 if (sync_err && err == NULL)
7643 err = sync_err;
7644 done:
7645 got_fileindex_free(fileindex);
7646 free(fileindex_path);
7647 free(new_head_commit_id);
7648 unlockerr = lock_worktree(worktree, LOCK_SH);
7649 if (unlockerr && err == NULL)
7650 err = unlockerr;
7651 return err;
7654 const struct got_error *
7655 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7656 struct got_object_id *commit_id, struct got_repository *repo)
7658 const struct got_error *err;
7659 char *commit_ref_name;
7661 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7662 if (err)
7663 return err;
7665 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7666 if (err)
7667 goto done;
7669 err = delete_ref(commit_ref_name, repo);
7670 done:
7671 free(commit_ref_name);
7672 return err;
7675 const struct got_error *
7676 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7677 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7678 struct got_worktree *worktree, const char *refname,
7679 struct got_repository *repo)
7681 const struct got_error *err = NULL;
7682 char *fileindex_path = NULL;
7683 struct check_rebase_ok_arg ok_arg;
7685 *fileindex = NULL;
7686 *branch_ref = NULL;
7687 *base_branch_ref = NULL;
7689 err = lock_worktree(worktree, LOCK_EX);
7690 if (err)
7691 return err;
7693 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7694 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7695 "cannot integrate a branch into itself; "
7696 "update -b or different branch name required");
7697 goto done;
7700 err = open_fileindex(fileindex, &fileindex_path, worktree);
7701 if (err)
7702 goto done;
7704 /* Preconditions are the same as for rebase. */
7705 ok_arg.worktree = worktree;
7706 ok_arg.repo = repo;
7707 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7708 &ok_arg);
7709 if (err)
7710 goto done;
7712 err = got_ref_open(branch_ref, repo, refname, 1);
7713 if (err)
7714 goto done;
7716 err = got_ref_open(base_branch_ref, repo,
7717 got_worktree_get_head_ref_name(worktree), 1);
7718 done:
7719 if (err) {
7720 if (*branch_ref) {
7721 got_ref_close(*branch_ref);
7722 *branch_ref = NULL;
7724 if (*base_branch_ref) {
7725 got_ref_close(*base_branch_ref);
7726 *base_branch_ref = NULL;
7728 if (*fileindex) {
7729 got_fileindex_free(*fileindex);
7730 *fileindex = NULL;
7732 lock_worktree(worktree, LOCK_SH);
7734 return err;
7737 const struct got_error *
7738 got_worktree_integrate_continue(struct got_worktree *worktree,
7739 struct got_fileindex *fileindex, struct got_repository *repo,
7740 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7741 got_worktree_checkout_cb progress_cb, void *progress_arg,
7742 got_cancel_cb cancel_cb, void *cancel_arg)
7744 const struct got_error *err = NULL, *sync_err, *unlockerr;
7745 char *fileindex_path = NULL;
7746 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7747 struct got_commit_object *commit = NULL;
7749 err = get_fileindex_path(&fileindex_path, worktree);
7750 if (err)
7751 goto done;
7753 err = got_ref_resolve(&commit_id, repo, branch_ref);
7754 if (err)
7755 goto done;
7757 err = got_object_open_as_commit(&commit, repo, commit_id);
7758 if (err)
7759 goto done;
7761 err = got_object_id_by_path(&tree_id, repo, commit,
7762 worktree->path_prefix);
7763 if (err)
7764 goto done;
7766 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7767 if (err)
7768 goto done;
7770 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7771 progress_cb, progress_arg, cancel_cb, cancel_arg);
7772 if (err)
7773 goto sync;
7775 err = got_ref_change_ref(base_branch_ref, commit_id);
7776 if (err)
7777 goto sync;
7779 err = got_ref_write(base_branch_ref, repo);
7780 if (err)
7781 goto sync;
7783 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7784 sync:
7785 sync_err = sync_fileindex(fileindex, fileindex_path);
7786 if (sync_err && err == NULL)
7787 err = sync_err;
7789 done:
7790 unlockerr = got_ref_unlock(branch_ref);
7791 if (unlockerr && err == NULL)
7792 err = unlockerr;
7793 got_ref_close(branch_ref);
7795 unlockerr = got_ref_unlock(base_branch_ref);
7796 if (unlockerr && err == NULL)
7797 err = unlockerr;
7798 got_ref_close(base_branch_ref);
7800 got_fileindex_free(fileindex);
7801 free(fileindex_path);
7802 free(tree_id);
7803 if (commit)
7804 got_object_commit_close(commit);
7806 unlockerr = lock_worktree(worktree, LOCK_SH);
7807 if (unlockerr && err == NULL)
7808 err = unlockerr;
7809 return err;
7812 const struct got_error *
7813 got_worktree_integrate_abort(struct got_worktree *worktree,
7814 struct got_fileindex *fileindex, struct got_repository *repo,
7815 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7817 const struct got_error *err = NULL, *unlockerr = NULL;
7819 got_fileindex_free(fileindex);
7821 err = lock_worktree(worktree, LOCK_SH);
7823 unlockerr = got_ref_unlock(branch_ref);
7824 if (unlockerr && err == NULL)
7825 err = unlockerr;
7826 got_ref_close(branch_ref);
7828 unlockerr = got_ref_unlock(base_branch_ref);
7829 if (unlockerr && err == NULL)
7830 err = unlockerr;
7831 got_ref_close(base_branch_ref);
7833 return err;
7836 const struct got_error *
7837 got_worktree_merge_postpone(struct got_worktree *worktree,
7838 struct got_fileindex *fileindex)
7840 const struct got_error *err, *sync_err;
7841 char *fileindex_path = NULL;
7843 err = get_fileindex_path(&fileindex_path, worktree);
7844 if (err)
7845 goto done;
7847 sync_err = sync_fileindex(fileindex, fileindex_path);
7849 err = lock_worktree(worktree, LOCK_SH);
7850 if (sync_err && err == NULL)
7851 err = sync_err;
7852 done:
7853 got_fileindex_free(fileindex);
7854 free(fileindex_path);
7855 return err;
7858 static const struct got_error *
7859 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7861 const struct got_error *err;
7862 char *branch_refname = NULL, *commit_refname = NULL;
7864 err = get_merge_branch_ref_name(&branch_refname, worktree);
7865 if (err)
7866 goto done;
7867 err = delete_ref(branch_refname, repo);
7868 if (err)
7869 goto done;
7871 err = get_merge_commit_ref_name(&commit_refname, worktree);
7872 if (err)
7873 goto done;
7874 err = delete_ref(commit_refname, repo);
7875 if (err)
7876 goto done;
7878 done:
7879 free(branch_refname);
7880 free(commit_refname);
7881 return err;
7884 struct merge_commit_msg_arg {
7885 struct got_worktree *worktree;
7886 const char *branch_name;
7889 static const struct got_error *
7890 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7891 const char *diff_path, char **logmsg, void *arg)
7893 struct merge_commit_msg_arg *a = arg;
7895 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7896 got_worktree_get_head_ref_name(a->worktree)) == -1)
7897 return got_error_from_errno("asprintf");
7899 return NULL;
7903 const struct got_error *
7904 got_worktree_merge_branch(struct got_worktree *worktree,
7905 struct got_fileindex *fileindex,
7906 struct got_object_id *yca_commit_id,
7907 struct got_object_id *branch_tip,
7908 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7909 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7911 const struct got_error *err;
7912 char *fileindex_path = NULL;
7914 err = get_fileindex_path(&fileindex_path, worktree);
7915 if (err)
7916 goto done;
7918 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7919 worktree);
7920 if (err)
7921 goto done;
7923 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7924 branch_tip, repo, progress_cb, progress_arg,
7925 cancel_cb, cancel_arg);
7926 done:
7927 free(fileindex_path);
7928 return err;
7931 const struct got_error *
7932 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7933 struct got_worktree *worktree, struct got_fileindex *fileindex,
7934 const char *author, const char *committer, int allow_bad_symlinks,
7935 struct got_object_id *branch_tip, const char *branch_name,
7936 int allow_conflict, struct got_repository *repo,
7937 got_worktree_status_cb status_cb, void *status_arg)
7940 const struct got_error *err = NULL, *sync_err;
7941 struct got_pathlist_head commitable_paths;
7942 struct collect_commitables_arg cc_arg;
7943 struct got_pathlist_entry *pe;
7944 struct got_reference *head_ref = NULL;
7945 struct got_object_id *head_commit_id = NULL;
7946 int have_staged_files = 0;
7947 struct merge_commit_msg_arg mcm_arg;
7948 char *fileindex_path = NULL;
7950 memset(&cc_arg, 0, sizeof(cc_arg));
7951 *new_commit_id = NULL;
7953 TAILQ_INIT(&commitable_paths);
7955 err = get_fileindex_path(&fileindex_path, worktree);
7956 if (err)
7957 goto done;
7959 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7960 if (err)
7961 goto done;
7963 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7964 if (err)
7965 goto done;
7967 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7968 &have_staged_files);
7969 if (err && err->code != GOT_ERR_CANCELLED)
7970 goto done;
7971 if (have_staged_files) {
7972 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7973 goto done;
7976 cc_arg.commitable_paths = &commitable_paths;
7977 cc_arg.worktree = worktree;
7978 cc_arg.fileindex = fileindex;
7979 cc_arg.repo = repo;
7980 cc_arg.have_staged_files = have_staged_files;
7981 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7982 cc_arg.commit_conflicts = allow_conflict;
7983 err = worktree_status(worktree, "", fileindex, repo,
7984 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7985 if (err)
7986 goto done;
7988 if (TAILQ_EMPTY(&commitable_paths)) {
7989 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7990 "merge of %s cannot proceed", branch_name);
7991 goto done;
7994 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7995 struct got_commitable *ct = pe->data;
7996 const char *ct_path = ct->in_repo_path;
7998 while (ct_path[0] == '/')
7999 ct_path++;
8000 err = check_out_of_date(ct_path, ct->status,
8001 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8002 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8003 if (err)
8004 goto done;
8008 mcm_arg.worktree = worktree;
8009 mcm_arg.branch_name = branch_name;
8010 err = commit_worktree(new_commit_id, &commitable_paths,
8011 head_commit_id, branch_tip, worktree, author, committer, NULL,
8012 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8013 if (err)
8014 goto done;
8016 err = update_fileindex_after_commit(worktree, &commitable_paths,
8017 *new_commit_id, fileindex, have_staged_files);
8018 sync_err = sync_fileindex(fileindex, fileindex_path);
8019 if (sync_err && err == NULL)
8020 err = sync_err;
8021 done:
8022 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8023 struct got_commitable *ct = pe->data;
8025 free_commitable(ct);
8027 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8028 free(fileindex_path);
8029 return err;
8032 const struct got_error *
8033 got_worktree_merge_complete(struct got_worktree *worktree,
8034 struct got_fileindex *fileindex, struct got_repository *repo)
8036 const struct got_error *err, *unlockerr, *sync_err;
8037 char *fileindex_path = NULL;
8039 err = delete_merge_refs(worktree, repo);
8040 if (err)
8041 goto done;
8043 err = get_fileindex_path(&fileindex_path, worktree);
8044 if (err)
8045 goto done;
8046 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8047 sync_err = sync_fileindex(fileindex, fileindex_path);
8048 if (sync_err && err == NULL)
8049 err = sync_err;
8050 done:
8051 got_fileindex_free(fileindex);
8052 free(fileindex_path);
8053 unlockerr = lock_worktree(worktree, LOCK_SH);
8054 if (unlockerr && err == NULL)
8055 err = unlockerr;
8056 return err;
8059 const struct got_error *
8060 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8061 struct got_repository *repo)
8063 const struct got_error *err;
8064 char *branch_refname = NULL;
8065 struct got_reference *branch_ref = NULL;
8067 *in_progress = 0;
8069 err = get_merge_branch_ref_name(&branch_refname, worktree);
8070 if (err)
8071 return err;
8072 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8073 free(branch_refname);
8074 if (err) {
8075 if (err->code != GOT_ERR_NOT_REF)
8076 return err;
8077 } else
8078 *in_progress = 1;
8080 return NULL;
8083 const struct got_error *got_worktree_merge_prepare(
8084 struct got_fileindex **fileindex, struct got_worktree *worktree,
8085 struct got_reference *branch, struct got_repository *repo)
8087 const struct got_error *err = NULL;
8088 char *fileindex_path = NULL;
8089 char *branch_refname = NULL, *commit_refname = NULL;
8090 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8091 struct got_reference *commit_ref = NULL;
8092 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8093 struct check_rebase_ok_arg ok_arg;
8095 *fileindex = NULL;
8097 err = lock_worktree(worktree, LOCK_EX);
8098 if (err)
8099 return err;
8101 err = open_fileindex(fileindex, &fileindex_path, worktree);
8102 if (err)
8103 goto done;
8105 /* Preconditions are the same as for rebase. */
8106 ok_arg.worktree = worktree;
8107 ok_arg.repo = repo;
8108 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8109 &ok_arg);
8110 if (err)
8111 goto done;
8113 err = get_merge_branch_ref_name(&branch_refname, worktree);
8114 if (err)
8115 return err;
8117 err = get_merge_commit_ref_name(&commit_refname, worktree);
8118 if (err)
8119 return err;
8121 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8122 0);
8123 if (err)
8124 goto done;
8126 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8127 if (err)
8128 goto done;
8130 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8131 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8132 goto done;
8135 err = got_ref_resolve(&branch_tip, repo, branch);
8136 if (err)
8137 goto done;
8139 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8140 if (err)
8141 goto done;
8142 err = got_ref_write(branch_ref, repo);
8143 if (err)
8144 goto done;
8146 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8147 if (err)
8148 goto done;
8149 err = got_ref_write(commit_ref, repo);
8150 if (err)
8151 goto done;
8153 done:
8154 free(branch_refname);
8155 free(commit_refname);
8156 free(fileindex_path);
8157 if (branch_ref)
8158 got_ref_close(branch_ref);
8159 if (commit_ref)
8160 got_ref_close(commit_ref);
8161 if (wt_branch)
8162 got_ref_close(wt_branch);
8163 free(wt_branch_tip);
8164 if (err) {
8165 if (*fileindex) {
8166 got_fileindex_free(*fileindex);
8167 *fileindex = NULL;
8169 lock_worktree(worktree, LOCK_SH);
8171 return err;
8174 const struct got_error *
8175 got_worktree_merge_continue(char **branch_name,
8176 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8177 struct got_worktree *worktree, struct got_repository *repo)
8179 const struct got_error *err;
8180 char *commit_refname = NULL, *branch_refname = NULL;
8181 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8182 char *fileindex_path = NULL;
8183 int have_staged_files = 0;
8185 *branch_name = NULL;
8186 *branch_tip = NULL;
8187 *fileindex = NULL;
8189 err = lock_worktree(worktree, LOCK_EX);
8190 if (err)
8191 return err;
8193 err = open_fileindex(fileindex, &fileindex_path, worktree);
8194 if (err)
8195 goto done;
8197 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8198 &have_staged_files);
8199 if (err && err->code != GOT_ERR_CANCELLED)
8200 goto done;
8201 if (have_staged_files) {
8202 err = got_error(GOT_ERR_STAGED_PATHS);
8203 goto done;
8206 err = get_merge_branch_ref_name(&branch_refname, worktree);
8207 if (err)
8208 goto done;
8210 err = get_merge_commit_ref_name(&commit_refname, worktree);
8211 if (err)
8212 goto done;
8214 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8215 if (err)
8216 goto done;
8218 if (!got_ref_is_symbolic(branch_ref)) {
8219 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8220 "%s is not a symbolic reference",
8221 got_ref_get_name(branch_ref));
8222 goto done;
8224 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8225 if (*branch_name == NULL) {
8226 err = got_error_from_errno("strdup");
8227 goto done;
8230 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8231 if (err)
8232 goto done;
8234 err = got_ref_resolve(branch_tip, repo, commit_ref);
8235 if (err)
8236 goto done;
8237 done:
8238 free(commit_refname);
8239 free(branch_refname);
8240 free(fileindex_path);
8241 if (commit_ref)
8242 got_ref_close(commit_ref);
8243 if (branch_ref)
8244 got_ref_close(branch_ref);
8245 if (err) {
8246 if (*branch_name) {
8247 free(*branch_name);
8248 *branch_name = NULL;
8250 free(*branch_tip);
8251 *branch_tip = NULL;
8252 if (*fileindex) {
8253 got_fileindex_free(*fileindex);
8254 *fileindex = NULL;
8256 lock_worktree(worktree, LOCK_SH);
8258 return err;
8261 const struct got_error *
8262 got_worktree_merge_abort(struct got_worktree *worktree,
8263 struct got_fileindex *fileindex, struct got_repository *repo,
8264 got_worktree_checkout_cb progress_cb, void *progress_arg)
8266 const struct got_error *err, *unlockerr, *sync_err;
8267 struct got_object_id *commit_id = NULL;
8268 struct got_commit_object *commit = NULL;
8269 char *fileindex_path = NULL;
8270 struct revert_file_args rfa;
8271 struct got_object_id *tree_id = NULL;
8273 err = got_object_open_as_commit(&commit, repo,
8274 worktree->base_commit_id);
8275 if (err)
8276 goto done;
8278 err = got_object_id_by_path(&tree_id, repo, commit,
8279 worktree->path_prefix);
8280 if (err)
8281 goto done;
8283 err = delete_merge_refs(worktree, repo);
8284 if (err)
8285 goto done;
8287 err = get_fileindex_path(&fileindex_path, worktree);
8288 if (err)
8289 goto done;
8291 rfa.worktree = worktree;
8292 rfa.fileindex = fileindex;
8293 rfa.progress_cb = progress_cb;
8294 rfa.progress_arg = progress_arg;
8295 rfa.patch_cb = NULL;
8296 rfa.patch_arg = NULL;
8297 rfa.repo = repo;
8298 rfa.unlink_added_files = 1;
8299 err = worktree_status(worktree, "", fileindex, repo,
8300 revert_file, &rfa, NULL, NULL, 1, 0);
8301 if (err)
8302 goto sync;
8304 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8305 repo, progress_cb, progress_arg, NULL, NULL);
8306 sync:
8307 sync_err = sync_fileindex(fileindex, fileindex_path);
8308 if (sync_err && err == NULL)
8309 err = sync_err;
8310 done:
8311 free(tree_id);
8312 free(commit_id);
8313 if (commit)
8314 got_object_commit_close(commit);
8315 if (fileindex)
8316 got_fileindex_free(fileindex);
8317 free(fileindex_path);
8319 unlockerr = lock_worktree(worktree, LOCK_SH);
8320 if (unlockerr && err == NULL)
8321 err = unlockerr;
8322 return err;
8325 struct check_stage_ok_arg {
8326 struct got_object_id *head_commit_id;
8327 struct got_worktree *worktree;
8328 struct got_fileindex *fileindex;
8329 struct got_repository *repo;
8330 int have_changes;
8333 static const struct got_error *
8334 check_stage_ok(void *arg, unsigned char status,
8335 unsigned char staged_status, const char *relpath,
8336 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8337 struct got_object_id *commit_id, int dirfd, const char *de_name)
8339 struct check_stage_ok_arg *a = arg;
8340 const struct got_error *err = NULL;
8341 struct got_fileindex_entry *ie;
8342 struct got_object_id base_commit_id;
8343 struct got_object_id *base_commit_idp = NULL;
8344 char *in_repo_path = NULL, *p;
8346 if (status == GOT_STATUS_UNVERSIONED ||
8347 status == GOT_STATUS_NO_CHANGE)
8348 return NULL;
8349 if (status == GOT_STATUS_NONEXISTENT)
8350 return got_error_set_errno(ENOENT, relpath);
8352 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8353 if (ie == NULL)
8354 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8356 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8357 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8358 relpath) == -1)
8359 return got_error_from_errno("asprintf");
8361 if (got_fileindex_entry_has_commit(ie)) {
8362 base_commit_idp = got_fileindex_entry_get_commit_id(
8363 &base_commit_id, ie);
8366 if (status == GOT_STATUS_CONFLICT) {
8367 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8368 goto done;
8369 } else if (status != GOT_STATUS_ADD &&
8370 status != GOT_STATUS_MODIFY &&
8371 status != GOT_STATUS_DELETE) {
8372 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8373 goto done;
8376 a->have_changes = 1;
8378 p = in_repo_path;
8379 while (p[0] == '/')
8380 p++;
8381 err = check_out_of_date(p, status, staged_status,
8382 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8383 GOT_ERR_STAGE_OUT_OF_DATE);
8384 done:
8385 free(in_repo_path);
8386 return err;
8389 struct stage_path_arg {
8390 struct got_worktree *worktree;
8391 struct got_fileindex *fileindex;
8392 struct got_repository *repo;
8393 got_worktree_status_cb status_cb;
8394 void *status_arg;
8395 got_worktree_patch_cb patch_cb;
8396 void *patch_arg;
8397 int staged_something;
8398 int allow_bad_symlinks;
8401 static const struct got_error *
8402 stage_path(void *arg, unsigned char status,
8403 unsigned char staged_status, const char *relpath,
8404 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8405 struct got_object_id *commit_id, int dirfd, const char *de_name)
8407 struct stage_path_arg *a = arg;
8408 const struct got_error *err = NULL;
8409 struct got_fileindex_entry *ie;
8410 char *ondisk_path = NULL, *path_content = NULL;
8411 uint32_t stage;
8412 struct got_object_id *new_staged_blob_id = NULL;
8413 struct stat sb;
8415 if (status == GOT_STATUS_UNVERSIONED)
8416 return NULL;
8418 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8419 if (ie == NULL)
8420 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8422 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8423 relpath)== -1)
8424 return got_error_from_errno("asprintf");
8426 switch (status) {
8427 case GOT_STATUS_ADD:
8428 case GOT_STATUS_MODIFY:
8429 /* XXX could sb.st_mode be passed in by our caller? */
8430 if (lstat(ondisk_path, &sb) == -1) {
8431 err = got_error_from_errno2("lstat", ondisk_path);
8432 break;
8434 if (a->patch_cb) {
8435 if (status == GOT_STATUS_ADD) {
8436 int choice = GOT_PATCH_CHOICE_NONE;
8437 err = (*a->patch_cb)(&choice, a->patch_arg,
8438 status, ie->path, NULL, 1, 1);
8439 if (err)
8440 break;
8441 if (choice != GOT_PATCH_CHOICE_YES)
8442 break;
8443 } else {
8444 err = create_patched_content(&path_content, 0,
8445 staged_blob_id ? staged_blob_id : blob_id,
8446 ondisk_path, dirfd, de_name, ie->path,
8447 a->repo, a->patch_cb, a->patch_arg);
8448 if (err || path_content == NULL)
8449 break;
8452 err = got_object_blob_create(&new_staged_blob_id,
8453 path_content ? path_content : ondisk_path, a->repo);
8454 if (err)
8455 break;
8456 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8457 SHA1_DIGEST_LENGTH);
8458 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8459 stage = GOT_FILEIDX_STAGE_ADD;
8460 else
8461 stage = GOT_FILEIDX_STAGE_MODIFY;
8462 got_fileindex_entry_stage_set(ie, stage);
8463 if (S_ISLNK(sb.st_mode)) {
8464 int is_bad_symlink = 0;
8465 if (!a->allow_bad_symlinks) {
8466 char target_path[PATH_MAX];
8467 ssize_t target_len;
8468 target_len = readlink(ondisk_path, target_path,
8469 sizeof(target_path));
8470 if (target_len == -1) {
8471 err = got_error_from_errno2("readlink",
8472 ondisk_path);
8473 break;
8475 err = is_bad_symlink_target(&is_bad_symlink,
8476 target_path, target_len, ondisk_path,
8477 a->worktree->root_path);
8478 if (err)
8479 break;
8480 if (is_bad_symlink) {
8481 err = got_error_path(ondisk_path,
8482 GOT_ERR_BAD_SYMLINK);
8483 break;
8486 if (is_bad_symlink)
8487 got_fileindex_entry_staged_filetype_set(ie,
8488 GOT_FILEIDX_MODE_BAD_SYMLINK);
8489 else
8490 got_fileindex_entry_staged_filetype_set(ie,
8491 GOT_FILEIDX_MODE_SYMLINK);
8492 } else {
8493 got_fileindex_entry_staged_filetype_set(ie,
8494 GOT_FILEIDX_MODE_REGULAR_FILE);
8496 a->staged_something = 1;
8497 if (a->status_cb == NULL)
8498 break;
8499 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8500 get_staged_status(ie), relpath, blob_id,
8501 new_staged_blob_id, NULL, dirfd, de_name);
8502 if (err)
8503 break;
8505 * When staging the reverse of the staged diff,
8506 * implicitly unstage the file.
8508 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8509 sizeof(ie->blob_sha1)) == 0) {
8510 got_fileindex_entry_stage_set(ie,
8511 GOT_FILEIDX_STAGE_NONE);
8513 break;
8514 case GOT_STATUS_DELETE:
8515 if (staged_status == GOT_STATUS_DELETE)
8516 break;
8517 if (a->patch_cb) {
8518 int choice = GOT_PATCH_CHOICE_NONE;
8519 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8520 ie->path, NULL, 1, 1);
8521 if (err)
8522 break;
8523 if (choice == GOT_PATCH_CHOICE_NO)
8524 break;
8525 if (choice != GOT_PATCH_CHOICE_YES) {
8526 err = got_error(GOT_ERR_PATCH_CHOICE);
8527 break;
8530 stage = GOT_FILEIDX_STAGE_DELETE;
8531 got_fileindex_entry_stage_set(ie, stage);
8532 a->staged_something = 1;
8533 if (a->status_cb == NULL)
8534 break;
8535 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8536 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8537 de_name);
8538 break;
8539 case GOT_STATUS_NO_CHANGE:
8540 break;
8541 case GOT_STATUS_CONFLICT:
8542 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8543 break;
8544 case GOT_STATUS_NONEXISTENT:
8545 err = got_error_set_errno(ENOENT, relpath);
8546 break;
8547 default:
8548 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8549 break;
8552 if (path_content && unlink(path_content) == -1 && err == NULL)
8553 err = got_error_from_errno2("unlink", path_content);
8554 free(path_content);
8555 free(ondisk_path);
8556 free(new_staged_blob_id);
8557 return err;
8560 const struct got_error *
8561 got_worktree_stage(struct got_worktree *worktree,
8562 struct got_pathlist_head *paths,
8563 got_worktree_status_cb status_cb, void *status_arg,
8564 got_worktree_patch_cb patch_cb, void *patch_arg,
8565 int allow_bad_symlinks, struct got_repository *repo)
8567 const struct got_error *err = NULL, *sync_err, *unlockerr;
8568 struct got_pathlist_entry *pe;
8569 struct got_fileindex *fileindex = NULL;
8570 char *fileindex_path = NULL;
8571 struct got_reference *head_ref = NULL;
8572 struct got_object_id *head_commit_id = NULL;
8573 struct check_stage_ok_arg oka;
8574 struct stage_path_arg spa;
8576 err = lock_worktree(worktree, LOCK_EX);
8577 if (err)
8578 return err;
8580 err = got_ref_open(&head_ref, repo,
8581 got_worktree_get_head_ref_name(worktree), 0);
8582 if (err)
8583 goto done;
8584 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8585 if (err)
8586 goto done;
8587 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8588 if (err)
8589 goto done;
8591 /* Check pre-conditions before staging anything. */
8592 oka.head_commit_id = head_commit_id;
8593 oka.worktree = worktree;
8594 oka.fileindex = fileindex;
8595 oka.repo = repo;
8596 oka.have_changes = 0;
8597 TAILQ_FOREACH(pe, paths, entry) {
8598 err = worktree_status(worktree, pe->path, fileindex, repo,
8599 check_stage_ok, &oka, NULL, NULL, 1, 0);
8600 if (err)
8601 goto done;
8603 if (!oka.have_changes) {
8604 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8605 goto done;
8608 spa.worktree = worktree;
8609 spa.fileindex = fileindex;
8610 spa.repo = repo;
8611 spa.patch_cb = patch_cb;
8612 spa.patch_arg = patch_arg;
8613 spa.status_cb = status_cb;
8614 spa.status_arg = status_arg;
8615 spa.staged_something = 0;
8616 spa.allow_bad_symlinks = allow_bad_symlinks;
8617 TAILQ_FOREACH(pe, paths, entry) {
8618 err = worktree_status(worktree, pe->path, fileindex, repo,
8619 stage_path, &spa, NULL, NULL, 1, 0);
8620 if (err)
8621 goto done;
8623 if (!spa.staged_something) {
8624 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8625 goto done;
8628 sync_err = sync_fileindex(fileindex, fileindex_path);
8629 if (sync_err && err == NULL)
8630 err = sync_err;
8631 done:
8632 if (head_ref)
8633 got_ref_close(head_ref);
8634 free(head_commit_id);
8635 free(fileindex_path);
8636 if (fileindex)
8637 got_fileindex_free(fileindex);
8638 unlockerr = lock_worktree(worktree, LOCK_SH);
8639 if (unlockerr && err == NULL)
8640 err = unlockerr;
8641 return err;
8644 struct unstage_path_arg {
8645 struct got_worktree *worktree;
8646 struct got_fileindex *fileindex;
8647 struct got_repository *repo;
8648 got_worktree_checkout_cb progress_cb;
8649 void *progress_arg;
8650 got_worktree_patch_cb patch_cb;
8651 void *patch_arg;
8654 static const struct got_error *
8655 create_unstaged_content(char **path_unstaged_content,
8656 char **path_new_staged_content, struct got_object_id *blob_id,
8657 struct got_object_id *staged_blob_id, const char *relpath,
8658 struct got_repository *repo,
8659 got_worktree_patch_cb patch_cb, void *patch_arg)
8661 const struct got_error *err, *free_err;
8662 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8663 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8664 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8665 struct got_diffreg_result *diffreg_result = NULL;
8666 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8667 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8668 int fd1 = -1, fd2 = -1;
8670 *path_unstaged_content = NULL;
8671 *path_new_staged_content = NULL;
8673 err = got_object_id_str(&label1, blob_id);
8674 if (err)
8675 return err;
8677 fd1 = got_opentempfd();
8678 if (fd1 == -1) {
8679 err = got_error_from_errno("got_opentempfd");
8680 goto done;
8682 fd2 = got_opentempfd();
8683 if (fd2 == -1) {
8684 err = got_error_from_errno("got_opentempfd");
8685 goto done;
8688 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8689 if (err)
8690 goto done;
8692 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8693 if (err)
8694 goto done;
8696 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8697 if (err)
8698 goto done;
8700 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8701 fd2);
8702 if (err)
8703 goto done;
8705 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8706 if (err)
8707 goto done;
8709 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8710 if (err)
8711 goto done;
8713 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8714 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8715 if (err)
8716 goto done;
8718 err = got_opentemp_named(path_unstaged_content, &outfile,
8719 "got-unstaged-content", "");
8720 if (err)
8721 goto done;
8722 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8723 "got-new-staged-content", "");
8724 if (err)
8725 goto done;
8727 if (fseek(f1, 0L, SEEK_SET) == -1) {
8728 err = got_ferror(f1, GOT_ERR_IO);
8729 goto done;
8731 if (fseek(f2, 0L, SEEK_SET) == -1) {
8732 err = got_ferror(f2, GOT_ERR_IO);
8733 goto done;
8735 /* Count the number of actual changes in the diff result. */
8736 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8737 struct diff_chunk_context cc = {};
8738 diff_chunk_context_load_change(&cc, &nchunks_used,
8739 diffreg_result->result, n, 0);
8740 nchanges++;
8742 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8743 int choice;
8744 err = apply_or_reject_change(&choice, &nchunks_used,
8745 diffreg_result->result, n, relpath, f1, f2,
8746 &line_cur1, &line_cur2,
8747 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8748 if (err)
8749 goto done;
8750 if (choice == GOT_PATCH_CHOICE_YES)
8751 have_content = 1;
8752 else
8753 have_rejected_content = 1;
8754 if (choice == GOT_PATCH_CHOICE_QUIT)
8755 break;
8757 if (have_content || have_rejected_content)
8758 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8759 outfile, rejectfile);
8760 done:
8761 free(label1);
8762 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8763 err = got_error_from_errno("close");
8764 if (blob)
8765 got_object_blob_close(blob);
8766 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8767 err = got_error_from_errno("close");
8768 if (staged_blob)
8769 got_object_blob_close(staged_blob);
8770 free_err = got_diffreg_result_free(diffreg_result);
8771 if (free_err && err == NULL)
8772 err = free_err;
8773 if (f1 && fclose(f1) == EOF && err == NULL)
8774 err = got_error_from_errno2("fclose", path1);
8775 if (f2 && fclose(f2) == EOF && err == NULL)
8776 err = got_error_from_errno2("fclose", path2);
8777 if (outfile && fclose(outfile) == EOF && err == NULL)
8778 err = got_error_from_errno2("fclose", *path_unstaged_content);
8779 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8780 err = got_error_from_errno2("fclose", *path_new_staged_content);
8781 if (path1 && unlink(path1) == -1 && err == NULL)
8782 err = got_error_from_errno2("unlink", path1);
8783 if (path2 && unlink(path2) == -1 && err == NULL)
8784 err = got_error_from_errno2("unlink", path2);
8785 if (err || !have_content) {
8786 if (*path_unstaged_content &&
8787 unlink(*path_unstaged_content) == -1 && err == NULL)
8788 err = got_error_from_errno2("unlink",
8789 *path_unstaged_content);
8790 free(*path_unstaged_content);
8791 *path_unstaged_content = NULL;
8793 if (err || !have_content || !have_rejected_content) {
8794 if (*path_new_staged_content &&
8795 unlink(*path_new_staged_content) == -1 && err == NULL)
8796 err = got_error_from_errno2("unlink",
8797 *path_new_staged_content);
8798 free(*path_new_staged_content);
8799 *path_new_staged_content = NULL;
8801 free(path1);
8802 free(path2);
8803 return err;
8806 static const struct got_error *
8807 unstage_hunks(struct got_object_id *staged_blob_id,
8808 struct got_blob_object *blob_base,
8809 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8810 const char *ondisk_path, const char *label_orig,
8811 struct got_worktree *worktree, struct got_repository *repo,
8812 got_worktree_patch_cb patch_cb, void *patch_arg,
8813 got_worktree_checkout_cb progress_cb, void *progress_arg)
8815 const struct got_error *err = NULL;
8816 char *path_unstaged_content = NULL;
8817 char *path_new_staged_content = NULL;
8818 char *parent = NULL, *base_path = NULL;
8819 char *blob_base_path = NULL;
8820 struct got_object_id *new_staged_blob_id = NULL;
8821 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8822 struct stat sb;
8824 err = create_unstaged_content(&path_unstaged_content,
8825 &path_new_staged_content, blob_id, staged_blob_id,
8826 ie->path, repo, patch_cb, patch_arg);
8827 if (err)
8828 return err;
8830 if (path_unstaged_content == NULL)
8831 return NULL;
8833 if (path_new_staged_content) {
8834 err = got_object_blob_create(&new_staged_blob_id,
8835 path_new_staged_content, repo);
8836 if (err)
8837 goto done;
8840 f = fopen(path_unstaged_content, "re");
8841 if (f == NULL) {
8842 err = got_error_from_errno2("fopen",
8843 path_unstaged_content);
8844 goto done;
8846 if (fstat(fileno(f), &sb) == -1) {
8847 err = got_error_from_errno2("fstat", path_unstaged_content);
8848 goto done;
8850 if (got_fileindex_entry_staged_filetype_get(ie) ==
8851 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8852 char link_target[PATH_MAX];
8853 size_t r;
8854 r = fread(link_target, 1, sizeof(link_target), f);
8855 if (r == 0 && ferror(f)) {
8856 err = got_error_from_errno("fread");
8857 goto done;
8859 if (r >= sizeof(link_target)) { /* should not happen */
8860 err = got_error(GOT_ERR_NO_SPACE);
8861 goto done;
8863 link_target[r] = '\0';
8864 err = merge_symlink(worktree, blob_base,
8865 ondisk_path, ie->path, label_orig, link_target,
8866 worktree->base_commit_id, repo, progress_cb,
8867 progress_arg);
8868 } else {
8869 int local_changes_subsumed;
8871 err = got_path_dirname(&parent, ondisk_path);
8872 if (err)
8873 return err;
8875 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8876 parent) == -1) {
8877 err = got_error_from_errno("asprintf");
8878 base_path = NULL;
8879 goto done;
8882 err = got_opentemp_named(&blob_base_path, &f_base,
8883 base_path, "");
8884 if (err)
8885 goto done;
8886 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8887 blob_base);
8888 if (err)
8889 goto done;
8892 * In order the run a 3-way merge with a symlink we copy the symlink's
8893 * target path into a temporary file and use that file with diff3.
8895 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8896 err = dump_symlink_target_path_to_file(&f_deriv2,
8897 ondisk_path);
8898 if (err)
8899 goto done;
8900 } else {
8901 int fd;
8902 fd = open(ondisk_path,
8903 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8904 if (fd == -1) {
8905 err = got_error_from_errno2("open", ondisk_path);
8906 goto done;
8908 f_deriv2 = fdopen(fd, "r");
8909 if (f_deriv2 == NULL) {
8910 err = got_error_from_errno2("fdopen", ondisk_path);
8911 close(fd);
8912 goto done;
8916 err = merge_file(&local_changes_subsumed, worktree,
8917 f_base, f, f_deriv2, ondisk_path, ie->path,
8918 got_fileindex_perms_to_st(ie),
8919 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8920 repo, progress_cb, progress_arg);
8922 if (err)
8923 goto done;
8925 if (new_staged_blob_id) {
8926 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8927 SHA1_DIGEST_LENGTH);
8928 } else {
8929 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8930 got_fileindex_entry_staged_filetype_set(ie, 0);
8932 done:
8933 free(new_staged_blob_id);
8934 if (path_unstaged_content &&
8935 unlink(path_unstaged_content) == -1 && err == NULL)
8936 err = got_error_from_errno2("unlink", path_unstaged_content);
8937 if (path_new_staged_content &&
8938 unlink(path_new_staged_content) == -1 && err == NULL)
8939 err = got_error_from_errno2("unlink", path_new_staged_content);
8940 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8941 err = got_error_from_errno2("unlink", blob_base_path);
8942 if (f_base && fclose(f_base) == EOF && err == NULL)
8943 err = got_error_from_errno2("fclose", path_unstaged_content);
8944 if (f && fclose(f) == EOF && err == NULL)
8945 err = got_error_from_errno2("fclose", path_unstaged_content);
8946 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8947 err = got_error_from_errno2("fclose", ondisk_path);
8948 free(path_unstaged_content);
8949 free(path_new_staged_content);
8950 free(blob_base_path);
8951 free(parent);
8952 free(base_path);
8953 return err;
8956 static const struct got_error *
8957 unstage_path(void *arg, unsigned char status,
8958 unsigned char staged_status, const char *relpath,
8959 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8960 struct got_object_id *commit_id, int dirfd, const char *de_name)
8962 const struct got_error *err = NULL;
8963 struct unstage_path_arg *a = arg;
8964 struct got_fileindex_entry *ie;
8965 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8966 char *ondisk_path = NULL;
8967 char *id_str = NULL, *label_orig = NULL;
8968 int local_changes_subsumed;
8969 struct stat sb;
8970 int fd1 = -1, fd2 = -1;
8972 if (staged_status != GOT_STATUS_ADD &&
8973 staged_status != GOT_STATUS_MODIFY &&
8974 staged_status != GOT_STATUS_DELETE)
8975 return NULL;
8977 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8978 if (ie == NULL)
8979 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8982 == -1)
8983 return got_error_from_errno("asprintf");
8985 err = got_object_id_str(&id_str,
8986 commit_id ? commit_id : a->worktree->base_commit_id);
8987 if (err)
8988 goto done;
8989 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8990 id_str) == -1) {
8991 err = got_error_from_errno("asprintf");
8992 goto done;
8995 fd1 = got_opentempfd();
8996 if (fd1 == -1) {
8997 err = got_error_from_errno("got_opentempfd");
8998 goto done;
9000 fd2 = got_opentempfd();
9001 if (fd2 == -1) {
9002 err = got_error_from_errno("got_opentempfd");
9003 goto done;
9006 switch (staged_status) {
9007 case GOT_STATUS_MODIFY:
9008 err = got_object_open_as_blob(&blob_base, a->repo,
9009 blob_id, 8192, fd1);
9010 if (err)
9011 break;
9012 /* fall through */
9013 case GOT_STATUS_ADD:
9014 if (a->patch_cb) {
9015 if (staged_status == GOT_STATUS_ADD) {
9016 int choice = GOT_PATCH_CHOICE_NONE;
9017 err = (*a->patch_cb)(&choice, a->patch_arg,
9018 staged_status, ie->path, NULL, 1, 1);
9019 if (err)
9020 break;
9021 if (choice != GOT_PATCH_CHOICE_YES)
9022 break;
9023 } else {
9024 err = unstage_hunks(staged_blob_id,
9025 blob_base, blob_id, ie, ondisk_path,
9026 label_orig, a->worktree, a->repo,
9027 a->patch_cb, a->patch_arg,
9028 a->progress_cb, a->progress_arg);
9029 break; /* Done with this file. */
9032 err = got_object_open_as_blob(&blob_staged, a->repo,
9033 staged_blob_id, 8192, fd2);
9034 if (err)
9035 break;
9036 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9037 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9038 case GOT_FILEIDX_MODE_REGULAR_FILE:
9039 err = merge_blob(&local_changes_subsumed, a->worktree,
9040 blob_base, ondisk_path, relpath,
9041 got_fileindex_perms_to_st(ie), label_orig,
9042 blob_staged, commit_id ? commit_id :
9043 a->worktree->base_commit_id, a->repo,
9044 a->progress_cb, a->progress_arg);
9045 break;
9046 case GOT_FILEIDX_MODE_SYMLINK:
9047 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9048 char *staged_target;
9049 err = got_object_blob_read_to_str(
9050 &staged_target, blob_staged);
9051 if (err)
9052 goto done;
9053 err = merge_symlink(a->worktree, blob_base,
9054 ondisk_path, relpath, label_orig,
9055 staged_target, commit_id ? commit_id :
9056 a->worktree->base_commit_id,
9057 a->repo, a->progress_cb, a->progress_arg);
9058 free(staged_target);
9059 } else {
9060 err = merge_blob(&local_changes_subsumed,
9061 a->worktree, blob_base, ondisk_path,
9062 relpath, got_fileindex_perms_to_st(ie),
9063 label_orig, blob_staged,
9064 commit_id ? commit_id :
9065 a->worktree->base_commit_id, a->repo,
9066 a->progress_cb, a->progress_arg);
9068 break;
9069 default:
9070 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9071 break;
9073 if (err == NULL) {
9074 got_fileindex_entry_stage_set(ie,
9075 GOT_FILEIDX_STAGE_NONE);
9076 got_fileindex_entry_staged_filetype_set(ie, 0);
9078 break;
9079 case GOT_STATUS_DELETE:
9080 if (a->patch_cb) {
9081 int choice = GOT_PATCH_CHOICE_NONE;
9082 err = (*a->patch_cb)(&choice, a->patch_arg,
9083 staged_status, ie->path, NULL, 1, 1);
9084 if (err)
9085 break;
9086 if (choice == GOT_PATCH_CHOICE_NO)
9087 break;
9088 if (choice != GOT_PATCH_CHOICE_YES) {
9089 err = got_error(GOT_ERR_PATCH_CHOICE);
9090 break;
9093 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9094 got_fileindex_entry_staged_filetype_set(ie, 0);
9095 err = get_file_status(&status, &sb, ie, ondisk_path,
9096 dirfd, de_name, a->repo);
9097 if (err)
9098 break;
9099 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9100 break;
9102 done:
9103 free(ondisk_path);
9104 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9105 err = got_error_from_errno("close");
9106 if (blob_base)
9107 got_object_blob_close(blob_base);
9108 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9109 err = got_error_from_errno("close");
9110 if (blob_staged)
9111 got_object_blob_close(blob_staged);
9112 free(id_str);
9113 free(label_orig);
9114 return err;
9117 const struct got_error *
9118 got_worktree_unstage(struct got_worktree *worktree,
9119 struct got_pathlist_head *paths,
9120 got_worktree_checkout_cb progress_cb, void *progress_arg,
9121 got_worktree_patch_cb patch_cb, void *patch_arg,
9122 struct got_repository *repo)
9124 const struct got_error *err = NULL, *sync_err, *unlockerr;
9125 struct got_pathlist_entry *pe;
9126 struct got_fileindex *fileindex = NULL;
9127 char *fileindex_path = NULL;
9128 struct unstage_path_arg upa;
9130 err = lock_worktree(worktree, LOCK_EX);
9131 if (err)
9132 return err;
9134 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9135 if (err)
9136 goto done;
9138 upa.worktree = worktree;
9139 upa.fileindex = fileindex;
9140 upa.repo = repo;
9141 upa.progress_cb = progress_cb;
9142 upa.progress_arg = progress_arg;
9143 upa.patch_cb = patch_cb;
9144 upa.patch_arg = patch_arg;
9145 TAILQ_FOREACH(pe, paths, entry) {
9146 err = worktree_status(worktree, pe->path, fileindex, repo,
9147 unstage_path, &upa, NULL, NULL, 1, 0);
9148 if (err)
9149 goto done;
9152 sync_err = sync_fileindex(fileindex, fileindex_path);
9153 if (sync_err && err == NULL)
9154 err = sync_err;
9155 done:
9156 free(fileindex_path);
9157 if (fileindex)
9158 got_fileindex_free(fileindex);
9159 unlockerr = lock_worktree(worktree, LOCK_SH);
9160 if (unlockerr && err == NULL)
9161 err = unlockerr;
9162 return err;
9165 struct report_file_info_arg {
9166 struct got_worktree *worktree;
9167 got_worktree_path_info_cb info_cb;
9168 void *info_arg;
9169 struct got_pathlist_head *paths;
9170 got_cancel_cb cancel_cb;
9171 void *cancel_arg;
9174 static const struct got_error *
9175 report_file_info(void *arg, struct got_fileindex_entry *ie)
9177 struct report_file_info_arg *a = arg;
9178 struct got_pathlist_entry *pe;
9179 struct got_object_id blob_id, staged_blob_id, commit_id;
9180 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9181 struct got_object_id *commit_idp = NULL;
9182 int stage;
9184 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9185 return got_error(GOT_ERR_CANCELLED);
9187 TAILQ_FOREACH(pe, a->paths, entry) {
9188 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9189 got_path_is_child(ie->path, pe->path, pe->path_len))
9190 break;
9192 if (pe == NULL) /* not found */
9193 return NULL;
9195 if (got_fileindex_entry_has_blob(ie))
9196 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9197 stage = got_fileindex_entry_stage_get(ie);
9198 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9199 stage == GOT_FILEIDX_STAGE_ADD) {
9200 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9201 &staged_blob_id, ie);
9204 if (got_fileindex_entry_has_commit(ie))
9205 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9207 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9208 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9211 const struct got_error *
9212 got_worktree_path_info(struct got_worktree *worktree,
9213 struct got_pathlist_head *paths,
9214 got_worktree_path_info_cb info_cb, void *info_arg,
9215 got_cancel_cb cancel_cb, void *cancel_arg)
9218 const struct got_error *err = NULL, *unlockerr;
9219 struct got_fileindex *fileindex = NULL;
9220 char *fileindex_path = NULL;
9221 struct report_file_info_arg arg;
9223 err = lock_worktree(worktree, LOCK_SH);
9224 if (err)
9225 return err;
9227 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9228 if (err)
9229 goto done;
9231 arg.worktree = worktree;
9232 arg.info_cb = info_cb;
9233 arg.info_arg = info_arg;
9234 arg.paths = paths;
9235 arg.cancel_cb = cancel_cb;
9236 arg.cancel_arg = cancel_arg;
9237 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9238 &arg);
9239 done:
9240 free(fileindex_path);
9241 if (fileindex)
9242 got_fileindex_free(fileindex);
9243 unlockerr = lock_worktree(worktree, LOCK_UN);
9244 if (unlockerr && err == NULL)
9245 err = unlockerr;
9246 return err;
9249 static const struct got_error *
9250 patch_check_path(const char *p, char **path, unsigned char *status,
9251 unsigned char *staged_status, struct got_fileindex *fileindex,
9252 struct got_worktree *worktree, struct got_repository *repo)
9254 const struct got_error *err;
9255 struct got_fileindex_entry *ie;
9256 struct stat sb;
9257 char *ondisk_path = NULL;
9259 err = got_worktree_resolve_path(path, worktree, p);
9260 if (err)
9261 return err;
9263 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9264 *path[0] ? "/" : "", *path) == -1)
9265 return got_error_from_errno("asprintf");
9267 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9268 if (ie) {
9269 *staged_status = get_staged_status(ie);
9270 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9271 repo);
9272 if (err)
9273 goto done;
9274 } else {
9275 *staged_status = GOT_STATUS_NO_CHANGE;
9276 *status = GOT_STATUS_UNVERSIONED;
9277 if (lstat(ondisk_path, &sb) == -1) {
9278 if (errno != ENOENT) {
9279 err = got_error_from_errno2("lstat",
9280 ondisk_path);
9281 goto done;
9283 *status = GOT_STATUS_NONEXISTENT;
9287 done:
9288 free(ondisk_path);
9289 return err;
9292 static const struct got_error *
9293 patch_can_rm(const char *path, unsigned char status,
9294 unsigned char staged_status)
9296 if (status == GOT_STATUS_NONEXISTENT)
9297 return got_error_set_errno(ENOENT, path);
9298 if (status != GOT_STATUS_NO_CHANGE &&
9299 status != GOT_STATUS_ADD &&
9300 status != GOT_STATUS_MODIFY &&
9301 status != GOT_STATUS_MODE_CHANGE)
9302 return got_error_path(path, GOT_ERR_FILE_STATUS);
9303 if (staged_status == GOT_STATUS_DELETE)
9304 return got_error_path(path, GOT_ERR_FILE_STATUS);
9305 return NULL;
9308 static const struct got_error *
9309 patch_can_add(const char *path, unsigned char status)
9311 if (status != GOT_STATUS_NONEXISTENT)
9312 return got_error_path(path, GOT_ERR_FILE_STATUS);
9313 return NULL;
9316 static const struct got_error *
9317 patch_can_edit(const char *path, unsigned char status,
9318 unsigned char staged_status)
9320 if (status == GOT_STATUS_NONEXISTENT)
9321 return got_error_set_errno(ENOENT, path);
9322 if (status != GOT_STATUS_NO_CHANGE &&
9323 status != GOT_STATUS_ADD &&
9324 status != GOT_STATUS_MODIFY)
9325 return got_error_path(path, GOT_ERR_FILE_STATUS);
9326 if (staged_status == GOT_STATUS_DELETE)
9327 return got_error_path(path, GOT_ERR_FILE_STATUS);
9328 return NULL;
9331 const struct got_error *
9332 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9333 char **fileindex_path, struct got_worktree *worktree)
9335 return open_fileindex(fileindex, fileindex_path, worktree);
9338 const struct got_error *
9339 got_worktree_patch_check_path(const char *old, const char *new,
9340 char **oldpath, char **newpath, struct got_worktree *worktree,
9341 struct got_repository *repo, struct got_fileindex *fileindex)
9343 const struct got_error *err = NULL;
9344 int file_renamed = 0;
9345 unsigned char status_old, staged_status_old;
9346 unsigned char status_new, staged_status_new;
9348 *oldpath = NULL;
9349 *newpath = NULL;
9351 err = patch_check_path(old != NULL ? old : new, oldpath,
9352 &status_old, &staged_status_old, fileindex, worktree, repo);
9353 if (err)
9354 goto done;
9356 err = patch_check_path(new != NULL ? new : old, newpath,
9357 &status_new, &staged_status_new, fileindex, worktree, repo);
9358 if (err)
9359 goto done;
9361 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9362 file_renamed = 1;
9364 if (old != NULL && new == NULL)
9365 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9366 else if (file_renamed) {
9367 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9368 if (err == NULL)
9369 err = patch_can_add(*newpath, status_new);
9370 } else if (old == NULL)
9371 err = patch_can_add(*newpath, status_new);
9372 else
9373 err = patch_can_edit(*newpath, status_new, staged_status_new);
9375 done:
9376 if (err) {
9377 free(*oldpath);
9378 *oldpath = NULL;
9379 free(*newpath);
9380 *newpath = NULL;
9382 return err;
9385 const struct got_error *
9386 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9387 struct got_worktree *worktree, struct got_fileindex *fileindex,
9388 got_worktree_checkout_cb progress_cb, void *progress_arg)
9390 struct schedule_addition_args saa;
9392 memset(&saa, 0, sizeof(saa));
9393 saa.worktree = worktree;
9394 saa.fileindex = fileindex;
9395 saa.progress_cb = progress_cb;
9396 saa.progress_arg = progress_arg;
9397 saa.repo = repo;
9399 return worktree_status(worktree, path, fileindex, repo,
9400 schedule_addition, &saa, NULL, NULL, 1, 0);
9403 const struct got_error *
9404 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9405 struct got_worktree *worktree, struct got_fileindex *fileindex,
9406 got_worktree_delete_cb progress_cb, void *progress_arg)
9408 struct schedule_deletion_args sda;
9410 memset(&sda, 0, sizeof(sda));
9411 sda.worktree = worktree;
9412 sda.fileindex = fileindex;
9413 sda.progress_cb = progress_cb;
9414 sda.progress_arg = progress_arg;
9415 sda.repo = repo;
9416 sda.delete_local_mods = 0;
9417 sda.keep_on_disk = 0;
9418 sda.ignore_missing_paths = 0;
9419 sda.status_codes = NULL;
9421 return worktree_status(worktree, path, fileindex, repo,
9422 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9425 const struct got_error *
9426 got_worktree_patch_complete(struct got_fileindex *fileindex,
9427 const char *fileindex_path)
9429 const struct got_error *err = NULL;
9431 err = sync_fileindex(fileindex, fileindex_path);
9432 got_fileindex_free(fileindex);
9434 return err;