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, base_path);
996 if (err)
997 goto done;
998 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
999 blob_orig);
1000 if (err)
1001 goto done;
1002 free(base_path);
1003 } else {
1005 * No common ancestor exists. This is an "add vs add" conflict
1006 * and we simply use an empty ancestor file to make both files
1007 * appear in the merged result in their entirety.
1009 f_orig = got_opentemp();
1010 if (f_orig == NULL) {
1011 err = got_error_from_errno("got_opentemp");
1012 goto done;
1016 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1017 err = got_error_from_errno("asprintf");
1018 base_path = NULL;
1019 goto done;
1022 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1023 if (err)
1024 goto done;
1025 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1026 blob_deriv);
1027 if (err)
1028 goto done;
1030 err = got_object_id_str(&id_str, deriv_base_commit_id);
1031 if (err)
1032 goto done;
1033 if (asprintf(&label_deriv, "%s: commit %s",
1034 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1035 err = got_error_from_errno("asprintf");
1036 goto done;
1040 * In order the run a 3-way merge with a symlink we copy the symlink's
1041 * target path into a temporary file and use that file with diff3.
1043 if (S_ISLNK(st_mode)) {
1044 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1045 if (err)
1046 goto done;
1047 } else {
1048 int fd;
1049 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1050 if (fd == -1) {
1051 err = got_error_from_errno2("open", ondisk_path);
1052 goto done;
1054 f_deriv2 = fdopen(fd, "r");
1055 if (f_deriv2 == NULL) {
1056 err = got_error_from_errno2("fdopen", ondisk_path);
1057 close(fd);
1058 goto done;
1062 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1063 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1064 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1065 done:
1066 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 free(base_path);
1073 if (blob_orig_path) {
1074 unlink(blob_orig_path);
1075 free(blob_orig_path);
1077 if (blob_deriv_path) {
1078 unlink(blob_deriv_path);
1079 free(blob_deriv_path);
1081 free(id_str);
1082 free(label_deriv);
1083 free(parent);
1084 return err;
1087 static const struct got_error *
1088 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1089 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1090 int wt_fd, const char *path, struct got_object_id *blob_id)
1092 const struct got_error *err = NULL;
1093 struct got_fileindex_entry *new_ie;
1095 *new_iep = NULL;
1097 err = got_fileindex_entry_alloc(&new_ie, path);
1098 if (err)
1099 return err;
1101 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1102 blob_id->sha1, base_commit_id->sha1, 1);
1103 if (err)
1104 goto done;
1106 err = got_fileindex_entry_add(fileindex, new_ie);
1107 done:
1108 if (err)
1109 got_fileindex_entry_free(new_ie);
1110 else
1111 *new_iep = new_ie;
1112 return err;
1115 static mode_t
1116 get_ondisk_perms(int executable, mode_t st_mode)
1118 mode_t xbits = S_IXUSR;
1120 if (executable) {
1121 /* Map read bits to execute bits. */
1122 if (st_mode & S_IRGRP)
1123 xbits |= S_IXGRP;
1124 if (st_mode & S_IROTH)
1125 xbits |= S_IXOTH;
1126 return st_mode | xbits;
1129 return st_mode;
1132 static mode_t
1133 apply_umask(mode_t mode)
1135 mode_t um;
1137 um = umask(000);
1138 umask(um);
1139 return mode & ~um;
1142 /* forward declaration */
1143 static const struct got_error *
1144 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 const char *path, mode_t te_mode, mode_t st_mode,
1146 struct got_blob_object *blob, int restoring_missing_file,
1147 int reverting_versioned_file, int installing_bad_symlink,
1148 int path_is_unversioned, struct got_repository *repo,
1149 got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 * This function assumes that the provided symlink target points at a
1153 * safe location in the work tree!
1155 static const struct got_error *
1156 replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 const char *target_path, size_t target_len)
1159 const struct got_error *err = NULL;
1160 ssize_t elen;
1161 char etarget[PATH_MAX];
1162 int fd;
1164 *did_something = 0;
1167 * "Bad" symlinks (those pointing outside the work tree or into the
1168 * .got directory) are installed in the work tree as a regular file
1169 * which contains the bad symlink target path.
1170 * The new symlink target has already been checked for safety by our
1171 * caller. If we can successfully open a regular file then we simply
1172 * replace this file with a symlink below.
1174 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 if (fd == -1) {
1176 if (!got_err_open_nofollow_on_symlink())
1177 return got_error_from_errno2("open", ondisk_path);
1179 /* We are updating an existing on-disk symlink. */
1180 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 if (elen == -1)
1182 return got_error_from_errno2("readlink", ondisk_path);
1184 if (elen == target_len &&
1185 memcmp(etarget, target_path, target_len) == 0)
1186 return NULL; /* nothing to do */
1189 *did_something = 1;
1190 err = update_symlink(ondisk_path, target_path, target_len);
1191 if (fd != -1 && close(fd) == -1 && err == NULL)
1192 err = got_error_from_errno2("close", ondisk_path);
1193 return err;
1196 static const struct got_error *
1197 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 const struct got_error *err = NULL;
1201 char canonpath[PATH_MAX];
1202 char *path_got = NULL;
1204 *is_bad_symlink = 0;
1206 if (target_len >= sizeof(canonpath)) {
1207 *is_bad_symlink = 1;
1208 return NULL;
1212 * We do not use realpath(3) to resolve the symlink's target
1213 * path because we don't want to resolve symlinks recursively.
1214 * Instead we make the path absolute and then canonicalize it.
1215 * Relative symlink target lookup should begin at the directory
1216 * in which the blob object is being installed.
1218 if (!got_path_is_absolute(target_path)) {
1219 char *abspath, *parent;
1220 err = got_path_dirname(&parent, ondisk_path);
1221 if (err)
1222 return err;
1223 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 free(parent);
1225 return got_error_from_errno("asprintf");
1227 free(parent);
1228 if (strlen(abspath) >= sizeof(canonpath)) {
1229 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1230 free(abspath);
1231 return err;
1233 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1234 free(abspath);
1235 if (err)
1236 return err;
1237 } else {
1238 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1239 if (err)
1240 return err;
1243 /* Only allow symlinks pointing at paths within the work tree. */
1244 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 *is_bad_symlink = 1;
1246 return NULL;
1249 /* Do not allow symlinks pointing into the .got directory. */
1250 if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 GOT_WORKTREE_GOT_DIR) == -1)
1252 return got_error_from_errno("asprintf");
1253 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 *is_bad_symlink = 1;
1256 free(path_got);
1257 return NULL;
1260 static const struct got_error *
1261 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 int restoring_missing_file, int reverting_versioned_file,
1264 int path_is_unversioned, int allow_bad_symlinks,
1265 struct got_repository *repo,
1266 got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 const struct got_error *err = NULL;
1269 char target_path[PATH_MAX];
1270 size_t len, target_len = 0;
1271 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 *is_bad_symlink = 0;
1277 * Blob object content specifies the target path of the link.
1278 * If a symbolic link cannot be installed we instead create
1279 * a regular file which contains the link target path stored
1280 * in the blob object.
1282 do {
1283 err = got_object_blob_read_block(&len, blob);
1284 if (err)
1285 return err;
1287 if (len + target_len >= sizeof(target_path)) {
1288 /* Path too long; install as a regular file. */
1289 *is_bad_symlink = 1;
1290 got_object_blob_rewind(blob);
1291 return install_blob(worktree, ondisk_path, path,
1292 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 restoring_missing_file, reverting_versioned_file,
1294 1, path_is_unversioned, repo, progress_cb,
1295 progress_arg);
1297 if (len > 0) {
1298 /* Skip blob object header first time around. */
1299 memcpy(target_path + target_len, buf + hdrlen,
1300 len - hdrlen);
1301 target_len += len - hdrlen;
1302 hdrlen = 0;
1304 } while (len != 0);
1305 target_path[target_len] = '\0';
1307 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 ondisk_path, worktree->root_path);
1309 if (err)
1310 return err;
1312 if (*is_bad_symlink && !allow_bad_symlinks) {
1313 /* install as a regular file */
1314 got_object_blob_rewind(blob);
1315 err = install_blob(worktree, ondisk_path, path,
1316 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 restoring_missing_file, reverting_versioned_file, 1,
1318 path_is_unversioned, repo, progress_cb, progress_arg);
1319 return err;
1322 if (symlink(target_path, ondisk_path) == -1) {
1323 if (errno == EEXIST) {
1324 int symlink_replaced;
1325 if (path_is_unversioned) {
1326 err = (*progress_cb)(progress_arg,
1327 GOT_STATUS_UNVERSIONED, path);
1328 return err;
1330 err = replace_existing_symlink(&symlink_replaced,
1331 ondisk_path, target_path, target_len);
1332 if (err)
1333 return err;
1334 if (progress_cb) {
1335 if (symlink_replaced) {
1336 err = (*progress_cb)(progress_arg,
1337 reverting_versioned_file ?
1338 GOT_STATUS_REVERT :
1339 GOT_STATUS_UPDATE, path);
1340 } else {
1341 err = (*progress_cb)(progress_arg,
1342 GOT_STATUS_EXISTS, path);
1345 return err; /* Nothing else to do. */
1348 if (errno == ENOENT) {
1349 char *parent;
1350 err = got_path_dirname(&parent, ondisk_path);
1351 if (err)
1352 return err;
1353 err = add_dir_on_disk(worktree, parent);
1354 free(parent);
1355 if (err)
1356 return err;
1358 * Retry, and fall through to error handling
1359 * below if this second attempt fails.
1361 if (symlink(target_path, ondisk_path) != -1) {
1362 err = NULL; /* success */
1363 return err;
1367 /* Handle errors from first or second creation attempt. */
1368 if (errno == ENAMETOOLONG) {
1369 /* bad target path; install as a regular file */
1370 *is_bad_symlink = 1;
1371 got_object_blob_rewind(blob);
1372 err = install_blob(worktree, ondisk_path, path,
1373 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 restoring_missing_file, reverting_versioned_file, 1,
1375 path_is_unversioned, repo,
1376 progress_cb, progress_arg);
1377 } else if (errno == ENOTDIR) {
1378 err = got_error_path(ondisk_path,
1379 GOT_ERR_FILE_OBSTRUCTED);
1380 } else {
1381 err = got_error_from_errno3("symlink",
1382 target_path, ondisk_path);
1384 } else if (progress_cb)
1385 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1387 return err;
1390 static const struct got_error *
1391 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 const char *path, mode_t te_mode, mode_t st_mode,
1393 struct got_blob_object *blob, int restoring_missing_file,
1394 int reverting_versioned_file, int installing_bad_symlink,
1395 int path_is_unversioned, struct got_repository *repo,
1396 got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 const struct got_error *err = NULL;
1399 int fd = -1;
1400 size_t len, hdrlen;
1401 int update = 0;
1402 char *tmppath = NULL;
1403 mode_t mode;
1405 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1407 O_CLOEXEC, mode);
1408 if (fd == -1) {
1409 if (errno == ENOENT) {
1410 char *parent;
1411 err = got_path_dirname(&parent, path);
1412 if (err)
1413 return err;
1414 err = add_dir_on_disk(worktree, parent);
1415 free(parent);
1416 if (err)
1417 return err;
1418 fd = open(ondisk_path,
1419 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1420 mode);
1421 if (fd == -1)
1422 return got_error_from_errno2("open",
1423 ondisk_path);
1424 } else if (errno == EEXIST) {
1425 if (path_is_unversioned) {
1426 err = (*progress_cb)(progress_arg,
1427 GOT_STATUS_UNVERSIONED, path);
1428 goto done;
1430 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1431 !S_ISREG(st_mode) && !installing_bad_symlink) {
1432 /* TODO file is obstructed; do something */
1433 err = got_error_path(ondisk_path,
1434 GOT_ERR_FILE_OBSTRUCTED);
1435 goto done;
1436 } else {
1437 err = got_opentemp_named_fd(&tmppath, &fd,
1438 ondisk_path);
1439 if (err)
1440 goto done;
1441 update = 1;
1443 if (fchmod(fd, apply_umask(mode)) == -1) {
1444 err = got_error_from_errno2("fchmod",
1445 tmppath);
1446 goto done;
1449 } else
1450 return got_error_from_errno2("open", ondisk_path);
1453 if (progress_cb) {
1454 if (restoring_missing_file)
1455 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1456 path);
1457 else if (reverting_versioned_file)
1458 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1459 path);
1460 else
1461 err = (*progress_cb)(progress_arg,
1462 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1463 if (err)
1464 goto done;
1467 hdrlen = got_object_blob_get_hdrlen(blob);
1468 do {
1469 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1470 err = got_object_blob_read_block(&len, blob);
1471 if (err)
1472 break;
1473 if (len > 0) {
1474 /* Skip blob object header first time around. */
1475 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1476 if (outlen == -1) {
1477 err = got_error_from_errno("write");
1478 goto done;
1479 } else if (outlen != len - hdrlen) {
1480 err = got_error(GOT_ERR_IO);
1481 goto done;
1483 hdrlen = 0;
1485 } while (len != 0);
1487 if (fsync(fd) != 0) {
1488 err = got_error_from_errno("fsync");
1489 goto done;
1492 if (update) {
1493 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1494 err = got_error_from_errno2("unlink", ondisk_path);
1495 goto done;
1497 if (rename(tmppath, ondisk_path) != 0) {
1498 err = got_error_from_errno3("rename", tmppath,
1499 ondisk_path);
1500 goto done;
1502 free(tmppath);
1503 tmppath = NULL;
1506 done:
1507 if (fd != -1 && close(fd) == -1 && err == NULL)
1508 err = got_error_from_errno("close");
1509 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1510 err = got_error_from_errno2("unlink", tmppath);
1511 free(tmppath);
1512 return err;
1515 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1516 static const struct got_error *
1517 get_modified_file_content_status(unsigned char *status, FILE *f)
1519 const struct got_error *err = NULL;
1520 const char *markers[3] = {
1521 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1522 GOT_DIFF_CONFLICT_MARKER_SEP,
1523 GOT_DIFF_CONFLICT_MARKER_END
1525 int i = 0;
1526 char *line = NULL;
1527 size_t linesize = 0;
1528 ssize_t linelen;
1530 while (*status == GOT_STATUS_MODIFY) {
1531 linelen = getline(&line, &linesize, f);
1532 if (linelen == -1) {
1533 if (feof(f))
1534 break;
1535 err = got_ferror(f, GOT_ERR_IO);
1536 break;
1539 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1540 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1541 == 0)
1542 *status = GOT_STATUS_CONFLICT;
1543 else
1544 i++;
1547 free(line);
1549 return err;
1552 static int
1553 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1555 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1556 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1559 static int
1560 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1562 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1563 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1564 ie->mtime_sec == sb->st_mtim.tv_sec &&
1565 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1566 ie->size == (sb->st_size & 0xffffffff) &&
1567 !xbit_differs(ie, sb->st_mode));
1570 static unsigned char
1571 get_staged_status(struct got_fileindex_entry *ie)
1573 switch (got_fileindex_entry_stage_get(ie)) {
1574 case GOT_FILEIDX_STAGE_ADD:
1575 return GOT_STATUS_ADD;
1576 case GOT_FILEIDX_STAGE_DELETE:
1577 return GOT_STATUS_DELETE;
1578 case GOT_FILEIDX_STAGE_MODIFY:
1579 return GOT_STATUS_MODIFY;
1580 default:
1581 return GOT_STATUS_NO_CHANGE;
1585 static const struct got_error *
1586 get_symlink_modification_status(unsigned char *status,
1587 struct got_fileindex_entry *ie, const char *abspath,
1588 int dirfd, const char *de_name, struct got_blob_object *blob)
1590 const struct got_error *err = NULL;
1591 char target_path[PATH_MAX];
1592 char etarget[PATH_MAX];
1593 ssize_t elen;
1594 size_t len, target_len = 0;
1595 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1596 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1598 *status = GOT_STATUS_NO_CHANGE;
1600 /* Blob object content specifies the target path of the link. */
1601 do {
1602 err = got_object_blob_read_block(&len, blob);
1603 if (err)
1604 return err;
1605 if (len + target_len >= sizeof(target_path)) {
1607 * Should not happen. The blob contents were OK
1608 * when this symlink was installed.
1610 return got_error(GOT_ERR_NO_SPACE);
1612 if (len > 0) {
1613 /* Skip blob object header first time around. */
1614 memcpy(target_path + target_len, buf + hdrlen,
1615 len - hdrlen);
1616 target_len += len - hdrlen;
1617 hdrlen = 0;
1619 } while (len != 0);
1620 target_path[target_len] = '\0';
1622 if (dirfd != -1) {
1623 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1624 if (elen == -1)
1625 return got_error_from_errno2("readlinkat", abspath);
1626 } else {
1627 elen = readlink(abspath, etarget, sizeof(etarget));
1628 if (elen == -1)
1629 return got_error_from_errno2("readlink", abspath);
1632 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1633 *status = GOT_STATUS_MODIFY;
1635 return NULL;
1638 static const struct got_error *
1639 get_file_status(unsigned char *status, struct stat *sb,
1640 struct got_fileindex_entry *ie, const char *abspath,
1641 int dirfd, const char *de_name, struct got_repository *repo)
1643 const struct got_error *err = NULL;
1644 struct got_object_id id;
1645 size_t hdrlen;
1646 int fd = -1, fd1 = -1;
1647 FILE *f = NULL;
1648 uint8_t fbuf[8192];
1649 struct got_blob_object *blob = NULL;
1650 size_t flen, blen;
1651 unsigned char staged_status = get_staged_status(ie);
1653 *status = GOT_STATUS_NO_CHANGE;
1654 memset(sb, 0, sizeof(*sb));
1657 * Whenever the caller provides a directory descriptor and a
1658 * directory entry name for the file, use them! This prevents
1659 * race conditions if filesystem paths change beneath our feet.
1661 if (dirfd != -1) {
1662 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1663 if (errno == ENOENT) {
1664 if (got_fileindex_entry_has_file_on_disk(ie))
1665 *status = GOT_STATUS_MISSING;
1666 else
1667 *status = GOT_STATUS_DELETE;
1668 goto done;
1670 err = got_error_from_errno2("fstatat", abspath);
1671 goto done;
1673 } else {
1674 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1675 if (fd == -1 && errno != ENOENT &&
1676 !got_err_open_nofollow_on_symlink())
1677 return got_error_from_errno2("open", abspath);
1678 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1679 if (lstat(abspath, sb) == -1)
1680 return got_error_from_errno2("lstat", abspath);
1681 } else if (fd == -1 || fstat(fd, sb) == -1) {
1682 if (errno == ENOENT) {
1683 if (got_fileindex_entry_has_file_on_disk(ie))
1684 *status = GOT_STATUS_MISSING;
1685 else
1686 *status = GOT_STATUS_DELETE;
1687 goto done;
1689 err = got_error_from_errno2("fstat", abspath);
1690 goto done;
1694 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1695 *status = GOT_STATUS_OBSTRUCTED;
1696 goto done;
1699 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1700 *status = GOT_STATUS_DELETE;
1701 goto done;
1702 } else if (!got_fileindex_entry_has_blob(ie) &&
1703 staged_status != GOT_STATUS_ADD) {
1704 *status = GOT_STATUS_ADD;
1705 goto done;
1708 if (!stat_info_differs(ie, sb))
1709 goto done;
1711 if (S_ISLNK(sb->st_mode) &&
1712 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1713 *status = GOT_STATUS_MODIFY;
1714 goto done;
1717 if (staged_status == GOT_STATUS_MODIFY ||
1718 staged_status == GOT_STATUS_ADD)
1719 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1720 else
1721 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1723 fd1 = got_opentempfd();
1724 if (fd1 == -1) {
1725 err = got_error_from_errno("got_opentempfd");
1726 goto done;
1728 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1729 if (err)
1730 goto done;
1732 if (S_ISLNK(sb->st_mode)) {
1733 err = get_symlink_modification_status(status, ie,
1734 abspath, dirfd, de_name, blob);
1735 goto done;
1738 if (dirfd != -1) {
1739 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 if (fd == -1) {
1741 err = got_error_from_errno2("openat", abspath);
1742 goto done;
1746 f = fdopen(fd, "r");
1747 if (f == NULL) {
1748 err = got_error_from_errno2("fdopen", abspath);
1749 goto done;
1751 fd = -1;
1752 hdrlen = got_object_blob_get_hdrlen(blob);
1753 for (;;) {
1754 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1755 err = got_object_blob_read_block(&blen, blob);
1756 if (err)
1757 goto done;
1758 /* Skip length of blob object header first time around. */
1759 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1760 if (flen == 0 && ferror(f)) {
1761 err = got_error_from_errno("fread");
1762 goto done;
1764 if (blen - hdrlen == 0) {
1765 if (flen != 0)
1766 *status = GOT_STATUS_MODIFY;
1767 break;
1768 } else if (flen == 0) {
1769 if (blen - hdrlen != 0)
1770 *status = GOT_STATUS_MODIFY;
1771 break;
1772 } else if (blen - hdrlen == flen) {
1773 /* Skip blob object header first time around. */
1774 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1775 *status = GOT_STATUS_MODIFY;
1776 break;
1778 } else {
1779 *status = GOT_STATUS_MODIFY;
1780 break;
1782 hdrlen = 0;
1785 if (*status == GOT_STATUS_MODIFY) {
1786 rewind(f);
1787 err = get_modified_file_content_status(status, f);
1788 } else if (xbit_differs(ie, sb->st_mode))
1789 *status = GOT_STATUS_MODE_CHANGE;
1790 done:
1791 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1792 err = got_error_from_errno("close");
1793 if (blob)
1794 got_object_blob_close(blob);
1795 if (f != NULL && fclose(f) == EOF && err == NULL)
1796 err = got_error_from_errno2("fclose", abspath);
1797 if (fd != -1 && close(fd) == -1 && err == NULL)
1798 err = got_error_from_errno2("close", abspath);
1799 return err;
1803 * Update timestamps in the file index if a file is unmodified and
1804 * we had to run a full content comparison to find out.
1806 static const struct got_error *
1807 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1808 struct got_fileindex_entry *ie, struct stat *sb)
1810 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1811 return got_fileindex_entry_update(ie, wt_fd, path,
1812 ie->blob_sha1, ie->commit_sha1, 1);
1814 return NULL;
1817 static const struct got_error *
1818 update_blob(struct got_worktree *worktree,
1819 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1820 struct got_tree_entry *te, const char *path,
1821 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1822 void *progress_arg)
1824 const struct got_error *err = NULL;
1825 struct got_blob_object *blob = NULL;
1826 char *ondisk_path = NULL;
1827 unsigned char status = GOT_STATUS_NO_CHANGE;
1828 struct stat sb;
1829 int fd1 = -1, fd2 = -1;
1831 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 return got_error_from_errno("asprintf");
1834 if (ie) {
1835 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1837 goto done;
1839 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1840 repo);
1841 if (err)
1842 goto done;
1843 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 sb.st_mode = got_fileindex_perms_to_st(ie);
1845 } else {
1846 if (stat(ondisk_path, &sb) == -1) {
1847 if (errno != ENOENT) {
1848 err = got_error_from_errno2("stat",
1849 ondisk_path);
1850 goto done;
1852 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 status = GOT_STATUS_UNVERSIONED;
1854 } else {
1855 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1856 status = GOT_STATUS_UNVERSIONED;
1857 else
1858 status = GOT_STATUS_OBSTRUCTED;
1862 if (status == GOT_STATUS_OBSTRUCTED) {
1863 if (ie)
1864 got_fileindex_entry_mark_skipped(ie);
1865 err = (*progress_cb)(progress_arg, status, path);
1866 goto done;
1868 if (status == GOT_STATUS_CONFLICT) {
1869 if (ie)
1870 got_fileindex_entry_mark_skipped(ie);
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1872 path);
1873 goto done;
1876 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1877 (S_ISLNK(te->mode) ||
1878 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1880 * This is a regular file or an installed bad symlink.
1881 * If the file index indicates that this file is already
1882 * up-to-date with respect to the repository we can skip
1883 * updating contents of this file.
1885 if (got_fileindex_entry_has_commit(ie) &&
1886 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1887 SHA1_DIGEST_LENGTH) == 0) {
1888 /* Same commit. */
1889 err = sync_timestamps(worktree->root_fd,
1890 path, status, ie, &sb);
1891 if (err)
1892 goto done;
1893 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1894 path);
1895 goto done;
1897 if (got_fileindex_entry_has_blob(ie) &&
1898 memcmp(ie->blob_sha1, te->id.sha1,
1899 SHA1_DIGEST_LENGTH) == 0) {
1900 /* Different commit but the same blob. */
1901 err = sync_timestamps(worktree->root_fd,
1902 path, status, ie, &sb);
1903 if (err)
1904 goto done;
1905 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1906 path);
1907 goto done;
1911 fd1 = got_opentempfd();
1912 if (fd1 == -1) {
1913 err = got_error_from_errno("got_opentempfd");
1914 goto done;
1916 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1917 if (err)
1918 goto done;
1920 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1921 int update_timestamps;
1922 struct got_blob_object *blob2 = NULL;
1923 char *label_orig = NULL;
1924 if (got_fileindex_entry_has_blob(ie)) {
1925 fd2 = got_opentempfd();
1926 if (fd2 == -1) {
1927 err = got_error_from_errno("got_opentempfd");
1928 goto done;
1930 struct got_object_id id2;
1931 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1932 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1933 fd2);
1934 if (err)
1935 goto done;
1937 if (got_fileindex_entry_has_commit(ie)) {
1938 char id_str[SHA1_DIGEST_STRING_LENGTH];
1939 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1940 sizeof(id_str)) == NULL) {
1941 err = got_error_path(id_str,
1942 GOT_ERR_BAD_OBJ_ID_STR);
1943 goto done;
1945 if (asprintf(&label_orig, "%s: commit %s",
1946 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1947 err = got_error_from_errno("asprintf");
1948 goto done;
1951 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1952 char *link_target;
1953 err = got_object_blob_read_to_str(&link_target, blob);
1954 if (err)
1955 goto done;
1956 err = merge_symlink(worktree, blob2, ondisk_path, path,
1957 label_orig, link_target, worktree->base_commit_id,
1958 repo, progress_cb, progress_arg);
1959 free(link_target);
1960 } else {
1961 err = merge_blob(&update_timestamps, worktree, blob2,
1962 ondisk_path, path, sb.st_mode, label_orig, blob,
1963 worktree->base_commit_id, repo,
1964 progress_cb, progress_arg);
1966 free(label_orig);
1967 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1968 err = got_error_from_errno("close");
1969 goto done;
1971 if (blob2)
1972 got_object_blob_close(blob2);
1973 if (err)
1974 goto done;
1976 * Do not update timestamps of files with local changes.
1977 * Otherwise, a future status walk would treat them as
1978 * unmodified files again.
1980 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1981 blob->id.sha1, worktree->base_commit_id->sha1,
1982 update_timestamps);
1983 } else if (status == GOT_STATUS_MODE_CHANGE) {
1984 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1985 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1986 } else if (status == GOT_STATUS_DELETE) {
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1988 if (err)
1989 goto done;
1990 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1991 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1992 if (err)
1993 goto done;
1994 } else {
1995 int is_bad_symlink = 0;
1996 if (S_ISLNK(te->mode)) {
1997 err = install_symlink(&is_bad_symlink, worktree,
1998 ondisk_path, path, blob,
1999 status == GOT_STATUS_MISSING, 0,
2000 status == GOT_STATUS_UNVERSIONED, 0,
2001 repo, progress_cb, progress_arg);
2002 } else {
2003 err = install_blob(worktree, ondisk_path, path,
2004 te->mode, sb.st_mode, blob,
2005 status == GOT_STATUS_MISSING, 0, 0,
2006 status == GOT_STATUS_UNVERSIONED, repo,
2007 progress_cb, progress_arg);
2009 if (err)
2010 goto done;
2012 if (ie) {
2013 err = got_fileindex_entry_update(ie,
2014 worktree->root_fd, path, blob->id.sha1,
2015 worktree->base_commit_id->sha1, 1);
2016 } else {
2017 err = create_fileindex_entry(&ie, fileindex,
2018 worktree->base_commit_id, worktree->root_fd, path,
2019 &blob->id);
2021 if (err)
2022 goto done;
2024 if (is_bad_symlink) {
2025 got_fileindex_entry_filetype_set(ie,
2026 GOT_FILEIDX_MODE_BAD_SYMLINK);
2030 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2031 err = got_error_from_errno("close");
2032 goto done;
2034 got_object_blob_close(blob);
2035 done:
2036 free(ondisk_path);
2037 return err;
2040 static const struct got_error *
2041 remove_ondisk_file(const char *root_path, const char *path)
2043 const struct got_error *err = NULL;
2044 char *ondisk_path = NULL, *parent = NULL;
2046 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2047 return got_error_from_errno("asprintf");
2049 if (unlink(ondisk_path) == -1) {
2050 if (errno != ENOENT)
2051 err = got_error_from_errno2("unlink", ondisk_path);
2052 } else {
2053 size_t root_len = strlen(root_path);
2054 err = got_path_dirname(&parent, ondisk_path);
2055 if (err)
2056 goto done;
2057 while (got_path_cmp(parent, root_path,
2058 strlen(parent), root_len) != 0) {
2059 free(ondisk_path);
2060 ondisk_path = parent;
2061 parent = NULL;
2062 if (rmdir(ondisk_path) == -1) {
2063 if (errno != ENOTEMPTY)
2064 err = got_error_from_errno2("rmdir",
2065 ondisk_path);
2066 break;
2068 err = got_path_dirname(&parent, ondisk_path);
2069 if (err)
2070 break;
2073 done:
2074 free(ondisk_path);
2075 free(parent);
2076 return err;
2079 static const struct got_error *
2080 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2081 struct got_fileindex_entry *ie, struct got_repository *repo,
2082 got_worktree_checkout_cb progress_cb, void *progress_arg)
2084 const struct got_error *err = NULL;
2085 unsigned char status;
2086 struct stat sb;
2087 char *ondisk_path;
2089 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2090 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2092 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2093 == -1)
2094 return got_error_from_errno("asprintf");
2096 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2097 if (err)
2098 goto done;
2100 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2101 char ondisk_target[PATH_MAX];
2102 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2103 sizeof(ondisk_target));
2104 if (ondisk_len == -1) {
2105 err = got_error_from_errno2("readlink", ondisk_path);
2106 goto done;
2108 ondisk_target[ondisk_len] = '\0';
2109 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2110 NULL, NULL, /* XXX pass common ancestor info? */
2111 ondisk_target, ondisk_path);
2112 if (err)
2113 goto done;
2114 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2115 ie->path);
2116 goto done;
2119 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2120 status == GOT_STATUS_ADD) {
2121 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2122 if (err)
2123 goto done;
2125 * Preserve the working file and change the deleted blob's
2126 * entry into a schedule-add entry.
2128 err = got_fileindex_entry_update(ie, worktree->root_fd,
2129 ie->path, NULL, NULL, 0);
2130 } else {
2131 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2132 if (err)
2133 goto done;
2134 if (status == GOT_STATUS_NO_CHANGE) {
2135 err = remove_ondisk_file(worktree->root_path, ie->path);
2136 if (err)
2137 goto done;
2139 got_fileindex_entry_remove(fileindex, ie);
2141 done:
2142 free(ondisk_path);
2143 return err;
2146 struct diff_cb_arg {
2147 struct got_fileindex *fileindex;
2148 struct got_worktree *worktree;
2149 struct got_repository *repo;
2150 got_worktree_checkout_cb progress_cb;
2151 void *progress_arg;
2152 got_cancel_cb cancel_cb;
2153 void *cancel_arg;
2156 static const struct got_error *
2157 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2158 struct got_tree_entry *te, const char *parent_path)
2160 struct diff_cb_arg *a = arg;
2162 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2163 return got_error(GOT_ERR_CANCELLED);
2165 return update_blob(a->worktree, a->fileindex, ie, te,
2166 ie->path, a->repo, a->progress_cb, a->progress_arg);
2169 static const struct got_error *
2170 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2172 struct diff_cb_arg *a = arg;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 return delete_blob(a->worktree, a->fileindex, ie,
2178 a->repo, a->progress_cb, a->progress_arg);
2181 static const struct got_error *
2182 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2184 struct diff_cb_arg *a = arg;
2185 const struct got_error *err;
2186 char *path;
2188 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2189 return got_error(GOT_ERR_CANCELLED);
2191 if (got_object_tree_entry_is_submodule(te))
2192 return NULL;
2194 if (asprintf(&path, "%s%s%s", parent_path,
2195 parent_path[0] ? "/" : "", te->name)
2196 == -1)
2197 return got_error_from_errno("asprintf");
2199 if (S_ISDIR(te->mode))
2200 err = add_dir_on_disk(a->worktree, path);
2201 else
2202 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2203 a->repo, a->progress_cb, a->progress_arg);
2205 free(path);
2206 return err;
2209 const struct got_error *
2210 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2212 uint32_t uuid_status;
2214 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2215 if (uuid_status != uuid_s_ok) {
2216 *uuidstr = NULL;
2217 return got_error_uuid(uuid_status, "uuid_to_string");
2220 return NULL;
2223 static const struct got_error *
2224 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2226 const struct got_error *err = NULL;
2227 char *uuidstr = NULL;
2229 *refname = NULL;
2231 err = got_worktree_get_uuid(&uuidstr, worktree);
2232 if (err)
2233 return err;
2235 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2236 err = got_error_from_errno("asprintf");
2237 *refname = NULL;
2239 free(uuidstr);
2240 return err;
2243 const struct got_error *
2244 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2246 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2249 static const struct got_error *
2250 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2252 return get_ref_name(refname, worktree,
2253 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2256 static const struct got_error *
2257 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2259 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2269 static const struct got_error *
2270 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2290 static const struct got_error *
2291 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2293 return get_ref_name(refname, worktree,
2294 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2297 static const struct got_error *
2298 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2300 return get_ref_name(refname, worktree,
2301 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2304 const struct got_error *
2305 got_worktree_get_histedit_script_path(char **path,
2306 struct got_worktree *worktree)
2308 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2309 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2310 *path = NULL;
2311 return got_error_from_errno("asprintf");
2313 return NULL;
2316 static const struct got_error *
2317 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree,
2320 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2323 static const struct got_error *
2324 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree,
2327 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2331 * Prevent Git's garbage collector from deleting our base commit by
2332 * setting a reference to our base commit's ID.
2334 static const struct got_error *
2335 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2337 const struct got_error *err = NULL;
2338 struct got_reference *ref = NULL;
2339 char *refname;
2341 err = got_worktree_get_base_ref_name(&refname, worktree);
2342 if (err)
2343 return err;
2345 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2346 if (err)
2347 goto done;
2349 err = got_ref_write(ref, repo);
2350 done:
2351 free(refname);
2352 if (ref)
2353 got_ref_close(ref);
2354 return err;
2357 static const struct got_error *
2358 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2360 const struct got_error *err = NULL;
2362 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2363 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2364 err = got_error_from_errno("asprintf");
2365 *fileindex_path = NULL;
2367 return err;
2371 static const struct got_error *
2372 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2373 struct got_worktree *worktree)
2375 const struct got_error *err = NULL;
2376 FILE *index = NULL;
2378 *fileindex_path = NULL;
2379 *fileindex = got_fileindex_alloc();
2380 if (*fileindex == NULL)
2381 return got_error_from_errno("got_fileindex_alloc");
2383 err = get_fileindex_path(fileindex_path, worktree);
2384 if (err)
2385 goto done;
2387 index = fopen(*fileindex_path, "rbe");
2388 if (index == NULL) {
2389 if (errno != ENOENT)
2390 err = got_error_from_errno2("fopen", *fileindex_path);
2391 } else {
2392 err = got_fileindex_read(*fileindex, index);
2393 if (fclose(index) == EOF && err == NULL)
2394 err = got_error_from_errno("fclose");
2396 done:
2397 if (err) {
2398 free(*fileindex_path);
2399 *fileindex_path = NULL;
2400 got_fileindex_free(*fileindex);
2401 *fileindex = NULL;
2403 return err;
2406 struct bump_base_commit_id_arg {
2407 struct got_object_id *base_commit_id;
2408 const char *path;
2409 size_t path_len;
2410 const char *entry_name;
2411 got_worktree_checkout_cb progress_cb;
2412 void *progress_arg;
2415 /* Bump base commit ID of all files within an updated part of the work tree. */
2416 static const struct got_error *
2417 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2419 const struct got_error *err;
2420 struct bump_base_commit_id_arg *a = arg;
2422 if (a->entry_name) {
2423 if (strcmp(ie->path, a->path) != 0)
2424 return NULL;
2425 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2426 return NULL;
2428 if (got_fileindex_entry_was_skipped(ie))
2429 return NULL;
2431 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2432 SHA1_DIGEST_LENGTH) == 0)
2433 return NULL;
2435 if (a->progress_cb) {
2436 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2437 ie->path);
2438 if (err)
2439 return err;
2441 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2442 return NULL;
2445 static const struct got_error *
2446 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2447 struct got_fileindex *fileindex,
2448 got_worktree_checkout_cb progress_cb, void *progress_arg)
2450 struct bump_base_commit_id_arg bbc_arg;
2452 bbc_arg.base_commit_id = worktree->base_commit_id;
2453 bbc_arg.entry_name = NULL;
2454 bbc_arg.path = "";
2455 bbc_arg.path_len = 0;
2456 bbc_arg.progress_cb = progress_cb;
2457 bbc_arg.progress_arg = progress_arg;
2459 return got_fileindex_for_each_entry_safe(fileindex,
2460 bump_base_commit_id, &bbc_arg);
2463 static const struct got_error *
2464 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2466 const struct got_error *err = NULL;
2467 char *new_fileindex_path = NULL;
2468 FILE *new_index = NULL;
2469 struct timespec timeout;
2471 err = got_opentemp_named(&new_fileindex_path, &new_index,
2472 fileindex_path);
2473 if (err)
2474 goto done;
2476 err = got_fileindex_write(fileindex, new_index);
2477 if (err)
2478 goto done;
2480 if (rename(new_fileindex_path, fileindex_path) != 0) {
2481 err = got_error_from_errno3("rename", new_fileindex_path,
2482 fileindex_path);
2483 unlink(new_fileindex_path);
2487 * Sleep for a short amount of time to ensure that files modified after
2488 * this program exits have a different time stamp from the one which
2489 * was recorded in the file index.
2491 timeout.tv_sec = 0;
2492 timeout.tv_nsec = 1;
2493 nanosleep(&timeout, NULL);
2494 done:
2495 if (new_index)
2496 fclose(new_index);
2497 free(new_fileindex_path);
2498 return err;
2501 static const struct got_error *
2502 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2503 struct got_object_id **tree_id, const char *wt_relpath,
2504 struct got_commit_object *base_commit, struct got_worktree *worktree,
2505 struct got_repository *repo)
2507 const struct got_error *err = NULL;
2508 struct got_object_id *id = NULL;
2509 char *in_repo_path = NULL;
2510 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2512 *entry_type = GOT_OBJ_TYPE_ANY;
2513 *tree_relpath = NULL;
2514 *tree_id = NULL;
2516 if (wt_relpath[0] == '\0') {
2517 /* Check out all files within the work tree. */
2518 *entry_type = GOT_OBJ_TYPE_TREE;
2519 *tree_relpath = strdup("");
2520 if (*tree_relpath == NULL) {
2521 err = got_error_from_errno("strdup");
2522 goto done;
2524 err = got_object_id_by_path(tree_id, repo, base_commit,
2525 worktree->path_prefix);
2526 if (err)
2527 goto done;
2528 return NULL;
2531 /* Check out a subset of files in the work tree. */
2533 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2534 is_root_wt ? "" : "/", wt_relpath) == -1) {
2535 err = got_error_from_errno("asprintf");
2536 goto done;
2539 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2540 if (err)
2541 goto done;
2543 free(in_repo_path);
2544 in_repo_path = NULL;
2546 err = got_object_get_type(entry_type, repo, id);
2547 if (err)
2548 goto done;
2550 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2551 /* Check out a single file. */
2552 if (strchr(wt_relpath, '/') == NULL) {
2553 /* Check out a single file in work tree's root dir. */
2554 in_repo_path = strdup(worktree->path_prefix);
2555 if (in_repo_path == NULL) {
2556 err = got_error_from_errno("strdup");
2557 goto done;
2559 *tree_relpath = strdup("");
2560 if (*tree_relpath == NULL) {
2561 err = got_error_from_errno("strdup");
2562 goto done;
2564 } else {
2565 /* Check out a single file in a subdirectory. */
2566 err = got_path_dirname(tree_relpath, wt_relpath);
2567 if (err)
2568 return err;
2569 if (asprintf(&in_repo_path, "%s%s%s",
2570 worktree->path_prefix, is_root_wt ? "" : "/",
2571 *tree_relpath) == -1) {
2572 err = got_error_from_errno("asprintf");
2573 goto done;
2576 err = got_object_id_by_path(tree_id, repo,
2577 base_commit, in_repo_path);
2578 } else {
2579 /* Check out all files within a subdirectory. */
2580 *tree_id = got_object_id_dup(id);
2581 if (*tree_id == NULL) {
2582 err = got_error_from_errno("got_object_id_dup");
2583 goto done;
2585 *tree_relpath = strdup(wt_relpath);
2586 if (*tree_relpath == NULL) {
2587 err = got_error_from_errno("strdup");
2588 goto done;
2591 done:
2592 free(id);
2593 free(in_repo_path);
2594 if (err) {
2595 *entry_type = GOT_OBJ_TYPE_ANY;
2596 free(*tree_relpath);
2597 *tree_relpath = NULL;
2598 free(*tree_id);
2599 *tree_id = NULL;
2601 return err;
2604 static const struct got_error *
2605 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2606 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2607 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2608 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2610 const struct got_error *err = NULL;
2611 struct got_commit_object *commit = NULL;
2612 struct got_tree_object *tree = NULL;
2613 struct got_fileindex_diff_tree_cb diff_cb;
2614 struct diff_cb_arg arg;
2616 err = ref_base_commit(worktree, repo);
2617 if (err) {
2618 if (!(err->code == GOT_ERR_ERRNO &&
2619 (errno == EACCES || errno == EROFS)))
2620 goto done;
2621 err = (*progress_cb)(progress_arg,
2622 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2623 if (err)
2624 return err;
2627 err = got_object_open_as_commit(&commit, repo,
2628 worktree->base_commit_id);
2629 if (err)
2630 goto done;
2632 err = got_object_open_as_tree(&tree, repo, tree_id);
2633 if (err)
2634 goto done;
2636 if (entry_name &&
2637 got_object_tree_find_entry(tree, entry_name) == NULL) {
2638 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2639 goto done;
2642 diff_cb.diff_old_new = diff_old_new;
2643 diff_cb.diff_old = diff_old;
2644 diff_cb.diff_new = diff_new;
2645 arg.fileindex = fileindex;
2646 arg.worktree = worktree;
2647 arg.repo = repo;
2648 arg.progress_cb = progress_cb;
2649 arg.progress_arg = progress_arg;
2650 arg.cancel_cb = cancel_cb;
2651 arg.cancel_arg = cancel_arg;
2652 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2653 entry_name, repo, &diff_cb, &arg);
2654 done:
2655 if (tree)
2656 got_object_tree_close(tree);
2657 if (commit)
2658 got_object_commit_close(commit);
2659 return err;
2662 const struct got_error *
2663 got_worktree_checkout_files(struct got_worktree *worktree,
2664 struct got_pathlist_head *paths, struct got_repository *repo,
2665 got_worktree_checkout_cb progress_cb, void *progress_arg,
2666 got_cancel_cb cancel_cb, void *cancel_arg)
2668 const struct got_error *err = NULL, *sync_err, *unlockerr;
2669 struct got_commit_object *commit = NULL;
2670 struct got_tree_object *tree = NULL;
2671 struct got_fileindex *fileindex = NULL;
2672 char *fileindex_path = NULL;
2673 struct got_pathlist_entry *pe;
2674 struct tree_path_data {
2675 STAILQ_ENTRY(tree_path_data) entry;
2676 struct got_object_id *tree_id;
2677 int entry_type;
2678 char *relpath;
2679 char *entry_name;
2680 } *tpd = NULL;
2681 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2683 STAILQ_INIT(&tree_paths);
2685 err = lock_worktree(worktree, LOCK_EX);
2686 if (err)
2687 return err;
2689 err = got_object_open_as_commit(&commit, repo,
2690 worktree->base_commit_id);
2691 if (err)
2692 goto done;
2694 /* Map all specified paths to in-repository trees. */
2695 TAILQ_FOREACH(pe, paths, entry) {
2696 tpd = malloc(sizeof(*tpd));
2697 if (tpd == NULL) {
2698 err = got_error_from_errno("malloc");
2699 goto done;
2702 err = find_tree_entry_for_checkout(&tpd->entry_type,
2703 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2704 worktree, repo);
2705 if (err) {
2706 free(tpd);
2707 goto done;
2710 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2711 err = got_path_basename(&tpd->entry_name, pe->path);
2712 if (err) {
2713 free(tpd->relpath);
2714 free(tpd->tree_id);
2715 free(tpd);
2716 goto done;
2718 } else
2719 tpd->entry_name = NULL;
2721 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2725 * Read the file index.
2726 * Checking out files is supposed to be an idempotent operation.
2727 * If the on-disk file index is incomplete we will try to complete it.
2729 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2730 if (err)
2731 goto done;
2733 tpd = STAILQ_FIRST(&tree_paths);
2734 TAILQ_FOREACH(pe, paths, entry) {
2735 struct bump_base_commit_id_arg bbc_arg;
2737 err = checkout_files(worktree, fileindex, tpd->relpath,
2738 tpd->tree_id, tpd->entry_name, repo,
2739 progress_cb, progress_arg, cancel_cb, cancel_arg);
2740 if (err)
2741 break;
2743 bbc_arg.base_commit_id = worktree->base_commit_id;
2744 bbc_arg.entry_name = tpd->entry_name;
2745 bbc_arg.path = pe->path;
2746 bbc_arg.path_len = pe->path_len;
2747 bbc_arg.progress_cb = progress_cb;
2748 bbc_arg.progress_arg = progress_arg;
2749 err = got_fileindex_for_each_entry_safe(fileindex,
2750 bump_base_commit_id, &bbc_arg);
2751 if (err)
2752 break;
2754 tpd = STAILQ_NEXT(tpd, entry);
2756 sync_err = sync_fileindex(fileindex, fileindex_path);
2757 if (sync_err && err == NULL)
2758 err = sync_err;
2759 done:
2760 free(fileindex_path);
2761 if (tree)
2762 got_object_tree_close(tree);
2763 if (commit)
2764 got_object_commit_close(commit);
2765 if (fileindex)
2766 got_fileindex_free(fileindex);
2767 while (!STAILQ_EMPTY(&tree_paths)) {
2768 tpd = STAILQ_FIRST(&tree_paths);
2769 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2770 free(tpd->relpath);
2771 free(tpd->tree_id);
2772 free(tpd);
2774 unlockerr = lock_worktree(worktree, LOCK_SH);
2775 if (unlockerr && err == NULL)
2776 err = unlockerr;
2777 return err;
2780 struct merge_file_cb_arg {
2781 struct got_worktree *worktree;
2782 struct got_fileindex *fileindex;
2783 got_worktree_checkout_cb progress_cb;
2784 void *progress_arg;
2785 got_cancel_cb cancel_cb;
2786 void *cancel_arg;
2787 const char *label_orig;
2788 struct got_object_id *commit_id2;
2789 int allow_bad_symlinks;
2792 static const struct got_error *
2793 merge_file_cb(void *arg, struct got_blob_object *blob1,
2794 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2795 struct got_object_id *id1, struct got_object_id *id2,
2796 const char *path1, const char *path2,
2797 mode_t mode1, mode_t mode2, struct got_repository *repo)
2799 static const struct got_error *err = NULL;
2800 struct merge_file_cb_arg *a = arg;
2801 struct got_fileindex_entry *ie;
2802 char *ondisk_path = NULL;
2803 struct stat sb;
2804 unsigned char status;
2805 int local_changes_subsumed;
2806 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2807 char *id_str = NULL, *label_deriv2 = NULL;
2809 if (blob1 && blob2) {
2810 ie = got_fileindex_entry_get(a->fileindex, path2,
2811 strlen(path2));
2812 if (ie == NULL)
2813 return (*a->progress_cb)(a->progress_arg,
2814 GOT_STATUS_MISSING, path2);
2816 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2817 path2) == -1)
2818 return got_error_from_errno("asprintf");
2820 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2821 repo);
2822 if (err)
2823 goto done;
2825 if (status == GOT_STATUS_DELETE) {
2826 err = (*a->progress_cb)(a->progress_arg,
2827 GOT_STATUS_MERGE, path2);
2828 goto done;
2830 if (status != GOT_STATUS_NO_CHANGE &&
2831 status != GOT_STATUS_MODIFY &&
2832 status != GOT_STATUS_CONFLICT &&
2833 status != GOT_STATUS_ADD) {
2834 err = (*a->progress_cb)(a->progress_arg, status, path2);
2835 goto done;
2838 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2839 char *link_target2;
2840 err = got_object_blob_read_to_str(&link_target2, blob2);
2841 if (err)
2842 goto done;
2843 err = merge_symlink(a->worktree, blob1, ondisk_path,
2844 path2, a->label_orig, link_target2, a->commit_id2,
2845 repo, a->progress_cb, a->progress_arg);
2846 free(link_target2);
2847 } else {
2848 int fd;
2850 f_orig = got_opentemp();
2851 if (f_orig == NULL) {
2852 err = got_error_from_errno("got_opentemp");
2853 goto done;
2855 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2856 f_orig, blob1);
2857 if (err)
2858 goto done;
2860 f_deriv2 = got_opentemp();
2861 if (f_deriv2 == NULL)
2862 goto done;
2863 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2864 f_deriv2, blob2);
2865 if (err)
2866 goto done;
2868 fd = open(ondisk_path,
2869 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2870 if (fd == -1) {
2871 err = got_error_from_errno2("open",
2872 ondisk_path);
2873 goto done;
2875 f_deriv = fdopen(fd, "r");
2876 if (f_deriv == NULL) {
2877 err = got_error_from_errno2("fdopen",
2878 ondisk_path);
2879 close(fd);
2880 goto done;
2882 err = got_object_id_str(&id_str, a->commit_id2);
2883 if (err)
2884 goto done;
2885 if (asprintf(&label_deriv2, "%s: commit %s",
2886 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2887 err = got_error_from_errno("asprintf");
2888 goto done;
2890 err = merge_file(&local_changes_subsumed, a->worktree,
2891 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2892 sb.st_mode, a->label_orig, NULL, label_deriv2,
2893 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2894 a->progress_cb, a->progress_arg);
2896 } else if (blob1) {
2897 ie = got_fileindex_entry_get(a->fileindex, path1,
2898 strlen(path1));
2899 if (ie == NULL)
2900 return (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_MISSING, path1);
2903 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2904 path1) == -1)
2905 return got_error_from_errno("asprintf");
2907 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2908 repo);
2909 if (err)
2910 goto done;
2912 switch (status) {
2913 case GOT_STATUS_NO_CHANGE:
2914 err = (*a->progress_cb)(a->progress_arg,
2915 GOT_STATUS_DELETE, path1);
2916 if (err)
2917 goto done;
2918 err = remove_ondisk_file(a->worktree->root_path, path1);
2919 if (err)
2920 goto done;
2921 if (ie)
2922 got_fileindex_entry_mark_deleted_from_disk(ie);
2923 break;
2924 case GOT_STATUS_DELETE:
2925 case GOT_STATUS_MISSING:
2926 err = (*a->progress_cb)(a->progress_arg,
2927 GOT_STATUS_DELETE, path1);
2928 if (err)
2929 goto done;
2930 if (ie)
2931 got_fileindex_entry_mark_deleted_from_disk(ie);
2932 break;
2933 case GOT_STATUS_ADD: {
2934 struct got_object_id *id;
2935 FILE *blob1_f;
2936 off_t blob1_size;
2938 * Delete the added file only if its content already
2939 * exists in the repository.
2941 err = got_object_blob_file_create(&id, &blob1_f,
2942 &blob1_size, path1);
2943 if (err)
2944 goto done;
2945 if (got_object_id_cmp(id, id1) == 0) {
2946 err = (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_DELETE, path1);
2948 if (err)
2949 goto done;
2950 err = remove_ondisk_file(a->worktree->root_path,
2951 path1);
2952 if (err)
2953 goto done;
2954 if (ie)
2955 got_fileindex_entry_remove(a->fileindex,
2956 ie);
2957 } else {
2958 err = (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_CANNOT_DELETE, path1);
2961 if (fclose(blob1_f) == EOF && err == NULL)
2962 err = got_error_from_errno("fclose");
2963 free(id);
2964 if (err)
2965 goto done;
2966 break;
2968 case GOT_STATUS_MODIFY:
2969 case GOT_STATUS_CONFLICT:
2970 err = (*a->progress_cb)(a->progress_arg,
2971 GOT_STATUS_CANNOT_DELETE, path1);
2972 if (err)
2973 goto done;
2974 break;
2975 case GOT_STATUS_OBSTRUCTED:
2976 err = (*a->progress_cb)(a->progress_arg, status, path1);
2977 if (err)
2978 goto done;
2979 break;
2980 default:
2981 break;
2983 } else if (blob2) {
2984 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2985 path2) == -1)
2986 return got_error_from_errno("asprintf");
2987 ie = got_fileindex_entry_get(a->fileindex, path2,
2988 strlen(path2));
2989 if (ie) {
2990 err = get_file_status(&status, &sb, ie, ondisk_path,
2991 -1, NULL, repo);
2992 if (err)
2993 goto done;
2994 if (status != GOT_STATUS_NO_CHANGE &&
2995 status != GOT_STATUS_MODIFY &&
2996 status != GOT_STATUS_CONFLICT &&
2997 status != GOT_STATUS_ADD) {
2998 err = (*a->progress_cb)(a->progress_arg,
2999 status, path2);
3000 goto done;
3002 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3003 char *link_target2;
3004 err = got_object_blob_read_to_str(&link_target2,
3005 blob2);
3006 if (err)
3007 goto done;
3008 err = merge_symlink(a->worktree, NULL,
3009 ondisk_path, path2, a->label_orig,
3010 link_target2, a->commit_id2, repo,
3011 a->progress_cb, a->progress_arg);
3012 free(link_target2);
3013 } else if (S_ISREG(sb.st_mode)) {
3014 err = merge_blob(&local_changes_subsumed,
3015 a->worktree, NULL, ondisk_path, path2,
3016 sb.st_mode, a->label_orig, blob2,
3017 a->commit_id2, repo, a->progress_cb,
3018 a->progress_arg);
3019 } else {
3020 err = got_error_path(ondisk_path,
3021 GOT_ERR_FILE_OBSTRUCTED);
3023 if (err)
3024 goto done;
3025 if (status == GOT_STATUS_DELETE) {
3026 err = got_fileindex_entry_update(ie,
3027 a->worktree->root_fd, path2, blob2->id.sha1,
3028 a->worktree->base_commit_id->sha1, 0);
3029 if (err)
3030 goto done;
3032 } else {
3033 int is_bad_symlink = 0;
3034 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3035 if (S_ISLNK(mode2)) {
3036 err = install_symlink(&is_bad_symlink,
3037 a->worktree, ondisk_path, path2, blob2, 0,
3038 0, 1, a->allow_bad_symlinks, repo,
3039 a->progress_cb, a->progress_arg);
3040 } else {
3041 err = install_blob(a->worktree, ondisk_path, path2,
3042 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3043 a->progress_cb, a->progress_arg);
3045 if (err)
3046 goto done;
3047 err = got_fileindex_entry_alloc(&ie, path2);
3048 if (err)
3049 goto done;
3050 err = got_fileindex_entry_update(ie,
3051 a->worktree->root_fd, path2, NULL, NULL, 1);
3052 if (err) {
3053 got_fileindex_entry_free(ie);
3054 goto done;
3056 err = got_fileindex_entry_add(a->fileindex, ie);
3057 if (err) {
3058 got_fileindex_entry_free(ie);
3059 goto done;
3061 if (is_bad_symlink) {
3062 got_fileindex_entry_filetype_set(ie,
3063 GOT_FILEIDX_MODE_BAD_SYMLINK);
3067 done:
3068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3069 err = got_error_from_errno("fclose");
3070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3071 err = got_error_from_errno("fclose");
3072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3073 err = got_error_from_errno("fclose");
3074 free(id_str);
3075 free(label_deriv2);
3076 free(ondisk_path);
3077 return err;
3080 static const struct got_error *
3081 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3083 struct got_worktree *worktree = arg;
3085 /* Reject merges into a work tree with mixed base commits. */
3086 if (got_fileindex_entry_has_commit(ie) &&
3087 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3088 SHA1_DIGEST_LENGTH) != 0)
3089 return got_error(GOT_ERR_MIXED_COMMITS);
3091 return NULL;
3094 struct check_merge_conflicts_arg {
3095 struct got_worktree *worktree;
3096 struct got_fileindex *fileindex;
3097 struct got_repository *repo;
3100 static const struct got_error *
3101 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3102 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3103 struct got_object_id *id1, struct got_object_id *id2,
3104 const char *path1, const char *path2,
3105 mode_t mode1, mode_t mode2, struct got_repository *repo)
3107 const struct got_error *err = NULL;
3108 struct check_merge_conflicts_arg *a = arg;
3109 unsigned char status;
3110 struct stat sb;
3111 struct got_fileindex_entry *ie;
3112 const char *path = path2 ? path2 : path1;
3113 struct got_object_id *id = id2 ? id2 : id1;
3114 char *ondisk_path;
3116 if (id == NULL)
3117 return NULL;
3119 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3120 if (ie == NULL)
3121 return NULL;
3123 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3124 == -1)
3125 return got_error_from_errno("asprintf");
3127 /* Reject merges into a work tree with conflicted files. */
3128 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3129 free(ondisk_path);
3130 if (err)
3131 return err;
3132 if (status == GOT_STATUS_CONFLICT)
3133 return got_error(GOT_ERR_CONFLICTS);
3135 return NULL;
3138 static const struct got_error *
3139 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3140 const char *fileindex_path, struct got_object_id *commit_id1,
3141 struct got_object_id *commit_id2, struct got_repository *repo,
3142 got_worktree_checkout_cb progress_cb, void *progress_arg,
3143 got_cancel_cb cancel_cb, void *cancel_arg)
3145 const struct got_error *err = NULL, *sync_err;
3146 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3147 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3148 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3149 struct check_merge_conflicts_arg cmc_arg;
3150 struct merge_file_cb_arg arg;
3151 char *label_orig = NULL;
3152 FILE *f1 = NULL, *f2 = NULL;
3153 int fd1 = -1, fd2 = -1;
3155 if (commit_id1) {
3156 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3157 if (err)
3158 goto done;
3159 err = got_object_id_by_path(&tree_id1, repo, commit1,
3160 worktree->path_prefix);
3161 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3162 goto done;
3164 if (tree_id1) {
3165 char *id_str;
3167 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3168 if (err)
3169 goto done;
3171 err = got_object_id_str(&id_str, commit_id1);
3172 if (err)
3173 goto done;
3175 if (asprintf(&label_orig, "%s: commit %s",
3176 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3177 err = got_error_from_errno("asprintf");
3178 free(id_str);
3179 goto done;
3181 free(id_str);
3183 f1 = got_opentemp();
3184 if (f1 == NULL) {
3185 err = got_error_from_errno("got_opentemp");
3186 goto done;
3190 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3191 if (err)
3192 goto done;
3194 err = got_object_id_by_path(&tree_id2, repo, commit2,
3195 worktree->path_prefix);
3196 if (err)
3197 goto done;
3199 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3200 if (err)
3201 goto done;
3203 f2 = got_opentemp();
3204 if (f2 == NULL) {
3205 err = got_error_from_errno("got_opentemp");
3206 goto done;
3209 fd1 = got_opentempfd();
3210 if (fd1 == -1) {
3211 err = got_error_from_errno("got_opentempfd");
3212 goto done;
3215 fd2 = got_opentempfd();
3216 if (fd2 == -1) {
3217 err = got_error_from_errno("got_opentempfd");
3218 goto done;
3221 cmc_arg.worktree = worktree;
3222 cmc_arg.fileindex = fileindex;
3223 cmc_arg.repo = repo;
3224 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 check_merge_conflicts, &cmc_arg, 0);
3226 if (err)
3227 goto done;
3229 arg.worktree = worktree;
3230 arg.fileindex = fileindex;
3231 arg.progress_cb = progress_cb;
3232 arg.progress_arg = progress_arg;
3233 arg.cancel_cb = cancel_cb;
3234 arg.cancel_arg = cancel_arg;
3235 arg.label_orig = label_orig;
3236 arg.commit_id2 = commit_id2;
3237 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3238 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3239 merge_file_cb, &arg, 1);
3240 sync_err = sync_fileindex(fileindex, fileindex_path);
3241 if (sync_err && err == NULL)
3242 err = sync_err;
3243 done:
3244 if (commit1)
3245 got_object_commit_close(commit1);
3246 if (commit2)
3247 got_object_commit_close(commit2);
3248 if (tree1)
3249 got_object_tree_close(tree1);
3250 if (tree2)
3251 got_object_tree_close(tree2);
3252 if (f1 && fclose(f1) == EOF && err == NULL)
3253 err = got_error_from_errno("fclose");
3254 if (f2 && fclose(f2) == EOF && err == NULL)
3255 err = got_error_from_errno("fclose");
3256 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3257 err = got_error_from_errno("close");
3258 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3259 err = got_error_from_errno("close");
3260 free(label_orig);
3261 return err;
3264 const struct got_error *
3265 got_worktree_merge_files(struct got_worktree *worktree,
3266 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3267 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3268 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3270 const struct got_error *err, *unlockerr;
3271 char *fileindex_path = NULL;
3272 struct got_fileindex *fileindex = NULL;
3274 err = lock_worktree(worktree, LOCK_EX);
3275 if (err)
3276 return err;
3278 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3279 if (err)
3280 goto done;
3282 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3283 worktree);
3284 if (err)
3285 goto done;
3287 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3288 commit_id2, repo, progress_cb, progress_arg,
3289 cancel_cb, cancel_arg);
3290 done:
3291 if (fileindex)
3292 got_fileindex_free(fileindex);
3293 free(fileindex_path);
3294 unlockerr = lock_worktree(worktree, LOCK_SH);
3295 if (unlockerr && err == NULL)
3296 err = unlockerr;
3297 return err;
3300 struct diff_dir_cb_arg {
3301 struct got_fileindex *fileindex;
3302 struct got_worktree *worktree;
3303 const char *status_path;
3304 size_t status_path_len;
3305 struct got_repository *repo;
3306 got_worktree_status_cb status_cb;
3307 void *status_arg;
3308 got_cancel_cb cancel_cb;
3309 void *cancel_arg;
3310 /* A pathlist containing per-directory pathlists of ignore patterns. */
3311 struct got_pathlist_head *ignores;
3312 int report_unchanged;
3313 int no_ignores;
3316 static const struct got_error *
3317 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3318 int dirfd, const char *de_name,
3319 got_worktree_status_cb status_cb, void *status_arg,
3320 struct got_repository *repo, int report_unchanged)
3322 const struct got_error *err = NULL;
3323 unsigned char status = GOT_STATUS_NO_CHANGE;
3324 unsigned char staged_status = get_staged_status(ie);
3325 struct stat sb;
3326 struct got_object_id blob_id, commit_id, staged_blob_id;
3327 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3328 struct got_object_id *staged_blob_idp = NULL;
3330 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3331 if (err)
3332 return err;
3334 if (status == GOT_STATUS_NO_CHANGE &&
3335 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3336 return NULL;
3338 if (got_fileindex_entry_has_blob(ie)) {
3339 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3340 blob_idp = &blob_id;
3342 if (got_fileindex_entry_has_commit(ie)) {
3343 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3344 commit_idp = &commit_id;
3346 if (staged_status == GOT_STATUS_ADD ||
3347 staged_status == GOT_STATUS_MODIFY) {
3348 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3349 SHA1_DIGEST_LENGTH);
3350 staged_blob_idp = &staged_blob_id;
3353 return (*status_cb)(status_arg, status, staged_status,
3354 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3357 static const struct got_error *
3358 status_old_new(void *arg, struct got_fileindex_entry *ie,
3359 struct dirent *de, const char *parent_path, int dirfd)
3361 const struct got_error *err = NULL;
3362 struct diff_dir_cb_arg *a = arg;
3363 char *abspath;
3365 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3366 return got_error(GOT_ERR_CANCELLED);
3368 if (got_path_cmp(parent_path, a->status_path,
3369 strlen(parent_path), a->status_path_len) != 0 &&
3370 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3371 return NULL;
3373 if (parent_path[0]) {
3374 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3375 parent_path, de->d_name) == -1)
3376 return got_error_from_errno("asprintf");
3377 } else {
3378 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3379 de->d_name) == -1)
3380 return got_error_from_errno("asprintf");
3383 err = report_file_status(ie, abspath, dirfd, de->d_name,
3384 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3385 free(abspath);
3386 return err;
3389 static const struct got_error *
3390 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3392 struct diff_dir_cb_arg *a = arg;
3393 struct got_object_id blob_id, commit_id;
3394 unsigned char status;
3396 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3397 return got_error(GOT_ERR_CANCELLED);
3399 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3400 return NULL;
3402 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3403 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3404 if (got_fileindex_entry_has_file_on_disk(ie))
3405 status = GOT_STATUS_MISSING;
3406 else
3407 status = GOT_STATUS_DELETE;
3408 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3409 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3412 static void
3413 free_ignorelist(struct got_pathlist_head *ignorelist)
3415 struct got_pathlist_entry *pe;
3417 TAILQ_FOREACH(pe, ignorelist, entry)
3418 free((char *)pe->path);
3419 got_pathlist_free(ignorelist);
3422 static void
3423 free_ignores(struct got_pathlist_head *ignores)
3425 struct got_pathlist_entry *pe;
3427 TAILQ_FOREACH(pe, ignores, entry) {
3428 struct got_pathlist_head *ignorelist = pe->data;
3429 free_ignorelist(ignorelist);
3430 free((char *)pe->path);
3432 got_pathlist_free(ignores);
3435 static const struct got_error *
3436 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3438 const struct got_error *err = NULL;
3439 struct got_pathlist_entry *pe = NULL;
3440 struct got_pathlist_head *ignorelist;
3441 char *line = NULL, *pattern, *dirpath = NULL;
3442 size_t linesize = 0;
3443 ssize_t linelen;
3445 ignorelist = calloc(1, sizeof(*ignorelist));
3446 if (ignorelist == NULL)
3447 return got_error_from_errno("calloc");
3448 TAILQ_INIT(ignorelist);
3450 while ((linelen = getline(&line, &linesize, f)) != -1) {
3451 if (linelen > 0 && line[linelen - 1] == '\n')
3452 line[linelen - 1] = '\0';
3454 /* Git's ignores may contain comments. */
3455 if (line[0] == '#')
3456 continue;
3458 /* Git's negated patterns are not (yet?) supported. */
3459 if (line[0] == '!')
3460 continue;
3462 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3463 line) == -1) {
3464 err = got_error_from_errno("asprintf");
3465 goto done;
3467 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3468 if (err)
3469 goto done;
3471 if (ferror(f)) {
3472 err = got_error_from_errno("getline");
3473 goto done;
3476 dirpath = strdup(path);
3477 if (dirpath == NULL) {
3478 err = got_error_from_errno("strdup");
3479 goto done;
3481 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3482 done:
3483 free(line);
3484 if (err || pe == NULL) {
3485 free(dirpath);
3486 free_ignorelist(ignorelist);
3488 return err;
3491 static int
3492 match_ignores(struct got_pathlist_head *ignores, const char *path)
3494 struct got_pathlist_entry *pe;
3496 /* Handle patterns which match in all directories. */
3497 TAILQ_FOREACH(pe, ignores, entry) {
3498 struct got_pathlist_head *ignorelist = pe->data;
3499 struct got_pathlist_entry *pi;
3501 TAILQ_FOREACH(pi, ignorelist, entry) {
3502 const char *p, *pattern = pi->path;
3504 if (strncmp(pattern, "**/", 3) != 0)
3505 continue;
3506 pattern += 3;
3507 p = path;
3508 while (*p) {
3509 if (fnmatch(pattern, p,
3510 FNM_PATHNAME | FNM_LEADING_DIR)) {
3511 /* Retry in next directory. */
3512 while (*p && *p != '/')
3513 p++;
3514 while (*p == '/')
3515 p++;
3516 continue;
3518 return 1;
3524 * The ignores pathlist contains ignore lists from children before
3525 * parents, so we can find the most specific ignorelist by walking
3526 * ignores backwards.
3528 pe = TAILQ_LAST(ignores, got_pathlist_head);
3529 while (pe) {
3530 if (got_path_is_child(path, pe->path, pe->path_len)) {
3531 struct got_pathlist_head *ignorelist = pe->data;
3532 struct got_pathlist_entry *pi;
3533 TAILQ_FOREACH(pi, ignorelist, entry) {
3534 const char *pattern = pi->path;
3535 int flags = FNM_LEADING_DIR;
3536 if (strstr(pattern, "/**/") == NULL)
3537 flags |= FNM_PATHNAME;
3538 if (fnmatch(pattern, path, flags))
3539 continue;
3540 return 1;
3543 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3546 return 0;
3549 static const struct got_error *
3550 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3551 const char *path, int dirfd, const char *ignores_filename)
3553 const struct got_error *err = NULL;
3554 char *ignorespath;
3555 int fd = -1;
3556 FILE *ignoresfile = NULL;
3558 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3559 path[0] ? "/" : "", ignores_filename) == -1)
3560 return got_error_from_errno("asprintf");
3562 if (dirfd != -1) {
3563 fd = openat(dirfd, ignores_filename,
3564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3565 if (fd == -1) {
3566 if (errno != ENOENT && errno != EACCES)
3567 err = got_error_from_errno2("openat",
3568 ignorespath);
3569 } else {
3570 ignoresfile = fdopen(fd, "r");
3571 if (ignoresfile == NULL)
3572 err = got_error_from_errno2("fdopen",
3573 ignorespath);
3574 else {
3575 fd = -1;
3576 err = read_ignores(ignores, path, ignoresfile);
3579 } else {
3580 ignoresfile = fopen(ignorespath, "re");
3581 if (ignoresfile == NULL) {
3582 if (errno != ENOENT && errno != EACCES)
3583 err = got_error_from_errno2("fopen",
3584 ignorespath);
3585 } else
3586 err = read_ignores(ignores, path, ignoresfile);
3589 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3590 err = got_error_from_errno2("fclose", path);
3591 if (fd != -1 && close(fd) == -1 && err == NULL)
3592 err = got_error_from_errno2("close", path);
3593 free(ignorespath);
3594 return err;
3597 static const struct got_error *
3598 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3599 int dirfd)
3601 const struct got_error *err = NULL;
3602 struct diff_dir_cb_arg *a = arg;
3603 char *path = NULL;
3605 if (ignore != NULL)
3606 *ignore = 0;
3608 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3609 return got_error(GOT_ERR_CANCELLED);
3611 if (parent_path[0]) {
3612 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3613 return got_error_from_errno("asprintf");
3614 } else {
3615 path = de->d_name;
3618 if (de->d_type == DT_DIR) {
3619 if (!a->no_ignores && ignore != NULL &&
3620 match_ignores(a->ignores, path))
3621 *ignore = 1;
3622 } else if (!match_ignores(a->ignores, path) &&
3623 got_path_is_child(path, a->status_path, a->status_path_len))
3624 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3626 if (parent_path[0])
3627 free(path);
3628 return err;
3631 static const struct got_error *
3632 status_traverse(void *arg, const char *path, int dirfd)
3634 const struct got_error *err = NULL;
3635 struct diff_dir_cb_arg *a = arg;
3637 if (a->no_ignores)
3638 return NULL;
3640 err = add_ignores(a->ignores, a->worktree->root_path,
3641 path, dirfd, ".cvsignore");
3642 if (err)
3643 return err;
3645 err = add_ignores(a->ignores, a->worktree->root_path, path,
3646 dirfd, ".gitignore");
3648 return err;
3651 static const struct got_error *
3652 report_single_file_status(const char *path, const char *ondisk_path,
3653 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3654 void *status_arg, struct got_repository *repo, int report_unchanged,
3655 struct got_pathlist_head *ignores, int no_ignores)
3657 struct got_fileindex_entry *ie;
3658 struct stat sb;
3660 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3661 if (ie)
3662 return report_file_status(ie, ondisk_path, -1, NULL,
3663 status_cb, status_arg, repo, report_unchanged);
3665 if (lstat(ondisk_path, &sb) == -1) {
3666 if (errno != ENOENT)
3667 return got_error_from_errno2("lstat", ondisk_path);
3668 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3669 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3672 if (!no_ignores && match_ignores(ignores, path))
3673 return NULL;
3675 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3676 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3677 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3679 return NULL;
3682 static const struct got_error *
3683 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3684 const char *root_path, const char *path)
3686 const struct got_error *err;
3687 char *parent_path, *next_parent_path = NULL;
3689 err = add_ignores(ignores, root_path, "", -1,
3690 ".cvsignore");
3691 if (err)
3692 return err;
3694 err = add_ignores(ignores, root_path, "", -1,
3695 ".gitignore");
3696 if (err)
3697 return err;
3699 err = got_path_dirname(&parent_path, path);
3700 if (err) {
3701 if (err->code == GOT_ERR_BAD_PATH)
3702 return NULL; /* cannot traverse parent */
3703 return err;
3705 for (;;) {
3706 err = add_ignores(ignores, root_path, parent_path, -1,
3707 ".cvsignore");
3708 if (err)
3709 break;
3710 err = add_ignores(ignores, root_path, parent_path, -1,
3711 ".gitignore");
3712 if (err)
3713 break;
3714 err = got_path_dirname(&next_parent_path, parent_path);
3715 if (err) {
3716 if (err->code == GOT_ERR_BAD_PATH)
3717 err = NULL; /* traversed everything */
3718 break;
3720 if (got_path_is_root_dir(parent_path))
3721 break;
3722 free(parent_path);
3723 parent_path = next_parent_path;
3724 next_parent_path = NULL;
3727 free(parent_path);
3728 free(next_parent_path);
3729 return err;
3732 static const struct got_error *
3733 worktree_status(struct got_worktree *worktree, const char *path,
3734 struct got_fileindex *fileindex, struct got_repository *repo,
3735 got_worktree_status_cb status_cb, void *status_arg,
3736 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3737 int report_unchanged)
3739 const struct got_error *err = NULL;
3740 int fd = -1;
3741 struct got_fileindex_diff_dir_cb fdiff_cb;
3742 struct diff_dir_cb_arg arg;
3743 char *ondisk_path = NULL;
3744 struct got_pathlist_head ignores;
3745 struct got_fileindex_entry *ie;
3747 TAILQ_INIT(&ignores);
3749 if (asprintf(&ondisk_path, "%s%s%s",
3750 worktree->root_path, path[0] ? "/" : "", path) == -1)
3751 return got_error_from_errno("asprintf");
3753 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3754 if (ie) {
3755 err = report_single_file_status(path, ondisk_path,
3756 fileindex, status_cb, status_arg, repo,
3757 report_unchanged, &ignores, no_ignores);
3758 goto done;
3761 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3762 if (fd == -1) {
3763 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3764 !got_err_open_nofollow_on_symlink())
3765 err = got_error_from_errno2("open", ondisk_path);
3766 else {
3767 if (!no_ignores) {
3768 err = add_ignores_from_parent_paths(&ignores,
3769 worktree->root_path, ondisk_path);
3770 if (err)
3771 goto done;
3773 err = report_single_file_status(path, ondisk_path,
3774 fileindex, status_cb, status_arg, repo,
3775 report_unchanged, &ignores, no_ignores);
3777 } else {
3778 fdiff_cb.diff_old_new = status_old_new;
3779 fdiff_cb.diff_old = status_old;
3780 fdiff_cb.diff_new = status_new;
3781 fdiff_cb.diff_traverse = status_traverse;
3782 arg.fileindex = fileindex;
3783 arg.worktree = worktree;
3784 arg.status_path = path;
3785 arg.status_path_len = strlen(path);
3786 arg.repo = repo;
3787 arg.status_cb = status_cb;
3788 arg.status_arg = status_arg;
3789 arg.cancel_cb = cancel_cb;
3790 arg.cancel_arg = cancel_arg;
3791 arg.report_unchanged = report_unchanged;
3792 arg.no_ignores = no_ignores;
3793 if (!no_ignores) {
3794 err = add_ignores_from_parent_paths(&ignores,
3795 worktree->root_path, path);
3796 if (err)
3797 goto done;
3799 arg.ignores = &ignores;
3800 err = got_fileindex_diff_dir(fileindex, fd,
3801 worktree->root_path, path, repo, &fdiff_cb, &arg);
3803 done:
3804 free_ignores(&ignores);
3805 if (fd != -1 && close(fd) == -1 && err == NULL)
3806 err = got_error_from_errno("close");
3807 free(ondisk_path);
3808 return err;
3811 const struct got_error *
3812 got_worktree_status(struct got_worktree *worktree,
3813 struct got_pathlist_head *paths, struct got_repository *repo,
3814 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3815 got_cancel_cb cancel_cb, void *cancel_arg)
3817 const struct got_error *err = NULL;
3818 char *fileindex_path = NULL;
3819 struct got_fileindex *fileindex = NULL;
3820 struct got_pathlist_entry *pe;
3822 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3823 if (err)
3824 return err;
3826 TAILQ_FOREACH(pe, paths, entry) {
3827 err = worktree_status(worktree, pe->path, fileindex, repo,
3828 status_cb, status_arg, cancel_cb, cancel_arg,
3829 no_ignores, 0);
3830 if (err)
3831 break;
3833 free(fileindex_path);
3834 got_fileindex_free(fileindex);
3835 return err;
3838 const struct got_error *
3839 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3840 const char *arg)
3842 const struct got_error *err = NULL;
3843 char *resolved = NULL, *cwd = NULL, *path = NULL;
3844 size_t len;
3845 struct stat sb;
3846 char *abspath = NULL;
3847 char canonpath[PATH_MAX];
3849 *wt_path = NULL;
3851 cwd = getcwd(NULL, 0);
3852 if (cwd == NULL)
3853 return got_error_from_errno("getcwd");
3855 if (lstat(arg, &sb) == -1) {
3856 if (errno != ENOENT) {
3857 err = got_error_from_errno2("lstat", arg);
3858 goto done;
3860 sb.st_mode = 0;
3862 if (S_ISLNK(sb.st_mode)) {
3864 * We cannot use realpath(3) with symlinks since we want to
3865 * operate on the symlink itself.
3866 * But we can make the path absolute, assuming it is relative
3867 * to the current working directory, and then canonicalize it.
3869 if (!got_path_is_absolute(arg)) {
3870 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3871 err = got_error_from_errno("asprintf");
3872 goto done;
3876 err = got_canonpath(abspath ? abspath : arg, canonpath,
3877 sizeof(canonpath));
3878 if (err)
3879 goto done;
3880 resolved = strdup(canonpath);
3881 if (resolved == NULL) {
3882 err = got_error_from_errno("strdup");
3883 goto done;
3885 } else {
3886 resolved = realpath(arg, NULL);
3887 if (resolved == NULL) {
3888 if (errno != ENOENT) {
3889 err = got_error_from_errno2("realpath", arg);
3890 goto done;
3892 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3893 err = got_error_from_errno("asprintf");
3894 goto done;
3896 err = got_canonpath(abspath, canonpath,
3897 sizeof(canonpath));
3898 if (err)
3899 goto done;
3900 resolved = strdup(canonpath);
3901 if (resolved == NULL) {
3902 err = got_error_from_errno("strdup");
3903 goto done;
3908 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3909 strlen(got_worktree_get_root_path(worktree)))) {
3910 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3911 goto done;
3914 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3915 err = got_path_skip_common_ancestor(&path,
3916 got_worktree_get_root_path(worktree), resolved);
3917 if (err)
3918 goto done;
3919 } else {
3920 path = strdup("");
3921 if (path == NULL) {
3922 err = got_error_from_errno("strdup");
3923 goto done;
3927 /* XXX status walk can't deal with trailing slash! */
3928 len = strlen(path);
3929 while (len > 0 && path[len - 1] == '/') {
3930 path[len - 1] = '\0';
3931 len--;
3933 done:
3934 free(abspath);
3935 free(resolved);
3936 free(cwd);
3937 if (err == NULL)
3938 *wt_path = path;
3939 else
3940 free(path);
3941 return err;
3944 struct schedule_addition_args {
3945 struct got_worktree *worktree;
3946 struct got_fileindex *fileindex;
3947 got_worktree_checkout_cb progress_cb;
3948 void *progress_arg;
3949 struct got_repository *repo;
3952 static const struct got_error *
3953 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3954 const char *relpath, struct got_object_id *blob_id,
3955 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3956 int dirfd, const char *de_name)
3958 struct schedule_addition_args *a = arg;
3959 const struct got_error *err = NULL;
3960 struct got_fileindex_entry *ie;
3961 struct stat sb;
3962 char *ondisk_path;
3964 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3965 relpath) == -1)
3966 return got_error_from_errno("asprintf");
3968 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3969 if (ie) {
3970 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3971 de_name, a->repo);
3972 if (err)
3973 goto done;
3974 /* Re-adding an existing entry is a no-op. */
3975 if (status == GOT_STATUS_ADD)
3976 goto done;
3977 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3978 if (err)
3979 goto done;
3982 if (status != GOT_STATUS_UNVERSIONED) {
3983 if (status == GOT_STATUS_NONEXISTENT)
3984 err = got_error_set_errno(ENOENT, ondisk_path);
3985 else
3986 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3987 goto done;
3990 err = got_fileindex_entry_alloc(&ie, relpath);
3991 if (err)
3992 goto done;
3993 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3994 relpath, NULL, NULL, 1);
3995 if (err) {
3996 got_fileindex_entry_free(ie);
3997 goto done;
3999 err = got_fileindex_entry_add(a->fileindex, ie);
4000 if (err) {
4001 got_fileindex_entry_free(ie);
4002 goto done;
4004 done:
4005 free(ondisk_path);
4006 if (err)
4007 return err;
4008 if (status == GOT_STATUS_ADD)
4009 return NULL;
4010 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4013 const struct got_error *
4014 got_worktree_schedule_add(struct got_worktree *worktree,
4015 struct got_pathlist_head *paths,
4016 got_worktree_checkout_cb progress_cb, void *progress_arg,
4017 struct got_repository *repo, int no_ignores)
4019 struct got_fileindex *fileindex = NULL;
4020 char *fileindex_path = NULL;
4021 const struct got_error *err = NULL, *sync_err, *unlockerr;
4022 struct got_pathlist_entry *pe;
4023 struct schedule_addition_args saa;
4025 err = lock_worktree(worktree, LOCK_EX);
4026 if (err)
4027 return err;
4029 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4030 if (err)
4031 goto done;
4033 saa.worktree = worktree;
4034 saa.fileindex = fileindex;
4035 saa.progress_cb = progress_cb;
4036 saa.progress_arg = progress_arg;
4037 saa.repo = repo;
4039 TAILQ_FOREACH(pe, paths, entry) {
4040 err = worktree_status(worktree, pe->path, fileindex, repo,
4041 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4042 if (err)
4043 break;
4045 sync_err = sync_fileindex(fileindex, fileindex_path);
4046 if (sync_err && err == NULL)
4047 err = sync_err;
4048 done:
4049 free(fileindex_path);
4050 if (fileindex)
4051 got_fileindex_free(fileindex);
4052 unlockerr = lock_worktree(worktree, LOCK_SH);
4053 if (unlockerr && err == NULL)
4054 err = unlockerr;
4055 return err;
4058 struct schedule_deletion_args {
4059 struct got_worktree *worktree;
4060 struct got_fileindex *fileindex;
4061 got_worktree_delete_cb progress_cb;
4062 void *progress_arg;
4063 struct got_repository *repo;
4064 int delete_local_mods;
4065 int keep_on_disk;
4066 int ignore_missing_paths;
4067 const char *status_codes;
4070 static const struct got_error *
4071 schedule_for_deletion(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct schedule_deletion_args *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_fileindex_entry *ie = NULL;
4079 struct stat sb;
4080 char *ondisk_path;
4082 if (status == GOT_STATUS_NONEXISTENT) {
4083 if (a->ignore_missing_paths)
4084 return NULL;
4085 return got_error_set_errno(ENOENT, relpath);
4088 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4089 if (ie == NULL)
4090 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4092 staged_status = get_staged_status(ie);
4093 if (staged_status != GOT_STATUS_NO_CHANGE) {
4094 if (staged_status == GOT_STATUS_DELETE)
4095 return NULL;
4096 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4099 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4100 relpath) == -1)
4101 return got_error_from_errno("asprintf");
4103 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4104 a->repo);
4105 if (err)
4106 goto done;
4108 if (a->status_codes) {
4109 size_t ncodes = strlen(a->status_codes);
4110 int i;
4111 for (i = 0; i < ncodes ; i++) {
4112 if (status == a->status_codes[i])
4113 break;
4115 if (i == ncodes) {
4116 /* Do not delete files in non-matching status. */
4117 free(ondisk_path);
4118 return NULL;
4120 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4121 a->status_codes[i] != GOT_STATUS_MISSING) {
4122 static char msg[64];
4123 snprintf(msg, sizeof(msg),
4124 "invalid status code '%c'", a->status_codes[i]);
4125 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4126 goto done;
4130 if (status != GOT_STATUS_NO_CHANGE) {
4131 if (status == GOT_STATUS_DELETE)
4132 goto done;
4133 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4134 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4135 goto done;
4137 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4138 err = got_error_set_errno(ENOENT, relpath);
4139 goto done;
4141 if (status != GOT_STATUS_MODIFY &&
4142 status != GOT_STATUS_MISSING) {
4143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4144 goto done;
4148 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4149 size_t root_len;
4151 if (dirfd != -1) {
4152 if (unlinkat(dirfd, de_name, 0) == -1) {
4153 err = got_error_from_errno2("unlinkat",
4154 ondisk_path);
4155 goto done;
4157 } else if (unlink(ondisk_path) == -1) {
4158 err = got_error_from_errno2("unlink", ondisk_path);
4159 goto done;
4162 root_len = strlen(a->worktree->root_path);
4163 do {
4164 char *parent;
4165 err = got_path_dirname(&parent, ondisk_path);
4166 if (err)
4167 goto done;
4168 free(ondisk_path);
4169 ondisk_path = parent;
4170 if (rmdir(ondisk_path) == -1) {
4171 if (errno != ENOTEMPTY)
4172 err = got_error_from_errno2("rmdir",
4173 ondisk_path);
4174 break;
4176 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 strlen(ondisk_path), root_len) != 0);
4180 got_fileindex_entry_mark_deleted_from_disk(ie);
4181 done:
4182 free(ondisk_path);
4183 if (err)
4184 return err;
4185 if (status == GOT_STATUS_DELETE)
4186 return NULL;
4187 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 staged_status, relpath);
4191 const struct got_error *
4192 got_worktree_schedule_delete(struct got_worktree *worktree,
4193 struct got_pathlist_head *paths, int delete_local_mods,
4194 const char *status_codes,
4195 got_worktree_delete_cb progress_cb, void *progress_arg,
4196 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4198 struct got_fileindex *fileindex = NULL;
4199 char *fileindex_path = NULL;
4200 const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 struct got_pathlist_entry *pe;
4202 struct schedule_deletion_args sda;
4204 err = lock_worktree(worktree, LOCK_EX);
4205 if (err)
4206 return err;
4208 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4209 if (err)
4210 goto done;
4212 sda.worktree = worktree;
4213 sda.fileindex = fileindex;
4214 sda.progress_cb = progress_cb;
4215 sda.progress_arg = progress_arg;
4216 sda.repo = repo;
4217 sda.delete_local_mods = delete_local_mods;
4218 sda.keep_on_disk = keep_on_disk;
4219 sda.ignore_missing_paths = ignore_missing_paths;
4220 sda.status_codes = status_codes;
4222 TAILQ_FOREACH(pe, paths, entry) {
4223 err = worktree_status(worktree, pe->path, fileindex, repo,
4224 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4225 if (err)
4226 break;
4228 sync_err = sync_fileindex(fileindex, fileindex_path);
4229 if (sync_err && err == NULL)
4230 err = sync_err;
4231 done:
4232 free(fileindex_path);
4233 if (fileindex)
4234 got_fileindex_free(fileindex);
4235 unlockerr = lock_worktree(worktree, LOCK_SH);
4236 if (unlockerr && err == NULL)
4237 err = unlockerr;
4238 return err;
4241 static const struct got_error *
4242 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4244 const struct got_error *err = NULL;
4245 char *line = NULL;
4246 size_t linesize = 0, n;
4247 ssize_t linelen;
4249 linelen = getline(&line, &linesize, infile);
4250 if (linelen == -1) {
4251 if (ferror(infile)) {
4252 err = got_error_from_errno("getline");
4253 goto done;
4255 return NULL;
4257 if (outfile) {
4258 n = fwrite(line, 1, linelen, outfile);
4259 if (n != linelen) {
4260 err = got_ferror(outfile, GOT_ERR_IO);
4261 goto done;
4264 if (rejectfile) {
4265 n = fwrite(line, 1, linelen, rejectfile);
4266 if (n != linelen)
4267 err = got_ferror(outfile, GOT_ERR_IO);
4269 done:
4270 free(line);
4271 return err;
4274 static const struct got_error *
4275 skip_one_line(FILE *f)
4277 char *line = NULL;
4278 size_t linesize = 0;
4279 ssize_t linelen;
4281 linelen = getline(&line, &linesize, f);
4282 if (linelen == -1) {
4283 if (ferror(f))
4284 return got_error_from_errno("getline");
4285 return NULL;
4287 free(line);
4288 return NULL;
4291 static const struct got_error *
4292 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4293 int start_old, int end_old, int start_new, int end_new,
4294 FILE *outfile, FILE *rejectfile)
4296 const struct got_error *err;
4298 /* Copy old file's lines leading up to patch. */
4299 while (!feof(f1) && *line_cur1 < start_old) {
4300 err = copy_one_line(f1, outfile, NULL);
4301 if (err)
4302 return err;
4303 (*line_cur1)++;
4305 /* Skip new file's lines leading up to patch. */
4306 while (!feof(f2) && *line_cur2 < start_new) {
4307 if (rejectfile)
4308 err = copy_one_line(f2, NULL, rejectfile);
4309 else
4310 err = skip_one_line(f2);
4311 if (err)
4312 return err;
4313 (*line_cur2)++;
4315 /* Copy patched lines. */
4316 while (!feof(f2) && *line_cur2 <= end_new) {
4317 err = copy_one_line(f2, outfile, NULL);
4318 if (err)
4319 return err;
4320 (*line_cur2)++;
4322 /* Skip over old file's replaced lines. */
4323 while (!feof(f1) && *line_cur1 <= end_old) {
4324 if (rejectfile)
4325 err = copy_one_line(f1, NULL, rejectfile);
4326 else
4327 err = skip_one_line(f1);
4328 if (err)
4329 return err;
4330 (*line_cur1)++;
4333 return NULL;
4336 static const struct got_error *
4337 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4338 FILE *outfile, FILE *rejectfile)
4340 const struct got_error *err;
4342 if (outfile) {
4343 /* Copy old file's lines until EOF. */
4344 while (!feof(f1)) {
4345 err = copy_one_line(f1, outfile, NULL);
4346 if (err)
4347 return err;
4348 (*line_cur1)++;
4351 if (rejectfile) {
4352 /* Copy new file's lines until EOF. */
4353 while (!feof(f2)) {
4354 err = copy_one_line(f2, NULL, rejectfile);
4355 if (err)
4356 return err;
4357 (*line_cur2)++;
4361 return NULL;
4364 static const struct got_error *
4365 apply_or_reject_change(int *choice, int *nchunks_used,
4366 struct diff_result *diff_result, int n,
4367 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4368 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4369 got_worktree_patch_cb patch_cb, void *patch_arg)
4371 const struct got_error *err = NULL;
4372 struct diff_chunk_context cc = {};
4373 int start_old, end_old, start_new, end_new;
4374 FILE *hunkfile;
4375 struct diff_output_unidiff_state *diff_state;
4376 struct diff_input_info diff_info;
4377 int rc;
4379 *choice = GOT_PATCH_CHOICE_NONE;
4381 /* Get changed line numbers without context lines for copy_change(). */
4382 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4383 start_old = cc.left.start;
4384 end_old = cc.left.end;
4385 start_new = cc.right.start;
4386 end_new = cc.right.end;
4388 /* Get the same change with context lines for display. */
4389 memset(&cc, 0, sizeof(cc));
4390 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4392 memset(&diff_info, 0, sizeof(diff_info));
4393 diff_info.left_path = relpath;
4394 diff_info.right_path = relpath;
4396 diff_state = diff_output_unidiff_state_alloc();
4397 if (diff_state == NULL)
4398 return got_error_set_errno(ENOMEM,
4399 "diff_output_unidiff_state_alloc");
4401 hunkfile = got_opentemp();
4402 if (hunkfile == NULL) {
4403 err = got_error_from_errno("got_opentemp");
4404 goto done;
4407 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4408 diff_result, &cc);
4409 if (rc != DIFF_RC_OK) {
4410 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4411 goto done;
4414 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4415 err = got_ferror(hunkfile, GOT_ERR_IO);
4416 goto done;
4419 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4420 hunkfile, changeno, nchanges);
4421 if (err)
4422 goto done;
4424 switch (*choice) {
4425 case GOT_PATCH_CHOICE_YES:
4426 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4427 end_old, start_new, end_new, outfile, rejectfile);
4428 break;
4429 case GOT_PATCH_CHOICE_NO:
4430 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4431 end_old, start_new, end_new, rejectfile, outfile);
4432 break;
4433 case GOT_PATCH_CHOICE_QUIT:
4434 break;
4435 default:
4436 err = got_error(GOT_ERR_PATCH_CHOICE);
4437 break;
4439 done:
4440 diff_output_unidiff_state_free(diff_state);
4441 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4442 err = got_error_from_errno("fclose");
4443 return err;
4446 struct revert_file_args {
4447 struct got_worktree *worktree;
4448 struct got_fileindex *fileindex;
4449 got_worktree_checkout_cb progress_cb;
4450 void *progress_arg;
4451 got_worktree_patch_cb patch_cb;
4452 void *patch_arg;
4453 struct got_repository *repo;
4454 int unlink_added_files;
4457 static const struct got_error *
4458 create_patched_content(char **path_outfile, int reverse_patch,
4459 struct got_object_id *blob_id, const char *path2,
4460 int dirfd2, const char *de_name2,
4461 const char *relpath, struct got_repository *repo,
4462 got_worktree_patch_cb patch_cb, void *patch_arg)
4464 const struct got_error *err, *free_err;
4465 struct got_blob_object *blob = NULL;
4466 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4467 int fd = -1, fd2 = -1;
4468 char link_target[PATH_MAX];
4469 ssize_t link_len = 0;
4470 char *path1 = NULL, *id_str = NULL;
4471 struct stat sb2;
4472 struct got_diffreg_result *diffreg_result = NULL;
4473 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4474 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4476 *path_outfile = NULL;
4478 err = got_object_id_str(&id_str, blob_id);
4479 if (err)
4480 return err;
4482 if (dirfd2 != -1) {
4483 fd2 = openat(dirfd2, de_name2,
4484 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4485 if (fd2 == -1) {
4486 if (!got_err_open_nofollow_on_symlink()) {
4487 err = got_error_from_errno2("openat", path2);
4488 goto done;
4490 link_len = readlinkat(dirfd2, de_name2,
4491 link_target, sizeof(link_target));
4492 if (link_len == -1) {
4493 return got_error_from_errno2("readlinkat",
4494 path2);
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4499 } else {
4500 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4501 if (fd2 == -1) {
4502 if (!got_err_open_nofollow_on_symlink()) {
4503 err = got_error_from_errno2("open", path2);
4504 goto done;
4506 link_len = readlink(path2, link_target,
4507 sizeof(link_target));
4508 if (link_len == -1)
4509 return got_error_from_errno2("readlink", path2);
4510 sb2.st_mode = S_IFLNK;
4511 sb2.st_size = link_len;
4514 if (fd2 != -1) {
4515 if (fstat(fd2, &sb2) == -1) {
4516 err = got_error_from_errno2("fstat", path2);
4517 goto done;
4520 f2 = fdopen(fd2, "r");
4521 if (f2 == NULL) {
4522 err = got_error_from_errno2("fdopen", path2);
4523 goto done;
4525 fd2 = -1;
4526 } else {
4527 size_t n;
4528 f2 = got_opentemp();
4529 if (f2 == NULL) {
4530 err = got_error_from_errno2("got_opentemp", path2);
4531 goto done;
4533 n = fwrite(link_target, 1, link_len, f2);
4534 if (n != link_len) {
4535 err = got_ferror(f2, GOT_ERR_IO);
4536 goto done;
4538 if (fflush(f2) == EOF) {
4539 err = got_error_from_errno("fflush");
4540 goto done;
4542 rewind(f2);
4545 fd = got_opentempfd();
4546 if (fd == -1) {
4547 err = got_error_from_errno("got_opentempfd");
4548 goto done;
4551 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4552 if (err)
4553 goto done;
4555 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4556 if (err)
4557 goto done;
4559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4560 if (err)
4561 goto done;
4563 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4564 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4565 if (err)
4566 goto done;
4568 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4569 if (err)
4570 goto done;
4572 if (fseek(f1, 0L, SEEK_SET) == -1)
4573 return got_ferror(f1, GOT_ERR_IO);
4574 if (fseek(f2, 0L, SEEK_SET) == -1)
4575 return got_ferror(f2, GOT_ERR_IO);
4577 /* Count the number of actual changes in the diff result. */
4578 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4579 struct diff_chunk_context cc = {};
4580 diff_chunk_context_load_change(&cc, &nchunks_used,
4581 diffreg_result->result, n, 0);
4582 nchanges++;
4584 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4585 int choice;
4586 err = apply_or_reject_change(&choice, &nchunks_used,
4587 diffreg_result->result, n, relpath, f1, f2,
4588 &line_cur1, &line_cur2,
4589 reverse_patch ? NULL : outfile,
4590 reverse_patch ? outfile : NULL,
4591 ++i, nchanges, patch_cb, patch_arg);
4592 if (err)
4593 goto done;
4594 if (choice == GOT_PATCH_CHOICE_YES)
4595 have_content = 1;
4596 else if (choice == GOT_PATCH_CHOICE_QUIT)
4597 break;
4599 if (have_content) {
4600 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4601 reverse_patch ? NULL : outfile,
4602 reverse_patch ? outfile : NULL);
4603 if (err)
4604 goto done;
4606 if (!S_ISLNK(sb2.st_mode)) {
4607 mode_t mode;
4609 mode = apply_umask(sb2.st_mode);
4610 if (fchmod(fileno(outfile), mode) == -1) {
4611 err = got_error_from_errno2("fchmod", path2);
4612 goto done;
4616 done:
4617 free(id_str);
4618 if (fd != -1 && close(fd) == -1 && err == NULL)
4619 err = got_error_from_errno("close");
4620 if (blob)
4621 got_object_blob_close(blob);
4622 free_err = got_diffreg_result_free(diffreg_result);
4623 if (err == NULL)
4624 err = free_err;
4625 if (f1 && fclose(f1) == EOF && err == NULL)
4626 err = got_error_from_errno2("fclose", path1);
4627 if (f2 && fclose(f2) == EOF && err == NULL)
4628 err = got_error_from_errno2("fclose", path2);
4629 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4630 err = got_error_from_errno2("close", path2);
4631 if (outfile && fclose(outfile) == EOF && err == NULL)
4632 err = got_error_from_errno2("fclose", *path_outfile);
4633 if (path1 && unlink(path1) == -1 && err == NULL)
4634 err = got_error_from_errno2("unlink", path1);
4635 if (err || !have_content) {
4636 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4637 err = got_error_from_errno2("unlink", *path_outfile);
4638 free(*path_outfile);
4639 *path_outfile = NULL;
4641 free(path1);
4642 return err;
4645 static const struct got_error *
4646 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4647 const char *relpath, struct got_object_id *blob_id,
4648 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4649 int dirfd, const char *de_name)
4651 struct revert_file_args *a = arg;
4652 const struct got_error *err = NULL;
4653 char *parent_path = NULL;
4654 struct got_fileindex_entry *ie;
4655 struct got_commit_object *base_commit = NULL;
4656 struct got_tree_object *tree = NULL;
4657 struct got_object_id *tree_id = NULL;
4658 const struct got_tree_entry *te = NULL;
4659 char *tree_path = NULL, *te_name;
4660 char *ondisk_path = NULL, *path_content = NULL;
4661 struct got_blob_object *blob = NULL;
4662 int fd = -1;
4664 /* Reverting a staged deletion is a no-op. */
4665 if (status == GOT_STATUS_DELETE &&
4666 staged_status != GOT_STATUS_NO_CHANGE)
4667 return NULL;
4669 if (status == GOT_STATUS_UNVERSIONED)
4670 return (*a->progress_cb)(a->progress_arg,
4671 GOT_STATUS_UNVERSIONED, relpath);
4673 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4674 if (ie == NULL)
4675 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4677 /* Construct in-repository path of tree which contains this blob. */
4678 err = got_path_dirname(&parent_path, ie->path);
4679 if (err) {
4680 if (err->code != GOT_ERR_BAD_PATH)
4681 goto done;
4682 parent_path = strdup("/");
4683 if (parent_path == NULL) {
4684 err = got_error_from_errno("strdup");
4685 goto done;
4688 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4689 tree_path = strdup(parent_path);
4690 if (tree_path == NULL) {
4691 err = got_error_from_errno("strdup");
4692 goto done;
4694 } else {
4695 if (got_path_is_root_dir(parent_path)) {
4696 tree_path = strdup(a->worktree->path_prefix);
4697 if (tree_path == NULL) {
4698 err = got_error_from_errno("strdup");
4699 goto done;
4701 } else {
4702 if (asprintf(&tree_path, "%s/%s",
4703 a->worktree->path_prefix, parent_path) == -1) {
4704 err = got_error_from_errno("asprintf");
4705 goto done;
4710 err = got_object_open_as_commit(&base_commit, a->repo,
4711 a->worktree->base_commit_id);
4712 if (err)
4713 goto done;
4715 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4716 if (err) {
4717 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4718 (status == GOT_STATUS_ADD ||
4719 staged_status == GOT_STATUS_ADD)))
4720 goto done;
4721 } else {
4722 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4723 if (err)
4724 goto done;
4726 err = got_path_basename(&te_name, ie->path);
4727 if (err)
4728 goto done;
4730 te = got_object_tree_find_entry(tree, te_name);
4731 free(te_name);
4732 if (te == NULL && status != GOT_STATUS_ADD &&
4733 staged_status != GOT_STATUS_ADD) {
4734 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4735 goto done;
4739 switch (status) {
4740 case GOT_STATUS_ADD:
4741 if (a->patch_cb) {
4742 int choice = GOT_PATCH_CHOICE_NONE;
4743 err = (*a->patch_cb)(&choice, a->patch_arg,
4744 status, ie->path, NULL, 1, 1);
4745 if (err)
4746 goto done;
4747 if (choice != GOT_PATCH_CHOICE_YES)
4748 break;
4750 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4751 ie->path);
4752 if (err)
4753 goto done;
4754 got_fileindex_entry_remove(a->fileindex, ie);
4755 if (a->unlink_added_files) {
4756 if (asprintf(&ondisk_path, "%s/%s",
4757 got_worktree_get_root_path(a->worktree),
4758 relpath) == -1) {
4759 err = got_error_from_errno("asprintf");
4760 goto done;
4762 if (unlink(ondisk_path) == -1) {
4763 err = got_error_from_errno2("unlink",
4764 ondisk_path);
4765 break;
4768 break;
4769 case GOT_STATUS_DELETE:
4770 if (a->patch_cb) {
4771 int choice = GOT_PATCH_CHOICE_NONE;
4772 err = (*a->patch_cb)(&choice, a->patch_arg,
4773 status, ie->path, NULL, 1, 1);
4774 if (err)
4775 goto done;
4776 if (choice != GOT_PATCH_CHOICE_YES)
4777 break;
4779 /* fall through */
4780 case GOT_STATUS_MODIFY:
4781 case GOT_STATUS_MODE_CHANGE:
4782 case GOT_STATUS_CONFLICT:
4783 case GOT_STATUS_MISSING: {
4784 struct got_object_id id;
4785 if (staged_status == GOT_STATUS_ADD ||
4786 staged_status == GOT_STATUS_MODIFY) {
4787 memcpy(id.sha1, ie->staged_blob_sha1,
4788 SHA1_DIGEST_LENGTH);
4789 } else
4790 memcpy(id.sha1, ie->blob_sha1,
4791 SHA1_DIGEST_LENGTH);
4792 fd = got_opentempfd();
4793 if (fd == -1) {
4794 err = got_error_from_errno("got_opentempfd");
4795 goto done;
4798 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4799 if (err)
4800 goto done;
4802 if (asprintf(&ondisk_path, "%s/%s",
4803 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4804 err = got_error_from_errno("asprintf");
4805 goto done;
4808 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4809 status == GOT_STATUS_CONFLICT)) {
4810 int is_bad_symlink = 0;
4811 err = create_patched_content(&path_content, 1, &id,
4812 ondisk_path, dirfd, de_name, ie->path, a->repo,
4813 a->patch_cb, a->patch_arg);
4814 if (err || path_content == NULL)
4815 break;
4816 if (te && S_ISLNK(te->mode)) {
4817 if (unlink(path_content) == -1) {
4818 err = got_error_from_errno2("unlink",
4819 path_content);
4820 break;
4822 err = install_symlink(&is_bad_symlink,
4823 a->worktree, ondisk_path, ie->path,
4824 blob, 0, 1, 0, 0, a->repo,
4825 a->progress_cb, a->progress_arg);
4826 } else {
4827 if (rename(path_content, ondisk_path) == -1) {
4828 err = got_error_from_errno3("rename",
4829 path_content, ondisk_path);
4830 goto done;
4833 } else {
4834 int is_bad_symlink = 0;
4835 if (te && S_ISLNK(te->mode)) {
4836 err = install_symlink(&is_bad_symlink,
4837 a->worktree, ondisk_path, ie->path,
4838 blob, 0, 1, 0, 0, a->repo,
4839 a->progress_cb, a->progress_arg);
4840 } else {
4841 err = install_blob(a->worktree, ondisk_path,
4842 ie->path,
4843 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4844 got_fileindex_perms_to_st(ie), blob,
4845 0, 1, 0, 0, a->repo,
4846 a->progress_cb, a->progress_arg);
4848 if (err)
4849 goto done;
4850 if (status == GOT_STATUS_DELETE ||
4851 status == GOT_STATUS_MODE_CHANGE) {
4852 err = got_fileindex_entry_update(ie,
4853 a->worktree->root_fd, relpath,
4854 blob->id.sha1,
4855 a->worktree->base_commit_id->sha1, 1);
4856 if (err)
4857 goto done;
4859 if (is_bad_symlink) {
4860 got_fileindex_entry_filetype_set(ie,
4861 GOT_FILEIDX_MODE_BAD_SYMLINK);
4864 break;
4866 default:
4867 break;
4869 done:
4870 free(ondisk_path);
4871 free(path_content);
4872 free(parent_path);
4873 free(tree_path);
4874 if (fd != -1 && close(fd) == -1 && err == NULL)
4875 err = got_error_from_errno("close");
4876 if (blob)
4877 got_object_blob_close(blob);
4878 if (tree)
4879 got_object_tree_close(tree);
4880 free(tree_id);
4881 if (base_commit)
4882 got_object_commit_close(base_commit);
4883 return err;
4886 const struct got_error *
4887 got_worktree_revert(struct got_worktree *worktree,
4888 struct got_pathlist_head *paths,
4889 got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 got_worktree_patch_cb patch_cb, void *patch_arg,
4891 struct got_repository *repo)
4893 struct got_fileindex *fileindex = NULL;
4894 char *fileindex_path = NULL;
4895 const struct got_error *err = NULL, *unlockerr = NULL;
4896 const struct got_error *sync_err = NULL;
4897 struct got_pathlist_entry *pe;
4898 struct revert_file_args rfa;
4900 err = lock_worktree(worktree, LOCK_EX);
4901 if (err)
4902 return err;
4904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4905 if (err)
4906 goto done;
4908 rfa.worktree = worktree;
4909 rfa.fileindex = fileindex;
4910 rfa.progress_cb = progress_cb;
4911 rfa.progress_arg = progress_arg;
4912 rfa.patch_cb = patch_cb;
4913 rfa.patch_arg = patch_arg;
4914 rfa.repo = repo;
4915 rfa.unlink_added_files = 0;
4916 TAILQ_FOREACH(pe, paths, entry) {
4917 err = worktree_status(worktree, pe->path, fileindex, repo,
4918 revert_file, &rfa, NULL, NULL, 1, 0);
4919 if (err)
4920 break;
4922 sync_err = sync_fileindex(fileindex, fileindex_path);
4923 if (sync_err && err == NULL)
4924 err = sync_err;
4925 done:
4926 free(fileindex_path);
4927 if (fileindex)
4928 got_fileindex_free(fileindex);
4929 unlockerr = lock_worktree(worktree, LOCK_SH);
4930 if (unlockerr && err == NULL)
4931 err = unlockerr;
4932 return err;
4935 static void
4936 free_commitable(struct got_commitable *ct)
4938 free(ct->path);
4939 free(ct->in_repo_path);
4940 free(ct->ondisk_path);
4941 free(ct->blob_id);
4942 free(ct->base_blob_id);
4943 free(ct->staged_blob_id);
4944 free(ct->base_commit_id);
4945 free(ct);
4948 struct collect_commitables_arg {
4949 struct got_pathlist_head *commitable_paths;
4950 struct got_repository *repo;
4951 struct got_worktree *worktree;
4952 struct got_fileindex *fileindex;
4953 int have_staged_files;
4954 int allow_bad_symlinks;
4955 int diff_header_shown;
4956 FILE *diff_outfile;
4957 FILE *f1;
4958 FILE *f2;
4962 * Create a file which contains the target path of a symlink so we can feed
4963 * it as content to the diff engine.
4965 static const struct got_error *
4966 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4967 const char *abspath)
4969 const struct got_error *err = NULL;
4970 char target_path[PATH_MAX];
4971 ssize_t target_len, outlen;
4973 *fd = -1;
4975 if (dirfd != -1) {
4976 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4977 if (target_len == -1)
4978 return got_error_from_errno2("readlinkat", abspath);
4979 } else {
4980 target_len = readlink(abspath, target_path, PATH_MAX);
4981 if (target_len == -1)
4982 return got_error_from_errno2("readlink", abspath);
4985 *fd = got_opentempfd();
4986 if (*fd == -1)
4987 return got_error_from_errno("got_opentempfd");
4989 outlen = write(*fd, target_path, target_len);
4990 if (outlen == -1) {
4991 err = got_error_from_errno("got_opentempfd");
4992 goto done;
4995 if (lseek(*fd, 0, SEEK_SET) == -1) {
4996 err = got_error_from_errno2("lseek", abspath);
4997 goto done;
4999 done:
5000 if (err) {
5001 close(*fd);
5002 *fd = -1;
5004 return err;
5007 static const struct got_error *
5008 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5009 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5010 struct got_repository *repo, struct got_worktree *worktree)
5012 const struct got_error *err = NULL;
5013 struct got_blob_object *blob1 = NULL;
5014 int fd = -1, fd1 = -1, fd2 = -1;
5015 FILE *ondisk_file = NULL;
5016 char *label1 = NULL;
5017 struct stat sb;
5018 off_t size1 = 0;
5019 int f2_exists = 0;
5020 int diff_staged = (ct->staged_status != GOT_STATUS_NO_CHANGE);
5021 char *id_str = NULL;
5023 memset(&sb, 0, sizeof(sb));
5025 err = got_opentemp_truncate(f1);
5026 if (err)
5027 return got_error_from_errno("got_opentemp_truncate");
5028 err = got_opentemp_truncate(f2);
5029 if (err)
5030 return got_error_from_errno("got_opentemp_truncate");
5032 if (!*diff_header_shown) {
5033 err = got_object_id_str(&id_str, worktree->base_commit_id);
5034 if (err)
5035 return err;
5036 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5037 got_worktree_get_root_path(worktree));
5038 fprintf(diff_outfile, "commit - %s\n", id_str);
5039 fprintf(diff_outfile, "path + %s%s\n",
5040 got_worktree_get_root_path(worktree),
5041 diff_staged ? " (staged changes)" : "");
5042 *diff_header_shown = 1;
5045 if (diff_staged) {
5046 const char *label1 = NULL, *label2 = NULL;
5047 switch (ct->staged_status) {
5048 case GOT_STATUS_MODIFY:
5049 label1 = ct->path;
5050 label2 = ct->path;
5051 break;
5052 case GOT_STATUS_ADD:
5053 label2 = ct->path;
5054 break;
5055 case GOT_STATUS_DELETE:
5056 label1 = ct->path;
5057 break;
5058 default:
5059 return got_error(GOT_ERR_FILE_STATUS);
5061 fd1 = got_opentempfd();
5062 if (fd1 == -1) {
5063 err = got_error_from_errno("got_opentempfd");
5064 goto done;
5066 fd2 = got_opentempfd();
5067 if (fd2 == -1) {
5068 err = got_error_from_errno("got_opentempfd");
5069 goto done;
5071 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5072 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5073 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5074 repo, diff_outfile);
5075 goto done;
5078 fd1 = got_opentempfd();
5079 if (fd1 == -1) {
5080 err = got_error_from_errno("got_opentempfd");
5081 goto done;
5084 if (ct->status != GOT_STATUS_ADD) {
5085 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5086 8192, fd1);
5087 if (err)
5088 goto done;
5091 if (ct->status != GOT_STATUS_DELETE) {
5092 if (dirfd != -1) {
5093 fd = openat(dirfd, de_name,
5094 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5095 if (fd == -1) {
5096 if (!got_err_open_nofollow_on_symlink()) {
5097 err = got_error_from_errno2("openat",
5098 ct->ondisk_path);
5099 goto done;
5101 err = get_symlink_target_file(&fd, dirfd,
5102 de_name, ct->ondisk_path);
5103 if (err)
5104 goto done;
5106 } else {
5107 fd = open(ct->ondisk_path,
5108 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5109 if (fd == -1) {
5110 if (!got_err_open_nofollow_on_symlink()) {
5111 err = got_error_from_errno2("open",
5112 ct->ondisk_path);
5113 goto done;
5115 err = get_symlink_target_file(&fd, dirfd,
5116 de_name, ct->ondisk_path);
5117 if (err)
5118 goto done;
5121 if (fstatat(fd, ct->ondisk_path, &sb,
5122 AT_SYMLINK_NOFOLLOW) == -1) {
5123 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5124 goto done;
5126 ondisk_file = fdopen(fd, "r");
5127 if (ondisk_file == NULL) {
5128 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5129 goto done;
5131 fd = -1;
5132 f2_exists = 1;
5135 if (blob1) {
5136 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5137 f1, blob1);
5138 if (err)
5139 goto done;
5142 err = got_diff_blob_file(blob1, f1, size1, label1,
5143 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5144 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, diff_outfile);
5145 done:
5146 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5147 err = got_error_from_errno("close");
5148 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5149 err = got_error_from_errno("close");
5150 if (blob1)
5151 got_object_blob_close(blob1);
5152 if (fd != -1 && close(fd) == -1 && err == NULL)
5153 err = got_error_from_errno("close");
5154 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5155 err = got_error_from_errno("fclose");
5156 return err;
5159 static const struct got_error *
5160 collect_commitables(void *arg, unsigned char status,
5161 unsigned char staged_status, const char *relpath,
5162 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5163 struct got_object_id *commit_id, int dirfd, const char *de_name)
5165 struct collect_commitables_arg *a = arg;
5166 const struct got_error *err = NULL;
5167 struct got_commitable *ct = NULL;
5168 struct got_pathlist_entry *new = NULL;
5169 char *parent_path = NULL, *path = NULL;
5170 struct stat sb;
5172 if (a->have_staged_files) {
5173 if (staged_status != GOT_STATUS_MODIFY &&
5174 staged_status != GOT_STATUS_ADD &&
5175 staged_status != GOT_STATUS_DELETE)
5176 return NULL;
5177 } else {
5178 if (status == GOT_STATUS_CONFLICT)
5179 return got_error(GOT_ERR_COMMIT_CONFLICT);
5181 if (status != GOT_STATUS_MODIFY &&
5182 status != GOT_STATUS_MODE_CHANGE &&
5183 status != GOT_STATUS_ADD &&
5184 status != GOT_STATUS_DELETE)
5185 return NULL;
5188 if (asprintf(&path, "/%s", relpath) == -1) {
5189 err = got_error_from_errno("asprintf");
5190 goto done;
5192 if (strcmp(path, "/") == 0) {
5193 parent_path = strdup("");
5194 if (parent_path == NULL)
5195 return got_error_from_errno("strdup");
5196 } else {
5197 err = got_path_dirname(&parent_path, path);
5198 if (err)
5199 return err;
5202 ct = calloc(1, sizeof(*ct));
5203 if (ct == NULL) {
5204 err = got_error_from_errno("calloc");
5205 goto done;
5208 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5209 relpath) == -1) {
5210 err = got_error_from_errno("asprintf");
5211 goto done;
5214 if (staged_status == GOT_STATUS_ADD ||
5215 staged_status == GOT_STATUS_MODIFY) {
5216 struct got_fileindex_entry *ie;
5217 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5218 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5219 case GOT_FILEIDX_MODE_REGULAR_FILE:
5220 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5221 ct->mode = S_IFREG;
5222 break;
5223 case GOT_FILEIDX_MODE_SYMLINK:
5224 ct->mode = S_IFLNK;
5225 break;
5226 default:
5227 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5228 goto done;
5230 ct->mode |= got_fileindex_entry_perms_get(ie);
5231 } else if (status != GOT_STATUS_DELETE &&
5232 staged_status != GOT_STATUS_DELETE) {
5233 if (dirfd != -1) {
5234 if (fstatat(dirfd, de_name, &sb,
5235 AT_SYMLINK_NOFOLLOW) == -1) {
5236 err = got_error_from_errno2("fstatat",
5237 ct->ondisk_path);
5238 goto done;
5240 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5241 err = got_error_from_errno2("lstat", ct->ondisk_path);
5242 goto done;
5244 ct->mode = sb.st_mode;
5247 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5248 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5249 relpath) == -1) {
5250 err = got_error_from_errno("asprintf");
5251 goto done;
5254 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5255 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5256 int is_bad_symlink;
5257 char target_path[PATH_MAX];
5258 ssize_t target_len;
5259 target_len = readlink(ct->ondisk_path, target_path,
5260 sizeof(target_path));
5261 if (target_len == -1) {
5262 err = got_error_from_errno2("readlink",
5263 ct->ondisk_path);
5264 goto done;
5266 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5267 target_len, ct->ondisk_path, a->worktree->root_path);
5268 if (err)
5269 goto done;
5270 if (is_bad_symlink) {
5271 err = got_error_path(ct->ondisk_path,
5272 GOT_ERR_BAD_SYMLINK);
5273 goto done;
5278 ct->status = status;
5279 ct->staged_status = staged_status;
5280 ct->blob_id = NULL; /* will be filled in when blob gets created */
5281 if (ct->status != GOT_STATUS_ADD &&
5282 ct->staged_status != GOT_STATUS_ADD) {
5283 ct->base_blob_id = got_object_id_dup(blob_id);
5284 if (ct->base_blob_id == NULL) {
5285 err = got_error_from_errno("got_object_id_dup");
5286 goto done;
5288 ct->base_commit_id = got_object_id_dup(commit_id);
5289 if (ct->base_commit_id == NULL) {
5290 err = got_error_from_errno("got_object_id_dup");
5291 goto done;
5294 if (ct->staged_status == GOT_STATUS_ADD ||
5295 ct->staged_status == GOT_STATUS_MODIFY) {
5296 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5297 if (ct->staged_blob_id == NULL) {
5298 err = got_error_from_errno("got_object_id_dup");
5299 goto done;
5302 ct->path = strdup(path);
5303 if (ct->path == NULL) {
5304 err = got_error_from_errno("strdup");
5305 goto done;
5307 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5308 if (err)
5309 goto done;
5311 if (a->diff_outfile && ct && new != NULL) {
5312 err = append_ct_diff(ct, &a->diff_header_shown,
5313 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5314 a->repo, a->worktree);
5315 if (err)
5316 goto done;
5318 done:
5319 if (ct && (err || new == NULL))
5320 free_commitable(ct);
5321 free(parent_path);
5322 free(path);
5323 return err;
5326 static const struct got_error *write_tree(struct got_object_id **, int *,
5327 struct got_tree_object *, const char *, struct got_pathlist_head *,
5328 got_worktree_status_cb status_cb, void *status_arg,
5329 struct got_repository *);
5331 static const struct got_error *
5332 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5333 struct got_tree_entry *te, const char *parent_path,
5334 struct got_pathlist_head *commitable_paths,
5335 got_worktree_status_cb status_cb, void *status_arg,
5336 struct got_repository *repo)
5338 const struct got_error *err = NULL;
5339 struct got_tree_object *subtree;
5340 char *subpath;
5342 if (asprintf(&subpath, "%s%s%s", parent_path,
5343 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5344 return got_error_from_errno("asprintf");
5346 err = got_object_open_as_tree(&subtree, repo, &te->id);
5347 if (err)
5348 return err;
5350 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5351 commitable_paths, status_cb, status_arg, repo);
5352 got_object_tree_close(subtree);
5353 free(subpath);
5354 return err;
5357 static const struct got_error *
5358 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5360 const struct got_error *err = NULL;
5361 char *ct_parent_path = NULL;
5363 *match = 0;
5365 if (strchr(ct->in_repo_path, '/') == NULL) {
5366 *match = got_path_is_root_dir(path);
5367 return NULL;
5370 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5371 if (err)
5372 return err;
5373 *match = (strcmp(path, ct_parent_path) == 0);
5374 free(ct_parent_path);
5375 return err;
5378 static mode_t
5379 get_ct_file_mode(struct got_commitable *ct)
5381 if (S_ISLNK(ct->mode))
5382 return S_IFLNK;
5384 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5387 static const struct got_error *
5388 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5389 struct got_tree_entry *te, struct got_commitable *ct)
5391 const struct got_error *err = NULL;
5393 *new_te = NULL;
5395 err = got_object_tree_entry_dup(new_te, te);
5396 if (err)
5397 goto done;
5399 (*new_te)->mode = get_ct_file_mode(ct);
5401 if (ct->staged_status == GOT_STATUS_MODIFY)
5402 memcpy(&(*new_te)->id, ct->staged_blob_id,
5403 sizeof((*new_te)->id));
5404 else
5405 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5406 done:
5407 if (err && *new_te) {
5408 free(*new_te);
5409 *new_te = NULL;
5411 return err;
5414 static const struct got_error *
5415 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5416 struct got_commitable *ct)
5418 const struct got_error *err = NULL;
5419 char *ct_name = NULL;
5421 *new_te = NULL;
5423 *new_te = calloc(1, sizeof(**new_te));
5424 if (*new_te == NULL)
5425 return got_error_from_errno("calloc");
5427 err = got_path_basename(&ct_name, ct->path);
5428 if (err)
5429 goto done;
5430 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5431 sizeof((*new_te)->name)) {
5432 err = got_error(GOT_ERR_NO_SPACE);
5433 goto done;
5436 (*new_te)->mode = get_ct_file_mode(ct);
5438 if (ct->staged_status == GOT_STATUS_ADD)
5439 memcpy(&(*new_te)->id, ct->staged_blob_id,
5440 sizeof((*new_te)->id));
5441 else
5442 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5443 done:
5444 free(ct_name);
5445 if (err && *new_te) {
5446 free(*new_te);
5447 *new_te = NULL;
5449 return err;
5452 static const struct got_error *
5453 insert_tree_entry(struct got_tree_entry *new_te,
5454 struct got_pathlist_head *paths)
5456 const struct got_error *err = NULL;
5457 struct got_pathlist_entry *new_pe;
5459 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5460 if (err)
5461 return err;
5462 if (new_pe == NULL)
5463 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5464 return NULL;
5467 static const struct got_error *
5468 report_ct_status(struct got_commitable *ct,
5469 got_worktree_status_cb status_cb, void *status_arg)
5471 const char *ct_path = ct->path;
5472 unsigned char status;
5474 if (status_cb == NULL) /* no commit progress output desired */
5475 return NULL;
5477 while (ct_path[0] == '/')
5478 ct_path++;
5480 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5481 status = ct->staged_status;
5482 else
5483 status = ct->status;
5485 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5486 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5489 static const struct got_error *
5490 match_modified_subtree(int *modified, struct got_tree_entry *te,
5491 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5493 const struct got_error *err = NULL;
5494 struct got_pathlist_entry *pe;
5495 char *te_path;
5497 *modified = 0;
5499 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5500 got_path_is_root_dir(base_tree_path) ? "" : "/",
5501 te->name) == -1)
5502 return got_error_from_errno("asprintf");
5504 TAILQ_FOREACH(pe, commitable_paths, entry) {
5505 struct got_commitable *ct = pe->data;
5506 *modified = got_path_is_child(ct->in_repo_path, te_path,
5507 strlen(te_path));
5508 if (*modified)
5509 break;
5512 free(te_path);
5513 return err;
5516 static const struct got_error *
5517 match_deleted_or_modified_ct(struct got_commitable **ctp,
5518 struct got_tree_entry *te, const char *base_tree_path,
5519 struct got_pathlist_head *commitable_paths)
5521 const struct got_error *err = NULL;
5522 struct got_pathlist_entry *pe;
5524 *ctp = NULL;
5526 TAILQ_FOREACH(pe, commitable_paths, entry) {
5527 struct got_commitable *ct = pe->data;
5528 char *ct_name = NULL;
5529 int path_matches;
5531 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5532 if (ct->status != GOT_STATUS_MODIFY &&
5533 ct->status != GOT_STATUS_MODE_CHANGE &&
5534 ct->status != GOT_STATUS_DELETE)
5535 continue;
5536 } else {
5537 if (ct->staged_status != GOT_STATUS_MODIFY &&
5538 ct->staged_status != GOT_STATUS_DELETE)
5539 continue;
5542 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5543 continue;
5545 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5546 if (err)
5547 return err;
5548 if (!path_matches)
5549 continue;
5551 err = got_path_basename(&ct_name, pe->path);
5552 if (err)
5553 return err;
5555 if (strcmp(te->name, ct_name) != 0) {
5556 free(ct_name);
5557 continue;
5559 free(ct_name);
5561 *ctp = ct;
5562 break;
5565 return err;
5568 static const struct got_error *
5569 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5570 const char *child_path, const char *path_base_tree,
5571 struct got_pathlist_head *commitable_paths,
5572 got_worktree_status_cb status_cb, void *status_arg,
5573 struct got_repository *repo)
5575 const struct got_error *err = NULL;
5576 struct got_tree_entry *new_te;
5577 char *subtree_path;
5578 struct got_object_id *id = NULL;
5579 int nentries;
5581 *new_tep = NULL;
5583 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5584 got_path_is_root_dir(path_base_tree) ? "" : "/",
5585 child_path) == -1)
5586 return got_error_from_errno("asprintf");
5588 new_te = calloc(1, sizeof(*new_te));
5589 if (new_te == NULL)
5590 return got_error_from_errno("calloc");
5591 new_te->mode = S_IFDIR;
5593 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5594 sizeof(new_te->name)) {
5595 err = got_error(GOT_ERR_NO_SPACE);
5596 goto done;
5598 err = write_tree(&id, &nentries, NULL, subtree_path,
5599 commitable_paths, status_cb, status_arg, repo);
5600 if (err) {
5601 free(new_te);
5602 goto done;
5604 memcpy(&new_te->id, id, sizeof(new_te->id));
5605 done:
5606 free(id);
5607 free(subtree_path);
5608 if (err == NULL)
5609 *new_tep = new_te;
5610 return err;
5613 static const struct got_error *
5614 write_tree(struct got_object_id **new_tree_id, int *nentries,
5615 struct got_tree_object *base_tree, const char *path_base_tree,
5616 struct got_pathlist_head *commitable_paths,
5617 got_worktree_status_cb status_cb, void *status_arg,
5618 struct got_repository *repo)
5620 const struct got_error *err = NULL;
5621 struct got_pathlist_head paths;
5622 struct got_tree_entry *te, *new_te = NULL;
5623 struct got_pathlist_entry *pe;
5625 TAILQ_INIT(&paths);
5626 *nentries = 0;
5628 /* Insert, and recurse into, newly added entries first. */
5629 TAILQ_FOREACH(pe, commitable_paths, entry) {
5630 struct got_commitable *ct = pe->data;
5631 char *child_path = NULL, *slash;
5633 if ((ct->status != GOT_STATUS_ADD &&
5634 ct->staged_status != GOT_STATUS_ADD) ||
5635 (ct->flags & GOT_COMMITABLE_ADDED))
5636 continue;
5638 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5639 strlen(path_base_tree)))
5640 continue;
5642 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5643 ct->in_repo_path);
5644 if (err)
5645 goto done;
5647 slash = strchr(child_path, '/');
5648 if (slash == NULL) {
5649 err = alloc_added_blob_tree_entry(&new_te, ct);
5650 if (err)
5651 goto done;
5652 err = report_ct_status(ct, status_cb, status_arg);
5653 if (err)
5654 goto done;
5655 ct->flags |= GOT_COMMITABLE_ADDED;
5656 err = insert_tree_entry(new_te, &paths);
5657 if (err)
5658 goto done;
5659 (*nentries)++;
5660 } else {
5661 *slash = '\0'; /* trim trailing path components */
5662 if (base_tree == NULL ||
5663 got_object_tree_find_entry(base_tree, child_path)
5664 == NULL) {
5665 err = make_subtree_for_added_blob(&new_te,
5666 child_path, path_base_tree,
5667 commitable_paths, status_cb, status_arg,
5668 repo);
5669 if (err)
5670 goto done;
5671 err = insert_tree_entry(new_te, &paths);
5672 if (err)
5673 goto done;
5674 (*nentries)++;
5679 if (base_tree) {
5680 int i, nbase_entries;
5681 /* Handle modified and deleted entries. */
5682 nbase_entries = got_object_tree_get_nentries(base_tree);
5683 for (i = 0; i < nbase_entries; i++) {
5684 struct got_commitable *ct = NULL;
5686 te = got_object_tree_get_entry(base_tree, i);
5687 if (got_object_tree_entry_is_submodule(te)) {
5688 /* Entry is a submodule; just copy it. */
5689 err = got_object_tree_entry_dup(&new_te, te);
5690 if (err)
5691 goto done;
5692 err = insert_tree_entry(new_te, &paths);
5693 if (err)
5694 goto done;
5695 (*nentries)++;
5696 continue;
5699 if (S_ISDIR(te->mode)) {
5700 int modified;
5701 err = got_object_tree_entry_dup(&new_te, te);
5702 if (err)
5703 goto done;
5704 err = match_modified_subtree(&modified, te,
5705 path_base_tree, commitable_paths);
5706 if (err)
5707 goto done;
5708 /* Avoid recursion into unmodified subtrees. */
5709 if (modified) {
5710 struct got_object_id *new_id;
5711 int nsubentries;
5712 err = write_subtree(&new_id,
5713 &nsubentries, te,
5714 path_base_tree, commitable_paths,
5715 status_cb, status_arg, repo);
5716 if (err)
5717 goto done;
5718 if (nsubentries == 0) {
5719 /* All entries were deleted. */
5720 free(new_id);
5721 continue;
5723 memcpy(&new_te->id, new_id,
5724 sizeof(new_te->id));
5725 free(new_id);
5727 err = insert_tree_entry(new_te, &paths);
5728 if (err)
5729 goto done;
5730 (*nentries)++;
5731 continue;
5734 err = match_deleted_or_modified_ct(&ct, te,
5735 path_base_tree, commitable_paths);
5736 if (err)
5737 goto done;
5738 if (ct) {
5739 /* NB: Deleted entries get dropped here. */
5740 if (ct->status == GOT_STATUS_MODIFY ||
5741 ct->status == GOT_STATUS_MODE_CHANGE ||
5742 ct->staged_status == GOT_STATUS_MODIFY) {
5743 err = alloc_modified_blob_tree_entry(
5744 &new_te, te, ct);
5745 if (err)
5746 goto done;
5747 err = insert_tree_entry(new_te, &paths);
5748 if (err)
5749 goto done;
5750 (*nentries)++;
5752 err = report_ct_status(ct, status_cb,
5753 status_arg);
5754 if (err)
5755 goto done;
5756 } else {
5757 /* Entry is unchanged; just copy it. */
5758 err = got_object_tree_entry_dup(&new_te, te);
5759 if (err)
5760 goto done;
5761 err = insert_tree_entry(new_te, &paths);
5762 if (err)
5763 goto done;
5764 (*nentries)++;
5769 /* Write new list of entries; deleted entries have been dropped. */
5770 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5771 done:
5772 got_pathlist_free(&paths);
5773 return err;
5776 static const struct got_error *
5777 update_fileindex_after_commit(struct got_worktree *worktree,
5778 struct got_pathlist_head *commitable_paths,
5779 struct got_object_id *new_base_commit_id,
5780 struct got_fileindex *fileindex, int have_staged_files)
5782 const struct got_error *err = NULL;
5783 struct got_pathlist_entry *pe;
5784 char *relpath = NULL;
5786 TAILQ_FOREACH(pe, commitable_paths, entry) {
5787 struct got_fileindex_entry *ie;
5788 struct got_commitable *ct = pe->data;
5790 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5792 err = got_path_skip_common_ancestor(&relpath,
5793 worktree->root_path, ct->ondisk_path);
5794 if (err)
5795 goto done;
5797 if (ie) {
5798 if (ct->status == GOT_STATUS_DELETE ||
5799 ct->staged_status == GOT_STATUS_DELETE) {
5800 got_fileindex_entry_remove(fileindex, ie);
5801 } else if (ct->staged_status == GOT_STATUS_ADD ||
5802 ct->staged_status == GOT_STATUS_MODIFY) {
5803 got_fileindex_entry_stage_set(ie,
5804 GOT_FILEIDX_STAGE_NONE);
5805 got_fileindex_entry_staged_filetype_set(ie, 0);
5807 err = got_fileindex_entry_update(ie,
5808 worktree->root_fd, relpath,
5809 ct->staged_blob_id->sha1,
5810 new_base_commit_id->sha1,
5811 !have_staged_files);
5812 } else
5813 err = got_fileindex_entry_update(ie,
5814 worktree->root_fd, relpath,
5815 ct->blob_id->sha1,
5816 new_base_commit_id->sha1,
5817 !have_staged_files);
5818 } else {
5819 err = got_fileindex_entry_alloc(&ie, pe->path);
5820 if (err)
5821 goto done;
5822 err = got_fileindex_entry_update(ie,
5823 worktree->root_fd, relpath, ct->blob_id->sha1,
5824 new_base_commit_id->sha1, 1);
5825 if (err) {
5826 got_fileindex_entry_free(ie);
5827 goto done;
5829 err = got_fileindex_entry_add(fileindex, ie);
5830 if (err) {
5831 got_fileindex_entry_free(ie);
5832 goto done;
5835 free(relpath);
5836 relpath = NULL;
5838 done:
5839 free(relpath);
5840 return err;
5844 static const struct got_error *
5845 check_out_of_date(const char *in_repo_path, unsigned char status,
5846 unsigned char staged_status, struct got_object_id *base_blob_id,
5847 struct got_object_id *base_commit_id,
5848 struct got_object_id *head_commit_id, struct got_repository *repo,
5849 int ood_errcode)
5851 const struct got_error *err = NULL;
5852 struct got_commit_object *commit = NULL;
5853 struct got_object_id *id = NULL;
5855 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5856 /* Trivial case: base commit == head commit */
5857 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5858 return NULL;
5860 * Ensure file content which local changes were based
5861 * on matches file content in the branch head.
5863 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5864 if (err)
5865 goto done;
5866 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5867 if (err) {
5868 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5869 err = got_error(ood_errcode);
5870 goto done;
5871 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5872 err = got_error(ood_errcode);
5873 } else {
5874 /* Require that added files don't exist in the branch head. */
5875 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5876 if (err)
5877 goto done;
5878 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5879 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5880 goto done;
5881 err = id ? got_error(ood_errcode) : NULL;
5883 done:
5884 free(id);
5885 if (commit)
5886 got_object_commit_close(commit);
5887 return err;
5890 static const struct got_error *
5891 commit_worktree(struct got_object_id **new_commit_id,
5892 struct got_pathlist_head *commitable_paths,
5893 struct got_object_id *head_commit_id,
5894 struct got_object_id *parent_id2,
5895 struct got_worktree *worktree,
5896 const char *author, const char *committer, char *diff_path,
5897 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5898 got_worktree_status_cb status_cb, void *status_arg,
5899 struct got_repository *repo)
5901 const struct got_error *err = NULL, *unlockerr = NULL;
5902 struct got_pathlist_entry *pe;
5903 const char *head_ref_name = NULL;
5904 struct got_commit_object *head_commit = NULL;
5905 struct got_reference *head_ref2 = NULL;
5906 struct got_object_id *head_commit_id2 = NULL;
5907 struct got_tree_object *head_tree = NULL;
5908 struct got_object_id *new_tree_id = NULL;
5909 int nentries, nparents = 0;
5910 struct got_object_id_queue parent_ids;
5911 struct got_object_qid *pid = NULL;
5912 char *logmsg = NULL;
5913 time_t timestamp;
5915 *new_commit_id = NULL;
5917 STAILQ_INIT(&parent_ids);
5919 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5920 if (err)
5921 goto done;
5923 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5924 if (err)
5925 goto done;
5927 if (commit_msg_cb != NULL) {
5928 err = commit_msg_cb(commitable_paths, diff_path,
5929 &logmsg, commit_arg);
5930 if (err)
5931 goto done;
5934 if (logmsg == NULL || strlen(logmsg) == 0) {
5935 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5936 goto done;
5939 /* Create blobs from added and modified files and record their IDs. */
5940 TAILQ_FOREACH(pe, commitable_paths, entry) {
5941 struct got_commitable *ct = pe->data;
5942 char *ondisk_path;
5944 /* Blobs for staged files already exist. */
5945 if (ct->staged_status == GOT_STATUS_ADD ||
5946 ct->staged_status == GOT_STATUS_MODIFY)
5947 continue;
5949 if (ct->status != GOT_STATUS_ADD &&
5950 ct->status != GOT_STATUS_MODIFY &&
5951 ct->status != GOT_STATUS_MODE_CHANGE)
5952 continue;
5954 if (asprintf(&ondisk_path, "%s/%s",
5955 worktree->root_path, pe->path) == -1) {
5956 err = got_error_from_errno("asprintf");
5957 goto done;
5959 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5960 free(ondisk_path);
5961 if (err)
5962 goto done;
5965 /* Recursively write new tree objects. */
5966 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5967 commitable_paths, status_cb, status_arg, repo);
5968 if (err)
5969 goto done;
5971 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5972 if (err)
5973 goto done;
5974 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5975 nparents++;
5976 if (parent_id2) {
5977 err = got_object_qid_alloc(&pid, parent_id2);
5978 if (err)
5979 goto done;
5980 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5981 nparents++;
5983 timestamp = time(NULL);
5984 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5985 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5986 if (logmsg != NULL)
5987 free(logmsg);
5988 if (err)
5989 goto done;
5991 /* Check if a concurrent commit to our branch has occurred. */
5992 head_ref_name = got_worktree_get_head_ref_name(worktree);
5993 if (head_ref_name == NULL) {
5994 err = got_error_from_errno("got_worktree_get_head_ref_name");
5995 goto done;
5997 /* Lock the reference here to prevent concurrent modification. */
5998 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5999 if (err)
6000 goto done;
6001 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6002 if (err)
6003 goto done;
6004 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6005 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6006 goto done;
6008 /* Update branch head in repository. */
6009 err = got_ref_change_ref(head_ref2, *new_commit_id);
6010 if (err)
6011 goto done;
6012 err = got_ref_write(head_ref2, repo);
6013 if (err)
6014 goto done;
6016 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6017 if (err)
6018 goto done;
6020 err = ref_base_commit(worktree, repo);
6021 if (err)
6022 goto done;
6023 done:
6024 got_object_id_queue_free(&parent_ids);
6025 if (head_tree)
6026 got_object_tree_close(head_tree);
6027 if (head_commit)
6028 got_object_commit_close(head_commit);
6029 free(head_commit_id2);
6030 if (head_ref2) {
6031 unlockerr = got_ref_unlock(head_ref2);
6032 if (unlockerr && err == NULL)
6033 err = unlockerr;
6034 got_ref_close(head_ref2);
6036 return err;
6039 static const struct got_error *
6040 check_path_is_commitable(const char *path,
6041 struct got_pathlist_head *commitable_paths)
6043 struct got_pathlist_entry *cpe = NULL;
6044 size_t path_len = strlen(path);
6046 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6047 struct got_commitable *ct = cpe->data;
6048 const char *ct_path = ct->path;
6050 while (ct_path[0] == '/')
6051 ct_path++;
6053 if (strcmp(path, ct_path) == 0 ||
6054 got_path_is_child(ct_path, path, path_len))
6055 break;
6058 if (cpe == NULL)
6059 return got_error_path(path, GOT_ERR_BAD_PATH);
6061 return NULL;
6064 static const struct got_error *
6065 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6067 int *have_staged_files = arg;
6069 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6070 *have_staged_files = 1;
6071 return got_error(GOT_ERR_CANCELLED);
6074 return NULL;
6077 static const struct got_error *
6078 check_non_staged_files(struct got_fileindex *fileindex,
6079 struct got_pathlist_head *paths)
6081 struct got_pathlist_entry *pe;
6082 struct got_fileindex_entry *ie;
6084 TAILQ_FOREACH(pe, paths, entry) {
6085 if (pe->path[0] == '\0')
6086 continue;
6087 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6088 if (ie == NULL)
6089 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6090 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6091 return got_error_path(pe->path,
6092 GOT_ERR_FILE_NOT_STAGED);
6095 return NULL;
6098 const struct got_error *
6099 got_worktree_commit(struct got_object_id **new_commit_id,
6100 struct got_worktree *worktree, struct got_pathlist_head *paths,
6101 const char *author, const char *committer, int allow_bad_symlinks,
6102 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6103 got_worktree_status_cb status_cb, void *status_arg,
6104 struct got_repository *repo)
6106 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6107 struct got_fileindex *fileindex = NULL;
6108 char *fileindex_path = NULL;
6109 struct got_pathlist_head commitable_paths;
6110 struct collect_commitables_arg cc_arg;
6111 struct got_pathlist_entry *pe;
6112 struct got_reference *head_ref = NULL;
6113 struct got_object_id *head_commit_id = NULL;
6114 char *diff_path = NULL;
6115 int have_staged_files = 0;
6117 *new_commit_id = NULL;
6119 memset(&cc_arg, 0, sizeof(cc_arg));
6120 TAILQ_INIT(&commitable_paths);
6122 err = lock_worktree(worktree, LOCK_EX);
6123 if (err)
6124 goto done;
6126 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6127 if (err)
6128 goto done;
6130 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6131 if (err)
6132 goto done;
6134 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6135 if (err)
6136 goto done;
6138 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6139 &have_staged_files);
6140 if (err && err->code != GOT_ERR_CANCELLED)
6141 goto done;
6142 if (have_staged_files) {
6143 err = check_non_staged_files(fileindex, paths);
6144 if (err)
6145 goto done;
6148 cc_arg.commitable_paths = &commitable_paths;
6149 cc_arg.worktree = worktree;
6150 cc_arg.fileindex = fileindex;
6151 cc_arg.repo = repo;
6152 cc_arg.have_staged_files = have_staged_files;
6153 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6154 cc_arg.diff_header_shown = 0;
6155 if (show_diff) {
6156 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6157 GOT_TMPDIR_STR "/got-diff");
6158 if (err)
6159 goto done;
6160 cc_arg.f1 = got_opentemp();
6161 if (cc_arg.f1 == NULL) {
6162 err = got_error_from_errno("got_opentemp");
6163 goto done;
6165 cc_arg.f2 = got_opentemp();
6166 if (cc_arg.f2 == NULL) {
6167 err = got_error_from_errno("got_opentemp");
6168 goto done;
6172 TAILQ_FOREACH(pe, paths, entry) {
6173 err = worktree_status(worktree, pe->path, fileindex, repo,
6174 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6175 if (err)
6176 goto done;
6179 if (show_diff) {
6180 if (fflush(cc_arg.diff_outfile) == EOF) {
6181 err = got_error_from_errno("fflush");
6182 goto done;
6186 if (TAILQ_EMPTY(&commitable_paths)) {
6187 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6188 goto done;
6191 TAILQ_FOREACH(pe, paths, entry) {
6192 err = check_path_is_commitable(pe->path, &commitable_paths);
6193 if (err)
6194 goto done;
6197 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6198 struct got_commitable *ct = pe->data;
6199 const char *ct_path = ct->in_repo_path;
6201 while (ct_path[0] == '/')
6202 ct_path++;
6203 err = check_out_of_date(ct_path, ct->status,
6204 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6205 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6206 if (err)
6207 goto done;
6211 err = commit_worktree(new_commit_id, &commitable_paths,
6212 head_commit_id, NULL, worktree, author, committer, diff_path,
6213 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6214 if (err)
6215 goto done;
6217 err = update_fileindex_after_commit(worktree, &commitable_paths,
6218 *new_commit_id, fileindex, have_staged_files);
6219 sync_err = sync_fileindex(fileindex, fileindex_path);
6220 if (sync_err && err == NULL)
6221 err = sync_err;
6222 done:
6223 if (fileindex)
6224 got_fileindex_free(fileindex);
6225 free(fileindex_path);
6226 unlockerr = lock_worktree(worktree, LOCK_SH);
6227 if (unlockerr && err == NULL)
6228 err = unlockerr;
6229 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6230 struct got_commitable *ct = pe->data;
6231 free_commitable(ct);
6233 got_pathlist_free(&commitable_paths);
6234 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6235 err = got_error_from_errno2("unlink", diff_path);
6236 free(diff_path);
6237 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6238 err == NULL)
6239 err = got_error_from_errno("fclose");
6240 return err;
6243 const char *
6244 got_commitable_get_path(struct got_commitable *ct)
6246 return ct->path;
6249 unsigned int
6250 got_commitable_get_status(struct got_commitable *ct)
6252 return ct->status;
6255 struct check_rebase_ok_arg {
6256 struct got_worktree *worktree;
6257 struct got_repository *repo;
6260 static const struct got_error *
6261 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6263 const struct got_error *err = NULL;
6264 struct check_rebase_ok_arg *a = arg;
6265 unsigned char status;
6266 struct stat sb;
6267 char *ondisk_path;
6269 /* Reject rebase of a work tree with mixed base commits. */
6270 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6271 SHA1_DIGEST_LENGTH))
6272 return got_error(GOT_ERR_MIXED_COMMITS);
6274 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6275 == -1)
6276 return got_error_from_errno("asprintf");
6278 /* Reject rebase of a work tree with modified or staged files. */
6279 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6280 free(ondisk_path);
6281 if (err)
6282 return err;
6284 if (status != GOT_STATUS_NO_CHANGE)
6285 return got_error(GOT_ERR_MODIFIED);
6286 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6287 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6289 return NULL;
6292 const struct got_error *
6293 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6294 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6295 struct got_worktree *worktree, struct got_reference *branch,
6296 struct got_repository *repo)
6298 const struct got_error *err = NULL;
6299 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6300 char *branch_ref_name = NULL;
6301 char *fileindex_path = NULL;
6302 struct check_rebase_ok_arg ok_arg;
6303 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6304 struct got_object_id *wt_branch_tip = NULL;
6306 *new_base_branch_ref = NULL;
6307 *tmp_branch = NULL;
6308 *fileindex = NULL;
6310 err = lock_worktree(worktree, LOCK_EX);
6311 if (err)
6312 return err;
6314 err = open_fileindex(fileindex, &fileindex_path, worktree);
6315 if (err)
6316 goto done;
6318 ok_arg.worktree = worktree;
6319 ok_arg.repo = repo;
6320 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6321 &ok_arg);
6322 if (err)
6323 goto done;
6325 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6326 if (err)
6327 goto done;
6329 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6330 if (err)
6331 goto done;
6333 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6334 if (err)
6335 goto done;
6337 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6338 0);
6339 if (err)
6340 goto done;
6342 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6343 if (err)
6344 goto done;
6345 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6346 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6347 goto done;
6350 err = got_ref_alloc_symref(new_base_branch_ref,
6351 new_base_branch_ref_name, wt_branch);
6352 if (err)
6353 goto done;
6354 err = got_ref_write(*new_base_branch_ref, repo);
6355 if (err)
6356 goto done;
6358 /* TODO Lock original branch's ref while rebasing? */
6360 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6361 if (err)
6362 goto done;
6364 err = got_ref_write(branch_ref, repo);
6365 if (err)
6366 goto done;
6368 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6369 worktree->base_commit_id);
6370 if (err)
6371 goto done;
6372 err = got_ref_write(*tmp_branch, repo);
6373 if (err)
6374 goto done;
6376 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6377 if (err)
6378 goto done;
6379 done:
6380 free(fileindex_path);
6381 free(tmp_branch_name);
6382 free(new_base_branch_ref_name);
6383 free(branch_ref_name);
6384 if (branch_ref)
6385 got_ref_close(branch_ref);
6386 if (wt_branch)
6387 got_ref_close(wt_branch);
6388 free(wt_branch_tip);
6389 if (err) {
6390 if (*new_base_branch_ref) {
6391 got_ref_close(*new_base_branch_ref);
6392 *new_base_branch_ref = NULL;
6394 if (*tmp_branch) {
6395 got_ref_close(*tmp_branch);
6396 *tmp_branch = NULL;
6398 if (*fileindex) {
6399 got_fileindex_free(*fileindex);
6400 *fileindex = NULL;
6402 lock_worktree(worktree, LOCK_SH);
6404 return err;
6407 const struct got_error *
6408 got_worktree_rebase_continue(struct got_object_id **commit_id,
6409 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6410 struct got_reference **branch, struct got_fileindex **fileindex,
6411 struct got_worktree *worktree, struct got_repository *repo)
6413 const struct got_error *err;
6414 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6415 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6416 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6417 char *fileindex_path = NULL;
6418 int have_staged_files = 0;
6420 *commit_id = NULL;
6421 *new_base_branch = NULL;
6422 *tmp_branch = NULL;
6423 *branch = NULL;
6424 *fileindex = NULL;
6426 err = lock_worktree(worktree, LOCK_EX);
6427 if (err)
6428 return err;
6430 err = open_fileindex(fileindex, &fileindex_path, worktree);
6431 if (err)
6432 goto done;
6434 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6435 &have_staged_files);
6436 if (err && err->code != GOT_ERR_CANCELLED)
6437 goto done;
6438 if (have_staged_files) {
6439 err = got_error(GOT_ERR_STAGED_PATHS);
6440 goto done;
6443 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6444 if (err)
6445 goto done;
6447 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6448 if (err)
6449 goto done;
6451 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6452 if (err)
6453 goto done;
6455 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6456 if (err)
6457 goto done;
6459 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6460 if (err)
6461 goto done;
6463 err = got_ref_open(branch, repo,
6464 got_ref_get_symref_target(branch_ref), 0);
6465 if (err)
6466 goto done;
6468 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6469 if (err)
6470 goto done;
6472 err = got_ref_resolve(commit_id, repo, commit_ref);
6473 if (err)
6474 goto done;
6476 err = got_ref_open(new_base_branch, repo,
6477 new_base_branch_ref_name, 0);
6478 if (err)
6479 goto done;
6481 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6482 if (err)
6483 goto done;
6484 done:
6485 free(commit_ref_name);
6486 free(branch_ref_name);
6487 free(fileindex_path);
6488 if (commit_ref)
6489 got_ref_close(commit_ref);
6490 if (branch_ref)
6491 got_ref_close(branch_ref);
6492 if (err) {
6493 free(*commit_id);
6494 *commit_id = NULL;
6495 if (*tmp_branch) {
6496 got_ref_close(*tmp_branch);
6497 *tmp_branch = NULL;
6499 if (*new_base_branch) {
6500 got_ref_close(*new_base_branch);
6501 *new_base_branch = NULL;
6503 if (*branch) {
6504 got_ref_close(*branch);
6505 *branch = NULL;
6507 if (*fileindex) {
6508 got_fileindex_free(*fileindex);
6509 *fileindex = NULL;
6511 lock_worktree(worktree, LOCK_SH);
6513 return err;
6516 const struct got_error *
6517 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6519 const struct got_error *err;
6520 char *tmp_branch_name = NULL;
6522 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6523 if (err)
6524 return err;
6526 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6527 free(tmp_branch_name);
6528 return NULL;
6531 static const struct got_error *
6532 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6533 const char *diff_path, char **logmsg, void *arg)
6535 *logmsg = arg;
6536 return NULL;
6539 static const struct got_error *
6540 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6541 const char *path, struct got_object_id *blob_id,
6542 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6543 int dirfd, const char *de_name)
6545 return NULL;
6548 struct collect_merged_paths_arg {
6549 got_worktree_checkout_cb progress_cb;
6550 void *progress_arg;
6551 struct got_pathlist_head *merged_paths;
6554 static const struct got_error *
6555 collect_merged_paths(void *arg, unsigned char status, const char *path)
6557 const struct got_error *err;
6558 struct collect_merged_paths_arg *a = arg;
6559 char *p;
6560 struct got_pathlist_entry *new;
6562 err = (*a->progress_cb)(a->progress_arg, status, path);
6563 if (err)
6564 return err;
6566 if (status != GOT_STATUS_MERGE &&
6567 status != GOT_STATUS_ADD &&
6568 status != GOT_STATUS_DELETE &&
6569 status != GOT_STATUS_CONFLICT)
6570 return NULL;
6572 p = strdup(path);
6573 if (p == NULL)
6574 return got_error_from_errno("strdup");
6576 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6577 if (err || new == NULL)
6578 free(p);
6579 return err;
6582 void
6583 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6585 struct got_pathlist_entry *pe;
6587 TAILQ_FOREACH(pe, merged_paths, entry)
6588 free((char *)pe->path);
6590 got_pathlist_free(merged_paths);
6593 static const struct got_error *
6594 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6595 int is_rebase, struct got_repository *repo)
6597 const struct got_error *err;
6598 struct got_reference *commit_ref = NULL;
6600 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6601 if (err) {
6602 if (err->code != GOT_ERR_NOT_REF)
6603 goto done;
6604 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6605 if (err)
6606 goto done;
6607 err = got_ref_write(commit_ref, repo);
6608 if (err)
6609 goto done;
6610 } else if (is_rebase) {
6611 struct got_object_id *stored_id;
6612 int cmp;
6614 err = got_ref_resolve(&stored_id, repo, commit_ref);
6615 if (err)
6616 goto done;
6617 cmp = got_object_id_cmp(commit_id, stored_id);
6618 free(stored_id);
6619 if (cmp != 0) {
6620 err = got_error(GOT_ERR_REBASE_COMMITID);
6621 goto done;
6624 done:
6625 if (commit_ref)
6626 got_ref_close(commit_ref);
6627 return err;
6630 static const struct got_error *
6631 rebase_merge_files(struct got_pathlist_head *merged_paths,
6632 const char *commit_ref_name, struct got_worktree *worktree,
6633 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6634 struct got_object_id *commit_id, struct got_repository *repo,
6635 got_worktree_checkout_cb progress_cb, void *progress_arg,
6636 got_cancel_cb cancel_cb, void *cancel_arg)
6638 const struct got_error *err;
6639 struct got_reference *commit_ref = NULL;
6640 struct collect_merged_paths_arg cmp_arg;
6641 char *fileindex_path;
6643 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6645 err = get_fileindex_path(&fileindex_path, worktree);
6646 if (err)
6647 return err;
6649 cmp_arg.progress_cb = progress_cb;
6650 cmp_arg.progress_arg = progress_arg;
6651 cmp_arg.merged_paths = merged_paths;
6652 err = merge_files(worktree, fileindex, fileindex_path,
6653 parent_commit_id, commit_id, repo, collect_merged_paths,
6654 &cmp_arg, cancel_cb, cancel_arg);
6655 if (commit_ref)
6656 got_ref_close(commit_ref);
6657 return err;
6660 const struct got_error *
6661 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6662 struct got_worktree *worktree, struct got_fileindex *fileindex,
6663 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6664 struct got_repository *repo,
6665 got_worktree_checkout_cb progress_cb, void *progress_arg,
6666 got_cancel_cb cancel_cb, void *cancel_arg)
6668 const struct got_error *err;
6669 char *commit_ref_name;
6671 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6672 if (err)
6673 return err;
6675 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6676 if (err)
6677 goto done;
6679 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6680 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6681 progress_arg, cancel_cb, cancel_arg);
6682 done:
6683 free(commit_ref_name);
6684 return err;
6687 const struct got_error *
6688 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6689 struct got_worktree *worktree, struct got_fileindex *fileindex,
6690 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6691 struct got_repository *repo,
6692 got_worktree_checkout_cb progress_cb, void *progress_arg,
6693 got_cancel_cb cancel_cb, void *cancel_arg)
6695 const struct got_error *err;
6696 char *commit_ref_name;
6698 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6699 if (err)
6700 return err;
6702 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6703 if (err)
6704 goto done;
6706 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6707 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6708 progress_arg, cancel_cb, cancel_arg);
6709 done:
6710 free(commit_ref_name);
6711 return err;
6714 static const struct got_error *
6715 rebase_commit(struct got_object_id **new_commit_id,
6716 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6717 struct got_worktree *worktree, struct got_fileindex *fileindex,
6718 struct got_reference *tmp_branch, const char *committer,
6719 struct got_commit_object *orig_commit, const char *new_logmsg,
6720 struct got_repository *repo)
6722 const struct got_error *err, *sync_err;
6723 struct got_pathlist_head commitable_paths;
6724 struct collect_commitables_arg cc_arg;
6725 char *fileindex_path = NULL;
6726 struct got_reference *head_ref = NULL;
6727 struct got_object_id *head_commit_id = NULL;
6728 char *logmsg = NULL;
6730 memset(&cc_arg, 0, sizeof(cc_arg));
6731 TAILQ_INIT(&commitable_paths);
6732 *new_commit_id = NULL;
6734 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6736 err = get_fileindex_path(&fileindex_path, worktree);
6737 if (err)
6738 return err;
6740 cc_arg.commitable_paths = &commitable_paths;
6741 cc_arg.worktree = worktree;
6742 cc_arg.repo = repo;
6743 cc_arg.have_staged_files = 0;
6745 * If possible get the status of individual files directly to
6746 * avoid crawling the entire work tree once per rebased commit.
6748 * Ideally, merged_paths would contain a list of commitables
6749 * we could use so we could skip worktree_status() entirely.
6750 * However, we would then need carefully keep track of cumulative
6751 * effects of operations such as file additions and deletions
6752 * in 'got histedit -f' (folding multiple commits into one),
6753 * and this extra complexity is not really worth it.
6755 if (merged_paths) {
6756 struct got_pathlist_entry *pe;
6757 TAILQ_FOREACH(pe, merged_paths, entry) {
6758 err = worktree_status(worktree, pe->path, fileindex,
6759 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6760 0);
6761 if (err)
6762 goto done;
6764 } else {
6765 err = worktree_status(worktree, "", fileindex, repo,
6766 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6767 if (err)
6768 goto done;
6771 if (TAILQ_EMPTY(&commitable_paths)) {
6772 /* No-op change; commit will be elided. */
6773 err = got_ref_delete(commit_ref, repo);
6774 if (err)
6775 goto done;
6776 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6777 goto done;
6780 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6781 if (err)
6782 goto done;
6784 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6785 if (err)
6786 goto done;
6788 if (new_logmsg) {
6789 logmsg = strdup(new_logmsg);
6790 if (logmsg == NULL) {
6791 err = got_error_from_errno("strdup");
6792 goto done;
6794 } else {
6795 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6796 if (err)
6797 goto done;
6800 /* NB: commit_worktree will call free(logmsg) */
6801 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6802 NULL, worktree, got_object_commit_get_author(orig_commit),
6803 committer ? committer :
6804 got_object_commit_get_committer(orig_commit), NULL,
6805 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6806 if (err)
6807 goto done;
6809 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6810 if (err)
6811 goto done;
6813 err = got_ref_delete(commit_ref, repo);
6814 if (err)
6815 goto done;
6817 err = update_fileindex_after_commit(worktree, &commitable_paths,
6818 *new_commit_id, fileindex, 0);
6819 sync_err = sync_fileindex(fileindex, fileindex_path);
6820 if (sync_err && err == NULL)
6821 err = sync_err;
6822 done:
6823 free(fileindex_path);
6824 free(head_commit_id);
6825 if (head_ref)
6826 got_ref_close(head_ref);
6827 if (err) {
6828 free(*new_commit_id);
6829 *new_commit_id = NULL;
6831 return err;
6834 const struct got_error *
6835 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6836 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6837 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6838 const char *committer, struct got_commit_object *orig_commit,
6839 struct got_object_id *orig_commit_id, struct got_repository *repo)
6841 const struct got_error *err;
6842 char *commit_ref_name;
6843 struct got_reference *commit_ref = NULL;
6844 struct got_object_id *commit_id = NULL;
6846 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6847 if (err)
6848 return err;
6850 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6851 if (err)
6852 goto done;
6853 err = got_ref_resolve(&commit_id, repo, commit_ref);
6854 if (err)
6855 goto done;
6856 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6857 err = got_error(GOT_ERR_REBASE_COMMITID);
6858 goto done;
6861 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6862 worktree, fileindex, tmp_branch, committer, orig_commit,
6863 NULL, repo);
6864 done:
6865 if (commit_ref)
6866 got_ref_close(commit_ref);
6867 free(commit_ref_name);
6868 free(commit_id);
6869 return err;
6872 const struct got_error *
6873 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6874 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6875 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6876 const char *committer, struct got_commit_object *orig_commit,
6877 struct got_object_id *orig_commit_id, const char *new_logmsg,
6878 struct got_repository *repo)
6880 const struct got_error *err;
6881 char *commit_ref_name;
6882 struct got_reference *commit_ref = NULL;
6884 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6885 if (err)
6886 return err;
6888 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6889 if (err)
6890 goto done;
6892 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6893 worktree, fileindex, tmp_branch, committer, orig_commit,
6894 new_logmsg, repo);
6895 done:
6896 if (commit_ref)
6897 got_ref_close(commit_ref);
6898 free(commit_ref_name);
6899 return err;
6902 const struct got_error *
6903 got_worktree_rebase_postpone(struct got_worktree *worktree,
6904 struct got_fileindex *fileindex)
6906 if (fileindex)
6907 got_fileindex_free(fileindex);
6908 return lock_worktree(worktree, LOCK_SH);
6911 static const struct got_error *
6912 delete_ref(const char *name, struct got_repository *repo)
6914 const struct got_error *err;
6915 struct got_reference *ref;
6917 err = got_ref_open(&ref, repo, name, 0);
6918 if (err) {
6919 if (err->code == GOT_ERR_NOT_REF)
6920 return NULL;
6921 return err;
6924 err = got_ref_delete(ref, repo);
6925 got_ref_close(ref);
6926 return err;
6929 static const struct got_error *
6930 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6932 const struct got_error *err;
6933 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6934 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6936 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6937 if (err)
6938 goto done;
6939 err = delete_ref(tmp_branch_name, repo);
6940 if (err)
6941 goto done;
6943 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6944 if (err)
6945 goto done;
6946 err = delete_ref(new_base_branch_ref_name, repo);
6947 if (err)
6948 goto done;
6950 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6951 if (err)
6952 goto done;
6953 err = delete_ref(branch_ref_name, repo);
6954 if (err)
6955 goto done;
6957 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6958 if (err)
6959 goto done;
6960 err = delete_ref(commit_ref_name, repo);
6961 if (err)
6962 goto done;
6964 done:
6965 free(tmp_branch_name);
6966 free(new_base_branch_ref_name);
6967 free(branch_ref_name);
6968 free(commit_ref_name);
6969 return err;
6972 static const struct got_error *
6973 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6974 struct got_object_id *new_commit_id, struct got_repository *repo)
6976 const struct got_error *err;
6977 struct got_reference *ref = NULL;
6978 struct got_object_id *old_commit_id = NULL;
6979 const char *branch_name = NULL;
6980 char *new_id_str = NULL;
6981 char *refname = NULL;
6983 branch_name = got_ref_get_name(branch);
6984 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6985 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6986 branch_name += 11;
6988 err = got_object_id_str(&new_id_str, new_commit_id);
6989 if (err)
6990 return err;
6992 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6993 new_id_str) == -1) {
6994 err = got_error_from_errno("asprintf");
6995 goto done;
6998 err = got_ref_resolve(&old_commit_id, repo, branch);
6999 if (err)
7000 goto done;
7002 err = got_ref_alloc(&ref, refname, old_commit_id);
7003 if (err)
7004 goto done;
7006 err = got_ref_write(ref, repo);
7007 done:
7008 free(new_id_str);
7009 free(refname);
7010 free(old_commit_id);
7011 if (ref)
7012 got_ref_close(ref);
7013 return err;
7016 const struct got_error *
7017 got_worktree_rebase_complete(struct got_worktree *worktree,
7018 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
7019 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
7020 struct got_repository *repo, int create_backup)
7022 const struct got_error *err, *unlockerr, *sync_err;
7023 struct got_object_id *new_head_commit_id = NULL;
7024 char *fileindex_path = NULL;
7026 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7027 if (err)
7028 return err;
7030 if (create_backup) {
7031 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7032 rebased_branch, new_head_commit_id, repo);
7033 if (err)
7034 goto done;
7037 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7038 if (err)
7039 goto done;
7041 err = got_ref_write(rebased_branch, repo);
7042 if (err)
7043 goto done;
7045 err = got_worktree_set_head_ref(worktree, rebased_branch);
7046 if (err)
7047 goto done;
7049 err = delete_rebase_refs(worktree, repo);
7050 if (err)
7051 goto done;
7053 err = get_fileindex_path(&fileindex_path, worktree);
7054 if (err)
7055 goto done;
7056 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7057 sync_err = sync_fileindex(fileindex, fileindex_path);
7058 if (sync_err && err == NULL)
7059 err = sync_err;
7060 done:
7061 got_fileindex_free(fileindex);
7062 free(fileindex_path);
7063 free(new_head_commit_id);
7064 unlockerr = lock_worktree(worktree, LOCK_SH);
7065 if (unlockerr && err == NULL)
7066 err = unlockerr;
7067 return err;
7070 const struct got_error *
7071 got_worktree_rebase_abort(struct got_worktree *worktree,
7072 struct got_fileindex *fileindex, struct got_repository *repo,
7073 struct got_reference *new_base_branch,
7074 got_worktree_checkout_cb progress_cb, void *progress_arg)
7076 const struct got_error *err, *unlockerr, *sync_err;
7077 struct got_reference *resolved = NULL;
7078 struct got_object_id *commit_id = NULL;
7079 struct got_commit_object *commit = NULL;
7080 char *fileindex_path = NULL;
7081 struct revert_file_args rfa;
7082 struct got_object_id *tree_id = NULL;
7084 err = lock_worktree(worktree, LOCK_EX);
7085 if (err)
7086 return err;
7088 err = got_object_open_as_commit(&commit, repo,
7089 worktree->base_commit_id);
7090 if (err)
7091 goto done;
7093 err = got_ref_open(&resolved, repo,
7094 got_ref_get_symref_target(new_base_branch), 0);
7095 if (err)
7096 goto done;
7098 err = got_worktree_set_head_ref(worktree, resolved);
7099 if (err)
7100 goto done;
7103 * XXX commits to the base branch could have happened while
7104 * we were busy rebasing; should we store the original commit ID
7105 * when rebase begins and read it back here?
7107 err = got_ref_resolve(&commit_id, repo, resolved);
7108 if (err)
7109 goto done;
7111 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7112 if (err)
7113 goto done;
7115 err = got_object_id_by_path(&tree_id, repo, commit,
7116 worktree->path_prefix);
7117 if (err)
7118 goto done;
7120 err = delete_rebase_refs(worktree, repo);
7121 if (err)
7122 goto done;
7124 err = get_fileindex_path(&fileindex_path, worktree);
7125 if (err)
7126 goto done;
7128 rfa.worktree = worktree;
7129 rfa.fileindex = fileindex;
7130 rfa.progress_cb = progress_cb;
7131 rfa.progress_arg = progress_arg;
7132 rfa.patch_cb = NULL;
7133 rfa.patch_arg = NULL;
7134 rfa.repo = repo;
7135 rfa.unlink_added_files = 0;
7136 err = worktree_status(worktree, "", fileindex, repo,
7137 revert_file, &rfa, NULL, NULL, 1, 0);
7138 if (err)
7139 goto sync;
7141 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7142 repo, progress_cb, progress_arg, NULL, NULL);
7143 sync:
7144 sync_err = sync_fileindex(fileindex, fileindex_path);
7145 if (sync_err && err == NULL)
7146 err = sync_err;
7147 done:
7148 got_ref_close(resolved);
7149 free(tree_id);
7150 free(commit_id);
7151 if (commit)
7152 got_object_commit_close(commit);
7153 if (fileindex)
7154 got_fileindex_free(fileindex);
7155 free(fileindex_path);
7157 unlockerr = lock_worktree(worktree, LOCK_SH);
7158 if (unlockerr && err == NULL)
7159 err = unlockerr;
7160 return err;
7163 const struct got_error *
7164 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7165 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7166 struct got_fileindex **fileindex, struct got_worktree *worktree,
7167 struct got_repository *repo)
7169 const struct got_error *err = NULL;
7170 char *tmp_branch_name = NULL;
7171 char *branch_ref_name = NULL;
7172 char *base_commit_ref_name = NULL;
7173 char *fileindex_path = NULL;
7174 struct check_rebase_ok_arg ok_arg;
7175 struct got_reference *wt_branch = NULL;
7176 struct got_reference *base_commit_ref = NULL;
7178 *tmp_branch = NULL;
7179 *branch_ref = NULL;
7180 *base_commit_id = NULL;
7181 *fileindex = NULL;
7183 err = lock_worktree(worktree, LOCK_EX);
7184 if (err)
7185 return err;
7187 err = open_fileindex(fileindex, &fileindex_path, worktree);
7188 if (err)
7189 goto done;
7191 ok_arg.worktree = worktree;
7192 ok_arg.repo = repo;
7193 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7194 &ok_arg);
7195 if (err)
7196 goto done;
7198 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7199 if (err)
7200 goto done;
7202 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7203 if (err)
7204 goto done;
7206 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7207 worktree);
7208 if (err)
7209 goto done;
7211 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7212 0);
7213 if (err)
7214 goto done;
7216 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7217 if (err)
7218 goto done;
7220 err = got_ref_write(*branch_ref, repo);
7221 if (err)
7222 goto done;
7224 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7225 worktree->base_commit_id);
7226 if (err)
7227 goto done;
7228 err = got_ref_write(base_commit_ref, repo);
7229 if (err)
7230 goto done;
7231 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7232 if (*base_commit_id == NULL) {
7233 err = got_error_from_errno("got_object_id_dup");
7234 goto done;
7237 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7238 worktree->base_commit_id);
7239 if (err)
7240 goto done;
7241 err = got_ref_write(*tmp_branch, repo);
7242 if (err)
7243 goto done;
7245 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7246 if (err)
7247 goto done;
7248 done:
7249 free(fileindex_path);
7250 free(tmp_branch_name);
7251 free(branch_ref_name);
7252 free(base_commit_ref_name);
7253 if (wt_branch)
7254 got_ref_close(wt_branch);
7255 if (err) {
7256 if (*branch_ref) {
7257 got_ref_close(*branch_ref);
7258 *branch_ref = NULL;
7260 if (*tmp_branch) {
7261 got_ref_close(*tmp_branch);
7262 *tmp_branch = NULL;
7264 free(*base_commit_id);
7265 if (*fileindex) {
7266 got_fileindex_free(*fileindex);
7267 *fileindex = NULL;
7269 lock_worktree(worktree, LOCK_SH);
7271 return err;
7274 const struct got_error *
7275 got_worktree_histedit_postpone(struct got_worktree *worktree,
7276 struct got_fileindex *fileindex)
7278 if (fileindex)
7279 got_fileindex_free(fileindex);
7280 return lock_worktree(worktree, LOCK_SH);
7283 const struct got_error *
7284 got_worktree_histedit_in_progress(int *in_progress,
7285 struct got_worktree *worktree)
7287 const struct got_error *err;
7288 char *tmp_branch_name = NULL;
7290 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7291 if (err)
7292 return err;
7294 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7295 free(tmp_branch_name);
7296 return NULL;
7299 const struct got_error *
7300 got_worktree_histedit_continue(struct got_object_id **commit_id,
7301 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7302 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7303 struct got_worktree *worktree, struct got_repository *repo)
7305 const struct got_error *err;
7306 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7307 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7308 struct got_reference *commit_ref = NULL;
7309 struct got_reference *base_commit_ref = NULL;
7310 char *fileindex_path = NULL;
7311 int have_staged_files = 0;
7313 *commit_id = NULL;
7314 *tmp_branch = NULL;
7315 *base_commit_id = NULL;
7316 *fileindex = NULL;
7318 err = lock_worktree(worktree, LOCK_EX);
7319 if (err)
7320 return err;
7322 err = open_fileindex(fileindex, &fileindex_path, worktree);
7323 if (err)
7324 goto done;
7326 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7327 &have_staged_files);
7328 if (err && err->code != GOT_ERR_CANCELLED)
7329 goto done;
7330 if (have_staged_files) {
7331 err = got_error(GOT_ERR_STAGED_PATHS);
7332 goto done;
7335 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7336 if (err)
7337 goto done;
7339 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7340 if (err)
7341 goto done;
7343 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7344 if (err)
7345 goto done;
7347 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7348 worktree);
7349 if (err)
7350 goto done;
7352 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7353 if (err)
7354 goto done;
7356 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7357 if (err)
7358 goto done;
7359 err = got_ref_resolve(commit_id, repo, commit_ref);
7360 if (err)
7361 goto done;
7363 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7364 if (err)
7365 goto done;
7366 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7367 if (err)
7368 goto done;
7370 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7371 if (err)
7372 goto done;
7373 done:
7374 free(commit_ref_name);
7375 free(branch_ref_name);
7376 free(fileindex_path);
7377 if (commit_ref)
7378 got_ref_close(commit_ref);
7379 if (base_commit_ref)
7380 got_ref_close(base_commit_ref);
7381 if (err) {
7382 free(*commit_id);
7383 *commit_id = NULL;
7384 free(*base_commit_id);
7385 *base_commit_id = NULL;
7386 if (*tmp_branch) {
7387 got_ref_close(*tmp_branch);
7388 *tmp_branch = NULL;
7390 if (*fileindex) {
7391 got_fileindex_free(*fileindex);
7392 *fileindex = NULL;
7394 lock_worktree(worktree, LOCK_EX);
7396 return err;
7399 static const struct got_error *
7400 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7402 const struct got_error *err;
7403 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7404 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7406 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7407 if (err)
7408 goto done;
7409 err = delete_ref(tmp_branch_name, repo);
7410 if (err)
7411 goto done;
7413 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7414 worktree);
7415 if (err)
7416 goto done;
7417 err = delete_ref(base_commit_ref_name, repo);
7418 if (err)
7419 goto done;
7421 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7422 if (err)
7423 goto done;
7424 err = delete_ref(branch_ref_name, repo);
7425 if (err)
7426 goto done;
7428 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7429 if (err)
7430 goto done;
7431 err = delete_ref(commit_ref_name, repo);
7432 if (err)
7433 goto done;
7434 done:
7435 free(tmp_branch_name);
7436 free(base_commit_ref_name);
7437 free(branch_ref_name);
7438 free(commit_ref_name);
7439 return err;
7442 const struct got_error *
7443 got_worktree_histedit_abort(struct got_worktree *worktree,
7444 struct got_fileindex *fileindex, struct got_repository *repo,
7445 struct got_reference *branch, struct got_object_id *base_commit_id,
7446 got_worktree_checkout_cb progress_cb, void *progress_arg)
7448 const struct got_error *err, *unlockerr, *sync_err;
7449 struct got_reference *resolved = NULL;
7450 char *fileindex_path = NULL;
7451 struct got_commit_object *commit = NULL;
7452 struct got_object_id *tree_id = NULL;
7453 struct revert_file_args rfa;
7455 err = lock_worktree(worktree, LOCK_EX);
7456 if (err)
7457 return err;
7459 err = got_object_open_as_commit(&commit, repo,
7460 worktree->base_commit_id);
7461 if (err)
7462 goto done;
7464 err = got_ref_open(&resolved, repo,
7465 got_ref_get_symref_target(branch), 0);
7466 if (err)
7467 goto done;
7469 err = got_worktree_set_head_ref(worktree, resolved);
7470 if (err)
7471 goto done;
7473 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7474 if (err)
7475 goto done;
7477 err = got_object_id_by_path(&tree_id, repo, commit,
7478 worktree->path_prefix);
7479 if (err)
7480 goto done;
7482 err = delete_histedit_refs(worktree, repo);
7483 if (err)
7484 goto done;
7486 err = get_fileindex_path(&fileindex_path, worktree);
7487 if (err)
7488 goto done;
7490 rfa.worktree = worktree;
7491 rfa.fileindex = fileindex;
7492 rfa.progress_cb = progress_cb;
7493 rfa.progress_arg = progress_arg;
7494 rfa.patch_cb = NULL;
7495 rfa.patch_arg = NULL;
7496 rfa.repo = repo;
7497 rfa.unlink_added_files = 0;
7498 err = worktree_status(worktree, "", fileindex, repo,
7499 revert_file, &rfa, NULL, NULL, 1, 0);
7500 if (err)
7501 goto sync;
7503 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7504 repo, progress_cb, progress_arg, NULL, NULL);
7505 sync:
7506 sync_err = sync_fileindex(fileindex, fileindex_path);
7507 if (sync_err && err == NULL)
7508 err = sync_err;
7509 done:
7510 got_ref_close(resolved);
7511 free(tree_id);
7512 free(fileindex_path);
7514 unlockerr = lock_worktree(worktree, LOCK_SH);
7515 if (unlockerr && err == NULL)
7516 err = unlockerr;
7517 return err;
7520 const struct got_error *
7521 got_worktree_histedit_complete(struct got_worktree *worktree,
7522 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7523 struct got_reference *edited_branch, struct got_repository *repo)
7525 const struct got_error *err, *unlockerr, *sync_err;
7526 struct got_object_id *new_head_commit_id = NULL;
7527 struct got_reference *resolved = NULL;
7528 char *fileindex_path = NULL;
7530 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7531 if (err)
7532 return err;
7534 err = got_ref_open(&resolved, repo,
7535 got_ref_get_symref_target(edited_branch), 0);
7536 if (err)
7537 goto done;
7539 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7540 resolved, new_head_commit_id, repo);
7541 if (err)
7542 goto done;
7544 err = got_ref_change_ref(resolved, new_head_commit_id);
7545 if (err)
7546 goto done;
7548 err = got_ref_write(resolved, repo);
7549 if (err)
7550 goto done;
7552 err = got_worktree_set_head_ref(worktree, resolved);
7553 if (err)
7554 goto done;
7556 err = delete_histedit_refs(worktree, repo);
7557 if (err)
7558 goto done;
7560 err = get_fileindex_path(&fileindex_path, worktree);
7561 if (err)
7562 goto done;
7563 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7564 sync_err = sync_fileindex(fileindex, fileindex_path);
7565 if (sync_err && err == NULL)
7566 err = sync_err;
7567 done:
7568 got_fileindex_free(fileindex);
7569 free(fileindex_path);
7570 free(new_head_commit_id);
7571 unlockerr = lock_worktree(worktree, LOCK_SH);
7572 if (unlockerr && err == NULL)
7573 err = unlockerr;
7574 return err;
7577 const struct got_error *
7578 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7579 struct got_object_id *commit_id, struct got_repository *repo)
7581 const struct got_error *err;
7582 char *commit_ref_name;
7584 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7585 if (err)
7586 return err;
7588 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7589 if (err)
7590 goto done;
7592 err = delete_ref(commit_ref_name, repo);
7593 done:
7594 free(commit_ref_name);
7595 return err;
7598 const struct got_error *
7599 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7600 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7601 struct got_worktree *worktree, const char *refname,
7602 struct got_repository *repo)
7604 const struct got_error *err = NULL;
7605 char *fileindex_path = NULL;
7606 struct check_rebase_ok_arg ok_arg;
7608 *fileindex = NULL;
7609 *branch_ref = NULL;
7610 *base_branch_ref = NULL;
7612 err = lock_worktree(worktree, LOCK_EX);
7613 if (err)
7614 return err;
7616 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7617 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7618 "cannot integrate a branch into itself; "
7619 "update -b or different branch name required");
7620 goto done;
7623 err = open_fileindex(fileindex, &fileindex_path, worktree);
7624 if (err)
7625 goto done;
7627 /* Preconditions are the same as for rebase. */
7628 ok_arg.worktree = worktree;
7629 ok_arg.repo = repo;
7630 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7631 &ok_arg);
7632 if (err)
7633 goto done;
7635 err = got_ref_open(branch_ref, repo, refname, 1);
7636 if (err)
7637 goto done;
7639 err = got_ref_open(base_branch_ref, repo,
7640 got_worktree_get_head_ref_name(worktree), 1);
7641 done:
7642 if (err) {
7643 if (*branch_ref) {
7644 got_ref_close(*branch_ref);
7645 *branch_ref = NULL;
7647 if (*base_branch_ref) {
7648 got_ref_close(*base_branch_ref);
7649 *base_branch_ref = NULL;
7651 if (*fileindex) {
7652 got_fileindex_free(*fileindex);
7653 *fileindex = NULL;
7655 lock_worktree(worktree, LOCK_SH);
7657 return err;
7660 const struct got_error *
7661 got_worktree_integrate_continue(struct got_worktree *worktree,
7662 struct got_fileindex *fileindex, struct got_repository *repo,
7663 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7664 got_worktree_checkout_cb progress_cb, void *progress_arg,
7665 got_cancel_cb cancel_cb, void *cancel_arg)
7667 const struct got_error *err = NULL, *sync_err, *unlockerr;
7668 char *fileindex_path = NULL;
7669 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7670 struct got_commit_object *commit = NULL;
7672 err = get_fileindex_path(&fileindex_path, worktree);
7673 if (err)
7674 goto done;
7676 err = got_ref_resolve(&commit_id, repo, branch_ref);
7677 if (err)
7678 goto done;
7680 err = got_object_open_as_commit(&commit, repo, commit_id);
7681 if (err)
7682 goto done;
7684 err = got_object_id_by_path(&tree_id, repo, commit,
7685 worktree->path_prefix);
7686 if (err)
7687 goto done;
7689 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7690 if (err)
7691 goto done;
7693 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7694 progress_cb, progress_arg, cancel_cb, cancel_arg);
7695 if (err)
7696 goto sync;
7698 err = got_ref_change_ref(base_branch_ref, commit_id);
7699 if (err)
7700 goto sync;
7702 err = got_ref_write(base_branch_ref, repo);
7703 if (err)
7704 goto sync;
7706 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7707 sync:
7708 sync_err = sync_fileindex(fileindex, fileindex_path);
7709 if (sync_err && err == NULL)
7710 err = sync_err;
7712 done:
7713 unlockerr = got_ref_unlock(branch_ref);
7714 if (unlockerr && err == NULL)
7715 err = unlockerr;
7716 got_ref_close(branch_ref);
7718 unlockerr = got_ref_unlock(base_branch_ref);
7719 if (unlockerr && err == NULL)
7720 err = unlockerr;
7721 got_ref_close(base_branch_ref);
7723 got_fileindex_free(fileindex);
7724 free(fileindex_path);
7725 free(tree_id);
7726 if (commit)
7727 got_object_commit_close(commit);
7729 unlockerr = lock_worktree(worktree, LOCK_SH);
7730 if (unlockerr && err == NULL)
7731 err = unlockerr;
7732 return err;
7735 const struct got_error *
7736 got_worktree_integrate_abort(struct got_worktree *worktree,
7737 struct got_fileindex *fileindex, struct got_repository *repo,
7738 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7740 const struct got_error *err = NULL, *unlockerr = NULL;
7742 got_fileindex_free(fileindex);
7744 err = lock_worktree(worktree, LOCK_SH);
7746 unlockerr = got_ref_unlock(branch_ref);
7747 if (unlockerr && err == NULL)
7748 err = unlockerr;
7749 got_ref_close(branch_ref);
7751 unlockerr = got_ref_unlock(base_branch_ref);
7752 if (unlockerr && err == NULL)
7753 err = unlockerr;
7754 got_ref_close(base_branch_ref);
7756 return err;
7759 const struct got_error *
7760 got_worktree_merge_postpone(struct got_worktree *worktree,
7761 struct got_fileindex *fileindex)
7763 const struct got_error *err, *sync_err;
7764 char *fileindex_path = NULL;
7766 err = get_fileindex_path(&fileindex_path, worktree);
7767 if (err)
7768 goto done;
7770 sync_err = sync_fileindex(fileindex, fileindex_path);
7772 err = lock_worktree(worktree, LOCK_SH);
7773 if (sync_err && err == NULL)
7774 err = sync_err;
7775 done:
7776 got_fileindex_free(fileindex);
7777 free(fileindex_path);
7778 return err;
7781 static const struct got_error *
7782 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7784 const struct got_error *err;
7785 char *branch_refname = NULL, *commit_refname = NULL;
7787 err = get_merge_branch_ref_name(&branch_refname, worktree);
7788 if (err)
7789 goto done;
7790 err = delete_ref(branch_refname, repo);
7791 if (err)
7792 goto done;
7794 err = get_merge_commit_ref_name(&commit_refname, worktree);
7795 if (err)
7796 goto done;
7797 err = delete_ref(commit_refname, repo);
7798 if (err)
7799 goto done;
7801 done:
7802 free(branch_refname);
7803 free(commit_refname);
7804 return err;
7807 struct merge_commit_msg_arg {
7808 struct got_worktree *worktree;
7809 const char *branch_name;
7812 static const struct got_error *
7813 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7814 const char *diff_path, char **logmsg, void *arg)
7816 struct merge_commit_msg_arg *a = arg;
7818 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7819 got_worktree_get_head_ref_name(a->worktree)) == -1)
7820 return got_error_from_errno("asprintf");
7822 return NULL;
7826 const struct got_error *
7827 got_worktree_merge_branch(struct got_worktree *worktree,
7828 struct got_fileindex *fileindex,
7829 struct got_object_id *yca_commit_id,
7830 struct got_object_id *branch_tip,
7831 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7832 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7834 const struct got_error *err;
7835 char *fileindex_path = NULL;
7837 err = get_fileindex_path(&fileindex_path, worktree);
7838 if (err)
7839 goto done;
7841 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7842 worktree);
7843 if (err)
7844 goto done;
7846 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7847 branch_tip, repo, progress_cb, progress_arg,
7848 cancel_cb, cancel_arg);
7849 done:
7850 free(fileindex_path);
7851 return err;
7854 const struct got_error *
7855 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7856 struct got_worktree *worktree, struct got_fileindex *fileindex,
7857 const char *author, const char *committer, int allow_bad_symlinks,
7858 struct got_object_id *branch_tip, const char *branch_name,
7859 struct got_repository *repo,
7860 got_worktree_status_cb status_cb, void *status_arg)
7863 const struct got_error *err = NULL, *sync_err;
7864 struct got_pathlist_head commitable_paths;
7865 struct collect_commitables_arg cc_arg;
7866 struct got_pathlist_entry *pe;
7867 struct got_reference *head_ref = NULL;
7868 struct got_object_id *head_commit_id = NULL;
7869 int have_staged_files = 0;
7870 struct merge_commit_msg_arg mcm_arg;
7871 char *fileindex_path = NULL;
7873 memset(&cc_arg, 0, sizeof(cc_arg));
7874 *new_commit_id = NULL;
7876 TAILQ_INIT(&commitable_paths);
7878 err = get_fileindex_path(&fileindex_path, worktree);
7879 if (err)
7880 goto done;
7882 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7883 if (err)
7884 goto done;
7886 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7887 if (err)
7888 goto done;
7890 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7891 &have_staged_files);
7892 if (err && err->code != GOT_ERR_CANCELLED)
7893 goto done;
7894 if (have_staged_files) {
7895 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7896 goto done;
7899 cc_arg.commitable_paths = &commitable_paths;
7900 cc_arg.worktree = worktree;
7901 cc_arg.fileindex = fileindex;
7902 cc_arg.repo = repo;
7903 cc_arg.have_staged_files = have_staged_files;
7904 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7905 err = worktree_status(worktree, "", fileindex, repo,
7906 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7907 if (err)
7908 goto done;
7910 if (TAILQ_EMPTY(&commitable_paths)) {
7911 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7912 "merge of %s cannot proceed", branch_name);
7913 goto done;
7916 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7917 struct got_commitable *ct = pe->data;
7918 const char *ct_path = ct->in_repo_path;
7920 while (ct_path[0] == '/')
7921 ct_path++;
7922 err = check_out_of_date(ct_path, ct->status,
7923 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7924 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7925 if (err)
7926 goto done;
7930 mcm_arg.worktree = worktree;
7931 mcm_arg.branch_name = branch_name;
7932 err = commit_worktree(new_commit_id, &commitable_paths,
7933 head_commit_id, branch_tip, worktree, author, committer, NULL,
7934 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7935 if (err)
7936 goto done;
7938 err = update_fileindex_after_commit(worktree, &commitable_paths,
7939 *new_commit_id, fileindex, have_staged_files);
7940 sync_err = sync_fileindex(fileindex, fileindex_path);
7941 if (sync_err && err == NULL)
7942 err = sync_err;
7943 done:
7944 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7945 struct got_commitable *ct = pe->data;
7946 free_commitable(ct);
7948 got_pathlist_free(&commitable_paths);
7949 free(fileindex_path);
7950 return err;
7953 const struct got_error *
7954 got_worktree_merge_complete(struct got_worktree *worktree,
7955 struct got_fileindex *fileindex, struct got_repository *repo)
7957 const struct got_error *err, *unlockerr, *sync_err;
7958 char *fileindex_path = NULL;
7960 err = delete_merge_refs(worktree, repo);
7961 if (err)
7962 goto done;
7964 err = get_fileindex_path(&fileindex_path, worktree);
7965 if (err)
7966 goto done;
7967 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7968 sync_err = sync_fileindex(fileindex, fileindex_path);
7969 if (sync_err && err == NULL)
7970 err = sync_err;
7971 done:
7972 got_fileindex_free(fileindex);
7973 free(fileindex_path);
7974 unlockerr = lock_worktree(worktree, LOCK_SH);
7975 if (unlockerr && err == NULL)
7976 err = unlockerr;
7977 return err;
7980 const struct got_error *
7981 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7982 struct got_repository *repo)
7984 const struct got_error *err;
7985 char *branch_refname = NULL;
7986 struct got_reference *branch_ref = NULL;
7988 *in_progress = 0;
7990 err = get_merge_branch_ref_name(&branch_refname, worktree);
7991 if (err)
7992 return err;
7993 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7994 free(branch_refname);
7995 if (err) {
7996 if (err->code != GOT_ERR_NOT_REF)
7997 return err;
7998 } else
7999 *in_progress = 1;
8001 return NULL;
8004 const struct got_error *got_worktree_merge_prepare(
8005 struct got_fileindex **fileindex, struct got_worktree *worktree,
8006 struct got_reference *branch, struct got_repository *repo)
8008 const struct got_error *err = NULL;
8009 char *fileindex_path = NULL;
8010 char *branch_refname = NULL, *commit_refname = NULL;
8011 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8012 struct got_reference *commit_ref = NULL;
8013 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8014 struct check_rebase_ok_arg ok_arg;
8016 *fileindex = NULL;
8018 err = lock_worktree(worktree, LOCK_EX);
8019 if (err)
8020 return err;
8022 err = open_fileindex(fileindex, &fileindex_path, worktree);
8023 if (err)
8024 goto done;
8026 /* Preconditions are the same as for rebase. */
8027 ok_arg.worktree = worktree;
8028 ok_arg.repo = repo;
8029 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8030 &ok_arg);
8031 if (err)
8032 goto done;
8034 err = get_merge_branch_ref_name(&branch_refname, worktree);
8035 if (err)
8036 return err;
8038 err = get_merge_commit_ref_name(&commit_refname, worktree);
8039 if (err)
8040 return err;
8042 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8043 0);
8044 if (err)
8045 goto done;
8047 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8048 if (err)
8049 goto done;
8051 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8052 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8053 goto done;
8056 err = got_ref_resolve(&branch_tip, repo, branch);
8057 if (err)
8058 goto done;
8060 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8061 if (err)
8062 goto done;
8063 err = got_ref_write(branch_ref, repo);
8064 if (err)
8065 goto done;
8067 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8068 if (err)
8069 goto done;
8070 err = got_ref_write(commit_ref, repo);
8071 if (err)
8072 goto done;
8074 done:
8075 free(branch_refname);
8076 free(commit_refname);
8077 free(fileindex_path);
8078 if (branch_ref)
8079 got_ref_close(branch_ref);
8080 if (commit_ref)
8081 got_ref_close(commit_ref);
8082 if (wt_branch)
8083 got_ref_close(wt_branch);
8084 free(wt_branch_tip);
8085 if (err) {
8086 if (*fileindex) {
8087 got_fileindex_free(*fileindex);
8088 *fileindex = NULL;
8090 lock_worktree(worktree, LOCK_SH);
8092 return err;
8095 const struct got_error *
8096 got_worktree_merge_continue(char **branch_name,
8097 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8098 struct got_worktree *worktree, struct got_repository *repo)
8100 const struct got_error *err;
8101 char *commit_refname = NULL, *branch_refname = NULL;
8102 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8103 char *fileindex_path = NULL;
8104 int have_staged_files = 0;
8106 *branch_name = NULL;
8107 *branch_tip = NULL;
8108 *fileindex = NULL;
8110 err = lock_worktree(worktree, LOCK_EX);
8111 if (err)
8112 return err;
8114 err = open_fileindex(fileindex, &fileindex_path, worktree);
8115 if (err)
8116 goto done;
8118 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8119 &have_staged_files);
8120 if (err && err->code != GOT_ERR_CANCELLED)
8121 goto done;
8122 if (have_staged_files) {
8123 err = got_error(GOT_ERR_STAGED_PATHS);
8124 goto done;
8127 err = get_merge_branch_ref_name(&branch_refname, worktree);
8128 if (err)
8129 goto done;
8131 err = get_merge_commit_ref_name(&commit_refname, worktree);
8132 if (err)
8133 goto done;
8135 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8136 if (err)
8137 goto done;
8139 if (!got_ref_is_symbolic(branch_ref)) {
8140 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8141 "%s is not a symbolic reference",
8142 got_ref_get_name(branch_ref));
8143 goto done;
8145 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8146 if (*branch_name == NULL) {
8147 err = got_error_from_errno("strdup");
8148 goto done;
8151 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8152 if (err)
8153 goto done;
8155 err = got_ref_resolve(branch_tip, repo, commit_ref);
8156 if (err)
8157 goto done;
8158 done:
8159 free(commit_refname);
8160 free(branch_refname);
8161 free(fileindex_path);
8162 if (commit_ref)
8163 got_ref_close(commit_ref);
8164 if (branch_ref)
8165 got_ref_close(branch_ref);
8166 if (err) {
8167 if (*branch_name) {
8168 free(*branch_name);
8169 *branch_name = NULL;
8171 free(*branch_tip);
8172 *branch_tip = NULL;
8173 if (*fileindex) {
8174 got_fileindex_free(*fileindex);
8175 *fileindex = NULL;
8177 lock_worktree(worktree, LOCK_SH);
8179 return err;
8182 const struct got_error *
8183 got_worktree_merge_abort(struct got_worktree *worktree,
8184 struct got_fileindex *fileindex, struct got_repository *repo,
8185 got_worktree_checkout_cb progress_cb, void *progress_arg)
8187 const struct got_error *err, *unlockerr, *sync_err;
8188 struct got_object_id *commit_id = NULL;
8189 struct got_commit_object *commit = NULL;
8190 char *fileindex_path = NULL;
8191 struct revert_file_args rfa;
8192 struct got_object_id *tree_id = NULL;
8194 err = got_object_open_as_commit(&commit, repo,
8195 worktree->base_commit_id);
8196 if (err)
8197 goto done;
8199 err = got_object_id_by_path(&tree_id, repo, commit,
8200 worktree->path_prefix);
8201 if (err)
8202 goto done;
8204 err = delete_merge_refs(worktree, repo);
8205 if (err)
8206 goto done;
8208 err = get_fileindex_path(&fileindex_path, worktree);
8209 if (err)
8210 goto done;
8212 rfa.worktree = worktree;
8213 rfa.fileindex = fileindex;
8214 rfa.progress_cb = progress_cb;
8215 rfa.progress_arg = progress_arg;
8216 rfa.patch_cb = NULL;
8217 rfa.patch_arg = NULL;
8218 rfa.repo = repo;
8219 rfa.unlink_added_files = 1;
8220 err = worktree_status(worktree, "", fileindex, repo,
8221 revert_file, &rfa, NULL, NULL, 1, 0);
8222 if (err)
8223 goto sync;
8225 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8226 repo, progress_cb, progress_arg, NULL, NULL);
8227 sync:
8228 sync_err = sync_fileindex(fileindex, fileindex_path);
8229 if (sync_err && err == NULL)
8230 err = sync_err;
8231 done:
8232 free(tree_id);
8233 free(commit_id);
8234 if (commit)
8235 got_object_commit_close(commit);
8236 if (fileindex)
8237 got_fileindex_free(fileindex);
8238 free(fileindex_path);
8240 unlockerr = lock_worktree(worktree, LOCK_SH);
8241 if (unlockerr && err == NULL)
8242 err = unlockerr;
8243 return err;
8246 struct check_stage_ok_arg {
8247 struct got_object_id *head_commit_id;
8248 struct got_worktree *worktree;
8249 struct got_fileindex *fileindex;
8250 struct got_repository *repo;
8251 int have_changes;
8254 static const struct got_error *
8255 check_stage_ok(void *arg, unsigned char status,
8256 unsigned char staged_status, const char *relpath,
8257 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8258 struct got_object_id *commit_id, int dirfd, const char *de_name)
8260 struct check_stage_ok_arg *a = arg;
8261 const struct got_error *err = NULL;
8262 struct got_fileindex_entry *ie;
8263 struct got_object_id base_commit_id;
8264 struct got_object_id *base_commit_idp = NULL;
8265 char *in_repo_path = NULL, *p;
8267 if (status == GOT_STATUS_UNVERSIONED ||
8268 status == GOT_STATUS_NO_CHANGE)
8269 return NULL;
8270 if (status == GOT_STATUS_NONEXISTENT)
8271 return got_error_set_errno(ENOENT, relpath);
8273 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8274 if (ie == NULL)
8275 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8277 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8278 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8279 relpath) == -1)
8280 return got_error_from_errno("asprintf");
8282 if (got_fileindex_entry_has_commit(ie)) {
8283 memcpy(base_commit_id.sha1, ie->commit_sha1,
8284 SHA1_DIGEST_LENGTH);
8285 base_commit_idp = &base_commit_id;
8288 if (status == GOT_STATUS_CONFLICT) {
8289 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8290 goto done;
8291 } else if (status != GOT_STATUS_ADD &&
8292 status != GOT_STATUS_MODIFY &&
8293 status != GOT_STATUS_DELETE) {
8294 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8295 goto done;
8298 a->have_changes = 1;
8300 p = in_repo_path;
8301 while (p[0] == '/')
8302 p++;
8303 err = check_out_of_date(p, status, staged_status,
8304 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8305 GOT_ERR_STAGE_OUT_OF_DATE);
8306 done:
8307 free(in_repo_path);
8308 return err;
8311 struct stage_path_arg {
8312 struct got_worktree *worktree;
8313 struct got_fileindex *fileindex;
8314 struct got_repository *repo;
8315 got_worktree_status_cb status_cb;
8316 void *status_arg;
8317 got_worktree_patch_cb patch_cb;
8318 void *patch_arg;
8319 int staged_something;
8320 int allow_bad_symlinks;
8323 static const struct got_error *
8324 stage_path(void *arg, unsigned char status,
8325 unsigned char staged_status, const char *relpath,
8326 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8327 struct got_object_id *commit_id, int dirfd, const char *de_name)
8329 struct stage_path_arg *a = arg;
8330 const struct got_error *err = NULL;
8331 struct got_fileindex_entry *ie;
8332 char *ondisk_path = NULL, *path_content = NULL;
8333 uint32_t stage;
8334 struct got_object_id *new_staged_blob_id = NULL;
8335 struct stat sb;
8337 if (status == GOT_STATUS_UNVERSIONED)
8338 return NULL;
8340 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8341 if (ie == NULL)
8342 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8344 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8345 relpath)== -1)
8346 return got_error_from_errno("asprintf");
8348 switch (status) {
8349 case GOT_STATUS_ADD:
8350 case GOT_STATUS_MODIFY:
8351 /* XXX could sb.st_mode be passed in by our caller? */
8352 if (lstat(ondisk_path, &sb) == -1) {
8353 err = got_error_from_errno2("lstat", ondisk_path);
8354 break;
8356 if (a->patch_cb) {
8357 if (status == GOT_STATUS_ADD) {
8358 int choice = GOT_PATCH_CHOICE_NONE;
8359 err = (*a->patch_cb)(&choice, a->patch_arg,
8360 status, ie->path, NULL, 1, 1);
8361 if (err)
8362 break;
8363 if (choice != GOT_PATCH_CHOICE_YES)
8364 break;
8365 } else {
8366 err = create_patched_content(&path_content, 0,
8367 staged_blob_id ? staged_blob_id : blob_id,
8368 ondisk_path, dirfd, de_name, ie->path,
8369 a->repo, a->patch_cb, a->patch_arg);
8370 if (err || path_content == NULL)
8371 break;
8374 err = got_object_blob_create(&new_staged_blob_id,
8375 path_content ? path_content : ondisk_path, a->repo);
8376 if (err)
8377 break;
8378 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8379 SHA1_DIGEST_LENGTH);
8380 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8381 stage = GOT_FILEIDX_STAGE_ADD;
8382 else
8383 stage = GOT_FILEIDX_STAGE_MODIFY;
8384 got_fileindex_entry_stage_set(ie, stage);
8385 if (S_ISLNK(sb.st_mode)) {
8386 int is_bad_symlink = 0;
8387 if (!a->allow_bad_symlinks) {
8388 char target_path[PATH_MAX];
8389 ssize_t target_len;
8390 target_len = readlink(ondisk_path, target_path,
8391 sizeof(target_path));
8392 if (target_len == -1) {
8393 err = got_error_from_errno2("readlink",
8394 ondisk_path);
8395 break;
8397 err = is_bad_symlink_target(&is_bad_symlink,
8398 target_path, target_len, ondisk_path,
8399 a->worktree->root_path);
8400 if (err)
8401 break;
8402 if (is_bad_symlink) {
8403 err = got_error_path(ondisk_path,
8404 GOT_ERR_BAD_SYMLINK);
8405 break;
8408 if (is_bad_symlink)
8409 got_fileindex_entry_staged_filetype_set(ie,
8410 GOT_FILEIDX_MODE_BAD_SYMLINK);
8411 else
8412 got_fileindex_entry_staged_filetype_set(ie,
8413 GOT_FILEIDX_MODE_SYMLINK);
8414 } else {
8415 got_fileindex_entry_staged_filetype_set(ie,
8416 GOT_FILEIDX_MODE_REGULAR_FILE);
8418 a->staged_something = 1;
8419 if (a->status_cb == NULL)
8420 break;
8421 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8422 get_staged_status(ie), relpath, blob_id,
8423 new_staged_blob_id, NULL, dirfd, de_name);
8424 if (err)
8425 break;
8427 * When staging the reverse of the staged diff,
8428 * implicitly unstage the file.
8430 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8431 sizeof(ie->blob_sha1)) == 0) {
8432 got_fileindex_entry_stage_set(ie,
8433 GOT_FILEIDX_STAGE_NONE);
8435 break;
8436 case GOT_STATUS_DELETE:
8437 if (staged_status == GOT_STATUS_DELETE)
8438 break;
8439 if (a->patch_cb) {
8440 int choice = GOT_PATCH_CHOICE_NONE;
8441 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8442 ie->path, NULL, 1, 1);
8443 if (err)
8444 break;
8445 if (choice == GOT_PATCH_CHOICE_NO)
8446 break;
8447 if (choice != GOT_PATCH_CHOICE_YES) {
8448 err = got_error(GOT_ERR_PATCH_CHOICE);
8449 break;
8452 stage = GOT_FILEIDX_STAGE_DELETE;
8453 got_fileindex_entry_stage_set(ie, stage);
8454 a->staged_something = 1;
8455 if (a->status_cb == NULL)
8456 break;
8457 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8458 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8459 de_name);
8460 break;
8461 case GOT_STATUS_NO_CHANGE:
8462 break;
8463 case GOT_STATUS_CONFLICT:
8464 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8465 break;
8466 case GOT_STATUS_NONEXISTENT:
8467 err = got_error_set_errno(ENOENT, relpath);
8468 break;
8469 default:
8470 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8471 break;
8474 if (path_content && unlink(path_content) == -1 && err == NULL)
8475 err = got_error_from_errno2("unlink", path_content);
8476 free(path_content);
8477 free(ondisk_path);
8478 free(new_staged_blob_id);
8479 return err;
8482 const struct got_error *
8483 got_worktree_stage(struct got_worktree *worktree,
8484 struct got_pathlist_head *paths,
8485 got_worktree_status_cb status_cb, void *status_arg,
8486 got_worktree_patch_cb patch_cb, void *patch_arg,
8487 int allow_bad_symlinks, struct got_repository *repo)
8489 const struct got_error *err = NULL, *sync_err, *unlockerr;
8490 struct got_pathlist_entry *pe;
8491 struct got_fileindex *fileindex = NULL;
8492 char *fileindex_path = NULL;
8493 struct got_reference *head_ref = NULL;
8494 struct got_object_id *head_commit_id = NULL;
8495 struct check_stage_ok_arg oka;
8496 struct stage_path_arg spa;
8498 err = lock_worktree(worktree, LOCK_EX);
8499 if (err)
8500 return err;
8502 err = got_ref_open(&head_ref, repo,
8503 got_worktree_get_head_ref_name(worktree), 0);
8504 if (err)
8505 goto done;
8506 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8507 if (err)
8508 goto done;
8509 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8510 if (err)
8511 goto done;
8513 /* Check pre-conditions before staging anything. */
8514 oka.head_commit_id = head_commit_id;
8515 oka.worktree = worktree;
8516 oka.fileindex = fileindex;
8517 oka.repo = repo;
8518 oka.have_changes = 0;
8519 TAILQ_FOREACH(pe, paths, entry) {
8520 err = worktree_status(worktree, pe->path, fileindex, repo,
8521 check_stage_ok, &oka, NULL, NULL, 1, 0);
8522 if (err)
8523 goto done;
8525 if (!oka.have_changes) {
8526 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8527 goto done;
8530 spa.worktree = worktree;
8531 spa.fileindex = fileindex;
8532 spa.repo = repo;
8533 spa.patch_cb = patch_cb;
8534 spa.patch_arg = patch_arg;
8535 spa.status_cb = status_cb;
8536 spa.status_arg = status_arg;
8537 spa.staged_something = 0;
8538 spa.allow_bad_symlinks = allow_bad_symlinks;
8539 TAILQ_FOREACH(pe, paths, entry) {
8540 err = worktree_status(worktree, pe->path, fileindex, repo,
8541 stage_path, &spa, NULL, NULL, 1, 0);
8542 if (err)
8543 goto done;
8545 if (!spa.staged_something) {
8546 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8547 goto done;
8550 sync_err = sync_fileindex(fileindex, fileindex_path);
8551 if (sync_err && err == NULL)
8552 err = sync_err;
8553 done:
8554 if (head_ref)
8555 got_ref_close(head_ref);
8556 free(head_commit_id);
8557 free(fileindex_path);
8558 if (fileindex)
8559 got_fileindex_free(fileindex);
8560 unlockerr = lock_worktree(worktree, LOCK_SH);
8561 if (unlockerr && err == NULL)
8562 err = unlockerr;
8563 return err;
8566 struct unstage_path_arg {
8567 struct got_worktree *worktree;
8568 struct got_fileindex *fileindex;
8569 struct got_repository *repo;
8570 got_worktree_checkout_cb progress_cb;
8571 void *progress_arg;
8572 got_worktree_patch_cb patch_cb;
8573 void *patch_arg;
8576 static const struct got_error *
8577 create_unstaged_content(char **path_unstaged_content,
8578 char **path_new_staged_content, struct got_object_id *blob_id,
8579 struct got_object_id *staged_blob_id, const char *relpath,
8580 struct got_repository *repo,
8581 got_worktree_patch_cb patch_cb, void *patch_arg)
8583 const struct got_error *err, *free_err;
8584 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8585 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8586 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8587 struct got_diffreg_result *diffreg_result = NULL;
8588 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8589 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8590 int fd1 = -1, fd2 = -1;
8592 *path_unstaged_content = NULL;
8593 *path_new_staged_content = NULL;
8595 err = got_object_id_str(&label1, blob_id);
8596 if (err)
8597 return err;
8599 fd1 = got_opentempfd();
8600 if (fd1 == -1) {
8601 err = got_error_from_errno("got_opentempfd");
8602 goto done;
8604 fd2 = got_opentempfd();
8605 if (fd2 == -1) {
8606 err = got_error_from_errno("got_opentempfd");
8607 goto done;
8610 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8611 if (err)
8612 goto done;
8614 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8615 if (err)
8616 goto done;
8618 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8619 if (err)
8620 goto done;
8622 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8623 fd2);
8624 if (err)
8625 goto done;
8627 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8628 if (err)
8629 goto done;
8631 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8632 if (err)
8633 goto done;
8635 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8636 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8637 if (err)
8638 goto done;
8640 err = got_opentemp_named(path_unstaged_content, &outfile,
8641 "got-unstaged-content");
8642 if (err)
8643 goto done;
8644 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8645 "got-new-staged-content");
8646 if (err)
8647 goto done;
8649 if (fseek(f1, 0L, SEEK_SET) == -1) {
8650 err = got_ferror(f1, GOT_ERR_IO);
8651 goto done;
8653 if (fseek(f2, 0L, SEEK_SET) == -1) {
8654 err = got_ferror(f2, GOT_ERR_IO);
8655 goto done;
8657 /* Count the number of actual changes in the diff result. */
8658 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8659 struct diff_chunk_context cc = {};
8660 diff_chunk_context_load_change(&cc, &nchunks_used,
8661 diffreg_result->result, n, 0);
8662 nchanges++;
8664 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8665 int choice;
8666 err = apply_or_reject_change(&choice, &nchunks_used,
8667 diffreg_result->result, n, relpath, f1, f2,
8668 &line_cur1, &line_cur2,
8669 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8670 if (err)
8671 goto done;
8672 if (choice == GOT_PATCH_CHOICE_YES)
8673 have_content = 1;
8674 else
8675 have_rejected_content = 1;
8676 if (choice == GOT_PATCH_CHOICE_QUIT)
8677 break;
8679 if (have_content || have_rejected_content)
8680 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8681 outfile, rejectfile);
8682 done:
8683 free(label1);
8684 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8685 err = got_error_from_errno("close");
8686 if (blob)
8687 got_object_blob_close(blob);
8688 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8689 err = got_error_from_errno("close");
8690 if (staged_blob)
8691 got_object_blob_close(staged_blob);
8692 free_err = got_diffreg_result_free(diffreg_result);
8693 if (free_err && err == NULL)
8694 err = free_err;
8695 if (f1 && fclose(f1) == EOF && err == NULL)
8696 err = got_error_from_errno2("fclose", path1);
8697 if (f2 && fclose(f2) == EOF && err == NULL)
8698 err = got_error_from_errno2("fclose", path2);
8699 if (outfile && fclose(outfile) == EOF && err == NULL)
8700 err = got_error_from_errno2("fclose", *path_unstaged_content);
8701 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8702 err = got_error_from_errno2("fclose", *path_new_staged_content);
8703 if (path1 && unlink(path1) == -1 && err == NULL)
8704 err = got_error_from_errno2("unlink", path1);
8705 if (path2 && unlink(path2) == -1 && err == NULL)
8706 err = got_error_from_errno2("unlink", path2);
8707 if (err || !have_content) {
8708 if (*path_unstaged_content &&
8709 unlink(*path_unstaged_content) == -1 && err == NULL)
8710 err = got_error_from_errno2("unlink",
8711 *path_unstaged_content);
8712 free(*path_unstaged_content);
8713 *path_unstaged_content = NULL;
8715 if (err || !have_content || !have_rejected_content) {
8716 if (*path_new_staged_content &&
8717 unlink(*path_new_staged_content) == -1 && err == NULL)
8718 err = got_error_from_errno2("unlink",
8719 *path_new_staged_content);
8720 free(*path_new_staged_content);
8721 *path_new_staged_content = NULL;
8723 free(path1);
8724 free(path2);
8725 return err;
8728 static const struct got_error *
8729 unstage_hunks(struct got_object_id *staged_blob_id,
8730 struct got_blob_object *blob_base,
8731 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8732 const char *ondisk_path, const char *label_orig,
8733 struct got_worktree *worktree, struct got_repository *repo,
8734 got_worktree_patch_cb patch_cb, void *patch_arg,
8735 got_worktree_checkout_cb progress_cb, void *progress_arg)
8737 const struct got_error *err = NULL;
8738 char *path_unstaged_content = NULL;
8739 char *path_new_staged_content = NULL;
8740 char *parent = NULL, *base_path = NULL;
8741 char *blob_base_path = NULL;
8742 struct got_object_id *new_staged_blob_id = NULL;
8743 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8744 struct stat sb;
8746 err = create_unstaged_content(&path_unstaged_content,
8747 &path_new_staged_content, blob_id, staged_blob_id,
8748 ie->path, repo, patch_cb, patch_arg);
8749 if (err)
8750 return err;
8752 if (path_unstaged_content == NULL)
8753 return NULL;
8755 if (path_new_staged_content) {
8756 err = got_object_blob_create(&new_staged_blob_id,
8757 path_new_staged_content, repo);
8758 if (err)
8759 goto done;
8762 f = fopen(path_unstaged_content, "re");
8763 if (f == NULL) {
8764 err = got_error_from_errno2("fopen",
8765 path_unstaged_content);
8766 goto done;
8768 if (fstat(fileno(f), &sb) == -1) {
8769 err = got_error_from_errno2("fstat", path_unstaged_content);
8770 goto done;
8772 if (got_fileindex_entry_staged_filetype_get(ie) ==
8773 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8774 char link_target[PATH_MAX];
8775 size_t r;
8776 r = fread(link_target, 1, sizeof(link_target), f);
8777 if (r == 0 && ferror(f)) {
8778 err = got_error_from_errno("fread");
8779 goto done;
8781 if (r >= sizeof(link_target)) { /* should not happen */
8782 err = got_error(GOT_ERR_NO_SPACE);
8783 goto done;
8785 link_target[r] = '\0';
8786 err = merge_symlink(worktree, blob_base,
8787 ondisk_path, ie->path, label_orig, link_target,
8788 worktree->base_commit_id, repo, progress_cb,
8789 progress_arg);
8790 } else {
8791 int local_changes_subsumed;
8793 err = got_path_dirname(&parent, ondisk_path);
8794 if (err)
8795 return err;
8797 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8798 parent) == -1) {
8799 err = got_error_from_errno("asprintf");
8800 base_path = NULL;
8801 goto done;
8804 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8805 if (err)
8806 goto done;
8807 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8808 blob_base);
8809 if (err)
8810 goto done;
8813 * In order the run a 3-way merge with a symlink we copy the symlink's
8814 * target path into a temporary file and use that file with diff3.
8816 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8817 err = dump_symlink_target_path_to_file(&f_deriv2,
8818 ondisk_path);
8819 if (err)
8820 goto done;
8821 } else {
8822 int fd;
8823 fd = open(ondisk_path,
8824 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8825 if (fd == -1) {
8826 err = got_error_from_errno2("open", ondisk_path);
8827 goto done;
8829 f_deriv2 = fdopen(fd, "r");
8830 if (f_deriv2 == NULL) {
8831 err = got_error_from_errno2("fdopen", ondisk_path);
8832 close(fd);
8833 goto done;
8837 err = merge_file(&local_changes_subsumed, worktree,
8838 f_base, f, f_deriv2, ondisk_path, ie->path,
8839 got_fileindex_perms_to_st(ie),
8840 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8841 repo, progress_cb, progress_arg);
8843 if (err)
8844 goto done;
8846 if (new_staged_blob_id) {
8847 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8848 SHA1_DIGEST_LENGTH);
8849 } else {
8850 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8851 got_fileindex_entry_staged_filetype_set(ie, 0);
8853 done:
8854 free(new_staged_blob_id);
8855 if (path_unstaged_content &&
8856 unlink(path_unstaged_content) == -1 && err == NULL)
8857 err = got_error_from_errno2("unlink", path_unstaged_content);
8858 if (path_new_staged_content &&
8859 unlink(path_new_staged_content) == -1 && err == NULL)
8860 err = got_error_from_errno2("unlink", path_new_staged_content);
8861 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8862 err = got_error_from_errno2("unlink", blob_base_path);
8863 if (f_base && fclose(f_base) == EOF && err == NULL)
8864 err = got_error_from_errno2("fclose", path_unstaged_content);
8865 if (f && fclose(f) == EOF && err == NULL)
8866 err = got_error_from_errno2("fclose", path_unstaged_content);
8867 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8868 err = got_error_from_errno2("fclose", ondisk_path);
8869 free(path_unstaged_content);
8870 free(path_new_staged_content);
8871 free(blob_base_path);
8872 free(parent);
8873 free(base_path);
8874 return err;
8877 static const struct got_error *
8878 unstage_path(void *arg, unsigned char status,
8879 unsigned char staged_status, const char *relpath,
8880 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8881 struct got_object_id *commit_id, int dirfd, const char *de_name)
8883 const struct got_error *err = NULL;
8884 struct unstage_path_arg *a = arg;
8885 struct got_fileindex_entry *ie;
8886 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8887 char *ondisk_path = NULL;
8888 char *id_str = NULL, *label_orig = NULL;
8889 int local_changes_subsumed;
8890 struct stat sb;
8891 int fd1 = -1, fd2 = -1;
8893 if (staged_status != GOT_STATUS_ADD &&
8894 staged_status != GOT_STATUS_MODIFY &&
8895 staged_status != GOT_STATUS_DELETE)
8896 return NULL;
8898 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8899 if (ie == NULL)
8900 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8902 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8903 == -1)
8904 return got_error_from_errno("asprintf");
8906 err = got_object_id_str(&id_str,
8907 commit_id ? commit_id : a->worktree->base_commit_id);
8908 if (err)
8909 goto done;
8910 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8911 id_str) == -1) {
8912 err = got_error_from_errno("asprintf");
8913 goto done;
8916 fd1 = got_opentempfd();
8917 if (fd1 == -1) {
8918 err = got_error_from_errno("got_opentempfd");
8919 goto done;
8921 fd2 = got_opentempfd();
8922 if (fd2 == -1) {
8923 err = got_error_from_errno("got_opentempfd");
8924 goto done;
8927 switch (staged_status) {
8928 case GOT_STATUS_MODIFY:
8929 err = got_object_open_as_blob(&blob_base, a->repo,
8930 blob_id, 8192, fd1);
8931 if (err)
8932 break;
8933 /* fall through */
8934 case GOT_STATUS_ADD:
8935 if (a->patch_cb) {
8936 if (staged_status == GOT_STATUS_ADD) {
8937 int choice = GOT_PATCH_CHOICE_NONE;
8938 err = (*a->patch_cb)(&choice, a->patch_arg,
8939 staged_status, ie->path, NULL, 1, 1);
8940 if (err)
8941 break;
8942 if (choice != GOT_PATCH_CHOICE_YES)
8943 break;
8944 } else {
8945 err = unstage_hunks(staged_blob_id,
8946 blob_base, blob_id, ie, ondisk_path,
8947 label_orig, a->worktree, a->repo,
8948 a->patch_cb, a->patch_arg,
8949 a->progress_cb, a->progress_arg);
8950 break; /* Done with this file. */
8953 err = got_object_open_as_blob(&blob_staged, a->repo,
8954 staged_blob_id, 8192, fd2);
8955 if (err)
8956 break;
8957 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8958 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8959 case GOT_FILEIDX_MODE_REGULAR_FILE:
8960 err = merge_blob(&local_changes_subsumed, a->worktree,
8961 blob_base, ondisk_path, relpath,
8962 got_fileindex_perms_to_st(ie), label_orig,
8963 blob_staged, commit_id ? commit_id :
8964 a->worktree->base_commit_id, a->repo,
8965 a->progress_cb, a->progress_arg);
8966 break;
8967 case GOT_FILEIDX_MODE_SYMLINK:
8968 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8969 char *staged_target;
8970 err = got_object_blob_read_to_str(
8971 &staged_target, blob_staged);
8972 if (err)
8973 goto done;
8974 err = merge_symlink(a->worktree, blob_base,
8975 ondisk_path, relpath, label_orig,
8976 staged_target, commit_id ? commit_id :
8977 a->worktree->base_commit_id,
8978 a->repo, a->progress_cb, a->progress_arg);
8979 free(staged_target);
8980 } else {
8981 err = merge_blob(&local_changes_subsumed,
8982 a->worktree, blob_base, ondisk_path,
8983 relpath, got_fileindex_perms_to_st(ie),
8984 label_orig, blob_staged,
8985 commit_id ? commit_id :
8986 a->worktree->base_commit_id, a->repo,
8987 a->progress_cb, a->progress_arg);
8989 break;
8990 default:
8991 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8992 break;
8994 if (err == NULL) {
8995 got_fileindex_entry_stage_set(ie,
8996 GOT_FILEIDX_STAGE_NONE);
8997 got_fileindex_entry_staged_filetype_set(ie, 0);
8999 break;
9000 case GOT_STATUS_DELETE:
9001 if (a->patch_cb) {
9002 int choice = GOT_PATCH_CHOICE_NONE;
9003 err = (*a->patch_cb)(&choice, a->patch_arg,
9004 staged_status, ie->path, NULL, 1, 1);
9005 if (err)
9006 break;
9007 if (choice == GOT_PATCH_CHOICE_NO)
9008 break;
9009 if (choice != GOT_PATCH_CHOICE_YES) {
9010 err = got_error(GOT_ERR_PATCH_CHOICE);
9011 break;
9014 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9015 got_fileindex_entry_staged_filetype_set(ie, 0);
9016 err = get_file_status(&status, &sb, ie, ondisk_path,
9017 dirfd, de_name, a->repo);
9018 if (err)
9019 break;
9020 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9021 break;
9023 done:
9024 free(ondisk_path);
9025 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9026 err = got_error_from_errno("close");
9027 if (blob_base)
9028 got_object_blob_close(blob_base);
9029 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9030 err = got_error_from_errno("close");
9031 if (blob_staged)
9032 got_object_blob_close(blob_staged);
9033 free(id_str);
9034 free(label_orig);
9035 return err;
9038 const struct got_error *
9039 got_worktree_unstage(struct got_worktree *worktree,
9040 struct got_pathlist_head *paths,
9041 got_worktree_checkout_cb progress_cb, void *progress_arg,
9042 got_worktree_patch_cb patch_cb, void *patch_arg,
9043 struct got_repository *repo)
9045 const struct got_error *err = NULL, *sync_err, *unlockerr;
9046 struct got_pathlist_entry *pe;
9047 struct got_fileindex *fileindex = NULL;
9048 char *fileindex_path = NULL;
9049 struct unstage_path_arg upa;
9051 err = lock_worktree(worktree, LOCK_EX);
9052 if (err)
9053 return err;
9055 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9056 if (err)
9057 goto done;
9059 upa.worktree = worktree;
9060 upa.fileindex = fileindex;
9061 upa.repo = repo;
9062 upa.progress_cb = progress_cb;
9063 upa.progress_arg = progress_arg;
9064 upa.patch_cb = patch_cb;
9065 upa.patch_arg = patch_arg;
9066 TAILQ_FOREACH(pe, paths, entry) {
9067 err = worktree_status(worktree, pe->path, fileindex, repo,
9068 unstage_path, &upa, NULL, NULL, 1, 0);
9069 if (err)
9070 goto done;
9073 sync_err = sync_fileindex(fileindex, fileindex_path);
9074 if (sync_err && err == NULL)
9075 err = sync_err;
9076 done:
9077 free(fileindex_path);
9078 if (fileindex)
9079 got_fileindex_free(fileindex);
9080 unlockerr = lock_worktree(worktree, LOCK_SH);
9081 if (unlockerr && err == NULL)
9082 err = unlockerr;
9083 return err;
9086 struct report_file_info_arg {
9087 struct got_worktree *worktree;
9088 got_worktree_path_info_cb info_cb;
9089 void *info_arg;
9090 struct got_pathlist_head *paths;
9091 got_cancel_cb cancel_cb;
9092 void *cancel_arg;
9095 static const struct got_error *
9096 report_file_info(void *arg, struct got_fileindex_entry *ie)
9098 struct report_file_info_arg *a = arg;
9099 struct got_pathlist_entry *pe;
9100 struct got_object_id blob_id, staged_blob_id, commit_id;
9101 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9102 struct got_object_id *commit_idp = NULL;
9103 int stage;
9105 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9106 return got_error(GOT_ERR_CANCELLED);
9108 TAILQ_FOREACH(pe, a->paths, entry) {
9109 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9110 got_path_is_child(ie->path, pe->path, pe->path_len))
9111 break;
9113 if (pe == NULL) /* not found */
9114 return NULL;
9116 if (got_fileindex_entry_has_blob(ie)) {
9117 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
9118 blob_idp = &blob_id;
9120 stage = got_fileindex_entry_stage_get(ie);
9121 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9122 stage == GOT_FILEIDX_STAGE_ADD) {
9123 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
9124 SHA1_DIGEST_LENGTH);
9125 staged_blob_idp = &staged_blob_id;
9128 if (got_fileindex_entry_has_commit(ie)) {
9129 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
9130 commit_idp = &commit_id;
9133 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9134 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9137 const struct got_error *
9138 got_worktree_path_info(struct got_worktree *worktree,
9139 struct got_pathlist_head *paths,
9140 got_worktree_path_info_cb info_cb, void *info_arg,
9141 got_cancel_cb cancel_cb, void *cancel_arg)
9144 const struct got_error *err = NULL, *unlockerr;
9145 struct got_fileindex *fileindex = NULL;
9146 char *fileindex_path = NULL;
9147 struct report_file_info_arg arg;
9149 err = lock_worktree(worktree, LOCK_SH);
9150 if (err)
9151 return err;
9153 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9154 if (err)
9155 goto done;
9157 arg.worktree = worktree;
9158 arg.info_cb = info_cb;
9159 arg.info_arg = info_arg;
9160 arg.paths = paths;
9161 arg.cancel_cb = cancel_cb;
9162 arg.cancel_arg = cancel_arg;
9163 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9164 &arg);
9165 done:
9166 free(fileindex_path);
9167 if (fileindex)
9168 got_fileindex_free(fileindex);
9169 unlockerr = lock_worktree(worktree, LOCK_UN);
9170 if (unlockerr && err == NULL)
9171 err = unlockerr;
9172 return err;
9175 static const struct got_error *
9176 patch_check_path(const char *p, char **path, unsigned char *status,
9177 unsigned char *staged_status, struct got_fileindex *fileindex,
9178 struct got_worktree *worktree, struct got_repository *repo)
9180 const struct got_error *err;
9181 struct got_fileindex_entry *ie;
9182 struct stat sb;
9183 char *ondisk_path = NULL;
9185 err = got_worktree_resolve_path(path, worktree, p);
9186 if (err)
9187 return err;
9189 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9190 *path[0] ? "/" : "", *path) == -1)
9191 return got_error_from_errno("asprintf");
9193 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9194 if (ie) {
9195 *staged_status = get_staged_status(ie);
9196 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9197 repo);
9198 if (err)
9199 goto done;
9200 } else {
9201 *staged_status = GOT_STATUS_NO_CHANGE;
9202 *status = GOT_STATUS_UNVERSIONED;
9203 if (lstat(ondisk_path, &sb) == -1) {
9204 if (errno != ENOENT) {
9205 err = got_error_from_errno2("lstat",
9206 ondisk_path);
9207 goto done;
9209 *status = GOT_STATUS_NONEXISTENT;
9213 done:
9214 free(ondisk_path);
9215 return err;
9218 static const struct got_error *
9219 patch_can_rm(const char *path, unsigned char status,
9220 unsigned char staged_status)
9222 if (status == GOT_STATUS_NONEXISTENT)
9223 return got_error_set_errno(ENOENT, path);
9224 if (status != GOT_STATUS_NO_CHANGE &&
9225 status != GOT_STATUS_ADD &&
9226 status != GOT_STATUS_MODIFY &&
9227 status != GOT_STATUS_MODE_CHANGE)
9228 return got_error_path(path, GOT_ERR_FILE_STATUS);
9229 if (staged_status == GOT_STATUS_DELETE)
9230 return got_error_path(path, GOT_ERR_FILE_STATUS);
9231 return NULL;
9234 static const struct got_error *
9235 patch_can_add(const char *path, unsigned char status)
9237 if (status != GOT_STATUS_NONEXISTENT)
9238 return got_error_path(path, GOT_ERR_FILE_STATUS);
9239 return NULL;
9242 static const struct got_error *
9243 patch_can_edit(const char *path, unsigned char status,
9244 unsigned char staged_status)
9246 if (status == GOT_STATUS_NONEXISTENT)
9247 return got_error_set_errno(ENOENT, path);
9248 if (status != GOT_STATUS_NO_CHANGE &&
9249 status != GOT_STATUS_ADD &&
9250 status != GOT_STATUS_MODIFY)
9251 return got_error_path(path, GOT_ERR_FILE_STATUS);
9252 if (staged_status == GOT_STATUS_DELETE)
9253 return got_error_path(path, GOT_ERR_FILE_STATUS);
9254 return NULL;
9257 const struct got_error *
9258 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9259 char **fileindex_path, struct got_worktree *worktree)
9261 return open_fileindex(fileindex, fileindex_path, worktree);
9264 const struct got_error *
9265 got_worktree_patch_check_path(const char *old, const char *new,
9266 char **oldpath, char **newpath, struct got_worktree *worktree,
9267 struct got_repository *repo, struct got_fileindex *fileindex)
9269 const struct got_error *err = NULL;
9270 int file_renamed = 0;
9271 unsigned char status_old, staged_status_old;
9272 unsigned char status_new, staged_status_new;
9274 *oldpath = NULL;
9275 *newpath = NULL;
9277 err = patch_check_path(old != NULL ? old : new, oldpath,
9278 &status_old, &staged_status_old, fileindex, worktree, repo);
9279 if (err)
9280 goto done;
9282 err = patch_check_path(new != NULL ? new : old, newpath,
9283 &status_new, &staged_status_new, fileindex, worktree, repo);
9284 if (err)
9285 goto done;
9287 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9288 file_renamed = 1;
9290 if (old != NULL && new == NULL)
9291 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9292 else if (file_renamed) {
9293 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9294 if (err == NULL)
9295 err = patch_can_add(*newpath, status_new);
9296 } else if (old == NULL)
9297 err = patch_can_add(*newpath, status_new);
9298 else
9299 err = patch_can_edit(*newpath, status_new, staged_status_new);
9301 done:
9302 if (err) {
9303 free(*oldpath);
9304 *oldpath = NULL;
9305 free(*newpath);
9306 *newpath = NULL;
9308 return err;
9311 const struct got_error *
9312 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9313 struct got_worktree *worktree, struct got_fileindex *fileindex,
9314 got_worktree_checkout_cb progress_cb, void *progress_arg)
9316 struct schedule_addition_args saa;
9318 memset(&saa, 0, sizeof(saa));
9319 saa.worktree = worktree;
9320 saa.fileindex = fileindex;
9321 saa.progress_cb = progress_cb;
9322 saa.progress_arg = progress_arg;
9323 saa.repo = repo;
9325 return worktree_status(worktree, path, fileindex, repo,
9326 schedule_addition, &saa, NULL, NULL, 1, 0);
9329 const struct got_error *
9330 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9331 struct got_worktree *worktree, struct got_fileindex *fileindex,
9332 got_worktree_delete_cb progress_cb, void *progress_arg)
9334 struct schedule_deletion_args sda;
9336 memset(&sda, 0, sizeof(sda));
9337 sda.worktree = worktree;
9338 sda.fileindex = fileindex;
9339 sda.progress_cb = progress_cb;
9340 sda.progress_arg = progress_arg;
9341 sda.repo = repo;
9342 sda.delete_local_mods = 0;
9343 sda.keep_on_disk = 0;
9344 sda.ignore_missing_paths = 0;
9345 sda.status_codes = NULL;
9347 return worktree_status(worktree, path, fileindex, repo,
9348 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9351 const struct got_error *
9352 got_worktree_patch_complete(struct got_fileindex *fileindex,
9353 const char *fileindex_path)
9355 const struct got_error *err = NULL;
9357 err = sync_fileindex(fileindex, fileindex_path);
9358 got_fileindex_free(fileindex);
9360 return err;