Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
71 {
72 const struct got_error *err = NULL;
73 char *path;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
79 free(path);
80 return err;
81 }
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
85 {
86 const struct got_error *err = NULL;
87 FILE *tmpfile = NULL;
88 char *tmppath = NULL;
89 char *path = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
93 path = NULL;
94 goto done;
95 }
97 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
98 if (err)
99 goto done;
101 if (content) {
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
105 goto done;
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
111 unlink(tmppath);
112 goto done;
115 done:
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
118 free(tmppath);
119 return err;
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
126 char *refstr = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
130 if (refstr == NULL)
131 return got_error_from_errno("got_ref_to_str");
132 } else {
133 refstr = strdup(got_ref_get_name(head_ref));
134 if (refstr == NULL)
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 free(refstr);
139 return err;
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
148 uuid_t uuid;
149 uint32_t uuid_status;
150 int obj_type;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
159 goto done;
162 err = got_ref_resolve(&commit_id, repo, head_ref);
163 if (err)
164 return err;
165 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (err)
167 return err;
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
179 goto done;
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
185 goto done;
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
189 goto done;
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
194 if (err)
195 goto done;
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
199 if (err)
200 goto done;
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
204 if (err)
205 goto done;
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
209 if (err)
210 goto done;
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 if (err)
213 goto done;
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
218 if (err)
219 goto done;
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
224 if (err)
225 goto done;
227 /* Generate UUID. */
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
231 goto done;
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
236 goto done;
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
239 if (err)
240 goto done;
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 if (err)
249 goto done;
251 done:
252 free(commit_id);
253 free(path_got);
254 free(formatstr);
255 free(absprefix);
256 free(basestr);
257 free(uuidstr);
258 return err;
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
273 free(absprefix);
274 return NULL;
277 const char *
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
293 path_got = NULL;
294 goto done;
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
300 goto done;
303 err = write_head_ref(path_got, head_ref);
304 if (err)
305 goto done;
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
309 done:
310 free(path_got);
311 if (err)
312 free(head_ref_name);
313 return err;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
328 char *id_str = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
334 path_got = NULL;
335 goto done;
338 err = got_object_open(&obj, repo, commit_id);
339 if (err)
340 return err;
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
344 goto done;
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
349 if (err)
350 goto done;
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
352 if (err)
353 goto done;
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
359 goto done;
361 done:
362 if (obj)
363 got_object_close(obj);
364 free(id_str);
365 free(path_got);
366 return err;
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
382 return NULL;
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
389 char *abspath;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 struct stat sb;
397 err = NULL;
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
405 free(abspath);
406 return err;
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
413 uint8_t fbuf1[8192];
414 uint8_t fbuf2[8192];
415 size_t flen1 = 0, flen2 = 0;
417 *same = 1;
419 for (;;) {
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
423 break;
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
428 break;
430 if (flen1 == 0) {
431 if (flen2 != 0)
432 *same = 0;
433 break;
434 } else if (flen2 == 0) {
435 if (flen1 != 0)
436 *same = 0;
437 break;
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
440 *same = 0;
441 break;
443 } else {
444 *same = 0;
445 break;
449 return err;
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
455 struct stat sb;
456 size_t size1, size2;
458 *same = 1;
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
462 size1 = sb.st_size;
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
466 size2 = sb.st_size;
468 if (size1 != size2) {
469 *same = 0;
470 return NULL;
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
484 uint8_t fbuf[65536];
485 size_t flen;
486 ssize_t outlen;
488 *outsize = 0;
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
493 for (;;) {
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 if (flen == 0) {
496 if (ferror(f))
497 return got_error_from_errno("fread");
498 if (feof(f))
499 break;
501 outlen = write(outfd, fbuf, flen);
502 if (outlen == -1)
503 return got_error_from_errno("write");
504 if (outlen != flen)
505 return got_error(GOT_ERR_IO);
506 *outsize += outlen;
509 return NULL;
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
526 *overlapcnt = 0;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 if (err)
530 return err;
532 if (same_content)
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
536 if (err)
537 return err;
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 if (err)
541 return err;
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
545 *overlapcnt = 1;
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
552 goto done;
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
556 goto done;
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 base_path_orig, "");
560 if (err)
561 goto done;
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv, "");
564 if (err)
565 goto done;
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2, "");
568 if (err)
569 goto done;
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 if (err)
572 goto done;
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 if (err)
575 goto done;
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (err)
578 goto done;
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
582 goto done;
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
588 path_deriv) < 0) {
589 err = got_error_from_errno("dprintf");
590 goto done;
592 if (size_orig > 0) {
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
597 path_orig) < 0) {
598 err = got_error_from_errno("dprintf");
599 goto done;
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
604 path_deriv2,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
609 goto done;
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 done:
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 err == NULL)
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
625 free(path_orig);
626 free(path_deriv);
627 free(path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
631 return err;
634 /*
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
640 */
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
650 int merged_fd = -1;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
653 int overlapcnt = 0;
654 char *parent = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
659 if (err)
660 return err;
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
664 goto done;
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
668 if (err)
669 goto done;
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 if (err) {
674 if (err->code != GOT_ERR_FILE_BINARY)
675 goto done;
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 ondisk_path);
679 if (err)
680 goto done;
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
685 if (err)
686 goto done;
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
690 goto done;
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
696 goto done;
698 merged_fd = -1;
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
703 f_merged);
704 if (err)
705 goto done;
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
710 goto done;
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
715 ondisk_path);
716 goto done;
718 done:
719 if (err) {
720 if (merged_path)
721 unlink(merged_path);
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
727 free(merged_path);
728 free(base_path);
729 free(parent);
730 return err;
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
735 size_t target_len)
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
742 ondisk_path);
743 return NULL;
746 /*
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
757 */
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 FILE *f = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
768 if (err)
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
774 goto done;
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
778 if (err)
779 goto done;
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
783 goto done;
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
796 goto done;
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
801 goto done;
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
805 goto done;
807 done:
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
810 free(path);
811 free(id_str);
812 free(label_deriv);
813 return err;
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
823 /*
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
828 */
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
838 struct stat sb;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
851 ondisk_path);
852 goto done;
854 ondisk_target[ondisk_len] = '\0';
856 if (blob_orig) {
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 if (err)
859 goto done;
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 path);
883 } else {
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
888 if (err)
889 goto done;
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 path);
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
897 if (err)
898 goto done;
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 path);
906 } else {
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 path);
917 done:
918 free(ancestor_target);
919 return err;
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
927 ssize_t target_len;
928 size_t n;
929 FILE *f;
931 *outfile = NULL;
933 f = got_opentemp();
934 if (f == NULL)
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
939 goto done;
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
944 goto done;
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
948 goto done;
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
952 goto done;
954 done:
955 if (err)
956 fclose(f);
957 else
958 *outfile = f;
959 return err;
962 /*
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
966 */
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
984 if (err)
985 return err;
987 if (blob_orig) {
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 parent) == -1) {
990 err = got_error_from_errno("asprintf");
991 base_path = NULL;
992 goto done;
995 err = got_opentemp_named(&blob_orig_path, &f_orig,
996 base_path, "");
997 if (err)
998 goto done;
999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 blob_orig);
1001 if (err)
1002 goto done;
1003 free(base_path);
1004 } else {
1006 * No common ancestor exists. This is an "add vs add" conflict
1007 * and we simply use an empty ancestor file to make both files
1008 * appear in the merged result in their entirety.
1010 f_orig = got_opentemp();
1011 if (f_orig == NULL) {
1012 err = got_error_from_errno("got_opentemp");
1013 goto done;
1017 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 err = got_error_from_errno("asprintf");
1019 base_path = NULL;
1020 goto done;
1023 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 if (err)
1025 goto done;
1026 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1027 blob_deriv);
1028 if (err)
1029 goto done;
1031 err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 if (err)
1033 goto done;
1034 if (asprintf(&label_deriv, "%s: commit %s",
1035 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 err = got_error_from_errno("asprintf");
1037 goto done;
1041 * In order the run a 3-way merge with a symlink we copy the symlink's
1042 * target path into a temporary file and use that file with diff3.
1044 if (S_ISLNK(st_mode)) {
1045 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1046 if (err)
1047 goto done;
1048 } else {
1049 int fd;
1050 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 if (fd == -1) {
1052 err = got_error_from_errno2("open", ondisk_path);
1053 goto done;
1055 f_deriv2 = fdopen(fd, "r");
1056 if (f_deriv2 == NULL) {
1057 err = got_error_from_errno2("fdopen", ondisk_path);
1058 close(fd);
1059 goto done;
1063 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 done:
1067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 err = got_error_from_errno("fclose");
1073 free(base_path);
1074 if (blob_orig_path) {
1075 unlink(blob_orig_path);
1076 free(blob_orig_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1082 free(id_str);
1083 free(label_deriv);
1084 free(parent);
1085 return err;
1088 static const struct got_error *
1089 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 int wt_fd, const char *path, struct got_object_id *blob_id)
1093 const struct got_error *err = NULL;
1094 struct got_fileindex_entry *new_ie;
1096 *new_iep = NULL;
1098 err = got_fileindex_entry_alloc(&new_ie, path);
1099 if (err)
1100 return err;
1102 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 blob_id->sha1, base_commit_id->sha1, 1);
1104 if (err)
1105 goto done;
1107 err = got_fileindex_entry_add(fileindex, new_ie);
1108 done:
1109 if (err)
1110 got_fileindex_entry_free(new_ie);
1111 else
1112 *new_iep = new_ie;
1113 return err;
1116 static mode_t
1117 get_ondisk_perms(int executable, mode_t st_mode)
1119 mode_t xbits = S_IXUSR;
1121 if (executable) {
1122 /* Map read bits to execute bits. */
1123 if (st_mode & S_IRGRP)
1124 xbits |= S_IXGRP;
1125 if (st_mode & S_IROTH)
1126 xbits |= S_IXOTH;
1127 return st_mode | xbits;
1130 return st_mode;
1133 static mode_t
1134 apply_umask(mode_t mode)
1136 mode_t um;
1138 um = umask(000);
1139 umask(um);
1140 return mode & ~um;
1143 /* forward declaration */
1144 static const struct got_error *
1145 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 const char *path, mode_t te_mode, mode_t st_mode,
1147 struct got_blob_object *blob, int restoring_missing_file,
1148 int reverting_versioned_file, int installing_bad_symlink,
1149 int path_is_unversioned, struct got_repository *repo,
1150 got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 * This function assumes that the provided symlink target points at a
1154 * safe location in the work tree!
1156 static const struct got_error *
1157 replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 const char *target_path, size_t target_len)
1160 const struct got_error *err = NULL;
1161 ssize_t elen;
1162 char etarget[PATH_MAX];
1163 int fd;
1165 *did_something = 0;
1168 * "Bad" symlinks (those pointing outside the work tree or into the
1169 * .got directory) are installed in the work tree as a regular file
1170 * which contains the bad symlink target path.
1171 * The new symlink target has already been checked for safety by our
1172 * caller. If we can successfully open a regular file then we simply
1173 * replace this file with a symlink below.
1175 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 if (fd == -1) {
1177 if (!got_err_open_nofollow_on_symlink())
1178 return got_error_from_errno2("open", ondisk_path);
1180 /* We are updating an existing on-disk symlink. */
1181 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 if (elen == -1)
1183 return got_error_from_errno2("readlink", ondisk_path);
1185 if (elen == target_len &&
1186 memcmp(etarget, target_path, target_len) == 0)
1187 return NULL; /* nothing to do */
1190 *did_something = 1;
1191 err = update_symlink(ondisk_path, target_path, target_len);
1192 if (fd != -1 && close(fd) == -1 && err == NULL)
1193 err = got_error_from_errno2("close", ondisk_path);
1194 return err;
1197 static const struct got_error *
1198 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 const struct got_error *err = NULL;
1202 char canonpath[PATH_MAX];
1203 char *path_got = NULL;
1205 *is_bad_symlink = 0;
1207 if (target_len >= sizeof(canonpath)) {
1208 *is_bad_symlink = 1;
1209 return NULL;
1213 * We do not use realpath(3) to resolve the symlink's target
1214 * path because we don't want to resolve symlinks recursively.
1215 * Instead we make the path absolute and then canonicalize it.
1216 * Relative symlink target lookup should begin at the directory
1217 * in which the blob object is being installed.
1219 if (!got_path_is_absolute(target_path)) {
1220 char *abspath, *parent;
1221 err = got_path_dirname(&parent, ondisk_path);
1222 if (err)
1223 return err;
1224 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 free(parent);
1226 return got_error_from_errno("asprintf");
1228 free(parent);
1229 if (strlen(abspath) >= sizeof(canonpath)) {
1230 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 free(abspath);
1232 return err;
1234 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 free(abspath);
1236 if (err)
1237 return err;
1238 } else {
1239 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 if (err)
1241 return err;
1244 /* Only allow symlinks pointing at paths within the work tree. */
1245 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 *is_bad_symlink = 1;
1247 return NULL;
1250 /* Do not allow symlinks pointing into the .got directory. */
1251 if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 GOT_WORKTREE_GOT_DIR) == -1)
1253 return got_error_from_errno("asprintf");
1254 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 *is_bad_symlink = 1;
1257 free(path_got);
1258 return NULL;
1261 static const struct got_error *
1262 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 int restoring_missing_file, int reverting_versioned_file,
1265 int path_is_unversioned, int allow_bad_symlinks,
1266 struct got_repository *repo,
1267 got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 const struct got_error *err = NULL;
1270 char target_path[PATH_MAX];
1271 size_t len, target_len = 0;
1272 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 *is_bad_symlink = 0;
1278 * Blob object content specifies the target path of the link.
1279 * If a symbolic link cannot be installed we instead create
1280 * a regular file which contains the link target path stored
1281 * in the blob object.
1283 do {
1284 err = got_object_blob_read_block(&len, blob);
1285 if (err)
1286 return err;
1288 if (len + target_len >= sizeof(target_path)) {
1289 /* Path too long; install as a regular file. */
1290 *is_bad_symlink = 1;
1291 got_object_blob_rewind(blob);
1292 return install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 restoring_missing_file, reverting_versioned_file,
1295 1, path_is_unversioned, repo, progress_cb,
1296 progress_arg);
1298 if (len > 0) {
1299 /* Skip blob object header first time around. */
1300 memcpy(target_path + target_len, buf + hdrlen,
1301 len - hdrlen);
1302 target_len += len - hdrlen;
1303 hdrlen = 0;
1305 } while (len != 0);
1306 target_path[target_len] = '\0';
1308 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 ondisk_path, worktree->root_path);
1310 if (err)
1311 return err;
1313 if (*is_bad_symlink && !allow_bad_symlinks) {
1314 /* install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 path_is_unversioned, repo, progress_cb, progress_arg);
1320 return err;
1323 if (symlink(target_path, ondisk_path) == -1) {
1324 if (errno == EEXIST) {
1325 int symlink_replaced;
1326 if (path_is_unversioned) {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_UNVERSIONED, path);
1329 return err;
1331 err = replace_existing_symlink(&symlink_replaced,
1332 ondisk_path, target_path, target_len);
1333 if (err)
1334 return err;
1335 if (progress_cb) {
1336 if (symlink_replaced) {
1337 err = (*progress_cb)(progress_arg,
1338 reverting_versioned_file ?
1339 GOT_STATUS_REVERT :
1340 GOT_STATUS_UPDATE, path);
1341 } else {
1342 err = (*progress_cb)(progress_arg,
1343 GOT_STATUS_EXISTS, path);
1346 return err; /* Nothing else to do. */
1349 if (errno == ENOENT) {
1350 char *parent;
1351 err = got_path_dirname(&parent, ondisk_path);
1352 if (err)
1353 return err;
1354 err = add_dir_on_disk(worktree, parent);
1355 free(parent);
1356 if (err)
1357 return err;
1359 * Retry, and fall through to error handling
1360 * below if this second attempt fails.
1362 if (symlink(target_path, ondisk_path) != -1) {
1363 err = NULL; /* success */
1364 return err;
1368 /* Handle errors from first or second creation attempt. */
1369 if (errno == ENAMETOOLONG) {
1370 /* bad target path; install as a regular file */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 err = install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file, 1,
1376 path_is_unversioned, repo,
1377 progress_cb, progress_arg);
1378 } else if (errno == ENOTDIR) {
1379 err = got_error_path(ondisk_path,
1380 GOT_ERR_FILE_OBSTRUCTED);
1381 } else {
1382 err = got_error_from_errno3("symlink",
1383 target_path, ondisk_path);
1385 } else if (progress_cb)
1386 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 return err;
1391 static const struct got_error *
1392 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 const char *path, mode_t te_mode, mode_t st_mode,
1394 struct got_blob_object *blob, int restoring_missing_file,
1395 int reverting_versioned_file, int installing_bad_symlink,
1396 int path_is_unversioned, struct got_repository *repo,
1397 got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 const struct got_error *err = NULL;
1400 int fd = -1;
1401 size_t len, hdrlen;
1402 int update = 0;
1403 char *tmppath = NULL;
1404 mode_t mode;
1406 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 O_CLOEXEC, mode);
1409 if (fd == -1) {
1410 if (errno == ENOENT) {
1411 char *parent;
1412 err = got_path_dirname(&parent, path);
1413 if (err)
1414 return err;
1415 err = add_dir_on_disk(worktree, parent);
1416 free(parent);
1417 if (err)
1418 return err;
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 mode);
1422 if (fd == -1)
1423 return got_error_from_errno2("open",
1424 ondisk_path);
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1429 goto done;
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1436 goto done;
1437 } else {
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1439 ondisk_path, "");
1440 if (err)
1441 goto done;
1442 update = 1;
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1446 tmppath);
1447 goto done;
1450 } else
1451 return got_error_from_errno2("open", ondisk_path);
1454 if (progress_cb) {
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 path);
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 path);
1461 else
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 if (err)
1465 goto done;
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1469 do {
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1472 if (err)
1473 break;
1474 if (len > 0) {
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 if (outlen == -1) {
1478 err = got_error_from_errno("write");
1479 goto done;
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1482 goto done;
1484 hdrlen = 0;
1486 } while (len != 0);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1490 goto done;
1493 if (update) {
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1496 goto done;
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1500 ondisk_path);
1501 goto done;
1503 free(tmppath);
1504 tmppath = NULL;
1507 done:
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1512 free(tmppath);
1513 return err;
1516 static const struct got_error *skip_one_line(FILE *);
1519 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1520 * conflict marker is found in newly added lines only.
1522 static const struct got_error *
1523 get_modified_file_content_status(unsigned char *status,
1524 struct got_blob_object *blob, const char *path, struct stat *sb,
1525 FILE *ondisk_file)
1527 const struct got_error *err, *free_err;
1528 const char *markers[3] = {
1529 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1530 GOT_DIFF_CONFLICT_MARKER_SEP,
1531 GOT_DIFF_CONFLICT_MARKER_END
1533 FILE *f1 = NULL;
1534 struct got_diffreg_result *diffreg_result = NULL;
1535 struct diff_result *r;
1536 int nchunks_parsed, n, i = 0, ln = 0;
1537 char *line = NULL;
1538 size_t linesize = 0;
1539 ssize_t linelen;
1541 if (*status != GOT_STATUS_MODIFY)
1542 return NULL;
1544 f1 = got_opentemp();
1545 if (f1 == NULL)
1546 return got_error_from_errno("got_opentemp");
1548 if (blob) {
1549 got_object_blob_rewind(blob);
1550 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1551 if (err)
1552 goto done;
1555 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1556 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1557 if (err)
1558 goto done;
1560 if (fseek(ondisk_file, 0L, SEEK_SET) == -1) {
1561 err = got_ferror(ondisk_file, GOT_ERR_IO);
1562 goto done;
1565 r = diffreg_result->result;
1567 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1568 struct diff_chunk *c;
1569 struct diff_chunk_context cc = {};
1570 int clc, crc;
1573 * We can optimise a little by advancing straight
1574 * to the next chunk if this one has no added lines.
1576 c = diff_chunk_get(r, n);
1577 clc = diff_chunk_get_left_count(c);
1578 crc = diff_chunk_get_right_count(c);
1580 if (!crc && clc) {
1581 nchunks_parsed = 1;
1582 continue; /* removed lines */
1585 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1587 while (ln < cc.right.start) {
1588 err = skip_one_line(ondisk_file);
1589 if (err)
1590 goto done;
1591 ++ln;
1594 while (ln < cc.right.end) {
1595 linelen = getline(&line, &linesize, ondisk_file);
1596 if (linelen == -1) {
1597 if (feof(ondisk_file))
1598 break;
1599 err = got_ferror(ondisk_file, GOT_ERR_IO);
1600 break;
1603 if (line && strncmp(line, markers[i],
1604 strlen(markers[i])) == 0) {
1605 if (strcmp(markers[i],
1606 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1607 *status = GOT_STATUS_CONFLICT;
1608 goto done;
1609 } else
1610 i++;
1612 ++ln;
1616 done:
1617 free(line);
1618 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1619 err = got_error_from_errno("fclose");
1620 free_err = got_diffreg_result_free(diffreg_result);
1621 if (err == NULL)
1622 err = free_err;
1624 return err;
1627 static int
1628 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1630 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1631 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1634 static int
1635 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1637 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1638 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1639 ie->mtime_sec == sb->st_mtim.tv_sec &&
1640 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1641 ie->size == (sb->st_size & 0xffffffff) &&
1642 !xbit_differs(ie, sb->st_mode));
1645 static unsigned char
1646 get_staged_status(struct got_fileindex_entry *ie)
1648 switch (got_fileindex_entry_stage_get(ie)) {
1649 case GOT_FILEIDX_STAGE_ADD:
1650 return GOT_STATUS_ADD;
1651 case GOT_FILEIDX_STAGE_DELETE:
1652 return GOT_STATUS_DELETE;
1653 case GOT_FILEIDX_STAGE_MODIFY:
1654 return GOT_STATUS_MODIFY;
1655 default:
1656 return GOT_STATUS_NO_CHANGE;
1660 static const struct got_error *
1661 get_symlink_modification_status(unsigned char *status,
1662 struct got_fileindex_entry *ie, const char *abspath,
1663 int dirfd, const char *de_name, struct got_blob_object *blob)
1665 const struct got_error *err = NULL;
1666 char target_path[PATH_MAX];
1667 char etarget[PATH_MAX];
1668 ssize_t elen;
1669 size_t len, target_len = 0;
1670 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1671 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1673 *status = GOT_STATUS_NO_CHANGE;
1675 /* Blob object content specifies the target path of the link. */
1676 do {
1677 err = got_object_blob_read_block(&len, blob);
1678 if (err)
1679 return err;
1680 if (len + target_len >= sizeof(target_path)) {
1682 * Should not happen. The blob contents were OK
1683 * when this symlink was installed.
1685 return got_error(GOT_ERR_NO_SPACE);
1687 if (len > 0) {
1688 /* Skip blob object header first time around. */
1689 memcpy(target_path + target_len, buf + hdrlen,
1690 len - hdrlen);
1691 target_len += len - hdrlen;
1692 hdrlen = 0;
1694 } while (len != 0);
1695 target_path[target_len] = '\0';
1697 if (dirfd != -1) {
1698 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1699 if (elen == -1)
1700 return got_error_from_errno2("readlinkat", abspath);
1701 } else {
1702 elen = readlink(abspath, etarget, sizeof(etarget));
1703 if (elen == -1)
1704 return got_error_from_errno2("readlink", abspath);
1707 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1708 *status = GOT_STATUS_MODIFY;
1710 return NULL;
1713 static const struct got_error *
1714 get_file_status(unsigned char *status, struct stat *sb,
1715 struct got_fileindex_entry *ie, const char *abspath,
1716 int dirfd, const char *de_name, struct got_repository *repo)
1718 const struct got_error *err = NULL;
1719 struct got_object_id id;
1720 size_t hdrlen;
1721 int fd = -1, fd1 = -1;
1722 FILE *f = NULL;
1723 uint8_t fbuf[8192];
1724 struct got_blob_object *blob = NULL;
1725 size_t flen, blen;
1726 unsigned char staged_status;
1728 staged_status = get_staged_status(ie);
1729 *status = GOT_STATUS_NO_CHANGE;
1730 memset(sb, 0, sizeof(*sb));
1733 * Whenever the caller provides a directory descriptor and a
1734 * directory entry name for the file, use them! This prevents
1735 * race conditions if filesystem paths change beneath our feet.
1737 if (dirfd != -1) {
1738 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1739 if (errno == ENOENT) {
1740 if (got_fileindex_entry_has_file_on_disk(ie))
1741 *status = GOT_STATUS_MISSING;
1742 else
1743 *status = GOT_STATUS_DELETE;
1744 goto done;
1746 err = got_error_from_errno2("fstatat", abspath);
1747 goto done;
1749 } else {
1750 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1751 if (fd == -1 && errno != ENOENT &&
1752 !got_err_open_nofollow_on_symlink())
1753 return got_error_from_errno2("open", abspath);
1754 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1755 if (lstat(abspath, sb) == -1)
1756 return got_error_from_errno2("lstat", abspath);
1757 } else if (fd == -1 || fstat(fd, sb) == -1) {
1758 if (errno == ENOENT) {
1759 if (got_fileindex_entry_has_file_on_disk(ie))
1760 *status = GOT_STATUS_MISSING;
1761 else
1762 *status = GOT_STATUS_DELETE;
1763 goto done;
1765 err = got_error_from_errno2("fstat", abspath);
1766 goto done;
1770 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1771 *status = GOT_STATUS_OBSTRUCTED;
1772 goto done;
1775 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1776 *status = GOT_STATUS_DELETE;
1777 goto done;
1778 } else if (!got_fileindex_entry_has_blob(ie) &&
1779 staged_status != GOT_STATUS_ADD) {
1780 *status = GOT_STATUS_ADD;
1781 goto done;
1784 if (!stat_info_differs(ie, sb))
1785 goto done;
1787 if (S_ISLNK(sb->st_mode) &&
1788 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1789 *status = GOT_STATUS_MODIFY;
1790 goto done;
1793 if (staged_status == GOT_STATUS_MODIFY ||
1794 staged_status == GOT_STATUS_ADD)
1795 got_fileindex_entry_get_staged_blob_id(&id, ie);
1796 else
1797 got_fileindex_entry_get_blob_id(&id, ie);
1799 fd1 = got_opentempfd();
1800 if (fd1 == -1) {
1801 err = got_error_from_errno("got_opentempfd");
1802 goto done;
1804 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1805 if (err)
1806 goto done;
1808 if (S_ISLNK(sb->st_mode)) {
1809 err = get_symlink_modification_status(status, ie,
1810 abspath, dirfd, de_name, blob);
1811 goto done;
1814 if (dirfd != -1) {
1815 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1816 if (fd == -1) {
1817 err = got_error_from_errno2("openat", abspath);
1818 goto done;
1822 f = fdopen(fd, "r");
1823 if (f == NULL) {
1824 err = got_error_from_errno2("fdopen", abspath);
1825 goto done;
1827 fd = -1;
1828 hdrlen = got_object_blob_get_hdrlen(blob);
1829 for (;;) {
1830 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1831 err = got_object_blob_read_block(&blen, blob);
1832 if (err)
1833 goto done;
1834 /* Skip length of blob object header first time around. */
1835 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1836 if (flen == 0 && ferror(f)) {
1837 err = got_error_from_errno("fread");
1838 goto done;
1840 if (blen - hdrlen == 0) {
1841 if (flen != 0)
1842 *status = GOT_STATUS_MODIFY;
1843 break;
1844 } else if (flen == 0) {
1845 if (blen - hdrlen != 0)
1846 *status = GOT_STATUS_MODIFY;
1847 break;
1848 } else if (blen - hdrlen == flen) {
1849 /* Skip blob object header first time around. */
1850 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1851 *status = GOT_STATUS_MODIFY;
1852 break;
1854 } else {
1855 *status = GOT_STATUS_MODIFY;
1856 break;
1858 hdrlen = 0;
1861 if (*status == GOT_STATUS_MODIFY) {
1862 rewind(f);
1863 err = get_modified_file_content_status(status, blob, ie->path,
1864 sb, f);
1865 } else if (xbit_differs(ie, sb->st_mode))
1866 *status = GOT_STATUS_MODE_CHANGE;
1867 done:
1868 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1869 err = got_error_from_errno("close");
1870 if (blob)
1871 got_object_blob_close(blob);
1872 if (f != NULL && fclose(f) == EOF && err == NULL)
1873 err = got_error_from_errno2("fclose", abspath);
1874 if (fd != -1 && close(fd) == -1 && err == NULL)
1875 err = got_error_from_errno2("close", abspath);
1876 return err;
1880 * Update timestamps in the file index if a file is unmodified and
1881 * we had to run a full content comparison to find out.
1883 static const struct got_error *
1884 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1885 struct got_fileindex_entry *ie, struct stat *sb)
1887 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1888 return got_fileindex_entry_update(ie, wt_fd, path,
1889 ie->blob_sha1, ie->commit_sha1, 1);
1891 return NULL;
1894 static const struct got_error *
1895 update_blob(struct got_worktree *worktree,
1896 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1897 struct got_tree_entry *te, const char *path,
1898 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1899 void *progress_arg)
1901 const struct got_error *err = NULL;
1902 struct got_blob_object *blob = NULL;
1903 char *ondisk_path = NULL;
1904 unsigned char status = GOT_STATUS_NO_CHANGE;
1905 struct stat sb;
1906 int fd1 = -1, fd2 = -1;
1908 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1909 return got_error_from_errno("asprintf");
1911 if (ie) {
1912 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1913 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1914 goto done;
1916 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1917 repo);
1918 if (err)
1919 goto done;
1920 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1921 sb.st_mode = got_fileindex_perms_to_st(ie);
1922 } else {
1923 if (stat(ondisk_path, &sb) == -1) {
1924 if (errno != ENOENT) {
1925 err = got_error_from_errno2("stat",
1926 ondisk_path);
1927 goto done;
1929 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1930 status = GOT_STATUS_UNVERSIONED;
1931 } else {
1932 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1933 status = GOT_STATUS_UNVERSIONED;
1934 else
1935 status = GOT_STATUS_OBSTRUCTED;
1939 if (status == GOT_STATUS_OBSTRUCTED) {
1940 if (ie)
1941 got_fileindex_entry_mark_skipped(ie);
1942 err = (*progress_cb)(progress_arg, status, path);
1943 goto done;
1945 if (status == GOT_STATUS_CONFLICT) {
1946 if (ie)
1947 got_fileindex_entry_mark_skipped(ie);
1948 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1949 path);
1950 goto done;
1953 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1954 (S_ISLNK(te->mode) ||
1955 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1957 * This is a regular file or an installed bad symlink.
1958 * If the file index indicates that this file is already
1959 * up-to-date with respect to the repository we can skip
1960 * updating contents of this file.
1962 if (got_fileindex_entry_has_commit(ie) &&
1963 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1964 SHA1_DIGEST_LENGTH) == 0) {
1965 /* Same commit. */
1966 err = sync_timestamps(worktree->root_fd,
1967 path, status, ie, &sb);
1968 if (err)
1969 goto done;
1970 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1971 path);
1972 goto done;
1974 if (got_fileindex_entry_has_blob(ie) &&
1975 memcmp(ie->blob_sha1, te->id.sha1,
1976 SHA1_DIGEST_LENGTH) == 0) {
1977 /* Different commit but the same blob. */
1978 err = sync_timestamps(worktree->root_fd,
1979 path, status, ie, &sb);
1980 if (err)
1981 goto done;
1982 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1983 path);
1984 goto done;
1988 fd1 = got_opentempfd();
1989 if (fd1 == -1) {
1990 err = got_error_from_errno("got_opentempfd");
1991 goto done;
1993 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1994 if (err)
1995 goto done;
1997 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1998 int update_timestamps;
1999 struct got_blob_object *blob2 = NULL;
2000 char *label_orig = NULL;
2001 if (got_fileindex_entry_has_blob(ie)) {
2002 fd2 = got_opentempfd();
2003 if (fd2 == -1) {
2004 err = got_error_from_errno("got_opentempfd");
2005 goto done;
2007 struct got_object_id id2;
2008 got_fileindex_entry_get_blob_id(&id2, ie);
2009 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2010 fd2);
2011 if (err)
2012 goto done;
2014 if (got_fileindex_entry_has_commit(ie)) {
2015 char id_str[SHA1_DIGEST_STRING_LENGTH];
2016 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2017 sizeof(id_str)) == NULL) {
2018 err = got_error_path(id_str,
2019 GOT_ERR_BAD_OBJ_ID_STR);
2020 goto done;
2022 if (asprintf(&label_orig, "%s: commit %s",
2023 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2024 err = got_error_from_errno("asprintf");
2025 goto done;
2028 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2029 char *link_target;
2030 err = got_object_blob_read_to_str(&link_target, blob);
2031 if (err)
2032 goto done;
2033 err = merge_symlink(worktree, blob2, ondisk_path, path,
2034 label_orig, link_target, worktree->base_commit_id,
2035 repo, progress_cb, progress_arg);
2036 free(link_target);
2037 } else {
2038 err = merge_blob(&update_timestamps, worktree, blob2,
2039 ondisk_path, path, sb.st_mode, label_orig, blob,
2040 worktree->base_commit_id, repo,
2041 progress_cb, progress_arg);
2043 free(label_orig);
2044 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2045 err = got_error_from_errno("close");
2046 goto done;
2048 if (blob2)
2049 got_object_blob_close(blob2);
2050 if (err)
2051 goto done;
2053 * Do not update timestamps of files with local changes.
2054 * Otherwise, a future status walk would treat them as
2055 * unmodified files again.
2057 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2058 blob->id.sha1, worktree->base_commit_id->sha1,
2059 update_timestamps);
2060 } else if (status == GOT_STATUS_MODE_CHANGE) {
2061 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2062 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2063 } else if (status == GOT_STATUS_DELETE) {
2064 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2065 if (err)
2066 goto done;
2067 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2068 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2069 if (err)
2070 goto done;
2071 } else {
2072 int is_bad_symlink = 0;
2073 if (S_ISLNK(te->mode)) {
2074 err = install_symlink(&is_bad_symlink, worktree,
2075 ondisk_path, path, blob,
2076 status == GOT_STATUS_MISSING, 0,
2077 status == GOT_STATUS_UNVERSIONED, 0,
2078 repo, progress_cb, progress_arg);
2079 } else {
2080 err = install_blob(worktree, ondisk_path, path,
2081 te->mode, sb.st_mode, blob,
2082 status == GOT_STATUS_MISSING, 0, 0,
2083 status == GOT_STATUS_UNVERSIONED, repo,
2084 progress_cb, progress_arg);
2086 if (err)
2087 goto done;
2089 if (ie) {
2090 err = got_fileindex_entry_update(ie,
2091 worktree->root_fd, path, blob->id.sha1,
2092 worktree->base_commit_id->sha1, 1);
2093 } else {
2094 err = create_fileindex_entry(&ie, fileindex,
2095 worktree->base_commit_id, worktree->root_fd, path,
2096 &blob->id);
2098 if (err)
2099 goto done;
2101 if (is_bad_symlink) {
2102 got_fileindex_entry_filetype_set(ie,
2103 GOT_FILEIDX_MODE_BAD_SYMLINK);
2107 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2108 err = got_error_from_errno("close");
2109 goto done;
2111 got_object_blob_close(blob);
2112 done:
2113 free(ondisk_path);
2114 return err;
2117 static const struct got_error *
2118 remove_ondisk_file(const char *root_path, const char *path)
2120 const struct got_error *err = NULL;
2121 char *ondisk_path = NULL, *parent = NULL;
2123 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2124 return got_error_from_errno("asprintf");
2126 if (unlink(ondisk_path) == -1) {
2127 if (errno != ENOENT)
2128 err = got_error_from_errno2("unlink", ondisk_path);
2129 } else {
2130 size_t root_len = strlen(root_path);
2131 err = got_path_dirname(&parent, ondisk_path);
2132 if (err)
2133 goto done;
2134 while (got_path_cmp(parent, root_path,
2135 strlen(parent), root_len) != 0) {
2136 free(ondisk_path);
2137 ondisk_path = parent;
2138 parent = NULL;
2139 if (rmdir(ondisk_path) == -1) {
2140 if (errno != ENOTEMPTY)
2141 err = got_error_from_errno2("rmdir",
2142 ondisk_path);
2143 break;
2145 err = got_path_dirname(&parent, ondisk_path);
2146 if (err)
2147 break;
2150 done:
2151 free(ondisk_path);
2152 free(parent);
2153 return err;
2156 static const struct got_error *
2157 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2158 struct got_fileindex_entry *ie, struct got_repository *repo,
2159 got_worktree_checkout_cb progress_cb, void *progress_arg)
2161 const struct got_error *err = NULL;
2162 unsigned char status;
2163 struct stat sb;
2164 char *ondisk_path;
2166 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2167 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2169 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2170 == -1)
2171 return got_error_from_errno("asprintf");
2173 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2174 if (err)
2175 goto done;
2177 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2178 char ondisk_target[PATH_MAX];
2179 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2180 sizeof(ondisk_target));
2181 if (ondisk_len == -1) {
2182 err = got_error_from_errno2("readlink", ondisk_path);
2183 goto done;
2185 ondisk_target[ondisk_len] = '\0';
2186 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2187 NULL, NULL, /* XXX pass common ancestor info? */
2188 ondisk_target, ondisk_path);
2189 if (err)
2190 goto done;
2191 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2192 ie->path);
2193 goto done;
2196 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2197 status == GOT_STATUS_ADD) {
2198 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2199 if (err)
2200 goto done;
2202 * Preserve the working file and change the deleted blob's
2203 * entry into a schedule-add entry.
2205 err = got_fileindex_entry_update(ie, worktree->root_fd,
2206 ie->path, NULL, NULL, 0);
2207 } else {
2208 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2209 if (err)
2210 goto done;
2211 if (status == GOT_STATUS_NO_CHANGE) {
2212 err = remove_ondisk_file(worktree->root_path, ie->path);
2213 if (err)
2214 goto done;
2216 got_fileindex_entry_remove(fileindex, ie);
2218 done:
2219 free(ondisk_path);
2220 return err;
2223 struct diff_cb_arg {
2224 struct got_fileindex *fileindex;
2225 struct got_worktree *worktree;
2226 struct got_repository *repo;
2227 got_worktree_checkout_cb progress_cb;
2228 void *progress_arg;
2229 got_cancel_cb cancel_cb;
2230 void *cancel_arg;
2233 static const struct got_error *
2234 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2235 struct got_tree_entry *te, const char *parent_path)
2237 struct diff_cb_arg *a = arg;
2239 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2240 return got_error(GOT_ERR_CANCELLED);
2242 return update_blob(a->worktree, a->fileindex, ie, te,
2243 ie->path, a->repo, a->progress_cb, a->progress_arg);
2246 static const struct got_error *
2247 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2249 struct diff_cb_arg *a = arg;
2251 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2252 return got_error(GOT_ERR_CANCELLED);
2254 return delete_blob(a->worktree, a->fileindex, ie,
2255 a->repo, a->progress_cb, a->progress_arg);
2258 static const struct got_error *
2259 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2261 struct diff_cb_arg *a = arg;
2262 const struct got_error *err;
2263 char *path;
2265 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2266 return got_error(GOT_ERR_CANCELLED);
2268 if (got_object_tree_entry_is_submodule(te))
2269 return NULL;
2271 if (asprintf(&path, "%s%s%s", parent_path,
2272 parent_path[0] ? "/" : "", te->name)
2273 == -1)
2274 return got_error_from_errno("asprintf");
2276 if (S_ISDIR(te->mode))
2277 err = add_dir_on_disk(a->worktree, path);
2278 else
2279 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2280 a->repo, a->progress_cb, a->progress_arg);
2282 free(path);
2283 return err;
2286 const struct got_error *
2287 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2289 uint32_t uuid_status;
2291 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2292 if (uuid_status != uuid_s_ok) {
2293 *uuidstr = NULL;
2294 return got_error_uuid(uuid_status, "uuid_to_string");
2297 return NULL;
2300 static const struct got_error *
2301 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2303 const struct got_error *err = NULL;
2304 char *uuidstr = NULL;
2306 *refname = NULL;
2308 err = got_worktree_get_uuid(&uuidstr, worktree);
2309 if (err)
2310 return err;
2312 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2313 err = got_error_from_errno("asprintf");
2314 *refname = NULL;
2316 free(uuidstr);
2317 return err;
2320 const struct got_error *
2321 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2322 const char *prefix)
2324 return get_ref_name(refname, worktree, prefix);
2327 const struct got_error *
2328 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2333 static const struct got_error *
2334 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2336 return get_ref_name(refname, worktree,
2337 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2340 static const struct got_error *
2341 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2343 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2346 static const struct got_error *
2347 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2349 return get_ref_name(refname, worktree,
2350 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2353 static const struct got_error *
2354 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2356 return get_ref_name(refname, worktree,
2357 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2360 static const struct got_error *
2361 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2363 return get_ref_name(refname, worktree,
2364 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2367 static const struct got_error *
2368 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2370 return get_ref_name(refname, worktree,
2371 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2374 static const struct got_error *
2375 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2377 return get_ref_name(refname, worktree,
2378 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2381 static const struct got_error *
2382 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2384 return get_ref_name(refname, worktree,
2385 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2388 const struct got_error *
2389 got_worktree_get_histedit_script_path(char **path,
2390 struct got_worktree *worktree)
2392 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2393 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2394 *path = NULL;
2395 return got_error_from_errno("asprintf");
2397 return NULL;
2400 static const struct got_error *
2401 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2403 return get_ref_name(refname, worktree,
2404 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2407 static const struct got_error *
2408 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2410 return get_ref_name(refname, worktree,
2411 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2415 * Prevent Git's garbage collector from deleting our base commit by
2416 * setting a reference to our base commit's ID.
2418 static const struct got_error *
2419 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2421 const struct got_error *err = NULL;
2422 struct got_reference *ref = NULL;
2423 char *refname;
2425 err = got_worktree_get_base_ref_name(&refname, worktree);
2426 if (err)
2427 return err;
2429 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2430 if (err)
2431 goto done;
2433 err = got_ref_write(ref, repo);
2434 done:
2435 free(refname);
2436 if (ref)
2437 got_ref_close(ref);
2438 return err;
2441 static const struct got_error *
2442 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2444 const struct got_error *err = NULL;
2446 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2447 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2448 err = got_error_from_errno("asprintf");
2449 *fileindex_path = NULL;
2451 return err;
2455 static const struct got_error *
2456 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2457 struct got_worktree *worktree)
2459 const struct got_error *err = NULL;
2460 FILE *index = NULL;
2462 *fileindex_path = NULL;
2463 *fileindex = got_fileindex_alloc();
2464 if (*fileindex == NULL)
2465 return got_error_from_errno("got_fileindex_alloc");
2467 err = get_fileindex_path(fileindex_path, worktree);
2468 if (err)
2469 goto done;
2471 index = fopen(*fileindex_path, "rbe");
2472 if (index == NULL) {
2473 if (errno != ENOENT)
2474 err = got_error_from_errno2("fopen", *fileindex_path);
2475 } else {
2476 err = got_fileindex_read(*fileindex, index);
2477 if (fclose(index) == EOF && err == NULL)
2478 err = got_error_from_errno("fclose");
2480 done:
2481 if (err) {
2482 free(*fileindex_path);
2483 *fileindex_path = NULL;
2484 got_fileindex_free(*fileindex);
2485 *fileindex = NULL;
2487 return err;
2490 struct bump_base_commit_id_arg {
2491 struct got_object_id *base_commit_id;
2492 const char *path;
2493 size_t path_len;
2494 const char *entry_name;
2495 got_worktree_checkout_cb progress_cb;
2496 void *progress_arg;
2499 static const struct got_error *
2500 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2502 const struct got_error *err;
2503 struct bump_base_commit_id_arg *a = arg;
2505 if (a->entry_name) {
2506 if (strcmp(ie->path, a->path) != 0)
2507 return NULL;
2508 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2509 return NULL;
2511 if (got_fileindex_entry_was_skipped(ie))
2512 return NULL;
2514 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2515 SHA1_DIGEST_LENGTH) == 0)
2516 return NULL;
2518 if (a->progress_cb) {
2519 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2520 ie->path);
2521 if (err)
2522 return err;
2524 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2525 return NULL;
2528 /* Bump base commit ID of all files within an updated part of the work tree. */
2529 static const struct got_error *
2530 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2531 struct got_fileindex *fileindex,
2532 got_worktree_checkout_cb progress_cb, void *progress_arg)
2534 struct bump_base_commit_id_arg bbc_arg;
2536 bbc_arg.base_commit_id = worktree->base_commit_id;
2537 bbc_arg.entry_name = NULL;
2538 bbc_arg.path = "";
2539 bbc_arg.path_len = 0;
2540 bbc_arg.progress_cb = progress_cb;
2541 bbc_arg.progress_arg = progress_arg;
2543 return got_fileindex_for_each_entry_safe(fileindex,
2544 bump_base_commit_id, &bbc_arg);
2547 static const struct got_error *
2548 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2550 const struct got_error *err = NULL;
2551 char *new_fileindex_path = NULL;
2552 FILE *new_index = NULL;
2553 struct timespec timeout;
2555 err = got_opentemp_named(&new_fileindex_path, &new_index,
2556 fileindex_path, "");
2557 if (err)
2558 goto done;
2560 err = got_fileindex_write(fileindex, new_index);
2561 if (err)
2562 goto done;
2564 if (rename(new_fileindex_path, fileindex_path) != 0) {
2565 err = got_error_from_errno3("rename", new_fileindex_path,
2566 fileindex_path);
2567 unlink(new_fileindex_path);
2571 * Sleep for a short amount of time to ensure that files modified after
2572 * this program exits have a different time stamp from the one which
2573 * was recorded in the file index.
2575 timeout.tv_sec = 0;
2576 timeout.tv_nsec = 1;
2577 nanosleep(&timeout, NULL);
2578 done:
2579 if (new_index)
2580 fclose(new_index);
2581 free(new_fileindex_path);
2582 return err;
2585 static const struct got_error *
2586 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2587 struct got_object_id **tree_id, const char *wt_relpath,
2588 struct got_commit_object *base_commit, struct got_worktree *worktree,
2589 struct got_repository *repo)
2591 const struct got_error *err = NULL;
2592 struct got_object_id *id = NULL;
2593 char *in_repo_path = NULL;
2594 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2596 *entry_type = GOT_OBJ_TYPE_ANY;
2597 *tree_relpath = NULL;
2598 *tree_id = NULL;
2600 if (wt_relpath[0] == '\0') {
2601 /* Check out all files within the work tree. */
2602 *entry_type = GOT_OBJ_TYPE_TREE;
2603 *tree_relpath = strdup("");
2604 if (*tree_relpath == NULL) {
2605 err = got_error_from_errno("strdup");
2606 goto done;
2608 err = got_object_id_by_path(tree_id, repo, base_commit,
2609 worktree->path_prefix);
2610 if (err)
2611 goto done;
2612 return NULL;
2615 /* Check out a subset of files in the work tree. */
2617 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2618 is_root_wt ? "" : "/", wt_relpath) == -1) {
2619 err = got_error_from_errno("asprintf");
2620 goto done;
2623 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2624 if (err)
2625 goto done;
2627 free(in_repo_path);
2628 in_repo_path = NULL;
2630 err = got_object_get_type(entry_type, repo, id);
2631 if (err)
2632 goto done;
2634 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2635 /* Check out a single file. */
2636 if (strchr(wt_relpath, '/') == NULL) {
2637 /* Check out a single file in work tree's root dir. */
2638 in_repo_path = strdup(worktree->path_prefix);
2639 if (in_repo_path == NULL) {
2640 err = got_error_from_errno("strdup");
2641 goto done;
2643 *tree_relpath = strdup("");
2644 if (*tree_relpath == NULL) {
2645 err = got_error_from_errno("strdup");
2646 goto done;
2648 } else {
2649 /* Check out a single file in a subdirectory. */
2650 err = got_path_dirname(tree_relpath, wt_relpath);
2651 if (err)
2652 return err;
2653 if (asprintf(&in_repo_path, "%s%s%s",
2654 worktree->path_prefix, is_root_wt ? "" : "/",
2655 *tree_relpath) == -1) {
2656 err = got_error_from_errno("asprintf");
2657 goto done;
2660 err = got_object_id_by_path(tree_id, repo,
2661 base_commit, in_repo_path);
2662 } else {
2663 /* Check out all files within a subdirectory. */
2664 *tree_id = got_object_id_dup(id);
2665 if (*tree_id == NULL) {
2666 err = got_error_from_errno("got_object_id_dup");
2667 goto done;
2669 *tree_relpath = strdup(wt_relpath);
2670 if (*tree_relpath == NULL) {
2671 err = got_error_from_errno("strdup");
2672 goto done;
2675 done:
2676 free(id);
2677 free(in_repo_path);
2678 if (err) {
2679 *entry_type = GOT_OBJ_TYPE_ANY;
2680 free(*tree_relpath);
2681 *tree_relpath = NULL;
2682 free(*tree_id);
2683 *tree_id = NULL;
2685 return err;
2688 static const struct got_error *
2689 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2690 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2691 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2692 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2694 const struct got_error *err = NULL;
2695 struct got_commit_object *commit = NULL;
2696 struct got_tree_object *tree = NULL;
2697 struct got_fileindex_diff_tree_cb diff_cb;
2698 struct diff_cb_arg arg;
2700 err = ref_base_commit(worktree, repo);
2701 if (err) {
2702 if (!(err->code == GOT_ERR_ERRNO &&
2703 (errno == EACCES || errno == EROFS)))
2704 goto done;
2705 err = (*progress_cb)(progress_arg,
2706 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2707 if (err)
2708 return err;
2711 err = got_object_open_as_commit(&commit, repo,
2712 worktree->base_commit_id);
2713 if (err)
2714 goto done;
2716 err = got_object_open_as_tree(&tree, repo, tree_id);
2717 if (err)
2718 goto done;
2720 if (entry_name &&
2721 got_object_tree_find_entry(tree, entry_name) == NULL) {
2722 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2723 goto done;
2726 diff_cb.diff_old_new = diff_old_new;
2727 diff_cb.diff_old = diff_old;
2728 diff_cb.diff_new = diff_new;
2729 arg.fileindex = fileindex;
2730 arg.worktree = worktree;
2731 arg.repo = repo;
2732 arg.progress_cb = progress_cb;
2733 arg.progress_arg = progress_arg;
2734 arg.cancel_cb = cancel_cb;
2735 arg.cancel_arg = cancel_arg;
2736 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2737 entry_name, repo, &diff_cb, &arg);
2738 done:
2739 if (tree)
2740 got_object_tree_close(tree);
2741 if (commit)
2742 got_object_commit_close(commit);
2743 return err;
2746 const struct got_error *
2747 got_worktree_checkout_files(struct got_worktree *worktree,
2748 struct got_pathlist_head *paths, struct got_repository *repo,
2749 got_worktree_checkout_cb progress_cb, void *progress_arg,
2750 got_cancel_cb cancel_cb, void *cancel_arg)
2752 const struct got_error *err = NULL, *sync_err, *unlockerr;
2753 struct got_commit_object *commit = NULL;
2754 struct got_tree_object *tree = NULL;
2755 struct got_fileindex *fileindex = NULL;
2756 char *fileindex_path = NULL;
2757 struct got_pathlist_entry *pe;
2758 struct tree_path_data {
2759 STAILQ_ENTRY(tree_path_data) entry;
2760 struct got_object_id *tree_id;
2761 int entry_type;
2762 char *relpath;
2763 char *entry_name;
2764 } *tpd = NULL;
2765 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2767 STAILQ_INIT(&tree_paths);
2769 err = lock_worktree(worktree, LOCK_EX);
2770 if (err)
2771 return err;
2773 err = got_object_open_as_commit(&commit, repo,
2774 worktree->base_commit_id);
2775 if (err)
2776 goto done;
2778 /* Map all specified paths to in-repository trees. */
2779 TAILQ_FOREACH(pe, paths, entry) {
2780 tpd = malloc(sizeof(*tpd));
2781 if (tpd == NULL) {
2782 err = got_error_from_errno("malloc");
2783 goto done;
2786 err = find_tree_entry_for_checkout(&tpd->entry_type,
2787 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2788 worktree, repo);
2789 if (err) {
2790 free(tpd);
2791 goto done;
2794 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2795 err = got_path_basename(&tpd->entry_name, pe->path);
2796 if (err) {
2797 free(tpd->relpath);
2798 free(tpd->tree_id);
2799 free(tpd);
2800 goto done;
2802 } else
2803 tpd->entry_name = NULL;
2805 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2809 * Read the file index.
2810 * Checking out files is supposed to be an idempotent operation.
2811 * If the on-disk file index is incomplete we will try to complete it.
2813 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2814 if (err)
2815 goto done;
2817 tpd = STAILQ_FIRST(&tree_paths);
2818 TAILQ_FOREACH(pe, paths, entry) {
2819 struct bump_base_commit_id_arg bbc_arg;
2821 err = checkout_files(worktree, fileindex, tpd->relpath,
2822 tpd->tree_id, tpd->entry_name, repo,
2823 progress_cb, progress_arg, cancel_cb, cancel_arg);
2824 if (err)
2825 break;
2827 bbc_arg.base_commit_id = worktree->base_commit_id;
2828 bbc_arg.entry_name = tpd->entry_name;
2829 bbc_arg.path = pe->path;
2830 bbc_arg.path_len = pe->path_len;
2831 bbc_arg.progress_cb = progress_cb;
2832 bbc_arg.progress_arg = progress_arg;
2833 err = got_fileindex_for_each_entry_safe(fileindex,
2834 bump_base_commit_id, &bbc_arg);
2835 if (err)
2836 break;
2838 tpd = STAILQ_NEXT(tpd, entry);
2840 sync_err = sync_fileindex(fileindex, fileindex_path);
2841 if (sync_err && err == NULL)
2842 err = sync_err;
2843 done:
2844 free(fileindex_path);
2845 if (tree)
2846 got_object_tree_close(tree);
2847 if (commit)
2848 got_object_commit_close(commit);
2849 if (fileindex)
2850 got_fileindex_free(fileindex);
2851 while (!STAILQ_EMPTY(&tree_paths)) {
2852 tpd = STAILQ_FIRST(&tree_paths);
2853 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2854 free(tpd->relpath);
2855 free(tpd->tree_id);
2856 free(tpd);
2858 unlockerr = lock_worktree(worktree, LOCK_SH);
2859 if (unlockerr && err == NULL)
2860 err = unlockerr;
2861 return err;
2864 struct merge_file_cb_arg {
2865 struct got_worktree *worktree;
2866 struct got_fileindex *fileindex;
2867 got_worktree_checkout_cb progress_cb;
2868 void *progress_arg;
2869 got_cancel_cb cancel_cb;
2870 void *cancel_arg;
2871 const char *label_orig;
2872 struct got_object_id *commit_id2;
2873 int allow_bad_symlinks;
2876 static const struct got_error *
2877 merge_file_cb(void *arg, struct got_blob_object *blob1,
2878 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2879 struct got_object_id *id1, struct got_object_id *id2,
2880 const char *path1, const char *path2,
2881 mode_t mode1, mode_t mode2, struct got_repository *repo)
2883 static const struct got_error *err = NULL;
2884 struct merge_file_cb_arg *a = arg;
2885 struct got_fileindex_entry *ie;
2886 char *ondisk_path = NULL;
2887 struct stat sb;
2888 unsigned char status;
2889 int local_changes_subsumed;
2890 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2891 char *id_str = NULL, *label_deriv2 = NULL;
2893 if (blob1 && blob2) {
2894 ie = got_fileindex_entry_get(a->fileindex, path2,
2895 strlen(path2));
2896 if (ie == NULL)
2897 return (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_MISSING, path2);
2900 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2901 path2) == -1)
2902 return got_error_from_errno("asprintf");
2904 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2905 repo);
2906 if (err)
2907 goto done;
2909 if (status == GOT_STATUS_DELETE) {
2910 err = (*a->progress_cb)(a->progress_arg,
2911 GOT_STATUS_MERGE, path2);
2912 goto done;
2914 if (status != GOT_STATUS_NO_CHANGE &&
2915 status != GOT_STATUS_MODIFY &&
2916 status != GOT_STATUS_CONFLICT &&
2917 status != GOT_STATUS_ADD) {
2918 err = (*a->progress_cb)(a->progress_arg, status, path2);
2919 goto done;
2922 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2923 char *link_target2;
2924 err = got_object_blob_read_to_str(&link_target2, blob2);
2925 if (err)
2926 goto done;
2927 err = merge_symlink(a->worktree, blob1, ondisk_path,
2928 path2, a->label_orig, link_target2, a->commit_id2,
2929 repo, a->progress_cb, a->progress_arg);
2930 free(link_target2);
2931 } else {
2932 int fd;
2934 f_orig = got_opentemp();
2935 if (f_orig == NULL) {
2936 err = got_error_from_errno("got_opentemp");
2937 goto done;
2939 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2940 f_orig, blob1);
2941 if (err)
2942 goto done;
2944 f_deriv2 = got_opentemp();
2945 if (f_deriv2 == NULL)
2946 goto done;
2947 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2948 f_deriv2, blob2);
2949 if (err)
2950 goto done;
2952 fd = open(ondisk_path,
2953 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2954 if (fd == -1) {
2955 err = got_error_from_errno2("open",
2956 ondisk_path);
2957 goto done;
2959 f_deriv = fdopen(fd, "r");
2960 if (f_deriv == NULL) {
2961 err = got_error_from_errno2("fdopen",
2962 ondisk_path);
2963 close(fd);
2964 goto done;
2966 err = got_object_id_str(&id_str, a->commit_id2);
2967 if (err)
2968 goto done;
2969 if (asprintf(&label_deriv2, "%s: commit %s",
2970 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2971 err = got_error_from_errno("asprintf");
2972 goto done;
2974 err = merge_file(&local_changes_subsumed, a->worktree,
2975 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2976 mode2, a->label_orig, NULL, label_deriv2,
2977 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2978 a->progress_cb, a->progress_arg);
2980 } else if (blob1) {
2981 ie = got_fileindex_entry_get(a->fileindex, path1,
2982 strlen(path1));
2983 if (ie == NULL)
2984 return (*a->progress_cb)(a->progress_arg,
2985 GOT_STATUS_MISSING, path1);
2987 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2988 path1) == -1)
2989 return got_error_from_errno("asprintf");
2991 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2992 repo);
2993 if (err)
2994 goto done;
2996 switch (status) {
2997 case GOT_STATUS_NO_CHANGE:
2998 err = (*a->progress_cb)(a->progress_arg,
2999 GOT_STATUS_DELETE, path1);
3000 if (err)
3001 goto done;
3002 err = remove_ondisk_file(a->worktree->root_path, path1);
3003 if (err)
3004 goto done;
3005 if (ie)
3006 got_fileindex_entry_mark_deleted_from_disk(ie);
3007 break;
3008 case GOT_STATUS_DELETE:
3009 case GOT_STATUS_MISSING:
3010 err = (*a->progress_cb)(a->progress_arg,
3011 GOT_STATUS_DELETE, path1);
3012 if (err)
3013 goto done;
3014 if (ie)
3015 got_fileindex_entry_mark_deleted_from_disk(ie);
3016 break;
3017 case GOT_STATUS_ADD: {
3018 struct got_object_id *id;
3019 FILE *blob1_f;
3020 off_t blob1_size;
3022 * Delete the added file only if its content already
3023 * exists in the repository.
3025 err = got_object_blob_file_create(&id, &blob1_f,
3026 &blob1_size, path1);
3027 if (err)
3028 goto done;
3029 if (got_object_id_cmp(id, id1) == 0) {
3030 err = (*a->progress_cb)(a->progress_arg,
3031 GOT_STATUS_DELETE, path1);
3032 if (err)
3033 goto done;
3034 err = remove_ondisk_file(a->worktree->root_path,
3035 path1);
3036 if (err)
3037 goto done;
3038 if (ie)
3039 got_fileindex_entry_remove(a->fileindex,
3040 ie);
3041 } else {
3042 err = (*a->progress_cb)(a->progress_arg,
3043 GOT_STATUS_CANNOT_DELETE, path1);
3045 if (fclose(blob1_f) == EOF && err == NULL)
3046 err = got_error_from_errno("fclose");
3047 free(id);
3048 if (err)
3049 goto done;
3050 break;
3052 case GOT_STATUS_MODIFY:
3053 case GOT_STATUS_CONFLICT:
3054 err = (*a->progress_cb)(a->progress_arg,
3055 GOT_STATUS_CANNOT_DELETE, path1);
3056 if (err)
3057 goto done;
3058 break;
3059 case GOT_STATUS_OBSTRUCTED:
3060 err = (*a->progress_cb)(a->progress_arg, status, path1);
3061 if (err)
3062 goto done;
3063 break;
3064 default:
3065 break;
3067 } else if (blob2) {
3068 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3069 path2) == -1)
3070 return got_error_from_errno("asprintf");
3071 ie = got_fileindex_entry_get(a->fileindex, path2,
3072 strlen(path2));
3073 if (ie) {
3074 err = get_file_status(&status, &sb, ie, ondisk_path,
3075 -1, NULL, repo);
3076 if (err)
3077 goto done;
3078 if (status != GOT_STATUS_NO_CHANGE &&
3079 status != GOT_STATUS_MODIFY &&
3080 status != GOT_STATUS_CONFLICT &&
3081 status != GOT_STATUS_ADD) {
3082 err = (*a->progress_cb)(a->progress_arg,
3083 status, path2);
3084 goto done;
3086 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3087 char *link_target2;
3088 err = got_object_blob_read_to_str(&link_target2,
3089 blob2);
3090 if (err)
3091 goto done;
3092 err = merge_symlink(a->worktree, NULL,
3093 ondisk_path, path2, a->label_orig,
3094 link_target2, a->commit_id2, repo,
3095 a->progress_cb, a->progress_arg);
3096 free(link_target2);
3097 } else if (S_ISREG(sb.st_mode)) {
3098 err = merge_blob(&local_changes_subsumed,
3099 a->worktree, NULL, ondisk_path, path2,
3100 sb.st_mode, a->label_orig, blob2,
3101 a->commit_id2, repo, a->progress_cb,
3102 a->progress_arg);
3103 } else {
3104 err = got_error_path(ondisk_path,
3105 GOT_ERR_FILE_OBSTRUCTED);
3107 if (err)
3108 goto done;
3109 if (status == GOT_STATUS_DELETE) {
3110 err = got_fileindex_entry_update(ie,
3111 a->worktree->root_fd, path2, blob2->id.sha1,
3112 a->worktree->base_commit_id->sha1, 0);
3113 if (err)
3114 goto done;
3116 } else {
3117 int is_bad_symlink = 0;
3118 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3119 if (S_ISLNK(mode2)) {
3120 err = install_symlink(&is_bad_symlink,
3121 a->worktree, ondisk_path, path2, blob2, 0,
3122 0, 1, a->allow_bad_symlinks, repo,
3123 a->progress_cb, a->progress_arg);
3124 } else {
3125 err = install_blob(a->worktree, ondisk_path, path2,
3126 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3127 a->progress_cb, a->progress_arg);
3129 if (err)
3130 goto done;
3131 err = got_fileindex_entry_alloc(&ie, path2);
3132 if (err)
3133 goto done;
3134 err = got_fileindex_entry_update(ie,
3135 a->worktree->root_fd, path2, NULL, NULL, 1);
3136 if (err) {
3137 got_fileindex_entry_free(ie);
3138 goto done;
3140 err = got_fileindex_entry_add(a->fileindex, ie);
3141 if (err) {
3142 got_fileindex_entry_free(ie);
3143 goto done;
3145 if (is_bad_symlink) {
3146 got_fileindex_entry_filetype_set(ie,
3147 GOT_FILEIDX_MODE_BAD_SYMLINK);
3151 done:
3152 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3153 err = got_error_from_errno("fclose");
3154 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3155 err = got_error_from_errno("fclose");
3156 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3157 err = got_error_from_errno("fclose");
3158 free(id_str);
3159 free(label_deriv2);
3160 free(ondisk_path);
3161 return err;
3164 static const struct got_error *
3165 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3167 struct got_worktree *worktree = arg;
3169 /* Reject merges into a work tree with mixed base commits. */
3170 if (got_fileindex_entry_has_commit(ie) &&
3171 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3172 SHA1_DIGEST_LENGTH) != 0)
3173 return got_error(GOT_ERR_MIXED_COMMITS);
3175 return NULL;
3178 struct check_merge_conflicts_arg {
3179 struct got_worktree *worktree;
3180 struct got_fileindex *fileindex;
3181 struct got_repository *repo;
3184 static const struct got_error *
3185 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3186 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3187 struct got_object_id *id1, struct got_object_id *id2,
3188 const char *path1, const char *path2,
3189 mode_t mode1, mode_t mode2, struct got_repository *repo)
3191 const struct got_error *err = NULL;
3192 struct check_merge_conflicts_arg *a = arg;
3193 unsigned char status;
3194 struct stat sb;
3195 struct got_fileindex_entry *ie;
3196 const char *path = path2 ? path2 : path1;
3197 struct got_object_id *id = id2 ? id2 : id1;
3198 char *ondisk_path;
3200 if (id == NULL)
3201 return NULL;
3203 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3204 if (ie == NULL)
3205 return NULL;
3207 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3208 == -1)
3209 return got_error_from_errno("asprintf");
3211 /* Reject merges into a work tree with conflicted files. */
3212 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3213 free(ondisk_path);
3214 if (err)
3215 return err;
3216 if (status == GOT_STATUS_CONFLICT)
3217 return got_error(GOT_ERR_CONFLICTS);
3219 return NULL;
3222 static const struct got_error *
3223 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3224 const char *fileindex_path, struct got_object_id *commit_id1,
3225 struct got_object_id *commit_id2, struct got_repository *repo,
3226 got_worktree_checkout_cb progress_cb, void *progress_arg,
3227 got_cancel_cb cancel_cb, void *cancel_arg)
3229 const struct got_error *err = NULL, *sync_err;
3230 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3231 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3232 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3233 struct check_merge_conflicts_arg cmc_arg;
3234 struct merge_file_cb_arg arg;
3235 char *label_orig = NULL;
3236 FILE *f1 = NULL, *f2 = NULL;
3237 int fd1 = -1, fd2 = -1;
3239 if (commit_id1) {
3240 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3241 if (err)
3242 goto done;
3243 err = got_object_id_by_path(&tree_id1, repo, commit1,
3244 worktree->path_prefix);
3245 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3246 goto done;
3248 if (tree_id1) {
3249 char *id_str;
3251 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3252 if (err)
3253 goto done;
3255 err = got_object_id_str(&id_str, commit_id1);
3256 if (err)
3257 goto done;
3259 if (asprintf(&label_orig, "%s: commit %s",
3260 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3261 err = got_error_from_errno("asprintf");
3262 free(id_str);
3263 goto done;
3265 free(id_str);
3267 f1 = got_opentemp();
3268 if (f1 == NULL) {
3269 err = got_error_from_errno("got_opentemp");
3270 goto done;
3274 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3275 if (err)
3276 goto done;
3278 err = got_object_id_by_path(&tree_id2, repo, commit2,
3279 worktree->path_prefix);
3280 if (err)
3281 goto done;
3283 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3284 if (err)
3285 goto done;
3287 f2 = got_opentemp();
3288 if (f2 == NULL) {
3289 err = got_error_from_errno("got_opentemp");
3290 goto done;
3293 fd1 = got_opentempfd();
3294 if (fd1 == -1) {
3295 err = got_error_from_errno("got_opentempfd");
3296 goto done;
3299 fd2 = got_opentempfd();
3300 if (fd2 == -1) {
3301 err = got_error_from_errno("got_opentempfd");
3302 goto done;
3305 cmc_arg.worktree = worktree;
3306 cmc_arg.fileindex = fileindex;
3307 cmc_arg.repo = repo;
3308 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3309 check_merge_conflicts, &cmc_arg, 0);
3310 if (err)
3311 goto done;
3313 arg.worktree = worktree;
3314 arg.fileindex = fileindex;
3315 arg.progress_cb = progress_cb;
3316 arg.progress_arg = progress_arg;
3317 arg.cancel_cb = cancel_cb;
3318 arg.cancel_arg = cancel_arg;
3319 arg.label_orig = label_orig;
3320 arg.commit_id2 = commit_id2;
3321 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3322 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3323 merge_file_cb, &arg, 1);
3324 sync_err = sync_fileindex(fileindex, fileindex_path);
3325 if (sync_err && err == NULL)
3326 err = sync_err;
3327 done:
3328 if (commit1)
3329 got_object_commit_close(commit1);
3330 if (commit2)
3331 got_object_commit_close(commit2);
3332 if (tree1)
3333 got_object_tree_close(tree1);
3334 if (tree2)
3335 got_object_tree_close(tree2);
3336 if (f1 && fclose(f1) == EOF && err == NULL)
3337 err = got_error_from_errno("fclose");
3338 if (f2 && fclose(f2) == EOF && err == NULL)
3339 err = got_error_from_errno("fclose");
3340 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3341 err = got_error_from_errno("close");
3342 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3343 err = got_error_from_errno("close");
3344 free(label_orig);
3345 return err;
3348 const struct got_error *
3349 got_worktree_merge_files(struct got_worktree *worktree,
3350 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3351 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3352 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3354 const struct got_error *err, *unlockerr;
3355 char *fileindex_path = NULL;
3356 struct got_fileindex *fileindex = NULL;
3358 err = lock_worktree(worktree, LOCK_EX);
3359 if (err)
3360 return err;
3362 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3363 if (err)
3364 goto done;
3366 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3367 worktree);
3368 if (err)
3369 goto done;
3371 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3372 commit_id2, repo, progress_cb, progress_arg,
3373 cancel_cb, cancel_arg);
3374 done:
3375 if (fileindex)
3376 got_fileindex_free(fileindex);
3377 free(fileindex_path);
3378 unlockerr = lock_worktree(worktree, LOCK_SH);
3379 if (unlockerr && err == NULL)
3380 err = unlockerr;
3381 return err;
3384 struct diff_dir_cb_arg {
3385 struct got_fileindex *fileindex;
3386 struct got_worktree *worktree;
3387 const char *status_path;
3388 size_t status_path_len;
3389 struct got_repository *repo;
3390 got_worktree_status_cb status_cb;
3391 void *status_arg;
3392 got_cancel_cb cancel_cb;
3393 void *cancel_arg;
3394 /* A pathlist containing per-directory pathlists of ignore patterns. */
3395 struct got_pathlist_head *ignores;
3396 int report_unchanged;
3397 int no_ignores;
3400 static const struct got_error *
3401 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3402 int dirfd, const char *de_name,
3403 got_worktree_status_cb status_cb, void *status_arg,
3404 struct got_repository *repo, int report_unchanged)
3406 const struct got_error *err = NULL;
3407 unsigned char status = GOT_STATUS_NO_CHANGE;
3408 unsigned char staged_status;
3409 struct stat sb;
3410 struct got_object_id blob_id, commit_id, staged_blob_id;
3411 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3412 struct got_object_id *staged_blob_idp = NULL;
3414 staged_status = get_staged_status(ie);
3415 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3416 if (err)
3417 return err;
3419 if (status == GOT_STATUS_NO_CHANGE &&
3420 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3421 return NULL;
3423 if (got_fileindex_entry_has_blob(ie))
3424 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3425 if (got_fileindex_entry_has_commit(ie))
3426 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3427 if (staged_status == GOT_STATUS_ADD ||
3428 staged_status == GOT_STATUS_MODIFY) {
3429 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3430 &staged_blob_id, ie);
3433 return (*status_cb)(status_arg, status, staged_status,
3434 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3437 static const struct got_error *
3438 status_old_new(void *arg, struct got_fileindex_entry *ie,
3439 struct dirent *de, const char *parent_path, int dirfd)
3441 const struct got_error *err = NULL;
3442 struct diff_dir_cb_arg *a = arg;
3443 char *abspath;
3445 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3446 return got_error(GOT_ERR_CANCELLED);
3448 if (got_path_cmp(parent_path, a->status_path,
3449 strlen(parent_path), a->status_path_len) != 0 &&
3450 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3451 return NULL;
3453 if (parent_path[0]) {
3454 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3455 parent_path, de->d_name) == -1)
3456 return got_error_from_errno("asprintf");
3457 } else {
3458 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3459 de->d_name) == -1)
3460 return got_error_from_errno("asprintf");
3463 err = report_file_status(ie, abspath, dirfd, de->d_name,
3464 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3465 free(abspath);
3466 return err;
3469 static const struct got_error *
3470 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3472 struct diff_dir_cb_arg *a = arg;
3473 struct got_object_id blob_id, commit_id;
3474 unsigned char status;
3476 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3477 return got_error(GOT_ERR_CANCELLED);
3479 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3480 return NULL;
3482 got_fileindex_entry_get_blob_id(&blob_id, ie);
3483 got_fileindex_entry_get_commit_id(&commit_id, ie);
3484 if (got_fileindex_entry_has_file_on_disk(ie))
3485 status = GOT_STATUS_MISSING;
3486 else
3487 status = GOT_STATUS_DELETE;
3488 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3489 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3492 static void
3493 free_ignores(struct got_pathlist_head *ignores)
3495 struct got_pathlist_entry *pe;
3497 TAILQ_FOREACH(pe, ignores, entry) {
3498 struct got_pathlist_head *ignorelist = pe->data;
3500 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3502 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3505 static const struct got_error *
3506 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3508 const struct got_error *err = NULL;
3509 struct got_pathlist_entry *pe = NULL;
3510 struct got_pathlist_head *ignorelist;
3511 char *line = NULL, *pattern, *dirpath = NULL;
3512 size_t linesize = 0;
3513 ssize_t linelen;
3515 ignorelist = calloc(1, sizeof(*ignorelist));
3516 if (ignorelist == NULL)
3517 return got_error_from_errno("calloc");
3518 TAILQ_INIT(ignorelist);
3520 while ((linelen = getline(&line, &linesize, f)) != -1) {
3521 if (linelen > 0 && line[linelen - 1] == '\n')
3522 line[linelen - 1] = '\0';
3524 /* Git's ignores may contain comments. */
3525 if (line[0] == '#')
3526 continue;
3528 /* Git's negated patterns are not (yet?) supported. */
3529 if (line[0] == '!')
3530 continue;
3532 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3533 line) == -1) {
3534 err = got_error_from_errno("asprintf");
3535 goto done;
3537 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3538 if (err)
3539 goto done;
3541 if (ferror(f)) {
3542 err = got_error_from_errno("getline");
3543 goto done;
3546 dirpath = strdup(path);
3547 if (dirpath == NULL) {
3548 err = got_error_from_errno("strdup");
3549 goto done;
3551 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3552 done:
3553 free(line);
3554 if (err || pe == NULL) {
3555 free(dirpath);
3556 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3558 return err;
3561 static int
3562 match_ignores(struct got_pathlist_head *ignores, const char *path)
3564 struct got_pathlist_entry *pe;
3566 /* Handle patterns which match in all directories. */
3567 TAILQ_FOREACH(pe, ignores, entry) {
3568 struct got_pathlist_head *ignorelist = pe->data;
3569 struct got_pathlist_entry *pi;
3571 TAILQ_FOREACH(pi, ignorelist, entry) {
3572 const char *p, *pattern = pi->path;
3574 if (strncmp(pattern, "**/", 3) != 0)
3575 continue;
3576 pattern += 3;
3577 p = path;
3578 while (*p) {
3579 if (fnmatch(pattern, p,
3580 FNM_PATHNAME | FNM_LEADING_DIR)) {
3581 /* Retry in next directory. */
3582 while (*p && *p != '/')
3583 p++;
3584 while (*p == '/')
3585 p++;
3586 continue;
3588 return 1;
3594 * The ignores pathlist contains ignore lists from children before
3595 * parents, so we can find the most specific ignorelist by walking
3596 * ignores backwards.
3598 pe = TAILQ_LAST(ignores, got_pathlist_head);
3599 while (pe) {
3600 if (got_path_is_child(path, pe->path, pe->path_len)) {
3601 struct got_pathlist_head *ignorelist = pe->data;
3602 struct got_pathlist_entry *pi;
3603 TAILQ_FOREACH(pi, ignorelist, entry) {
3604 const char *pattern = pi->path;
3605 int flags = FNM_LEADING_DIR;
3606 if (strstr(pattern, "/**/") == NULL)
3607 flags |= FNM_PATHNAME;
3608 if (fnmatch(pattern, path, flags))
3609 continue;
3610 return 1;
3613 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3616 return 0;
3619 static const struct got_error *
3620 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3621 const char *path, int dirfd, const char *ignores_filename)
3623 const struct got_error *err = NULL;
3624 char *ignorespath;
3625 int fd = -1;
3626 FILE *ignoresfile = NULL;
3628 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3629 path[0] ? "/" : "", ignores_filename) == -1)
3630 return got_error_from_errno("asprintf");
3632 if (dirfd != -1) {
3633 fd = openat(dirfd, ignores_filename,
3634 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3635 if (fd == -1) {
3636 if (errno != ENOENT && errno != EACCES)
3637 err = got_error_from_errno2("openat",
3638 ignorespath);
3639 } else {
3640 ignoresfile = fdopen(fd, "r");
3641 if (ignoresfile == NULL)
3642 err = got_error_from_errno2("fdopen",
3643 ignorespath);
3644 else {
3645 fd = -1;
3646 err = read_ignores(ignores, path, ignoresfile);
3649 } else {
3650 ignoresfile = fopen(ignorespath, "re");
3651 if (ignoresfile == NULL) {
3652 if (errno != ENOENT && errno != EACCES)
3653 err = got_error_from_errno2("fopen",
3654 ignorespath);
3655 } else
3656 err = read_ignores(ignores, path, ignoresfile);
3659 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3660 err = got_error_from_errno2("fclose", path);
3661 if (fd != -1 && close(fd) == -1 && err == NULL)
3662 err = got_error_from_errno2("close", path);
3663 free(ignorespath);
3664 return err;
3667 static const struct got_error *
3668 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3669 int dirfd)
3671 const struct got_error *err = NULL;
3672 struct diff_dir_cb_arg *a = arg;
3673 char *path = NULL;
3675 if (ignore != NULL)
3676 *ignore = 0;
3678 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3679 return got_error(GOT_ERR_CANCELLED);
3681 if (parent_path[0]) {
3682 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3683 return got_error_from_errno("asprintf");
3684 } else {
3685 path = de->d_name;
3688 if (de->d_type == DT_DIR) {
3689 if (!a->no_ignores && ignore != NULL &&
3690 match_ignores(a->ignores, path))
3691 *ignore = 1;
3692 } else if (!match_ignores(a->ignores, path) &&
3693 got_path_is_child(path, a->status_path, a->status_path_len))
3694 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3695 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3696 if (parent_path[0])
3697 free(path);
3698 return err;
3701 static const struct got_error *
3702 status_traverse(void *arg, const char *path, int dirfd)
3704 const struct got_error *err = NULL;
3705 struct diff_dir_cb_arg *a = arg;
3707 if (a->no_ignores)
3708 return NULL;
3710 err = add_ignores(a->ignores, a->worktree->root_path,
3711 path, dirfd, ".cvsignore");
3712 if (err)
3713 return err;
3715 err = add_ignores(a->ignores, a->worktree->root_path, path,
3716 dirfd, ".gitignore");
3718 return err;
3721 static const struct got_error *
3722 report_single_file_status(const char *path, const char *ondisk_path,
3723 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3724 void *status_arg, struct got_repository *repo, int report_unchanged,
3725 struct got_pathlist_head *ignores, int no_ignores)
3727 struct got_fileindex_entry *ie;
3728 struct stat sb;
3730 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3731 if (ie)
3732 return report_file_status(ie, ondisk_path, -1, NULL,
3733 status_cb, status_arg, repo, report_unchanged);
3735 if (lstat(ondisk_path, &sb) == -1) {
3736 if (errno != ENOENT)
3737 return got_error_from_errno2("lstat", ondisk_path);
3738 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3739 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3742 if (!no_ignores && match_ignores(ignores, path))
3743 return NULL;
3745 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3746 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3747 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3749 return NULL;
3752 static const struct got_error *
3753 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3754 const char *root_path, const char *path)
3756 const struct got_error *err;
3757 char *parent_path, *next_parent_path = NULL;
3759 err = add_ignores(ignores, root_path, "", -1,
3760 ".cvsignore");
3761 if (err)
3762 return err;
3764 err = add_ignores(ignores, root_path, "", -1,
3765 ".gitignore");
3766 if (err)
3767 return err;
3769 err = got_path_dirname(&parent_path, path);
3770 if (err) {
3771 if (err->code == GOT_ERR_BAD_PATH)
3772 return NULL; /* cannot traverse parent */
3773 return err;
3775 for (;;) {
3776 err = add_ignores(ignores, root_path, parent_path, -1,
3777 ".cvsignore");
3778 if (err)
3779 break;
3780 err = add_ignores(ignores, root_path, parent_path, -1,
3781 ".gitignore");
3782 if (err)
3783 break;
3784 err = got_path_dirname(&next_parent_path, parent_path);
3785 if (err) {
3786 if (err->code == GOT_ERR_BAD_PATH)
3787 err = NULL; /* traversed everything */
3788 break;
3790 if (got_path_is_root_dir(parent_path))
3791 break;
3792 free(parent_path);
3793 parent_path = next_parent_path;
3794 next_parent_path = NULL;
3797 free(parent_path);
3798 free(next_parent_path);
3799 return err;
3802 static const struct got_error *
3803 worktree_status(struct got_worktree *worktree, const char *path,
3804 struct got_fileindex *fileindex, struct got_repository *repo,
3805 got_worktree_status_cb status_cb, void *status_arg,
3806 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3807 int report_unchanged)
3809 const struct got_error *err = NULL;
3810 int fd = -1;
3811 struct got_fileindex_diff_dir_cb fdiff_cb;
3812 struct diff_dir_cb_arg arg;
3813 char *ondisk_path = NULL;
3814 struct got_pathlist_head ignores;
3815 struct got_fileindex_entry *ie;
3817 TAILQ_INIT(&ignores);
3819 if (asprintf(&ondisk_path, "%s%s%s",
3820 worktree->root_path, path[0] ? "/" : "", path) == -1)
3821 return got_error_from_errno("asprintf");
3823 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3824 if (ie) {
3825 err = report_single_file_status(path, ondisk_path,
3826 fileindex, status_cb, status_arg, repo,
3827 report_unchanged, &ignores, no_ignores);
3828 goto done;
3831 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3832 if (fd == -1) {
3833 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3834 !got_err_open_nofollow_on_symlink())
3835 err = got_error_from_errno2("open", ondisk_path);
3836 else {
3837 if (!no_ignores) {
3838 err = add_ignores_from_parent_paths(&ignores,
3839 worktree->root_path, ondisk_path);
3840 if (err)
3841 goto done;
3843 err = report_single_file_status(path, ondisk_path,
3844 fileindex, status_cb, status_arg, repo,
3845 report_unchanged, &ignores, no_ignores);
3847 } else {
3848 fdiff_cb.diff_old_new = status_old_new;
3849 fdiff_cb.diff_old = status_old;
3850 fdiff_cb.diff_new = status_new;
3851 fdiff_cb.diff_traverse = status_traverse;
3852 arg.fileindex = fileindex;
3853 arg.worktree = worktree;
3854 arg.status_path = path;
3855 arg.status_path_len = strlen(path);
3856 arg.repo = repo;
3857 arg.status_cb = status_cb;
3858 arg.status_arg = status_arg;
3859 arg.cancel_cb = cancel_cb;
3860 arg.cancel_arg = cancel_arg;
3861 arg.report_unchanged = report_unchanged;
3862 arg.no_ignores = no_ignores;
3863 if (!no_ignores) {
3864 err = add_ignores_from_parent_paths(&ignores,
3865 worktree->root_path, path);
3866 if (err)
3867 goto done;
3869 arg.ignores = &ignores;
3870 err = got_fileindex_diff_dir(fileindex, fd,
3871 worktree->root_path, path, repo, &fdiff_cb, &arg);
3873 done:
3874 free_ignores(&ignores);
3875 if (fd != -1 && close(fd) == -1 && err == NULL)
3876 err = got_error_from_errno("close");
3877 free(ondisk_path);
3878 return err;
3881 const struct got_error *
3882 got_worktree_status(struct got_worktree *worktree,
3883 struct got_pathlist_head *paths, struct got_repository *repo,
3884 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3885 got_cancel_cb cancel_cb, void *cancel_arg)
3887 const struct got_error *err = NULL;
3888 char *fileindex_path = NULL;
3889 struct got_fileindex *fileindex = NULL;
3890 struct got_pathlist_entry *pe;
3892 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3893 if (err)
3894 return err;
3896 TAILQ_FOREACH(pe, paths, entry) {
3897 err = worktree_status(worktree, pe->path, fileindex, repo,
3898 status_cb, status_arg, cancel_cb, cancel_arg,
3899 no_ignores, 0);
3900 if (err)
3901 break;
3903 free(fileindex_path);
3904 got_fileindex_free(fileindex);
3905 return err;
3908 const struct got_error *
3909 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3910 const char *arg)
3912 const struct got_error *err = NULL;
3913 char *resolved = NULL, *cwd = NULL, *path = NULL;
3914 size_t len;
3915 struct stat sb;
3916 char *abspath = NULL;
3917 char canonpath[PATH_MAX];
3919 *wt_path = NULL;
3921 cwd = getcwd(NULL, 0);
3922 if (cwd == NULL)
3923 return got_error_from_errno("getcwd");
3925 if (lstat(arg, &sb) == -1) {
3926 if (errno != ENOENT) {
3927 err = got_error_from_errno2("lstat", arg);
3928 goto done;
3930 sb.st_mode = 0;
3932 if (S_ISLNK(sb.st_mode)) {
3934 * We cannot use realpath(3) with symlinks since we want to
3935 * operate on the symlink itself.
3936 * But we can make the path absolute, assuming it is relative
3937 * to the current working directory, and then canonicalize it.
3939 if (!got_path_is_absolute(arg)) {
3940 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3941 err = got_error_from_errno("asprintf");
3942 goto done;
3946 err = got_canonpath(abspath ? abspath : arg, canonpath,
3947 sizeof(canonpath));
3948 if (err)
3949 goto done;
3950 resolved = strdup(canonpath);
3951 if (resolved == NULL) {
3952 err = got_error_from_errno("strdup");
3953 goto done;
3955 } else {
3956 resolved = realpath(arg, NULL);
3957 if (resolved == NULL) {
3958 if (errno != ENOENT) {
3959 err = got_error_from_errno2("realpath", arg);
3960 goto done;
3962 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3963 err = got_error_from_errno("asprintf");
3964 goto done;
3966 err = got_canonpath(abspath, canonpath,
3967 sizeof(canonpath));
3968 if (err)
3969 goto done;
3970 resolved = strdup(canonpath);
3971 if (resolved == NULL) {
3972 err = got_error_from_errno("strdup");
3973 goto done;
3978 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3979 strlen(got_worktree_get_root_path(worktree)))) {
3980 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3981 goto done;
3984 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3985 err = got_path_skip_common_ancestor(&path,
3986 got_worktree_get_root_path(worktree), resolved);
3987 if (err)
3988 goto done;
3989 } else {
3990 path = strdup("");
3991 if (path == NULL) {
3992 err = got_error_from_errno("strdup");
3993 goto done;
3997 /* XXX status walk can't deal with trailing slash! */
3998 len = strlen(path);
3999 while (len > 0 && path[len - 1] == '/') {
4000 path[len - 1] = '\0';
4001 len--;
4003 done:
4004 free(abspath);
4005 free(resolved);
4006 free(cwd);
4007 if (err == NULL)
4008 *wt_path = path;
4009 else
4010 free(path);
4011 return err;
4014 struct schedule_addition_args {
4015 struct got_worktree *worktree;
4016 struct got_fileindex *fileindex;
4017 got_worktree_checkout_cb progress_cb;
4018 void *progress_arg;
4019 struct got_repository *repo;
4022 static const struct got_error *
4023 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4024 const char *relpath, struct got_object_id *blob_id,
4025 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4026 int dirfd, const char *de_name)
4028 struct schedule_addition_args *a = arg;
4029 const struct got_error *err = NULL;
4030 struct got_fileindex_entry *ie;
4031 struct stat sb;
4032 char *ondisk_path;
4034 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4035 relpath) == -1)
4036 return got_error_from_errno("asprintf");
4038 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4039 if (ie) {
4040 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4041 de_name, a->repo);
4042 if (err)
4043 goto done;
4044 /* Re-adding an existing entry is a no-op. */
4045 if (status == GOT_STATUS_ADD)
4046 goto done;
4047 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4048 if (err)
4049 goto done;
4052 if (status != GOT_STATUS_UNVERSIONED) {
4053 if (status == GOT_STATUS_NONEXISTENT)
4054 err = got_error_set_errno(ENOENT, ondisk_path);
4055 else
4056 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4057 goto done;
4060 err = got_fileindex_entry_alloc(&ie, relpath);
4061 if (err)
4062 goto done;
4063 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4064 relpath, NULL, NULL, 1);
4065 if (err) {
4066 got_fileindex_entry_free(ie);
4067 goto done;
4069 err = got_fileindex_entry_add(a->fileindex, ie);
4070 if (err) {
4071 got_fileindex_entry_free(ie);
4072 goto done;
4074 done:
4075 free(ondisk_path);
4076 if (err)
4077 return err;
4078 if (status == GOT_STATUS_ADD)
4079 return NULL;
4080 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4083 const struct got_error *
4084 got_worktree_schedule_add(struct got_worktree *worktree,
4085 struct got_pathlist_head *paths,
4086 got_worktree_checkout_cb progress_cb, void *progress_arg,
4087 struct got_repository *repo, int no_ignores)
4089 struct got_fileindex *fileindex = NULL;
4090 char *fileindex_path = NULL;
4091 const struct got_error *err = NULL, *sync_err, *unlockerr;
4092 struct got_pathlist_entry *pe;
4093 struct schedule_addition_args saa;
4095 err = lock_worktree(worktree, LOCK_EX);
4096 if (err)
4097 return err;
4099 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4100 if (err)
4101 goto done;
4103 saa.worktree = worktree;
4104 saa.fileindex = fileindex;
4105 saa.progress_cb = progress_cb;
4106 saa.progress_arg = progress_arg;
4107 saa.repo = repo;
4109 TAILQ_FOREACH(pe, paths, entry) {
4110 err = worktree_status(worktree, pe->path, fileindex, repo,
4111 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4112 if (err)
4113 break;
4115 sync_err = sync_fileindex(fileindex, fileindex_path);
4116 if (sync_err && err == NULL)
4117 err = sync_err;
4118 done:
4119 free(fileindex_path);
4120 if (fileindex)
4121 got_fileindex_free(fileindex);
4122 unlockerr = lock_worktree(worktree, LOCK_SH);
4123 if (unlockerr && err == NULL)
4124 err = unlockerr;
4125 return err;
4128 struct schedule_deletion_args {
4129 struct got_worktree *worktree;
4130 struct got_fileindex *fileindex;
4131 got_worktree_delete_cb progress_cb;
4132 void *progress_arg;
4133 struct got_repository *repo;
4134 int delete_local_mods;
4135 int keep_on_disk;
4136 int ignore_missing_paths;
4137 const char *status_codes;
4140 static const struct got_error *
4141 schedule_for_deletion(void *arg, unsigned char status,
4142 unsigned char staged_status, const char *relpath,
4143 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4144 struct got_object_id *commit_id, int dirfd, const char *de_name)
4146 struct schedule_deletion_args *a = arg;
4147 const struct got_error *err = NULL;
4148 struct got_fileindex_entry *ie = NULL;
4149 struct stat sb;
4150 char *ondisk_path;
4152 if (status == GOT_STATUS_NONEXISTENT) {
4153 if (a->ignore_missing_paths)
4154 return NULL;
4155 return got_error_set_errno(ENOENT, relpath);
4158 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4159 if (ie == NULL)
4160 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4162 staged_status = get_staged_status(ie);
4163 if (staged_status != GOT_STATUS_NO_CHANGE) {
4164 if (staged_status == GOT_STATUS_DELETE)
4165 return NULL;
4166 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4169 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4170 relpath) == -1)
4171 return got_error_from_errno("asprintf");
4173 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4174 a->repo);
4175 if (err)
4176 goto done;
4178 if (a->status_codes) {
4179 size_t ncodes = strlen(a->status_codes);
4180 int i;
4181 for (i = 0; i < ncodes ; i++) {
4182 if (status == a->status_codes[i])
4183 break;
4185 if (i == ncodes) {
4186 /* Do not delete files in non-matching status. */
4187 free(ondisk_path);
4188 return NULL;
4190 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4191 a->status_codes[i] != GOT_STATUS_MISSING) {
4192 static char msg[64];
4193 snprintf(msg, sizeof(msg),
4194 "invalid status code '%c'", a->status_codes[i]);
4195 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4196 goto done;
4200 if (status != GOT_STATUS_NO_CHANGE) {
4201 if (status == GOT_STATUS_DELETE)
4202 goto done;
4203 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4204 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4205 goto done;
4207 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4208 err = got_error_set_errno(ENOENT, relpath);
4209 goto done;
4211 if (status != GOT_STATUS_MODIFY &&
4212 status != GOT_STATUS_MISSING) {
4213 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4214 goto done;
4218 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4219 size_t root_len;
4221 if (dirfd != -1) {
4222 if (unlinkat(dirfd, de_name, 0) == -1) {
4223 err = got_error_from_errno2("unlinkat",
4224 ondisk_path);
4225 goto done;
4227 } else if (unlink(ondisk_path) == -1) {
4228 err = got_error_from_errno2("unlink", ondisk_path);
4229 goto done;
4232 root_len = strlen(a->worktree->root_path);
4233 do {
4234 char *parent;
4235 err = got_path_dirname(&parent, ondisk_path);
4236 if (err)
4237 goto done;
4238 free(ondisk_path);
4239 ondisk_path = parent;
4240 if (rmdir(ondisk_path) == -1) {
4241 if (errno != ENOTEMPTY)
4242 err = got_error_from_errno2("rmdir",
4243 ondisk_path);
4244 break;
4246 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4247 strlen(ondisk_path), root_len) != 0);
4250 got_fileindex_entry_mark_deleted_from_disk(ie);
4251 done:
4252 free(ondisk_path);
4253 if (err)
4254 return err;
4255 if (status == GOT_STATUS_DELETE)
4256 return NULL;
4257 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4258 staged_status, relpath);
4261 const struct got_error *
4262 got_worktree_schedule_delete(struct got_worktree *worktree,
4263 struct got_pathlist_head *paths, int delete_local_mods,
4264 const char *status_codes,
4265 got_worktree_delete_cb progress_cb, void *progress_arg,
4266 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4268 struct got_fileindex *fileindex = NULL;
4269 char *fileindex_path = NULL;
4270 const struct got_error *err = NULL, *sync_err, *unlockerr;
4271 struct got_pathlist_entry *pe;
4272 struct schedule_deletion_args sda;
4274 err = lock_worktree(worktree, LOCK_EX);
4275 if (err)
4276 return err;
4278 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4279 if (err)
4280 goto done;
4282 sda.worktree = worktree;
4283 sda.fileindex = fileindex;
4284 sda.progress_cb = progress_cb;
4285 sda.progress_arg = progress_arg;
4286 sda.repo = repo;
4287 sda.delete_local_mods = delete_local_mods;
4288 sda.keep_on_disk = keep_on_disk;
4289 sda.ignore_missing_paths = ignore_missing_paths;
4290 sda.status_codes = status_codes;
4292 TAILQ_FOREACH(pe, paths, entry) {
4293 err = worktree_status(worktree, pe->path, fileindex, repo,
4294 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4295 if (err)
4296 break;
4298 sync_err = sync_fileindex(fileindex, fileindex_path);
4299 if (sync_err && err == NULL)
4300 err = sync_err;
4301 done:
4302 free(fileindex_path);
4303 if (fileindex)
4304 got_fileindex_free(fileindex);
4305 unlockerr = lock_worktree(worktree, LOCK_SH);
4306 if (unlockerr && err == NULL)
4307 err = unlockerr;
4308 return err;
4311 static const struct got_error *
4312 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4314 const struct got_error *err = NULL;
4315 char *line = NULL;
4316 size_t linesize = 0, n;
4317 ssize_t linelen;
4319 linelen = getline(&line, &linesize, infile);
4320 if (linelen == -1) {
4321 if (ferror(infile)) {
4322 err = got_error_from_errno("getline");
4323 goto done;
4325 return NULL;
4327 if (outfile) {
4328 n = fwrite(line, 1, linelen, outfile);
4329 if (n != linelen) {
4330 err = got_ferror(outfile, GOT_ERR_IO);
4331 goto done;
4334 if (rejectfile) {
4335 n = fwrite(line, 1, linelen, rejectfile);
4336 if (n != linelen)
4337 err = got_ferror(rejectfile, GOT_ERR_IO);
4339 done:
4340 free(line);
4341 return err;
4344 static const struct got_error *
4345 skip_one_line(FILE *f)
4347 char *line = NULL;
4348 size_t linesize = 0;
4349 ssize_t linelen;
4351 linelen = getline(&line, &linesize, f);
4352 if (linelen == -1) {
4353 if (ferror(f))
4354 return got_error_from_errno("getline");
4355 return NULL;
4357 free(line);
4358 return NULL;
4361 static const struct got_error *
4362 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4363 int start_old, int end_old, int start_new, int end_new,
4364 FILE *outfile, FILE *rejectfile)
4366 const struct got_error *err;
4368 /* Copy old file's lines leading up to patch. */
4369 while (!feof(f1) && *line_cur1 < start_old) {
4370 err = copy_one_line(f1, outfile, NULL);
4371 if (err)
4372 return err;
4373 (*line_cur1)++;
4375 /* Skip new file's lines leading up to patch. */
4376 while (!feof(f2) && *line_cur2 < start_new) {
4377 if (rejectfile)
4378 err = copy_one_line(f2, NULL, rejectfile);
4379 else
4380 err = skip_one_line(f2);
4381 if (err)
4382 return err;
4383 (*line_cur2)++;
4385 /* Copy patched lines. */
4386 while (!feof(f2) && *line_cur2 <= end_new) {
4387 err = copy_one_line(f2, outfile, NULL);
4388 if (err)
4389 return err;
4390 (*line_cur2)++;
4392 /* Skip over old file's replaced lines. */
4393 while (!feof(f1) && *line_cur1 <= end_old) {
4394 if (rejectfile)
4395 err = copy_one_line(f1, NULL, rejectfile);
4396 else
4397 err = skip_one_line(f1);
4398 if (err)
4399 return err;
4400 (*line_cur1)++;
4403 return NULL;
4406 static const struct got_error *
4407 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4408 FILE *outfile, FILE *rejectfile)
4410 const struct got_error *err;
4412 if (outfile) {
4413 /* Copy old file's lines until EOF. */
4414 while (!feof(f1)) {
4415 err = copy_one_line(f1, outfile, NULL);
4416 if (err)
4417 return err;
4418 (*line_cur1)++;
4421 if (rejectfile) {
4422 /* Copy new file's lines until EOF. */
4423 while (!feof(f2)) {
4424 err = copy_one_line(f2, NULL, rejectfile);
4425 if (err)
4426 return err;
4427 (*line_cur2)++;
4431 return NULL;
4434 static const struct got_error *
4435 apply_or_reject_change(int *choice, int *nchunks_used,
4436 struct diff_result *diff_result, int n,
4437 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4438 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4439 got_worktree_patch_cb patch_cb, void *patch_arg)
4441 const struct got_error *err = NULL;
4442 struct diff_chunk_context cc = {};
4443 int start_old, end_old, start_new, end_new;
4444 FILE *hunkfile;
4445 struct diff_output_unidiff_state *diff_state;
4446 struct diff_input_info diff_info;
4447 int rc;
4449 *choice = GOT_PATCH_CHOICE_NONE;
4451 /* Get changed line numbers without context lines for copy_change(). */
4452 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4453 start_old = cc.left.start;
4454 end_old = cc.left.end;
4455 start_new = cc.right.start;
4456 end_new = cc.right.end;
4458 /* Get the same change with context lines for display. */
4459 memset(&cc, 0, sizeof(cc));
4460 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4462 memset(&diff_info, 0, sizeof(diff_info));
4463 diff_info.left_path = relpath;
4464 diff_info.right_path = relpath;
4466 diff_state = diff_output_unidiff_state_alloc();
4467 if (diff_state == NULL)
4468 return got_error_set_errno(ENOMEM,
4469 "diff_output_unidiff_state_alloc");
4471 hunkfile = got_opentemp();
4472 if (hunkfile == NULL) {
4473 err = got_error_from_errno("got_opentemp");
4474 goto done;
4477 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4478 diff_result, &cc);
4479 if (rc != DIFF_RC_OK) {
4480 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4481 goto done;
4484 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4485 err = got_ferror(hunkfile, GOT_ERR_IO);
4486 goto done;
4489 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4490 hunkfile, changeno, nchanges);
4491 if (err)
4492 goto done;
4494 switch (*choice) {
4495 case GOT_PATCH_CHOICE_YES:
4496 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4497 end_old, start_new, end_new, outfile, rejectfile);
4498 break;
4499 case GOT_PATCH_CHOICE_NO:
4500 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4501 end_old, start_new, end_new, rejectfile, outfile);
4502 break;
4503 case GOT_PATCH_CHOICE_QUIT:
4504 break;
4505 default:
4506 err = got_error(GOT_ERR_PATCH_CHOICE);
4507 break;
4509 done:
4510 diff_output_unidiff_state_free(diff_state);
4511 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4512 err = got_error_from_errno("fclose");
4513 return err;
4516 struct revert_file_args {
4517 struct got_worktree *worktree;
4518 struct got_fileindex *fileindex;
4519 got_worktree_checkout_cb progress_cb;
4520 void *progress_arg;
4521 got_worktree_patch_cb patch_cb;
4522 void *patch_arg;
4523 struct got_repository *repo;
4524 int unlink_added_files;
4527 static const struct got_error *
4528 create_patched_content(char **path_outfile, int reverse_patch,
4529 struct got_object_id *blob_id, const char *path2,
4530 int dirfd2, const char *de_name2,
4531 const char *relpath, struct got_repository *repo,
4532 got_worktree_patch_cb patch_cb, void *patch_arg)
4534 const struct got_error *err, *free_err;
4535 struct got_blob_object *blob = NULL;
4536 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4537 int fd = -1, fd2 = -1;
4538 char link_target[PATH_MAX];
4539 ssize_t link_len = 0;
4540 char *path1 = NULL, *id_str = NULL;
4541 struct stat sb2;
4542 struct got_diffreg_result *diffreg_result = NULL;
4543 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4544 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4546 *path_outfile = NULL;
4548 err = got_object_id_str(&id_str, blob_id);
4549 if (err)
4550 return err;
4552 if (dirfd2 != -1) {
4553 fd2 = openat(dirfd2, de_name2,
4554 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4555 if (fd2 == -1) {
4556 if (!got_err_open_nofollow_on_symlink()) {
4557 err = got_error_from_errno2("openat", path2);
4558 goto done;
4560 link_len = readlinkat(dirfd2, de_name2,
4561 link_target, sizeof(link_target));
4562 if (link_len == -1) {
4563 return got_error_from_errno2("readlinkat",
4564 path2);
4566 sb2.st_mode = S_IFLNK;
4567 sb2.st_size = link_len;
4569 } else {
4570 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4571 if (fd2 == -1) {
4572 if (!got_err_open_nofollow_on_symlink()) {
4573 err = got_error_from_errno2("open", path2);
4574 goto done;
4576 link_len = readlink(path2, link_target,
4577 sizeof(link_target));
4578 if (link_len == -1)
4579 return got_error_from_errno2("readlink", path2);
4580 sb2.st_mode = S_IFLNK;
4581 sb2.st_size = link_len;
4584 if (fd2 != -1) {
4585 if (fstat(fd2, &sb2) == -1) {
4586 err = got_error_from_errno2("fstat", path2);
4587 goto done;
4590 f2 = fdopen(fd2, "r");
4591 if (f2 == NULL) {
4592 err = got_error_from_errno2("fdopen", path2);
4593 goto done;
4595 fd2 = -1;
4596 } else {
4597 size_t n;
4598 f2 = got_opentemp();
4599 if (f2 == NULL) {
4600 err = got_error_from_errno2("got_opentemp", path2);
4601 goto done;
4603 n = fwrite(link_target, 1, link_len, f2);
4604 if (n != link_len) {
4605 err = got_ferror(f2, GOT_ERR_IO);
4606 goto done;
4608 if (fflush(f2) == EOF) {
4609 err = got_error_from_errno("fflush");
4610 goto done;
4612 rewind(f2);
4615 fd = got_opentempfd();
4616 if (fd == -1) {
4617 err = got_error_from_errno("got_opentempfd");
4618 goto done;
4621 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4622 if (err)
4623 goto done;
4625 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4626 if (err)
4627 goto done;
4629 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4630 if (err)
4631 goto done;
4633 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4634 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4635 if (err)
4636 goto done;
4638 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4639 "");
4640 if (err)
4641 goto done;
4643 if (fseek(f1, 0L, SEEK_SET) == -1)
4644 return got_ferror(f1, GOT_ERR_IO);
4645 if (fseek(f2, 0L, SEEK_SET) == -1)
4646 return got_ferror(f2, GOT_ERR_IO);
4648 /* Count the number of actual changes in the diff result. */
4649 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4650 struct diff_chunk_context cc = {};
4651 diff_chunk_context_load_change(&cc, &nchunks_used,
4652 diffreg_result->result, n, 0);
4653 nchanges++;
4655 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4656 int choice;
4657 err = apply_or_reject_change(&choice, &nchunks_used,
4658 diffreg_result->result, n, relpath, f1, f2,
4659 &line_cur1, &line_cur2,
4660 reverse_patch ? NULL : outfile,
4661 reverse_patch ? outfile : NULL,
4662 ++i, nchanges, patch_cb, patch_arg);
4663 if (err)
4664 goto done;
4665 if (choice == GOT_PATCH_CHOICE_YES)
4666 have_content = 1;
4667 else if (choice == GOT_PATCH_CHOICE_QUIT)
4668 break;
4670 if (have_content) {
4671 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4672 reverse_patch ? NULL : outfile,
4673 reverse_patch ? outfile : NULL);
4674 if (err)
4675 goto done;
4677 if (!S_ISLNK(sb2.st_mode)) {
4678 mode_t mode;
4680 mode = apply_umask(sb2.st_mode);
4681 if (fchmod(fileno(outfile), mode) == -1) {
4682 err = got_error_from_errno2("fchmod", path2);
4683 goto done;
4687 done:
4688 free(id_str);
4689 if (fd != -1 && close(fd) == -1 && err == NULL)
4690 err = got_error_from_errno("close");
4691 if (blob)
4692 got_object_blob_close(blob);
4693 free_err = got_diffreg_result_free(diffreg_result);
4694 if (err == NULL)
4695 err = free_err;
4696 if (f1 && fclose(f1) == EOF && err == NULL)
4697 err = got_error_from_errno2("fclose", path1);
4698 if (f2 && fclose(f2) == EOF && err == NULL)
4699 err = got_error_from_errno2("fclose", path2);
4700 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4701 err = got_error_from_errno2("close", path2);
4702 if (outfile && fclose(outfile) == EOF && err == NULL)
4703 err = got_error_from_errno2("fclose", *path_outfile);
4704 if (path1 && unlink(path1) == -1 && err == NULL)
4705 err = got_error_from_errno2("unlink", path1);
4706 if (err || !have_content) {
4707 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4708 err = got_error_from_errno2("unlink", *path_outfile);
4709 free(*path_outfile);
4710 *path_outfile = NULL;
4712 free(path1);
4713 return err;
4716 static const struct got_error *
4717 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4718 const char *relpath, struct got_object_id *blob_id,
4719 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4720 int dirfd, const char *de_name)
4722 struct revert_file_args *a = arg;
4723 const struct got_error *err = NULL;
4724 char *parent_path = NULL;
4725 struct got_fileindex_entry *ie;
4726 struct got_commit_object *base_commit = NULL;
4727 struct got_tree_object *tree = NULL;
4728 struct got_object_id *tree_id = NULL;
4729 const struct got_tree_entry *te = NULL;
4730 char *tree_path = NULL, *te_name;
4731 char *ondisk_path = NULL, *path_content = NULL;
4732 struct got_blob_object *blob = NULL;
4733 int fd = -1;
4735 /* Reverting a staged deletion is a no-op. */
4736 if (status == GOT_STATUS_DELETE &&
4737 staged_status != GOT_STATUS_NO_CHANGE)
4738 return NULL;
4740 if (status == GOT_STATUS_UNVERSIONED)
4741 return (*a->progress_cb)(a->progress_arg,
4742 GOT_STATUS_UNVERSIONED, relpath);
4744 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4745 if (ie == NULL)
4746 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4748 /* Construct in-repository path of tree which contains this blob. */
4749 err = got_path_dirname(&parent_path, ie->path);
4750 if (err) {
4751 if (err->code != GOT_ERR_BAD_PATH)
4752 goto done;
4753 parent_path = strdup("/");
4754 if (parent_path == NULL) {
4755 err = got_error_from_errno("strdup");
4756 goto done;
4759 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4760 tree_path = strdup(parent_path);
4761 if (tree_path == NULL) {
4762 err = got_error_from_errno("strdup");
4763 goto done;
4765 } else {
4766 if (got_path_is_root_dir(parent_path)) {
4767 tree_path = strdup(a->worktree->path_prefix);
4768 if (tree_path == NULL) {
4769 err = got_error_from_errno("strdup");
4770 goto done;
4772 } else {
4773 if (asprintf(&tree_path, "%s/%s",
4774 a->worktree->path_prefix, parent_path) == -1) {
4775 err = got_error_from_errno("asprintf");
4776 goto done;
4781 err = got_object_open_as_commit(&base_commit, a->repo,
4782 a->worktree->base_commit_id);
4783 if (err)
4784 goto done;
4786 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4787 if (err) {
4788 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4789 (status == GOT_STATUS_ADD ||
4790 staged_status == GOT_STATUS_ADD)))
4791 goto done;
4792 } else {
4793 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4794 if (err)
4795 goto done;
4797 err = got_path_basename(&te_name, ie->path);
4798 if (err)
4799 goto done;
4801 te = got_object_tree_find_entry(tree, te_name);
4802 free(te_name);
4803 if (te == NULL && status != GOT_STATUS_ADD &&
4804 staged_status != GOT_STATUS_ADD) {
4805 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4806 goto done;
4810 switch (status) {
4811 case GOT_STATUS_ADD:
4812 if (a->patch_cb) {
4813 int choice = GOT_PATCH_CHOICE_NONE;
4814 err = (*a->patch_cb)(&choice, a->patch_arg,
4815 status, ie->path, NULL, 1, 1);
4816 if (err)
4817 goto done;
4818 if (choice != GOT_PATCH_CHOICE_YES)
4819 break;
4821 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4822 ie->path);
4823 if (err)
4824 goto done;
4825 got_fileindex_entry_remove(a->fileindex, ie);
4826 if (a->unlink_added_files) {
4827 if (asprintf(&ondisk_path, "%s/%s",
4828 got_worktree_get_root_path(a->worktree),
4829 relpath) == -1) {
4830 err = got_error_from_errno("asprintf");
4831 goto done;
4833 if (unlink(ondisk_path) == -1) {
4834 err = got_error_from_errno2("unlink",
4835 ondisk_path);
4836 break;
4839 break;
4840 case GOT_STATUS_DELETE:
4841 if (a->patch_cb) {
4842 int choice = GOT_PATCH_CHOICE_NONE;
4843 err = (*a->patch_cb)(&choice, a->patch_arg,
4844 status, ie->path, NULL, 1, 1);
4845 if (err)
4846 goto done;
4847 if (choice != GOT_PATCH_CHOICE_YES)
4848 break;
4850 /* fall through */
4851 case GOT_STATUS_MODIFY:
4852 case GOT_STATUS_MODE_CHANGE:
4853 case GOT_STATUS_CONFLICT:
4854 case GOT_STATUS_MISSING: {
4855 struct got_object_id id;
4856 if (staged_status == GOT_STATUS_ADD ||
4857 staged_status == GOT_STATUS_MODIFY)
4858 got_fileindex_entry_get_staged_blob_id(&id, ie);
4859 else
4860 got_fileindex_entry_get_blob_id(&id, ie);
4861 fd = got_opentempfd();
4862 if (fd == -1) {
4863 err = got_error_from_errno("got_opentempfd");
4864 goto done;
4867 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4868 if (err)
4869 goto done;
4871 if (asprintf(&ondisk_path, "%s/%s",
4872 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4873 err = got_error_from_errno("asprintf");
4874 goto done;
4877 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4878 status == GOT_STATUS_CONFLICT)) {
4879 int is_bad_symlink = 0;
4880 err = create_patched_content(&path_content, 1, &id,
4881 ondisk_path, dirfd, de_name, ie->path, a->repo,
4882 a->patch_cb, a->patch_arg);
4883 if (err || path_content == NULL)
4884 break;
4885 if (te && S_ISLNK(te->mode)) {
4886 if (unlink(path_content) == -1) {
4887 err = got_error_from_errno2("unlink",
4888 path_content);
4889 break;
4891 err = install_symlink(&is_bad_symlink,
4892 a->worktree, ondisk_path, ie->path,
4893 blob, 0, 1, 0, 0, a->repo,
4894 a->progress_cb, a->progress_arg);
4895 } else {
4896 if (rename(path_content, ondisk_path) == -1) {
4897 err = got_error_from_errno3("rename",
4898 path_content, ondisk_path);
4899 goto done;
4902 } else {
4903 int is_bad_symlink = 0;
4904 if (te && S_ISLNK(te->mode)) {
4905 err = install_symlink(&is_bad_symlink,
4906 a->worktree, ondisk_path, ie->path,
4907 blob, 0, 1, 0, 0, a->repo,
4908 a->progress_cb, a->progress_arg);
4909 } else {
4910 err = install_blob(a->worktree, ondisk_path,
4911 ie->path,
4912 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4913 got_fileindex_perms_to_st(ie), blob,
4914 0, 1, 0, 0, a->repo,
4915 a->progress_cb, a->progress_arg);
4917 if (err)
4918 goto done;
4919 if (status == GOT_STATUS_DELETE ||
4920 status == GOT_STATUS_MODE_CHANGE) {
4921 err = got_fileindex_entry_update(ie,
4922 a->worktree->root_fd, relpath,
4923 blob->id.sha1,
4924 a->worktree->base_commit_id->sha1, 1);
4925 if (err)
4926 goto done;
4928 if (is_bad_symlink) {
4929 got_fileindex_entry_filetype_set(ie,
4930 GOT_FILEIDX_MODE_BAD_SYMLINK);
4933 break;
4935 default:
4936 break;
4938 done:
4939 free(ondisk_path);
4940 free(path_content);
4941 free(parent_path);
4942 free(tree_path);
4943 if (fd != -1 && close(fd) == -1 && err == NULL)
4944 err = got_error_from_errno("close");
4945 if (blob)
4946 got_object_blob_close(blob);
4947 if (tree)
4948 got_object_tree_close(tree);
4949 free(tree_id);
4950 if (base_commit)
4951 got_object_commit_close(base_commit);
4952 return err;
4955 const struct got_error *
4956 got_worktree_revert(struct got_worktree *worktree,
4957 struct got_pathlist_head *paths,
4958 got_worktree_checkout_cb progress_cb, void *progress_arg,
4959 got_worktree_patch_cb patch_cb, void *patch_arg,
4960 struct got_repository *repo)
4962 struct got_fileindex *fileindex = NULL;
4963 char *fileindex_path = NULL;
4964 const struct got_error *err = NULL, *unlockerr = NULL;
4965 const struct got_error *sync_err = NULL;
4966 struct got_pathlist_entry *pe;
4967 struct revert_file_args rfa;
4969 err = lock_worktree(worktree, LOCK_EX);
4970 if (err)
4971 return err;
4973 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4974 if (err)
4975 goto done;
4977 rfa.worktree = worktree;
4978 rfa.fileindex = fileindex;
4979 rfa.progress_cb = progress_cb;
4980 rfa.progress_arg = progress_arg;
4981 rfa.patch_cb = patch_cb;
4982 rfa.patch_arg = patch_arg;
4983 rfa.repo = repo;
4984 rfa.unlink_added_files = 0;
4985 TAILQ_FOREACH(pe, paths, entry) {
4986 err = worktree_status(worktree, pe->path, fileindex, repo,
4987 revert_file, &rfa, NULL, NULL, 1, 0);
4988 if (err)
4989 break;
4991 sync_err = sync_fileindex(fileindex, fileindex_path);
4992 if (sync_err && err == NULL)
4993 err = sync_err;
4994 done:
4995 free(fileindex_path);
4996 if (fileindex)
4997 got_fileindex_free(fileindex);
4998 unlockerr = lock_worktree(worktree, LOCK_SH);
4999 if (unlockerr && err == NULL)
5000 err = unlockerr;
5001 return err;
5004 static void
5005 free_commitable(struct got_commitable *ct)
5007 free(ct->path);
5008 free(ct->in_repo_path);
5009 free(ct->ondisk_path);
5010 free(ct->blob_id);
5011 free(ct->base_blob_id);
5012 free(ct->staged_blob_id);
5013 free(ct->base_commit_id);
5014 free(ct);
5017 struct collect_commitables_arg {
5018 struct got_pathlist_head *commitable_paths;
5019 struct got_repository *repo;
5020 struct got_worktree *worktree;
5021 struct got_fileindex *fileindex;
5022 int have_staged_files;
5023 int allow_bad_symlinks;
5024 int diff_header_shown;
5025 int commit_conflicts;
5026 FILE *diff_outfile;
5027 FILE *f1;
5028 FILE *f2;
5032 * Create a file which contains the target path of a symlink so we can feed
5033 * it as content to the diff engine.
5035 static const struct got_error *
5036 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5037 const char *abspath)
5039 const struct got_error *err = NULL;
5040 char target_path[PATH_MAX];
5041 ssize_t target_len, outlen;
5043 *fd = -1;
5045 if (dirfd != -1) {
5046 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5047 if (target_len == -1)
5048 return got_error_from_errno2("readlinkat", abspath);
5049 } else {
5050 target_len = readlink(abspath, target_path, PATH_MAX);
5051 if (target_len == -1)
5052 return got_error_from_errno2("readlink", abspath);
5055 *fd = got_opentempfd();
5056 if (*fd == -1)
5057 return got_error_from_errno("got_opentempfd");
5059 outlen = write(*fd, target_path, target_len);
5060 if (outlen == -1) {
5061 err = got_error_from_errno("got_opentempfd");
5062 goto done;
5065 if (lseek(*fd, 0, SEEK_SET) == -1) {
5066 err = got_error_from_errno2("lseek", abspath);
5067 goto done;
5069 done:
5070 if (err) {
5071 close(*fd);
5072 *fd = -1;
5074 return err;
5077 static const struct got_error *
5078 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5079 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5080 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5082 const struct got_error *err = NULL;
5083 struct got_blob_object *blob1 = NULL;
5084 int fd = -1, fd1 = -1, fd2 = -1;
5085 FILE *ondisk_file = NULL;
5086 char *label1 = NULL;
5087 struct stat sb;
5088 off_t size1 = 0;
5089 int f2_exists = 0;
5090 char *id_str = NULL;
5092 memset(&sb, 0, sizeof(sb));
5094 if (diff_staged) {
5095 if (ct->staged_status != GOT_STATUS_MODIFY &&
5096 ct->staged_status != GOT_STATUS_ADD &&
5097 ct->staged_status != GOT_STATUS_DELETE)
5098 return NULL;
5099 } else {
5100 if (ct->status != GOT_STATUS_MODIFY &&
5101 ct->status != GOT_STATUS_ADD &&
5102 ct->status != GOT_STATUS_DELETE &&
5103 ct->status != GOT_STATUS_CONFLICT)
5104 return NULL;
5107 err = got_opentemp_truncate(f1);
5108 if (err)
5109 return got_error_from_errno("got_opentemp_truncate");
5110 err = got_opentemp_truncate(f2);
5111 if (err)
5112 return got_error_from_errno("got_opentemp_truncate");
5114 if (!*diff_header_shown) {
5115 err = got_object_id_str(&id_str, worktree->base_commit_id);
5116 if (err)
5117 return err;
5118 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5119 got_worktree_get_root_path(worktree));
5120 fprintf(diff_outfile, "commit - %s\n", id_str);
5121 fprintf(diff_outfile, "path + %s%s\n",
5122 got_worktree_get_root_path(worktree),
5123 diff_staged ? " (staged changes)" : "");
5124 *diff_header_shown = 1;
5127 if (diff_staged) {
5128 const char *label1 = NULL, *label2 = NULL;
5129 switch (ct->staged_status) {
5130 case GOT_STATUS_MODIFY:
5131 label1 = ct->path;
5132 label2 = ct->path;
5133 break;
5134 case GOT_STATUS_ADD:
5135 label2 = ct->path;
5136 break;
5137 case GOT_STATUS_DELETE:
5138 label1 = ct->path;
5139 break;
5140 default:
5141 return got_error(GOT_ERR_FILE_STATUS);
5143 fd1 = got_opentempfd();
5144 if (fd1 == -1) {
5145 err = got_error_from_errno("got_opentempfd");
5146 goto done;
5148 fd2 = got_opentempfd();
5149 if (fd2 == -1) {
5150 err = got_error_from_errno("got_opentempfd");
5151 goto done;
5153 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5154 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5155 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5156 NULL, repo, diff_outfile);
5157 goto done;
5160 fd1 = got_opentempfd();
5161 if (fd1 == -1) {
5162 err = got_error_from_errno("got_opentempfd");
5163 goto done;
5166 if (ct->status != GOT_STATUS_ADD) {
5167 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5168 8192, fd1);
5169 if (err)
5170 goto done;
5173 if (ct->status != GOT_STATUS_DELETE) {
5174 if (dirfd != -1) {
5175 fd = openat(dirfd, de_name,
5176 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5177 if (fd == -1) {
5178 if (!got_err_open_nofollow_on_symlink()) {
5179 err = got_error_from_errno2("openat",
5180 ct->ondisk_path);
5181 goto done;
5183 err = get_symlink_target_file(&fd, dirfd,
5184 de_name, ct->ondisk_path);
5185 if (err)
5186 goto done;
5188 } else {
5189 fd = open(ct->ondisk_path,
5190 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5191 if (fd == -1) {
5192 if (!got_err_open_nofollow_on_symlink()) {
5193 err = got_error_from_errno2("open",
5194 ct->ondisk_path);
5195 goto done;
5197 err = get_symlink_target_file(&fd, dirfd,
5198 de_name, ct->ondisk_path);
5199 if (err)
5200 goto done;
5203 if (fstatat(fd, ct->ondisk_path, &sb,
5204 AT_SYMLINK_NOFOLLOW) == -1) {
5205 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5206 goto done;
5208 ondisk_file = fdopen(fd, "r");
5209 if (ondisk_file == NULL) {
5210 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5211 goto done;
5213 fd = -1;
5214 f2_exists = 1;
5217 if (blob1) {
5218 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5219 f1, blob1);
5220 if (err)
5221 goto done;
5224 err = got_diff_blob_file(blob1, f1, size1, label1,
5225 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5226 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5227 done:
5228 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5229 err = got_error_from_errno("close");
5230 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5231 err = got_error_from_errno("close");
5232 if (blob1)
5233 got_object_blob_close(blob1);
5234 if (fd != -1 && close(fd) == -1 && err == NULL)
5235 err = got_error_from_errno("close");
5236 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5237 err = got_error_from_errno("fclose");
5238 return err;
5241 static const struct got_error *
5242 collect_commitables(void *arg, unsigned char status,
5243 unsigned char staged_status, const char *relpath,
5244 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5245 struct got_object_id *commit_id, int dirfd, const char *de_name)
5247 struct collect_commitables_arg *a = arg;
5248 const struct got_error *err = NULL;
5249 struct got_commitable *ct = NULL;
5250 struct got_pathlist_entry *new = NULL;
5251 char *parent_path = NULL, *path = NULL;
5252 struct stat sb;
5254 if (a->have_staged_files) {
5255 if (staged_status != GOT_STATUS_MODIFY &&
5256 staged_status != GOT_STATUS_ADD &&
5257 staged_status != GOT_STATUS_DELETE)
5258 return NULL;
5259 } else {
5260 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5261 printf("C %s\n", relpath);
5262 return got_error(GOT_ERR_COMMIT_CONFLICT);
5265 if (status != GOT_STATUS_MODIFY &&
5266 status != GOT_STATUS_MODE_CHANGE &&
5267 status != GOT_STATUS_ADD &&
5268 status != GOT_STATUS_DELETE &&
5269 status != GOT_STATUS_CONFLICT)
5270 return NULL;
5273 if (asprintf(&path, "/%s", relpath) == -1) {
5274 err = got_error_from_errno("asprintf");
5275 goto done;
5277 if (strcmp(path, "/") == 0) {
5278 parent_path = strdup("");
5279 if (parent_path == NULL)
5280 return got_error_from_errno("strdup");
5281 } else {
5282 err = got_path_dirname(&parent_path, path);
5283 if (err)
5284 return err;
5287 ct = calloc(1, sizeof(*ct));
5288 if (ct == NULL) {
5289 err = got_error_from_errno("calloc");
5290 goto done;
5293 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5294 relpath) == -1) {
5295 err = got_error_from_errno("asprintf");
5296 goto done;
5299 if (staged_status == GOT_STATUS_ADD ||
5300 staged_status == GOT_STATUS_MODIFY) {
5301 struct got_fileindex_entry *ie;
5302 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5303 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5304 case GOT_FILEIDX_MODE_REGULAR_FILE:
5305 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5306 ct->mode = S_IFREG;
5307 break;
5308 case GOT_FILEIDX_MODE_SYMLINK:
5309 ct->mode = S_IFLNK;
5310 break;
5311 default:
5312 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5313 goto done;
5315 ct->mode |= got_fileindex_entry_perms_get(ie);
5316 } else if (status != GOT_STATUS_DELETE &&
5317 staged_status != GOT_STATUS_DELETE) {
5318 if (dirfd != -1) {
5319 if (fstatat(dirfd, de_name, &sb,
5320 AT_SYMLINK_NOFOLLOW) == -1) {
5321 err = got_error_from_errno2("fstatat",
5322 ct->ondisk_path);
5323 goto done;
5325 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5326 err = got_error_from_errno2("lstat", ct->ondisk_path);
5327 goto done;
5329 ct->mode = sb.st_mode;
5332 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5333 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5334 relpath) == -1) {
5335 err = got_error_from_errno("asprintf");
5336 goto done;
5339 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5340 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5341 int is_bad_symlink;
5342 char target_path[PATH_MAX];
5343 ssize_t target_len;
5344 target_len = readlink(ct->ondisk_path, target_path,
5345 sizeof(target_path));
5346 if (target_len == -1) {
5347 err = got_error_from_errno2("readlink",
5348 ct->ondisk_path);
5349 goto done;
5351 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5352 target_len, ct->ondisk_path, a->worktree->root_path);
5353 if (err)
5354 goto done;
5355 if (is_bad_symlink) {
5356 err = got_error_path(ct->ondisk_path,
5357 GOT_ERR_BAD_SYMLINK);
5358 goto done;
5363 ct->status = status;
5364 ct->staged_status = staged_status;
5365 ct->blob_id = NULL; /* will be filled in when blob gets created */
5366 if (ct->status != GOT_STATUS_ADD &&
5367 ct->staged_status != GOT_STATUS_ADD) {
5368 ct->base_blob_id = got_object_id_dup(blob_id);
5369 if (ct->base_blob_id == NULL) {
5370 err = got_error_from_errno("got_object_id_dup");
5371 goto done;
5373 ct->base_commit_id = got_object_id_dup(commit_id);
5374 if (ct->base_commit_id == NULL) {
5375 err = got_error_from_errno("got_object_id_dup");
5376 goto done;
5379 if (ct->staged_status == GOT_STATUS_ADD ||
5380 ct->staged_status == GOT_STATUS_MODIFY) {
5381 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5382 if (ct->staged_blob_id == NULL) {
5383 err = got_error_from_errno("got_object_id_dup");
5384 goto done;
5387 ct->path = strdup(path);
5388 if (ct->path == NULL) {
5389 err = got_error_from_errno("strdup");
5390 goto done;
5392 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5393 if (err)
5394 goto done;
5396 if (a->diff_outfile && ct && new != NULL) {
5397 err = append_ct_diff(ct, &a->diff_header_shown,
5398 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5399 a->have_staged_files, a->repo, a->worktree);
5400 if (err)
5401 goto done;
5403 done:
5404 if (ct && (err || new == NULL))
5405 free_commitable(ct);
5406 free(parent_path);
5407 free(path);
5408 return err;
5411 static const struct got_error *write_tree(struct got_object_id **, int *,
5412 struct got_tree_object *, const char *, struct got_pathlist_head *,
5413 got_worktree_status_cb status_cb, void *status_arg,
5414 struct got_repository *);
5416 static const struct got_error *
5417 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5418 struct got_tree_entry *te, const char *parent_path,
5419 struct got_pathlist_head *commitable_paths,
5420 got_worktree_status_cb status_cb, void *status_arg,
5421 struct got_repository *repo)
5423 const struct got_error *err = NULL;
5424 struct got_tree_object *subtree;
5425 char *subpath;
5427 if (asprintf(&subpath, "%s%s%s", parent_path,
5428 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5429 return got_error_from_errno("asprintf");
5431 err = got_object_open_as_tree(&subtree, repo, &te->id);
5432 if (err)
5433 return err;
5435 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5436 commitable_paths, status_cb, status_arg, repo);
5437 got_object_tree_close(subtree);
5438 free(subpath);
5439 return err;
5442 static const struct got_error *
5443 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5445 const struct got_error *err = NULL;
5446 char *ct_parent_path = NULL;
5448 *match = 0;
5450 if (strchr(ct->in_repo_path, '/') == NULL) {
5451 *match = got_path_is_root_dir(path);
5452 return NULL;
5455 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5456 if (err)
5457 return err;
5458 *match = (strcmp(path, ct_parent_path) == 0);
5459 free(ct_parent_path);
5460 return err;
5463 static mode_t
5464 get_ct_file_mode(struct got_commitable *ct)
5466 if (S_ISLNK(ct->mode))
5467 return S_IFLNK;
5469 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5472 static const struct got_error *
5473 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5474 struct got_tree_entry *te, struct got_commitable *ct)
5476 const struct got_error *err = NULL;
5478 *new_te = NULL;
5480 err = got_object_tree_entry_dup(new_te, te);
5481 if (err)
5482 goto done;
5484 (*new_te)->mode = get_ct_file_mode(ct);
5486 if (ct->staged_status == GOT_STATUS_MODIFY)
5487 memcpy(&(*new_te)->id, ct->staged_blob_id,
5488 sizeof((*new_te)->id));
5489 else
5490 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5491 done:
5492 if (err && *new_te) {
5493 free(*new_te);
5494 *new_te = NULL;
5496 return err;
5499 static const struct got_error *
5500 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5501 struct got_commitable *ct)
5503 const struct got_error *err = NULL;
5504 char *ct_name = NULL;
5506 *new_te = NULL;
5508 *new_te = calloc(1, sizeof(**new_te));
5509 if (*new_te == NULL)
5510 return got_error_from_errno("calloc");
5512 err = got_path_basename(&ct_name, ct->path);
5513 if (err)
5514 goto done;
5515 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5516 sizeof((*new_te)->name)) {
5517 err = got_error(GOT_ERR_NO_SPACE);
5518 goto done;
5521 (*new_te)->mode = get_ct_file_mode(ct);
5523 if (ct->staged_status == GOT_STATUS_ADD)
5524 memcpy(&(*new_te)->id, ct->staged_blob_id,
5525 sizeof((*new_te)->id));
5526 else
5527 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5528 done:
5529 free(ct_name);
5530 if (err && *new_te) {
5531 free(*new_te);
5532 *new_te = NULL;
5534 return err;
5537 static const struct got_error *
5538 insert_tree_entry(struct got_tree_entry *new_te,
5539 struct got_pathlist_head *paths)
5541 const struct got_error *err = NULL;
5542 struct got_pathlist_entry *new_pe;
5544 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5545 if (err)
5546 return err;
5547 if (new_pe == NULL)
5548 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5549 return NULL;
5552 static const struct got_error *
5553 report_ct_status(struct got_commitable *ct,
5554 got_worktree_status_cb status_cb, void *status_arg)
5556 const char *ct_path = ct->path;
5557 unsigned char status;
5559 if (status_cb == NULL) /* no commit progress output desired */
5560 return NULL;
5562 while (ct_path[0] == '/')
5563 ct_path++;
5565 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5566 status = ct->staged_status;
5567 else
5568 status = ct->status;
5570 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5571 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5574 static const struct got_error *
5575 match_modified_subtree(int *modified, struct got_tree_entry *te,
5576 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5578 const struct got_error *err = NULL;
5579 struct got_pathlist_entry *pe;
5580 char *te_path;
5582 *modified = 0;
5584 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5585 got_path_is_root_dir(base_tree_path) ? "" : "/",
5586 te->name) == -1)
5587 return got_error_from_errno("asprintf");
5589 TAILQ_FOREACH(pe, commitable_paths, entry) {
5590 struct got_commitable *ct = pe->data;
5591 *modified = got_path_is_child(ct->in_repo_path, te_path,
5592 strlen(te_path));
5593 if (*modified)
5594 break;
5597 free(te_path);
5598 return err;
5601 static const struct got_error *
5602 match_deleted_or_modified_ct(struct got_commitable **ctp,
5603 struct got_tree_entry *te, const char *base_tree_path,
5604 struct got_pathlist_head *commitable_paths)
5606 const struct got_error *err = NULL;
5607 struct got_pathlist_entry *pe;
5609 *ctp = NULL;
5611 TAILQ_FOREACH(pe, commitable_paths, entry) {
5612 struct got_commitable *ct = pe->data;
5613 char *ct_name = NULL;
5614 int path_matches;
5616 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5617 if (ct->status != GOT_STATUS_MODIFY &&
5618 ct->status != GOT_STATUS_MODE_CHANGE &&
5619 ct->status != GOT_STATUS_DELETE &&
5620 ct->status != GOT_STATUS_CONFLICT)
5621 continue;
5622 } else {
5623 if (ct->staged_status != GOT_STATUS_MODIFY &&
5624 ct->staged_status != GOT_STATUS_DELETE)
5625 continue;
5628 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5629 continue;
5631 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5632 if (err)
5633 return err;
5634 if (!path_matches)
5635 continue;
5637 err = got_path_basename(&ct_name, pe->path);
5638 if (err)
5639 return err;
5641 if (strcmp(te->name, ct_name) != 0) {
5642 free(ct_name);
5643 continue;
5645 free(ct_name);
5647 *ctp = ct;
5648 break;
5651 return err;
5654 static const struct got_error *
5655 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5656 const char *child_path, const char *path_base_tree,
5657 struct got_pathlist_head *commitable_paths,
5658 got_worktree_status_cb status_cb, void *status_arg,
5659 struct got_repository *repo)
5661 const struct got_error *err = NULL;
5662 struct got_tree_entry *new_te;
5663 char *subtree_path;
5664 struct got_object_id *id = NULL;
5665 int nentries;
5667 *new_tep = NULL;
5669 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5670 got_path_is_root_dir(path_base_tree) ? "" : "/",
5671 child_path) == -1)
5672 return got_error_from_errno("asprintf");
5674 new_te = calloc(1, sizeof(*new_te));
5675 if (new_te == NULL)
5676 return got_error_from_errno("calloc");
5677 new_te->mode = S_IFDIR;
5679 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5680 sizeof(new_te->name)) {
5681 err = got_error(GOT_ERR_NO_SPACE);
5682 goto done;
5684 err = write_tree(&id, &nentries, NULL, subtree_path,
5685 commitable_paths, status_cb, status_arg, repo);
5686 if (err) {
5687 free(new_te);
5688 goto done;
5690 memcpy(&new_te->id, id, sizeof(new_te->id));
5691 done:
5692 free(id);
5693 free(subtree_path);
5694 if (err == NULL)
5695 *new_tep = new_te;
5696 return err;
5699 static const struct got_error *
5700 write_tree(struct got_object_id **new_tree_id, int *nentries,
5701 struct got_tree_object *base_tree, const char *path_base_tree,
5702 struct got_pathlist_head *commitable_paths,
5703 got_worktree_status_cb status_cb, void *status_arg,
5704 struct got_repository *repo)
5706 const struct got_error *err = NULL;
5707 struct got_pathlist_head paths;
5708 struct got_tree_entry *te, *new_te = NULL;
5709 struct got_pathlist_entry *pe;
5711 TAILQ_INIT(&paths);
5712 *nentries = 0;
5714 /* Insert, and recurse into, newly added entries first. */
5715 TAILQ_FOREACH(pe, commitable_paths, entry) {
5716 struct got_commitable *ct = pe->data;
5717 char *child_path = NULL, *slash;
5719 if ((ct->status != GOT_STATUS_ADD &&
5720 ct->staged_status != GOT_STATUS_ADD) ||
5721 (ct->flags & GOT_COMMITABLE_ADDED))
5722 continue;
5724 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5725 strlen(path_base_tree)))
5726 continue;
5728 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5729 ct->in_repo_path);
5730 if (err)
5731 goto done;
5733 slash = strchr(child_path, '/');
5734 if (slash == NULL) {
5735 err = alloc_added_blob_tree_entry(&new_te, ct);
5736 if (err)
5737 goto done;
5738 err = report_ct_status(ct, status_cb, status_arg);
5739 if (err)
5740 goto done;
5741 ct->flags |= GOT_COMMITABLE_ADDED;
5742 err = insert_tree_entry(new_te, &paths);
5743 if (err)
5744 goto done;
5745 (*nentries)++;
5746 } else {
5747 *slash = '\0'; /* trim trailing path components */
5748 if (base_tree == NULL ||
5749 got_object_tree_find_entry(base_tree, child_path)
5750 == NULL) {
5751 err = make_subtree_for_added_blob(&new_te,
5752 child_path, path_base_tree,
5753 commitable_paths, status_cb, status_arg,
5754 repo);
5755 if (err)
5756 goto done;
5757 err = insert_tree_entry(new_te, &paths);
5758 if (err)
5759 goto done;
5760 (*nentries)++;
5765 if (base_tree) {
5766 int i, nbase_entries;
5767 /* Handle modified and deleted entries. */
5768 nbase_entries = got_object_tree_get_nentries(base_tree);
5769 for (i = 0; i < nbase_entries; i++) {
5770 struct got_commitable *ct = NULL;
5772 te = got_object_tree_get_entry(base_tree, i);
5773 if (got_object_tree_entry_is_submodule(te)) {
5774 /* Entry is a submodule; just copy it. */
5775 err = got_object_tree_entry_dup(&new_te, te);
5776 if (err)
5777 goto done;
5778 err = insert_tree_entry(new_te, &paths);
5779 if (err)
5780 goto done;
5781 (*nentries)++;
5782 continue;
5785 if (S_ISDIR(te->mode)) {
5786 int modified;
5787 err = got_object_tree_entry_dup(&new_te, te);
5788 if (err)
5789 goto done;
5790 err = match_modified_subtree(&modified, te,
5791 path_base_tree, commitable_paths);
5792 if (err)
5793 goto done;
5794 /* Avoid recursion into unmodified subtrees. */
5795 if (modified) {
5796 struct got_object_id *new_id;
5797 int nsubentries;
5798 err = write_subtree(&new_id,
5799 &nsubentries, te,
5800 path_base_tree, commitable_paths,
5801 status_cb, status_arg, repo);
5802 if (err)
5803 goto done;
5804 if (nsubentries == 0) {
5805 /* All entries were deleted. */
5806 free(new_id);
5807 continue;
5809 memcpy(&new_te->id, new_id,
5810 sizeof(new_te->id));
5811 free(new_id);
5813 err = insert_tree_entry(new_te, &paths);
5814 if (err)
5815 goto done;
5816 (*nentries)++;
5817 continue;
5820 err = match_deleted_or_modified_ct(&ct, te,
5821 path_base_tree, commitable_paths);
5822 if (err)
5823 goto done;
5824 if (ct) {
5825 /* NB: Deleted entries get dropped here. */
5826 if (ct->status == GOT_STATUS_MODIFY ||
5827 ct->status == GOT_STATUS_MODE_CHANGE ||
5828 ct->status == GOT_STATUS_CONFLICT ||
5829 ct->staged_status == GOT_STATUS_MODIFY) {
5830 err = alloc_modified_blob_tree_entry(
5831 &new_te, te, ct);
5832 if (err)
5833 goto done;
5834 err = insert_tree_entry(new_te, &paths);
5835 if (err)
5836 goto done;
5837 (*nentries)++;
5839 err = report_ct_status(ct, status_cb,
5840 status_arg);
5841 if (err)
5842 goto done;
5843 } else {
5844 /* Entry is unchanged; just copy it. */
5845 err = got_object_tree_entry_dup(&new_te, te);
5846 if (err)
5847 goto done;
5848 err = insert_tree_entry(new_te, &paths);
5849 if (err)
5850 goto done;
5851 (*nentries)++;
5856 /* Write new list of entries; deleted entries have been dropped. */
5857 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5858 done:
5859 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5860 return err;
5863 static const struct got_error *
5864 update_fileindex_after_commit(struct got_worktree *worktree,
5865 struct got_pathlist_head *commitable_paths,
5866 struct got_object_id *new_base_commit_id,
5867 struct got_fileindex *fileindex, int have_staged_files)
5869 const struct got_error *err = NULL;
5870 struct got_pathlist_entry *pe;
5871 char *relpath = NULL;
5873 TAILQ_FOREACH(pe, commitable_paths, entry) {
5874 struct got_fileindex_entry *ie;
5875 struct got_commitable *ct = pe->data;
5877 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5879 err = got_path_skip_common_ancestor(&relpath,
5880 worktree->root_path, ct->ondisk_path);
5881 if (err)
5882 goto done;
5884 if (ie) {
5885 if (ct->status == GOT_STATUS_DELETE ||
5886 ct->staged_status == GOT_STATUS_DELETE) {
5887 got_fileindex_entry_remove(fileindex, ie);
5888 } else if (ct->staged_status == GOT_STATUS_ADD ||
5889 ct->staged_status == GOT_STATUS_MODIFY) {
5890 got_fileindex_entry_stage_set(ie,
5891 GOT_FILEIDX_STAGE_NONE);
5892 got_fileindex_entry_staged_filetype_set(ie, 0);
5894 err = got_fileindex_entry_update(ie,
5895 worktree->root_fd, relpath,
5896 ct->staged_blob_id->sha1,
5897 new_base_commit_id->sha1,
5898 !have_staged_files);
5899 } else
5900 err = got_fileindex_entry_update(ie,
5901 worktree->root_fd, relpath,
5902 ct->blob_id->sha1,
5903 new_base_commit_id->sha1,
5904 !have_staged_files);
5905 } else {
5906 err = got_fileindex_entry_alloc(&ie, pe->path);
5907 if (err)
5908 goto done;
5909 err = got_fileindex_entry_update(ie,
5910 worktree->root_fd, relpath, ct->blob_id->sha1,
5911 new_base_commit_id->sha1, 1);
5912 if (err) {
5913 got_fileindex_entry_free(ie);
5914 goto done;
5916 err = got_fileindex_entry_add(fileindex, ie);
5917 if (err) {
5918 got_fileindex_entry_free(ie);
5919 goto done;
5922 free(relpath);
5923 relpath = NULL;
5925 done:
5926 free(relpath);
5927 return err;
5931 static const struct got_error *
5932 check_out_of_date(const char *in_repo_path, unsigned char status,
5933 unsigned char staged_status, struct got_object_id *base_blob_id,
5934 struct got_object_id *base_commit_id,
5935 struct got_object_id *head_commit_id, struct got_repository *repo,
5936 int ood_errcode)
5938 const struct got_error *err = NULL;
5939 struct got_commit_object *commit = NULL;
5940 struct got_object_id *id = NULL;
5942 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5943 /* Trivial case: base commit == head commit */
5944 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5945 return NULL;
5947 * Ensure file content which local changes were based
5948 * on matches file content in the branch head.
5950 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5951 if (err)
5952 goto done;
5953 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5954 if (err) {
5955 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5956 err = got_error(ood_errcode);
5957 goto done;
5958 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5959 err = got_error(ood_errcode);
5960 } else {
5961 /* Require that added files don't exist in the branch head. */
5962 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5963 if (err)
5964 goto done;
5965 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5966 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5967 goto done;
5968 err = id ? got_error(ood_errcode) : NULL;
5970 done:
5971 free(id);
5972 if (commit)
5973 got_object_commit_close(commit);
5974 return err;
5977 static const struct got_error *
5978 commit_worktree(struct got_object_id **new_commit_id,
5979 struct got_pathlist_head *commitable_paths,
5980 struct got_object_id *head_commit_id,
5981 struct got_object_id *parent_id2,
5982 struct got_worktree *worktree,
5983 const char *author, const char *committer, char *diff_path,
5984 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5985 got_worktree_status_cb status_cb, void *status_arg,
5986 struct got_repository *repo)
5988 const struct got_error *err = NULL, *unlockerr = NULL;
5989 struct got_pathlist_entry *pe;
5990 const char *head_ref_name = NULL;
5991 struct got_commit_object *head_commit = NULL;
5992 struct got_reference *head_ref2 = NULL;
5993 struct got_object_id *head_commit_id2 = NULL;
5994 struct got_tree_object *head_tree = NULL;
5995 struct got_object_id *new_tree_id = NULL;
5996 int nentries, nparents = 0;
5997 struct got_object_id_queue parent_ids;
5998 struct got_object_qid *pid = NULL;
5999 char *logmsg = NULL;
6000 time_t timestamp;
6002 *new_commit_id = NULL;
6004 STAILQ_INIT(&parent_ids);
6006 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6007 if (err)
6008 goto done;
6010 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6011 if (err)
6012 goto done;
6014 if (commit_msg_cb != NULL) {
6015 err = commit_msg_cb(commitable_paths, diff_path,
6016 &logmsg, commit_arg);
6017 if (err)
6018 goto done;
6021 if (logmsg == NULL || strlen(logmsg) == 0) {
6022 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6023 goto done;
6026 /* Create blobs from added and modified files and record their IDs. */
6027 TAILQ_FOREACH(pe, commitable_paths, entry) {
6028 struct got_commitable *ct = pe->data;
6029 char *ondisk_path;
6031 /* Blobs for staged files already exist. */
6032 if (ct->staged_status == GOT_STATUS_ADD ||
6033 ct->staged_status == GOT_STATUS_MODIFY)
6034 continue;
6036 if (ct->status != GOT_STATUS_ADD &&
6037 ct->status != GOT_STATUS_MODIFY &&
6038 ct->status != GOT_STATUS_MODE_CHANGE &&
6039 ct->status != GOT_STATUS_CONFLICT)
6040 continue;
6042 if (asprintf(&ondisk_path, "%s/%s",
6043 worktree->root_path, pe->path) == -1) {
6044 err = got_error_from_errno("asprintf");
6045 goto done;
6047 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6048 free(ondisk_path);
6049 if (err)
6050 goto done;
6053 /* Recursively write new tree objects. */
6054 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6055 commitable_paths, status_cb, status_arg, repo);
6056 if (err)
6057 goto done;
6059 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6060 if (err)
6061 goto done;
6062 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6063 nparents++;
6064 if (parent_id2) {
6065 err = got_object_qid_alloc(&pid, parent_id2);
6066 if (err)
6067 goto done;
6068 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6069 nparents++;
6071 timestamp = time(NULL);
6072 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6073 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6074 if (logmsg != NULL)
6075 free(logmsg);
6076 if (err)
6077 goto done;
6079 /* Check if a concurrent commit to our branch has occurred. */
6080 head_ref_name = got_worktree_get_head_ref_name(worktree);
6081 if (head_ref_name == NULL) {
6082 err = got_error_from_errno("got_worktree_get_head_ref_name");
6083 goto done;
6085 /* Lock the reference here to prevent concurrent modification. */
6086 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6087 if (err)
6088 goto done;
6089 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6090 if (err)
6091 goto done;
6092 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6093 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6094 goto done;
6096 /* Update branch head in repository. */
6097 err = got_ref_change_ref(head_ref2, *new_commit_id);
6098 if (err)
6099 goto done;
6100 err = got_ref_write(head_ref2, repo);
6101 if (err)
6102 goto done;
6104 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6105 if (err)
6106 goto done;
6108 err = ref_base_commit(worktree, repo);
6109 if (err)
6110 goto done;
6111 done:
6112 got_object_id_queue_free(&parent_ids);
6113 if (head_tree)
6114 got_object_tree_close(head_tree);
6115 if (head_commit)
6116 got_object_commit_close(head_commit);
6117 free(head_commit_id2);
6118 if (head_ref2) {
6119 unlockerr = got_ref_unlock(head_ref2);
6120 if (unlockerr && err == NULL)
6121 err = unlockerr;
6122 got_ref_close(head_ref2);
6124 return err;
6127 static const struct got_error *
6128 check_path_is_commitable(const char *path,
6129 struct got_pathlist_head *commitable_paths)
6131 struct got_pathlist_entry *cpe = NULL;
6132 size_t path_len = strlen(path);
6134 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6135 struct got_commitable *ct = cpe->data;
6136 const char *ct_path = ct->path;
6138 while (ct_path[0] == '/')
6139 ct_path++;
6141 if (strcmp(path, ct_path) == 0 ||
6142 got_path_is_child(ct_path, path, path_len))
6143 break;
6146 if (cpe == NULL)
6147 return got_error_path(path, GOT_ERR_BAD_PATH);
6149 return NULL;
6152 static const struct got_error *
6153 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6155 int *have_staged_files = arg;
6157 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6158 *have_staged_files = 1;
6159 return got_error(GOT_ERR_CANCELLED);
6162 return NULL;
6165 static const struct got_error *
6166 check_non_staged_files(struct got_fileindex *fileindex,
6167 struct got_pathlist_head *paths)
6169 struct got_pathlist_entry *pe;
6170 struct got_fileindex_entry *ie;
6172 TAILQ_FOREACH(pe, paths, entry) {
6173 if (pe->path[0] == '\0')
6174 continue;
6175 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6176 if (ie == NULL)
6177 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6178 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6179 return got_error_path(pe->path,
6180 GOT_ERR_FILE_NOT_STAGED);
6183 return NULL;
6186 const struct got_error *
6187 got_worktree_commit(struct got_object_id **new_commit_id,
6188 struct got_worktree *worktree, struct got_pathlist_head *paths,
6189 const char *author, const char *committer, int allow_bad_symlinks,
6190 int show_diff, int commit_conflicts,
6191 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6192 got_worktree_status_cb status_cb, void *status_arg,
6193 struct got_repository *repo)
6195 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6196 struct got_fileindex *fileindex = NULL;
6197 char *fileindex_path = NULL;
6198 struct got_pathlist_head commitable_paths;
6199 struct collect_commitables_arg cc_arg;
6200 struct got_pathlist_entry *pe;
6201 struct got_reference *head_ref = NULL;
6202 struct got_object_id *head_commit_id = NULL;
6203 char *diff_path = NULL;
6204 int have_staged_files = 0;
6206 *new_commit_id = NULL;
6208 memset(&cc_arg, 0, sizeof(cc_arg));
6209 TAILQ_INIT(&commitable_paths);
6211 err = lock_worktree(worktree, LOCK_EX);
6212 if (err)
6213 goto done;
6215 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6216 if (err)
6217 goto done;
6219 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6220 if (err)
6221 goto done;
6223 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6224 if (err)
6225 goto done;
6227 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6228 &have_staged_files);
6229 if (err && err->code != GOT_ERR_CANCELLED)
6230 goto done;
6231 if (have_staged_files) {
6232 err = check_non_staged_files(fileindex, paths);
6233 if (err)
6234 goto done;
6237 cc_arg.commitable_paths = &commitable_paths;
6238 cc_arg.worktree = worktree;
6239 cc_arg.fileindex = fileindex;
6240 cc_arg.repo = repo;
6241 cc_arg.have_staged_files = have_staged_files;
6242 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6243 cc_arg.diff_header_shown = 0;
6244 cc_arg.commit_conflicts = commit_conflicts;
6245 if (show_diff) {
6246 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6247 GOT_TMPDIR_STR "/got", ".diff");
6248 if (err)
6249 goto done;
6250 cc_arg.f1 = got_opentemp();
6251 if (cc_arg.f1 == NULL) {
6252 err = got_error_from_errno("got_opentemp");
6253 goto done;
6255 cc_arg.f2 = got_opentemp();
6256 if (cc_arg.f2 == NULL) {
6257 err = got_error_from_errno("got_opentemp");
6258 goto done;
6262 TAILQ_FOREACH(pe, paths, entry) {
6263 err = worktree_status(worktree, pe->path, fileindex, repo,
6264 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6265 if (err)
6266 goto done;
6269 if (show_diff) {
6270 if (fflush(cc_arg.diff_outfile) == EOF) {
6271 err = got_error_from_errno("fflush");
6272 goto done;
6276 if (TAILQ_EMPTY(&commitable_paths)) {
6277 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6278 goto done;
6281 TAILQ_FOREACH(pe, paths, entry) {
6282 err = check_path_is_commitable(pe->path, &commitable_paths);
6283 if (err)
6284 goto done;
6287 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6288 struct got_commitable *ct = pe->data;
6289 const char *ct_path = ct->in_repo_path;
6291 while (ct_path[0] == '/')
6292 ct_path++;
6293 err = check_out_of_date(ct_path, ct->status,
6294 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6295 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6296 if (err)
6297 goto done;
6301 err = commit_worktree(new_commit_id, &commitable_paths,
6302 head_commit_id, NULL, worktree, author, committer,
6303 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6304 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6305 if (err)
6306 goto done;
6308 err = update_fileindex_after_commit(worktree, &commitable_paths,
6309 *new_commit_id, fileindex, have_staged_files);
6310 sync_err = sync_fileindex(fileindex, fileindex_path);
6311 if (sync_err && err == NULL)
6312 err = sync_err;
6313 done:
6314 if (fileindex)
6315 got_fileindex_free(fileindex);
6316 free(fileindex_path);
6317 unlockerr = lock_worktree(worktree, LOCK_SH);
6318 if (unlockerr && err == NULL)
6319 err = unlockerr;
6320 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6321 struct got_commitable *ct = pe->data;
6323 free_commitable(ct);
6325 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6326 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6327 err = got_error_from_errno2("unlink", diff_path);
6328 free(diff_path);
6329 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6330 err == NULL)
6331 err = got_error_from_errno("fclose");
6332 return err;
6335 const char *
6336 got_commitable_get_path(struct got_commitable *ct)
6338 return ct->path;
6341 unsigned int
6342 got_commitable_get_status(struct got_commitable *ct)
6344 return ct->status;
6347 struct check_rebase_ok_arg {
6348 struct got_worktree *worktree;
6349 struct got_repository *repo;
6352 static const struct got_error *
6353 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6355 const struct got_error *err = NULL;
6356 struct check_rebase_ok_arg *a = arg;
6357 unsigned char status;
6358 struct stat sb;
6359 char *ondisk_path;
6361 /* Reject rebase of a work tree with mixed base commits. */
6362 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6363 SHA1_DIGEST_LENGTH))
6364 return got_error(GOT_ERR_MIXED_COMMITS);
6366 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6367 == -1)
6368 return got_error_from_errno("asprintf");
6370 /* Reject rebase of a work tree with modified or staged files. */
6371 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6372 free(ondisk_path);
6373 if (err)
6374 return err;
6376 if (status != GOT_STATUS_NO_CHANGE)
6377 return got_error(GOT_ERR_MODIFIED);
6378 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6379 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6381 return NULL;
6384 const struct got_error *
6385 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6386 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6387 struct got_worktree *worktree, struct got_reference *branch,
6388 struct got_repository *repo)
6390 const struct got_error *err = NULL;
6391 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6392 char *branch_ref_name = NULL;
6393 char *fileindex_path = NULL;
6394 struct check_rebase_ok_arg ok_arg;
6395 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6396 struct got_object_id *wt_branch_tip = NULL;
6398 *new_base_branch_ref = NULL;
6399 *tmp_branch = NULL;
6400 *fileindex = NULL;
6402 err = lock_worktree(worktree, LOCK_EX);
6403 if (err)
6404 return err;
6406 err = open_fileindex(fileindex, &fileindex_path, worktree);
6407 if (err)
6408 goto done;
6410 ok_arg.worktree = worktree;
6411 ok_arg.repo = repo;
6412 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6413 &ok_arg);
6414 if (err)
6415 goto done;
6417 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6418 if (err)
6419 goto done;
6421 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6422 if (err)
6423 goto done;
6425 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6426 if (err)
6427 goto done;
6429 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6430 0);
6431 if (err)
6432 goto done;
6434 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6435 if (err)
6436 goto done;
6437 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6438 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6439 goto done;
6442 err = got_ref_alloc_symref(new_base_branch_ref,
6443 new_base_branch_ref_name, wt_branch);
6444 if (err)
6445 goto done;
6446 err = got_ref_write(*new_base_branch_ref, repo);
6447 if (err)
6448 goto done;
6450 /* TODO Lock original branch's ref while rebasing? */
6452 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6453 if (err)
6454 goto done;
6456 err = got_ref_write(branch_ref, repo);
6457 if (err)
6458 goto done;
6460 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6461 worktree->base_commit_id);
6462 if (err)
6463 goto done;
6464 err = got_ref_write(*tmp_branch, repo);
6465 if (err)
6466 goto done;
6468 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6469 if (err)
6470 goto done;
6471 done:
6472 free(fileindex_path);
6473 free(tmp_branch_name);
6474 free(new_base_branch_ref_name);
6475 free(branch_ref_name);
6476 if (branch_ref)
6477 got_ref_close(branch_ref);
6478 if (wt_branch)
6479 got_ref_close(wt_branch);
6480 free(wt_branch_tip);
6481 if (err) {
6482 if (*new_base_branch_ref) {
6483 got_ref_close(*new_base_branch_ref);
6484 *new_base_branch_ref = NULL;
6486 if (*tmp_branch) {
6487 got_ref_close(*tmp_branch);
6488 *tmp_branch = NULL;
6490 if (*fileindex) {
6491 got_fileindex_free(*fileindex);
6492 *fileindex = NULL;
6494 lock_worktree(worktree, LOCK_SH);
6496 return err;
6499 const struct got_error *
6500 got_worktree_rebase_continue(struct got_object_id **commit_id,
6501 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6502 struct got_reference **branch, struct got_fileindex **fileindex,
6503 struct got_worktree *worktree, struct got_repository *repo)
6505 const struct got_error *err;
6506 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6507 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6508 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6509 char *fileindex_path = NULL;
6510 int have_staged_files = 0;
6512 *commit_id = NULL;
6513 *new_base_branch = NULL;
6514 *tmp_branch = NULL;
6515 *branch = NULL;
6516 *fileindex = NULL;
6518 err = lock_worktree(worktree, LOCK_EX);
6519 if (err)
6520 return err;
6522 err = open_fileindex(fileindex, &fileindex_path, worktree);
6523 if (err)
6524 goto done;
6526 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6527 &have_staged_files);
6528 if (err && err->code != GOT_ERR_CANCELLED)
6529 goto done;
6530 if (have_staged_files) {
6531 err = got_error(GOT_ERR_STAGED_PATHS);
6532 goto done;
6535 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6536 if (err)
6537 goto done;
6539 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6540 if (err)
6541 goto done;
6543 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6544 if (err)
6545 goto done;
6547 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6548 if (err)
6549 goto done;
6551 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6552 if (err)
6553 goto done;
6555 err = got_ref_open(branch, repo,
6556 got_ref_get_symref_target(branch_ref), 0);
6557 if (err)
6558 goto done;
6560 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6561 if (err)
6562 goto done;
6564 err = got_ref_resolve(commit_id, repo, commit_ref);
6565 if (err)
6566 goto done;
6568 err = got_ref_open(new_base_branch, repo,
6569 new_base_branch_ref_name, 0);
6570 if (err)
6571 goto done;
6573 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6574 if (err)
6575 goto done;
6576 done:
6577 free(commit_ref_name);
6578 free(branch_ref_name);
6579 free(fileindex_path);
6580 if (commit_ref)
6581 got_ref_close(commit_ref);
6582 if (branch_ref)
6583 got_ref_close(branch_ref);
6584 if (err) {
6585 free(*commit_id);
6586 *commit_id = NULL;
6587 if (*tmp_branch) {
6588 got_ref_close(*tmp_branch);
6589 *tmp_branch = NULL;
6591 if (*new_base_branch) {
6592 got_ref_close(*new_base_branch);
6593 *new_base_branch = NULL;
6595 if (*branch) {
6596 got_ref_close(*branch);
6597 *branch = NULL;
6599 if (*fileindex) {
6600 got_fileindex_free(*fileindex);
6601 *fileindex = NULL;
6603 lock_worktree(worktree, LOCK_SH);
6605 return err;
6608 const struct got_error *
6609 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6611 const struct got_error *err;
6612 char *tmp_branch_name = NULL;
6614 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6615 if (err)
6616 return err;
6618 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6619 free(tmp_branch_name);
6620 return NULL;
6623 static const struct got_error *
6624 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6625 const char *diff_path, char **logmsg, void *arg)
6627 *logmsg = arg;
6628 return NULL;
6631 static const struct got_error *
6632 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6633 const char *path, struct got_object_id *blob_id,
6634 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6635 int dirfd, const char *de_name)
6637 return NULL;
6640 struct collect_merged_paths_arg {
6641 got_worktree_checkout_cb progress_cb;
6642 void *progress_arg;
6643 struct got_pathlist_head *merged_paths;
6646 static const struct got_error *
6647 collect_merged_paths(void *arg, unsigned char status, const char *path)
6649 const struct got_error *err;
6650 struct collect_merged_paths_arg *a = arg;
6651 char *p;
6652 struct got_pathlist_entry *new;
6654 err = (*a->progress_cb)(a->progress_arg, status, path);
6655 if (err)
6656 return err;
6658 if (status != GOT_STATUS_MERGE &&
6659 status != GOT_STATUS_ADD &&
6660 status != GOT_STATUS_DELETE &&
6661 status != GOT_STATUS_CONFLICT)
6662 return NULL;
6664 p = strdup(path);
6665 if (p == NULL)
6666 return got_error_from_errno("strdup");
6668 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6669 if (err || new == NULL)
6670 free(p);
6671 return err;
6674 static const struct got_error *
6675 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6676 int is_rebase, struct got_repository *repo)
6678 const struct got_error *err;
6679 struct got_reference *commit_ref = NULL;
6681 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6682 if (err) {
6683 if (err->code != GOT_ERR_NOT_REF)
6684 goto done;
6685 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6686 if (err)
6687 goto done;
6688 err = got_ref_write(commit_ref, repo);
6689 if (err)
6690 goto done;
6691 } else if (is_rebase) {
6692 struct got_object_id *stored_id;
6693 int cmp;
6695 err = got_ref_resolve(&stored_id, repo, commit_ref);
6696 if (err)
6697 goto done;
6698 cmp = got_object_id_cmp(commit_id, stored_id);
6699 free(stored_id);
6700 if (cmp != 0) {
6701 err = got_error(GOT_ERR_REBASE_COMMITID);
6702 goto done;
6705 done:
6706 if (commit_ref)
6707 got_ref_close(commit_ref);
6708 return err;
6711 static const struct got_error *
6712 rebase_merge_files(struct got_pathlist_head *merged_paths,
6713 const char *commit_ref_name, struct got_worktree *worktree,
6714 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6715 struct got_object_id *commit_id, struct got_repository *repo,
6716 got_worktree_checkout_cb progress_cb, void *progress_arg,
6717 got_cancel_cb cancel_cb, void *cancel_arg)
6719 const struct got_error *err;
6720 struct got_reference *commit_ref = NULL;
6721 struct collect_merged_paths_arg cmp_arg;
6722 char *fileindex_path;
6724 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6726 err = get_fileindex_path(&fileindex_path, worktree);
6727 if (err)
6728 return err;
6730 cmp_arg.progress_cb = progress_cb;
6731 cmp_arg.progress_arg = progress_arg;
6732 cmp_arg.merged_paths = merged_paths;
6733 err = merge_files(worktree, fileindex, fileindex_path,
6734 parent_commit_id, commit_id, repo, collect_merged_paths,
6735 &cmp_arg, cancel_cb, cancel_arg);
6736 if (commit_ref)
6737 got_ref_close(commit_ref);
6738 return err;
6741 const struct got_error *
6742 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6743 struct got_worktree *worktree, struct got_fileindex *fileindex,
6744 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6745 struct got_repository *repo,
6746 got_worktree_checkout_cb progress_cb, void *progress_arg,
6747 got_cancel_cb cancel_cb, void *cancel_arg)
6749 const struct got_error *err;
6750 char *commit_ref_name;
6752 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6753 if (err)
6754 return err;
6756 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6757 if (err)
6758 goto done;
6760 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6761 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6762 progress_arg, cancel_cb, cancel_arg);
6763 done:
6764 free(commit_ref_name);
6765 return err;
6768 const struct got_error *
6769 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6770 struct got_worktree *worktree, struct got_fileindex *fileindex,
6771 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6772 struct got_repository *repo,
6773 got_worktree_checkout_cb progress_cb, void *progress_arg,
6774 got_cancel_cb cancel_cb, void *cancel_arg)
6776 const struct got_error *err;
6777 char *commit_ref_name;
6779 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6780 if (err)
6781 return err;
6783 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6784 if (err)
6785 goto done;
6787 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6788 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6789 progress_arg, cancel_cb, cancel_arg);
6790 done:
6791 free(commit_ref_name);
6792 return err;
6795 static const struct got_error *
6796 rebase_commit(struct got_object_id **new_commit_id,
6797 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6798 struct got_worktree *worktree, struct got_fileindex *fileindex,
6799 struct got_reference *tmp_branch, const char *committer,
6800 struct got_commit_object *orig_commit, const char *new_logmsg,
6801 int allow_conflict, struct got_repository *repo)
6803 const struct got_error *err, *sync_err;
6804 struct got_pathlist_head commitable_paths;
6805 struct collect_commitables_arg cc_arg;
6806 char *fileindex_path = NULL;
6807 struct got_reference *head_ref = NULL;
6808 struct got_object_id *head_commit_id = NULL;
6809 char *logmsg = NULL;
6811 memset(&cc_arg, 0, sizeof(cc_arg));
6812 TAILQ_INIT(&commitable_paths);
6813 *new_commit_id = NULL;
6815 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6817 err = get_fileindex_path(&fileindex_path, worktree);
6818 if (err)
6819 return err;
6821 cc_arg.commitable_paths = &commitable_paths;
6822 cc_arg.worktree = worktree;
6823 cc_arg.repo = repo;
6824 cc_arg.have_staged_files = 0;
6825 cc_arg.commit_conflicts = allow_conflict;
6827 * If possible get the status of individual files directly to
6828 * avoid crawling the entire work tree once per rebased commit.
6830 * Ideally, merged_paths would contain a list of commitables
6831 * we could use so we could skip worktree_status() entirely.
6832 * However, we would then need carefully keep track of cumulative
6833 * effects of operations such as file additions and deletions
6834 * in 'got histedit -f' (folding multiple commits into one),
6835 * and this extra complexity is not really worth it.
6837 if (merged_paths) {
6838 struct got_pathlist_entry *pe;
6839 TAILQ_FOREACH(pe, merged_paths, entry) {
6840 err = worktree_status(worktree, pe->path, fileindex,
6841 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6842 0);
6843 if (err)
6844 goto done;
6846 } else {
6847 err = worktree_status(worktree, "", fileindex, repo,
6848 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6849 if (err)
6850 goto done;
6853 if (TAILQ_EMPTY(&commitable_paths)) {
6854 /* No-op change; commit will be elided. */
6855 err = got_ref_delete(commit_ref, repo);
6856 if (err)
6857 goto done;
6858 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6859 goto done;
6862 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6863 if (err)
6864 goto done;
6866 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6867 if (err)
6868 goto done;
6870 if (new_logmsg) {
6871 logmsg = strdup(new_logmsg);
6872 if (logmsg == NULL) {
6873 err = got_error_from_errno("strdup");
6874 goto done;
6876 } else {
6877 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6878 if (err)
6879 goto done;
6882 /* NB: commit_worktree will call free(logmsg) */
6883 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6884 NULL, worktree, got_object_commit_get_author(orig_commit),
6885 committer ? committer :
6886 got_object_commit_get_committer(orig_commit), NULL,
6887 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6888 if (err)
6889 goto done;
6891 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6892 if (err)
6893 goto done;
6895 err = got_ref_delete(commit_ref, repo);
6896 if (err)
6897 goto done;
6899 err = update_fileindex_after_commit(worktree, &commitable_paths,
6900 *new_commit_id, fileindex, 0);
6901 sync_err = sync_fileindex(fileindex, fileindex_path);
6902 if (sync_err && err == NULL)
6903 err = sync_err;
6904 done:
6905 free(fileindex_path);
6906 free(head_commit_id);
6907 if (head_ref)
6908 got_ref_close(head_ref);
6909 if (err) {
6910 free(*new_commit_id);
6911 *new_commit_id = NULL;
6913 return err;
6916 const struct got_error *
6917 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6918 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6919 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6920 const char *committer, struct got_commit_object *orig_commit,
6921 struct got_object_id *orig_commit_id, int allow_conflict,
6922 struct got_repository *repo)
6924 const struct got_error *err;
6925 char *commit_ref_name;
6926 struct got_reference *commit_ref = NULL;
6927 struct got_object_id *commit_id = NULL;
6929 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6930 if (err)
6931 return err;
6933 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6934 if (err)
6935 goto done;
6936 err = got_ref_resolve(&commit_id, repo, commit_ref);
6937 if (err)
6938 goto done;
6939 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6940 err = got_error(GOT_ERR_REBASE_COMMITID);
6941 goto done;
6944 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6945 worktree, fileindex, tmp_branch, committer, orig_commit,
6946 NULL, allow_conflict, repo);
6947 done:
6948 if (commit_ref)
6949 got_ref_close(commit_ref);
6950 free(commit_ref_name);
6951 free(commit_id);
6952 return err;
6955 const struct got_error *
6956 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6957 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6958 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6959 const char *committer, struct got_commit_object *orig_commit,
6960 struct got_object_id *orig_commit_id, const char *new_logmsg,
6961 int allow_conflict, struct got_repository *repo)
6963 const struct got_error *err;
6964 char *commit_ref_name;
6965 struct got_reference *commit_ref = NULL;
6967 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6968 if (err)
6969 return err;
6971 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6972 if (err)
6973 goto done;
6975 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6976 worktree, fileindex, tmp_branch, committer, orig_commit,
6977 new_logmsg, allow_conflict, repo);
6978 done:
6979 if (commit_ref)
6980 got_ref_close(commit_ref);
6981 free(commit_ref_name);
6982 return err;
6985 const struct got_error *
6986 got_worktree_rebase_postpone(struct got_worktree *worktree,
6987 struct got_fileindex *fileindex)
6989 if (fileindex)
6990 got_fileindex_free(fileindex);
6991 return lock_worktree(worktree, LOCK_SH);
6994 static const struct got_error *
6995 delete_ref(const char *name, struct got_repository *repo)
6997 const struct got_error *err;
6998 struct got_reference *ref;
7000 err = got_ref_open(&ref, repo, name, 0);
7001 if (err) {
7002 if (err->code == GOT_ERR_NOT_REF)
7003 return NULL;
7004 return err;
7007 err = got_ref_delete(ref, repo);
7008 got_ref_close(ref);
7009 return err;
7012 static const struct got_error *
7013 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7015 const struct got_error *err;
7016 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7017 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7019 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7020 if (err)
7021 goto done;
7022 err = delete_ref(tmp_branch_name, repo);
7023 if (err)
7024 goto done;
7026 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7027 if (err)
7028 goto done;
7029 err = delete_ref(new_base_branch_ref_name, repo);
7030 if (err)
7031 goto done;
7033 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7034 if (err)
7035 goto done;
7036 err = delete_ref(branch_ref_name, repo);
7037 if (err)
7038 goto done;
7040 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7041 if (err)
7042 goto done;
7043 err = delete_ref(commit_ref_name, repo);
7044 if (err)
7045 goto done;
7047 done:
7048 free(tmp_branch_name);
7049 free(new_base_branch_ref_name);
7050 free(branch_ref_name);
7051 free(commit_ref_name);
7052 return err;
7055 static const struct got_error *
7056 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7057 struct got_object_id *new_commit_id, struct got_repository *repo)
7059 const struct got_error *err;
7060 struct got_reference *ref = NULL;
7061 struct got_object_id *old_commit_id = NULL;
7062 const char *branch_name = NULL;
7063 char *new_id_str = NULL;
7064 char *refname = NULL;
7066 branch_name = got_ref_get_name(branch);
7067 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7068 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7069 branch_name += 11;
7071 err = got_object_id_str(&new_id_str, new_commit_id);
7072 if (err)
7073 return err;
7075 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7076 new_id_str) == -1) {
7077 err = got_error_from_errno("asprintf");
7078 goto done;
7081 err = got_ref_resolve(&old_commit_id, repo, branch);
7082 if (err)
7083 goto done;
7085 err = got_ref_alloc(&ref, refname, old_commit_id);
7086 if (err)
7087 goto done;
7089 err = got_ref_write(ref, repo);
7090 done:
7091 free(new_id_str);
7092 free(refname);
7093 free(old_commit_id);
7094 if (ref)
7095 got_ref_close(ref);
7096 return err;
7099 const struct got_error *
7100 got_worktree_rebase_complete(struct got_worktree *worktree,
7101 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7102 struct got_reference *rebased_branch, struct got_repository *repo,
7103 int create_backup)
7105 const struct got_error *err, *unlockerr, *sync_err;
7106 struct got_object_id *new_head_commit_id = NULL;
7107 char *fileindex_path = NULL;
7109 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7110 if (err)
7111 return err;
7113 if (create_backup) {
7114 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7115 rebased_branch, new_head_commit_id, repo);
7116 if (err)
7117 goto done;
7120 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7121 if (err)
7122 goto done;
7124 err = got_ref_write(rebased_branch, repo);
7125 if (err)
7126 goto done;
7128 err = got_worktree_set_head_ref(worktree, rebased_branch);
7129 if (err)
7130 goto done;
7132 err = delete_rebase_refs(worktree, repo);
7133 if (err)
7134 goto done;
7136 err = get_fileindex_path(&fileindex_path, worktree);
7137 if (err)
7138 goto done;
7139 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7140 sync_err = sync_fileindex(fileindex, fileindex_path);
7141 if (sync_err && err == NULL)
7142 err = sync_err;
7143 done:
7144 got_fileindex_free(fileindex);
7145 free(fileindex_path);
7146 free(new_head_commit_id);
7147 unlockerr = lock_worktree(worktree, LOCK_SH);
7148 if (unlockerr && err == NULL)
7149 err = unlockerr;
7150 return err;
7153 const struct got_error *
7154 got_worktree_rebase_abort(struct got_worktree *worktree,
7155 struct got_fileindex *fileindex, struct got_repository *repo,
7156 struct got_reference *new_base_branch,
7157 got_worktree_checkout_cb progress_cb, void *progress_arg)
7159 const struct got_error *err, *unlockerr, *sync_err;
7160 struct got_reference *resolved = NULL;
7161 struct got_object_id *commit_id = NULL;
7162 struct got_commit_object *commit = NULL;
7163 char *fileindex_path = NULL;
7164 struct revert_file_args rfa;
7165 struct got_object_id *tree_id = NULL;
7167 err = lock_worktree(worktree, LOCK_EX);
7168 if (err)
7169 return err;
7171 err = got_object_open_as_commit(&commit, repo,
7172 worktree->base_commit_id);
7173 if (err)
7174 goto done;
7176 err = got_ref_open(&resolved, repo,
7177 got_ref_get_symref_target(new_base_branch), 0);
7178 if (err)
7179 goto done;
7181 err = got_worktree_set_head_ref(worktree, resolved);
7182 if (err)
7183 goto done;
7186 * XXX commits to the base branch could have happened while
7187 * we were busy rebasing; should we store the original commit ID
7188 * when rebase begins and read it back here?
7190 err = got_ref_resolve(&commit_id, repo, resolved);
7191 if (err)
7192 goto done;
7194 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7195 if (err)
7196 goto done;
7198 err = got_object_id_by_path(&tree_id, repo, commit,
7199 worktree->path_prefix);
7200 if (err)
7201 goto done;
7203 err = delete_rebase_refs(worktree, repo);
7204 if (err)
7205 goto done;
7207 err = get_fileindex_path(&fileindex_path, worktree);
7208 if (err)
7209 goto done;
7211 rfa.worktree = worktree;
7212 rfa.fileindex = fileindex;
7213 rfa.progress_cb = progress_cb;
7214 rfa.progress_arg = progress_arg;
7215 rfa.patch_cb = NULL;
7216 rfa.patch_arg = NULL;
7217 rfa.repo = repo;
7218 rfa.unlink_added_files = 0;
7219 err = worktree_status(worktree, "", fileindex, repo,
7220 revert_file, &rfa, NULL, NULL, 1, 0);
7221 if (err)
7222 goto sync;
7224 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7225 repo, progress_cb, progress_arg, NULL, NULL);
7226 sync:
7227 sync_err = sync_fileindex(fileindex, fileindex_path);
7228 if (sync_err && err == NULL)
7229 err = sync_err;
7230 done:
7231 got_ref_close(resolved);
7232 free(tree_id);
7233 free(commit_id);
7234 if (commit)
7235 got_object_commit_close(commit);
7236 if (fileindex)
7237 got_fileindex_free(fileindex);
7238 free(fileindex_path);
7240 unlockerr = lock_worktree(worktree, LOCK_SH);
7241 if (unlockerr && err == NULL)
7242 err = unlockerr;
7243 return err;
7246 const struct got_error *
7247 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7248 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7249 struct got_fileindex **fileindex, struct got_worktree *worktree,
7250 struct got_repository *repo)
7252 const struct got_error *err = NULL;
7253 char *tmp_branch_name = NULL;
7254 char *branch_ref_name = NULL;
7255 char *base_commit_ref_name = NULL;
7256 char *fileindex_path = NULL;
7257 struct check_rebase_ok_arg ok_arg;
7258 struct got_reference *wt_branch = NULL;
7259 struct got_reference *base_commit_ref = NULL;
7261 *tmp_branch = NULL;
7262 *branch_ref = NULL;
7263 *base_commit_id = NULL;
7264 *fileindex = NULL;
7266 err = lock_worktree(worktree, LOCK_EX);
7267 if (err)
7268 return err;
7270 err = open_fileindex(fileindex, &fileindex_path, worktree);
7271 if (err)
7272 goto done;
7274 ok_arg.worktree = worktree;
7275 ok_arg.repo = repo;
7276 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7277 &ok_arg);
7278 if (err)
7279 goto done;
7281 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7282 if (err)
7283 goto done;
7285 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7286 if (err)
7287 goto done;
7289 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7290 worktree);
7291 if (err)
7292 goto done;
7294 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7295 0);
7296 if (err)
7297 goto done;
7299 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7300 if (err)
7301 goto done;
7303 err = got_ref_write(*branch_ref, repo);
7304 if (err)
7305 goto done;
7307 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7308 worktree->base_commit_id);
7309 if (err)
7310 goto done;
7311 err = got_ref_write(base_commit_ref, repo);
7312 if (err)
7313 goto done;
7314 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7315 if (*base_commit_id == NULL) {
7316 err = got_error_from_errno("got_object_id_dup");
7317 goto done;
7320 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7321 worktree->base_commit_id);
7322 if (err)
7323 goto done;
7324 err = got_ref_write(*tmp_branch, repo);
7325 if (err)
7326 goto done;
7328 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7329 if (err)
7330 goto done;
7331 done:
7332 free(fileindex_path);
7333 free(tmp_branch_name);
7334 free(branch_ref_name);
7335 free(base_commit_ref_name);
7336 if (wt_branch)
7337 got_ref_close(wt_branch);
7338 if (err) {
7339 if (*branch_ref) {
7340 got_ref_close(*branch_ref);
7341 *branch_ref = NULL;
7343 if (*tmp_branch) {
7344 got_ref_close(*tmp_branch);
7345 *tmp_branch = NULL;
7347 free(*base_commit_id);
7348 if (*fileindex) {
7349 got_fileindex_free(*fileindex);
7350 *fileindex = NULL;
7352 lock_worktree(worktree, LOCK_SH);
7354 return err;
7357 const struct got_error *
7358 got_worktree_histedit_postpone(struct got_worktree *worktree,
7359 struct got_fileindex *fileindex)
7361 if (fileindex)
7362 got_fileindex_free(fileindex);
7363 return lock_worktree(worktree, LOCK_SH);
7366 const struct got_error *
7367 got_worktree_histedit_in_progress(int *in_progress,
7368 struct got_worktree *worktree)
7370 const struct got_error *err;
7371 char *tmp_branch_name = NULL;
7373 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7374 if (err)
7375 return err;
7377 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7378 free(tmp_branch_name);
7379 return NULL;
7382 const struct got_error *
7383 got_worktree_histedit_continue(struct got_object_id **commit_id,
7384 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7385 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7386 struct got_worktree *worktree, struct got_repository *repo)
7388 const struct got_error *err;
7389 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7390 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7391 struct got_reference *commit_ref = NULL;
7392 struct got_reference *base_commit_ref = NULL;
7393 char *fileindex_path = NULL;
7394 int have_staged_files = 0;
7396 *commit_id = NULL;
7397 *tmp_branch = NULL;
7398 *base_commit_id = NULL;
7399 *fileindex = NULL;
7401 err = lock_worktree(worktree, LOCK_EX);
7402 if (err)
7403 return err;
7405 err = open_fileindex(fileindex, &fileindex_path, worktree);
7406 if (err)
7407 goto done;
7409 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7410 &have_staged_files);
7411 if (err && err->code != GOT_ERR_CANCELLED)
7412 goto done;
7413 if (have_staged_files) {
7414 err = got_error(GOT_ERR_STAGED_PATHS);
7415 goto done;
7418 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7419 if (err)
7420 goto done;
7422 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7423 if (err)
7424 goto done;
7426 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7427 if (err)
7428 goto done;
7430 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7431 worktree);
7432 if (err)
7433 goto done;
7435 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7436 if (err)
7437 goto done;
7439 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7440 if (err)
7441 goto done;
7442 err = got_ref_resolve(commit_id, repo, commit_ref);
7443 if (err)
7444 goto done;
7446 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7447 if (err)
7448 goto done;
7449 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7450 if (err)
7451 goto done;
7453 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7454 if (err)
7455 goto done;
7456 done:
7457 free(commit_ref_name);
7458 free(branch_ref_name);
7459 free(fileindex_path);
7460 if (commit_ref)
7461 got_ref_close(commit_ref);
7462 if (base_commit_ref)
7463 got_ref_close(base_commit_ref);
7464 if (err) {
7465 free(*commit_id);
7466 *commit_id = NULL;
7467 free(*base_commit_id);
7468 *base_commit_id = NULL;
7469 if (*tmp_branch) {
7470 got_ref_close(*tmp_branch);
7471 *tmp_branch = NULL;
7473 if (*fileindex) {
7474 got_fileindex_free(*fileindex);
7475 *fileindex = NULL;
7477 lock_worktree(worktree, LOCK_EX);
7479 return err;
7482 static const struct got_error *
7483 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7485 const struct got_error *err;
7486 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7487 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7489 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7490 if (err)
7491 goto done;
7492 err = delete_ref(tmp_branch_name, repo);
7493 if (err)
7494 goto done;
7496 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7497 worktree);
7498 if (err)
7499 goto done;
7500 err = delete_ref(base_commit_ref_name, repo);
7501 if (err)
7502 goto done;
7504 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7505 if (err)
7506 goto done;
7507 err = delete_ref(branch_ref_name, repo);
7508 if (err)
7509 goto done;
7511 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7512 if (err)
7513 goto done;
7514 err = delete_ref(commit_ref_name, repo);
7515 if (err)
7516 goto done;
7517 done:
7518 free(tmp_branch_name);
7519 free(base_commit_ref_name);
7520 free(branch_ref_name);
7521 free(commit_ref_name);
7522 return err;
7525 const struct got_error *
7526 got_worktree_histedit_abort(struct got_worktree *worktree,
7527 struct got_fileindex *fileindex, struct got_repository *repo,
7528 struct got_reference *branch, struct got_object_id *base_commit_id,
7529 got_worktree_checkout_cb progress_cb, void *progress_arg)
7531 const struct got_error *err, *unlockerr, *sync_err;
7532 struct got_reference *resolved = NULL;
7533 char *fileindex_path = NULL;
7534 struct got_commit_object *commit = NULL;
7535 struct got_object_id *tree_id = NULL;
7536 struct revert_file_args rfa;
7538 err = lock_worktree(worktree, LOCK_EX);
7539 if (err)
7540 return err;
7542 err = got_object_open_as_commit(&commit, repo,
7543 worktree->base_commit_id);
7544 if (err)
7545 goto done;
7547 err = got_ref_open(&resolved, repo,
7548 got_ref_get_symref_target(branch), 0);
7549 if (err)
7550 goto done;
7552 err = got_worktree_set_head_ref(worktree, resolved);
7553 if (err)
7554 goto done;
7556 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7557 if (err)
7558 goto done;
7560 err = got_object_id_by_path(&tree_id, repo, commit,
7561 worktree->path_prefix);
7562 if (err)
7563 goto done;
7565 err = delete_histedit_refs(worktree, repo);
7566 if (err)
7567 goto done;
7569 err = get_fileindex_path(&fileindex_path, worktree);
7570 if (err)
7571 goto done;
7573 rfa.worktree = worktree;
7574 rfa.fileindex = fileindex;
7575 rfa.progress_cb = progress_cb;
7576 rfa.progress_arg = progress_arg;
7577 rfa.patch_cb = NULL;
7578 rfa.patch_arg = NULL;
7579 rfa.repo = repo;
7580 rfa.unlink_added_files = 0;
7581 err = worktree_status(worktree, "", fileindex, repo,
7582 revert_file, &rfa, NULL, NULL, 1, 0);
7583 if (err)
7584 goto sync;
7586 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7587 repo, progress_cb, progress_arg, NULL, NULL);
7588 sync:
7589 sync_err = sync_fileindex(fileindex, fileindex_path);
7590 if (sync_err && err == NULL)
7591 err = sync_err;
7592 done:
7593 got_ref_close(resolved);
7594 free(tree_id);
7595 free(fileindex_path);
7597 unlockerr = lock_worktree(worktree, LOCK_SH);
7598 if (unlockerr && err == NULL)
7599 err = unlockerr;
7600 return err;
7603 const struct got_error *
7604 got_worktree_histedit_complete(struct got_worktree *worktree,
7605 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7606 struct got_reference *edited_branch, struct got_repository *repo)
7608 const struct got_error *err, *unlockerr, *sync_err;
7609 struct got_object_id *new_head_commit_id = NULL;
7610 struct got_reference *resolved = NULL;
7611 char *fileindex_path = NULL;
7613 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7614 if (err)
7615 return err;
7617 err = got_ref_open(&resolved, repo,
7618 got_ref_get_symref_target(edited_branch), 0);
7619 if (err)
7620 goto done;
7622 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7623 resolved, new_head_commit_id, repo);
7624 if (err)
7625 goto done;
7627 err = got_ref_change_ref(resolved, new_head_commit_id);
7628 if (err)
7629 goto done;
7631 err = got_ref_write(resolved, repo);
7632 if (err)
7633 goto done;
7635 err = got_worktree_set_head_ref(worktree, resolved);
7636 if (err)
7637 goto done;
7639 err = delete_histedit_refs(worktree, repo);
7640 if (err)
7641 goto done;
7643 err = get_fileindex_path(&fileindex_path, worktree);
7644 if (err)
7645 goto done;
7646 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7647 sync_err = sync_fileindex(fileindex, fileindex_path);
7648 if (sync_err && err == NULL)
7649 err = sync_err;
7650 done:
7651 got_fileindex_free(fileindex);
7652 free(fileindex_path);
7653 free(new_head_commit_id);
7654 unlockerr = lock_worktree(worktree, LOCK_SH);
7655 if (unlockerr && err == NULL)
7656 err = unlockerr;
7657 return err;
7660 const struct got_error *
7661 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7662 struct got_object_id *commit_id, struct got_repository *repo)
7664 const struct got_error *err;
7665 char *commit_ref_name;
7667 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7668 if (err)
7669 return err;
7671 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7672 if (err)
7673 goto done;
7675 err = delete_ref(commit_ref_name, repo);
7676 done:
7677 free(commit_ref_name);
7678 return err;
7681 const struct got_error *
7682 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7683 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7684 struct got_worktree *worktree, const char *refname,
7685 struct got_repository *repo)
7687 const struct got_error *err = NULL;
7688 char *fileindex_path = NULL;
7689 struct check_rebase_ok_arg ok_arg;
7691 *fileindex = NULL;
7692 *branch_ref = NULL;
7693 *base_branch_ref = NULL;
7695 err = lock_worktree(worktree, LOCK_EX);
7696 if (err)
7697 return err;
7699 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7700 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7701 "cannot integrate a branch into itself; "
7702 "update -b or different branch name required");
7703 goto done;
7706 err = open_fileindex(fileindex, &fileindex_path, worktree);
7707 if (err)
7708 goto done;
7710 /* Preconditions are the same as for rebase. */
7711 ok_arg.worktree = worktree;
7712 ok_arg.repo = repo;
7713 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7714 &ok_arg);
7715 if (err)
7716 goto done;
7718 err = got_ref_open(branch_ref, repo, refname, 1);
7719 if (err)
7720 goto done;
7722 err = got_ref_open(base_branch_ref, repo,
7723 got_worktree_get_head_ref_name(worktree), 1);
7724 done:
7725 if (err) {
7726 if (*branch_ref) {
7727 got_ref_close(*branch_ref);
7728 *branch_ref = NULL;
7730 if (*base_branch_ref) {
7731 got_ref_close(*base_branch_ref);
7732 *base_branch_ref = NULL;
7734 if (*fileindex) {
7735 got_fileindex_free(*fileindex);
7736 *fileindex = NULL;
7738 lock_worktree(worktree, LOCK_SH);
7740 return err;
7743 const struct got_error *
7744 got_worktree_integrate_continue(struct got_worktree *worktree,
7745 struct got_fileindex *fileindex, struct got_repository *repo,
7746 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7747 got_worktree_checkout_cb progress_cb, void *progress_arg,
7748 got_cancel_cb cancel_cb, void *cancel_arg)
7750 const struct got_error *err = NULL, *sync_err, *unlockerr;
7751 char *fileindex_path = NULL;
7752 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7753 struct got_commit_object *commit = NULL;
7755 err = get_fileindex_path(&fileindex_path, worktree);
7756 if (err)
7757 goto done;
7759 err = got_ref_resolve(&commit_id, repo, branch_ref);
7760 if (err)
7761 goto done;
7763 err = got_object_open_as_commit(&commit, repo, commit_id);
7764 if (err)
7765 goto done;
7767 err = got_object_id_by_path(&tree_id, repo, commit,
7768 worktree->path_prefix);
7769 if (err)
7770 goto done;
7772 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7773 if (err)
7774 goto done;
7776 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7777 progress_cb, progress_arg, cancel_cb, cancel_arg);
7778 if (err)
7779 goto sync;
7781 err = got_ref_change_ref(base_branch_ref, commit_id);
7782 if (err)
7783 goto sync;
7785 err = got_ref_write(base_branch_ref, repo);
7786 if (err)
7787 goto sync;
7789 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7790 sync:
7791 sync_err = sync_fileindex(fileindex, fileindex_path);
7792 if (sync_err && err == NULL)
7793 err = sync_err;
7795 done:
7796 unlockerr = got_ref_unlock(branch_ref);
7797 if (unlockerr && err == NULL)
7798 err = unlockerr;
7799 got_ref_close(branch_ref);
7801 unlockerr = got_ref_unlock(base_branch_ref);
7802 if (unlockerr && err == NULL)
7803 err = unlockerr;
7804 got_ref_close(base_branch_ref);
7806 got_fileindex_free(fileindex);
7807 free(fileindex_path);
7808 free(tree_id);
7809 if (commit)
7810 got_object_commit_close(commit);
7812 unlockerr = lock_worktree(worktree, LOCK_SH);
7813 if (unlockerr && err == NULL)
7814 err = unlockerr;
7815 return err;
7818 const struct got_error *
7819 got_worktree_integrate_abort(struct got_worktree *worktree,
7820 struct got_fileindex *fileindex, struct got_repository *repo,
7821 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7823 const struct got_error *err = NULL, *unlockerr = NULL;
7825 got_fileindex_free(fileindex);
7827 err = lock_worktree(worktree, LOCK_SH);
7829 unlockerr = got_ref_unlock(branch_ref);
7830 if (unlockerr && err == NULL)
7831 err = unlockerr;
7832 got_ref_close(branch_ref);
7834 unlockerr = got_ref_unlock(base_branch_ref);
7835 if (unlockerr && err == NULL)
7836 err = unlockerr;
7837 got_ref_close(base_branch_ref);
7839 return err;
7842 const struct got_error *
7843 got_worktree_merge_postpone(struct got_worktree *worktree,
7844 struct got_fileindex *fileindex)
7846 const struct got_error *err, *sync_err;
7847 char *fileindex_path = NULL;
7849 err = get_fileindex_path(&fileindex_path, worktree);
7850 if (err)
7851 goto done;
7853 sync_err = sync_fileindex(fileindex, fileindex_path);
7855 err = lock_worktree(worktree, LOCK_SH);
7856 if (sync_err && err == NULL)
7857 err = sync_err;
7858 done:
7859 got_fileindex_free(fileindex);
7860 free(fileindex_path);
7861 return err;
7864 static const struct got_error *
7865 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7867 const struct got_error *err;
7868 char *branch_refname = NULL, *commit_refname = NULL;
7870 err = get_merge_branch_ref_name(&branch_refname, worktree);
7871 if (err)
7872 goto done;
7873 err = delete_ref(branch_refname, repo);
7874 if (err)
7875 goto done;
7877 err = get_merge_commit_ref_name(&commit_refname, worktree);
7878 if (err)
7879 goto done;
7880 err = delete_ref(commit_refname, repo);
7881 if (err)
7882 goto done;
7884 done:
7885 free(branch_refname);
7886 free(commit_refname);
7887 return err;
7890 struct merge_commit_msg_arg {
7891 struct got_worktree *worktree;
7892 const char *branch_name;
7895 static const struct got_error *
7896 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7897 const char *diff_path, char **logmsg, void *arg)
7899 struct merge_commit_msg_arg *a = arg;
7901 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7902 got_worktree_get_head_ref_name(a->worktree)) == -1)
7903 return got_error_from_errno("asprintf");
7905 return NULL;
7909 const struct got_error *
7910 got_worktree_merge_branch(struct got_worktree *worktree,
7911 struct got_fileindex *fileindex,
7912 struct got_object_id *yca_commit_id,
7913 struct got_object_id *branch_tip,
7914 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7915 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7917 const struct got_error *err;
7918 char *fileindex_path = NULL;
7920 err = get_fileindex_path(&fileindex_path, worktree);
7921 if (err)
7922 goto done;
7924 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7925 worktree);
7926 if (err)
7927 goto done;
7929 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7930 branch_tip, repo, progress_cb, progress_arg,
7931 cancel_cb, cancel_arg);
7932 done:
7933 free(fileindex_path);
7934 return err;
7937 const struct got_error *
7938 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7939 struct got_worktree *worktree, struct got_fileindex *fileindex,
7940 const char *author, const char *committer, int allow_bad_symlinks,
7941 struct got_object_id *branch_tip, const char *branch_name,
7942 int allow_conflict, struct got_repository *repo,
7943 got_worktree_status_cb status_cb, void *status_arg)
7946 const struct got_error *err = NULL, *sync_err;
7947 struct got_pathlist_head commitable_paths;
7948 struct collect_commitables_arg cc_arg;
7949 struct got_pathlist_entry *pe;
7950 struct got_reference *head_ref = NULL;
7951 struct got_object_id *head_commit_id = NULL;
7952 int have_staged_files = 0;
7953 struct merge_commit_msg_arg mcm_arg;
7954 char *fileindex_path = NULL;
7956 memset(&cc_arg, 0, sizeof(cc_arg));
7957 *new_commit_id = NULL;
7959 TAILQ_INIT(&commitable_paths);
7961 err = get_fileindex_path(&fileindex_path, worktree);
7962 if (err)
7963 goto done;
7965 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7966 if (err)
7967 goto done;
7969 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7970 if (err)
7971 goto done;
7973 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7974 &have_staged_files);
7975 if (err && err->code != GOT_ERR_CANCELLED)
7976 goto done;
7977 if (have_staged_files) {
7978 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7979 goto done;
7982 cc_arg.commitable_paths = &commitable_paths;
7983 cc_arg.worktree = worktree;
7984 cc_arg.fileindex = fileindex;
7985 cc_arg.repo = repo;
7986 cc_arg.have_staged_files = have_staged_files;
7987 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7988 cc_arg.commit_conflicts = allow_conflict;
7989 err = worktree_status(worktree, "", fileindex, repo,
7990 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7991 if (err)
7992 goto done;
7994 if (TAILQ_EMPTY(&commitable_paths)) {
7995 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7996 "merge of %s cannot proceed", branch_name);
7997 goto done;
8000 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8001 struct got_commitable *ct = pe->data;
8002 const char *ct_path = ct->in_repo_path;
8004 while (ct_path[0] == '/')
8005 ct_path++;
8006 err = check_out_of_date(ct_path, ct->status,
8007 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8008 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8009 if (err)
8010 goto done;
8014 mcm_arg.worktree = worktree;
8015 mcm_arg.branch_name = branch_name;
8016 err = commit_worktree(new_commit_id, &commitable_paths,
8017 head_commit_id, branch_tip, worktree, author, committer, NULL,
8018 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8019 if (err)
8020 goto done;
8022 err = update_fileindex_after_commit(worktree, &commitable_paths,
8023 *new_commit_id, fileindex, have_staged_files);
8024 sync_err = sync_fileindex(fileindex, fileindex_path);
8025 if (sync_err && err == NULL)
8026 err = sync_err;
8027 done:
8028 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8029 struct got_commitable *ct = pe->data;
8031 free_commitable(ct);
8033 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8034 free(fileindex_path);
8035 return err;
8038 const struct got_error *
8039 got_worktree_merge_complete(struct got_worktree *worktree,
8040 struct got_fileindex *fileindex, struct got_repository *repo)
8042 const struct got_error *err, *unlockerr, *sync_err;
8043 char *fileindex_path = NULL;
8045 err = delete_merge_refs(worktree, repo);
8046 if (err)
8047 goto done;
8049 err = get_fileindex_path(&fileindex_path, worktree);
8050 if (err)
8051 goto done;
8052 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8053 sync_err = sync_fileindex(fileindex, fileindex_path);
8054 if (sync_err && err == NULL)
8055 err = sync_err;
8056 done:
8057 got_fileindex_free(fileindex);
8058 free(fileindex_path);
8059 unlockerr = lock_worktree(worktree, LOCK_SH);
8060 if (unlockerr && err == NULL)
8061 err = unlockerr;
8062 return err;
8065 const struct got_error *
8066 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8067 struct got_repository *repo)
8069 const struct got_error *err;
8070 char *branch_refname = NULL;
8071 struct got_reference *branch_ref = NULL;
8073 *in_progress = 0;
8075 err = get_merge_branch_ref_name(&branch_refname, worktree);
8076 if (err)
8077 return err;
8078 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8079 free(branch_refname);
8080 if (err) {
8081 if (err->code != GOT_ERR_NOT_REF)
8082 return err;
8083 } else
8084 *in_progress = 1;
8086 return NULL;
8089 const struct got_error *got_worktree_merge_prepare(
8090 struct got_fileindex **fileindex, struct got_worktree *worktree,
8091 struct got_reference *branch, struct got_repository *repo)
8093 const struct got_error *err = NULL;
8094 char *fileindex_path = NULL;
8095 char *branch_refname = NULL, *commit_refname = NULL;
8096 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8097 struct got_reference *commit_ref = NULL;
8098 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8099 struct check_rebase_ok_arg ok_arg;
8101 *fileindex = NULL;
8103 err = lock_worktree(worktree, LOCK_EX);
8104 if (err)
8105 return err;
8107 err = open_fileindex(fileindex, &fileindex_path, worktree);
8108 if (err)
8109 goto done;
8111 /* Preconditions are the same as for rebase. */
8112 ok_arg.worktree = worktree;
8113 ok_arg.repo = repo;
8114 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8115 &ok_arg);
8116 if (err)
8117 goto done;
8119 err = get_merge_branch_ref_name(&branch_refname, worktree);
8120 if (err)
8121 return err;
8123 err = get_merge_commit_ref_name(&commit_refname, worktree);
8124 if (err)
8125 return err;
8127 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8128 0);
8129 if (err)
8130 goto done;
8132 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8133 if (err)
8134 goto done;
8136 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8137 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8138 goto done;
8141 err = got_ref_resolve(&branch_tip, repo, branch);
8142 if (err)
8143 goto done;
8145 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8146 if (err)
8147 goto done;
8148 err = got_ref_write(branch_ref, repo);
8149 if (err)
8150 goto done;
8152 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8153 if (err)
8154 goto done;
8155 err = got_ref_write(commit_ref, repo);
8156 if (err)
8157 goto done;
8159 done:
8160 free(branch_refname);
8161 free(commit_refname);
8162 free(fileindex_path);
8163 if (branch_ref)
8164 got_ref_close(branch_ref);
8165 if (commit_ref)
8166 got_ref_close(commit_ref);
8167 if (wt_branch)
8168 got_ref_close(wt_branch);
8169 free(wt_branch_tip);
8170 if (err) {
8171 if (*fileindex) {
8172 got_fileindex_free(*fileindex);
8173 *fileindex = NULL;
8175 lock_worktree(worktree, LOCK_SH);
8177 return err;
8180 const struct got_error *
8181 got_worktree_merge_continue(char **branch_name,
8182 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8183 struct got_worktree *worktree, struct got_repository *repo)
8185 const struct got_error *err;
8186 char *commit_refname = NULL, *branch_refname = NULL;
8187 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8188 char *fileindex_path = NULL;
8189 int have_staged_files = 0;
8191 *branch_name = NULL;
8192 *branch_tip = NULL;
8193 *fileindex = NULL;
8195 err = lock_worktree(worktree, LOCK_EX);
8196 if (err)
8197 return err;
8199 err = open_fileindex(fileindex, &fileindex_path, worktree);
8200 if (err)
8201 goto done;
8203 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8204 &have_staged_files);
8205 if (err && err->code != GOT_ERR_CANCELLED)
8206 goto done;
8207 if (have_staged_files) {
8208 err = got_error(GOT_ERR_STAGED_PATHS);
8209 goto done;
8212 err = get_merge_branch_ref_name(&branch_refname, worktree);
8213 if (err)
8214 goto done;
8216 err = get_merge_commit_ref_name(&commit_refname, worktree);
8217 if (err)
8218 goto done;
8220 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8221 if (err)
8222 goto done;
8224 if (!got_ref_is_symbolic(branch_ref)) {
8225 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8226 "%s is not a symbolic reference",
8227 got_ref_get_name(branch_ref));
8228 goto done;
8230 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8231 if (*branch_name == NULL) {
8232 err = got_error_from_errno("strdup");
8233 goto done;
8236 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8237 if (err)
8238 goto done;
8240 err = got_ref_resolve(branch_tip, repo, commit_ref);
8241 if (err)
8242 goto done;
8243 done:
8244 free(commit_refname);
8245 free(branch_refname);
8246 free(fileindex_path);
8247 if (commit_ref)
8248 got_ref_close(commit_ref);
8249 if (branch_ref)
8250 got_ref_close(branch_ref);
8251 if (err) {
8252 if (*branch_name) {
8253 free(*branch_name);
8254 *branch_name = NULL;
8256 free(*branch_tip);
8257 *branch_tip = NULL;
8258 if (*fileindex) {
8259 got_fileindex_free(*fileindex);
8260 *fileindex = NULL;
8262 lock_worktree(worktree, LOCK_SH);
8264 return err;
8267 const struct got_error *
8268 got_worktree_merge_abort(struct got_worktree *worktree,
8269 struct got_fileindex *fileindex, struct got_repository *repo,
8270 got_worktree_checkout_cb progress_cb, void *progress_arg)
8272 const struct got_error *err, *unlockerr, *sync_err;
8273 struct got_object_id *commit_id = NULL;
8274 struct got_commit_object *commit = NULL;
8275 char *fileindex_path = NULL;
8276 struct revert_file_args rfa;
8277 struct got_object_id *tree_id = NULL;
8279 err = got_object_open_as_commit(&commit, repo,
8280 worktree->base_commit_id);
8281 if (err)
8282 goto done;
8284 err = got_object_id_by_path(&tree_id, repo, commit,
8285 worktree->path_prefix);
8286 if (err)
8287 goto done;
8289 err = delete_merge_refs(worktree, repo);
8290 if (err)
8291 goto done;
8293 err = get_fileindex_path(&fileindex_path, worktree);
8294 if (err)
8295 goto done;
8297 rfa.worktree = worktree;
8298 rfa.fileindex = fileindex;
8299 rfa.progress_cb = progress_cb;
8300 rfa.progress_arg = progress_arg;
8301 rfa.patch_cb = NULL;
8302 rfa.patch_arg = NULL;
8303 rfa.repo = repo;
8304 rfa.unlink_added_files = 1;
8305 err = worktree_status(worktree, "", fileindex, repo,
8306 revert_file, &rfa, NULL, NULL, 1, 0);
8307 if (err)
8308 goto sync;
8310 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8311 repo, progress_cb, progress_arg, NULL, NULL);
8312 sync:
8313 sync_err = sync_fileindex(fileindex, fileindex_path);
8314 if (sync_err && err == NULL)
8315 err = sync_err;
8316 done:
8317 free(tree_id);
8318 free(commit_id);
8319 if (commit)
8320 got_object_commit_close(commit);
8321 if (fileindex)
8322 got_fileindex_free(fileindex);
8323 free(fileindex_path);
8325 unlockerr = lock_worktree(worktree, LOCK_SH);
8326 if (unlockerr && err == NULL)
8327 err = unlockerr;
8328 return err;
8331 struct check_stage_ok_arg {
8332 struct got_object_id *head_commit_id;
8333 struct got_worktree *worktree;
8334 struct got_fileindex *fileindex;
8335 struct got_repository *repo;
8336 int have_changes;
8339 static const struct got_error *
8340 check_stage_ok(void *arg, unsigned char status,
8341 unsigned char staged_status, const char *relpath,
8342 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8343 struct got_object_id *commit_id, int dirfd, const char *de_name)
8345 struct check_stage_ok_arg *a = arg;
8346 const struct got_error *err = NULL;
8347 struct got_fileindex_entry *ie;
8348 struct got_object_id base_commit_id;
8349 struct got_object_id *base_commit_idp = NULL;
8350 char *in_repo_path = NULL, *p;
8352 if (status == GOT_STATUS_UNVERSIONED ||
8353 status == GOT_STATUS_NO_CHANGE)
8354 return NULL;
8355 if (status == GOT_STATUS_NONEXISTENT)
8356 return got_error_set_errno(ENOENT, relpath);
8358 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8359 if (ie == NULL)
8360 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8362 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8363 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8364 relpath) == -1)
8365 return got_error_from_errno("asprintf");
8367 if (got_fileindex_entry_has_commit(ie)) {
8368 base_commit_idp = got_fileindex_entry_get_commit_id(
8369 &base_commit_id, ie);
8372 if (status == GOT_STATUS_CONFLICT) {
8373 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8374 goto done;
8375 } else if (status != GOT_STATUS_ADD &&
8376 status != GOT_STATUS_MODIFY &&
8377 status != GOT_STATUS_DELETE) {
8378 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8379 goto done;
8382 a->have_changes = 1;
8384 p = in_repo_path;
8385 while (p[0] == '/')
8386 p++;
8387 err = check_out_of_date(p, status, staged_status,
8388 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8389 GOT_ERR_STAGE_OUT_OF_DATE);
8390 done:
8391 free(in_repo_path);
8392 return err;
8395 struct stage_path_arg {
8396 struct got_worktree *worktree;
8397 struct got_fileindex *fileindex;
8398 struct got_repository *repo;
8399 got_worktree_status_cb status_cb;
8400 void *status_arg;
8401 got_worktree_patch_cb patch_cb;
8402 void *patch_arg;
8403 int staged_something;
8404 int allow_bad_symlinks;
8407 static const struct got_error *
8408 stage_path(void *arg, unsigned char status,
8409 unsigned char staged_status, const char *relpath,
8410 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8411 struct got_object_id *commit_id, int dirfd, const char *de_name)
8413 struct stage_path_arg *a = arg;
8414 const struct got_error *err = NULL;
8415 struct got_fileindex_entry *ie;
8416 char *ondisk_path = NULL, *path_content = NULL;
8417 uint32_t stage;
8418 struct got_object_id *new_staged_blob_id = NULL;
8419 struct stat sb;
8421 if (status == GOT_STATUS_UNVERSIONED)
8422 return NULL;
8424 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8425 if (ie == NULL)
8426 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8428 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8429 relpath)== -1)
8430 return got_error_from_errno("asprintf");
8432 switch (status) {
8433 case GOT_STATUS_ADD:
8434 case GOT_STATUS_MODIFY:
8435 /* XXX could sb.st_mode be passed in by our caller? */
8436 if (lstat(ondisk_path, &sb) == -1) {
8437 err = got_error_from_errno2("lstat", ondisk_path);
8438 break;
8440 if (a->patch_cb) {
8441 if (status == GOT_STATUS_ADD) {
8442 int choice = GOT_PATCH_CHOICE_NONE;
8443 err = (*a->patch_cb)(&choice, a->patch_arg,
8444 status, ie->path, NULL, 1, 1);
8445 if (err)
8446 break;
8447 if (choice != GOT_PATCH_CHOICE_YES)
8448 break;
8449 } else {
8450 err = create_patched_content(&path_content, 0,
8451 staged_blob_id ? staged_blob_id : blob_id,
8452 ondisk_path, dirfd, de_name, ie->path,
8453 a->repo, a->patch_cb, a->patch_arg);
8454 if (err || path_content == NULL)
8455 break;
8458 err = got_object_blob_create(&new_staged_blob_id,
8459 path_content ? path_content : ondisk_path, a->repo);
8460 if (err)
8461 break;
8462 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8463 SHA1_DIGEST_LENGTH);
8464 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8465 stage = GOT_FILEIDX_STAGE_ADD;
8466 else
8467 stage = GOT_FILEIDX_STAGE_MODIFY;
8468 got_fileindex_entry_stage_set(ie, stage);
8469 if (S_ISLNK(sb.st_mode)) {
8470 int is_bad_symlink = 0;
8471 if (!a->allow_bad_symlinks) {
8472 char target_path[PATH_MAX];
8473 ssize_t target_len;
8474 target_len = readlink(ondisk_path, target_path,
8475 sizeof(target_path));
8476 if (target_len == -1) {
8477 err = got_error_from_errno2("readlink",
8478 ondisk_path);
8479 break;
8481 err = is_bad_symlink_target(&is_bad_symlink,
8482 target_path, target_len, ondisk_path,
8483 a->worktree->root_path);
8484 if (err)
8485 break;
8486 if (is_bad_symlink) {
8487 err = got_error_path(ondisk_path,
8488 GOT_ERR_BAD_SYMLINK);
8489 break;
8492 if (is_bad_symlink)
8493 got_fileindex_entry_staged_filetype_set(ie,
8494 GOT_FILEIDX_MODE_BAD_SYMLINK);
8495 else
8496 got_fileindex_entry_staged_filetype_set(ie,
8497 GOT_FILEIDX_MODE_SYMLINK);
8498 } else {
8499 got_fileindex_entry_staged_filetype_set(ie,
8500 GOT_FILEIDX_MODE_REGULAR_FILE);
8502 a->staged_something = 1;
8503 if (a->status_cb == NULL)
8504 break;
8505 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8506 get_staged_status(ie), relpath, blob_id,
8507 new_staged_blob_id, NULL, dirfd, de_name);
8508 if (err)
8509 break;
8511 * When staging the reverse of the staged diff,
8512 * implicitly unstage the file.
8514 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8515 sizeof(ie->blob_sha1)) == 0) {
8516 got_fileindex_entry_stage_set(ie,
8517 GOT_FILEIDX_STAGE_NONE);
8519 break;
8520 case GOT_STATUS_DELETE:
8521 if (staged_status == GOT_STATUS_DELETE)
8522 break;
8523 if (a->patch_cb) {
8524 int choice = GOT_PATCH_CHOICE_NONE;
8525 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8526 ie->path, NULL, 1, 1);
8527 if (err)
8528 break;
8529 if (choice == GOT_PATCH_CHOICE_NO)
8530 break;
8531 if (choice != GOT_PATCH_CHOICE_YES) {
8532 err = got_error(GOT_ERR_PATCH_CHOICE);
8533 break;
8536 stage = GOT_FILEIDX_STAGE_DELETE;
8537 got_fileindex_entry_stage_set(ie, stage);
8538 a->staged_something = 1;
8539 if (a->status_cb == NULL)
8540 break;
8541 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8542 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8543 de_name);
8544 break;
8545 case GOT_STATUS_NO_CHANGE:
8546 break;
8547 case GOT_STATUS_CONFLICT:
8548 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8549 break;
8550 case GOT_STATUS_NONEXISTENT:
8551 err = got_error_set_errno(ENOENT, relpath);
8552 break;
8553 default:
8554 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8555 break;
8558 if (path_content && unlink(path_content) == -1 && err == NULL)
8559 err = got_error_from_errno2("unlink", path_content);
8560 free(path_content);
8561 free(ondisk_path);
8562 free(new_staged_blob_id);
8563 return err;
8566 const struct got_error *
8567 got_worktree_stage(struct got_worktree *worktree,
8568 struct got_pathlist_head *paths,
8569 got_worktree_status_cb status_cb, void *status_arg,
8570 got_worktree_patch_cb patch_cb, void *patch_arg,
8571 int allow_bad_symlinks, struct got_repository *repo)
8573 const struct got_error *err = NULL, *sync_err, *unlockerr;
8574 struct got_pathlist_entry *pe;
8575 struct got_fileindex *fileindex = NULL;
8576 char *fileindex_path = NULL;
8577 struct got_reference *head_ref = NULL;
8578 struct got_object_id *head_commit_id = NULL;
8579 struct check_stage_ok_arg oka;
8580 struct stage_path_arg spa;
8582 err = lock_worktree(worktree, LOCK_EX);
8583 if (err)
8584 return err;
8586 err = got_ref_open(&head_ref, repo,
8587 got_worktree_get_head_ref_name(worktree), 0);
8588 if (err)
8589 goto done;
8590 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8591 if (err)
8592 goto done;
8593 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8594 if (err)
8595 goto done;
8597 /* Check pre-conditions before staging anything. */
8598 oka.head_commit_id = head_commit_id;
8599 oka.worktree = worktree;
8600 oka.fileindex = fileindex;
8601 oka.repo = repo;
8602 oka.have_changes = 0;
8603 TAILQ_FOREACH(pe, paths, entry) {
8604 err = worktree_status(worktree, pe->path, fileindex, repo,
8605 check_stage_ok, &oka, NULL, NULL, 1, 0);
8606 if (err)
8607 goto done;
8609 if (!oka.have_changes) {
8610 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8611 goto done;
8614 spa.worktree = worktree;
8615 spa.fileindex = fileindex;
8616 spa.repo = repo;
8617 spa.patch_cb = patch_cb;
8618 spa.patch_arg = patch_arg;
8619 spa.status_cb = status_cb;
8620 spa.status_arg = status_arg;
8621 spa.staged_something = 0;
8622 spa.allow_bad_symlinks = allow_bad_symlinks;
8623 TAILQ_FOREACH(pe, paths, entry) {
8624 err = worktree_status(worktree, pe->path, fileindex, repo,
8625 stage_path, &spa, NULL, NULL, 1, 0);
8626 if (err)
8627 goto done;
8629 if (!spa.staged_something) {
8630 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8631 goto done;
8634 sync_err = sync_fileindex(fileindex, fileindex_path);
8635 if (sync_err && err == NULL)
8636 err = sync_err;
8637 done:
8638 if (head_ref)
8639 got_ref_close(head_ref);
8640 free(head_commit_id);
8641 free(fileindex_path);
8642 if (fileindex)
8643 got_fileindex_free(fileindex);
8644 unlockerr = lock_worktree(worktree, LOCK_SH);
8645 if (unlockerr && err == NULL)
8646 err = unlockerr;
8647 return err;
8650 struct unstage_path_arg {
8651 struct got_worktree *worktree;
8652 struct got_fileindex *fileindex;
8653 struct got_repository *repo;
8654 got_worktree_checkout_cb progress_cb;
8655 void *progress_arg;
8656 got_worktree_patch_cb patch_cb;
8657 void *patch_arg;
8660 static const struct got_error *
8661 create_unstaged_content(char **path_unstaged_content,
8662 char **path_new_staged_content, struct got_object_id *blob_id,
8663 struct got_object_id *staged_blob_id, const char *relpath,
8664 struct got_repository *repo,
8665 got_worktree_patch_cb patch_cb, void *patch_arg)
8667 const struct got_error *err, *free_err;
8668 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8669 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8670 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8671 struct got_diffreg_result *diffreg_result = NULL;
8672 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8673 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8674 int fd1 = -1, fd2 = -1;
8676 *path_unstaged_content = NULL;
8677 *path_new_staged_content = NULL;
8679 err = got_object_id_str(&label1, blob_id);
8680 if (err)
8681 return err;
8683 fd1 = got_opentempfd();
8684 if (fd1 == -1) {
8685 err = got_error_from_errno("got_opentempfd");
8686 goto done;
8688 fd2 = got_opentempfd();
8689 if (fd2 == -1) {
8690 err = got_error_from_errno("got_opentempfd");
8691 goto done;
8694 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8695 if (err)
8696 goto done;
8698 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8699 if (err)
8700 goto done;
8702 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8703 if (err)
8704 goto done;
8706 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8707 fd2);
8708 if (err)
8709 goto done;
8711 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8712 if (err)
8713 goto done;
8715 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8716 if (err)
8717 goto done;
8719 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8720 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8721 if (err)
8722 goto done;
8724 err = got_opentemp_named(path_unstaged_content, &outfile,
8725 "got-unstaged-content", "");
8726 if (err)
8727 goto done;
8728 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8729 "got-new-staged-content", "");
8730 if (err)
8731 goto done;
8733 if (fseek(f1, 0L, SEEK_SET) == -1) {
8734 err = got_ferror(f1, GOT_ERR_IO);
8735 goto done;
8737 if (fseek(f2, 0L, SEEK_SET) == -1) {
8738 err = got_ferror(f2, GOT_ERR_IO);
8739 goto done;
8741 /* Count the number of actual changes in the diff result. */
8742 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8743 struct diff_chunk_context cc = {};
8744 diff_chunk_context_load_change(&cc, &nchunks_used,
8745 diffreg_result->result, n, 0);
8746 nchanges++;
8748 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8749 int choice;
8750 err = apply_or_reject_change(&choice, &nchunks_used,
8751 diffreg_result->result, n, relpath, f1, f2,
8752 &line_cur1, &line_cur2,
8753 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8754 if (err)
8755 goto done;
8756 if (choice == GOT_PATCH_CHOICE_YES)
8757 have_content = 1;
8758 else
8759 have_rejected_content = 1;
8760 if (choice == GOT_PATCH_CHOICE_QUIT)
8761 break;
8763 if (have_content || have_rejected_content)
8764 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8765 outfile, rejectfile);
8766 done:
8767 free(label1);
8768 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8769 err = got_error_from_errno("close");
8770 if (blob)
8771 got_object_blob_close(blob);
8772 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8773 err = got_error_from_errno("close");
8774 if (staged_blob)
8775 got_object_blob_close(staged_blob);
8776 free_err = got_diffreg_result_free(diffreg_result);
8777 if (free_err && err == NULL)
8778 err = free_err;
8779 if (f1 && fclose(f1) == EOF && err == NULL)
8780 err = got_error_from_errno2("fclose", path1);
8781 if (f2 && fclose(f2) == EOF && err == NULL)
8782 err = got_error_from_errno2("fclose", path2);
8783 if (outfile && fclose(outfile) == EOF && err == NULL)
8784 err = got_error_from_errno2("fclose", *path_unstaged_content);
8785 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8786 err = got_error_from_errno2("fclose", *path_new_staged_content);
8787 if (path1 && unlink(path1) == -1 && err == NULL)
8788 err = got_error_from_errno2("unlink", path1);
8789 if (path2 && unlink(path2) == -1 && err == NULL)
8790 err = got_error_from_errno2("unlink", path2);
8791 if (err || !have_content) {
8792 if (*path_unstaged_content &&
8793 unlink(*path_unstaged_content) == -1 && err == NULL)
8794 err = got_error_from_errno2("unlink",
8795 *path_unstaged_content);
8796 free(*path_unstaged_content);
8797 *path_unstaged_content = NULL;
8799 if (err || !have_content || !have_rejected_content) {
8800 if (*path_new_staged_content &&
8801 unlink(*path_new_staged_content) == -1 && err == NULL)
8802 err = got_error_from_errno2("unlink",
8803 *path_new_staged_content);
8804 free(*path_new_staged_content);
8805 *path_new_staged_content = NULL;
8807 free(path1);
8808 free(path2);
8809 return err;
8812 static const struct got_error *
8813 unstage_hunks(struct got_object_id *staged_blob_id,
8814 struct got_blob_object *blob_base,
8815 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8816 const char *ondisk_path, const char *label_orig,
8817 struct got_worktree *worktree, struct got_repository *repo,
8818 got_worktree_patch_cb patch_cb, void *patch_arg,
8819 got_worktree_checkout_cb progress_cb, void *progress_arg)
8821 const struct got_error *err = NULL;
8822 char *path_unstaged_content = NULL;
8823 char *path_new_staged_content = NULL;
8824 char *parent = NULL, *base_path = NULL;
8825 char *blob_base_path = NULL;
8826 struct got_object_id *new_staged_blob_id = NULL;
8827 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8828 struct stat sb;
8830 err = create_unstaged_content(&path_unstaged_content,
8831 &path_new_staged_content, blob_id, staged_blob_id,
8832 ie->path, repo, patch_cb, patch_arg);
8833 if (err)
8834 return err;
8836 if (path_unstaged_content == NULL)
8837 return NULL;
8839 if (path_new_staged_content) {
8840 err = got_object_blob_create(&new_staged_blob_id,
8841 path_new_staged_content, repo);
8842 if (err)
8843 goto done;
8846 f = fopen(path_unstaged_content, "re");
8847 if (f == NULL) {
8848 err = got_error_from_errno2("fopen",
8849 path_unstaged_content);
8850 goto done;
8852 if (fstat(fileno(f), &sb) == -1) {
8853 err = got_error_from_errno2("fstat", path_unstaged_content);
8854 goto done;
8856 if (got_fileindex_entry_staged_filetype_get(ie) ==
8857 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8858 char link_target[PATH_MAX];
8859 size_t r;
8860 r = fread(link_target, 1, sizeof(link_target), f);
8861 if (r == 0 && ferror(f)) {
8862 err = got_error_from_errno("fread");
8863 goto done;
8865 if (r >= sizeof(link_target)) { /* should not happen */
8866 err = got_error(GOT_ERR_NO_SPACE);
8867 goto done;
8869 link_target[r] = '\0';
8870 err = merge_symlink(worktree, blob_base,
8871 ondisk_path, ie->path, label_orig, link_target,
8872 worktree->base_commit_id, repo, progress_cb,
8873 progress_arg);
8874 } else {
8875 int local_changes_subsumed;
8877 err = got_path_dirname(&parent, ondisk_path);
8878 if (err)
8879 return err;
8881 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8882 parent) == -1) {
8883 err = got_error_from_errno("asprintf");
8884 base_path = NULL;
8885 goto done;
8888 err = got_opentemp_named(&blob_base_path, &f_base,
8889 base_path, "");
8890 if (err)
8891 goto done;
8892 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8893 blob_base);
8894 if (err)
8895 goto done;
8898 * In order the run a 3-way merge with a symlink we copy the symlink's
8899 * target path into a temporary file and use that file with diff3.
8901 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8902 err = dump_symlink_target_path_to_file(&f_deriv2,
8903 ondisk_path);
8904 if (err)
8905 goto done;
8906 } else {
8907 int fd;
8908 fd = open(ondisk_path,
8909 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8910 if (fd == -1) {
8911 err = got_error_from_errno2("open", ondisk_path);
8912 goto done;
8914 f_deriv2 = fdopen(fd, "r");
8915 if (f_deriv2 == NULL) {
8916 err = got_error_from_errno2("fdopen", ondisk_path);
8917 close(fd);
8918 goto done;
8922 err = merge_file(&local_changes_subsumed, worktree,
8923 f_base, f, f_deriv2, ondisk_path, ie->path,
8924 got_fileindex_perms_to_st(ie),
8925 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8926 repo, progress_cb, progress_arg);
8928 if (err)
8929 goto done;
8931 if (new_staged_blob_id) {
8932 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8933 SHA1_DIGEST_LENGTH);
8934 } else {
8935 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8936 got_fileindex_entry_staged_filetype_set(ie, 0);
8938 done:
8939 free(new_staged_blob_id);
8940 if (path_unstaged_content &&
8941 unlink(path_unstaged_content) == -1 && err == NULL)
8942 err = got_error_from_errno2("unlink", path_unstaged_content);
8943 if (path_new_staged_content &&
8944 unlink(path_new_staged_content) == -1 && err == NULL)
8945 err = got_error_from_errno2("unlink", path_new_staged_content);
8946 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8947 err = got_error_from_errno2("unlink", blob_base_path);
8948 if (f_base && fclose(f_base) == EOF && err == NULL)
8949 err = got_error_from_errno2("fclose", path_unstaged_content);
8950 if (f && fclose(f) == EOF && err == NULL)
8951 err = got_error_from_errno2("fclose", path_unstaged_content);
8952 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8953 err = got_error_from_errno2("fclose", ondisk_path);
8954 free(path_unstaged_content);
8955 free(path_new_staged_content);
8956 free(blob_base_path);
8957 free(parent);
8958 free(base_path);
8959 return err;
8962 static const struct got_error *
8963 unstage_path(void *arg, unsigned char status,
8964 unsigned char staged_status, const char *relpath,
8965 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8966 struct got_object_id *commit_id, int dirfd, const char *de_name)
8968 const struct got_error *err = NULL;
8969 struct unstage_path_arg *a = arg;
8970 struct got_fileindex_entry *ie;
8971 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8972 char *ondisk_path = NULL;
8973 char *id_str = NULL, *label_orig = NULL;
8974 int local_changes_subsumed;
8975 struct stat sb;
8976 int fd1 = -1, fd2 = -1;
8978 if (staged_status != GOT_STATUS_ADD &&
8979 staged_status != GOT_STATUS_MODIFY &&
8980 staged_status != GOT_STATUS_DELETE)
8981 return NULL;
8983 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8984 if (ie == NULL)
8985 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8987 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8988 == -1)
8989 return got_error_from_errno("asprintf");
8991 err = got_object_id_str(&id_str,
8992 commit_id ? commit_id : a->worktree->base_commit_id);
8993 if (err)
8994 goto done;
8995 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8996 id_str) == -1) {
8997 err = got_error_from_errno("asprintf");
8998 goto done;
9001 fd1 = got_opentempfd();
9002 if (fd1 == -1) {
9003 err = got_error_from_errno("got_opentempfd");
9004 goto done;
9006 fd2 = got_opentempfd();
9007 if (fd2 == -1) {
9008 err = got_error_from_errno("got_opentempfd");
9009 goto done;
9012 switch (staged_status) {
9013 case GOT_STATUS_MODIFY:
9014 err = got_object_open_as_blob(&blob_base, a->repo,
9015 blob_id, 8192, fd1);
9016 if (err)
9017 break;
9018 /* fall through */
9019 case GOT_STATUS_ADD:
9020 if (a->patch_cb) {
9021 if (staged_status == GOT_STATUS_ADD) {
9022 int choice = GOT_PATCH_CHOICE_NONE;
9023 err = (*a->patch_cb)(&choice, a->patch_arg,
9024 staged_status, ie->path, NULL, 1, 1);
9025 if (err)
9026 break;
9027 if (choice != GOT_PATCH_CHOICE_YES)
9028 break;
9029 } else {
9030 err = unstage_hunks(staged_blob_id,
9031 blob_base, blob_id, ie, ondisk_path,
9032 label_orig, a->worktree, a->repo,
9033 a->patch_cb, a->patch_arg,
9034 a->progress_cb, a->progress_arg);
9035 break; /* Done with this file. */
9038 err = got_object_open_as_blob(&blob_staged, a->repo,
9039 staged_blob_id, 8192, fd2);
9040 if (err)
9041 break;
9042 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9043 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9044 case GOT_FILEIDX_MODE_REGULAR_FILE:
9045 err = merge_blob(&local_changes_subsumed, a->worktree,
9046 blob_base, ondisk_path, relpath,
9047 got_fileindex_perms_to_st(ie), label_orig,
9048 blob_staged, commit_id ? commit_id :
9049 a->worktree->base_commit_id, a->repo,
9050 a->progress_cb, a->progress_arg);
9051 break;
9052 case GOT_FILEIDX_MODE_SYMLINK:
9053 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9054 char *staged_target;
9055 err = got_object_blob_read_to_str(
9056 &staged_target, blob_staged);
9057 if (err)
9058 goto done;
9059 err = merge_symlink(a->worktree, blob_base,
9060 ondisk_path, relpath, label_orig,
9061 staged_target, commit_id ? commit_id :
9062 a->worktree->base_commit_id,
9063 a->repo, a->progress_cb, a->progress_arg);
9064 free(staged_target);
9065 } else {
9066 err = merge_blob(&local_changes_subsumed,
9067 a->worktree, blob_base, ondisk_path,
9068 relpath, got_fileindex_perms_to_st(ie),
9069 label_orig, blob_staged,
9070 commit_id ? commit_id :
9071 a->worktree->base_commit_id, a->repo,
9072 a->progress_cb, a->progress_arg);
9074 break;
9075 default:
9076 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9077 break;
9079 if (err == NULL) {
9080 got_fileindex_entry_stage_set(ie,
9081 GOT_FILEIDX_STAGE_NONE);
9082 got_fileindex_entry_staged_filetype_set(ie, 0);
9084 break;
9085 case GOT_STATUS_DELETE:
9086 if (a->patch_cb) {
9087 int choice = GOT_PATCH_CHOICE_NONE;
9088 err = (*a->patch_cb)(&choice, a->patch_arg,
9089 staged_status, ie->path, NULL, 1, 1);
9090 if (err)
9091 break;
9092 if (choice == GOT_PATCH_CHOICE_NO)
9093 break;
9094 if (choice != GOT_PATCH_CHOICE_YES) {
9095 err = got_error(GOT_ERR_PATCH_CHOICE);
9096 break;
9099 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9100 got_fileindex_entry_staged_filetype_set(ie, 0);
9101 err = get_file_status(&status, &sb, ie, ondisk_path,
9102 dirfd, de_name, a->repo);
9103 if (err)
9104 break;
9105 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9106 break;
9108 done:
9109 free(ondisk_path);
9110 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9111 err = got_error_from_errno("close");
9112 if (blob_base)
9113 got_object_blob_close(blob_base);
9114 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9115 err = got_error_from_errno("close");
9116 if (blob_staged)
9117 got_object_blob_close(blob_staged);
9118 free(id_str);
9119 free(label_orig);
9120 return err;
9123 const struct got_error *
9124 got_worktree_unstage(struct got_worktree *worktree,
9125 struct got_pathlist_head *paths,
9126 got_worktree_checkout_cb progress_cb, void *progress_arg,
9127 got_worktree_patch_cb patch_cb, void *patch_arg,
9128 struct got_repository *repo)
9130 const struct got_error *err = NULL, *sync_err, *unlockerr;
9131 struct got_pathlist_entry *pe;
9132 struct got_fileindex *fileindex = NULL;
9133 char *fileindex_path = NULL;
9134 struct unstage_path_arg upa;
9136 err = lock_worktree(worktree, LOCK_EX);
9137 if (err)
9138 return err;
9140 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9141 if (err)
9142 goto done;
9144 upa.worktree = worktree;
9145 upa.fileindex = fileindex;
9146 upa.repo = repo;
9147 upa.progress_cb = progress_cb;
9148 upa.progress_arg = progress_arg;
9149 upa.patch_cb = patch_cb;
9150 upa.patch_arg = patch_arg;
9151 TAILQ_FOREACH(pe, paths, entry) {
9152 err = worktree_status(worktree, pe->path, fileindex, repo,
9153 unstage_path, &upa, NULL, NULL, 1, 0);
9154 if (err)
9155 goto done;
9158 sync_err = sync_fileindex(fileindex, fileindex_path);
9159 if (sync_err && err == NULL)
9160 err = sync_err;
9161 done:
9162 free(fileindex_path);
9163 if (fileindex)
9164 got_fileindex_free(fileindex);
9165 unlockerr = lock_worktree(worktree, LOCK_SH);
9166 if (unlockerr && err == NULL)
9167 err = unlockerr;
9168 return err;
9171 struct report_file_info_arg {
9172 struct got_worktree *worktree;
9173 got_worktree_path_info_cb info_cb;
9174 void *info_arg;
9175 struct got_pathlist_head *paths;
9176 got_cancel_cb cancel_cb;
9177 void *cancel_arg;
9180 static const struct got_error *
9181 report_file_info(void *arg, struct got_fileindex_entry *ie)
9183 struct report_file_info_arg *a = arg;
9184 struct got_pathlist_entry *pe;
9185 struct got_object_id blob_id, staged_blob_id, commit_id;
9186 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9187 struct got_object_id *commit_idp = NULL;
9188 int stage;
9190 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9191 return got_error(GOT_ERR_CANCELLED);
9193 TAILQ_FOREACH(pe, a->paths, entry) {
9194 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9195 got_path_is_child(ie->path, pe->path, pe->path_len))
9196 break;
9198 if (pe == NULL) /* not found */
9199 return NULL;
9201 if (got_fileindex_entry_has_blob(ie))
9202 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9203 stage = got_fileindex_entry_stage_get(ie);
9204 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9205 stage == GOT_FILEIDX_STAGE_ADD) {
9206 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9207 &staged_blob_id, ie);
9210 if (got_fileindex_entry_has_commit(ie))
9211 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9213 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9214 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9217 const struct got_error *
9218 got_worktree_path_info(struct got_worktree *worktree,
9219 struct got_pathlist_head *paths,
9220 got_worktree_path_info_cb info_cb, void *info_arg,
9221 got_cancel_cb cancel_cb, void *cancel_arg)
9224 const struct got_error *err = NULL, *unlockerr;
9225 struct got_fileindex *fileindex = NULL;
9226 char *fileindex_path = NULL;
9227 struct report_file_info_arg arg;
9229 err = lock_worktree(worktree, LOCK_SH);
9230 if (err)
9231 return err;
9233 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9234 if (err)
9235 goto done;
9237 arg.worktree = worktree;
9238 arg.info_cb = info_cb;
9239 arg.info_arg = info_arg;
9240 arg.paths = paths;
9241 arg.cancel_cb = cancel_cb;
9242 arg.cancel_arg = cancel_arg;
9243 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9244 &arg);
9245 done:
9246 free(fileindex_path);
9247 if (fileindex)
9248 got_fileindex_free(fileindex);
9249 unlockerr = lock_worktree(worktree, LOCK_UN);
9250 if (unlockerr && err == NULL)
9251 err = unlockerr;
9252 return err;
9255 static const struct got_error *
9256 patch_check_path(const char *p, char **path, unsigned char *status,
9257 unsigned char *staged_status, struct got_fileindex *fileindex,
9258 struct got_worktree *worktree, struct got_repository *repo)
9260 const struct got_error *err;
9261 struct got_fileindex_entry *ie;
9262 struct stat sb;
9263 char *ondisk_path = NULL;
9265 err = got_worktree_resolve_path(path, worktree, p);
9266 if (err)
9267 return err;
9269 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9270 *path[0] ? "/" : "", *path) == -1)
9271 return got_error_from_errno("asprintf");
9273 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9274 if (ie) {
9275 *staged_status = get_staged_status(ie);
9276 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9277 repo);
9278 if (err)
9279 goto done;
9280 } else {
9281 *staged_status = GOT_STATUS_NO_CHANGE;
9282 *status = GOT_STATUS_UNVERSIONED;
9283 if (lstat(ondisk_path, &sb) == -1) {
9284 if (errno != ENOENT) {
9285 err = got_error_from_errno2("lstat",
9286 ondisk_path);
9287 goto done;
9289 *status = GOT_STATUS_NONEXISTENT;
9293 done:
9294 free(ondisk_path);
9295 return err;
9298 static const struct got_error *
9299 patch_can_rm(const char *path, unsigned char status,
9300 unsigned char staged_status)
9302 if (status == GOT_STATUS_NONEXISTENT)
9303 return got_error_set_errno(ENOENT, path);
9304 if (status != GOT_STATUS_NO_CHANGE &&
9305 status != GOT_STATUS_ADD &&
9306 status != GOT_STATUS_MODIFY &&
9307 status != GOT_STATUS_MODE_CHANGE)
9308 return got_error_path(path, GOT_ERR_FILE_STATUS);
9309 if (staged_status == GOT_STATUS_DELETE)
9310 return got_error_path(path, GOT_ERR_FILE_STATUS);
9311 return NULL;
9314 static const struct got_error *
9315 patch_can_add(const char *path, unsigned char status)
9317 if (status != GOT_STATUS_NONEXISTENT)
9318 return got_error_path(path, GOT_ERR_FILE_STATUS);
9319 return NULL;
9322 static const struct got_error *
9323 patch_can_edit(const char *path, unsigned char status,
9324 unsigned char staged_status)
9326 if (status == GOT_STATUS_NONEXISTENT)
9327 return got_error_set_errno(ENOENT, path);
9328 if (status != GOT_STATUS_NO_CHANGE &&
9329 status != GOT_STATUS_ADD &&
9330 status != GOT_STATUS_MODIFY)
9331 return got_error_path(path, GOT_ERR_FILE_STATUS);
9332 if (staged_status == GOT_STATUS_DELETE)
9333 return got_error_path(path, GOT_ERR_FILE_STATUS);
9334 return NULL;
9337 const struct got_error *
9338 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9339 char **fileindex_path, struct got_worktree *worktree)
9341 return open_fileindex(fileindex, fileindex_path, worktree);
9344 const struct got_error *
9345 got_worktree_patch_check_path(const char *old, const char *new,
9346 char **oldpath, char **newpath, struct got_worktree *worktree,
9347 struct got_repository *repo, struct got_fileindex *fileindex)
9349 const struct got_error *err = NULL;
9350 int file_renamed = 0;
9351 unsigned char status_old, staged_status_old;
9352 unsigned char status_new, staged_status_new;
9354 *oldpath = NULL;
9355 *newpath = NULL;
9357 err = patch_check_path(old != NULL ? old : new, oldpath,
9358 &status_old, &staged_status_old, fileindex, worktree, repo);
9359 if (err)
9360 goto done;
9362 err = patch_check_path(new != NULL ? new : old, newpath,
9363 &status_new, &staged_status_new, fileindex, worktree, repo);
9364 if (err)
9365 goto done;
9367 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9368 file_renamed = 1;
9370 if (old != NULL && new == NULL)
9371 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9372 else if (file_renamed) {
9373 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9374 if (err == NULL)
9375 err = patch_can_add(*newpath, status_new);
9376 } else if (old == NULL)
9377 err = patch_can_add(*newpath, status_new);
9378 else
9379 err = patch_can_edit(*newpath, status_new, staged_status_new);
9381 done:
9382 if (err) {
9383 free(*oldpath);
9384 *oldpath = NULL;
9385 free(*newpath);
9386 *newpath = NULL;
9388 return err;
9391 const struct got_error *
9392 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9393 struct got_worktree *worktree, struct got_fileindex *fileindex,
9394 got_worktree_checkout_cb progress_cb, void *progress_arg)
9396 struct schedule_addition_args saa;
9398 memset(&saa, 0, sizeof(saa));
9399 saa.worktree = worktree;
9400 saa.fileindex = fileindex;
9401 saa.progress_cb = progress_cb;
9402 saa.progress_arg = progress_arg;
9403 saa.repo = repo;
9405 return worktree_status(worktree, path, fileindex, repo,
9406 schedule_addition, &saa, NULL, NULL, 1, 0);
9409 const struct got_error *
9410 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9411 struct got_worktree *worktree, struct got_fileindex *fileindex,
9412 got_worktree_delete_cb progress_cb, void *progress_arg)
9414 struct schedule_deletion_args sda;
9416 memset(&sda, 0, sizeof(sda));
9417 sda.worktree = worktree;
9418 sda.fileindex = fileindex;
9419 sda.progress_cb = progress_cb;
9420 sda.progress_arg = progress_arg;
9421 sda.repo = repo;
9422 sda.delete_local_mods = 0;
9423 sda.keep_on_disk = 0;
9424 sda.ignore_missing_paths = 0;
9425 sda.status_codes = NULL;
9427 return worktree_status(worktree, path, fileindex, repo,
9428 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9431 const struct got_error *
9432 got_worktree_patch_complete(struct got_fileindex *fileindex,
9433 const char *fileindex_path)
9435 const struct got_error *err = NULL;
9437 err = sync_fileindex(fileindex, fileindex_path);
9438 got_fileindex_free(fileindex);
9440 return err;