Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
71 {
72 const struct got_error *err = NULL;
73 char *path;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
79 free(path);
80 return err;
81 }
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
85 {
86 const struct got_error *err = NULL;
87 FILE *tmpfile = NULL;
88 char *tmppath = NULL;
89 char *path = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
93 path = NULL;
94 goto done;
95 }
97 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
98 if (err)
99 goto done;
101 if (content) {
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
105 goto done;
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
111 unlink(tmppath);
112 goto done;
115 done:
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
118 free(tmppath);
119 return err;
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
126 char *refstr = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
130 if (refstr == NULL)
131 return got_error_from_errno("got_ref_to_str");
132 } else {
133 refstr = strdup(got_ref_get_name(head_ref));
134 if (refstr == NULL)
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 free(refstr);
139 return err;
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
148 uuid_t uuid;
149 uint32_t uuid_status;
150 int obj_type;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
159 goto done;
162 err = got_ref_resolve(&commit_id, repo, head_ref);
163 if (err)
164 return err;
165 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (err)
167 return err;
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
179 goto done;
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
185 goto done;
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
189 goto done;
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
194 if (err)
195 goto done;
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
199 if (err)
200 goto done;
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
204 if (err)
205 goto done;
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
209 if (err)
210 goto done;
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 if (err)
213 goto done;
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
218 if (err)
219 goto done;
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
224 if (err)
225 goto done;
227 /* Generate UUID. */
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
231 goto done;
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
236 goto done;
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
239 if (err)
240 goto done;
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 if (err)
249 goto done;
251 done:
252 free(commit_id);
253 free(path_got);
254 free(formatstr);
255 free(absprefix);
256 free(basestr);
257 free(uuidstr);
258 return err;
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
273 free(absprefix);
274 return NULL;
277 const char *
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
293 path_got = NULL;
294 goto done;
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
300 goto done;
303 err = write_head_ref(path_got, head_ref);
304 if (err)
305 goto done;
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
309 done:
310 free(path_got);
311 if (err)
312 free(head_ref_name);
313 return err;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
328 char *id_str = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
334 path_got = NULL;
335 goto done;
338 err = got_object_open(&obj, repo, commit_id);
339 if (err)
340 return err;
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
344 goto done;
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
349 if (err)
350 goto done;
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
352 if (err)
353 goto done;
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
359 goto done;
361 done:
362 if (obj)
363 got_object_close(obj);
364 free(id_str);
365 free(path_got);
366 return err;
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
382 return NULL;
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
389 char *abspath;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 struct stat sb;
397 err = NULL;
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
405 free(abspath);
406 return err;
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
413 uint8_t fbuf1[8192];
414 uint8_t fbuf2[8192];
415 size_t flen1 = 0, flen2 = 0;
417 *same = 1;
419 for (;;) {
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
423 break;
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
428 break;
430 if (flen1 == 0) {
431 if (flen2 != 0)
432 *same = 0;
433 break;
434 } else if (flen2 == 0) {
435 if (flen1 != 0)
436 *same = 0;
437 break;
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
440 *same = 0;
441 break;
443 } else {
444 *same = 0;
445 break;
449 return err;
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
455 struct stat sb;
456 size_t size1, size2;
458 *same = 1;
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
462 size1 = sb.st_size;
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
466 size2 = sb.st_size;
468 if (size1 != size2) {
469 *same = 0;
470 return NULL;
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
484 uint8_t fbuf[65536];
485 size_t flen;
486 ssize_t outlen;
488 *outsize = 0;
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
493 for (;;) {
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 if (flen == 0) {
496 if (ferror(f))
497 return got_error_from_errno("fread");
498 if (feof(f))
499 break;
501 outlen = write(outfd, fbuf, flen);
502 if (outlen == -1)
503 return got_error_from_errno("write");
504 if (outlen != flen)
505 return got_error(GOT_ERR_IO);
506 *outsize += outlen;
509 return NULL;
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
526 *overlapcnt = 0;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 if (err)
530 return err;
532 if (same_content)
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
536 if (err)
537 return err;
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 if (err)
541 return err;
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
545 *overlapcnt = 1;
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
552 goto done;
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
556 goto done;
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 base_path_orig, "");
560 if (err)
561 goto done;
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv, "");
564 if (err)
565 goto done;
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2, "");
568 if (err)
569 goto done;
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 if (err)
572 goto done;
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 if (err)
575 goto done;
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (err)
578 goto done;
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
582 goto done;
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
588 path_deriv) < 0) {
589 err = got_error_from_errno("dprintf");
590 goto done;
592 if (size_orig > 0) {
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
597 path_orig) < 0) {
598 err = got_error_from_errno("dprintf");
599 goto done;
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
604 path_deriv2,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
609 goto done;
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 done:
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 err == NULL)
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
625 free(path_orig);
626 free(path_deriv);
627 free(path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
631 return err;
634 /*
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
640 */
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
650 int merged_fd = -1;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
653 int overlapcnt = 0;
654 char *parent = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
659 if (err)
660 return err;
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
664 goto done;
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
668 if (err)
669 goto done;
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 if (err) {
674 if (err->code != GOT_ERR_FILE_BINARY)
675 goto done;
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 ondisk_path);
679 if (err)
680 goto done;
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
685 if (err)
686 goto done;
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
690 goto done;
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
696 goto done;
698 merged_fd = -1;
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
703 f_merged);
704 if (err)
705 goto done;
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
710 goto done;
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
715 ondisk_path);
716 goto done;
718 done:
719 if (err) {
720 if (merged_path)
721 unlink(merged_path);
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
727 free(merged_path);
728 free(base_path);
729 free(parent);
730 return err;
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
735 size_t target_len)
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
742 ondisk_path);
743 return NULL;
746 /*
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
757 */
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 FILE *f = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
768 if (err)
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
774 goto done;
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
778 if (err)
779 goto done;
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
783 goto done;
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
796 goto done;
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
801 goto done;
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
805 goto done;
807 done:
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
810 free(path);
811 free(id_str);
812 free(label_deriv);
813 return err;
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
823 /*
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
828 */
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
838 struct stat sb;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
851 ondisk_path);
852 goto done;
854 ondisk_target[ondisk_len] = '\0';
856 if (blob_orig) {
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 if (err)
859 goto done;
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 path);
883 } else {
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
888 if (err)
889 goto done;
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 path);
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
897 if (err)
898 goto done;
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 path);
906 } else {
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 path);
917 done:
918 free(ancestor_target);
919 return err;
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
927 ssize_t target_len;
928 size_t n;
929 FILE *f;
931 *outfile = NULL;
933 f = got_opentemp();
934 if (f == NULL)
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
939 goto done;
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
944 goto done;
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
948 goto done;
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
952 goto done;
954 done:
955 if (err)
956 fclose(f);
957 else
958 *outfile = f;
959 return err;
962 /*
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
966 */
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
984 if (err)
985 return err;
987 if (blob_orig) {
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 parent) == -1) {
990 err = got_error_from_errno("asprintf");
991 base_path = NULL;
992 goto done;
995 err = got_opentemp_named(&blob_orig_path, &f_orig,
996 base_path, "");
997 if (err)
998 goto done;
999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 blob_orig);
1001 if (err)
1002 goto done;
1003 free(base_path);
1004 } else {
1006 * No common ancestor exists. This is an "add vs add" conflict
1007 * and we simply use an empty ancestor file to make both files
1008 * appear in the merged result in their entirety.
1010 f_orig = got_opentemp();
1011 if (f_orig == NULL) {
1012 err = got_error_from_errno("got_opentemp");
1013 goto done;
1017 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 err = got_error_from_errno("asprintf");
1019 base_path = NULL;
1020 goto done;
1023 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 if (err)
1025 goto done;
1026 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1027 blob_deriv);
1028 if (err)
1029 goto done;
1031 err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 if (err)
1033 goto done;
1034 if (asprintf(&label_deriv, "%s: commit %s",
1035 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 err = got_error_from_errno("asprintf");
1037 goto done;
1041 * In order the run a 3-way merge with a symlink we copy the symlink's
1042 * target path into a temporary file and use that file with diff3.
1044 if (S_ISLNK(st_mode)) {
1045 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1046 if (err)
1047 goto done;
1048 } else {
1049 int fd;
1050 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 if (fd == -1) {
1052 err = got_error_from_errno2("open", ondisk_path);
1053 goto done;
1055 f_deriv2 = fdopen(fd, "r");
1056 if (f_deriv2 == NULL) {
1057 err = got_error_from_errno2("fdopen", ondisk_path);
1058 close(fd);
1059 goto done;
1063 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 done:
1067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 err = got_error_from_errno("fclose");
1073 free(base_path);
1074 if (blob_orig_path) {
1075 unlink(blob_orig_path);
1076 free(blob_orig_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1082 free(id_str);
1083 free(label_deriv);
1084 free(parent);
1085 return err;
1088 static const struct got_error *
1089 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 int wt_fd, const char *path, struct got_object_id *blob_id)
1093 const struct got_error *err = NULL;
1094 struct got_fileindex_entry *new_ie;
1096 *new_iep = NULL;
1098 err = got_fileindex_entry_alloc(&new_ie, path);
1099 if (err)
1100 return err;
1102 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 blob_id->sha1, base_commit_id->sha1, 1);
1104 if (err)
1105 goto done;
1107 err = got_fileindex_entry_add(fileindex, new_ie);
1108 done:
1109 if (err)
1110 got_fileindex_entry_free(new_ie);
1111 else
1112 *new_iep = new_ie;
1113 return err;
1116 static mode_t
1117 get_ondisk_perms(int executable, mode_t st_mode)
1119 mode_t xbits = S_IXUSR;
1121 if (executable) {
1122 /* Map read bits to execute bits. */
1123 if (st_mode & S_IRGRP)
1124 xbits |= S_IXGRP;
1125 if (st_mode & S_IROTH)
1126 xbits |= S_IXOTH;
1127 return st_mode | xbits;
1130 return st_mode;
1133 static mode_t
1134 apply_umask(mode_t mode)
1136 mode_t um;
1138 um = umask(000);
1139 umask(um);
1140 return mode & ~um;
1143 /* forward declaration */
1144 static const struct got_error *
1145 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 const char *path, mode_t te_mode, mode_t st_mode,
1147 struct got_blob_object *blob, int restoring_missing_file,
1148 int reverting_versioned_file, int installing_bad_symlink,
1149 int path_is_unversioned, struct got_repository *repo,
1150 got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 * This function assumes that the provided symlink target points at a
1154 * safe location in the work tree!
1156 static const struct got_error *
1157 replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 const char *target_path, size_t target_len)
1160 const struct got_error *err = NULL;
1161 ssize_t elen;
1162 char etarget[PATH_MAX];
1163 int fd;
1165 *did_something = 0;
1168 * "Bad" symlinks (those pointing outside the work tree or into the
1169 * .got directory) are installed in the work tree as a regular file
1170 * which contains the bad symlink target path.
1171 * The new symlink target has already been checked for safety by our
1172 * caller. If we can successfully open a regular file then we simply
1173 * replace this file with a symlink below.
1175 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 if (fd == -1) {
1177 if (!got_err_open_nofollow_on_symlink())
1178 return got_error_from_errno2("open", ondisk_path);
1180 /* We are updating an existing on-disk symlink. */
1181 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 if (elen == -1)
1183 return got_error_from_errno2("readlink", ondisk_path);
1185 if (elen == target_len &&
1186 memcmp(etarget, target_path, target_len) == 0)
1187 return NULL; /* nothing to do */
1190 *did_something = 1;
1191 err = update_symlink(ondisk_path, target_path, target_len);
1192 if (fd != -1 && close(fd) == -1 && err == NULL)
1193 err = got_error_from_errno2("close", ondisk_path);
1194 return err;
1197 static const struct got_error *
1198 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 const struct got_error *err = NULL;
1202 char canonpath[PATH_MAX];
1203 char *path_got = NULL;
1205 *is_bad_symlink = 0;
1207 if (target_len >= sizeof(canonpath)) {
1208 *is_bad_symlink = 1;
1209 return NULL;
1213 * We do not use realpath(3) to resolve the symlink's target
1214 * path because we don't want to resolve symlinks recursively.
1215 * Instead we make the path absolute and then canonicalize it.
1216 * Relative symlink target lookup should begin at the directory
1217 * in which the blob object is being installed.
1219 if (!got_path_is_absolute(target_path)) {
1220 char *abspath, *parent;
1221 err = got_path_dirname(&parent, ondisk_path);
1222 if (err)
1223 return err;
1224 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 free(parent);
1226 return got_error_from_errno("asprintf");
1228 free(parent);
1229 if (strlen(abspath) >= sizeof(canonpath)) {
1230 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 free(abspath);
1232 return err;
1234 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 free(abspath);
1236 if (err)
1237 return err;
1238 } else {
1239 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 if (err)
1241 return err;
1244 /* Only allow symlinks pointing at paths within the work tree. */
1245 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 *is_bad_symlink = 1;
1247 return NULL;
1250 /* Do not allow symlinks pointing into the .got directory. */
1251 if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 GOT_WORKTREE_GOT_DIR) == -1)
1253 return got_error_from_errno("asprintf");
1254 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 *is_bad_symlink = 1;
1257 free(path_got);
1258 return NULL;
1261 static const struct got_error *
1262 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 int restoring_missing_file, int reverting_versioned_file,
1265 int path_is_unversioned, int allow_bad_symlinks,
1266 struct got_repository *repo,
1267 got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 const struct got_error *err = NULL;
1270 char target_path[PATH_MAX];
1271 size_t len, target_len = 0;
1272 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 *is_bad_symlink = 0;
1278 * Blob object content specifies the target path of the link.
1279 * If a symbolic link cannot be installed we instead create
1280 * a regular file which contains the link target path stored
1281 * in the blob object.
1283 do {
1284 err = got_object_blob_read_block(&len, blob);
1285 if (err)
1286 return err;
1288 if (len + target_len >= sizeof(target_path)) {
1289 /* Path too long; install as a regular file. */
1290 *is_bad_symlink = 1;
1291 got_object_blob_rewind(blob);
1292 return install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 restoring_missing_file, reverting_versioned_file,
1295 1, path_is_unversioned, repo, progress_cb,
1296 progress_arg);
1298 if (len > 0) {
1299 /* Skip blob object header first time around. */
1300 memcpy(target_path + target_len, buf + hdrlen,
1301 len - hdrlen);
1302 target_len += len - hdrlen;
1303 hdrlen = 0;
1305 } while (len != 0);
1306 target_path[target_len] = '\0';
1308 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 ondisk_path, worktree->root_path);
1310 if (err)
1311 return err;
1313 if (*is_bad_symlink && !allow_bad_symlinks) {
1314 /* install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 path_is_unversioned, repo, progress_cb, progress_arg);
1320 return err;
1323 if (symlink(target_path, ondisk_path) == -1) {
1324 if (errno == EEXIST) {
1325 int symlink_replaced;
1326 if (path_is_unversioned) {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_UNVERSIONED, path);
1329 return err;
1331 err = replace_existing_symlink(&symlink_replaced,
1332 ondisk_path, target_path, target_len);
1333 if (err)
1334 return err;
1335 if (progress_cb) {
1336 if (symlink_replaced) {
1337 err = (*progress_cb)(progress_arg,
1338 reverting_versioned_file ?
1339 GOT_STATUS_REVERT :
1340 GOT_STATUS_UPDATE, path);
1341 } else {
1342 err = (*progress_cb)(progress_arg,
1343 GOT_STATUS_EXISTS, path);
1346 return err; /* Nothing else to do. */
1349 if (errno == ENOENT) {
1350 char *parent;
1351 err = got_path_dirname(&parent, ondisk_path);
1352 if (err)
1353 return err;
1354 err = add_dir_on_disk(worktree, parent);
1355 free(parent);
1356 if (err)
1357 return err;
1359 * Retry, and fall through to error handling
1360 * below if this second attempt fails.
1362 if (symlink(target_path, ondisk_path) != -1) {
1363 err = NULL; /* success */
1364 return err;
1368 /* Handle errors from first or second creation attempt. */
1369 if (errno == ENAMETOOLONG) {
1370 /* bad target path; install as a regular file */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 err = install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file, 1,
1376 path_is_unversioned, repo,
1377 progress_cb, progress_arg);
1378 } else if (errno == ENOTDIR) {
1379 err = got_error_path(ondisk_path,
1380 GOT_ERR_FILE_OBSTRUCTED);
1381 } else {
1382 err = got_error_from_errno3("symlink",
1383 target_path, ondisk_path);
1385 } else if (progress_cb)
1386 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 return err;
1391 static const struct got_error *
1392 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 const char *path, mode_t te_mode, mode_t st_mode,
1394 struct got_blob_object *blob, int restoring_missing_file,
1395 int reverting_versioned_file, int installing_bad_symlink,
1396 int path_is_unversioned, struct got_repository *repo,
1397 got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 const struct got_error *err = NULL;
1400 int fd = -1;
1401 size_t len, hdrlen;
1402 int update = 0;
1403 char *tmppath = NULL;
1404 mode_t mode;
1406 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 O_CLOEXEC, mode);
1409 if (fd == -1) {
1410 if (errno == ENOENT) {
1411 char *parent;
1412 err = got_path_dirname(&parent, path);
1413 if (err)
1414 return err;
1415 err = add_dir_on_disk(worktree, parent);
1416 free(parent);
1417 if (err)
1418 return err;
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 mode);
1422 if (fd == -1)
1423 return got_error_from_errno2("open",
1424 ondisk_path);
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1429 goto done;
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1436 goto done;
1437 } else {
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1439 ondisk_path, "");
1440 if (err)
1441 goto done;
1442 update = 1;
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1446 tmppath);
1447 goto done;
1450 } else
1451 return got_error_from_errno2("open", ondisk_path);
1454 if (progress_cb) {
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 path);
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 path);
1461 else
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 if (err)
1465 goto done;
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1469 do {
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1472 if (err)
1473 break;
1474 if (len > 0) {
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 if (outlen == -1) {
1478 err = got_error_from_errno("write");
1479 goto done;
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1482 goto done;
1484 hdrlen = 0;
1486 } while (len != 0);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1490 goto done;
1493 if (update) {
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1496 goto done;
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1500 ondisk_path);
1501 goto done;
1503 free(tmppath);
1504 tmppath = NULL;
1507 done:
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1512 free(tmppath);
1513 return err;
1517 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 * conflict marker is found in newly added lines only.
1520 static const struct got_error *
1521 get_modified_file_content_status(unsigned char *status,
1522 struct got_blob_object *blob, const char *path, struct stat *sb,
1523 FILE *ondisk_file)
1525 const struct got_error *err, *free_err;
1526 const char *markers[3] = {
1527 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 GOT_DIFF_CONFLICT_MARKER_SEP,
1529 GOT_DIFF_CONFLICT_MARKER_END
1531 FILE *f1 = NULL;
1532 struct got_diffreg_result *diffreg_result = NULL;
1533 struct diff_result *r;
1534 int nchunks_parsed, n, i = 0, ln = 0;
1535 char *line = NULL;
1536 size_t linesize = 0;
1537 ssize_t linelen;
1539 if (*status != GOT_STATUS_MODIFY)
1540 return NULL;
1542 f1 = got_opentemp();
1543 if (f1 == NULL)
1544 return got_error_from_errno("got_opentemp");
1546 if (blob) {
1547 got_object_blob_rewind(blob);
1548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1549 if (err)
1550 goto done;
1553 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1555 if (err)
1556 goto done;
1558 r = diffreg_result->result;
1560 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 struct diff_chunk *c;
1562 struct diff_chunk_context cc = {};
1563 off_t pos;
1564 int clc, crc;
1567 * We can optimise a little by advancing straight
1568 * to the next chunk if this one has no added lines.
1570 c = diff_chunk_get(r, n);
1571 clc = diff_chunk_get_left_count(c);
1572 crc = diff_chunk_get_right_count(c);
1574 if (!crc || crc == clc) {
1575 nchunks_parsed = 1;
1576 continue; /* removed or unchanged lines */
1579 pos = diff_chunk_get_right_start_pos(c);
1580 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 err = got_ferror(ondisk_file, GOT_ERR_IO);
1582 goto done;
1585 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 ln = cc.right.start;
1588 while (ln < cc.right.end) {
1589 linelen = getline(&line, &linesize, ondisk_file);
1590 if (linelen == -1) {
1591 if (feof(ondisk_file))
1592 break;
1593 err = got_ferror(ondisk_file, GOT_ERR_IO);
1594 break;
1597 if (line && strncmp(line, markers[i],
1598 strlen(markers[i])) == 0) {
1599 if (strcmp(markers[i],
1600 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 *status = GOT_STATUS_CONFLICT;
1602 goto done;
1603 } else
1604 i++;
1606 ++ln;
1610 done:
1611 free(line);
1612 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 err = got_error_from_errno("fclose");
1614 free_err = got_diffreg_result_free(diffreg_result);
1615 if (err == NULL)
1616 err = free_err;
1618 return err;
1621 static int
1622 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1624 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1628 static int
1629 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1631 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 ie->size == (sb->st_size & 0xffffffff) &&
1636 !xbit_differs(ie, sb->st_mode));
1639 static unsigned char
1640 get_staged_status(struct got_fileindex_entry *ie)
1642 switch (got_fileindex_entry_stage_get(ie)) {
1643 case GOT_FILEIDX_STAGE_ADD:
1644 return GOT_STATUS_ADD;
1645 case GOT_FILEIDX_STAGE_DELETE:
1646 return GOT_STATUS_DELETE;
1647 case GOT_FILEIDX_STAGE_MODIFY:
1648 return GOT_STATUS_MODIFY;
1649 default:
1650 return GOT_STATUS_NO_CHANGE;
1654 static const struct got_error *
1655 get_symlink_modification_status(unsigned char *status,
1656 struct got_fileindex_entry *ie, const char *abspath,
1657 int dirfd, const char *de_name, struct got_blob_object *blob)
1659 const struct got_error *err = NULL;
1660 char target_path[PATH_MAX];
1661 char etarget[PATH_MAX];
1662 ssize_t elen;
1663 size_t len, target_len = 0;
1664 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1667 *status = GOT_STATUS_NO_CHANGE;
1669 /* Blob object content specifies the target path of the link. */
1670 do {
1671 err = got_object_blob_read_block(&len, blob);
1672 if (err)
1673 return err;
1674 if (len + target_len >= sizeof(target_path)) {
1676 * Should not happen. The blob contents were OK
1677 * when this symlink was installed.
1679 return got_error(GOT_ERR_NO_SPACE);
1681 if (len > 0) {
1682 /* Skip blob object header first time around. */
1683 memcpy(target_path + target_len, buf + hdrlen,
1684 len - hdrlen);
1685 target_len += len - hdrlen;
1686 hdrlen = 0;
1688 } while (len != 0);
1689 target_path[target_len] = '\0';
1691 if (dirfd != -1) {
1692 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1693 if (elen == -1)
1694 return got_error_from_errno2("readlinkat", abspath);
1695 } else {
1696 elen = readlink(abspath, etarget, sizeof(etarget));
1697 if (elen == -1)
1698 return got_error_from_errno2("readlink", abspath);
1701 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 *status = GOT_STATUS_MODIFY;
1704 return NULL;
1707 static const struct got_error *
1708 get_file_status(unsigned char *status, struct stat *sb,
1709 struct got_fileindex_entry *ie, const char *abspath,
1710 int dirfd, const char *de_name, struct got_repository *repo)
1712 const struct got_error *err = NULL;
1713 struct got_object_id id;
1714 size_t hdrlen;
1715 int fd = -1, fd1 = -1;
1716 FILE *f = NULL;
1717 uint8_t fbuf[8192];
1718 struct got_blob_object *blob = NULL;
1719 size_t flen, blen;
1720 unsigned char staged_status;
1722 staged_status = get_staged_status(ie);
1723 *status = GOT_STATUS_NO_CHANGE;
1724 memset(sb, 0, sizeof(*sb));
1727 * Whenever the caller provides a directory descriptor and a
1728 * directory entry name for the file, use them! This prevents
1729 * race conditions if filesystem paths change beneath our feet.
1731 if (dirfd != -1) {
1732 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 if (errno == ENOENT) {
1734 if (got_fileindex_entry_has_file_on_disk(ie))
1735 *status = GOT_STATUS_MISSING;
1736 else
1737 *status = GOT_STATUS_DELETE;
1738 goto done;
1740 err = got_error_from_errno2("fstatat", abspath);
1741 goto done;
1743 } else {
1744 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 if (fd == -1 && errno != ENOENT &&
1746 !got_err_open_nofollow_on_symlink())
1747 return got_error_from_errno2("open", abspath);
1748 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 if (lstat(abspath, sb) == -1)
1750 return got_error_from_errno2("lstat", abspath);
1751 } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 if (errno == ENOENT) {
1753 if (got_fileindex_entry_has_file_on_disk(ie))
1754 *status = GOT_STATUS_MISSING;
1755 else
1756 *status = GOT_STATUS_DELETE;
1757 goto done;
1759 err = got_error_from_errno2("fstat", abspath);
1760 goto done;
1764 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 *status = GOT_STATUS_OBSTRUCTED;
1766 goto done;
1769 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 *status = GOT_STATUS_DELETE;
1771 goto done;
1772 } else if (!got_fileindex_entry_has_blob(ie) &&
1773 staged_status != GOT_STATUS_ADD) {
1774 *status = GOT_STATUS_ADD;
1775 goto done;
1778 if (!stat_info_differs(ie, sb))
1779 goto done;
1781 if (S_ISLNK(sb->st_mode) &&
1782 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 *status = GOT_STATUS_MODIFY;
1784 goto done;
1787 if (staged_status == GOT_STATUS_MODIFY ||
1788 staged_status == GOT_STATUS_ADD)
1789 got_fileindex_entry_get_staged_blob_id(&id, ie);
1790 else
1791 got_fileindex_entry_get_blob_id(&id, ie);
1793 fd1 = got_opentempfd();
1794 if (fd1 == -1) {
1795 err = got_error_from_errno("got_opentempfd");
1796 goto done;
1798 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1799 if (err)
1800 goto done;
1802 if (S_ISLNK(sb->st_mode)) {
1803 err = get_symlink_modification_status(status, ie,
1804 abspath, dirfd, de_name, blob);
1805 goto done;
1808 if (dirfd != -1) {
1809 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1810 if (fd == -1) {
1811 err = got_error_from_errno2("openat", abspath);
1812 goto done;
1816 f = fdopen(fd, "r");
1817 if (f == NULL) {
1818 err = got_error_from_errno2("fdopen", abspath);
1819 goto done;
1821 fd = -1;
1822 hdrlen = got_object_blob_get_hdrlen(blob);
1823 for (;;) {
1824 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 err = got_object_blob_read_block(&blen, blob);
1826 if (err)
1827 goto done;
1828 /* Skip length of blob object header first time around. */
1829 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 if (flen == 0 && ferror(f)) {
1831 err = got_error_from_errno("fread");
1832 goto done;
1834 if (blen - hdrlen == 0) {
1835 if (flen != 0)
1836 *status = GOT_STATUS_MODIFY;
1837 break;
1838 } else if (flen == 0) {
1839 if (blen - hdrlen != 0)
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1842 } else if (blen - hdrlen == flen) {
1843 /* Skip blob object header first time around. */
1844 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 *status = GOT_STATUS_MODIFY;
1846 break;
1848 } else {
1849 *status = GOT_STATUS_MODIFY;
1850 break;
1852 hdrlen = 0;
1855 if (*status == GOT_STATUS_MODIFY) {
1856 rewind(f);
1857 err = get_modified_file_content_status(status, blob, ie->path,
1858 sb, f);
1859 } else if (xbit_differs(ie, sb->st_mode))
1860 *status = GOT_STATUS_MODE_CHANGE;
1861 done:
1862 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 err = got_error_from_errno("close");
1864 if (blob)
1865 got_object_blob_close(blob);
1866 if (f != NULL && fclose(f) == EOF && err == NULL)
1867 err = got_error_from_errno2("fclose", abspath);
1868 if (fd != -1 && close(fd) == -1 && err == NULL)
1869 err = got_error_from_errno2("close", abspath);
1870 return err;
1874 * Update timestamps in the file index if a file is unmodified and
1875 * we had to run a full content comparison to find out.
1877 static const struct got_error *
1878 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 struct got_fileindex_entry *ie, struct stat *sb)
1881 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 return got_fileindex_entry_update(ie, wt_fd, path,
1883 ie->blob_sha1, ie->commit_sha1, 1);
1885 return NULL;
1888 static const struct got_error *
1889 update_blob(struct got_worktree *worktree,
1890 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1891 struct got_tree_entry *te, const char *path,
1892 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1893 void *progress_arg)
1895 const struct got_error *err = NULL;
1896 struct got_blob_object *blob = NULL;
1897 char *ondisk_path = NULL;
1898 unsigned char status = GOT_STATUS_NO_CHANGE;
1899 struct stat sb;
1900 int fd1 = -1, fd2 = -1;
1902 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 return got_error_from_errno("asprintf");
1905 if (ie) {
1906 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1908 goto done;
1910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1911 repo);
1912 if (err)
1913 goto done;
1914 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 sb.st_mode = got_fileindex_perms_to_st(ie);
1916 } else {
1917 if (stat(ondisk_path, &sb) == -1) {
1918 if (errno != ENOENT) {
1919 err = got_error_from_errno2("stat",
1920 ondisk_path);
1921 goto done;
1923 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1924 status = GOT_STATUS_UNVERSIONED;
1925 } else {
1926 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1927 status = GOT_STATUS_UNVERSIONED;
1928 else
1929 status = GOT_STATUS_OBSTRUCTED;
1933 if (status == GOT_STATUS_OBSTRUCTED) {
1934 if (ie)
1935 got_fileindex_entry_mark_skipped(ie);
1936 err = (*progress_cb)(progress_arg, status, path);
1937 goto done;
1939 if (status == GOT_STATUS_CONFLICT) {
1940 if (ie)
1941 got_fileindex_entry_mark_skipped(ie);
1942 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1943 path);
1944 goto done;
1947 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1948 (S_ISLNK(te->mode) ||
1949 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1951 * This is a regular file or an installed bad symlink.
1952 * If the file index indicates that this file is already
1953 * up-to-date with respect to the repository we can skip
1954 * updating contents of this file.
1956 if (got_fileindex_entry_has_commit(ie) &&
1957 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1958 SHA1_DIGEST_LENGTH) == 0) {
1959 /* Same commit. */
1960 err = sync_timestamps(worktree->root_fd,
1961 path, status, ie, &sb);
1962 if (err)
1963 goto done;
1964 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1965 path);
1966 goto done;
1968 if (got_fileindex_entry_has_blob(ie) &&
1969 memcmp(ie->blob_sha1, te->id.sha1,
1970 SHA1_DIGEST_LENGTH) == 0) {
1971 /* Different commit but the same blob. */
1972 err = sync_timestamps(worktree->root_fd,
1973 path, status, ie, &sb);
1974 if (err)
1975 goto done;
1976 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1977 path);
1978 goto done;
1982 fd1 = got_opentempfd();
1983 if (fd1 == -1) {
1984 err = got_error_from_errno("got_opentempfd");
1985 goto done;
1987 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1988 if (err)
1989 goto done;
1991 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1992 int update_timestamps;
1993 struct got_blob_object *blob2 = NULL;
1994 char *label_orig = NULL;
1995 if (got_fileindex_entry_has_blob(ie)) {
1996 fd2 = got_opentempfd();
1997 if (fd2 == -1) {
1998 err = got_error_from_errno("got_opentempfd");
1999 goto done;
2001 struct got_object_id id2;
2002 got_fileindex_entry_get_blob_id(&id2, ie);
2003 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2004 fd2);
2005 if (err)
2006 goto done;
2008 if (got_fileindex_entry_has_commit(ie)) {
2009 char id_str[SHA1_DIGEST_STRING_LENGTH];
2010 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2011 sizeof(id_str)) == NULL) {
2012 err = got_error_path(id_str,
2013 GOT_ERR_BAD_OBJ_ID_STR);
2014 goto done;
2016 if (asprintf(&label_orig, "%s: commit %s",
2017 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2018 err = got_error_from_errno("asprintf");
2019 goto done;
2022 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2023 char *link_target;
2024 err = got_object_blob_read_to_str(&link_target, blob);
2025 if (err)
2026 goto done;
2027 err = merge_symlink(worktree, blob2, ondisk_path, path,
2028 label_orig, link_target, worktree->base_commit_id,
2029 repo, progress_cb, progress_arg);
2030 free(link_target);
2031 } else {
2032 err = merge_blob(&update_timestamps, worktree, blob2,
2033 ondisk_path, path, sb.st_mode, label_orig, blob,
2034 worktree->base_commit_id, repo,
2035 progress_cb, progress_arg);
2037 free(label_orig);
2038 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2039 err = got_error_from_errno("close");
2040 goto done;
2042 if (blob2)
2043 got_object_blob_close(blob2);
2044 if (err)
2045 goto done;
2047 * Do not update timestamps of files with local changes.
2048 * Otherwise, a future status walk would treat them as
2049 * unmodified files again.
2051 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2052 blob->id.sha1, worktree->base_commit_id->sha1,
2053 update_timestamps);
2054 } else if (status == GOT_STATUS_MODE_CHANGE) {
2055 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2056 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2057 } else if (status == GOT_STATUS_DELETE) {
2058 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2059 if (err)
2060 goto done;
2061 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2062 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2063 if (err)
2064 goto done;
2065 } else {
2066 int is_bad_symlink = 0;
2067 if (S_ISLNK(te->mode)) {
2068 err = install_symlink(&is_bad_symlink, worktree,
2069 ondisk_path, path, blob,
2070 status == GOT_STATUS_MISSING, 0,
2071 status == GOT_STATUS_UNVERSIONED, 0,
2072 repo, progress_cb, progress_arg);
2073 } else {
2074 err = install_blob(worktree, ondisk_path, path,
2075 te->mode, sb.st_mode, blob,
2076 status == GOT_STATUS_MISSING, 0, 0,
2077 status == GOT_STATUS_UNVERSIONED, repo,
2078 progress_cb, progress_arg);
2080 if (err)
2081 goto done;
2083 if (ie) {
2084 err = got_fileindex_entry_update(ie,
2085 worktree->root_fd, path, blob->id.sha1,
2086 worktree->base_commit_id->sha1, 1);
2087 } else {
2088 err = create_fileindex_entry(&ie, fileindex,
2089 worktree->base_commit_id, worktree->root_fd, path,
2090 &blob->id);
2092 if (err)
2093 goto done;
2095 if (is_bad_symlink) {
2096 got_fileindex_entry_filetype_set(ie,
2097 GOT_FILEIDX_MODE_BAD_SYMLINK);
2101 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2102 err = got_error_from_errno("close");
2103 goto done;
2105 got_object_blob_close(blob);
2106 done:
2107 free(ondisk_path);
2108 return err;
2111 static const struct got_error *
2112 remove_ondisk_file(const char *root_path, const char *path)
2114 const struct got_error *err = NULL;
2115 char *ondisk_path = NULL, *parent = NULL;
2117 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2118 return got_error_from_errno("asprintf");
2120 if (unlink(ondisk_path) == -1) {
2121 if (errno != ENOENT)
2122 err = got_error_from_errno2("unlink", ondisk_path);
2123 } else {
2124 size_t root_len = strlen(root_path);
2125 err = got_path_dirname(&parent, ondisk_path);
2126 if (err)
2127 goto done;
2128 while (got_path_cmp(parent, root_path,
2129 strlen(parent), root_len) != 0) {
2130 free(ondisk_path);
2131 ondisk_path = parent;
2132 parent = NULL;
2133 if (rmdir(ondisk_path) == -1) {
2134 if (errno != ENOTEMPTY)
2135 err = got_error_from_errno2("rmdir",
2136 ondisk_path);
2137 break;
2139 err = got_path_dirname(&parent, ondisk_path);
2140 if (err)
2141 break;
2144 done:
2145 free(ondisk_path);
2146 free(parent);
2147 return err;
2150 static const struct got_error *
2151 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2152 struct got_fileindex_entry *ie, struct got_repository *repo,
2153 got_worktree_checkout_cb progress_cb, void *progress_arg)
2155 const struct got_error *err = NULL;
2156 unsigned char status;
2157 struct stat sb;
2158 char *ondisk_path;
2160 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2161 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2163 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2164 == -1)
2165 return got_error_from_errno("asprintf");
2167 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2168 if (err)
2169 goto done;
2171 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2172 char ondisk_target[PATH_MAX];
2173 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2174 sizeof(ondisk_target));
2175 if (ondisk_len == -1) {
2176 err = got_error_from_errno2("readlink", ondisk_path);
2177 goto done;
2179 ondisk_target[ondisk_len] = '\0';
2180 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2181 NULL, NULL, /* XXX pass common ancestor info? */
2182 ondisk_target, ondisk_path);
2183 if (err)
2184 goto done;
2185 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2186 ie->path);
2187 goto done;
2190 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2191 status == GOT_STATUS_ADD) {
2192 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2193 if (err)
2194 goto done;
2196 * Preserve the working file and change the deleted blob's
2197 * entry into a schedule-add entry.
2199 err = got_fileindex_entry_update(ie, worktree->root_fd,
2200 ie->path, NULL, NULL, 0);
2201 } else {
2202 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2203 if (err)
2204 goto done;
2205 if (status == GOT_STATUS_NO_CHANGE) {
2206 err = remove_ondisk_file(worktree->root_path, ie->path);
2207 if (err)
2208 goto done;
2210 got_fileindex_entry_remove(fileindex, ie);
2212 done:
2213 free(ondisk_path);
2214 return err;
2217 struct diff_cb_arg {
2218 struct got_fileindex *fileindex;
2219 struct got_worktree *worktree;
2220 struct got_repository *repo;
2221 got_worktree_checkout_cb progress_cb;
2222 void *progress_arg;
2223 got_cancel_cb cancel_cb;
2224 void *cancel_arg;
2227 static const struct got_error *
2228 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2229 struct got_tree_entry *te, const char *parent_path)
2231 struct diff_cb_arg *a = arg;
2233 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2234 return got_error(GOT_ERR_CANCELLED);
2236 return update_blob(a->worktree, a->fileindex, ie, te,
2237 ie->path, a->repo, a->progress_cb, a->progress_arg);
2240 static const struct got_error *
2241 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2243 struct diff_cb_arg *a = arg;
2245 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2246 return got_error(GOT_ERR_CANCELLED);
2248 return delete_blob(a->worktree, a->fileindex, ie,
2249 a->repo, a->progress_cb, a->progress_arg);
2252 static const struct got_error *
2253 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2255 struct diff_cb_arg *a = arg;
2256 const struct got_error *err;
2257 char *path;
2259 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2260 return got_error(GOT_ERR_CANCELLED);
2262 if (got_object_tree_entry_is_submodule(te))
2263 return NULL;
2265 if (asprintf(&path, "%s%s%s", parent_path,
2266 parent_path[0] ? "/" : "", te->name)
2267 == -1)
2268 return got_error_from_errno("asprintf");
2270 if (S_ISDIR(te->mode))
2271 err = add_dir_on_disk(a->worktree, path);
2272 else
2273 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2274 a->repo, a->progress_cb, a->progress_arg);
2276 free(path);
2277 return err;
2280 const struct got_error *
2281 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2283 uint32_t uuid_status;
2285 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2286 if (uuid_status != uuid_s_ok) {
2287 *uuidstr = NULL;
2288 return got_error_uuid(uuid_status, "uuid_to_string");
2291 return NULL;
2294 static const struct got_error *
2295 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2297 const struct got_error *err = NULL;
2298 char *uuidstr = NULL;
2300 *refname = NULL;
2302 err = got_worktree_get_uuid(&uuidstr, worktree);
2303 if (err)
2304 return err;
2306 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2307 err = got_error_from_errno("asprintf");
2308 *refname = NULL;
2310 free(uuidstr);
2311 return err;
2314 const struct got_error *
2315 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2316 const char *prefix)
2318 return get_ref_name(refname, worktree, prefix);
2321 const struct got_error *
2322 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2334 static const struct got_error *
2335 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2340 static const struct got_error *
2341 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2343 return get_ref_name(refname, worktree,
2344 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2347 static const struct got_error *
2348 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2350 return get_ref_name(refname, worktree,
2351 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2354 static const struct got_error *
2355 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2357 return get_ref_name(refname, worktree,
2358 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2361 static const struct got_error *
2362 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2364 return get_ref_name(refname, worktree,
2365 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2368 static const struct got_error *
2369 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree,
2372 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2375 static const struct got_error *
2376 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 return get_ref_name(refname, worktree,
2379 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2382 const struct got_error *
2383 got_worktree_get_histedit_script_path(char **path,
2384 struct got_worktree *worktree)
2386 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2387 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2388 *path = NULL;
2389 return got_error_from_errno("asprintf");
2391 return NULL;
2394 static const struct got_error *
2395 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2397 return get_ref_name(refname, worktree,
2398 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2401 static const struct got_error *
2402 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 return get_ref_name(refname, worktree,
2405 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2409 * Prevent Git's garbage collector from deleting our base commit by
2410 * setting a reference to our base commit's ID.
2412 static const struct got_error *
2413 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2415 const struct got_error *err = NULL;
2416 struct got_reference *ref = NULL;
2417 char *refname;
2419 err = got_worktree_get_base_ref_name(&refname, worktree);
2420 if (err)
2421 return err;
2423 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2424 if (err)
2425 goto done;
2427 err = got_ref_write(ref, repo);
2428 done:
2429 free(refname);
2430 if (ref)
2431 got_ref_close(ref);
2432 return err;
2435 static const struct got_error *
2436 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2438 const struct got_error *err = NULL;
2440 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2441 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2442 err = got_error_from_errno("asprintf");
2443 *fileindex_path = NULL;
2445 return err;
2449 static const struct got_error *
2450 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2451 struct got_worktree *worktree)
2453 const struct got_error *err = NULL;
2454 FILE *index = NULL;
2456 *fileindex_path = NULL;
2457 *fileindex = got_fileindex_alloc();
2458 if (*fileindex == NULL)
2459 return got_error_from_errno("got_fileindex_alloc");
2461 err = get_fileindex_path(fileindex_path, worktree);
2462 if (err)
2463 goto done;
2465 index = fopen(*fileindex_path, "rbe");
2466 if (index == NULL) {
2467 if (errno != ENOENT)
2468 err = got_error_from_errno2("fopen", *fileindex_path);
2469 } else {
2470 err = got_fileindex_read(*fileindex, index);
2471 if (fclose(index) == EOF && err == NULL)
2472 err = got_error_from_errno("fclose");
2474 done:
2475 if (err) {
2476 free(*fileindex_path);
2477 *fileindex_path = NULL;
2478 got_fileindex_free(*fileindex);
2479 *fileindex = NULL;
2481 return err;
2484 struct bump_base_commit_id_arg {
2485 struct got_object_id *base_commit_id;
2486 const char *path;
2487 size_t path_len;
2488 const char *entry_name;
2489 got_worktree_checkout_cb progress_cb;
2490 void *progress_arg;
2493 static const struct got_error *
2494 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2496 const struct got_error *err;
2497 struct bump_base_commit_id_arg *a = arg;
2499 if (a->entry_name) {
2500 if (strcmp(ie->path, a->path) != 0)
2501 return NULL;
2502 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2503 return NULL;
2505 if (got_fileindex_entry_was_skipped(ie))
2506 return NULL;
2508 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2509 SHA1_DIGEST_LENGTH) == 0)
2510 return NULL;
2512 if (a->progress_cb) {
2513 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2514 ie->path);
2515 if (err)
2516 return err;
2518 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2519 return NULL;
2522 /* Bump base commit ID of all files within an updated part of the work tree. */
2523 static const struct got_error *
2524 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2525 struct got_fileindex *fileindex,
2526 got_worktree_checkout_cb progress_cb, void *progress_arg)
2528 struct bump_base_commit_id_arg bbc_arg;
2530 bbc_arg.base_commit_id = worktree->base_commit_id;
2531 bbc_arg.entry_name = NULL;
2532 bbc_arg.path = "";
2533 bbc_arg.path_len = 0;
2534 bbc_arg.progress_cb = progress_cb;
2535 bbc_arg.progress_arg = progress_arg;
2537 return got_fileindex_for_each_entry_safe(fileindex,
2538 bump_base_commit_id, &bbc_arg);
2541 static const struct got_error *
2542 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2544 const struct got_error *err = NULL;
2545 char *new_fileindex_path = NULL;
2546 FILE *new_index = NULL;
2547 struct timespec timeout;
2549 err = got_opentemp_named(&new_fileindex_path, &new_index,
2550 fileindex_path, "");
2551 if (err)
2552 goto done;
2554 err = got_fileindex_write(fileindex, new_index);
2555 if (err)
2556 goto done;
2558 if (rename(new_fileindex_path, fileindex_path) != 0) {
2559 err = got_error_from_errno3("rename", new_fileindex_path,
2560 fileindex_path);
2561 unlink(new_fileindex_path);
2565 * Sleep for a short amount of time to ensure that files modified after
2566 * this program exits have a different time stamp from the one which
2567 * was recorded in the file index.
2569 timeout.tv_sec = 0;
2570 timeout.tv_nsec = 1;
2571 nanosleep(&timeout, NULL);
2572 done:
2573 if (new_index)
2574 fclose(new_index);
2575 free(new_fileindex_path);
2576 return err;
2579 static const struct got_error *
2580 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2581 struct got_object_id **tree_id, const char *wt_relpath,
2582 struct got_commit_object *base_commit, struct got_worktree *worktree,
2583 struct got_repository *repo)
2585 const struct got_error *err = NULL;
2586 struct got_object_id *id = NULL;
2587 char *in_repo_path = NULL;
2588 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2590 *entry_type = GOT_OBJ_TYPE_ANY;
2591 *tree_relpath = NULL;
2592 *tree_id = NULL;
2594 if (wt_relpath[0] == '\0') {
2595 /* Check out all files within the work tree. */
2596 *entry_type = GOT_OBJ_TYPE_TREE;
2597 *tree_relpath = strdup("");
2598 if (*tree_relpath == NULL) {
2599 err = got_error_from_errno("strdup");
2600 goto done;
2602 err = got_object_id_by_path(tree_id, repo, base_commit,
2603 worktree->path_prefix);
2604 if (err)
2605 goto done;
2606 return NULL;
2609 /* Check out a subset of files in the work tree. */
2611 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2612 is_root_wt ? "" : "/", wt_relpath) == -1) {
2613 err = got_error_from_errno("asprintf");
2614 goto done;
2617 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2618 if (err)
2619 goto done;
2621 free(in_repo_path);
2622 in_repo_path = NULL;
2624 err = got_object_get_type(entry_type, repo, id);
2625 if (err)
2626 goto done;
2628 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2629 /* Check out a single file. */
2630 if (strchr(wt_relpath, '/') == NULL) {
2631 /* Check out a single file in work tree's root dir. */
2632 in_repo_path = strdup(worktree->path_prefix);
2633 if (in_repo_path == NULL) {
2634 err = got_error_from_errno("strdup");
2635 goto done;
2637 *tree_relpath = strdup("");
2638 if (*tree_relpath == NULL) {
2639 err = got_error_from_errno("strdup");
2640 goto done;
2642 } else {
2643 /* Check out a single file in a subdirectory. */
2644 err = got_path_dirname(tree_relpath, wt_relpath);
2645 if (err)
2646 return err;
2647 if (asprintf(&in_repo_path, "%s%s%s",
2648 worktree->path_prefix, is_root_wt ? "" : "/",
2649 *tree_relpath) == -1) {
2650 err = got_error_from_errno("asprintf");
2651 goto done;
2654 err = got_object_id_by_path(tree_id, repo,
2655 base_commit, in_repo_path);
2656 } else {
2657 /* Check out all files within a subdirectory. */
2658 *tree_id = got_object_id_dup(id);
2659 if (*tree_id == NULL) {
2660 err = got_error_from_errno("got_object_id_dup");
2661 goto done;
2663 *tree_relpath = strdup(wt_relpath);
2664 if (*tree_relpath == NULL) {
2665 err = got_error_from_errno("strdup");
2666 goto done;
2669 done:
2670 free(id);
2671 free(in_repo_path);
2672 if (err) {
2673 *entry_type = GOT_OBJ_TYPE_ANY;
2674 free(*tree_relpath);
2675 *tree_relpath = NULL;
2676 free(*tree_id);
2677 *tree_id = NULL;
2679 return err;
2682 static const struct got_error *
2683 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2684 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2685 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2686 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2688 const struct got_error *err = NULL;
2689 struct got_commit_object *commit = NULL;
2690 struct got_tree_object *tree = NULL;
2691 struct got_fileindex_diff_tree_cb diff_cb;
2692 struct diff_cb_arg arg;
2694 err = ref_base_commit(worktree, repo);
2695 if (err) {
2696 if (!(err->code == GOT_ERR_ERRNO &&
2697 (errno == EACCES || errno == EROFS)))
2698 goto done;
2699 err = (*progress_cb)(progress_arg,
2700 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2701 if (err)
2702 return err;
2705 err = got_object_open_as_commit(&commit, repo,
2706 worktree->base_commit_id);
2707 if (err)
2708 goto done;
2710 err = got_object_open_as_tree(&tree, repo, tree_id);
2711 if (err)
2712 goto done;
2714 if (entry_name &&
2715 got_object_tree_find_entry(tree, entry_name) == NULL) {
2716 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2717 goto done;
2720 diff_cb.diff_old_new = diff_old_new;
2721 diff_cb.diff_old = diff_old;
2722 diff_cb.diff_new = diff_new;
2723 arg.fileindex = fileindex;
2724 arg.worktree = worktree;
2725 arg.repo = repo;
2726 arg.progress_cb = progress_cb;
2727 arg.progress_arg = progress_arg;
2728 arg.cancel_cb = cancel_cb;
2729 arg.cancel_arg = cancel_arg;
2730 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2731 entry_name, repo, &diff_cb, &arg);
2732 done:
2733 if (tree)
2734 got_object_tree_close(tree);
2735 if (commit)
2736 got_object_commit_close(commit);
2737 return err;
2740 const struct got_error *
2741 got_worktree_checkout_files(struct got_worktree *worktree,
2742 struct got_pathlist_head *paths, struct got_repository *repo,
2743 got_worktree_checkout_cb progress_cb, void *progress_arg,
2744 got_cancel_cb cancel_cb, void *cancel_arg)
2746 const struct got_error *err = NULL, *sync_err, *unlockerr;
2747 struct got_commit_object *commit = NULL;
2748 struct got_tree_object *tree = NULL;
2749 struct got_fileindex *fileindex = NULL;
2750 char *fileindex_path = NULL;
2751 struct got_pathlist_entry *pe;
2752 struct tree_path_data {
2753 STAILQ_ENTRY(tree_path_data) entry;
2754 struct got_object_id *tree_id;
2755 int entry_type;
2756 char *relpath;
2757 char *entry_name;
2758 } *tpd = NULL;
2759 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2761 STAILQ_INIT(&tree_paths);
2763 err = lock_worktree(worktree, LOCK_EX);
2764 if (err)
2765 return err;
2767 err = got_object_open_as_commit(&commit, repo,
2768 worktree->base_commit_id);
2769 if (err)
2770 goto done;
2772 /* Map all specified paths to in-repository trees. */
2773 TAILQ_FOREACH(pe, paths, entry) {
2774 tpd = malloc(sizeof(*tpd));
2775 if (tpd == NULL) {
2776 err = got_error_from_errno("malloc");
2777 goto done;
2780 err = find_tree_entry_for_checkout(&tpd->entry_type,
2781 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2782 worktree, repo);
2783 if (err) {
2784 free(tpd);
2785 goto done;
2788 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2789 err = got_path_basename(&tpd->entry_name, pe->path);
2790 if (err) {
2791 free(tpd->relpath);
2792 free(tpd->tree_id);
2793 free(tpd);
2794 goto done;
2796 } else
2797 tpd->entry_name = NULL;
2799 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2803 * Read the file index.
2804 * Checking out files is supposed to be an idempotent operation.
2805 * If the on-disk file index is incomplete we will try to complete it.
2807 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2808 if (err)
2809 goto done;
2811 tpd = STAILQ_FIRST(&tree_paths);
2812 TAILQ_FOREACH(pe, paths, entry) {
2813 struct bump_base_commit_id_arg bbc_arg;
2815 err = checkout_files(worktree, fileindex, tpd->relpath,
2816 tpd->tree_id, tpd->entry_name, repo,
2817 progress_cb, progress_arg, cancel_cb, cancel_arg);
2818 if (err)
2819 break;
2821 bbc_arg.base_commit_id = worktree->base_commit_id;
2822 bbc_arg.entry_name = tpd->entry_name;
2823 bbc_arg.path = pe->path;
2824 bbc_arg.path_len = pe->path_len;
2825 bbc_arg.progress_cb = progress_cb;
2826 bbc_arg.progress_arg = progress_arg;
2827 err = got_fileindex_for_each_entry_safe(fileindex,
2828 bump_base_commit_id, &bbc_arg);
2829 if (err)
2830 break;
2832 tpd = STAILQ_NEXT(tpd, entry);
2834 sync_err = sync_fileindex(fileindex, fileindex_path);
2835 if (sync_err && err == NULL)
2836 err = sync_err;
2837 done:
2838 free(fileindex_path);
2839 if (tree)
2840 got_object_tree_close(tree);
2841 if (commit)
2842 got_object_commit_close(commit);
2843 if (fileindex)
2844 got_fileindex_free(fileindex);
2845 while (!STAILQ_EMPTY(&tree_paths)) {
2846 tpd = STAILQ_FIRST(&tree_paths);
2847 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2848 free(tpd->relpath);
2849 free(tpd->tree_id);
2850 free(tpd);
2852 unlockerr = lock_worktree(worktree, LOCK_SH);
2853 if (unlockerr && err == NULL)
2854 err = unlockerr;
2855 return err;
2858 struct merge_file_cb_arg {
2859 struct got_worktree *worktree;
2860 struct got_fileindex *fileindex;
2861 got_worktree_checkout_cb progress_cb;
2862 void *progress_arg;
2863 got_cancel_cb cancel_cb;
2864 void *cancel_arg;
2865 const char *label_orig;
2866 struct got_object_id *commit_id2;
2867 int allow_bad_symlinks;
2870 static const struct got_error *
2871 merge_file_cb(void *arg, struct got_blob_object *blob1,
2872 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2873 struct got_object_id *id1, struct got_object_id *id2,
2874 const char *path1, const char *path2,
2875 mode_t mode1, mode_t mode2, struct got_repository *repo)
2877 static const struct got_error *err = NULL;
2878 struct merge_file_cb_arg *a = arg;
2879 struct got_fileindex_entry *ie;
2880 char *ondisk_path = NULL;
2881 struct stat sb;
2882 unsigned char status;
2883 int local_changes_subsumed;
2884 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2885 char *id_str = NULL, *label_deriv2 = NULL;
2887 if (blob1 && blob2) {
2888 ie = got_fileindex_entry_get(a->fileindex, path2,
2889 strlen(path2));
2890 if (ie == NULL)
2891 return (*a->progress_cb)(a->progress_arg,
2892 GOT_STATUS_MISSING, path2);
2894 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2895 path2) == -1)
2896 return got_error_from_errno("asprintf");
2898 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2899 repo);
2900 if (err)
2901 goto done;
2903 if (status == GOT_STATUS_DELETE) {
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_MERGE, path2);
2906 goto done;
2908 if (status != GOT_STATUS_NO_CHANGE &&
2909 status != GOT_STATUS_MODIFY &&
2910 status != GOT_STATUS_CONFLICT &&
2911 status != GOT_STATUS_ADD) {
2912 err = (*a->progress_cb)(a->progress_arg, status, path2);
2913 goto done;
2916 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2917 char *link_target2;
2918 err = got_object_blob_read_to_str(&link_target2, blob2);
2919 if (err)
2920 goto done;
2921 err = merge_symlink(a->worktree, blob1, ondisk_path,
2922 path2, a->label_orig, link_target2, a->commit_id2,
2923 repo, a->progress_cb, a->progress_arg);
2924 free(link_target2);
2925 } else {
2926 int fd;
2928 f_orig = got_opentemp();
2929 if (f_orig == NULL) {
2930 err = got_error_from_errno("got_opentemp");
2931 goto done;
2933 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2934 f_orig, blob1);
2935 if (err)
2936 goto done;
2938 f_deriv2 = got_opentemp();
2939 if (f_deriv2 == NULL)
2940 goto done;
2941 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2942 f_deriv2, blob2);
2943 if (err)
2944 goto done;
2946 fd = open(ondisk_path,
2947 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2948 if (fd == -1) {
2949 err = got_error_from_errno2("open",
2950 ondisk_path);
2951 goto done;
2953 f_deriv = fdopen(fd, "r");
2954 if (f_deriv == NULL) {
2955 err = got_error_from_errno2("fdopen",
2956 ondisk_path);
2957 close(fd);
2958 goto done;
2960 err = got_object_id_str(&id_str, a->commit_id2);
2961 if (err)
2962 goto done;
2963 if (asprintf(&label_deriv2, "%s: commit %s",
2964 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2965 err = got_error_from_errno("asprintf");
2966 goto done;
2968 err = merge_file(&local_changes_subsumed, a->worktree,
2969 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2970 mode2, a->label_orig, NULL, label_deriv2,
2971 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2972 a->progress_cb, a->progress_arg);
2974 } else if (blob1) {
2975 ie = got_fileindex_entry_get(a->fileindex, path1,
2976 strlen(path1));
2977 if (ie == NULL)
2978 return (*a->progress_cb)(a->progress_arg,
2979 GOT_STATUS_MISSING, path1);
2981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2982 path1) == -1)
2983 return got_error_from_errno("asprintf");
2985 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2986 repo);
2987 if (err)
2988 goto done;
2990 switch (status) {
2991 case GOT_STATUS_NO_CHANGE:
2992 err = (*a->progress_cb)(a->progress_arg,
2993 GOT_STATUS_DELETE, path1);
2994 if (err)
2995 goto done;
2996 err = remove_ondisk_file(a->worktree->root_path, path1);
2997 if (err)
2998 goto done;
2999 if (ie)
3000 got_fileindex_entry_mark_deleted_from_disk(ie);
3001 break;
3002 case GOT_STATUS_DELETE:
3003 case GOT_STATUS_MISSING:
3004 err = (*a->progress_cb)(a->progress_arg,
3005 GOT_STATUS_DELETE, path1);
3006 if (err)
3007 goto done;
3008 if (ie)
3009 got_fileindex_entry_mark_deleted_from_disk(ie);
3010 break;
3011 case GOT_STATUS_ADD: {
3012 struct got_object_id *id;
3013 FILE *blob1_f;
3014 off_t blob1_size;
3016 * Delete the added file only if its content already
3017 * exists in the repository.
3019 err = got_object_blob_file_create(&id, &blob1_f,
3020 &blob1_size, path1);
3021 if (err)
3022 goto done;
3023 if (got_object_id_cmp(id, id1) == 0) {
3024 err = (*a->progress_cb)(a->progress_arg,
3025 GOT_STATUS_DELETE, path1);
3026 if (err)
3027 goto done;
3028 err = remove_ondisk_file(a->worktree->root_path,
3029 path1);
3030 if (err)
3031 goto done;
3032 if (ie)
3033 got_fileindex_entry_remove(a->fileindex,
3034 ie);
3035 } else {
3036 err = (*a->progress_cb)(a->progress_arg,
3037 GOT_STATUS_CANNOT_DELETE, path1);
3039 if (fclose(blob1_f) == EOF && err == NULL)
3040 err = got_error_from_errno("fclose");
3041 free(id);
3042 if (err)
3043 goto done;
3044 break;
3046 case GOT_STATUS_MODIFY:
3047 case GOT_STATUS_CONFLICT:
3048 err = (*a->progress_cb)(a->progress_arg,
3049 GOT_STATUS_CANNOT_DELETE, path1);
3050 if (err)
3051 goto done;
3052 break;
3053 case GOT_STATUS_OBSTRUCTED:
3054 err = (*a->progress_cb)(a->progress_arg, status, path1);
3055 if (err)
3056 goto done;
3057 break;
3058 default:
3059 break;
3061 } else if (blob2) {
3062 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3063 path2) == -1)
3064 return got_error_from_errno("asprintf");
3065 ie = got_fileindex_entry_get(a->fileindex, path2,
3066 strlen(path2));
3067 if (ie) {
3068 err = get_file_status(&status, &sb, ie, ondisk_path,
3069 -1, NULL, repo);
3070 if (err)
3071 goto done;
3072 if (status != GOT_STATUS_NO_CHANGE &&
3073 status != GOT_STATUS_MODIFY &&
3074 status != GOT_STATUS_CONFLICT &&
3075 status != GOT_STATUS_ADD) {
3076 err = (*a->progress_cb)(a->progress_arg,
3077 status, path2);
3078 goto done;
3080 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3081 char *link_target2;
3082 err = got_object_blob_read_to_str(&link_target2,
3083 blob2);
3084 if (err)
3085 goto done;
3086 err = merge_symlink(a->worktree, NULL,
3087 ondisk_path, path2, a->label_orig,
3088 link_target2, a->commit_id2, repo,
3089 a->progress_cb, a->progress_arg);
3090 free(link_target2);
3091 } else if (S_ISREG(sb.st_mode)) {
3092 err = merge_blob(&local_changes_subsumed,
3093 a->worktree, NULL, ondisk_path, path2,
3094 sb.st_mode, a->label_orig, blob2,
3095 a->commit_id2, repo, a->progress_cb,
3096 a->progress_arg);
3097 } else {
3098 err = got_error_path(ondisk_path,
3099 GOT_ERR_FILE_OBSTRUCTED);
3101 if (err)
3102 goto done;
3103 if (status == GOT_STATUS_DELETE) {
3104 err = got_fileindex_entry_update(ie,
3105 a->worktree->root_fd, path2, blob2->id.sha1,
3106 a->worktree->base_commit_id->sha1, 0);
3107 if (err)
3108 goto done;
3110 } else {
3111 int is_bad_symlink = 0;
3112 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3113 if (S_ISLNK(mode2)) {
3114 err = install_symlink(&is_bad_symlink,
3115 a->worktree, ondisk_path, path2, blob2, 0,
3116 0, 1, a->allow_bad_symlinks, repo,
3117 a->progress_cb, a->progress_arg);
3118 } else {
3119 err = install_blob(a->worktree, ondisk_path, path2,
3120 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3121 a->progress_cb, a->progress_arg);
3123 if (err)
3124 goto done;
3125 err = got_fileindex_entry_alloc(&ie, path2);
3126 if (err)
3127 goto done;
3128 err = got_fileindex_entry_update(ie,
3129 a->worktree->root_fd, path2, NULL, NULL, 1);
3130 if (err) {
3131 got_fileindex_entry_free(ie);
3132 goto done;
3134 err = got_fileindex_entry_add(a->fileindex, ie);
3135 if (err) {
3136 got_fileindex_entry_free(ie);
3137 goto done;
3139 if (is_bad_symlink) {
3140 got_fileindex_entry_filetype_set(ie,
3141 GOT_FILEIDX_MODE_BAD_SYMLINK);
3145 done:
3146 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3147 err = got_error_from_errno("fclose");
3148 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3149 err = got_error_from_errno("fclose");
3150 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3151 err = got_error_from_errno("fclose");
3152 free(id_str);
3153 free(label_deriv2);
3154 free(ondisk_path);
3155 return err;
3158 static const struct got_error *
3159 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3161 struct got_worktree *worktree = arg;
3163 /* Reject merges into a work tree with mixed base commits. */
3164 if (got_fileindex_entry_has_commit(ie) &&
3165 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3166 SHA1_DIGEST_LENGTH) != 0)
3167 return got_error(GOT_ERR_MIXED_COMMITS);
3169 return NULL;
3172 struct check_merge_conflicts_arg {
3173 struct got_worktree *worktree;
3174 struct got_fileindex *fileindex;
3175 struct got_repository *repo;
3178 static const struct got_error *
3179 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3180 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3181 struct got_object_id *id1, struct got_object_id *id2,
3182 const char *path1, const char *path2,
3183 mode_t mode1, mode_t mode2, struct got_repository *repo)
3185 const struct got_error *err = NULL;
3186 struct check_merge_conflicts_arg *a = arg;
3187 unsigned char status;
3188 struct stat sb;
3189 struct got_fileindex_entry *ie;
3190 const char *path = path2 ? path2 : path1;
3191 struct got_object_id *id = id2 ? id2 : id1;
3192 char *ondisk_path;
3194 if (id == NULL)
3195 return NULL;
3197 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3198 if (ie == NULL)
3199 return NULL;
3201 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3202 == -1)
3203 return got_error_from_errno("asprintf");
3205 /* Reject merges into a work tree with conflicted files. */
3206 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3207 free(ondisk_path);
3208 if (err)
3209 return err;
3210 if (status == GOT_STATUS_CONFLICT)
3211 return got_error(GOT_ERR_CONFLICTS);
3213 return NULL;
3216 static const struct got_error *
3217 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3218 const char *fileindex_path, struct got_object_id *commit_id1,
3219 struct got_object_id *commit_id2, struct got_repository *repo,
3220 got_worktree_checkout_cb progress_cb, void *progress_arg,
3221 got_cancel_cb cancel_cb, void *cancel_arg)
3223 const struct got_error *err = NULL, *sync_err;
3224 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3225 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3226 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3227 struct check_merge_conflicts_arg cmc_arg;
3228 struct merge_file_cb_arg arg;
3229 char *label_orig = NULL;
3230 FILE *f1 = NULL, *f2 = NULL;
3231 int fd1 = -1, fd2 = -1;
3233 if (commit_id1) {
3234 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3235 if (err)
3236 goto done;
3237 err = got_object_id_by_path(&tree_id1, repo, commit1,
3238 worktree->path_prefix);
3239 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3240 goto done;
3242 if (tree_id1) {
3243 char *id_str;
3245 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3246 if (err)
3247 goto done;
3249 err = got_object_id_str(&id_str, commit_id1);
3250 if (err)
3251 goto done;
3253 if (asprintf(&label_orig, "%s: commit %s",
3254 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3255 err = got_error_from_errno("asprintf");
3256 free(id_str);
3257 goto done;
3259 free(id_str);
3261 f1 = got_opentemp();
3262 if (f1 == NULL) {
3263 err = got_error_from_errno("got_opentemp");
3264 goto done;
3268 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3269 if (err)
3270 goto done;
3272 err = got_object_id_by_path(&tree_id2, repo, commit2,
3273 worktree->path_prefix);
3274 if (err)
3275 goto done;
3277 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3278 if (err)
3279 goto done;
3281 f2 = got_opentemp();
3282 if (f2 == NULL) {
3283 err = got_error_from_errno("got_opentemp");
3284 goto done;
3287 fd1 = got_opentempfd();
3288 if (fd1 == -1) {
3289 err = got_error_from_errno("got_opentempfd");
3290 goto done;
3293 fd2 = got_opentempfd();
3294 if (fd2 == -1) {
3295 err = got_error_from_errno("got_opentempfd");
3296 goto done;
3299 cmc_arg.worktree = worktree;
3300 cmc_arg.fileindex = fileindex;
3301 cmc_arg.repo = repo;
3302 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3303 check_merge_conflicts, &cmc_arg, 0);
3304 if (err)
3305 goto done;
3307 arg.worktree = worktree;
3308 arg.fileindex = fileindex;
3309 arg.progress_cb = progress_cb;
3310 arg.progress_arg = progress_arg;
3311 arg.cancel_cb = cancel_cb;
3312 arg.cancel_arg = cancel_arg;
3313 arg.label_orig = label_orig;
3314 arg.commit_id2 = commit_id2;
3315 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3316 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3317 merge_file_cb, &arg, 1);
3318 sync_err = sync_fileindex(fileindex, fileindex_path);
3319 if (sync_err && err == NULL)
3320 err = sync_err;
3321 done:
3322 if (commit1)
3323 got_object_commit_close(commit1);
3324 if (commit2)
3325 got_object_commit_close(commit2);
3326 if (tree1)
3327 got_object_tree_close(tree1);
3328 if (tree2)
3329 got_object_tree_close(tree2);
3330 if (f1 && fclose(f1) == EOF && err == NULL)
3331 err = got_error_from_errno("fclose");
3332 if (f2 && fclose(f2) == EOF && err == NULL)
3333 err = got_error_from_errno("fclose");
3334 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3335 err = got_error_from_errno("close");
3336 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3337 err = got_error_from_errno("close");
3338 free(label_orig);
3339 return err;
3342 const struct got_error *
3343 got_worktree_merge_files(struct got_worktree *worktree,
3344 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3345 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3346 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3348 const struct got_error *err, *unlockerr;
3349 char *fileindex_path = NULL;
3350 struct got_fileindex *fileindex = NULL;
3352 err = lock_worktree(worktree, LOCK_EX);
3353 if (err)
3354 return err;
3356 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3357 if (err)
3358 goto done;
3360 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3361 worktree);
3362 if (err)
3363 goto done;
3365 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3366 commit_id2, repo, progress_cb, progress_arg,
3367 cancel_cb, cancel_arg);
3368 done:
3369 if (fileindex)
3370 got_fileindex_free(fileindex);
3371 free(fileindex_path);
3372 unlockerr = lock_worktree(worktree, LOCK_SH);
3373 if (unlockerr && err == NULL)
3374 err = unlockerr;
3375 return err;
3378 struct diff_dir_cb_arg {
3379 struct got_fileindex *fileindex;
3380 struct got_worktree *worktree;
3381 const char *status_path;
3382 size_t status_path_len;
3383 struct got_repository *repo;
3384 got_worktree_status_cb status_cb;
3385 void *status_arg;
3386 got_cancel_cb cancel_cb;
3387 void *cancel_arg;
3388 /* A pathlist containing per-directory pathlists of ignore patterns. */
3389 struct got_pathlist_head *ignores;
3390 int report_unchanged;
3391 int no_ignores;
3394 static const struct got_error *
3395 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3396 int dirfd, const char *de_name,
3397 got_worktree_status_cb status_cb, void *status_arg,
3398 struct got_repository *repo, int report_unchanged)
3400 const struct got_error *err = NULL;
3401 unsigned char status = GOT_STATUS_NO_CHANGE;
3402 unsigned char staged_status;
3403 struct stat sb;
3404 struct got_object_id blob_id, commit_id, staged_blob_id;
3405 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3406 struct got_object_id *staged_blob_idp = NULL;
3408 staged_status = get_staged_status(ie);
3409 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3410 if (err)
3411 return err;
3413 if (status == GOT_STATUS_NO_CHANGE &&
3414 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3415 return NULL;
3417 if (got_fileindex_entry_has_blob(ie))
3418 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3419 if (got_fileindex_entry_has_commit(ie))
3420 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3421 if (staged_status == GOT_STATUS_ADD ||
3422 staged_status == GOT_STATUS_MODIFY) {
3423 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3424 &staged_blob_id, ie);
3427 return (*status_cb)(status_arg, status, staged_status,
3428 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3431 static const struct got_error *
3432 status_old_new(void *arg, struct got_fileindex_entry *ie,
3433 struct dirent *de, const char *parent_path, int dirfd)
3435 const struct got_error *err = NULL;
3436 struct diff_dir_cb_arg *a = arg;
3437 char *abspath;
3439 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3440 return got_error(GOT_ERR_CANCELLED);
3442 if (got_path_cmp(parent_path, a->status_path,
3443 strlen(parent_path), a->status_path_len) != 0 &&
3444 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3445 return NULL;
3447 if (parent_path[0]) {
3448 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3449 parent_path, de->d_name) == -1)
3450 return got_error_from_errno("asprintf");
3451 } else {
3452 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3453 de->d_name) == -1)
3454 return got_error_from_errno("asprintf");
3457 err = report_file_status(ie, abspath, dirfd, de->d_name,
3458 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3459 free(abspath);
3460 return err;
3463 static const struct got_error *
3464 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3466 struct diff_dir_cb_arg *a = arg;
3467 struct got_object_id blob_id, commit_id;
3468 unsigned char status;
3470 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3471 return got_error(GOT_ERR_CANCELLED);
3473 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3474 return NULL;
3476 got_fileindex_entry_get_blob_id(&blob_id, ie);
3477 got_fileindex_entry_get_commit_id(&commit_id, ie);
3478 if (got_fileindex_entry_has_file_on_disk(ie))
3479 status = GOT_STATUS_MISSING;
3480 else
3481 status = GOT_STATUS_DELETE;
3482 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3483 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3486 static void
3487 free_ignores(struct got_pathlist_head *ignores)
3489 struct got_pathlist_entry *pe;
3491 TAILQ_FOREACH(pe, ignores, entry) {
3492 struct got_pathlist_head *ignorelist = pe->data;
3494 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3496 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3499 static const struct got_error *
3500 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3502 const struct got_error *err = NULL;
3503 struct got_pathlist_entry *pe = NULL;
3504 struct got_pathlist_head *ignorelist;
3505 char *line = NULL, *pattern, *dirpath = NULL;
3506 size_t linesize = 0;
3507 ssize_t linelen;
3509 ignorelist = calloc(1, sizeof(*ignorelist));
3510 if (ignorelist == NULL)
3511 return got_error_from_errno("calloc");
3512 TAILQ_INIT(ignorelist);
3514 while ((linelen = getline(&line, &linesize, f)) != -1) {
3515 if (linelen > 0 && line[linelen - 1] == '\n')
3516 line[linelen - 1] = '\0';
3518 /* Git's ignores may contain comments. */
3519 if (line[0] == '#')
3520 continue;
3522 /* Git's negated patterns are not (yet?) supported. */
3523 if (line[0] == '!')
3524 continue;
3526 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3527 line) == -1) {
3528 err = got_error_from_errno("asprintf");
3529 goto done;
3531 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3532 if (err)
3533 goto done;
3535 if (ferror(f)) {
3536 err = got_error_from_errno("getline");
3537 goto done;
3540 dirpath = strdup(path);
3541 if (dirpath == NULL) {
3542 err = got_error_from_errno("strdup");
3543 goto done;
3545 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3546 done:
3547 free(line);
3548 if (err || pe == NULL) {
3549 free(dirpath);
3550 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3552 return err;
3555 static int
3556 match_path(const char *pattern, size_t pattern_len, const char *path,
3557 int flags)
3559 char buf[PATH_MAX];
3562 * Trailing slashes signify directories.
3563 * Append a * to make such patterns conform to fnmatch rules.
3565 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3566 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3567 return FNM_NOMATCH; /* XXX */
3569 return fnmatch(buf, path, flags);
3572 return fnmatch(pattern, path, flags);
3575 static int
3576 match_ignores(struct got_pathlist_head *ignores, const char *path)
3578 struct got_pathlist_entry *pe;
3580 /* Handle patterns which match in all directories. */
3581 TAILQ_FOREACH(pe, ignores, entry) {
3582 struct got_pathlist_head *ignorelist = pe->data;
3583 struct got_pathlist_entry *pi;
3585 TAILQ_FOREACH(pi, ignorelist, entry) {
3586 const char *p;
3588 if (pi->path_len < 3 ||
3589 strncmp(pi->path, "**/", 3) != 0)
3590 continue;
3591 p = path;
3592 while (*p) {
3593 if (match_path(pi->path + 3,
3594 pi->path_len - 3, p,
3595 FNM_PATHNAME | FNM_LEADING_DIR)) {
3596 /* Retry in next directory. */
3597 while (*p && *p != '/')
3598 p++;
3599 while (*p == '/')
3600 p++;
3601 continue;
3603 return 1;
3609 * The ignores pathlist contains ignore lists from children before
3610 * parents, so we can find the most specific ignorelist by walking
3611 * ignores backwards.
3613 pe = TAILQ_LAST(ignores, got_pathlist_head);
3614 while (pe) {
3615 if (got_path_is_child(path, pe->path, pe->path_len)) {
3616 struct got_pathlist_head *ignorelist = pe->data;
3617 struct got_pathlist_entry *pi;
3618 TAILQ_FOREACH(pi, ignorelist, entry) {
3619 int flags = FNM_LEADING_DIR;
3620 if (strstr(pi->path, "/**/") == NULL)
3621 flags |= FNM_PATHNAME;
3622 if (match_path(pi->path, pi->path_len,
3623 path, flags))
3624 continue;
3625 return 1;
3628 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3631 return 0;
3634 static const struct got_error *
3635 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3636 const char *path, int dirfd, const char *ignores_filename)
3638 const struct got_error *err = NULL;
3639 char *ignorespath;
3640 int fd = -1;
3641 FILE *ignoresfile = NULL;
3643 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3644 path[0] ? "/" : "", ignores_filename) == -1)
3645 return got_error_from_errno("asprintf");
3647 if (dirfd != -1) {
3648 fd = openat(dirfd, ignores_filename,
3649 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3650 if (fd == -1) {
3651 if (errno != ENOENT && errno != EACCES)
3652 err = got_error_from_errno2("openat",
3653 ignorespath);
3654 } else {
3655 ignoresfile = fdopen(fd, "r");
3656 if (ignoresfile == NULL)
3657 err = got_error_from_errno2("fdopen",
3658 ignorespath);
3659 else {
3660 fd = -1;
3661 err = read_ignores(ignores, path, ignoresfile);
3664 } else {
3665 ignoresfile = fopen(ignorespath, "re");
3666 if (ignoresfile == NULL) {
3667 if (errno != ENOENT && errno != EACCES)
3668 err = got_error_from_errno2("fopen",
3669 ignorespath);
3670 } else
3671 err = read_ignores(ignores, path, ignoresfile);
3674 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3675 err = got_error_from_errno2("fclose", path);
3676 if (fd != -1 && close(fd) == -1 && err == NULL)
3677 err = got_error_from_errno2("close", path);
3678 free(ignorespath);
3679 return err;
3682 static const struct got_error *
3683 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3684 int dirfd)
3686 const struct got_error *err = NULL;
3687 struct diff_dir_cb_arg *a = arg;
3688 char *path = NULL;
3690 if (ignore != NULL)
3691 *ignore = 0;
3693 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3694 return got_error(GOT_ERR_CANCELLED);
3696 if (parent_path[0]) {
3697 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3698 return got_error_from_errno("asprintf");
3699 } else {
3700 path = de->d_name;
3703 if (de->d_type == DT_DIR) {
3704 if (!a->no_ignores && ignore != NULL &&
3705 match_ignores(a->ignores, path))
3706 *ignore = 1;
3707 } else if (!match_ignores(a->ignores, path) &&
3708 got_path_is_child(path, a->status_path, a->status_path_len))
3709 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3710 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3711 if (parent_path[0])
3712 free(path);
3713 return err;
3716 static const struct got_error *
3717 status_traverse(void *arg, const char *path, int dirfd)
3719 const struct got_error *err = NULL;
3720 struct diff_dir_cb_arg *a = arg;
3722 if (a->no_ignores)
3723 return NULL;
3725 err = add_ignores(a->ignores, a->worktree->root_path,
3726 path, dirfd, ".cvsignore");
3727 if (err)
3728 return err;
3730 err = add_ignores(a->ignores, a->worktree->root_path, path,
3731 dirfd, ".gitignore");
3733 return err;
3736 static const struct got_error *
3737 report_single_file_status(const char *path, const char *ondisk_path,
3738 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3739 void *status_arg, struct got_repository *repo, int report_unchanged,
3740 struct got_pathlist_head *ignores, int no_ignores)
3742 struct got_fileindex_entry *ie;
3743 struct stat sb;
3745 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3746 if (ie)
3747 return report_file_status(ie, ondisk_path, -1, NULL,
3748 status_cb, status_arg, repo, report_unchanged);
3750 if (lstat(ondisk_path, &sb) == -1) {
3751 if (errno != ENOENT)
3752 return got_error_from_errno2("lstat", ondisk_path);
3753 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3754 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3757 if (!no_ignores && match_ignores(ignores, path))
3758 return NULL;
3760 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3761 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3762 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3764 return NULL;
3767 static const struct got_error *
3768 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3769 const char *root_path, const char *path)
3771 const struct got_error *err;
3772 char *parent_path, *next_parent_path = NULL;
3774 err = add_ignores(ignores, root_path, "", -1,
3775 ".cvsignore");
3776 if (err)
3777 return err;
3779 err = add_ignores(ignores, root_path, "", -1,
3780 ".gitignore");
3781 if (err)
3782 return err;
3784 err = got_path_dirname(&parent_path, path);
3785 if (err) {
3786 if (err->code == GOT_ERR_BAD_PATH)
3787 return NULL; /* cannot traverse parent */
3788 return err;
3790 for (;;) {
3791 err = add_ignores(ignores, root_path, parent_path, -1,
3792 ".cvsignore");
3793 if (err)
3794 break;
3795 err = add_ignores(ignores, root_path, parent_path, -1,
3796 ".gitignore");
3797 if (err)
3798 break;
3799 err = got_path_dirname(&next_parent_path, parent_path);
3800 if (err) {
3801 if (err->code == GOT_ERR_BAD_PATH)
3802 err = NULL; /* traversed everything */
3803 break;
3805 if (got_path_is_root_dir(parent_path))
3806 break;
3807 free(parent_path);
3808 parent_path = next_parent_path;
3809 next_parent_path = NULL;
3812 free(parent_path);
3813 free(next_parent_path);
3814 return err;
3817 static const struct got_error *
3818 worktree_status(struct got_worktree *worktree, const char *path,
3819 struct got_fileindex *fileindex, struct got_repository *repo,
3820 got_worktree_status_cb status_cb, void *status_arg,
3821 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3822 int report_unchanged)
3824 const struct got_error *err = NULL;
3825 int fd = -1;
3826 struct got_fileindex_diff_dir_cb fdiff_cb;
3827 struct diff_dir_cb_arg arg;
3828 char *ondisk_path = NULL;
3829 struct got_pathlist_head ignores;
3830 struct got_fileindex_entry *ie;
3832 TAILQ_INIT(&ignores);
3834 if (asprintf(&ondisk_path, "%s%s%s",
3835 worktree->root_path, path[0] ? "/" : "", path) == -1)
3836 return got_error_from_errno("asprintf");
3838 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3839 if (ie) {
3840 err = report_single_file_status(path, ondisk_path,
3841 fileindex, status_cb, status_arg, repo,
3842 report_unchanged, &ignores, no_ignores);
3843 goto done;
3846 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3847 if (fd == -1) {
3848 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3849 !got_err_open_nofollow_on_symlink())
3850 err = got_error_from_errno2("open", ondisk_path);
3851 else {
3852 if (!no_ignores) {
3853 err = add_ignores_from_parent_paths(&ignores,
3854 worktree->root_path, ondisk_path);
3855 if (err)
3856 goto done;
3858 err = report_single_file_status(path, ondisk_path,
3859 fileindex, status_cb, status_arg, repo,
3860 report_unchanged, &ignores, no_ignores);
3862 } else {
3863 fdiff_cb.diff_old_new = status_old_new;
3864 fdiff_cb.diff_old = status_old;
3865 fdiff_cb.diff_new = status_new;
3866 fdiff_cb.diff_traverse = status_traverse;
3867 arg.fileindex = fileindex;
3868 arg.worktree = worktree;
3869 arg.status_path = path;
3870 arg.status_path_len = strlen(path);
3871 arg.repo = repo;
3872 arg.status_cb = status_cb;
3873 arg.status_arg = status_arg;
3874 arg.cancel_cb = cancel_cb;
3875 arg.cancel_arg = cancel_arg;
3876 arg.report_unchanged = report_unchanged;
3877 arg.no_ignores = no_ignores;
3878 if (!no_ignores) {
3879 err = add_ignores_from_parent_paths(&ignores,
3880 worktree->root_path, path);
3881 if (err)
3882 goto done;
3884 arg.ignores = &ignores;
3885 err = got_fileindex_diff_dir(fileindex, fd,
3886 worktree->root_path, path, repo, &fdiff_cb, &arg);
3888 done:
3889 free_ignores(&ignores);
3890 if (fd != -1 && close(fd) == -1 && err == NULL)
3891 err = got_error_from_errno("close");
3892 free(ondisk_path);
3893 return err;
3896 const struct got_error *
3897 got_worktree_status(struct got_worktree *worktree,
3898 struct got_pathlist_head *paths, struct got_repository *repo,
3899 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3900 got_cancel_cb cancel_cb, void *cancel_arg)
3902 const struct got_error *err = NULL;
3903 char *fileindex_path = NULL;
3904 struct got_fileindex *fileindex = NULL;
3905 struct got_pathlist_entry *pe;
3907 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3908 if (err)
3909 return err;
3911 TAILQ_FOREACH(pe, paths, entry) {
3912 err = worktree_status(worktree, pe->path, fileindex, repo,
3913 status_cb, status_arg, cancel_cb, cancel_arg,
3914 no_ignores, 0);
3915 if (err)
3916 break;
3918 free(fileindex_path);
3919 got_fileindex_free(fileindex);
3920 return err;
3923 const struct got_error *
3924 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3925 const char *arg)
3927 const struct got_error *err = NULL;
3928 char *resolved = NULL, *cwd = NULL, *path = NULL;
3929 size_t len;
3930 struct stat sb;
3931 char *abspath = NULL;
3932 char canonpath[PATH_MAX];
3934 *wt_path = NULL;
3936 cwd = getcwd(NULL, 0);
3937 if (cwd == NULL)
3938 return got_error_from_errno("getcwd");
3940 if (lstat(arg, &sb) == -1) {
3941 if (errno != ENOENT) {
3942 err = got_error_from_errno2("lstat", arg);
3943 goto done;
3945 sb.st_mode = 0;
3947 if (S_ISLNK(sb.st_mode)) {
3949 * We cannot use realpath(3) with symlinks since we want to
3950 * operate on the symlink itself.
3951 * But we can make the path absolute, assuming it is relative
3952 * to the current working directory, and then canonicalize it.
3954 if (!got_path_is_absolute(arg)) {
3955 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3956 err = got_error_from_errno("asprintf");
3957 goto done;
3961 err = got_canonpath(abspath ? abspath : arg, canonpath,
3962 sizeof(canonpath));
3963 if (err)
3964 goto done;
3965 resolved = strdup(canonpath);
3966 if (resolved == NULL) {
3967 err = got_error_from_errno("strdup");
3968 goto done;
3970 } else {
3971 resolved = realpath(arg, NULL);
3972 if (resolved == NULL) {
3973 if (errno != ENOENT) {
3974 err = got_error_from_errno2("realpath", arg);
3975 goto done;
3977 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3978 err = got_error_from_errno("asprintf");
3979 goto done;
3981 err = got_canonpath(abspath, canonpath,
3982 sizeof(canonpath));
3983 if (err)
3984 goto done;
3985 resolved = strdup(canonpath);
3986 if (resolved == NULL) {
3987 err = got_error_from_errno("strdup");
3988 goto done;
3993 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3994 strlen(got_worktree_get_root_path(worktree)))) {
3995 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3996 goto done;
3999 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4000 err = got_path_skip_common_ancestor(&path,
4001 got_worktree_get_root_path(worktree), resolved);
4002 if (err)
4003 goto done;
4004 } else {
4005 path = strdup("");
4006 if (path == NULL) {
4007 err = got_error_from_errno("strdup");
4008 goto done;
4012 /* XXX status walk can't deal with trailing slash! */
4013 len = strlen(path);
4014 while (len > 0 && path[len - 1] == '/') {
4015 path[len - 1] = '\0';
4016 len--;
4018 done:
4019 free(abspath);
4020 free(resolved);
4021 free(cwd);
4022 if (err == NULL)
4023 *wt_path = path;
4024 else
4025 free(path);
4026 return err;
4029 struct schedule_addition_args {
4030 struct got_worktree *worktree;
4031 struct got_fileindex *fileindex;
4032 got_worktree_checkout_cb progress_cb;
4033 void *progress_arg;
4034 struct got_repository *repo;
4037 static const struct got_error *
4038 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4039 const char *relpath, struct got_object_id *blob_id,
4040 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4041 int dirfd, const char *de_name)
4043 struct schedule_addition_args *a = arg;
4044 const struct got_error *err = NULL;
4045 struct got_fileindex_entry *ie;
4046 struct stat sb;
4047 char *ondisk_path;
4049 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4050 relpath) == -1)
4051 return got_error_from_errno("asprintf");
4053 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4054 if (ie) {
4055 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4056 de_name, a->repo);
4057 if (err)
4058 goto done;
4059 /* Re-adding an existing entry is a no-op. */
4060 if (status == GOT_STATUS_ADD)
4061 goto done;
4062 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4063 if (err)
4064 goto done;
4067 if (status != GOT_STATUS_UNVERSIONED) {
4068 if (status == GOT_STATUS_NONEXISTENT)
4069 err = got_error_set_errno(ENOENT, ondisk_path);
4070 else
4071 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4072 goto done;
4075 err = got_fileindex_entry_alloc(&ie, relpath);
4076 if (err)
4077 goto done;
4078 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4079 relpath, NULL, NULL, 1);
4080 if (err) {
4081 got_fileindex_entry_free(ie);
4082 goto done;
4084 err = got_fileindex_entry_add(a->fileindex, ie);
4085 if (err) {
4086 got_fileindex_entry_free(ie);
4087 goto done;
4089 done:
4090 free(ondisk_path);
4091 if (err)
4092 return err;
4093 if (status == GOT_STATUS_ADD)
4094 return NULL;
4095 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4098 const struct got_error *
4099 got_worktree_schedule_add(struct got_worktree *worktree,
4100 struct got_pathlist_head *paths,
4101 got_worktree_checkout_cb progress_cb, void *progress_arg,
4102 struct got_repository *repo, int no_ignores)
4104 struct got_fileindex *fileindex = NULL;
4105 char *fileindex_path = NULL;
4106 const struct got_error *err = NULL, *sync_err, *unlockerr;
4107 struct got_pathlist_entry *pe;
4108 struct schedule_addition_args saa;
4110 err = lock_worktree(worktree, LOCK_EX);
4111 if (err)
4112 return err;
4114 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4115 if (err)
4116 goto done;
4118 saa.worktree = worktree;
4119 saa.fileindex = fileindex;
4120 saa.progress_cb = progress_cb;
4121 saa.progress_arg = progress_arg;
4122 saa.repo = repo;
4124 TAILQ_FOREACH(pe, paths, entry) {
4125 err = worktree_status(worktree, pe->path, fileindex, repo,
4126 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4127 if (err)
4128 break;
4130 sync_err = sync_fileindex(fileindex, fileindex_path);
4131 if (sync_err && err == NULL)
4132 err = sync_err;
4133 done:
4134 free(fileindex_path);
4135 if (fileindex)
4136 got_fileindex_free(fileindex);
4137 unlockerr = lock_worktree(worktree, LOCK_SH);
4138 if (unlockerr && err == NULL)
4139 err = unlockerr;
4140 return err;
4143 struct schedule_deletion_args {
4144 struct got_worktree *worktree;
4145 struct got_fileindex *fileindex;
4146 got_worktree_delete_cb progress_cb;
4147 void *progress_arg;
4148 struct got_repository *repo;
4149 int delete_local_mods;
4150 int keep_on_disk;
4151 int ignore_missing_paths;
4152 const char *status_codes;
4155 static const struct got_error *
4156 schedule_for_deletion(void *arg, unsigned char status,
4157 unsigned char staged_status, const char *relpath,
4158 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4159 struct got_object_id *commit_id, int dirfd, const char *de_name)
4161 struct schedule_deletion_args *a = arg;
4162 const struct got_error *err = NULL;
4163 struct got_fileindex_entry *ie = NULL;
4164 struct stat sb;
4165 char *ondisk_path;
4167 if (status == GOT_STATUS_NONEXISTENT) {
4168 if (a->ignore_missing_paths)
4169 return NULL;
4170 return got_error_set_errno(ENOENT, relpath);
4173 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4174 if (ie == NULL)
4175 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4177 staged_status = get_staged_status(ie);
4178 if (staged_status != GOT_STATUS_NO_CHANGE) {
4179 if (staged_status == GOT_STATUS_DELETE)
4180 return NULL;
4181 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4184 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4185 relpath) == -1)
4186 return got_error_from_errno("asprintf");
4188 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4189 a->repo);
4190 if (err)
4191 goto done;
4193 if (a->status_codes) {
4194 size_t ncodes = strlen(a->status_codes);
4195 int i;
4196 for (i = 0; i < ncodes ; i++) {
4197 if (status == a->status_codes[i])
4198 break;
4200 if (i == ncodes) {
4201 /* Do not delete files in non-matching status. */
4202 free(ondisk_path);
4203 return NULL;
4205 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4206 a->status_codes[i] != GOT_STATUS_MISSING) {
4207 static char msg[64];
4208 snprintf(msg, sizeof(msg),
4209 "invalid status code '%c'", a->status_codes[i]);
4210 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4211 goto done;
4215 if (status != GOT_STATUS_NO_CHANGE) {
4216 if (status == GOT_STATUS_DELETE)
4217 goto done;
4218 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4219 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4220 goto done;
4222 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4223 err = got_error_set_errno(ENOENT, relpath);
4224 goto done;
4226 if (status != GOT_STATUS_MODIFY &&
4227 status != GOT_STATUS_MISSING) {
4228 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4229 goto done;
4233 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4234 size_t root_len;
4236 if (dirfd != -1) {
4237 if (unlinkat(dirfd, de_name, 0) == -1) {
4238 err = got_error_from_errno2("unlinkat",
4239 ondisk_path);
4240 goto done;
4242 } else if (unlink(ondisk_path) == -1) {
4243 err = got_error_from_errno2("unlink", ondisk_path);
4244 goto done;
4247 root_len = strlen(a->worktree->root_path);
4248 do {
4249 char *parent;
4250 err = got_path_dirname(&parent, ondisk_path);
4251 if (err)
4252 goto done;
4253 free(ondisk_path);
4254 ondisk_path = parent;
4255 if (rmdir(ondisk_path) == -1) {
4256 if (errno != ENOTEMPTY)
4257 err = got_error_from_errno2("rmdir",
4258 ondisk_path);
4259 break;
4261 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4262 strlen(ondisk_path), root_len) != 0);
4265 got_fileindex_entry_mark_deleted_from_disk(ie);
4266 done:
4267 free(ondisk_path);
4268 if (err)
4269 return err;
4270 if (status == GOT_STATUS_DELETE)
4271 return NULL;
4272 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4273 staged_status, relpath);
4276 const struct got_error *
4277 got_worktree_schedule_delete(struct got_worktree *worktree,
4278 struct got_pathlist_head *paths, int delete_local_mods,
4279 const char *status_codes,
4280 got_worktree_delete_cb progress_cb, void *progress_arg,
4281 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4283 struct got_fileindex *fileindex = NULL;
4284 char *fileindex_path = NULL;
4285 const struct got_error *err = NULL, *sync_err, *unlockerr;
4286 struct got_pathlist_entry *pe;
4287 struct schedule_deletion_args sda;
4289 err = lock_worktree(worktree, LOCK_EX);
4290 if (err)
4291 return err;
4293 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4294 if (err)
4295 goto done;
4297 sda.worktree = worktree;
4298 sda.fileindex = fileindex;
4299 sda.progress_cb = progress_cb;
4300 sda.progress_arg = progress_arg;
4301 sda.repo = repo;
4302 sda.delete_local_mods = delete_local_mods;
4303 sda.keep_on_disk = keep_on_disk;
4304 sda.ignore_missing_paths = ignore_missing_paths;
4305 sda.status_codes = status_codes;
4307 TAILQ_FOREACH(pe, paths, entry) {
4308 err = worktree_status(worktree, pe->path, fileindex, repo,
4309 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4310 if (err)
4311 break;
4313 sync_err = sync_fileindex(fileindex, fileindex_path);
4314 if (sync_err && err == NULL)
4315 err = sync_err;
4316 done:
4317 free(fileindex_path);
4318 if (fileindex)
4319 got_fileindex_free(fileindex);
4320 unlockerr = lock_worktree(worktree, LOCK_SH);
4321 if (unlockerr && err == NULL)
4322 err = unlockerr;
4323 return err;
4326 static const struct got_error *
4327 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4329 const struct got_error *err = NULL;
4330 char *line = NULL;
4331 size_t linesize = 0, n;
4332 ssize_t linelen;
4334 linelen = getline(&line, &linesize, infile);
4335 if (linelen == -1) {
4336 if (ferror(infile)) {
4337 err = got_error_from_errno("getline");
4338 goto done;
4340 return NULL;
4342 if (outfile) {
4343 n = fwrite(line, 1, linelen, outfile);
4344 if (n != linelen) {
4345 err = got_ferror(outfile, GOT_ERR_IO);
4346 goto done;
4349 if (rejectfile) {
4350 n = fwrite(line, 1, linelen, rejectfile);
4351 if (n != linelen)
4352 err = got_ferror(rejectfile, GOT_ERR_IO);
4354 done:
4355 free(line);
4356 return err;
4359 static const struct got_error *
4360 skip_one_line(FILE *f)
4362 char *line = NULL;
4363 size_t linesize = 0;
4364 ssize_t linelen;
4366 linelen = getline(&line, &linesize, f);
4367 if (linelen == -1) {
4368 if (ferror(f))
4369 return got_error_from_errno("getline");
4370 return NULL;
4372 free(line);
4373 return NULL;
4376 static const struct got_error *
4377 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4378 int start_old, int end_old, int start_new, int end_new,
4379 FILE *outfile, FILE *rejectfile)
4381 const struct got_error *err;
4383 /* Copy old file's lines leading up to patch. */
4384 while (!feof(f1) && *line_cur1 < start_old) {
4385 err = copy_one_line(f1, outfile, NULL);
4386 if (err)
4387 return err;
4388 (*line_cur1)++;
4390 /* Skip new file's lines leading up to patch. */
4391 while (!feof(f2) && *line_cur2 < start_new) {
4392 if (rejectfile)
4393 err = copy_one_line(f2, NULL, rejectfile);
4394 else
4395 err = skip_one_line(f2);
4396 if (err)
4397 return err;
4398 (*line_cur2)++;
4400 /* Copy patched lines. */
4401 while (!feof(f2) && *line_cur2 <= end_new) {
4402 err = copy_one_line(f2, outfile, NULL);
4403 if (err)
4404 return err;
4405 (*line_cur2)++;
4407 /* Skip over old file's replaced lines. */
4408 while (!feof(f1) && *line_cur1 <= end_old) {
4409 if (rejectfile)
4410 err = copy_one_line(f1, NULL, rejectfile);
4411 else
4412 err = skip_one_line(f1);
4413 if (err)
4414 return err;
4415 (*line_cur1)++;
4418 return NULL;
4421 static const struct got_error *
4422 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4423 FILE *outfile, FILE *rejectfile)
4425 const struct got_error *err;
4427 if (outfile) {
4428 /* Copy old file's lines until EOF. */
4429 while (!feof(f1)) {
4430 err = copy_one_line(f1, outfile, NULL);
4431 if (err)
4432 return err;
4433 (*line_cur1)++;
4436 if (rejectfile) {
4437 /* Copy new file's lines until EOF. */
4438 while (!feof(f2)) {
4439 err = copy_one_line(f2, NULL, rejectfile);
4440 if (err)
4441 return err;
4442 (*line_cur2)++;
4446 return NULL;
4449 static const struct got_error *
4450 apply_or_reject_change(int *choice, int *nchunks_used,
4451 struct diff_result *diff_result, int n,
4452 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4453 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4454 got_worktree_patch_cb patch_cb, void *patch_arg)
4456 const struct got_error *err = NULL;
4457 struct diff_chunk_context cc = {};
4458 int start_old, end_old, start_new, end_new;
4459 FILE *hunkfile;
4460 struct diff_output_unidiff_state *diff_state;
4461 struct diff_input_info diff_info;
4462 int rc;
4464 *choice = GOT_PATCH_CHOICE_NONE;
4466 /* Get changed line numbers without context lines for copy_change(). */
4467 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4468 start_old = cc.left.start;
4469 end_old = cc.left.end;
4470 start_new = cc.right.start;
4471 end_new = cc.right.end;
4473 /* Get the same change with context lines for display. */
4474 memset(&cc, 0, sizeof(cc));
4475 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4477 memset(&diff_info, 0, sizeof(diff_info));
4478 diff_info.left_path = relpath;
4479 diff_info.right_path = relpath;
4481 diff_state = diff_output_unidiff_state_alloc();
4482 if (diff_state == NULL)
4483 return got_error_set_errno(ENOMEM,
4484 "diff_output_unidiff_state_alloc");
4486 hunkfile = got_opentemp();
4487 if (hunkfile == NULL) {
4488 err = got_error_from_errno("got_opentemp");
4489 goto done;
4492 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4493 diff_result, &cc);
4494 if (rc != DIFF_RC_OK) {
4495 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4496 goto done;
4499 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4500 err = got_ferror(hunkfile, GOT_ERR_IO);
4501 goto done;
4504 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4505 hunkfile, changeno, nchanges);
4506 if (err)
4507 goto done;
4509 switch (*choice) {
4510 case GOT_PATCH_CHOICE_YES:
4511 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4512 end_old, start_new, end_new, outfile, rejectfile);
4513 break;
4514 case GOT_PATCH_CHOICE_NO:
4515 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4516 end_old, start_new, end_new, rejectfile, outfile);
4517 break;
4518 case GOT_PATCH_CHOICE_QUIT:
4519 break;
4520 default:
4521 err = got_error(GOT_ERR_PATCH_CHOICE);
4522 break;
4524 done:
4525 diff_output_unidiff_state_free(diff_state);
4526 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4527 err = got_error_from_errno("fclose");
4528 return err;
4531 struct revert_file_args {
4532 struct got_worktree *worktree;
4533 struct got_fileindex *fileindex;
4534 got_worktree_checkout_cb progress_cb;
4535 void *progress_arg;
4536 got_worktree_patch_cb patch_cb;
4537 void *patch_arg;
4538 struct got_repository *repo;
4539 int unlink_added_files;
4542 static const struct got_error *
4543 create_patched_content(char **path_outfile, int reverse_patch,
4544 struct got_object_id *blob_id, const char *path2,
4545 int dirfd2, const char *de_name2,
4546 const char *relpath, struct got_repository *repo,
4547 got_worktree_patch_cb patch_cb, void *patch_arg)
4549 const struct got_error *err, *free_err;
4550 struct got_blob_object *blob = NULL;
4551 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4552 int fd = -1, fd2 = -1;
4553 char link_target[PATH_MAX];
4554 ssize_t link_len = 0;
4555 char *path1 = NULL, *id_str = NULL;
4556 struct stat sb2;
4557 struct got_diffreg_result *diffreg_result = NULL;
4558 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4559 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4561 *path_outfile = NULL;
4563 err = got_object_id_str(&id_str, blob_id);
4564 if (err)
4565 return err;
4567 if (dirfd2 != -1) {
4568 fd2 = openat(dirfd2, de_name2,
4569 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4570 if (fd2 == -1) {
4571 if (!got_err_open_nofollow_on_symlink()) {
4572 err = got_error_from_errno2("openat", path2);
4573 goto done;
4575 link_len = readlinkat(dirfd2, de_name2,
4576 link_target, sizeof(link_target));
4577 if (link_len == -1) {
4578 return got_error_from_errno2("readlinkat",
4579 path2);
4581 sb2.st_mode = S_IFLNK;
4582 sb2.st_size = link_len;
4584 } else {
4585 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4586 if (fd2 == -1) {
4587 if (!got_err_open_nofollow_on_symlink()) {
4588 err = got_error_from_errno2("open", path2);
4589 goto done;
4591 link_len = readlink(path2, link_target,
4592 sizeof(link_target));
4593 if (link_len == -1)
4594 return got_error_from_errno2("readlink", path2);
4595 sb2.st_mode = S_IFLNK;
4596 sb2.st_size = link_len;
4599 if (fd2 != -1) {
4600 if (fstat(fd2, &sb2) == -1) {
4601 err = got_error_from_errno2("fstat", path2);
4602 goto done;
4605 f2 = fdopen(fd2, "r");
4606 if (f2 == NULL) {
4607 err = got_error_from_errno2("fdopen", path2);
4608 goto done;
4610 fd2 = -1;
4611 } else {
4612 size_t n;
4613 f2 = got_opentemp();
4614 if (f2 == NULL) {
4615 err = got_error_from_errno2("got_opentemp", path2);
4616 goto done;
4618 n = fwrite(link_target, 1, link_len, f2);
4619 if (n != link_len) {
4620 err = got_ferror(f2, GOT_ERR_IO);
4621 goto done;
4623 if (fflush(f2) == EOF) {
4624 err = got_error_from_errno("fflush");
4625 goto done;
4627 rewind(f2);
4630 fd = got_opentempfd();
4631 if (fd == -1) {
4632 err = got_error_from_errno("got_opentempfd");
4633 goto done;
4636 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4637 if (err)
4638 goto done;
4640 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4641 if (err)
4642 goto done;
4644 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4645 if (err)
4646 goto done;
4648 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4649 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4650 if (err)
4651 goto done;
4653 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4654 "");
4655 if (err)
4656 goto done;
4658 if (fseek(f1, 0L, SEEK_SET) == -1)
4659 return got_ferror(f1, GOT_ERR_IO);
4660 if (fseek(f2, 0L, SEEK_SET) == -1)
4661 return got_ferror(f2, GOT_ERR_IO);
4663 /* Count the number of actual changes in the diff result. */
4664 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4665 struct diff_chunk_context cc = {};
4666 diff_chunk_context_load_change(&cc, &nchunks_used,
4667 diffreg_result->result, n, 0);
4668 nchanges++;
4670 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4671 int choice;
4672 err = apply_or_reject_change(&choice, &nchunks_used,
4673 diffreg_result->result, n, relpath, f1, f2,
4674 &line_cur1, &line_cur2,
4675 reverse_patch ? NULL : outfile,
4676 reverse_patch ? outfile : NULL,
4677 ++i, nchanges, patch_cb, patch_arg);
4678 if (err)
4679 goto done;
4680 if (choice == GOT_PATCH_CHOICE_YES)
4681 have_content = 1;
4682 else if (choice == GOT_PATCH_CHOICE_QUIT)
4683 break;
4685 if (have_content) {
4686 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4687 reverse_patch ? NULL : outfile,
4688 reverse_patch ? outfile : NULL);
4689 if (err)
4690 goto done;
4692 if (!S_ISLNK(sb2.st_mode)) {
4693 mode_t mode;
4695 mode = apply_umask(sb2.st_mode);
4696 if (fchmod(fileno(outfile), mode) == -1) {
4697 err = got_error_from_errno2("fchmod", path2);
4698 goto done;
4702 done:
4703 free(id_str);
4704 if (fd != -1 && close(fd) == -1 && err == NULL)
4705 err = got_error_from_errno("close");
4706 if (blob)
4707 got_object_blob_close(blob);
4708 free_err = got_diffreg_result_free(diffreg_result);
4709 if (err == NULL)
4710 err = free_err;
4711 if (f1 && fclose(f1) == EOF && err == NULL)
4712 err = got_error_from_errno2("fclose", path1);
4713 if (f2 && fclose(f2) == EOF && err == NULL)
4714 err = got_error_from_errno2("fclose", path2);
4715 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4716 err = got_error_from_errno2("close", path2);
4717 if (outfile && fclose(outfile) == EOF && err == NULL)
4718 err = got_error_from_errno2("fclose", *path_outfile);
4719 if (path1 && unlink(path1) == -1 && err == NULL)
4720 err = got_error_from_errno2("unlink", path1);
4721 if (err || !have_content) {
4722 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4723 err = got_error_from_errno2("unlink", *path_outfile);
4724 free(*path_outfile);
4725 *path_outfile = NULL;
4727 free(path1);
4728 return err;
4731 static const struct got_error *
4732 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4733 const char *relpath, struct got_object_id *blob_id,
4734 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4735 int dirfd, const char *de_name)
4737 struct revert_file_args *a = arg;
4738 const struct got_error *err = NULL;
4739 char *parent_path = NULL;
4740 struct got_fileindex_entry *ie;
4741 struct got_commit_object *base_commit = NULL;
4742 struct got_tree_object *tree = NULL;
4743 struct got_object_id *tree_id = NULL;
4744 const struct got_tree_entry *te = NULL;
4745 char *tree_path = NULL, *te_name;
4746 char *ondisk_path = NULL, *path_content = NULL;
4747 struct got_blob_object *blob = NULL;
4748 int fd = -1;
4750 /* Reverting a staged deletion is a no-op. */
4751 if (status == GOT_STATUS_DELETE &&
4752 staged_status != GOT_STATUS_NO_CHANGE)
4753 return NULL;
4755 if (status == GOT_STATUS_UNVERSIONED)
4756 return (*a->progress_cb)(a->progress_arg,
4757 GOT_STATUS_UNVERSIONED, relpath);
4759 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4760 if (ie == NULL)
4761 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4763 /* Construct in-repository path of tree which contains this blob. */
4764 err = got_path_dirname(&parent_path, ie->path);
4765 if (err) {
4766 if (err->code != GOT_ERR_BAD_PATH)
4767 goto done;
4768 parent_path = strdup("/");
4769 if (parent_path == NULL) {
4770 err = got_error_from_errno("strdup");
4771 goto done;
4774 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4775 tree_path = strdup(parent_path);
4776 if (tree_path == NULL) {
4777 err = got_error_from_errno("strdup");
4778 goto done;
4780 } else {
4781 if (got_path_is_root_dir(parent_path)) {
4782 tree_path = strdup(a->worktree->path_prefix);
4783 if (tree_path == NULL) {
4784 err = got_error_from_errno("strdup");
4785 goto done;
4787 } else {
4788 if (asprintf(&tree_path, "%s/%s",
4789 a->worktree->path_prefix, parent_path) == -1) {
4790 err = got_error_from_errno("asprintf");
4791 goto done;
4796 err = got_object_open_as_commit(&base_commit, a->repo,
4797 a->worktree->base_commit_id);
4798 if (err)
4799 goto done;
4801 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4802 if (err) {
4803 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4804 (status == GOT_STATUS_ADD ||
4805 staged_status == GOT_STATUS_ADD)))
4806 goto done;
4807 } else {
4808 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4809 if (err)
4810 goto done;
4812 err = got_path_basename(&te_name, ie->path);
4813 if (err)
4814 goto done;
4816 te = got_object_tree_find_entry(tree, te_name);
4817 free(te_name);
4818 if (te == NULL && status != GOT_STATUS_ADD &&
4819 staged_status != GOT_STATUS_ADD) {
4820 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4821 goto done;
4825 switch (status) {
4826 case GOT_STATUS_ADD:
4827 if (a->patch_cb) {
4828 int choice = GOT_PATCH_CHOICE_NONE;
4829 err = (*a->patch_cb)(&choice, a->patch_arg,
4830 status, ie->path, NULL, 1, 1);
4831 if (err)
4832 goto done;
4833 if (choice != GOT_PATCH_CHOICE_YES)
4834 break;
4836 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4837 ie->path);
4838 if (err)
4839 goto done;
4840 got_fileindex_entry_remove(a->fileindex, ie);
4841 if (a->unlink_added_files) {
4842 if (asprintf(&ondisk_path, "%s/%s",
4843 got_worktree_get_root_path(a->worktree),
4844 relpath) == -1) {
4845 err = got_error_from_errno("asprintf");
4846 goto done;
4848 if (unlink(ondisk_path) == -1) {
4849 err = got_error_from_errno2("unlink",
4850 ondisk_path);
4851 break;
4854 break;
4855 case GOT_STATUS_DELETE:
4856 if (a->patch_cb) {
4857 int choice = GOT_PATCH_CHOICE_NONE;
4858 err = (*a->patch_cb)(&choice, a->patch_arg,
4859 status, ie->path, NULL, 1, 1);
4860 if (err)
4861 goto done;
4862 if (choice != GOT_PATCH_CHOICE_YES)
4863 break;
4865 /* fall through */
4866 case GOT_STATUS_MODIFY:
4867 case GOT_STATUS_MODE_CHANGE:
4868 case GOT_STATUS_CONFLICT:
4869 case GOT_STATUS_MISSING: {
4870 struct got_object_id id;
4871 if (staged_status == GOT_STATUS_ADD ||
4872 staged_status == GOT_STATUS_MODIFY)
4873 got_fileindex_entry_get_staged_blob_id(&id, ie);
4874 else
4875 got_fileindex_entry_get_blob_id(&id, ie);
4876 fd = got_opentempfd();
4877 if (fd == -1) {
4878 err = got_error_from_errno("got_opentempfd");
4879 goto done;
4882 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4883 if (err)
4884 goto done;
4886 if (asprintf(&ondisk_path, "%s/%s",
4887 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4888 err = got_error_from_errno("asprintf");
4889 goto done;
4892 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4893 status == GOT_STATUS_CONFLICT)) {
4894 int is_bad_symlink = 0;
4895 err = create_patched_content(&path_content, 1, &id,
4896 ondisk_path, dirfd, de_name, ie->path, a->repo,
4897 a->patch_cb, a->patch_arg);
4898 if (err || path_content == NULL)
4899 break;
4900 if (te && S_ISLNK(te->mode)) {
4901 if (unlink(path_content) == -1) {
4902 err = got_error_from_errno2("unlink",
4903 path_content);
4904 break;
4906 err = install_symlink(&is_bad_symlink,
4907 a->worktree, ondisk_path, ie->path,
4908 blob, 0, 1, 0, 0, a->repo,
4909 a->progress_cb, a->progress_arg);
4910 } else {
4911 if (rename(path_content, ondisk_path) == -1) {
4912 err = got_error_from_errno3("rename",
4913 path_content, ondisk_path);
4914 goto done;
4917 } else {
4918 int is_bad_symlink = 0;
4919 if (te && S_ISLNK(te->mode)) {
4920 err = install_symlink(&is_bad_symlink,
4921 a->worktree, ondisk_path, ie->path,
4922 blob, 0, 1, 0, 0, a->repo,
4923 a->progress_cb, a->progress_arg);
4924 } else {
4925 err = install_blob(a->worktree, ondisk_path,
4926 ie->path,
4927 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4928 got_fileindex_perms_to_st(ie), blob,
4929 0, 1, 0, 0, a->repo,
4930 a->progress_cb, a->progress_arg);
4932 if (err)
4933 goto done;
4934 if (status == GOT_STATUS_DELETE ||
4935 status == GOT_STATUS_MODE_CHANGE) {
4936 err = got_fileindex_entry_update(ie,
4937 a->worktree->root_fd, relpath,
4938 blob->id.sha1,
4939 a->worktree->base_commit_id->sha1, 1);
4940 if (err)
4941 goto done;
4943 if (is_bad_symlink) {
4944 got_fileindex_entry_filetype_set(ie,
4945 GOT_FILEIDX_MODE_BAD_SYMLINK);
4948 break;
4950 default:
4951 break;
4953 done:
4954 free(ondisk_path);
4955 free(path_content);
4956 free(parent_path);
4957 free(tree_path);
4958 if (fd != -1 && close(fd) == -1 && err == NULL)
4959 err = got_error_from_errno("close");
4960 if (blob)
4961 got_object_blob_close(blob);
4962 if (tree)
4963 got_object_tree_close(tree);
4964 free(tree_id);
4965 if (base_commit)
4966 got_object_commit_close(base_commit);
4967 return err;
4970 const struct got_error *
4971 got_worktree_revert(struct got_worktree *worktree,
4972 struct got_pathlist_head *paths,
4973 got_worktree_checkout_cb progress_cb, void *progress_arg,
4974 got_worktree_patch_cb patch_cb, void *patch_arg,
4975 struct got_repository *repo)
4977 struct got_fileindex *fileindex = NULL;
4978 char *fileindex_path = NULL;
4979 const struct got_error *err = NULL, *unlockerr = NULL;
4980 const struct got_error *sync_err = NULL;
4981 struct got_pathlist_entry *pe;
4982 struct revert_file_args rfa;
4984 err = lock_worktree(worktree, LOCK_EX);
4985 if (err)
4986 return err;
4988 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4989 if (err)
4990 goto done;
4992 rfa.worktree = worktree;
4993 rfa.fileindex = fileindex;
4994 rfa.progress_cb = progress_cb;
4995 rfa.progress_arg = progress_arg;
4996 rfa.patch_cb = patch_cb;
4997 rfa.patch_arg = patch_arg;
4998 rfa.repo = repo;
4999 rfa.unlink_added_files = 0;
5000 TAILQ_FOREACH(pe, paths, entry) {
5001 err = worktree_status(worktree, pe->path, fileindex, repo,
5002 revert_file, &rfa, NULL, NULL, 1, 0);
5003 if (err)
5004 break;
5006 sync_err = sync_fileindex(fileindex, fileindex_path);
5007 if (sync_err && err == NULL)
5008 err = sync_err;
5009 done:
5010 free(fileindex_path);
5011 if (fileindex)
5012 got_fileindex_free(fileindex);
5013 unlockerr = lock_worktree(worktree, LOCK_SH);
5014 if (unlockerr && err == NULL)
5015 err = unlockerr;
5016 return err;
5019 static void
5020 free_commitable(struct got_commitable *ct)
5022 free(ct->path);
5023 free(ct->in_repo_path);
5024 free(ct->ondisk_path);
5025 free(ct->blob_id);
5026 free(ct->base_blob_id);
5027 free(ct->staged_blob_id);
5028 free(ct->base_commit_id);
5029 free(ct);
5032 struct collect_commitables_arg {
5033 struct got_pathlist_head *commitable_paths;
5034 struct got_repository *repo;
5035 struct got_worktree *worktree;
5036 struct got_fileindex *fileindex;
5037 int have_staged_files;
5038 int allow_bad_symlinks;
5039 int diff_header_shown;
5040 int commit_conflicts;
5041 FILE *diff_outfile;
5042 FILE *f1;
5043 FILE *f2;
5047 * Create a file which contains the target path of a symlink so we can feed
5048 * it as content to the diff engine.
5050 static const struct got_error *
5051 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5052 const char *abspath)
5054 const struct got_error *err = NULL;
5055 char target_path[PATH_MAX];
5056 ssize_t target_len, outlen;
5058 *fd = -1;
5060 if (dirfd != -1) {
5061 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5062 if (target_len == -1)
5063 return got_error_from_errno2("readlinkat", abspath);
5064 } else {
5065 target_len = readlink(abspath, target_path, PATH_MAX);
5066 if (target_len == -1)
5067 return got_error_from_errno2("readlink", abspath);
5070 *fd = got_opentempfd();
5071 if (*fd == -1)
5072 return got_error_from_errno("got_opentempfd");
5074 outlen = write(*fd, target_path, target_len);
5075 if (outlen == -1) {
5076 err = got_error_from_errno("got_opentempfd");
5077 goto done;
5080 if (lseek(*fd, 0, SEEK_SET) == -1) {
5081 err = got_error_from_errno2("lseek", abspath);
5082 goto done;
5084 done:
5085 if (err) {
5086 close(*fd);
5087 *fd = -1;
5089 return err;
5092 static const struct got_error *
5093 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5094 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5095 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5097 const struct got_error *err = NULL;
5098 struct got_blob_object *blob1 = NULL;
5099 int fd = -1, fd1 = -1, fd2 = -1;
5100 FILE *ondisk_file = NULL;
5101 char *label1 = NULL;
5102 struct stat sb;
5103 off_t size1 = 0;
5104 int f2_exists = 0;
5105 char *id_str = NULL;
5107 memset(&sb, 0, sizeof(sb));
5109 if (diff_staged) {
5110 if (ct->staged_status != GOT_STATUS_MODIFY &&
5111 ct->staged_status != GOT_STATUS_ADD &&
5112 ct->staged_status != GOT_STATUS_DELETE)
5113 return NULL;
5114 } else {
5115 if (ct->status != GOT_STATUS_MODIFY &&
5116 ct->status != GOT_STATUS_ADD &&
5117 ct->status != GOT_STATUS_DELETE &&
5118 ct->status != GOT_STATUS_CONFLICT)
5119 return NULL;
5122 err = got_opentemp_truncate(f1);
5123 if (err)
5124 return got_error_from_errno("got_opentemp_truncate");
5125 err = got_opentemp_truncate(f2);
5126 if (err)
5127 return got_error_from_errno("got_opentemp_truncate");
5129 if (!*diff_header_shown) {
5130 err = got_object_id_str(&id_str, worktree->base_commit_id);
5131 if (err)
5132 return err;
5133 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5134 got_worktree_get_root_path(worktree));
5135 fprintf(diff_outfile, "commit - %s\n", id_str);
5136 fprintf(diff_outfile, "path + %s%s\n",
5137 got_worktree_get_root_path(worktree),
5138 diff_staged ? " (staged changes)" : "");
5139 *diff_header_shown = 1;
5142 if (diff_staged) {
5143 const char *label1 = NULL, *label2 = NULL;
5144 switch (ct->staged_status) {
5145 case GOT_STATUS_MODIFY:
5146 label1 = ct->path;
5147 label2 = ct->path;
5148 break;
5149 case GOT_STATUS_ADD:
5150 label2 = ct->path;
5151 break;
5152 case GOT_STATUS_DELETE:
5153 label1 = ct->path;
5154 break;
5155 default:
5156 return got_error(GOT_ERR_FILE_STATUS);
5158 fd1 = got_opentempfd();
5159 if (fd1 == -1) {
5160 err = got_error_from_errno("got_opentempfd");
5161 goto done;
5163 fd2 = got_opentempfd();
5164 if (fd2 == -1) {
5165 err = got_error_from_errno("got_opentempfd");
5166 goto done;
5168 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5169 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5170 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5171 NULL, repo, diff_outfile);
5172 goto done;
5175 fd1 = got_opentempfd();
5176 if (fd1 == -1) {
5177 err = got_error_from_errno("got_opentempfd");
5178 goto done;
5181 if (ct->status != GOT_STATUS_ADD) {
5182 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5183 8192, fd1);
5184 if (err)
5185 goto done;
5188 if (ct->status != GOT_STATUS_DELETE) {
5189 if (dirfd != -1) {
5190 fd = openat(dirfd, de_name,
5191 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5192 if (fd == -1) {
5193 if (!got_err_open_nofollow_on_symlink()) {
5194 err = got_error_from_errno2("openat",
5195 ct->ondisk_path);
5196 goto done;
5198 err = get_symlink_target_file(&fd, dirfd,
5199 de_name, ct->ondisk_path);
5200 if (err)
5201 goto done;
5203 } else {
5204 fd = open(ct->ondisk_path,
5205 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5206 if (fd == -1) {
5207 if (!got_err_open_nofollow_on_symlink()) {
5208 err = got_error_from_errno2("open",
5209 ct->ondisk_path);
5210 goto done;
5212 err = get_symlink_target_file(&fd, dirfd,
5213 de_name, ct->ondisk_path);
5214 if (err)
5215 goto done;
5218 if (fstatat(fd, ct->ondisk_path, &sb,
5219 AT_SYMLINK_NOFOLLOW) == -1) {
5220 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5221 goto done;
5223 ondisk_file = fdopen(fd, "r");
5224 if (ondisk_file == NULL) {
5225 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5226 goto done;
5228 fd = -1;
5229 f2_exists = 1;
5232 if (blob1) {
5233 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5234 f1, blob1);
5235 if (err)
5236 goto done;
5239 err = got_diff_blob_file(blob1, f1, size1, label1,
5240 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5241 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5242 done:
5243 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5244 err = got_error_from_errno("close");
5245 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5246 err = got_error_from_errno("close");
5247 if (blob1)
5248 got_object_blob_close(blob1);
5249 if (fd != -1 && close(fd) == -1 && err == NULL)
5250 err = got_error_from_errno("close");
5251 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5252 err = got_error_from_errno("fclose");
5253 return err;
5256 static const struct got_error *
5257 collect_commitables(void *arg, unsigned char status,
5258 unsigned char staged_status, const char *relpath,
5259 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5260 struct got_object_id *commit_id, int dirfd, const char *de_name)
5262 struct collect_commitables_arg *a = arg;
5263 const struct got_error *err = NULL;
5264 struct got_commitable *ct = NULL;
5265 struct got_pathlist_entry *new = NULL;
5266 char *parent_path = NULL, *path = NULL;
5267 struct stat sb;
5269 if (a->have_staged_files) {
5270 if (staged_status != GOT_STATUS_MODIFY &&
5271 staged_status != GOT_STATUS_ADD &&
5272 staged_status != GOT_STATUS_DELETE)
5273 return NULL;
5274 } else {
5275 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5276 printf("C %s\n", relpath);
5277 return got_error(GOT_ERR_COMMIT_CONFLICT);
5280 if (status != GOT_STATUS_MODIFY &&
5281 status != GOT_STATUS_MODE_CHANGE &&
5282 status != GOT_STATUS_ADD &&
5283 status != GOT_STATUS_DELETE &&
5284 status != GOT_STATUS_CONFLICT)
5285 return NULL;
5288 if (asprintf(&path, "/%s", relpath) == -1) {
5289 err = got_error_from_errno("asprintf");
5290 goto done;
5292 if (strcmp(path, "/") == 0) {
5293 parent_path = strdup("");
5294 if (parent_path == NULL)
5295 return got_error_from_errno("strdup");
5296 } else {
5297 err = got_path_dirname(&parent_path, path);
5298 if (err)
5299 return err;
5302 ct = calloc(1, sizeof(*ct));
5303 if (ct == NULL) {
5304 err = got_error_from_errno("calloc");
5305 goto done;
5308 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5309 relpath) == -1) {
5310 err = got_error_from_errno("asprintf");
5311 goto done;
5314 if (staged_status == GOT_STATUS_ADD ||
5315 staged_status == GOT_STATUS_MODIFY) {
5316 struct got_fileindex_entry *ie;
5317 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5318 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5319 case GOT_FILEIDX_MODE_REGULAR_FILE:
5320 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5321 ct->mode = S_IFREG;
5322 break;
5323 case GOT_FILEIDX_MODE_SYMLINK:
5324 ct->mode = S_IFLNK;
5325 break;
5326 default:
5327 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5328 goto done;
5330 ct->mode |= got_fileindex_entry_perms_get(ie);
5331 } else if (status != GOT_STATUS_DELETE &&
5332 staged_status != GOT_STATUS_DELETE) {
5333 if (dirfd != -1) {
5334 if (fstatat(dirfd, de_name, &sb,
5335 AT_SYMLINK_NOFOLLOW) == -1) {
5336 err = got_error_from_errno2("fstatat",
5337 ct->ondisk_path);
5338 goto done;
5340 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5341 err = got_error_from_errno2("lstat", ct->ondisk_path);
5342 goto done;
5344 ct->mode = sb.st_mode;
5347 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5348 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5349 relpath) == -1) {
5350 err = got_error_from_errno("asprintf");
5351 goto done;
5354 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5355 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5356 int is_bad_symlink;
5357 char target_path[PATH_MAX];
5358 ssize_t target_len;
5359 target_len = readlink(ct->ondisk_path, target_path,
5360 sizeof(target_path));
5361 if (target_len == -1) {
5362 err = got_error_from_errno2("readlink",
5363 ct->ondisk_path);
5364 goto done;
5366 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5367 target_len, ct->ondisk_path, a->worktree->root_path);
5368 if (err)
5369 goto done;
5370 if (is_bad_symlink) {
5371 err = got_error_path(ct->ondisk_path,
5372 GOT_ERR_BAD_SYMLINK);
5373 goto done;
5378 ct->status = status;
5379 ct->staged_status = staged_status;
5380 ct->blob_id = NULL; /* will be filled in when blob gets created */
5381 if (ct->status != GOT_STATUS_ADD &&
5382 ct->staged_status != GOT_STATUS_ADD) {
5383 ct->base_blob_id = got_object_id_dup(blob_id);
5384 if (ct->base_blob_id == NULL) {
5385 err = got_error_from_errno("got_object_id_dup");
5386 goto done;
5388 ct->base_commit_id = got_object_id_dup(commit_id);
5389 if (ct->base_commit_id == NULL) {
5390 err = got_error_from_errno("got_object_id_dup");
5391 goto done;
5394 if (ct->staged_status == GOT_STATUS_ADD ||
5395 ct->staged_status == GOT_STATUS_MODIFY) {
5396 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5397 if (ct->staged_blob_id == NULL) {
5398 err = got_error_from_errno("got_object_id_dup");
5399 goto done;
5402 ct->path = strdup(path);
5403 if (ct->path == NULL) {
5404 err = got_error_from_errno("strdup");
5405 goto done;
5407 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5408 if (err)
5409 goto done;
5411 if (a->diff_outfile && ct && new != NULL) {
5412 err = append_ct_diff(ct, &a->diff_header_shown,
5413 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5414 a->have_staged_files, a->repo, a->worktree);
5415 if (err)
5416 goto done;
5418 done:
5419 if (ct && (err || new == NULL))
5420 free_commitable(ct);
5421 free(parent_path);
5422 free(path);
5423 return err;
5426 static const struct got_error *write_tree(struct got_object_id **, int *,
5427 struct got_tree_object *, const char *, struct got_pathlist_head *,
5428 got_worktree_status_cb status_cb, void *status_arg,
5429 struct got_repository *);
5431 static const struct got_error *
5432 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5433 struct got_tree_entry *te, const char *parent_path,
5434 struct got_pathlist_head *commitable_paths,
5435 got_worktree_status_cb status_cb, void *status_arg,
5436 struct got_repository *repo)
5438 const struct got_error *err = NULL;
5439 struct got_tree_object *subtree;
5440 char *subpath;
5442 if (asprintf(&subpath, "%s%s%s", parent_path,
5443 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5444 return got_error_from_errno("asprintf");
5446 err = got_object_open_as_tree(&subtree, repo, &te->id);
5447 if (err)
5448 return err;
5450 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5451 commitable_paths, status_cb, status_arg, repo);
5452 got_object_tree_close(subtree);
5453 free(subpath);
5454 return err;
5457 static const struct got_error *
5458 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5460 const struct got_error *err = NULL;
5461 char *ct_parent_path = NULL;
5463 *match = 0;
5465 if (strchr(ct->in_repo_path, '/') == NULL) {
5466 *match = got_path_is_root_dir(path);
5467 return NULL;
5470 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5471 if (err)
5472 return err;
5473 *match = (strcmp(path, ct_parent_path) == 0);
5474 free(ct_parent_path);
5475 return err;
5478 static mode_t
5479 get_ct_file_mode(struct got_commitable *ct)
5481 if (S_ISLNK(ct->mode))
5482 return S_IFLNK;
5484 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5487 static const struct got_error *
5488 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5489 struct got_tree_entry *te, struct got_commitable *ct)
5491 const struct got_error *err = NULL;
5493 *new_te = NULL;
5495 err = got_object_tree_entry_dup(new_te, te);
5496 if (err)
5497 goto done;
5499 (*new_te)->mode = get_ct_file_mode(ct);
5501 if (ct->staged_status == GOT_STATUS_MODIFY)
5502 memcpy(&(*new_te)->id, ct->staged_blob_id,
5503 sizeof((*new_te)->id));
5504 else
5505 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5506 done:
5507 if (err && *new_te) {
5508 free(*new_te);
5509 *new_te = NULL;
5511 return err;
5514 static const struct got_error *
5515 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5516 struct got_commitable *ct)
5518 const struct got_error *err = NULL;
5519 char *ct_name = NULL;
5521 *new_te = NULL;
5523 *new_te = calloc(1, sizeof(**new_te));
5524 if (*new_te == NULL)
5525 return got_error_from_errno("calloc");
5527 err = got_path_basename(&ct_name, ct->path);
5528 if (err)
5529 goto done;
5530 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5531 sizeof((*new_te)->name)) {
5532 err = got_error(GOT_ERR_NO_SPACE);
5533 goto done;
5536 (*new_te)->mode = get_ct_file_mode(ct);
5538 if (ct->staged_status == GOT_STATUS_ADD)
5539 memcpy(&(*new_te)->id, ct->staged_blob_id,
5540 sizeof((*new_te)->id));
5541 else
5542 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5543 done:
5544 free(ct_name);
5545 if (err && *new_te) {
5546 free(*new_te);
5547 *new_te = NULL;
5549 return err;
5552 static const struct got_error *
5553 insert_tree_entry(struct got_tree_entry *new_te,
5554 struct got_pathlist_head *paths)
5556 const struct got_error *err = NULL;
5557 struct got_pathlist_entry *new_pe;
5559 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5560 if (err)
5561 return err;
5562 if (new_pe == NULL)
5563 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5564 return NULL;
5567 static const struct got_error *
5568 report_ct_status(struct got_commitable *ct,
5569 got_worktree_status_cb status_cb, void *status_arg)
5571 const char *ct_path = ct->path;
5572 unsigned char status;
5574 if (status_cb == NULL) /* no commit progress output desired */
5575 return NULL;
5577 while (ct_path[0] == '/')
5578 ct_path++;
5580 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5581 status = ct->staged_status;
5582 else
5583 status = ct->status;
5585 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5586 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5589 static const struct got_error *
5590 match_modified_subtree(int *modified, struct got_tree_entry *te,
5591 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5593 const struct got_error *err = NULL;
5594 struct got_pathlist_entry *pe;
5595 char *te_path;
5597 *modified = 0;
5599 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5600 got_path_is_root_dir(base_tree_path) ? "" : "/",
5601 te->name) == -1)
5602 return got_error_from_errno("asprintf");
5604 TAILQ_FOREACH(pe, commitable_paths, entry) {
5605 struct got_commitable *ct = pe->data;
5606 *modified = got_path_is_child(ct->in_repo_path, te_path,
5607 strlen(te_path));
5608 if (*modified)
5609 break;
5612 free(te_path);
5613 return err;
5616 static const struct got_error *
5617 match_deleted_or_modified_ct(struct got_commitable **ctp,
5618 struct got_tree_entry *te, const char *base_tree_path,
5619 struct got_pathlist_head *commitable_paths)
5621 const struct got_error *err = NULL;
5622 struct got_pathlist_entry *pe;
5624 *ctp = NULL;
5626 TAILQ_FOREACH(pe, commitable_paths, entry) {
5627 struct got_commitable *ct = pe->data;
5628 char *ct_name = NULL;
5629 int path_matches;
5631 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5632 if (ct->status != GOT_STATUS_MODIFY &&
5633 ct->status != GOT_STATUS_MODE_CHANGE &&
5634 ct->status != GOT_STATUS_DELETE &&
5635 ct->status != GOT_STATUS_CONFLICT)
5636 continue;
5637 } else {
5638 if (ct->staged_status != GOT_STATUS_MODIFY &&
5639 ct->staged_status != GOT_STATUS_DELETE)
5640 continue;
5643 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5644 continue;
5646 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5647 if (err)
5648 return err;
5649 if (!path_matches)
5650 continue;
5652 err = got_path_basename(&ct_name, pe->path);
5653 if (err)
5654 return err;
5656 if (strcmp(te->name, ct_name) != 0) {
5657 free(ct_name);
5658 continue;
5660 free(ct_name);
5662 *ctp = ct;
5663 break;
5666 return err;
5669 static const struct got_error *
5670 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5671 const char *child_path, const char *path_base_tree,
5672 struct got_pathlist_head *commitable_paths,
5673 got_worktree_status_cb status_cb, void *status_arg,
5674 struct got_repository *repo)
5676 const struct got_error *err = NULL;
5677 struct got_tree_entry *new_te;
5678 char *subtree_path;
5679 struct got_object_id *id = NULL;
5680 int nentries;
5682 *new_tep = NULL;
5684 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5685 got_path_is_root_dir(path_base_tree) ? "" : "/",
5686 child_path) == -1)
5687 return got_error_from_errno("asprintf");
5689 new_te = calloc(1, sizeof(*new_te));
5690 if (new_te == NULL)
5691 return got_error_from_errno("calloc");
5692 new_te->mode = S_IFDIR;
5694 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5695 sizeof(new_te->name)) {
5696 err = got_error(GOT_ERR_NO_SPACE);
5697 goto done;
5699 err = write_tree(&id, &nentries, NULL, subtree_path,
5700 commitable_paths, status_cb, status_arg, repo);
5701 if (err) {
5702 free(new_te);
5703 goto done;
5705 memcpy(&new_te->id, id, sizeof(new_te->id));
5706 done:
5707 free(id);
5708 free(subtree_path);
5709 if (err == NULL)
5710 *new_tep = new_te;
5711 return err;
5714 static const struct got_error *
5715 write_tree(struct got_object_id **new_tree_id, int *nentries,
5716 struct got_tree_object *base_tree, const char *path_base_tree,
5717 struct got_pathlist_head *commitable_paths,
5718 got_worktree_status_cb status_cb, void *status_arg,
5719 struct got_repository *repo)
5721 const struct got_error *err = NULL;
5722 struct got_pathlist_head paths;
5723 struct got_tree_entry *te, *new_te = NULL;
5724 struct got_pathlist_entry *pe;
5726 TAILQ_INIT(&paths);
5727 *nentries = 0;
5729 /* Insert, and recurse into, newly added entries first. */
5730 TAILQ_FOREACH(pe, commitable_paths, entry) {
5731 struct got_commitable *ct = pe->data;
5732 char *child_path = NULL, *slash;
5734 if ((ct->status != GOT_STATUS_ADD &&
5735 ct->staged_status != GOT_STATUS_ADD) ||
5736 (ct->flags & GOT_COMMITABLE_ADDED))
5737 continue;
5739 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5740 strlen(path_base_tree)))
5741 continue;
5743 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5744 ct->in_repo_path);
5745 if (err)
5746 goto done;
5748 slash = strchr(child_path, '/');
5749 if (slash == NULL) {
5750 err = alloc_added_blob_tree_entry(&new_te, ct);
5751 if (err)
5752 goto done;
5753 err = report_ct_status(ct, status_cb, status_arg);
5754 if (err)
5755 goto done;
5756 ct->flags |= GOT_COMMITABLE_ADDED;
5757 err = insert_tree_entry(new_te, &paths);
5758 if (err)
5759 goto done;
5760 (*nentries)++;
5761 } else {
5762 *slash = '\0'; /* trim trailing path components */
5763 if (base_tree == NULL ||
5764 got_object_tree_find_entry(base_tree, child_path)
5765 == NULL) {
5766 err = make_subtree_for_added_blob(&new_te,
5767 child_path, path_base_tree,
5768 commitable_paths, status_cb, status_arg,
5769 repo);
5770 if (err)
5771 goto done;
5772 err = insert_tree_entry(new_te, &paths);
5773 if (err)
5774 goto done;
5775 (*nentries)++;
5780 if (base_tree) {
5781 int i, nbase_entries;
5782 /* Handle modified and deleted entries. */
5783 nbase_entries = got_object_tree_get_nentries(base_tree);
5784 for (i = 0; i < nbase_entries; i++) {
5785 struct got_commitable *ct = NULL;
5787 te = got_object_tree_get_entry(base_tree, i);
5788 if (got_object_tree_entry_is_submodule(te)) {
5789 /* Entry is a submodule; just copy it. */
5790 err = got_object_tree_entry_dup(&new_te, te);
5791 if (err)
5792 goto done;
5793 err = insert_tree_entry(new_te, &paths);
5794 if (err)
5795 goto done;
5796 (*nentries)++;
5797 continue;
5800 if (S_ISDIR(te->mode)) {
5801 int modified;
5802 err = got_object_tree_entry_dup(&new_te, te);
5803 if (err)
5804 goto done;
5805 err = match_modified_subtree(&modified, te,
5806 path_base_tree, commitable_paths);
5807 if (err)
5808 goto done;
5809 /* Avoid recursion into unmodified subtrees. */
5810 if (modified) {
5811 struct got_object_id *new_id;
5812 int nsubentries;
5813 err = write_subtree(&new_id,
5814 &nsubentries, te,
5815 path_base_tree, commitable_paths,
5816 status_cb, status_arg, repo);
5817 if (err)
5818 goto done;
5819 if (nsubentries == 0) {
5820 /* All entries were deleted. */
5821 free(new_id);
5822 continue;
5824 memcpy(&new_te->id, new_id,
5825 sizeof(new_te->id));
5826 free(new_id);
5828 err = insert_tree_entry(new_te, &paths);
5829 if (err)
5830 goto done;
5831 (*nentries)++;
5832 continue;
5835 err = match_deleted_or_modified_ct(&ct, te,
5836 path_base_tree, commitable_paths);
5837 if (err)
5838 goto done;
5839 if (ct) {
5840 /* NB: Deleted entries get dropped here. */
5841 if (ct->status == GOT_STATUS_MODIFY ||
5842 ct->status == GOT_STATUS_MODE_CHANGE ||
5843 ct->status == GOT_STATUS_CONFLICT ||
5844 ct->staged_status == GOT_STATUS_MODIFY) {
5845 err = alloc_modified_blob_tree_entry(
5846 &new_te, te, ct);
5847 if (err)
5848 goto done;
5849 err = insert_tree_entry(new_te, &paths);
5850 if (err)
5851 goto done;
5852 (*nentries)++;
5854 err = report_ct_status(ct, status_cb,
5855 status_arg);
5856 if (err)
5857 goto done;
5858 } else {
5859 /* Entry is unchanged; just copy it. */
5860 err = got_object_tree_entry_dup(&new_te, te);
5861 if (err)
5862 goto done;
5863 err = insert_tree_entry(new_te, &paths);
5864 if (err)
5865 goto done;
5866 (*nentries)++;
5871 /* Write new list of entries; deleted entries have been dropped. */
5872 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5873 done:
5874 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5875 return err;
5878 static const struct got_error *
5879 update_fileindex_after_commit(struct got_worktree *worktree,
5880 struct got_pathlist_head *commitable_paths,
5881 struct got_object_id *new_base_commit_id,
5882 struct got_fileindex *fileindex, int have_staged_files)
5884 const struct got_error *err = NULL;
5885 struct got_pathlist_entry *pe;
5886 char *relpath = NULL;
5888 TAILQ_FOREACH(pe, commitable_paths, entry) {
5889 struct got_fileindex_entry *ie;
5890 struct got_commitable *ct = pe->data;
5892 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5894 err = got_path_skip_common_ancestor(&relpath,
5895 worktree->root_path, ct->ondisk_path);
5896 if (err)
5897 goto done;
5899 if (ie) {
5900 if (ct->status == GOT_STATUS_DELETE ||
5901 ct->staged_status == GOT_STATUS_DELETE) {
5902 got_fileindex_entry_remove(fileindex, ie);
5903 } else if (ct->staged_status == GOT_STATUS_ADD ||
5904 ct->staged_status == GOT_STATUS_MODIFY) {
5905 got_fileindex_entry_stage_set(ie,
5906 GOT_FILEIDX_STAGE_NONE);
5907 got_fileindex_entry_staged_filetype_set(ie, 0);
5909 err = got_fileindex_entry_update(ie,
5910 worktree->root_fd, relpath,
5911 ct->staged_blob_id->sha1,
5912 new_base_commit_id->sha1,
5913 !have_staged_files);
5914 } else
5915 err = got_fileindex_entry_update(ie,
5916 worktree->root_fd, relpath,
5917 ct->blob_id->sha1,
5918 new_base_commit_id->sha1,
5919 !have_staged_files);
5920 } else {
5921 err = got_fileindex_entry_alloc(&ie, pe->path);
5922 if (err)
5923 goto done;
5924 err = got_fileindex_entry_update(ie,
5925 worktree->root_fd, relpath, ct->blob_id->sha1,
5926 new_base_commit_id->sha1, 1);
5927 if (err) {
5928 got_fileindex_entry_free(ie);
5929 goto done;
5931 err = got_fileindex_entry_add(fileindex, ie);
5932 if (err) {
5933 got_fileindex_entry_free(ie);
5934 goto done;
5937 free(relpath);
5938 relpath = NULL;
5940 done:
5941 free(relpath);
5942 return err;
5946 static const struct got_error *
5947 check_out_of_date(const char *in_repo_path, unsigned char status,
5948 unsigned char staged_status, struct got_object_id *base_blob_id,
5949 struct got_object_id *base_commit_id,
5950 struct got_object_id *head_commit_id, struct got_repository *repo,
5951 int ood_errcode)
5953 const struct got_error *err = NULL;
5954 struct got_commit_object *commit = NULL;
5955 struct got_object_id *id = NULL;
5957 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5958 /* Trivial case: base commit == head commit */
5959 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5960 return NULL;
5962 * Ensure file content which local changes were based
5963 * on matches file content in the branch head.
5965 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5966 if (err)
5967 goto done;
5968 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5969 if (err) {
5970 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5971 err = got_error(ood_errcode);
5972 goto done;
5973 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5974 err = got_error(ood_errcode);
5975 } else {
5976 /* Require that added files don't exist in the branch head. */
5977 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5978 if (err)
5979 goto done;
5980 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5981 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5982 goto done;
5983 err = id ? got_error(ood_errcode) : NULL;
5985 done:
5986 free(id);
5987 if (commit)
5988 got_object_commit_close(commit);
5989 return err;
5992 static const struct got_error *
5993 commit_worktree(struct got_object_id **new_commit_id,
5994 struct got_pathlist_head *commitable_paths,
5995 struct got_object_id *head_commit_id,
5996 struct got_object_id *parent_id2,
5997 struct got_worktree *worktree,
5998 const char *author, const char *committer, char *diff_path,
5999 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6000 got_worktree_status_cb status_cb, void *status_arg,
6001 struct got_repository *repo)
6003 const struct got_error *err = NULL, *unlockerr = NULL;
6004 struct got_pathlist_entry *pe;
6005 const char *head_ref_name = NULL;
6006 struct got_commit_object *head_commit = NULL;
6007 struct got_reference *head_ref2 = NULL;
6008 struct got_object_id *head_commit_id2 = NULL;
6009 struct got_tree_object *head_tree = NULL;
6010 struct got_object_id *new_tree_id = NULL;
6011 int nentries, nparents = 0;
6012 struct got_object_id_queue parent_ids;
6013 struct got_object_qid *pid = NULL;
6014 char *logmsg = NULL;
6015 time_t timestamp;
6017 *new_commit_id = NULL;
6019 STAILQ_INIT(&parent_ids);
6021 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6022 if (err)
6023 goto done;
6025 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6026 if (err)
6027 goto done;
6029 if (commit_msg_cb != NULL) {
6030 err = commit_msg_cb(commitable_paths, diff_path,
6031 &logmsg, commit_arg);
6032 if (err)
6033 goto done;
6036 if (logmsg == NULL || strlen(logmsg) == 0) {
6037 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6038 goto done;
6041 /* Create blobs from added and modified files and record their IDs. */
6042 TAILQ_FOREACH(pe, commitable_paths, entry) {
6043 struct got_commitable *ct = pe->data;
6044 char *ondisk_path;
6046 /* Blobs for staged files already exist. */
6047 if (ct->staged_status == GOT_STATUS_ADD ||
6048 ct->staged_status == GOT_STATUS_MODIFY)
6049 continue;
6051 if (ct->status != GOT_STATUS_ADD &&
6052 ct->status != GOT_STATUS_MODIFY &&
6053 ct->status != GOT_STATUS_MODE_CHANGE &&
6054 ct->status != GOT_STATUS_CONFLICT)
6055 continue;
6057 if (asprintf(&ondisk_path, "%s/%s",
6058 worktree->root_path, pe->path) == -1) {
6059 err = got_error_from_errno("asprintf");
6060 goto done;
6062 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6063 free(ondisk_path);
6064 if (err)
6065 goto done;
6068 /* Recursively write new tree objects. */
6069 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6070 commitable_paths, status_cb, status_arg, repo);
6071 if (err)
6072 goto done;
6074 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6075 if (err)
6076 goto done;
6077 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6078 nparents++;
6079 if (parent_id2) {
6080 err = got_object_qid_alloc(&pid, parent_id2);
6081 if (err)
6082 goto done;
6083 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6084 nparents++;
6086 timestamp = time(NULL);
6087 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6088 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6089 if (logmsg != NULL)
6090 free(logmsg);
6091 if (err)
6092 goto done;
6094 /* Check if a concurrent commit to our branch has occurred. */
6095 head_ref_name = got_worktree_get_head_ref_name(worktree);
6096 if (head_ref_name == NULL) {
6097 err = got_error_from_errno("got_worktree_get_head_ref_name");
6098 goto done;
6100 /* Lock the reference here to prevent concurrent modification. */
6101 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6102 if (err)
6103 goto done;
6104 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6105 if (err)
6106 goto done;
6107 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6108 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6109 goto done;
6111 /* Update branch head in repository. */
6112 err = got_ref_change_ref(head_ref2, *new_commit_id);
6113 if (err)
6114 goto done;
6115 err = got_ref_write(head_ref2, repo);
6116 if (err)
6117 goto done;
6119 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6120 if (err)
6121 goto done;
6123 err = ref_base_commit(worktree, repo);
6124 if (err)
6125 goto done;
6126 done:
6127 got_object_id_queue_free(&parent_ids);
6128 if (head_tree)
6129 got_object_tree_close(head_tree);
6130 if (head_commit)
6131 got_object_commit_close(head_commit);
6132 free(head_commit_id2);
6133 if (head_ref2) {
6134 unlockerr = got_ref_unlock(head_ref2);
6135 if (unlockerr && err == NULL)
6136 err = unlockerr;
6137 got_ref_close(head_ref2);
6139 return err;
6142 static const struct got_error *
6143 check_path_is_commitable(const char *path,
6144 struct got_pathlist_head *commitable_paths)
6146 struct got_pathlist_entry *cpe = NULL;
6147 size_t path_len = strlen(path);
6149 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6150 struct got_commitable *ct = cpe->data;
6151 const char *ct_path = ct->path;
6153 while (ct_path[0] == '/')
6154 ct_path++;
6156 if (strcmp(path, ct_path) == 0 ||
6157 got_path_is_child(ct_path, path, path_len))
6158 break;
6161 if (cpe == NULL)
6162 return got_error_path(path, GOT_ERR_BAD_PATH);
6164 return NULL;
6167 static const struct got_error *
6168 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6170 int *have_staged_files = arg;
6172 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6173 *have_staged_files = 1;
6174 return got_error(GOT_ERR_CANCELLED);
6177 return NULL;
6180 static const struct got_error *
6181 check_non_staged_files(struct got_fileindex *fileindex,
6182 struct got_pathlist_head *paths)
6184 struct got_pathlist_entry *pe;
6185 struct got_fileindex_entry *ie;
6187 TAILQ_FOREACH(pe, paths, entry) {
6188 if (pe->path[0] == '\0')
6189 continue;
6190 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6191 if (ie == NULL)
6192 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6193 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6194 return got_error_path(pe->path,
6195 GOT_ERR_FILE_NOT_STAGED);
6198 return NULL;
6201 const struct got_error *
6202 got_worktree_commit(struct got_object_id **new_commit_id,
6203 struct got_worktree *worktree, struct got_pathlist_head *paths,
6204 const char *author, const char *committer, int allow_bad_symlinks,
6205 int show_diff, int commit_conflicts,
6206 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6207 got_worktree_status_cb status_cb, void *status_arg,
6208 struct got_repository *repo)
6210 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6211 struct got_fileindex *fileindex = NULL;
6212 char *fileindex_path = NULL;
6213 struct got_pathlist_head commitable_paths;
6214 struct collect_commitables_arg cc_arg;
6215 struct got_pathlist_entry *pe;
6216 struct got_reference *head_ref = NULL;
6217 struct got_object_id *head_commit_id = NULL;
6218 char *diff_path = NULL;
6219 int have_staged_files = 0;
6221 *new_commit_id = NULL;
6223 memset(&cc_arg, 0, sizeof(cc_arg));
6224 TAILQ_INIT(&commitable_paths);
6226 err = lock_worktree(worktree, LOCK_EX);
6227 if (err)
6228 goto done;
6230 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6231 if (err)
6232 goto done;
6234 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6235 if (err)
6236 goto done;
6238 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6239 if (err)
6240 goto done;
6242 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6243 &have_staged_files);
6244 if (err && err->code != GOT_ERR_CANCELLED)
6245 goto done;
6246 if (have_staged_files) {
6247 err = check_non_staged_files(fileindex, paths);
6248 if (err)
6249 goto done;
6252 cc_arg.commitable_paths = &commitable_paths;
6253 cc_arg.worktree = worktree;
6254 cc_arg.fileindex = fileindex;
6255 cc_arg.repo = repo;
6256 cc_arg.have_staged_files = have_staged_files;
6257 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6258 cc_arg.diff_header_shown = 0;
6259 cc_arg.commit_conflicts = commit_conflicts;
6260 if (show_diff) {
6261 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6262 GOT_TMPDIR_STR "/got", ".diff");
6263 if (err)
6264 goto done;
6265 cc_arg.f1 = got_opentemp();
6266 if (cc_arg.f1 == NULL) {
6267 err = got_error_from_errno("got_opentemp");
6268 goto done;
6270 cc_arg.f2 = got_opentemp();
6271 if (cc_arg.f2 == NULL) {
6272 err = got_error_from_errno("got_opentemp");
6273 goto done;
6277 TAILQ_FOREACH(pe, paths, entry) {
6278 err = worktree_status(worktree, pe->path, fileindex, repo,
6279 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6280 if (err)
6281 goto done;
6284 if (show_diff) {
6285 if (fflush(cc_arg.diff_outfile) == EOF) {
6286 err = got_error_from_errno("fflush");
6287 goto done;
6291 if (TAILQ_EMPTY(&commitable_paths)) {
6292 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6293 goto done;
6296 TAILQ_FOREACH(pe, paths, entry) {
6297 err = check_path_is_commitable(pe->path, &commitable_paths);
6298 if (err)
6299 goto done;
6302 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6303 struct got_commitable *ct = pe->data;
6304 const char *ct_path = ct->in_repo_path;
6306 while (ct_path[0] == '/')
6307 ct_path++;
6308 err = check_out_of_date(ct_path, ct->status,
6309 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6310 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6311 if (err)
6312 goto done;
6316 err = commit_worktree(new_commit_id, &commitable_paths,
6317 head_commit_id, NULL, worktree, author, committer,
6318 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6319 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6320 if (err)
6321 goto done;
6323 err = update_fileindex_after_commit(worktree, &commitable_paths,
6324 *new_commit_id, fileindex, have_staged_files);
6325 sync_err = sync_fileindex(fileindex, fileindex_path);
6326 if (sync_err && err == NULL)
6327 err = sync_err;
6328 done:
6329 if (fileindex)
6330 got_fileindex_free(fileindex);
6331 free(fileindex_path);
6332 unlockerr = lock_worktree(worktree, LOCK_SH);
6333 if (unlockerr && err == NULL)
6334 err = unlockerr;
6335 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6336 struct got_commitable *ct = pe->data;
6338 free_commitable(ct);
6340 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6341 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6342 err = got_error_from_errno2("unlink", diff_path);
6343 free(diff_path);
6344 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6345 err == NULL)
6346 err = got_error_from_errno("fclose");
6347 return err;
6350 const char *
6351 got_commitable_get_path(struct got_commitable *ct)
6353 return ct->path;
6356 unsigned int
6357 got_commitable_get_status(struct got_commitable *ct)
6359 return ct->status;
6362 struct check_rebase_ok_arg {
6363 struct got_worktree *worktree;
6364 struct got_repository *repo;
6367 static const struct got_error *
6368 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6370 const struct got_error *err = NULL;
6371 struct check_rebase_ok_arg *a = arg;
6372 unsigned char status;
6373 struct stat sb;
6374 char *ondisk_path;
6376 /* Reject rebase of a work tree with mixed base commits. */
6377 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6378 SHA1_DIGEST_LENGTH))
6379 return got_error(GOT_ERR_MIXED_COMMITS);
6381 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6382 == -1)
6383 return got_error_from_errno("asprintf");
6385 /* Reject rebase of a work tree with modified or staged files. */
6386 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6387 free(ondisk_path);
6388 if (err)
6389 return err;
6391 if (status != GOT_STATUS_NO_CHANGE)
6392 return got_error(GOT_ERR_MODIFIED);
6393 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6394 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6396 return NULL;
6399 const struct got_error *
6400 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6401 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6402 struct got_worktree *worktree, struct got_reference *branch,
6403 struct got_repository *repo)
6405 const struct got_error *err = NULL;
6406 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6407 char *branch_ref_name = NULL;
6408 char *fileindex_path = NULL;
6409 struct check_rebase_ok_arg ok_arg;
6410 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6411 struct got_object_id *wt_branch_tip = NULL;
6413 *new_base_branch_ref = NULL;
6414 *tmp_branch = NULL;
6415 *fileindex = NULL;
6417 err = lock_worktree(worktree, LOCK_EX);
6418 if (err)
6419 return err;
6421 err = open_fileindex(fileindex, &fileindex_path, worktree);
6422 if (err)
6423 goto done;
6425 ok_arg.worktree = worktree;
6426 ok_arg.repo = repo;
6427 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6428 &ok_arg);
6429 if (err)
6430 goto done;
6432 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6433 if (err)
6434 goto done;
6436 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6437 if (err)
6438 goto done;
6440 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6441 if (err)
6442 goto done;
6444 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6445 0);
6446 if (err)
6447 goto done;
6449 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6450 if (err)
6451 goto done;
6452 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6453 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6454 goto done;
6457 err = got_ref_alloc_symref(new_base_branch_ref,
6458 new_base_branch_ref_name, wt_branch);
6459 if (err)
6460 goto done;
6461 err = got_ref_write(*new_base_branch_ref, repo);
6462 if (err)
6463 goto done;
6465 /* TODO Lock original branch's ref while rebasing? */
6467 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6468 if (err)
6469 goto done;
6471 err = got_ref_write(branch_ref, repo);
6472 if (err)
6473 goto done;
6475 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6476 worktree->base_commit_id);
6477 if (err)
6478 goto done;
6479 err = got_ref_write(*tmp_branch, repo);
6480 if (err)
6481 goto done;
6483 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6484 if (err)
6485 goto done;
6486 done:
6487 free(fileindex_path);
6488 free(tmp_branch_name);
6489 free(new_base_branch_ref_name);
6490 free(branch_ref_name);
6491 if (branch_ref)
6492 got_ref_close(branch_ref);
6493 if (wt_branch)
6494 got_ref_close(wt_branch);
6495 free(wt_branch_tip);
6496 if (err) {
6497 if (*new_base_branch_ref) {
6498 got_ref_close(*new_base_branch_ref);
6499 *new_base_branch_ref = NULL;
6501 if (*tmp_branch) {
6502 got_ref_close(*tmp_branch);
6503 *tmp_branch = NULL;
6505 if (*fileindex) {
6506 got_fileindex_free(*fileindex);
6507 *fileindex = NULL;
6509 lock_worktree(worktree, LOCK_SH);
6511 return err;
6514 const struct got_error *
6515 got_worktree_rebase_continue(struct got_object_id **commit_id,
6516 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6517 struct got_reference **branch, struct got_fileindex **fileindex,
6518 struct got_worktree *worktree, struct got_repository *repo)
6520 const struct got_error *err;
6521 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6522 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6523 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6524 char *fileindex_path = NULL;
6525 int have_staged_files = 0;
6527 *commit_id = NULL;
6528 *new_base_branch = NULL;
6529 *tmp_branch = NULL;
6530 *branch = NULL;
6531 *fileindex = NULL;
6533 err = lock_worktree(worktree, LOCK_EX);
6534 if (err)
6535 return err;
6537 err = open_fileindex(fileindex, &fileindex_path, worktree);
6538 if (err)
6539 goto done;
6541 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6542 &have_staged_files);
6543 if (err && err->code != GOT_ERR_CANCELLED)
6544 goto done;
6545 if (have_staged_files) {
6546 err = got_error(GOT_ERR_STAGED_PATHS);
6547 goto done;
6550 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6551 if (err)
6552 goto done;
6554 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6555 if (err)
6556 goto done;
6558 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6559 if (err)
6560 goto done;
6562 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6563 if (err)
6564 goto done;
6566 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6567 if (err)
6568 goto done;
6570 err = got_ref_open(branch, repo,
6571 got_ref_get_symref_target(branch_ref), 0);
6572 if (err)
6573 goto done;
6575 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6576 if (err)
6577 goto done;
6579 err = got_ref_resolve(commit_id, repo, commit_ref);
6580 if (err)
6581 goto done;
6583 err = got_ref_open(new_base_branch, repo,
6584 new_base_branch_ref_name, 0);
6585 if (err)
6586 goto done;
6588 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6589 if (err)
6590 goto done;
6591 done:
6592 free(commit_ref_name);
6593 free(branch_ref_name);
6594 free(fileindex_path);
6595 if (commit_ref)
6596 got_ref_close(commit_ref);
6597 if (branch_ref)
6598 got_ref_close(branch_ref);
6599 if (err) {
6600 free(*commit_id);
6601 *commit_id = NULL;
6602 if (*tmp_branch) {
6603 got_ref_close(*tmp_branch);
6604 *tmp_branch = NULL;
6606 if (*new_base_branch) {
6607 got_ref_close(*new_base_branch);
6608 *new_base_branch = NULL;
6610 if (*branch) {
6611 got_ref_close(*branch);
6612 *branch = NULL;
6614 if (*fileindex) {
6615 got_fileindex_free(*fileindex);
6616 *fileindex = NULL;
6618 lock_worktree(worktree, LOCK_SH);
6620 return err;
6623 const struct got_error *
6624 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6626 const struct got_error *err;
6627 char *tmp_branch_name = NULL;
6629 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6630 if (err)
6631 return err;
6633 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6634 free(tmp_branch_name);
6635 return NULL;
6638 static const struct got_error *
6639 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6640 const char *diff_path, char **logmsg, void *arg)
6642 *logmsg = arg;
6643 return NULL;
6646 static const struct got_error *
6647 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6648 const char *path, struct got_object_id *blob_id,
6649 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6650 int dirfd, const char *de_name)
6652 return NULL;
6655 struct collect_merged_paths_arg {
6656 got_worktree_checkout_cb progress_cb;
6657 void *progress_arg;
6658 struct got_pathlist_head *merged_paths;
6661 static const struct got_error *
6662 collect_merged_paths(void *arg, unsigned char status, const char *path)
6664 const struct got_error *err;
6665 struct collect_merged_paths_arg *a = arg;
6666 char *p;
6667 struct got_pathlist_entry *new;
6669 err = (*a->progress_cb)(a->progress_arg, status, path);
6670 if (err)
6671 return err;
6673 if (status != GOT_STATUS_MERGE &&
6674 status != GOT_STATUS_ADD &&
6675 status != GOT_STATUS_DELETE &&
6676 status != GOT_STATUS_CONFLICT)
6677 return NULL;
6679 p = strdup(path);
6680 if (p == NULL)
6681 return got_error_from_errno("strdup");
6683 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6684 if (err || new == NULL)
6685 free(p);
6686 return err;
6689 static const struct got_error *
6690 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6691 int is_rebase, struct got_repository *repo)
6693 const struct got_error *err;
6694 struct got_reference *commit_ref = NULL;
6696 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6697 if (err) {
6698 if (err->code != GOT_ERR_NOT_REF)
6699 goto done;
6700 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6701 if (err)
6702 goto done;
6703 err = got_ref_write(commit_ref, repo);
6704 if (err)
6705 goto done;
6706 } else if (is_rebase) {
6707 struct got_object_id *stored_id;
6708 int cmp;
6710 err = got_ref_resolve(&stored_id, repo, commit_ref);
6711 if (err)
6712 goto done;
6713 cmp = got_object_id_cmp(commit_id, stored_id);
6714 free(stored_id);
6715 if (cmp != 0) {
6716 err = got_error(GOT_ERR_REBASE_COMMITID);
6717 goto done;
6720 done:
6721 if (commit_ref)
6722 got_ref_close(commit_ref);
6723 return err;
6726 static const struct got_error *
6727 rebase_merge_files(struct got_pathlist_head *merged_paths,
6728 const char *commit_ref_name, struct got_worktree *worktree,
6729 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6730 struct got_object_id *commit_id, struct got_repository *repo,
6731 got_worktree_checkout_cb progress_cb, void *progress_arg,
6732 got_cancel_cb cancel_cb, void *cancel_arg)
6734 const struct got_error *err;
6735 struct got_reference *commit_ref = NULL;
6736 struct collect_merged_paths_arg cmp_arg;
6737 char *fileindex_path;
6739 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6741 err = get_fileindex_path(&fileindex_path, worktree);
6742 if (err)
6743 return err;
6745 cmp_arg.progress_cb = progress_cb;
6746 cmp_arg.progress_arg = progress_arg;
6747 cmp_arg.merged_paths = merged_paths;
6748 err = merge_files(worktree, fileindex, fileindex_path,
6749 parent_commit_id, commit_id, repo, collect_merged_paths,
6750 &cmp_arg, cancel_cb, cancel_arg);
6751 if (commit_ref)
6752 got_ref_close(commit_ref);
6753 return err;
6756 const struct got_error *
6757 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6758 struct got_worktree *worktree, struct got_fileindex *fileindex,
6759 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6760 struct got_repository *repo,
6761 got_worktree_checkout_cb progress_cb, void *progress_arg,
6762 got_cancel_cb cancel_cb, void *cancel_arg)
6764 const struct got_error *err;
6765 char *commit_ref_name;
6767 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6768 if (err)
6769 return err;
6771 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6772 if (err)
6773 goto done;
6775 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6776 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6777 progress_arg, cancel_cb, cancel_arg);
6778 done:
6779 free(commit_ref_name);
6780 return err;
6783 const struct got_error *
6784 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6785 struct got_worktree *worktree, struct got_fileindex *fileindex,
6786 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6787 struct got_repository *repo,
6788 got_worktree_checkout_cb progress_cb, void *progress_arg,
6789 got_cancel_cb cancel_cb, void *cancel_arg)
6791 const struct got_error *err;
6792 char *commit_ref_name;
6794 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6795 if (err)
6796 return err;
6798 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6799 if (err)
6800 goto done;
6802 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6803 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6804 progress_arg, cancel_cb, cancel_arg);
6805 done:
6806 free(commit_ref_name);
6807 return err;
6810 static const struct got_error *
6811 rebase_commit(struct got_object_id **new_commit_id,
6812 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6813 struct got_worktree *worktree, struct got_fileindex *fileindex,
6814 struct got_reference *tmp_branch, const char *committer,
6815 struct got_commit_object *orig_commit, const char *new_logmsg,
6816 int allow_conflict, struct got_repository *repo)
6818 const struct got_error *err, *sync_err;
6819 struct got_pathlist_head commitable_paths;
6820 struct collect_commitables_arg cc_arg;
6821 char *fileindex_path = NULL;
6822 struct got_reference *head_ref = NULL;
6823 struct got_object_id *head_commit_id = NULL;
6824 char *logmsg = NULL;
6826 memset(&cc_arg, 0, sizeof(cc_arg));
6827 TAILQ_INIT(&commitable_paths);
6828 *new_commit_id = NULL;
6830 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6832 err = get_fileindex_path(&fileindex_path, worktree);
6833 if (err)
6834 return err;
6836 cc_arg.commitable_paths = &commitable_paths;
6837 cc_arg.worktree = worktree;
6838 cc_arg.repo = repo;
6839 cc_arg.have_staged_files = 0;
6840 cc_arg.commit_conflicts = allow_conflict;
6842 * If possible get the status of individual files directly to
6843 * avoid crawling the entire work tree once per rebased commit.
6845 * Ideally, merged_paths would contain a list of commitables
6846 * we could use so we could skip worktree_status() entirely.
6847 * However, we would then need carefully keep track of cumulative
6848 * effects of operations such as file additions and deletions
6849 * in 'got histedit -f' (folding multiple commits into one),
6850 * and this extra complexity is not really worth it.
6852 if (merged_paths) {
6853 struct got_pathlist_entry *pe;
6854 TAILQ_FOREACH(pe, merged_paths, entry) {
6855 err = worktree_status(worktree, pe->path, fileindex,
6856 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6857 0);
6858 if (err)
6859 goto done;
6861 } else {
6862 err = worktree_status(worktree, "", fileindex, repo,
6863 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6864 if (err)
6865 goto done;
6868 if (TAILQ_EMPTY(&commitable_paths)) {
6869 /* No-op change; commit will be elided. */
6870 err = got_ref_delete(commit_ref, repo);
6871 if (err)
6872 goto done;
6873 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6874 goto done;
6877 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6878 if (err)
6879 goto done;
6881 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6882 if (err)
6883 goto done;
6885 if (new_logmsg) {
6886 logmsg = strdup(new_logmsg);
6887 if (logmsg == NULL) {
6888 err = got_error_from_errno("strdup");
6889 goto done;
6891 } else {
6892 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6893 if (err)
6894 goto done;
6897 /* NB: commit_worktree will call free(logmsg) */
6898 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6899 NULL, worktree, got_object_commit_get_author(orig_commit),
6900 committer ? committer :
6901 got_object_commit_get_committer(orig_commit), NULL,
6902 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6903 if (err)
6904 goto done;
6906 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6907 if (err)
6908 goto done;
6910 err = got_ref_delete(commit_ref, repo);
6911 if (err)
6912 goto done;
6914 err = update_fileindex_after_commit(worktree, &commitable_paths,
6915 *new_commit_id, fileindex, 0);
6916 sync_err = sync_fileindex(fileindex, fileindex_path);
6917 if (sync_err && err == NULL)
6918 err = sync_err;
6919 done:
6920 free(fileindex_path);
6921 free(head_commit_id);
6922 if (head_ref)
6923 got_ref_close(head_ref);
6924 if (err) {
6925 free(*new_commit_id);
6926 *new_commit_id = NULL;
6928 return err;
6931 const struct got_error *
6932 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6933 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6934 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6935 const char *committer, struct got_commit_object *orig_commit,
6936 struct got_object_id *orig_commit_id, int allow_conflict,
6937 struct got_repository *repo)
6939 const struct got_error *err;
6940 char *commit_ref_name;
6941 struct got_reference *commit_ref = NULL;
6942 struct got_object_id *commit_id = NULL;
6944 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6945 if (err)
6946 return err;
6948 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6949 if (err)
6950 goto done;
6951 err = got_ref_resolve(&commit_id, repo, commit_ref);
6952 if (err)
6953 goto done;
6954 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6955 err = got_error(GOT_ERR_REBASE_COMMITID);
6956 goto done;
6959 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6960 worktree, fileindex, tmp_branch, committer, orig_commit,
6961 NULL, allow_conflict, repo);
6962 done:
6963 if (commit_ref)
6964 got_ref_close(commit_ref);
6965 free(commit_ref_name);
6966 free(commit_id);
6967 return err;
6970 const struct got_error *
6971 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6972 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6973 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6974 const char *committer, struct got_commit_object *orig_commit,
6975 struct got_object_id *orig_commit_id, const char *new_logmsg,
6976 int allow_conflict, struct got_repository *repo)
6978 const struct got_error *err;
6979 char *commit_ref_name;
6980 struct got_reference *commit_ref = NULL;
6982 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6983 if (err)
6984 return err;
6986 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6987 if (err)
6988 goto done;
6990 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6991 worktree, fileindex, tmp_branch, committer, orig_commit,
6992 new_logmsg, allow_conflict, repo);
6993 done:
6994 if (commit_ref)
6995 got_ref_close(commit_ref);
6996 free(commit_ref_name);
6997 return err;
7000 const struct got_error *
7001 got_worktree_rebase_postpone(struct got_worktree *worktree,
7002 struct got_fileindex *fileindex)
7004 if (fileindex)
7005 got_fileindex_free(fileindex);
7006 return lock_worktree(worktree, LOCK_SH);
7009 static const struct got_error *
7010 delete_ref(const char *name, struct got_repository *repo)
7012 const struct got_error *err;
7013 struct got_reference *ref;
7015 err = got_ref_open(&ref, repo, name, 0);
7016 if (err) {
7017 if (err->code == GOT_ERR_NOT_REF)
7018 return NULL;
7019 return err;
7022 err = got_ref_delete(ref, repo);
7023 got_ref_close(ref);
7024 return err;
7027 static const struct got_error *
7028 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7030 const struct got_error *err;
7031 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7032 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7034 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7035 if (err)
7036 goto done;
7037 err = delete_ref(tmp_branch_name, repo);
7038 if (err)
7039 goto done;
7041 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7042 if (err)
7043 goto done;
7044 err = delete_ref(new_base_branch_ref_name, repo);
7045 if (err)
7046 goto done;
7048 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7049 if (err)
7050 goto done;
7051 err = delete_ref(branch_ref_name, repo);
7052 if (err)
7053 goto done;
7055 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7056 if (err)
7057 goto done;
7058 err = delete_ref(commit_ref_name, repo);
7059 if (err)
7060 goto done;
7062 done:
7063 free(tmp_branch_name);
7064 free(new_base_branch_ref_name);
7065 free(branch_ref_name);
7066 free(commit_ref_name);
7067 return err;
7070 static const struct got_error *
7071 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7072 struct got_object_id *new_commit_id, struct got_repository *repo)
7074 const struct got_error *err;
7075 struct got_reference *ref = NULL;
7076 struct got_object_id *old_commit_id = NULL;
7077 const char *branch_name = NULL;
7078 char *new_id_str = NULL;
7079 char *refname = NULL;
7081 branch_name = got_ref_get_name(branch);
7082 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7083 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7084 branch_name += 11;
7086 err = got_object_id_str(&new_id_str, new_commit_id);
7087 if (err)
7088 return err;
7090 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7091 new_id_str) == -1) {
7092 err = got_error_from_errno("asprintf");
7093 goto done;
7096 err = got_ref_resolve(&old_commit_id, repo, branch);
7097 if (err)
7098 goto done;
7100 err = got_ref_alloc(&ref, refname, old_commit_id);
7101 if (err)
7102 goto done;
7104 err = got_ref_write(ref, repo);
7105 done:
7106 free(new_id_str);
7107 free(refname);
7108 free(old_commit_id);
7109 if (ref)
7110 got_ref_close(ref);
7111 return err;
7114 const struct got_error *
7115 got_worktree_rebase_complete(struct got_worktree *worktree,
7116 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7117 struct got_reference *rebased_branch, struct got_repository *repo,
7118 int create_backup)
7120 const struct got_error *err, *unlockerr, *sync_err;
7121 struct got_object_id *new_head_commit_id = NULL;
7122 char *fileindex_path = NULL;
7124 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7125 if (err)
7126 return err;
7128 if (create_backup) {
7129 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7130 rebased_branch, new_head_commit_id, repo);
7131 if (err)
7132 goto done;
7135 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7136 if (err)
7137 goto done;
7139 err = got_ref_write(rebased_branch, repo);
7140 if (err)
7141 goto done;
7143 err = got_worktree_set_head_ref(worktree, rebased_branch);
7144 if (err)
7145 goto done;
7147 err = delete_rebase_refs(worktree, repo);
7148 if (err)
7149 goto done;
7151 err = get_fileindex_path(&fileindex_path, worktree);
7152 if (err)
7153 goto done;
7154 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7155 sync_err = sync_fileindex(fileindex, fileindex_path);
7156 if (sync_err && err == NULL)
7157 err = sync_err;
7158 done:
7159 got_fileindex_free(fileindex);
7160 free(fileindex_path);
7161 free(new_head_commit_id);
7162 unlockerr = lock_worktree(worktree, LOCK_SH);
7163 if (unlockerr && err == NULL)
7164 err = unlockerr;
7165 return err;
7168 const struct got_error *
7169 got_worktree_rebase_abort(struct got_worktree *worktree,
7170 struct got_fileindex *fileindex, struct got_repository *repo,
7171 struct got_reference *new_base_branch,
7172 got_worktree_checkout_cb progress_cb, void *progress_arg)
7174 const struct got_error *err, *unlockerr, *sync_err;
7175 struct got_reference *resolved = NULL;
7176 struct got_object_id *commit_id = NULL;
7177 struct got_commit_object *commit = NULL;
7178 char *fileindex_path = NULL;
7179 struct revert_file_args rfa;
7180 struct got_object_id *tree_id = NULL;
7182 err = lock_worktree(worktree, LOCK_EX);
7183 if (err)
7184 return err;
7186 err = got_object_open_as_commit(&commit, repo,
7187 worktree->base_commit_id);
7188 if (err)
7189 goto done;
7191 err = got_ref_open(&resolved, repo,
7192 got_ref_get_symref_target(new_base_branch), 0);
7193 if (err)
7194 goto done;
7196 err = got_worktree_set_head_ref(worktree, resolved);
7197 if (err)
7198 goto done;
7201 * XXX commits to the base branch could have happened while
7202 * we were busy rebasing; should we store the original commit ID
7203 * when rebase begins and read it back here?
7205 err = got_ref_resolve(&commit_id, repo, resolved);
7206 if (err)
7207 goto done;
7209 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7210 if (err)
7211 goto done;
7213 err = got_object_id_by_path(&tree_id, repo, commit,
7214 worktree->path_prefix);
7215 if (err)
7216 goto done;
7218 err = delete_rebase_refs(worktree, repo);
7219 if (err)
7220 goto done;
7222 err = get_fileindex_path(&fileindex_path, worktree);
7223 if (err)
7224 goto done;
7226 rfa.worktree = worktree;
7227 rfa.fileindex = fileindex;
7228 rfa.progress_cb = progress_cb;
7229 rfa.progress_arg = progress_arg;
7230 rfa.patch_cb = NULL;
7231 rfa.patch_arg = NULL;
7232 rfa.repo = repo;
7233 rfa.unlink_added_files = 0;
7234 err = worktree_status(worktree, "", fileindex, repo,
7235 revert_file, &rfa, NULL, NULL, 1, 0);
7236 if (err)
7237 goto sync;
7239 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7240 repo, progress_cb, progress_arg, NULL, NULL);
7241 sync:
7242 sync_err = sync_fileindex(fileindex, fileindex_path);
7243 if (sync_err && err == NULL)
7244 err = sync_err;
7245 done:
7246 got_ref_close(resolved);
7247 free(tree_id);
7248 free(commit_id);
7249 if (commit)
7250 got_object_commit_close(commit);
7251 if (fileindex)
7252 got_fileindex_free(fileindex);
7253 free(fileindex_path);
7255 unlockerr = lock_worktree(worktree, LOCK_SH);
7256 if (unlockerr && err == NULL)
7257 err = unlockerr;
7258 return err;
7261 const struct got_error *
7262 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7263 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7264 struct got_fileindex **fileindex, struct got_worktree *worktree,
7265 struct got_repository *repo)
7267 const struct got_error *err = NULL;
7268 char *tmp_branch_name = NULL;
7269 char *branch_ref_name = NULL;
7270 char *base_commit_ref_name = NULL;
7271 char *fileindex_path = NULL;
7272 struct check_rebase_ok_arg ok_arg;
7273 struct got_reference *wt_branch = NULL;
7274 struct got_reference *base_commit_ref = NULL;
7276 *tmp_branch = NULL;
7277 *branch_ref = NULL;
7278 *base_commit_id = NULL;
7279 *fileindex = NULL;
7281 err = lock_worktree(worktree, LOCK_EX);
7282 if (err)
7283 return err;
7285 err = open_fileindex(fileindex, &fileindex_path, worktree);
7286 if (err)
7287 goto done;
7289 ok_arg.worktree = worktree;
7290 ok_arg.repo = repo;
7291 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7292 &ok_arg);
7293 if (err)
7294 goto done;
7296 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7297 if (err)
7298 goto done;
7300 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7301 if (err)
7302 goto done;
7304 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7305 worktree);
7306 if (err)
7307 goto done;
7309 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7310 0);
7311 if (err)
7312 goto done;
7314 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7315 if (err)
7316 goto done;
7318 err = got_ref_write(*branch_ref, repo);
7319 if (err)
7320 goto done;
7322 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7323 worktree->base_commit_id);
7324 if (err)
7325 goto done;
7326 err = got_ref_write(base_commit_ref, repo);
7327 if (err)
7328 goto done;
7329 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7330 if (*base_commit_id == NULL) {
7331 err = got_error_from_errno("got_object_id_dup");
7332 goto done;
7335 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7336 worktree->base_commit_id);
7337 if (err)
7338 goto done;
7339 err = got_ref_write(*tmp_branch, repo);
7340 if (err)
7341 goto done;
7343 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7344 if (err)
7345 goto done;
7346 done:
7347 free(fileindex_path);
7348 free(tmp_branch_name);
7349 free(branch_ref_name);
7350 free(base_commit_ref_name);
7351 if (wt_branch)
7352 got_ref_close(wt_branch);
7353 if (err) {
7354 if (*branch_ref) {
7355 got_ref_close(*branch_ref);
7356 *branch_ref = NULL;
7358 if (*tmp_branch) {
7359 got_ref_close(*tmp_branch);
7360 *tmp_branch = NULL;
7362 free(*base_commit_id);
7363 if (*fileindex) {
7364 got_fileindex_free(*fileindex);
7365 *fileindex = NULL;
7367 lock_worktree(worktree, LOCK_SH);
7369 return err;
7372 const struct got_error *
7373 got_worktree_histedit_postpone(struct got_worktree *worktree,
7374 struct got_fileindex *fileindex)
7376 if (fileindex)
7377 got_fileindex_free(fileindex);
7378 return lock_worktree(worktree, LOCK_SH);
7381 const struct got_error *
7382 got_worktree_histedit_in_progress(int *in_progress,
7383 struct got_worktree *worktree)
7385 const struct got_error *err;
7386 char *tmp_branch_name = NULL;
7388 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7389 if (err)
7390 return err;
7392 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7393 free(tmp_branch_name);
7394 return NULL;
7397 const struct got_error *
7398 got_worktree_histedit_continue(struct got_object_id **commit_id,
7399 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7400 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7401 struct got_worktree *worktree, struct got_repository *repo)
7403 const struct got_error *err;
7404 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7405 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7406 struct got_reference *commit_ref = NULL;
7407 struct got_reference *base_commit_ref = NULL;
7408 char *fileindex_path = NULL;
7409 int have_staged_files = 0;
7411 *commit_id = NULL;
7412 *tmp_branch = NULL;
7413 *base_commit_id = NULL;
7414 *fileindex = NULL;
7416 err = lock_worktree(worktree, LOCK_EX);
7417 if (err)
7418 return err;
7420 err = open_fileindex(fileindex, &fileindex_path, worktree);
7421 if (err)
7422 goto done;
7424 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7425 &have_staged_files);
7426 if (err && err->code != GOT_ERR_CANCELLED)
7427 goto done;
7428 if (have_staged_files) {
7429 err = got_error(GOT_ERR_STAGED_PATHS);
7430 goto done;
7433 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7434 if (err)
7435 goto done;
7437 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7438 if (err)
7439 goto done;
7441 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7442 if (err)
7443 goto done;
7445 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7446 worktree);
7447 if (err)
7448 goto done;
7450 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7451 if (err)
7452 goto done;
7454 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7455 if (err)
7456 goto done;
7457 err = got_ref_resolve(commit_id, repo, commit_ref);
7458 if (err)
7459 goto done;
7461 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7462 if (err)
7463 goto done;
7464 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7465 if (err)
7466 goto done;
7468 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7469 if (err)
7470 goto done;
7471 done:
7472 free(commit_ref_name);
7473 free(branch_ref_name);
7474 free(fileindex_path);
7475 if (commit_ref)
7476 got_ref_close(commit_ref);
7477 if (base_commit_ref)
7478 got_ref_close(base_commit_ref);
7479 if (err) {
7480 free(*commit_id);
7481 *commit_id = NULL;
7482 free(*base_commit_id);
7483 *base_commit_id = NULL;
7484 if (*tmp_branch) {
7485 got_ref_close(*tmp_branch);
7486 *tmp_branch = NULL;
7488 if (*fileindex) {
7489 got_fileindex_free(*fileindex);
7490 *fileindex = NULL;
7492 lock_worktree(worktree, LOCK_EX);
7494 return err;
7497 static const struct got_error *
7498 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7500 const struct got_error *err;
7501 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7502 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7504 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7505 if (err)
7506 goto done;
7507 err = delete_ref(tmp_branch_name, repo);
7508 if (err)
7509 goto done;
7511 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7512 worktree);
7513 if (err)
7514 goto done;
7515 err = delete_ref(base_commit_ref_name, repo);
7516 if (err)
7517 goto done;
7519 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7520 if (err)
7521 goto done;
7522 err = delete_ref(branch_ref_name, repo);
7523 if (err)
7524 goto done;
7526 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7527 if (err)
7528 goto done;
7529 err = delete_ref(commit_ref_name, repo);
7530 if (err)
7531 goto done;
7532 done:
7533 free(tmp_branch_name);
7534 free(base_commit_ref_name);
7535 free(branch_ref_name);
7536 free(commit_ref_name);
7537 return err;
7540 const struct got_error *
7541 got_worktree_histedit_abort(struct got_worktree *worktree,
7542 struct got_fileindex *fileindex, struct got_repository *repo,
7543 struct got_reference *branch, struct got_object_id *base_commit_id,
7544 got_worktree_checkout_cb progress_cb, void *progress_arg)
7546 const struct got_error *err, *unlockerr, *sync_err;
7547 struct got_reference *resolved = NULL;
7548 char *fileindex_path = NULL;
7549 struct got_commit_object *commit = NULL;
7550 struct got_object_id *tree_id = NULL;
7551 struct revert_file_args rfa;
7553 err = lock_worktree(worktree, LOCK_EX);
7554 if (err)
7555 return err;
7557 err = got_object_open_as_commit(&commit, repo,
7558 worktree->base_commit_id);
7559 if (err)
7560 goto done;
7562 err = got_ref_open(&resolved, repo,
7563 got_ref_get_symref_target(branch), 0);
7564 if (err)
7565 goto done;
7567 err = got_worktree_set_head_ref(worktree, resolved);
7568 if (err)
7569 goto done;
7571 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7572 if (err)
7573 goto done;
7575 err = got_object_id_by_path(&tree_id, repo, commit,
7576 worktree->path_prefix);
7577 if (err)
7578 goto done;
7580 err = delete_histedit_refs(worktree, repo);
7581 if (err)
7582 goto done;
7584 err = get_fileindex_path(&fileindex_path, worktree);
7585 if (err)
7586 goto done;
7588 rfa.worktree = worktree;
7589 rfa.fileindex = fileindex;
7590 rfa.progress_cb = progress_cb;
7591 rfa.progress_arg = progress_arg;
7592 rfa.patch_cb = NULL;
7593 rfa.patch_arg = NULL;
7594 rfa.repo = repo;
7595 rfa.unlink_added_files = 0;
7596 err = worktree_status(worktree, "", fileindex, repo,
7597 revert_file, &rfa, NULL, NULL, 1, 0);
7598 if (err)
7599 goto sync;
7601 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7602 repo, progress_cb, progress_arg, NULL, NULL);
7603 sync:
7604 sync_err = sync_fileindex(fileindex, fileindex_path);
7605 if (sync_err && err == NULL)
7606 err = sync_err;
7607 done:
7608 got_ref_close(resolved);
7609 free(tree_id);
7610 free(fileindex_path);
7612 unlockerr = lock_worktree(worktree, LOCK_SH);
7613 if (unlockerr && err == NULL)
7614 err = unlockerr;
7615 return err;
7618 const struct got_error *
7619 got_worktree_histedit_complete(struct got_worktree *worktree,
7620 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7621 struct got_reference *edited_branch, struct got_repository *repo)
7623 const struct got_error *err, *unlockerr, *sync_err;
7624 struct got_object_id *new_head_commit_id = NULL;
7625 struct got_reference *resolved = NULL;
7626 char *fileindex_path = NULL;
7628 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7629 if (err)
7630 return err;
7632 err = got_ref_open(&resolved, repo,
7633 got_ref_get_symref_target(edited_branch), 0);
7634 if (err)
7635 goto done;
7637 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7638 resolved, new_head_commit_id, repo);
7639 if (err)
7640 goto done;
7642 err = got_ref_change_ref(resolved, new_head_commit_id);
7643 if (err)
7644 goto done;
7646 err = got_ref_write(resolved, repo);
7647 if (err)
7648 goto done;
7650 err = got_worktree_set_head_ref(worktree, resolved);
7651 if (err)
7652 goto done;
7654 err = delete_histedit_refs(worktree, repo);
7655 if (err)
7656 goto done;
7658 err = get_fileindex_path(&fileindex_path, worktree);
7659 if (err)
7660 goto done;
7661 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7662 sync_err = sync_fileindex(fileindex, fileindex_path);
7663 if (sync_err && err == NULL)
7664 err = sync_err;
7665 done:
7666 got_fileindex_free(fileindex);
7667 free(fileindex_path);
7668 free(new_head_commit_id);
7669 unlockerr = lock_worktree(worktree, LOCK_SH);
7670 if (unlockerr && err == NULL)
7671 err = unlockerr;
7672 return err;
7675 const struct got_error *
7676 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7677 struct got_object_id *commit_id, struct got_repository *repo)
7679 const struct got_error *err;
7680 char *commit_ref_name;
7682 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7683 if (err)
7684 return err;
7686 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7687 if (err)
7688 goto done;
7690 err = delete_ref(commit_ref_name, repo);
7691 done:
7692 free(commit_ref_name);
7693 return err;
7696 const struct got_error *
7697 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7698 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7699 struct got_worktree *worktree, const char *refname,
7700 struct got_repository *repo)
7702 const struct got_error *err = NULL;
7703 char *fileindex_path = NULL;
7704 struct check_rebase_ok_arg ok_arg;
7706 *fileindex = NULL;
7707 *branch_ref = NULL;
7708 *base_branch_ref = NULL;
7710 err = lock_worktree(worktree, LOCK_EX);
7711 if (err)
7712 return err;
7714 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7715 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7716 "cannot integrate a branch into itself; "
7717 "update -b or different branch name required");
7718 goto done;
7721 err = open_fileindex(fileindex, &fileindex_path, worktree);
7722 if (err)
7723 goto done;
7725 /* Preconditions are the same as for rebase. */
7726 ok_arg.worktree = worktree;
7727 ok_arg.repo = repo;
7728 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7729 &ok_arg);
7730 if (err)
7731 goto done;
7733 err = got_ref_open(branch_ref, repo, refname, 1);
7734 if (err)
7735 goto done;
7737 err = got_ref_open(base_branch_ref, repo,
7738 got_worktree_get_head_ref_name(worktree), 1);
7739 done:
7740 if (err) {
7741 if (*branch_ref) {
7742 got_ref_close(*branch_ref);
7743 *branch_ref = NULL;
7745 if (*base_branch_ref) {
7746 got_ref_close(*base_branch_ref);
7747 *base_branch_ref = NULL;
7749 if (*fileindex) {
7750 got_fileindex_free(*fileindex);
7751 *fileindex = NULL;
7753 lock_worktree(worktree, LOCK_SH);
7755 return err;
7758 const struct got_error *
7759 got_worktree_integrate_continue(struct got_worktree *worktree,
7760 struct got_fileindex *fileindex, struct got_repository *repo,
7761 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7762 got_worktree_checkout_cb progress_cb, void *progress_arg,
7763 got_cancel_cb cancel_cb, void *cancel_arg)
7765 const struct got_error *err = NULL, *sync_err, *unlockerr;
7766 char *fileindex_path = NULL;
7767 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7768 struct got_commit_object *commit = NULL;
7770 err = get_fileindex_path(&fileindex_path, worktree);
7771 if (err)
7772 goto done;
7774 err = got_ref_resolve(&commit_id, repo, branch_ref);
7775 if (err)
7776 goto done;
7778 err = got_object_open_as_commit(&commit, repo, commit_id);
7779 if (err)
7780 goto done;
7782 err = got_object_id_by_path(&tree_id, repo, commit,
7783 worktree->path_prefix);
7784 if (err)
7785 goto done;
7787 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7788 if (err)
7789 goto done;
7791 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7792 progress_cb, progress_arg, cancel_cb, cancel_arg);
7793 if (err)
7794 goto sync;
7796 err = got_ref_change_ref(base_branch_ref, commit_id);
7797 if (err)
7798 goto sync;
7800 err = got_ref_write(base_branch_ref, repo);
7801 if (err)
7802 goto sync;
7804 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7805 sync:
7806 sync_err = sync_fileindex(fileindex, fileindex_path);
7807 if (sync_err && err == NULL)
7808 err = sync_err;
7810 done:
7811 unlockerr = got_ref_unlock(branch_ref);
7812 if (unlockerr && err == NULL)
7813 err = unlockerr;
7814 got_ref_close(branch_ref);
7816 unlockerr = got_ref_unlock(base_branch_ref);
7817 if (unlockerr && err == NULL)
7818 err = unlockerr;
7819 got_ref_close(base_branch_ref);
7821 got_fileindex_free(fileindex);
7822 free(fileindex_path);
7823 free(tree_id);
7824 if (commit)
7825 got_object_commit_close(commit);
7827 unlockerr = lock_worktree(worktree, LOCK_SH);
7828 if (unlockerr && err == NULL)
7829 err = unlockerr;
7830 return err;
7833 const struct got_error *
7834 got_worktree_integrate_abort(struct got_worktree *worktree,
7835 struct got_fileindex *fileindex, struct got_repository *repo,
7836 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7838 const struct got_error *err = NULL, *unlockerr = NULL;
7840 got_fileindex_free(fileindex);
7842 err = lock_worktree(worktree, LOCK_SH);
7844 unlockerr = got_ref_unlock(branch_ref);
7845 if (unlockerr && err == NULL)
7846 err = unlockerr;
7847 got_ref_close(branch_ref);
7849 unlockerr = got_ref_unlock(base_branch_ref);
7850 if (unlockerr && err == NULL)
7851 err = unlockerr;
7852 got_ref_close(base_branch_ref);
7854 return err;
7857 const struct got_error *
7858 got_worktree_merge_postpone(struct got_worktree *worktree,
7859 struct got_fileindex *fileindex)
7861 const struct got_error *err, *sync_err;
7862 char *fileindex_path = NULL;
7864 err = get_fileindex_path(&fileindex_path, worktree);
7865 if (err)
7866 goto done;
7868 sync_err = sync_fileindex(fileindex, fileindex_path);
7870 err = lock_worktree(worktree, LOCK_SH);
7871 if (sync_err && err == NULL)
7872 err = sync_err;
7873 done:
7874 got_fileindex_free(fileindex);
7875 free(fileindex_path);
7876 return err;
7879 static const struct got_error *
7880 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7882 const struct got_error *err;
7883 char *branch_refname = NULL, *commit_refname = NULL;
7885 err = get_merge_branch_ref_name(&branch_refname, worktree);
7886 if (err)
7887 goto done;
7888 err = delete_ref(branch_refname, repo);
7889 if (err)
7890 goto done;
7892 err = get_merge_commit_ref_name(&commit_refname, worktree);
7893 if (err)
7894 goto done;
7895 err = delete_ref(commit_refname, repo);
7896 if (err)
7897 goto done;
7899 done:
7900 free(branch_refname);
7901 free(commit_refname);
7902 return err;
7905 struct merge_commit_msg_arg {
7906 struct got_worktree *worktree;
7907 const char *branch_name;
7910 static const struct got_error *
7911 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7912 const char *diff_path, char **logmsg, void *arg)
7914 struct merge_commit_msg_arg *a = arg;
7916 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7917 got_worktree_get_head_ref_name(a->worktree)) == -1)
7918 return got_error_from_errno("asprintf");
7920 return NULL;
7924 const struct got_error *
7925 got_worktree_merge_branch(struct got_worktree *worktree,
7926 struct got_fileindex *fileindex,
7927 struct got_object_id *yca_commit_id,
7928 struct got_object_id *branch_tip,
7929 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7930 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7932 const struct got_error *err;
7933 char *fileindex_path = NULL;
7935 err = get_fileindex_path(&fileindex_path, worktree);
7936 if (err)
7937 goto done;
7939 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7940 worktree);
7941 if (err)
7942 goto done;
7944 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7945 branch_tip, repo, progress_cb, progress_arg,
7946 cancel_cb, cancel_arg);
7947 done:
7948 free(fileindex_path);
7949 return err;
7952 const struct got_error *
7953 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7954 struct got_worktree *worktree, struct got_fileindex *fileindex,
7955 const char *author, const char *committer, int allow_bad_symlinks,
7956 struct got_object_id *branch_tip, const char *branch_name,
7957 int allow_conflict, struct got_repository *repo,
7958 got_worktree_status_cb status_cb, void *status_arg)
7961 const struct got_error *err = NULL, *sync_err;
7962 struct got_pathlist_head commitable_paths;
7963 struct collect_commitables_arg cc_arg;
7964 struct got_pathlist_entry *pe;
7965 struct got_reference *head_ref = NULL;
7966 struct got_object_id *head_commit_id = NULL;
7967 int have_staged_files = 0;
7968 struct merge_commit_msg_arg mcm_arg;
7969 char *fileindex_path = NULL;
7971 memset(&cc_arg, 0, sizeof(cc_arg));
7972 *new_commit_id = NULL;
7974 TAILQ_INIT(&commitable_paths);
7976 err = get_fileindex_path(&fileindex_path, worktree);
7977 if (err)
7978 goto done;
7980 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7981 if (err)
7982 goto done;
7984 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7985 if (err)
7986 goto done;
7988 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7989 &have_staged_files);
7990 if (err && err->code != GOT_ERR_CANCELLED)
7991 goto done;
7992 if (have_staged_files) {
7993 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7994 goto done;
7997 cc_arg.commitable_paths = &commitable_paths;
7998 cc_arg.worktree = worktree;
7999 cc_arg.fileindex = fileindex;
8000 cc_arg.repo = repo;
8001 cc_arg.have_staged_files = have_staged_files;
8002 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8003 cc_arg.commit_conflicts = allow_conflict;
8004 err = worktree_status(worktree, "", fileindex, repo,
8005 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8006 if (err)
8007 goto done;
8009 if (TAILQ_EMPTY(&commitable_paths)) {
8010 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8011 "merge of %s cannot proceed", branch_name);
8012 goto done;
8015 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8016 struct got_commitable *ct = pe->data;
8017 const char *ct_path = ct->in_repo_path;
8019 while (ct_path[0] == '/')
8020 ct_path++;
8021 err = check_out_of_date(ct_path, ct->status,
8022 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8023 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8024 if (err)
8025 goto done;
8029 mcm_arg.worktree = worktree;
8030 mcm_arg.branch_name = branch_name;
8031 err = commit_worktree(new_commit_id, &commitable_paths,
8032 head_commit_id, branch_tip, worktree, author, committer, NULL,
8033 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8034 if (err)
8035 goto done;
8037 err = update_fileindex_after_commit(worktree, &commitable_paths,
8038 *new_commit_id, fileindex, have_staged_files);
8039 sync_err = sync_fileindex(fileindex, fileindex_path);
8040 if (sync_err && err == NULL)
8041 err = sync_err;
8042 done:
8043 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8044 struct got_commitable *ct = pe->data;
8046 free_commitable(ct);
8048 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8049 free(fileindex_path);
8050 return err;
8053 const struct got_error *
8054 got_worktree_merge_complete(struct got_worktree *worktree,
8055 struct got_fileindex *fileindex, struct got_repository *repo)
8057 const struct got_error *err, *unlockerr, *sync_err;
8058 char *fileindex_path = NULL;
8060 err = delete_merge_refs(worktree, repo);
8061 if (err)
8062 goto done;
8064 err = get_fileindex_path(&fileindex_path, worktree);
8065 if (err)
8066 goto done;
8067 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8068 sync_err = sync_fileindex(fileindex, fileindex_path);
8069 if (sync_err && err == NULL)
8070 err = sync_err;
8071 done:
8072 got_fileindex_free(fileindex);
8073 free(fileindex_path);
8074 unlockerr = lock_worktree(worktree, LOCK_SH);
8075 if (unlockerr && err == NULL)
8076 err = unlockerr;
8077 return err;
8080 const struct got_error *
8081 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8082 struct got_repository *repo)
8084 const struct got_error *err;
8085 char *branch_refname = NULL;
8086 struct got_reference *branch_ref = NULL;
8088 *in_progress = 0;
8090 err = get_merge_branch_ref_name(&branch_refname, worktree);
8091 if (err)
8092 return err;
8093 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8094 free(branch_refname);
8095 if (err) {
8096 if (err->code != GOT_ERR_NOT_REF)
8097 return err;
8098 } else
8099 *in_progress = 1;
8101 return NULL;
8104 const struct got_error *got_worktree_merge_prepare(
8105 struct got_fileindex **fileindex, struct got_worktree *worktree,
8106 struct got_reference *branch, struct got_repository *repo)
8108 const struct got_error *err = NULL;
8109 char *fileindex_path = NULL;
8110 char *branch_refname = NULL, *commit_refname = NULL;
8111 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8112 struct got_reference *commit_ref = NULL;
8113 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8114 struct check_rebase_ok_arg ok_arg;
8116 *fileindex = NULL;
8118 err = lock_worktree(worktree, LOCK_EX);
8119 if (err)
8120 return err;
8122 err = open_fileindex(fileindex, &fileindex_path, worktree);
8123 if (err)
8124 goto done;
8126 /* Preconditions are the same as for rebase. */
8127 ok_arg.worktree = worktree;
8128 ok_arg.repo = repo;
8129 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8130 &ok_arg);
8131 if (err)
8132 goto done;
8134 err = get_merge_branch_ref_name(&branch_refname, worktree);
8135 if (err)
8136 return err;
8138 err = get_merge_commit_ref_name(&commit_refname, worktree);
8139 if (err)
8140 return err;
8142 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8143 0);
8144 if (err)
8145 goto done;
8147 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8148 if (err)
8149 goto done;
8151 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8152 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8153 goto done;
8156 err = got_ref_resolve(&branch_tip, repo, branch);
8157 if (err)
8158 goto done;
8160 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8161 if (err)
8162 goto done;
8163 err = got_ref_write(branch_ref, repo);
8164 if (err)
8165 goto done;
8167 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8168 if (err)
8169 goto done;
8170 err = got_ref_write(commit_ref, repo);
8171 if (err)
8172 goto done;
8174 done:
8175 free(branch_refname);
8176 free(commit_refname);
8177 free(fileindex_path);
8178 if (branch_ref)
8179 got_ref_close(branch_ref);
8180 if (commit_ref)
8181 got_ref_close(commit_ref);
8182 if (wt_branch)
8183 got_ref_close(wt_branch);
8184 free(wt_branch_tip);
8185 if (err) {
8186 if (*fileindex) {
8187 got_fileindex_free(*fileindex);
8188 *fileindex = NULL;
8190 lock_worktree(worktree, LOCK_SH);
8192 return err;
8195 const struct got_error *
8196 got_worktree_merge_continue(char **branch_name,
8197 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8198 struct got_worktree *worktree, struct got_repository *repo)
8200 const struct got_error *err;
8201 char *commit_refname = NULL, *branch_refname = NULL;
8202 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8203 char *fileindex_path = NULL;
8204 int have_staged_files = 0;
8206 *branch_name = NULL;
8207 *branch_tip = NULL;
8208 *fileindex = NULL;
8210 err = lock_worktree(worktree, LOCK_EX);
8211 if (err)
8212 return err;
8214 err = open_fileindex(fileindex, &fileindex_path, worktree);
8215 if (err)
8216 goto done;
8218 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8219 &have_staged_files);
8220 if (err && err->code != GOT_ERR_CANCELLED)
8221 goto done;
8222 if (have_staged_files) {
8223 err = got_error(GOT_ERR_STAGED_PATHS);
8224 goto done;
8227 err = get_merge_branch_ref_name(&branch_refname, worktree);
8228 if (err)
8229 goto done;
8231 err = get_merge_commit_ref_name(&commit_refname, worktree);
8232 if (err)
8233 goto done;
8235 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8236 if (err)
8237 goto done;
8239 if (!got_ref_is_symbolic(branch_ref)) {
8240 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8241 "%s is not a symbolic reference",
8242 got_ref_get_name(branch_ref));
8243 goto done;
8245 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8246 if (*branch_name == NULL) {
8247 err = got_error_from_errno("strdup");
8248 goto done;
8251 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8252 if (err)
8253 goto done;
8255 err = got_ref_resolve(branch_tip, repo, commit_ref);
8256 if (err)
8257 goto done;
8258 done:
8259 free(commit_refname);
8260 free(branch_refname);
8261 free(fileindex_path);
8262 if (commit_ref)
8263 got_ref_close(commit_ref);
8264 if (branch_ref)
8265 got_ref_close(branch_ref);
8266 if (err) {
8267 if (*branch_name) {
8268 free(*branch_name);
8269 *branch_name = NULL;
8271 free(*branch_tip);
8272 *branch_tip = NULL;
8273 if (*fileindex) {
8274 got_fileindex_free(*fileindex);
8275 *fileindex = NULL;
8277 lock_worktree(worktree, LOCK_SH);
8279 return err;
8282 const struct got_error *
8283 got_worktree_merge_abort(struct got_worktree *worktree,
8284 struct got_fileindex *fileindex, struct got_repository *repo,
8285 got_worktree_checkout_cb progress_cb, void *progress_arg)
8287 const struct got_error *err, *unlockerr, *sync_err;
8288 struct got_object_id *commit_id = NULL;
8289 struct got_commit_object *commit = NULL;
8290 char *fileindex_path = NULL;
8291 struct revert_file_args rfa;
8292 struct got_object_id *tree_id = NULL;
8294 err = got_object_open_as_commit(&commit, repo,
8295 worktree->base_commit_id);
8296 if (err)
8297 goto done;
8299 err = got_object_id_by_path(&tree_id, repo, commit,
8300 worktree->path_prefix);
8301 if (err)
8302 goto done;
8304 err = delete_merge_refs(worktree, repo);
8305 if (err)
8306 goto done;
8308 err = get_fileindex_path(&fileindex_path, worktree);
8309 if (err)
8310 goto done;
8312 rfa.worktree = worktree;
8313 rfa.fileindex = fileindex;
8314 rfa.progress_cb = progress_cb;
8315 rfa.progress_arg = progress_arg;
8316 rfa.patch_cb = NULL;
8317 rfa.patch_arg = NULL;
8318 rfa.repo = repo;
8319 rfa.unlink_added_files = 1;
8320 err = worktree_status(worktree, "", fileindex, repo,
8321 revert_file, &rfa, NULL, NULL, 1, 0);
8322 if (err)
8323 goto sync;
8325 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8326 repo, progress_cb, progress_arg, NULL, NULL);
8327 sync:
8328 sync_err = sync_fileindex(fileindex, fileindex_path);
8329 if (sync_err && err == NULL)
8330 err = sync_err;
8331 done:
8332 free(tree_id);
8333 free(commit_id);
8334 if (commit)
8335 got_object_commit_close(commit);
8336 if (fileindex)
8337 got_fileindex_free(fileindex);
8338 free(fileindex_path);
8340 unlockerr = lock_worktree(worktree, LOCK_SH);
8341 if (unlockerr && err == NULL)
8342 err = unlockerr;
8343 return err;
8346 struct check_stage_ok_arg {
8347 struct got_object_id *head_commit_id;
8348 struct got_worktree *worktree;
8349 struct got_fileindex *fileindex;
8350 struct got_repository *repo;
8351 int have_changes;
8354 static const struct got_error *
8355 check_stage_ok(void *arg, unsigned char status,
8356 unsigned char staged_status, const char *relpath,
8357 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8358 struct got_object_id *commit_id, int dirfd, const char *de_name)
8360 struct check_stage_ok_arg *a = arg;
8361 const struct got_error *err = NULL;
8362 struct got_fileindex_entry *ie;
8363 struct got_object_id base_commit_id;
8364 struct got_object_id *base_commit_idp = NULL;
8365 char *in_repo_path = NULL, *p;
8367 if (status == GOT_STATUS_UNVERSIONED ||
8368 status == GOT_STATUS_NO_CHANGE)
8369 return NULL;
8370 if (status == GOT_STATUS_NONEXISTENT)
8371 return got_error_set_errno(ENOENT, relpath);
8373 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8374 if (ie == NULL)
8375 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8377 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8378 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8379 relpath) == -1)
8380 return got_error_from_errno("asprintf");
8382 if (got_fileindex_entry_has_commit(ie)) {
8383 base_commit_idp = got_fileindex_entry_get_commit_id(
8384 &base_commit_id, ie);
8387 if (status == GOT_STATUS_CONFLICT) {
8388 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8389 goto done;
8390 } else if (status != GOT_STATUS_ADD &&
8391 status != GOT_STATUS_MODIFY &&
8392 status != GOT_STATUS_DELETE) {
8393 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8394 goto done;
8397 a->have_changes = 1;
8399 p = in_repo_path;
8400 while (p[0] == '/')
8401 p++;
8402 err = check_out_of_date(p, status, staged_status,
8403 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8404 GOT_ERR_STAGE_OUT_OF_DATE);
8405 done:
8406 free(in_repo_path);
8407 return err;
8410 struct stage_path_arg {
8411 struct got_worktree *worktree;
8412 struct got_fileindex *fileindex;
8413 struct got_repository *repo;
8414 got_worktree_status_cb status_cb;
8415 void *status_arg;
8416 got_worktree_patch_cb patch_cb;
8417 void *patch_arg;
8418 int staged_something;
8419 int allow_bad_symlinks;
8422 static const struct got_error *
8423 stage_path(void *arg, unsigned char status,
8424 unsigned char staged_status, const char *relpath,
8425 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8426 struct got_object_id *commit_id, int dirfd, const char *de_name)
8428 struct stage_path_arg *a = arg;
8429 const struct got_error *err = NULL;
8430 struct got_fileindex_entry *ie;
8431 char *ondisk_path = NULL, *path_content = NULL;
8432 uint32_t stage;
8433 struct got_object_id *new_staged_blob_id = NULL;
8434 struct stat sb;
8436 if (status == GOT_STATUS_UNVERSIONED)
8437 return NULL;
8439 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8440 if (ie == NULL)
8441 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8443 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8444 relpath)== -1)
8445 return got_error_from_errno("asprintf");
8447 switch (status) {
8448 case GOT_STATUS_ADD:
8449 case GOT_STATUS_MODIFY:
8450 /* XXX could sb.st_mode be passed in by our caller? */
8451 if (lstat(ondisk_path, &sb) == -1) {
8452 err = got_error_from_errno2("lstat", ondisk_path);
8453 break;
8455 if (a->patch_cb) {
8456 if (status == GOT_STATUS_ADD) {
8457 int choice = GOT_PATCH_CHOICE_NONE;
8458 err = (*a->patch_cb)(&choice, a->patch_arg,
8459 status, ie->path, NULL, 1, 1);
8460 if (err)
8461 break;
8462 if (choice != GOT_PATCH_CHOICE_YES)
8463 break;
8464 } else {
8465 err = create_patched_content(&path_content, 0,
8466 staged_blob_id ? staged_blob_id : blob_id,
8467 ondisk_path, dirfd, de_name, ie->path,
8468 a->repo, a->patch_cb, a->patch_arg);
8469 if (err || path_content == NULL)
8470 break;
8473 err = got_object_blob_create(&new_staged_blob_id,
8474 path_content ? path_content : ondisk_path, a->repo);
8475 if (err)
8476 break;
8477 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8478 SHA1_DIGEST_LENGTH);
8479 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8480 stage = GOT_FILEIDX_STAGE_ADD;
8481 else
8482 stage = GOT_FILEIDX_STAGE_MODIFY;
8483 got_fileindex_entry_stage_set(ie, stage);
8484 if (S_ISLNK(sb.st_mode)) {
8485 int is_bad_symlink = 0;
8486 if (!a->allow_bad_symlinks) {
8487 char target_path[PATH_MAX];
8488 ssize_t target_len;
8489 target_len = readlink(ondisk_path, target_path,
8490 sizeof(target_path));
8491 if (target_len == -1) {
8492 err = got_error_from_errno2("readlink",
8493 ondisk_path);
8494 break;
8496 err = is_bad_symlink_target(&is_bad_symlink,
8497 target_path, target_len, ondisk_path,
8498 a->worktree->root_path);
8499 if (err)
8500 break;
8501 if (is_bad_symlink) {
8502 err = got_error_path(ondisk_path,
8503 GOT_ERR_BAD_SYMLINK);
8504 break;
8507 if (is_bad_symlink)
8508 got_fileindex_entry_staged_filetype_set(ie,
8509 GOT_FILEIDX_MODE_BAD_SYMLINK);
8510 else
8511 got_fileindex_entry_staged_filetype_set(ie,
8512 GOT_FILEIDX_MODE_SYMLINK);
8513 } else {
8514 got_fileindex_entry_staged_filetype_set(ie,
8515 GOT_FILEIDX_MODE_REGULAR_FILE);
8517 a->staged_something = 1;
8518 if (a->status_cb == NULL)
8519 break;
8520 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8521 get_staged_status(ie), relpath, blob_id,
8522 new_staged_blob_id, NULL, dirfd, de_name);
8523 if (err)
8524 break;
8526 * When staging the reverse of the staged diff,
8527 * implicitly unstage the file.
8529 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8530 sizeof(ie->blob_sha1)) == 0) {
8531 got_fileindex_entry_stage_set(ie,
8532 GOT_FILEIDX_STAGE_NONE);
8534 break;
8535 case GOT_STATUS_DELETE:
8536 if (staged_status == GOT_STATUS_DELETE)
8537 break;
8538 if (a->patch_cb) {
8539 int choice = GOT_PATCH_CHOICE_NONE;
8540 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8541 ie->path, NULL, 1, 1);
8542 if (err)
8543 break;
8544 if (choice == GOT_PATCH_CHOICE_NO)
8545 break;
8546 if (choice != GOT_PATCH_CHOICE_YES) {
8547 err = got_error(GOT_ERR_PATCH_CHOICE);
8548 break;
8551 stage = GOT_FILEIDX_STAGE_DELETE;
8552 got_fileindex_entry_stage_set(ie, stage);
8553 a->staged_something = 1;
8554 if (a->status_cb == NULL)
8555 break;
8556 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8557 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8558 de_name);
8559 break;
8560 case GOT_STATUS_NO_CHANGE:
8561 break;
8562 case GOT_STATUS_CONFLICT:
8563 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8564 break;
8565 case GOT_STATUS_NONEXISTENT:
8566 err = got_error_set_errno(ENOENT, relpath);
8567 break;
8568 default:
8569 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8570 break;
8573 if (path_content && unlink(path_content) == -1 && err == NULL)
8574 err = got_error_from_errno2("unlink", path_content);
8575 free(path_content);
8576 free(ondisk_path);
8577 free(new_staged_blob_id);
8578 return err;
8581 const struct got_error *
8582 got_worktree_stage(struct got_worktree *worktree,
8583 struct got_pathlist_head *paths,
8584 got_worktree_status_cb status_cb, void *status_arg,
8585 got_worktree_patch_cb patch_cb, void *patch_arg,
8586 int allow_bad_symlinks, struct got_repository *repo)
8588 const struct got_error *err = NULL, *sync_err, *unlockerr;
8589 struct got_pathlist_entry *pe;
8590 struct got_fileindex *fileindex = NULL;
8591 char *fileindex_path = NULL;
8592 struct got_reference *head_ref = NULL;
8593 struct got_object_id *head_commit_id = NULL;
8594 struct check_stage_ok_arg oka;
8595 struct stage_path_arg spa;
8597 err = lock_worktree(worktree, LOCK_EX);
8598 if (err)
8599 return err;
8601 err = got_ref_open(&head_ref, repo,
8602 got_worktree_get_head_ref_name(worktree), 0);
8603 if (err)
8604 goto done;
8605 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8606 if (err)
8607 goto done;
8608 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8609 if (err)
8610 goto done;
8612 /* Check pre-conditions before staging anything. */
8613 oka.head_commit_id = head_commit_id;
8614 oka.worktree = worktree;
8615 oka.fileindex = fileindex;
8616 oka.repo = repo;
8617 oka.have_changes = 0;
8618 TAILQ_FOREACH(pe, paths, entry) {
8619 err = worktree_status(worktree, pe->path, fileindex, repo,
8620 check_stage_ok, &oka, NULL, NULL, 1, 0);
8621 if (err)
8622 goto done;
8624 if (!oka.have_changes) {
8625 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8626 goto done;
8629 spa.worktree = worktree;
8630 spa.fileindex = fileindex;
8631 spa.repo = repo;
8632 spa.patch_cb = patch_cb;
8633 spa.patch_arg = patch_arg;
8634 spa.status_cb = status_cb;
8635 spa.status_arg = status_arg;
8636 spa.staged_something = 0;
8637 spa.allow_bad_symlinks = allow_bad_symlinks;
8638 TAILQ_FOREACH(pe, paths, entry) {
8639 err = worktree_status(worktree, pe->path, fileindex, repo,
8640 stage_path, &spa, NULL, NULL, 1, 0);
8641 if (err)
8642 goto done;
8644 if (!spa.staged_something) {
8645 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8646 goto done;
8649 sync_err = sync_fileindex(fileindex, fileindex_path);
8650 if (sync_err && err == NULL)
8651 err = sync_err;
8652 done:
8653 if (head_ref)
8654 got_ref_close(head_ref);
8655 free(head_commit_id);
8656 free(fileindex_path);
8657 if (fileindex)
8658 got_fileindex_free(fileindex);
8659 unlockerr = lock_worktree(worktree, LOCK_SH);
8660 if (unlockerr && err == NULL)
8661 err = unlockerr;
8662 return err;
8665 struct unstage_path_arg {
8666 struct got_worktree *worktree;
8667 struct got_fileindex *fileindex;
8668 struct got_repository *repo;
8669 got_worktree_checkout_cb progress_cb;
8670 void *progress_arg;
8671 got_worktree_patch_cb patch_cb;
8672 void *patch_arg;
8675 static const struct got_error *
8676 create_unstaged_content(char **path_unstaged_content,
8677 char **path_new_staged_content, struct got_object_id *blob_id,
8678 struct got_object_id *staged_blob_id, const char *relpath,
8679 struct got_repository *repo,
8680 got_worktree_patch_cb patch_cb, void *patch_arg)
8682 const struct got_error *err, *free_err;
8683 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8684 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8685 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8686 struct got_diffreg_result *diffreg_result = NULL;
8687 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8688 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8689 int fd1 = -1, fd2 = -1;
8691 *path_unstaged_content = NULL;
8692 *path_new_staged_content = NULL;
8694 err = got_object_id_str(&label1, blob_id);
8695 if (err)
8696 return err;
8698 fd1 = got_opentempfd();
8699 if (fd1 == -1) {
8700 err = got_error_from_errno("got_opentempfd");
8701 goto done;
8703 fd2 = got_opentempfd();
8704 if (fd2 == -1) {
8705 err = got_error_from_errno("got_opentempfd");
8706 goto done;
8709 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8710 if (err)
8711 goto done;
8713 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8714 if (err)
8715 goto done;
8717 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8718 if (err)
8719 goto done;
8721 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8722 fd2);
8723 if (err)
8724 goto done;
8726 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8727 if (err)
8728 goto done;
8730 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8731 if (err)
8732 goto done;
8734 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8735 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8736 if (err)
8737 goto done;
8739 err = got_opentemp_named(path_unstaged_content, &outfile,
8740 "got-unstaged-content", "");
8741 if (err)
8742 goto done;
8743 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8744 "got-new-staged-content", "");
8745 if (err)
8746 goto done;
8748 if (fseek(f1, 0L, SEEK_SET) == -1) {
8749 err = got_ferror(f1, GOT_ERR_IO);
8750 goto done;
8752 if (fseek(f2, 0L, SEEK_SET) == -1) {
8753 err = got_ferror(f2, GOT_ERR_IO);
8754 goto done;
8756 /* Count the number of actual changes in the diff result. */
8757 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8758 struct diff_chunk_context cc = {};
8759 diff_chunk_context_load_change(&cc, &nchunks_used,
8760 diffreg_result->result, n, 0);
8761 nchanges++;
8763 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8764 int choice;
8765 err = apply_or_reject_change(&choice, &nchunks_used,
8766 diffreg_result->result, n, relpath, f1, f2,
8767 &line_cur1, &line_cur2,
8768 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8769 if (err)
8770 goto done;
8771 if (choice == GOT_PATCH_CHOICE_YES)
8772 have_content = 1;
8773 else
8774 have_rejected_content = 1;
8775 if (choice == GOT_PATCH_CHOICE_QUIT)
8776 break;
8778 if (have_content || have_rejected_content)
8779 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8780 outfile, rejectfile);
8781 done:
8782 free(label1);
8783 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8784 err = got_error_from_errno("close");
8785 if (blob)
8786 got_object_blob_close(blob);
8787 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8788 err = got_error_from_errno("close");
8789 if (staged_blob)
8790 got_object_blob_close(staged_blob);
8791 free_err = got_diffreg_result_free(diffreg_result);
8792 if (free_err && err == NULL)
8793 err = free_err;
8794 if (f1 && fclose(f1) == EOF && err == NULL)
8795 err = got_error_from_errno2("fclose", path1);
8796 if (f2 && fclose(f2) == EOF && err == NULL)
8797 err = got_error_from_errno2("fclose", path2);
8798 if (outfile && fclose(outfile) == EOF && err == NULL)
8799 err = got_error_from_errno2("fclose", *path_unstaged_content);
8800 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8801 err = got_error_from_errno2("fclose", *path_new_staged_content);
8802 if (path1 && unlink(path1) == -1 && err == NULL)
8803 err = got_error_from_errno2("unlink", path1);
8804 if (path2 && unlink(path2) == -1 && err == NULL)
8805 err = got_error_from_errno2("unlink", path2);
8806 if (err || !have_content) {
8807 if (*path_unstaged_content &&
8808 unlink(*path_unstaged_content) == -1 && err == NULL)
8809 err = got_error_from_errno2("unlink",
8810 *path_unstaged_content);
8811 free(*path_unstaged_content);
8812 *path_unstaged_content = NULL;
8814 if (err || !have_content || !have_rejected_content) {
8815 if (*path_new_staged_content &&
8816 unlink(*path_new_staged_content) == -1 && err == NULL)
8817 err = got_error_from_errno2("unlink",
8818 *path_new_staged_content);
8819 free(*path_new_staged_content);
8820 *path_new_staged_content = NULL;
8822 free(path1);
8823 free(path2);
8824 return err;
8827 static const struct got_error *
8828 unstage_hunks(struct got_object_id *staged_blob_id,
8829 struct got_blob_object *blob_base,
8830 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8831 const char *ondisk_path, const char *label_orig,
8832 struct got_worktree *worktree, struct got_repository *repo,
8833 got_worktree_patch_cb patch_cb, void *patch_arg,
8834 got_worktree_checkout_cb progress_cb, void *progress_arg)
8836 const struct got_error *err = NULL;
8837 char *path_unstaged_content = NULL;
8838 char *path_new_staged_content = NULL;
8839 char *parent = NULL, *base_path = NULL;
8840 char *blob_base_path = NULL;
8841 struct got_object_id *new_staged_blob_id = NULL;
8842 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8843 struct stat sb;
8845 err = create_unstaged_content(&path_unstaged_content,
8846 &path_new_staged_content, blob_id, staged_blob_id,
8847 ie->path, repo, patch_cb, patch_arg);
8848 if (err)
8849 return err;
8851 if (path_unstaged_content == NULL)
8852 return NULL;
8854 if (path_new_staged_content) {
8855 err = got_object_blob_create(&new_staged_blob_id,
8856 path_new_staged_content, repo);
8857 if (err)
8858 goto done;
8861 f = fopen(path_unstaged_content, "re");
8862 if (f == NULL) {
8863 err = got_error_from_errno2("fopen",
8864 path_unstaged_content);
8865 goto done;
8867 if (fstat(fileno(f), &sb) == -1) {
8868 err = got_error_from_errno2("fstat", path_unstaged_content);
8869 goto done;
8871 if (got_fileindex_entry_staged_filetype_get(ie) ==
8872 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8873 char link_target[PATH_MAX];
8874 size_t r;
8875 r = fread(link_target, 1, sizeof(link_target), f);
8876 if (r == 0 && ferror(f)) {
8877 err = got_error_from_errno("fread");
8878 goto done;
8880 if (r >= sizeof(link_target)) { /* should not happen */
8881 err = got_error(GOT_ERR_NO_SPACE);
8882 goto done;
8884 link_target[r] = '\0';
8885 err = merge_symlink(worktree, blob_base,
8886 ondisk_path, ie->path, label_orig, link_target,
8887 worktree->base_commit_id, repo, progress_cb,
8888 progress_arg);
8889 } else {
8890 int local_changes_subsumed;
8892 err = got_path_dirname(&parent, ondisk_path);
8893 if (err)
8894 return err;
8896 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8897 parent) == -1) {
8898 err = got_error_from_errno("asprintf");
8899 base_path = NULL;
8900 goto done;
8903 err = got_opentemp_named(&blob_base_path, &f_base,
8904 base_path, "");
8905 if (err)
8906 goto done;
8907 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8908 blob_base);
8909 if (err)
8910 goto done;
8913 * In order the run a 3-way merge with a symlink we copy the symlink's
8914 * target path into a temporary file and use that file with diff3.
8916 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8917 err = dump_symlink_target_path_to_file(&f_deriv2,
8918 ondisk_path);
8919 if (err)
8920 goto done;
8921 } else {
8922 int fd;
8923 fd = open(ondisk_path,
8924 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8925 if (fd == -1) {
8926 err = got_error_from_errno2("open", ondisk_path);
8927 goto done;
8929 f_deriv2 = fdopen(fd, "r");
8930 if (f_deriv2 == NULL) {
8931 err = got_error_from_errno2("fdopen", ondisk_path);
8932 close(fd);
8933 goto done;
8937 err = merge_file(&local_changes_subsumed, worktree,
8938 f_base, f, f_deriv2, ondisk_path, ie->path,
8939 got_fileindex_perms_to_st(ie),
8940 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8941 repo, progress_cb, progress_arg);
8943 if (err)
8944 goto done;
8946 if (new_staged_blob_id) {
8947 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8948 SHA1_DIGEST_LENGTH);
8949 } else {
8950 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8951 got_fileindex_entry_staged_filetype_set(ie, 0);
8953 done:
8954 free(new_staged_blob_id);
8955 if (path_unstaged_content &&
8956 unlink(path_unstaged_content) == -1 && err == NULL)
8957 err = got_error_from_errno2("unlink", path_unstaged_content);
8958 if (path_new_staged_content &&
8959 unlink(path_new_staged_content) == -1 && err == NULL)
8960 err = got_error_from_errno2("unlink", path_new_staged_content);
8961 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8962 err = got_error_from_errno2("unlink", blob_base_path);
8963 if (f_base && fclose(f_base) == EOF && err == NULL)
8964 err = got_error_from_errno2("fclose", path_unstaged_content);
8965 if (f && fclose(f) == EOF && err == NULL)
8966 err = got_error_from_errno2("fclose", path_unstaged_content);
8967 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8968 err = got_error_from_errno2("fclose", ondisk_path);
8969 free(path_unstaged_content);
8970 free(path_new_staged_content);
8971 free(blob_base_path);
8972 free(parent);
8973 free(base_path);
8974 return err;
8977 static const struct got_error *
8978 unstage_path(void *arg, unsigned char status,
8979 unsigned char staged_status, const char *relpath,
8980 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8981 struct got_object_id *commit_id, int dirfd, const char *de_name)
8983 const struct got_error *err = NULL;
8984 struct unstage_path_arg *a = arg;
8985 struct got_fileindex_entry *ie;
8986 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8987 char *ondisk_path = NULL;
8988 char *id_str = NULL, *label_orig = NULL;
8989 int local_changes_subsumed;
8990 struct stat sb;
8991 int fd1 = -1, fd2 = -1;
8993 if (staged_status != GOT_STATUS_ADD &&
8994 staged_status != GOT_STATUS_MODIFY &&
8995 staged_status != GOT_STATUS_DELETE)
8996 return NULL;
8998 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8999 if (ie == NULL)
9000 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9002 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9003 == -1)
9004 return got_error_from_errno("asprintf");
9006 err = got_object_id_str(&id_str,
9007 commit_id ? commit_id : a->worktree->base_commit_id);
9008 if (err)
9009 goto done;
9010 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9011 id_str) == -1) {
9012 err = got_error_from_errno("asprintf");
9013 goto done;
9016 fd1 = got_opentempfd();
9017 if (fd1 == -1) {
9018 err = got_error_from_errno("got_opentempfd");
9019 goto done;
9021 fd2 = got_opentempfd();
9022 if (fd2 == -1) {
9023 err = got_error_from_errno("got_opentempfd");
9024 goto done;
9027 switch (staged_status) {
9028 case GOT_STATUS_MODIFY:
9029 err = got_object_open_as_blob(&blob_base, a->repo,
9030 blob_id, 8192, fd1);
9031 if (err)
9032 break;
9033 /* fall through */
9034 case GOT_STATUS_ADD:
9035 if (a->patch_cb) {
9036 if (staged_status == GOT_STATUS_ADD) {
9037 int choice = GOT_PATCH_CHOICE_NONE;
9038 err = (*a->patch_cb)(&choice, a->patch_arg,
9039 staged_status, ie->path, NULL, 1, 1);
9040 if (err)
9041 break;
9042 if (choice != GOT_PATCH_CHOICE_YES)
9043 break;
9044 } else {
9045 err = unstage_hunks(staged_blob_id,
9046 blob_base, blob_id, ie, ondisk_path,
9047 label_orig, a->worktree, a->repo,
9048 a->patch_cb, a->patch_arg,
9049 a->progress_cb, a->progress_arg);
9050 break; /* Done with this file. */
9053 err = got_object_open_as_blob(&blob_staged, a->repo,
9054 staged_blob_id, 8192, fd2);
9055 if (err)
9056 break;
9057 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9058 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9059 case GOT_FILEIDX_MODE_REGULAR_FILE:
9060 err = merge_blob(&local_changes_subsumed, a->worktree,
9061 blob_base, ondisk_path, relpath,
9062 got_fileindex_perms_to_st(ie), label_orig,
9063 blob_staged, commit_id ? commit_id :
9064 a->worktree->base_commit_id, a->repo,
9065 a->progress_cb, a->progress_arg);
9066 break;
9067 case GOT_FILEIDX_MODE_SYMLINK:
9068 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9069 char *staged_target;
9070 err = got_object_blob_read_to_str(
9071 &staged_target, blob_staged);
9072 if (err)
9073 goto done;
9074 err = merge_symlink(a->worktree, blob_base,
9075 ondisk_path, relpath, label_orig,
9076 staged_target, commit_id ? commit_id :
9077 a->worktree->base_commit_id,
9078 a->repo, a->progress_cb, a->progress_arg);
9079 free(staged_target);
9080 } else {
9081 err = merge_blob(&local_changes_subsumed,
9082 a->worktree, blob_base, ondisk_path,
9083 relpath, got_fileindex_perms_to_st(ie),
9084 label_orig, blob_staged,
9085 commit_id ? commit_id :
9086 a->worktree->base_commit_id, a->repo,
9087 a->progress_cb, a->progress_arg);
9089 break;
9090 default:
9091 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9092 break;
9094 if (err == NULL) {
9095 got_fileindex_entry_stage_set(ie,
9096 GOT_FILEIDX_STAGE_NONE);
9097 got_fileindex_entry_staged_filetype_set(ie, 0);
9099 break;
9100 case GOT_STATUS_DELETE:
9101 if (a->patch_cb) {
9102 int choice = GOT_PATCH_CHOICE_NONE;
9103 err = (*a->patch_cb)(&choice, a->patch_arg,
9104 staged_status, ie->path, NULL, 1, 1);
9105 if (err)
9106 break;
9107 if (choice == GOT_PATCH_CHOICE_NO)
9108 break;
9109 if (choice != GOT_PATCH_CHOICE_YES) {
9110 err = got_error(GOT_ERR_PATCH_CHOICE);
9111 break;
9114 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9115 got_fileindex_entry_staged_filetype_set(ie, 0);
9116 err = get_file_status(&status, &sb, ie, ondisk_path,
9117 dirfd, de_name, a->repo);
9118 if (err)
9119 break;
9120 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9121 break;
9123 done:
9124 free(ondisk_path);
9125 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9126 err = got_error_from_errno("close");
9127 if (blob_base)
9128 got_object_blob_close(blob_base);
9129 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9130 err = got_error_from_errno("close");
9131 if (blob_staged)
9132 got_object_blob_close(blob_staged);
9133 free(id_str);
9134 free(label_orig);
9135 return err;
9138 const struct got_error *
9139 got_worktree_unstage(struct got_worktree *worktree,
9140 struct got_pathlist_head *paths,
9141 got_worktree_checkout_cb progress_cb, void *progress_arg,
9142 got_worktree_patch_cb patch_cb, void *patch_arg,
9143 struct got_repository *repo)
9145 const struct got_error *err = NULL, *sync_err, *unlockerr;
9146 struct got_pathlist_entry *pe;
9147 struct got_fileindex *fileindex = NULL;
9148 char *fileindex_path = NULL;
9149 struct unstage_path_arg upa;
9151 err = lock_worktree(worktree, LOCK_EX);
9152 if (err)
9153 return err;
9155 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9156 if (err)
9157 goto done;
9159 upa.worktree = worktree;
9160 upa.fileindex = fileindex;
9161 upa.repo = repo;
9162 upa.progress_cb = progress_cb;
9163 upa.progress_arg = progress_arg;
9164 upa.patch_cb = patch_cb;
9165 upa.patch_arg = patch_arg;
9166 TAILQ_FOREACH(pe, paths, entry) {
9167 err = worktree_status(worktree, pe->path, fileindex, repo,
9168 unstage_path, &upa, NULL, NULL, 1, 0);
9169 if (err)
9170 goto done;
9173 sync_err = sync_fileindex(fileindex, fileindex_path);
9174 if (sync_err && err == NULL)
9175 err = sync_err;
9176 done:
9177 free(fileindex_path);
9178 if (fileindex)
9179 got_fileindex_free(fileindex);
9180 unlockerr = lock_worktree(worktree, LOCK_SH);
9181 if (unlockerr && err == NULL)
9182 err = unlockerr;
9183 return err;
9186 struct report_file_info_arg {
9187 struct got_worktree *worktree;
9188 got_worktree_path_info_cb info_cb;
9189 void *info_arg;
9190 struct got_pathlist_head *paths;
9191 got_cancel_cb cancel_cb;
9192 void *cancel_arg;
9195 static const struct got_error *
9196 report_file_info(void *arg, struct got_fileindex_entry *ie)
9198 struct report_file_info_arg *a = arg;
9199 struct got_pathlist_entry *pe;
9200 struct got_object_id blob_id, staged_blob_id, commit_id;
9201 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9202 struct got_object_id *commit_idp = NULL;
9203 int stage;
9205 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9206 return got_error(GOT_ERR_CANCELLED);
9208 TAILQ_FOREACH(pe, a->paths, entry) {
9209 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9210 got_path_is_child(ie->path, pe->path, pe->path_len))
9211 break;
9213 if (pe == NULL) /* not found */
9214 return NULL;
9216 if (got_fileindex_entry_has_blob(ie))
9217 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9218 stage = got_fileindex_entry_stage_get(ie);
9219 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9220 stage == GOT_FILEIDX_STAGE_ADD) {
9221 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9222 &staged_blob_id, ie);
9225 if (got_fileindex_entry_has_commit(ie))
9226 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9228 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9229 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9232 const struct got_error *
9233 got_worktree_path_info(struct got_worktree *worktree,
9234 struct got_pathlist_head *paths,
9235 got_worktree_path_info_cb info_cb, void *info_arg,
9236 got_cancel_cb cancel_cb, void *cancel_arg)
9239 const struct got_error *err = NULL, *unlockerr;
9240 struct got_fileindex *fileindex = NULL;
9241 char *fileindex_path = NULL;
9242 struct report_file_info_arg arg;
9244 err = lock_worktree(worktree, LOCK_SH);
9245 if (err)
9246 return err;
9248 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9249 if (err)
9250 goto done;
9252 arg.worktree = worktree;
9253 arg.info_cb = info_cb;
9254 arg.info_arg = info_arg;
9255 arg.paths = paths;
9256 arg.cancel_cb = cancel_cb;
9257 arg.cancel_arg = cancel_arg;
9258 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9259 &arg);
9260 done:
9261 free(fileindex_path);
9262 if (fileindex)
9263 got_fileindex_free(fileindex);
9264 unlockerr = lock_worktree(worktree, LOCK_UN);
9265 if (unlockerr && err == NULL)
9266 err = unlockerr;
9267 return err;
9270 static const struct got_error *
9271 patch_check_path(const char *p, char **path, unsigned char *status,
9272 unsigned char *staged_status, struct got_fileindex *fileindex,
9273 struct got_worktree *worktree, struct got_repository *repo)
9275 const struct got_error *err;
9276 struct got_fileindex_entry *ie;
9277 struct stat sb;
9278 char *ondisk_path = NULL;
9280 err = got_worktree_resolve_path(path, worktree, p);
9281 if (err)
9282 return err;
9284 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9285 *path[0] ? "/" : "", *path) == -1)
9286 return got_error_from_errno("asprintf");
9288 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9289 if (ie) {
9290 *staged_status = get_staged_status(ie);
9291 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9292 repo);
9293 if (err)
9294 goto done;
9295 } else {
9296 *staged_status = GOT_STATUS_NO_CHANGE;
9297 *status = GOT_STATUS_UNVERSIONED;
9298 if (lstat(ondisk_path, &sb) == -1) {
9299 if (errno != ENOENT) {
9300 err = got_error_from_errno2("lstat",
9301 ondisk_path);
9302 goto done;
9304 *status = GOT_STATUS_NONEXISTENT;
9308 done:
9309 free(ondisk_path);
9310 return err;
9313 static const struct got_error *
9314 patch_can_rm(const char *path, unsigned char status,
9315 unsigned char staged_status)
9317 if (status == GOT_STATUS_NONEXISTENT)
9318 return got_error_set_errno(ENOENT, path);
9319 if (status != GOT_STATUS_NO_CHANGE &&
9320 status != GOT_STATUS_ADD &&
9321 status != GOT_STATUS_MODIFY &&
9322 status != GOT_STATUS_MODE_CHANGE)
9323 return got_error_path(path, GOT_ERR_FILE_STATUS);
9324 if (staged_status == GOT_STATUS_DELETE)
9325 return got_error_path(path, GOT_ERR_FILE_STATUS);
9326 return NULL;
9329 static const struct got_error *
9330 patch_can_add(const char *path, unsigned char status)
9332 if (status != GOT_STATUS_NONEXISTENT)
9333 return got_error_path(path, GOT_ERR_FILE_STATUS);
9334 return NULL;
9337 static const struct got_error *
9338 patch_can_edit(const char *path, unsigned char status,
9339 unsigned char staged_status)
9341 if (status == GOT_STATUS_NONEXISTENT)
9342 return got_error_set_errno(ENOENT, path);
9343 if (status != GOT_STATUS_NO_CHANGE &&
9344 status != GOT_STATUS_ADD &&
9345 status != GOT_STATUS_MODIFY)
9346 return got_error_path(path, GOT_ERR_FILE_STATUS);
9347 if (staged_status == GOT_STATUS_DELETE)
9348 return got_error_path(path, GOT_ERR_FILE_STATUS);
9349 return NULL;
9352 const struct got_error *
9353 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9354 char **fileindex_path, struct got_worktree *worktree)
9356 return open_fileindex(fileindex, fileindex_path, worktree);
9359 const struct got_error *
9360 got_worktree_patch_check_path(const char *old, const char *new,
9361 char **oldpath, char **newpath, struct got_worktree *worktree,
9362 struct got_repository *repo, struct got_fileindex *fileindex)
9364 const struct got_error *err = NULL;
9365 int file_renamed = 0;
9366 unsigned char status_old, staged_status_old;
9367 unsigned char status_new, staged_status_new;
9369 *oldpath = NULL;
9370 *newpath = NULL;
9372 err = patch_check_path(old != NULL ? old : new, oldpath,
9373 &status_old, &staged_status_old, fileindex, worktree, repo);
9374 if (err)
9375 goto done;
9377 err = patch_check_path(new != NULL ? new : old, newpath,
9378 &status_new, &staged_status_new, fileindex, worktree, repo);
9379 if (err)
9380 goto done;
9382 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9383 file_renamed = 1;
9385 if (old != NULL && new == NULL)
9386 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9387 else if (file_renamed) {
9388 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9389 if (err == NULL)
9390 err = patch_can_add(*newpath, status_new);
9391 } else if (old == NULL)
9392 err = patch_can_add(*newpath, status_new);
9393 else
9394 err = patch_can_edit(*newpath, status_new, staged_status_new);
9396 done:
9397 if (err) {
9398 free(*oldpath);
9399 *oldpath = NULL;
9400 free(*newpath);
9401 *newpath = NULL;
9403 return err;
9406 const struct got_error *
9407 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9408 struct got_worktree *worktree, struct got_fileindex *fileindex,
9409 got_worktree_checkout_cb progress_cb, void *progress_arg)
9411 struct schedule_addition_args saa;
9413 memset(&saa, 0, sizeof(saa));
9414 saa.worktree = worktree;
9415 saa.fileindex = fileindex;
9416 saa.progress_cb = progress_cb;
9417 saa.progress_arg = progress_arg;
9418 saa.repo = repo;
9420 return worktree_status(worktree, path, fileindex, repo,
9421 schedule_addition, &saa, NULL, NULL, 1, 0);
9424 const struct got_error *
9425 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9426 struct got_worktree *worktree, struct got_fileindex *fileindex,
9427 got_worktree_delete_cb progress_cb, void *progress_arg)
9429 struct schedule_deletion_args sda;
9431 memset(&sda, 0, sizeof(sda));
9432 sda.worktree = worktree;
9433 sda.fileindex = fileindex;
9434 sda.progress_cb = progress_cb;
9435 sda.progress_arg = progress_arg;
9436 sda.repo = repo;
9437 sda.delete_local_mods = 0;
9438 sda.keep_on_disk = 0;
9439 sda.ignore_missing_paths = 0;
9440 sda.status_codes = NULL;
9442 return worktree_status(worktree, path, fileindex, repo,
9443 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9446 const struct got_error *
9447 got_worktree_patch_complete(struct got_fileindex *fileindex,
9448 const char *fileindex_path)
9450 const struct got_error *err = NULL;
9452 err = sync_fileindex(fileindex, fileindex_path);
9453 got_fileindex_free(fileindex);
9455 return err;