Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
71 {
72 const struct got_error *err = NULL;
73 char *path;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
79 free(path);
80 return err;
81 }
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
85 {
86 const struct got_error *err = NULL;
87 FILE *tmpfile = NULL;
88 char *tmppath = NULL;
89 char *path = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
93 path = NULL;
94 goto done;
95 }
97 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
98 if (err)
99 goto done;
101 if (content) {
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
105 goto done;
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
111 unlink(tmppath);
112 goto done;
115 done:
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
118 free(tmppath);
119 return err;
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
126 char *refstr = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
130 if (refstr == NULL)
131 return got_error_from_errno("got_ref_to_str");
132 } else {
133 refstr = strdup(got_ref_get_name(head_ref));
134 if (refstr == NULL)
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 free(refstr);
139 return err;
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
148 uuid_t uuid;
149 uint32_t uuid_status;
150 int obj_type;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
159 goto done;
162 err = got_ref_resolve(&commit_id, repo, head_ref);
163 if (err)
164 return err;
165 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (err)
167 return err;
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
179 goto done;
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
185 goto done;
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
189 goto done;
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
194 if (err)
195 goto done;
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
199 if (err)
200 goto done;
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
204 if (err)
205 goto done;
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
209 if (err)
210 goto done;
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 if (err)
213 goto done;
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
218 if (err)
219 goto done;
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
224 if (err)
225 goto done;
227 /* Generate UUID. */
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
231 goto done;
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
236 goto done;
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
239 if (err)
240 goto done;
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 if (err)
249 goto done;
251 done:
252 free(commit_id);
253 free(path_got);
254 free(formatstr);
255 free(absprefix);
256 free(basestr);
257 free(uuidstr);
258 return err;
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
273 free(absprefix);
274 return NULL;
277 const char *
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
293 path_got = NULL;
294 goto done;
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
300 goto done;
303 err = write_head_ref(path_got, head_ref);
304 if (err)
305 goto done;
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
309 done:
310 free(path_got);
311 if (err)
312 free(head_ref_name);
313 return err;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
328 char *id_str = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
334 path_got = NULL;
335 goto done;
338 err = got_object_open(&obj, repo, commit_id);
339 if (err)
340 return err;
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
344 goto done;
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
349 if (err)
350 goto done;
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
352 if (err)
353 goto done;
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
359 goto done;
361 done:
362 if (obj)
363 got_object_close(obj);
364 free(id_str);
365 free(path_got);
366 return err;
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
382 return NULL;
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
389 char *abspath;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 struct stat sb;
397 err = NULL;
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
405 free(abspath);
406 return err;
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
413 uint8_t fbuf1[8192];
414 uint8_t fbuf2[8192];
415 size_t flen1 = 0, flen2 = 0;
417 *same = 1;
419 for (;;) {
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
423 break;
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
428 break;
430 if (flen1 == 0) {
431 if (flen2 != 0)
432 *same = 0;
433 break;
434 } else if (flen2 == 0) {
435 if (flen1 != 0)
436 *same = 0;
437 break;
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
440 *same = 0;
441 break;
443 } else {
444 *same = 0;
445 break;
449 return err;
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
455 struct stat sb;
456 size_t size1, size2;
458 *same = 1;
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
462 size1 = sb.st_size;
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
466 size2 = sb.st_size;
468 if (size1 != size2) {
469 *same = 0;
470 return NULL;
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
484 uint8_t fbuf[65536];
485 size_t flen;
486 ssize_t outlen;
488 *outsize = 0;
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
493 for (;;) {
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 if (flen == 0) {
496 if (ferror(f))
497 return got_error_from_errno("fread");
498 if (feof(f))
499 break;
501 outlen = write(outfd, fbuf, flen);
502 if (outlen == -1)
503 return got_error_from_errno("write");
504 if (outlen != flen)
505 return got_error(GOT_ERR_IO);
506 *outsize += outlen;
509 return NULL;
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
526 *overlapcnt = 0;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 if (err)
530 return err;
532 if (same_content)
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
536 if (err)
537 return err;
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 if (err)
541 return err;
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
545 *overlapcnt = 1;
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
552 goto done;
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
556 goto done;
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 base_path_orig, "");
560 if (err)
561 goto done;
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv, "");
564 if (err)
565 goto done;
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2, "");
568 if (err)
569 goto done;
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 if (err)
572 goto done;
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 if (err)
575 goto done;
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (err)
578 goto done;
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
582 goto done;
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
588 path_deriv) < 0) {
589 err = got_error_from_errno("dprintf");
590 goto done;
592 if (size_orig > 0) {
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
597 path_orig) < 0) {
598 err = got_error_from_errno("dprintf");
599 goto done;
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
604 path_deriv2,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
609 goto done;
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 done:
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 err == NULL)
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
625 free(path_orig);
626 free(path_deriv);
627 free(path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
631 return err;
634 /*
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
640 */
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
650 int merged_fd = -1;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
653 int overlapcnt = 0;
654 char *parent = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
659 if (err)
660 return err;
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
664 goto done;
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
668 if (err)
669 goto done;
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 if (err) {
674 if (err->code != GOT_ERR_FILE_BINARY)
675 goto done;
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 ondisk_path);
679 if (err)
680 goto done;
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
685 if (err)
686 goto done;
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
690 goto done;
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
696 goto done;
698 merged_fd = -1;
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
703 f_merged);
704 if (err)
705 goto done;
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
710 goto done;
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
715 ondisk_path);
716 goto done;
718 done:
719 if (err) {
720 if (merged_path)
721 unlink(merged_path);
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
727 free(merged_path);
728 free(base_path);
729 free(parent);
730 return err;
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
735 size_t target_len)
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
742 ondisk_path);
743 return NULL;
746 /*
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
757 */
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 FILE *f = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
768 if (err)
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
774 goto done;
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
778 if (err)
779 goto done;
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
783 goto done;
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
796 goto done;
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
801 goto done;
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
805 goto done;
807 done:
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
810 free(path);
811 free(id_str);
812 free(label_deriv);
813 return err;
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
823 /*
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
828 */
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
838 struct stat sb;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
851 ondisk_path);
852 goto done;
854 ondisk_target[ondisk_len] = '\0';
856 if (blob_orig) {
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 if (err)
859 goto done;
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 path);
883 } else {
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
888 if (err)
889 goto done;
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 path);
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
897 if (err)
898 goto done;
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 path);
906 } else {
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 path);
917 done:
918 free(ancestor_target);
919 return err;
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
927 ssize_t target_len;
928 size_t n;
929 FILE *f;
931 *outfile = NULL;
933 f = got_opentemp();
934 if (f == NULL)
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
939 goto done;
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
944 goto done;
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
948 goto done;
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
952 goto done;
954 done:
955 if (err)
956 fclose(f);
957 else
958 *outfile = f;
959 return err;
962 /*
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
966 */
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
984 if (err)
985 return err;
987 if (blob_orig) {
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 parent) == -1) {
990 err = got_error_from_errno("asprintf");
991 base_path = NULL;
992 goto done;
995 err = got_opentemp_named(&blob_orig_path, &f_orig,
996 base_path, "");
997 if (err)
998 goto done;
999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 blob_orig);
1001 if (err)
1002 goto done;
1003 free(base_path);
1004 } else {
1006 * No common ancestor exists. This is an "add vs add" conflict
1007 * and we simply use an empty ancestor file to make both files
1008 * appear in the merged result in their entirety.
1010 f_orig = got_opentemp();
1011 if (f_orig == NULL) {
1012 err = got_error_from_errno("got_opentemp");
1013 goto done;
1017 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 err = got_error_from_errno("asprintf");
1019 base_path = NULL;
1020 goto done;
1023 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 if (err)
1025 goto done;
1026 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1027 blob_deriv);
1028 if (err)
1029 goto done;
1031 err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 if (err)
1033 goto done;
1034 if (asprintf(&label_deriv, "%s: commit %s",
1035 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 err = got_error_from_errno("asprintf");
1037 goto done;
1041 * In order the run a 3-way merge with a symlink we copy the symlink's
1042 * target path into a temporary file and use that file with diff3.
1044 if (S_ISLNK(st_mode)) {
1045 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1046 if (err)
1047 goto done;
1048 } else {
1049 int fd;
1050 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 if (fd == -1) {
1052 err = got_error_from_errno2("open", ondisk_path);
1053 goto done;
1055 f_deriv2 = fdopen(fd, "r");
1056 if (f_deriv2 == NULL) {
1057 err = got_error_from_errno2("fdopen", ondisk_path);
1058 close(fd);
1059 goto done;
1063 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 done:
1067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 err = got_error_from_errno("fclose");
1073 free(base_path);
1074 if (blob_orig_path) {
1075 unlink(blob_orig_path);
1076 free(blob_orig_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1082 free(id_str);
1083 free(label_deriv);
1084 free(parent);
1085 return err;
1088 static const struct got_error *
1089 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 int wt_fd, const char *path, struct got_object_id *blob_id)
1093 const struct got_error *err = NULL;
1094 struct got_fileindex_entry *new_ie;
1096 *new_iep = NULL;
1098 err = got_fileindex_entry_alloc(&new_ie, path);
1099 if (err)
1100 return err;
1102 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 blob_id->sha1, base_commit_id->sha1, 1);
1104 if (err)
1105 goto done;
1107 err = got_fileindex_entry_add(fileindex, new_ie);
1108 done:
1109 if (err)
1110 got_fileindex_entry_free(new_ie);
1111 else
1112 *new_iep = new_ie;
1113 return err;
1116 static mode_t
1117 get_ondisk_perms(int executable, mode_t st_mode)
1119 mode_t xbits = S_IXUSR;
1121 if (executable) {
1122 /* Map read bits to execute bits. */
1123 if (st_mode & S_IRGRP)
1124 xbits |= S_IXGRP;
1125 if (st_mode & S_IROTH)
1126 xbits |= S_IXOTH;
1127 return st_mode | xbits;
1130 return st_mode;
1133 static mode_t
1134 apply_umask(mode_t mode)
1136 mode_t um;
1138 um = umask(000);
1139 umask(um);
1140 return mode & ~um;
1143 /* forward declaration */
1144 static const struct got_error *
1145 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 const char *path, mode_t te_mode, mode_t st_mode,
1147 struct got_blob_object *blob, int restoring_missing_file,
1148 int reverting_versioned_file, int installing_bad_symlink,
1149 int path_is_unversioned, struct got_repository *repo,
1150 got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 * This function assumes that the provided symlink target points at a
1154 * safe location in the work tree!
1156 static const struct got_error *
1157 replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 const char *target_path, size_t target_len)
1160 const struct got_error *err = NULL;
1161 ssize_t elen;
1162 char etarget[PATH_MAX];
1163 int fd;
1165 *did_something = 0;
1168 * "Bad" symlinks (those pointing outside the work tree or into the
1169 * .got directory) are installed in the work tree as a regular file
1170 * which contains the bad symlink target path.
1171 * The new symlink target has already been checked for safety by our
1172 * caller. If we can successfully open a regular file then we simply
1173 * replace this file with a symlink below.
1175 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 if (fd == -1) {
1177 if (!got_err_open_nofollow_on_symlink())
1178 return got_error_from_errno2("open", ondisk_path);
1180 /* We are updating an existing on-disk symlink. */
1181 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 if (elen == -1)
1183 return got_error_from_errno2("readlink", ondisk_path);
1185 if (elen == target_len &&
1186 memcmp(etarget, target_path, target_len) == 0)
1187 return NULL; /* nothing to do */
1190 *did_something = 1;
1191 err = update_symlink(ondisk_path, target_path, target_len);
1192 if (fd != -1 && close(fd) == -1 && err == NULL)
1193 err = got_error_from_errno2("close", ondisk_path);
1194 return err;
1197 static const struct got_error *
1198 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 const struct got_error *err = NULL;
1202 char canonpath[PATH_MAX];
1203 char *path_got = NULL;
1205 *is_bad_symlink = 0;
1207 if (target_len >= sizeof(canonpath)) {
1208 *is_bad_symlink = 1;
1209 return NULL;
1213 * We do not use realpath(3) to resolve the symlink's target
1214 * path because we don't want to resolve symlinks recursively.
1215 * Instead we make the path absolute and then canonicalize it.
1216 * Relative symlink target lookup should begin at the directory
1217 * in which the blob object is being installed.
1219 if (!got_path_is_absolute(target_path)) {
1220 char *abspath, *parent;
1221 err = got_path_dirname(&parent, ondisk_path);
1222 if (err)
1223 return err;
1224 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 free(parent);
1226 return got_error_from_errno("asprintf");
1228 free(parent);
1229 if (strlen(abspath) >= sizeof(canonpath)) {
1230 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 free(abspath);
1232 return err;
1234 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 free(abspath);
1236 if (err)
1237 return err;
1238 } else {
1239 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 if (err)
1241 return err;
1244 /* Only allow symlinks pointing at paths within the work tree. */
1245 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 *is_bad_symlink = 1;
1247 return NULL;
1250 /* Do not allow symlinks pointing into the .got directory. */
1251 if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 GOT_WORKTREE_GOT_DIR) == -1)
1253 return got_error_from_errno("asprintf");
1254 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 *is_bad_symlink = 1;
1257 free(path_got);
1258 return NULL;
1261 static const struct got_error *
1262 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 int restoring_missing_file, int reverting_versioned_file,
1265 int path_is_unversioned, int allow_bad_symlinks,
1266 struct got_repository *repo,
1267 got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 const struct got_error *err = NULL;
1270 char target_path[PATH_MAX];
1271 size_t len, target_len = 0;
1272 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 *is_bad_symlink = 0;
1278 * Blob object content specifies the target path of the link.
1279 * If a symbolic link cannot be installed we instead create
1280 * a regular file which contains the link target path stored
1281 * in the blob object.
1283 do {
1284 err = got_object_blob_read_block(&len, blob);
1285 if (err)
1286 return err;
1288 if (len + target_len >= sizeof(target_path)) {
1289 /* Path too long; install as a regular file. */
1290 *is_bad_symlink = 1;
1291 got_object_blob_rewind(blob);
1292 return install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 restoring_missing_file, reverting_versioned_file,
1295 1, path_is_unversioned, repo, progress_cb,
1296 progress_arg);
1298 if (len > 0) {
1299 /* Skip blob object header first time around. */
1300 memcpy(target_path + target_len, buf + hdrlen,
1301 len - hdrlen);
1302 target_len += len - hdrlen;
1303 hdrlen = 0;
1305 } while (len != 0);
1306 target_path[target_len] = '\0';
1308 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 ondisk_path, worktree->root_path);
1310 if (err)
1311 return err;
1313 if (*is_bad_symlink && !allow_bad_symlinks) {
1314 /* install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 path_is_unversioned, repo, progress_cb, progress_arg);
1320 return err;
1323 if (symlink(target_path, ondisk_path) == -1) {
1324 if (errno == EEXIST) {
1325 int symlink_replaced;
1326 if (path_is_unversioned) {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_UNVERSIONED, path);
1329 return err;
1331 err = replace_existing_symlink(&symlink_replaced,
1332 ondisk_path, target_path, target_len);
1333 if (err)
1334 return err;
1335 if (progress_cb) {
1336 if (symlink_replaced) {
1337 err = (*progress_cb)(progress_arg,
1338 reverting_versioned_file ?
1339 GOT_STATUS_REVERT :
1340 GOT_STATUS_UPDATE, path);
1341 } else {
1342 err = (*progress_cb)(progress_arg,
1343 GOT_STATUS_EXISTS, path);
1346 return err; /* Nothing else to do. */
1349 if (errno == ENOENT) {
1350 char *parent;
1351 err = got_path_dirname(&parent, ondisk_path);
1352 if (err)
1353 return err;
1354 err = add_dir_on_disk(worktree, parent);
1355 free(parent);
1356 if (err)
1357 return err;
1359 * Retry, and fall through to error handling
1360 * below if this second attempt fails.
1362 if (symlink(target_path, ondisk_path) != -1) {
1363 err = NULL; /* success */
1364 return err;
1368 /* Handle errors from first or second creation attempt. */
1369 if (errno == ENAMETOOLONG) {
1370 /* bad target path; install as a regular file */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 err = install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file, 1,
1376 path_is_unversioned, repo,
1377 progress_cb, progress_arg);
1378 } else if (errno == ENOTDIR) {
1379 err = got_error_path(ondisk_path,
1380 GOT_ERR_FILE_OBSTRUCTED);
1381 } else {
1382 err = got_error_from_errno3("symlink",
1383 target_path, ondisk_path);
1385 } else if (progress_cb)
1386 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 return err;
1391 static const struct got_error *
1392 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 const char *path, mode_t te_mode, mode_t st_mode,
1394 struct got_blob_object *blob, int restoring_missing_file,
1395 int reverting_versioned_file, int installing_bad_symlink,
1396 int path_is_unversioned, struct got_repository *repo,
1397 got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 const struct got_error *err = NULL;
1400 int fd = -1;
1401 size_t len, hdrlen;
1402 int update = 0;
1403 char *tmppath = NULL;
1404 mode_t mode;
1406 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 O_CLOEXEC, mode);
1409 if (fd == -1) {
1410 if (errno == ENOENT) {
1411 char *parent;
1412 err = got_path_dirname(&parent, path);
1413 if (err)
1414 return err;
1415 err = add_dir_on_disk(worktree, parent);
1416 free(parent);
1417 if (err)
1418 return err;
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 mode);
1422 if (fd == -1)
1423 return got_error_from_errno2("open",
1424 ondisk_path);
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1429 goto done;
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1436 goto done;
1437 } else {
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1439 ondisk_path, "");
1440 if (err)
1441 goto done;
1442 update = 1;
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1446 tmppath);
1447 goto done;
1450 } else
1451 return got_error_from_errno2("open", ondisk_path);
1454 if (progress_cb) {
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 path);
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 path);
1461 else
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 if (err)
1465 goto done;
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1469 do {
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1472 if (err)
1473 break;
1474 if (len > 0) {
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 if (outlen == -1) {
1478 err = got_error_from_errno("write");
1479 goto done;
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1482 goto done;
1484 hdrlen = 0;
1486 } while (len != 0);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1490 goto done;
1493 if (update) {
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1496 goto done;
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1500 ondisk_path);
1501 goto done;
1503 free(tmppath);
1504 tmppath = NULL;
1507 done:
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1512 free(tmppath);
1513 return err;
1516 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1517 static const struct got_error *
1518 get_modified_file_content_status(unsigned char *status, FILE *f)
1520 const struct got_error *err = NULL;
1521 const char *markers[3] = {
1522 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1523 GOT_DIFF_CONFLICT_MARKER_SEP,
1524 GOT_DIFF_CONFLICT_MARKER_END
1526 int i = 0;
1527 char *line = NULL;
1528 size_t linesize = 0;
1529 ssize_t linelen;
1531 while (*status == GOT_STATUS_MODIFY) {
1532 linelen = getline(&line, &linesize, f);
1533 if (linelen == -1) {
1534 if (feof(f))
1535 break;
1536 err = got_ferror(f, GOT_ERR_IO);
1537 break;
1540 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1541 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1542 == 0)
1543 *status = GOT_STATUS_CONFLICT;
1544 else
1545 i++;
1548 free(line);
1550 return err;
1553 static int
1554 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1556 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1557 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1560 static int
1561 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1563 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1564 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1565 ie->mtime_sec == sb->st_mtim.tv_sec &&
1566 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1567 ie->size == (sb->st_size & 0xffffffff) &&
1568 !xbit_differs(ie, sb->st_mode));
1571 static unsigned char
1572 get_staged_status(struct got_fileindex_entry *ie)
1574 switch (got_fileindex_entry_stage_get(ie)) {
1575 case GOT_FILEIDX_STAGE_ADD:
1576 return GOT_STATUS_ADD;
1577 case GOT_FILEIDX_STAGE_DELETE:
1578 return GOT_STATUS_DELETE;
1579 case GOT_FILEIDX_STAGE_MODIFY:
1580 return GOT_STATUS_MODIFY;
1581 default:
1582 return GOT_STATUS_NO_CHANGE;
1586 static const struct got_error *
1587 get_symlink_modification_status(unsigned char *status,
1588 struct got_fileindex_entry *ie, const char *abspath,
1589 int dirfd, const char *de_name, struct got_blob_object *blob)
1591 const struct got_error *err = NULL;
1592 char target_path[PATH_MAX];
1593 char etarget[PATH_MAX];
1594 ssize_t elen;
1595 size_t len, target_len = 0;
1596 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1597 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1599 *status = GOT_STATUS_NO_CHANGE;
1601 /* Blob object content specifies the target path of the link. */
1602 do {
1603 err = got_object_blob_read_block(&len, blob);
1604 if (err)
1605 return err;
1606 if (len + target_len >= sizeof(target_path)) {
1608 * Should not happen. The blob contents were OK
1609 * when this symlink was installed.
1611 return got_error(GOT_ERR_NO_SPACE);
1613 if (len > 0) {
1614 /* Skip blob object header first time around. */
1615 memcpy(target_path + target_len, buf + hdrlen,
1616 len - hdrlen);
1617 target_len += len - hdrlen;
1618 hdrlen = 0;
1620 } while (len != 0);
1621 target_path[target_len] = '\0';
1623 if (dirfd != -1) {
1624 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1625 if (elen == -1)
1626 return got_error_from_errno2("readlinkat", abspath);
1627 } else {
1628 elen = readlink(abspath, etarget, sizeof(etarget));
1629 if (elen == -1)
1630 return got_error_from_errno2("readlink", abspath);
1633 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1634 *status = GOT_STATUS_MODIFY;
1636 return NULL;
1639 static const struct got_error *
1640 get_file_status(unsigned char *status, struct stat *sb,
1641 struct got_fileindex_entry *ie, const char *abspath,
1642 int dirfd, const char *de_name, struct got_repository *repo)
1644 const struct got_error *err = NULL;
1645 struct got_object_id id;
1646 size_t hdrlen;
1647 int fd = -1, fd1 = -1;
1648 FILE *f = NULL;
1649 uint8_t fbuf[8192];
1650 struct got_blob_object *blob = NULL;
1651 size_t flen, blen;
1652 unsigned char staged_status = get_staged_status(ie);
1654 *status = GOT_STATUS_NO_CHANGE;
1655 memset(sb, 0, sizeof(*sb));
1658 * Whenever the caller provides a directory descriptor and a
1659 * directory entry name for the file, use them! This prevents
1660 * race conditions if filesystem paths change beneath our feet.
1662 if (dirfd != -1) {
1663 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1664 if (errno == ENOENT) {
1665 if (got_fileindex_entry_has_file_on_disk(ie))
1666 *status = GOT_STATUS_MISSING;
1667 else
1668 *status = GOT_STATUS_DELETE;
1669 goto done;
1671 err = got_error_from_errno2("fstatat", abspath);
1672 goto done;
1674 } else {
1675 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1676 if (fd == -1 && errno != ENOENT &&
1677 !got_err_open_nofollow_on_symlink())
1678 return got_error_from_errno2("open", abspath);
1679 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1680 if (lstat(abspath, sb) == -1)
1681 return got_error_from_errno2("lstat", abspath);
1682 } else if (fd == -1 || fstat(fd, sb) == -1) {
1683 if (errno == ENOENT) {
1684 if (got_fileindex_entry_has_file_on_disk(ie))
1685 *status = GOT_STATUS_MISSING;
1686 else
1687 *status = GOT_STATUS_DELETE;
1688 goto done;
1690 err = got_error_from_errno2("fstat", abspath);
1691 goto done;
1695 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1696 *status = GOT_STATUS_OBSTRUCTED;
1697 goto done;
1700 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1701 *status = GOT_STATUS_DELETE;
1702 goto done;
1703 } else if (!got_fileindex_entry_has_blob(ie) &&
1704 staged_status != GOT_STATUS_ADD) {
1705 *status = GOT_STATUS_ADD;
1706 goto done;
1709 if (!stat_info_differs(ie, sb))
1710 goto done;
1712 if (S_ISLNK(sb->st_mode) &&
1713 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1714 *status = GOT_STATUS_MODIFY;
1715 goto done;
1718 if (staged_status == GOT_STATUS_MODIFY ||
1719 staged_status == GOT_STATUS_ADD)
1720 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1721 else
1722 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1724 fd1 = got_opentempfd();
1725 if (fd1 == -1) {
1726 err = got_error_from_errno("got_opentempfd");
1727 goto done;
1729 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1730 if (err)
1731 goto done;
1733 if (S_ISLNK(sb->st_mode)) {
1734 err = get_symlink_modification_status(status, ie,
1735 abspath, dirfd, de_name, blob);
1736 goto done;
1739 if (dirfd != -1) {
1740 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1741 if (fd == -1) {
1742 err = got_error_from_errno2("openat", abspath);
1743 goto done;
1747 f = fdopen(fd, "r");
1748 if (f == NULL) {
1749 err = got_error_from_errno2("fdopen", abspath);
1750 goto done;
1752 fd = -1;
1753 hdrlen = got_object_blob_get_hdrlen(blob);
1754 for (;;) {
1755 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1756 err = got_object_blob_read_block(&blen, blob);
1757 if (err)
1758 goto done;
1759 /* Skip length of blob object header first time around. */
1760 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1761 if (flen == 0 && ferror(f)) {
1762 err = got_error_from_errno("fread");
1763 goto done;
1765 if (blen - hdrlen == 0) {
1766 if (flen != 0)
1767 *status = GOT_STATUS_MODIFY;
1768 break;
1769 } else if (flen == 0) {
1770 if (blen - hdrlen != 0)
1771 *status = GOT_STATUS_MODIFY;
1772 break;
1773 } else if (blen - hdrlen == flen) {
1774 /* Skip blob object header first time around. */
1775 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1776 *status = GOT_STATUS_MODIFY;
1777 break;
1779 } else {
1780 *status = GOT_STATUS_MODIFY;
1781 break;
1783 hdrlen = 0;
1786 if (*status == GOT_STATUS_MODIFY) {
1787 rewind(f);
1788 err = get_modified_file_content_status(status, f);
1789 } else if (xbit_differs(ie, sb->st_mode))
1790 *status = GOT_STATUS_MODE_CHANGE;
1791 done:
1792 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1793 err = got_error_from_errno("close");
1794 if (blob)
1795 got_object_blob_close(blob);
1796 if (f != NULL && fclose(f) == EOF && err == NULL)
1797 err = got_error_from_errno2("fclose", abspath);
1798 if (fd != -1 && close(fd) == -1 && err == NULL)
1799 err = got_error_from_errno2("close", abspath);
1800 return err;
1804 * Update timestamps in the file index if a file is unmodified and
1805 * we had to run a full content comparison to find out.
1807 static const struct got_error *
1808 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1809 struct got_fileindex_entry *ie, struct stat *sb)
1811 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1812 return got_fileindex_entry_update(ie, wt_fd, path,
1813 ie->blob_sha1, ie->commit_sha1, 1);
1815 return NULL;
1818 static const struct got_error *
1819 update_blob(struct got_worktree *worktree,
1820 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1821 struct got_tree_entry *te, const char *path,
1822 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1823 void *progress_arg)
1825 const struct got_error *err = NULL;
1826 struct got_blob_object *blob = NULL;
1827 char *ondisk_path = NULL;
1828 unsigned char status = GOT_STATUS_NO_CHANGE;
1829 struct stat sb;
1830 int fd1 = -1, fd2 = -1;
1832 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1833 return got_error_from_errno("asprintf");
1835 if (ie) {
1836 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1837 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1838 goto done;
1840 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1841 repo);
1842 if (err)
1843 goto done;
1844 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1845 sb.st_mode = got_fileindex_perms_to_st(ie);
1846 } else {
1847 if (stat(ondisk_path, &sb) == -1) {
1848 if (errno != ENOENT) {
1849 err = got_error_from_errno2("stat",
1850 ondisk_path);
1851 goto done;
1853 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1854 status = GOT_STATUS_UNVERSIONED;
1855 } else {
1856 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1857 status = GOT_STATUS_UNVERSIONED;
1858 else
1859 status = GOT_STATUS_OBSTRUCTED;
1863 if (status == GOT_STATUS_OBSTRUCTED) {
1864 if (ie)
1865 got_fileindex_entry_mark_skipped(ie);
1866 err = (*progress_cb)(progress_arg, status, path);
1867 goto done;
1869 if (status == GOT_STATUS_CONFLICT) {
1870 if (ie)
1871 got_fileindex_entry_mark_skipped(ie);
1872 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1873 path);
1874 goto done;
1877 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1878 (S_ISLNK(te->mode) ||
1879 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1881 * This is a regular file or an installed bad symlink.
1882 * If the file index indicates that this file is already
1883 * up-to-date with respect to the repository we can skip
1884 * updating contents of this file.
1886 if (got_fileindex_entry_has_commit(ie) &&
1887 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1888 SHA1_DIGEST_LENGTH) == 0) {
1889 /* Same commit. */
1890 err = sync_timestamps(worktree->root_fd,
1891 path, status, ie, &sb);
1892 if (err)
1893 goto done;
1894 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1895 path);
1896 goto done;
1898 if (got_fileindex_entry_has_blob(ie) &&
1899 memcmp(ie->blob_sha1, te->id.sha1,
1900 SHA1_DIGEST_LENGTH) == 0) {
1901 /* Different commit but the same blob. */
1902 err = sync_timestamps(worktree->root_fd,
1903 path, status, ie, &sb);
1904 if (err)
1905 goto done;
1906 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1907 path);
1908 goto done;
1912 fd1 = got_opentempfd();
1913 if (fd1 == -1) {
1914 err = got_error_from_errno("got_opentempfd");
1915 goto done;
1917 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1918 if (err)
1919 goto done;
1921 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1922 int update_timestamps;
1923 struct got_blob_object *blob2 = NULL;
1924 char *label_orig = NULL;
1925 if (got_fileindex_entry_has_blob(ie)) {
1926 fd2 = got_opentempfd();
1927 if (fd2 == -1) {
1928 err = got_error_from_errno("got_opentempfd");
1929 goto done;
1931 struct got_object_id id2;
1932 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1933 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1934 fd2);
1935 if (err)
1936 goto done;
1938 if (got_fileindex_entry_has_commit(ie)) {
1939 char id_str[SHA1_DIGEST_STRING_LENGTH];
1940 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1941 sizeof(id_str)) == NULL) {
1942 err = got_error_path(id_str,
1943 GOT_ERR_BAD_OBJ_ID_STR);
1944 goto done;
1946 if (asprintf(&label_orig, "%s: commit %s",
1947 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1948 err = got_error_from_errno("asprintf");
1949 goto done;
1952 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1953 char *link_target;
1954 err = got_object_blob_read_to_str(&link_target, blob);
1955 if (err)
1956 goto done;
1957 err = merge_symlink(worktree, blob2, ondisk_path, path,
1958 label_orig, link_target, worktree->base_commit_id,
1959 repo, progress_cb, progress_arg);
1960 free(link_target);
1961 } else {
1962 err = merge_blob(&update_timestamps, worktree, blob2,
1963 ondisk_path, path, sb.st_mode, label_orig, blob,
1964 worktree->base_commit_id, repo,
1965 progress_cb, progress_arg);
1967 free(label_orig);
1968 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1969 err = got_error_from_errno("close");
1970 goto done;
1972 if (blob2)
1973 got_object_blob_close(blob2);
1974 if (err)
1975 goto done;
1977 * Do not update timestamps of files with local changes.
1978 * Otherwise, a future status walk would treat them as
1979 * unmodified files again.
1981 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1982 blob->id.sha1, worktree->base_commit_id->sha1,
1983 update_timestamps);
1984 } else if (status == GOT_STATUS_MODE_CHANGE) {
1985 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1986 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1987 } else if (status == GOT_STATUS_DELETE) {
1988 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1989 if (err)
1990 goto done;
1991 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1992 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1993 if (err)
1994 goto done;
1995 } else {
1996 int is_bad_symlink = 0;
1997 if (S_ISLNK(te->mode)) {
1998 err = install_symlink(&is_bad_symlink, worktree,
1999 ondisk_path, path, blob,
2000 status == GOT_STATUS_MISSING, 0,
2001 status == GOT_STATUS_UNVERSIONED, 0,
2002 repo, progress_cb, progress_arg);
2003 } else {
2004 err = install_blob(worktree, ondisk_path, path,
2005 te->mode, sb.st_mode, blob,
2006 status == GOT_STATUS_MISSING, 0, 0,
2007 status == GOT_STATUS_UNVERSIONED, repo,
2008 progress_cb, progress_arg);
2010 if (err)
2011 goto done;
2013 if (ie) {
2014 err = got_fileindex_entry_update(ie,
2015 worktree->root_fd, path, blob->id.sha1,
2016 worktree->base_commit_id->sha1, 1);
2017 } else {
2018 err = create_fileindex_entry(&ie, fileindex,
2019 worktree->base_commit_id, worktree->root_fd, path,
2020 &blob->id);
2022 if (err)
2023 goto done;
2025 if (is_bad_symlink) {
2026 got_fileindex_entry_filetype_set(ie,
2027 GOT_FILEIDX_MODE_BAD_SYMLINK);
2031 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2032 err = got_error_from_errno("close");
2033 goto done;
2035 got_object_blob_close(blob);
2036 done:
2037 free(ondisk_path);
2038 return err;
2041 static const struct got_error *
2042 remove_ondisk_file(const char *root_path, const char *path)
2044 const struct got_error *err = NULL;
2045 char *ondisk_path = NULL, *parent = NULL;
2047 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2048 return got_error_from_errno("asprintf");
2050 if (unlink(ondisk_path) == -1) {
2051 if (errno != ENOENT)
2052 err = got_error_from_errno2("unlink", ondisk_path);
2053 } else {
2054 size_t root_len = strlen(root_path);
2055 err = got_path_dirname(&parent, ondisk_path);
2056 if (err)
2057 goto done;
2058 while (got_path_cmp(parent, root_path,
2059 strlen(parent), root_len) != 0) {
2060 free(ondisk_path);
2061 ondisk_path = parent;
2062 parent = NULL;
2063 if (rmdir(ondisk_path) == -1) {
2064 if (errno != ENOTEMPTY)
2065 err = got_error_from_errno2("rmdir",
2066 ondisk_path);
2067 break;
2069 err = got_path_dirname(&parent, ondisk_path);
2070 if (err)
2071 break;
2074 done:
2075 free(ondisk_path);
2076 free(parent);
2077 return err;
2080 static const struct got_error *
2081 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2082 struct got_fileindex_entry *ie, struct got_repository *repo,
2083 got_worktree_checkout_cb progress_cb, void *progress_arg)
2085 const struct got_error *err = NULL;
2086 unsigned char status;
2087 struct stat sb;
2088 char *ondisk_path;
2090 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2091 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2093 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2094 == -1)
2095 return got_error_from_errno("asprintf");
2097 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2098 if (err)
2099 goto done;
2101 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2102 char ondisk_target[PATH_MAX];
2103 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2104 sizeof(ondisk_target));
2105 if (ondisk_len == -1) {
2106 err = got_error_from_errno2("readlink", ondisk_path);
2107 goto done;
2109 ondisk_target[ondisk_len] = '\0';
2110 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2111 NULL, NULL, /* XXX pass common ancestor info? */
2112 ondisk_target, ondisk_path);
2113 if (err)
2114 goto done;
2115 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2116 ie->path);
2117 goto done;
2120 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2121 status == GOT_STATUS_ADD) {
2122 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2123 if (err)
2124 goto done;
2126 * Preserve the working file and change the deleted blob's
2127 * entry into a schedule-add entry.
2129 err = got_fileindex_entry_update(ie, worktree->root_fd,
2130 ie->path, NULL, NULL, 0);
2131 } else {
2132 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2133 if (err)
2134 goto done;
2135 if (status == GOT_STATUS_NO_CHANGE) {
2136 err = remove_ondisk_file(worktree->root_path, ie->path);
2137 if (err)
2138 goto done;
2140 got_fileindex_entry_remove(fileindex, ie);
2142 done:
2143 free(ondisk_path);
2144 return err;
2147 struct diff_cb_arg {
2148 struct got_fileindex *fileindex;
2149 struct got_worktree *worktree;
2150 struct got_repository *repo;
2151 got_worktree_checkout_cb progress_cb;
2152 void *progress_arg;
2153 got_cancel_cb cancel_cb;
2154 void *cancel_arg;
2157 static const struct got_error *
2158 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2159 struct got_tree_entry *te, const char *parent_path)
2161 struct diff_cb_arg *a = arg;
2163 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2164 return got_error(GOT_ERR_CANCELLED);
2166 return update_blob(a->worktree, a->fileindex, ie, te,
2167 ie->path, a->repo, a->progress_cb, a->progress_arg);
2170 static const struct got_error *
2171 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2173 struct diff_cb_arg *a = arg;
2175 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2176 return got_error(GOT_ERR_CANCELLED);
2178 return delete_blob(a->worktree, a->fileindex, ie,
2179 a->repo, a->progress_cb, a->progress_arg);
2182 static const struct got_error *
2183 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2185 struct diff_cb_arg *a = arg;
2186 const struct got_error *err;
2187 char *path;
2189 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2190 return got_error(GOT_ERR_CANCELLED);
2192 if (got_object_tree_entry_is_submodule(te))
2193 return NULL;
2195 if (asprintf(&path, "%s%s%s", parent_path,
2196 parent_path[0] ? "/" : "", te->name)
2197 == -1)
2198 return got_error_from_errno("asprintf");
2200 if (S_ISDIR(te->mode))
2201 err = add_dir_on_disk(a->worktree, path);
2202 else
2203 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2204 a->repo, a->progress_cb, a->progress_arg);
2206 free(path);
2207 return err;
2210 const struct got_error *
2211 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2213 uint32_t uuid_status;
2215 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2216 if (uuid_status != uuid_s_ok) {
2217 *uuidstr = NULL;
2218 return got_error_uuid(uuid_status, "uuid_to_string");
2221 return NULL;
2224 static const struct got_error *
2225 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2227 const struct got_error *err = NULL;
2228 char *uuidstr = NULL;
2230 *refname = NULL;
2232 err = got_worktree_get_uuid(&uuidstr, worktree);
2233 if (err)
2234 return err;
2236 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2237 err = got_error_from_errno("asprintf");
2238 *refname = NULL;
2240 free(uuidstr);
2241 return err;
2244 const struct got_error *
2245 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2247 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2250 static const struct got_error *
2251 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2253 return get_ref_name(refname, worktree,
2254 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2257 static const struct got_error *
2258 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2260 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2263 static const struct got_error *
2264 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2266 return get_ref_name(refname, worktree,
2267 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2270 static const struct got_error *
2271 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2273 return get_ref_name(refname, worktree,
2274 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2277 static const struct got_error *
2278 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2280 return get_ref_name(refname, worktree,
2281 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2284 static const struct got_error *
2285 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2287 return get_ref_name(refname, worktree,
2288 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2291 static const struct got_error *
2292 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2294 return get_ref_name(refname, worktree,
2295 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2298 static const struct got_error *
2299 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2301 return get_ref_name(refname, worktree,
2302 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2305 const struct got_error *
2306 got_worktree_get_histedit_script_path(char **path,
2307 struct got_worktree *worktree)
2309 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2310 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2311 *path = NULL;
2312 return got_error_from_errno("asprintf");
2314 return NULL;
2317 static const struct got_error *
2318 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2320 return get_ref_name(refname, worktree,
2321 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2324 static const struct got_error *
2325 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree,
2328 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2332 * Prevent Git's garbage collector from deleting our base commit by
2333 * setting a reference to our base commit's ID.
2335 static const struct got_error *
2336 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2338 const struct got_error *err = NULL;
2339 struct got_reference *ref = NULL;
2340 char *refname;
2342 err = got_worktree_get_base_ref_name(&refname, worktree);
2343 if (err)
2344 return err;
2346 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2347 if (err)
2348 goto done;
2350 err = got_ref_write(ref, repo);
2351 done:
2352 free(refname);
2353 if (ref)
2354 got_ref_close(ref);
2355 return err;
2358 static const struct got_error *
2359 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2361 const struct got_error *err = NULL;
2363 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2364 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2365 err = got_error_from_errno("asprintf");
2366 *fileindex_path = NULL;
2368 return err;
2372 static const struct got_error *
2373 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2374 struct got_worktree *worktree)
2376 const struct got_error *err = NULL;
2377 FILE *index = NULL;
2379 *fileindex_path = NULL;
2380 *fileindex = got_fileindex_alloc();
2381 if (*fileindex == NULL)
2382 return got_error_from_errno("got_fileindex_alloc");
2384 err = get_fileindex_path(fileindex_path, worktree);
2385 if (err)
2386 goto done;
2388 index = fopen(*fileindex_path, "rbe");
2389 if (index == NULL) {
2390 if (errno != ENOENT)
2391 err = got_error_from_errno2("fopen", *fileindex_path);
2392 } else {
2393 err = got_fileindex_read(*fileindex, index);
2394 if (fclose(index) == EOF && err == NULL)
2395 err = got_error_from_errno("fclose");
2397 done:
2398 if (err) {
2399 free(*fileindex_path);
2400 *fileindex_path = NULL;
2401 got_fileindex_free(*fileindex);
2402 *fileindex = NULL;
2404 return err;
2407 struct bump_base_commit_id_arg {
2408 struct got_object_id *base_commit_id;
2409 const char *path;
2410 size_t path_len;
2411 const char *entry_name;
2412 got_worktree_checkout_cb progress_cb;
2413 void *progress_arg;
2416 /* Bump base commit ID of all files within an updated part of the work tree. */
2417 static const struct got_error *
2418 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2420 const struct got_error *err;
2421 struct bump_base_commit_id_arg *a = arg;
2423 if (a->entry_name) {
2424 if (strcmp(ie->path, a->path) != 0)
2425 return NULL;
2426 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2427 return NULL;
2429 if (got_fileindex_entry_was_skipped(ie))
2430 return NULL;
2432 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2433 SHA1_DIGEST_LENGTH) == 0)
2434 return NULL;
2436 if (a->progress_cb) {
2437 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2438 ie->path);
2439 if (err)
2440 return err;
2442 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2443 return NULL;
2446 static const struct got_error *
2447 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2448 struct got_fileindex *fileindex,
2449 got_worktree_checkout_cb progress_cb, void *progress_arg)
2451 struct bump_base_commit_id_arg bbc_arg;
2453 bbc_arg.base_commit_id = worktree->base_commit_id;
2454 bbc_arg.entry_name = NULL;
2455 bbc_arg.path = "";
2456 bbc_arg.path_len = 0;
2457 bbc_arg.progress_cb = progress_cb;
2458 bbc_arg.progress_arg = progress_arg;
2460 return got_fileindex_for_each_entry_safe(fileindex,
2461 bump_base_commit_id, &bbc_arg);
2464 static const struct got_error *
2465 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2467 const struct got_error *err = NULL;
2468 char *new_fileindex_path = NULL;
2469 FILE *new_index = NULL;
2470 struct timespec timeout;
2472 err = got_opentemp_named(&new_fileindex_path, &new_index,
2473 fileindex_path, "");
2474 if (err)
2475 goto done;
2477 err = got_fileindex_write(fileindex, new_index);
2478 if (err)
2479 goto done;
2481 if (rename(new_fileindex_path, fileindex_path) != 0) {
2482 err = got_error_from_errno3("rename", new_fileindex_path,
2483 fileindex_path);
2484 unlink(new_fileindex_path);
2488 * Sleep for a short amount of time to ensure that files modified after
2489 * this program exits have a different time stamp from the one which
2490 * was recorded in the file index.
2492 timeout.tv_sec = 0;
2493 timeout.tv_nsec = 1;
2494 nanosleep(&timeout, NULL);
2495 done:
2496 if (new_index)
2497 fclose(new_index);
2498 free(new_fileindex_path);
2499 return err;
2502 static const struct got_error *
2503 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2504 struct got_object_id **tree_id, const char *wt_relpath,
2505 struct got_commit_object *base_commit, struct got_worktree *worktree,
2506 struct got_repository *repo)
2508 const struct got_error *err = NULL;
2509 struct got_object_id *id = NULL;
2510 char *in_repo_path = NULL;
2511 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2513 *entry_type = GOT_OBJ_TYPE_ANY;
2514 *tree_relpath = NULL;
2515 *tree_id = NULL;
2517 if (wt_relpath[0] == '\0') {
2518 /* Check out all files within the work tree. */
2519 *entry_type = GOT_OBJ_TYPE_TREE;
2520 *tree_relpath = strdup("");
2521 if (*tree_relpath == NULL) {
2522 err = got_error_from_errno("strdup");
2523 goto done;
2525 err = got_object_id_by_path(tree_id, repo, base_commit,
2526 worktree->path_prefix);
2527 if (err)
2528 goto done;
2529 return NULL;
2532 /* Check out a subset of files in the work tree. */
2534 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2535 is_root_wt ? "" : "/", wt_relpath) == -1) {
2536 err = got_error_from_errno("asprintf");
2537 goto done;
2540 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2541 if (err)
2542 goto done;
2544 free(in_repo_path);
2545 in_repo_path = NULL;
2547 err = got_object_get_type(entry_type, repo, id);
2548 if (err)
2549 goto done;
2551 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2552 /* Check out a single file. */
2553 if (strchr(wt_relpath, '/') == NULL) {
2554 /* Check out a single file in work tree's root dir. */
2555 in_repo_path = strdup(worktree->path_prefix);
2556 if (in_repo_path == NULL) {
2557 err = got_error_from_errno("strdup");
2558 goto done;
2560 *tree_relpath = strdup("");
2561 if (*tree_relpath == NULL) {
2562 err = got_error_from_errno("strdup");
2563 goto done;
2565 } else {
2566 /* Check out a single file in a subdirectory. */
2567 err = got_path_dirname(tree_relpath, wt_relpath);
2568 if (err)
2569 return err;
2570 if (asprintf(&in_repo_path, "%s%s%s",
2571 worktree->path_prefix, is_root_wt ? "" : "/",
2572 *tree_relpath) == -1) {
2573 err = got_error_from_errno("asprintf");
2574 goto done;
2577 err = got_object_id_by_path(tree_id, repo,
2578 base_commit, in_repo_path);
2579 } else {
2580 /* Check out all files within a subdirectory. */
2581 *tree_id = got_object_id_dup(id);
2582 if (*tree_id == NULL) {
2583 err = got_error_from_errno("got_object_id_dup");
2584 goto done;
2586 *tree_relpath = strdup(wt_relpath);
2587 if (*tree_relpath == NULL) {
2588 err = got_error_from_errno("strdup");
2589 goto done;
2592 done:
2593 free(id);
2594 free(in_repo_path);
2595 if (err) {
2596 *entry_type = GOT_OBJ_TYPE_ANY;
2597 free(*tree_relpath);
2598 *tree_relpath = NULL;
2599 free(*tree_id);
2600 *tree_id = NULL;
2602 return err;
2605 static const struct got_error *
2606 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2607 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2608 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2609 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2611 const struct got_error *err = NULL;
2612 struct got_commit_object *commit = NULL;
2613 struct got_tree_object *tree = NULL;
2614 struct got_fileindex_diff_tree_cb diff_cb;
2615 struct diff_cb_arg arg;
2617 err = ref_base_commit(worktree, repo);
2618 if (err) {
2619 if (!(err->code == GOT_ERR_ERRNO &&
2620 (errno == EACCES || errno == EROFS)))
2621 goto done;
2622 err = (*progress_cb)(progress_arg,
2623 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2624 if (err)
2625 return err;
2628 err = got_object_open_as_commit(&commit, repo,
2629 worktree->base_commit_id);
2630 if (err)
2631 goto done;
2633 err = got_object_open_as_tree(&tree, repo, tree_id);
2634 if (err)
2635 goto done;
2637 if (entry_name &&
2638 got_object_tree_find_entry(tree, entry_name) == NULL) {
2639 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2640 goto done;
2643 diff_cb.diff_old_new = diff_old_new;
2644 diff_cb.diff_old = diff_old;
2645 diff_cb.diff_new = diff_new;
2646 arg.fileindex = fileindex;
2647 arg.worktree = worktree;
2648 arg.repo = repo;
2649 arg.progress_cb = progress_cb;
2650 arg.progress_arg = progress_arg;
2651 arg.cancel_cb = cancel_cb;
2652 arg.cancel_arg = cancel_arg;
2653 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2654 entry_name, repo, &diff_cb, &arg);
2655 done:
2656 if (tree)
2657 got_object_tree_close(tree);
2658 if (commit)
2659 got_object_commit_close(commit);
2660 return err;
2663 const struct got_error *
2664 got_worktree_checkout_files(struct got_worktree *worktree,
2665 struct got_pathlist_head *paths, struct got_repository *repo,
2666 got_worktree_checkout_cb progress_cb, void *progress_arg,
2667 got_cancel_cb cancel_cb, void *cancel_arg)
2669 const struct got_error *err = NULL, *sync_err, *unlockerr;
2670 struct got_commit_object *commit = NULL;
2671 struct got_tree_object *tree = NULL;
2672 struct got_fileindex *fileindex = NULL;
2673 char *fileindex_path = NULL;
2674 struct got_pathlist_entry *pe;
2675 struct tree_path_data {
2676 STAILQ_ENTRY(tree_path_data) entry;
2677 struct got_object_id *tree_id;
2678 int entry_type;
2679 char *relpath;
2680 char *entry_name;
2681 } *tpd = NULL;
2682 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2684 STAILQ_INIT(&tree_paths);
2686 err = lock_worktree(worktree, LOCK_EX);
2687 if (err)
2688 return err;
2690 err = got_object_open_as_commit(&commit, repo,
2691 worktree->base_commit_id);
2692 if (err)
2693 goto done;
2695 /* Map all specified paths to in-repository trees. */
2696 TAILQ_FOREACH(pe, paths, entry) {
2697 tpd = malloc(sizeof(*tpd));
2698 if (tpd == NULL) {
2699 err = got_error_from_errno("malloc");
2700 goto done;
2703 err = find_tree_entry_for_checkout(&tpd->entry_type,
2704 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2705 worktree, repo);
2706 if (err) {
2707 free(tpd);
2708 goto done;
2711 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2712 err = got_path_basename(&tpd->entry_name, pe->path);
2713 if (err) {
2714 free(tpd->relpath);
2715 free(tpd->tree_id);
2716 free(tpd);
2717 goto done;
2719 } else
2720 tpd->entry_name = NULL;
2722 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2726 * Read the file index.
2727 * Checking out files is supposed to be an idempotent operation.
2728 * If the on-disk file index is incomplete we will try to complete it.
2730 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2731 if (err)
2732 goto done;
2734 tpd = STAILQ_FIRST(&tree_paths);
2735 TAILQ_FOREACH(pe, paths, entry) {
2736 struct bump_base_commit_id_arg bbc_arg;
2738 err = checkout_files(worktree, fileindex, tpd->relpath,
2739 tpd->tree_id, tpd->entry_name, repo,
2740 progress_cb, progress_arg, cancel_cb, cancel_arg);
2741 if (err)
2742 break;
2744 bbc_arg.base_commit_id = worktree->base_commit_id;
2745 bbc_arg.entry_name = tpd->entry_name;
2746 bbc_arg.path = pe->path;
2747 bbc_arg.path_len = pe->path_len;
2748 bbc_arg.progress_cb = progress_cb;
2749 bbc_arg.progress_arg = progress_arg;
2750 err = got_fileindex_for_each_entry_safe(fileindex,
2751 bump_base_commit_id, &bbc_arg);
2752 if (err)
2753 break;
2755 tpd = STAILQ_NEXT(tpd, entry);
2757 sync_err = sync_fileindex(fileindex, fileindex_path);
2758 if (sync_err && err == NULL)
2759 err = sync_err;
2760 done:
2761 free(fileindex_path);
2762 if (tree)
2763 got_object_tree_close(tree);
2764 if (commit)
2765 got_object_commit_close(commit);
2766 if (fileindex)
2767 got_fileindex_free(fileindex);
2768 while (!STAILQ_EMPTY(&tree_paths)) {
2769 tpd = STAILQ_FIRST(&tree_paths);
2770 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2771 free(tpd->relpath);
2772 free(tpd->tree_id);
2773 free(tpd);
2775 unlockerr = lock_worktree(worktree, LOCK_SH);
2776 if (unlockerr && err == NULL)
2777 err = unlockerr;
2778 return err;
2781 struct merge_file_cb_arg {
2782 struct got_worktree *worktree;
2783 struct got_fileindex *fileindex;
2784 got_worktree_checkout_cb progress_cb;
2785 void *progress_arg;
2786 got_cancel_cb cancel_cb;
2787 void *cancel_arg;
2788 const char *label_orig;
2789 struct got_object_id *commit_id2;
2790 int allow_bad_symlinks;
2793 static const struct got_error *
2794 merge_file_cb(void *arg, struct got_blob_object *blob1,
2795 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2796 struct got_object_id *id1, struct got_object_id *id2,
2797 const char *path1, const char *path2,
2798 mode_t mode1, mode_t mode2, struct got_repository *repo)
2800 static const struct got_error *err = NULL;
2801 struct merge_file_cb_arg *a = arg;
2802 struct got_fileindex_entry *ie;
2803 char *ondisk_path = NULL;
2804 struct stat sb;
2805 unsigned char status;
2806 int local_changes_subsumed;
2807 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2808 char *id_str = NULL, *label_deriv2 = NULL;
2810 if (blob1 && blob2) {
2811 ie = got_fileindex_entry_get(a->fileindex, path2,
2812 strlen(path2));
2813 if (ie == NULL)
2814 return (*a->progress_cb)(a->progress_arg,
2815 GOT_STATUS_MISSING, path2);
2817 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2818 path2) == -1)
2819 return got_error_from_errno("asprintf");
2821 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2822 repo);
2823 if (err)
2824 goto done;
2826 if (status == GOT_STATUS_DELETE) {
2827 err = (*a->progress_cb)(a->progress_arg,
2828 GOT_STATUS_MERGE, path2);
2829 goto done;
2831 if (status != GOT_STATUS_NO_CHANGE &&
2832 status != GOT_STATUS_MODIFY &&
2833 status != GOT_STATUS_CONFLICT &&
2834 status != GOT_STATUS_ADD) {
2835 err = (*a->progress_cb)(a->progress_arg, status, path2);
2836 goto done;
2839 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2840 char *link_target2;
2841 err = got_object_blob_read_to_str(&link_target2, blob2);
2842 if (err)
2843 goto done;
2844 err = merge_symlink(a->worktree, blob1, ondisk_path,
2845 path2, a->label_orig, link_target2, a->commit_id2,
2846 repo, a->progress_cb, a->progress_arg);
2847 free(link_target2);
2848 } else {
2849 int fd;
2851 f_orig = got_opentemp();
2852 if (f_orig == NULL) {
2853 err = got_error_from_errno("got_opentemp");
2854 goto done;
2856 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2857 f_orig, blob1);
2858 if (err)
2859 goto done;
2861 f_deriv2 = got_opentemp();
2862 if (f_deriv2 == NULL)
2863 goto done;
2864 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2865 f_deriv2, blob2);
2866 if (err)
2867 goto done;
2869 fd = open(ondisk_path,
2870 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2871 if (fd == -1) {
2872 err = got_error_from_errno2("open",
2873 ondisk_path);
2874 goto done;
2876 f_deriv = fdopen(fd, "r");
2877 if (f_deriv == NULL) {
2878 err = got_error_from_errno2("fdopen",
2879 ondisk_path);
2880 close(fd);
2881 goto done;
2883 err = got_object_id_str(&id_str, a->commit_id2);
2884 if (err)
2885 goto done;
2886 if (asprintf(&label_deriv2, "%s: commit %s",
2887 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2888 err = got_error_from_errno("asprintf");
2889 goto done;
2891 err = merge_file(&local_changes_subsumed, a->worktree,
2892 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2893 sb.st_mode, a->label_orig, NULL, label_deriv2,
2894 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2895 a->progress_cb, a->progress_arg);
2897 } else if (blob1) {
2898 ie = got_fileindex_entry_get(a->fileindex, path1,
2899 strlen(path1));
2900 if (ie == NULL)
2901 return (*a->progress_cb)(a->progress_arg,
2902 GOT_STATUS_MISSING, path1);
2904 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2905 path1) == -1)
2906 return got_error_from_errno("asprintf");
2908 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2909 repo);
2910 if (err)
2911 goto done;
2913 switch (status) {
2914 case GOT_STATUS_NO_CHANGE:
2915 err = (*a->progress_cb)(a->progress_arg,
2916 GOT_STATUS_DELETE, path1);
2917 if (err)
2918 goto done;
2919 err = remove_ondisk_file(a->worktree->root_path, path1);
2920 if (err)
2921 goto done;
2922 if (ie)
2923 got_fileindex_entry_mark_deleted_from_disk(ie);
2924 break;
2925 case GOT_STATUS_DELETE:
2926 case GOT_STATUS_MISSING:
2927 err = (*a->progress_cb)(a->progress_arg,
2928 GOT_STATUS_DELETE, path1);
2929 if (err)
2930 goto done;
2931 if (ie)
2932 got_fileindex_entry_mark_deleted_from_disk(ie);
2933 break;
2934 case GOT_STATUS_ADD: {
2935 struct got_object_id *id;
2936 FILE *blob1_f;
2937 off_t blob1_size;
2939 * Delete the added file only if its content already
2940 * exists in the repository.
2942 err = got_object_blob_file_create(&id, &blob1_f,
2943 &blob1_size, path1);
2944 if (err)
2945 goto done;
2946 if (got_object_id_cmp(id, id1) == 0) {
2947 err = (*a->progress_cb)(a->progress_arg,
2948 GOT_STATUS_DELETE, path1);
2949 if (err)
2950 goto done;
2951 err = remove_ondisk_file(a->worktree->root_path,
2952 path1);
2953 if (err)
2954 goto done;
2955 if (ie)
2956 got_fileindex_entry_remove(a->fileindex,
2957 ie);
2958 } else {
2959 err = (*a->progress_cb)(a->progress_arg,
2960 GOT_STATUS_CANNOT_DELETE, path1);
2962 if (fclose(blob1_f) == EOF && err == NULL)
2963 err = got_error_from_errno("fclose");
2964 free(id);
2965 if (err)
2966 goto done;
2967 break;
2969 case GOT_STATUS_MODIFY:
2970 case GOT_STATUS_CONFLICT:
2971 err = (*a->progress_cb)(a->progress_arg,
2972 GOT_STATUS_CANNOT_DELETE, path1);
2973 if (err)
2974 goto done;
2975 break;
2976 case GOT_STATUS_OBSTRUCTED:
2977 err = (*a->progress_cb)(a->progress_arg, status, path1);
2978 if (err)
2979 goto done;
2980 break;
2981 default:
2982 break;
2984 } else if (blob2) {
2985 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2986 path2) == -1)
2987 return got_error_from_errno("asprintf");
2988 ie = got_fileindex_entry_get(a->fileindex, path2,
2989 strlen(path2));
2990 if (ie) {
2991 err = get_file_status(&status, &sb, ie, ondisk_path,
2992 -1, NULL, repo);
2993 if (err)
2994 goto done;
2995 if (status != GOT_STATUS_NO_CHANGE &&
2996 status != GOT_STATUS_MODIFY &&
2997 status != GOT_STATUS_CONFLICT &&
2998 status != GOT_STATUS_ADD) {
2999 err = (*a->progress_cb)(a->progress_arg,
3000 status, path2);
3001 goto done;
3003 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3004 char *link_target2;
3005 err = got_object_blob_read_to_str(&link_target2,
3006 blob2);
3007 if (err)
3008 goto done;
3009 err = merge_symlink(a->worktree, NULL,
3010 ondisk_path, path2, a->label_orig,
3011 link_target2, a->commit_id2, repo,
3012 a->progress_cb, a->progress_arg);
3013 free(link_target2);
3014 } else if (S_ISREG(sb.st_mode)) {
3015 err = merge_blob(&local_changes_subsumed,
3016 a->worktree, NULL, ondisk_path, path2,
3017 sb.st_mode, a->label_orig, blob2,
3018 a->commit_id2, repo, a->progress_cb,
3019 a->progress_arg);
3020 } else {
3021 err = got_error_path(ondisk_path,
3022 GOT_ERR_FILE_OBSTRUCTED);
3024 if (err)
3025 goto done;
3026 if (status == GOT_STATUS_DELETE) {
3027 err = got_fileindex_entry_update(ie,
3028 a->worktree->root_fd, path2, blob2->id.sha1,
3029 a->worktree->base_commit_id->sha1, 0);
3030 if (err)
3031 goto done;
3033 } else {
3034 int is_bad_symlink = 0;
3035 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3036 if (S_ISLNK(mode2)) {
3037 err = install_symlink(&is_bad_symlink,
3038 a->worktree, ondisk_path, path2, blob2, 0,
3039 0, 1, a->allow_bad_symlinks, repo,
3040 a->progress_cb, a->progress_arg);
3041 } else {
3042 err = install_blob(a->worktree, ondisk_path, path2,
3043 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3044 a->progress_cb, a->progress_arg);
3046 if (err)
3047 goto done;
3048 err = got_fileindex_entry_alloc(&ie, path2);
3049 if (err)
3050 goto done;
3051 err = got_fileindex_entry_update(ie,
3052 a->worktree->root_fd, path2, NULL, NULL, 1);
3053 if (err) {
3054 got_fileindex_entry_free(ie);
3055 goto done;
3057 err = got_fileindex_entry_add(a->fileindex, ie);
3058 if (err) {
3059 got_fileindex_entry_free(ie);
3060 goto done;
3062 if (is_bad_symlink) {
3063 got_fileindex_entry_filetype_set(ie,
3064 GOT_FILEIDX_MODE_BAD_SYMLINK);
3068 done:
3069 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3070 err = got_error_from_errno("fclose");
3071 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3072 err = got_error_from_errno("fclose");
3073 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3074 err = got_error_from_errno("fclose");
3075 free(id_str);
3076 free(label_deriv2);
3077 free(ondisk_path);
3078 return err;
3081 static const struct got_error *
3082 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3084 struct got_worktree *worktree = arg;
3086 /* Reject merges into a work tree with mixed base commits. */
3087 if (got_fileindex_entry_has_commit(ie) &&
3088 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3089 SHA1_DIGEST_LENGTH) != 0)
3090 return got_error(GOT_ERR_MIXED_COMMITS);
3092 return NULL;
3095 struct check_merge_conflicts_arg {
3096 struct got_worktree *worktree;
3097 struct got_fileindex *fileindex;
3098 struct got_repository *repo;
3101 static const struct got_error *
3102 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3103 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3104 struct got_object_id *id1, struct got_object_id *id2,
3105 const char *path1, const char *path2,
3106 mode_t mode1, mode_t mode2, struct got_repository *repo)
3108 const struct got_error *err = NULL;
3109 struct check_merge_conflicts_arg *a = arg;
3110 unsigned char status;
3111 struct stat sb;
3112 struct got_fileindex_entry *ie;
3113 const char *path = path2 ? path2 : path1;
3114 struct got_object_id *id = id2 ? id2 : id1;
3115 char *ondisk_path;
3117 if (id == NULL)
3118 return NULL;
3120 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3121 if (ie == NULL)
3122 return NULL;
3124 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3125 == -1)
3126 return got_error_from_errno("asprintf");
3128 /* Reject merges into a work tree with conflicted files. */
3129 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3130 free(ondisk_path);
3131 if (err)
3132 return err;
3133 if (status == GOT_STATUS_CONFLICT)
3134 return got_error(GOT_ERR_CONFLICTS);
3136 return NULL;
3139 static const struct got_error *
3140 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3141 const char *fileindex_path, struct got_object_id *commit_id1,
3142 struct got_object_id *commit_id2, struct got_repository *repo,
3143 got_worktree_checkout_cb progress_cb, void *progress_arg,
3144 got_cancel_cb cancel_cb, void *cancel_arg)
3146 const struct got_error *err = NULL, *sync_err;
3147 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3148 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3149 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3150 struct check_merge_conflicts_arg cmc_arg;
3151 struct merge_file_cb_arg arg;
3152 char *label_orig = NULL;
3153 FILE *f1 = NULL, *f2 = NULL;
3154 int fd1 = -1, fd2 = -1;
3156 if (commit_id1) {
3157 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3158 if (err)
3159 goto done;
3160 err = got_object_id_by_path(&tree_id1, repo, commit1,
3161 worktree->path_prefix);
3162 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3163 goto done;
3165 if (tree_id1) {
3166 char *id_str;
3168 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3169 if (err)
3170 goto done;
3172 err = got_object_id_str(&id_str, commit_id1);
3173 if (err)
3174 goto done;
3176 if (asprintf(&label_orig, "%s: commit %s",
3177 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3178 err = got_error_from_errno("asprintf");
3179 free(id_str);
3180 goto done;
3182 free(id_str);
3184 f1 = got_opentemp();
3185 if (f1 == NULL) {
3186 err = got_error_from_errno("got_opentemp");
3187 goto done;
3191 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3192 if (err)
3193 goto done;
3195 err = got_object_id_by_path(&tree_id2, repo, commit2,
3196 worktree->path_prefix);
3197 if (err)
3198 goto done;
3200 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3201 if (err)
3202 goto done;
3204 f2 = got_opentemp();
3205 if (f2 == NULL) {
3206 err = got_error_from_errno("got_opentemp");
3207 goto done;
3210 fd1 = got_opentempfd();
3211 if (fd1 == -1) {
3212 err = got_error_from_errno("got_opentempfd");
3213 goto done;
3216 fd2 = got_opentempfd();
3217 if (fd2 == -1) {
3218 err = got_error_from_errno("got_opentempfd");
3219 goto done;
3222 cmc_arg.worktree = worktree;
3223 cmc_arg.fileindex = fileindex;
3224 cmc_arg.repo = repo;
3225 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3226 check_merge_conflicts, &cmc_arg, 0);
3227 if (err)
3228 goto done;
3230 arg.worktree = worktree;
3231 arg.fileindex = fileindex;
3232 arg.progress_cb = progress_cb;
3233 arg.progress_arg = progress_arg;
3234 arg.cancel_cb = cancel_cb;
3235 arg.cancel_arg = cancel_arg;
3236 arg.label_orig = label_orig;
3237 arg.commit_id2 = commit_id2;
3238 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3239 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3240 merge_file_cb, &arg, 1);
3241 sync_err = sync_fileindex(fileindex, fileindex_path);
3242 if (sync_err && err == NULL)
3243 err = sync_err;
3244 done:
3245 if (commit1)
3246 got_object_commit_close(commit1);
3247 if (commit2)
3248 got_object_commit_close(commit2);
3249 if (tree1)
3250 got_object_tree_close(tree1);
3251 if (tree2)
3252 got_object_tree_close(tree2);
3253 if (f1 && fclose(f1) == EOF && err == NULL)
3254 err = got_error_from_errno("fclose");
3255 if (f2 && fclose(f2) == EOF && err == NULL)
3256 err = got_error_from_errno("fclose");
3257 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3258 err = got_error_from_errno("close");
3259 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3260 err = got_error_from_errno("close");
3261 free(label_orig);
3262 return err;
3265 const struct got_error *
3266 got_worktree_merge_files(struct got_worktree *worktree,
3267 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3268 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3269 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3271 const struct got_error *err, *unlockerr;
3272 char *fileindex_path = NULL;
3273 struct got_fileindex *fileindex = NULL;
3275 err = lock_worktree(worktree, LOCK_EX);
3276 if (err)
3277 return err;
3279 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3280 if (err)
3281 goto done;
3283 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3284 worktree);
3285 if (err)
3286 goto done;
3288 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3289 commit_id2, repo, progress_cb, progress_arg,
3290 cancel_cb, cancel_arg);
3291 done:
3292 if (fileindex)
3293 got_fileindex_free(fileindex);
3294 free(fileindex_path);
3295 unlockerr = lock_worktree(worktree, LOCK_SH);
3296 if (unlockerr && err == NULL)
3297 err = unlockerr;
3298 return err;
3301 struct diff_dir_cb_arg {
3302 struct got_fileindex *fileindex;
3303 struct got_worktree *worktree;
3304 const char *status_path;
3305 size_t status_path_len;
3306 struct got_repository *repo;
3307 got_worktree_status_cb status_cb;
3308 void *status_arg;
3309 got_cancel_cb cancel_cb;
3310 void *cancel_arg;
3311 /* A pathlist containing per-directory pathlists of ignore patterns. */
3312 struct got_pathlist_head *ignores;
3313 int report_unchanged;
3314 int no_ignores;
3317 static const struct got_error *
3318 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3319 int dirfd, const char *de_name,
3320 got_worktree_status_cb status_cb, void *status_arg,
3321 struct got_repository *repo, int report_unchanged)
3323 const struct got_error *err = NULL;
3324 unsigned char status = GOT_STATUS_NO_CHANGE;
3325 unsigned char staged_status = get_staged_status(ie);
3326 struct stat sb;
3327 struct got_object_id blob_id, commit_id, staged_blob_id;
3328 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3329 struct got_object_id *staged_blob_idp = NULL;
3331 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3332 if (err)
3333 return err;
3335 if (status == GOT_STATUS_NO_CHANGE &&
3336 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3337 return NULL;
3339 if (got_fileindex_entry_has_blob(ie)) {
3340 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3341 blob_idp = &blob_id;
3343 if (got_fileindex_entry_has_commit(ie)) {
3344 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3345 commit_idp = &commit_id;
3347 if (staged_status == GOT_STATUS_ADD ||
3348 staged_status == GOT_STATUS_MODIFY) {
3349 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3350 SHA1_DIGEST_LENGTH);
3351 staged_blob_idp = &staged_blob_id;
3354 return (*status_cb)(status_arg, status, staged_status,
3355 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3358 static const struct got_error *
3359 status_old_new(void *arg, struct got_fileindex_entry *ie,
3360 struct dirent *de, const char *parent_path, int dirfd)
3362 const struct got_error *err = NULL;
3363 struct diff_dir_cb_arg *a = arg;
3364 char *abspath;
3366 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3367 return got_error(GOT_ERR_CANCELLED);
3369 if (got_path_cmp(parent_path, a->status_path,
3370 strlen(parent_path), a->status_path_len) != 0 &&
3371 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3372 return NULL;
3374 if (parent_path[0]) {
3375 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3376 parent_path, de->d_name) == -1)
3377 return got_error_from_errno("asprintf");
3378 } else {
3379 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3380 de->d_name) == -1)
3381 return got_error_from_errno("asprintf");
3384 err = report_file_status(ie, abspath, dirfd, de->d_name,
3385 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3386 free(abspath);
3387 return err;
3390 static const struct got_error *
3391 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3393 struct diff_dir_cb_arg *a = arg;
3394 struct got_object_id blob_id, commit_id;
3395 unsigned char status;
3397 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3398 return got_error(GOT_ERR_CANCELLED);
3400 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3401 return NULL;
3403 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3404 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3405 if (got_fileindex_entry_has_file_on_disk(ie))
3406 status = GOT_STATUS_MISSING;
3407 else
3408 status = GOT_STATUS_DELETE;
3409 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3410 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3413 static void
3414 free_ignorelist(struct got_pathlist_head *ignorelist)
3416 struct got_pathlist_entry *pe;
3418 TAILQ_FOREACH(pe, ignorelist, entry)
3419 free((char *)pe->path);
3420 got_pathlist_free(ignorelist);
3423 static void
3424 free_ignores(struct got_pathlist_head *ignores)
3426 struct got_pathlist_entry *pe;
3428 TAILQ_FOREACH(pe, ignores, entry) {
3429 struct got_pathlist_head *ignorelist = pe->data;
3430 free_ignorelist(ignorelist);
3431 free((char *)pe->path);
3433 got_pathlist_free(ignores);
3436 static const struct got_error *
3437 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3439 const struct got_error *err = NULL;
3440 struct got_pathlist_entry *pe = NULL;
3441 struct got_pathlist_head *ignorelist;
3442 char *line = NULL, *pattern, *dirpath = NULL;
3443 size_t linesize = 0;
3444 ssize_t linelen;
3446 ignorelist = calloc(1, sizeof(*ignorelist));
3447 if (ignorelist == NULL)
3448 return got_error_from_errno("calloc");
3449 TAILQ_INIT(ignorelist);
3451 while ((linelen = getline(&line, &linesize, f)) != -1) {
3452 if (linelen > 0 && line[linelen - 1] == '\n')
3453 line[linelen - 1] = '\0';
3455 /* Git's ignores may contain comments. */
3456 if (line[0] == '#')
3457 continue;
3459 /* Git's negated patterns are not (yet?) supported. */
3460 if (line[0] == '!')
3461 continue;
3463 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3464 line) == -1) {
3465 err = got_error_from_errno("asprintf");
3466 goto done;
3468 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3469 if (err)
3470 goto done;
3472 if (ferror(f)) {
3473 err = got_error_from_errno("getline");
3474 goto done;
3477 dirpath = strdup(path);
3478 if (dirpath == NULL) {
3479 err = got_error_from_errno("strdup");
3480 goto done;
3482 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3483 done:
3484 free(line);
3485 if (err || pe == NULL) {
3486 free(dirpath);
3487 free_ignorelist(ignorelist);
3489 return err;
3492 static int
3493 match_ignores(struct got_pathlist_head *ignores, const char *path)
3495 struct got_pathlist_entry *pe;
3497 /* Handle patterns which match in all directories. */
3498 TAILQ_FOREACH(pe, ignores, entry) {
3499 struct got_pathlist_head *ignorelist = pe->data;
3500 struct got_pathlist_entry *pi;
3502 TAILQ_FOREACH(pi, ignorelist, entry) {
3503 const char *p, *pattern = pi->path;
3505 if (strncmp(pattern, "**/", 3) != 0)
3506 continue;
3507 pattern += 3;
3508 p = path;
3509 while (*p) {
3510 if (fnmatch(pattern, p,
3511 FNM_PATHNAME | FNM_LEADING_DIR)) {
3512 /* Retry in next directory. */
3513 while (*p && *p != '/')
3514 p++;
3515 while (*p == '/')
3516 p++;
3517 continue;
3519 return 1;
3525 * The ignores pathlist contains ignore lists from children before
3526 * parents, so we can find the most specific ignorelist by walking
3527 * ignores backwards.
3529 pe = TAILQ_LAST(ignores, got_pathlist_head);
3530 while (pe) {
3531 if (got_path_is_child(path, pe->path, pe->path_len)) {
3532 struct got_pathlist_head *ignorelist = pe->data;
3533 struct got_pathlist_entry *pi;
3534 TAILQ_FOREACH(pi, ignorelist, entry) {
3535 const char *pattern = pi->path;
3536 int flags = FNM_LEADING_DIR;
3537 if (strstr(pattern, "/**/") == NULL)
3538 flags |= FNM_PATHNAME;
3539 if (fnmatch(pattern, path, flags))
3540 continue;
3541 return 1;
3544 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3547 return 0;
3550 static const struct got_error *
3551 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3552 const char *path, int dirfd, const char *ignores_filename)
3554 const struct got_error *err = NULL;
3555 char *ignorespath;
3556 int fd = -1;
3557 FILE *ignoresfile = NULL;
3559 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3560 path[0] ? "/" : "", ignores_filename) == -1)
3561 return got_error_from_errno("asprintf");
3563 if (dirfd != -1) {
3564 fd = openat(dirfd, ignores_filename,
3565 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3566 if (fd == -1) {
3567 if (errno != ENOENT && errno != EACCES)
3568 err = got_error_from_errno2("openat",
3569 ignorespath);
3570 } else {
3571 ignoresfile = fdopen(fd, "r");
3572 if (ignoresfile == NULL)
3573 err = got_error_from_errno2("fdopen",
3574 ignorespath);
3575 else {
3576 fd = -1;
3577 err = read_ignores(ignores, path, ignoresfile);
3580 } else {
3581 ignoresfile = fopen(ignorespath, "re");
3582 if (ignoresfile == NULL) {
3583 if (errno != ENOENT && errno != EACCES)
3584 err = got_error_from_errno2("fopen",
3585 ignorespath);
3586 } else
3587 err = read_ignores(ignores, path, ignoresfile);
3590 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3591 err = got_error_from_errno2("fclose", path);
3592 if (fd != -1 && close(fd) == -1 && err == NULL)
3593 err = got_error_from_errno2("close", path);
3594 free(ignorespath);
3595 return err;
3598 static const struct got_error *
3599 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3600 int dirfd)
3602 const struct got_error *err = NULL;
3603 struct diff_dir_cb_arg *a = arg;
3604 char *path = NULL;
3606 if (ignore != NULL)
3607 *ignore = 0;
3609 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3610 return got_error(GOT_ERR_CANCELLED);
3612 if (parent_path[0]) {
3613 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3614 return got_error_from_errno("asprintf");
3615 } else {
3616 path = de->d_name;
3619 if (de->d_type == DT_DIR) {
3620 if (!a->no_ignores && ignore != NULL &&
3621 match_ignores(a->ignores, path))
3622 *ignore = 1;
3623 } else if (!match_ignores(a->ignores, path) &&
3624 got_path_is_child(path, a->status_path, a->status_path_len))
3625 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3626 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3627 if (parent_path[0])
3628 free(path);
3629 return err;
3632 static const struct got_error *
3633 status_traverse(void *arg, const char *path, int dirfd)
3635 const struct got_error *err = NULL;
3636 struct diff_dir_cb_arg *a = arg;
3638 if (a->no_ignores)
3639 return NULL;
3641 err = add_ignores(a->ignores, a->worktree->root_path,
3642 path, dirfd, ".cvsignore");
3643 if (err)
3644 return err;
3646 err = add_ignores(a->ignores, a->worktree->root_path, path,
3647 dirfd, ".gitignore");
3649 return err;
3652 static const struct got_error *
3653 report_single_file_status(const char *path, const char *ondisk_path,
3654 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3655 void *status_arg, struct got_repository *repo, int report_unchanged,
3656 struct got_pathlist_head *ignores, int no_ignores)
3658 struct got_fileindex_entry *ie;
3659 struct stat sb;
3661 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3662 if (ie)
3663 return report_file_status(ie, ondisk_path, -1, NULL,
3664 status_cb, status_arg, repo, report_unchanged);
3666 if (lstat(ondisk_path, &sb) == -1) {
3667 if (errno != ENOENT)
3668 return got_error_from_errno2("lstat", ondisk_path);
3669 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3670 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3673 if (!no_ignores && match_ignores(ignores, path))
3674 return NULL;
3676 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3677 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3678 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3680 return NULL;
3683 static const struct got_error *
3684 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3685 const char *root_path, const char *path)
3687 const struct got_error *err;
3688 char *parent_path, *next_parent_path = NULL;
3690 err = add_ignores(ignores, root_path, "", -1,
3691 ".cvsignore");
3692 if (err)
3693 return err;
3695 err = add_ignores(ignores, root_path, "", -1,
3696 ".gitignore");
3697 if (err)
3698 return err;
3700 err = got_path_dirname(&parent_path, path);
3701 if (err) {
3702 if (err->code == GOT_ERR_BAD_PATH)
3703 return NULL; /* cannot traverse parent */
3704 return err;
3706 for (;;) {
3707 err = add_ignores(ignores, root_path, parent_path, -1,
3708 ".cvsignore");
3709 if (err)
3710 break;
3711 err = add_ignores(ignores, root_path, parent_path, -1,
3712 ".gitignore");
3713 if (err)
3714 break;
3715 err = got_path_dirname(&next_parent_path, parent_path);
3716 if (err) {
3717 if (err->code == GOT_ERR_BAD_PATH)
3718 err = NULL; /* traversed everything */
3719 break;
3721 if (got_path_is_root_dir(parent_path))
3722 break;
3723 free(parent_path);
3724 parent_path = next_parent_path;
3725 next_parent_path = NULL;
3728 free(parent_path);
3729 free(next_parent_path);
3730 return err;
3733 static const struct got_error *
3734 worktree_status(struct got_worktree *worktree, const char *path,
3735 struct got_fileindex *fileindex, struct got_repository *repo,
3736 got_worktree_status_cb status_cb, void *status_arg,
3737 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3738 int report_unchanged)
3740 const struct got_error *err = NULL;
3741 int fd = -1;
3742 struct got_fileindex_diff_dir_cb fdiff_cb;
3743 struct diff_dir_cb_arg arg;
3744 char *ondisk_path = NULL;
3745 struct got_pathlist_head ignores;
3746 struct got_fileindex_entry *ie;
3748 TAILQ_INIT(&ignores);
3750 if (asprintf(&ondisk_path, "%s%s%s",
3751 worktree->root_path, path[0] ? "/" : "", path) == -1)
3752 return got_error_from_errno("asprintf");
3754 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3755 if (ie) {
3756 err = report_single_file_status(path, ondisk_path,
3757 fileindex, status_cb, status_arg, repo,
3758 report_unchanged, &ignores, no_ignores);
3759 goto done;
3762 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3763 if (fd == -1) {
3764 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3765 !got_err_open_nofollow_on_symlink())
3766 err = got_error_from_errno2("open", ondisk_path);
3767 else {
3768 if (!no_ignores) {
3769 err = add_ignores_from_parent_paths(&ignores,
3770 worktree->root_path, ondisk_path);
3771 if (err)
3772 goto done;
3774 err = report_single_file_status(path, ondisk_path,
3775 fileindex, status_cb, status_arg, repo,
3776 report_unchanged, &ignores, no_ignores);
3778 } else {
3779 fdiff_cb.diff_old_new = status_old_new;
3780 fdiff_cb.diff_old = status_old;
3781 fdiff_cb.diff_new = status_new;
3782 fdiff_cb.diff_traverse = status_traverse;
3783 arg.fileindex = fileindex;
3784 arg.worktree = worktree;
3785 arg.status_path = path;
3786 arg.status_path_len = strlen(path);
3787 arg.repo = repo;
3788 arg.status_cb = status_cb;
3789 arg.status_arg = status_arg;
3790 arg.cancel_cb = cancel_cb;
3791 arg.cancel_arg = cancel_arg;
3792 arg.report_unchanged = report_unchanged;
3793 arg.no_ignores = no_ignores;
3794 if (!no_ignores) {
3795 err = add_ignores_from_parent_paths(&ignores,
3796 worktree->root_path, path);
3797 if (err)
3798 goto done;
3800 arg.ignores = &ignores;
3801 err = got_fileindex_diff_dir(fileindex, fd,
3802 worktree->root_path, path, repo, &fdiff_cb, &arg);
3804 done:
3805 free_ignores(&ignores);
3806 if (fd != -1 && close(fd) == -1 && err == NULL)
3807 err = got_error_from_errno("close");
3808 free(ondisk_path);
3809 return err;
3812 const struct got_error *
3813 got_worktree_status(struct got_worktree *worktree,
3814 struct got_pathlist_head *paths, struct got_repository *repo,
3815 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3816 got_cancel_cb cancel_cb, void *cancel_arg)
3818 const struct got_error *err = NULL;
3819 char *fileindex_path = NULL;
3820 struct got_fileindex *fileindex = NULL;
3821 struct got_pathlist_entry *pe;
3823 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3824 if (err)
3825 return err;
3827 TAILQ_FOREACH(pe, paths, entry) {
3828 err = worktree_status(worktree, pe->path, fileindex, repo,
3829 status_cb, status_arg, cancel_cb, cancel_arg,
3830 no_ignores, 0);
3831 if (err)
3832 break;
3834 free(fileindex_path);
3835 got_fileindex_free(fileindex);
3836 return err;
3839 const struct got_error *
3840 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3841 const char *arg)
3843 const struct got_error *err = NULL;
3844 char *resolved = NULL, *cwd = NULL, *path = NULL;
3845 size_t len;
3846 struct stat sb;
3847 char *abspath = NULL;
3848 char canonpath[PATH_MAX];
3850 *wt_path = NULL;
3852 cwd = getcwd(NULL, 0);
3853 if (cwd == NULL)
3854 return got_error_from_errno("getcwd");
3856 if (lstat(arg, &sb) == -1) {
3857 if (errno != ENOENT) {
3858 err = got_error_from_errno2("lstat", arg);
3859 goto done;
3861 sb.st_mode = 0;
3863 if (S_ISLNK(sb.st_mode)) {
3865 * We cannot use realpath(3) with symlinks since we want to
3866 * operate on the symlink itself.
3867 * But we can make the path absolute, assuming it is relative
3868 * to the current working directory, and then canonicalize it.
3870 if (!got_path_is_absolute(arg)) {
3871 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3872 err = got_error_from_errno("asprintf");
3873 goto done;
3877 err = got_canonpath(abspath ? abspath : arg, canonpath,
3878 sizeof(canonpath));
3879 if (err)
3880 goto done;
3881 resolved = strdup(canonpath);
3882 if (resolved == NULL) {
3883 err = got_error_from_errno("strdup");
3884 goto done;
3886 } else {
3887 resolved = realpath(arg, NULL);
3888 if (resolved == NULL) {
3889 if (errno != ENOENT) {
3890 err = got_error_from_errno2("realpath", arg);
3891 goto done;
3893 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3894 err = got_error_from_errno("asprintf");
3895 goto done;
3897 err = got_canonpath(abspath, canonpath,
3898 sizeof(canonpath));
3899 if (err)
3900 goto done;
3901 resolved = strdup(canonpath);
3902 if (resolved == NULL) {
3903 err = got_error_from_errno("strdup");
3904 goto done;
3909 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3910 strlen(got_worktree_get_root_path(worktree)))) {
3911 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3912 goto done;
3915 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3916 err = got_path_skip_common_ancestor(&path,
3917 got_worktree_get_root_path(worktree), resolved);
3918 if (err)
3919 goto done;
3920 } else {
3921 path = strdup("");
3922 if (path == NULL) {
3923 err = got_error_from_errno("strdup");
3924 goto done;
3928 /* XXX status walk can't deal with trailing slash! */
3929 len = strlen(path);
3930 while (len > 0 && path[len - 1] == '/') {
3931 path[len - 1] = '\0';
3932 len--;
3934 done:
3935 free(abspath);
3936 free(resolved);
3937 free(cwd);
3938 if (err == NULL)
3939 *wt_path = path;
3940 else
3941 free(path);
3942 return err;
3945 struct schedule_addition_args {
3946 struct got_worktree *worktree;
3947 struct got_fileindex *fileindex;
3948 got_worktree_checkout_cb progress_cb;
3949 void *progress_arg;
3950 struct got_repository *repo;
3953 static const struct got_error *
3954 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3955 const char *relpath, struct got_object_id *blob_id,
3956 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3957 int dirfd, const char *de_name)
3959 struct schedule_addition_args *a = arg;
3960 const struct got_error *err = NULL;
3961 struct got_fileindex_entry *ie;
3962 struct stat sb;
3963 char *ondisk_path;
3965 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3966 relpath) == -1)
3967 return got_error_from_errno("asprintf");
3969 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3970 if (ie) {
3971 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3972 de_name, a->repo);
3973 if (err)
3974 goto done;
3975 /* Re-adding an existing entry is a no-op. */
3976 if (status == GOT_STATUS_ADD)
3977 goto done;
3978 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3979 if (err)
3980 goto done;
3983 if (status != GOT_STATUS_UNVERSIONED) {
3984 if (status == GOT_STATUS_NONEXISTENT)
3985 err = got_error_set_errno(ENOENT, ondisk_path);
3986 else
3987 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3988 goto done;
3991 err = got_fileindex_entry_alloc(&ie, relpath);
3992 if (err)
3993 goto done;
3994 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3995 relpath, NULL, NULL, 1);
3996 if (err) {
3997 got_fileindex_entry_free(ie);
3998 goto done;
4000 err = got_fileindex_entry_add(a->fileindex, ie);
4001 if (err) {
4002 got_fileindex_entry_free(ie);
4003 goto done;
4005 done:
4006 free(ondisk_path);
4007 if (err)
4008 return err;
4009 if (status == GOT_STATUS_ADD)
4010 return NULL;
4011 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4014 const struct got_error *
4015 got_worktree_schedule_add(struct got_worktree *worktree,
4016 struct got_pathlist_head *paths,
4017 got_worktree_checkout_cb progress_cb, void *progress_arg,
4018 struct got_repository *repo, int no_ignores)
4020 struct got_fileindex *fileindex = NULL;
4021 char *fileindex_path = NULL;
4022 const struct got_error *err = NULL, *sync_err, *unlockerr;
4023 struct got_pathlist_entry *pe;
4024 struct schedule_addition_args saa;
4026 err = lock_worktree(worktree, LOCK_EX);
4027 if (err)
4028 return err;
4030 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4031 if (err)
4032 goto done;
4034 saa.worktree = worktree;
4035 saa.fileindex = fileindex;
4036 saa.progress_cb = progress_cb;
4037 saa.progress_arg = progress_arg;
4038 saa.repo = repo;
4040 TAILQ_FOREACH(pe, paths, entry) {
4041 err = worktree_status(worktree, pe->path, fileindex, repo,
4042 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4043 if (err)
4044 break;
4046 sync_err = sync_fileindex(fileindex, fileindex_path);
4047 if (sync_err && err == NULL)
4048 err = sync_err;
4049 done:
4050 free(fileindex_path);
4051 if (fileindex)
4052 got_fileindex_free(fileindex);
4053 unlockerr = lock_worktree(worktree, LOCK_SH);
4054 if (unlockerr && err == NULL)
4055 err = unlockerr;
4056 return err;
4059 struct schedule_deletion_args {
4060 struct got_worktree *worktree;
4061 struct got_fileindex *fileindex;
4062 got_worktree_delete_cb progress_cb;
4063 void *progress_arg;
4064 struct got_repository *repo;
4065 int delete_local_mods;
4066 int keep_on_disk;
4067 int ignore_missing_paths;
4068 const char *status_codes;
4071 static const struct got_error *
4072 schedule_for_deletion(void *arg, unsigned char status,
4073 unsigned char staged_status, const char *relpath,
4074 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4075 struct got_object_id *commit_id, int dirfd, const char *de_name)
4077 struct schedule_deletion_args *a = arg;
4078 const struct got_error *err = NULL;
4079 struct got_fileindex_entry *ie = NULL;
4080 struct stat sb;
4081 char *ondisk_path;
4083 if (status == GOT_STATUS_NONEXISTENT) {
4084 if (a->ignore_missing_paths)
4085 return NULL;
4086 return got_error_set_errno(ENOENT, relpath);
4089 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4090 if (ie == NULL)
4091 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4093 staged_status = get_staged_status(ie);
4094 if (staged_status != GOT_STATUS_NO_CHANGE) {
4095 if (staged_status == GOT_STATUS_DELETE)
4096 return NULL;
4097 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4100 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4101 relpath) == -1)
4102 return got_error_from_errno("asprintf");
4104 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4105 a->repo);
4106 if (err)
4107 goto done;
4109 if (a->status_codes) {
4110 size_t ncodes = strlen(a->status_codes);
4111 int i;
4112 for (i = 0; i < ncodes ; i++) {
4113 if (status == a->status_codes[i])
4114 break;
4116 if (i == ncodes) {
4117 /* Do not delete files in non-matching status. */
4118 free(ondisk_path);
4119 return NULL;
4121 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4122 a->status_codes[i] != GOT_STATUS_MISSING) {
4123 static char msg[64];
4124 snprintf(msg, sizeof(msg),
4125 "invalid status code '%c'", a->status_codes[i]);
4126 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4127 goto done;
4131 if (status != GOT_STATUS_NO_CHANGE) {
4132 if (status == GOT_STATUS_DELETE)
4133 goto done;
4134 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4135 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4136 goto done;
4138 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4139 err = got_error_set_errno(ENOENT, relpath);
4140 goto done;
4142 if (status != GOT_STATUS_MODIFY &&
4143 status != GOT_STATUS_MISSING) {
4144 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4145 goto done;
4149 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4150 size_t root_len;
4152 if (dirfd != -1) {
4153 if (unlinkat(dirfd, de_name, 0) == -1) {
4154 err = got_error_from_errno2("unlinkat",
4155 ondisk_path);
4156 goto done;
4158 } else if (unlink(ondisk_path) == -1) {
4159 err = got_error_from_errno2("unlink", ondisk_path);
4160 goto done;
4163 root_len = strlen(a->worktree->root_path);
4164 do {
4165 char *parent;
4166 err = got_path_dirname(&parent, ondisk_path);
4167 if (err)
4168 goto done;
4169 free(ondisk_path);
4170 ondisk_path = parent;
4171 if (rmdir(ondisk_path) == -1) {
4172 if (errno != ENOTEMPTY)
4173 err = got_error_from_errno2("rmdir",
4174 ondisk_path);
4175 break;
4177 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4178 strlen(ondisk_path), root_len) != 0);
4181 got_fileindex_entry_mark_deleted_from_disk(ie);
4182 done:
4183 free(ondisk_path);
4184 if (err)
4185 return err;
4186 if (status == GOT_STATUS_DELETE)
4187 return NULL;
4188 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4189 staged_status, relpath);
4192 const struct got_error *
4193 got_worktree_schedule_delete(struct got_worktree *worktree,
4194 struct got_pathlist_head *paths, int delete_local_mods,
4195 const char *status_codes,
4196 got_worktree_delete_cb progress_cb, void *progress_arg,
4197 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4199 struct got_fileindex *fileindex = NULL;
4200 char *fileindex_path = NULL;
4201 const struct got_error *err = NULL, *sync_err, *unlockerr;
4202 struct got_pathlist_entry *pe;
4203 struct schedule_deletion_args sda;
4205 err = lock_worktree(worktree, LOCK_EX);
4206 if (err)
4207 return err;
4209 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4210 if (err)
4211 goto done;
4213 sda.worktree = worktree;
4214 sda.fileindex = fileindex;
4215 sda.progress_cb = progress_cb;
4216 sda.progress_arg = progress_arg;
4217 sda.repo = repo;
4218 sda.delete_local_mods = delete_local_mods;
4219 sda.keep_on_disk = keep_on_disk;
4220 sda.ignore_missing_paths = ignore_missing_paths;
4221 sda.status_codes = status_codes;
4223 TAILQ_FOREACH(pe, paths, entry) {
4224 err = worktree_status(worktree, pe->path, fileindex, repo,
4225 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4226 if (err)
4227 break;
4229 sync_err = sync_fileindex(fileindex, fileindex_path);
4230 if (sync_err && err == NULL)
4231 err = sync_err;
4232 done:
4233 free(fileindex_path);
4234 if (fileindex)
4235 got_fileindex_free(fileindex);
4236 unlockerr = lock_worktree(worktree, LOCK_SH);
4237 if (unlockerr && err == NULL)
4238 err = unlockerr;
4239 return err;
4242 static const struct got_error *
4243 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4245 const struct got_error *err = NULL;
4246 char *line = NULL;
4247 size_t linesize = 0, n;
4248 ssize_t linelen;
4250 linelen = getline(&line, &linesize, infile);
4251 if (linelen == -1) {
4252 if (ferror(infile)) {
4253 err = got_error_from_errno("getline");
4254 goto done;
4256 return NULL;
4258 if (outfile) {
4259 n = fwrite(line, 1, linelen, outfile);
4260 if (n != linelen) {
4261 err = got_ferror(outfile, GOT_ERR_IO);
4262 goto done;
4265 if (rejectfile) {
4266 n = fwrite(line, 1, linelen, rejectfile);
4267 if (n != linelen)
4268 err = got_ferror(outfile, GOT_ERR_IO);
4270 done:
4271 free(line);
4272 return err;
4275 static const struct got_error *
4276 skip_one_line(FILE *f)
4278 char *line = NULL;
4279 size_t linesize = 0;
4280 ssize_t linelen;
4282 linelen = getline(&line, &linesize, f);
4283 if (linelen == -1) {
4284 if (ferror(f))
4285 return got_error_from_errno("getline");
4286 return NULL;
4288 free(line);
4289 return NULL;
4292 static const struct got_error *
4293 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4294 int start_old, int end_old, int start_new, int end_new,
4295 FILE *outfile, FILE *rejectfile)
4297 const struct got_error *err;
4299 /* Copy old file's lines leading up to patch. */
4300 while (!feof(f1) && *line_cur1 < start_old) {
4301 err = copy_one_line(f1, outfile, NULL);
4302 if (err)
4303 return err;
4304 (*line_cur1)++;
4306 /* Skip new file's lines leading up to patch. */
4307 while (!feof(f2) && *line_cur2 < start_new) {
4308 if (rejectfile)
4309 err = copy_one_line(f2, NULL, rejectfile);
4310 else
4311 err = skip_one_line(f2);
4312 if (err)
4313 return err;
4314 (*line_cur2)++;
4316 /* Copy patched lines. */
4317 while (!feof(f2) && *line_cur2 <= end_new) {
4318 err = copy_one_line(f2, outfile, NULL);
4319 if (err)
4320 return err;
4321 (*line_cur2)++;
4323 /* Skip over old file's replaced lines. */
4324 while (!feof(f1) && *line_cur1 <= end_old) {
4325 if (rejectfile)
4326 err = copy_one_line(f1, NULL, rejectfile);
4327 else
4328 err = skip_one_line(f1);
4329 if (err)
4330 return err;
4331 (*line_cur1)++;
4334 return NULL;
4337 static const struct got_error *
4338 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4339 FILE *outfile, FILE *rejectfile)
4341 const struct got_error *err;
4343 if (outfile) {
4344 /* Copy old file's lines until EOF. */
4345 while (!feof(f1)) {
4346 err = copy_one_line(f1, outfile, NULL);
4347 if (err)
4348 return err;
4349 (*line_cur1)++;
4352 if (rejectfile) {
4353 /* Copy new file's lines until EOF. */
4354 while (!feof(f2)) {
4355 err = copy_one_line(f2, NULL, rejectfile);
4356 if (err)
4357 return err;
4358 (*line_cur2)++;
4362 return NULL;
4365 static const struct got_error *
4366 apply_or_reject_change(int *choice, int *nchunks_used,
4367 struct diff_result *diff_result, int n,
4368 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4369 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4370 got_worktree_patch_cb patch_cb, void *patch_arg)
4372 const struct got_error *err = NULL;
4373 struct diff_chunk_context cc = {};
4374 int start_old, end_old, start_new, end_new;
4375 FILE *hunkfile;
4376 struct diff_output_unidiff_state *diff_state;
4377 struct diff_input_info diff_info;
4378 int rc;
4380 *choice = GOT_PATCH_CHOICE_NONE;
4382 /* Get changed line numbers without context lines for copy_change(). */
4383 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4384 start_old = cc.left.start;
4385 end_old = cc.left.end;
4386 start_new = cc.right.start;
4387 end_new = cc.right.end;
4389 /* Get the same change with context lines for display. */
4390 memset(&cc, 0, sizeof(cc));
4391 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4393 memset(&diff_info, 0, sizeof(diff_info));
4394 diff_info.left_path = relpath;
4395 diff_info.right_path = relpath;
4397 diff_state = diff_output_unidiff_state_alloc();
4398 if (diff_state == NULL)
4399 return got_error_set_errno(ENOMEM,
4400 "diff_output_unidiff_state_alloc");
4402 hunkfile = got_opentemp();
4403 if (hunkfile == NULL) {
4404 err = got_error_from_errno("got_opentemp");
4405 goto done;
4408 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4409 diff_result, &cc);
4410 if (rc != DIFF_RC_OK) {
4411 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4412 goto done;
4415 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4416 err = got_ferror(hunkfile, GOT_ERR_IO);
4417 goto done;
4420 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4421 hunkfile, changeno, nchanges);
4422 if (err)
4423 goto done;
4425 switch (*choice) {
4426 case GOT_PATCH_CHOICE_YES:
4427 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4428 end_old, start_new, end_new, outfile, rejectfile);
4429 break;
4430 case GOT_PATCH_CHOICE_NO:
4431 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4432 end_old, start_new, end_new, rejectfile, outfile);
4433 break;
4434 case GOT_PATCH_CHOICE_QUIT:
4435 break;
4436 default:
4437 err = got_error(GOT_ERR_PATCH_CHOICE);
4438 break;
4440 done:
4441 diff_output_unidiff_state_free(diff_state);
4442 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4443 err = got_error_from_errno("fclose");
4444 return err;
4447 struct revert_file_args {
4448 struct got_worktree *worktree;
4449 struct got_fileindex *fileindex;
4450 got_worktree_checkout_cb progress_cb;
4451 void *progress_arg;
4452 got_worktree_patch_cb patch_cb;
4453 void *patch_arg;
4454 struct got_repository *repo;
4455 int unlink_added_files;
4458 static const struct got_error *
4459 create_patched_content(char **path_outfile, int reverse_patch,
4460 struct got_object_id *blob_id, const char *path2,
4461 int dirfd2, const char *de_name2,
4462 const char *relpath, struct got_repository *repo,
4463 got_worktree_patch_cb patch_cb, void *patch_arg)
4465 const struct got_error *err, *free_err;
4466 struct got_blob_object *blob = NULL;
4467 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4468 int fd = -1, fd2 = -1;
4469 char link_target[PATH_MAX];
4470 ssize_t link_len = 0;
4471 char *path1 = NULL, *id_str = NULL;
4472 struct stat sb2;
4473 struct got_diffreg_result *diffreg_result = NULL;
4474 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4475 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4477 *path_outfile = NULL;
4479 err = got_object_id_str(&id_str, blob_id);
4480 if (err)
4481 return err;
4483 if (dirfd2 != -1) {
4484 fd2 = openat(dirfd2, de_name2,
4485 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4486 if (fd2 == -1) {
4487 if (!got_err_open_nofollow_on_symlink()) {
4488 err = got_error_from_errno2("openat", path2);
4489 goto done;
4491 link_len = readlinkat(dirfd2, de_name2,
4492 link_target, sizeof(link_target));
4493 if (link_len == -1) {
4494 return got_error_from_errno2("readlinkat",
4495 path2);
4497 sb2.st_mode = S_IFLNK;
4498 sb2.st_size = link_len;
4500 } else {
4501 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4502 if (fd2 == -1) {
4503 if (!got_err_open_nofollow_on_symlink()) {
4504 err = got_error_from_errno2("open", path2);
4505 goto done;
4507 link_len = readlink(path2, link_target,
4508 sizeof(link_target));
4509 if (link_len == -1)
4510 return got_error_from_errno2("readlink", path2);
4511 sb2.st_mode = S_IFLNK;
4512 sb2.st_size = link_len;
4515 if (fd2 != -1) {
4516 if (fstat(fd2, &sb2) == -1) {
4517 err = got_error_from_errno2("fstat", path2);
4518 goto done;
4521 f2 = fdopen(fd2, "r");
4522 if (f2 == NULL) {
4523 err = got_error_from_errno2("fdopen", path2);
4524 goto done;
4526 fd2 = -1;
4527 } else {
4528 size_t n;
4529 f2 = got_opentemp();
4530 if (f2 == NULL) {
4531 err = got_error_from_errno2("got_opentemp", path2);
4532 goto done;
4534 n = fwrite(link_target, 1, link_len, f2);
4535 if (n != link_len) {
4536 err = got_ferror(f2, GOT_ERR_IO);
4537 goto done;
4539 if (fflush(f2) == EOF) {
4540 err = got_error_from_errno("fflush");
4541 goto done;
4543 rewind(f2);
4546 fd = got_opentempfd();
4547 if (fd == -1) {
4548 err = got_error_from_errno("got_opentempfd");
4549 goto done;
4552 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4553 if (err)
4554 goto done;
4556 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4557 if (err)
4558 goto done;
4560 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4561 if (err)
4562 goto done;
4564 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4565 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4566 if (err)
4567 goto done;
4569 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4570 "");
4571 if (err)
4572 goto done;
4574 if (fseek(f1, 0L, SEEK_SET) == -1)
4575 return got_ferror(f1, GOT_ERR_IO);
4576 if (fseek(f2, 0L, SEEK_SET) == -1)
4577 return got_ferror(f2, GOT_ERR_IO);
4579 /* Count the number of actual changes in the diff result. */
4580 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4581 struct diff_chunk_context cc = {};
4582 diff_chunk_context_load_change(&cc, &nchunks_used,
4583 diffreg_result->result, n, 0);
4584 nchanges++;
4586 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4587 int choice;
4588 err = apply_or_reject_change(&choice, &nchunks_used,
4589 diffreg_result->result, n, relpath, f1, f2,
4590 &line_cur1, &line_cur2,
4591 reverse_patch ? NULL : outfile,
4592 reverse_patch ? outfile : NULL,
4593 ++i, nchanges, patch_cb, patch_arg);
4594 if (err)
4595 goto done;
4596 if (choice == GOT_PATCH_CHOICE_YES)
4597 have_content = 1;
4598 else if (choice == GOT_PATCH_CHOICE_QUIT)
4599 break;
4601 if (have_content) {
4602 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4603 reverse_patch ? NULL : outfile,
4604 reverse_patch ? outfile : NULL);
4605 if (err)
4606 goto done;
4608 if (!S_ISLNK(sb2.st_mode)) {
4609 mode_t mode;
4611 mode = apply_umask(sb2.st_mode);
4612 if (fchmod(fileno(outfile), mode) == -1) {
4613 err = got_error_from_errno2("fchmod", path2);
4614 goto done;
4618 done:
4619 free(id_str);
4620 if (fd != -1 && close(fd) == -1 && err == NULL)
4621 err = got_error_from_errno("close");
4622 if (blob)
4623 got_object_blob_close(blob);
4624 free_err = got_diffreg_result_free(diffreg_result);
4625 if (err == NULL)
4626 err = free_err;
4627 if (f1 && fclose(f1) == EOF && err == NULL)
4628 err = got_error_from_errno2("fclose", path1);
4629 if (f2 && fclose(f2) == EOF && err == NULL)
4630 err = got_error_from_errno2("fclose", path2);
4631 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4632 err = got_error_from_errno2("close", path2);
4633 if (outfile && fclose(outfile) == EOF && err == NULL)
4634 err = got_error_from_errno2("fclose", *path_outfile);
4635 if (path1 && unlink(path1) == -1 && err == NULL)
4636 err = got_error_from_errno2("unlink", path1);
4637 if (err || !have_content) {
4638 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4639 err = got_error_from_errno2("unlink", *path_outfile);
4640 free(*path_outfile);
4641 *path_outfile = NULL;
4643 free(path1);
4644 return err;
4647 static const struct got_error *
4648 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4649 const char *relpath, struct got_object_id *blob_id,
4650 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4651 int dirfd, const char *de_name)
4653 struct revert_file_args *a = arg;
4654 const struct got_error *err = NULL;
4655 char *parent_path = NULL;
4656 struct got_fileindex_entry *ie;
4657 struct got_commit_object *base_commit = NULL;
4658 struct got_tree_object *tree = NULL;
4659 struct got_object_id *tree_id = NULL;
4660 const struct got_tree_entry *te = NULL;
4661 char *tree_path = NULL, *te_name;
4662 char *ondisk_path = NULL, *path_content = NULL;
4663 struct got_blob_object *blob = NULL;
4664 int fd = -1;
4666 /* Reverting a staged deletion is a no-op. */
4667 if (status == GOT_STATUS_DELETE &&
4668 staged_status != GOT_STATUS_NO_CHANGE)
4669 return NULL;
4671 if (status == GOT_STATUS_UNVERSIONED)
4672 return (*a->progress_cb)(a->progress_arg,
4673 GOT_STATUS_UNVERSIONED, relpath);
4675 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4676 if (ie == NULL)
4677 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4679 /* Construct in-repository path of tree which contains this blob. */
4680 err = got_path_dirname(&parent_path, ie->path);
4681 if (err) {
4682 if (err->code != GOT_ERR_BAD_PATH)
4683 goto done;
4684 parent_path = strdup("/");
4685 if (parent_path == NULL) {
4686 err = got_error_from_errno("strdup");
4687 goto done;
4690 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4691 tree_path = strdup(parent_path);
4692 if (tree_path == NULL) {
4693 err = got_error_from_errno("strdup");
4694 goto done;
4696 } else {
4697 if (got_path_is_root_dir(parent_path)) {
4698 tree_path = strdup(a->worktree->path_prefix);
4699 if (tree_path == NULL) {
4700 err = got_error_from_errno("strdup");
4701 goto done;
4703 } else {
4704 if (asprintf(&tree_path, "%s/%s",
4705 a->worktree->path_prefix, parent_path) == -1) {
4706 err = got_error_from_errno("asprintf");
4707 goto done;
4712 err = got_object_open_as_commit(&base_commit, a->repo,
4713 a->worktree->base_commit_id);
4714 if (err)
4715 goto done;
4717 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4718 if (err) {
4719 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4720 (status == GOT_STATUS_ADD ||
4721 staged_status == GOT_STATUS_ADD)))
4722 goto done;
4723 } else {
4724 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4725 if (err)
4726 goto done;
4728 err = got_path_basename(&te_name, ie->path);
4729 if (err)
4730 goto done;
4732 te = got_object_tree_find_entry(tree, te_name);
4733 free(te_name);
4734 if (te == NULL && status != GOT_STATUS_ADD &&
4735 staged_status != GOT_STATUS_ADD) {
4736 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4737 goto done;
4741 switch (status) {
4742 case GOT_STATUS_ADD:
4743 if (a->patch_cb) {
4744 int choice = GOT_PATCH_CHOICE_NONE;
4745 err = (*a->patch_cb)(&choice, a->patch_arg,
4746 status, ie->path, NULL, 1, 1);
4747 if (err)
4748 goto done;
4749 if (choice != GOT_PATCH_CHOICE_YES)
4750 break;
4752 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4753 ie->path);
4754 if (err)
4755 goto done;
4756 got_fileindex_entry_remove(a->fileindex, ie);
4757 if (a->unlink_added_files) {
4758 if (asprintf(&ondisk_path, "%s/%s",
4759 got_worktree_get_root_path(a->worktree),
4760 relpath) == -1) {
4761 err = got_error_from_errno("asprintf");
4762 goto done;
4764 if (unlink(ondisk_path) == -1) {
4765 err = got_error_from_errno2("unlink",
4766 ondisk_path);
4767 break;
4770 break;
4771 case GOT_STATUS_DELETE:
4772 if (a->patch_cb) {
4773 int choice = GOT_PATCH_CHOICE_NONE;
4774 err = (*a->patch_cb)(&choice, a->patch_arg,
4775 status, ie->path, NULL, 1, 1);
4776 if (err)
4777 goto done;
4778 if (choice != GOT_PATCH_CHOICE_YES)
4779 break;
4781 /* fall through */
4782 case GOT_STATUS_MODIFY:
4783 case GOT_STATUS_MODE_CHANGE:
4784 case GOT_STATUS_CONFLICT:
4785 case GOT_STATUS_MISSING: {
4786 struct got_object_id id;
4787 if (staged_status == GOT_STATUS_ADD ||
4788 staged_status == GOT_STATUS_MODIFY) {
4789 memcpy(id.sha1, ie->staged_blob_sha1,
4790 SHA1_DIGEST_LENGTH);
4791 } else
4792 memcpy(id.sha1, ie->blob_sha1,
4793 SHA1_DIGEST_LENGTH);
4794 fd = got_opentempfd();
4795 if (fd == -1) {
4796 err = got_error_from_errno("got_opentempfd");
4797 goto done;
4800 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4801 if (err)
4802 goto done;
4804 if (asprintf(&ondisk_path, "%s/%s",
4805 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4806 err = got_error_from_errno("asprintf");
4807 goto done;
4810 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4811 status == GOT_STATUS_CONFLICT)) {
4812 int is_bad_symlink = 0;
4813 err = create_patched_content(&path_content, 1, &id,
4814 ondisk_path, dirfd, de_name, ie->path, a->repo,
4815 a->patch_cb, a->patch_arg);
4816 if (err || path_content == NULL)
4817 break;
4818 if (te && S_ISLNK(te->mode)) {
4819 if (unlink(path_content) == -1) {
4820 err = got_error_from_errno2("unlink",
4821 path_content);
4822 break;
4824 err = install_symlink(&is_bad_symlink,
4825 a->worktree, ondisk_path, ie->path,
4826 blob, 0, 1, 0, 0, a->repo,
4827 a->progress_cb, a->progress_arg);
4828 } else {
4829 if (rename(path_content, ondisk_path) == -1) {
4830 err = got_error_from_errno3("rename",
4831 path_content, ondisk_path);
4832 goto done;
4835 } else {
4836 int is_bad_symlink = 0;
4837 if (te && S_ISLNK(te->mode)) {
4838 err = install_symlink(&is_bad_symlink,
4839 a->worktree, ondisk_path, ie->path,
4840 blob, 0, 1, 0, 0, a->repo,
4841 a->progress_cb, a->progress_arg);
4842 } else {
4843 err = install_blob(a->worktree, ondisk_path,
4844 ie->path,
4845 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4846 got_fileindex_perms_to_st(ie), blob,
4847 0, 1, 0, 0, a->repo,
4848 a->progress_cb, a->progress_arg);
4850 if (err)
4851 goto done;
4852 if (status == GOT_STATUS_DELETE ||
4853 status == GOT_STATUS_MODE_CHANGE) {
4854 err = got_fileindex_entry_update(ie,
4855 a->worktree->root_fd, relpath,
4856 blob->id.sha1,
4857 a->worktree->base_commit_id->sha1, 1);
4858 if (err)
4859 goto done;
4861 if (is_bad_symlink) {
4862 got_fileindex_entry_filetype_set(ie,
4863 GOT_FILEIDX_MODE_BAD_SYMLINK);
4866 break;
4868 default:
4869 break;
4871 done:
4872 free(ondisk_path);
4873 free(path_content);
4874 free(parent_path);
4875 free(tree_path);
4876 if (fd != -1 && close(fd) == -1 && err == NULL)
4877 err = got_error_from_errno("close");
4878 if (blob)
4879 got_object_blob_close(blob);
4880 if (tree)
4881 got_object_tree_close(tree);
4882 free(tree_id);
4883 if (base_commit)
4884 got_object_commit_close(base_commit);
4885 return err;
4888 const struct got_error *
4889 got_worktree_revert(struct got_worktree *worktree,
4890 struct got_pathlist_head *paths,
4891 got_worktree_checkout_cb progress_cb, void *progress_arg,
4892 got_worktree_patch_cb patch_cb, void *patch_arg,
4893 struct got_repository *repo)
4895 struct got_fileindex *fileindex = NULL;
4896 char *fileindex_path = NULL;
4897 const struct got_error *err = NULL, *unlockerr = NULL;
4898 const struct got_error *sync_err = NULL;
4899 struct got_pathlist_entry *pe;
4900 struct revert_file_args rfa;
4902 err = lock_worktree(worktree, LOCK_EX);
4903 if (err)
4904 return err;
4906 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4907 if (err)
4908 goto done;
4910 rfa.worktree = worktree;
4911 rfa.fileindex = fileindex;
4912 rfa.progress_cb = progress_cb;
4913 rfa.progress_arg = progress_arg;
4914 rfa.patch_cb = patch_cb;
4915 rfa.patch_arg = patch_arg;
4916 rfa.repo = repo;
4917 rfa.unlink_added_files = 0;
4918 TAILQ_FOREACH(pe, paths, entry) {
4919 err = worktree_status(worktree, pe->path, fileindex, repo,
4920 revert_file, &rfa, NULL, NULL, 1, 0);
4921 if (err)
4922 break;
4924 sync_err = sync_fileindex(fileindex, fileindex_path);
4925 if (sync_err && err == NULL)
4926 err = sync_err;
4927 done:
4928 free(fileindex_path);
4929 if (fileindex)
4930 got_fileindex_free(fileindex);
4931 unlockerr = lock_worktree(worktree, LOCK_SH);
4932 if (unlockerr && err == NULL)
4933 err = unlockerr;
4934 return err;
4937 static void
4938 free_commitable(struct got_commitable *ct)
4940 free(ct->path);
4941 free(ct->in_repo_path);
4942 free(ct->ondisk_path);
4943 free(ct->blob_id);
4944 free(ct->base_blob_id);
4945 free(ct->staged_blob_id);
4946 free(ct->base_commit_id);
4947 free(ct);
4950 struct collect_commitables_arg {
4951 struct got_pathlist_head *commitable_paths;
4952 struct got_repository *repo;
4953 struct got_worktree *worktree;
4954 struct got_fileindex *fileindex;
4955 int have_staged_files;
4956 int allow_bad_symlinks;
4957 int diff_header_shown;
4958 FILE *diff_outfile;
4959 FILE *f1;
4960 FILE *f2;
4964 * Create a file which contains the target path of a symlink so we can feed
4965 * it as content to the diff engine.
4967 static const struct got_error *
4968 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4969 const char *abspath)
4971 const struct got_error *err = NULL;
4972 char target_path[PATH_MAX];
4973 ssize_t target_len, outlen;
4975 *fd = -1;
4977 if (dirfd != -1) {
4978 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4979 if (target_len == -1)
4980 return got_error_from_errno2("readlinkat", abspath);
4981 } else {
4982 target_len = readlink(abspath, target_path, PATH_MAX);
4983 if (target_len == -1)
4984 return got_error_from_errno2("readlink", abspath);
4987 *fd = got_opentempfd();
4988 if (*fd == -1)
4989 return got_error_from_errno("got_opentempfd");
4991 outlen = write(*fd, target_path, target_len);
4992 if (outlen == -1) {
4993 err = got_error_from_errno("got_opentempfd");
4994 goto done;
4997 if (lseek(*fd, 0, SEEK_SET) == -1) {
4998 err = got_error_from_errno2("lseek", abspath);
4999 goto done;
5001 done:
5002 if (err) {
5003 close(*fd);
5004 *fd = -1;
5006 return err;
5009 static const struct got_error *
5010 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5011 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5012 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5014 const struct got_error *err = NULL;
5015 struct got_blob_object *blob1 = NULL;
5016 int fd = -1, fd1 = -1, fd2 = -1;
5017 FILE *ondisk_file = NULL;
5018 char *label1 = NULL;
5019 struct stat sb;
5020 off_t size1 = 0;
5021 int f2_exists = 0;
5022 char *id_str = NULL;
5024 memset(&sb, 0, sizeof(sb));
5026 if (diff_staged) {
5027 if (ct->staged_status != GOT_STATUS_MODIFY &&
5028 ct->staged_status != GOT_STATUS_ADD &&
5029 ct->staged_status != GOT_STATUS_DELETE)
5030 return NULL;
5031 } else {
5032 if (ct->status != GOT_STATUS_MODIFY &&
5033 ct->status != GOT_STATUS_ADD &&
5034 ct->status != GOT_STATUS_DELETE)
5035 return NULL;
5038 err = got_opentemp_truncate(f1);
5039 if (err)
5040 return got_error_from_errno("got_opentemp_truncate");
5041 err = got_opentemp_truncate(f2);
5042 if (err)
5043 return got_error_from_errno("got_opentemp_truncate");
5045 if (!*diff_header_shown) {
5046 err = got_object_id_str(&id_str, worktree->base_commit_id);
5047 if (err)
5048 return err;
5049 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5050 got_worktree_get_root_path(worktree));
5051 fprintf(diff_outfile, "commit - %s\n", id_str);
5052 fprintf(diff_outfile, "path + %s%s\n",
5053 got_worktree_get_root_path(worktree),
5054 diff_staged ? " (staged changes)" : "");
5055 *diff_header_shown = 1;
5058 if (diff_staged) {
5059 const char *label1 = NULL, *label2 = NULL;
5060 switch (ct->staged_status) {
5061 case GOT_STATUS_MODIFY:
5062 label1 = ct->path;
5063 label2 = ct->path;
5064 break;
5065 case GOT_STATUS_ADD:
5066 label2 = ct->path;
5067 break;
5068 case GOT_STATUS_DELETE:
5069 label1 = ct->path;
5070 break;
5071 default:
5072 return got_error(GOT_ERR_FILE_STATUS);
5074 fd1 = got_opentempfd();
5075 if (fd1 == -1) {
5076 err = got_error_from_errno("got_opentempfd");
5077 goto done;
5079 fd2 = got_opentempfd();
5080 if (fd2 == -1) {
5081 err = got_error_from_errno("got_opentempfd");
5082 goto done;
5084 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5085 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5086 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5087 repo, diff_outfile);
5088 goto done;
5091 fd1 = got_opentempfd();
5092 if (fd1 == -1) {
5093 err = got_error_from_errno("got_opentempfd");
5094 goto done;
5097 if (ct->status != GOT_STATUS_ADD) {
5098 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5099 8192, fd1);
5100 if (err)
5101 goto done;
5104 if (ct->status != GOT_STATUS_DELETE) {
5105 if (dirfd != -1) {
5106 fd = openat(dirfd, de_name,
5107 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5108 if (fd == -1) {
5109 if (!got_err_open_nofollow_on_symlink()) {
5110 err = got_error_from_errno2("openat",
5111 ct->ondisk_path);
5112 goto done;
5114 err = get_symlink_target_file(&fd, dirfd,
5115 de_name, ct->ondisk_path);
5116 if (err)
5117 goto done;
5119 } else {
5120 fd = open(ct->ondisk_path,
5121 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5122 if (fd == -1) {
5123 if (!got_err_open_nofollow_on_symlink()) {
5124 err = got_error_from_errno2("open",
5125 ct->ondisk_path);
5126 goto done;
5128 err = get_symlink_target_file(&fd, dirfd,
5129 de_name, ct->ondisk_path);
5130 if (err)
5131 goto done;
5134 if (fstatat(fd, ct->ondisk_path, &sb,
5135 AT_SYMLINK_NOFOLLOW) == -1) {
5136 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5137 goto done;
5139 ondisk_file = fdopen(fd, "r");
5140 if (ondisk_file == NULL) {
5141 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5142 goto done;
5144 fd = -1;
5145 f2_exists = 1;
5148 if (blob1) {
5149 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5150 f1, blob1);
5151 if (err)
5152 goto done;
5155 err = got_diff_blob_file(blob1, f1, size1, label1,
5156 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5157 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, diff_outfile);
5158 done:
5159 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5160 err = got_error_from_errno("close");
5161 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5162 err = got_error_from_errno("close");
5163 if (blob1)
5164 got_object_blob_close(blob1);
5165 if (fd != -1 && close(fd) == -1 && err == NULL)
5166 err = got_error_from_errno("close");
5167 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5168 err = got_error_from_errno("fclose");
5169 return err;
5172 static const struct got_error *
5173 collect_commitables(void *arg, unsigned char status,
5174 unsigned char staged_status, const char *relpath,
5175 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5176 struct got_object_id *commit_id, int dirfd, const char *de_name)
5178 struct collect_commitables_arg *a = arg;
5179 const struct got_error *err = NULL;
5180 struct got_commitable *ct = NULL;
5181 struct got_pathlist_entry *new = NULL;
5182 char *parent_path = NULL, *path = NULL;
5183 struct stat sb;
5185 if (a->have_staged_files) {
5186 if (staged_status != GOT_STATUS_MODIFY &&
5187 staged_status != GOT_STATUS_ADD &&
5188 staged_status != GOT_STATUS_DELETE)
5189 return NULL;
5190 } else {
5191 if (status == GOT_STATUS_CONFLICT)
5192 return got_error(GOT_ERR_COMMIT_CONFLICT);
5194 if (status != GOT_STATUS_MODIFY &&
5195 status != GOT_STATUS_MODE_CHANGE &&
5196 status != GOT_STATUS_ADD &&
5197 status != GOT_STATUS_DELETE)
5198 return NULL;
5201 if (asprintf(&path, "/%s", relpath) == -1) {
5202 err = got_error_from_errno("asprintf");
5203 goto done;
5205 if (strcmp(path, "/") == 0) {
5206 parent_path = strdup("");
5207 if (parent_path == NULL)
5208 return got_error_from_errno("strdup");
5209 } else {
5210 err = got_path_dirname(&parent_path, path);
5211 if (err)
5212 return err;
5215 ct = calloc(1, sizeof(*ct));
5216 if (ct == NULL) {
5217 err = got_error_from_errno("calloc");
5218 goto done;
5221 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5222 relpath) == -1) {
5223 err = got_error_from_errno("asprintf");
5224 goto done;
5227 if (staged_status == GOT_STATUS_ADD ||
5228 staged_status == GOT_STATUS_MODIFY) {
5229 struct got_fileindex_entry *ie;
5230 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5231 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5232 case GOT_FILEIDX_MODE_REGULAR_FILE:
5233 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5234 ct->mode = S_IFREG;
5235 break;
5236 case GOT_FILEIDX_MODE_SYMLINK:
5237 ct->mode = S_IFLNK;
5238 break;
5239 default:
5240 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5241 goto done;
5243 ct->mode |= got_fileindex_entry_perms_get(ie);
5244 } else if (status != GOT_STATUS_DELETE &&
5245 staged_status != GOT_STATUS_DELETE) {
5246 if (dirfd != -1) {
5247 if (fstatat(dirfd, de_name, &sb,
5248 AT_SYMLINK_NOFOLLOW) == -1) {
5249 err = got_error_from_errno2("fstatat",
5250 ct->ondisk_path);
5251 goto done;
5253 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5254 err = got_error_from_errno2("lstat", ct->ondisk_path);
5255 goto done;
5257 ct->mode = sb.st_mode;
5260 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5261 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5262 relpath) == -1) {
5263 err = got_error_from_errno("asprintf");
5264 goto done;
5267 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5268 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5269 int is_bad_symlink;
5270 char target_path[PATH_MAX];
5271 ssize_t target_len;
5272 target_len = readlink(ct->ondisk_path, target_path,
5273 sizeof(target_path));
5274 if (target_len == -1) {
5275 err = got_error_from_errno2("readlink",
5276 ct->ondisk_path);
5277 goto done;
5279 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5280 target_len, ct->ondisk_path, a->worktree->root_path);
5281 if (err)
5282 goto done;
5283 if (is_bad_symlink) {
5284 err = got_error_path(ct->ondisk_path,
5285 GOT_ERR_BAD_SYMLINK);
5286 goto done;
5291 ct->status = status;
5292 ct->staged_status = staged_status;
5293 ct->blob_id = NULL; /* will be filled in when blob gets created */
5294 if (ct->status != GOT_STATUS_ADD &&
5295 ct->staged_status != GOT_STATUS_ADD) {
5296 ct->base_blob_id = got_object_id_dup(blob_id);
5297 if (ct->base_blob_id == NULL) {
5298 err = got_error_from_errno("got_object_id_dup");
5299 goto done;
5301 ct->base_commit_id = got_object_id_dup(commit_id);
5302 if (ct->base_commit_id == NULL) {
5303 err = got_error_from_errno("got_object_id_dup");
5304 goto done;
5307 if (ct->staged_status == GOT_STATUS_ADD ||
5308 ct->staged_status == GOT_STATUS_MODIFY) {
5309 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5310 if (ct->staged_blob_id == NULL) {
5311 err = got_error_from_errno("got_object_id_dup");
5312 goto done;
5315 ct->path = strdup(path);
5316 if (ct->path == NULL) {
5317 err = got_error_from_errno("strdup");
5318 goto done;
5320 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5321 if (err)
5322 goto done;
5324 if (a->diff_outfile && ct && new != NULL) {
5325 err = append_ct_diff(ct, &a->diff_header_shown,
5326 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5327 a->have_staged_files, a->repo, a->worktree);
5328 if (err)
5329 goto done;
5331 done:
5332 if (ct && (err || new == NULL))
5333 free_commitable(ct);
5334 free(parent_path);
5335 free(path);
5336 return err;
5339 static const struct got_error *write_tree(struct got_object_id **, int *,
5340 struct got_tree_object *, const char *, struct got_pathlist_head *,
5341 got_worktree_status_cb status_cb, void *status_arg,
5342 struct got_repository *);
5344 static const struct got_error *
5345 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5346 struct got_tree_entry *te, const char *parent_path,
5347 struct got_pathlist_head *commitable_paths,
5348 got_worktree_status_cb status_cb, void *status_arg,
5349 struct got_repository *repo)
5351 const struct got_error *err = NULL;
5352 struct got_tree_object *subtree;
5353 char *subpath;
5355 if (asprintf(&subpath, "%s%s%s", parent_path,
5356 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5357 return got_error_from_errno("asprintf");
5359 err = got_object_open_as_tree(&subtree, repo, &te->id);
5360 if (err)
5361 return err;
5363 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5364 commitable_paths, status_cb, status_arg, repo);
5365 got_object_tree_close(subtree);
5366 free(subpath);
5367 return err;
5370 static const struct got_error *
5371 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5373 const struct got_error *err = NULL;
5374 char *ct_parent_path = NULL;
5376 *match = 0;
5378 if (strchr(ct->in_repo_path, '/') == NULL) {
5379 *match = got_path_is_root_dir(path);
5380 return NULL;
5383 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5384 if (err)
5385 return err;
5386 *match = (strcmp(path, ct_parent_path) == 0);
5387 free(ct_parent_path);
5388 return err;
5391 static mode_t
5392 get_ct_file_mode(struct got_commitable *ct)
5394 if (S_ISLNK(ct->mode))
5395 return S_IFLNK;
5397 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5400 static const struct got_error *
5401 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5402 struct got_tree_entry *te, struct got_commitable *ct)
5404 const struct got_error *err = NULL;
5406 *new_te = NULL;
5408 err = got_object_tree_entry_dup(new_te, te);
5409 if (err)
5410 goto done;
5412 (*new_te)->mode = get_ct_file_mode(ct);
5414 if (ct->staged_status == GOT_STATUS_MODIFY)
5415 memcpy(&(*new_te)->id, ct->staged_blob_id,
5416 sizeof((*new_te)->id));
5417 else
5418 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5419 done:
5420 if (err && *new_te) {
5421 free(*new_te);
5422 *new_te = NULL;
5424 return err;
5427 static const struct got_error *
5428 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5429 struct got_commitable *ct)
5431 const struct got_error *err = NULL;
5432 char *ct_name = NULL;
5434 *new_te = NULL;
5436 *new_te = calloc(1, sizeof(**new_te));
5437 if (*new_te == NULL)
5438 return got_error_from_errno("calloc");
5440 err = got_path_basename(&ct_name, ct->path);
5441 if (err)
5442 goto done;
5443 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5444 sizeof((*new_te)->name)) {
5445 err = got_error(GOT_ERR_NO_SPACE);
5446 goto done;
5449 (*new_te)->mode = get_ct_file_mode(ct);
5451 if (ct->staged_status == GOT_STATUS_ADD)
5452 memcpy(&(*new_te)->id, ct->staged_blob_id,
5453 sizeof((*new_te)->id));
5454 else
5455 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5456 done:
5457 free(ct_name);
5458 if (err && *new_te) {
5459 free(*new_te);
5460 *new_te = NULL;
5462 return err;
5465 static const struct got_error *
5466 insert_tree_entry(struct got_tree_entry *new_te,
5467 struct got_pathlist_head *paths)
5469 const struct got_error *err = NULL;
5470 struct got_pathlist_entry *new_pe;
5472 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5473 if (err)
5474 return err;
5475 if (new_pe == NULL)
5476 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5477 return NULL;
5480 static const struct got_error *
5481 report_ct_status(struct got_commitable *ct,
5482 got_worktree_status_cb status_cb, void *status_arg)
5484 const char *ct_path = ct->path;
5485 unsigned char status;
5487 if (status_cb == NULL) /* no commit progress output desired */
5488 return NULL;
5490 while (ct_path[0] == '/')
5491 ct_path++;
5493 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5494 status = ct->staged_status;
5495 else
5496 status = ct->status;
5498 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5499 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5502 static const struct got_error *
5503 match_modified_subtree(int *modified, struct got_tree_entry *te,
5504 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5506 const struct got_error *err = NULL;
5507 struct got_pathlist_entry *pe;
5508 char *te_path;
5510 *modified = 0;
5512 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5513 got_path_is_root_dir(base_tree_path) ? "" : "/",
5514 te->name) == -1)
5515 return got_error_from_errno("asprintf");
5517 TAILQ_FOREACH(pe, commitable_paths, entry) {
5518 struct got_commitable *ct = pe->data;
5519 *modified = got_path_is_child(ct->in_repo_path, te_path,
5520 strlen(te_path));
5521 if (*modified)
5522 break;
5525 free(te_path);
5526 return err;
5529 static const struct got_error *
5530 match_deleted_or_modified_ct(struct got_commitable **ctp,
5531 struct got_tree_entry *te, const char *base_tree_path,
5532 struct got_pathlist_head *commitable_paths)
5534 const struct got_error *err = NULL;
5535 struct got_pathlist_entry *pe;
5537 *ctp = NULL;
5539 TAILQ_FOREACH(pe, commitable_paths, entry) {
5540 struct got_commitable *ct = pe->data;
5541 char *ct_name = NULL;
5542 int path_matches;
5544 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5545 if (ct->status != GOT_STATUS_MODIFY &&
5546 ct->status != GOT_STATUS_MODE_CHANGE &&
5547 ct->status != GOT_STATUS_DELETE)
5548 continue;
5549 } else {
5550 if (ct->staged_status != GOT_STATUS_MODIFY &&
5551 ct->staged_status != GOT_STATUS_DELETE)
5552 continue;
5555 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5556 continue;
5558 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5559 if (err)
5560 return err;
5561 if (!path_matches)
5562 continue;
5564 err = got_path_basename(&ct_name, pe->path);
5565 if (err)
5566 return err;
5568 if (strcmp(te->name, ct_name) != 0) {
5569 free(ct_name);
5570 continue;
5572 free(ct_name);
5574 *ctp = ct;
5575 break;
5578 return err;
5581 static const struct got_error *
5582 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5583 const char *child_path, const char *path_base_tree,
5584 struct got_pathlist_head *commitable_paths,
5585 got_worktree_status_cb status_cb, void *status_arg,
5586 struct got_repository *repo)
5588 const struct got_error *err = NULL;
5589 struct got_tree_entry *new_te;
5590 char *subtree_path;
5591 struct got_object_id *id = NULL;
5592 int nentries;
5594 *new_tep = NULL;
5596 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5597 got_path_is_root_dir(path_base_tree) ? "" : "/",
5598 child_path) == -1)
5599 return got_error_from_errno("asprintf");
5601 new_te = calloc(1, sizeof(*new_te));
5602 if (new_te == NULL)
5603 return got_error_from_errno("calloc");
5604 new_te->mode = S_IFDIR;
5606 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5607 sizeof(new_te->name)) {
5608 err = got_error(GOT_ERR_NO_SPACE);
5609 goto done;
5611 err = write_tree(&id, &nentries, NULL, subtree_path,
5612 commitable_paths, status_cb, status_arg, repo);
5613 if (err) {
5614 free(new_te);
5615 goto done;
5617 memcpy(&new_te->id, id, sizeof(new_te->id));
5618 done:
5619 free(id);
5620 free(subtree_path);
5621 if (err == NULL)
5622 *new_tep = new_te;
5623 return err;
5626 static const struct got_error *
5627 write_tree(struct got_object_id **new_tree_id, int *nentries,
5628 struct got_tree_object *base_tree, const char *path_base_tree,
5629 struct got_pathlist_head *commitable_paths,
5630 got_worktree_status_cb status_cb, void *status_arg,
5631 struct got_repository *repo)
5633 const struct got_error *err = NULL;
5634 struct got_pathlist_head paths;
5635 struct got_tree_entry *te, *new_te = NULL;
5636 struct got_pathlist_entry *pe;
5638 TAILQ_INIT(&paths);
5639 *nentries = 0;
5641 /* Insert, and recurse into, newly added entries first. */
5642 TAILQ_FOREACH(pe, commitable_paths, entry) {
5643 struct got_commitable *ct = pe->data;
5644 char *child_path = NULL, *slash;
5646 if ((ct->status != GOT_STATUS_ADD &&
5647 ct->staged_status != GOT_STATUS_ADD) ||
5648 (ct->flags & GOT_COMMITABLE_ADDED))
5649 continue;
5651 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5652 strlen(path_base_tree)))
5653 continue;
5655 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5656 ct->in_repo_path);
5657 if (err)
5658 goto done;
5660 slash = strchr(child_path, '/');
5661 if (slash == NULL) {
5662 err = alloc_added_blob_tree_entry(&new_te, ct);
5663 if (err)
5664 goto done;
5665 err = report_ct_status(ct, status_cb, status_arg);
5666 if (err)
5667 goto done;
5668 ct->flags |= GOT_COMMITABLE_ADDED;
5669 err = insert_tree_entry(new_te, &paths);
5670 if (err)
5671 goto done;
5672 (*nentries)++;
5673 } else {
5674 *slash = '\0'; /* trim trailing path components */
5675 if (base_tree == NULL ||
5676 got_object_tree_find_entry(base_tree, child_path)
5677 == NULL) {
5678 err = make_subtree_for_added_blob(&new_te,
5679 child_path, path_base_tree,
5680 commitable_paths, status_cb, status_arg,
5681 repo);
5682 if (err)
5683 goto done;
5684 err = insert_tree_entry(new_te, &paths);
5685 if (err)
5686 goto done;
5687 (*nentries)++;
5692 if (base_tree) {
5693 int i, nbase_entries;
5694 /* Handle modified and deleted entries. */
5695 nbase_entries = got_object_tree_get_nentries(base_tree);
5696 for (i = 0; i < nbase_entries; i++) {
5697 struct got_commitable *ct = NULL;
5699 te = got_object_tree_get_entry(base_tree, i);
5700 if (got_object_tree_entry_is_submodule(te)) {
5701 /* Entry is a submodule; just copy it. */
5702 err = got_object_tree_entry_dup(&new_te, te);
5703 if (err)
5704 goto done;
5705 err = insert_tree_entry(new_te, &paths);
5706 if (err)
5707 goto done;
5708 (*nentries)++;
5709 continue;
5712 if (S_ISDIR(te->mode)) {
5713 int modified;
5714 err = got_object_tree_entry_dup(&new_te, te);
5715 if (err)
5716 goto done;
5717 err = match_modified_subtree(&modified, te,
5718 path_base_tree, commitable_paths);
5719 if (err)
5720 goto done;
5721 /* Avoid recursion into unmodified subtrees. */
5722 if (modified) {
5723 struct got_object_id *new_id;
5724 int nsubentries;
5725 err = write_subtree(&new_id,
5726 &nsubentries, te,
5727 path_base_tree, commitable_paths,
5728 status_cb, status_arg, repo);
5729 if (err)
5730 goto done;
5731 if (nsubentries == 0) {
5732 /* All entries were deleted. */
5733 free(new_id);
5734 continue;
5736 memcpy(&new_te->id, new_id,
5737 sizeof(new_te->id));
5738 free(new_id);
5740 err = insert_tree_entry(new_te, &paths);
5741 if (err)
5742 goto done;
5743 (*nentries)++;
5744 continue;
5747 err = match_deleted_or_modified_ct(&ct, te,
5748 path_base_tree, commitable_paths);
5749 if (err)
5750 goto done;
5751 if (ct) {
5752 /* NB: Deleted entries get dropped here. */
5753 if (ct->status == GOT_STATUS_MODIFY ||
5754 ct->status == GOT_STATUS_MODE_CHANGE ||
5755 ct->staged_status == GOT_STATUS_MODIFY) {
5756 err = alloc_modified_blob_tree_entry(
5757 &new_te, te, ct);
5758 if (err)
5759 goto done;
5760 err = insert_tree_entry(new_te, &paths);
5761 if (err)
5762 goto done;
5763 (*nentries)++;
5765 err = report_ct_status(ct, status_cb,
5766 status_arg);
5767 if (err)
5768 goto done;
5769 } else {
5770 /* Entry is unchanged; just copy it. */
5771 err = got_object_tree_entry_dup(&new_te, te);
5772 if (err)
5773 goto done;
5774 err = insert_tree_entry(new_te, &paths);
5775 if (err)
5776 goto done;
5777 (*nentries)++;
5782 /* Write new list of entries; deleted entries have been dropped. */
5783 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5784 done:
5785 got_pathlist_free(&paths);
5786 return err;
5789 static const struct got_error *
5790 update_fileindex_after_commit(struct got_worktree *worktree,
5791 struct got_pathlist_head *commitable_paths,
5792 struct got_object_id *new_base_commit_id,
5793 struct got_fileindex *fileindex, int have_staged_files)
5795 const struct got_error *err = NULL;
5796 struct got_pathlist_entry *pe;
5797 char *relpath = NULL;
5799 TAILQ_FOREACH(pe, commitable_paths, entry) {
5800 struct got_fileindex_entry *ie;
5801 struct got_commitable *ct = pe->data;
5803 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5805 err = got_path_skip_common_ancestor(&relpath,
5806 worktree->root_path, ct->ondisk_path);
5807 if (err)
5808 goto done;
5810 if (ie) {
5811 if (ct->status == GOT_STATUS_DELETE ||
5812 ct->staged_status == GOT_STATUS_DELETE) {
5813 got_fileindex_entry_remove(fileindex, ie);
5814 } else if (ct->staged_status == GOT_STATUS_ADD ||
5815 ct->staged_status == GOT_STATUS_MODIFY) {
5816 got_fileindex_entry_stage_set(ie,
5817 GOT_FILEIDX_STAGE_NONE);
5818 got_fileindex_entry_staged_filetype_set(ie, 0);
5820 err = got_fileindex_entry_update(ie,
5821 worktree->root_fd, relpath,
5822 ct->staged_blob_id->sha1,
5823 new_base_commit_id->sha1,
5824 !have_staged_files);
5825 } else
5826 err = got_fileindex_entry_update(ie,
5827 worktree->root_fd, relpath,
5828 ct->blob_id->sha1,
5829 new_base_commit_id->sha1,
5830 !have_staged_files);
5831 } else {
5832 err = got_fileindex_entry_alloc(&ie, pe->path);
5833 if (err)
5834 goto done;
5835 err = got_fileindex_entry_update(ie,
5836 worktree->root_fd, relpath, ct->blob_id->sha1,
5837 new_base_commit_id->sha1, 1);
5838 if (err) {
5839 got_fileindex_entry_free(ie);
5840 goto done;
5842 err = got_fileindex_entry_add(fileindex, ie);
5843 if (err) {
5844 got_fileindex_entry_free(ie);
5845 goto done;
5848 free(relpath);
5849 relpath = NULL;
5851 done:
5852 free(relpath);
5853 return err;
5857 static const struct got_error *
5858 check_out_of_date(const char *in_repo_path, unsigned char status,
5859 unsigned char staged_status, struct got_object_id *base_blob_id,
5860 struct got_object_id *base_commit_id,
5861 struct got_object_id *head_commit_id, struct got_repository *repo,
5862 int ood_errcode)
5864 const struct got_error *err = NULL;
5865 struct got_commit_object *commit = NULL;
5866 struct got_object_id *id = NULL;
5868 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5869 /* Trivial case: base commit == head commit */
5870 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5871 return NULL;
5873 * Ensure file content which local changes were based
5874 * on matches file content in the branch head.
5876 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5877 if (err)
5878 goto done;
5879 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5880 if (err) {
5881 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5882 err = got_error(ood_errcode);
5883 goto done;
5884 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5885 err = got_error(ood_errcode);
5886 } else {
5887 /* Require that added files don't exist in the branch head. */
5888 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5889 if (err)
5890 goto done;
5891 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5892 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5893 goto done;
5894 err = id ? got_error(ood_errcode) : NULL;
5896 done:
5897 free(id);
5898 if (commit)
5899 got_object_commit_close(commit);
5900 return err;
5903 static const struct got_error *
5904 commit_worktree(struct got_object_id **new_commit_id,
5905 struct got_pathlist_head *commitable_paths,
5906 struct got_object_id *head_commit_id,
5907 struct got_object_id *parent_id2,
5908 struct got_worktree *worktree,
5909 const char *author, const char *committer, char *diff_path,
5910 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5911 got_worktree_status_cb status_cb, void *status_arg,
5912 struct got_repository *repo)
5914 const struct got_error *err = NULL, *unlockerr = NULL;
5915 struct got_pathlist_entry *pe;
5916 const char *head_ref_name = NULL;
5917 struct got_commit_object *head_commit = NULL;
5918 struct got_reference *head_ref2 = NULL;
5919 struct got_object_id *head_commit_id2 = NULL;
5920 struct got_tree_object *head_tree = NULL;
5921 struct got_object_id *new_tree_id = NULL;
5922 int nentries, nparents = 0;
5923 struct got_object_id_queue parent_ids;
5924 struct got_object_qid *pid = NULL;
5925 char *logmsg = NULL;
5926 time_t timestamp;
5928 *new_commit_id = NULL;
5930 STAILQ_INIT(&parent_ids);
5932 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5933 if (err)
5934 goto done;
5936 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5937 if (err)
5938 goto done;
5940 if (commit_msg_cb != NULL) {
5941 err = commit_msg_cb(commitable_paths, diff_path,
5942 &logmsg, commit_arg);
5943 if (err)
5944 goto done;
5947 if (logmsg == NULL || strlen(logmsg) == 0) {
5948 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5949 goto done;
5952 /* Create blobs from added and modified files and record their IDs. */
5953 TAILQ_FOREACH(pe, commitable_paths, entry) {
5954 struct got_commitable *ct = pe->data;
5955 char *ondisk_path;
5957 /* Blobs for staged files already exist. */
5958 if (ct->staged_status == GOT_STATUS_ADD ||
5959 ct->staged_status == GOT_STATUS_MODIFY)
5960 continue;
5962 if (ct->status != GOT_STATUS_ADD &&
5963 ct->status != GOT_STATUS_MODIFY &&
5964 ct->status != GOT_STATUS_MODE_CHANGE)
5965 continue;
5967 if (asprintf(&ondisk_path, "%s/%s",
5968 worktree->root_path, pe->path) == -1) {
5969 err = got_error_from_errno("asprintf");
5970 goto done;
5972 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5973 free(ondisk_path);
5974 if (err)
5975 goto done;
5978 /* Recursively write new tree objects. */
5979 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5980 commitable_paths, status_cb, status_arg, repo);
5981 if (err)
5982 goto done;
5984 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5985 if (err)
5986 goto done;
5987 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5988 nparents++;
5989 if (parent_id2) {
5990 err = got_object_qid_alloc(&pid, parent_id2);
5991 if (err)
5992 goto done;
5993 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5994 nparents++;
5996 timestamp = time(NULL);
5997 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5998 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5999 if (logmsg != NULL)
6000 free(logmsg);
6001 if (err)
6002 goto done;
6004 /* Check if a concurrent commit to our branch has occurred. */
6005 head_ref_name = got_worktree_get_head_ref_name(worktree);
6006 if (head_ref_name == NULL) {
6007 err = got_error_from_errno("got_worktree_get_head_ref_name");
6008 goto done;
6010 /* Lock the reference here to prevent concurrent modification. */
6011 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6012 if (err)
6013 goto done;
6014 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6015 if (err)
6016 goto done;
6017 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6018 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6019 goto done;
6021 /* Update branch head in repository. */
6022 err = got_ref_change_ref(head_ref2, *new_commit_id);
6023 if (err)
6024 goto done;
6025 err = got_ref_write(head_ref2, repo);
6026 if (err)
6027 goto done;
6029 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6030 if (err)
6031 goto done;
6033 err = ref_base_commit(worktree, repo);
6034 if (err)
6035 goto done;
6036 done:
6037 got_object_id_queue_free(&parent_ids);
6038 if (head_tree)
6039 got_object_tree_close(head_tree);
6040 if (head_commit)
6041 got_object_commit_close(head_commit);
6042 free(head_commit_id2);
6043 if (head_ref2) {
6044 unlockerr = got_ref_unlock(head_ref2);
6045 if (unlockerr && err == NULL)
6046 err = unlockerr;
6047 got_ref_close(head_ref2);
6049 return err;
6052 static const struct got_error *
6053 check_path_is_commitable(const char *path,
6054 struct got_pathlist_head *commitable_paths)
6056 struct got_pathlist_entry *cpe = NULL;
6057 size_t path_len = strlen(path);
6059 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6060 struct got_commitable *ct = cpe->data;
6061 const char *ct_path = ct->path;
6063 while (ct_path[0] == '/')
6064 ct_path++;
6066 if (strcmp(path, ct_path) == 0 ||
6067 got_path_is_child(ct_path, path, path_len))
6068 break;
6071 if (cpe == NULL)
6072 return got_error_path(path, GOT_ERR_BAD_PATH);
6074 return NULL;
6077 static const struct got_error *
6078 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6080 int *have_staged_files = arg;
6082 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6083 *have_staged_files = 1;
6084 return got_error(GOT_ERR_CANCELLED);
6087 return NULL;
6090 static const struct got_error *
6091 check_non_staged_files(struct got_fileindex *fileindex,
6092 struct got_pathlist_head *paths)
6094 struct got_pathlist_entry *pe;
6095 struct got_fileindex_entry *ie;
6097 TAILQ_FOREACH(pe, paths, entry) {
6098 if (pe->path[0] == '\0')
6099 continue;
6100 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6101 if (ie == NULL)
6102 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6103 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6104 return got_error_path(pe->path,
6105 GOT_ERR_FILE_NOT_STAGED);
6108 return NULL;
6111 const struct got_error *
6112 got_worktree_commit(struct got_object_id **new_commit_id,
6113 struct got_worktree *worktree, struct got_pathlist_head *paths,
6114 const char *author, const char *committer, int allow_bad_symlinks,
6115 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6116 got_worktree_status_cb status_cb, void *status_arg,
6117 struct got_repository *repo)
6119 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6120 struct got_fileindex *fileindex = NULL;
6121 char *fileindex_path = NULL;
6122 struct got_pathlist_head commitable_paths;
6123 struct collect_commitables_arg cc_arg;
6124 struct got_pathlist_entry *pe;
6125 struct got_reference *head_ref = NULL;
6126 struct got_object_id *head_commit_id = NULL;
6127 char *diff_path = NULL;
6128 int have_staged_files = 0;
6130 *new_commit_id = NULL;
6132 memset(&cc_arg, 0, sizeof(cc_arg));
6133 TAILQ_INIT(&commitable_paths);
6135 err = lock_worktree(worktree, LOCK_EX);
6136 if (err)
6137 goto done;
6139 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6140 if (err)
6141 goto done;
6143 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6144 if (err)
6145 goto done;
6147 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6148 if (err)
6149 goto done;
6151 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6152 &have_staged_files);
6153 if (err && err->code != GOT_ERR_CANCELLED)
6154 goto done;
6155 if (have_staged_files) {
6156 err = check_non_staged_files(fileindex, paths);
6157 if (err)
6158 goto done;
6161 cc_arg.commitable_paths = &commitable_paths;
6162 cc_arg.worktree = worktree;
6163 cc_arg.fileindex = fileindex;
6164 cc_arg.repo = repo;
6165 cc_arg.have_staged_files = have_staged_files;
6166 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6167 cc_arg.diff_header_shown = 0;
6168 if (show_diff) {
6169 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6170 GOT_TMPDIR_STR "/got", ".diff");
6171 if (err)
6172 goto done;
6173 cc_arg.f1 = got_opentemp();
6174 if (cc_arg.f1 == NULL) {
6175 err = got_error_from_errno("got_opentemp");
6176 goto done;
6178 cc_arg.f2 = got_opentemp();
6179 if (cc_arg.f2 == NULL) {
6180 err = got_error_from_errno("got_opentemp");
6181 goto done;
6185 TAILQ_FOREACH(pe, paths, entry) {
6186 err = worktree_status(worktree, pe->path, fileindex, repo,
6187 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6188 if (err)
6189 goto done;
6192 if (show_diff) {
6193 if (fflush(cc_arg.diff_outfile) == EOF) {
6194 err = got_error_from_errno("fflush");
6195 goto done;
6199 if (TAILQ_EMPTY(&commitable_paths)) {
6200 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6201 goto done;
6204 TAILQ_FOREACH(pe, paths, entry) {
6205 err = check_path_is_commitable(pe->path, &commitable_paths);
6206 if (err)
6207 goto done;
6210 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6211 struct got_commitable *ct = pe->data;
6212 const char *ct_path = ct->in_repo_path;
6214 while (ct_path[0] == '/')
6215 ct_path++;
6216 err = check_out_of_date(ct_path, ct->status,
6217 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6218 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6219 if (err)
6220 goto done;
6224 err = commit_worktree(new_commit_id, &commitable_paths,
6225 head_commit_id, NULL, worktree, author, committer,
6226 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6227 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6228 if (err)
6229 goto done;
6231 err = update_fileindex_after_commit(worktree, &commitable_paths,
6232 *new_commit_id, fileindex, have_staged_files);
6233 sync_err = sync_fileindex(fileindex, fileindex_path);
6234 if (sync_err && err == NULL)
6235 err = sync_err;
6236 done:
6237 if (fileindex)
6238 got_fileindex_free(fileindex);
6239 free(fileindex_path);
6240 unlockerr = lock_worktree(worktree, LOCK_SH);
6241 if (unlockerr && err == NULL)
6242 err = unlockerr;
6243 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6244 struct got_commitable *ct = pe->data;
6245 free_commitable(ct);
6247 got_pathlist_free(&commitable_paths);
6248 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6249 err = got_error_from_errno2("unlink", diff_path);
6250 free(diff_path);
6251 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6252 err == NULL)
6253 err = got_error_from_errno("fclose");
6254 return err;
6257 const char *
6258 got_commitable_get_path(struct got_commitable *ct)
6260 return ct->path;
6263 unsigned int
6264 got_commitable_get_status(struct got_commitable *ct)
6266 return ct->status;
6269 struct check_rebase_ok_arg {
6270 struct got_worktree *worktree;
6271 struct got_repository *repo;
6274 static const struct got_error *
6275 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6277 const struct got_error *err = NULL;
6278 struct check_rebase_ok_arg *a = arg;
6279 unsigned char status;
6280 struct stat sb;
6281 char *ondisk_path;
6283 /* Reject rebase of a work tree with mixed base commits. */
6284 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6285 SHA1_DIGEST_LENGTH))
6286 return got_error(GOT_ERR_MIXED_COMMITS);
6288 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6289 == -1)
6290 return got_error_from_errno("asprintf");
6292 /* Reject rebase of a work tree with modified or staged files. */
6293 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6294 free(ondisk_path);
6295 if (err)
6296 return err;
6298 if (status != GOT_STATUS_NO_CHANGE)
6299 return got_error(GOT_ERR_MODIFIED);
6300 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6301 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6303 return NULL;
6306 const struct got_error *
6307 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6308 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6309 struct got_worktree *worktree, struct got_reference *branch,
6310 struct got_repository *repo)
6312 const struct got_error *err = NULL;
6313 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6314 char *branch_ref_name = NULL;
6315 char *fileindex_path = NULL;
6316 struct check_rebase_ok_arg ok_arg;
6317 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6318 struct got_object_id *wt_branch_tip = NULL;
6320 *new_base_branch_ref = NULL;
6321 *tmp_branch = NULL;
6322 *fileindex = NULL;
6324 err = lock_worktree(worktree, LOCK_EX);
6325 if (err)
6326 return err;
6328 err = open_fileindex(fileindex, &fileindex_path, worktree);
6329 if (err)
6330 goto done;
6332 ok_arg.worktree = worktree;
6333 ok_arg.repo = repo;
6334 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6335 &ok_arg);
6336 if (err)
6337 goto done;
6339 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6340 if (err)
6341 goto done;
6343 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6344 if (err)
6345 goto done;
6347 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6348 if (err)
6349 goto done;
6351 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6352 0);
6353 if (err)
6354 goto done;
6356 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6357 if (err)
6358 goto done;
6359 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6360 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6361 goto done;
6364 err = got_ref_alloc_symref(new_base_branch_ref,
6365 new_base_branch_ref_name, wt_branch);
6366 if (err)
6367 goto done;
6368 err = got_ref_write(*new_base_branch_ref, repo);
6369 if (err)
6370 goto done;
6372 /* TODO Lock original branch's ref while rebasing? */
6374 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6375 if (err)
6376 goto done;
6378 err = got_ref_write(branch_ref, repo);
6379 if (err)
6380 goto done;
6382 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6383 worktree->base_commit_id);
6384 if (err)
6385 goto done;
6386 err = got_ref_write(*tmp_branch, repo);
6387 if (err)
6388 goto done;
6390 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6391 if (err)
6392 goto done;
6393 done:
6394 free(fileindex_path);
6395 free(tmp_branch_name);
6396 free(new_base_branch_ref_name);
6397 free(branch_ref_name);
6398 if (branch_ref)
6399 got_ref_close(branch_ref);
6400 if (wt_branch)
6401 got_ref_close(wt_branch);
6402 free(wt_branch_tip);
6403 if (err) {
6404 if (*new_base_branch_ref) {
6405 got_ref_close(*new_base_branch_ref);
6406 *new_base_branch_ref = NULL;
6408 if (*tmp_branch) {
6409 got_ref_close(*tmp_branch);
6410 *tmp_branch = NULL;
6412 if (*fileindex) {
6413 got_fileindex_free(*fileindex);
6414 *fileindex = NULL;
6416 lock_worktree(worktree, LOCK_SH);
6418 return err;
6421 const struct got_error *
6422 got_worktree_rebase_continue(struct got_object_id **commit_id,
6423 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6424 struct got_reference **branch, struct got_fileindex **fileindex,
6425 struct got_worktree *worktree, struct got_repository *repo)
6427 const struct got_error *err;
6428 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6429 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6430 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6431 char *fileindex_path = NULL;
6432 int have_staged_files = 0;
6434 *commit_id = NULL;
6435 *new_base_branch = NULL;
6436 *tmp_branch = NULL;
6437 *branch = NULL;
6438 *fileindex = NULL;
6440 err = lock_worktree(worktree, LOCK_EX);
6441 if (err)
6442 return err;
6444 err = open_fileindex(fileindex, &fileindex_path, worktree);
6445 if (err)
6446 goto done;
6448 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6449 &have_staged_files);
6450 if (err && err->code != GOT_ERR_CANCELLED)
6451 goto done;
6452 if (have_staged_files) {
6453 err = got_error(GOT_ERR_STAGED_PATHS);
6454 goto done;
6457 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6458 if (err)
6459 goto done;
6461 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6462 if (err)
6463 goto done;
6465 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6466 if (err)
6467 goto done;
6469 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6470 if (err)
6471 goto done;
6473 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6474 if (err)
6475 goto done;
6477 err = got_ref_open(branch, repo,
6478 got_ref_get_symref_target(branch_ref), 0);
6479 if (err)
6480 goto done;
6482 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6483 if (err)
6484 goto done;
6486 err = got_ref_resolve(commit_id, repo, commit_ref);
6487 if (err)
6488 goto done;
6490 err = got_ref_open(new_base_branch, repo,
6491 new_base_branch_ref_name, 0);
6492 if (err)
6493 goto done;
6495 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6496 if (err)
6497 goto done;
6498 done:
6499 free(commit_ref_name);
6500 free(branch_ref_name);
6501 free(fileindex_path);
6502 if (commit_ref)
6503 got_ref_close(commit_ref);
6504 if (branch_ref)
6505 got_ref_close(branch_ref);
6506 if (err) {
6507 free(*commit_id);
6508 *commit_id = NULL;
6509 if (*tmp_branch) {
6510 got_ref_close(*tmp_branch);
6511 *tmp_branch = NULL;
6513 if (*new_base_branch) {
6514 got_ref_close(*new_base_branch);
6515 *new_base_branch = NULL;
6517 if (*branch) {
6518 got_ref_close(*branch);
6519 *branch = NULL;
6521 if (*fileindex) {
6522 got_fileindex_free(*fileindex);
6523 *fileindex = NULL;
6525 lock_worktree(worktree, LOCK_SH);
6527 return err;
6530 const struct got_error *
6531 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6533 const struct got_error *err;
6534 char *tmp_branch_name = NULL;
6536 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6537 if (err)
6538 return err;
6540 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6541 free(tmp_branch_name);
6542 return NULL;
6545 static const struct got_error *
6546 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6547 const char *diff_path, char **logmsg, void *arg)
6549 *logmsg = arg;
6550 return NULL;
6553 static const struct got_error *
6554 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6555 const char *path, struct got_object_id *blob_id,
6556 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6557 int dirfd, const char *de_name)
6559 return NULL;
6562 struct collect_merged_paths_arg {
6563 got_worktree_checkout_cb progress_cb;
6564 void *progress_arg;
6565 struct got_pathlist_head *merged_paths;
6568 static const struct got_error *
6569 collect_merged_paths(void *arg, unsigned char status, const char *path)
6571 const struct got_error *err;
6572 struct collect_merged_paths_arg *a = arg;
6573 char *p;
6574 struct got_pathlist_entry *new;
6576 err = (*a->progress_cb)(a->progress_arg, status, path);
6577 if (err)
6578 return err;
6580 if (status != GOT_STATUS_MERGE &&
6581 status != GOT_STATUS_ADD &&
6582 status != GOT_STATUS_DELETE &&
6583 status != GOT_STATUS_CONFLICT)
6584 return NULL;
6586 p = strdup(path);
6587 if (p == NULL)
6588 return got_error_from_errno("strdup");
6590 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6591 if (err || new == NULL)
6592 free(p);
6593 return err;
6596 void
6597 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6599 struct got_pathlist_entry *pe;
6601 TAILQ_FOREACH(pe, merged_paths, entry)
6602 free((char *)pe->path);
6604 got_pathlist_free(merged_paths);
6607 static const struct got_error *
6608 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6609 int is_rebase, struct got_repository *repo)
6611 const struct got_error *err;
6612 struct got_reference *commit_ref = NULL;
6614 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6615 if (err) {
6616 if (err->code != GOT_ERR_NOT_REF)
6617 goto done;
6618 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6619 if (err)
6620 goto done;
6621 err = got_ref_write(commit_ref, repo);
6622 if (err)
6623 goto done;
6624 } else if (is_rebase) {
6625 struct got_object_id *stored_id;
6626 int cmp;
6628 err = got_ref_resolve(&stored_id, repo, commit_ref);
6629 if (err)
6630 goto done;
6631 cmp = got_object_id_cmp(commit_id, stored_id);
6632 free(stored_id);
6633 if (cmp != 0) {
6634 err = got_error(GOT_ERR_REBASE_COMMITID);
6635 goto done;
6638 done:
6639 if (commit_ref)
6640 got_ref_close(commit_ref);
6641 return err;
6644 static const struct got_error *
6645 rebase_merge_files(struct got_pathlist_head *merged_paths,
6646 const char *commit_ref_name, struct got_worktree *worktree,
6647 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6648 struct got_object_id *commit_id, struct got_repository *repo,
6649 got_worktree_checkout_cb progress_cb, void *progress_arg,
6650 got_cancel_cb cancel_cb, void *cancel_arg)
6652 const struct got_error *err;
6653 struct got_reference *commit_ref = NULL;
6654 struct collect_merged_paths_arg cmp_arg;
6655 char *fileindex_path;
6657 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6659 err = get_fileindex_path(&fileindex_path, worktree);
6660 if (err)
6661 return err;
6663 cmp_arg.progress_cb = progress_cb;
6664 cmp_arg.progress_arg = progress_arg;
6665 cmp_arg.merged_paths = merged_paths;
6666 err = merge_files(worktree, fileindex, fileindex_path,
6667 parent_commit_id, commit_id, repo, collect_merged_paths,
6668 &cmp_arg, cancel_cb, cancel_arg);
6669 if (commit_ref)
6670 got_ref_close(commit_ref);
6671 return err;
6674 const struct got_error *
6675 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6676 struct got_worktree *worktree, struct got_fileindex *fileindex,
6677 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6678 struct got_repository *repo,
6679 got_worktree_checkout_cb progress_cb, void *progress_arg,
6680 got_cancel_cb cancel_cb, void *cancel_arg)
6682 const struct got_error *err;
6683 char *commit_ref_name;
6685 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6686 if (err)
6687 return err;
6689 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6690 if (err)
6691 goto done;
6693 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6694 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6695 progress_arg, cancel_cb, cancel_arg);
6696 done:
6697 free(commit_ref_name);
6698 return err;
6701 const struct got_error *
6702 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6703 struct got_worktree *worktree, struct got_fileindex *fileindex,
6704 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6705 struct got_repository *repo,
6706 got_worktree_checkout_cb progress_cb, void *progress_arg,
6707 got_cancel_cb cancel_cb, void *cancel_arg)
6709 const struct got_error *err;
6710 char *commit_ref_name;
6712 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6713 if (err)
6714 return err;
6716 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6717 if (err)
6718 goto done;
6720 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6721 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6722 progress_arg, cancel_cb, cancel_arg);
6723 done:
6724 free(commit_ref_name);
6725 return err;
6728 static const struct got_error *
6729 rebase_commit(struct got_object_id **new_commit_id,
6730 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6731 struct got_worktree *worktree, struct got_fileindex *fileindex,
6732 struct got_reference *tmp_branch, const char *committer,
6733 struct got_commit_object *orig_commit, const char *new_logmsg,
6734 struct got_repository *repo)
6736 const struct got_error *err, *sync_err;
6737 struct got_pathlist_head commitable_paths;
6738 struct collect_commitables_arg cc_arg;
6739 char *fileindex_path = NULL;
6740 struct got_reference *head_ref = NULL;
6741 struct got_object_id *head_commit_id = NULL;
6742 char *logmsg = NULL;
6744 memset(&cc_arg, 0, sizeof(cc_arg));
6745 TAILQ_INIT(&commitable_paths);
6746 *new_commit_id = NULL;
6748 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6750 err = get_fileindex_path(&fileindex_path, worktree);
6751 if (err)
6752 return err;
6754 cc_arg.commitable_paths = &commitable_paths;
6755 cc_arg.worktree = worktree;
6756 cc_arg.repo = repo;
6757 cc_arg.have_staged_files = 0;
6759 * If possible get the status of individual files directly to
6760 * avoid crawling the entire work tree once per rebased commit.
6762 * Ideally, merged_paths would contain a list of commitables
6763 * we could use so we could skip worktree_status() entirely.
6764 * However, we would then need carefully keep track of cumulative
6765 * effects of operations such as file additions and deletions
6766 * in 'got histedit -f' (folding multiple commits into one),
6767 * and this extra complexity is not really worth it.
6769 if (merged_paths) {
6770 struct got_pathlist_entry *pe;
6771 TAILQ_FOREACH(pe, merged_paths, entry) {
6772 err = worktree_status(worktree, pe->path, fileindex,
6773 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6774 0);
6775 if (err)
6776 goto done;
6778 } else {
6779 err = worktree_status(worktree, "", fileindex, repo,
6780 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6781 if (err)
6782 goto done;
6785 if (TAILQ_EMPTY(&commitable_paths)) {
6786 /* No-op change; commit will be elided. */
6787 err = got_ref_delete(commit_ref, repo);
6788 if (err)
6789 goto done;
6790 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6791 goto done;
6794 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6795 if (err)
6796 goto done;
6798 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6799 if (err)
6800 goto done;
6802 if (new_logmsg) {
6803 logmsg = strdup(new_logmsg);
6804 if (logmsg == NULL) {
6805 err = got_error_from_errno("strdup");
6806 goto done;
6808 } else {
6809 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6810 if (err)
6811 goto done;
6814 /* NB: commit_worktree will call free(logmsg) */
6815 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6816 NULL, worktree, got_object_commit_get_author(orig_commit),
6817 committer ? committer :
6818 got_object_commit_get_committer(orig_commit), NULL,
6819 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6820 if (err)
6821 goto done;
6823 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6824 if (err)
6825 goto done;
6827 err = got_ref_delete(commit_ref, repo);
6828 if (err)
6829 goto done;
6831 err = update_fileindex_after_commit(worktree, &commitable_paths,
6832 *new_commit_id, fileindex, 0);
6833 sync_err = sync_fileindex(fileindex, fileindex_path);
6834 if (sync_err && err == NULL)
6835 err = sync_err;
6836 done:
6837 free(fileindex_path);
6838 free(head_commit_id);
6839 if (head_ref)
6840 got_ref_close(head_ref);
6841 if (err) {
6842 free(*new_commit_id);
6843 *new_commit_id = NULL;
6845 return err;
6848 const struct got_error *
6849 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6850 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6851 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6852 const char *committer, struct got_commit_object *orig_commit,
6853 struct got_object_id *orig_commit_id, struct got_repository *repo)
6855 const struct got_error *err;
6856 char *commit_ref_name;
6857 struct got_reference *commit_ref = NULL;
6858 struct got_object_id *commit_id = NULL;
6860 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6861 if (err)
6862 return err;
6864 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6865 if (err)
6866 goto done;
6867 err = got_ref_resolve(&commit_id, repo, commit_ref);
6868 if (err)
6869 goto done;
6870 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6871 err = got_error(GOT_ERR_REBASE_COMMITID);
6872 goto done;
6875 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6876 worktree, fileindex, tmp_branch, committer, orig_commit,
6877 NULL, repo);
6878 done:
6879 if (commit_ref)
6880 got_ref_close(commit_ref);
6881 free(commit_ref_name);
6882 free(commit_id);
6883 return err;
6886 const struct got_error *
6887 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6888 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6889 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6890 const char *committer, struct got_commit_object *orig_commit,
6891 struct got_object_id *orig_commit_id, const char *new_logmsg,
6892 struct got_repository *repo)
6894 const struct got_error *err;
6895 char *commit_ref_name;
6896 struct got_reference *commit_ref = NULL;
6898 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6899 if (err)
6900 return err;
6902 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6903 if (err)
6904 goto done;
6906 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6907 worktree, fileindex, tmp_branch, committer, orig_commit,
6908 new_logmsg, repo);
6909 done:
6910 if (commit_ref)
6911 got_ref_close(commit_ref);
6912 free(commit_ref_name);
6913 return err;
6916 const struct got_error *
6917 got_worktree_rebase_postpone(struct got_worktree *worktree,
6918 struct got_fileindex *fileindex)
6920 if (fileindex)
6921 got_fileindex_free(fileindex);
6922 return lock_worktree(worktree, LOCK_SH);
6925 static const struct got_error *
6926 delete_ref(const char *name, struct got_repository *repo)
6928 const struct got_error *err;
6929 struct got_reference *ref;
6931 err = got_ref_open(&ref, repo, name, 0);
6932 if (err) {
6933 if (err->code == GOT_ERR_NOT_REF)
6934 return NULL;
6935 return err;
6938 err = got_ref_delete(ref, repo);
6939 got_ref_close(ref);
6940 return err;
6943 static const struct got_error *
6944 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6946 const struct got_error *err;
6947 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6948 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6950 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6951 if (err)
6952 goto done;
6953 err = delete_ref(tmp_branch_name, repo);
6954 if (err)
6955 goto done;
6957 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6958 if (err)
6959 goto done;
6960 err = delete_ref(new_base_branch_ref_name, repo);
6961 if (err)
6962 goto done;
6964 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6965 if (err)
6966 goto done;
6967 err = delete_ref(branch_ref_name, repo);
6968 if (err)
6969 goto done;
6971 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6972 if (err)
6973 goto done;
6974 err = delete_ref(commit_ref_name, repo);
6975 if (err)
6976 goto done;
6978 done:
6979 free(tmp_branch_name);
6980 free(new_base_branch_ref_name);
6981 free(branch_ref_name);
6982 free(commit_ref_name);
6983 return err;
6986 static const struct got_error *
6987 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6988 struct got_object_id *new_commit_id, struct got_repository *repo)
6990 const struct got_error *err;
6991 struct got_reference *ref = NULL;
6992 struct got_object_id *old_commit_id = NULL;
6993 const char *branch_name = NULL;
6994 char *new_id_str = NULL;
6995 char *refname = NULL;
6997 branch_name = got_ref_get_name(branch);
6998 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6999 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7000 branch_name += 11;
7002 err = got_object_id_str(&new_id_str, new_commit_id);
7003 if (err)
7004 return err;
7006 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7007 new_id_str) == -1) {
7008 err = got_error_from_errno("asprintf");
7009 goto done;
7012 err = got_ref_resolve(&old_commit_id, repo, branch);
7013 if (err)
7014 goto done;
7016 err = got_ref_alloc(&ref, refname, old_commit_id);
7017 if (err)
7018 goto done;
7020 err = got_ref_write(ref, repo);
7021 done:
7022 free(new_id_str);
7023 free(refname);
7024 free(old_commit_id);
7025 if (ref)
7026 got_ref_close(ref);
7027 return err;
7030 const struct got_error *
7031 got_worktree_rebase_complete(struct got_worktree *worktree,
7032 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
7033 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
7034 struct got_repository *repo, int create_backup)
7036 const struct got_error *err, *unlockerr, *sync_err;
7037 struct got_object_id *new_head_commit_id = NULL;
7038 char *fileindex_path = NULL;
7040 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7041 if (err)
7042 return err;
7044 if (create_backup) {
7045 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7046 rebased_branch, new_head_commit_id, repo);
7047 if (err)
7048 goto done;
7051 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7052 if (err)
7053 goto done;
7055 err = got_ref_write(rebased_branch, repo);
7056 if (err)
7057 goto done;
7059 err = got_worktree_set_head_ref(worktree, rebased_branch);
7060 if (err)
7061 goto done;
7063 err = delete_rebase_refs(worktree, repo);
7064 if (err)
7065 goto done;
7067 err = get_fileindex_path(&fileindex_path, worktree);
7068 if (err)
7069 goto done;
7070 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7071 sync_err = sync_fileindex(fileindex, fileindex_path);
7072 if (sync_err && err == NULL)
7073 err = sync_err;
7074 done:
7075 got_fileindex_free(fileindex);
7076 free(fileindex_path);
7077 free(new_head_commit_id);
7078 unlockerr = lock_worktree(worktree, LOCK_SH);
7079 if (unlockerr && err == NULL)
7080 err = unlockerr;
7081 return err;
7084 const struct got_error *
7085 got_worktree_rebase_abort(struct got_worktree *worktree,
7086 struct got_fileindex *fileindex, struct got_repository *repo,
7087 struct got_reference *new_base_branch,
7088 got_worktree_checkout_cb progress_cb, void *progress_arg)
7090 const struct got_error *err, *unlockerr, *sync_err;
7091 struct got_reference *resolved = NULL;
7092 struct got_object_id *commit_id = NULL;
7093 struct got_commit_object *commit = NULL;
7094 char *fileindex_path = NULL;
7095 struct revert_file_args rfa;
7096 struct got_object_id *tree_id = NULL;
7098 err = lock_worktree(worktree, LOCK_EX);
7099 if (err)
7100 return err;
7102 err = got_object_open_as_commit(&commit, repo,
7103 worktree->base_commit_id);
7104 if (err)
7105 goto done;
7107 err = got_ref_open(&resolved, repo,
7108 got_ref_get_symref_target(new_base_branch), 0);
7109 if (err)
7110 goto done;
7112 err = got_worktree_set_head_ref(worktree, resolved);
7113 if (err)
7114 goto done;
7117 * XXX commits to the base branch could have happened while
7118 * we were busy rebasing; should we store the original commit ID
7119 * when rebase begins and read it back here?
7121 err = got_ref_resolve(&commit_id, repo, resolved);
7122 if (err)
7123 goto done;
7125 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7126 if (err)
7127 goto done;
7129 err = got_object_id_by_path(&tree_id, repo, commit,
7130 worktree->path_prefix);
7131 if (err)
7132 goto done;
7134 err = delete_rebase_refs(worktree, repo);
7135 if (err)
7136 goto done;
7138 err = get_fileindex_path(&fileindex_path, worktree);
7139 if (err)
7140 goto done;
7142 rfa.worktree = worktree;
7143 rfa.fileindex = fileindex;
7144 rfa.progress_cb = progress_cb;
7145 rfa.progress_arg = progress_arg;
7146 rfa.patch_cb = NULL;
7147 rfa.patch_arg = NULL;
7148 rfa.repo = repo;
7149 rfa.unlink_added_files = 0;
7150 err = worktree_status(worktree, "", fileindex, repo,
7151 revert_file, &rfa, NULL, NULL, 1, 0);
7152 if (err)
7153 goto sync;
7155 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7156 repo, progress_cb, progress_arg, NULL, NULL);
7157 sync:
7158 sync_err = sync_fileindex(fileindex, fileindex_path);
7159 if (sync_err && err == NULL)
7160 err = sync_err;
7161 done:
7162 got_ref_close(resolved);
7163 free(tree_id);
7164 free(commit_id);
7165 if (commit)
7166 got_object_commit_close(commit);
7167 if (fileindex)
7168 got_fileindex_free(fileindex);
7169 free(fileindex_path);
7171 unlockerr = lock_worktree(worktree, LOCK_SH);
7172 if (unlockerr && err == NULL)
7173 err = unlockerr;
7174 return err;
7177 const struct got_error *
7178 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7179 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7180 struct got_fileindex **fileindex, struct got_worktree *worktree,
7181 struct got_repository *repo)
7183 const struct got_error *err = NULL;
7184 char *tmp_branch_name = NULL;
7185 char *branch_ref_name = NULL;
7186 char *base_commit_ref_name = NULL;
7187 char *fileindex_path = NULL;
7188 struct check_rebase_ok_arg ok_arg;
7189 struct got_reference *wt_branch = NULL;
7190 struct got_reference *base_commit_ref = NULL;
7192 *tmp_branch = NULL;
7193 *branch_ref = NULL;
7194 *base_commit_id = NULL;
7195 *fileindex = NULL;
7197 err = lock_worktree(worktree, LOCK_EX);
7198 if (err)
7199 return err;
7201 err = open_fileindex(fileindex, &fileindex_path, worktree);
7202 if (err)
7203 goto done;
7205 ok_arg.worktree = worktree;
7206 ok_arg.repo = repo;
7207 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7208 &ok_arg);
7209 if (err)
7210 goto done;
7212 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7213 if (err)
7214 goto done;
7216 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7217 if (err)
7218 goto done;
7220 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7221 worktree);
7222 if (err)
7223 goto done;
7225 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7226 0);
7227 if (err)
7228 goto done;
7230 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7231 if (err)
7232 goto done;
7234 err = got_ref_write(*branch_ref, repo);
7235 if (err)
7236 goto done;
7238 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7239 worktree->base_commit_id);
7240 if (err)
7241 goto done;
7242 err = got_ref_write(base_commit_ref, repo);
7243 if (err)
7244 goto done;
7245 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7246 if (*base_commit_id == NULL) {
7247 err = got_error_from_errno("got_object_id_dup");
7248 goto done;
7251 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7252 worktree->base_commit_id);
7253 if (err)
7254 goto done;
7255 err = got_ref_write(*tmp_branch, repo);
7256 if (err)
7257 goto done;
7259 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7260 if (err)
7261 goto done;
7262 done:
7263 free(fileindex_path);
7264 free(tmp_branch_name);
7265 free(branch_ref_name);
7266 free(base_commit_ref_name);
7267 if (wt_branch)
7268 got_ref_close(wt_branch);
7269 if (err) {
7270 if (*branch_ref) {
7271 got_ref_close(*branch_ref);
7272 *branch_ref = NULL;
7274 if (*tmp_branch) {
7275 got_ref_close(*tmp_branch);
7276 *tmp_branch = NULL;
7278 free(*base_commit_id);
7279 if (*fileindex) {
7280 got_fileindex_free(*fileindex);
7281 *fileindex = NULL;
7283 lock_worktree(worktree, LOCK_SH);
7285 return err;
7288 const struct got_error *
7289 got_worktree_histedit_postpone(struct got_worktree *worktree,
7290 struct got_fileindex *fileindex)
7292 if (fileindex)
7293 got_fileindex_free(fileindex);
7294 return lock_worktree(worktree, LOCK_SH);
7297 const struct got_error *
7298 got_worktree_histedit_in_progress(int *in_progress,
7299 struct got_worktree *worktree)
7301 const struct got_error *err;
7302 char *tmp_branch_name = NULL;
7304 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7305 if (err)
7306 return err;
7308 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7309 free(tmp_branch_name);
7310 return NULL;
7313 const struct got_error *
7314 got_worktree_histedit_continue(struct got_object_id **commit_id,
7315 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7316 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7317 struct got_worktree *worktree, struct got_repository *repo)
7319 const struct got_error *err;
7320 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7321 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7322 struct got_reference *commit_ref = NULL;
7323 struct got_reference *base_commit_ref = NULL;
7324 char *fileindex_path = NULL;
7325 int have_staged_files = 0;
7327 *commit_id = NULL;
7328 *tmp_branch = NULL;
7329 *base_commit_id = NULL;
7330 *fileindex = NULL;
7332 err = lock_worktree(worktree, LOCK_EX);
7333 if (err)
7334 return err;
7336 err = open_fileindex(fileindex, &fileindex_path, worktree);
7337 if (err)
7338 goto done;
7340 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7341 &have_staged_files);
7342 if (err && err->code != GOT_ERR_CANCELLED)
7343 goto done;
7344 if (have_staged_files) {
7345 err = got_error(GOT_ERR_STAGED_PATHS);
7346 goto done;
7349 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7350 if (err)
7351 goto done;
7353 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7354 if (err)
7355 goto done;
7357 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7358 if (err)
7359 goto done;
7361 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7362 worktree);
7363 if (err)
7364 goto done;
7366 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7367 if (err)
7368 goto done;
7370 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7371 if (err)
7372 goto done;
7373 err = got_ref_resolve(commit_id, repo, commit_ref);
7374 if (err)
7375 goto done;
7377 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7378 if (err)
7379 goto done;
7380 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7381 if (err)
7382 goto done;
7384 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7385 if (err)
7386 goto done;
7387 done:
7388 free(commit_ref_name);
7389 free(branch_ref_name);
7390 free(fileindex_path);
7391 if (commit_ref)
7392 got_ref_close(commit_ref);
7393 if (base_commit_ref)
7394 got_ref_close(base_commit_ref);
7395 if (err) {
7396 free(*commit_id);
7397 *commit_id = NULL;
7398 free(*base_commit_id);
7399 *base_commit_id = NULL;
7400 if (*tmp_branch) {
7401 got_ref_close(*tmp_branch);
7402 *tmp_branch = NULL;
7404 if (*fileindex) {
7405 got_fileindex_free(*fileindex);
7406 *fileindex = NULL;
7408 lock_worktree(worktree, LOCK_EX);
7410 return err;
7413 static const struct got_error *
7414 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7416 const struct got_error *err;
7417 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7418 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7420 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7421 if (err)
7422 goto done;
7423 err = delete_ref(tmp_branch_name, repo);
7424 if (err)
7425 goto done;
7427 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7428 worktree);
7429 if (err)
7430 goto done;
7431 err = delete_ref(base_commit_ref_name, repo);
7432 if (err)
7433 goto done;
7435 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7436 if (err)
7437 goto done;
7438 err = delete_ref(branch_ref_name, repo);
7439 if (err)
7440 goto done;
7442 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7443 if (err)
7444 goto done;
7445 err = delete_ref(commit_ref_name, repo);
7446 if (err)
7447 goto done;
7448 done:
7449 free(tmp_branch_name);
7450 free(base_commit_ref_name);
7451 free(branch_ref_name);
7452 free(commit_ref_name);
7453 return err;
7456 const struct got_error *
7457 got_worktree_histedit_abort(struct got_worktree *worktree,
7458 struct got_fileindex *fileindex, struct got_repository *repo,
7459 struct got_reference *branch, struct got_object_id *base_commit_id,
7460 got_worktree_checkout_cb progress_cb, void *progress_arg)
7462 const struct got_error *err, *unlockerr, *sync_err;
7463 struct got_reference *resolved = NULL;
7464 char *fileindex_path = NULL;
7465 struct got_commit_object *commit = NULL;
7466 struct got_object_id *tree_id = NULL;
7467 struct revert_file_args rfa;
7469 err = lock_worktree(worktree, LOCK_EX);
7470 if (err)
7471 return err;
7473 err = got_object_open_as_commit(&commit, repo,
7474 worktree->base_commit_id);
7475 if (err)
7476 goto done;
7478 err = got_ref_open(&resolved, repo,
7479 got_ref_get_symref_target(branch), 0);
7480 if (err)
7481 goto done;
7483 err = got_worktree_set_head_ref(worktree, resolved);
7484 if (err)
7485 goto done;
7487 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7488 if (err)
7489 goto done;
7491 err = got_object_id_by_path(&tree_id, repo, commit,
7492 worktree->path_prefix);
7493 if (err)
7494 goto done;
7496 err = delete_histedit_refs(worktree, repo);
7497 if (err)
7498 goto done;
7500 err = get_fileindex_path(&fileindex_path, worktree);
7501 if (err)
7502 goto done;
7504 rfa.worktree = worktree;
7505 rfa.fileindex = fileindex;
7506 rfa.progress_cb = progress_cb;
7507 rfa.progress_arg = progress_arg;
7508 rfa.patch_cb = NULL;
7509 rfa.patch_arg = NULL;
7510 rfa.repo = repo;
7511 rfa.unlink_added_files = 0;
7512 err = worktree_status(worktree, "", fileindex, repo,
7513 revert_file, &rfa, NULL, NULL, 1, 0);
7514 if (err)
7515 goto sync;
7517 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7518 repo, progress_cb, progress_arg, NULL, NULL);
7519 sync:
7520 sync_err = sync_fileindex(fileindex, fileindex_path);
7521 if (sync_err && err == NULL)
7522 err = sync_err;
7523 done:
7524 got_ref_close(resolved);
7525 free(tree_id);
7526 free(fileindex_path);
7528 unlockerr = lock_worktree(worktree, LOCK_SH);
7529 if (unlockerr && err == NULL)
7530 err = unlockerr;
7531 return err;
7534 const struct got_error *
7535 got_worktree_histedit_complete(struct got_worktree *worktree,
7536 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7537 struct got_reference *edited_branch, struct got_repository *repo)
7539 const struct got_error *err, *unlockerr, *sync_err;
7540 struct got_object_id *new_head_commit_id = NULL;
7541 struct got_reference *resolved = NULL;
7542 char *fileindex_path = NULL;
7544 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7545 if (err)
7546 return err;
7548 err = got_ref_open(&resolved, repo,
7549 got_ref_get_symref_target(edited_branch), 0);
7550 if (err)
7551 goto done;
7553 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7554 resolved, new_head_commit_id, repo);
7555 if (err)
7556 goto done;
7558 err = got_ref_change_ref(resolved, new_head_commit_id);
7559 if (err)
7560 goto done;
7562 err = got_ref_write(resolved, repo);
7563 if (err)
7564 goto done;
7566 err = got_worktree_set_head_ref(worktree, resolved);
7567 if (err)
7568 goto done;
7570 err = delete_histedit_refs(worktree, repo);
7571 if (err)
7572 goto done;
7574 err = get_fileindex_path(&fileindex_path, worktree);
7575 if (err)
7576 goto done;
7577 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7578 sync_err = sync_fileindex(fileindex, fileindex_path);
7579 if (sync_err && err == NULL)
7580 err = sync_err;
7581 done:
7582 got_fileindex_free(fileindex);
7583 free(fileindex_path);
7584 free(new_head_commit_id);
7585 unlockerr = lock_worktree(worktree, LOCK_SH);
7586 if (unlockerr && err == NULL)
7587 err = unlockerr;
7588 return err;
7591 const struct got_error *
7592 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7593 struct got_object_id *commit_id, struct got_repository *repo)
7595 const struct got_error *err;
7596 char *commit_ref_name;
7598 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7599 if (err)
7600 return err;
7602 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7603 if (err)
7604 goto done;
7606 err = delete_ref(commit_ref_name, repo);
7607 done:
7608 free(commit_ref_name);
7609 return err;
7612 const struct got_error *
7613 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7614 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7615 struct got_worktree *worktree, const char *refname,
7616 struct got_repository *repo)
7618 const struct got_error *err = NULL;
7619 char *fileindex_path = NULL;
7620 struct check_rebase_ok_arg ok_arg;
7622 *fileindex = NULL;
7623 *branch_ref = NULL;
7624 *base_branch_ref = NULL;
7626 err = lock_worktree(worktree, LOCK_EX);
7627 if (err)
7628 return err;
7630 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7631 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7632 "cannot integrate a branch into itself; "
7633 "update -b or different branch name required");
7634 goto done;
7637 err = open_fileindex(fileindex, &fileindex_path, worktree);
7638 if (err)
7639 goto done;
7641 /* Preconditions are the same as for rebase. */
7642 ok_arg.worktree = worktree;
7643 ok_arg.repo = repo;
7644 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7645 &ok_arg);
7646 if (err)
7647 goto done;
7649 err = got_ref_open(branch_ref, repo, refname, 1);
7650 if (err)
7651 goto done;
7653 err = got_ref_open(base_branch_ref, repo,
7654 got_worktree_get_head_ref_name(worktree), 1);
7655 done:
7656 if (err) {
7657 if (*branch_ref) {
7658 got_ref_close(*branch_ref);
7659 *branch_ref = NULL;
7661 if (*base_branch_ref) {
7662 got_ref_close(*base_branch_ref);
7663 *base_branch_ref = NULL;
7665 if (*fileindex) {
7666 got_fileindex_free(*fileindex);
7667 *fileindex = NULL;
7669 lock_worktree(worktree, LOCK_SH);
7671 return err;
7674 const struct got_error *
7675 got_worktree_integrate_continue(struct got_worktree *worktree,
7676 struct got_fileindex *fileindex, struct got_repository *repo,
7677 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7678 got_worktree_checkout_cb progress_cb, void *progress_arg,
7679 got_cancel_cb cancel_cb, void *cancel_arg)
7681 const struct got_error *err = NULL, *sync_err, *unlockerr;
7682 char *fileindex_path = NULL;
7683 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7684 struct got_commit_object *commit = NULL;
7686 err = get_fileindex_path(&fileindex_path, worktree);
7687 if (err)
7688 goto done;
7690 err = got_ref_resolve(&commit_id, repo, branch_ref);
7691 if (err)
7692 goto done;
7694 err = got_object_open_as_commit(&commit, repo, commit_id);
7695 if (err)
7696 goto done;
7698 err = got_object_id_by_path(&tree_id, repo, commit,
7699 worktree->path_prefix);
7700 if (err)
7701 goto done;
7703 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7704 if (err)
7705 goto done;
7707 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7708 progress_cb, progress_arg, cancel_cb, cancel_arg);
7709 if (err)
7710 goto sync;
7712 err = got_ref_change_ref(base_branch_ref, commit_id);
7713 if (err)
7714 goto sync;
7716 err = got_ref_write(base_branch_ref, repo);
7717 if (err)
7718 goto sync;
7720 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7721 sync:
7722 sync_err = sync_fileindex(fileindex, fileindex_path);
7723 if (sync_err && err == NULL)
7724 err = sync_err;
7726 done:
7727 unlockerr = got_ref_unlock(branch_ref);
7728 if (unlockerr && err == NULL)
7729 err = unlockerr;
7730 got_ref_close(branch_ref);
7732 unlockerr = got_ref_unlock(base_branch_ref);
7733 if (unlockerr && err == NULL)
7734 err = unlockerr;
7735 got_ref_close(base_branch_ref);
7737 got_fileindex_free(fileindex);
7738 free(fileindex_path);
7739 free(tree_id);
7740 if (commit)
7741 got_object_commit_close(commit);
7743 unlockerr = lock_worktree(worktree, LOCK_SH);
7744 if (unlockerr && err == NULL)
7745 err = unlockerr;
7746 return err;
7749 const struct got_error *
7750 got_worktree_integrate_abort(struct got_worktree *worktree,
7751 struct got_fileindex *fileindex, struct got_repository *repo,
7752 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7754 const struct got_error *err = NULL, *unlockerr = NULL;
7756 got_fileindex_free(fileindex);
7758 err = lock_worktree(worktree, LOCK_SH);
7760 unlockerr = got_ref_unlock(branch_ref);
7761 if (unlockerr && err == NULL)
7762 err = unlockerr;
7763 got_ref_close(branch_ref);
7765 unlockerr = got_ref_unlock(base_branch_ref);
7766 if (unlockerr && err == NULL)
7767 err = unlockerr;
7768 got_ref_close(base_branch_ref);
7770 return err;
7773 const struct got_error *
7774 got_worktree_merge_postpone(struct got_worktree *worktree,
7775 struct got_fileindex *fileindex)
7777 const struct got_error *err, *sync_err;
7778 char *fileindex_path = NULL;
7780 err = get_fileindex_path(&fileindex_path, worktree);
7781 if (err)
7782 goto done;
7784 sync_err = sync_fileindex(fileindex, fileindex_path);
7786 err = lock_worktree(worktree, LOCK_SH);
7787 if (sync_err && err == NULL)
7788 err = sync_err;
7789 done:
7790 got_fileindex_free(fileindex);
7791 free(fileindex_path);
7792 return err;
7795 static const struct got_error *
7796 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7798 const struct got_error *err;
7799 char *branch_refname = NULL, *commit_refname = NULL;
7801 err = get_merge_branch_ref_name(&branch_refname, worktree);
7802 if (err)
7803 goto done;
7804 err = delete_ref(branch_refname, repo);
7805 if (err)
7806 goto done;
7808 err = get_merge_commit_ref_name(&commit_refname, worktree);
7809 if (err)
7810 goto done;
7811 err = delete_ref(commit_refname, repo);
7812 if (err)
7813 goto done;
7815 done:
7816 free(branch_refname);
7817 free(commit_refname);
7818 return err;
7821 struct merge_commit_msg_arg {
7822 struct got_worktree *worktree;
7823 const char *branch_name;
7826 static const struct got_error *
7827 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7828 const char *diff_path, char **logmsg, void *arg)
7830 struct merge_commit_msg_arg *a = arg;
7832 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7833 got_worktree_get_head_ref_name(a->worktree)) == -1)
7834 return got_error_from_errno("asprintf");
7836 return NULL;
7840 const struct got_error *
7841 got_worktree_merge_branch(struct got_worktree *worktree,
7842 struct got_fileindex *fileindex,
7843 struct got_object_id *yca_commit_id,
7844 struct got_object_id *branch_tip,
7845 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7846 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7848 const struct got_error *err;
7849 char *fileindex_path = NULL;
7851 err = get_fileindex_path(&fileindex_path, worktree);
7852 if (err)
7853 goto done;
7855 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7856 worktree);
7857 if (err)
7858 goto done;
7860 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7861 branch_tip, repo, progress_cb, progress_arg,
7862 cancel_cb, cancel_arg);
7863 done:
7864 free(fileindex_path);
7865 return err;
7868 const struct got_error *
7869 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7870 struct got_worktree *worktree, struct got_fileindex *fileindex,
7871 const char *author, const char *committer, int allow_bad_symlinks,
7872 struct got_object_id *branch_tip, const char *branch_name,
7873 struct got_repository *repo,
7874 got_worktree_status_cb status_cb, void *status_arg)
7877 const struct got_error *err = NULL, *sync_err;
7878 struct got_pathlist_head commitable_paths;
7879 struct collect_commitables_arg cc_arg;
7880 struct got_pathlist_entry *pe;
7881 struct got_reference *head_ref = NULL;
7882 struct got_object_id *head_commit_id = NULL;
7883 int have_staged_files = 0;
7884 struct merge_commit_msg_arg mcm_arg;
7885 char *fileindex_path = NULL;
7887 memset(&cc_arg, 0, sizeof(cc_arg));
7888 *new_commit_id = NULL;
7890 TAILQ_INIT(&commitable_paths);
7892 err = get_fileindex_path(&fileindex_path, worktree);
7893 if (err)
7894 goto done;
7896 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7897 if (err)
7898 goto done;
7900 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7901 if (err)
7902 goto done;
7904 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7905 &have_staged_files);
7906 if (err && err->code != GOT_ERR_CANCELLED)
7907 goto done;
7908 if (have_staged_files) {
7909 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7910 goto done;
7913 cc_arg.commitable_paths = &commitable_paths;
7914 cc_arg.worktree = worktree;
7915 cc_arg.fileindex = fileindex;
7916 cc_arg.repo = repo;
7917 cc_arg.have_staged_files = have_staged_files;
7918 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7919 err = worktree_status(worktree, "", fileindex, repo,
7920 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7921 if (err)
7922 goto done;
7924 if (TAILQ_EMPTY(&commitable_paths)) {
7925 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7926 "merge of %s cannot proceed", branch_name);
7927 goto done;
7930 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7931 struct got_commitable *ct = pe->data;
7932 const char *ct_path = ct->in_repo_path;
7934 while (ct_path[0] == '/')
7935 ct_path++;
7936 err = check_out_of_date(ct_path, ct->status,
7937 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7938 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7939 if (err)
7940 goto done;
7944 mcm_arg.worktree = worktree;
7945 mcm_arg.branch_name = branch_name;
7946 err = commit_worktree(new_commit_id, &commitable_paths,
7947 head_commit_id, branch_tip, worktree, author, committer, NULL,
7948 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7949 if (err)
7950 goto done;
7952 err = update_fileindex_after_commit(worktree, &commitable_paths,
7953 *new_commit_id, fileindex, have_staged_files);
7954 sync_err = sync_fileindex(fileindex, fileindex_path);
7955 if (sync_err && err == NULL)
7956 err = sync_err;
7957 done:
7958 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7959 struct got_commitable *ct = pe->data;
7960 free_commitable(ct);
7962 got_pathlist_free(&commitable_paths);
7963 free(fileindex_path);
7964 return err;
7967 const struct got_error *
7968 got_worktree_merge_complete(struct got_worktree *worktree,
7969 struct got_fileindex *fileindex, struct got_repository *repo)
7971 const struct got_error *err, *unlockerr, *sync_err;
7972 char *fileindex_path = NULL;
7974 err = delete_merge_refs(worktree, repo);
7975 if (err)
7976 goto done;
7978 err = get_fileindex_path(&fileindex_path, worktree);
7979 if (err)
7980 goto done;
7981 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7982 sync_err = sync_fileindex(fileindex, fileindex_path);
7983 if (sync_err && err == NULL)
7984 err = sync_err;
7985 done:
7986 got_fileindex_free(fileindex);
7987 free(fileindex_path);
7988 unlockerr = lock_worktree(worktree, LOCK_SH);
7989 if (unlockerr && err == NULL)
7990 err = unlockerr;
7991 return err;
7994 const struct got_error *
7995 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7996 struct got_repository *repo)
7998 const struct got_error *err;
7999 char *branch_refname = NULL;
8000 struct got_reference *branch_ref = NULL;
8002 *in_progress = 0;
8004 err = get_merge_branch_ref_name(&branch_refname, worktree);
8005 if (err)
8006 return err;
8007 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8008 free(branch_refname);
8009 if (err) {
8010 if (err->code != GOT_ERR_NOT_REF)
8011 return err;
8012 } else
8013 *in_progress = 1;
8015 return NULL;
8018 const struct got_error *got_worktree_merge_prepare(
8019 struct got_fileindex **fileindex, struct got_worktree *worktree,
8020 struct got_reference *branch, struct got_repository *repo)
8022 const struct got_error *err = NULL;
8023 char *fileindex_path = NULL;
8024 char *branch_refname = NULL, *commit_refname = NULL;
8025 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8026 struct got_reference *commit_ref = NULL;
8027 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8028 struct check_rebase_ok_arg ok_arg;
8030 *fileindex = NULL;
8032 err = lock_worktree(worktree, LOCK_EX);
8033 if (err)
8034 return err;
8036 err = open_fileindex(fileindex, &fileindex_path, worktree);
8037 if (err)
8038 goto done;
8040 /* Preconditions are the same as for rebase. */
8041 ok_arg.worktree = worktree;
8042 ok_arg.repo = repo;
8043 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8044 &ok_arg);
8045 if (err)
8046 goto done;
8048 err = get_merge_branch_ref_name(&branch_refname, worktree);
8049 if (err)
8050 return err;
8052 err = get_merge_commit_ref_name(&commit_refname, worktree);
8053 if (err)
8054 return err;
8056 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8057 0);
8058 if (err)
8059 goto done;
8061 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8062 if (err)
8063 goto done;
8065 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8066 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8067 goto done;
8070 err = got_ref_resolve(&branch_tip, repo, branch);
8071 if (err)
8072 goto done;
8074 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8075 if (err)
8076 goto done;
8077 err = got_ref_write(branch_ref, repo);
8078 if (err)
8079 goto done;
8081 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8082 if (err)
8083 goto done;
8084 err = got_ref_write(commit_ref, repo);
8085 if (err)
8086 goto done;
8088 done:
8089 free(branch_refname);
8090 free(commit_refname);
8091 free(fileindex_path);
8092 if (branch_ref)
8093 got_ref_close(branch_ref);
8094 if (commit_ref)
8095 got_ref_close(commit_ref);
8096 if (wt_branch)
8097 got_ref_close(wt_branch);
8098 free(wt_branch_tip);
8099 if (err) {
8100 if (*fileindex) {
8101 got_fileindex_free(*fileindex);
8102 *fileindex = NULL;
8104 lock_worktree(worktree, LOCK_SH);
8106 return err;
8109 const struct got_error *
8110 got_worktree_merge_continue(char **branch_name,
8111 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8112 struct got_worktree *worktree, struct got_repository *repo)
8114 const struct got_error *err;
8115 char *commit_refname = NULL, *branch_refname = NULL;
8116 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8117 char *fileindex_path = NULL;
8118 int have_staged_files = 0;
8120 *branch_name = NULL;
8121 *branch_tip = NULL;
8122 *fileindex = NULL;
8124 err = lock_worktree(worktree, LOCK_EX);
8125 if (err)
8126 return err;
8128 err = open_fileindex(fileindex, &fileindex_path, worktree);
8129 if (err)
8130 goto done;
8132 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8133 &have_staged_files);
8134 if (err && err->code != GOT_ERR_CANCELLED)
8135 goto done;
8136 if (have_staged_files) {
8137 err = got_error(GOT_ERR_STAGED_PATHS);
8138 goto done;
8141 err = get_merge_branch_ref_name(&branch_refname, worktree);
8142 if (err)
8143 goto done;
8145 err = get_merge_commit_ref_name(&commit_refname, worktree);
8146 if (err)
8147 goto done;
8149 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8150 if (err)
8151 goto done;
8153 if (!got_ref_is_symbolic(branch_ref)) {
8154 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8155 "%s is not a symbolic reference",
8156 got_ref_get_name(branch_ref));
8157 goto done;
8159 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8160 if (*branch_name == NULL) {
8161 err = got_error_from_errno("strdup");
8162 goto done;
8165 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8166 if (err)
8167 goto done;
8169 err = got_ref_resolve(branch_tip, repo, commit_ref);
8170 if (err)
8171 goto done;
8172 done:
8173 free(commit_refname);
8174 free(branch_refname);
8175 free(fileindex_path);
8176 if (commit_ref)
8177 got_ref_close(commit_ref);
8178 if (branch_ref)
8179 got_ref_close(branch_ref);
8180 if (err) {
8181 if (*branch_name) {
8182 free(*branch_name);
8183 *branch_name = NULL;
8185 free(*branch_tip);
8186 *branch_tip = NULL;
8187 if (*fileindex) {
8188 got_fileindex_free(*fileindex);
8189 *fileindex = NULL;
8191 lock_worktree(worktree, LOCK_SH);
8193 return err;
8196 const struct got_error *
8197 got_worktree_merge_abort(struct got_worktree *worktree,
8198 struct got_fileindex *fileindex, struct got_repository *repo,
8199 got_worktree_checkout_cb progress_cb, void *progress_arg)
8201 const struct got_error *err, *unlockerr, *sync_err;
8202 struct got_object_id *commit_id = NULL;
8203 struct got_commit_object *commit = NULL;
8204 char *fileindex_path = NULL;
8205 struct revert_file_args rfa;
8206 struct got_object_id *tree_id = NULL;
8208 err = got_object_open_as_commit(&commit, repo,
8209 worktree->base_commit_id);
8210 if (err)
8211 goto done;
8213 err = got_object_id_by_path(&tree_id, repo, commit,
8214 worktree->path_prefix);
8215 if (err)
8216 goto done;
8218 err = delete_merge_refs(worktree, repo);
8219 if (err)
8220 goto done;
8222 err = get_fileindex_path(&fileindex_path, worktree);
8223 if (err)
8224 goto done;
8226 rfa.worktree = worktree;
8227 rfa.fileindex = fileindex;
8228 rfa.progress_cb = progress_cb;
8229 rfa.progress_arg = progress_arg;
8230 rfa.patch_cb = NULL;
8231 rfa.patch_arg = NULL;
8232 rfa.repo = repo;
8233 rfa.unlink_added_files = 1;
8234 err = worktree_status(worktree, "", fileindex, repo,
8235 revert_file, &rfa, NULL, NULL, 1, 0);
8236 if (err)
8237 goto sync;
8239 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8240 repo, progress_cb, progress_arg, NULL, NULL);
8241 sync:
8242 sync_err = sync_fileindex(fileindex, fileindex_path);
8243 if (sync_err && err == NULL)
8244 err = sync_err;
8245 done:
8246 free(tree_id);
8247 free(commit_id);
8248 if (commit)
8249 got_object_commit_close(commit);
8250 if (fileindex)
8251 got_fileindex_free(fileindex);
8252 free(fileindex_path);
8254 unlockerr = lock_worktree(worktree, LOCK_SH);
8255 if (unlockerr && err == NULL)
8256 err = unlockerr;
8257 return err;
8260 struct check_stage_ok_arg {
8261 struct got_object_id *head_commit_id;
8262 struct got_worktree *worktree;
8263 struct got_fileindex *fileindex;
8264 struct got_repository *repo;
8265 int have_changes;
8268 static const struct got_error *
8269 check_stage_ok(void *arg, unsigned char status,
8270 unsigned char staged_status, const char *relpath,
8271 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8272 struct got_object_id *commit_id, int dirfd, const char *de_name)
8274 struct check_stage_ok_arg *a = arg;
8275 const struct got_error *err = NULL;
8276 struct got_fileindex_entry *ie;
8277 struct got_object_id base_commit_id;
8278 struct got_object_id *base_commit_idp = NULL;
8279 char *in_repo_path = NULL, *p;
8281 if (status == GOT_STATUS_UNVERSIONED ||
8282 status == GOT_STATUS_NO_CHANGE)
8283 return NULL;
8284 if (status == GOT_STATUS_NONEXISTENT)
8285 return got_error_set_errno(ENOENT, relpath);
8287 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8288 if (ie == NULL)
8289 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8291 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8292 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8293 relpath) == -1)
8294 return got_error_from_errno("asprintf");
8296 if (got_fileindex_entry_has_commit(ie)) {
8297 memcpy(base_commit_id.sha1, ie->commit_sha1,
8298 SHA1_DIGEST_LENGTH);
8299 base_commit_idp = &base_commit_id;
8302 if (status == GOT_STATUS_CONFLICT) {
8303 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8304 goto done;
8305 } else if (status != GOT_STATUS_ADD &&
8306 status != GOT_STATUS_MODIFY &&
8307 status != GOT_STATUS_DELETE) {
8308 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8309 goto done;
8312 a->have_changes = 1;
8314 p = in_repo_path;
8315 while (p[0] == '/')
8316 p++;
8317 err = check_out_of_date(p, status, staged_status,
8318 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8319 GOT_ERR_STAGE_OUT_OF_DATE);
8320 done:
8321 free(in_repo_path);
8322 return err;
8325 struct stage_path_arg {
8326 struct got_worktree *worktree;
8327 struct got_fileindex *fileindex;
8328 struct got_repository *repo;
8329 got_worktree_status_cb status_cb;
8330 void *status_arg;
8331 got_worktree_patch_cb patch_cb;
8332 void *patch_arg;
8333 int staged_something;
8334 int allow_bad_symlinks;
8337 static const struct got_error *
8338 stage_path(void *arg, unsigned char status,
8339 unsigned char staged_status, const char *relpath,
8340 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8341 struct got_object_id *commit_id, int dirfd, const char *de_name)
8343 struct stage_path_arg *a = arg;
8344 const struct got_error *err = NULL;
8345 struct got_fileindex_entry *ie;
8346 char *ondisk_path = NULL, *path_content = NULL;
8347 uint32_t stage;
8348 struct got_object_id *new_staged_blob_id = NULL;
8349 struct stat sb;
8351 if (status == GOT_STATUS_UNVERSIONED)
8352 return NULL;
8354 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8355 if (ie == NULL)
8356 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8358 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8359 relpath)== -1)
8360 return got_error_from_errno("asprintf");
8362 switch (status) {
8363 case GOT_STATUS_ADD:
8364 case GOT_STATUS_MODIFY:
8365 /* XXX could sb.st_mode be passed in by our caller? */
8366 if (lstat(ondisk_path, &sb) == -1) {
8367 err = got_error_from_errno2("lstat", ondisk_path);
8368 break;
8370 if (a->patch_cb) {
8371 if (status == GOT_STATUS_ADD) {
8372 int choice = GOT_PATCH_CHOICE_NONE;
8373 err = (*a->patch_cb)(&choice, a->patch_arg,
8374 status, ie->path, NULL, 1, 1);
8375 if (err)
8376 break;
8377 if (choice != GOT_PATCH_CHOICE_YES)
8378 break;
8379 } else {
8380 err = create_patched_content(&path_content, 0,
8381 staged_blob_id ? staged_blob_id : blob_id,
8382 ondisk_path, dirfd, de_name, ie->path,
8383 a->repo, a->patch_cb, a->patch_arg);
8384 if (err || path_content == NULL)
8385 break;
8388 err = got_object_blob_create(&new_staged_blob_id,
8389 path_content ? path_content : ondisk_path, a->repo);
8390 if (err)
8391 break;
8392 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8393 SHA1_DIGEST_LENGTH);
8394 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8395 stage = GOT_FILEIDX_STAGE_ADD;
8396 else
8397 stage = GOT_FILEIDX_STAGE_MODIFY;
8398 got_fileindex_entry_stage_set(ie, stage);
8399 if (S_ISLNK(sb.st_mode)) {
8400 int is_bad_symlink = 0;
8401 if (!a->allow_bad_symlinks) {
8402 char target_path[PATH_MAX];
8403 ssize_t target_len;
8404 target_len = readlink(ondisk_path, target_path,
8405 sizeof(target_path));
8406 if (target_len == -1) {
8407 err = got_error_from_errno2("readlink",
8408 ondisk_path);
8409 break;
8411 err = is_bad_symlink_target(&is_bad_symlink,
8412 target_path, target_len, ondisk_path,
8413 a->worktree->root_path);
8414 if (err)
8415 break;
8416 if (is_bad_symlink) {
8417 err = got_error_path(ondisk_path,
8418 GOT_ERR_BAD_SYMLINK);
8419 break;
8422 if (is_bad_symlink)
8423 got_fileindex_entry_staged_filetype_set(ie,
8424 GOT_FILEIDX_MODE_BAD_SYMLINK);
8425 else
8426 got_fileindex_entry_staged_filetype_set(ie,
8427 GOT_FILEIDX_MODE_SYMLINK);
8428 } else {
8429 got_fileindex_entry_staged_filetype_set(ie,
8430 GOT_FILEIDX_MODE_REGULAR_FILE);
8432 a->staged_something = 1;
8433 if (a->status_cb == NULL)
8434 break;
8435 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8436 get_staged_status(ie), relpath, blob_id,
8437 new_staged_blob_id, NULL, dirfd, de_name);
8438 if (err)
8439 break;
8441 * When staging the reverse of the staged diff,
8442 * implicitly unstage the file.
8444 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8445 sizeof(ie->blob_sha1)) == 0) {
8446 got_fileindex_entry_stage_set(ie,
8447 GOT_FILEIDX_STAGE_NONE);
8449 break;
8450 case GOT_STATUS_DELETE:
8451 if (staged_status == GOT_STATUS_DELETE)
8452 break;
8453 if (a->patch_cb) {
8454 int choice = GOT_PATCH_CHOICE_NONE;
8455 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8456 ie->path, NULL, 1, 1);
8457 if (err)
8458 break;
8459 if (choice == GOT_PATCH_CHOICE_NO)
8460 break;
8461 if (choice != GOT_PATCH_CHOICE_YES) {
8462 err = got_error(GOT_ERR_PATCH_CHOICE);
8463 break;
8466 stage = GOT_FILEIDX_STAGE_DELETE;
8467 got_fileindex_entry_stage_set(ie, stage);
8468 a->staged_something = 1;
8469 if (a->status_cb == NULL)
8470 break;
8471 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8472 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8473 de_name);
8474 break;
8475 case GOT_STATUS_NO_CHANGE:
8476 break;
8477 case GOT_STATUS_CONFLICT:
8478 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8479 break;
8480 case GOT_STATUS_NONEXISTENT:
8481 err = got_error_set_errno(ENOENT, relpath);
8482 break;
8483 default:
8484 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8485 break;
8488 if (path_content && unlink(path_content) == -1 && err == NULL)
8489 err = got_error_from_errno2("unlink", path_content);
8490 free(path_content);
8491 free(ondisk_path);
8492 free(new_staged_blob_id);
8493 return err;
8496 const struct got_error *
8497 got_worktree_stage(struct got_worktree *worktree,
8498 struct got_pathlist_head *paths,
8499 got_worktree_status_cb status_cb, void *status_arg,
8500 got_worktree_patch_cb patch_cb, void *patch_arg,
8501 int allow_bad_symlinks, struct got_repository *repo)
8503 const struct got_error *err = NULL, *sync_err, *unlockerr;
8504 struct got_pathlist_entry *pe;
8505 struct got_fileindex *fileindex = NULL;
8506 char *fileindex_path = NULL;
8507 struct got_reference *head_ref = NULL;
8508 struct got_object_id *head_commit_id = NULL;
8509 struct check_stage_ok_arg oka;
8510 struct stage_path_arg spa;
8512 err = lock_worktree(worktree, LOCK_EX);
8513 if (err)
8514 return err;
8516 err = got_ref_open(&head_ref, repo,
8517 got_worktree_get_head_ref_name(worktree), 0);
8518 if (err)
8519 goto done;
8520 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8521 if (err)
8522 goto done;
8523 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8524 if (err)
8525 goto done;
8527 /* Check pre-conditions before staging anything. */
8528 oka.head_commit_id = head_commit_id;
8529 oka.worktree = worktree;
8530 oka.fileindex = fileindex;
8531 oka.repo = repo;
8532 oka.have_changes = 0;
8533 TAILQ_FOREACH(pe, paths, entry) {
8534 err = worktree_status(worktree, pe->path, fileindex, repo,
8535 check_stage_ok, &oka, NULL, NULL, 1, 0);
8536 if (err)
8537 goto done;
8539 if (!oka.have_changes) {
8540 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8541 goto done;
8544 spa.worktree = worktree;
8545 spa.fileindex = fileindex;
8546 spa.repo = repo;
8547 spa.patch_cb = patch_cb;
8548 spa.patch_arg = patch_arg;
8549 spa.status_cb = status_cb;
8550 spa.status_arg = status_arg;
8551 spa.staged_something = 0;
8552 spa.allow_bad_symlinks = allow_bad_symlinks;
8553 TAILQ_FOREACH(pe, paths, entry) {
8554 err = worktree_status(worktree, pe->path, fileindex, repo,
8555 stage_path, &spa, NULL, NULL, 1, 0);
8556 if (err)
8557 goto done;
8559 if (!spa.staged_something) {
8560 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8561 goto done;
8564 sync_err = sync_fileindex(fileindex, fileindex_path);
8565 if (sync_err && err == NULL)
8566 err = sync_err;
8567 done:
8568 if (head_ref)
8569 got_ref_close(head_ref);
8570 free(head_commit_id);
8571 free(fileindex_path);
8572 if (fileindex)
8573 got_fileindex_free(fileindex);
8574 unlockerr = lock_worktree(worktree, LOCK_SH);
8575 if (unlockerr && err == NULL)
8576 err = unlockerr;
8577 return err;
8580 struct unstage_path_arg {
8581 struct got_worktree *worktree;
8582 struct got_fileindex *fileindex;
8583 struct got_repository *repo;
8584 got_worktree_checkout_cb progress_cb;
8585 void *progress_arg;
8586 got_worktree_patch_cb patch_cb;
8587 void *patch_arg;
8590 static const struct got_error *
8591 create_unstaged_content(char **path_unstaged_content,
8592 char **path_new_staged_content, struct got_object_id *blob_id,
8593 struct got_object_id *staged_blob_id, const char *relpath,
8594 struct got_repository *repo,
8595 got_worktree_patch_cb patch_cb, void *patch_arg)
8597 const struct got_error *err, *free_err;
8598 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8599 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8600 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8601 struct got_diffreg_result *diffreg_result = NULL;
8602 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8603 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8604 int fd1 = -1, fd2 = -1;
8606 *path_unstaged_content = NULL;
8607 *path_new_staged_content = NULL;
8609 err = got_object_id_str(&label1, blob_id);
8610 if (err)
8611 return err;
8613 fd1 = got_opentempfd();
8614 if (fd1 == -1) {
8615 err = got_error_from_errno("got_opentempfd");
8616 goto done;
8618 fd2 = got_opentempfd();
8619 if (fd2 == -1) {
8620 err = got_error_from_errno("got_opentempfd");
8621 goto done;
8624 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8625 if (err)
8626 goto done;
8628 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8629 if (err)
8630 goto done;
8632 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8633 if (err)
8634 goto done;
8636 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8637 fd2);
8638 if (err)
8639 goto done;
8641 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8642 if (err)
8643 goto done;
8645 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8646 if (err)
8647 goto done;
8649 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8650 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8651 if (err)
8652 goto done;
8654 err = got_opentemp_named(path_unstaged_content, &outfile,
8655 "got-unstaged-content", "");
8656 if (err)
8657 goto done;
8658 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8659 "got-new-staged-content", "");
8660 if (err)
8661 goto done;
8663 if (fseek(f1, 0L, SEEK_SET) == -1) {
8664 err = got_ferror(f1, GOT_ERR_IO);
8665 goto done;
8667 if (fseek(f2, 0L, SEEK_SET) == -1) {
8668 err = got_ferror(f2, GOT_ERR_IO);
8669 goto done;
8671 /* Count the number of actual changes in the diff result. */
8672 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8673 struct diff_chunk_context cc = {};
8674 diff_chunk_context_load_change(&cc, &nchunks_used,
8675 diffreg_result->result, n, 0);
8676 nchanges++;
8678 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8679 int choice;
8680 err = apply_or_reject_change(&choice, &nchunks_used,
8681 diffreg_result->result, n, relpath, f1, f2,
8682 &line_cur1, &line_cur2,
8683 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8684 if (err)
8685 goto done;
8686 if (choice == GOT_PATCH_CHOICE_YES)
8687 have_content = 1;
8688 else
8689 have_rejected_content = 1;
8690 if (choice == GOT_PATCH_CHOICE_QUIT)
8691 break;
8693 if (have_content || have_rejected_content)
8694 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8695 outfile, rejectfile);
8696 done:
8697 free(label1);
8698 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8699 err = got_error_from_errno("close");
8700 if (blob)
8701 got_object_blob_close(blob);
8702 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8703 err = got_error_from_errno("close");
8704 if (staged_blob)
8705 got_object_blob_close(staged_blob);
8706 free_err = got_diffreg_result_free(diffreg_result);
8707 if (free_err && err == NULL)
8708 err = free_err;
8709 if (f1 && fclose(f1) == EOF && err == NULL)
8710 err = got_error_from_errno2("fclose", path1);
8711 if (f2 && fclose(f2) == EOF && err == NULL)
8712 err = got_error_from_errno2("fclose", path2);
8713 if (outfile && fclose(outfile) == EOF && err == NULL)
8714 err = got_error_from_errno2("fclose", *path_unstaged_content);
8715 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8716 err = got_error_from_errno2("fclose", *path_new_staged_content);
8717 if (path1 && unlink(path1) == -1 && err == NULL)
8718 err = got_error_from_errno2("unlink", path1);
8719 if (path2 && unlink(path2) == -1 && err == NULL)
8720 err = got_error_from_errno2("unlink", path2);
8721 if (err || !have_content) {
8722 if (*path_unstaged_content &&
8723 unlink(*path_unstaged_content) == -1 && err == NULL)
8724 err = got_error_from_errno2("unlink",
8725 *path_unstaged_content);
8726 free(*path_unstaged_content);
8727 *path_unstaged_content = NULL;
8729 if (err || !have_content || !have_rejected_content) {
8730 if (*path_new_staged_content &&
8731 unlink(*path_new_staged_content) == -1 && err == NULL)
8732 err = got_error_from_errno2("unlink",
8733 *path_new_staged_content);
8734 free(*path_new_staged_content);
8735 *path_new_staged_content = NULL;
8737 free(path1);
8738 free(path2);
8739 return err;
8742 static const struct got_error *
8743 unstage_hunks(struct got_object_id *staged_blob_id,
8744 struct got_blob_object *blob_base,
8745 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8746 const char *ondisk_path, const char *label_orig,
8747 struct got_worktree *worktree, struct got_repository *repo,
8748 got_worktree_patch_cb patch_cb, void *patch_arg,
8749 got_worktree_checkout_cb progress_cb, void *progress_arg)
8751 const struct got_error *err = NULL;
8752 char *path_unstaged_content = NULL;
8753 char *path_new_staged_content = NULL;
8754 char *parent = NULL, *base_path = NULL;
8755 char *blob_base_path = NULL;
8756 struct got_object_id *new_staged_blob_id = NULL;
8757 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8758 struct stat sb;
8760 err = create_unstaged_content(&path_unstaged_content,
8761 &path_new_staged_content, blob_id, staged_blob_id,
8762 ie->path, repo, patch_cb, patch_arg);
8763 if (err)
8764 return err;
8766 if (path_unstaged_content == NULL)
8767 return NULL;
8769 if (path_new_staged_content) {
8770 err = got_object_blob_create(&new_staged_blob_id,
8771 path_new_staged_content, repo);
8772 if (err)
8773 goto done;
8776 f = fopen(path_unstaged_content, "re");
8777 if (f == NULL) {
8778 err = got_error_from_errno2("fopen",
8779 path_unstaged_content);
8780 goto done;
8782 if (fstat(fileno(f), &sb) == -1) {
8783 err = got_error_from_errno2("fstat", path_unstaged_content);
8784 goto done;
8786 if (got_fileindex_entry_staged_filetype_get(ie) ==
8787 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8788 char link_target[PATH_MAX];
8789 size_t r;
8790 r = fread(link_target, 1, sizeof(link_target), f);
8791 if (r == 0 && ferror(f)) {
8792 err = got_error_from_errno("fread");
8793 goto done;
8795 if (r >= sizeof(link_target)) { /* should not happen */
8796 err = got_error(GOT_ERR_NO_SPACE);
8797 goto done;
8799 link_target[r] = '\0';
8800 err = merge_symlink(worktree, blob_base,
8801 ondisk_path, ie->path, label_orig, link_target,
8802 worktree->base_commit_id, repo, progress_cb,
8803 progress_arg);
8804 } else {
8805 int local_changes_subsumed;
8807 err = got_path_dirname(&parent, ondisk_path);
8808 if (err)
8809 return err;
8811 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8812 parent) == -1) {
8813 err = got_error_from_errno("asprintf");
8814 base_path = NULL;
8815 goto done;
8818 err = got_opentemp_named(&blob_base_path, &f_base,
8819 base_path, "");
8820 if (err)
8821 goto done;
8822 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8823 blob_base);
8824 if (err)
8825 goto done;
8828 * In order the run a 3-way merge with a symlink we copy the symlink's
8829 * target path into a temporary file and use that file with diff3.
8831 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8832 err = dump_symlink_target_path_to_file(&f_deriv2,
8833 ondisk_path);
8834 if (err)
8835 goto done;
8836 } else {
8837 int fd;
8838 fd = open(ondisk_path,
8839 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8840 if (fd == -1) {
8841 err = got_error_from_errno2("open", ondisk_path);
8842 goto done;
8844 f_deriv2 = fdopen(fd, "r");
8845 if (f_deriv2 == NULL) {
8846 err = got_error_from_errno2("fdopen", ondisk_path);
8847 close(fd);
8848 goto done;
8852 err = merge_file(&local_changes_subsumed, worktree,
8853 f_base, f, f_deriv2, ondisk_path, ie->path,
8854 got_fileindex_perms_to_st(ie),
8855 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8856 repo, progress_cb, progress_arg);
8858 if (err)
8859 goto done;
8861 if (new_staged_blob_id) {
8862 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8863 SHA1_DIGEST_LENGTH);
8864 } else {
8865 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8866 got_fileindex_entry_staged_filetype_set(ie, 0);
8868 done:
8869 free(new_staged_blob_id);
8870 if (path_unstaged_content &&
8871 unlink(path_unstaged_content) == -1 && err == NULL)
8872 err = got_error_from_errno2("unlink", path_unstaged_content);
8873 if (path_new_staged_content &&
8874 unlink(path_new_staged_content) == -1 && err == NULL)
8875 err = got_error_from_errno2("unlink", path_new_staged_content);
8876 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8877 err = got_error_from_errno2("unlink", blob_base_path);
8878 if (f_base && fclose(f_base) == EOF && err == NULL)
8879 err = got_error_from_errno2("fclose", path_unstaged_content);
8880 if (f && fclose(f) == EOF && err == NULL)
8881 err = got_error_from_errno2("fclose", path_unstaged_content);
8882 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8883 err = got_error_from_errno2("fclose", ondisk_path);
8884 free(path_unstaged_content);
8885 free(path_new_staged_content);
8886 free(blob_base_path);
8887 free(parent);
8888 free(base_path);
8889 return err;
8892 static const struct got_error *
8893 unstage_path(void *arg, unsigned char status,
8894 unsigned char staged_status, const char *relpath,
8895 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8896 struct got_object_id *commit_id, int dirfd, const char *de_name)
8898 const struct got_error *err = NULL;
8899 struct unstage_path_arg *a = arg;
8900 struct got_fileindex_entry *ie;
8901 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8902 char *ondisk_path = NULL;
8903 char *id_str = NULL, *label_orig = NULL;
8904 int local_changes_subsumed;
8905 struct stat sb;
8906 int fd1 = -1, fd2 = -1;
8908 if (staged_status != GOT_STATUS_ADD &&
8909 staged_status != GOT_STATUS_MODIFY &&
8910 staged_status != GOT_STATUS_DELETE)
8911 return NULL;
8913 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8914 if (ie == NULL)
8915 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8917 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8918 == -1)
8919 return got_error_from_errno("asprintf");
8921 err = got_object_id_str(&id_str,
8922 commit_id ? commit_id : a->worktree->base_commit_id);
8923 if (err)
8924 goto done;
8925 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8926 id_str) == -1) {
8927 err = got_error_from_errno("asprintf");
8928 goto done;
8931 fd1 = got_opentempfd();
8932 if (fd1 == -1) {
8933 err = got_error_from_errno("got_opentempfd");
8934 goto done;
8936 fd2 = got_opentempfd();
8937 if (fd2 == -1) {
8938 err = got_error_from_errno("got_opentempfd");
8939 goto done;
8942 switch (staged_status) {
8943 case GOT_STATUS_MODIFY:
8944 err = got_object_open_as_blob(&blob_base, a->repo,
8945 blob_id, 8192, fd1);
8946 if (err)
8947 break;
8948 /* fall through */
8949 case GOT_STATUS_ADD:
8950 if (a->patch_cb) {
8951 if (staged_status == GOT_STATUS_ADD) {
8952 int choice = GOT_PATCH_CHOICE_NONE;
8953 err = (*a->patch_cb)(&choice, a->patch_arg,
8954 staged_status, ie->path, NULL, 1, 1);
8955 if (err)
8956 break;
8957 if (choice != GOT_PATCH_CHOICE_YES)
8958 break;
8959 } else {
8960 err = unstage_hunks(staged_blob_id,
8961 blob_base, blob_id, ie, ondisk_path,
8962 label_orig, a->worktree, a->repo,
8963 a->patch_cb, a->patch_arg,
8964 a->progress_cb, a->progress_arg);
8965 break; /* Done with this file. */
8968 err = got_object_open_as_blob(&blob_staged, a->repo,
8969 staged_blob_id, 8192, fd2);
8970 if (err)
8971 break;
8972 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8973 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8974 case GOT_FILEIDX_MODE_REGULAR_FILE:
8975 err = merge_blob(&local_changes_subsumed, a->worktree,
8976 blob_base, ondisk_path, relpath,
8977 got_fileindex_perms_to_st(ie), label_orig,
8978 blob_staged, commit_id ? commit_id :
8979 a->worktree->base_commit_id, a->repo,
8980 a->progress_cb, a->progress_arg);
8981 break;
8982 case GOT_FILEIDX_MODE_SYMLINK:
8983 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8984 char *staged_target;
8985 err = got_object_blob_read_to_str(
8986 &staged_target, blob_staged);
8987 if (err)
8988 goto done;
8989 err = merge_symlink(a->worktree, blob_base,
8990 ondisk_path, relpath, label_orig,
8991 staged_target, commit_id ? commit_id :
8992 a->worktree->base_commit_id,
8993 a->repo, a->progress_cb, a->progress_arg);
8994 free(staged_target);
8995 } else {
8996 err = merge_blob(&local_changes_subsumed,
8997 a->worktree, blob_base, ondisk_path,
8998 relpath, got_fileindex_perms_to_st(ie),
8999 label_orig, blob_staged,
9000 commit_id ? commit_id :
9001 a->worktree->base_commit_id, a->repo,
9002 a->progress_cb, a->progress_arg);
9004 break;
9005 default:
9006 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9007 break;
9009 if (err == NULL) {
9010 got_fileindex_entry_stage_set(ie,
9011 GOT_FILEIDX_STAGE_NONE);
9012 got_fileindex_entry_staged_filetype_set(ie, 0);
9014 break;
9015 case GOT_STATUS_DELETE:
9016 if (a->patch_cb) {
9017 int choice = GOT_PATCH_CHOICE_NONE;
9018 err = (*a->patch_cb)(&choice, a->patch_arg,
9019 staged_status, ie->path, NULL, 1, 1);
9020 if (err)
9021 break;
9022 if (choice == GOT_PATCH_CHOICE_NO)
9023 break;
9024 if (choice != GOT_PATCH_CHOICE_YES) {
9025 err = got_error(GOT_ERR_PATCH_CHOICE);
9026 break;
9029 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9030 got_fileindex_entry_staged_filetype_set(ie, 0);
9031 err = get_file_status(&status, &sb, ie, ondisk_path,
9032 dirfd, de_name, a->repo);
9033 if (err)
9034 break;
9035 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9036 break;
9038 done:
9039 free(ondisk_path);
9040 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9041 err = got_error_from_errno("close");
9042 if (blob_base)
9043 got_object_blob_close(blob_base);
9044 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9045 err = got_error_from_errno("close");
9046 if (blob_staged)
9047 got_object_blob_close(blob_staged);
9048 free(id_str);
9049 free(label_orig);
9050 return err;
9053 const struct got_error *
9054 got_worktree_unstage(struct got_worktree *worktree,
9055 struct got_pathlist_head *paths,
9056 got_worktree_checkout_cb progress_cb, void *progress_arg,
9057 got_worktree_patch_cb patch_cb, void *patch_arg,
9058 struct got_repository *repo)
9060 const struct got_error *err = NULL, *sync_err, *unlockerr;
9061 struct got_pathlist_entry *pe;
9062 struct got_fileindex *fileindex = NULL;
9063 char *fileindex_path = NULL;
9064 struct unstage_path_arg upa;
9066 err = lock_worktree(worktree, LOCK_EX);
9067 if (err)
9068 return err;
9070 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9071 if (err)
9072 goto done;
9074 upa.worktree = worktree;
9075 upa.fileindex = fileindex;
9076 upa.repo = repo;
9077 upa.progress_cb = progress_cb;
9078 upa.progress_arg = progress_arg;
9079 upa.patch_cb = patch_cb;
9080 upa.patch_arg = patch_arg;
9081 TAILQ_FOREACH(pe, paths, entry) {
9082 err = worktree_status(worktree, pe->path, fileindex, repo,
9083 unstage_path, &upa, NULL, NULL, 1, 0);
9084 if (err)
9085 goto done;
9088 sync_err = sync_fileindex(fileindex, fileindex_path);
9089 if (sync_err && err == NULL)
9090 err = sync_err;
9091 done:
9092 free(fileindex_path);
9093 if (fileindex)
9094 got_fileindex_free(fileindex);
9095 unlockerr = lock_worktree(worktree, LOCK_SH);
9096 if (unlockerr && err == NULL)
9097 err = unlockerr;
9098 return err;
9101 struct report_file_info_arg {
9102 struct got_worktree *worktree;
9103 got_worktree_path_info_cb info_cb;
9104 void *info_arg;
9105 struct got_pathlist_head *paths;
9106 got_cancel_cb cancel_cb;
9107 void *cancel_arg;
9110 static const struct got_error *
9111 report_file_info(void *arg, struct got_fileindex_entry *ie)
9113 struct report_file_info_arg *a = arg;
9114 struct got_pathlist_entry *pe;
9115 struct got_object_id blob_id, staged_blob_id, commit_id;
9116 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9117 struct got_object_id *commit_idp = NULL;
9118 int stage;
9120 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9121 return got_error(GOT_ERR_CANCELLED);
9123 TAILQ_FOREACH(pe, a->paths, entry) {
9124 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9125 got_path_is_child(ie->path, pe->path, pe->path_len))
9126 break;
9128 if (pe == NULL) /* not found */
9129 return NULL;
9131 if (got_fileindex_entry_has_blob(ie)) {
9132 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
9133 blob_idp = &blob_id;
9135 stage = got_fileindex_entry_stage_get(ie);
9136 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9137 stage == GOT_FILEIDX_STAGE_ADD) {
9138 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
9139 SHA1_DIGEST_LENGTH);
9140 staged_blob_idp = &staged_blob_id;
9143 if (got_fileindex_entry_has_commit(ie)) {
9144 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
9145 commit_idp = &commit_id;
9148 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9149 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9152 const struct got_error *
9153 got_worktree_path_info(struct got_worktree *worktree,
9154 struct got_pathlist_head *paths,
9155 got_worktree_path_info_cb info_cb, void *info_arg,
9156 got_cancel_cb cancel_cb, void *cancel_arg)
9159 const struct got_error *err = NULL, *unlockerr;
9160 struct got_fileindex *fileindex = NULL;
9161 char *fileindex_path = NULL;
9162 struct report_file_info_arg arg;
9164 err = lock_worktree(worktree, LOCK_SH);
9165 if (err)
9166 return err;
9168 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9169 if (err)
9170 goto done;
9172 arg.worktree = worktree;
9173 arg.info_cb = info_cb;
9174 arg.info_arg = info_arg;
9175 arg.paths = paths;
9176 arg.cancel_cb = cancel_cb;
9177 arg.cancel_arg = cancel_arg;
9178 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9179 &arg);
9180 done:
9181 free(fileindex_path);
9182 if (fileindex)
9183 got_fileindex_free(fileindex);
9184 unlockerr = lock_worktree(worktree, LOCK_UN);
9185 if (unlockerr && err == NULL)
9186 err = unlockerr;
9187 return err;
9190 static const struct got_error *
9191 patch_check_path(const char *p, char **path, unsigned char *status,
9192 unsigned char *staged_status, struct got_fileindex *fileindex,
9193 struct got_worktree *worktree, struct got_repository *repo)
9195 const struct got_error *err;
9196 struct got_fileindex_entry *ie;
9197 struct stat sb;
9198 char *ondisk_path = NULL;
9200 err = got_worktree_resolve_path(path, worktree, p);
9201 if (err)
9202 return err;
9204 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9205 *path[0] ? "/" : "", *path) == -1)
9206 return got_error_from_errno("asprintf");
9208 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9209 if (ie) {
9210 *staged_status = get_staged_status(ie);
9211 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9212 repo);
9213 if (err)
9214 goto done;
9215 } else {
9216 *staged_status = GOT_STATUS_NO_CHANGE;
9217 *status = GOT_STATUS_UNVERSIONED;
9218 if (lstat(ondisk_path, &sb) == -1) {
9219 if (errno != ENOENT) {
9220 err = got_error_from_errno2("lstat",
9221 ondisk_path);
9222 goto done;
9224 *status = GOT_STATUS_NONEXISTENT;
9228 done:
9229 free(ondisk_path);
9230 return err;
9233 static const struct got_error *
9234 patch_can_rm(const char *path, unsigned char status,
9235 unsigned char staged_status)
9237 if (status == GOT_STATUS_NONEXISTENT)
9238 return got_error_set_errno(ENOENT, path);
9239 if (status != GOT_STATUS_NO_CHANGE &&
9240 status != GOT_STATUS_ADD &&
9241 status != GOT_STATUS_MODIFY &&
9242 status != GOT_STATUS_MODE_CHANGE)
9243 return got_error_path(path, GOT_ERR_FILE_STATUS);
9244 if (staged_status == GOT_STATUS_DELETE)
9245 return got_error_path(path, GOT_ERR_FILE_STATUS);
9246 return NULL;
9249 static const struct got_error *
9250 patch_can_add(const char *path, unsigned char status)
9252 if (status != GOT_STATUS_NONEXISTENT)
9253 return got_error_path(path, GOT_ERR_FILE_STATUS);
9254 return NULL;
9257 static const struct got_error *
9258 patch_can_edit(const char *path, unsigned char status,
9259 unsigned char staged_status)
9261 if (status == GOT_STATUS_NONEXISTENT)
9262 return got_error_set_errno(ENOENT, path);
9263 if (status != GOT_STATUS_NO_CHANGE &&
9264 status != GOT_STATUS_ADD &&
9265 status != GOT_STATUS_MODIFY)
9266 return got_error_path(path, GOT_ERR_FILE_STATUS);
9267 if (staged_status == GOT_STATUS_DELETE)
9268 return got_error_path(path, GOT_ERR_FILE_STATUS);
9269 return NULL;
9272 const struct got_error *
9273 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9274 char **fileindex_path, struct got_worktree *worktree)
9276 return open_fileindex(fileindex, fileindex_path, worktree);
9279 const struct got_error *
9280 got_worktree_patch_check_path(const char *old, const char *new,
9281 char **oldpath, char **newpath, struct got_worktree *worktree,
9282 struct got_repository *repo, struct got_fileindex *fileindex)
9284 const struct got_error *err = NULL;
9285 int file_renamed = 0;
9286 unsigned char status_old, staged_status_old;
9287 unsigned char status_new, staged_status_new;
9289 *oldpath = NULL;
9290 *newpath = NULL;
9292 err = patch_check_path(old != NULL ? old : new, oldpath,
9293 &status_old, &staged_status_old, fileindex, worktree, repo);
9294 if (err)
9295 goto done;
9297 err = patch_check_path(new != NULL ? new : old, newpath,
9298 &status_new, &staged_status_new, fileindex, worktree, repo);
9299 if (err)
9300 goto done;
9302 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9303 file_renamed = 1;
9305 if (old != NULL && new == NULL)
9306 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9307 else if (file_renamed) {
9308 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9309 if (err == NULL)
9310 err = patch_can_add(*newpath, status_new);
9311 } else if (old == NULL)
9312 err = patch_can_add(*newpath, status_new);
9313 else
9314 err = patch_can_edit(*newpath, status_new, staged_status_new);
9316 done:
9317 if (err) {
9318 free(*oldpath);
9319 *oldpath = NULL;
9320 free(*newpath);
9321 *newpath = NULL;
9323 return err;
9326 const struct got_error *
9327 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9328 struct got_worktree *worktree, struct got_fileindex *fileindex,
9329 got_worktree_checkout_cb progress_cb, void *progress_arg)
9331 struct schedule_addition_args saa;
9333 memset(&saa, 0, sizeof(saa));
9334 saa.worktree = worktree;
9335 saa.fileindex = fileindex;
9336 saa.progress_cb = progress_cb;
9337 saa.progress_arg = progress_arg;
9338 saa.repo = repo;
9340 return worktree_status(worktree, path, fileindex, repo,
9341 schedule_addition, &saa, NULL, NULL, 1, 0);
9344 const struct got_error *
9345 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9346 struct got_worktree *worktree, struct got_fileindex *fileindex,
9347 got_worktree_delete_cb progress_cb, void *progress_arg)
9349 struct schedule_deletion_args sda;
9351 memset(&sda, 0, sizeof(sda));
9352 sda.worktree = worktree;
9353 sda.fileindex = fileindex;
9354 sda.progress_cb = progress_cb;
9355 sda.progress_arg = progress_arg;
9356 sda.repo = repo;
9357 sda.delete_local_mods = 0;
9358 sda.keep_on_disk = 0;
9359 sda.ignore_missing_paths = 0;
9360 sda.status_codes = NULL;
9362 return worktree_status(worktree, path, fileindex, repo,
9363 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9366 const struct got_error *
9367 got_worktree_patch_complete(struct got_fileindex *fileindex,
9368 const char *fileindex_path)
9370 const struct got_error *err = NULL;
9372 err = sync_fileindex(fileindex, fileindex_path);
9373 got_fileindex_free(fileindex);
9375 return err;