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;
1654 staged_status = get_staged_status(ie);
1655 *status = GOT_STATUS_NO_CHANGE;
1656 memset(sb, 0, sizeof(*sb));
1659 * Whenever the caller provides a directory descriptor and a
1660 * directory entry name for the file, use them! This prevents
1661 * race conditions if filesystem paths change beneath our feet.
1663 if (dirfd != -1) {
1664 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1668 else
1669 *status = GOT_STATUS_DELETE;
1670 goto done;
1672 err = got_error_from_errno2("fstatat", abspath);
1673 goto done;
1675 } else {
1676 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1677 if (fd == -1 && errno != ENOENT &&
1678 !got_err_open_nofollow_on_symlink())
1679 return got_error_from_errno2("open", abspath);
1680 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1681 if (lstat(abspath, sb) == -1)
1682 return got_error_from_errno2("lstat", abspath);
1683 } else if (fd == -1 || fstat(fd, sb) == -1) {
1684 if (errno == ENOENT) {
1685 if (got_fileindex_entry_has_file_on_disk(ie))
1686 *status = GOT_STATUS_MISSING;
1687 else
1688 *status = GOT_STATUS_DELETE;
1689 goto done;
1691 err = got_error_from_errno2("fstat", abspath);
1692 goto done;
1696 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1697 *status = GOT_STATUS_OBSTRUCTED;
1698 goto done;
1701 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1702 *status = GOT_STATUS_DELETE;
1703 goto done;
1704 } else if (!got_fileindex_entry_has_blob(ie) &&
1705 staged_status != GOT_STATUS_ADD) {
1706 *status = GOT_STATUS_ADD;
1707 goto done;
1710 if (!stat_info_differs(ie, sb))
1711 goto done;
1713 if (S_ISLNK(sb->st_mode) &&
1714 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1715 *status = GOT_STATUS_MODIFY;
1716 goto done;
1719 if (staged_status == GOT_STATUS_MODIFY ||
1720 staged_status == GOT_STATUS_ADD)
1721 got_fileindex_entry_get_staged_blob_id(&id, ie);
1722 else
1723 got_fileindex_entry_get_blob_id(&id, ie);
1725 fd1 = got_opentempfd();
1726 if (fd1 == -1) {
1727 err = got_error_from_errno("got_opentempfd");
1728 goto done;
1730 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1731 if (err)
1732 goto done;
1734 if (S_ISLNK(sb->st_mode)) {
1735 err = get_symlink_modification_status(status, ie,
1736 abspath, dirfd, de_name, blob);
1737 goto done;
1740 if (dirfd != -1) {
1741 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 if (fd == -1) {
1743 err = got_error_from_errno2("openat", abspath);
1744 goto done;
1748 f = fdopen(fd, "r");
1749 if (f == NULL) {
1750 err = got_error_from_errno2("fdopen", abspath);
1751 goto done;
1753 fd = -1;
1754 hdrlen = got_object_blob_get_hdrlen(blob);
1755 for (;;) {
1756 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1757 err = got_object_blob_read_block(&blen, blob);
1758 if (err)
1759 goto done;
1760 /* Skip length of blob object header first time around. */
1761 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1762 if (flen == 0 && ferror(f)) {
1763 err = got_error_from_errno("fread");
1764 goto done;
1766 if (blen - hdrlen == 0) {
1767 if (flen != 0)
1768 *status = GOT_STATUS_MODIFY;
1769 break;
1770 } else if (flen == 0) {
1771 if (blen - hdrlen != 0)
1772 *status = GOT_STATUS_MODIFY;
1773 break;
1774 } else if (blen - hdrlen == flen) {
1775 /* Skip blob object header first time around. */
1776 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1777 *status = GOT_STATUS_MODIFY;
1778 break;
1780 } else {
1781 *status = GOT_STATUS_MODIFY;
1782 break;
1784 hdrlen = 0;
1787 if (*status == GOT_STATUS_MODIFY) {
1788 rewind(f);
1789 err = get_modified_file_content_status(status, f);
1790 } else if (xbit_differs(ie, sb->st_mode))
1791 *status = GOT_STATUS_MODE_CHANGE;
1792 done:
1793 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1794 err = got_error_from_errno("close");
1795 if (blob)
1796 got_object_blob_close(blob);
1797 if (f != NULL && fclose(f) == EOF && err == NULL)
1798 err = got_error_from_errno2("fclose", abspath);
1799 if (fd != -1 && close(fd) == -1 && err == NULL)
1800 err = got_error_from_errno2("close", abspath);
1801 return err;
1805 * Update timestamps in the file index if a file is unmodified and
1806 * we had to run a full content comparison to find out.
1808 static const struct got_error *
1809 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1810 struct got_fileindex_entry *ie, struct stat *sb)
1812 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1813 return got_fileindex_entry_update(ie, wt_fd, path,
1814 ie->blob_sha1, ie->commit_sha1, 1);
1816 return NULL;
1819 static const struct got_error *
1820 update_blob(struct got_worktree *worktree,
1821 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1822 struct got_tree_entry *te, const char *path,
1823 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1824 void *progress_arg)
1826 const struct got_error *err = NULL;
1827 struct got_blob_object *blob = NULL;
1828 char *ondisk_path = NULL;
1829 unsigned char status = GOT_STATUS_NO_CHANGE;
1830 struct stat sb;
1831 int fd1 = -1, fd2 = -1;
1833 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1834 return got_error_from_errno("asprintf");
1836 if (ie) {
1837 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1838 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1839 goto done;
1841 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1842 repo);
1843 if (err)
1844 goto done;
1845 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1846 sb.st_mode = got_fileindex_perms_to_st(ie);
1847 } else {
1848 if (stat(ondisk_path, &sb) == -1) {
1849 if (errno != ENOENT) {
1850 err = got_error_from_errno2("stat",
1851 ondisk_path);
1852 goto done;
1854 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1855 status = GOT_STATUS_UNVERSIONED;
1856 } else {
1857 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1858 status = GOT_STATUS_UNVERSIONED;
1859 else
1860 status = GOT_STATUS_OBSTRUCTED;
1864 if (status == GOT_STATUS_OBSTRUCTED) {
1865 if (ie)
1866 got_fileindex_entry_mark_skipped(ie);
1867 err = (*progress_cb)(progress_arg, status, path);
1868 goto done;
1870 if (status == GOT_STATUS_CONFLICT) {
1871 if (ie)
1872 got_fileindex_entry_mark_skipped(ie);
1873 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1874 path);
1875 goto done;
1878 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1879 (S_ISLNK(te->mode) ||
1880 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1882 * This is a regular file or an installed bad symlink.
1883 * If the file index indicates that this file is already
1884 * up-to-date with respect to the repository we can skip
1885 * updating contents of this file.
1887 if (got_fileindex_entry_has_commit(ie) &&
1888 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1889 SHA1_DIGEST_LENGTH) == 0) {
1890 /* Same commit. */
1891 err = sync_timestamps(worktree->root_fd,
1892 path, status, ie, &sb);
1893 if (err)
1894 goto done;
1895 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1896 path);
1897 goto done;
1899 if (got_fileindex_entry_has_blob(ie) &&
1900 memcmp(ie->blob_sha1, te->id.sha1,
1901 SHA1_DIGEST_LENGTH) == 0) {
1902 /* Different commit but the same blob. */
1903 err = sync_timestamps(worktree->root_fd,
1904 path, status, ie, &sb);
1905 if (err)
1906 goto done;
1907 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1908 path);
1909 goto done;
1913 fd1 = got_opentempfd();
1914 if (fd1 == -1) {
1915 err = got_error_from_errno("got_opentempfd");
1916 goto done;
1918 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1919 if (err)
1920 goto done;
1922 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1923 int update_timestamps;
1924 struct got_blob_object *blob2 = NULL;
1925 char *label_orig = NULL;
1926 if (got_fileindex_entry_has_blob(ie)) {
1927 fd2 = got_opentempfd();
1928 if (fd2 == -1) {
1929 err = got_error_from_errno("got_opentempfd");
1930 goto done;
1932 struct got_object_id id2;
1933 got_fileindex_entry_get_blob_id(&id2, ie);
1934 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1935 fd2);
1936 if (err)
1937 goto done;
1939 if (got_fileindex_entry_has_commit(ie)) {
1940 char id_str[SHA1_DIGEST_STRING_LENGTH];
1941 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1942 sizeof(id_str)) == NULL) {
1943 err = got_error_path(id_str,
1944 GOT_ERR_BAD_OBJ_ID_STR);
1945 goto done;
1947 if (asprintf(&label_orig, "%s: commit %s",
1948 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1949 err = got_error_from_errno("asprintf");
1950 goto done;
1953 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1954 char *link_target;
1955 err = got_object_blob_read_to_str(&link_target, blob);
1956 if (err)
1957 goto done;
1958 err = merge_symlink(worktree, blob2, ondisk_path, path,
1959 label_orig, link_target, worktree->base_commit_id,
1960 repo, progress_cb, progress_arg);
1961 free(link_target);
1962 } else {
1963 err = merge_blob(&update_timestamps, worktree, blob2,
1964 ondisk_path, path, sb.st_mode, label_orig, blob,
1965 worktree->base_commit_id, repo,
1966 progress_cb, progress_arg);
1968 free(label_orig);
1969 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1970 err = got_error_from_errno("close");
1971 goto done;
1973 if (blob2)
1974 got_object_blob_close(blob2);
1975 if (err)
1976 goto done;
1978 * Do not update timestamps of files with local changes.
1979 * Otherwise, a future status walk would treat them as
1980 * unmodified files again.
1982 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1983 blob->id.sha1, worktree->base_commit_id->sha1,
1984 update_timestamps);
1985 } else if (status == GOT_STATUS_MODE_CHANGE) {
1986 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1987 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1988 } else if (status == GOT_STATUS_DELETE) {
1989 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1990 if (err)
1991 goto done;
1992 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1993 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1994 if (err)
1995 goto done;
1996 } else {
1997 int is_bad_symlink = 0;
1998 if (S_ISLNK(te->mode)) {
1999 err = install_symlink(&is_bad_symlink, worktree,
2000 ondisk_path, path, blob,
2001 status == GOT_STATUS_MISSING, 0,
2002 status == GOT_STATUS_UNVERSIONED, 0,
2003 repo, progress_cb, progress_arg);
2004 } else {
2005 err = install_blob(worktree, ondisk_path, path,
2006 te->mode, sb.st_mode, blob,
2007 status == GOT_STATUS_MISSING, 0, 0,
2008 status == GOT_STATUS_UNVERSIONED, repo,
2009 progress_cb, progress_arg);
2011 if (err)
2012 goto done;
2014 if (ie) {
2015 err = got_fileindex_entry_update(ie,
2016 worktree->root_fd, path, blob->id.sha1,
2017 worktree->base_commit_id->sha1, 1);
2018 } else {
2019 err = create_fileindex_entry(&ie, fileindex,
2020 worktree->base_commit_id, worktree->root_fd, path,
2021 &blob->id);
2023 if (err)
2024 goto done;
2026 if (is_bad_symlink) {
2027 got_fileindex_entry_filetype_set(ie,
2028 GOT_FILEIDX_MODE_BAD_SYMLINK);
2032 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2033 err = got_error_from_errno("close");
2034 goto done;
2036 got_object_blob_close(blob);
2037 done:
2038 free(ondisk_path);
2039 return err;
2042 static const struct got_error *
2043 remove_ondisk_file(const char *root_path, const char *path)
2045 const struct got_error *err = NULL;
2046 char *ondisk_path = NULL, *parent = NULL;
2048 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2049 return got_error_from_errno("asprintf");
2051 if (unlink(ondisk_path) == -1) {
2052 if (errno != ENOENT)
2053 err = got_error_from_errno2("unlink", ondisk_path);
2054 } else {
2055 size_t root_len = strlen(root_path);
2056 err = got_path_dirname(&parent, ondisk_path);
2057 if (err)
2058 goto done;
2059 while (got_path_cmp(parent, root_path,
2060 strlen(parent), root_len) != 0) {
2061 free(ondisk_path);
2062 ondisk_path = parent;
2063 parent = NULL;
2064 if (rmdir(ondisk_path) == -1) {
2065 if (errno != ENOTEMPTY)
2066 err = got_error_from_errno2("rmdir",
2067 ondisk_path);
2068 break;
2070 err = got_path_dirname(&parent, ondisk_path);
2071 if (err)
2072 break;
2075 done:
2076 free(ondisk_path);
2077 free(parent);
2078 return err;
2081 static const struct got_error *
2082 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 struct got_fileindex_entry *ie, struct got_repository *repo,
2084 got_worktree_checkout_cb progress_cb, void *progress_arg)
2086 const struct got_error *err = NULL;
2087 unsigned char status;
2088 struct stat sb;
2089 char *ondisk_path;
2091 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2092 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2094 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2095 == -1)
2096 return got_error_from_errno("asprintf");
2098 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2099 if (err)
2100 goto done;
2102 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2103 char ondisk_target[PATH_MAX];
2104 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2105 sizeof(ondisk_target));
2106 if (ondisk_len == -1) {
2107 err = got_error_from_errno2("readlink", ondisk_path);
2108 goto done;
2110 ondisk_target[ondisk_len] = '\0';
2111 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2112 NULL, NULL, /* XXX pass common ancestor info? */
2113 ondisk_target, ondisk_path);
2114 if (err)
2115 goto done;
2116 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2117 ie->path);
2118 goto done;
2121 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2122 status == GOT_STATUS_ADD) {
2123 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2124 if (err)
2125 goto done;
2127 * Preserve the working file and change the deleted blob's
2128 * entry into a schedule-add entry.
2130 err = got_fileindex_entry_update(ie, worktree->root_fd,
2131 ie->path, NULL, NULL, 0);
2132 } else {
2133 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2134 if (err)
2135 goto done;
2136 if (status == GOT_STATUS_NO_CHANGE) {
2137 err = remove_ondisk_file(worktree->root_path, ie->path);
2138 if (err)
2139 goto done;
2141 got_fileindex_entry_remove(fileindex, ie);
2143 done:
2144 free(ondisk_path);
2145 return err;
2148 struct diff_cb_arg {
2149 struct got_fileindex *fileindex;
2150 struct got_worktree *worktree;
2151 struct got_repository *repo;
2152 got_worktree_checkout_cb progress_cb;
2153 void *progress_arg;
2154 got_cancel_cb cancel_cb;
2155 void *cancel_arg;
2158 static const struct got_error *
2159 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2160 struct got_tree_entry *te, const char *parent_path)
2162 struct diff_cb_arg *a = arg;
2164 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2165 return got_error(GOT_ERR_CANCELLED);
2167 return update_blob(a->worktree, a->fileindex, ie, te,
2168 ie->path, a->repo, a->progress_cb, a->progress_arg);
2171 static const struct got_error *
2172 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2174 struct diff_cb_arg *a = arg;
2176 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2177 return got_error(GOT_ERR_CANCELLED);
2179 return delete_blob(a->worktree, a->fileindex, ie,
2180 a->repo, a->progress_cb, a->progress_arg);
2183 static const struct got_error *
2184 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2186 struct diff_cb_arg *a = arg;
2187 const struct got_error *err;
2188 char *path;
2190 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2191 return got_error(GOT_ERR_CANCELLED);
2193 if (got_object_tree_entry_is_submodule(te))
2194 return NULL;
2196 if (asprintf(&path, "%s%s%s", parent_path,
2197 parent_path[0] ? "/" : "", te->name)
2198 == -1)
2199 return got_error_from_errno("asprintf");
2201 if (S_ISDIR(te->mode))
2202 err = add_dir_on_disk(a->worktree, path);
2203 else
2204 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2205 a->repo, a->progress_cb, a->progress_arg);
2207 free(path);
2208 return err;
2211 const struct got_error *
2212 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2214 uint32_t uuid_status;
2216 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2217 if (uuid_status != uuid_s_ok) {
2218 *uuidstr = NULL;
2219 return got_error_uuid(uuid_status, "uuid_to_string");
2222 return NULL;
2225 static const struct got_error *
2226 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2228 const struct got_error *err = NULL;
2229 char *uuidstr = NULL;
2231 *refname = NULL;
2233 err = got_worktree_get_uuid(&uuidstr, worktree);
2234 if (err)
2235 return err;
2237 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2238 err = got_error_from_errno("asprintf");
2239 *refname = NULL;
2241 free(uuidstr);
2242 return err;
2245 const struct got_error *
2246 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2247 const char *prefix)
2249 return get_ref_name(refname, worktree, prefix);
2252 const struct got_error *
2253 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2258 static const struct got_error *
2259 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree,
2262 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2265 static const struct got_error *
2266 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2271 static const struct got_error *
2272 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2285 static const struct got_error *
2286 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2288 return get_ref_name(refname, worktree,
2289 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2292 static const struct got_error *
2293 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2295 return get_ref_name(refname, worktree,
2296 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2299 static const struct got_error *
2300 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2302 return get_ref_name(refname, worktree,
2303 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2306 static const struct got_error *
2307 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 return get_ref_name(refname, worktree,
2310 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2313 const struct got_error *
2314 got_worktree_get_histedit_script_path(char **path,
2315 struct got_worktree *worktree)
2317 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2319 *path = NULL;
2320 return got_error_from_errno("asprintf");
2322 return NULL;
2325 static const struct got_error *
2326 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2328 return get_ref_name(refname, worktree,
2329 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2332 static const struct got_error *
2333 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2335 return get_ref_name(refname, worktree,
2336 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2340 * Prevent Git's garbage collector from deleting our base commit by
2341 * setting a reference to our base commit's ID.
2343 static const struct got_error *
2344 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2346 const struct got_error *err = NULL;
2347 struct got_reference *ref = NULL;
2348 char *refname;
2350 err = got_worktree_get_base_ref_name(&refname, worktree);
2351 if (err)
2352 return err;
2354 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2355 if (err)
2356 goto done;
2358 err = got_ref_write(ref, repo);
2359 done:
2360 free(refname);
2361 if (ref)
2362 got_ref_close(ref);
2363 return err;
2366 static const struct got_error *
2367 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2369 const struct got_error *err = NULL;
2371 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2372 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2373 err = got_error_from_errno("asprintf");
2374 *fileindex_path = NULL;
2376 return err;
2380 static const struct got_error *
2381 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2382 struct got_worktree *worktree)
2384 const struct got_error *err = NULL;
2385 FILE *index = NULL;
2387 *fileindex_path = NULL;
2388 *fileindex = got_fileindex_alloc();
2389 if (*fileindex == NULL)
2390 return got_error_from_errno("got_fileindex_alloc");
2392 err = get_fileindex_path(fileindex_path, worktree);
2393 if (err)
2394 goto done;
2396 index = fopen(*fileindex_path, "rbe");
2397 if (index == NULL) {
2398 if (errno != ENOENT)
2399 err = got_error_from_errno2("fopen", *fileindex_path);
2400 } else {
2401 err = got_fileindex_read(*fileindex, index);
2402 if (fclose(index) == EOF && err == NULL)
2403 err = got_error_from_errno("fclose");
2405 done:
2406 if (err) {
2407 free(*fileindex_path);
2408 *fileindex_path = NULL;
2409 got_fileindex_free(*fileindex);
2410 *fileindex = NULL;
2412 return err;
2415 struct bump_base_commit_id_arg {
2416 struct got_object_id *base_commit_id;
2417 const char *path;
2418 size_t path_len;
2419 const char *entry_name;
2420 got_worktree_checkout_cb progress_cb;
2421 void *progress_arg;
2424 static const struct got_error *
2425 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2427 const struct got_error *err;
2428 struct bump_base_commit_id_arg *a = arg;
2430 if (a->entry_name) {
2431 if (strcmp(ie->path, a->path) != 0)
2432 return NULL;
2433 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2434 return NULL;
2436 if (got_fileindex_entry_was_skipped(ie))
2437 return NULL;
2439 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2440 SHA1_DIGEST_LENGTH) == 0)
2441 return NULL;
2443 if (a->progress_cb) {
2444 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2445 ie->path);
2446 if (err)
2447 return err;
2449 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2450 return NULL;
2453 /* Bump base commit ID of all files within an updated part of the work tree. */
2454 static const struct got_error *
2455 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2456 struct got_fileindex *fileindex,
2457 got_worktree_checkout_cb progress_cb, void *progress_arg)
2459 struct bump_base_commit_id_arg bbc_arg;
2461 bbc_arg.base_commit_id = worktree->base_commit_id;
2462 bbc_arg.entry_name = NULL;
2463 bbc_arg.path = "";
2464 bbc_arg.path_len = 0;
2465 bbc_arg.progress_cb = progress_cb;
2466 bbc_arg.progress_arg = progress_arg;
2468 return got_fileindex_for_each_entry_safe(fileindex,
2469 bump_base_commit_id, &bbc_arg);
2472 static const struct got_error *
2473 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2475 const struct got_error *err = NULL;
2476 char *new_fileindex_path = NULL;
2477 FILE *new_index = NULL;
2478 struct timespec timeout;
2480 err = got_opentemp_named(&new_fileindex_path, &new_index,
2481 fileindex_path, "");
2482 if (err)
2483 goto done;
2485 err = got_fileindex_write(fileindex, new_index);
2486 if (err)
2487 goto done;
2489 if (rename(new_fileindex_path, fileindex_path) != 0) {
2490 err = got_error_from_errno3("rename", new_fileindex_path,
2491 fileindex_path);
2492 unlink(new_fileindex_path);
2496 * Sleep for a short amount of time to ensure that files modified after
2497 * this program exits have a different time stamp from the one which
2498 * was recorded in the file index.
2500 timeout.tv_sec = 0;
2501 timeout.tv_nsec = 1;
2502 nanosleep(&timeout, NULL);
2503 done:
2504 if (new_index)
2505 fclose(new_index);
2506 free(new_fileindex_path);
2507 return err;
2510 static const struct got_error *
2511 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2512 struct got_object_id **tree_id, const char *wt_relpath,
2513 struct got_commit_object *base_commit, struct got_worktree *worktree,
2514 struct got_repository *repo)
2516 const struct got_error *err = NULL;
2517 struct got_object_id *id = NULL;
2518 char *in_repo_path = NULL;
2519 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2521 *entry_type = GOT_OBJ_TYPE_ANY;
2522 *tree_relpath = NULL;
2523 *tree_id = NULL;
2525 if (wt_relpath[0] == '\0') {
2526 /* Check out all files within the work tree. */
2527 *entry_type = GOT_OBJ_TYPE_TREE;
2528 *tree_relpath = strdup("");
2529 if (*tree_relpath == NULL) {
2530 err = got_error_from_errno("strdup");
2531 goto done;
2533 err = got_object_id_by_path(tree_id, repo, base_commit,
2534 worktree->path_prefix);
2535 if (err)
2536 goto done;
2537 return NULL;
2540 /* Check out a subset of files in the work tree. */
2542 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2543 is_root_wt ? "" : "/", wt_relpath) == -1) {
2544 err = got_error_from_errno("asprintf");
2545 goto done;
2548 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2549 if (err)
2550 goto done;
2552 free(in_repo_path);
2553 in_repo_path = NULL;
2555 err = got_object_get_type(entry_type, repo, id);
2556 if (err)
2557 goto done;
2559 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2560 /* Check out a single file. */
2561 if (strchr(wt_relpath, '/') == NULL) {
2562 /* Check out a single file in work tree's root dir. */
2563 in_repo_path = strdup(worktree->path_prefix);
2564 if (in_repo_path == NULL) {
2565 err = got_error_from_errno("strdup");
2566 goto done;
2568 *tree_relpath = strdup("");
2569 if (*tree_relpath == NULL) {
2570 err = got_error_from_errno("strdup");
2571 goto done;
2573 } else {
2574 /* Check out a single file in a subdirectory. */
2575 err = got_path_dirname(tree_relpath, wt_relpath);
2576 if (err)
2577 return err;
2578 if (asprintf(&in_repo_path, "%s%s%s",
2579 worktree->path_prefix, is_root_wt ? "" : "/",
2580 *tree_relpath) == -1) {
2581 err = got_error_from_errno("asprintf");
2582 goto done;
2585 err = got_object_id_by_path(tree_id, repo,
2586 base_commit, in_repo_path);
2587 } else {
2588 /* Check out all files within a subdirectory. */
2589 *tree_id = got_object_id_dup(id);
2590 if (*tree_id == NULL) {
2591 err = got_error_from_errno("got_object_id_dup");
2592 goto done;
2594 *tree_relpath = strdup(wt_relpath);
2595 if (*tree_relpath == NULL) {
2596 err = got_error_from_errno("strdup");
2597 goto done;
2600 done:
2601 free(id);
2602 free(in_repo_path);
2603 if (err) {
2604 *entry_type = GOT_OBJ_TYPE_ANY;
2605 free(*tree_relpath);
2606 *tree_relpath = NULL;
2607 free(*tree_id);
2608 *tree_id = NULL;
2610 return err;
2613 static const struct got_error *
2614 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2615 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2616 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2617 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2619 const struct got_error *err = NULL;
2620 struct got_commit_object *commit = NULL;
2621 struct got_tree_object *tree = NULL;
2622 struct got_fileindex_diff_tree_cb diff_cb;
2623 struct diff_cb_arg arg;
2625 err = ref_base_commit(worktree, repo);
2626 if (err) {
2627 if (!(err->code == GOT_ERR_ERRNO &&
2628 (errno == EACCES || errno == EROFS)))
2629 goto done;
2630 err = (*progress_cb)(progress_arg,
2631 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2632 if (err)
2633 return err;
2636 err = got_object_open_as_commit(&commit, repo,
2637 worktree->base_commit_id);
2638 if (err)
2639 goto done;
2641 err = got_object_open_as_tree(&tree, repo, tree_id);
2642 if (err)
2643 goto done;
2645 if (entry_name &&
2646 got_object_tree_find_entry(tree, entry_name) == NULL) {
2647 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2648 goto done;
2651 diff_cb.diff_old_new = diff_old_new;
2652 diff_cb.diff_old = diff_old;
2653 diff_cb.diff_new = diff_new;
2654 arg.fileindex = fileindex;
2655 arg.worktree = worktree;
2656 arg.repo = repo;
2657 arg.progress_cb = progress_cb;
2658 arg.progress_arg = progress_arg;
2659 arg.cancel_cb = cancel_cb;
2660 arg.cancel_arg = cancel_arg;
2661 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2662 entry_name, repo, &diff_cb, &arg);
2663 done:
2664 if (tree)
2665 got_object_tree_close(tree);
2666 if (commit)
2667 got_object_commit_close(commit);
2668 return err;
2671 const struct got_error *
2672 got_worktree_checkout_files(struct got_worktree *worktree,
2673 struct got_pathlist_head *paths, struct got_repository *repo,
2674 got_worktree_checkout_cb progress_cb, void *progress_arg,
2675 got_cancel_cb cancel_cb, void *cancel_arg)
2677 const struct got_error *err = NULL, *sync_err, *unlockerr;
2678 struct got_commit_object *commit = NULL;
2679 struct got_tree_object *tree = NULL;
2680 struct got_fileindex *fileindex = NULL;
2681 char *fileindex_path = NULL;
2682 struct got_pathlist_entry *pe;
2683 struct tree_path_data {
2684 STAILQ_ENTRY(tree_path_data) entry;
2685 struct got_object_id *tree_id;
2686 int entry_type;
2687 char *relpath;
2688 char *entry_name;
2689 } *tpd = NULL;
2690 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2692 STAILQ_INIT(&tree_paths);
2694 err = lock_worktree(worktree, LOCK_EX);
2695 if (err)
2696 return err;
2698 err = got_object_open_as_commit(&commit, repo,
2699 worktree->base_commit_id);
2700 if (err)
2701 goto done;
2703 /* Map all specified paths to in-repository trees. */
2704 TAILQ_FOREACH(pe, paths, entry) {
2705 tpd = malloc(sizeof(*tpd));
2706 if (tpd == NULL) {
2707 err = got_error_from_errno("malloc");
2708 goto done;
2711 err = find_tree_entry_for_checkout(&tpd->entry_type,
2712 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2713 worktree, repo);
2714 if (err) {
2715 free(tpd);
2716 goto done;
2719 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2720 err = got_path_basename(&tpd->entry_name, pe->path);
2721 if (err) {
2722 free(tpd->relpath);
2723 free(tpd->tree_id);
2724 free(tpd);
2725 goto done;
2727 } else
2728 tpd->entry_name = NULL;
2730 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2734 * Read the file index.
2735 * Checking out files is supposed to be an idempotent operation.
2736 * If the on-disk file index is incomplete we will try to complete it.
2738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2739 if (err)
2740 goto done;
2742 tpd = STAILQ_FIRST(&tree_paths);
2743 TAILQ_FOREACH(pe, paths, entry) {
2744 struct bump_base_commit_id_arg bbc_arg;
2746 err = checkout_files(worktree, fileindex, tpd->relpath,
2747 tpd->tree_id, tpd->entry_name, repo,
2748 progress_cb, progress_arg, cancel_cb, cancel_arg);
2749 if (err)
2750 break;
2752 bbc_arg.base_commit_id = worktree->base_commit_id;
2753 bbc_arg.entry_name = tpd->entry_name;
2754 bbc_arg.path = pe->path;
2755 bbc_arg.path_len = pe->path_len;
2756 bbc_arg.progress_cb = progress_cb;
2757 bbc_arg.progress_arg = progress_arg;
2758 err = got_fileindex_for_each_entry_safe(fileindex,
2759 bump_base_commit_id, &bbc_arg);
2760 if (err)
2761 break;
2763 tpd = STAILQ_NEXT(tpd, entry);
2765 sync_err = sync_fileindex(fileindex, fileindex_path);
2766 if (sync_err && err == NULL)
2767 err = sync_err;
2768 done:
2769 free(fileindex_path);
2770 if (tree)
2771 got_object_tree_close(tree);
2772 if (commit)
2773 got_object_commit_close(commit);
2774 if (fileindex)
2775 got_fileindex_free(fileindex);
2776 while (!STAILQ_EMPTY(&tree_paths)) {
2777 tpd = STAILQ_FIRST(&tree_paths);
2778 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2779 free(tpd->relpath);
2780 free(tpd->tree_id);
2781 free(tpd);
2783 unlockerr = lock_worktree(worktree, LOCK_SH);
2784 if (unlockerr && err == NULL)
2785 err = unlockerr;
2786 return err;
2789 struct merge_file_cb_arg {
2790 struct got_worktree *worktree;
2791 struct got_fileindex *fileindex;
2792 got_worktree_checkout_cb progress_cb;
2793 void *progress_arg;
2794 got_cancel_cb cancel_cb;
2795 void *cancel_arg;
2796 const char *label_orig;
2797 struct got_object_id *commit_id2;
2798 int allow_bad_symlinks;
2801 static const struct got_error *
2802 merge_file_cb(void *arg, struct got_blob_object *blob1,
2803 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2804 struct got_object_id *id1, struct got_object_id *id2,
2805 const char *path1, const char *path2,
2806 mode_t mode1, mode_t mode2, struct got_repository *repo)
2808 static const struct got_error *err = NULL;
2809 struct merge_file_cb_arg *a = arg;
2810 struct got_fileindex_entry *ie;
2811 char *ondisk_path = NULL;
2812 struct stat sb;
2813 unsigned char status;
2814 int local_changes_subsumed;
2815 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2816 char *id_str = NULL, *label_deriv2 = NULL;
2818 if (blob1 && blob2) {
2819 ie = got_fileindex_entry_get(a->fileindex, path2,
2820 strlen(path2));
2821 if (ie == NULL)
2822 return (*a->progress_cb)(a->progress_arg,
2823 GOT_STATUS_MISSING, path2);
2825 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2826 path2) == -1)
2827 return got_error_from_errno("asprintf");
2829 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2830 repo);
2831 if (err)
2832 goto done;
2834 if (status == GOT_STATUS_DELETE) {
2835 err = (*a->progress_cb)(a->progress_arg,
2836 GOT_STATUS_MERGE, path2);
2837 goto done;
2839 if (status != GOT_STATUS_NO_CHANGE &&
2840 status != GOT_STATUS_MODIFY &&
2841 status != GOT_STATUS_CONFLICT &&
2842 status != GOT_STATUS_ADD) {
2843 err = (*a->progress_cb)(a->progress_arg, status, path2);
2844 goto done;
2847 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2848 char *link_target2;
2849 err = got_object_blob_read_to_str(&link_target2, blob2);
2850 if (err)
2851 goto done;
2852 err = merge_symlink(a->worktree, blob1, ondisk_path,
2853 path2, a->label_orig, link_target2, a->commit_id2,
2854 repo, a->progress_cb, a->progress_arg);
2855 free(link_target2);
2856 } else {
2857 int fd;
2859 f_orig = got_opentemp();
2860 if (f_orig == NULL) {
2861 err = got_error_from_errno("got_opentemp");
2862 goto done;
2864 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2865 f_orig, blob1);
2866 if (err)
2867 goto done;
2869 f_deriv2 = got_opentemp();
2870 if (f_deriv2 == NULL)
2871 goto done;
2872 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2873 f_deriv2, blob2);
2874 if (err)
2875 goto done;
2877 fd = open(ondisk_path,
2878 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2879 if (fd == -1) {
2880 err = got_error_from_errno2("open",
2881 ondisk_path);
2882 goto done;
2884 f_deriv = fdopen(fd, "r");
2885 if (f_deriv == NULL) {
2886 err = got_error_from_errno2("fdopen",
2887 ondisk_path);
2888 close(fd);
2889 goto done;
2891 err = got_object_id_str(&id_str, a->commit_id2);
2892 if (err)
2893 goto done;
2894 if (asprintf(&label_deriv2, "%s: commit %s",
2895 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2896 err = got_error_from_errno("asprintf");
2897 goto done;
2899 err = merge_file(&local_changes_subsumed, a->worktree,
2900 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2901 mode2, a->label_orig, NULL, label_deriv2,
2902 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2903 a->progress_cb, a->progress_arg);
2905 } else if (blob1) {
2906 ie = got_fileindex_entry_get(a->fileindex, path1,
2907 strlen(path1));
2908 if (ie == NULL)
2909 return (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_MISSING, path1);
2912 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2913 path1) == -1)
2914 return got_error_from_errno("asprintf");
2916 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2917 repo);
2918 if (err)
2919 goto done;
2921 switch (status) {
2922 case GOT_STATUS_NO_CHANGE:
2923 err = (*a->progress_cb)(a->progress_arg,
2924 GOT_STATUS_DELETE, path1);
2925 if (err)
2926 goto done;
2927 err = remove_ondisk_file(a->worktree->root_path, path1);
2928 if (err)
2929 goto done;
2930 if (ie)
2931 got_fileindex_entry_mark_deleted_from_disk(ie);
2932 break;
2933 case GOT_STATUS_DELETE:
2934 case GOT_STATUS_MISSING:
2935 err = (*a->progress_cb)(a->progress_arg,
2936 GOT_STATUS_DELETE, path1);
2937 if (err)
2938 goto done;
2939 if (ie)
2940 got_fileindex_entry_mark_deleted_from_disk(ie);
2941 break;
2942 case GOT_STATUS_ADD: {
2943 struct got_object_id *id;
2944 FILE *blob1_f;
2945 off_t blob1_size;
2947 * Delete the added file only if its content already
2948 * exists in the repository.
2950 err = got_object_blob_file_create(&id, &blob1_f,
2951 &blob1_size, path1);
2952 if (err)
2953 goto done;
2954 if (got_object_id_cmp(id, id1) == 0) {
2955 err = (*a->progress_cb)(a->progress_arg,
2956 GOT_STATUS_DELETE, path1);
2957 if (err)
2958 goto done;
2959 err = remove_ondisk_file(a->worktree->root_path,
2960 path1);
2961 if (err)
2962 goto done;
2963 if (ie)
2964 got_fileindex_entry_remove(a->fileindex,
2965 ie);
2966 } else {
2967 err = (*a->progress_cb)(a->progress_arg,
2968 GOT_STATUS_CANNOT_DELETE, path1);
2970 if (fclose(blob1_f) == EOF && err == NULL)
2971 err = got_error_from_errno("fclose");
2972 free(id);
2973 if (err)
2974 goto done;
2975 break;
2977 case GOT_STATUS_MODIFY:
2978 case GOT_STATUS_CONFLICT:
2979 err = (*a->progress_cb)(a->progress_arg,
2980 GOT_STATUS_CANNOT_DELETE, path1);
2981 if (err)
2982 goto done;
2983 break;
2984 case GOT_STATUS_OBSTRUCTED:
2985 err = (*a->progress_cb)(a->progress_arg, status, path1);
2986 if (err)
2987 goto done;
2988 break;
2989 default:
2990 break;
2992 } else if (blob2) {
2993 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2994 path2) == -1)
2995 return got_error_from_errno("asprintf");
2996 ie = got_fileindex_entry_get(a->fileindex, path2,
2997 strlen(path2));
2998 if (ie) {
2999 err = get_file_status(&status, &sb, ie, ondisk_path,
3000 -1, NULL, repo);
3001 if (err)
3002 goto done;
3003 if (status != GOT_STATUS_NO_CHANGE &&
3004 status != GOT_STATUS_MODIFY &&
3005 status != GOT_STATUS_CONFLICT &&
3006 status != GOT_STATUS_ADD) {
3007 err = (*a->progress_cb)(a->progress_arg,
3008 status, path2);
3009 goto done;
3011 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3012 char *link_target2;
3013 err = got_object_blob_read_to_str(&link_target2,
3014 blob2);
3015 if (err)
3016 goto done;
3017 err = merge_symlink(a->worktree, NULL,
3018 ondisk_path, path2, a->label_orig,
3019 link_target2, a->commit_id2, repo,
3020 a->progress_cb, a->progress_arg);
3021 free(link_target2);
3022 } else if (S_ISREG(sb.st_mode)) {
3023 err = merge_blob(&local_changes_subsumed,
3024 a->worktree, NULL, ondisk_path, path2,
3025 sb.st_mode, a->label_orig, blob2,
3026 a->commit_id2, repo, a->progress_cb,
3027 a->progress_arg);
3028 } else {
3029 err = got_error_path(ondisk_path,
3030 GOT_ERR_FILE_OBSTRUCTED);
3032 if (err)
3033 goto done;
3034 if (status == GOT_STATUS_DELETE) {
3035 err = got_fileindex_entry_update(ie,
3036 a->worktree->root_fd, path2, blob2->id.sha1,
3037 a->worktree->base_commit_id->sha1, 0);
3038 if (err)
3039 goto done;
3041 } else {
3042 int is_bad_symlink = 0;
3043 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3044 if (S_ISLNK(mode2)) {
3045 err = install_symlink(&is_bad_symlink,
3046 a->worktree, ondisk_path, path2, blob2, 0,
3047 0, 1, a->allow_bad_symlinks, repo,
3048 a->progress_cb, a->progress_arg);
3049 } else {
3050 err = install_blob(a->worktree, ondisk_path, path2,
3051 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3052 a->progress_cb, a->progress_arg);
3054 if (err)
3055 goto done;
3056 err = got_fileindex_entry_alloc(&ie, path2);
3057 if (err)
3058 goto done;
3059 err = got_fileindex_entry_update(ie,
3060 a->worktree->root_fd, path2, NULL, NULL, 1);
3061 if (err) {
3062 got_fileindex_entry_free(ie);
3063 goto done;
3065 err = got_fileindex_entry_add(a->fileindex, ie);
3066 if (err) {
3067 got_fileindex_entry_free(ie);
3068 goto done;
3070 if (is_bad_symlink) {
3071 got_fileindex_entry_filetype_set(ie,
3072 GOT_FILEIDX_MODE_BAD_SYMLINK);
3076 done:
3077 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3078 err = got_error_from_errno("fclose");
3079 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3080 err = got_error_from_errno("fclose");
3081 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3082 err = got_error_from_errno("fclose");
3083 free(id_str);
3084 free(label_deriv2);
3085 free(ondisk_path);
3086 return err;
3089 static const struct got_error *
3090 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3092 struct got_worktree *worktree = arg;
3094 /* Reject merges into a work tree with mixed base commits. */
3095 if (got_fileindex_entry_has_commit(ie) &&
3096 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3097 SHA1_DIGEST_LENGTH) != 0)
3098 return got_error(GOT_ERR_MIXED_COMMITS);
3100 return NULL;
3103 struct check_merge_conflicts_arg {
3104 struct got_worktree *worktree;
3105 struct got_fileindex *fileindex;
3106 struct got_repository *repo;
3109 static const struct got_error *
3110 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3111 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3112 struct got_object_id *id1, struct got_object_id *id2,
3113 const char *path1, const char *path2,
3114 mode_t mode1, mode_t mode2, struct got_repository *repo)
3116 const struct got_error *err = NULL;
3117 struct check_merge_conflicts_arg *a = arg;
3118 unsigned char status;
3119 struct stat sb;
3120 struct got_fileindex_entry *ie;
3121 const char *path = path2 ? path2 : path1;
3122 struct got_object_id *id = id2 ? id2 : id1;
3123 char *ondisk_path;
3125 if (id == NULL)
3126 return NULL;
3128 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3129 if (ie == NULL)
3130 return NULL;
3132 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3133 == -1)
3134 return got_error_from_errno("asprintf");
3136 /* Reject merges into a work tree with conflicted files. */
3137 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3138 free(ondisk_path);
3139 if (err)
3140 return err;
3141 if (status == GOT_STATUS_CONFLICT)
3142 return got_error(GOT_ERR_CONFLICTS);
3144 return NULL;
3147 static const struct got_error *
3148 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3149 const char *fileindex_path, struct got_object_id *commit_id1,
3150 struct got_object_id *commit_id2, struct got_repository *repo,
3151 got_worktree_checkout_cb progress_cb, void *progress_arg,
3152 got_cancel_cb cancel_cb, void *cancel_arg)
3154 const struct got_error *err = NULL, *sync_err;
3155 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3156 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3157 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3158 struct check_merge_conflicts_arg cmc_arg;
3159 struct merge_file_cb_arg arg;
3160 char *label_orig = NULL;
3161 FILE *f1 = NULL, *f2 = NULL;
3162 int fd1 = -1, fd2 = -1;
3164 if (commit_id1) {
3165 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3166 if (err)
3167 goto done;
3168 err = got_object_id_by_path(&tree_id1, repo, commit1,
3169 worktree->path_prefix);
3170 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3171 goto done;
3173 if (tree_id1) {
3174 char *id_str;
3176 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3177 if (err)
3178 goto done;
3180 err = got_object_id_str(&id_str, commit_id1);
3181 if (err)
3182 goto done;
3184 if (asprintf(&label_orig, "%s: commit %s",
3185 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3186 err = got_error_from_errno("asprintf");
3187 free(id_str);
3188 goto done;
3190 free(id_str);
3192 f1 = got_opentemp();
3193 if (f1 == NULL) {
3194 err = got_error_from_errno("got_opentemp");
3195 goto done;
3199 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3200 if (err)
3201 goto done;
3203 err = got_object_id_by_path(&tree_id2, repo, commit2,
3204 worktree->path_prefix);
3205 if (err)
3206 goto done;
3208 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3209 if (err)
3210 goto done;
3212 f2 = got_opentemp();
3213 if (f2 == NULL) {
3214 err = got_error_from_errno("got_opentemp");
3215 goto done;
3218 fd1 = got_opentempfd();
3219 if (fd1 == -1) {
3220 err = got_error_from_errno("got_opentempfd");
3221 goto done;
3224 fd2 = got_opentempfd();
3225 if (fd2 == -1) {
3226 err = got_error_from_errno("got_opentempfd");
3227 goto done;
3230 cmc_arg.worktree = worktree;
3231 cmc_arg.fileindex = fileindex;
3232 cmc_arg.repo = repo;
3233 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3234 check_merge_conflicts, &cmc_arg, 0);
3235 if (err)
3236 goto done;
3238 arg.worktree = worktree;
3239 arg.fileindex = fileindex;
3240 arg.progress_cb = progress_cb;
3241 arg.progress_arg = progress_arg;
3242 arg.cancel_cb = cancel_cb;
3243 arg.cancel_arg = cancel_arg;
3244 arg.label_orig = label_orig;
3245 arg.commit_id2 = commit_id2;
3246 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3247 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3248 merge_file_cb, &arg, 1);
3249 sync_err = sync_fileindex(fileindex, fileindex_path);
3250 if (sync_err && err == NULL)
3251 err = sync_err;
3252 done:
3253 if (commit1)
3254 got_object_commit_close(commit1);
3255 if (commit2)
3256 got_object_commit_close(commit2);
3257 if (tree1)
3258 got_object_tree_close(tree1);
3259 if (tree2)
3260 got_object_tree_close(tree2);
3261 if (f1 && fclose(f1) == EOF && err == NULL)
3262 err = got_error_from_errno("fclose");
3263 if (f2 && fclose(f2) == EOF && err == NULL)
3264 err = got_error_from_errno("fclose");
3265 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3266 err = got_error_from_errno("close");
3267 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3268 err = got_error_from_errno("close");
3269 free(label_orig);
3270 return err;
3273 const struct got_error *
3274 got_worktree_merge_files(struct got_worktree *worktree,
3275 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3276 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3277 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3279 const struct got_error *err, *unlockerr;
3280 char *fileindex_path = NULL;
3281 struct got_fileindex *fileindex = NULL;
3283 err = lock_worktree(worktree, LOCK_EX);
3284 if (err)
3285 return err;
3287 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3288 if (err)
3289 goto done;
3291 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3292 worktree);
3293 if (err)
3294 goto done;
3296 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3297 commit_id2, repo, progress_cb, progress_arg,
3298 cancel_cb, cancel_arg);
3299 done:
3300 if (fileindex)
3301 got_fileindex_free(fileindex);
3302 free(fileindex_path);
3303 unlockerr = lock_worktree(worktree, LOCK_SH);
3304 if (unlockerr && err == NULL)
3305 err = unlockerr;
3306 return err;
3309 struct diff_dir_cb_arg {
3310 struct got_fileindex *fileindex;
3311 struct got_worktree *worktree;
3312 const char *status_path;
3313 size_t status_path_len;
3314 struct got_repository *repo;
3315 got_worktree_status_cb status_cb;
3316 void *status_arg;
3317 got_cancel_cb cancel_cb;
3318 void *cancel_arg;
3319 /* A pathlist containing per-directory pathlists of ignore patterns. */
3320 struct got_pathlist_head *ignores;
3321 int report_unchanged;
3322 int no_ignores;
3325 static const struct got_error *
3326 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3327 int dirfd, const char *de_name,
3328 got_worktree_status_cb status_cb, void *status_arg,
3329 struct got_repository *repo, int report_unchanged)
3331 const struct got_error *err = NULL;
3332 unsigned char status = GOT_STATUS_NO_CHANGE;
3333 unsigned char staged_status;
3334 struct stat sb;
3335 struct got_object_id blob_id, commit_id, staged_blob_id;
3336 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3337 struct got_object_id *staged_blob_idp = NULL;
3339 staged_status = get_staged_status(ie);
3340 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3341 if (err)
3342 return err;
3344 if (status == GOT_STATUS_NO_CHANGE &&
3345 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3346 return NULL;
3348 if (got_fileindex_entry_has_blob(ie))
3349 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3350 if (got_fileindex_entry_has_commit(ie))
3351 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3352 if (staged_status == GOT_STATUS_ADD ||
3353 staged_status == GOT_STATUS_MODIFY) {
3354 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3355 &staged_blob_id, ie);
3358 return (*status_cb)(status_arg, status, staged_status,
3359 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3362 static const struct got_error *
3363 status_old_new(void *arg, struct got_fileindex_entry *ie,
3364 struct dirent *de, const char *parent_path, int dirfd)
3366 const struct got_error *err = NULL;
3367 struct diff_dir_cb_arg *a = arg;
3368 char *abspath;
3370 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3371 return got_error(GOT_ERR_CANCELLED);
3373 if (got_path_cmp(parent_path, a->status_path,
3374 strlen(parent_path), a->status_path_len) != 0 &&
3375 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3376 return NULL;
3378 if (parent_path[0]) {
3379 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3380 parent_path, de->d_name) == -1)
3381 return got_error_from_errno("asprintf");
3382 } else {
3383 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3384 de->d_name) == -1)
3385 return got_error_from_errno("asprintf");
3388 err = report_file_status(ie, abspath, dirfd, de->d_name,
3389 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3390 free(abspath);
3391 return err;
3394 static const struct got_error *
3395 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3397 struct diff_dir_cb_arg *a = arg;
3398 struct got_object_id blob_id, commit_id;
3399 unsigned char status;
3401 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3402 return got_error(GOT_ERR_CANCELLED);
3404 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3405 return NULL;
3407 got_fileindex_entry_get_blob_id(&blob_id, ie);
3408 got_fileindex_entry_get_commit_id(&commit_id, ie);
3409 if (got_fileindex_entry_has_file_on_disk(ie))
3410 status = GOT_STATUS_MISSING;
3411 else
3412 status = GOT_STATUS_DELETE;
3413 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3414 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3417 static void
3418 free_ignores(struct got_pathlist_head *ignores)
3420 struct got_pathlist_entry *pe;
3422 TAILQ_FOREACH(pe, ignores, entry) {
3423 struct got_pathlist_head *ignorelist = pe->data;
3425 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3427 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3430 static const struct got_error *
3431 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3433 const struct got_error *err = NULL;
3434 struct got_pathlist_entry *pe = NULL;
3435 struct got_pathlist_head *ignorelist;
3436 char *line = NULL, *pattern, *dirpath = NULL;
3437 size_t linesize = 0;
3438 ssize_t linelen;
3440 ignorelist = calloc(1, sizeof(*ignorelist));
3441 if (ignorelist == NULL)
3442 return got_error_from_errno("calloc");
3443 TAILQ_INIT(ignorelist);
3445 while ((linelen = getline(&line, &linesize, f)) != -1) {
3446 if (linelen > 0 && line[linelen - 1] == '\n')
3447 line[linelen - 1] = '\0';
3449 /* Git's ignores may contain comments. */
3450 if (line[0] == '#')
3451 continue;
3453 /* Git's negated patterns are not (yet?) supported. */
3454 if (line[0] == '!')
3455 continue;
3457 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3458 line) == -1) {
3459 err = got_error_from_errno("asprintf");
3460 goto done;
3462 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3463 if (err)
3464 goto done;
3466 if (ferror(f)) {
3467 err = got_error_from_errno("getline");
3468 goto done;
3471 dirpath = strdup(path);
3472 if (dirpath == NULL) {
3473 err = got_error_from_errno("strdup");
3474 goto done;
3476 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3477 done:
3478 free(line);
3479 if (err || pe == NULL) {
3480 free(dirpath);
3481 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3483 return err;
3486 static int
3487 match_ignores(struct got_pathlist_head *ignores, const char *path)
3489 struct got_pathlist_entry *pe;
3491 /* Handle patterns which match in all directories. */
3492 TAILQ_FOREACH(pe, ignores, entry) {
3493 struct got_pathlist_head *ignorelist = pe->data;
3494 struct got_pathlist_entry *pi;
3496 TAILQ_FOREACH(pi, ignorelist, entry) {
3497 const char *p, *pattern = pi->path;
3499 if (strncmp(pattern, "**/", 3) != 0)
3500 continue;
3501 pattern += 3;
3502 p = path;
3503 while (*p) {
3504 if (fnmatch(pattern, p,
3505 FNM_PATHNAME | FNM_LEADING_DIR)) {
3506 /* Retry in next directory. */
3507 while (*p && *p != '/')
3508 p++;
3509 while (*p == '/')
3510 p++;
3511 continue;
3513 return 1;
3519 * The ignores pathlist contains ignore lists from children before
3520 * parents, so we can find the most specific ignorelist by walking
3521 * ignores backwards.
3523 pe = TAILQ_LAST(ignores, got_pathlist_head);
3524 while (pe) {
3525 if (got_path_is_child(path, pe->path, pe->path_len)) {
3526 struct got_pathlist_head *ignorelist = pe->data;
3527 struct got_pathlist_entry *pi;
3528 TAILQ_FOREACH(pi, ignorelist, entry) {
3529 const char *pattern = pi->path;
3530 int flags = FNM_LEADING_DIR;
3531 if (strstr(pattern, "/**/") == NULL)
3532 flags |= FNM_PATHNAME;
3533 if (fnmatch(pattern, path, flags))
3534 continue;
3535 return 1;
3538 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3541 return 0;
3544 static const struct got_error *
3545 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3546 const char *path, int dirfd, const char *ignores_filename)
3548 const struct got_error *err = NULL;
3549 char *ignorespath;
3550 int fd = -1;
3551 FILE *ignoresfile = NULL;
3553 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3554 path[0] ? "/" : "", ignores_filename) == -1)
3555 return got_error_from_errno("asprintf");
3557 if (dirfd != -1) {
3558 fd = openat(dirfd, ignores_filename,
3559 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3560 if (fd == -1) {
3561 if (errno != ENOENT && errno != EACCES)
3562 err = got_error_from_errno2("openat",
3563 ignorespath);
3564 } else {
3565 ignoresfile = fdopen(fd, "r");
3566 if (ignoresfile == NULL)
3567 err = got_error_from_errno2("fdopen",
3568 ignorespath);
3569 else {
3570 fd = -1;
3571 err = read_ignores(ignores, path, ignoresfile);
3574 } else {
3575 ignoresfile = fopen(ignorespath, "re");
3576 if (ignoresfile == NULL) {
3577 if (errno != ENOENT && errno != EACCES)
3578 err = got_error_from_errno2("fopen",
3579 ignorespath);
3580 } else
3581 err = read_ignores(ignores, path, ignoresfile);
3584 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3585 err = got_error_from_errno2("fclose", path);
3586 if (fd != -1 && close(fd) == -1 && err == NULL)
3587 err = got_error_from_errno2("close", path);
3588 free(ignorespath);
3589 return err;
3592 static const struct got_error *
3593 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3594 int dirfd)
3596 const struct got_error *err = NULL;
3597 struct diff_dir_cb_arg *a = arg;
3598 char *path = NULL;
3600 if (ignore != NULL)
3601 *ignore = 0;
3603 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3604 return got_error(GOT_ERR_CANCELLED);
3606 if (parent_path[0]) {
3607 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3608 return got_error_from_errno("asprintf");
3609 } else {
3610 path = de->d_name;
3613 if (de->d_type == DT_DIR) {
3614 if (!a->no_ignores && ignore != NULL &&
3615 match_ignores(a->ignores, path))
3616 *ignore = 1;
3617 } else if (!match_ignores(a->ignores, path) &&
3618 got_path_is_child(path, a->status_path, a->status_path_len))
3619 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3620 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3621 if (parent_path[0])
3622 free(path);
3623 return err;
3626 static const struct got_error *
3627 status_traverse(void *arg, const char *path, int dirfd)
3629 const struct got_error *err = NULL;
3630 struct diff_dir_cb_arg *a = arg;
3632 if (a->no_ignores)
3633 return NULL;
3635 err = add_ignores(a->ignores, a->worktree->root_path,
3636 path, dirfd, ".cvsignore");
3637 if (err)
3638 return err;
3640 err = add_ignores(a->ignores, a->worktree->root_path, path,
3641 dirfd, ".gitignore");
3643 return err;
3646 static const struct got_error *
3647 report_single_file_status(const char *path, const char *ondisk_path,
3648 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3649 void *status_arg, struct got_repository *repo, int report_unchanged,
3650 struct got_pathlist_head *ignores, int no_ignores)
3652 struct got_fileindex_entry *ie;
3653 struct stat sb;
3655 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3656 if (ie)
3657 return report_file_status(ie, ondisk_path, -1, NULL,
3658 status_cb, status_arg, repo, report_unchanged);
3660 if (lstat(ondisk_path, &sb) == -1) {
3661 if (errno != ENOENT)
3662 return got_error_from_errno2("lstat", ondisk_path);
3663 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3664 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3667 if (!no_ignores && match_ignores(ignores, path))
3668 return NULL;
3670 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3671 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3672 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3674 return NULL;
3677 static const struct got_error *
3678 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3679 const char *root_path, const char *path)
3681 const struct got_error *err;
3682 char *parent_path, *next_parent_path = NULL;
3684 err = add_ignores(ignores, root_path, "", -1,
3685 ".cvsignore");
3686 if (err)
3687 return err;
3689 err = add_ignores(ignores, root_path, "", -1,
3690 ".gitignore");
3691 if (err)
3692 return err;
3694 err = got_path_dirname(&parent_path, path);
3695 if (err) {
3696 if (err->code == GOT_ERR_BAD_PATH)
3697 return NULL; /* cannot traverse parent */
3698 return err;
3700 for (;;) {
3701 err = add_ignores(ignores, root_path, parent_path, -1,
3702 ".cvsignore");
3703 if (err)
3704 break;
3705 err = add_ignores(ignores, root_path, parent_path, -1,
3706 ".gitignore");
3707 if (err)
3708 break;
3709 err = got_path_dirname(&next_parent_path, parent_path);
3710 if (err) {
3711 if (err->code == GOT_ERR_BAD_PATH)
3712 err = NULL; /* traversed everything */
3713 break;
3715 if (got_path_is_root_dir(parent_path))
3716 break;
3717 free(parent_path);
3718 parent_path = next_parent_path;
3719 next_parent_path = NULL;
3722 free(parent_path);
3723 free(next_parent_path);
3724 return err;
3727 static const struct got_error *
3728 worktree_status(struct got_worktree *worktree, const char *path,
3729 struct got_fileindex *fileindex, struct got_repository *repo,
3730 got_worktree_status_cb status_cb, void *status_arg,
3731 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3732 int report_unchanged)
3734 const struct got_error *err = NULL;
3735 int fd = -1;
3736 struct got_fileindex_diff_dir_cb fdiff_cb;
3737 struct diff_dir_cb_arg arg;
3738 char *ondisk_path = NULL;
3739 struct got_pathlist_head ignores;
3740 struct got_fileindex_entry *ie;
3742 TAILQ_INIT(&ignores);
3744 if (asprintf(&ondisk_path, "%s%s%s",
3745 worktree->root_path, path[0] ? "/" : "", path) == -1)
3746 return got_error_from_errno("asprintf");
3748 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3749 if (ie) {
3750 err = report_single_file_status(path, ondisk_path,
3751 fileindex, status_cb, status_arg, repo,
3752 report_unchanged, &ignores, no_ignores);
3753 goto done;
3756 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3757 if (fd == -1) {
3758 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3759 !got_err_open_nofollow_on_symlink())
3760 err = got_error_from_errno2("open", ondisk_path);
3761 else {
3762 if (!no_ignores) {
3763 err = add_ignores_from_parent_paths(&ignores,
3764 worktree->root_path, ondisk_path);
3765 if (err)
3766 goto done;
3768 err = report_single_file_status(path, ondisk_path,
3769 fileindex, status_cb, status_arg, repo,
3770 report_unchanged, &ignores, no_ignores);
3772 } else {
3773 fdiff_cb.diff_old_new = status_old_new;
3774 fdiff_cb.diff_old = status_old;
3775 fdiff_cb.diff_new = status_new;
3776 fdiff_cb.diff_traverse = status_traverse;
3777 arg.fileindex = fileindex;
3778 arg.worktree = worktree;
3779 arg.status_path = path;
3780 arg.status_path_len = strlen(path);
3781 arg.repo = repo;
3782 arg.status_cb = status_cb;
3783 arg.status_arg = status_arg;
3784 arg.cancel_cb = cancel_cb;
3785 arg.cancel_arg = cancel_arg;
3786 arg.report_unchanged = report_unchanged;
3787 arg.no_ignores = no_ignores;
3788 if (!no_ignores) {
3789 err = add_ignores_from_parent_paths(&ignores,
3790 worktree->root_path, path);
3791 if (err)
3792 goto done;
3794 arg.ignores = &ignores;
3795 err = got_fileindex_diff_dir(fileindex, fd,
3796 worktree->root_path, path, repo, &fdiff_cb, &arg);
3798 done:
3799 free_ignores(&ignores);
3800 if (fd != -1 && close(fd) == -1 && err == NULL)
3801 err = got_error_from_errno("close");
3802 free(ondisk_path);
3803 return err;
3806 const struct got_error *
3807 got_worktree_status(struct got_worktree *worktree,
3808 struct got_pathlist_head *paths, struct got_repository *repo,
3809 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3810 got_cancel_cb cancel_cb, void *cancel_arg)
3812 const struct got_error *err = NULL;
3813 char *fileindex_path = NULL;
3814 struct got_fileindex *fileindex = NULL;
3815 struct got_pathlist_entry *pe;
3817 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3818 if (err)
3819 return err;
3821 TAILQ_FOREACH(pe, paths, entry) {
3822 err = worktree_status(worktree, pe->path, fileindex, repo,
3823 status_cb, status_arg, cancel_cb, cancel_arg,
3824 no_ignores, 0);
3825 if (err)
3826 break;
3828 free(fileindex_path);
3829 got_fileindex_free(fileindex);
3830 return err;
3833 const struct got_error *
3834 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3835 const char *arg)
3837 const struct got_error *err = NULL;
3838 char *resolved = NULL, *cwd = NULL, *path = NULL;
3839 size_t len;
3840 struct stat sb;
3841 char *abspath = NULL;
3842 char canonpath[PATH_MAX];
3844 *wt_path = NULL;
3846 cwd = getcwd(NULL, 0);
3847 if (cwd == NULL)
3848 return got_error_from_errno("getcwd");
3850 if (lstat(arg, &sb) == -1) {
3851 if (errno != ENOENT) {
3852 err = got_error_from_errno2("lstat", arg);
3853 goto done;
3855 sb.st_mode = 0;
3857 if (S_ISLNK(sb.st_mode)) {
3859 * We cannot use realpath(3) with symlinks since we want to
3860 * operate on the symlink itself.
3861 * But we can make the path absolute, assuming it is relative
3862 * to the current working directory, and then canonicalize it.
3864 if (!got_path_is_absolute(arg)) {
3865 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3866 err = got_error_from_errno("asprintf");
3867 goto done;
3871 err = got_canonpath(abspath ? abspath : arg, canonpath,
3872 sizeof(canonpath));
3873 if (err)
3874 goto done;
3875 resolved = strdup(canonpath);
3876 if (resolved == NULL) {
3877 err = got_error_from_errno("strdup");
3878 goto done;
3880 } else {
3881 resolved = realpath(arg, NULL);
3882 if (resolved == NULL) {
3883 if (errno != ENOENT) {
3884 err = got_error_from_errno2("realpath", arg);
3885 goto done;
3887 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3888 err = got_error_from_errno("asprintf");
3889 goto done;
3891 err = got_canonpath(abspath, canonpath,
3892 sizeof(canonpath));
3893 if (err)
3894 goto done;
3895 resolved = strdup(canonpath);
3896 if (resolved == NULL) {
3897 err = got_error_from_errno("strdup");
3898 goto done;
3903 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3904 strlen(got_worktree_get_root_path(worktree)))) {
3905 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3906 goto done;
3909 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3910 err = got_path_skip_common_ancestor(&path,
3911 got_worktree_get_root_path(worktree), resolved);
3912 if (err)
3913 goto done;
3914 } else {
3915 path = strdup("");
3916 if (path == NULL) {
3917 err = got_error_from_errno("strdup");
3918 goto done;
3922 /* XXX status walk can't deal with trailing slash! */
3923 len = strlen(path);
3924 while (len > 0 && path[len - 1] == '/') {
3925 path[len - 1] = '\0';
3926 len--;
3928 done:
3929 free(abspath);
3930 free(resolved);
3931 free(cwd);
3932 if (err == NULL)
3933 *wt_path = path;
3934 else
3935 free(path);
3936 return err;
3939 struct schedule_addition_args {
3940 struct got_worktree *worktree;
3941 struct got_fileindex *fileindex;
3942 got_worktree_checkout_cb progress_cb;
3943 void *progress_arg;
3944 struct got_repository *repo;
3947 static const struct got_error *
3948 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3949 const char *relpath, struct got_object_id *blob_id,
3950 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3951 int dirfd, const char *de_name)
3953 struct schedule_addition_args *a = arg;
3954 const struct got_error *err = NULL;
3955 struct got_fileindex_entry *ie;
3956 struct stat sb;
3957 char *ondisk_path;
3959 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3960 relpath) == -1)
3961 return got_error_from_errno("asprintf");
3963 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3964 if (ie) {
3965 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3966 de_name, a->repo);
3967 if (err)
3968 goto done;
3969 /* Re-adding an existing entry is a no-op. */
3970 if (status == GOT_STATUS_ADD)
3971 goto done;
3972 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3973 if (err)
3974 goto done;
3977 if (status != GOT_STATUS_UNVERSIONED) {
3978 if (status == GOT_STATUS_NONEXISTENT)
3979 err = got_error_set_errno(ENOENT, ondisk_path);
3980 else
3981 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3982 goto done;
3985 err = got_fileindex_entry_alloc(&ie, relpath);
3986 if (err)
3987 goto done;
3988 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3989 relpath, NULL, NULL, 1);
3990 if (err) {
3991 got_fileindex_entry_free(ie);
3992 goto done;
3994 err = got_fileindex_entry_add(a->fileindex, ie);
3995 if (err) {
3996 got_fileindex_entry_free(ie);
3997 goto done;
3999 done:
4000 free(ondisk_path);
4001 if (err)
4002 return err;
4003 if (status == GOT_STATUS_ADD)
4004 return NULL;
4005 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4008 const struct got_error *
4009 got_worktree_schedule_add(struct got_worktree *worktree,
4010 struct got_pathlist_head *paths,
4011 got_worktree_checkout_cb progress_cb, void *progress_arg,
4012 struct got_repository *repo, int no_ignores)
4014 struct got_fileindex *fileindex = NULL;
4015 char *fileindex_path = NULL;
4016 const struct got_error *err = NULL, *sync_err, *unlockerr;
4017 struct got_pathlist_entry *pe;
4018 struct schedule_addition_args saa;
4020 err = lock_worktree(worktree, LOCK_EX);
4021 if (err)
4022 return err;
4024 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4025 if (err)
4026 goto done;
4028 saa.worktree = worktree;
4029 saa.fileindex = fileindex;
4030 saa.progress_cb = progress_cb;
4031 saa.progress_arg = progress_arg;
4032 saa.repo = repo;
4034 TAILQ_FOREACH(pe, paths, entry) {
4035 err = worktree_status(worktree, pe->path, fileindex, repo,
4036 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4037 if (err)
4038 break;
4040 sync_err = sync_fileindex(fileindex, fileindex_path);
4041 if (sync_err && err == NULL)
4042 err = sync_err;
4043 done:
4044 free(fileindex_path);
4045 if (fileindex)
4046 got_fileindex_free(fileindex);
4047 unlockerr = lock_worktree(worktree, LOCK_SH);
4048 if (unlockerr && err == NULL)
4049 err = unlockerr;
4050 return err;
4053 struct schedule_deletion_args {
4054 struct got_worktree *worktree;
4055 struct got_fileindex *fileindex;
4056 got_worktree_delete_cb progress_cb;
4057 void *progress_arg;
4058 struct got_repository *repo;
4059 int delete_local_mods;
4060 int keep_on_disk;
4061 int ignore_missing_paths;
4062 const char *status_codes;
4065 static const struct got_error *
4066 schedule_for_deletion(void *arg, unsigned char status,
4067 unsigned char staged_status, const char *relpath,
4068 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4069 struct got_object_id *commit_id, int dirfd, const char *de_name)
4071 struct schedule_deletion_args *a = arg;
4072 const struct got_error *err = NULL;
4073 struct got_fileindex_entry *ie = NULL;
4074 struct stat sb;
4075 char *ondisk_path;
4077 if (status == GOT_STATUS_NONEXISTENT) {
4078 if (a->ignore_missing_paths)
4079 return NULL;
4080 return got_error_set_errno(ENOENT, relpath);
4083 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4084 if (ie == NULL)
4085 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4087 staged_status = get_staged_status(ie);
4088 if (staged_status != GOT_STATUS_NO_CHANGE) {
4089 if (staged_status == GOT_STATUS_DELETE)
4090 return NULL;
4091 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4094 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4095 relpath) == -1)
4096 return got_error_from_errno("asprintf");
4098 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4099 a->repo);
4100 if (err)
4101 goto done;
4103 if (a->status_codes) {
4104 size_t ncodes = strlen(a->status_codes);
4105 int i;
4106 for (i = 0; i < ncodes ; i++) {
4107 if (status == a->status_codes[i])
4108 break;
4110 if (i == ncodes) {
4111 /* Do not delete files in non-matching status. */
4112 free(ondisk_path);
4113 return NULL;
4115 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4116 a->status_codes[i] != GOT_STATUS_MISSING) {
4117 static char msg[64];
4118 snprintf(msg, sizeof(msg),
4119 "invalid status code '%c'", a->status_codes[i]);
4120 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4121 goto done;
4125 if (status != GOT_STATUS_NO_CHANGE) {
4126 if (status == GOT_STATUS_DELETE)
4127 goto done;
4128 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4129 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4130 goto done;
4132 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4133 err = got_error_set_errno(ENOENT, relpath);
4134 goto done;
4136 if (status != GOT_STATUS_MODIFY &&
4137 status != GOT_STATUS_MISSING) {
4138 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4139 goto done;
4143 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4144 size_t root_len;
4146 if (dirfd != -1) {
4147 if (unlinkat(dirfd, de_name, 0) == -1) {
4148 err = got_error_from_errno2("unlinkat",
4149 ondisk_path);
4150 goto done;
4152 } else if (unlink(ondisk_path) == -1) {
4153 err = got_error_from_errno2("unlink", ondisk_path);
4154 goto done;
4157 root_len = strlen(a->worktree->root_path);
4158 do {
4159 char *parent;
4160 err = got_path_dirname(&parent, ondisk_path);
4161 if (err)
4162 goto done;
4163 free(ondisk_path);
4164 ondisk_path = parent;
4165 if (rmdir(ondisk_path) == -1) {
4166 if (errno != ENOTEMPTY)
4167 err = got_error_from_errno2("rmdir",
4168 ondisk_path);
4169 break;
4171 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4172 strlen(ondisk_path), root_len) != 0);
4175 got_fileindex_entry_mark_deleted_from_disk(ie);
4176 done:
4177 free(ondisk_path);
4178 if (err)
4179 return err;
4180 if (status == GOT_STATUS_DELETE)
4181 return NULL;
4182 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4183 staged_status, relpath);
4186 const struct got_error *
4187 got_worktree_schedule_delete(struct got_worktree *worktree,
4188 struct got_pathlist_head *paths, int delete_local_mods,
4189 const char *status_codes,
4190 got_worktree_delete_cb progress_cb, void *progress_arg,
4191 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4193 struct got_fileindex *fileindex = NULL;
4194 char *fileindex_path = NULL;
4195 const struct got_error *err = NULL, *sync_err, *unlockerr;
4196 struct got_pathlist_entry *pe;
4197 struct schedule_deletion_args sda;
4199 err = lock_worktree(worktree, LOCK_EX);
4200 if (err)
4201 return err;
4203 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4204 if (err)
4205 goto done;
4207 sda.worktree = worktree;
4208 sda.fileindex = fileindex;
4209 sda.progress_cb = progress_cb;
4210 sda.progress_arg = progress_arg;
4211 sda.repo = repo;
4212 sda.delete_local_mods = delete_local_mods;
4213 sda.keep_on_disk = keep_on_disk;
4214 sda.ignore_missing_paths = ignore_missing_paths;
4215 sda.status_codes = status_codes;
4217 TAILQ_FOREACH(pe, paths, entry) {
4218 err = worktree_status(worktree, pe->path, fileindex, repo,
4219 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4220 if (err)
4221 break;
4223 sync_err = sync_fileindex(fileindex, fileindex_path);
4224 if (sync_err && err == NULL)
4225 err = sync_err;
4226 done:
4227 free(fileindex_path);
4228 if (fileindex)
4229 got_fileindex_free(fileindex);
4230 unlockerr = lock_worktree(worktree, LOCK_SH);
4231 if (unlockerr && err == NULL)
4232 err = unlockerr;
4233 return err;
4236 static const struct got_error *
4237 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4239 const struct got_error *err = NULL;
4240 char *line = NULL;
4241 size_t linesize = 0, n;
4242 ssize_t linelen;
4244 linelen = getline(&line, &linesize, infile);
4245 if (linelen == -1) {
4246 if (ferror(infile)) {
4247 err = got_error_from_errno("getline");
4248 goto done;
4250 return NULL;
4252 if (outfile) {
4253 n = fwrite(line, 1, linelen, outfile);
4254 if (n != linelen) {
4255 err = got_ferror(outfile, GOT_ERR_IO);
4256 goto done;
4259 if (rejectfile) {
4260 n = fwrite(line, 1, linelen, rejectfile);
4261 if (n != linelen)
4262 err = got_ferror(rejectfile, GOT_ERR_IO);
4264 done:
4265 free(line);
4266 return err;
4269 static const struct got_error *
4270 skip_one_line(FILE *f)
4272 char *line = NULL;
4273 size_t linesize = 0;
4274 ssize_t linelen;
4276 linelen = getline(&line, &linesize, f);
4277 if (linelen == -1) {
4278 if (ferror(f))
4279 return got_error_from_errno("getline");
4280 return NULL;
4282 free(line);
4283 return NULL;
4286 static const struct got_error *
4287 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4288 int start_old, int end_old, int start_new, int end_new,
4289 FILE *outfile, FILE *rejectfile)
4291 const struct got_error *err;
4293 /* Copy old file's lines leading up to patch. */
4294 while (!feof(f1) && *line_cur1 < start_old) {
4295 err = copy_one_line(f1, outfile, NULL);
4296 if (err)
4297 return err;
4298 (*line_cur1)++;
4300 /* Skip new file's lines leading up to patch. */
4301 while (!feof(f2) && *line_cur2 < start_new) {
4302 if (rejectfile)
4303 err = copy_one_line(f2, NULL, rejectfile);
4304 else
4305 err = skip_one_line(f2);
4306 if (err)
4307 return err;
4308 (*line_cur2)++;
4310 /* Copy patched lines. */
4311 while (!feof(f2) && *line_cur2 <= end_new) {
4312 err = copy_one_line(f2, outfile, NULL);
4313 if (err)
4314 return err;
4315 (*line_cur2)++;
4317 /* Skip over old file's replaced lines. */
4318 while (!feof(f1) && *line_cur1 <= end_old) {
4319 if (rejectfile)
4320 err = copy_one_line(f1, NULL, rejectfile);
4321 else
4322 err = skip_one_line(f1);
4323 if (err)
4324 return err;
4325 (*line_cur1)++;
4328 return NULL;
4331 static const struct got_error *
4332 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4333 FILE *outfile, FILE *rejectfile)
4335 const struct got_error *err;
4337 if (outfile) {
4338 /* Copy old file's lines until EOF. */
4339 while (!feof(f1)) {
4340 err = copy_one_line(f1, outfile, NULL);
4341 if (err)
4342 return err;
4343 (*line_cur1)++;
4346 if (rejectfile) {
4347 /* Copy new file's lines until EOF. */
4348 while (!feof(f2)) {
4349 err = copy_one_line(f2, NULL, rejectfile);
4350 if (err)
4351 return err;
4352 (*line_cur2)++;
4356 return NULL;
4359 static const struct got_error *
4360 apply_or_reject_change(int *choice, int *nchunks_used,
4361 struct diff_result *diff_result, int n,
4362 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4363 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4364 got_worktree_patch_cb patch_cb, void *patch_arg)
4366 const struct got_error *err = NULL;
4367 struct diff_chunk_context cc = {};
4368 int start_old, end_old, start_new, end_new;
4369 FILE *hunkfile;
4370 struct diff_output_unidiff_state *diff_state;
4371 struct diff_input_info diff_info;
4372 int rc;
4374 *choice = GOT_PATCH_CHOICE_NONE;
4376 /* Get changed line numbers without context lines for copy_change(). */
4377 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4378 start_old = cc.left.start;
4379 end_old = cc.left.end;
4380 start_new = cc.right.start;
4381 end_new = cc.right.end;
4383 /* Get the same change with context lines for display. */
4384 memset(&cc, 0, sizeof(cc));
4385 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4387 memset(&diff_info, 0, sizeof(diff_info));
4388 diff_info.left_path = relpath;
4389 diff_info.right_path = relpath;
4391 diff_state = diff_output_unidiff_state_alloc();
4392 if (diff_state == NULL)
4393 return got_error_set_errno(ENOMEM,
4394 "diff_output_unidiff_state_alloc");
4396 hunkfile = got_opentemp();
4397 if (hunkfile == NULL) {
4398 err = got_error_from_errno("got_opentemp");
4399 goto done;
4402 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4403 diff_result, &cc);
4404 if (rc != DIFF_RC_OK) {
4405 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4406 goto done;
4409 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4410 err = got_ferror(hunkfile, GOT_ERR_IO);
4411 goto done;
4414 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4415 hunkfile, changeno, nchanges);
4416 if (err)
4417 goto done;
4419 switch (*choice) {
4420 case GOT_PATCH_CHOICE_YES:
4421 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4422 end_old, start_new, end_new, outfile, rejectfile);
4423 break;
4424 case GOT_PATCH_CHOICE_NO:
4425 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4426 end_old, start_new, end_new, rejectfile, outfile);
4427 break;
4428 case GOT_PATCH_CHOICE_QUIT:
4429 break;
4430 default:
4431 err = got_error(GOT_ERR_PATCH_CHOICE);
4432 break;
4434 done:
4435 diff_output_unidiff_state_free(diff_state);
4436 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4437 err = got_error_from_errno("fclose");
4438 return err;
4441 struct revert_file_args {
4442 struct got_worktree *worktree;
4443 struct got_fileindex *fileindex;
4444 got_worktree_checkout_cb progress_cb;
4445 void *progress_arg;
4446 got_worktree_patch_cb patch_cb;
4447 void *patch_arg;
4448 struct got_repository *repo;
4449 int unlink_added_files;
4452 static const struct got_error *
4453 create_patched_content(char **path_outfile, int reverse_patch,
4454 struct got_object_id *blob_id, const char *path2,
4455 int dirfd2, const char *de_name2,
4456 const char *relpath, struct got_repository *repo,
4457 got_worktree_patch_cb patch_cb, void *patch_arg)
4459 const struct got_error *err, *free_err;
4460 struct got_blob_object *blob = NULL;
4461 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4462 int fd = -1, fd2 = -1;
4463 char link_target[PATH_MAX];
4464 ssize_t link_len = 0;
4465 char *path1 = NULL, *id_str = NULL;
4466 struct stat sb2;
4467 struct got_diffreg_result *diffreg_result = NULL;
4468 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4469 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4471 *path_outfile = NULL;
4473 err = got_object_id_str(&id_str, blob_id);
4474 if (err)
4475 return err;
4477 if (dirfd2 != -1) {
4478 fd2 = openat(dirfd2, de_name2,
4479 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4480 if (fd2 == -1) {
4481 if (!got_err_open_nofollow_on_symlink()) {
4482 err = got_error_from_errno2("openat", path2);
4483 goto done;
4485 link_len = readlinkat(dirfd2, de_name2,
4486 link_target, sizeof(link_target));
4487 if (link_len == -1) {
4488 return got_error_from_errno2("readlinkat",
4489 path2);
4491 sb2.st_mode = S_IFLNK;
4492 sb2.st_size = link_len;
4494 } else {
4495 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4496 if (fd2 == -1) {
4497 if (!got_err_open_nofollow_on_symlink()) {
4498 err = got_error_from_errno2("open", path2);
4499 goto done;
4501 link_len = readlink(path2, link_target,
4502 sizeof(link_target));
4503 if (link_len == -1)
4504 return got_error_from_errno2("readlink", path2);
4505 sb2.st_mode = S_IFLNK;
4506 sb2.st_size = link_len;
4509 if (fd2 != -1) {
4510 if (fstat(fd2, &sb2) == -1) {
4511 err = got_error_from_errno2("fstat", path2);
4512 goto done;
4515 f2 = fdopen(fd2, "r");
4516 if (f2 == NULL) {
4517 err = got_error_from_errno2("fdopen", path2);
4518 goto done;
4520 fd2 = -1;
4521 } else {
4522 size_t n;
4523 f2 = got_opentemp();
4524 if (f2 == NULL) {
4525 err = got_error_from_errno2("got_opentemp", path2);
4526 goto done;
4528 n = fwrite(link_target, 1, link_len, f2);
4529 if (n != link_len) {
4530 err = got_ferror(f2, GOT_ERR_IO);
4531 goto done;
4533 if (fflush(f2) == EOF) {
4534 err = got_error_from_errno("fflush");
4535 goto done;
4537 rewind(f2);
4540 fd = got_opentempfd();
4541 if (fd == -1) {
4542 err = got_error_from_errno("got_opentempfd");
4543 goto done;
4546 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4547 if (err)
4548 goto done;
4550 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4551 if (err)
4552 goto done;
4554 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4555 if (err)
4556 goto done;
4558 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4559 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4560 if (err)
4561 goto done;
4563 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4564 "");
4565 if (err)
4566 goto done;
4568 if (fseek(f1, 0L, SEEK_SET) == -1)
4569 return got_ferror(f1, GOT_ERR_IO);
4570 if (fseek(f2, 0L, SEEK_SET) == -1)
4571 return got_ferror(f2, GOT_ERR_IO);
4573 /* Count the number of actual changes in the diff result. */
4574 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4575 struct diff_chunk_context cc = {};
4576 diff_chunk_context_load_change(&cc, &nchunks_used,
4577 diffreg_result->result, n, 0);
4578 nchanges++;
4580 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4581 int choice;
4582 err = apply_or_reject_change(&choice, &nchunks_used,
4583 diffreg_result->result, n, relpath, f1, f2,
4584 &line_cur1, &line_cur2,
4585 reverse_patch ? NULL : outfile,
4586 reverse_patch ? outfile : NULL,
4587 ++i, nchanges, patch_cb, patch_arg);
4588 if (err)
4589 goto done;
4590 if (choice == GOT_PATCH_CHOICE_YES)
4591 have_content = 1;
4592 else if (choice == GOT_PATCH_CHOICE_QUIT)
4593 break;
4595 if (have_content) {
4596 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4597 reverse_patch ? NULL : outfile,
4598 reverse_patch ? outfile : NULL);
4599 if (err)
4600 goto done;
4602 if (!S_ISLNK(sb2.st_mode)) {
4603 mode_t mode;
4605 mode = apply_umask(sb2.st_mode);
4606 if (fchmod(fileno(outfile), mode) == -1) {
4607 err = got_error_from_errno2("fchmod", path2);
4608 goto done;
4612 done:
4613 free(id_str);
4614 if (fd != -1 && close(fd) == -1 && err == NULL)
4615 err = got_error_from_errno("close");
4616 if (blob)
4617 got_object_blob_close(blob);
4618 free_err = got_diffreg_result_free(diffreg_result);
4619 if (err == NULL)
4620 err = free_err;
4621 if (f1 && fclose(f1) == EOF && err == NULL)
4622 err = got_error_from_errno2("fclose", path1);
4623 if (f2 && fclose(f2) == EOF && err == NULL)
4624 err = got_error_from_errno2("fclose", path2);
4625 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4626 err = got_error_from_errno2("close", path2);
4627 if (outfile && fclose(outfile) == EOF && err == NULL)
4628 err = got_error_from_errno2("fclose", *path_outfile);
4629 if (path1 && unlink(path1) == -1 && err == NULL)
4630 err = got_error_from_errno2("unlink", path1);
4631 if (err || !have_content) {
4632 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4633 err = got_error_from_errno2("unlink", *path_outfile);
4634 free(*path_outfile);
4635 *path_outfile = NULL;
4637 free(path1);
4638 return err;
4641 static const struct got_error *
4642 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4643 const char *relpath, struct got_object_id *blob_id,
4644 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4645 int dirfd, const char *de_name)
4647 struct revert_file_args *a = arg;
4648 const struct got_error *err = NULL;
4649 char *parent_path = NULL;
4650 struct got_fileindex_entry *ie;
4651 struct got_commit_object *base_commit = NULL;
4652 struct got_tree_object *tree = NULL;
4653 struct got_object_id *tree_id = NULL;
4654 const struct got_tree_entry *te = NULL;
4655 char *tree_path = NULL, *te_name;
4656 char *ondisk_path = NULL, *path_content = NULL;
4657 struct got_blob_object *blob = NULL;
4658 int fd = -1;
4660 /* Reverting a staged deletion is a no-op. */
4661 if (status == GOT_STATUS_DELETE &&
4662 staged_status != GOT_STATUS_NO_CHANGE)
4663 return NULL;
4665 if (status == GOT_STATUS_UNVERSIONED)
4666 return (*a->progress_cb)(a->progress_arg,
4667 GOT_STATUS_UNVERSIONED, relpath);
4669 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4670 if (ie == NULL)
4671 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4673 /* Construct in-repository path of tree which contains this blob. */
4674 err = got_path_dirname(&parent_path, ie->path);
4675 if (err) {
4676 if (err->code != GOT_ERR_BAD_PATH)
4677 goto done;
4678 parent_path = strdup("/");
4679 if (parent_path == NULL) {
4680 err = got_error_from_errno("strdup");
4681 goto done;
4684 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4685 tree_path = strdup(parent_path);
4686 if (tree_path == NULL) {
4687 err = got_error_from_errno("strdup");
4688 goto done;
4690 } else {
4691 if (got_path_is_root_dir(parent_path)) {
4692 tree_path = strdup(a->worktree->path_prefix);
4693 if (tree_path == NULL) {
4694 err = got_error_from_errno("strdup");
4695 goto done;
4697 } else {
4698 if (asprintf(&tree_path, "%s/%s",
4699 a->worktree->path_prefix, parent_path) == -1) {
4700 err = got_error_from_errno("asprintf");
4701 goto done;
4706 err = got_object_open_as_commit(&base_commit, a->repo,
4707 a->worktree->base_commit_id);
4708 if (err)
4709 goto done;
4711 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4712 if (err) {
4713 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4714 (status == GOT_STATUS_ADD ||
4715 staged_status == GOT_STATUS_ADD)))
4716 goto done;
4717 } else {
4718 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4719 if (err)
4720 goto done;
4722 err = got_path_basename(&te_name, ie->path);
4723 if (err)
4724 goto done;
4726 te = got_object_tree_find_entry(tree, te_name);
4727 free(te_name);
4728 if (te == NULL && status != GOT_STATUS_ADD &&
4729 staged_status != GOT_STATUS_ADD) {
4730 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4731 goto done;
4735 switch (status) {
4736 case GOT_STATUS_ADD:
4737 if (a->patch_cb) {
4738 int choice = GOT_PATCH_CHOICE_NONE;
4739 err = (*a->patch_cb)(&choice, a->patch_arg,
4740 status, ie->path, NULL, 1, 1);
4741 if (err)
4742 goto done;
4743 if (choice != GOT_PATCH_CHOICE_YES)
4744 break;
4746 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4747 ie->path);
4748 if (err)
4749 goto done;
4750 got_fileindex_entry_remove(a->fileindex, ie);
4751 if (a->unlink_added_files) {
4752 if (asprintf(&ondisk_path, "%s/%s",
4753 got_worktree_get_root_path(a->worktree),
4754 relpath) == -1) {
4755 err = got_error_from_errno("asprintf");
4756 goto done;
4758 if (unlink(ondisk_path) == -1) {
4759 err = got_error_from_errno2("unlink",
4760 ondisk_path);
4761 break;
4764 break;
4765 case GOT_STATUS_DELETE:
4766 if (a->patch_cb) {
4767 int choice = GOT_PATCH_CHOICE_NONE;
4768 err = (*a->patch_cb)(&choice, a->patch_arg,
4769 status, ie->path, NULL, 1, 1);
4770 if (err)
4771 goto done;
4772 if (choice != GOT_PATCH_CHOICE_YES)
4773 break;
4775 /* fall through */
4776 case GOT_STATUS_MODIFY:
4777 case GOT_STATUS_MODE_CHANGE:
4778 case GOT_STATUS_CONFLICT:
4779 case GOT_STATUS_MISSING: {
4780 struct got_object_id id;
4781 if (staged_status == GOT_STATUS_ADD ||
4782 staged_status == GOT_STATUS_MODIFY)
4783 got_fileindex_entry_get_staged_blob_id(&id, ie);
4784 else
4785 got_fileindex_entry_get_blob_id(&id, ie);
4786 fd = got_opentempfd();
4787 if (fd == -1) {
4788 err = got_error_from_errno("got_opentempfd");
4789 goto done;
4792 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4793 if (err)
4794 goto done;
4796 if (asprintf(&ondisk_path, "%s/%s",
4797 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4798 err = got_error_from_errno("asprintf");
4799 goto done;
4802 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4803 status == GOT_STATUS_CONFLICT)) {
4804 int is_bad_symlink = 0;
4805 err = create_patched_content(&path_content, 1, &id,
4806 ondisk_path, dirfd, de_name, ie->path, a->repo,
4807 a->patch_cb, a->patch_arg);
4808 if (err || path_content == NULL)
4809 break;
4810 if (te && S_ISLNK(te->mode)) {
4811 if (unlink(path_content) == -1) {
4812 err = got_error_from_errno2("unlink",
4813 path_content);
4814 break;
4816 err = install_symlink(&is_bad_symlink,
4817 a->worktree, ondisk_path, ie->path,
4818 blob, 0, 1, 0, 0, a->repo,
4819 a->progress_cb, a->progress_arg);
4820 } else {
4821 if (rename(path_content, ondisk_path) == -1) {
4822 err = got_error_from_errno3("rename",
4823 path_content, ondisk_path);
4824 goto done;
4827 } else {
4828 int is_bad_symlink = 0;
4829 if (te && S_ISLNK(te->mode)) {
4830 err = install_symlink(&is_bad_symlink,
4831 a->worktree, ondisk_path, ie->path,
4832 blob, 0, 1, 0, 0, a->repo,
4833 a->progress_cb, a->progress_arg);
4834 } else {
4835 err = install_blob(a->worktree, ondisk_path,
4836 ie->path,
4837 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4838 got_fileindex_perms_to_st(ie), blob,
4839 0, 1, 0, 0, a->repo,
4840 a->progress_cb, a->progress_arg);
4842 if (err)
4843 goto done;
4844 if (status == GOT_STATUS_DELETE ||
4845 status == GOT_STATUS_MODE_CHANGE) {
4846 err = got_fileindex_entry_update(ie,
4847 a->worktree->root_fd, relpath,
4848 blob->id.sha1,
4849 a->worktree->base_commit_id->sha1, 1);
4850 if (err)
4851 goto done;
4853 if (is_bad_symlink) {
4854 got_fileindex_entry_filetype_set(ie,
4855 GOT_FILEIDX_MODE_BAD_SYMLINK);
4858 break;
4860 default:
4861 break;
4863 done:
4864 free(ondisk_path);
4865 free(path_content);
4866 free(parent_path);
4867 free(tree_path);
4868 if (fd != -1 && close(fd) == -1 && err == NULL)
4869 err = got_error_from_errno("close");
4870 if (blob)
4871 got_object_blob_close(blob);
4872 if (tree)
4873 got_object_tree_close(tree);
4874 free(tree_id);
4875 if (base_commit)
4876 got_object_commit_close(base_commit);
4877 return err;
4880 const struct got_error *
4881 got_worktree_revert(struct got_worktree *worktree,
4882 struct got_pathlist_head *paths,
4883 got_worktree_checkout_cb progress_cb, void *progress_arg,
4884 got_worktree_patch_cb patch_cb, void *patch_arg,
4885 struct got_repository *repo)
4887 struct got_fileindex *fileindex = NULL;
4888 char *fileindex_path = NULL;
4889 const struct got_error *err = NULL, *unlockerr = NULL;
4890 const struct got_error *sync_err = NULL;
4891 struct got_pathlist_entry *pe;
4892 struct revert_file_args rfa;
4894 err = lock_worktree(worktree, LOCK_EX);
4895 if (err)
4896 return err;
4898 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4899 if (err)
4900 goto done;
4902 rfa.worktree = worktree;
4903 rfa.fileindex = fileindex;
4904 rfa.progress_cb = progress_cb;
4905 rfa.progress_arg = progress_arg;
4906 rfa.patch_cb = patch_cb;
4907 rfa.patch_arg = patch_arg;
4908 rfa.repo = repo;
4909 rfa.unlink_added_files = 0;
4910 TAILQ_FOREACH(pe, paths, entry) {
4911 err = worktree_status(worktree, pe->path, fileindex, repo,
4912 revert_file, &rfa, NULL, NULL, 1, 0);
4913 if (err)
4914 break;
4916 sync_err = sync_fileindex(fileindex, fileindex_path);
4917 if (sync_err && err == NULL)
4918 err = sync_err;
4919 done:
4920 free(fileindex_path);
4921 if (fileindex)
4922 got_fileindex_free(fileindex);
4923 unlockerr = lock_worktree(worktree, LOCK_SH);
4924 if (unlockerr && err == NULL)
4925 err = unlockerr;
4926 return err;
4929 static void
4930 free_commitable(struct got_commitable *ct)
4932 free(ct->path);
4933 free(ct->in_repo_path);
4934 free(ct->ondisk_path);
4935 free(ct->blob_id);
4936 free(ct->base_blob_id);
4937 free(ct->staged_blob_id);
4938 free(ct->base_commit_id);
4939 free(ct);
4942 struct collect_commitables_arg {
4943 struct got_pathlist_head *commitable_paths;
4944 struct got_repository *repo;
4945 struct got_worktree *worktree;
4946 struct got_fileindex *fileindex;
4947 int have_staged_files;
4948 int allow_bad_symlinks;
4949 int diff_header_shown;
4950 FILE *diff_outfile;
4951 FILE *f1;
4952 FILE *f2;
4956 * Create a file which contains the target path of a symlink so we can feed
4957 * it as content to the diff engine.
4959 static const struct got_error *
4960 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4961 const char *abspath)
4963 const struct got_error *err = NULL;
4964 char target_path[PATH_MAX];
4965 ssize_t target_len, outlen;
4967 *fd = -1;
4969 if (dirfd != -1) {
4970 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4971 if (target_len == -1)
4972 return got_error_from_errno2("readlinkat", abspath);
4973 } else {
4974 target_len = readlink(abspath, target_path, PATH_MAX);
4975 if (target_len == -1)
4976 return got_error_from_errno2("readlink", abspath);
4979 *fd = got_opentempfd();
4980 if (*fd == -1)
4981 return got_error_from_errno("got_opentempfd");
4983 outlen = write(*fd, target_path, target_len);
4984 if (outlen == -1) {
4985 err = got_error_from_errno("got_opentempfd");
4986 goto done;
4989 if (lseek(*fd, 0, SEEK_SET) == -1) {
4990 err = got_error_from_errno2("lseek", abspath);
4991 goto done;
4993 done:
4994 if (err) {
4995 close(*fd);
4996 *fd = -1;
4998 return err;
5001 static const struct got_error *
5002 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5003 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5004 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5006 const struct got_error *err = NULL;
5007 struct got_blob_object *blob1 = NULL;
5008 int fd = -1, fd1 = -1, fd2 = -1;
5009 FILE *ondisk_file = NULL;
5010 char *label1 = NULL;
5011 struct stat sb;
5012 off_t size1 = 0;
5013 int f2_exists = 0;
5014 char *id_str = NULL;
5016 memset(&sb, 0, sizeof(sb));
5018 if (diff_staged) {
5019 if (ct->staged_status != GOT_STATUS_MODIFY &&
5020 ct->staged_status != GOT_STATUS_ADD &&
5021 ct->staged_status != GOT_STATUS_DELETE)
5022 return NULL;
5023 } else {
5024 if (ct->status != GOT_STATUS_MODIFY &&
5025 ct->status != GOT_STATUS_ADD &&
5026 ct->status != GOT_STATUS_DELETE)
5027 return NULL;
5030 err = got_opentemp_truncate(f1);
5031 if (err)
5032 return got_error_from_errno("got_opentemp_truncate");
5033 err = got_opentemp_truncate(f2);
5034 if (err)
5035 return got_error_from_errno("got_opentemp_truncate");
5037 if (!*diff_header_shown) {
5038 err = got_object_id_str(&id_str, worktree->base_commit_id);
5039 if (err)
5040 return err;
5041 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5042 got_worktree_get_root_path(worktree));
5043 fprintf(diff_outfile, "commit - %s\n", id_str);
5044 fprintf(diff_outfile, "path + %s%s\n",
5045 got_worktree_get_root_path(worktree),
5046 diff_staged ? " (staged changes)" : "");
5047 *diff_header_shown = 1;
5050 if (diff_staged) {
5051 const char *label1 = NULL, *label2 = NULL;
5052 switch (ct->staged_status) {
5053 case GOT_STATUS_MODIFY:
5054 label1 = ct->path;
5055 label2 = ct->path;
5056 break;
5057 case GOT_STATUS_ADD:
5058 label2 = ct->path;
5059 break;
5060 case GOT_STATUS_DELETE:
5061 label1 = ct->path;
5062 break;
5063 default:
5064 return got_error(GOT_ERR_FILE_STATUS);
5066 fd1 = got_opentempfd();
5067 if (fd1 == -1) {
5068 err = got_error_from_errno("got_opentempfd");
5069 goto done;
5071 fd2 = got_opentempfd();
5072 if (fd2 == -1) {
5073 err = got_error_from_errno("got_opentempfd");
5074 goto done;
5076 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5077 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5078 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5079 NULL, repo, diff_outfile);
5080 goto done;
5083 fd1 = got_opentempfd();
5084 if (fd1 == -1) {
5085 err = got_error_from_errno("got_opentempfd");
5086 goto done;
5089 if (ct->status != GOT_STATUS_ADD) {
5090 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5091 8192, fd1);
5092 if (err)
5093 goto done;
5096 if (ct->status != GOT_STATUS_DELETE) {
5097 if (dirfd != -1) {
5098 fd = openat(dirfd, de_name,
5099 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5100 if (fd == -1) {
5101 if (!got_err_open_nofollow_on_symlink()) {
5102 err = got_error_from_errno2("openat",
5103 ct->ondisk_path);
5104 goto done;
5106 err = get_symlink_target_file(&fd, dirfd,
5107 de_name, ct->ondisk_path);
5108 if (err)
5109 goto done;
5111 } else {
5112 fd = open(ct->ondisk_path,
5113 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5114 if (fd == -1) {
5115 if (!got_err_open_nofollow_on_symlink()) {
5116 err = got_error_from_errno2("open",
5117 ct->ondisk_path);
5118 goto done;
5120 err = get_symlink_target_file(&fd, dirfd,
5121 de_name, ct->ondisk_path);
5122 if (err)
5123 goto done;
5126 if (fstatat(fd, ct->ondisk_path, &sb,
5127 AT_SYMLINK_NOFOLLOW) == -1) {
5128 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5129 goto done;
5131 ondisk_file = fdopen(fd, "r");
5132 if (ondisk_file == NULL) {
5133 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5134 goto done;
5136 fd = -1;
5137 f2_exists = 1;
5140 if (blob1) {
5141 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5142 f1, blob1);
5143 if (err)
5144 goto done;
5147 err = got_diff_blob_file(blob1, f1, size1, label1,
5148 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5149 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5150 done:
5151 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5152 err = got_error_from_errno("close");
5153 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5154 err = got_error_from_errno("close");
5155 if (blob1)
5156 got_object_blob_close(blob1);
5157 if (fd != -1 && close(fd) == -1 && err == NULL)
5158 err = got_error_from_errno("close");
5159 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5160 err = got_error_from_errno("fclose");
5161 return err;
5164 static const struct got_error *
5165 collect_commitables(void *arg, unsigned char status,
5166 unsigned char staged_status, const char *relpath,
5167 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5168 struct got_object_id *commit_id, int dirfd, const char *de_name)
5170 struct collect_commitables_arg *a = arg;
5171 const struct got_error *err = NULL;
5172 struct got_commitable *ct = NULL;
5173 struct got_pathlist_entry *new = NULL;
5174 char *parent_path = NULL, *path = NULL;
5175 struct stat sb;
5177 if (a->have_staged_files) {
5178 if (staged_status != GOT_STATUS_MODIFY &&
5179 staged_status != GOT_STATUS_ADD &&
5180 staged_status != GOT_STATUS_DELETE)
5181 return NULL;
5182 } else {
5183 if (status == GOT_STATUS_CONFLICT)
5184 return got_error(GOT_ERR_COMMIT_CONFLICT);
5186 if (status != GOT_STATUS_MODIFY &&
5187 status != GOT_STATUS_MODE_CHANGE &&
5188 status != GOT_STATUS_ADD &&
5189 status != GOT_STATUS_DELETE)
5190 return NULL;
5193 if (asprintf(&path, "/%s", relpath) == -1) {
5194 err = got_error_from_errno("asprintf");
5195 goto done;
5197 if (strcmp(path, "/") == 0) {
5198 parent_path = strdup("");
5199 if (parent_path == NULL)
5200 return got_error_from_errno("strdup");
5201 } else {
5202 err = got_path_dirname(&parent_path, path);
5203 if (err)
5204 return err;
5207 ct = calloc(1, sizeof(*ct));
5208 if (ct == NULL) {
5209 err = got_error_from_errno("calloc");
5210 goto done;
5213 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5214 relpath) == -1) {
5215 err = got_error_from_errno("asprintf");
5216 goto done;
5219 if (staged_status == GOT_STATUS_ADD ||
5220 staged_status == GOT_STATUS_MODIFY) {
5221 struct got_fileindex_entry *ie;
5222 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5223 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5224 case GOT_FILEIDX_MODE_REGULAR_FILE:
5225 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5226 ct->mode = S_IFREG;
5227 break;
5228 case GOT_FILEIDX_MODE_SYMLINK:
5229 ct->mode = S_IFLNK;
5230 break;
5231 default:
5232 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5233 goto done;
5235 ct->mode |= got_fileindex_entry_perms_get(ie);
5236 } else if (status != GOT_STATUS_DELETE &&
5237 staged_status != GOT_STATUS_DELETE) {
5238 if (dirfd != -1) {
5239 if (fstatat(dirfd, de_name, &sb,
5240 AT_SYMLINK_NOFOLLOW) == -1) {
5241 err = got_error_from_errno2("fstatat",
5242 ct->ondisk_path);
5243 goto done;
5245 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5246 err = got_error_from_errno2("lstat", ct->ondisk_path);
5247 goto done;
5249 ct->mode = sb.st_mode;
5252 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5253 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5254 relpath) == -1) {
5255 err = got_error_from_errno("asprintf");
5256 goto done;
5259 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5260 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5261 int is_bad_symlink;
5262 char target_path[PATH_MAX];
5263 ssize_t target_len;
5264 target_len = readlink(ct->ondisk_path, target_path,
5265 sizeof(target_path));
5266 if (target_len == -1) {
5267 err = got_error_from_errno2("readlink",
5268 ct->ondisk_path);
5269 goto done;
5271 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5272 target_len, ct->ondisk_path, a->worktree->root_path);
5273 if (err)
5274 goto done;
5275 if (is_bad_symlink) {
5276 err = got_error_path(ct->ondisk_path,
5277 GOT_ERR_BAD_SYMLINK);
5278 goto done;
5283 ct->status = status;
5284 ct->staged_status = staged_status;
5285 ct->blob_id = NULL; /* will be filled in when blob gets created */
5286 if (ct->status != GOT_STATUS_ADD &&
5287 ct->staged_status != GOT_STATUS_ADD) {
5288 ct->base_blob_id = got_object_id_dup(blob_id);
5289 if (ct->base_blob_id == NULL) {
5290 err = got_error_from_errno("got_object_id_dup");
5291 goto done;
5293 ct->base_commit_id = got_object_id_dup(commit_id);
5294 if (ct->base_commit_id == NULL) {
5295 err = got_error_from_errno("got_object_id_dup");
5296 goto done;
5299 if (ct->staged_status == GOT_STATUS_ADD ||
5300 ct->staged_status == GOT_STATUS_MODIFY) {
5301 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5302 if (ct->staged_blob_id == NULL) {
5303 err = got_error_from_errno("got_object_id_dup");
5304 goto done;
5307 ct->path = strdup(path);
5308 if (ct->path == NULL) {
5309 err = got_error_from_errno("strdup");
5310 goto done;
5312 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5313 if (err)
5314 goto done;
5316 if (a->diff_outfile && ct && new != NULL) {
5317 err = append_ct_diff(ct, &a->diff_header_shown,
5318 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5319 a->have_staged_files, a->repo, a->worktree);
5320 if (err)
5321 goto done;
5323 done:
5324 if (ct && (err || new == NULL))
5325 free_commitable(ct);
5326 free(parent_path);
5327 free(path);
5328 return err;
5331 static const struct got_error *write_tree(struct got_object_id **, int *,
5332 struct got_tree_object *, const char *, struct got_pathlist_head *,
5333 got_worktree_status_cb status_cb, void *status_arg,
5334 struct got_repository *);
5336 static const struct got_error *
5337 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5338 struct got_tree_entry *te, const char *parent_path,
5339 struct got_pathlist_head *commitable_paths,
5340 got_worktree_status_cb status_cb, void *status_arg,
5341 struct got_repository *repo)
5343 const struct got_error *err = NULL;
5344 struct got_tree_object *subtree;
5345 char *subpath;
5347 if (asprintf(&subpath, "%s%s%s", parent_path,
5348 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5349 return got_error_from_errno("asprintf");
5351 err = got_object_open_as_tree(&subtree, repo, &te->id);
5352 if (err)
5353 return err;
5355 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5356 commitable_paths, status_cb, status_arg, repo);
5357 got_object_tree_close(subtree);
5358 free(subpath);
5359 return err;
5362 static const struct got_error *
5363 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5365 const struct got_error *err = NULL;
5366 char *ct_parent_path = NULL;
5368 *match = 0;
5370 if (strchr(ct->in_repo_path, '/') == NULL) {
5371 *match = got_path_is_root_dir(path);
5372 return NULL;
5375 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5376 if (err)
5377 return err;
5378 *match = (strcmp(path, ct_parent_path) == 0);
5379 free(ct_parent_path);
5380 return err;
5383 static mode_t
5384 get_ct_file_mode(struct got_commitable *ct)
5386 if (S_ISLNK(ct->mode))
5387 return S_IFLNK;
5389 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5392 static const struct got_error *
5393 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5394 struct got_tree_entry *te, struct got_commitable *ct)
5396 const struct got_error *err = NULL;
5398 *new_te = NULL;
5400 err = got_object_tree_entry_dup(new_te, te);
5401 if (err)
5402 goto done;
5404 (*new_te)->mode = get_ct_file_mode(ct);
5406 if (ct->staged_status == GOT_STATUS_MODIFY)
5407 memcpy(&(*new_te)->id, ct->staged_blob_id,
5408 sizeof((*new_te)->id));
5409 else
5410 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5411 done:
5412 if (err && *new_te) {
5413 free(*new_te);
5414 *new_te = NULL;
5416 return err;
5419 static const struct got_error *
5420 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5421 struct got_commitable *ct)
5423 const struct got_error *err = NULL;
5424 char *ct_name = NULL;
5426 *new_te = NULL;
5428 *new_te = calloc(1, sizeof(**new_te));
5429 if (*new_te == NULL)
5430 return got_error_from_errno("calloc");
5432 err = got_path_basename(&ct_name, ct->path);
5433 if (err)
5434 goto done;
5435 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5436 sizeof((*new_te)->name)) {
5437 err = got_error(GOT_ERR_NO_SPACE);
5438 goto done;
5441 (*new_te)->mode = get_ct_file_mode(ct);
5443 if (ct->staged_status == GOT_STATUS_ADD)
5444 memcpy(&(*new_te)->id, ct->staged_blob_id,
5445 sizeof((*new_te)->id));
5446 else
5447 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5448 done:
5449 free(ct_name);
5450 if (err && *new_te) {
5451 free(*new_te);
5452 *new_te = NULL;
5454 return err;
5457 static const struct got_error *
5458 insert_tree_entry(struct got_tree_entry *new_te,
5459 struct got_pathlist_head *paths)
5461 const struct got_error *err = NULL;
5462 struct got_pathlist_entry *new_pe;
5464 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5465 if (err)
5466 return err;
5467 if (new_pe == NULL)
5468 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5469 return NULL;
5472 static const struct got_error *
5473 report_ct_status(struct got_commitable *ct,
5474 got_worktree_status_cb status_cb, void *status_arg)
5476 const char *ct_path = ct->path;
5477 unsigned char status;
5479 if (status_cb == NULL) /* no commit progress output desired */
5480 return NULL;
5482 while (ct_path[0] == '/')
5483 ct_path++;
5485 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5486 status = ct->staged_status;
5487 else
5488 status = ct->status;
5490 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5491 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5494 static const struct got_error *
5495 match_modified_subtree(int *modified, struct got_tree_entry *te,
5496 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5498 const struct got_error *err = NULL;
5499 struct got_pathlist_entry *pe;
5500 char *te_path;
5502 *modified = 0;
5504 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5505 got_path_is_root_dir(base_tree_path) ? "" : "/",
5506 te->name) == -1)
5507 return got_error_from_errno("asprintf");
5509 TAILQ_FOREACH(pe, commitable_paths, entry) {
5510 struct got_commitable *ct = pe->data;
5511 *modified = got_path_is_child(ct->in_repo_path, te_path,
5512 strlen(te_path));
5513 if (*modified)
5514 break;
5517 free(te_path);
5518 return err;
5521 static const struct got_error *
5522 match_deleted_or_modified_ct(struct got_commitable **ctp,
5523 struct got_tree_entry *te, const char *base_tree_path,
5524 struct got_pathlist_head *commitable_paths)
5526 const struct got_error *err = NULL;
5527 struct got_pathlist_entry *pe;
5529 *ctp = NULL;
5531 TAILQ_FOREACH(pe, commitable_paths, entry) {
5532 struct got_commitable *ct = pe->data;
5533 char *ct_name = NULL;
5534 int path_matches;
5536 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5537 if (ct->status != GOT_STATUS_MODIFY &&
5538 ct->status != GOT_STATUS_MODE_CHANGE &&
5539 ct->status != GOT_STATUS_DELETE)
5540 continue;
5541 } else {
5542 if (ct->staged_status != GOT_STATUS_MODIFY &&
5543 ct->staged_status != GOT_STATUS_DELETE)
5544 continue;
5547 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5548 continue;
5550 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5551 if (err)
5552 return err;
5553 if (!path_matches)
5554 continue;
5556 err = got_path_basename(&ct_name, pe->path);
5557 if (err)
5558 return err;
5560 if (strcmp(te->name, ct_name) != 0) {
5561 free(ct_name);
5562 continue;
5564 free(ct_name);
5566 *ctp = ct;
5567 break;
5570 return err;
5573 static const struct got_error *
5574 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5575 const char *child_path, const char *path_base_tree,
5576 struct got_pathlist_head *commitable_paths,
5577 got_worktree_status_cb status_cb, void *status_arg,
5578 struct got_repository *repo)
5580 const struct got_error *err = NULL;
5581 struct got_tree_entry *new_te;
5582 char *subtree_path;
5583 struct got_object_id *id = NULL;
5584 int nentries;
5586 *new_tep = NULL;
5588 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5589 got_path_is_root_dir(path_base_tree) ? "" : "/",
5590 child_path) == -1)
5591 return got_error_from_errno("asprintf");
5593 new_te = calloc(1, sizeof(*new_te));
5594 if (new_te == NULL)
5595 return got_error_from_errno("calloc");
5596 new_te->mode = S_IFDIR;
5598 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5599 sizeof(new_te->name)) {
5600 err = got_error(GOT_ERR_NO_SPACE);
5601 goto done;
5603 err = write_tree(&id, &nentries, NULL, subtree_path,
5604 commitable_paths, status_cb, status_arg, repo);
5605 if (err) {
5606 free(new_te);
5607 goto done;
5609 memcpy(&new_te->id, id, sizeof(new_te->id));
5610 done:
5611 free(id);
5612 free(subtree_path);
5613 if (err == NULL)
5614 *new_tep = new_te;
5615 return err;
5618 static const struct got_error *
5619 write_tree(struct got_object_id **new_tree_id, int *nentries,
5620 struct got_tree_object *base_tree, const char *path_base_tree,
5621 struct got_pathlist_head *commitable_paths,
5622 got_worktree_status_cb status_cb, void *status_arg,
5623 struct got_repository *repo)
5625 const struct got_error *err = NULL;
5626 struct got_pathlist_head paths;
5627 struct got_tree_entry *te, *new_te = NULL;
5628 struct got_pathlist_entry *pe;
5630 TAILQ_INIT(&paths);
5631 *nentries = 0;
5633 /* Insert, and recurse into, newly added entries first. */
5634 TAILQ_FOREACH(pe, commitable_paths, entry) {
5635 struct got_commitable *ct = pe->data;
5636 char *child_path = NULL, *slash;
5638 if ((ct->status != GOT_STATUS_ADD &&
5639 ct->staged_status != GOT_STATUS_ADD) ||
5640 (ct->flags & GOT_COMMITABLE_ADDED))
5641 continue;
5643 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5644 strlen(path_base_tree)))
5645 continue;
5647 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5648 ct->in_repo_path);
5649 if (err)
5650 goto done;
5652 slash = strchr(child_path, '/');
5653 if (slash == NULL) {
5654 err = alloc_added_blob_tree_entry(&new_te, ct);
5655 if (err)
5656 goto done;
5657 err = report_ct_status(ct, status_cb, status_arg);
5658 if (err)
5659 goto done;
5660 ct->flags |= GOT_COMMITABLE_ADDED;
5661 err = insert_tree_entry(new_te, &paths);
5662 if (err)
5663 goto done;
5664 (*nentries)++;
5665 } else {
5666 *slash = '\0'; /* trim trailing path components */
5667 if (base_tree == NULL ||
5668 got_object_tree_find_entry(base_tree, child_path)
5669 == NULL) {
5670 err = make_subtree_for_added_blob(&new_te,
5671 child_path, path_base_tree,
5672 commitable_paths, status_cb, status_arg,
5673 repo);
5674 if (err)
5675 goto done;
5676 err = insert_tree_entry(new_te, &paths);
5677 if (err)
5678 goto done;
5679 (*nentries)++;
5684 if (base_tree) {
5685 int i, nbase_entries;
5686 /* Handle modified and deleted entries. */
5687 nbase_entries = got_object_tree_get_nentries(base_tree);
5688 for (i = 0; i < nbase_entries; i++) {
5689 struct got_commitable *ct = NULL;
5691 te = got_object_tree_get_entry(base_tree, i);
5692 if (got_object_tree_entry_is_submodule(te)) {
5693 /* Entry is a submodule; just copy it. */
5694 err = got_object_tree_entry_dup(&new_te, te);
5695 if (err)
5696 goto done;
5697 err = insert_tree_entry(new_te, &paths);
5698 if (err)
5699 goto done;
5700 (*nentries)++;
5701 continue;
5704 if (S_ISDIR(te->mode)) {
5705 int modified;
5706 err = got_object_tree_entry_dup(&new_te, te);
5707 if (err)
5708 goto done;
5709 err = match_modified_subtree(&modified, te,
5710 path_base_tree, commitable_paths);
5711 if (err)
5712 goto done;
5713 /* Avoid recursion into unmodified subtrees. */
5714 if (modified) {
5715 struct got_object_id *new_id;
5716 int nsubentries;
5717 err = write_subtree(&new_id,
5718 &nsubentries, te,
5719 path_base_tree, commitable_paths,
5720 status_cb, status_arg, repo);
5721 if (err)
5722 goto done;
5723 if (nsubentries == 0) {
5724 /* All entries were deleted. */
5725 free(new_id);
5726 continue;
5728 memcpy(&new_te->id, new_id,
5729 sizeof(new_te->id));
5730 free(new_id);
5732 err = insert_tree_entry(new_te, &paths);
5733 if (err)
5734 goto done;
5735 (*nentries)++;
5736 continue;
5739 err = match_deleted_or_modified_ct(&ct, te,
5740 path_base_tree, commitable_paths);
5741 if (err)
5742 goto done;
5743 if (ct) {
5744 /* NB: Deleted entries get dropped here. */
5745 if (ct->status == GOT_STATUS_MODIFY ||
5746 ct->status == GOT_STATUS_MODE_CHANGE ||
5747 ct->staged_status == GOT_STATUS_MODIFY) {
5748 err = alloc_modified_blob_tree_entry(
5749 &new_te, te, ct);
5750 if (err)
5751 goto done;
5752 err = insert_tree_entry(new_te, &paths);
5753 if (err)
5754 goto done;
5755 (*nentries)++;
5757 err = report_ct_status(ct, status_cb,
5758 status_arg);
5759 if (err)
5760 goto done;
5761 } else {
5762 /* Entry is unchanged; just copy it. */
5763 err = got_object_tree_entry_dup(&new_te, te);
5764 if (err)
5765 goto done;
5766 err = insert_tree_entry(new_te, &paths);
5767 if (err)
5768 goto done;
5769 (*nentries)++;
5774 /* Write new list of entries; deleted entries have been dropped. */
5775 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5776 done:
5777 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5778 return err;
5781 static const struct got_error *
5782 update_fileindex_after_commit(struct got_worktree *worktree,
5783 struct got_pathlist_head *commitable_paths,
5784 struct got_object_id *new_base_commit_id,
5785 struct got_fileindex *fileindex, int have_staged_files)
5787 const struct got_error *err = NULL;
5788 struct got_pathlist_entry *pe;
5789 char *relpath = NULL;
5791 TAILQ_FOREACH(pe, commitable_paths, entry) {
5792 struct got_fileindex_entry *ie;
5793 struct got_commitable *ct = pe->data;
5795 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5797 err = got_path_skip_common_ancestor(&relpath,
5798 worktree->root_path, ct->ondisk_path);
5799 if (err)
5800 goto done;
5802 if (ie) {
5803 if (ct->status == GOT_STATUS_DELETE ||
5804 ct->staged_status == GOT_STATUS_DELETE) {
5805 got_fileindex_entry_remove(fileindex, ie);
5806 } else if (ct->staged_status == GOT_STATUS_ADD ||
5807 ct->staged_status == GOT_STATUS_MODIFY) {
5808 got_fileindex_entry_stage_set(ie,
5809 GOT_FILEIDX_STAGE_NONE);
5810 got_fileindex_entry_staged_filetype_set(ie, 0);
5812 err = got_fileindex_entry_update(ie,
5813 worktree->root_fd, relpath,
5814 ct->staged_blob_id->sha1,
5815 new_base_commit_id->sha1,
5816 !have_staged_files);
5817 } else
5818 err = got_fileindex_entry_update(ie,
5819 worktree->root_fd, relpath,
5820 ct->blob_id->sha1,
5821 new_base_commit_id->sha1,
5822 !have_staged_files);
5823 } else {
5824 err = got_fileindex_entry_alloc(&ie, pe->path);
5825 if (err)
5826 goto done;
5827 err = got_fileindex_entry_update(ie,
5828 worktree->root_fd, relpath, ct->blob_id->sha1,
5829 new_base_commit_id->sha1, 1);
5830 if (err) {
5831 got_fileindex_entry_free(ie);
5832 goto done;
5834 err = got_fileindex_entry_add(fileindex, ie);
5835 if (err) {
5836 got_fileindex_entry_free(ie);
5837 goto done;
5840 free(relpath);
5841 relpath = NULL;
5843 done:
5844 free(relpath);
5845 return err;
5849 static const struct got_error *
5850 check_out_of_date(const char *in_repo_path, unsigned char status,
5851 unsigned char staged_status, struct got_object_id *base_blob_id,
5852 struct got_object_id *base_commit_id,
5853 struct got_object_id *head_commit_id, struct got_repository *repo,
5854 int ood_errcode)
5856 const struct got_error *err = NULL;
5857 struct got_commit_object *commit = NULL;
5858 struct got_object_id *id = NULL;
5860 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5861 /* Trivial case: base commit == head commit */
5862 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5863 return NULL;
5865 * Ensure file content which local changes were based
5866 * on matches file content in the branch head.
5868 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5869 if (err)
5870 goto done;
5871 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5872 if (err) {
5873 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5874 err = got_error(ood_errcode);
5875 goto done;
5876 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5877 err = got_error(ood_errcode);
5878 } else {
5879 /* Require that added files don't exist in the branch head. */
5880 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5881 if (err)
5882 goto done;
5883 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5884 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5885 goto done;
5886 err = id ? got_error(ood_errcode) : NULL;
5888 done:
5889 free(id);
5890 if (commit)
5891 got_object_commit_close(commit);
5892 return err;
5895 static const struct got_error *
5896 commit_worktree(struct got_object_id **new_commit_id,
5897 struct got_pathlist_head *commitable_paths,
5898 struct got_object_id *head_commit_id,
5899 struct got_object_id *parent_id2,
5900 struct got_worktree *worktree,
5901 const char *author, const char *committer, char *diff_path,
5902 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5903 got_worktree_status_cb status_cb, void *status_arg,
5904 struct got_repository *repo)
5906 const struct got_error *err = NULL, *unlockerr = NULL;
5907 struct got_pathlist_entry *pe;
5908 const char *head_ref_name = NULL;
5909 struct got_commit_object *head_commit = NULL;
5910 struct got_reference *head_ref2 = NULL;
5911 struct got_object_id *head_commit_id2 = NULL;
5912 struct got_tree_object *head_tree = NULL;
5913 struct got_object_id *new_tree_id = NULL;
5914 int nentries, nparents = 0;
5915 struct got_object_id_queue parent_ids;
5916 struct got_object_qid *pid = NULL;
5917 char *logmsg = NULL;
5918 time_t timestamp;
5920 *new_commit_id = NULL;
5922 STAILQ_INIT(&parent_ids);
5924 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5925 if (err)
5926 goto done;
5928 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5929 if (err)
5930 goto done;
5932 if (commit_msg_cb != NULL) {
5933 err = commit_msg_cb(commitable_paths, diff_path,
5934 &logmsg, commit_arg);
5935 if (err)
5936 goto done;
5939 if (logmsg == NULL || strlen(logmsg) == 0) {
5940 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5941 goto done;
5944 /* Create blobs from added and modified files and record their IDs. */
5945 TAILQ_FOREACH(pe, commitable_paths, entry) {
5946 struct got_commitable *ct = pe->data;
5947 char *ondisk_path;
5949 /* Blobs for staged files already exist. */
5950 if (ct->staged_status == GOT_STATUS_ADD ||
5951 ct->staged_status == GOT_STATUS_MODIFY)
5952 continue;
5954 if (ct->status != GOT_STATUS_ADD &&
5955 ct->status != GOT_STATUS_MODIFY &&
5956 ct->status != GOT_STATUS_MODE_CHANGE)
5957 continue;
5959 if (asprintf(&ondisk_path, "%s/%s",
5960 worktree->root_path, pe->path) == -1) {
5961 err = got_error_from_errno("asprintf");
5962 goto done;
5964 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5965 free(ondisk_path);
5966 if (err)
5967 goto done;
5970 /* Recursively write new tree objects. */
5971 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5972 commitable_paths, status_cb, status_arg, repo);
5973 if (err)
5974 goto done;
5976 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5977 if (err)
5978 goto done;
5979 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5980 nparents++;
5981 if (parent_id2) {
5982 err = got_object_qid_alloc(&pid, parent_id2);
5983 if (err)
5984 goto done;
5985 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5986 nparents++;
5988 timestamp = time(NULL);
5989 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5990 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5991 if (logmsg != NULL)
5992 free(logmsg);
5993 if (err)
5994 goto done;
5996 /* Check if a concurrent commit to our branch has occurred. */
5997 head_ref_name = got_worktree_get_head_ref_name(worktree);
5998 if (head_ref_name == NULL) {
5999 err = got_error_from_errno("got_worktree_get_head_ref_name");
6000 goto done;
6002 /* Lock the reference here to prevent concurrent modification. */
6003 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6004 if (err)
6005 goto done;
6006 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6007 if (err)
6008 goto done;
6009 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6010 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6011 goto done;
6013 /* Update branch head in repository. */
6014 err = got_ref_change_ref(head_ref2, *new_commit_id);
6015 if (err)
6016 goto done;
6017 err = got_ref_write(head_ref2, repo);
6018 if (err)
6019 goto done;
6021 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6022 if (err)
6023 goto done;
6025 err = ref_base_commit(worktree, repo);
6026 if (err)
6027 goto done;
6028 done:
6029 got_object_id_queue_free(&parent_ids);
6030 if (head_tree)
6031 got_object_tree_close(head_tree);
6032 if (head_commit)
6033 got_object_commit_close(head_commit);
6034 free(head_commit_id2);
6035 if (head_ref2) {
6036 unlockerr = got_ref_unlock(head_ref2);
6037 if (unlockerr && err == NULL)
6038 err = unlockerr;
6039 got_ref_close(head_ref2);
6041 return err;
6044 static const struct got_error *
6045 check_path_is_commitable(const char *path,
6046 struct got_pathlist_head *commitable_paths)
6048 struct got_pathlist_entry *cpe = NULL;
6049 size_t path_len = strlen(path);
6051 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6052 struct got_commitable *ct = cpe->data;
6053 const char *ct_path = ct->path;
6055 while (ct_path[0] == '/')
6056 ct_path++;
6058 if (strcmp(path, ct_path) == 0 ||
6059 got_path_is_child(ct_path, path, path_len))
6060 break;
6063 if (cpe == NULL)
6064 return got_error_path(path, GOT_ERR_BAD_PATH);
6066 return NULL;
6069 static const struct got_error *
6070 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6072 int *have_staged_files = arg;
6074 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6075 *have_staged_files = 1;
6076 return got_error(GOT_ERR_CANCELLED);
6079 return NULL;
6082 static const struct got_error *
6083 check_non_staged_files(struct got_fileindex *fileindex,
6084 struct got_pathlist_head *paths)
6086 struct got_pathlist_entry *pe;
6087 struct got_fileindex_entry *ie;
6089 TAILQ_FOREACH(pe, paths, entry) {
6090 if (pe->path[0] == '\0')
6091 continue;
6092 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6093 if (ie == NULL)
6094 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6095 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6096 return got_error_path(pe->path,
6097 GOT_ERR_FILE_NOT_STAGED);
6100 return NULL;
6103 const struct got_error *
6104 got_worktree_commit(struct got_object_id **new_commit_id,
6105 struct got_worktree *worktree, struct got_pathlist_head *paths,
6106 const char *author, const char *committer, int allow_bad_symlinks,
6107 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6108 got_worktree_status_cb status_cb, void *status_arg,
6109 struct got_repository *repo)
6111 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6112 struct got_fileindex *fileindex = NULL;
6113 char *fileindex_path = NULL;
6114 struct got_pathlist_head commitable_paths;
6115 struct collect_commitables_arg cc_arg;
6116 struct got_pathlist_entry *pe;
6117 struct got_reference *head_ref = NULL;
6118 struct got_object_id *head_commit_id = NULL;
6119 char *diff_path = NULL;
6120 int have_staged_files = 0;
6122 *new_commit_id = NULL;
6124 memset(&cc_arg, 0, sizeof(cc_arg));
6125 TAILQ_INIT(&commitable_paths);
6127 err = lock_worktree(worktree, LOCK_EX);
6128 if (err)
6129 goto done;
6131 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6132 if (err)
6133 goto done;
6135 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6136 if (err)
6137 goto done;
6139 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6140 if (err)
6141 goto done;
6143 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6144 &have_staged_files);
6145 if (err && err->code != GOT_ERR_CANCELLED)
6146 goto done;
6147 if (have_staged_files) {
6148 err = check_non_staged_files(fileindex, paths);
6149 if (err)
6150 goto done;
6153 cc_arg.commitable_paths = &commitable_paths;
6154 cc_arg.worktree = worktree;
6155 cc_arg.fileindex = fileindex;
6156 cc_arg.repo = repo;
6157 cc_arg.have_staged_files = have_staged_files;
6158 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6159 cc_arg.diff_header_shown = 0;
6160 if (show_diff) {
6161 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6162 GOT_TMPDIR_STR "/got", ".diff");
6163 if (err)
6164 goto done;
6165 cc_arg.f1 = got_opentemp();
6166 if (cc_arg.f1 == NULL) {
6167 err = got_error_from_errno("got_opentemp");
6168 goto done;
6170 cc_arg.f2 = got_opentemp();
6171 if (cc_arg.f2 == NULL) {
6172 err = got_error_from_errno("got_opentemp");
6173 goto done;
6177 TAILQ_FOREACH(pe, paths, entry) {
6178 err = worktree_status(worktree, pe->path, fileindex, repo,
6179 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6180 if (err)
6181 goto done;
6184 if (show_diff) {
6185 if (fflush(cc_arg.diff_outfile) == EOF) {
6186 err = got_error_from_errno("fflush");
6187 goto done;
6191 if (TAILQ_EMPTY(&commitable_paths)) {
6192 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6193 goto done;
6196 TAILQ_FOREACH(pe, paths, entry) {
6197 err = check_path_is_commitable(pe->path, &commitable_paths);
6198 if (err)
6199 goto done;
6202 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6203 struct got_commitable *ct = pe->data;
6204 const char *ct_path = ct->in_repo_path;
6206 while (ct_path[0] == '/')
6207 ct_path++;
6208 err = check_out_of_date(ct_path, ct->status,
6209 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6210 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6211 if (err)
6212 goto done;
6216 err = commit_worktree(new_commit_id, &commitable_paths,
6217 head_commit_id, NULL, worktree, author, committer,
6218 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6219 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6220 if (err)
6221 goto done;
6223 err = update_fileindex_after_commit(worktree, &commitable_paths,
6224 *new_commit_id, fileindex, have_staged_files);
6225 sync_err = sync_fileindex(fileindex, fileindex_path);
6226 if (sync_err && err == NULL)
6227 err = sync_err;
6228 done:
6229 if (fileindex)
6230 got_fileindex_free(fileindex);
6231 free(fileindex_path);
6232 unlockerr = lock_worktree(worktree, LOCK_SH);
6233 if (unlockerr && err == NULL)
6234 err = unlockerr;
6235 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6236 struct got_commitable *ct = pe->data;
6238 free_commitable(ct);
6240 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6241 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6242 err = got_error_from_errno2("unlink", diff_path);
6243 free(diff_path);
6244 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6245 err == NULL)
6246 err = got_error_from_errno("fclose");
6247 return err;
6250 const char *
6251 got_commitable_get_path(struct got_commitable *ct)
6253 return ct->path;
6256 unsigned int
6257 got_commitable_get_status(struct got_commitable *ct)
6259 return ct->status;
6262 struct check_rebase_ok_arg {
6263 struct got_worktree *worktree;
6264 struct got_repository *repo;
6267 static const struct got_error *
6268 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6270 const struct got_error *err = NULL;
6271 struct check_rebase_ok_arg *a = arg;
6272 unsigned char status;
6273 struct stat sb;
6274 char *ondisk_path;
6276 /* Reject rebase of a work tree with mixed base commits. */
6277 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6278 SHA1_DIGEST_LENGTH))
6279 return got_error(GOT_ERR_MIXED_COMMITS);
6281 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6282 == -1)
6283 return got_error_from_errno("asprintf");
6285 /* Reject rebase of a work tree with modified or staged files. */
6286 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6287 free(ondisk_path);
6288 if (err)
6289 return err;
6291 if (status != GOT_STATUS_NO_CHANGE)
6292 return got_error(GOT_ERR_MODIFIED);
6293 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6294 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6296 return NULL;
6299 const struct got_error *
6300 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6301 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6302 struct got_worktree *worktree, struct got_reference *branch,
6303 struct got_repository *repo)
6305 const struct got_error *err = NULL;
6306 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6307 char *branch_ref_name = NULL;
6308 char *fileindex_path = NULL;
6309 struct check_rebase_ok_arg ok_arg;
6310 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6311 struct got_object_id *wt_branch_tip = NULL;
6313 *new_base_branch_ref = NULL;
6314 *tmp_branch = NULL;
6315 *fileindex = NULL;
6317 err = lock_worktree(worktree, LOCK_EX);
6318 if (err)
6319 return err;
6321 err = open_fileindex(fileindex, &fileindex_path, worktree);
6322 if (err)
6323 goto done;
6325 ok_arg.worktree = worktree;
6326 ok_arg.repo = repo;
6327 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6328 &ok_arg);
6329 if (err)
6330 goto done;
6332 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6333 if (err)
6334 goto done;
6336 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6337 if (err)
6338 goto done;
6340 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6341 if (err)
6342 goto done;
6344 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6345 0);
6346 if (err)
6347 goto done;
6349 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6350 if (err)
6351 goto done;
6352 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6353 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6354 goto done;
6357 err = got_ref_alloc_symref(new_base_branch_ref,
6358 new_base_branch_ref_name, wt_branch);
6359 if (err)
6360 goto done;
6361 err = got_ref_write(*new_base_branch_ref, repo);
6362 if (err)
6363 goto done;
6365 /* TODO Lock original branch's ref while rebasing? */
6367 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6368 if (err)
6369 goto done;
6371 err = got_ref_write(branch_ref, repo);
6372 if (err)
6373 goto done;
6375 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6376 worktree->base_commit_id);
6377 if (err)
6378 goto done;
6379 err = got_ref_write(*tmp_branch, repo);
6380 if (err)
6381 goto done;
6383 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6384 if (err)
6385 goto done;
6386 done:
6387 free(fileindex_path);
6388 free(tmp_branch_name);
6389 free(new_base_branch_ref_name);
6390 free(branch_ref_name);
6391 if (branch_ref)
6392 got_ref_close(branch_ref);
6393 if (wt_branch)
6394 got_ref_close(wt_branch);
6395 free(wt_branch_tip);
6396 if (err) {
6397 if (*new_base_branch_ref) {
6398 got_ref_close(*new_base_branch_ref);
6399 *new_base_branch_ref = NULL;
6401 if (*tmp_branch) {
6402 got_ref_close(*tmp_branch);
6403 *tmp_branch = NULL;
6405 if (*fileindex) {
6406 got_fileindex_free(*fileindex);
6407 *fileindex = NULL;
6409 lock_worktree(worktree, LOCK_SH);
6411 return err;
6414 const struct got_error *
6415 got_worktree_rebase_continue(struct got_object_id **commit_id,
6416 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6417 struct got_reference **branch, struct got_fileindex **fileindex,
6418 struct got_worktree *worktree, struct got_repository *repo)
6420 const struct got_error *err;
6421 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6422 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6423 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6424 char *fileindex_path = NULL;
6425 int have_staged_files = 0;
6427 *commit_id = NULL;
6428 *new_base_branch = NULL;
6429 *tmp_branch = NULL;
6430 *branch = NULL;
6431 *fileindex = NULL;
6433 err = lock_worktree(worktree, LOCK_EX);
6434 if (err)
6435 return err;
6437 err = open_fileindex(fileindex, &fileindex_path, worktree);
6438 if (err)
6439 goto done;
6441 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6442 &have_staged_files);
6443 if (err && err->code != GOT_ERR_CANCELLED)
6444 goto done;
6445 if (have_staged_files) {
6446 err = got_error(GOT_ERR_STAGED_PATHS);
6447 goto done;
6450 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6451 if (err)
6452 goto done;
6454 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6455 if (err)
6456 goto done;
6458 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6459 if (err)
6460 goto done;
6462 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6463 if (err)
6464 goto done;
6466 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6467 if (err)
6468 goto done;
6470 err = got_ref_open(branch, repo,
6471 got_ref_get_symref_target(branch_ref), 0);
6472 if (err)
6473 goto done;
6475 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6476 if (err)
6477 goto done;
6479 err = got_ref_resolve(commit_id, repo, commit_ref);
6480 if (err)
6481 goto done;
6483 err = got_ref_open(new_base_branch, repo,
6484 new_base_branch_ref_name, 0);
6485 if (err)
6486 goto done;
6488 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6489 if (err)
6490 goto done;
6491 done:
6492 free(commit_ref_name);
6493 free(branch_ref_name);
6494 free(fileindex_path);
6495 if (commit_ref)
6496 got_ref_close(commit_ref);
6497 if (branch_ref)
6498 got_ref_close(branch_ref);
6499 if (err) {
6500 free(*commit_id);
6501 *commit_id = NULL;
6502 if (*tmp_branch) {
6503 got_ref_close(*tmp_branch);
6504 *tmp_branch = NULL;
6506 if (*new_base_branch) {
6507 got_ref_close(*new_base_branch);
6508 *new_base_branch = NULL;
6510 if (*branch) {
6511 got_ref_close(*branch);
6512 *branch = NULL;
6514 if (*fileindex) {
6515 got_fileindex_free(*fileindex);
6516 *fileindex = NULL;
6518 lock_worktree(worktree, LOCK_SH);
6520 return err;
6523 const struct got_error *
6524 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6526 const struct got_error *err;
6527 char *tmp_branch_name = NULL;
6529 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6530 if (err)
6531 return err;
6533 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6534 free(tmp_branch_name);
6535 return NULL;
6538 static const struct got_error *
6539 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6540 const char *diff_path, char **logmsg, void *arg)
6542 *logmsg = arg;
6543 return NULL;
6546 static const struct got_error *
6547 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6548 const char *path, struct got_object_id *blob_id,
6549 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6550 int dirfd, const char *de_name)
6552 return NULL;
6555 struct collect_merged_paths_arg {
6556 got_worktree_checkout_cb progress_cb;
6557 void *progress_arg;
6558 struct got_pathlist_head *merged_paths;
6561 static const struct got_error *
6562 collect_merged_paths(void *arg, unsigned char status, const char *path)
6564 const struct got_error *err;
6565 struct collect_merged_paths_arg *a = arg;
6566 char *p;
6567 struct got_pathlist_entry *new;
6569 err = (*a->progress_cb)(a->progress_arg, status, path);
6570 if (err)
6571 return err;
6573 if (status != GOT_STATUS_MERGE &&
6574 status != GOT_STATUS_ADD &&
6575 status != GOT_STATUS_DELETE &&
6576 status != GOT_STATUS_CONFLICT)
6577 return NULL;
6579 p = strdup(path);
6580 if (p == NULL)
6581 return got_error_from_errno("strdup");
6583 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6584 if (err || new == NULL)
6585 free(p);
6586 return err;
6589 static const struct got_error *
6590 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6591 int is_rebase, struct got_repository *repo)
6593 const struct got_error *err;
6594 struct got_reference *commit_ref = NULL;
6596 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6597 if (err) {
6598 if (err->code != GOT_ERR_NOT_REF)
6599 goto done;
6600 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6601 if (err)
6602 goto done;
6603 err = got_ref_write(commit_ref, repo);
6604 if (err)
6605 goto done;
6606 } else if (is_rebase) {
6607 struct got_object_id *stored_id;
6608 int cmp;
6610 err = got_ref_resolve(&stored_id, repo, commit_ref);
6611 if (err)
6612 goto done;
6613 cmp = got_object_id_cmp(commit_id, stored_id);
6614 free(stored_id);
6615 if (cmp != 0) {
6616 err = got_error(GOT_ERR_REBASE_COMMITID);
6617 goto done;
6620 done:
6621 if (commit_ref)
6622 got_ref_close(commit_ref);
6623 return err;
6626 static const struct got_error *
6627 rebase_merge_files(struct got_pathlist_head *merged_paths,
6628 const char *commit_ref_name, struct got_worktree *worktree,
6629 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6630 struct got_object_id *commit_id, struct got_repository *repo,
6631 got_worktree_checkout_cb progress_cb, void *progress_arg,
6632 got_cancel_cb cancel_cb, void *cancel_arg)
6634 const struct got_error *err;
6635 struct got_reference *commit_ref = NULL;
6636 struct collect_merged_paths_arg cmp_arg;
6637 char *fileindex_path;
6639 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6641 err = get_fileindex_path(&fileindex_path, worktree);
6642 if (err)
6643 return err;
6645 cmp_arg.progress_cb = progress_cb;
6646 cmp_arg.progress_arg = progress_arg;
6647 cmp_arg.merged_paths = merged_paths;
6648 err = merge_files(worktree, fileindex, fileindex_path,
6649 parent_commit_id, commit_id, repo, collect_merged_paths,
6650 &cmp_arg, cancel_cb, cancel_arg);
6651 if (commit_ref)
6652 got_ref_close(commit_ref);
6653 return err;
6656 const struct got_error *
6657 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6658 struct got_worktree *worktree, struct got_fileindex *fileindex,
6659 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6660 struct got_repository *repo,
6661 got_worktree_checkout_cb progress_cb, void *progress_arg,
6662 got_cancel_cb cancel_cb, void *cancel_arg)
6664 const struct got_error *err;
6665 char *commit_ref_name;
6667 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6668 if (err)
6669 return err;
6671 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6672 if (err)
6673 goto done;
6675 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6676 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6677 progress_arg, cancel_cb, cancel_arg);
6678 done:
6679 free(commit_ref_name);
6680 return err;
6683 const struct got_error *
6684 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6685 struct got_worktree *worktree, struct got_fileindex *fileindex,
6686 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6687 struct got_repository *repo,
6688 got_worktree_checkout_cb progress_cb, void *progress_arg,
6689 got_cancel_cb cancel_cb, void *cancel_arg)
6691 const struct got_error *err;
6692 char *commit_ref_name;
6694 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6695 if (err)
6696 return err;
6698 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6699 if (err)
6700 goto done;
6702 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6703 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6704 progress_arg, cancel_cb, cancel_arg);
6705 done:
6706 free(commit_ref_name);
6707 return err;
6710 static const struct got_error *
6711 rebase_commit(struct got_object_id **new_commit_id,
6712 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6713 struct got_worktree *worktree, struct got_fileindex *fileindex,
6714 struct got_reference *tmp_branch, const char *committer,
6715 struct got_commit_object *orig_commit, const char *new_logmsg,
6716 struct got_repository *repo)
6718 const struct got_error *err, *sync_err;
6719 struct got_pathlist_head commitable_paths;
6720 struct collect_commitables_arg cc_arg;
6721 char *fileindex_path = NULL;
6722 struct got_reference *head_ref = NULL;
6723 struct got_object_id *head_commit_id = NULL;
6724 char *logmsg = NULL;
6726 memset(&cc_arg, 0, sizeof(cc_arg));
6727 TAILQ_INIT(&commitable_paths);
6728 *new_commit_id = NULL;
6730 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6732 err = get_fileindex_path(&fileindex_path, worktree);
6733 if (err)
6734 return err;
6736 cc_arg.commitable_paths = &commitable_paths;
6737 cc_arg.worktree = worktree;
6738 cc_arg.repo = repo;
6739 cc_arg.have_staged_files = 0;
6741 * If possible get the status of individual files directly to
6742 * avoid crawling the entire work tree once per rebased commit.
6744 * Ideally, merged_paths would contain a list of commitables
6745 * we could use so we could skip worktree_status() entirely.
6746 * However, we would then need carefully keep track of cumulative
6747 * effects of operations such as file additions and deletions
6748 * in 'got histedit -f' (folding multiple commits into one),
6749 * and this extra complexity is not really worth it.
6751 if (merged_paths) {
6752 struct got_pathlist_entry *pe;
6753 TAILQ_FOREACH(pe, merged_paths, entry) {
6754 err = worktree_status(worktree, pe->path, fileindex,
6755 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6756 0);
6757 if (err)
6758 goto done;
6760 } else {
6761 err = worktree_status(worktree, "", fileindex, repo,
6762 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6763 if (err)
6764 goto done;
6767 if (TAILQ_EMPTY(&commitable_paths)) {
6768 /* No-op change; commit will be elided. */
6769 err = got_ref_delete(commit_ref, repo);
6770 if (err)
6771 goto done;
6772 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6773 goto done;
6776 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6777 if (err)
6778 goto done;
6780 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6781 if (err)
6782 goto done;
6784 if (new_logmsg) {
6785 logmsg = strdup(new_logmsg);
6786 if (logmsg == NULL) {
6787 err = got_error_from_errno("strdup");
6788 goto done;
6790 } else {
6791 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6792 if (err)
6793 goto done;
6796 /* NB: commit_worktree will call free(logmsg) */
6797 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6798 NULL, worktree, got_object_commit_get_author(orig_commit),
6799 committer ? committer :
6800 got_object_commit_get_committer(orig_commit), NULL,
6801 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6802 if (err)
6803 goto done;
6805 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6806 if (err)
6807 goto done;
6809 err = got_ref_delete(commit_ref, repo);
6810 if (err)
6811 goto done;
6813 err = update_fileindex_after_commit(worktree, &commitable_paths,
6814 *new_commit_id, fileindex, 0);
6815 sync_err = sync_fileindex(fileindex, fileindex_path);
6816 if (sync_err && err == NULL)
6817 err = sync_err;
6818 done:
6819 free(fileindex_path);
6820 free(head_commit_id);
6821 if (head_ref)
6822 got_ref_close(head_ref);
6823 if (err) {
6824 free(*new_commit_id);
6825 *new_commit_id = NULL;
6827 return err;
6830 const struct got_error *
6831 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6832 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6833 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6834 const char *committer, struct got_commit_object *orig_commit,
6835 struct got_object_id *orig_commit_id, struct got_repository *repo)
6837 const struct got_error *err;
6838 char *commit_ref_name;
6839 struct got_reference *commit_ref = NULL;
6840 struct got_object_id *commit_id = NULL;
6842 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6843 if (err)
6844 return err;
6846 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6847 if (err)
6848 goto done;
6849 err = got_ref_resolve(&commit_id, repo, commit_ref);
6850 if (err)
6851 goto done;
6852 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6853 err = got_error(GOT_ERR_REBASE_COMMITID);
6854 goto done;
6857 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6858 worktree, fileindex, tmp_branch, committer, orig_commit,
6859 NULL, repo);
6860 done:
6861 if (commit_ref)
6862 got_ref_close(commit_ref);
6863 free(commit_ref_name);
6864 free(commit_id);
6865 return err;
6868 const struct got_error *
6869 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6870 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6871 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6872 const char *committer, struct got_commit_object *orig_commit,
6873 struct got_object_id *orig_commit_id, const char *new_logmsg,
6874 struct got_repository *repo)
6876 const struct got_error *err;
6877 char *commit_ref_name;
6878 struct got_reference *commit_ref = NULL;
6880 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6881 if (err)
6882 return err;
6884 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6885 if (err)
6886 goto done;
6888 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6889 worktree, fileindex, tmp_branch, committer, orig_commit,
6890 new_logmsg, repo);
6891 done:
6892 if (commit_ref)
6893 got_ref_close(commit_ref);
6894 free(commit_ref_name);
6895 return err;
6898 const struct got_error *
6899 got_worktree_rebase_postpone(struct got_worktree *worktree,
6900 struct got_fileindex *fileindex)
6902 if (fileindex)
6903 got_fileindex_free(fileindex);
6904 return lock_worktree(worktree, LOCK_SH);
6907 static const struct got_error *
6908 delete_ref(const char *name, struct got_repository *repo)
6910 const struct got_error *err;
6911 struct got_reference *ref;
6913 err = got_ref_open(&ref, repo, name, 0);
6914 if (err) {
6915 if (err->code == GOT_ERR_NOT_REF)
6916 return NULL;
6917 return err;
6920 err = got_ref_delete(ref, repo);
6921 got_ref_close(ref);
6922 return err;
6925 static const struct got_error *
6926 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6928 const struct got_error *err;
6929 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6930 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6932 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6933 if (err)
6934 goto done;
6935 err = delete_ref(tmp_branch_name, repo);
6936 if (err)
6937 goto done;
6939 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6940 if (err)
6941 goto done;
6942 err = delete_ref(new_base_branch_ref_name, repo);
6943 if (err)
6944 goto done;
6946 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6947 if (err)
6948 goto done;
6949 err = delete_ref(branch_ref_name, repo);
6950 if (err)
6951 goto done;
6953 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6954 if (err)
6955 goto done;
6956 err = delete_ref(commit_ref_name, repo);
6957 if (err)
6958 goto done;
6960 done:
6961 free(tmp_branch_name);
6962 free(new_base_branch_ref_name);
6963 free(branch_ref_name);
6964 free(commit_ref_name);
6965 return err;
6968 static const struct got_error *
6969 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6970 struct got_object_id *new_commit_id, struct got_repository *repo)
6972 const struct got_error *err;
6973 struct got_reference *ref = NULL;
6974 struct got_object_id *old_commit_id = NULL;
6975 const char *branch_name = NULL;
6976 char *new_id_str = NULL;
6977 char *refname = NULL;
6979 branch_name = got_ref_get_name(branch);
6980 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6981 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6982 branch_name += 11;
6984 err = got_object_id_str(&new_id_str, new_commit_id);
6985 if (err)
6986 return err;
6988 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6989 new_id_str) == -1) {
6990 err = got_error_from_errno("asprintf");
6991 goto done;
6994 err = got_ref_resolve(&old_commit_id, repo, branch);
6995 if (err)
6996 goto done;
6998 err = got_ref_alloc(&ref, refname, old_commit_id);
6999 if (err)
7000 goto done;
7002 err = got_ref_write(ref, repo);
7003 done:
7004 free(new_id_str);
7005 free(refname);
7006 free(old_commit_id);
7007 if (ref)
7008 got_ref_close(ref);
7009 return err;
7012 const struct got_error *
7013 got_worktree_rebase_complete(struct got_worktree *worktree,
7014 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7015 struct got_reference *rebased_branch, struct got_repository *repo,
7016 int create_backup)
7018 const struct got_error *err, *unlockerr, *sync_err;
7019 struct got_object_id *new_head_commit_id = NULL;
7020 char *fileindex_path = NULL;
7022 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7023 if (err)
7024 return err;
7026 if (create_backup) {
7027 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7028 rebased_branch, new_head_commit_id, repo);
7029 if (err)
7030 goto done;
7033 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7034 if (err)
7035 goto done;
7037 err = got_ref_write(rebased_branch, repo);
7038 if (err)
7039 goto done;
7041 err = got_worktree_set_head_ref(worktree, rebased_branch);
7042 if (err)
7043 goto done;
7045 err = delete_rebase_refs(worktree, repo);
7046 if (err)
7047 goto done;
7049 err = get_fileindex_path(&fileindex_path, worktree);
7050 if (err)
7051 goto done;
7052 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7053 sync_err = sync_fileindex(fileindex, fileindex_path);
7054 if (sync_err && err == NULL)
7055 err = sync_err;
7056 done:
7057 got_fileindex_free(fileindex);
7058 free(fileindex_path);
7059 free(new_head_commit_id);
7060 unlockerr = lock_worktree(worktree, LOCK_SH);
7061 if (unlockerr && err == NULL)
7062 err = unlockerr;
7063 return err;
7066 const struct got_error *
7067 got_worktree_rebase_abort(struct got_worktree *worktree,
7068 struct got_fileindex *fileindex, struct got_repository *repo,
7069 struct got_reference *new_base_branch,
7070 got_worktree_checkout_cb progress_cb, void *progress_arg)
7072 const struct got_error *err, *unlockerr, *sync_err;
7073 struct got_reference *resolved = NULL;
7074 struct got_object_id *commit_id = NULL;
7075 struct got_commit_object *commit = NULL;
7076 char *fileindex_path = NULL;
7077 struct revert_file_args rfa;
7078 struct got_object_id *tree_id = NULL;
7080 err = lock_worktree(worktree, LOCK_EX);
7081 if (err)
7082 return err;
7084 err = got_object_open_as_commit(&commit, repo,
7085 worktree->base_commit_id);
7086 if (err)
7087 goto done;
7089 err = got_ref_open(&resolved, repo,
7090 got_ref_get_symref_target(new_base_branch), 0);
7091 if (err)
7092 goto done;
7094 err = got_worktree_set_head_ref(worktree, resolved);
7095 if (err)
7096 goto done;
7099 * XXX commits to the base branch could have happened while
7100 * we were busy rebasing; should we store the original commit ID
7101 * when rebase begins and read it back here?
7103 err = got_ref_resolve(&commit_id, repo, resolved);
7104 if (err)
7105 goto done;
7107 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7108 if (err)
7109 goto done;
7111 err = got_object_id_by_path(&tree_id, repo, commit,
7112 worktree->path_prefix);
7113 if (err)
7114 goto done;
7116 err = delete_rebase_refs(worktree, repo);
7117 if (err)
7118 goto done;
7120 err = get_fileindex_path(&fileindex_path, worktree);
7121 if (err)
7122 goto done;
7124 rfa.worktree = worktree;
7125 rfa.fileindex = fileindex;
7126 rfa.progress_cb = progress_cb;
7127 rfa.progress_arg = progress_arg;
7128 rfa.patch_cb = NULL;
7129 rfa.patch_arg = NULL;
7130 rfa.repo = repo;
7131 rfa.unlink_added_files = 0;
7132 err = worktree_status(worktree, "", fileindex, repo,
7133 revert_file, &rfa, NULL, NULL, 1, 0);
7134 if (err)
7135 goto sync;
7137 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7138 repo, progress_cb, progress_arg, NULL, NULL);
7139 sync:
7140 sync_err = sync_fileindex(fileindex, fileindex_path);
7141 if (sync_err && err == NULL)
7142 err = sync_err;
7143 done:
7144 got_ref_close(resolved);
7145 free(tree_id);
7146 free(commit_id);
7147 if (commit)
7148 got_object_commit_close(commit);
7149 if (fileindex)
7150 got_fileindex_free(fileindex);
7151 free(fileindex_path);
7153 unlockerr = lock_worktree(worktree, LOCK_SH);
7154 if (unlockerr && err == NULL)
7155 err = unlockerr;
7156 return err;
7159 const struct got_error *
7160 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7161 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7162 struct got_fileindex **fileindex, struct got_worktree *worktree,
7163 struct got_repository *repo)
7165 const struct got_error *err = NULL;
7166 char *tmp_branch_name = NULL;
7167 char *branch_ref_name = NULL;
7168 char *base_commit_ref_name = NULL;
7169 char *fileindex_path = NULL;
7170 struct check_rebase_ok_arg ok_arg;
7171 struct got_reference *wt_branch = NULL;
7172 struct got_reference *base_commit_ref = NULL;
7174 *tmp_branch = NULL;
7175 *branch_ref = NULL;
7176 *base_commit_id = NULL;
7177 *fileindex = NULL;
7179 err = lock_worktree(worktree, LOCK_EX);
7180 if (err)
7181 return err;
7183 err = open_fileindex(fileindex, &fileindex_path, worktree);
7184 if (err)
7185 goto done;
7187 ok_arg.worktree = worktree;
7188 ok_arg.repo = repo;
7189 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7190 &ok_arg);
7191 if (err)
7192 goto done;
7194 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7195 if (err)
7196 goto done;
7198 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7199 if (err)
7200 goto done;
7202 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7203 worktree);
7204 if (err)
7205 goto done;
7207 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7208 0);
7209 if (err)
7210 goto done;
7212 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7213 if (err)
7214 goto done;
7216 err = got_ref_write(*branch_ref, repo);
7217 if (err)
7218 goto done;
7220 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7221 worktree->base_commit_id);
7222 if (err)
7223 goto done;
7224 err = got_ref_write(base_commit_ref, repo);
7225 if (err)
7226 goto done;
7227 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7228 if (*base_commit_id == NULL) {
7229 err = got_error_from_errno("got_object_id_dup");
7230 goto done;
7233 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7234 worktree->base_commit_id);
7235 if (err)
7236 goto done;
7237 err = got_ref_write(*tmp_branch, repo);
7238 if (err)
7239 goto done;
7241 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7242 if (err)
7243 goto done;
7244 done:
7245 free(fileindex_path);
7246 free(tmp_branch_name);
7247 free(branch_ref_name);
7248 free(base_commit_ref_name);
7249 if (wt_branch)
7250 got_ref_close(wt_branch);
7251 if (err) {
7252 if (*branch_ref) {
7253 got_ref_close(*branch_ref);
7254 *branch_ref = NULL;
7256 if (*tmp_branch) {
7257 got_ref_close(*tmp_branch);
7258 *tmp_branch = NULL;
7260 free(*base_commit_id);
7261 if (*fileindex) {
7262 got_fileindex_free(*fileindex);
7263 *fileindex = NULL;
7265 lock_worktree(worktree, LOCK_SH);
7267 return err;
7270 const struct got_error *
7271 got_worktree_histedit_postpone(struct got_worktree *worktree,
7272 struct got_fileindex *fileindex)
7274 if (fileindex)
7275 got_fileindex_free(fileindex);
7276 return lock_worktree(worktree, LOCK_SH);
7279 const struct got_error *
7280 got_worktree_histedit_in_progress(int *in_progress,
7281 struct got_worktree *worktree)
7283 const struct got_error *err;
7284 char *tmp_branch_name = NULL;
7286 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7287 if (err)
7288 return err;
7290 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7291 free(tmp_branch_name);
7292 return NULL;
7295 const struct got_error *
7296 got_worktree_histedit_continue(struct got_object_id **commit_id,
7297 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7298 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7299 struct got_worktree *worktree, struct got_repository *repo)
7301 const struct got_error *err;
7302 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7303 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7304 struct got_reference *commit_ref = NULL;
7305 struct got_reference *base_commit_ref = NULL;
7306 char *fileindex_path = NULL;
7307 int have_staged_files = 0;
7309 *commit_id = NULL;
7310 *tmp_branch = NULL;
7311 *base_commit_id = NULL;
7312 *fileindex = NULL;
7314 err = lock_worktree(worktree, LOCK_EX);
7315 if (err)
7316 return err;
7318 err = open_fileindex(fileindex, &fileindex_path, worktree);
7319 if (err)
7320 goto done;
7322 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7323 &have_staged_files);
7324 if (err && err->code != GOT_ERR_CANCELLED)
7325 goto done;
7326 if (have_staged_files) {
7327 err = got_error(GOT_ERR_STAGED_PATHS);
7328 goto done;
7331 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7332 if (err)
7333 goto done;
7335 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7336 if (err)
7337 goto done;
7339 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7340 if (err)
7341 goto done;
7343 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7344 worktree);
7345 if (err)
7346 goto done;
7348 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7349 if (err)
7350 goto done;
7352 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7353 if (err)
7354 goto done;
7355 err = got_ref_resolve(commit_id, repo, commit_ref);
7356 if (err)
7357 goto done;
7359 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7360 if (err)
7361 goto done;
7362 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7363 if (err)
7364 goto done;
7366 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7367 if (err)
7368 goto done;
7369 done:
7370 free(commit_ref_name);
7371 free(branch_ref_name);
7372 free(fileindex_path);
7373 if (commit_ref)
7374 got_ref_close(commit_ref);
7375 if (base_commit_ref)
7376 got_ref_close(base_commit_ref);
7377 if (err) {
7378 free(*commit_id);
7379 *commit_id = NULL;
7380 free(*base_commit_id);
7381 *base_commit_id = NULL;
7382 if (*tmp_branch) {
7383 got_ref_close(*tmp_branch);
7384 *tmp_branch = NULL;
7386 if (*fileindex) {
7387 got_fileindex_free(*fileindex);
7388 *fileindex = NULL;
7390 lock_worktree(worktree, LOCK_EX);
7392 return err;
7395 static const struct got_error *
7396 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7398 const struct got_error *err;
7399 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7400 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7402 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7403 if (err)
7404 goto done;
7405 err = delete_ref(tmp_branch_name, repo);
7406 if (err)
7407 goto done;
7409 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7410 worktree);
7411 if (err)
7412 goto done;
7413 err = delete_ref(base_commit_ref_name, repo);
7414 if (err)
7415 goto done;
7417 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7418 if (err)
7419 goto done;
7420 err = delete_ref(branch_ref_name, repo);
7421 if (err)
7422 goto done;
7424 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7425 if (err)
7426 goto done;
7427 err = delete_ref(commit_ref_name, repo);
7428 if (err)
7429 goto done;
7430 done:
7431 free(tmp_branch_name);
7432 free(base_commit_ref_name);
7433 free(branch_ref_name);
7434 free(commit_ref_name);
7435 return err;
7438 const struct got_error *
7439 got_worktree_histedit_abort(struct got_worktree *worktree,
7440 struct got_fileindex *fileindex, struct got_repository *repo,
7441 struct got_reference *branch, struct got_object_id *base_commit_id,
7442 got_worktree_checkout_cb progress_cb, void *progress_arg)
7444 const struct got_error *err, *unlockerr, *sync_err;
7445 struct got_reference *resolved = NULL;
7446 char *fileindex_path = NULL;
7447 struct got_commit_object *commit = NULL;
7448 struct got_object_id *tree_id = NULL;
7449 struct revert_file_args rfa;
7451 err = lock_worktree(worktree, LOCK_EX);
7452 if (err)
7453 return err;
7455 err = got_object_open_as_commit(&commit, repo,
7456 worktree->base_commit_id);
7457 if (err)
7458 goto done;
7460 err = got_ref_open(&resolved, repo,
7461 got_ref_get_symref_target(branch), 0);
7462 if (err)
7463 goto done;
7465 err = got_worktree_set_head_ref(worktree, resolved);
7466 if (err)
7467 goto done;
7469 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7470 if (err)
7471 goto done;
7473 err = got_object_id_by_path(&tree_id, repo, commit,
7474 worktree->path_prefix);
7475 if (err)
7476 goto done;
7478 err = delete_histedit_refs(worktree, repo);
7479 if (err)
7480 goto done;
7482 err = get_fileindex_path(&fileindex_path, worktree);
7483 if (err)
7484 goto done;
7486 rfa.worktree = worktree;
7487 rfa.fileindex = fileindex;
7488 rfa.progress_cb = progress_cb;
7489 rfa.progress_arg = progress_arg;
7490 rfa.patch_cb = NULL;
7491 rfa.patch_arg = NULL;
7492 rfa.repo = repo;
7493 rfa.unlink_added_files = 0;
7494 err = worktree_status(worktree, "", fileindex, repo,
7495 revert_file, &rfa, NULL, NULL, 1, 0);
7496 if (err)
7497 goto sync;
7499 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7500 repo, progress_cb, progress_arg, NULL, NULL);
7501 sync:
7502 sync_err = sync_fileindex(fileindex, fileindex_path);
7503 if (sync_err && err == NULL)
7504 err = sync_err;
7505 done:
7506 got_ref_close(resolved);
7507 free(tree_id);
7508 free(fileindex_path);
7510 unlockerr = lock_worktree(worktree, LOCK_SH);
7511 if (unlockerr && err == NULL)
7512 err = unlockerr;
7513 return err;
7516 const struct got_error *
7517 got_worktree_histedit_complete(struct got_worktree *worktree,
7518 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7519 struct got_reference *edited_branch, struct got_repository *repo)
7521 const struct got_error *err, *unlockerr, *sync_err;
7522 struct got_object_id *new_head_commit_id = NULL;
7523 struct got_reference *resolved = NULL;
7524 char *fileindex_path = NULL;
7526 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7527 if (err)
7528 return err;
7530 err = got_ref_open(&resolved, repo,
7531 got_ref_get_symref_target(edited_branch), 0);
7532 if (err)
7533 goto done;
7535 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7536 resolved, new_head_commit_id, repo);
7537 if (err)
7538 goto done;
7540 err = got_ref_change_ref(resolved, new_head_commit_id);
7541 if (err)
7542 goto done;
7544 err = got_ref_write(resolved, repo);
7545 if (err)
7546 goto done;
7548 err = got_worktree_set_head_ref(worktree, resolved);
7549 if (err)
7550 goto done;
7552 err = delete_histedit_refs(worktree, repo);
7553 if (err)
7554 goto done;
7556 err = get_fileindex_path(&fileindex_path, worktree);
7557 if (err)
7558 goto done;
7559 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7560 sync_err = sync_fileindex(fileindex, fileindex_path);
7561 if (sync_err && err == NULL)
7562 err = sync_err;
7563 done:
7564 got_fileindex_free(fileindex);
7565 free(fileindex_path);
7566 free(new_head_commit_id);
7567 unlockerr = lock_worktree(worktree, LOCK_SH);
7568 if (unlockerr && err == NULL)
7569 err = unlockerr;
7570 return err;
7573 const struct got_error *
7574 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7575 struct got_object_id *commit_id, struct got_repository *repo)
7577 const struct got_error *err;
7578 char *commit_ref_name;
7580 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7581 if (err)
7582 return err;
7584 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7585 if (err)
7586 goto done;
7588 err = delete_ref(commit_ref_name, repo);
7589 done:
7590 free(commit_ref_name);
7591 return err;
7594 const struct got_error *
7595 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7596 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7597 struct got_worktree *worktree, const char *refname,
7598 struct got_repository *repo)
7600 const struct got_error *err = NULL;
7601 char *fileindex_path = NULL;
7602 struct check_rebase_ok_arg ok_arg;
7604 *fileindex = NULL;
7605 *branch_ref = NULL;
7606 *base_branch_ref = NULL;
7608 err = lock_worktree(worktree, LOCK_EX);
7609 if (err)
7610 return err;
7612 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7613 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7614 "cannot integrate a branch into itself; "
7615 "update -b or different branch name required");
7616 goto done;
7619 err = open_fileindex(fileindex, &fileindex_path, worktree);
7620 if (err)
7621 goto done;
7623 /* Preconditions are the same as for rebase. */
7624 ok_arg.worktree = worktree;
7625 ok_arg.repo = repo;
7626 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7627 &ok_arg);
7628 if (err)
7629 goto done;
7631 err = got_ref_open(branch_ref, repo, refname, 1);
7632 if (err)
7633 goto done;
7635 err = got_ref_open(base_branch_ref, repo,
7636 got_worktree_get_head_ref_name(worktree), 1);
7637 done:
7638 if (err) {
7639 if (*branch_ref) {
7640 got_ref_close(*branch_ref);
7641 *branch_ref = NULL;
7643 if (*base_branch_ref) {
7644 got_ref_close(*base_branch_ref);
7645 *base_branch_ref = NULL;
7647 if (*fileindex) {
7648 got_fileindex_free(*fileindex);
7649 *fileindex = NULL;
7651 lock_worktree(worktree, LOCK_SH);
7653 return err;
7656 const struct got_error *
7657 got_worktree_integrate_continue(struct got_worktree *worktree,
7658 struct got_fileindex *fileindex, struct got_repository *repo,
7659 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7660 got_worktree_checkout_cb progress_cb, void *progress_arg,
7661 got_cancel_cb cancel_cb, void *cancel_arg)
7663 const struct got_error *err = NULL, *sync_err, *unlockerr;
7664 char *fileindex_path = NULL;
7665 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7666 struct got_commit_object *commit = NULL;
7668 err = get_fileindex_path(&fileindex_path, worktree);
7669 if (err)
7670 goto done;
7672 err = got_ref_resolve(&commit_id, repo, branch_ref);
7673 if (err)
7674 goto done;
7676 err = got_object_open_as_commit(&commit, repo, commit_id);
7677 if (err)
7678 goto done;
7680 err = got_object_id_by_path(&tree_id, repo, commit,
7681 worktree->path_prefix);
7682 if (err)
7683 goto done;
7685 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7686 if (err)
7687 goto done;
7689 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7690 progress_cb, progress_arg, cancel_cb, cancel_arg);
7691 if (err)
7692 goto sync;
7694 err = got_ref_change_ref(base_branch_ref, commit_id);
7695 if (err)
7696 goto sync;
7698 err = got_ref_write(base_branch_ref, repo);
7699 if (err)
7700 goto sync;
7702 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7703 sync:
7704 sync_err = sync_fileindex(fileindex, fileindex_path);
7705 if (sync_err && err == NULL)
7706 err = sync_err;
7708 done:
7709 unlockerr = got_ref_unlock(branch_ref);
7710 if (unlockerr && err == NULL)
7711 err = unlockerr;
7712 got_ref_close(branch_ref);
7714 unlockerr = got_ref_unlock(base_branch_ref);
7715 if (unlockerr && err == NULL)
7716 err = unlockerr;
7717 got_ref_close(base_branch_ref);
7719 got_fileindex_free(fileindex);
7720 free(fileindex_path);
7721 free(tree_id);
7722 if (commit)
7723 got_object_commit_close(commit);
7725 unlockerr = lock_worktree(worktree, LOCK_SH);
7726 if (unlockerr && err == NULL)
7727 err = unlockerr;
7728 return err;
7731 const struct got_error *
7732 got_worktree_integrate_abort(struct got_worktree *worktree,
7733 struct got_fileindex *fileindex, struct got_repository *repo,
7734 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7736 const struct got_error *err = NULL, *unlockerr = NULL;
7738 got_fileindex_free(fileindex);
7740 err = lock_worktree(worktree, LOCK_SH);
7742 unlockerr = got_ref_unlock(branch_ref);
7743 if (unlockerr && err == NULL)
7744 err = unlockerr;
7745 got_ref_close(branch_ref);
7747 unlockerr = got_ref_unlock(base_branch_ref);
7748 if (unlockerr && err == NULL)
7749 err = unlockerr;
7750 got_ref_close(base_branch_ref);
7752 return err;
7755 const struct got_error *
7756 got_worktree_merge_postpone(struct got_worktree *worktree,
7757 struct got_fileindex *fileindex)
7759 const struct got_error *err, *sync_err;
7760 char *fileindex_path = NULL;
7762 err = get_fileindex_path(&fileindex_path, worktree);
7763 if (err)
7764 goto done;
7766 sync_err = sync_fileindex(fileindex, fileindex_path);
7768 err = lock_worktree(worktree, LOCK_SH);
7769 if (sync_err && err == NULL)
7770 err = sync_err;
7771 done:
7772 got_fileindex_free(fileindex);
7773 free(fileindex_path);
7774 return err;
7777 static const struct got_error *
7778 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7780 const struct got_error *err;
7781 char *branch_refname = NULL, *commit_refname = NULL;
7783 err = get_merge_branch_ref_name(&branch_refname, worktree);
7784 if (err)
7785 goto done;
7786 err = delete_ref(branch_refname, repo);
7787 if (err)
7788 goto done;
7790 err = get_merge_commit_ref_name(&commit_refname, worktree);
7791 if (err)
7792 goto done;
7793 err = delete_ref(commit_refname, repo);
7794 if (err)
7795 goto done;
7797 done:
7798 free(branch_refname);
7799 free(commit_refname);
7800 return err;
7803 struct merge_commit_msg_arg {
7804 struct got_worktree *worktree;
7805 const char *branch_name;
7808 static const struct got_error *
7809 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7810 const char *diff_path, char **logmsg, void *arg)
7812 struct merge_commit_msg_arg *a = arg;
7814 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7815 got_worktree_get_head_ref_name(a->worktree)) == -1)
7816 return got_error_from_errno("asprintf");
7818 return NULL;
7822 const struct got_error *
7823 got_worktree_merge_branch(struct got_worktree *worktree,
7824 struct got_fileindex *fileindex,
7825 struct got_object_id *yca_commit_id,
7826 struct got_object_id *branch_tip,
7827 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7828 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7830 const struct got_error *err;
7831 char *fileindex_path = NULL;
7833 err = get_fileindex_path(&fileindex_path, worktree);
7834 if (err)
7835 goto done;
7837 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7838 worktree);
7839 if (err)
7840 goto done;
7842 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7843 branch_tip, repo, progress_cb, progress_arg,
7844 cancel_cb, cancel_arg);
7845 done:
7846 free(fileindex_path);
7847 return err;
7850 const struct got_error *
7851 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7852 struct got_worktree *worktree, struct got_fileindex *fileindex,
7853 const char *author, const char *committer, int allow_bad_symlinks,
7854 struct got_object_id *branch_tip, const char *branch_name,
7855 struct got_repository *repo,
7856 got_worktree_status_cb status_cb, void *status_arg)
7859 const struct got_error *err = NULL, *sync_err;
7860 struct got_pathlist_head commitable_paths;
7861 struct collect_commitables_arg cc_arg;
7862 struct got_pathlist_entry *pe;
7863 struct got_reference *head_ref = NULL;
7864 struct got_object_id *head_commit_id = NULL;
7865 int have_staged_files = 0;
7866 struct merge_commit_msg_arg mcm_arg;
7867 char *fileindex_path = NULL;
7869 memset(&cc_arg, 0, sizeof(cc_arg));
7870 *new_commit_id = NULL;
7872 TAILQ_INIT(&commitable_paths);
7874 err = get_fileindex_path(&fileindex_path, worktree);
7875 if (err)
7876 goto done;
7878 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7879 if (err)
7880 goto done;
7882 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7883 if (err)
7884 goto done;
7886 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7887 &have_staged_files);
7888 if (err && err->code != GOT_ERR_CANCELLED)
7889 goto done;
7890 if (have_staged_files) {
7891 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7892 goto done;
7895 cc_arg.commitable_paths = &commitable_paths;
7896 cc_arg.worktree = worktree;
7897 cc_arg.fileindex = fileindex;
7898 cc_arg.repo = repo;
7899 cc_arg.have_staged_files = have_staged_files;
7900 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7901 err = worktree_status(worktree, "", fileindex, repo,
7902 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7903 if (err)
7904 goto done;
7906 if (TAILQ_EMPTY(&commitable_paths)) {
7907 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7908 "merge of %s cannot proceed", branch_name);
7909 goto done;
7912 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7913 struct got_commitable *ct = pe->data;
7914 const char *ct_path = ct->in_repo_path;
7916 while (ct_path[0] == '/')
7917 ct_path++;
7918 err = check_out_of_date(ct_path, ct->status,
7919 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7920 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7921 if (err)
7922 goto done;
7926 mcm_arg.worktree = worktree;
7927 mcm_arg.branch_name = branch_name;
7928 err = commit_worktree(new_commit_id, &commitable_paths,
7929 head_commit_id, branch_tip, worktree, author, committer, NULL,
7930 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7931 if (err)
7932 goto done;
7934 err = update_fileindex_after_commit(worktree, &commitable_paths,
7935 *new_commit_id, fileindex, have_staged_files);
7936 sync_err = sync_fileindex(fileindex, fileindex_path);
7937 if (sync_err && err == NULL)
7938 err = sync_err;
7939 done:
7940 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7941 struct got_commitable *ct = pe->data;
7943 free_commitable(ct);
7945 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7946 free(fileindex_path);
7947 return err;
7950 const struct got_error *
7951 got_worktree_merge_complete(struct got_worktree *worktree,
7952 struct got_fileindex *fileindex, struct got_repository *repo)
7954 const struct got_error *err, *unlockerr, *sync_err;
7955 char *fileindex_path = NULL;
7957 err = delete_merge_refs(worktree, repo);
7958 if (err)
7959 goto done;
7961 err = get_fileindex_path(&fileindex_path, worktree);
7962 if (err)
7963 goto done;
7964 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7965 sync_err = sync_fileindex(fileindex, fileindex_path);
7966 if (sync_err && err == NULL)
7967 err = sync_err;
7968 done:
7969 got_fileindex_free(fileindex);
7970 free(fileindex_path);
7971 unlockerr = lock_worktree(worktree, LOCK_SH);
7972 if (unlockerr && err == NULL)
7973 err = unlockerr;
7974 return err;
7977 const struct got_error *
7978 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7979 struct got_repository *repo)
7981 const struct got_error *err;
7982 char *branch_refname = NULL;
7983 struct got_reference *branch_ref = NULL;
7985 *in_progress = 0;
7987 err = get_merge_branch_ref_name(&branch_refname, worktree);
7988 if (err)
7989 return err;
7990 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7991 free(branch_refname);
7992 if (err) {
7993 if (err->code != GOT_ERR_NOT_REF)
7994 return err;
7995 } else
7996 *in_progress = 1;
7998 return NULL;
8001 const struct got_error *got_worktree_merge_prepare(
8002 struct got_fileindex **fileindex, struct got_worktree *worktree,
8003 struct got_reference *branch, struct got_repository *repo)
8005 const struct got_error *err = NULL;
8006 char *fileindex_path = NULL;
8007 char *branch_refname = NULL, *commit_refname = NULL;
8008 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8009 struct got_reference *commit_ref = NULL;
8010 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8011 struct check_rebase_ok_arg ok_arg;
8013 *fileindex = NULL;
8015 err = lock_worktree(worktree, LOCK_EX);
8016 if (err)
8017 return err;
8019 err = open_fileindex(fileindex, &fileindex_path, worktree);
8020 if (err)
8021 goto done;
8023 /* Preconditions are the same as for rebase. */
8024 ok_arg.worktree = worktree;
8025 ok_arg.repo = repo;
8026 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8027 &ok_arg);
8028 if (err)
8029 goto done;
8031 err = get_merge_branch_ref_name(&branch_refname, worktree);
8032 if (err)
8033 return err;
8035 err = get_merge_commit_ref_name(&commit_refname, worktree);
8036 if (err)
8037 return err;
8039 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8040 0);
8041 if (err)
8042 goto done;
8044 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8045 if (err)
8046 goto done;
8048 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8049 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8050 goto done;
8053 err = got_ref_resolve(&branch_tip, repo, branch);
8054 if (err)
8055 goto done;
8057 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8058 if (err)
8059 goto done;
8060 err = got_ref_write(branch_ref, repo);
8061 if (err)
8062 goto done;
8064 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8065 if (err)
8066 goto done;
8067 err = got_ref_write(commit_ref, repo);
8068 if (err)
8069 goto done;
8071 done:
8072 free(branch_refname);
8073 free(commit_refname);
8074 free(fileindex_path);
8075 if (branch_ref)
8076 got_ref_close(branch_ref);
8077 if (commit_ref)
8078 got_ref_close(commit_ref);
8079 if (wt_branch)
8080 got_ref_close(wt_branch);
8081 free(wt_branch_tip);
8082 if (err) {
8083 if (*fileindex) {
8084 got_fileindex_free(*fileindex);
8085 *fileindex = NULL;
8087 lock_worktree(worktree, LOCK_SH);
8089 return err;
8092 const struct got_error *
8093 got_worktree_merge_continue(char **branch_name,
8094 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8095 struct got_worktree *worktree, struct got_repository *repo)
8097 const struct got_error *err;
8098 char *commit_refname = NULL, *branch_refname = NULL;
8099 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8100 char *fileindex_path = NULL;
8101 int have_staged_files = 0;
8103 *branch_name = NULL;
8104 *branch_tip = NULL;
8105 *fileindex = NULL;
8107 err = lock_worktree(worktree, LOCK_EX);
8108 if (err)
8109 return err;
8111 err = open_fileindex(fileindex, &fileindex_path, worktree);
8112 if (err)
8113 goto done;
8115 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8116 &have_staged_files);
8117 if (err && err->code != GOT_ERR_CANCELLED)
8118 goto done;
8119 if (have_staged_files) {
8120 err = got_error(GOT_ERR_STAGED_PATHS);
8121 goto done;
8124 err = get_merge_branch_ref_name(&branch_refname, worktree);
8125 if (err)
8126 goto done;
8128 err = get_merge_commit_ref_name(&commit_refname, worktree);
8129 if (err)
8130 goto done;
8132 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8133 if (err)
8134 goto done;
8136 if (!got_ref_is_symbolic(branch_ref)) {
8137 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8138 "%s is not a symbolic reference",
8139 got_ref_get_name(branch_ref));
8140 goto done;
8142 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8143 if (*branch_name == NULL) {
8144 err = got_error_from_errno("strdup");
8145 goto done;
8148 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8149 if (err)
8150 goto done;
8152 err = got_ref_resolve(branch_tip, repo, commit_ref);
8153 if (err)
8154 goto done;
8155 done:
8156 free(commit_refname);
8157 free(branch_refname);
8158 free(fileindex_path);
8159 if (commit_ref)
8160 got_ref_close(commit_ref);
8161 if (branch_ref)
8162 got_ref_close(branch_ref);
8163 if (err) {
8164 if (*branch_name) {
8165 free(*branch_name);
8166 *branch_name = NULL;
8168 free(*branch_tip);
8169 *branch_tip = NULL;
8170 if (*fileindex) {
8171 got_fileindex_free(*fileindex);
8172 *fileindex = NULL;
8174 lock_worktree(worktree, LOCK_SH);
8176 return err;
8179 const struct got_error *
8180 got_worktree_merge_abort(struct got_worktree *worktree,
8181 struct got_fileindex *fileindex, struct got_repository *repo,
8182 got_worktree_checkout_cb progress_cb, void *progress_arg)
8184 const struct got_error *err, *unlockerr, *sync_err;
8185 struct got_object_id *commit_id = NULL;
8186 struct got_commit_object *commit = NULL;
8187 char *fileindex_path = NULL;
8188 struct revert_file_args rfa;
8189 struct got_object_id *tree_id = NULL;
8191 err = got_object_open_as_commit(&commit, repo,
8192 worktree->base_commit_id);
8193 if (err)
8194 goto done;
8196 err = got_object_id_by_path(&tree_id, repo, commit,
8197 worktree->path_prefix);
8198 if (err)
8199 goto done;
8201 err = delete_merge_refs(worktree, repo);
8202 if (err)
8203 goto done;
8205 err = get_fileindex_path(&fileindex_path, worktree);
8206 if (err)
8207 goto done;
8209 rfa.worktree = worktree;
8210 rfa.fileindex = fileindex;
8211 rfa.progress_cb = progress_cb;
8212 rfa.progress_arg = progress_arg;
8213 rfa.patch_cb = NULL;
8214 rfa.patch_arg = NULL;
8215 rfa.repo = repo;
8216 rfa.unlink_added_files = 1;
8217 err = worktree_status(worktree, "", fileindex, repo,
8218 revert_file, &rfa, NULL, NULL, 1, 0);
8219 if (err)
8220 goto sync;
8222 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8223 repo, progress_cb, progress_arg, NULL, NULL);
8224 sync:
8225 sync_err = sync_fileindex(fileindex, fileindex_path);
8226 if (sync_err && err == NULL)
8227 err = sync_err;
8228 done:
8229 free(tree_id);
8230 free(commit_id);
8231 if (commit)
8232 got_object_commit_close(commit);
8233 if (fileindex)
8234 got_fileindex_free(fileindex);
8235 free(fileindex_path);
8237 unlockerr = lock_worktree(worktree, LOCK_SH);
8238 if (unlockerr && err == NULL)
8239 err = unlockerr;
8240 return err;
8243 struct check_stage_ok_arg {
8244 struct got_object_id *head_commit_id;
8245 struct got_worktree *worktree;
8246 struct got_fileindex *fileindex;
8247 struct got_repository *repo;
8248 int have_changes;
8251 static const struct got_error *
8252 check_stage_ok(void *arg, unsigned char status,
8253 unsigned char staged_status, const char *relpath,
8254 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8255 struct got_object_id *commit_id, int dirfd, const char *de_name)
8257 struct check_stage_ok_arg *a = arg;
8258 const struct got_error *err = NULL;
8259 struct got_fileindex_entry *ie;
8260 struct got_object_id base_commit_id;
8261 struct got_object_id *base_commit_idp = NULL;
8262 char *in_repo_path = NULL, *p;
8264 if (status == GOT_STATUS_UNVERSIONED ||
8265 status == GOT_STATUS_NO_CHANGE)
8266 return NULL;
8267 if (status == GOT_STATUS_NONEXISTENT)
8268 return got_error_set_errno(ENOENT, relpath);
8270 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8271 if (ie == NULL)
8272 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8274 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8275 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8276 relpath) == -1)
8277 return got_error_from_errno("asprintf");
8279 if (got_fileindex_entry_has_commit(ie)) {
8280 base_commit_idp = got_fileindex_entry_get_commit_id(
8281 &base_commit_id, ie);
8284 if (status == GOT_STATUS_CONFLICT) {
8285 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8286 goto done;
8287 } else if (status != GOT_STATUS_ADD &&
8288 status != GOT_STATUS_MODIFY &&
8289 status != GOT_STATUS_DELETE) {
8290 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8291 goto done;
8294 a->have_changes = 1;
8296 p = in_repo_path;
8297 while (p[0] == '/')
8298 p++;
8299 err = check_out_of_date(p, status, staged_status,
8300 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8301 GOT_ERR_STAGE_OUT_OF_DATE);
8302 done:
8303 free(in_repo_path);
8304 return err;
8307 struct stage_path_arg {
8308 struct got_worktree *worktree;
8309 struct got_fileindex *fileindex;
8310 struct got_repository *repo;
8311 got_worktree_status_cb status_cb;
8312 void *status_arg;
8313 got_worktree_patch_cb patch_cb;
8314 void *patch_arg;
8315 int staged_something;
8316 int allow_bad_symlinks;
8319 static const struct got_error *
8320 stage_path(void *arg, unsigned char status,
8321 unsigned char staged_status, const char *relpath,
8322 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8323 struct got_object_id *commit_id, int dirfd, const char *de_name)
8325 struct stage_path_arg *a = arg;
8326 const struct got_error *err = NULL;
8327 struct got_fileindex_entry *ie;
8328 char *ondisk_path = NULL, *path_content = NULL;
8329 uint32_t stage;
8330 struct got_object_id *new_staged_blob_id = NULL;
8331 struct stat sb;
8333 if (status == GOT_STATUS_UNVERSIONED)
8334 return NULL;
8336 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8337 if (ie == NULL)
8338 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8340 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8341 relpath)== -1)
8342 return got_error_from_errno("asprintf");
8344 switch (status) {
8345 case GOT_STATUS_ADD:
8346 case GOT_STATUS_MODIFY:
8347 /* XXX could sb.st_mode be passed in by our caller? */
8348 if (lstat(ondisk_path, &sb) == -1) {
8349 err = got_error_from_errno2("lstat", ondisk_path);
8350 break;
8352 if (a->patch_cb) {
8353 if (status == GOT_STATUS_ADD) {
8354 int choice = GOT_PATCH_CHOICE_NONE;
8355 err = (*a->patch_cb)(&choice, a->patch_arg,
8356 status, ie->path, NULL, 1, 1);
8357 if (err)
8358 break;
8359 if (choice != GOT_PATCH_CHOICE_YES)
8360 break;
8361 } else {
8362 err = create_patched_content(&path_content, 0,
8363 staged_blob_id ? staged_blob_id : blob_id,
8364 ondisk_path, dirfd, de_name, ie->path,
8365 a->repo, a->patch_cb, a->patch_arg);
8366 if (err || path_content == NULL)
8367 break;
8370 err = got_object_blob_create(&new_staged_blob_id,
8371 path_content ? path_content : ondisk_path, a->repo);
8372 if (err)
8373 break;
8374 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8375 SHA1_DIGEST_LENGTH);
8376 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8377 stage = GOT_FILEIDX_STAGE_ADD;
8378 else
8379 stage = GOT_FILEIDX_STAGE_MODIFY;
8380 got_fileindex_entry_stage_set(ie, stage);
8381 if (S_ISLNK(sb.st_mode)) {
8382 int is_bad_symlink = 0;
8383 if (!a->allow_bad_symlinks) {
8384 char target_path[PATH_MAX];
8385 ssize_t target_len;
8386 target_len = readlink(ondisk_path, target_path,
8387 sizeof(target_path));
8388 if (target_len == -1) {
8389 err = got_error_from_errno2("readlink",
8390 ondisk_path);
8391 break;
8393 err = is_bad_symlink_target(&is_bad_symlink,
8394 target_path, target_len, ondisk_path,
8395 a->worktree->root_path);
8396 if (err)
8397 break;
8398 if (is_bad_symlink) {
8399 err = got_error_path(ondisk_path,
8400 GOT_ERR_BAD_SYMLINK);
8401 break;
8404 if (is_bad_symlink)
8405 got_fileindex_entry_staged_filetype_set(ie,
8406 GOT_FILEIDX_MODE_BAD_SYMLINK);
8407 else
8408 got_fileindex_entry_staged_filetype_set(ie,
8409 GOT_FILEIDX_MODE_SYMLINK);
8410 } else {
8411 got_fileindex_entry_staged_filetype_set(ie,
8412 GOT_FILEIDX_MODE_REGULAR_FILE);
8414 a->staged_something = 1;
8415 if (a->status_cb == NULL)
8416 break;
8417 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8418 get_staged_status(ie), relpath, blob_id,
8419 new_staged_blob_id, NULL, dirfd, de_name);
8420 if (err)
8421 break;
8423 * When staging the reverse of the staged diff,
8424 * implicitly unstage the file.
8426 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8427 sizeof(ie->blob_sha1)) == 0) {
8428 got_fileindex_entry_stage_set(ie,
8429 GOT_FILEIDX_STAGE_NONE);
8431 break;
8432 case GOT_STATUS_DELETE:
8433 if (staged_status == GOT_STATUS_DELETE)
8434 break;
8435 if (a->patch_cb) {
8436 int choice = GOT_PATCH_CHOICE_NONE;
8437 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8438 ie->path, NULL, 1, 1);
8439 if (err)
8440 break;
8441 if (choice == GOT_PATCH_CHOICE_NO)
8442 break;
8443 if (choice != GOT_PATCH_CHOICE_YES) {
8444 err = got_error(GOT_ERR_PATCH_CHOICE);
8445 break;
8448 stage = GOT_FILEIDX_STAGE_DELETE;
8449 got_fileindex_entry_stage_set(ie, stage);
8450 a->staged_something = 1;
8451 if (a->status_cb == NULL)
8452 break;
8453 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8454 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8455 de_name);
8456 break;
8457 case GOT_STATUS_NO_CHANGE:
8458 break;
8459 case GOT_STATUS_CONFLICT:
8460 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8461 break;
8462 case GOT_STATUS_NONEXISTENT:
8463 err = got_error_set_errno(ENOENT, relpath);
8464 break;
8465 default:
8466 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8467 break;
8470 if (path_content && unlink(path_content) == -1 && err == NULL)
8471 err = got_error_from_errno2("unlink", path_content);
8472 free(path_content);
8473 free(ondisk_path);
8474 free(new_staged_blob_id);
8475 return err;
8478 const struct got_error *
8479 got_worktree_stage(struct got_worktree *worktree,
8480 struct got_pathlist_head *paths,
8481 got_worktree_status_cb status_cb, void *status_arg,
8482 got_worktree_patch_cb patch_cb, void *patch_arg,
8483 int allow_bad_symlinks, struct got_repository *repo)
8485 const struct got_error *err = NULL, *sync_err, *unlockerr;
8486 struct got_pathlist_entry *pe;
8487 struct got_fileindex *fileindex = NULL;
8488 char *fileindex_path = NULL;
8489 struct got_reference *head_ref = NULL;
8490 struct got_object_id *head_commit_id = NULL;
8491 struct check_stage_ok_arg oka;
8492 struct stage_path_arg spa;
8494 err = lock_worktree(worktree, LOCK_EX);
8495 if (err)
8496 return err;
8498 err = got_ref_open(&head_ref, repo,
8499 got_worktree_get_head_ref_name(worktree), 0);
8500 if (err)
8501 goto done;
8502 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8503 if (err)
8504 goto done;
8505 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8506 if (err)
8507 goto done;
8509 /* Check pre-conditions before staging anything. */
8510 oka.head_commit_id = head_commit_id;
8511 oka.worktree = worktree;
8512 oka.fileindex = fileindex;
8513 oka.repo = repo;
8514 oka.have_changes = 0;
8515 TAILQ_FOREACH(pe, paths, entry) {
8516 err = worktree_status(worktree, pe->path, fileindex, repo,
8517 check_stage_ok, &oka, NULL, NULL, 1, 0);
8518 if (err)
8519 goto done;
8521 if (!oka.have_changes) {
8522 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8523 goto done;
8526 spa.worktree = worktree;
8527 spa.fileindex = fileindex;
8528 spa.repo = repo;
8529 spa.patch_cb = patch_cb;
8530 spa.patch_arg = patch_arg;
8531 spa.status_cb = status_cb;
8532 spa.status_arg = status_arg;
8533 spa.staged_something = 0;
8534 spa.allow_bad_symlinks = allow_bad_symlinks;
8535 TAILQ_FOREACH(pe, paths, entry) {
8536 err = worktree_status(worktree, pe->path, fileindex, repo,
8537 stage_path, &spa, NULL, NULL, 1, 0);
8538 if (err)
8539 goto done;
8541 if (!spa.staged_something) {
8542 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8543 goto done;
8546 sync_err = sync_fileindex(fileindex, fileindex_path);
8547 if (sync_err && err == NULL)
8548 err = sync_err;
8549 done:
8550 if (head_ref)
8551 got_ref_close(head_ref);
8552 free(head_commit_id);
8553 free(fileindex_path);
8554 if (fileindex)
8555 got_fileindex_free(fileindex);
8556 unlockerr = lock_worktree(worktree, LOCK_SH);
8557 if (unlockerr && err == NULL)
8558 err = unlockerr;
8559 return err;
8562 struct unstage_path_arg {
8563 struct got_worktree *worktree;
8564 struct got_fileindex *fileindex;
8565 struct got_repository *repo;
8566 got_worktree_checkout_cb progress_cb;
8567 void *progress_arg;
8568 got_worktree_patch_cb patch_cb;
8569 void *patch_arg;
8572 static const struct got_error *
8573 create_unstaged_content(char **path_unstaged_content,
8574 char **path_new_staged_content, struct got_object_id *blob_id,
8575 struct got_object_id *staged_blob_id, const char *relpath,
8576 struct got_repository *repo,
8577 got_worktree_patch_cb patch_cb, void *patch_arg)
8579 const struct got_error *err, *free_err;
8580 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8581 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8582 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8583 struct got_diffreg_result *diffreg_result = NULL;
8584 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8585 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8586 int fd1 = -1, fd2 = -1;
8588 *path_unstaged_content = NULL;
8589 *path_new_staged_content = NULL;
8591 err = got_object_id_str(&label1, blob_id);
8592 if (err)
8593 return err;
8595 fd1 = got_opentempfd();
8596 if (fd1 == -1) {
8597 err = got_error_from_errno("got_opentempfd");
8598 goto done;
8600 fd2 = got_opentempfd();
8601 if (fd2 == -1) {
8602 err = got_error_from_errno("got_opentempfd");
8603 goto done;
8606 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8607 if (err)
8608 goto done;
8610 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8611 if (err)
8612 goto done;
8614 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8615 if (err)
8616 goto done;
8618 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8619 fd2);
8620 if (err)
8621 goto done;
8623 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8624 if (err)
8625 goto done;
8627 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8628 if (err)
8629 goto done;
8631 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8632 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8633 if (err)
8634 goto done;
8636 err = got_opentemp_named(path_unstaged_content, &outfile,
8637 "got-unstaged-content", "");
8638 if (err)
8639 goto done;
8640 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8641 "got-new-staged-content", "");
8642 if (err)
8643 goto done;
8645 if (fseek(f1, 0L, SEEK_SET) == -1) {
8646 err = got_ferror(f1, GOT_ERR_IO);
8647 goto done;
8649 if (fseek(f2, 0L, SEEK_SET) == -1) {
8650 err = got_ferror(f2, GOT_ERR_IO);
8651 goto done;
8653 /* Count the number of actual changes in the diff result. */
8654 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8655 struct diff_chunk_context cc = {};
8656 diff_chunk_context_load_change(&cc, &nchunks_used,
8657 diffreg_result->result, n, 0);
8658 nchanges++;
8660 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8661 int choice;
8662 err = apply_or_reject_change(&choice, &nchunks_used,
8663 diffreg_result->result, n, relpath, f1, f2,
8664 &line_cur1, &line_cur2,
8665 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8666 if (err)
8667 goto done;
8668 if (choice == GOT_PATCH_CHOICE_YES)
8669 have_content = 1;
8670 else
8671 have_rejected_content = 1;
8672 if (choice == GOT_PATCH_CHOICE_QUIT)
8673 break;
8675 if (have_content || have_rejected_content)
8676 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8677 outfile, rejectfile);
8678 done:
8679 free(label1);
8680 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8681 err = got_error_from_errno("close");
8682 if (blob)
8683 got_object_blob_close(blob);
8684 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8685 err = got_error_from_errno("close");
8686 if (staged_blob)
8687 got_object_blob_close(staged_blob);
8688 free_err = got_diffreg_result_free(diffreg_result);
8689 if (free_err && err == NULL)
8690 err = free_err;
8691 if (f1 && fclose(f1) == EOF && err == NULL)
8692 err = got_error_from_errno2("fclose", path1);
8693 if (f2 && fclose(f2) == EOF && err == NULL)
8694 err = got_error_from_errno2("fclose", path2);
8695 if (outfile && fclose(outfile) == EOF && err == NULL)
8696 err = got_error_from_errno2("fclose", *path_unstaged_content);
8697 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8698 err = got_error_from_errno2("fclose", *path_new_staged_content);
8699 if (path1 && unlink(path1) == -1 && err == NULL)
8700 err = got_error_from_errno2("unlink", path1);
8701 if (path2 && unlink(path2) == -1 && err == NULL)
8702 err = got_error_from_errno2("unlink", path2);
8703 if (err || !have_content) {
8704 if (*path_unstaged_content &&
8705 unlink(*path_unstaged_content) == -1 && err == NULL)
8706 err = got_error_from_errno2("unlink",
8707 *path_unstaged_content);
8708 free(*path_unstaged_content);
8709 *path_unstaged_content = NULL;
8711 if (err || !have_content || !have_rejected_content) {
8712 if (*path_new_staged_content &&
8713 unlink(*path_new_staged_content) == -1 && err == NULL)
8714 err = got_error_from_errno2("unlink",
8715 *path_new_staged_content);
8716 free(*path_new_staged_content);
8717 *path_new_staged_content = NULL;
8719 free(path1);
8720 free(path2);
8721 return err;
8724 static const struct got_error *
8725 unstage_hunks(struct got_object_id *staged_blob_id,
8726 struct got_blob_object *blob_base,
8727 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8728 const char *ondisk_path, const char *label_orig,
8729 struct got_worktree *worktree, struct got_repository *repo,
8730 got_worktree_patch_cb patch_cb, void *patch_arg,
8731 got_worktree_checkout_cb progress_cb, void *progress_arg)
8733 const struct got_error *err = NULL;
8734 char *path_unstaged_content = NULL;
8735 char *path_new_staged_content = NULL;
8736 char *parent = NULL, *base_path = NULL;
8737 char *blob_base_path = NULL;
8738 struct got_object_id *new_staged_blob_id = NULL;
8739 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8740 struct stat sb;
8742 err = create_unstaged_content(&path_unstaged_content,
8743 &path_new_staged_content, blob_id, staged_blob_id,
8744 ie->path, repo, patch_cb, patch_arg);
8745 if (err)
8746 return err;
8748 if (path_unstaged_content == NULL)
8749 return NULL;
8751 if (path_new_staged_content) {
8752 err = got_object_blob_create(&new_staged_blob_id,
8753 path_new_staged_content, repo);
8754 if (err)
8755 goto done;
8758 f = fopen(path_unstaged_content, "re");
8759 if (f == NULL) {
8760 err = got_error_from_errno2("fopen",
8761 path_unstaged_content);
8762 goto done;
8764 if (fstat(fileno(f), &sb) == -1) {
8765 err = got_error_from_errno2("fstat", path_unstaged_content);
8766 goto done;
8768 if (got_fileindex_entry_staged_filetype_get(ie) ==
8769 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8770 char link_target[PATH_MAX];
8771 size_t r;
8772 r = fread(link_target, 1, sizeof(link_target), f);
8773 if (r == 0 && ferror(f)) {
8774 err = got_error_from_errno("fread");
8775 goto done;
8777 if (r >= sizeof(link_target)) { /* should not happen */
8778 err = got_error(GOT_ERR_NO_SPACE);
8779 goto done;
8781 link_target[r] = '\0';
8782 err = merge_symlink(worktree, blob_base,
8783 ondisk_path, ie->path, label_orig, link_target,
8784 worktree->base_commit_id, repo, progress_cb,
8785 progress_arg);
8786 } else {
8787 int local_changes_subsumed;
8789 err = got_path_dirname(&parent, ondisk_path);
8790 if (err)
8791 return err;
8793 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8794 parent) == -1) {
8795 err = got_error_from_errno("asprintf");
8796 base_path = NULL;
8797 goto done;
8800 err = got_opentemp_named(&blob_base_path, &f_base,
8801 base_path, "");
8802 if (err)
8803 goto done;
8804 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8805 blob_base);
8806 if (err)
8807 goto done;
8810 * In order the run a 3-way merge with a symlink we copy the symlink's
8811 * target path into a temporary file and use that file with diff3.
8813 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8814 err = dump_symlink_target_path_to_file(&f_deriv2,
8815 ondisk_path);
8816 if (err)
8817 goto done;
8818 } else {
8819 int fd;
8820 fd = open(ondisk_path,
8821 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8822 if (fd == -1) {
8823 err = got_error_from_errno2("open", ondisk_path);
8824 goto done;
8826 f_deriv2 = fdopen(fd, "r");
8827 if (f_deriv2 == NULL) {
8828 err = got_error_from_errno2("fdopen", ondisk_path);
8829 close(fd);
8830 goto done;
8834 err = merge_file(&local_changes_subsumed, worktree,
8835 f_base, f, f_deriv2, ondisk_path, ie->path,
8836 got_fileindex_perms_to_st(ie),
8837 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8838 repo, progress_cb, progress_arg);
8840 if (err)
8841 goto done;
8843 if (new_staged_blob_id) {
8844 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8845 SHA1_DIGEST_LENGTH);
8846 } else {
8847 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8848 got_fileindex_entry_staged_filetype_set(ie, 0);
8850 done:
8851 free(new_staged_blob_id);
8852 if (path_unstaged_content &&
8853 unlink(path_unstaged_content) == -1 && err == NULL)
8854 err = got_error_from_errno2("unlink", path_unstaged_content);
8855 if (path_new_staged_content &&
8856 unlink(path_new_staged_content) == -1 && err == NULL)
8857 err = got_error_from_errno2("unlink", path_new_staged_content);
8858 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8859 err = got_error_from_errno2("unlink", blob_base_path);
8860 if (f_base && fclose(f_base) == EOF && err == NULL)
8861 err = got_error_from_errno2("fclose", path_unstaged_content);
8862 if (f && fclose(f) == EOF && err == NULL)
8863 err = got_error_from_errno2("fclose", path_unstaged_content);
8864 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8865 err = got_error_from_errno2("fclose", ondisk_path);
8866 free(path_unstaged_content);
8867 free(path_new_staged_content);
8868 free(blob_base_path);
8869 free(parent);
8870 free(base_path);
8871 return err;
8874 static const struct got_error *
8875 unstage_path(void *arg, unsigned char status,
8876 unsigned char staged_status, const char *relpath,
8877 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8878 struct got_object_id *commit_id, int dirfd, const char *de_name)
8880 const struct got_error *err = NULL;
8881 struct unstage_path_arg *a = arg;
8882 struct got_fileindex_entry *ie;
8883 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8884 char *ondisk_path = NULL;
8885 char *id_str = NULL, *label_orig = NULL;
8886 int local_changes_subsumed;
8887 struct stat sb;
8888 int fd1 = -1, fd2 = -1;
8890 if (staged_status != GOT_STATUS_ADD &&
8891 staged_status != GOT_STATUS_MODIFY &&
8892 staged_status != GOT_STATUS_DELETE)
8893 return NULL;
8895 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8896 if (ie == NULL)
8897 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8899 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8900 == -1)
8901 return got_error_from_errno("asprintf");
8903 err = got_object_id_str(&id_str,
8904 commit_id ? commit_id : a->worktree->base_commit_id);
8905 if (err)
8906 goto done;
8907 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8908 id_str) == -1) {
8909 err = got_error_from_errno("asprintf");
8910 goto done;
8913 fd1 = got_opentempfd();
8914 if (fd1 == -1) {
8915 err = got_error_from_errno("got_opentempfd");
8916 goto done;
8918 fd2 = got_opentempfd();
8919 if (fd2 == -1) {
8920 err = got_error_from_errno("got_opentempfd");
8921 goto done;
8924 switch (staged_status) {
8925 case GOT_STATUS_MODIFY:
8926 err = got_object_open_as_blob(&blob_base, a->repo,
8927 blob_id, 8192, fd1);
8928 if (err)
8929 break;
8930 /* fall through */
8931 case GOT_STATUS_ADD:
8932 if (a->patch_cb) {
8933 if (staged_status == GOT_STATUS_ADD) {
8934 int choice = GOT_PATCH_CHOICE_NONE;
8935 err = (*a->patch_cb)(&choice, a->patch_arg,
8936 staged_status, ie->path, NULL, 1, 1);
8937 if (err)
8938 break;
8939 if (choice != GOT_PATCH_CHOICE_YES)
8940 break;
8941 } else {
8942 err = unstage_hunks(staged_blob_id,
8943 blob_base, blob_id, ie, ondisk_path,
8944 label_orig, a->worktree, a->repo,
8945 a->patch_cb, a->patch_arg,
8946 a->progress_cb, a->progress_arg);
8947 break; /* Done with this file. */
8950 err = got_object_open_as_blob(&blob_staged, a->repo,
8951 staged_blob_id, 8192, fd2);
8952 if (err)
8953 break;
8954 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8955 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8956 case GOT_FILEIDX_MODE_REGULAR_FILE:
8957 err = merge_blob(&local_changes_subsumed, a->worktree,
8958 blob_base, ondisk_path, relpath,
8959 got_fileindex_perms_to_st(ie), label_orig,
8960 blob_staged, commit_id ? commit_id :
8961 a->worktree->base_commit_id, a->repo,
8962 a->progress_cb, a->progress_arg);
8963 break;
8964 case GOT_FILEIDX_MODE_SYMLINK:
8965 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8966 char *staged_target;
8967 err = got_object_blob_read_to_str(
8968 &staged_target, blob_staged);
8969 if (err)
8970 goto done;
8971 err = merge_symlink(a->worktree, blob_base,
8972 ondisk_path, relpath, label_orig,
8973 staged_target, commit_id ? commit_id :
8974 a->worktree->base_commit_id,
8975 a->repo, a->progress_cb, a->progress_arg);
8976 free(staged_target);
8977 } else {
8978 err = merge_blob(&local_changes_subsumed,
8979 a->worktree, blob_base, ondisk_path,
8980 relpath, got_fileindex_perms_to_st(ie),
8981 label_orig, blob_staged,
8982 commit_id ? commit_id :
8983 a->worktree->base_commit_id, a->repo,
8984 a->progress_cb, a->progress_arg);
8986 break;
8987 default:
8988 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8989 break;
8991 if (err == NULL) {
8992 got_fileindex_entry_stage_set(ie,
8993 GOT_FILEIDX_STAGE_NONE);
8994 got_fileindex_entry_staged_filetype_set(ie, 0);
8996 break;
8997 case GOT_STATUS_DELETE:
8998 if (a->patch_cb) {
8999 int choice = GOT_PATCH_CHOICE_NONE;
9000 err = (*a->patch_cb)(&choice, a->patch_arg,
9001 staged_status, ie->path, NULL, 1, 1);
9002 if (err)
9003 break;
9004 if (choice == GOT_PATCH_CHOICE_NO)
9005 break;
9006 if (choice != GOT_PATCH_CHOICE_YES) {
9007 err = got_error(GOT_ERR_PATCH_CHOICE);
9008 break;
9011 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9012 got_fileindex_entry_staged_filetype_set(ie, 0);
9013 err = get_file_status(&status, &sb, ie, ondisk_path,
9014 dirfd, de_name, a->repo);
9015 if (err)
9016 break;
9017 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9018 break;
9020 done:
9021 free(ondisk_path);
9022 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9023 err = got_error_from_errno("close");
9024 if (blob_base)
9025 got_object_blob_close(blob_base);
9026 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9027 err = got_error_from_errno("close");
9028 if (blob_staged)
9029 got_object_blob_close(blob_staged);
9030 free(id_str);
9031 free(label_orig);
9032 return err;
9035 const struct got_error *
9036 got_worktree_unstage(struct got_worktree *worktree,
9037 struct got_pathlist_head *paths,
9038 got_worktree_checkout_cb progress_cb, void *progress_arg,
9039 got_worktree_patch_cb patch_cb, void *patch_arg,
9040 struct got_repository *repo)
9042 const struct got_error *err = NULL, *sync_err, *unlockerr;
9043 struct got_pathlist_entry *pe;
9044 struct got_fileindex *fileindex = NULL;
9045 char *fileindex_path = NULL;
9046 struct unstage_path_arg upa;
9048 err = lock_worktree(worktree, LOCK_EX);
9049 if (err)
9050 return err;
9052 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9053 if (err)
9054 goto done;
9056 upa.worktree = worktree;
9057 upa.fileindex = fileindex;
9058 upa.repo = repo;
9059 upa.progress_cb = progress_cb;
9060 upa.progress_arg = progress_arg;
9061 upa.patch_cb = patch_cb;
9062 upa.patch_arg = patch_arg;
9063 TAILQ_FOREACH(pe, paths, entry) {
9064 err = worktree_status(worktree, pe->path, fileindex, repo,
9065 unstage_path, &upa, NULL, NULL, 1, 0);
9066 if (err)
9067 goto done;
9070 sync_err = sync_fileindex(fileindex, fileindex_path);
9071 if (sync_err && err == NULL)
9072 err = sync_err;
9073 done:
9074 free(fileindex_path);
9075 if (fileindex)
9076 got_fileindex_free(fileindex);
9077 unlockerr = lock_worktree(worktree, LOCK_SH);
9078 if (unlockerr && err == NULL)
9079 err = unlockerr;
9080 return err;
9083 struct report_file_info_arg {
9084 struct got_worktree *worktree;
9085 got_worktree_path_info_cb info_cb;
9086 void *info_arg;
9087 struct got_pathlist_head *paths;
9088 got_cancel_cb cancel_cb;
9089 void *cancel_arg;
9092 static const struct got_error *
9093 report_file_info(void *arg, struct got_fileindex_entry *ie)
9095 struct report_file_info_arg *a = arg;
9096 struct got_pathlist_entry *pe;
9097 struct got_object_id blob_id, staged_blob_id, commit_id;
9098 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9099 struct got_object_id *commit_idp = NULL;
9100 int stage;
9102 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9103 return got_error(GOT_ERR_CANCELLED);
9105 TAILQ_FOREACH(pe, a->paths, entry) {
9106 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9107 got_path_is_child(ie->path, pe->path, pe->path_len))
9108 break;
9110 if (pe == NULL) /* not found */
9111 return NULL;
9113 if (got_fileindex_entry_has_blob(ie))
9114 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9115 stage = got_fileindex_entry_stage_get(ie);
9116 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9117 stage == GOT_FILEIDX_STAGE_ADD) {
9118 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9119 &staged_blob_id, ie);
9122 if (got_fileindex_entry_has_commit(ie))
9123 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9125 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9126 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9129 const struct got_error *
9130 got_worktree_path_info(struct got_worktree *worktree,
9131 struct got_pathlist_head *paths,
9132 got_worktree_path_info_cb info_cb, void *info_arg,
9133 got_cancel_cb cancel_cb, void *cancel_arg)
9136 const struct got_error *err = NULL, *unlockerr;
9137 struct got_fileindex *fileindex = NULL;
9138 char *fileindex_path = NULL;
9139 struct report_file_info_arg arg;
9141 err = lock_worktree(worktree, LOCK_SH);
9142 if (err)
9143 return err;
9145 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9146 if (err)
9147 goto done;
9149 arg.worktree = worktree;
9150 arg.info_cb = info_cb;
9151 arg.info_arg = info_arg;
9152 arg.paths = paths;
9153 arg.cancel_cb = cancel_cb;
9154 arg.cancel_arg = cancel_arg;
9155 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9156 &arg);
9157 done:
9158 free(fileindex_path);
9159 if (fileindex)
9160 got_fileindex_free(fileindex);
9161 unlockerr = lock_worktree(worktree, LOCK_UN);
9162 if (unlockerr && err == NULL)
9163 err = unlockerr;
9164 return err;
9167 static const struct got_error *
9168 patch_check_path(const char *p, char **path, unsigned char *status,
9169 unsigned char *staged_status, struct got_fileindex *fileindex,
9170 struct got_worktree *worktree, struct got_repository *repo)
9172 const struct got_error *err;
9173 struct got_fileindex_entry *ie;
9174 struct stat sb;
9175 char *ondisk_path = NULL;
9177 err = got_worktree_resolve_path(path, worktree, p);
9178 if (err)
9179 return err;
9181 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9182 *path[0] ? "/" : "", *path) == -1)
9183 return got_error_from_errno("asprintf");
9185 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9186 if (ie) {
9187 *staged_status = get_staged_status(ie);
9188 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9189 repo);
9190 if (err)
9191 goto done;
9192 } else {
9193 *staged_status = GOT_STATUS_NO_CHANGE;
9194 *status = GOT_STATUS_UNVERSIONED;
9195 if (lstat(ondisk_path, &sb) == -1) {
9196 if (errno != ENOENT) {
9197 err = got_error_from_errno2("lstat",
9198 ondisk_path);
9199 goto done;
9201 *status = GOT_STATUS_NONEXISTENT;
9205 done:
9206 free(ondisk_path);
9207 return err;
9210 static const struct got_error *
9211 patch_can_rm(const char *path, unsigned char status,
9212 unsigned char staged_status)
9214 if (status == GOT_STATUS_NONEXISTENT)
9215 return got_error_set_errno(ENOENT, path);
9216 if (status != GOT_STATUS_NO_CHANGE &&
9217 status != GOT_STATUS_ADD &&
9218 status != GOT_STATUS_MODIFY &&
9219 status != GOT_STATUS_MODE_CHANGE)
9220 return got_error_path(path, GOT_ERR_FILE_STATUS);
9221 if (staged_status == GOT_STATUS_DELETE)
9222 return got_error_path(path, GOT_ERR_FILE_STATUS);
9223 return NULL;
9226 static const struct got_error *
9227 patch_can_add(const char *path, unsigned char status)
9229 if (status != GOT_STATUS_NONEXISTENT)
9230 return got_error_path(path, GOT_ERR_FILE_STATUS);
9231 return NULL;
9234 static const struct got_error *
9235 patch_can_edit(const char *path, unsigned char status,
9236 unsigned char staged_status)
9238 if (status == GOT_STATUS_NONEXISTENT)
9239 return got_error_set_errno(ENOENT, path);
9240 if (status != GOT_STATUS_NO_CHANGE &&
9241 status != GOT_STATUS_ADD &&
9242 status != GOT_STATUS_MODIFY)
9243 return got_error_path(path, GOT_ERR_FILE_STATUS);
9244 if (staged_status == GOT_STATUS_DELETE)
9245 return got_error_path(path, GOT_ERR_FILE_STATUS);
9246 return NULL;
9249 const struct got_error *
9250 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9251 char **fileindex_path, struct got_worktree *worktree)
9253 return open_fileindex(fileindex, fileindex_path, worktree);
9256 const struct got_error *
9257 got_worktree_patch_check_path(const char *old, const char *new,
9258 char **oldpath, char **newpath, struct got_worktree *worktree,
9259 struct got_repository *repo, struct got_fileindex *fileindex)
9261 const struct got_error *err = NULL;
9262 int file_renamed = 0;
9263 unsigned char status_old, staged_status_old;
9264 unsigned char status_new, staged_status_new;
9266 *oldpath = NULL;
9267 *newpath = NULL;
9269 err = patch_check_path(old != NULL ? old : new, oldpath,
9270 &status_old, &staged_status_old, fileindex, worktree, repo);
9271 if (err)
9272 goto done;
9274 err = patch_check_path(new != NULL ? new : old, newpath,
9275 &status_new, &staged_status_new, fileindex, worktree, repo);
9276 if (err)
9277 goto done;
9279 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9280 file_renamed = 1;
9282 if (old != NULL && new == NULL)
9283 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9284 else if (file_renamed) {
9285 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9286 if (err == NULL)
9287 err = patch_can_add(*newpath, status_new);
9288 } else if (old == NULL)
9289 err = patch_can_add(*newpath, status_new);
9290 else
9291 err = patch_can_edit(*newpath, status_new, staged_status_new);
9293 done:
9294 if (err) {
9295 free(*oldpath);
9296 *oldpath = NULL;
9297 free(*newpath);
9298 *newpath = NULL;
9300 return err;
9303 const struct got_error *
9304 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9305 struct got_worktree *worktree, struct got_fileindex *fileindex,
9306 got_worktree_checkout_cb progress_cb, void *progress_arg)
9308 struct schedule_addition_args saa;
9310 memset(&saa, 0, sizeof(saa));
9311 saa.worktree = worktree;
9312 saa.fileindex = fileindex;
9313 saa.progress_cb = progress_cb;
9314 saa.progress_arg = progress_arg;
9315 saa.repo = repo;
9317 return worktree_status(worktree, path, fileindex, repo,
9318 schedule_addition, &saa, NULL, NULL, 1, 0);
9321 const struct got_error *
9322 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9323 struct got_worktree *worktree, struct got_fileindex *fileindex,
9324 got_worktree_delete_cb progress_cb, void *progress_arg)
9326 struct schedule_deletion_args sda;
9328 memset(&sda, 0, sizeof(sda));
9329 sda.worktree = worktree;
9330 sda.fileindex = fileindex;
9331 sda.progress_cb = progress_cb;
9332 sda.progress_arg = progress_arg;
9333 sda.repo = repo;
9334 sda.delete_local_mods = 0;
9335 sda.keep_on_disk = 0;
9336 sda.ignore_missing_paths = 0;
9337 sda.status_codes = NULL;
9339 return worktree_status(worktree, path, fileindex, repo,
9340 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9343 const struct got_error *
9344 got_worktree_patch_complete(struct got_fileindex *fileindex,
9345 const char *fileindex_path)
9347 const struct got_error *err = NULL;
9349 err = sync_fileindex(fileindex, fileindex_path);
9350 got_fileindex_free(fileindex);
9352 return err;