Blob


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