Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
71 {
72 const struct got_error *err = NULL;
73 char *path;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
79 free(path);
80 return err;
81 }
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
85 {
86 const struct got_error *err = NULL;
87 FILE *tmpfile = NULL;
88 char *tmppath = NULL;
89 char *path = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
93 path = NULL;
94 goto done;
95 }
97 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
98 if (err)
99 goto done;
101 if (content) {
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
105 goto done;
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
111 unlink(tmppath);
112 goto done;
115 done:
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
118 free(tmppath);
119 return err;
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
126 char *refstr = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
130 if (refstr == NULL)
131 return got_error_from_errno("got_ref_to_str");
132 } else {
133 refstr = strdup(got_ref_get_name(head_ref));
134 if (refstr == NULL)
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 free(refstr);
139 return err;
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
148 uuid_t uuid;
149 uint32_t uuid_status;
150 int obj_type;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
159 goto done;
162 err = got_ref_resolve(&commit_id, repo, head_ref);
163 if (err)
164 return err;
165 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (err)
167 return err;
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
179 goto done;
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
185 goto done;
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
189 goto done;
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
194 if (err)
195 goto done;
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
199 if (err)
200 goto done;
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
204 if (err)
205 goto done;
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
209 if (err)
210 goto done;
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 if (err)
213 goto done;
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
218 if (err)
219 goto done;
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
224 if (err)
225 goto done;
227 /* Generate UUID. */
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
231 goto done;
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
236 goto done;
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
239 if (err)
240 goto done;
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 if (err)
249 goto done;
251 done:
252 free(commit_id);
253 free(path_got);
254 free(formatstr);
255 free(absprefix);
256 free(basestr);
257 free(uuidstr);
258 return err;
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
273 free(absprefix);
274 return NULL;
277 const char *
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
293 path_got = NULL;
294 goto done;
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
300 goto done;
303 err = write_head_ref(path_got, head_ref);
304 if (err)
305 goto done;
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
309 done:
310 free(path_got);
311 if (err)
312 free(head_ref_name);
313 return err;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
328 char *id_str = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
334 path_got = NULL;
335 goto done;
338 err = got_object_open(&obj, repo, commit_id);
339 if (err)
340 return err;
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
344 goto done;
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
349 if (err)
350 goto done;
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
352 if (err)
353 goto done;
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
359 goto done;
361 done:
362 if (obj)
363 got_object_close(obj);
364 free(id_str);
365 free(path_got);
366 return err;
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
382 return NULL;
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
389 char *abspath;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 struct stat sb;
397 err = NULL;
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
405 free(abspath);
406 return err;
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
413 uint8_t fbuf1[8192];
414 uint8_t fbuf2[8192];
415 size_t flen1 = 0, flen2 = 0;
417 *same = 1;
419 for (;;) {
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
423 break;
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
428 break;
430 if (flen1 == 0) {
431 if (flen2 != 0)
432 *same = 0;
433 break;
434 } else if (flen2 == 0) {
435 if (flen1 != 0)
436 *same = 0;
437 break;
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
440 *same = 0;
441 break;
443 } else {
444 *same = 0;
445 break;
449 return err;
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
455 struct stat sb;
456 size_t size1, size2;
458 *same = 1;
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
462 size1 = sb.st_size;
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
466 size2 = sb.st_size;
468 if (size1 != size2) {
469 *same = 0;
470 return NULL;
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
484 uint8_t fbuf[65536];
485 size_t flen;
486 ssize_t outlen;
488 *outsize = 0;
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
493 for (;;) {
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 if (flen == 0) {
496 if (ferror(f))
497 return got_error_from_errno("fread");
498 if (feof(f))
499 break;
501 outlen = write(outfd, fbuf, flen);
502 if (outlen == -1)
503 return got_error_from_errno("write");
504 if (outlen != flen)
505 return got_error(GOT_ERR_IO);
506 *outsize += outlen;
509 return NULL;
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
526 *overlapcnt = 0;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 if (err)
530 return err;
532 if (same_content)
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
536 if (err)
537 return err;
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 if (err)
541 return err;
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
545 *overlapcnt = 1;
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
552 goto done;
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
556 goto done;
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 base_path_orig, "");
560 if (err)
561 goto done;
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv, "");
564 if (err)
565 goto done;
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2, "");
568 if (err)
569 goto done;
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 if (err)
572 goto done;
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 if (err)
575 goto done;
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (err)
578 goto done;
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
582 goto done;
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
588 path_deriv) < 0) {
589 err = got_error_from_errno("dprintf");
590 goto done;
592 if (size_orig > 0) {
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
597 path_orig) < 0) {
598 err = got_error_from_errno("dprintf");
599 goto done;
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
604 path_deriv2,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
609 goto done;
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 done:
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 err == NULL)
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
625 free(path_orig);
626 free(path_deriv);
627 free(path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
631 return err;
634 /*
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
640 */
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
650 int merged_fd = -1;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
653 int overlapcnt = 0;
654 char *parent = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
659 if (err)
660 return err;
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
664 goto done;
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
668 if (err)
669 goto done;
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 if (err) {
674 if (err->code != GOT_ERR_FILE_BINARY)
675 goto done;
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 ondisk_path);
679 if (err)
680 goto done;
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
685 if (err)
686 goto done;
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
690 goto done;
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
696 goto done;
698 merged_fd = -1;
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
703 f_merged);
704 if (err)
705 goto done;
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
710 goto done;
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
715 ondisk_path);
716 goto done;
718 done:
719 if (err) {
720 if (merged_path)
721 unlink(merged_path);
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
727 free(merged_path);
728 free(base_path);
729 free(parent);
730 return err;
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
735 size_t target_len)
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
742 ondisk_path);
743 return NULL;
746 /*
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
757 */
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 FILE *f = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
768 if (err)
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
774 goto done;
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
778 if (err)
779 goto done;
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
783 goto done;
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
796 goto done;
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
801 goto done;
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
805 goto done;
807 done:
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
810 free(path);
811 free(id_str);
812 free(label_deriv);
813 return err;
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
823 /*
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
828 */
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
838 struct stat sb;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
851 ondisk_path);
852 goto done;
854 ondisk_target[ondisk_len] = '\0';
856 if (blob_orig) {
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 if (err)
859 goto done;
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 path);
883 } else {
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
888 if (err)
889 goto done;
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 path);
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
897 if (err)
898 goto done;
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 path);
906 } else {
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 path);
917 done:
918 free(ancestor_target);
919 return err;
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
927 ssize_t target_len;
928 size_t n;
929 FILE *f;
931 *outfile = NULL;
933 f = got_opentemp();
934 if (f == NULL)
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
939 goto done;
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
944 goto done;
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
948 goto done;
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
952 goto done;
954 done:
955 if (err)
956 fclose(f);
957 else
958 *outfile = f;
959 return err;
962 /*
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
966 */
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
984 if (err)
985 return err;
987 if (blob_orig) {
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 parent) == -1) {
990 err = got_error_from_errno("asprintf");
991 base_path = NULL;
992 goto done;
995 err = got_opentemp_named(&blob_orig_path, &f_orig,
996 base_path, "");
997 if (err)
998 goto done;
999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 blob_orig);
1001 if (err)
1002 goto done;
1003 free(base_path);
1004 } else {
1006 * No common ancestor exists. This is an "add vs add" conflict
1007 * and we simply use an empty ancestor file to make both files
1008 * appear in the merged result in their entirety.
1010 f_orig = got_opentemp();
1011 if (f_orig == NULL) {
1012 err = got_error_from_errno("got_opentemp");
1013 goto done;
1017 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 err = got_error_from_errno("asprintf");
1019 base_path = NULL;
1020 goto done;
1023 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 if (err)
1025 goto done;
1026 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1027 blob_deriv);
1028 if (err)
1029 goto done;
1031 err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 if (err)
1033 goto done;
1034 if (asprintf(&label_deriv, "%s: commit %s",
1035 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 err = got_error_from_errno("asprintf");
1037 goto done;
1041 * In order the run a 3-way merge with a symlink we copy the symlink's
1042 * target path into a temporary file and use that file with diff3.
1044 if (S_ISLNK(st_mode)) {
1045 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1046 if (err)
1047 goto done;
1048 } else {
1049 int fd;
1050 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 if (fd == -1) {
1052 err = got_error_from_errno2("open", ondisk_path);
1053 goto done;
1055 f_deriv2 = fdopen(fd, "r");
1056 if (f_deriv2 == NULL) {
1057 err = got_error_from_errno2("fdopen", ondisk_path);
1058 close(fd);
1059 goto done;
1063 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 done:
1067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 err = got_error_from_errno("fclose");
1073 free(base_path);
1074 if (blob_orig_path) {
1075 unlink(blob_orig_path);
1076 free(blob_orig_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1082 free(id_str);
1083 free(label_deriv);
1084 free(parent);
1085 return err;
1088 static const struct got_error *
1089 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 int wt_fd, const char *path, struct got_object_id *blob_id)
1093 const struct got_error *err = NULL;
1094 struct got_fileindex_entry *new_ie;
1096 *new_iep = NULL;
1098 err = got_fileindex_entry_alloc(&new_ie, path);
1099 if (err)
1100 return err;
1102 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 blob_id->sha1, base_commit_id->sha1, 1);
1104 if (err)
1105 goto done;
1107 err = got_fileindex_entry_add(fileindex, new_ie);
1108 done:
1109 if (err)
1110 got_fileindex_entry_free(new_ie);
1111 else
1112 *new_iep = new_ie;
1113 return err;
1116 static mode_t
1117 get_ondisk_perms(int executable, mode_t st_mode)
1119 mode_t xbits = S_IXUSR;
1121 if (executable) {
1122 /* Map read bits to execute bits. */
1123 if (st_mode & S_IRGRP)
1124 xbits |= S_IXGRP;
1125 if (st_mode & S_IROTH)
1126 xbits |= S_IXOTH;
1127 return st_mode | xbits;
1130 return st_mode;
1133 static mode_t
1134 apply_umask(mode_t mode)
1136 mode_t um;
1138 um = umask(000);
1139 umask(um);
1140 return mode & ~um;
1143 /* forward declaration */
1144 static const struct got_error *
1145 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 const char *path, mode_t te_mode, mode_t st_mode,
1147 struct got_blob_object *blob, int restoring_missing_file,
1148 int reverting_versioned_file, int installing_bad_symlink,
1149 int path_is_unversioned, struct got_repository *repo,
1150 got_worktree_checkout_cb progress_cb, void *progress_arg);
1153 * This function assumes that the provided symlink target points at a
1154 * safe location in the work tree!
1156 static const struct got_error *
1157 replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 const char *target_path, size_t target_len)
1160 const struct got_error *err = NULL;
1161 ssize_t elen;
1162 char etarget[PATH_MAX];
1163 int fd;
1165 *did_something = 0;
1168 * "Bad" symlinks (those pointing outside the work tree or into the
1169 * .got directory) are installed in the work tree as a regular file
1170 * which contains the bad symlink target path.
1171 * The new symlink target has already been checked for safety by our
1172 * caller. If we can successfully open a regular file then we simply
1173 * replace this file with a symlink below.
1175 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 if (fd == -1) {
1177 if (!got_err_open_nofollow_on_symlink())
1178 return got_error_from_errno2("open", ondisk_path);
1180 /* We are updating an existing on-disk symlink. */
1181 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 if (elen == -1)
1183 return got_error_from_errno2("readlink", ondisk_path);
1185 if (elen == target_len &&
1186 memcmp(etarget, target_path, target_len) == 0)
1187 return NULL; /* nothing to do */
1190 *did_something = 1;
1191 err = update_symlink(ondisk_path, target_path, target_len);
1192 if (fd != -1 && close(fd) == -1 && err == NULL)
1193 err = got_error_from_errno2("close", ondisk_path);
1194 return err;
1197 static const struct got_error *
1198 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 const struct got_error *err = NULL;
1202 char canonpath[PATH_MAX];
1203 char *path_got = NULL;
1205 *is_bad_symlink = 0;
1207 if (target_len >= sizeof(canonpath)) {
1208 *is_bad_symlink = 1;
1209 return NULL;
1213 * We do not use realpath(3) to resolve the symlink's target
1214 * path because we don't want to resolve symlinks recursively.
1215 * Instead we make the path absolute and then canonicalize it.
1216 * Relative symlink target lookup should begin at the directory
1217 * in which the blob object is being installed.
1219 if (!got_path_is_absolute(target_path)) {
1220 char *abspath, *parent;
1221 err = got_path_dirname(&parent, ondisk_path);
1222 if (err)
1223 return err;
1224 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 free(parent);
1226 return got_error_from_errno("asprintf");
1228 free(parent);
1229 if (strlen(abspath) >= sizeof(canonpath)) {
1230 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 free(abspath);
1232 return err;
1234 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 free(abspath);
1236 if (err)
1237 return err;
1238 } else {
1239 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 if (err)
1241 return err;
1244 /* Only allow symlinks pointing at paths within the work tree. */
1245 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 *is_bad_symlink = 1;
1247 return NULL;
1250 /* Do not allow symlinks pointing into the .got directory. */
1251 if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 GOT_WORKTREE_GOT_DIR) == -1)
1253 return got_error_from_errno("asprintf");
1254 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 *is_bad_symlink = 1;
1257 free(path_got);
1258 return NULL;
1261 static const struct got_error *
1262 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 int restoring_missing_file, int reverting_versioned_file,
1265 int path_is_unversioned, int allow_bad_symlinks,
1266 struct got_repository *repo,
1267 got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 const struct got_error *err = NULL;
1270 char target_path[PATH_MAX];
1271 size_t len, target_len = 0;
1272 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 *is_bad_symlink = 0;
1278 * Blob object content specifies the target path of the link.
1279 * If a symbolic link cannot be installed we instead create
1280 * a regular file which contains the link target path stored
1281 * in the blob object.
1283 do {
1284 err = got_object_blob_read_block(&len, blob);
1285 if (err)
1286 return err;
1288 if (len + target_len >= sizeof(target_path)) {
1289 /* Path too long; install as a regular file. */
1290 *is_bad_symlink = 1;
1291 got_object_blob_rewind(blob);
1292 return install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 restoring_missing_file, reverting_versioned_file,
1295 1, path_is_unversioned, repo, progress_cb,
1296 progress_arg);
1298 if (len > 0) {
1299 /* Skip blob object header first time around. */
1300 memcpy(target_path + target_len, buf + hdrlen,
1301 len - hdrlen);
1302 target_len += len - hdrlen;
1303 hdrlen = 0;
1305 } while (len != 0);
1306 target_path[target_len] = '\0';
1308 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 ondisk_path, worktree->root_path);
1310 if (err)
1311 return err;
1313 if (*is_bad_symlink && !allow_bad_symlinks) {
1314 /* install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 path_is_unversioned, repo, progress_cb, progress_arg);
1320 return err;
1323 if (symlink(target_path, ondisk_path) == -1) {
1324 if (errno == EEXIST) {
1325 int symlink_replaced;
1326 if (path_is_unversioned) {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_UNVERSIONED, path);
1329 return err;
1331 err = replace_existing_symlink(&symlink_replaced,
1332 ondisk_path, target_path, target_len);
1333 if (err)
1334 return err;
1335 if (progress_cb) {
1336 if (symlink_replaced) {
1337 err = (*progress_cb)(progress_arg,
1338 reverting_versioned_file ?
1339 GOT_STATUS_REVERT :
1340 GOT_STATUS_UPDATE, path);
1341 } else {
1342 err = (*progress_cb)(progress_arg,
1343 GOT_STATUS_EXISTS, path);
1346 return err; /* Nothing else to do. */
1349 if (errno == ENOENT) {
1350 char *parent;
1351 err = got_path_dirname(&parent, ondisk_path);
1352 if (err)
1353 return err;
1354 err = add_dir_on_disk(worktree, parent);
1355 free(parent);
1356 if (err)
1357 return err;
1359 * Retry, and fall through to error handling
1360 * below if this second attempt fails.
1362 if (symlink(target_path, ondisk_path) != -1) {
1363 err = NULL; /* success */
1364 return err;
1368 /* Handle errors from first or second creation attempt. */
1369 if (errno == ENAMETOOLONG) {
1370 /* bad target path; install as a regular file */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 err = install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file, 1,
1376 path_is_unversioned, repo,
1377 progress_cb, progress_arg);
1378 } else if (errno == ENOTDIR) {
1379 err = got_error_path(ondisk_path,
1380 GOT_ERR_FILE_OBSTRUCTED);
1381 } else {
1382 err = got_error_from_errno3("symlink",
1383 target_path, ondisk_path);
1385 } else if (progress_cb)
1386 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 return err;
1391 static const struct got_error *
1392 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 const char *path, mode_t te_mode, mode_t st_mode,
1394 struct got_blob_object *blob, int restoring_missing_file,
1395 int reverting_versioned_file, int installing_bad_symlink,
1396 int path_is_unversioned, struct got_repository *repo,
1397 got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 const struct got_error *err = NULL;
1400 int fd = -1;
1401 size_t len, hdrlen;
1402 int update = 0;
1403 char *tmppath = NULL;
1404 mode_t mode;
1406 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 O_CLOEXEC, mode);
1409 if (fd == -1) {
1410 if (errno == ENOENT) {
1411 char *parent;
1412 err = got_path_dirname(&parent, path);
1413 if (err)
1414 return err;
1415 err = add_dir_on_disk(worktree, parent);
1416 free(parent);
1417 if (err)
1418 return err;
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 mode);
1422 if (fd == -1)
1423 return got_error_from_errno2("open",
1424 ondisk_path);
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1429 goto done;
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1436 goto done;
1437 } else {
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1439 ondisk_path, "");
1440 if (err)
1441 goto done;
1442 update = 1;
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1446 tmppath);
1447 goto done;
1450 } else
1451 return got_error_from_errno2("open", ondisk_path);
1454 if (progress_cb) {
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 path);
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 path);
1461 else
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 if (err)
1465 goto done;
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1469 do {
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1472 if (err)
1473 break;
1474 if (len > 0) {
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 if (outlen == -1) {
1478 err = got_error_from_errno("write");
1479 goto done;
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1482 goto done;
1484 hdrlen = 0;
1486 } while (len != 0);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1490 goto done;
1493 if (update) {
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1496 goto done;
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1500 ondisk_path);
1501 goto done;
1503 free(tmppath);
1504 tmppath = NULL;
1507 done:
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1512 free(tmppath);
1513 return err;
1516 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1517 static const struct got_error *
1518 get_modified_file_content_status(unsigned char *status, FILE *f)
1520 const struct got_error *err = NULL;
1521 const char *markers[3] = {
1522 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1523 GOT_DIFF_CONFLICT_MARKER_SEP,
1524 GOT_DIFF_CONFLICT_MARKER_END
1526 int i = 0;
1527 char *line = NULL;
1528 size_t linesize = 0;
1529 ssize_t linelen;
1531 while (*status == GOT_STATUS_MODIFY) {
1532 linelen = getline(&line, &linesize, f);
1533 if (linelen == -1) {
1534 if (feof(f))
1535 break;
1536 err = got_ferror(f, GOT_ERR_IO);
1537 break;
1540 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1541 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1542 == 0)
1543 *status = GOT_STATUS_CONFLICT;
1544 else
1545 i++;
1548 free(line);
1550 return err;
1553 static int
1554 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1556 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1557 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1560 static int
1561 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1563 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1564 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1565 ie->mtime_sec == sb->st_mtim.tv_sec &&
1566 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1567 ie->size == (sb->st_size & 0xffffffff) &&
1568 !xbit_differs(ie, sb->st_mode));
1571 static unsigned char
1572 get_staged_status(struct got_fileindex_entry *ie)
1574 switch (got_fileindex_entry_stage_get(ie)) {
1575 case GOT_FILEIDX_STAGE_ADD:
1576 return GOT_STATUS_ADD;
1577 case GOT_FILEIDX_STAGE_DELETE:
1578 return GOT_STATUS_DELETE;
1579 case GOT_FILEIDX_STAGE_MODIFY:
1580 return GOT_STATUS_MODIFY;
1581 default:
1582 return GOT_STATUS_NO_CHANGE;
1586 static const struct got_error *
1587 get_symlink_modification_status(unsigned char *status,
1588 struct got_fileindex_entry *ie, const char *abspath,
1589 int dirfd, const char *de_name, struct got_blob_object *blob)
1591 const struct got_error *err = NULL;
1592 char target_path[PATH_MAX];
1593 char etarget[PATH_MAX];
1594 ssize_t elen;
1595 size_t len, target_len = 0;
1596 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1597 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1599 *status = GOT_STATUS_NO_CHANGE;
1601 /* Blob object content specifies the target path of the link. */
1602 do {
1603 err = got_object_blob_read_block(&len, blob);
1604 if (err)
1605 return err;
1606 if (len + target_len >= sizeof(target_path)) {
1608 * Should not happen. The blob contents were OK
1609 * when this symlink was installed.
1611 return got_error(GOT_ERR_NO_SPACE);
1613 if (len > 0) {
1614 /* Skip blob object header first time around. */
1615 memcpy(target_path + target_len, buf + hdrlen,
1616 len - hdrlen);
1617 target_len += len - hdrlen;
1618 hdrlen = 0;
1620 } while (len != 0);
1621 target_path[target_len] = '\0';
1623 if (dirfd != -1) {
1624 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1625 if (elen == -1)
1626 return got_error_from_errno2("readlinkat", abspath);
1627 } else {
1628 elen = readlink(abspath, etarget, sizeof(etarget));
1629 if (elen == -1)
1630 return got_error_from_errno2("readlink", abspath);
1633 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1634 *status = GOT_STATUS_MODIFY;
1636 return NULL;
1639 static const struct got_error *
1640 get_file_status(unsigned char *status, struct stat *sb,
1641 struct got_fileindex_entry *ie, const char *abspath,
1642 int dirfd, const char *de_name, struct got_repository *repo)
1644 const struct got_error *err = NULL;
1645 struct got_object_id id;
1646 size_t hdrlen;
1647 int fd = -1, fd1 = -1;
1648 FILE *f = NULL;
1649 uint8_t fbuf[8192];
1650 struct got_blob_object *blob = NULL;
1651 size_t flen, blen;
1652 unsigned char staged_status = get_staged_status(ie);
1654 *status = GOT_STATUS_NO_CHANGE;
1655 memset(sb, 0, sizeof(*sb));
1658 * Whenever the caller provides a directory descriptor and a
1659 * directory entry name for the file, use them! This prevents
1660 * race conditions if filesystem paths change beneath our feet.
1662 if (dirfd != -1) {
1663 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1664 if (errno == ENOENT) {
1665 if (got_fileindex_entry_has_file_on_disk(ie))
1666 *status = GOT_STATUS_MISSING;
1667 else
1668 *status = GOT_STATUS_DELETE;
1669 goto done;
1671 err = got_error_from_errno2("fstatat", abspath);
1672 goto done;
1674 } else {
1675 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1676 if (fd == -1 && errno != ENOENT &&
1677 !got_err_open_nofollow_on_symlink())
1678 return got_error_from_errno2("open", abspath);
1679 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1680 if (lstat(abspath, sb) == -1)
1681 return got_error_from_errno2("lstat", abspath);
1682 } else if (fd == -1 || fstat(fd, sb) == -1) {
1683 if (errno == ENOENT) {
1684 if (got_fileindex_entry_has_file_on_disk(ie))
1685 *status = GOT_STATUS_MISSING;
1686 else
1687 *status = GOT_STATUS_DELETE;
1688 goto done;
1690 err = got_error_from_errno2("fstat", abspath);
1691 goto done;
1695 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1696 *status = GOT_STATUS_OBSTRUCTED;
1697 goto done;
1700 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1701 *status = GOT_STATUS_DELETE;
1702 goto done;
1703 } else if (!got_fileindex_entry_has_blob(ie) &&
1704 staged_status != GOT_STATUS_ADD) {
1705 *status = GOT_STATUS_ADD;
1706 goto done;
1709 if (!stat_info_differs(ie, sb))
1710 goto done;
1712 if (S_ISLNK(sb->st_mode) &&
1713 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1714 *status = GOT_STATUS_MODIFY;
1715 goto done;
1718 if (staged_status == GOT_STATUS_MODIFY ||
1719 staged_status == GOT_STATUS_ADD)
1720 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1721 else
1722 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1724 fd1 = got_opentempfd();
1725 if (fd1 == -1) {
1726 err = got_error_from_errno("got_opentempfd");
1727 goto done;
1729 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1730 if (err)
1731 goto done;
1733 if (S_ISLNK(sb->st_mode)) {
1734 err = get_symlink_modification_status(status, ie,
1735 abspath, dirfd, de_name, blob);
1736 goto done;
1739 if (dirfd != -1) {
1740 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1741 if (fd == -1) {
1742 err = got_error_from_errno2("openat", abspath);
1743 goto done;
1747 f = fdopen(fd, "r");
1748 if (f == NULL) {
1749 err = got_error_from_errno2("fdopen", abspath);
1750 goto done;
1752 fd = -1;
1753 hdrlen = got_object_blob_get_hdrlen(blob);
1754 for (;;) {
1755 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1756 err = got_object_blob_read_block(&blen, blob);
1757 if (err)
1758 goto done;
1759 /* Skip length of blob object header first time around. */
1760 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1761 if (flen == 0 && ferror(f)) {
1762 err = got_error_from_errno("fread");
1763 goto done;
1765 if (blen - hdrlen == 0) {
1766 if (flen != 0)
1767 *status = GOT_STATUS_MODIFY;
1768 break;
1769 } else if (flen == 0) {
1770 if (blen - hdrlen != 0)
1771 *status = GOT_STATUS_MODIFY;
1772 break;
1773 } else if (blen - hdrlen == flen) {
1774 /* Skip blob object header first time around. */
1775 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1776 *status = GOT_STATUS_MODIFY;
1777 break;
1779 } else {
1780 *status = GOT_STATUS_MODIFY;
1781 break;
1783 hdrlen = 0;
1786 if (*status == GOT_STATUS_MODIFY) {
1787 rewind(f);
1788 err = get_modified_file_content_status(status, f);
1789 } else if (xbit_differs(ie, sb->st_mode))
1790 *status = GOT_STATUS_MODE_CHANGE;
1791 done:
1792 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1793 err = got_error_from_errno("close");
1794 if (blob)
1795 got_object_blob_close(blob);
1796 if (f != NULL && fclose(f) == EOF && err == NULL)
1797 err = got_error_from_errno2("fclose", abspath);
1798 if (fd != -1 && close(fd) == -1 && err == NULL)
1799 err = got_error_from_errno2("close", abspath);
1800 return err;
1804 * Update timestamps in the file index if a file is unmodified and
1805 * we had to run a full content comparison to find out.
1807 static const struct got_error *
1808 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1809 struct got_fileindex_entry *ie, struct stat *sb)
1811 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1812 return got_fileindex_entry_update(ie, wt_fd, path,
1813 ie->blob_sha1, ie->commit_sha1, 1);
1815 return NULL;
1818 static const struct got_error *
1819 update_blob(struct got_worktree *worktree,
1820 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1821 struct got_tree_entry *te, const char *path,
1822 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1823 void *progress_arg)
1825 const struct got_error *err = NULL;
1826 struct got_blob_object *blob = NULL;
1827 char *ondisk_path = NULL;
1828 unsigned char status = GOT_STATUS_NO_CHANGE;
1829 struct stat sb;
1830 int fd1 = -1, fd2 = -1;
1832 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1833 return got_error_from_errno("asprintf");
1835 if (ie) {
1836 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1837 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1838 goto done;
1840 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1841 repo);
1842 if (err)
1843 goto done;
1844 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1845 sb.st_mode = got_fileindex_perms_to_st(ie);
1846 } else {
1847 if (stat(ondisk_path, &sb) == -1) {
1848 if (errno != ENOENT) {
1849 err = got_error_from_errno2("stat",
1850 ondisk_path);
1851 goto done;
1853 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1854 status = GOT_STATUS_UNVERSIONED;
1855 } else {
1856 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1857 status = GOT_STATUS_UNVERSIONED;
1858 else
1859 status = GOT_STATUS_OBSTRUCTED;
1863 if (status == GOT_STATUS_OBSTRUCTED) {
1864 if (ie)
1865 got_fileindex_entry_mark_skipped(ie);
1866 err = (*progress_cb)(progress_arg, status, path);
1867 goto done;
1869 if (status == GOT_STATUS_CONFLICT) {
1870 if (ie)
1871 got_fileindex_entry_mark_skipped(ie);
1872 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1873 path);
1874 goto done;
1877 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1878 (S_ISLNK(te->mode) ||
1879 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1881 * This is a regular file or an installed bad symlink.
1882 * If the file index indicates that this file is already
1883 * up-to-date with respect to the repository we can skip
1884 * updating contents of this file.
1886 if (got_fileindex_entry_has_commit(ie) &&
1887 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1888 SHA1_DIGEST_LENGTH) == 0) {
1889 /* Same commit. */
1890 err = sync_timestamps(worktree->root_fd,
1891 path, status, ie, &sb);
1892 if (err)
1893 goto done;
1894 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1895 path);
1896 goto done;
1898 if (got_fileindex_entry_has_blob(ie) &&
1899 memcmp(ie->blob_sha1, te->id.sha1,
1900 SHA1_DIGEST_LENGTH) == 0) {
1901 /* Different commit but the same blob. */
1902 err = sync_timestamps(worktree->root_fd,
1903 path, status, ie, &sb);
1904 if (err)
1905 goto done;
1906 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1907 path);
1908 goto done;
1912 fd1 = got_opentempfd();
1913 if (fd1 == -1) {
1914 err = got_error_from_errno("got_opentempfd");
1915 goto done;
1917 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1918 if (err)
1919 goto done;
1921 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1922 int update_timestamps;
1923 struct got_blob_object *blob2 = NULL;
1924 char *label_orig = NULL;
1925 if (got_fileindex_entry_has_blob(ie)) {
1926 fd2 = got_opentempfd();
1927 if (fd2 == -1) {
1928 err = got_error_from_errno("got_opentempfd");
1929 goto done;
1931 struct got_object_id id2;
1932 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1933 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1934 fd2);
1935 if (err)
1936 goto done;
1938 if (got_fileindex_entry_has_commit(ie)) {
1939 char id_str[SHA1_DIGEST_STRING_LENGTH];
1940 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1941 sizeof(id_str)) == NULL) {
1942 err = got_error_path(id_str,
1943 GOT_ERR_BAD_OBJ_ID_STR);
1944 goto done;
1946 if (asprintf(&label_orig, "%s: commit %s",
1947 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1948 err = got_error_from_errno("asprintf");
1949 goto done;
1952 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1953 char *link_target;
1954 err = got_object_blob_read_to_str(&link_target, blob);
1955 if (err)
1956 goto done;
1957 err = merge_symlink(worktree, blob2, ondisk_path, path,
1958 label_orig, link_target, worktree->base_commit_id,
1959 repo, progress_cb, progress_arg);
1960 free(link_target);
1961 } else {
1962 err = merge_blob(&update_timestamps, worktree, blob2,
1963 ondisk_path, path, sb.st_mode, label_orig, blob,
1964 worktree->base_commit_id, repo,
1965 progress_cb, progress_arg);
1967 free(label_orig);
1968 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1969 err = got_error_from_errno("close");
1970 goto done;
1972 if (blob2)
1973 got_object_blob_close(blob2);
1974 if (err)
1975 goto done;
1977 * Do not update timestamps of files with local changes.
1978 * Otherwise, a future status walk would treat them as
1979 * unmodified files again.
1981 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1982 blob->id.sha1, worktree->base_commit_id->sha1,
1983 update_timestamps);
1984 } else if (status == GOT_STATUS_MODE_CHANGE) {
1985 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1986 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1987 } else if (status == GOT_STATUS_DELETE) {
1988 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1989 if (err)
1990 goto done;
1991 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1992 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1993 if (err)
1994 goto done;
1995 } else {
1996 int is_bad_symlink = 0;
1997 if (S_ISLNK(te->mode)) {
1998 err = install_symlink(&is_bad_symlink, worktree,
1999 ondisk_path, path, blob,
2000 status == GOT_STATUS_MISSING, 0,
2001 status == GOT_STATUS_UNVERSIONED, 0,
2002 repo, progress_cb, progress_arg);
2003 } else {
2004 err = install_blob(worktree, ondisk_path, path,
2005 te->mode, sb.st_mode, blob,
2006 status == GOT_STATUS_MISSING, 0, 0,
2007 status == GOT_STATUS_UNVERSIONED, repo,
2008 progress_cb, progress_arg);
2010 if (err)
2011 goto done;
2013 if (ie) {
2014 err = got_fileindex_entry_update(ie,
2015 worktree->root_fd, path, blob->id.sha1,
2016 worktree->base_commit_id->sha1, 1);
2017 } else {
2018 err = create_fileindex_entry(&ie, fileindex,
2019 worktree->base_commit_id, worktree->root_fd, path,
2020 &blob->id);
2022 if (err)
2023 goto done;
2025 if (is_bad_symlink) {
2026 got_fileindex_entry_filetype_set(ie,
2027 GOT_FILEIDX_MODE_BAD_SYMLINK);
2031 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2032 err = got_error_from_errno("close");
2033 goto done;
2035 got_object_blob_close(blob);
2036 done:
2037 free(ondisk_path);
2038 return err;
2041 static const struct got_error *
2042 remove_ondisk_file(const char *root_path, const char *path)
2044 const struct got_error *err = NULL;
2045 char *ondisk_path = NULL, *parent = NULL;
2047 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2048 return got_error_from_errno("asprintf");
2050 if (unlink(ondisk_path) == -1) {
2051 if (errno != ENOENT)
2052 err = got_error_from_errno2("unlink", ondisk_path);
2053 } else {
2054 size_t root_len = strlen(root_path);
2055 err = got_path_dirname(&parent, ondisk_path);
2056 if (err)
2057 goto done;
2058 while (got_path_cmp(parent, root_path,
2059 strlen(parent), root_len) != 0) {
2060 free(ondisk_path);
2061 ondisk_path = parent;
2062 parent = NULL;
2063 if (rmdir(ondisk_path) == -1) {
2064 if (errno != ENOTEMPTY)
2065 err = got_error_from_errno2("rmdir",
2066 ondisk_path);
2067 break;
2069 err = got_path_dirname(&parent, ondisk_path);
2070 if (err)
2071 break;
2074 done:
2075 free(ondisk_path);
2076 free(parent);
2077 return err;
2080 static const struct got_error *
2081 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2082 struct got_fileindex_entry *ie, struct got_repository *repo,
2083 got_worktree_checkout_cb progress_cb, void *progress_arg)
2085 const struct got_error *err = NULL;
2086 unsigned char status;
2087 struct stat sb;
2088 char *ondisk_path;
2090 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2091 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2093 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2094 == -1)
2095 return got_error_from_errno("asprintf");
2097 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2098 if (err)
2099 goto done;
2101 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2102 char ondisk_target[PATH_MAX];
2103 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2104 sizeof(ondisk_target));
2105 if (ondisk_len == -1) {
2106 err = got_error_from_errno2("readlink", ondisk_path);
2107 goto done;
2109 ondisk_target[ondisk_len] = '\0';
2110 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2111 NULL, NULL, /* XXX pass common ancestor info? */
2112 ondisk_target, ondisk_path);
2113 if (err)
2114 goto done;
2115 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2116 ie->path);
2117 goto done;
2120 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2121 status == GOT_STATUS_ADD) {
2122 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2123 if (err)
2124 goto done;
2126 * Preserve the working file and change the deleted blob's
2127 * entry into a schedule-add entry.
2129 err = got_fileindex_entry_update(ie, worktree->root_fd,
2130 ie->path, NULL, NULL, 0);
2131 } else {
2132 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2133 if (err)
2134 goto done;
2135 if (status == GOT_STATUS_NO_CHANGE) {
2136 err = remove_ondisk_file(worktree->root_path, ie->path);
2137 if (err)
2138 goto done;
2140 got_fileindex_entry_remove(fileindex, ie);
2142 done:
2143 free(ondisk_path);
2144 return err;
2147 struct diff_cb_arg {
2148 struct got_fileindex *fileindex;
2149 struct got_worktree *worktree;
2150 struct got_repository *repo;
2151 got_worktree_checkout_cb progress_cb;
2152 void *progress_arg;
2153 got_cancel_cb cancel_cb;
2154 void *cancel_arg;
2157 static const struct got_error *
2158 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2159 struct got_tree_entry *te, const char *parent_path)
2161 struct diff_cb_arg *a = arg;
2163 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2164 return got_error(GOT_ERR_CANCELLED);
2166 return update_blob(a->worktree, a->fileindex, ie, te,
2167 ie->path, a->repo, a->progress_cb, a->progress_arg);
2170 static const struct got_error *
2171 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2173 struct diff_cb_arg *a = arg;
2175 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2176 return got_error(GOT_ERR_CANCELLED);
2178 return delete_blob(a->worktree, a->fileindex, ie,
2179 a->repo, a->progress_cb, a->progress_arg);
2182 static const struct got_error *
2183 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2185 struct diff_cb_arg *a = arg;
2186 const struct got_error *err;
2187 char *path;
2189 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2190 return got_error(GOT_ERR_CANCELLED);
2192 if (got_object_tree_entry_is_submodule(te))
2193 return NULL;
2195 if (asprintf(&path, "%s%s%s", parent_path,
2196 parent_path[0] ? "/" : "", te->name)
2197 == -1)
2198 return got_error_from_errno("asprintf");
2200 if (S_ISDIR(te->mode))
2201 err = add_dir_on_disk(a->worktree, path);
2202 else
2203 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2204 a->repo, a->progress_cb, a->progress_arg);
2206 free(path);
2207 return err;
2210 const struct got_error *
2211 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2213 uint32_t uuid_status;
2215 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2216 if (uuid_status != uuid_s_ok) {
2217 *uuidstr = NULL;
2218 return got_error_uuid(uuid_status, "uuid_to_string");
2221 return NULL;
2224 static const struct got_error *
2225 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2227 const struct got_error *err = NULL;
2228 char *uuidstr = NULL;
2230 *refname = NULL;
2232 err = got_worktree_get_uuid(&uuidstr, worktree);
2233 if (err)
2234 return err;
2236 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2237 err = got_error_from_errno("asprintf");
2238 *refname = NULL;
2240 free(uuidstr);
2241 return err;
2244 const struct got_error *
2245 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2246 const char *prefix)
2248 return get_ref_name(refname, worktree, prefix);
2251 const struct got_error *
2252 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2254 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2257 static const struct got_error *
2258 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2260 return get_ref_name(refname, worktree,
2261 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2264 static const struct got_error *
2265 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2267 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2270 static const struct got_error *
2271 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2273 return get_ref_name(refname, worktree,
2274 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2277 static const struct got_error *
2278 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2280 return get_ref_name(refname, worktree,
2281 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2284 static const struct got_error *
2285 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2287 return get_ref_name(refname, worktree,
2288 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2291 static const struct got_error *
2292 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2294 return get_ref_name(refname, worktree,
2295 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2298 static const struct got_error *
2299 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2301 return get_ref_name(refname, worktree,
2302 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2305 static const struct got_error *
2306 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2308 return get_ref_name(refname, worktree,
2309 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2312 const struct got_error *
2313 got_worktree_get_histedit_script_path(char **path,
2314 struct got_worktree *worktree)
2316 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2317 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2318 *path = NULL;
2319 return got_error_from_errno("asprintf");
2321 return NULL;
2324 static const struct got_error *
2325 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree,
2328 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2331 static const struct got_error *
2332 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2334 return get_ref_name(refname, worktree,
2335 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2339 * Prevent Git's garbage collector from deleting our base commit by
2340 * setting a reference to our base commit's ID.
2342 static const struct got_error *
2343 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2345 const struct got_error *err = NULL;
2346 struct got_reference *ref = NULL;
2347 char *refname;
2349 err = got_worktree_get_base_ref_name(&refname, worktree);
2350 if (err)
2351 return err;
2353 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2354 if (err)
2355 goto done;
2357 err = got_ref_write(ref, repo);
2358 done:
2359 free(refname);
2360 if (ref)
2361 got_ref_close(ref);
2362 return err;
2365 static const struct got_error *
2366 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2368 const struct got_error *err = NULL;
2370 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2371 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2372 err = got_error_from_errno("asprintf");
2373 *fileindex_path = NULL;
2375 return err;
2379 static const struct got_error *
2380 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2381 struct got_worktree *worktree)
2383 const struct got_error *err = NULL;
2384 FILE *index = NULL;
2386 *fileindex_path = NULL;
2387 *fileindex = got_fileindex_alloc();
2388 if (*fileindex == NULL)
2389 return got_error_from_errno("got_fileindex_alloc");
2391 err = get_fileindex_path(fileindex_path, worktree);
2392 if (err)
2393 goto done;
2395 index = fopen(*fileindex_path, "rbe");
2396 if (index == NULL) {
2397 if (errno != ENOENT)
2398 err = got_error_from_errno2("fopen", *fileindex_path);
2399 } else {
2400 err = got_fileindex_read(*fileindex, index);
2401 if (fclose(index) == EOF && err == NULL)
2402 err = got_error_from_errno("fclose");
2404 done:
2405 if (err) {
2406 free(*fileindex_path);
2407 *fileindex_path = NULL;
2408 got_fileindex_free(*fileindex);
2409 *fileindex = NULL;
2411 return err;
2414 struct bump_base_commit_id_arg {
2415 struct got_object_id *base_commit_id;
2416 const char *path;
2417 size_t path_len;
2418 const char *entry_name;
2419 got_worktree_checkout_cb progress_cb;
2420 void *progress_arg;
2423 /* Bump base commit ID of all files within an updated part of the work tree. */
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 static const struct got_error *
2454 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2455 struct got_fileindex *fileindex,
2456 got_worktree_checkout_cb progress_cb, void *progress_arg)
2458 struct bump_base_commit_id_arg bbc_arg;
2460 bbc_arg.base_commit_id = worktree->base_commit_id;
2461 bbc_arg.entry_name = NULL;
2462 bbc_arg.path = "";
2463 bbc_arg.path_len = 0;
2464 bbc_arg.progress_cb = progress_cb;
2465 bbc_arg.progress_arg = progress_arg;
2467 return got_fileindex_for_each_entry_safe(fileindex,
2468 bump_base_commit_id, &bbc_arg);
2471 static const struct got_error *
2472 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2474 const struct got_error *err = NULL;
2475 char *new_fileindex_path = NULL;
2476 FILE *new_index = NULL;
2477 struct timespec timeout;
2479 err = got_opentemp_named(&new_fileindex_path, &new_index,
2480 fileindex_path, "");
2481 if (err)
2482 goto done;
2484 err = got_fileindex_write(fileindex, new_index);
2485 if (err)
2486 goto done;
2488 if (rename(new_fileindex_path, fileindex_path) != 0) {
2489 err = got_error_from_errno3("rename", new_fileindex_path,
2490 fileindex_path);
2491 unlink(new_fileindex_path);
2495 * Sleep for a short amount of time to ensure that files modified after
2496 * this program exits have a different time stamp from the one which
2497 * was recorded in the file index.
2499 timeout.tv_sec = 0;
2500 timeout.tv_nsec = 1;
2501 nanosleep(&timeout, NULL);
2502 done:
2503 if (new_index)
2504 fclose(new_index);
2505 free(new_fileindex_path);
2506 return err;
2509 static const struct got_error *
2510 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2511 struct got_object_id **tree_id, const char *wt_relpath,
2512 struct got_commit_object *base_commit, struct got_worktree *worktree,
2513 struct got_repository *repo)
2515 const struct got_error *err = NULL;
2516 struct got_object_id *id = NULL;
2517 char *in_repo_path = NULL;
2518 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2520 *entry_type = GOT_OBJ_TYPE_ANY;
2521 *tree_relpath = NULL;
2522 *tree_id = NULL;
2524 if (wt_relpath[0] == '\0') {
2525 /* Check out all files within the work tree. */
2526 *entry_type = GOT_OBJ_TYPE_TREE;
2527 *tree_relpath = strdup("");
2528 if (*tree_relpath == NULL) {
2529 err = got_error_from_errno("strdup");
2530 goto done;
2532 err = got_object_id_by_path(tree_id, repo, base_commit,
2533 worktree->path_prefix);
2534 if (err)
2535 goto done;
2536 return NULL;
2539 /* Check out a subset of files in the work tree. */
2541 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2542 is_root_wt ? "" : "/", wt_relpath) == -1) {
2543 err = got_error_from_errno("asprintf");
2544 goto done;
2547 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2548 if (err)
2549 goto done;
2551 free(in_repo_path);
2552 in_repo_path = NULL;
2554 err = got_object_get_type(entry_type, repo, id);
2555 if (err)
2556 goto done;
2558 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2559 /* Check out a single file. */
2560 if (strchr(wt_relpath, '/') == NULL) {
2561 /* Check out a single file in work tree's root dir. */
2562 in_repo_path = strdup(worktree->path_prefix);
2563 if (in_repo_path == NULL) {
2564 err = got_error_from_errno("strdup");
2565 goto done;
2567 *tree_relpath = strdup("");
2568 if (*tree_relpath == NULL) {
2569 err = got_error_from_errno("strdup");
2570 goto done;
2572 } else {
2573 /* Check out a single file in a subdirectory. */
2574 err = got_path_dirname(tree_relpath, wt_relpath);
2575 if (err)
2576 return err;
2577 if (asprintf(&in_repo_path, "%s%s%s",
2578 worktree->path_prefix, is_root_wt ? "" : "/",
2579 *tree_relpath) == -1) {
2580 err = got_error_from_errno("asprintf");
2581 goto done;
2584 err = got_object_id_by_path(tree_id, repo,
2585 base_commit, in_repo_path);
2586 } else {
2587 /* Check out all files within a subdirectory. */
2588 *tree_id = got_object_id_dup(id);
2589 if (*tree_id == NULL) {
2590 err = got_error_from_errno("got_object_id_dup");
2591 goto done;
2593 *tree_relpath = strdup(wt_relpath);
2594 if (*tree_relpath == NULL) {
2595 err = got_error_from_errno("strdup");
2596 goto done;
2599 done:
2600 free(id);
2601 free(in_repo_path);
2602 if (err) {
2603 *entry_type = GOT_OBJ_TYPE_ANY;
2604 free(*tree_relpath);
2605 *tree_relpath = NULL;
2606 free(*tree_id);
2607 *tree_id = NULL;
2609 return err;
2612 static const struct got_error *
2613 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2614 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2615 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2616 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2618 const struct got_error *err = NULL;
2619 struct got_commit_object *commit = NULL;
2620 struct got_tree_object *tree = NULL;
2621 struct got_fileindex_diff_tree_cb diff_cb;
2622 struct diff_cb_arg arg;
2624 err = ref_base_commit(worktree, repo);
2625 if (err) {
2626 if (!(err->code == GOT_ERR_ERRNO &&
2627 (errno == EACCES || errno == EROFS)))
2628 goto done;
2629 err = (*progress_cb)(progress_arg,
2630 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2631 if (err)
2632 return err;
2635 err = got_object_open_as_commit(&commit, repo,
2636 worktree->base_commit_id);
2637 if (err)
2638 goto done;
2640 err = got_object_open_as_tree(&tree, repo, tree_id);
2641 if (err)
2642 goto done;
2644 if (entry_name &&
2645 got_object_tree_find_entry(tree, entry_name) == NULL) {
2646 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2647 goto done;
2650 diff_cb.diff_old_new = diff_old_new;
2651 diff_cb.diff_old = diff_old;
2652 diff_cb.diff_new = diff_new;
2653 arg.fileindex = fileindex;
2654 arg.worktree = worktree;
2655 arg.repo = repo;
2656 arg.progress_cb = progress_cb;
2657 arg.progress_arg = progress_arg;
2658 arg.cancel_cb = cancel_cb;
2659 arg.cancel_arg = cancel_arg;
2660 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2661 entry_name, repo, &diff_cb, &arg);
2662 done:
2663 if (tree)
2664 got_object_tree_close(tree);
2665 if (commit)
2666 got_object_commit_close(commit);
2667 return err;
2670 const struct got_error *
2671 got_worktree_checkout_files(struct got_worktree *worktree,
2672 struct got_pathlist_head *paths, struct got_repository *repo,
2673 got_worktree_checkout_cb progress_cb, void *progress_arg,
2674 got_cancel_cb cancel_cb, void *cancel_arg)
2676 const struct got_error *err = NULL, *sync_err, *unlockerr;
2677 struct got_commit_object *commit = NULL;
2678 struct got_tree_object *tree = NULL;
2679 struct got_fileindex *fileindex = NULL;
2680 char *fileindex_path = NULL;
2681 struct got_pathlist_entry *pe;
2682 struct tree_path_data {
2683 STAILQ_ENTRY(tree_path_data) entry;
2684 struct got_object_id *tree_id;
2685 int entry_type;
2686 char *relpath;
2687 char *entry_name;
2688 } *tpd = NULL;
2689 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2691 STAILQ_INIT(&tree_paths);
2693 err = lock_worktree(worktree, LOCK_EX);
2694 if (err)
2695 return err;
2697 err = got_object_open_as_commit(&commit, repo,
2698 worktree->base_commit_id);
2699 if (err)
2700 goto done;
2702 /* Map all specified paths to in-repository trees. */
2703 TAILQ_FOREACH(pe, paths, entry) {
2704 tpd = malloc(sizeof(*tpd));
2705 if (tpd == NULL) {
2706 err = got_error_from_errno("malloc");
2707 goto done;
2710 err = find_tree_entry_for_checkout(&tpd->entry_type,
2711 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2712 worktree, repo);
2713 if (err) {
2714 free(tpd);
2715 goto done;
2718 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2719 err = got_path_basename(&tpd->entry_name, pe->path);
2720 if (err) {
2721 free(tpd->relpath);
2722 free(tpd->tree_id);
2723 free(tpd);
2724 goto done;
2726 } else
2727 tpd->entry_name = NULL;
2729 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2733 * Read the file index.
2734 * Checking out files is supposed to be an idempotent operation.
2735 * If the on-disk file index is incomplete we will try to complete it.
2737 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2738 if (err)
2739 goto done;
2741 tpd = STAILQ_FIRST(&tree_paths);
2742 TAILQ_FOREACH(pe, paths, entry) {
2743 struct bump_base_commit_id_arg bbc_arg;
2745 err = checkout_files(worktree, fileindex, tpd->relpath,
2746 tpd->tree_id, tpd->entry_name, repo,
2747 progress_cb, progress_arg, cancel_cb, cancel_arg);
2748 if (err)
2749 break;
2751 bbc_arg.base_commit_id = worktree->base_commit_id;
2752 bbc_arg.entry_name = tpd->entry_name;
2753 bbc_arg.path = pe->path;
2754 bbc_arg.path_len = pe->path_len;
2755 bbc_arg.progress_cb = progress_cb;
2756 bbc_arg.progress_arg = progress_arg;
2757 err = got_fileindex_for_each_entry_safe(fileindex,
2758 bump_base_commit_id, &bbc_arg);
2759 if (err)
2760 break;
2762 tpd = STAILQ_NEXT(tpd, entry);
2764 sync_err = sync_fileindex(fileindex, fileindex_path);
2765 if (sync_err && err == NULL)
2766 err = sync_err;
2767 done:
2768 free(fileindex_path);
2769 if (tree)
2770 got_object_tree_close(tree);
2771 if (commit)
2772 got_object_commit_close(commit);
2773 if (fileindex)
2774 got_fileindex_free(fileindex);
2775 while (!STAILQ_EMPTY(&tree_paths)) {
2776 tpd = STAILQ_FIRST(&tree_paths);
2777 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2778 free(tpd->relpath);
2779 free(tpd->tree_id);
2780 free(tpd);
2782 unlockerr = lock_worktree(worktree, LOCK_SH);
2783 if (unlockerr && err == NULL)
2784 err = unlockerr;
2785 return err;
2788 struct merge_file_cb_arg {
2789 struct got_worktree *worktree;
2790 struct got_fileindex *fileindex;
2791 got_worktree_checkout_cb progress_cb;
2792 void *progress_arg;
2793 got_cancel_cb cancel_cb;
2794 void *cancel_arg;
2795 const char *label_orig;
2796 struct got_object_id *commit_id2;
2797 int allow_bad_symlinks;
2800 static const struct got_error *
2801 merge_file_cb(void *arg, struct got_blob_object *blob1,
2802 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2803 struct got_object_id *id1, struct got_object_id *id2,
2804 const char *path1, const char *path2,
2805 mode_t mode1, mode_t mode2, struct got_repository *repo)
2807 static const struct got_error *err = NULL;
2808 struct merge_file_cb_arg *a = arg;
2809 struct got_fileindex_entry *ie;
2810 char *ondisk_path = NULL;
2811 struct stat sb;
2812 unsigned char status;
2813 int local_changes_subsumed;
2814 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2815 char *id_str = NULL, *label_deriv2 = NULL;
2817 if (blob1 && blob2) {
2818 ie = got_fileindex_entry_get(a->fileindex, path2,
2819 strlen(path2));
2820 if (ie == NULL)
2821 return (*a->progress_cb)(a->progress_arg,
2822 GOT_STATUS_MISSING, path2);
2824 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2825 path2) == -1)
2826 return got_error_from_errno("asprintf");
2828 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2829 repo);
2830 if (err)
2831 goto done;
2833 if (status == GOT_STATUS_DELETE) {
2834 err = (*a->progress_cb)(a->progress_arg,
2835 GOT_STATUS_MERGE, path2);
2836 goto done;
2838 if (status != GOT_STATUS_NO_CHANGE &&
2839 status != GOT_STATUS_MODIFY &&
2840 status != GOT_STATUS_CONFLICT &&
2841 status != GOT_STATUS_ADD) {
2842 err = (*a->progress_cb)(a->progress_arg, status, path2);
2843 goto done;
2846 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2847 char *link_target2;
2848 err = got_object_blob_read_to_str(&link_target2, blob2);
2849 if (err)
2850 goto done;
2851 err = merge_symlink(a->worktree, blob1, ondisk_path,
2852 path2, a->label_orig, link_target2, a->commit_id2,
2853 repo, a->progress_cb, a->progress_arg);
2854 free(link_target2);
2855 } else {
2856 int fd;
2858 f_orig = got_opentemp();
2859 if (f_orig == NULL) {
2860 err = got_error_from_errno("got_opentemp");
2861 goto done;
2863 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2864 f_orig, blob1);
2865 if (err)
2866 goto done;
2868 f_deriv2 = got_opentemp();
2869 if (f_deriv2 == NULL)
2870 goto done;
2871 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2872 f_deriv2, blob2);
2873 if (err)
2874 goto done;
2876 fd = open(ondisk_path,
2877 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2878 if (fd == -1) {
2879 err = got_error_from_errno2("open",
2880 ondisk_path);
2881 goto done;
2883 f_deriv = fdopen(fd, "r");
2884 if (f_deriv == NULL) {
2885 err = got_error_from_errno2("fdopen",
2886 ondisk_path);
2887 close(fd);
2888 goto done;
2890 err = got_object_id_str(&id_str, a->commit_id2);
2891 if (err)
2892 goto done;
2893 if (asprintf(&label_deriv2, "%s: commit %s",
2894 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2895 err = got_error_from_errno("asprintf");
2896 goto done;
2898 err = merge_file(&local_changes_subsumed, a->worktree,
2899 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2900 mode2, a->label_orig, NULL, label_deriv2,
2901 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2902 a->progress_cb, a->progress_arg);
2904 } else if (blob1) {
2905 ie = got_fileindex_entry_get(a->fileindex, path1,
2906 strlen(path1));
2907 if (ie == NULL)
2908 return (*a->progress_cb)(a->progress_arg,
2909 GOT_STATUS_MISSING, path1);
2911 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2912 path1) == -1)
2913 return got_error_from_errno("asprintf");
2915 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2916 repo);
2917 if (err)
2918 goto done;
2920 switch (status) {
2921 case GOT_STATUS_NO_CHANGE:
2922 err = (*a->progress_cb)(a->progress_arg,
2923 GOT_STATUS_DELETE, path1);
2924 if (err)
2925 goto done;
2926 err = remove_ondisk_file(a->worktree->root_path, path1);
2927 if (err)
2928 goto done;
2929 if (ie)
2930 got_fileindex_entry_mark_deleted_from_disk(ie);
2931 break;
2932 case GOT_STATUS_DELETE:
2933 case GOT_STATUS_MISSING:
2934 err = (*a->progress_cb)(a->progress_arg,
2935 GOT_STATUS_DELETE, path1);
2936 if (err)
2937 goto done;
2938 if (ie)
2939 got_fileindex_entry_mark_deleted_from_disk(ie);
2940 break;
2941 case GOT_STATUS_ADD: {
2942 struct got_object_id *id;
2943 FILE *blob1_f;
2944 off_t blob1_size;
2946 * Delete the added file only if its content already
2947 * exists in the repository.
2949 err = got_object_blob_file_create(&id, &blob1_f,
2950 &blob1_size, path1);
2951 if (err)
2952 goto done;
2953 if (got_object_id_cmp(id, id1) == 0) {
2954 err = (*a->progress_cb)(a->progress_arg,
2955 GOT_STATUS_DELETE, path1);
2956 if (err)
2957 goto done;
2958 err = remove_ondisk_file(a->worktree->root_path,
2959 path1);
2960 if (err)
2961 goto done;
2962 if (ie)
2963 got_fileindex_entry_remove(a->fileindex,
2964 ie);
2965 } else {
2966 err = (*a->progress_cb)(a->progress_arg,
2967 GOT_STATUS_CANNOT_DELETE, path1);
2969 if (fclose(blob1_f) == EOF && err == NULL)
2970 err = got_error_from_errno("fclose");
2971 free(id);
2972 if (err)
2973 goto done;
2974 break;
2976 case GOT_STATUS_MODIFY:
2977 case GOT_STATUS_CONFLICT:
2978 err = (*a->progress_cb)(a->progress_arg,
2979 GOT_STATUS_CANNOT_DELETE, path1);
2980 if (err)
2981 goto done;
2982 break;
2983 case GOT_STATUS_OBSTRUCTED:
2984 err = (*a->progress_cb)(a->progress_arg, status, path1);
2985 if (err)
2986 goto done;
2987 break;
2988 default:
2989 break;
2991 } else if (blob2) {
2992 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2993 path2) == -1)
2994 return got_error_from_errno("asprintf");
2995 ie = got_fileindex_entry_get(a->fileindex, path2,
2996 strlen(path2));
2997 if (ie) {
2998 err = get_file_status(&status, &sb, ie, ondisk_path,
2999 -1, NULL, repo);
3000 if (err)
3001 goto done;
3002 if (status != GOT_STATUS_NO_CHANGE &&
3003 status != GOT_STATUS_MODIFY &&
3004 status != GOT_STATUS_CONFLICT &&
3005 status != GOT_STATUS_ADD) {
3006 err = (*a->progress_cb)(a->progress_arg,
3007 status, path2);
3008 goto done;
3010 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3011 char *link_target2;
3012 err = got_object_blob_read_to_str(&link_target2,
3013 blob2);
3014 if (err)
3015 goto done;
3016 err = merge_symlink(a->worktree, NULL,
3017 ondisk_path, path2, a->label_orig,
3018 link_target2, a->commit_id2, repo,
3019 a->progress_cb, a->progress_arg);
3020 free(link_target2);
3021 } else if (S_ISREG(sb.st_mode)) {
3022 err = merge_blob(&local_changes_subsumed,
3023 a->worktree, NULL, ondisk_path, path2,
3024 sb.st_mode, a->label_orig, blob2,
3025 a->commit_id2, repo, a->progress_cb,
3026 a->progress_arg);
3027 } else {
3028 err = got_error_path(ondisk_path,
3029 GOT_ERR_FILE_OBSTRUCTED);
3031 if (err)
3032 goto done;
3033 if (status == GOT_STATUS_DELETE) {
3034 err = got_fileindex_entry_update(ie,
3035 a->worktree->root_fd, path2, blob2->id.sha1,
3036 a->worktree->base_commit_id->sha1, 0);
3037 if (err)
3038 goto done;
3040 } else {
3041 int is_bad_symlink = 0;
3042 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3043 if (S_ISLNK(mode2)) {
3044 err = install_symlink(&is_bad_symlink,
3045 a->worktree, ondisk_path, path2, blob2, 0,
3046 0, 1, a->allow_bad_symlinks, repo,
3047 a->progress_cb, a->progress_arg);
3048 } else {
3049 err = install_blob(a->worktree, ondisk_path, path2,
3050 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3051 a->progress_cb, a->progress_arg);
3053 if (err)
3054 goto done;
3055 err = got_fileindex_entry_alloc(&ie, path2);
3056 if (err)
3057 goto done;
3058 err = got_fileindex_entry_update(ie,
3059 a->worktree->root_fd, path2, NULL, NULL, 1);
3060 if (err) {
3061 got_fileindex_entry_free(ie);
3062 goto done;
3064 err = got_fileindex_entry_add(a->fileindex, ie);
3065 if (err) {
3066 got_fileindex_entry_free(ie);
3067 goto done;
3069 if (is_bad_symlink) {
3070 got_fileindex_entry_filetype_set(ie,
3071 GOT_FILEIDX_MODE_BAD_SYMLINK);
3075 done:
3076 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3077 err = got_error_from_errno("fclose");
3078 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3079 err = got_error_from_errno("fclose");
3080 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3081 err = got_error_from_errno("fclose");
3082 free(id_str);
3083 free(label_deriv2);
3084 free(ondisk_path);
3085 return err;
3088 static const struct got_error *
3089 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3091 struct got_worktree *worktree = arg;
3093 /* Reject merges into a work tree with mixed base commits. */
3094 if (got_fileindex_entry_has_commit(ie) &&
3095 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3096 SHA1_DIGEST_LENGTH) != 0)
3097 return got_error(GOT_ERR_MIXED_COMMITS);
3099 return NULL;
3102 struct check_merge_conflicts_arg {
3103 struct got_worktree *worktree;
3104 struct got_fileindex *fileindex;
3105 struct got_repository *repo;
3108 static const struct got_error *
3109 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3110 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3111 struct got_object_id *id1, struct got_object_id *id2,
3112 const char *path1, const char *path2,
3113 mode_t mode1, mode_t mode2, struct got_repository *repo)
3115 const struct got_error *err = NULL;
3116 struct check_merge_conflicts_arg *a = arg;
3117 unsigned char status;
3118 struct stat sb;
3119 struct got_fileindex_entry *ie;
3120 const char *path = path2 ? path2 : path1;
3121 struct got_object_id *id = id2 ? id2 : id1;
3122 char *ondisk_path;
3124 if (id == NULL)
3125 return NULL;
3127 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3128 if (ie == NULL)
3129 return NULL;
3131 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3132 == -1)
3133 return got_error_from_errno("asprintf");
3135 /* Reject merges into a work tree with conflicted files. */
3136 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3137 free(ondisk_path);
3138 if (err)
3139 return err;
3140 if (status == GOT_STATUS_CONFLICT)
3141 return got_error(GOT_ERR_CONFLICTS);
3143 return NULL;
3146 static const struct got_error *
3147 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3148 const char *fileindex_path, struct got_object_id *commit_id1,
3149 struct got_object_id *commit_id2, struct got_repository *repo,
3150 got_worktree_checkout_cb progress_cb, void *progress_arg,
3151 got_cancel_cb cancel_cb, void *cancel_arg)
3153 const struct got_error *err = NULL, *sync_err;
3154 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3155 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3156 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3157 struct check_merge_conflicts_arg cmc_arg;
3158 struct merge_file_cb_arg arg;
3159 char *label_orig = NULL;
3160 FILE *f1 = NULL, *f2 = NULL;
3161 int fd1 = -1, fd2 = -1;
3163 if (commit_id1) {
3164 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3165 if (err)
3166 goto done;
3167 err = got_object_id_by_path(&tree_id1, repo, commit1,
3168 worktree->path_prefix);
3169 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3170 goto done;
3172 if (tree_id1) {
3173 char *id_str;
3175 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3176 if (err)
3177 goto done;
3179 err = got_object_id_str(&id_str, commit_id1);
3180 if (err)
3181 goto done;
3183 if (asprintf(&label_orig, "%s: commit %s",
3184 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3185 err = got_error_from_errno("asprintf");
3186 free(id_str);
3187 goto done;
3189 free(id_str);
3191 f1 = got_opentemp();
3192 if (f1 == NULL) {
3193 err = got_error_from_errno("got_opentemp");
3194 goto done;
3198 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3199 if (err)
3200 goto done;
3202 err = got_object_id_by_path(&tree_id2, repo, commit2,
3203 worktree->path_prefix);
3204 if (err)
3205 goto done;
3207 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3208 if (err)
3209 goto done;
3211 f2 = got_opentemp();
3212 if (f2 == NULL) {
3213 err = got_error_from_errno("got_opentemp");
3214 goto done;
3217 fd1 = got_opentempfd();
3218 if (fd1 == -1) {
3219 err = got_error_from_errno("got_opentempfd");
3220 goto done;
3223 fd2 = got_opentempfd();
3224 if (fd2 == -1) {
3225 err = got_error_from_errno("got_opentempfd");
3226 goto done;
3229 cmc_arg.worktree = worktree;
3230 cmc_arg.fileindex = fileindex;
3231 cmc_arg.repo = repo;
3232 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3233 check_merge_conflicts, &cmc_arg, 0);
3234 if (err)
3235 goto done;
3237 arg.worktree = worktree;
3238 arg.fileindex = fileindex;
3239 arg.progress_cb = progress_cb;
3240 arg.progress_arg = progress_arg;
3241 arg.cancel_cb = cancel_cb;
3242 arg.cancel_arg = cancel_arg;
3243 arg.label_orig = label_orig;
3244 arg.commit_id2 = commit_id2;
3245 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3246 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3247 merge_file_cb, &arg, 1);
3248 sync_err = sync_fileindex(fileindex, fileindex_path);
3249 if (sync_err && err == NULL)
3250 err = sync_err;
3251 done:
3252 if (commit1)
3253 got_object_commit_close(commit1);
3254 if (commit2)
3255 got_object_commit_close(commit2);
3256 if (tree1)
3257 got_object_tree_close(tree1);
3258 if (tree2)
3259 got_object_tree_close(tree2);
3260 if (f1 && fclose(f1) == EOF && err == NULL)
3261 err = got_error_from_errno("fclose");
3262 if (f2 && fclose(f2) == EOF && err == NULL)
3263 err = got_error_from_errno("fclose");
3264 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3265 err = got_error_from_errno("close");
3266 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3267 err = got_error_from_errno("close");
3268 free(label_orig);
3269 return err;
3272 const struct got_error *
3273 got_worktree_merge_files(struct got_worktree *worktree,
3274 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3275 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3276 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3278 const struct got_error *err, *unlockerr;
3279 char *fileindex_path = NULL;
3280 struct got_fileindex *fileindex = NULL;
3282 err = lock_worktree(worktree, LOCK_EX);
3283 if (err)
3284 return err;
3286 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3287 if (err)
3288 goto done;
3290 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3291 worktree);
3292 if (err)
3293 goto done;
3295 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3296 commit_id2, repo, progress_cb, progress_arg,
3297 cancel_cb, cancel_arg);
3298 done:
3299 if (fileindex)
3300 got_fileindex_free(fileindex);
3301 free(fileindex_path);
3302 unlockerr = lock_worktree(worktree, LOCK_SH);
3303 if (unlockerr && err == NULL)
3304 err = unlockerr;
3305 return err;
3308 struct diff_dir_cb_arg {
3309 struct got_fileindex *fileindex;
3310 struct got_worktree *worktree;
3311 const char *status_path;
3312 size_t status_path_len;
3313 struct got_repository *repo;
3314 got_worktree_status_cb status_cb;
3315 void *status_arg;
3316 got_cancel_cb cancel_cb;
3317 void *cancel_arg;
3318 /* A pathlist containing per-directory pathlists of ignore patterns. */
3319 struct got_pathlist_head *ignores;
3320 int report_unchanged;
3321 int no_ignores;
3324 static const struct got_error *
3325 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3326 int dirfd, const char *de_name,
3327 got_worktree_status_cb status_cb, void *status_arg,
3328 struct got_repository *repo, int report_unchanged)
3330 const struct got_error *err = NULL;
3331 unsigned char status = GOT_STATUS_NO_CHANGE;
3332 unsigned char staged_status = get_staged_status(ie);
3333 struct stat sb;
3334 struct got_object_id blob_id, commit_id, staged_blob_id;
3335 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3336 struct got_object_id *staged_blob_idp = NULL;
3338 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3339 if (err)
3340 return err;
3342 if (status == GOT_STATUS_NO_CHANGE &&
3343 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3344 return NULL;
3346 if (got_fileindex_entry_has_blob(ie)) {
3347 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3348 blob_idp = &blob_id;
3350 if (got_fileindex_entry_has_commit(ie)) {
3351 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3352 commit_idp = &commit_id;
3354 if (staged_status == GOT_STATUS_ADD ||
3355 staged_status == GOT_STATUS_MODIFY) {
3356 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3357 SHA1_DIGEST_LENGTH);
3358 staged_blob_idp = &staged_blob_id;
3361 return (*status_cb)(status_arg, status, staged_status,
3362 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3365 static const struct got_error *
3366 status_old_new(void *arg, struct got_fileindex_entry *ie,
3367 struct dirent *de, const char *parent_path, int dirfd)
3369 const struct got_error *err = NULL;
3370 struct diff_dir_cb_arg *a = arg;
3371 char *abspath;
3373 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3374 return got_error(GOT_ERR_CANCELLED);
3376 if (got_path_cmp(parent_path, a->status_path,
3377 strlen(parent_path), a->status_path_len) != 0 &&
3378 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3379 return NULL;
3381 if (parent_path[0]) {
3382 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3383 parent_path, de->d_name) == -1)
3384 return got_error_from_errno("asprintf");
3385 } else {
3386 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3387 de->d_name) == -1)
3388 return got_error_from_errno("asprintf");
3391 err = report_file_status(ie, abspath, dirfd, de->d_name,
3392 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3393 free(abspath);
3394 return err;
3397 static const struct got_error *
3398 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3400 struct diff_dir_cb_arg *a = arg;
3401 struct got_object_id blob_id, commit_id;
3402 unsigned char status;
3404 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3405 return got_error(GOT_ERR_CANCELLED);
3407 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3408 return NULL;
3410 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3411 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3412 if (got_fileindex_entry_has_file_on_disk(ie))
3413 status = GOT_STATUS_MISSING;
3414 else
3415 status = GOT_STATUS_DELETE;
3416 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3417 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3420 static void
3421 free_ignores(struct got_pathlist_head *ignores)
3423 struct got_pathlist_entry *pe;
3425 TAILQ_FOREACH(pe, ignores, entry) {
3426 struct got_pathlist_head *ignorelist = pe->data;
3428 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3430 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3433 static const struct got_error *
3434 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3436 const struct got_error *err = NULL;
3437 struct got_pathlist_entry *pe = NULL;
3438 struct got_pathlist_head *ignorelist;
3439 char *line = NULL, *pattern, *dirpath = NULL;
3440 size_t linesize = 0;
3441 ssize_t linelen;
3443 ignorelist = calloc(1, sizeof(*ignorelist));
3444 if (ignorelist == NULL)
3445 return got_error_from_errno("calloc");
3446 TAILQ_INIT(ignorelist);
3448 while ((linelen = getline(&line, &linesize, f)) != -1) {
3449 if (linelen > 0 && line[linelen - 1] == '\n')
3450 line[linelen - 1] = '\0';
3452 /* Git's ignores may contain comments. */
3453 if (line[0] == '#')
3454 continue;
3456 /* Git's negated patterns are not (yet?) supported. */
3457 if (line[0] == '!')
3458 continue;
3460 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3461 line) == -1) {
3462 err = got_error_from_errno("asprintf");
3463 goto done;
3465 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3466 if (err)
3467 goto done;
3469 if (ferror(f)) {
3470 err = got_error_from_errno("getline");
3471 goto done;
3474 dirpath = strdup(path);
3475 if (dirpath == NULL) {
3476 err = got_error_from_errno("strdup");
3477 goto done;
3479 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3480 done:
3481 free(line);
3482 if (err || pe == NULL) {
3483 free(dirpath);
3484 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3486 return err;
3489 static int
3490 match_ignores(struct got_pathlist_head *ignores, const char *path)
3492 struct got_pathlist_entry *pe;
3494 /* Handle patterns which match in all directories. */
3495 TAILQ_FOREACH(pe, ignores, entry) {
3496 struct got_pathlist_head *ignorelist = pe->data;
3497 struct got_pathlist_entry *pi;
3499 TAILQ_FOREACH(pi, ignorelist, entry) {
3500 const char *p, *pattern = pi->path;
3502 if (strncmp(pattern, "**/", 3) != 0)
3503 continue;
3504 pattern += 3;
3505 p = path;
3506 while (*p) {
3507 if (fnmatch(pattern, p,
3508 FNM_PATHNAME | FNM_LEADING_DIR)) {
3509 /* Retry in next directory. */
3510 while (*p && *p != '/')
3511 p++;
3512 while (*p == '/')
3513 p++;
3514 continue;
3516 return 1;
3522 * The ignores pathlist contains ignore lists from children before
3523 * parents, so we can find the most specific ignorelist by walking
3524 * ignores backwards.
3526 pe = TAILQ_LAST(ignores, got_pathlist_head);
3527 while (pe) {
3528 if (got_path_is_child(path, pe->path, pe->path_len)) {
3529 struct got_pathlist_head *ignorelist = pe->data;
3530 struct got_pathlist_entry *pi;
3531 TAILQ_FOREACH(pi, ignorelist, entry) {
3532 const char *pattern = pi->path;
3533 int flags = FNM_LEADING_DIR;
3534 if (strstr(pattern, "/**/") == NULL)
3535 flags |= FNM_PATHNAME;
3536 if (fnmatch(pattern, path, flags))
3537 continue;
3538 return 1;
3541 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3544 return 0;
3547 static const struct got_error *
3548 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3549 const char *path, int dirfd, const char *ignores_filename)
3551 const struct got_error *err = NULL;
3552 char *ignorespath;
3553 int fd = -1;
3554 FILE *ignoresfile = NULL;
3556 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3557 path[0] ? "/" : "", ignores_filename) == -1)
3558 return got_error_from_errno("asprintf");
3560 if (dirfd != -1) {
3561 fd = openat(dirfd, ignores_filename,
3562 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3563 if (fd == -1) {
3564 if (errno != ENOENT && errno != EACCES)
3565 err = got_error_from_errno2("openat",
3566 ignorespath);
3567 } else {
3568 ignoresfile = fdopen(fd, "r");
3569 if (ignoresfile == NULL)
3570 err = got_error_from_errno2("fdopen",
3571 ignorespath);
3572 else {
3573 fd = -1;
3574 err = read_ignores(ignores, path, ignoresfile);
3577 } else {
3578 ignoresfile = fopen(ignorespath, "re");
3579 if (ignoresfile == NULL) {
3580 if (errno != ENOENT && errno != EACCES)
3581 err = got_error_from_errno2("fopen",
3582 ignorespath);
3583 } else
3584 err = read_ignores(ignores, path, ignoresfile);
3587 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3588 err = got_error_from_errno2("fclose", path);
3589 if (fd != -1 && close(fd) == -1 && err == NULL)
3590 err = got_error_from_errno2("close", path);
3591 free(ignorespath);
3592 return err;
3595 static const struct got_error *
3596 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3597 int dirfd)
3599 const struct got_error *err = NULL;
3600 struct diff_dir_cb_arg *a = arg;
3601 char *path = NULL;
3603 if (ignore != NULL)
3604 *ignore = 0;
3606 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3607 return got_error(GOT_ERR_CANCELLED);
3609 if (parent_path[0]) {
3610 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3611 return got_error_from_errno("asprintf");
3612 } else {
3613 path = de->d_name;
3616 if (de->d_type == DT_DIR) {
3617 if (!a->no_ignores && ignore != NULL &&
3618 match_ignores(a->ignores, path))
3619 *ignore = 1;
3620 } else if (!match_ignores(a->ignores, path) &&
3621 got_path_is_child(path, a->status_path, a->status_path_len))
3622 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3623 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3624 if (parent_path[0])
3625 free(path);
3626 return err;
3629 static const struct got_error *
3630 status_traverse(void *arg, const char *path, int dirfd)
3632 const struct got_error *err = NULL;
3633 struct diff_dir_cb_arg *a = arg;
3635 if (a->no_ignores)
3636 return NULL;
3638 err = add_ignores(a->ignores, a->worktree->root_path,
3639 path, dirfd, ".cvsignore");
3640 if (err)
3641 return err;
3643 err = add_ignores(a->ignores, a->worktree->root_path, path,
3644 dirfd, ".gitignore");
3646 return err;
3649 static const struct got_error *
3650 report_single_file_status(const char *path, const char *ondisk_path,
3651 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3652 void *status_arg, struct got_repository *repo, int report_unchanged,
3653 struct got_pathlist_head *ignores, int no_ignores)
3655 struct got_fileindex_entry *ie;
3656 struct stat sb;
3658 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3659 if (ie)
3660 return report_file_status(ie, ondisk_path, -1, NULL,
3661 status_cb, status_arg, repo, report_unchanged);
3663 if (lstat(ondisk_path, &sb) == -1) {
3664 if (errno != ENOENT)
3665 return got_error_from_errno2("lstat", ondisk_path);
3666 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3667 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3670 if (!no_ignores && match_ignores(ignores, path))
3671 return NULL;
3673 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3674 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3675 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3677 return NULL;
3680 static const struct got_error *
3681 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3682 const char *root_path, const char *path)
3684 const struct got_error *err;
3685 char *parent_path, *next_parent_path = NULL;
3687 err = add_ignores(ignores, root_path, "", -1,
3688 ".cvsignore");
3689 if (err)
3690 return err;
3692 err = add_ignores(ignores, root_path, "", -1,
3693 ".gitignore");
3694 if (err)
3695 return err;
3697 err = got_path_dirname(&parent_path, path);
3698 if (err) {
3699 if (err->code == GOT_ERR_BAD_PATH)
3700 return NULL; /* cannot traverse parent */
3701 return err;
3703 for (;;) {
3704 err = add_ignores(ignores, root_path, parent_path, -1,
3705 ".cvsignore");
3706 if (err)
3707 break;
3708 err = add_ignores(ignores, root_path, parent_path, -1,
3709 ".gitignore");
3710 if (err)
3711 break;
3712 err = got_path_dirname(&next_parent_path, parent_path);
3713 if (err) {
3714 if (err->code == GOT_ERR_BAD_PATH)
3715 err = NULL; /* traversed everything */
3716 break;
3718 if (got_path_is_root_dir(parent_path))
3719 break;
3720 free(parent_path);
3721 parent_path = next_parent_path;
3722 next_parent_path = NULL;
3725 free(parent_path);
3726 free(next_parent_path);
3727 return err;
3730 static const struct got_error *
3731 worktree_status(struct got_worktree *worktree, const char *path,
3732 struct got_fileindex *fileindex, struct got_repository *repo,
3733 got_worktree_status_cb status_cb, void *status_arg,
3734 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3735 int report_unchanged)
3737 const struct got_error *err = NULL;
3738 int fd = -1;
3739 struct got_fileindex_diff_dir_cb fdiff_cb;
3740 struct diff_dir_cb_arg arg;
3741 char *ondisk_path = NULL;
3742 struct got_pathlist_head ignores;
3743 struct got_fileindex_entry *ie;
3745 TAILQ_INIT(&ignores);
3747 if (asprintf(&ondisk_path, "%s%s%s",
3748 worktree->root_path, path[0] ? "/" : "", path) == -1)
3749 return got_error_from_errno("asprintf");
3751 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3752 if (ie) {
3753 err = report_single_file_status(path, ondisk_path,
3754 fileindex, status_cb, status_arg, repo,
3755 report_unchanged, &ignores, no_ignores);
3756 goto done;
3759 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3760 if (fd == -1) {
3761 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3762 !got_err_open_nofollow_on_symlink())
3763 err = got_error_from_errno2("open", ondisk_path);
3764 else {
3765 if (!no_ignores) {
3766 err = add_ignores_from_parent_paths(&ignores,
3767 worktree->root_path, ondisk_path);
3768 if (err)
3769 goto done;
3771 err = report_single_file_status(path, ondisk_path,
3772 fileindex, status_cb, status_arg, repo,
3773 report_unchanged, &ignores, no_ignores);
3775 } else {
3776 fdiff_cb.diff_old_new = status_old_new;
3777 fdiff_cb.diff_old = status_old;
3778 fdiff_cb.diff_new = status_new;
3779 fdiff_cb.diff_traverse = status_traverse;
3780 arg.fileindex = fileindex;
3781 arg.worktree = worktree;
3782 arg.status_path = path;
3783 arg.status_path_len = strlen(path);
3784 arg.repo = repo;
3785 arg.status_cb = status_cb;
3786 arg.status_arg = status_arg;
3787 arg.cancel_cb = cancel_cb;
3788 arg.cancel_arg = cancel_arg;
3789 arg.report_unchanged = report_unchanged;
3790 arg.no_ignores = no_ignores;
3791 if (!no_ignores) {
3792 err = add_ignores_from_parent_paths(&ignores,
3793 worktree->root_path, path);
3794 if (err)
3795 goto done;
3797 arg.ignores = &ignores;
3798 err = got_fileindex_diff_dir(fileindex, fd,
3799 worktree->root_path, path, repo, &fdiff_cb, &arg);
3801 done:
3802 free_ignores(&ignores);
3803 if (fd != -1 && close(fd) == -1 && err == NULL)
3804 err = got_error_from_errno("close");
3805 free(ondisk_path);
3806 return err;
3809 const struct got_error *
3810 got_worktree_status(struct got_worktree *worktree,
3811 struct got_pathlist_head *paths, struct got_repository *repo,
3812 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3813 got_cancel_cb cancel_cb, void *cancel_arg)
3815 const struct got_error *err = NULL;
3816 char *fileindex_path = NULL;
3817 struct got_fileindex *fileindex = NULL;
3818 struct got_pathlist_entry *pe;
3820 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3821 if (err)
3822 return err;
3824 TAILQ_FOREACH(pe, paths, entry) {
3825 err = worktree_status(worktree, pe->path, fileindex, repo,
3826 status_cb, status_arg, cancel_cb, cancel_arg,
3827 no_ignores, 0);
3828 if (err)
3829 break;
3831 free(fileindex_path);
3832 got_fileindex_free(fileindex);
3833 return err;
3836 const struct got_error *
3837 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3838 const char *arg)
3840 const struct got_error *err = NULL;
3841 char *resolved = NULL, *cwd = NULL, *path = NULL;
3842 size_t len;
3843 struct stat sb;
3844 char *abspath = NULL;
3845 char canonpath[PATH_MAX];
3847 *wt_path = NULL;
3849 cwd = getcwd(NULL, 0);
3850 if (cwd == NULL)
3851 return got_error_from_errno("getcwd");
3853 if (lstat(arg, &sb) == -1) {
3854 if (errno != ENOENT) {
3855 err = got_error_from_errno2("lstat", arg);
3856 goto done;
3858 sb.st_mode = 0;
3860 if (S_ISLNK(sb.st_mode)) {
3862 * We cannot use realpath(3) with symlinks since we want to
3863 * operate on the symlink itself.
3864 * But we can make the path absolute, assuming it is relative
3865 * to the current working directory, and then canonicalize it.
3867 if (!got_path_is_absolute(arg)) {
3868 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3869 err = got_error_from_errno("asprintf");
3870 goto done;
3874 err = got_canonpath(abspath ? abspath : arg, canonpath,
3875 sizeof(canonpath));
3876 if (err)
3877 goto done;
3878 resolved = strdup(canonpath);
3879 if (resolved == NULL) {
3880 err = got_error_from_errno("strdup");
3881 goto done;
3883 } else {
3884 resolved = realpath(arg, NULL);
3885 if (resolved == NULL) {
3886 if (errno != ENOENT) {
3887 err = got_error_from_errno2("realpath", arg);
3888 goto done;
3890 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3891 err = got_error_from_errno("asprintf");
3892 goto done;
3894 err = got_canonpath(abspath, canonpath,
3895 sizeof(canonpath));
3896 if (err)
3897 goto done;
3898 resolved = strdup(canonpath);
3899 if (resolved == NULL) {
3900 err = got_error_from_errno("strdup");
3901 goto done;
3906 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3907 strlen(got_worktree_get_root_path(worktree)))) {
3908 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3909 goto done;
3912 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3913 err = got_path_skip_common_ancestor(&path,
3914 got_worktree_get_root_path(worktree), resolved);
3915 if (err)
3916 goto done;
3917 } else {
3918 path = strdup("");
3919 if (path == NULL) {
3920 err = got_error_from_errno("strdup");
3921 goto done;
3925 /* XXX status walk can't deal with trailing slash! */
3926 len = strlen(path);
3927 while (len > 0 && path[len - 1] == '/') {
3928 path[len - 1] = '\0';
3929 len--;
3931 done:
3932 free(abspath);
3933 free(resolved);
3934 free(cwd);
3935 if (err == NULL)
3936 *wt_path = path;
3937 else
3938 free(path);
3939 return err;
3942 struct schedule_addition_args {
3943 struct got_worktree *worktree;
3944 struct got_fileindex *fileindex;
3945 got_worktree_checkout_cb progress_cb;
3946 void *progress_arg;
3947 struct got_repository *repo;
3950 static const struct got_error *
3951 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3952 const char *relpath, struct got_object_id *blob_id,
3953 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3954 int dirfd, const char *de_name)
3956 struct schedule_addition_args *a = arg;
3957 const struct got_error *err = NULL;
3958 struct got_fileindex_entry *ie;
3959 struct stat sb;
3960 char *ondisk_path;
3962 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3963 relpath) == -1)
3964 return got_error_from_errno("asprintf");
3966 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3967 if (ie) {
3968 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3969 de_name, a->repo);
3970 if (err)
3971 goto done;
3972 /* Re-adding an existing entry is a no-op. */
3973 if (status == GOT_STATUS_ADD)
3974 goto done;
3975 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3976 if (err)
3977 goto done;
3980 if (status != GOT_STATUS_UNVERSIONED) {
3981 if (status == GOT_STATUS_NONEXISTENT)
3982 err = got_error_set_errno(ENOENT, ondisk_path);
3983 else
3984 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3985 goto done;
3988 err = got_fileindex_entry_alloc(&ie, relpath);
3989 if (err)
3990 goto done;
3991 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3992 relpath, NULL, NULL, 1);
3993 if (err) {
3994 got_fileindex_entry_free(ie);
3995 goto done;
3997 err = got_fileindex_entry_add(a->fileindex, ie);
3998 if (err) {
3999 got_fileindex_entry_free(ie);
4000 goto done;
4002 done:
4003 free(ondisk_path);
4004 if (err)
4005 return err;
4006 if (status == GOT_STATUS_ADD)
4007 return NULL;
4008 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4011 const struct got_error *
4012 got_worktree_schedule_add(struct got_worktree *worktree,
4013 struct got_pathlist_head *paths,
4014 got_worktree_checkout_cb progress_cb, void *progress_arg,
4015 struct got_repository *repo, int no_ignores)
4017 struct got_fileindex *fileindex = NULL;
4018 char *fileindex_path = NULL;
4019 const struct got_error *err = NULL, *sync_err, *unlockerr;
4020 struct got_pathlist_entry *pe;
4021 struct schedule_addition_args saa;
4023 err = lock_worktree(worktree, LOCK_EX);
4024 if (err)
4025 return err;
4027 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4028 if (err)
4029 goto done;
4031 saa.worktree = worktree;
4032 saa.fileindex = fileindex;
4033 saa.progress_cb = progress_cb;
4034 saa.progress_arg = progress_arg;
4035 saa.repo = repo;
4037 TAILQ_FOREACH(pe, paths, entry) {
4038 err = worktree_status(worktree, pe->path, fileindex, repo,
4039 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4040 if (err)
4041 break;
4043 sync_err = sync_fileindex(fileindex, fileindex_path);
4044 if (sync_err && err == NULL)
4045 err = sync_err;
4046 done:
4047 free(fileindex_path);
4048 if (fileindex)
4049 got_fileindex_free(fileindex);
4050 unlockerr = lock_worktree(worktree, LOCK_SH);
4051 if (unlockerr && err == NULL)
4052 err = unlockerr;
4053 return err;
4056 struct schedule_deletion_args {
4057 struct got_worktree *worktree;
4058 struct got_fileindex *fileindex;
4059 got_worktree_delete_cb progress_cb;
4060 void *progress_arg;
4061 struct got_repository *repo;
4062 int delete_local_mods;
4063 int keep_on_disk;
4064 int ignore_missing_paths;
4065 const char *status_codes;
4068 static const struct got_error *
4069 schedule_for_deletion(void *arg, unsigned char status,
4070 unsigned char staged_status, const char *relpath,
4071 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4072 struct got_object_id *commit_id, int dirfd, const char *de_name)
4074 struct schedule_deletion_args *a = arg;
4075 const struct got_error *err = NULL;
4076 struct got_fileindex_entry *ie = NULL;
4077 struct stat sb;
4078 char *ondisk_path;
4080 if (status == GOT_STATUS_NONEXISTENT) {
4081 if (a->ignore_missing_paths)
4082 return NULL;
4083 return got_error_set_errno(ENOENT, relpath);
4086 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4087 if (ie == NULL)
4088 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4090 staged_status = get_staged_status(ie);
4091 if (staged_status != GOT_STATUS_NO_CHANGE) {
4092 if (staged_status == GOT_STATUS_DELETE)
4093 return NULL;
4094 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4097 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4098 relpath) == -1)
4099 return got_error_from_errno("asprintf");
4101 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4102 a->repo);
4103 if (err)
4104 goto done;
4106 if (a->status_codes) {
4107 size_t ncodes = strlen(a->status_codes);
4108 int i;
4109 for (i = 0; i < ncodes ; i++) {
4110 if (status == a->status_codes[i])
4111 break;
4113 if (i == ncodes) {
4114 /* Do not delete files in non-matching status. */
4115 free(ondisk_path);
4116 return NULL;
4118 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4119 a->status_codes[i] != GOT_STATUS_MISSING) {
4120 static char msg[64];
4121 snprintf(msg, sizeof(msg),
4122 "invalid status code '%c'", a->status_codes[i]);
4123 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4124 goto done;
4128 if (status != GOT_STATUS_NO_CHANGE) {
4129 if (status == GOT_STATUS_DELETE)
4130 goto done;
4131 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4132 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4133 goto done;
4135 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4136 err = got_error_set_errno(ENOENT, relpath);
4137 goto done;
4139 if (status != GOT_STATUS_MODIFY &&
4140 status != GOT_STATUS_MISSING) {
4141 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4142 goto done;
4146 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4147 size_t root_len;
4149 if (dirfd != -1) {
4150 if (unlinkat(dirfd, de_name, 0) == -1) {
4151 err = got_error_from_errno2("unlinkat",
4152 ondisk_path);
4153 goto done;
4155 } else if (unlink(ondisk_path) == -1) {
4156 err = got_error_from_errno2("unlink", ondisk_path);
4157 goto done;
4160 root_len = strlen(a->worktree->root_path);
4161 do {
4162 char *parent;
4163 err = got_path_dirname(&parent, ondisk_path);
4164 if (err)
4165 goto done;
4166 free(ondisk_path);
4167 ondisk_path = parent;
4168 if (rmdir(ondisk_path) == -1) {
4169 if (errno != ENOTEMPTY)
4170 err = got_error_from_errno2("rmdir",
4171 ondisk_path);
4172 break;
4174 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4175 strlen(ondisk_path), root_len) != 0);
4178 got_fileindex_entry_mark_deleted_from_disk(ie);
4179 done:
4180 free(ondisk_path);
4181 if (err)
4182 return err;
4183 if (status == GOT_STATUS_DELETE)
4184 return NULL;
4185 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4186 staged_status, relpath);
4189 const struct got_error *
4190 got_worktree_schedule_delete(struct got_worktree *worktree,
4191 struct got_pathlist_head *paths, int delete_local_mods,
4192 const char *status_codes,
4193 got_worktree_delete_cb progress_cb, void *progress_arg,
4194 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4196 struct got_fileindex *fileindex = NULL;
4197 char *fileindex_path = NULL;
4198 const struct got_error *err = NULL, *sync_err, *unlockerr;
4199 struct got_pathlist_entry *pe;
4200 struct schedule_deletion_args sda;
4202 err = lock_worktree(worktree, LOCK_EX);
4203 if (err)
4204 return err;
4206 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4207 if (err)
4208 goto done;
4210 sda.worktree = worktree;
4211 sda.fileindex = fileindex;
4212 sda.progress_cb = progress_cb;
4213 sda.progress_arg = progress_arg;
4214 sda.repo = repo;
4215 sda.delete_local_mods = delete_local_mods;
4216 sda.keep_on_disk = keep_on_disk;
4217 sda.ignore_missing_paths = ignore_missing_paths;
4218 sda.status_codes = status_codes;
4220 TAILQ_FOREACH(pe, paths, entry) {
4221 err = worktree_status(worktree, pe->path, fileindex, repo,
4222 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4223 if (err)
4224 break;
4226 sync_err = sync_fileindex(fileindex, fileindex_path);
4227 if (sync_err && err == NULL)
4228 err = sync_err;
4229 done:
4230 free(fileindex_path);
4231 if (fileindex)
4232 got_fileindex_free(fileindex);
4233 unlockerr = lock_worktree(worktree, LOCK_SH);
4234 if (unlockerr && err == NULL)
4235 err = unlockerr;
4236 return err;
4239 static const struct got_error *
4240 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4242 const struct got_error *err = NULL;
4243 char *line = NULL;
4244 size_t linesize = 0, n;
4245 ssize_t linelen;
4247 linelen = getline(&line, &linesize, infile);
4248 if (linelen == -1) {
4249 if (ferror(infile)) {
4250 err = got_error_from_errno("getline");
4251 goto done;
4253 return NULL;
4255 if (outfile) {
4256 n = fwrite(line, 1, linelen, outfile);
4257 if (n != linelen) {
4258 err = got_ferror(outfile, GOT_ERR_IO);
4259 goto done;
4262 if (rejectfile) {
4263 n = fwrite(line, 1, linelen, rejectfile);
4264 if (n != linelen)
4265 err = got_ferror(rejectfile, GOT_ERR_IO);
4267 done:
4268 free(line);
4269 return err;
4272 static const struct got_error *
4273 skip_one_line(FILE *f)
4275 char *line = NULL;
4276 size_t linesize = 0;
4277 ssize_t linelen;
4279 linelen = getline(&line, &linesize, f);
4280 if (linelen == -1) {
4281 if (ferror(f))
4282 return got_error_from_errno("getline");
4283 return NULL;
4285 free(line);
4286 return NULL;
4289 static const struct got_error *
4290 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4291 int start_old, int end_old, int start_new, int end_new,
4292 FILE *outfile, FILE *rejectfile)
4294 const struct got_error *err;
4296 /* Copy old file's lines leading up to patch. */
4297 while (!feof(f1) && *line_cur1 < start_old) {
4298 err = copy_one_line(f1, outfile, NULL);
4299 if (err)
4300 return err;
4301 (*line_cur1)++;
4303 /* Skip new file's lines leading up to patch. */
4304 while (!feof(f2) && *line_cur2 < start_new) {
4305 if (rejectfile)
4306 err = copy_one_line(f2, NULL, rejectfile);
4307 else
4308 err = skip_one_line(f2);
4309 if (err)
4310 return err;
4311 (*line_cur2)++;
4313 /* Copy patched lines. */
4314 while (!feof(f2) && *line_cur2 <= end_new) {
4315 err = copy_one_line(f2, outfile, NULL);
4316 if (err)
4317 return err;
4318 (*line_cur2)++;
4320 /* Skip over old file's replaced lines. */
4321 while (!feof(f1) && *line_cur1 <= end_old) {
4322 if (rejectfile)
4323 err = copy_one_line(f1, NULL, rejectfile);
4324 else
4325 err = skip_one_line(f1);
4326 if (err)
4327 return err;
4328 (*line_cur1)++;
4331 return NULL;
4334 static const struct got_error *
4335 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4336 FILE *outfile, FILE *rejectfile)
4338 const struct got_error *err;
4340 if (outfile) {
4341 /* Copy old file's lines until EOF. */
4342 while (!feof(f1)) {
4343 err = copy_one_line(f1, outfile, NULL);
4344 if (err)
4345 return err;
4346 (*line_cur1)++;
4349 if (rejectfile) {
4350 /* Copy new file's lines until EOF. */
4351 while (!feof(f2)) {
4352 err = copy_one_line(f2, NULL, rejectfile);
4353 if (err)
4354 return err;
4355 (*line_cur2)++;
4359 return NULL;
4362 static const struct got_error *
4363 apply_or_reject_change(int *choice, int *nchunks_used,
4364 struct diff_result *diff_result, int n,
4365 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4366 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4367 got_worktree_patch_cb patch_cb, void *patch_arg)
4369 const struct got_error *err = NULL;
4370 struct diff_chunk_context cc = {};
4371 int start_old, end_old, start_new, end_new;
4372 FILE *hunkfile;
4373 struct diff_output_unidiff_state *diff_state;
4374 struct diff_input_info diff_info;
4375 int rc;
4377 *choice = GOT_PATCH_CHOICE_NONE;
4379 /* Get changed line numbers without context lines for copy_change(). */
4380 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4381 start_old = cc.left.start;
4382 end_old = cc.left.end;
4383 start_new = cc.right.start;
4384 end_new = cc.right.end;
4386 /* Get the same change with context lines for display. */
4387 memset(&cc, 0, sizeof(cc));
4388 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4390 memset(&diff_info, 0, sizeof(diff_info));
4391 diff_info.left_path = relpath;
4392 diff_info.right_path = relpath;
4394 diff_state = diff_output_unidiff_state_alloc();
4395 if (diff_state == NULL)
4396 return got_error_set_errno(ENOMEM,
4397 "diff_output_unidiff_state_alloc");
4399 hunkfile = got_opentemp();
4400 if (hunkfile == NULL) {
4401 err = got_error_from_errno("got_opentemp");
4402 goto done;
4405 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4406 diff_result, &cc);
4407 if (rc != DIFF_RC_OK) {
4408 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4409 goto done;
4412 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4413 err = got_ferror(hunkfile, GOT_ERR_IO);
4414 goto done;
4417 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4418 hunkfile, changeno, nchanges);
4419 if (err)
4420 goto done;
4422 switch (*choice) {
4423 case GOT_PATCH_CHOICE_YES:
4424 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4425 end_old, start_new, end_new, outfile, rejectfile);
4426 break;
4427 case GOT_PATCH_CHOICE_NO:
4428 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4429 end_old, start_new, end_new, rejectfile, outfile);
4430 break;
4431 case GOT_PATCH_CHOICE_QUIT:
4432 break;
4433 default:
4434 err = got_error(GOT_ERR_PATCH_CHOICE);
4435 break;
4437 done:
4438 diff_output_unidiff_state_free(diff_state);
4439 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4440 err = got_error_from_errno("fclose");
4441 return err;
4444 struct revert_file_args {
4445 struct got_worktree *worktree;
4446 struct got_fileindex *fileindex;
4447 got_worktree_checkout_cb progress_cb;
4448 void *progress_arg;
4449 got_worktree_patch_cb patch_cb;
4450 void *patch_arg;
4451 struct got_repository *repo;
4452 int unlink_added_files;
4455 static const struct got_error *
4456 create_patched_content(char **path_outfile, int reverse_patch,
4457 struct got_object_id *blob_id, const char *path2,
4458 int dirfd2, const char *de_name2,
4459 const char *relpath, struct got_repository *repo,
4460 got_worktree_patch_cb patch_cb, void *patch_arg)
4462 const struct got_error *err, *free_err;
4463 struct got_blob_object *blob = NULL;
4464 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4465 int fd = -1, fd2 = -1;
4466 char link_target[PATH_MAX];
4467 ssize_t link_len = 0;
4468 char *path1 = NULL, *id_str = NULL;
4469 struct stat sb2;
4470 struct got_diffreg_result *diffreg_result = NULL;
4471 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4472 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4474 *path_outfile = NULL;
4476 err = got_object_id_str(&id_str, blob_id);
4477 if (err)
4478 return err;
4480 if (dirfd2 != -1) {
4481 fd2 = openat(dirfd2, de_name2,
4482 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4483 if (fd2 == -1) {
4484 if (!got_err_open_nofollow_on_symlink()) {
4485 err = got_error_from_errno2("openat", path2);
4486 goto done;
4488 link_len = readlinkat(dirfd2, de_name2,
4489 link_target, sizeof(link_target));
4490 if (link_len == -1) {
4491 return got_error_from_errno2("readlinkat",
4492 path2);
4494 sb2.st_mode = S_IFLNK;
4495 sb2.st_size = link_len;
4497 } else {
4498 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4499 if (fd2 == -1) {
4500 if (!got_err_open_nofollow_on_symlink()) {
4501 err = got_error_from_errno2("open", path2);
4502 goto done;
4504 link_len = readlink(path2, link_target,
4505 sizeof(link_target));
4506 if (link_len == -1)
4507 return got_error_from_errno2("readlink", path2);
4508 sb2.st_mode = S_IFLNK;
4509 sb2.st_size = link_len;
4512 if (fd2 != -1) {
4513 if (fstat(fd2, &sb2) == -1) {
4514 err = got_error_from_errno2("fstat", path2);
4515 goto done;
4518 f2 = fdopen(fd2, "r");
4519 if (f2 == NULL) {
4520 err = got_error_from_errno2("fdopen", path2);
4521 goto done;
4523 fd2 = -1;
4524 } else {
4525 size_t n;
4526 f2 = got_opentemp();
4527 if (f2 == NULL) {
4528 err = got_error_from_errno2("got_opentemp", path2);
4529 goto done;
4531 n = fwrite(link_target, 1, link_len, f2);
4532 if (n != link_len) {
4533 err = got_ferror(f2, GOT_ERR_IO);
4534 goto done;
4536 if (fflush(f2) == EOF) {
4537 err = got_error_from_errno("fflush");
4538 goto done;
4540 rewind(f2);
4543 fd = got_opentempfd();
4544 if (fd == -1) {
4545 err = got_error_from_errno("got_opentempfd");
4546 goto done;
4549 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4550 if (err)
4551 goto done;
4553 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4554 if (err)
4555 goto done;
4557 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4558 if (err)
4559 goto done;
4561 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4562 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4563 if (err)
4564 goto done;
4566 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4567 "");
4568 if (err)
4569 goto done;
4571 if (fseek(f1, 0L, SEEK_SET) == -1)
4572 return got_ferror(f1, GOT_ERR_IO);
4573 if (fseek(f2, 0L, SEEK_SET) == -1)
4574 return got_ferror(f2, GOT_ERR_IO);
4576 /* Count the number of actual changes in the diff result. */
4577 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4578 struct diff_chunk_context cc = {};
4579 diff_chunk_context_load_change(&cc, &nchunks_used,
4580 diffreg_result->result, n, 0);
4581 nchanges++;
4583 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4584 int choice;
4585 err = apply_or_reject_change(&choice, &nchunks_used,
4586 diffreg_result->result, n, relpath, f1, f2,
4587 &line_cur1, &line_cur2,
4588 reverse_patch ? NULL : outfile,
4589 reverse_patch ? outfile : NULL,
4590 ++i, nchanges, patch_cb, patch_arg);
4591 if (err)
4592 goto done;
4593 if (choice == GOT_PATCH_CHOICE_YES)
4594 have_content = 1;
4595 else if (choice == GOT_PATCH_CHOICE_QUIT)
4596 break;
4598 if (have_content) {
4599 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4600 reverse_patch ? NULL : outfile,
4601 reverse_patch ? outfile : NULL);
4602 if (err)
4603 goto done;
4605 if (!S_ISLNK(sb2.st_mode)) {
4606 mode_t mode;
4608 mode = apply_umask(sb2.st_mode);
4609 if (fchmod(fileno(outfile), mode) == -1) {
4610 err = got_error_from_errno2("fchmod", path2);
4611 goto done;
4615 done:
4616 free(id_str);
4617 if (fd != -1 && close(fd) == -1 && err == NULL)
4618 err = got_error_from_errno("close");
4619 if (blob)
4620 got_object_blob_close(blob);
4621 free_err = got_diffreg_result_free(diffreg_result);
4622 if (err == NULL)
4623 err = free_err;
4624 if (f1 && fclose(f1) == EOF && err == NULL)
4625 err = got_error_from_errno2("fclose", path1);
4626 if (f2 && fclose(f2) == EOF && err == NULL)
4627 err = got_error_from_errno2("fclose", path2);
4628 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4629 err = got_error_from_errno2("close", path2);
4630 if (outfile && fclose(outfile) == EOF && err == NULL)
4631 err = got_error_from_errno2("fclose", *path_outfile);
4632 if (path1 && unlink(path1) == -1 && err == NULL)
4633 err = got_error_from_errno2("unlink", path1);
4634 if (err || !have_content) {
4635 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4636 err = got_error_from_errno2("unlink", *path_outfile);
4637 free(*path_outfile);
4638 *path_outfile = NULL;
4640 free(path1);
4641 return err;
4644 static const struct got_error *
4645 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4646 const char *relpath, struct got_object_id *blob_id,
4647 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4648 int dirfd, const char *de_name)
4650 struct revert_file_args *a = arg;
4651 const struct got_error *err = NULL;
4652 char *parent_path = NULL;
4653 struct got_fileindex_entry *ie;
4654 struct got_commit_object *base_commit = NULL;
4655 struct got_tree_object *tree = NULL;
4656 struct got_object_id *tree_id = NULL;
4657 const struct got_tree_entry *te = NULL;
4658 char *tree_path = NULL, *te_name;
4659 char *ondisk_path = NULL, *path_content = NULL;
4660 struct got_blob_object *blob = NULL;
4661 int fd = -1;
4663 /* Reverting a staged deletion is a no-op. */
4664 if (status == GOT_STATUS_DELETE &&
4665 staged_status != GOT_STATUS_NO_CHANGE)
4666 return NULL;
4668 if (status == GOT_STATUS_UNVERSIONED)
4669 return (*a->progress_cb)(a->progress_arg,
4670 GOT_STATUS_UNVERSIONED, relpath);
4672 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4673 if (ie == NULL)
4674 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4676 /* Construct in-repository path of tree which contains this blob. */
4677 err = got_path_dirname(&parent_path, ie->path);
4678 if (err) {
4679 if (err->code != GOT_ERR_BAD_PATH)
4680 goto done;
4681 parent_path = strdup("/");
4682 if (parent_path == NULL) {
4683 err = got_error_from_errno("strdup");
4684 goto done;
4687 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4688 tree_path = strdup(parent_path);
4689 if (tree_path == NULL) {
4690 err = got_error_from_errno("strdup");
4691 goto done;
4693 } else {
4694 if (got_path_is_root_dir(parent_path)) {
4695 tree_path = strdup(a->worktree->path_prefix);
4696 if (tree_path == NULL) {
4697 err = got_error_from_errno("strdup");
4698 goto done;
4700 } else {
4701 if (asprintf(&tree_path, "%s/%s",
4702 a->worktree->path_prefix, parent_path) == -1) {
4703 err = got_error_from_errno("asprintf");
4704 goto done;
4709 err = got_object_open_as_commit(&base_commit, a->repo,
4710 a->worktree->base_commit_id);
4711 if (err)
4712 goto done;
4714 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4715 if (err) {
4716 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4717 (status == GOT_STATUS_ADD ||
4718 staged_status == GOT_STATUS_ADD)))
4719 goto done;
4720 } else {
4721 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4722 if (err)
4723 goto done;
4725 err = got_path_basename(&te_name, ie->path);
4726 if (err)
4727 goto done;
4729 te = got_object_tree_find_entry(tree, te_name);
4730 free(te_name);
4731 if (te == NULL && status != GOT_STATUS_ADD &&
4732 staged_status != GOT_STATUS_ADD) {
4733 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4734 goto done;
4738 switch (status) {
4739 case GOT_STATUS_ADD:
4740 if (a->patch_cb) {
4741 int choice = GOT_PATCH_CHOICE_NONE;
4742 err = (*a->patch_cb)(&choice, a->patch_arg,
4743 status, ie->path, NULL, 1, 1);
4744 if (err)
4745 goto done;
4746 if (choice != GOT_PATCH_CHOICE_YES)
4747 break;
4749 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4750 ie->path);
4751 if (err)
4752 goto done;
4753 got_fileindex_entry_remove(a->fileindex, ie);
4754 if (a->unlink_added_files) {
4755 if (asprintf(&ondisk_path, "%s/%s",
4756 got_worktree_get_root_path(a->worktree),
4757 relpath) == -1) {
4758 err = got_error_from_errno("asprintf");
4759 goto done;
4761 if (unlink(ondisk_path) == -1) {
4762 err = got_error_from_errno2("unlink",
4763 ondisk_path);
4764 break;
4767 break;
4768 case GOT_STATUS_DELETE:
4769 if (a->patch_cb) {
4770 int choice = GOT_PATCH_CHOICE_NONE;
4771 err = (*a->patch_cb)(&choice, a->patch_arg,
4772 status, ie->path, NULL, 1, 1);
4773 if (err)
4774 goto done;
4775 if (choice != GOT_PATCH_CHOICE_YES)
4776 break;
4778 /* fall through */
4779 case GOT_STATUS_MODIFY:
4780 case GOT_STATUS_MODE_CHANGE:
4781 case GOT_STATUS_CONFLICT:
4782 case GOT_STATUS_MISSING: {
4783 struct got_object_id id;
4784 if (staged_status == GOT_STATUS_ADD ||
4785 staged_status == GOT_STATUS_MODIFY) {
4786 memcpy(id.sha1, ie->staged_blob_sha1,
4787 SHA1_DIGEST_LENGTH);
4788 } else
4789 memcpy(id.sha1, ie->blob_sha1,
4790 SHA1_DIGEST_LENGTH);
4791 fd = got_opentempfd();
4792 if (fd == -1) {
4793 err = got_error_from_errno("got_opentempfd");
4794 goto done;
4797 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4798 if (err)
4799 goto done;
4801 if (asprintf(&ondisk_path, "%s/%s",
4802 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4803 err = got_error_from_errno("asprintf");
4804 goto done;
4807 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4808 status == GOT_STATUS_CONFLICT)) {
4809 int is_bad_symlink = 0;
4810 err = create_patched_content(&path_content, 1, &id,
4811 ondisk_path, dirfd, de_name, ie->path, a->repo,
4812 a->patch_cb, a->patch_arg);
4813 if (err || path_content == NULL)
4814 break;
4815 if (te && S_ISLNK(te->mode)) {
4816 if (unlink(path_content) == -1) {
4817 err = got_error_from_errno2("unlink",
4818 path_content);
4819 break;
4821 err = install_symlink(&is_bad_symlink,
4822 a->worktree, ondisk_path, ie->path,
4823 blob, 0, 1, 0, 0, a->repo,
4824 a->progress_cb, a->progress_arg);
4825 } else {
4826 if (rename(path_content, ondisk_path) == -1) {
4827 err = got_error_from_errno3("rename",
4828 path_content, ondisk_path);
4829 goto done;
4832 } else {
4833 int is_bad_symlink = 0;
4834 if (te && S_ISLNK(te->mode)) {
4835 err = install_symlink(&is_bad_symlink,
4836 a->worktree, ondisk_path, ie->path,
4837 blob, 0, 1, 0, 0, a->repo,
4838 a->progress_cb, a->progress_arg);
4839 } else {
4840 err = install_blob(a->worktree, ondisk_path,
4841 ie->path,
4842 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4843 got_fileindex_perms_to_st(ie), blob,
4844 0, 1, 0, 0, a->repo,
4845 a->progress_cb, a->progress_arg);
4847 if (err)
4848 goto done;
4849 if (status == GOT_STATUS_DELETE ||
4850 status == GOT_STATUS_MODE_CHANGE) {
4851 err = got_fileindex_entry_update(ie,
4852 a->worktree->root_fd, relpath,
4853 blob->id.sha1,
4854 a->worktree->base_commit_id->sha1, 1);
4855 if (err)
4856 goto done;
4858 if (is_bad_symlink) {
4859 got_fileindex_entry_filetype_set(ie,
4860 GOT_FILEIDX_MODE_BAD_SYMLINK);
4863 break;
4865 default:
4866 break;
4868 done:
4869 free(ondisk_path);
4870 free(path_content);
4871 free(parent_path);
4872 free(tree_path);
4873 if (fd != -1 && close(fd) == -1 && err == NULL)
4874 err = got_error_from_errno("close");
4875 if (blob)
4876 got_object_blob_close(blob);
4877 if (tree)
4878 got_object_tree_close(tree);
4879 free(tree_id);
4880 if (base_commit)
4881 got_object_commit_close(base_commit);
4882 return err;
4885 const struct got_error *
4886 got_worktree_revert(struct got_worktree *worktree,
4887 struct got_pathlist_head *paths,
4888 got_worktree_checkout_cb progress_cb, void *progress_arg,
4889 got_worktree_patch_cb patch_cb, void *patch_arg,
4890 struct got_repository *repo)
4892 struct got_fileindex *fileindex = NULL;
4893 char *fileindex_path = NULL;
4894 const struct got_error *err = NULL, *unlockerr = NULL;
4895 const struct got_error *sync_err = NULL;
4896 struct got_pathlist_entry *pe;
4897 struct revert_file_args rfa;
4899 err = lock_worktree(worktree, LOCK_EX);
4900 if (err)
4901 return err;
4903 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4904 if (err)
4905 goto done;
4907 rfa.worktree = worktree;
4908 rfa.fileindex = fileindex;
4909 rfa.progress_cb = progress_cb;
4910 rfa.progress_arg = progress_arg;
4911 rfa.patch_cb = patch_cb;
4912 rfa.patch_arg = patch_arg;
4913 rfa.repo = repo;
4914 rfa.unlink_added_files = 0;
4915 TAILQ_FOREACH(pe, paths, entry) {
4916 err = worktree_status(worktree, pe->path, fileindex, repo,
4917 revert_file, &rfa, NULL, NULL, 1, 0);
4918 if (err)
4919 break;
4921 sync_err = sync_fileindex(fileindex, fileindex_path);
4922 if (sync_err && err == NULL)
4923 err = sync_err;
4924 done:
4925 free(fileindex_path);
4926 if (fileindex)
4927 got_fileindex_free(fileindex);
4928 unlockerr = lock_worktree(worktree, LOCK_SH);
4929 if (unlockerr && err == NULL)
4930 err = unlockerr;
4931 return err;
4934 static void
4935 free_commitable(struct got_commitable *ct)
4937 free(ct->path);
4938 free(ct->in_repo_path);
4939 free(ct->ondisk_path);
4940 free(ct->blob_id);
4941 free(ct->base_blob_id);
4942 free(ct->staged_blob_id);
4943 free(ct->base_commit_id);
4944 free(ct);
4947 struct collect_commitables_arg {
4948 struct got_pathlist_head *commitable_paths;
4949 struct got_repository *repo;
4950 struct got_worktree *worktree;
4951 struct got_fileindex *fileindex;
4952 int have_staged_files;
4953 int allow_bad_symlinks;
4954 int diff_header_shown;
4955 FILE *diff_outfile;
4956 FILE *f1;
4957 FILE *f2;
4961 * Create a file which contains the target path of a symlink so we can feed
4962 * it as content to the diff engine.
4964 static const struct got_error *
4965 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4966 const char *abspath)
4968 const struct got_error *err = NULL;
4969 char target_path[PATH_MAX];
4970 ssize_t target_len, outlen;
4972 *fd = -1;
4974 if (dirfd != -1) {
4975 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4976 if (target_len == -1)
4977 return got_error_from_errno2("readlinkat", abspath);
4978 } else {
4979 target_len = readlink(abspath, target_path, PATH_MAX);
4980 if (target_len == -1)
4981 return got_error_from_errno2("readlink", abspath);
4984 *fd = got_opentempfd();
4985 if (*fd == -1)
4986 return got_error_from_errno("got_opentempfd");
4988 outlen = write(*fd, target_path, target_len);
4989 if (outlen == -1) {
4990 err = got_error_from_errno("got_opentempfd");
4991 goto done;
4994 if (lseek(*fd, 0, SEEK_SET) == -1) {
4995 err = got_error_from_errno2("lseek", abspath);
4996 goto done;
4998 done:
4999 if (err) {
5000 close(*fd);
5001 *fd = -1;
5003 return err;
5006 static const struct got_error *
5007 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5008 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5009 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5011 const struct got_error *err = NULL;
5012 struct got_blob_object *blob1 = NULL;
5013 int fd = -1, fd1 = -1, fd2 = -1;
5014 FILE *ondisk_file = NULL;
5015 char *label1 = NULL;
5016 struct stat sb;
5017 off_t size1 = 0;
5018 int f2_exists = 0;
5019 char *id_str = NULL;
5021 memset(&sb, 0, sizeof(sb));
5023 if (diff_staged) {
5024 if (ct->staged_status != GOT_STATUS_MODIFY &&
5025 ct->staged_status != GOT_STATUS_ADD &&
5026 ct->staged_status != GOT_STATUS_DELETE)
5027 return NULL;
5028 } else {
5029 if (ct->status != GOT_STATUS_MODIFY &&
5030 ct->status != GOT_STATUS_ADD &&
5031 ct->status != GOT_STATUS_DELETE)
5032 return NULL;
5035 err = got_opentemp_truncate(f1);
5036 if (err)
5037 return got_error_from_errno("got_opentemp_truncate");
5038 err = got_opentemp_truncate(f2);
5039 if (err)
5040 return got_error_from_errno("got_opentemp_truncate");
5042 if (!*diff_header_shown) {
5043 err = got_object_id_str(&id_str, worktree->base_commit_id);
5044 if (err)
5045 return err;
5046 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5047 got_worktree_get_root_path(worktree));
5048 fprintf(diff_outfile, "commit - %s\n", id_str);
5049 fprintf(diff_outfile, "path + %s%s\n",
5050 got_worktree_get_root_path(worktree),
5051 diff_staged ? " (staged changes)" : "");
5052 *diff_header_shown = 1;
5055 if (diff_staged) {
5056 const char *label1 = NULL, *label2 = NULL;
5057 switch (ct->staged_status) {
5058 case GOT_STATUS_MODIFY:
5059 label1 = ct->path;
5060 label2 = ct->path;
5061 break;
5062 case GOT_STATUS_ADD:
5063 label2 = ct->path;
5064 break;
5065 case GOT_STATUS_DELETE:
5066 label1 = ct->path;
5067 break;
5068 default:
5069 return got_error(GOT_ERR_FILE_STATUS);
5071 fd1 = got_opentempfd();
5072 if (fd1 == -1) {
5073 err = got_error_from_errno("got_opentempfd");
5074 goto done;
5076 fd2 = got_opentempfd();
5077 if (fd2 == -1) {
5078 err = got_error_from_errno("got_opentempfd");
5079 goto done;
5081 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5082 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5083 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5084 NULL, repo, diff_outfile);
5085 goto done;
5088 fd1 = got_opentempfd();
5089 if (fd1 == -1) {
5090 err = got_error_from_errno("got_opentempfd");
5091 goto done;
5094 if (ct->status != GOT_STATUS_ADD) {
5095 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5096 8192, fd1);
5097 if (err)
5098 goto done;
5101 if (ct->status != GOT_STATUS_DELETE) {
5102 if (dirfd != -1) {
5103 fd = openat(dirfd, de_name,
5104 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5105 if (fd == -1) {
5106 if (!got_err_open_nofollow_on_symlink()) {
5107 err = got_error_from_errno2("openat",
5108 ct->ondisk_path);
5109 goto done;
5111 err = get_symlink_target_file(&fd, dirfd,
5112 de_name, ct->ondisk_path);
5113 if (err)
5114 goto done;
5116 } else {
5117 fd = open(ct->ondisk_path,
5118 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5119 if (fd == -1) {
5120 if (!got_err_open_nofollow_on_symlink()) {
5121 err = got_error_from_errno2("open",
5122 ct->ondisk_path);
5123 goto done;
5125 err = get_symlink_target_file(&fd, dirfd,
5126 de_name, ct->ondisk_path);
5127 if (err)
5128 goto done;
5131 if (fstatat(fd, ct->ondisk_path, &sb,
5132 AT_SYMLINK_NOFOLLOW) == -1) {
5133 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5134 goto done;
5136 ondisk_file = fdopen(fd, "r");
5137 if (ondisk_file == NULL) {
5138 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5139 goto done;
5141 fd = -1;
5142 f2_exists = 1;
5145 if (blob1) {
5146 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5147 f1, blob1);
5148 if (err)
5149 goto done;
5152 err = got_diff_blob_file(blob1, f1, size1, label1,
5153 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5154 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5155 done:
5156 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5157 err = got_error_from_errno("close");
5158 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5159 err = got_error_from_errno("close");
5160 if (blob1)
5161 got_object_blob_close(blob1);
5162 if (fd != -1 && close(fd) == -1 && err == NULL)
5163 err = got_error_from_errno("close");
5164 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5165 err = got_error_from_errno("fclose");
5166 return err;
5169 static const struct got_error *
5170 collect_commitables(void *arg, unsigned char status,
5171 unsigned char staged_status, const char *relpath,
5172 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5173 struct got_object_id *commit_id, int dirfd, const char *de_name)
5175 struct collect_commitables_arg *a = arg;
5176 const struct got_error *err = NULL;
5177 struct got_commitable *ct = NULL;
5178 struct got_pathlist_entry *new = NULL;
5179 char *parent_path = NULL, *path = NULL;
5180 struct stat sb;
5182 if (a->have_staged_files) {
5183 if (staged_status != GOT_STATUS_MODIFY &&
5184 staged_status != GOT_STATUS_ADD &&
5185 staged_status != GOT_STATUS_DELETE)
5186 return NULL;
5187 } else {
5188 if (status == GOT_STATUS_CONFLICT)
5189 return got_error(GOT_ERR_COMMIT_CONFLICT);
5191 if (status != GOT_STATUS_MODIFY &&
5192 status != GOT_STATUS_MODE_CHANGE &&
5193 status != GOT_STATUS_ADD &&
5194 status != GOT_STATUS_DELETE)
5195 return NULL;
5198 if (asprintf(&path, "/%s", relpath) == -1) {
5199 err = got_error_from_errno("asprintf");
5200 goto done;
5202 if (strcmp(path, "/") == 0) {
5203 parent_path = strdup("");
5204 if (parent_path == NULL)
5205 return got_error_from_errno("strdup");
5206 } else {
5207 err = got_path_dirname(&parent_path, path);
5208 if (err)
5209 return err;
5212 ct = calloc(1, sizeof(*ct));
5213 if (ct == NULL) {
5214 err = got_error_from_errno("calloc");
5215 goto done;
5218 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5219 relpath) == -1) {
5220 err = got_error_from_errno("asprintf");
5221 goto done;
5224 if (staged_status == GOT_STATUS_ADD ||
5225 staged_status == GOT_STATUS_MODIFY) {
5226 struct got_fileindex_entry *ie;
5227 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5228 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5229 case GOT_FILEIDX_MODE_REGULAR_FILE:
5230 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5231 ct->mode = S_IFREG;
5232 break;
5233 case GOT_FILEIDX_MODE_SYMLINK:
5234 ct->mode = S_IFLNK;
5235 break;
5236 default:
5237 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5238 goto done;
5240 ct->mode |= got_fileindex_entry_perms_get(ie);
5241 } else if (status != GOT_STATUS_DELETE &&
5242 staged_status != GOT_STATUS_DELETE) {
5243 if (dirfd != -1) {
5244 if (fstatat(dirfd, de_name, &sb,
5245 AT_SYMLINK_NOFOLLOW) == -1) {
5246 err = got_error_from_errno2("fstatat",
5247 ct->ondisk_path);
5248 goto done;
5250 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5251 err = got_error_from_errno2("lstat", ct->ondisk_path);
5252 goto done;
5254 ct->mode = sb.st_mode;
5257 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5258 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5259 relpath) == -1) {
5260 err = got_error_from_errno("asprintf");
5261 goto done;
5264 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5265 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5266 int is_bad_symlink;
5267 char target_path[PATH_MAX];
5268 ssize_t target_len;
5269 target_len = readlink(ct->ondisk_path, target_path,
5270 sizeof(target_path));
5271 if (target_len == -1) {
5272 err = got_error_from_errno2("readlink",
5273 ct->ondisk_path);
5274 goto done;
5276 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5277 target_len, ct->ondisk_path, a->worktree->root_path);
5278 if (err)
5279 goto done;
5280 if (is_bad_symlink) {
5281 err = got_error_path(ct->ondisk_path,
5282 GOT_ERR_BAD_SYMLINK);
5283 goto done;
5288 ct->status = status;
5289 ct->staged_status = staged_status;
5290 ct->blob_id = NULL; /* will be filled in when blob gets created */
5291 if (ct->status != GOT_STATUS_ADD &&
5292 ct->staged_status != GOT_STATUS_ADD) {
5293 ct->base_blob_id = got_object_id_dup(blob_id);
5294 if (ct->base_blob_id == NULL) {
5295 err = got_error_from_errno("got_object_id_dup");
5296 goto done;
5298 ct->base_commit_id = got_object_id_dup(commit_id);
5299 if (ct->base_commit_id == NULL) {
5300 err = got_error_from_errno("got_object_id_dup");
5301 goto done;
5304 if (ct->staged_status == GOT_STATUS_ADD ||
5305 ct->staged_status == GOT_STATUS_MODIFY) {
5306 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5307 if (ct->staged_blob_id == NULL) {
5308 err = got_error_from_errno("got_object_id_dup");
5309 goto done;
5312 ct->path = strdup(path);
5313 if (ct->path == NULL) {
5314 err = got_error_from_errno("strdup");
5315 goto done;
5317 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5318 if (err)
5319 goto done;
5321 if (a->diff_outfile && ct && new != NULL) {
5322 err = append_ct_diff(ct, &a->diff_header_shown,
5323 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5324 a->have_staged_files, a->repo, a->worktree);
5325 if (err)
5326 goto done;
5328 done:
5329 if (ct && (err || new == NULL))
5330 free_commitable(ct);
5331 free(parent_path);
5332 free(path);
5333 return err;
5336 static const struct got_error *write_tree(struct got_object_id **, int *,
5337 struct got_tree_object *, const char *, struct got_pathlist_head *,
5338 got_worktree_status_cb status_cb, void *status_arg,
5339 struct got_repository *);
5341 static const struct got_error *
5342 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5343 struct got_tree_entry *te, const char *parent_path,
5344 struct got_pathlist_head *commitable_paths,
5345 got_worktree_status_cb status_cb, void *status_arg,
5346 struct got_repository *repo)
5348 const struct got_error *err = NULL;
5349 struct got_tree_object *subtree;
5350 char *subpath;
5352 if (asprintf(&subpath, "%s%s%s", parent_path,
5353 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5354 return got_error_from_errno("asprintf");
5356 err = got_object_open_as_tree(&subtree, repo, &te->id);
5357 if (err)
5358 return err;
5360 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5361 commitable_paths, status_cb, status_arg, repo);
5362 got_object_tree_close(subtree);
5363 free(subpath);
5364 return err;
5367 static const struct got_error *
5368 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5370 const struct got_error *err = NULL;
5371 char *ct_parent_path = NULL;
5373 *match = 0;
5375 if (strchr(ct->in_repo_path, '/') == NULL) {
5376 *match = got_path_is_root_dir(path);
5377 return NULL;
5380 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5381 if (err)
5382 return err;
5383 *match = (strcmp(path, ct_parent_path) == 0);
5384 free(ct_parent_path);
5385 return err;
5388 static mode_t
5389 get_ct_file_mode(struct got_commitable *ct)
5391 if (S_ISLNK(ct->mode))
5392 return S_IFLNK;
5394 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5397 static const struct got_error *
5398 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5399 struct got_tree_entry *te, struct got_commitable *ct)
5401 const struct got_error *err = NULL;
5403 *new_te = NULL;
5405 err = got_object_tree_entry_dup(new_te, te);
5406 if (err)
5407 goto done;
5409 (*new_te)->mode = get_ct_file_mode(ct);
5411 if (ct->staged_status == GOT_STATUS_MODIFY)
5412 memcpy(&(*new_te)->id, ct->staged_blob_id,
5413 sizeof((*new_te)->id));
5414 else
5415 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5416 done:
5417 if (err && *new_te) {
5418 free(*new_te);
5419 *new_te = NULL;
5421 return err;
5424 static const struct got_error *
5425 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5426 struct got_commitable *ct)
5428 const struct got_error *err = NULL;
5429 char *ct_name = NULL;
5431 *new_te = NULL;
5433 *new_te = calloc(1, sizeof(**new_te));
5434 if (*new_te == NULL)
5435 return got_error_from_errno("calloc");
5437 err = got_path_basename(&ct_name, ct->path);
5438 if (err)
5439 goto done;
5440 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5441 sizeof((*new_te)->name)) {
5442 err = got_error(GOT_ERR_NO_SPACE);
5443 goto done;
5446 (*new_te)->mode = get_ct_file_mode(ct);
5448 if (ct->staged_status == GOT_STATUS_ADD)
5449 memcpy(&(*new_te)->id, ct->staged_blob_id,
5450 sizeof((*new_te)->id));
5451 else
5452 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5453 done:
5454 free(ct_name);
5455 if (err && *new_te) {
5456 free(*new_te);
5457 *new_te = NULL;
5459 return err;
5462 static const struct got_error *
5463 insert_tree_entry(struct got_tree_entry *new_te,
5464 struct got_pathlist_head *paths)
5466 const struct got_error *err = NULL;
5467 struct got_pathlist_entry *new_pe;
5469 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5470 if (err)
5471 return err;
5472 if (new_pe == NULL)
5473 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5474 return NULL;
5477 static const struct got_error *
5478 report_ct_status(struct got_commitable *ct,
5479 got_worktree_status_cb status_cb, void *status_arg)
5481 const char *ct_path = ct->path;
5482 unsigned char status;
5484 if (status_cb == NULL) /* no commit progress output desired */
5485 return NULL;
5487 while (ct_path[0] == '/')
5488 ct_path++;
5490 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5491 status = ct->staged_status;
5492 else
5493 status = ct->status;
5495 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5496 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5499 static const struct got_error *
5500 match_modified_subtree(int *modified, struct got_tree_entry *te,
5501 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5503 const struct got_error *err = NULL;
5504 struct got_pathlist_entry *pe;
5505 char *te_path;
5507 *modified = 0;
5509 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5510 got_path_is_root_dir(base_tree_path) ? "" : "/",
5511 te->name) == -1)
5512 return got_error_from_errno("asprintf");
5514 TAILQ_FOREACH(pe, commitable_paths, entry) {
5515 struct got_commitable *ct = pe->data;
5516 *modified = got_path_is_child(ct->in_repo_path, te_path,
5517 strlen(te_path));
5518 if (*modified)
5519 break;
5522 free(te_path);
5523 return err;
5526 static const struct got_error *
5527 match_deleted_or_modified_ct(struct got_commitable **ctp,
5528 struct got_tree_entry *te, const char *base_tree_path,
5529 struct got_pathlist_head *commitable_paths)
5531 const struct got_error *err = NULL;
5532 struct got_pathlist_entry *pe;
5534 *ctp = NULL;
5536 TAILQ_FOREACH(pe, commitable_paths, entry) {
5537 struct got_commitable *ct = pe->data;
5538 char *ct_name = NULL;
5539 int path_matches;
5541 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5542 if (ct->status != GOT_STATUS_MODIFY &&
5543 ct->status != GOT_STATUS_MODE_CHANGE &&
5544 ct->status != GOT_STATUS_DELETE)
5545 continue;
5546 } else {
5547 if (ct->staged_status != GOT_STATUS_MODIFY &&
5548 ct->staged_status != GOT_STATUS_DELETE)
5549 continue;
5552 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5553 continue;
5555 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5556 if (err)
5557 return err;
5558 if (!path_matches)
5559 continue;
5561 err = got_path_basename(&ct_name, pe->path);
5562 if (err)
5563 return err;
5565 if (strcmp(te->name, ct_name) != 0) {
5566 free(ct_name);
5567 continue;
5569 free(ct_name);
5571 *ctp = ct;
5572 break;
5575 return err;
5578 static const struct got_error *
5579 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5580 const char *child_path, const char *path_base_tree,
5581 struct got_pathlist_head *commitable_paths,
5582 got_worktree_status_cb status_cb, void *status_arg,
5583 struct got_repository *repo)
5585 const struct got_error *err = NULL;
5586 struct got_tree_entry *new_te;
5587 char *subtree_path;
5588 struct got_object_id *id = NULL;
5589 int nentries;
5591 *new_tep = NULL;
5593 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5594 got_path_is_root_dir(path_base_tree) ? "" : "/",
5595 child_path) == -1)
5596 return got_error_from_errno("asprintf");
5598 new_te = calloc(1, sizeof(*new_te));
5599 if (new_te == NULL)
5600 return got_error_from_errno("calloc");
5601 new_te->mode = S_IFDIR;
5603 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5604 sizeof(new_te->name)) {
5605 err = got_error(GOT_ERR_NO_SPACE);
5606 goto done;
5608 err = write_tree(&id, &nentries, NULL, subtree_path,
5609 commitable_paths, status_cb, status_arg, repo);
5610 if (err) {
5611 free(new_te);
5612 goto done;
5614 memcpy(&new_te->id, id, sizeof(new_te->id));
5615 done:
5616 free(id);
5617 free(subtree_path);
5618 if (err == NULL)
5619 *new_tep = new_te;
5620 return err;
5623 static const struct got_error *
5624 write_tree(struct got_object_id **new_tree_id, int *nentries,
5625 struct got_tree_object *base_tree, const char *path_base_tree,
5626 struct got_pathlist_head *commitable_paths,
5627 got_worktree_status_cb status_cb, void *status_arg,
5628 struct got_repository *repo)
5630 const struct got_error *err = NULL;
5631 struct got_pathlist_head paths;
5632 struct got_tree_entry *te, *new_te = NULL;
5633 struct got_pathlist_entry *pe;
5635 TAILQ_INIT(&paths);
5636 *nentries = 0;
5638 /* Insert, and recurse into, newly added entries first. */
5639 TAILQ_FOREACH(pe, commitable_paths, entry) {
5640 struct got_commitable *ct = pe->data;
5641 char *child_path = NULL, *slash;
5643 if ((ct->status != GOT_STATUS_ADD &&
5644 ct->staged_status != GOT_STATUS_ADD) ||
5645 (ct->flags & GOT_COMMITABLE_ADDED))
5646 continue;
5648 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5649 strlen(path_base_tree)))
5650 continue;
5652 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5653 ct->in_repo_path);
5654 if (err)
5655 goto done;
5657 slash = strchr(child_path, '/');
5658 if (slash == NULL) {
5659 err = alloc_added_blob_tree_entry(&new_te, ct);
5660 if (err)
5661 goto done;
5662 err = report_ct_status(ct, status_cb, status_arg);
5663 if (err)
5664 goto done;
5665 ct->flags |= GOT_COMMITABLE_ADDED;
5666 err = insert_tree_entry(new_te, &paths);
5667 if (err)
5668 goto done;
5669 (*nentries)++;
5670 } else {
5671 *slash = '\0'; /* trim trailing path components */
5672 if (base_tree == NULL ||
5673 got_object_tree_find_entry(base_tree, child_path)
5674 == NULL) {
5675 err = make_subtree_for_added_blob(&new_te,
5676 child_path, path_base_tree,
5677 commitable_paths, status_cb, status_arg,
5678 repo);
5679 if (err)
5680 goto done;
5681 err = insert_tree_entry(new_te, &paths);
5682 if (err)
5683 goto done;
5684 (*nentries)++;
5689 if (base_tree) {
5690 int i, nbase_entries;
5691 /* Handle modified and deleted entries. */
5692 nbase_entries = got_object_tree_get_nentries(base_tree);
5693 for (i = 0; i < nbase_entries; i++) {
5694 struct got_commitable *ct = NULL;
5696 te = got_object_tree_get_entry(base_tree, i);
5697 if (got_object_tree_entry_is_submodule(te)) {
5698 /* Entry is a submodule; just copy it. */
5699 err = got_object_tree_entry_dup(&new_te, te);
5700 if (err)
5701 goto done;
5702 err = insert_tree_entry(new_te, &paths);
5703 if (err)
5704 goto done;
5705 (*nentries)++;
5706 continue;
5709 if (S_ISDIR(te->mode)) {
5710 int modified;
5711 err = got_object_tree_entry_dup(&new_te, te);
5712 if (err)
5713 goto done;
5714 err = match_modified_subtree(&modified, te,
5715 path_base_tree, commitable_paths);
5716 if (err)
5717 goto done;
5718 /* Avoid recursion into unmodified subtrees. */
5719 if (modified) {
5720 struct got_object_id *new_id;
5721 int nsubentries;
5722 err = write_subtree(&new_id,
5723 &nsubentries, te,
5724 path_base_tree, commitable_paths,
5725 status_cb, status_arg, repo);
5726 if (err)
5727 goto done;
5728 if (nsubentries == 0) {
5729 /* All entries were deleted. */
5730 free(new_id);
5731 continue;
5733 memcpy(&new_te->id, new_id,
5734 sizeof(new_te->id));
5735 free(new_id);
5737 err = insert_tree_entry(new_te, &paths);
5738 if (err)
5739 goto done;
5740 (*nentries)++;
5741 continue;
5744 err = match_deleted_or_modified_ct(&ct, te,
5745 path_base_tree, commitable_paths);
5746 if (err)
5747 goto done;
5748 if (ct) {
5749 /* NB: Deleted entries get dropped here. */
5750 if (ct->status == GOT_STATUS_MODIFY ||
5751 ct->status == GOT_STATUS_MODE_CHANGE ||
5752 ct->staged_status == GOT_STATUS_MODIFY) {
5753 err = alloc_modified_blob_tree_entry(
5754 &new_te, te, ct);
5755 if (err)
5756 goto done;
5757 err = insert_tree_entry(new_te, &paths);
5758 if (err)
5759 goto done;
5760 (*nentries)++;
5762 err = report_ct_status(ct, status_cb,
5763 status_arg);
5764 if (err)
5765 goto done;
5766 } else {
5767 /* Entry is unchanged; just copy it. */
5768 err = got_object_tree_entry_dup(&new_te, te);
5769 if (err)
5770 goto done;
5771 err = insert_tree_entry(new_te, &paths);
5772 if (err)
5773 goto done;
5774 (*nentries)++;
5779 /* Write new list of entries; deleted entries have been dropped. */
5780 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5781 done:
5782 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5783 return err;
5786 static const struct got_error *
5787 update_fileindex_after_commit(struct got_worktree *worktree,
5788 struct got_pathlist_head *commitable_paths,
5789 struct got_object_id *new_base_commit_id,
5790 struct got_fileindex *fileindex, int have_staged_files)
5792 const struct got_error *err = NULL;
5793 struct got_pathlist_entry *pe;
5794 char *relpath = NULL;
5796 TAILQ_FOREACH(pe, commitable_paths, entry) {
5797 struct got_fileindex_entry *ie;
5798 struct got_commitable *ct = pe->data;
5800 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5802 err = got_path_skip_common_ancestor(&relpath,
5803 worktree->root_path, ct->ondisk_path);
5804 if (err)
5805 goto done;
5807 if (ie) {
5808 if (ct->status == GOT_STATUS_DELETE ||
5809 ct->staged_status == GOT_STATUS_DELETE) {
5810 got_fileindex_entry_remove(fileindex, ie);
5811 } else if (ct->staged_status == GOT_STATUS_ADD ||
5812 ct->staged_status == GOT_STATUS_MODIFY) {
5813 got_fileindex_entry_stage_set(ie,
5814 GOT_FILEIDX_STAGE_NONE);
5815 got_fileindex_entry_staged_filetype_set(ie, 0);
5817 err = got_fileindex_entry_update(ie,
5818 worktree->root_fd, relpath,
5819 ct->staged_blob_id->sha1,
5820 new_base_commit_id->sha1,
5821 !have_staged_files);
5822 } else
5823 err = got_fileindex_entry_update(ie,
5824 worktree->root_fd, relpath,
5825 ct->blob_id->sha1,
5826 new_base_commit_id->sha1,
5827 !have_staged_files);
5828 } else {
5829 err = got_fileindex_entry_alloc(&ie, pe->path);
5830 if (err)
5831 goto done;
5832 err = got_fileindex_entry_update(ie,
5833 worktree->root_fd, relpath, ct->blob_id->sha1,
5834 new_base_commit_id->sha1, 1);
5835 if (err) {
5836 got_fileindex_entry_free(ie);
5837 goto done;
5839 err = got_fileindex_entry_add(fileindex, ie);
5840 if (err) {
5841 got_fileindex_entry_free(ie);
5842 goto done;
5845 free(relpath);
5846 relpath = NULL;
5848 done:
5849 free(relpath);
5850 return err;
5854 static const struct got_error *
5855 check_out_of_date(const char *in_repo_path, unsigned char status,
5856 unsigned char staged_status, struct got_object_id *base_blob_id,
5857 struct got_object_id *base_commit_id,
5858 struct got_object_id *head_commit_id, struct got_repository *repo,
5859 int ood_errcode)
5861 const struct got_error *err = NULL;
5862 struct got_commit_object *commit = NULL;
5863 struct got_object_id *id = NULL;
5865 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5866 /* Trivial case: base commit == head commit */
5867 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5868 return NULL;
5870 * Ensure file content which local changes were based
5871 * on matches file content in the branch head.
5873 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5874 if (err)
5875 goto done;
5876 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5877 if (err) {
5878 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5879 err = got_error(ood_errcode);
5880 goto done;
5881 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5882 err = got_error(ood_errcode);
5883 } else {
5884 /* Require that added files don't exist in the branch head. */
5885 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5886 if (err)
5887 goto done;
5888 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5889 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5890 goto done;
5891 err = id ? got_error(ood_errcode) : NULL;
5893 done:
5894 free(id);
5895 if (commit)
5896 got_object_commit_close(commit);
5897 return err;
5900 static const struct got_error *
5901 commit_worktree(struct got_object_id **new_commit_id,
5902 struct got_pathlist_head *commitable_paths,
5903 struct got_object_id *head_commit_id,
5904 struct got_object_id *parent_id2,
5905 struct got_worktree *worktree,
5906 const char *author, const char *committer, char *diff_path,
5907 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5908 got_worktree_status_cb status_cb, void *status_arg,
5909 struct got_repository *repo)
5911 const struct got_error *err = NULL, *unlockerr = NULL;
5912 struct got_pathlist_entry *pe;
5913 const char *head_ref_name = NULL;
5914 struct got_commit_object *head_commit = NULL;
5915 struct got_reference *head_ref2 = NULL;
5916 struct got_object_id *head_commit_id2 = NULL;
5917 struct got_tree_object *head_tree = NULL;
5918 struct got_object_id *new_tree_id = NULL;
5919 int nentries, nparents = 0;
5920 struct got_object_id_queue parent_ids;
5921 struct got_object_qid *pid = NULL;
5922 char *logmsg = NULL;
5923 time_t timestamp;
5925 *new_commit_id = NULL;
5927 STAILQ_INIT(&parent_ids);
5929 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5930 if (err)
5931 goto done;
5933 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5934 if (err)
5935 goto done;
5937 if (commit_msg_cb != NULL) {
5938 err = commit_msg_cb(commitable_paths, diff_path,
5939 &logmsg, commit_arg);
5940 if (err)
5941 goto done;
5944 if (logmsg == NULL || strlen(logmsg) == 0) {
5945 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5946 goto done;
5949 /* Create blobs from added and modified files and record their IDs. */
5950 TAILQ_FOREACH(pe, commitable_paths, entry) {
5951 struct got_commitable *ct = pe->data;
5952 char *ondisk_path;
5954 /* Blobs for staged files already exist. */
5955 if (ct->staged_status == GOT_STATUS_ADD ||
5956 ct->staged_status == GOT_STATUS_MODIFY)
5957 continue;
5959 if (ct->status != GOT_STATUS_ADD &&
5960 ct->status != GOT_STATUS_MODIFY &&
5961 ct->status != GOT_STATUS_MODE_CHANGE)
5962 continue;
5964 if (asprintf(&ondisk_path, "%s/%s",
5965 worktree->root_path, pe->path) == -1) {
5966 err = got_error_from_errno("asprintf");
5967 goto done;
5969 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5970 free(ondisk_path);
5971 if (err)
5972 goto done;
5975 /* Recursively write new tree objects. */
5976 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5977 commitable_paths, status_cb, status_arg, repo);
5978 if (err)
5979 goto done;
5981 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5982 if (err)
5983 goto done;
5984 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5985 nparents++;
5986 if (parent_id2) {
5987 err = got_object_qid_alloc(&pid, parent_id2);
5988 if (err)
5989 goto done;
5990 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5991 nparents++;
5993 timestamp = time(NULL);
5994 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5995 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5996 if (logmsg != NULL)
5997 free(logmsg);
5998 if (err)
5999 goto done;
6001 /* Check if a concurrent commit to our branch has occurred. */
6002 head_ref_name = got_worktree_get_head_ref_name(worktree);
6003 if (head_ref_name == NULL) {
6004 err = got_error_from_errno("got_worktree_get_head_ref_name");
6005 goto done;
6007 /* Lock the reference here to prevent concurrent modification. */
6008 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6009 if (err)
6010 goto done;
6011 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6012 if (err)
6013 goto done;
6014 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6015 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6016 goto done;
6018 /* Update branch head in repository. */
6019 err = got_ref_change_ref(head_ref2, *new_commit_id);
6020 if (err)
6021 goto done;
6022 err = got_ref_write(head_ref2, repo);
6023 if (err)
6024 goto done;
6026 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6027 if (err)
6028 goto done;
6030 err = ref_base_commit(worktree, repo);
6031 if (err)
6032 goto done;
6033 done:
6034 got_object_id_queue_free(&parent_ids);
6035 if (head_tree)
6036 got_object_tree_close(head_tree);
6037 if (head_commit)
6038 got_object_commit_close(head_commit);
6039 free(head_commit_id2);
6040 if (head_ref2) {
6041 unlockerr = got_ref_unlock(head_ref2);
6042 if (unlockerr && err == NULL)
6043 err = unlockerr;
6044 got_ref_close(head_ref2);
6046 return err;
6049 static const struct got_error *
6050 check_path_is_commitable(const char *path,
6051 struct got_pathlist_head *commitable_paths)
6053 struct got_pathlist_entry *cpe = NULL;
6054 size_t path_len = strlen(path);
6056 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6057 struct got_commitable *ct = cpe->data;
6058 const char *ct_path = ct->path;
6060 while (ct_path[0] == '/')
6061 ct_path++;
6063 if (strcmp(path, ct_path) == 0 ||
6064 got_path_is_child(ct_path, path, path_len))
6065 break;
6068 if (cpe == NULL)
6069 return got_error_path(path, GOT_ERR_BAD_PATH);
6071 return NULL;
6074 static const struct got_error *
6075 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6077 int *have_staged_files = arg;
6079 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6080 *have_staged_files = 1;
6081 return got_error(GOT_ERR_CANCELLED);
6084 return NULL;
6087 static const struct got_error *
6088 check_non_staged_files(struct got_fileindex *fileindex,
6089 struct got_pathlist_head *paths)
6091 struct got_pathlist_entry *pe;
6092 struct got_fileindex_entry *ie;
6094 TAILQ_FOREACH(pe, paths, entry) {
6095 if (pe->path[0] == '\0')
6096 continue;
6097 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6098 if (ie == NULL)
6099 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6100 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6101 return got_error_path(pe->path,
6102 GOT_ERR_FILE_NOT_STAGED);
6105 return NULL;
6108 const struct got_error *
6109 got_worktree_commit(struct got_object_id **new_commit_id,
6110 struct got_worktree *worktree, struct got_pathlist_head *paths,
6111 const char *author, const char *committer, int allow_bad_symlinks,
6112 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6113 got_worktree_status_cb status_cb, void *status_arg,
6114 struct got_repository *repo)
6116 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6117 struct got_fileindex *fileindex = NULL;
6118 char *fileindex_path = NULL;
6119 struct got_pathlist_head commitable_paths;
6120 struct collect_commitables_arg cc_arg;
6121 struct got_pathlist_entry *pe;
6122 struct got_reference *head_ref = NULL;
6123 struct got_object_id *head_commit_id = NULL;
6124 char *diff_path = NULL;
6125 int have_staged_files = 0;
6127 *new_commit_id = NULL;
6129 memset(&cc_arg, 0, sizeof(cc_arg));
6130 TAILQ_INIT(&commitable_paths);
6132 err = lock_worktree(worktree, LOCK_EX);
6133 if (err)
6134 goto done;
6136 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6137 if (err)
6138 goto done;
6140 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6141 if (err)
6142 goto done;
6144 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6145 if (err)
6146 goto done;
6148 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6149 &have_staged_files);
6150 if (err && err->code != GOT_ERR_CANCELLED)
6151 goto done;
6152 if (have_staged_files) {
6153 err = check_non_staged_files(fileindex, paths);
6154 if (err)
6155 goto done;
6158 cc_arg.commitable_paths = &commitable_paths;
6159 cc_arg.worktree = worktree;
6160 cc_arg.fileindex = fileindex;
6161 cc_arg.repo = repo;
6162 cc_arg.have_staged_files = have_staged_files;
6163 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6164 cc_arg.diff_header_shown = 0;
6165 if (show_diff) {
6166 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6167 GOT_TMPDIR_STR "/got", ".diff");
6168 if (err)
6169 goto done;
6170 cc_arg.f1 = got_opentemp();
6171 if (cc_arg.f1 == NULL) {
6172 err = got_error_from_errno("got_opentemp");
6173 goto done;
6175 cc_arg.f2 = got_opentemp();
6176 if (cc_arg.f2 == NULL) {
6177 err = got_error_from_errno("got_opentemp");
6178 goto done;
6182 TAILQ_FOREACH(pe, paths, entry) {
6183 err = worktree_status(worktree, pe->path, fileindex, repo,
6184 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6185 if (err)
6186 goto done;
6189 if (show_diff) {
6190 if (fflush(cc_arg.diff_outfile) == EOF) {
6191 err = got_error_from_errno("fflush");
6192 goto done;
6196 if (TAILQ_EMPTY(&commitable_paths)) {
6197 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6198 goto done;
6201 TAILQ_FOREACH(pe, paths, entry) {
6202 err = check_path_is_commitable(pe->path, &commitable_paths);
6203 if (err)
6204 goto done;
6207 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6208 struct got_commitable *ct = pe->data;
6209 const char *ct_path = ct->in_repo_path;
6211 while (ct_path[0] == '/')
6212 ct_path++;
6213 err = check_out_of_date(ct_path, ct->status,
6214 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6215 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6216 if (err)
6217 goto done;
6221 err = commit_worktree(new_commit_id, &commitable_paths,
6222 head_commit_id, NULL, worktree, author, committer,
6223 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6224 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6225 if (err)
6226 goto done;
6228 err = update_fileindex_after_commit(worktree, &commitable_paths,
6229 *new_commit_id, fileindex, have_staged_files);
6230 sync_err = sync_fileindex(fileindex, fileindex_path);
6231 if (sync_err && err == NULL)
6232 err = sync_err;
6233 done:
6234 if (fileindex)
6235 got_fileindex_free(fileindex);
6236 free(fileindex_path);
6237 unlockerr = lock_worktree(worktree, LOCK_SH);
6238 if (unlockerr && err == NULL)
6239 err = unlockerr;
6240 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6241 struct got_commitable *ct = pe->data;
6243 free_commitable(ct);
6245 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6246 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6247 err = got_error_from_errno2("unlink", diff_path);
6248 free(diff_path);
6249 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6250 err == NULL)
6251 err = got_error_from_errno("fclose");
6252 return err;
6255 const char *
6256 got_commitable_get_path(struct got_commitable *ct)
6258 return ct->path;
6261 unsigned int
6262 got_commitable_get_status(struct got_commitable *ct)
6264 return ct->status;
6267 struct check_rebase_ok_arg {
6268 struct got_worktree *worktree;
6269 struct got_repository *repo;
6272 static const struct got_error *
6273 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6275 const struct got_error *err = NULL;
6276 struct check_rebase_ok_arg *a = arg;
6277 unsigned char status;
6278 struct stat sb;
6279 char *ondisk_path;
6281 /* Reject rebase of a work tree with mixed base commits. */
6282 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6283 SHA1_DIGEST_LENGTH))
6284 return got_error(GOT_ERR_MIXED_COMMITS);
6286 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6287 == -1)
6288 return got_error_from_errno("asprintf");
6290 /* Reject rebase of a work tree with modified or staged files. */
6291 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6292 free(ondisk_path);
6293 if (err)
6294 return err;
6296 if (status != GOT_STATUS_NO_CHANGE)
6297 return got_error(GOT_ERR_MODIFIED);
6298 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6299 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6301 return NULL;
6304 const struct got_error *
6305 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6306 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6307 struct got_worktree *worktree, struct got_reference *branch,
6308 struct got_repository *repo)
6310 const struct got_error *err = NULL;
6311 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6312 char *branch_ref_name = NULL;
6313 char *fileindex_path = NULL;
6314 struct check_rebase_ok_arg ok_arg;
6315 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6316 struct got_object_id *wt_branch_tip = NULL;
6318 *new_base_branch_ref = NULL;
6319 *tmp_branch = NULL;
6320 *fileindex = NULL;
6322 err = lock_worktree(worktree, LOCK_EX);
6323 if (err)
6324 return err;
6326 err = open_fileindex(fileindex, &fileindex_path, worktree);
6327 if (err)
6328 goto done;
6330 ok_arg.worktree = worktree;
6331 ok_arg.repo = repo;
6332 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6333 &ok_arg);
6334 if (err)
6335 goto done;
6337 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6338 if (err)
6339 goto done;
6341 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6342 if (err)
6343 goto done;
6345 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6346 if (err)
6347 goto done;
6349 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6350 0);
6351 if (err)
6352 goto done;
6354 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6355 if (err)
6356 goto done;
6357 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6358 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6359 goto done;
6362 err = got_ref_alloc_symref(new_base_branch_ref,
6363 new_base_branch_ref_name, wt_branch);
6364 if (err)
6365 goto done;
6366 err = got_ref_write(*new_base_branch_ref, repo);
6367 if (err)
6368 goto done;
6370 /* TODO Lock original branch's ref while rebasing? */
6372 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6373 if (err)
6374 goto done;
6376 err = got_ref_write(branch_ref, repo);
6377 if (err)
6378 goto done;
6380 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6381 worktree->base_commit_id);
6382 if (err)
6383 goto done;
6384 err = got_ref_write(*tmp_branch, repo);
6385 if (err)
6386 goto done;
6388 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6389 if (err)
6390 goto done;
6391 done:
6392 free(fileindex_path);
6393 free(tmp_branch_name);
6394 free(new_base_branch_ref_name);
6395 free(branch_ref_name);
6396 if (branch_ref)
6397 got_ref_close(branch_ref);
6398 if (wt_branch)
6399 got_ref_close(wt_branch);
6400 free(wt_branch_tip);
6401 if (err) {
6402 if (*new_base_branch_ref) {
6403 got_ref_close(*new_base_branch_ref);
6404 *new_base_branch_ref = NULL;
6406 if (*tmp_branch) {
6407 got_ref_close(*tmp_branch);
6408 *tmp_branch = NULL;
6410 if (*fileindex) {
6411 got_fileindex_free(*fileindex);
6412 *fileindex = NULL;
6414 lock_worktree(worktree, LOCK_SH);
6416 return err;
6419 const struct got_error *
6420 got_worktree_rebase_continue(struct got_object_id **commit_id,
6421 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6422 struct got_reference **branch, struct got_fileindex **fileindex,
6423 struct got_worktree *worktree, struct got_repository *repo)
6425 const struct got_error *err;
6426 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6427 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6428 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6429 char *fileindex_path = NULL;
6430 int have_staged_files = 0;
6432 *commit_id = NULL;
6433 *new_base_branch = NULL;
6434 *tmp_branch = NULL;
6435 *branch = NULL;
6436 *fileindex = NULL;
6438 err = lock_worktree(worktree, LOCK_EX);
6439 if (err)
6440 return err;
6442 err = open_fileindex(fileindex, &fileindex_path, worktree);
6443 if (err)
6444 goto done;
6446 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6447 &have_staged_files);
6448 if (err && err->code != GOT_ERR_CANCELLED)
6449 goto done;
6450 if (have_staged_files) {
6451 err = got_error(GOT_ERR_STAGED_PATHS);
6452 goto done;
6455 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6456 if (err)
6457 goto done;
6459 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6460 if (err)
6461 goto done;
6463 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6464 if (err)
6465 goto done;
6467 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6468 if (err)
6469 goto done;
6471 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6472 if (err)
6473 goto done;
6475 err = got_ref_open(branch, repo,
6476 got_ref_get_symref_target(branch_ref), 0);
6477 if (err)
6478 goto done;
6480 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6481 if (err)
6482 goto done;
6484 err = got_ref_resolve(commit_id, repo, commit_ref);
6485 if (err)
6486 goto done;
6488 err = got_ref_open(new_base_branch, repo,
6489 new_base_branch_ref_name, 0);
6490 if (err)
6491 goto done;
6493 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6494 if (err)
6495 goto done;
6496 done:
6497 free(commit_ref_name);
6498 free(branch_ref_name);
6499 free(fileindex_path);
6500 if (commit_ref)
6501 got_ref_close(commit_ref);
6502 if (branch_ref)
6503 got_ref_close(branch_ref);
6504 if (err) {
6505 free(*commit_id);
6506 *commit_id = NULL;
6507 if (*tmp_branch) {
6508 got_ref_close(*tmp_branch);
6509 *tmp_branch = NULL;
6511 if (*new_base_branch) {
6512 got_ref_close(*new_base_branch);
6513 *new_base_branch = NULL;
6515 if (*branch) {
6516 got_ref_close(*branch);
6517 *branch = NULL;
6519 if (*fileindex) {
6520 got_fileindex_free(*fileindex);
6521 *fileindex = NULL;
6523 lock_worktree(worktree, LOCK_SH);
6525 return err;
6528 const struct got_error *
6529 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6531 const struct got_error *err;
6532 char *tmp_branch_name = NULL;
6534 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6535 if (err)
6536 return err;
6538 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6539 free(tmp_branch_name);
6540 return NULL;
6543 static const struct got_error *
6544 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6545 const char *diff_path, char **logmsg, void *arg)
6547 *logmsg = arg;
6548 return NULL;
6551 static const struct got_error *
6552 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6553 const char *path, struct got_object_id *blob_id,
6554 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6555 int dirfd, const char *de_name)
6557 return NULL;
6560 struct collect_merged_paths_arg {
6561 got_worktree_checkout_cb progress_cb;
6562 void *progress_arg;
6563 struct got_pathlist_head *merged_paths;
6566 static const struct got_error *
6567 collect_merged_paths(void *arg, unsigned char status, const char *path)
6569 const struct got_error *err;
6570 struct collect_merged_paths_arg *a = arg;
6571 char *p;
6572 struct got_pathlist_entry *new;
6574 err = (*a->progress_cb)(a->progress_arg, status, path);
6575 if (err)
6576 return err;
6578 if (status != GOT_STATUS_MERGE &&
6579 status != GOT_STATUS_ADD &&
6580 status != GOT_STATUS_DELETE &&
6581 status != GOT_STATUS_CONFLICT)
6582 return NULL;
6584 p = strdup(path);
6585 if (p == NULL)
6586 return got_error_from_errno("strdup");
6588 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6589 if (err || new == NULL)
6590 free(p);
6591 return err;
6594 static const struct got_error *
6595 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6596 int is_rebase, struct got_repository *repo)
6598 const struct got_error *err;
6599 struct got_reference *commit_ref = NULL;
6601 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6602 if (err) {
6603 if (err->code != GOT_ERR_NOT_REF)
6604 goto done;
6605 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6606 if (err)
6607 goto done;
6608 err = got_ref_write(commit_ref, repo);
6609 if (err)
6610 goto done;
6611 } else if (is_rebase) {
6612 struct got_object_id *stored_id;
6613 int cmp;
6615 err = got_ref_resolve(&stored_id, repo, commit_ref);
6616 if (err)
6617 goto done;
6618 cmp = got_object_id_cmp(commit_id, stored_id);
6619 free(stored_id);
6620 if (cmp != 0) {
6621 err = got_error(GOT_ERR_REBASE_COMMITID);
6622 goto done;
6625 done:
6626 if (commit_ref)
6627 got_ref_close(commit_ref);
6628 return err;
6631 static const struct got_error *
6632 rebase_merge_files(struct got_pathlist_head *merged_paths,
6633 const char *commit_ref_name, struct got_worktree *worktree,
6634 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6635 struct got_object_id *commit_id, struct got_repository *repo,
6636 got_worktree_checkout_cb progress_cb, void *progress_arg,
6637 got_cancel_cb cancel_cb, void *cancel_arg)
6639 const struct got_error *err;
6640 struct got_reference *commit_ref = NULL;
6641 struct collect_merged_paths_arg cmp_arg;
6642 char *fileindex_path;
6644 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6646 err = get_fileindex_path(&fileindex_path, worktree);
6647 if (err)
6648 return err;
6650 cmp_arg.progress_cb = progress_cb;
6651 cmp_arg.progress_arg = progress_arg;
6652 cmp_arg.merged_paths = merged_paths;
6653 err = merge_files(worktree, fileindex, fileindex_path,
6654 parent_commit_id, commit_id, repo, collect_merged_paths,
6655 &cmp_arg, cancel_cb, cancel_arg);
6656 if (commit_ref)
6657 got_ref_close(commit_ref);
6658 return err;
6661 const struct got_error *
6662 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6663 struct got_worktree *worktree, struct got_fileindex *fileindex,
6664 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6665 struct got_repository *repo,
6666 got_worktree_checkout_cb progress_cb, void *progress_arg,
6667 got_cancel_cb cancel_cb, void *cancel_arg)
6669 const struct got_error *err;
6670 char *commit_ref_name;
6672 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6673 if (err)
6674 return err;
6676 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6677 if (err)
6678 goto done;
6680 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6681 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6682 progress_arg, cancel_cb, cancel_arg);
6683 done:
6684 free(commit_ref_name);
6685 return err;
6688 const struct got_error *
6689 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6690 struct got_worktree *worktree, struct got_fileindex *fileindex,
6691 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6692 struct got_repository *repo,
6693 got_worktree_checkout_cb progress_cb, void *progress_arg,
6694 got_cancel_cb cancel_cb, void *cancel_arg)
6696 const struct got_error *err;
6697 char *commit_ref_name;
6699 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6700 if (err)
6701 return err;
6703 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6704 if (err)
6705 goto done;
6707 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6708 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6709 progress_arg, cancel_cb, cancel_arg);
6710 done:
6711 free(commit_ref_name);
6712 return err;
6715 static const struct got_error *
6716 rebase_commit(struct got_object_id **new_commit_id,
6717 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6718 struct got_worktree *worktree, struct got_fileindex *fileindex,
6719 struct got_reference *tmp_branch, const char *committer,
6720 struct got_commit_object *orig_commit, const char *new_logmsg,
6721 struct got_repository *repo)
6723 const struct got_error *err, *sync_err;
6724 struct got_pathlist_head commitable_paths;
6725 struct collect_commitables_arg cc_arg;
6726 char *fileindex_path = NULL;
6727 struct got_reference *head_ref = NULL;
6728 struct got_object_id *head_commit_id = NULL;
6729 char *logmsg = NULL;
6731 memset(&cc_arg, 0, sizeof(cc_arg));
6732 TAILQ_INIT(&commitable_paths);
6733 *new_commit_id = NULL;
6735 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6737 err = get_fileindex_path(&fileindex_path, worktree);
6738 if (err)
6739 return err;
6741 cc_arg.commitable_paths = &commitable_paths;
6742 cc_arg.worktree = worktree;
6743 cc_arg.repo = repo;
6744 cc_arg.have_staged_files = 0;
6746 * If possible get the status of individual files directly to
6747 * avoid crawling the entire work tree once per rebased commit.
6749 * Ideally, merged_paths would contain a list of commitables
6750 * we could use so we could skip worktree_status() entirely.
6751 * However, we would then need carefully keep track of cumulative
6752 * effects of operations such as file additions and deletions
6753 * in 'got histedit -f' (folding multiple commits into one),
6754 * and this extra complexity is not really worth it.
6756 if (merged_paths) {
6757 struct got_pathlist_entry *pe;
6758 TAILQ_FOREACH(pe, merged_paths, entry) {
6759 err = worktree_status(worktree, pe->path, fileindex,
6760 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6761 0);
6762 if (err)
6763 goto done;
6765 } else {
6766 err = worktree_status(worktree, "", fileindex, repo,
6767 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6768 if (err)
6769 goto done;
6772 if (TAILQ_EMPTY(&commitable_paths)) {
6773 /* No-op change; commit will be elided. */
6774 err = got_ref_delete(commit_ref, repo);
6775 if (err)
6776 goto done;
6777 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6778 goto done;
6781 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6782 if (err)
6783 goto done;
6785 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6786 if (err)
6787 goto done;
6789 if (new_logmsg) {
6790 logmsg = strdup(new_logmsg);
6791 if (logmsg == NULL) {
6792 err = got_error_from_errno("strdup");
6793 goto done;
6795 } else {
6796 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6797 if (err)
6798 goto done;
6801 /* NB: commit_worktree will call free(logmsg) */
6802 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6803 NULL, worktree, got_object_commit_get_author(orig_commit),
6804 committer ? committer :
6805 got_object_commit_get_committer(orig_commit), NULL,
6806 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6807 if (err)
6808 goto done;
6810 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6811 if (err)
6812 goto done;
6814 err = got_ref_delete(commit_ref, repo);
6815 if (err)
6816 goto done;
6818 err = update_fileindex_after_commit(worktree, &commitable_paths,
6819 *new_commit_id, fileindex, 0);
6820 sync_err = sync_fileindex(fileindex, fileindex_path);
6821 if (sync_err && err == NULL)
6822 err = sync_err;
6823 done:
6824 free(fileindex_path);
6825 free(head_commit_id);
6826 if (head_ref)
6827 got_ref_close(head_ref);
6828 if (err) {
6829 free(*new_commit_id);
6830 *new_commit_id = NULL;
6832 return err;
6835 const struct got_error *
6836 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6837 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6838 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6839 const char *committer, struct got_commit_object *orig_commit,
6840 struct got_object_id *orig_commit_id, struct got_repository *repo)
6842 const struct got_error *err;
6843 char *commit_ref_name;
6844 struct got_reference *commit_ref = NULL;
6845 struct got_object_id *commit_id = NULL;
6847 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6848 if (err)
6849 return err;
6851 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6852 if (err)
6853 goto done;
6854 err = got_ref_resolve(&commit_id, repo, commit_ref);
6855 if (err)
6856 goto done;
6857 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6858 err = got_error(GOT_ERR_REBASE_COMMITID);
6859 goto done;
6862 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6863 worktree, fileindex, tmp_branch, committer, orig_commit,
6864 NULL, repo);
6865 done:
6866 if (commit_ref)
6867 got_ref_close(commit_ref);
6868 free(commit_ref_name);
6869 free(commit_id);
6870 return err;
6873 const struct got_error *
6874 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6875 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6876 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6877 const char *committer, struct got_commit_object *orig_commit,
6878 struct got_object_id *orig_commit_id, const char *new_logmsg,
6879 struct got_repository *repo)
6881 const struct got_error *err;
6882 char *commit_ref_name;
6883 struct got_reference *commit_ref = NULL;
6885 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6886 if (err)
6887 return err;
6889 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6890 if (err)
6891 goto done;
6893 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6894 worktree, fileindex, tmp_branch, committer, orig_commit,
6895 new_logmsg, repo);
6896 done:
6897 if (commit_ref)
6898 got_ref_close(commit_ref);
6899 free(commit_ref_name);
6900 return err;
6903 const struct got_error *
6904 got_worktree_rebase_postpone(struct got_worktree *worktree,
6905 struct got_fileindex *fileindex)
6907 if (fileindex)
6908 got_fileindex_free(fileindex);
6909 return lock_worktree(worktree, LOCK_SH);
6912 static const struct got_error *
6913 delete_ref(const char *name, struct got_repository *repo)
6915 const struct got_error *err;
6916 struct got_reference *ref;
6918 err = got_ref_open(&ref, repo, name, 0);
6919 if (err) {
6920 if (err->code == GOT_ERR_NOT_REF)
6921 return NULL;
6922 return err;
6925 err = got_ref_delete(ref, repo);
6926 got_ref_close(ref);
6927 return err;
6930 static const struct got_error *
6931 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6933 const struct got_error *err;
6934 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6935 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6937 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6938 if (err)
6939 goto done;
6940 err = delete_ref(tmp_branch_name, repo);
6941 if (err)
6942 goto done;
6944 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6945 if (err)
6946 goto done;
6947 err = delete_ref(new_base_branch_ref_name, repo);
6948 if (err)
6949 goto done;
6951 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6952 if (err)
6953 goto done;
6954 err = delete_ref(branch_ref_name, repo);
6955 if (err)
6956 goto done;
6958 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6959 if (err)
6960 goto done;
6961 err = delete_ref(commit_ref_name, repo);
6962 if (err)
6963 goto done;
6965 done:
6966 free(tmp_branch_name);
6967 free(new_base_branch_ref_name);
6968 free(branch_ref_name);
6969 free(commit_ref_name);
6970 return err;
6973 static const struct got_error *
6974 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6975 struct got_object_id *new_commit_id, struct got_repository *repo)
6977 const struct got_error *err;
6978 struct got_reference *ref = NULL;
6979 struct got_object_id *old_commit_id = NULL;
6980 const char *branch_name = NULL;
6981 char *new_id_str = NULL;
6982 char *refname = NULL;
6984 branch_name = got_ref_get_name(branch);
6985 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6986 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6987 branch_name += 11;
6989 err = got_object_id_str(&new_id_str, new_commit_id);
6990 if (err)
6991 return err;
6993 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6994 new_id_str) == -1) {
6995 err = got_error_from_errno("asprintf");
6996 goto done;
6999 err = got_ref_resolve(&old_commit_id, repo, branch);
7000 if (err)
7001 goto done;
7003 err = got_ref_alloc(&ref, refname, old_commit_id);
7004 if (err)
7005 goto done;
7007 err = got_ref_write(ref, repo);
7008 done:
7009 free(new_id_str);
7010 free(refname);
7011 free(old_commit_id);
7012 if (ref)
7013 got_ref_close(ref);
7014 return err;
7017 const struct got_error *
7018 got_worktree_rebase_complete(struct got_worktree *worktree,
7019 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
7020 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
7021 struct got_repository *repo, int create_backup)
7023 const struct got_error *err, *unlockerr, *sync_err;
7024 struct got_object_id *new_head_commit_id = NULL;
7025 char *fileindex_path = NULL;
7027 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7028 if (err)
7029 return err;
7031 if (create_backup) {
7032 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7033 rebased_branch, new_head_commit_id, repo);
7034 if (err)
7035 goto done;
7038 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7039 if (err)
7040 goto done;
7042 err = got_ref_write(rebased_branch, repo);
7043 if (err)
7044 goto done;
7046 err = got_worktree_set_head_ref(worktree, rebased_branch);
7047 if (err)
7048 goto done;
7050 err = delete_rebase_refs(worktree, repo);
7051 if (err)
7052 goto done;
7054 err = get_fileindex_path(&fileindex_path, worktree);
7055 if (err)
7056 goto done;
7057 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7058 sync_err = sync_fileindex(fileindex, fileindex_path);
7059 if (sync_err && err == NULL)
7060 err = sync_err;
7061 done:
7062 got_fileindex_free(fileindex);
7063 free(fileindex_path);
7064 free(new_head_commit_id);
7065 unlockerr = lock_worktree(worktree, LOCK_SH);
7066 if (unlockerr && err == NULL)
7067 err = unlockerr;
7068 return err;
7071 const struct got_error *
7072 got_worktree_rebase_abort(struct got_worktree *worktree,
7073 struct got_fileindex *fileindex, struct got_repository *repo,
7074 struct got_reference *new_base_branch,
7075 got_worktree_checkout_cb progress_cb, void *progress_arg)
7077 const struct got_error *err, *unlockerr, *sync_err;
7078 struct got_reference *resolved = NULL;
7079 struct got_object_id *commit_id = NULL;
7080 struct got_commit_object *commit = NULL;
7081 char *fileindex_path = NULL;
7082 struct revert_file_args rfa;
7083 struct got_object_id *tree_id = NULL;
7085 err = lock_worktree(worktree, LOCK_EX);
7086 if (err)
7087 return err;
7089 err = got_object_open_as_commit(&commit, repo,
7090 worktree->base_commit_id);
7091 if (err)
7092 goto done;
7094 err = got_ref_open(&resolved, repo,
7095 got_ref_get_symref_target(new_base_branch), 0);
7096 if (err)
7097 goto done;
7099 err = got_worktree_set_head_ref(worktree, resolved);
7100 if (err)
7101 goto done;
7104 * XXX commits to the base branch could have happened while
7105 * we were busy rebasing; should we store the original commit ID
7106 * when rebase begins and read it back here?
7108 err = got_ref_resolve(&commit_id, repo, resolved);
7109 if (err)
7110 goto done;
7112 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7113 if (err)
7114 goto done;
7116 err = got_object_id_by_path(&tree_id, repo, commit,
7117 worktree->path_prefix);
7118 if (err)
7119 goto done;
7121 err = delete_rebase_refs(worktree, repo);
7122 if (err)
7123 goto done;
7125 err = get_fileindex_path(&fileindex_path, worktree);
7126 if (err)
7127 goto done;
7129 rfa.worktree = worktree;
7130 rfa.fileindex = fileindex;
7131 rfa.progress_cb = progress_cb;
7132 rfa.progress_arg = progress_arg;
7133 rfa.patch_cb = NULL;
7134 rfa.patch_arg = NULL;
7135 rfa.repo = repo;
7136 rfa.unlink_added_files = 0;
7137 err = worktree_status(worktree, "", fileindex, repo,
7138 revert_file, &rfa, NULL, NULL, 1, 0);
7139 if (err)
7140 goto sync;
7142 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7143 repo, progress_cb, progress_arg, NULL, NULL);
7144 sync:
7145 sync_err = sync_fileindex(fileindex, fileindex_path);
7146 if (sync_err && err == NULL)
7147 err = sync_err;
7148 done:
7149 got_ref_close(resolved);
7150 free(tree_id);
7151 free(commit_id);
7152 if (commit)
7153 got_object_commit_close(commit);
7154 if (fileindex)
7155 got_fileindex_free(fileindex);
7156 free(fileindex_path);
7158 unlockerr = lock_worktree(worktree, LOCK_SH);
7159 if (unlockerr && err == NULL)
7160 err = unlockerr;
7161 return err;
7164 const struct got_error *
7165 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7166 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7167 struct got_fileindex **fileindex, struct got_worktree *worktree,
7168 struct got_repository *repo)
7170 const struct got_error *err = NULL;
7171 char *tmp_branch_name = NULL;
7172 char *branch_ref_name = NULL;
7173 char *base_commit_ref_name = NULL;
7174 char *fileindex_path = NULL;
7175 struct check_rebase_ok_arg ok_arg;
7176 struct got_reference *wt_branch = NULL;
7177 struct got_reference *base_commit_ref = NULL;
7179 *tmp_branch = NULL;
7180 *branch_ref = NULL;
7181 *base_commit_id = NULL;
7182 *fileindex = NULL;
7184 err = lock_worktree(worktree, LOCK_EX);
7185 if (err)
7186 return err;
7188 err = open_fileindex(fileindex, &fileindex_path, worktree);
7189 if (err)
7190 goto done;
7192 ok_arg.worktree = worktree;
7193 ok_arg.repo = repo;
7194 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7195 &ok_arg);
7196 if (err)
7197 goto done;
7199 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7200 if (err)
7201 goto done;
7203 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7204 if (err)
7205 goto done;
7207 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7208 worktree);
7209 if (err)
7210 goto done;
7212 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7213 0);
7214 if (err)
7215 goto done;
7217 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7218 if (err)
7219 goto done;
7221 err = got_ref_write(*branch_ref, repo);
7222 if (err)
7223 goto done;
7225 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7226 worktree->base_commit_id);
7227 if (err)
7228 goto done;
7229 err = got_ref_write(base_commit_ref, repo);
7230 if (err)
7231 goto done;
7232 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7233 if (*base_commit_id == NULL) {
7234 err = got_error_from_errno("got_object_id_dup");
7235 goto done;
7238 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7239 worktree->base_commit_id);
7240 if (err)
7241 goto done;
7242 err = got_ref_write(*tmp_branch, repo);
7243 if (err)
7244 goto done;
7246 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7247 if (err)
7248 goto done;
7249 done:
7250 free(fileindex_path);
7251 free(tmp_branch_name);
7252 free(branch_ref_name);
7253 free(base_commit_ref_name);
7254 if (wt_branch)
7255 got_ref_close(wt_branch);
7256 if (err) {
7257 if (*branch_ref) {
7258 got_ref_close(*branch_ref);
7259 *branch_ref = NULL;
7261 if (*tmp_branch) {
7262 got_ref_close(*tmp_branch);
7263 *tmp_branch = NULL;
7265 free(*base_commit_id);
7266 if (*fileindex) {
7267 got_fileindex_free(*fileindex);
7268 *fileindex = NULL;
7270 lock_worktree(worktree, LOCK_SH);
7272 return err;
7275 const struct got_error *
7276 got_worktree_histedit_postpone(struct got_worktree *worktree,
7277 struct got_fileindex *fileindex)
7279 if (fileindex)
7280 got_fileindex_free(fileindex);
7281 return lock_worktree(worktree, LOCK_SH);
7284 const struct got_error *
7285 got_worktree_histedit_in_progress(int *in_progress,
7286 struct got_worktree *worktree)
7288 const struct got_error *err;
7289 char *tmp_branch_name = NULL;
7291 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7292 if (err)
7293 return err;
7295 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7296 free(tmp_branch_name);
7297 return NULL;
7300 const struct got_error *
7301 got_worktree_histedit_continue(struct got_object_id **commit_id,
7302 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7303 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7304 struct got_worktree *worktree, struct got_repository *repo)
7306 const struct got_error *err;
7307 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7308 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7309 struct got_reference *commit_ref = NULL;
7310 struct got_reference *base_commit_ref = NULL;
7311 char *fileindex_path = NULL;
7312 int have_staged_files = 0;
7314 *commit_id = NULL;
7315 *tmp_branch = NULL;
7316 *base_commit_id = NULL;
7317 *fileindex = NULL;
7319 err = lock_worktree(worktree, LOCK_EX);
7320 if (err)
7321 return err;
7323 err = open_fileindex(fileindex, &fileindex_path, worktree);
7324 if (err)
7325 goto done;
7327 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7328 &have_staged_files);
7329 if (err && err->code != GOT_ERR_CANCELLED)
7330 goto done;
7331 if (have_staged_files) {
7332 err = got_error(GOT_ERR_STAGED_PATHS);
7333 goto done;
7336 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7337 if (err)
7338 goto done;
7340 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7341 if (err)
7342 goto done;
7344 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7345 if (err)
7346 goto done;
7348 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7349 worktree);
7350 if (err)
7351 goto done;
7353 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7354 if (err)
7355 goto done;
7357 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7358 if (err)
7359 goto done;
7360 err = got_ref_resolve(commit_id, repo, commit_ref);
7361 if (err)
7362 goto done;
7364 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7365 if (err)
7366 goto done;
7367 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7368 if (err)
7369 goto done;
7371 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7372 if (err)
7373 goto done;
7374 done:
7375 free(commit_ref_name);
7376 free(branch_ref_name);
7377 free(fileindex_path);
7378 if (commit_ref)
7379 got_ref_close(commit_ref);
7380 if (base_commit_ref)
7381 got_ref_close(base_commit_ref);
7382 if (err) {
7383 free(*commit_id);
7384 *commit_id = NULL;
7385 free(*base_commit_id);
7386 *base_commit_id = NULL;
7387 if (*tmp_branch) {
7388 got_ref_close(*tmp_branch);
7389 *tmp_branch = NULL;
7391 if (*fileindex) {
7392 got_fileindex_free(*fileindex);
7393 *fileindex = NULL;
7395 lock_worktree(worktree, LOCK_EX);
7397 return err;
7400 static const struct got_error *
7401 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7403 const struct got_error *err;
7404 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7405 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7407 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7408 if (err)
7409 goto done;
7410 err = delete_ref(tmp_branch_name, repo);
7411 if (err)
7412 goto done;
7414 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7415 worktree);
7416 if (err)
7417 goto done;
7418 err = delete_ref(base_commit_ref_name, repo);
7419 if (err)
7420 goto done;
7422 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7423 if (err)
7424 goto done;
7425 err = delete_ref(branch_ref_name, repo);
7426 if (err)
7427 goto done;
7429 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7430 if (err)
7431 goto done;
7432 err = delete_ref(commit_ref_name, repo);
7433 if (err)
7434 goto done;
7435 done:
7436 free(tmp_branch_name);
7437 free(base_commit_ref_name);
7438 free(branch_ref_name);
7439 free(commit_ref_name);
7440 return err;
7443 const struct got_error *
7444 got_worktree_histedit_abort(struct got_worktree *worktree,
7445 struct got_fileindex *fileindex, struct got_repository *repo,
7446 struct got_reference *branch, struct got_object_id *base_commit_id,
7447 got_worktree_checkout_cb progress_cb, void *progress_arg)
7449 const struct got_error *err, *unlockerr, *sync_err;
7450 struct got_reference *resolved = NULL;
7451 char *fileindex_path = NULL;
7452 struct got_commit_object *commit = NULL;
7453 struct got_object_id *tree_id = NULL;
7454 struct revert_file_args rfa;
7456 err = lock_worktree(worktree, LOCK_EX);
7457 if (err)
7458 return err;
7460 err = got_object_open_as_commit(&commit, repo,
7461 worktree->base_commit_id);
7462 if (err)
7463 goto done;
7465 err = got_ref_open(&resolved, repo,
7466 got_ref_get_symref_target(branch), 0);
7467 if (err)
7468 goto done;
7470 err = got_worktree_set_head_ref(worktree, resolved);
7471 if (err)
7472 goto done;
7474 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7475 if (err)
7476 goto done;
7478 err = got_object_id_by_path(&tree_id, repo, commit,
7479 worktree->path_prefix);
7480 if (err)
7481 goto done;
7483 err = delete_histedit_refs(worktree, repo);
7484 if (err)
7485 goto done;
7487 err = get_fileindex_path(&fileindex_path, worktree);
7488 if (err)
7489 goto done;
7491 rfa.worktree = worktree;
7492 rfa.fileindex = fileindex;
7493 rfa.progress_cb = progress_cb;
7494 rfa.progress_arg = progress_arg;
7495 rfa.patch_cb = NULL;
7496 rfa.patch_arg = NULL;
7497 rfa.repo = repo;
7498 rfa.unlink_added_files = 0;
7499 err = worktree_status(worktree, "", fileindex, repo,
7500 revert_file, &rfa, NULL, NULL, 1, 0);
7501 if (err)
7502 goto sync;
7504 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7505 repo, progress_cb, progress_arg, NULL, NULL);
7506 sync:
7507 sync_err = sync_fileindex(fileindex, fileindex_path);
7508 if (sync_err && err == NULL)
7509 err = sync_err;
7510 done:
7511 got_ref_close(resolved);
7512 free(tree_id);
7513 free(fileindex_path);
7515 unlockerr = lock_worktree(worktree, LOCK_SH);
7516 if (unlockerr && err == NULL)
7517 err = unlockerr;
7518 return err;
7521 const struct got_error *
7522 got_worktree_histedit_complete(struct got_worktree *worktree,
7523 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7524 struct got_reference *edited_branch, struct got_repository *repo)
7526 const struct got_error *err, *unlockerr, *sync_err;
7527 struct got_object_id *new_head_commit_id = NULL;
7528 struct got_reference *resolved = NULL;
7529 char *fileindex_path = NULL;
7531 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7532 if (err)
7533 return err;
7535 err = got_ref_open(&resolved, repo,
7536 got_ref_get_symref_target(edited_branch), 0);
7537 if (err)
7538 goto done;
7540 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7541 resolved, new_head_commit_id, repo);
7542 if (err)
7543 goto done;
7545 err = got_ref_change_ref(resolved, new_head_commit_id);
7546 if (err)
7547 goto done;
7549 err = got_ref_write(resolved, repo);
7550 if (err)
7551 goto done;
7553 err = got_worktree_set_head_ref(worktree, resolved);
7554 if (err)
7555 goto done;
7557 err = delete_histedit_refs(worktree, repo);
7558 if (err)
7559 goto done;
7561 err = get_fileindex_path(&fileindex_path, worktree);
7562 if (err)
7563 goto done;
7564 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7565 sync_err = sync_fileindex(fileindex, fileindex_path);
7566 if (sync_err && err == NULL)
7567 err = sync_err;
7568 done:
7569 got_fileindex_free(fileindex);
7570 free(fileindex_path);
7571 free(new_head_commit_id);
7572 unlockerr = lock_worktree(worktree, LOCK_SH);
7573 if (unlockerr && err == NULL)
7574 err = unlockerr;
7575 return err;
7578 const struct got_error *
7579 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7580 struct got_object_id *commit_id, struct got_repository *repo)
7582 const struct got_error *err;
7583 char *commit_ref_name;
7585 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7586 if (err)
7587 return err;
7589 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7590 if (err)
7591 goto done;
7593 err = delete_ref(commit_ref_name, repo);
7594 done:
7595 free(commit_ref_name);
7596 return err;
7599 const struct got_error *
7600 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7601 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7602 struct got_worktree *worktree, const char *refname,
7603 struct got_repository *repo)
7605 const struct got_error *err = NULL;
7606 char *fileindex_path = NULL;
7607 struct check_rebase_ok_arg ok_arg;
7609 *fileindex = NULL;
7610 *branch_ref = NULL;
7611 *base_branch_ref = NULL;
7613 err = lock_worktree(worktree, LOCK_EX);
7614 if (err)
7615 return err;
7617 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7618 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7619 "cannot integrate a branch into itself; "
7620 "update -b or different branch name required");
7621 goto done;
7624 err = open_fileindex(fileindex, &fileindex_path, worktree);
7625 if (err)
7626 goto done;
7628 /* Preconditions are the same as for rebase. */
7629 ok_arg.worktree = worktree;
7630 ok_arg.repo = repo;
7631 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7632 &ok_arg);
7633 if (err)
7634 goto done;
7636 err = got_ref_open(branch_ref, repo, refname, 1);
7637 if (err)
7638 goto done;
7640 err = got_ref_open(base_branch_ref, repo,
7641 got_worktree_get_head_ref_name(worktree), 1);
7642 done:
7643 if (err) {
7644 if (*branch_ref) {
7645 got_ref_close(*branch_ref);
7646 *branch_ref = NULL;
7648 if (*base_branch_ref) {
7649 got_ref_close(*base_branch_ref);
7650 *base_branch_ref = NULL;
7652 if (*fileindex) {
7653 got_fileindex_free(*fileindex);
7654 *fileindex = NULL;
7656 lock_worktree(worktree, LOCK_SH);
7658 return err;
7661 const struct got_error *
7662 got_worktree_integrate_continue(struct got_worktree *worktree,
7663 struct got_fileindex *fileindex, struct got_repository *repo,
7664 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7665 got_worktree_checkout_cb progress_cb, void *progress_arg,
7666 got_cancel_cb cancel_cb, void *cancel_arg)
7668 const struct got_error *err = NULL, *sync_err, *unlockerr;
7669 char *fileindex_path = NULL;
7670 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7671 struct got_commit_object *commit = NULL;
7673 err = get_fileindex_path(&fileindex_path, worktree);
7674 if (err)
7675 goto done;
7677 err = got_ref_resolve(&commit_id, repo, branch_ref);
7678 if (err)
7679 goto done;
7681 err = got_object_open_as_commit(&commit, repo, commit_id);
7682 if (err)
7683 goto done;
7685 err = got_object_id_by_path(&tree_id, repo, commit,
7686 worktree->path_prefix);
7687 if (err)
7688 goto done;
7690 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7691 if (err)
7692 goto done;
7694 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7695 progress_cb, progress_arg, cancel_cb, cancel_arg);
7696 if (err)
7697 goto sync;
7699 err = got_ref_change_ref(base_branch_ref, commit_id);
7700 if (err)
7701 goto sync;
7703 err = got_ref_write(base_branch_ref, repo);
7704 if (err)
7705 goto sync;
7707 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7708 sync:
7709 sync_err = sync_fileindex(fileindex, fileindex_path);
7710 if (sync_err && err == NULL)
7711 err = sync_err;
7713 done:
7714 unlockerr = got_ref_unlock(branch_ref);
7715 if (unlockerr && err == NULL)
7716 err = unlockerr;
7717 got_ref_close(branch_ref);
7719 unlockerr = got_ref_unlock(base_branch_ref);
7720 if (unlockerr && err == NULL)
7721 err = unlockerr;
7722 got_ref_close(base_branch_ref);
7724 got_fileindex_free(fileindex);
7725 free(fileindex_path);
7726 free(tree_id);
7727 if (commit)
7728 got_object_commit_close(commit);
7730 unlockerr = lock_worktree(worktree, LOCK_SH);
7731 if (unlockerr && err == NULL)
7732 err = unlockerr;
7733 return err;
7736 const struct got_error *
7737 got_worktree_integrate_abort(struct got_worktree *worktree,
7738 struct got_fileindex *fileindex, struct got_repository *repo,
7739 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7741 const struct got_error *err = NULL, *unlockerr = NULL;
7743 got_fileindex_free(fileindex);
7745 err = lock_worktree(worktree, LOCK_SH);
7747 unlockerr = got_ref_unlock(branch_ref);
7748 if (unlockerr && err == NULL)
7749 err = unlockerr;
7750 got_ref_close(branch_ref);
7752 unlockerr = got_ref_unlock(base_branch_ref);
7753 if (unlockerr && err == NULL)
7754 err = unlockerr;
7755 got_ref_close(base_branch_ref);
7757 return err;
7760 const struct got_error *
7761 got_worktree_merge_postpone(struct got_worktree *worktree,
7762 struct got_fileindex *fileindex)
7764 const struct got_error *err, *sync_err;
7765 char *fileindex_path = NULL;
7767 err = get_fileindex_path(&fileindex_path, worktree);
7768 if (err)
7769 goto done;
7771 sync_err = sync_fileindex(fileindex, fileindex_path);
7773 err = lock_worktree(worktree, LOCK_SH);
7774 if (sync_err && err == NULL)
7775 err = sync_err;
7776 done:
7777 got_fileindex_free(fileindex);
7778 free(fileindex_path);
7779 return err;
7782 static const struct got_error *
7783 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7785 const struct got_error *err;
7786 char *branch_refname = NULL, *commit_refname = NULL;
7788 err = get_merge_branch_ref_name(&branch_refname, worktree);
7789 if (err)
7790 goto done;
7791 err = delete_ref(branch_refname, repo);
7792 if (err)
7793 goto done;
7795 err = get_merge_commit_ref_name(&commit_refname, worktree);
7796 if (err)
7797 goto done;
7798 err = delete_ref(commit_refname, repo);
7799 if (err)
7800 goto done;
7802 done:
7803 free(branch_refname);
7804 free(commit_refname);
7805 return err;
7808 struct merge_commit_msg_arg {
7809 struct got_worktree *worktree;
7810 const char *branch_name;
7813 static const struct got_error *
7814 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7815 const char *diff_path, char **logmsg, void *arg)
7817 struct merge_commit_msg_arg *a = arg;
7819 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7820 got_worktree_get_head_ref_name(a->worktree)) == -1)
7821 return got_error_from_errno("asprintf");
7823 return NULL;
7827 const struct got_error *
7828 got_worktree_merge_branch(struct got_worktree *worktree,
7829 struct got_fileindex *fileindex,
7830 struct got_object_id *yca_commit_id,
7831 struct got_object_id *branch_tip,
7832 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7833 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7835 const struct got_error *err;
7836 char *fileindex_path = NULL;
7838 err = get_fileindex_path(&fileindex_path, worktree);
7839 if (err)
7840 goto done;
7842 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7843 worktree);
7844 if (err)
7845 goto done;
7847 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7848 branch_tip, repo, progress_cb, progress_arg,
7849 cancel_cb, cancel_arg);
7850 done:
7851 free(fileindex_path);
7852 return err;
7855 const struct got_error *
7856 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7857 struct got_worktree *worktree, struct got_fileindex *fileindex,
7858 const char *author, const char *committer, int allow_bad_symlinks,
7859 struct got_object_id *branch_tip, const char *branch_name,
7860 struct got_repository *repo,
7861 got_worktree_status_cb status_cb, void *status_arg)
7864 const struct got_error *err = NULL, *sync_err;
7865 struct got_pathlist_head commitable_paths;
7866 struct collect_commitables_arg cc_arg;
7867 struct got_pathlist_entry *pe;
7868 struct got_reference *head_ref = NULL;
7869 struct got_object_id *head_commit_id = NULL;
7870 int have_staged_files = 0;
7871 struct merge_commit_msg_arg mcm_arg;
7872 char *fileindex_path = NULL;
7874 memset(&cc_arg, 0, sizeof(cc_arg));
7875 *new_commit_id = NULL;
7877 TAILQ_INIT(&commitable_paths);
7879 err = get_fileindex_path(&fileindex_path, worktree);
7880 if (err)
7881 goto done;
7883 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7884 if (err)
7885 goto done;
7887 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7888 if (err)
7889 goto done;
7891 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7892 &have_staged_files);
7893 if (err && err->code != GOT_ERR_CANCELLED)
7894 goto done;
7895 if (have_staged_files) {
7896 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7897 goto done;
7900 cc_arg.commitable_paths = &commitable_paths;
7901 cc_arg.worktree = worktree;
7902 cc_arg.fileindex = fileindex;
7903 cc_arg.repo = repo;
7904 cc_arg.have_staged_files = have_staged_files;
7905 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7906 err = worktree_status(worktree, "", fileindex, repo,
7907 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7908 if (err)
7909 goto done;
7911 if (TAILQ_EMPTY(&commitable_paths)) {
7912 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7913 "merge of %s cannot proceed", branch_name);
7914 goto done;
7917 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7918 struct got_commitable *ct = pe->data;
7919 const char *ct_path = ct->in_repo_path;
7921 while (ct_path[0] == '/')
7922 ct_path++;
7923 err = check_out_of_date(ct_path, ct->status,
7924 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7925 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7926 if (err)
7927 goto done;
7931 mcm_arg.worktree = worktree;
7932 mcm_arg.branch_name = branch_name;
7933 err = commit_worktree(new_commit_id, &commitable_paths,
7934 head_commit_id, branch_tip, worktree, author, committer, NULL,
7935 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7936 if (err)
7937 goto done;
7939 err = update_fileindex_after_commit(worktree, &commitable_paths,
7940 *new_commit_id, fileindex, have_staged_files);
7941 sync_err = sync_fileindex(fileindex, fileindex_path);
7942 if (sync_err && err == NULL)
7943 err = sync_err;
7944 done:
7945 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7946 struct got_commitable *ct = pe->data;
7948 free_commitable(ct);
7950 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7951 free(fileindex_path);
7952 return err;
7955 const struct got_error *
7956 got_worktree_merge_complete(struct got_worktree *worktree,
7957 struct got_fileindex *fileindex, struct got_repository *repo)
7959 const struct got_error *err, *unlockerr, *sync_err;
7960 char *fileindex_path = NULL;
7962 err = delete_merge_refs(worktree, repo);
7963 if (err)
7964 goto done;
7966 err = get_fileindex_path(&fileindex_path, worktree);
7967 if (err)
7968 goto done;
7969 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7970 sync_err = sync_fileindex(fileindex, fileindex_path);
7971 if (sync_err && err == NULL)
7972 err = sync_err;
7973 done:
7974 got_fileindex_free(fileindex);
7975 free(fileindex_path);
7976 unlockerr = lock_worktree(worktree, LOCK_SH);
7977 if (unlockerr && err == NULL)
7978 err = unlockerr;
7979 return err;
7982 const struct got_error *
7983 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7984 struct got_repository *repo)
7986 const struct got_error *err;
7987 char *branch_refname = NULL;
7988 struct got_reference *branch_ref = NULL;
7990 *in_progress = 0;
7992 err = get_merge_branch_ref_name(&branch_refname, worktree);
7993 if (err)
7994 return err;
7995 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7996 free(branch_refname);
7997 if (err) {
7998 if (err->code != GOT_ERR_NOT_REF)
7999 return err;
8000 } else
8001 *in_progress = 1;
8003 return NULL;
8006 const struct got_error *got_worktree_merge_prepare(
8007 struct got_fileindex **fileindex, struct got_worktree *worktree,
8008 struct got_reference *branch, struct got_repository *repo)
8010 const struct got_error *err = NULL;
8011 char *fileindex_path = NULL;
8012 char *branch_refname = NULL, *commit_refname = NULL;
8013 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8014 struct got_reference *commit_ref = NULL;
8015 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8016 struct check_rebase_ok_arg ok_arg;
8018 *fileindex = NULL;
8020 err = lock_worktree(worktree, LOCK_EX);
8021 if (err)
8022 return err;
8024 err = open_fileindex(fileindex, &fileindex_path, worktree);
8025 if (err)
8026 goto done;
8028 /* Preconditions are the same as for rebase. */
8029 ok_arg.worktree = worktree;
8030 ok_arg.repo = repo;
8031 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8032 &ok_arg);
8033 if (err)
8034 goto done;
8036 err = get_merge_branch_ref_name(&branch_refname, worktree);
8037 if (err)
8038 return err;
8040 err = get_merge_commit_ref_name(&commit_refname, worktree);
8041 if (err)
8042 return err;
8044 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8045 0);
8046 if (err)
8047 goto done;
8049 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8050 if (err)
8051 goto done;
8053 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8054 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8055 goto done;
8058 err = got_ref_resolve(&branch_tip, repo, branch);
8059 if (err)
8060 goto done;
8062 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8063 if (err)
8064 goto done;
8065 err = got_ref_write(branch_ref, repo);
8066 if (err)
8067 goto done;
8069 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8070 if (err)
8071 goto done;
8072 err = got_ref_write(commit_ref, repo);
8073 if (err)
8074 goto done;
8076 done:
8077 free(branch_refname);
8078 free(commit_refname);
8079 free(fileindex_path);
8080 if (branch_ref)
8081 got_ref_close(branch_ref);
8082 if (commit_ref)
8083 got_ref_close(commit_ref);
8084 if (wt_branch)
8085 got_ref_close(wt_branch);
8086 free(wt_branch_tip);
8087 if (err) {
8088 if (*fileindex) {
8089 got_fileindex_free(*fileindex);
8090 *fileindex = NULL;
8092 lock_worktree(worktree, LOCK_SH);
8094 return err;
8097 const struct got_error *
8098 got_worktree_merge_continue(char **branch_name,
8099 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8100 struct got_worktree *worktree, struct got_repository *repo)
8102 const struct got_error *err;
8103 char *commit_refname = NULL, *branch_refname = NULL;
8104 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8105 char *fileindex_path = NULL;
8106 int have_staged_files = 0;
8108 *branch_name = NULL;
8109 *branch_tip = NULL;
8110 *fileindex = NULL;
8112 err = lock_worktree(worktree, LOCK_EX);
8113 if (err)
8114 return err;
8116 err = open_fileindex(fileindex, &fileindex_path, worktree);
8117 if (err)
8118 goto done;
8120 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8121 &have_staged_files);
8122 if (err && err->code != GOT_ERR_CANCELLED)
8123 goto done;
8124 if (have_staged_files) {
8125 err = got_error(GOT_ERR_STAGED_PATHS);
8126 goto done;
8129 err = get_merge_branch_ref_name(&branch_refname, worktree);
8130 if (err)
8131 goto done;
8133 err = get_merge_commit_ref_name(&commit_refname, worktree);
8134 if (err)
8135 goto done;
8137 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8138 if (err)
8139 goto done;
8141 if (!got_ref_is_symbolic(branch_ref)) {
8142 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8143 "%s is not a symbolic reference",
8144 got_ref_get_name(branch_ref));
8145 goto done;
8147 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8148 if (*branch_name == NULL) {
8149 err = got_error_from_errno("strdup");
8150 goto done;
8153 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8154 if (err)
8155 goto done;
8157 err = got_ref_resolve(branch_tip, repo, commit_ref);
8158 if (err)
8159 goto done;
8160 done:
8161 free(commit_refname);
8162 free(branch_refname);
8163 free(fileindex_path);
8164 if (commit_ref)
8165 got_ref_close(commit_ref);
8166 if (branch_ref)
8167 got_ref_close(branch_ref);
8168 if (err) {
8169 if (*branch_name) {
8170 free(*branch_name);
8171 *branch_name = NULL;
8173 free(*branch_tip);
8174 *branch_tip = NULL;
8175 if (*fileindex) {
8176 got_fileindex_free(*fileindex);
8177 *fileindex = NULL;
8179 lock_worktree(worktree, LOCK_SH);
8181 return err;
8184 const struct got_error *
8185 got_worktree_merge_abort(struct got_worktree *worktree,
8186 struct got_fileindex *fileindex, struct got_repository *repo,
8187 got_worktree_checkout_cb progress_cb, void *progress_arg)
8189 const struct got_error *err, *unlockerr, *sync_err;
8190 struct got_object_id *commit_id = NULL;
8191 struct got_commit_object *commit = NULL;
8192 char *fileindex_path = NULL;
8193 struct revert_file_args rfa;
8194 struct got_object_id *tree_id = NULL;
8196 err = got_object_open_as_commit(&commit, repo,
8197 worktree->base_commit_id);
8198 if (err)
8199 goto done;
8201 err = got_object_id_by_path(&tree_id, repo, commit,
8202 worktree->path_prefix);
8203 if (err)
8204 goto done;
8206 err = delete_merge_refs(worktree, repo);
8207 if (err)
8208 goto done;
8210 err = get_fileindex_path(&fileindex_path, worktree);
8211 if (err)
8212 goto done;
8214 rfa.worktree = worktree;
8215 rfa.fileindex = fileindex;
8216 rfa.progress_cb = progress_cb;
8217 rfa.progress_arg = progress_arg;
8218 rfa.patch_cb = NULL;
8219 rfa.patch_arg = NULL;
8220 rfa.repo = repo;
8221 rfa.unlink_added_files = 1;
8222 err = worktree_status(worktree, "", fileindex, repo,
8223 revert_file, &rfa, NULL, NULL, 1, 0);
8224 if (err)
8225 goto sync;
8227 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8228 repo, progress_cb, progress_arg, NULL, NULL);
8229 sync:
8230 sync_err = sync_fileindex(fileindex, fileindex_path);
8231 if (sync_err && err == NULL)
8232 err = sync_err;
8233 done:
8234 free(tree_id);
8235 free(commit_id);
8236 if (commit)
8237 got_object_commit_close(commit);
8238 if (fileindex)
8239 got_fileindex_free(fileindex);
8240 free(fileindex_path);
8242 unlockerr = lock_worktree(worktree, LOCK_SH);
8243 if (unlockerr && err == NULL)
8244 err = unlockerr;
8245 return err;
8248 struct check_stage_ok_arg {
8249 struct got_object_id *head_commit_id;
8250 struct got_worktree *worktree;
8251 struct got_fileindex *fileindex;
8252 struct got_repository *repo;
8253 int have_changes;
8256 static const struct got_error *
8257 check_stage_ok(void *arg, unsigned char status,
8258 unsigned char staged_status, const char *relpath,
8259 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8260 struct got_object_id *commit_id, int dirfd, const char *de_name)
8262 struct check_stage_ok_arg *a = arg;
8263 const struct got_error *err = NULL;
8264 struct got_fileindex_entry *ie;
8265 struct got_object_id base_commit_id;
8266 struct got_object_id *base_commit_idp = NULL;
8267 char *in_repo_path = NULL, *p;
8269 if (status == GOT_STATUS_UNVERSIONED ||
8270 status == GOT_STATUS_NO_CHANGE)
8271 return NULL;
8272 if (status == GOT_STATUS_NONEXISTENT)
8273 return got_error_set_errno(ENOENT, relpath);
8275 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8276 if (ie == NULL)
8277 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8279 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8280 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8281 relpath) == -1)
8282 return got_error_from_errno("asprintf");
8284 if (got_fileindex_entry_has_commit(ie)) {
8285 memcpy(base_commit_id.sha1, ie->commit_sha1,
8286 SHA1_DIGEST_LENGTH);
8287 base_commit_idp = &base_commit_id;
8290 if (status == GOT_STATUS_CONFLICT) {
8291 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8292 goto done;
8293 } else if (status != GOT_STATUS_ADD &&
8294 status != GOT_STATUS_MODIFY &&
8295 status != GOT_STATUS_DELETE) {
8296 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8297 goto done;
8300 a->have_changes = 1;
8302 p = in_repo_path;
8303 while (p[0] == '/')
8304 p++;
8305 err = check_out_of_date(p, status, staged_status,
8306 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8307 GOT_ERR_STAGE_OUT_OF_DATE);
8308 done:
8309 free(in_repo_path);
8310 return err;
8313 struct stage_path_arg {
8314 struct got_worktree *worktree;
8315 struct got_fileindex *fileindex;
8316 struct got_repository *repo;
8317 got_worktree_status_cb status_cb;
8318 void *status_arg;
8319 got_worktree_patch_cb patch_cb;
8320 void *patch_arg;
8321 int staged_something;
8322 int allow_bad_symlinks;
8325 static const struct got_error *
8326 stage_path(void *arg, unsigned char status,
8327 unsigned char staged_status, const char *relpath,
8328 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8329 struct got_object_id *commit_id, int dirfd, const char *de_name)
8331 struct stage_path_arg *a = arg;
8332 const struct got_error *err = NULL;
8333 struct got_fileindex_entry *ie;
8334 char *ondisk_path = NULL, *path_content = NULL;
8335 uint32_t stage;
8336 struct got_object_id *new_staged_blob_id = NULL;
8337 struct stat sb;
8339 if (status == GOT_STATUS_UNVERSIONED)
8340 return NULL;
8342 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8343 if (ie == NULL)
8344 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8346 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8347 relpath)== -1)
8348 return got_error_from_errno("asprintf");
8350 switch (status) {
8351 case GOT_STATUS_ADD:
8352 case GOT_STATUS_MODIFY:
8353 /* XXX could sb.st_mode be passed in by our caller? */
8354 if (lstat(ondisk_path, &sb) == -1) {
8355 err = got_error_from_errno2("lstat", ondisk_path);
8356 break;
8358 if (a->patch_cb) {
8359 if (status == GOT_STATUS_ADD) {
8360 int choice = GOT_PATCH_CHOICE_NONE;
8361 err = (*a->patch_cb)(&choice, a->patch_arg,
8362 status, ie->path, NULL, 1, 1);
8363 if (err)
8364 break;
8365 if (choice != GOT_PATCH_CHOICE_YES)
8366 break;
8367 } else {
8368 err = create_patched_content(&path_content, 0,
8369 staged_blob_id ? staged_blob_id : blob_id,
8370 ondisk_path, dirfd, de_name, ie->path,
8371 a->repo, a->patch_cb, a->patch_arg);
8372 if (err || path_content == NULL)
8373 break;
8376 err = got_object_blob_create(&new_staged_blob_id,
8377 path_content ? path_content : ondisk_path, a->repo);
8378 if (err)
8379 break;
8380 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8381 SHA1_DIGEST_LENGTH);
8382 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8383 stage = GOT_FILEIDX_STAGE_ADD;
8384 else
8385 stage = GOT_FILEIDX_STAGE_MODIFY;
8386 got_fileindex_entry_stage_set(ie, stage);
8387 if (S_ISLNK(sb.st_mode)) {
8388 int is_bad_symlink = 0;
8389 if (!a->allow_bad_symlinks) {
8390 char target_path[PATH_MAX];
8391 ssize_t target_len;
8392 target_len = readlink(ondisk_path, target_path,
8393 sizeof(target_path));
8394 if (target_len == -1) {
8395 err = got_error_from_errno2("readlink",
8396 ondisk_path);
8397 break;
8399 err = is_bad_symlink_target(&is_bad_symlink,
8400 target_path, target_len, ondisk_path,
8401 a->worktree->root_path);
8402 if (err)
8403 break;
8404 if (is_bad_symlink) {
8405 err = got_error_path(ondisk_path,
8406 GOT_ERR_BAD_SYMLINK);
8407 break;
8410 if (is_bad_symlink)
8411 got_fileindex_entry_staged_filetype_set(ie,
8412 GOT_FILEIDX_MODE_BAD_SYMLINK);
8413 else
8414 got_fileindex_entry_staged_filetype_set(ie,
8415 GOT_FILEIDX_MODE_SYMLINK);
8416 } else {
8417 got_fileindex_entry_staged_filetype_set(ie,
8418 GOT_FILEIDX_MODE_REGULAR_FILE);
8420 a->staged_something = 1;
8421 if (a->status_cb == NULL)
8422 break;
8423 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8424 get_staged_status(ie), relpath, blob_id,
8425 new_staged_blob_id, NULL, dirfd, de_name);
8426 if (err)
8427 break;
8429 * When staging the reverse of the staged diff,
8430 * implicitly unstage the file.
8432 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8433 sizeof(ie->blob_sha1)) == 0) {
8434 got_fileindex_entry_stage_set(ie,
8435 GOT_FILEIDX_STAGE_NONE);
8437 break;
8438 case GOT_STATUS_DELETE:
8439 if (staged_status == GOT_STATUS_DELETE)
8440 break;
8441 if (a->patch_cb) {
8442 int choice = GOT_PATCH_CHOICE_NONE;
8443 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8444 ie->path, NULL, 1, 1);
8445 if (err)
8446 break;
8447 if (choice == GOT_PATCH_CHOICE_NO)
8448 break;
8449 if (choice != GOT_PATCH_CHOICE_YES) {
8450 err = got_error(GOT_ERR_PATCH_CHOICE);
8451 break;
8454 stage = GOT_FILEIDX_STAGE_DELETE;
8455 got_fileindex_entry_stage_set(ie, stage);
8456 a->staged_something = 1;
8457 if (a->status_cb == NULL)
8458 break;
8459 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8460 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8461 de_name);
8462 break;
8463 case GOT_STATUS_NO_CHANGE:
8464 break;
8465 case GOT_STATUS_CONFLICT:
8466 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8467 break;
8468 case GOT_STATUS_NONEXISTENT:
8469 err = got_error_set_errno(ENOENT, relpath);
8470 break;
8471 default:
8472 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8473 break;
8476 if (path_content && unlink(path_content) == -1 && err == NULL)
8477 err = got_error_from_errno2("unlink", path_content);
8478 free(path_content);
8479 free(ondisk_path);
8480 free(new_staged_blob_id);
8481 return err;
8484 const struct got_error *
8485 got_worktree_stage(struct got_worktree *worktree,
8486 struct got_pathlist_head *paths,
8487 got_worktree_status_cb status_cb, void *status_arg,
8488 got_worktree_patch_cb patch_cb, void *patch_arg,
8489 int allow_bad_symlinks, struct got_repository *repo)
8491 const struct got_error *err = NULL, *sync_err, *unlockerr;
8492 struct got_pathlist_entry *pe;
8493 struct got_fileindex *fileindex = NULL;
8494 char *fileindex_path = NULL;
8495 struct got_reference *head_ref = NULL;
8496 struct got_object_id *head_commit_id = NULL;
8497 struct check_stage_ok_arg oka;
8498 struct stage_path_arg spa;
8500 err = lock_worktree(worktree, LOCK_EX);
8501 if (err)
8502 return err;
8504 err = got_ref_open(&head_ref, repo,
8505 got_worktree_get_head_ref_name(worktree), 0);
8506 if (err)
8507 goto done;
8508 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8509 if (err)
8510 goto done;
8511 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8512 if (err)
8513 goto done;
8515 /* Check pre-conditions before staging anything. */
8516 oka.head_commit_id = head_commit_id;
8517 oka.worktree = worktree;
8518 oka.fileindex = fileindex;
8519 oka.repo = repo;
8520 oka.have_changes = 0;
8521 TAILQ_FOREACH(pe, paths, entry) {
8522 err = worktree_status(worktree, pe->path, fileindex, repo,
8523 check_stage_ok, &oka, NULL, NULL, 1, 0);
8524 if (err)
8525 goto done;
8527 if (!oka.have_changes) {
8528 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8529 goto done;
8532 spa.worktree = worktree;
8533 spa.fileindex = fileindex;
8534 spa.repo = repo;
8535 spa.patch_cb = patch_cb;
8536 spa.patch_arg = patch_arg;
8537 spa.status_cb = status_cb;
8538 spa.status_arg = status_arg;
8539 spa.staged_something = 0;
8540 spa.allow_bad_symlinks = allow_bad_symlinks;
8541 TAILQ_FOREACH(pe, paths, entry) {
8542 err = worktree_status(worktree, pe->path, fileindex, repo,
8543 stage_path, &spa, NULL, NULL, 1, 0);
8544 if (err)
8545 goto done;
8547 if (!spa.staged_something) {
8548 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8549 goto done;
8552 sync_err = sync_fileindex(fileindex, fileindex_path);
8553 if (sync_err && err == NULL)
8554 err = sync_err;
8555 done:
8556 if (head_ref)
8557 got_ref_close(head_ref);
8558 free(head_commit_id);
8559 free(fileindex_path);
8560 if (fileindex)
8561 got_fileindex_free(fileindex);
8562 unlockerr = lock_worktree(worktree, LOCK_SH);
8563 if (unlockerr && err == NULL)
8564 err = unlockerr;
8565 return err;
8568 struct unstage_path_arg {
8569 struct got_worktree *worktree;
8570 struct got_fileindex *fileindex;
8571 struct got_repository *repo;
8572 got_worktree_checkout_cb progress_cb;
8573 void *progress_arg;
8574 got_worktree_patch_cb patch_cb;
8575 void *patch_arg;
8578 static const struct got_error *
8579 create_unstaged_content(char **path_unstaged_content,
8580 char **path_new_staged_content, struct got_object_id *blob_id,
8581 struct got_object_id *staged_blob_id, const char *relpath,
8582 struct got_repository *repo,
8583 got_worktree_patch_cb patch_cb, void *patch_arg)
8585 const struct got_error *err, *free_err;
8586 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8587 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8588 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8589 struct got_diffreg_result *diffreg_result = NULL;
8590 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8591 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8592 int fd1 = -1, fd2 = -1;
8594 *path_unstaged_content = NULL;
8595 *path_new_staged_content = NULL;
8597 err = got_object_id_str(&label1, blob_id);
8598 if (err)
8599 return err;
8601 fd1 = got_opentempfd();
8602 if (fd1 == -1) {
8603 err = got_error_from_errno("got_opentempfd");
8604 goto done;
8606 fd2 = got_opentempfd();
8607 if (fd2 == -1) {
8608 err = got_error_from_errno("got_opentempfd");
8609 goto done;
8612 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8613 if (err)
8614 goto done;
8616 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8617 if (err)
8618 goto done;
8620 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8621 if (err)
8622 goto done;
8624 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8625 fd2);
8626 if (err)
8627 goto done;
8629 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8630 if (err)
8631 goto done;
8633 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8634 if (err)
8635 goto done;
8637 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8638 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8639 if (err)
8640 goto done;
8642 err = got_opentemp_named(path_unstaged_content, &outfile,
8643 "got-unstaged-content", "");
8644 if (err)
8645 goto done;
8646 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8647 "got-new-staged-content", "");
8648 if (err)
8649 goto done;
8651 if (fseek(f1, 0L, SEEK_SET) == -1) {
8652 err = got_ferror(f1, GOT_ERR_IO);
8653 goto done;
8655 if (fseek(f2, 0L, SEEK_SET) == -1) {
8656 err = got_ferror(f2, GOT_ERR_IO);
8657 goto done;
8659 /* Count the number of actual changes in the diff result. */
8660 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8661 struct diff_chunk_context cc = {};
8662 diff_chunk_context_load_change(&cc, &nchunks_used,
8663 diffreg_result->result, n, 0);
8664 nchanges++;
8666 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8667 int choice;
8668 err = apply_or_reject_change(&choice, &nchunks_used,
8669 diffreg_result->result, n, relpath, f1, f2,
8670 &line_cur1, &line_cur2,
8671 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8672 if (err)
8673 goto done;
8674 if (choice == GOT_PATCH_CHOICE_YES)
8675 have_content = 1;
8676 else
8677 have_rejected_content = 1;
8678 if (choice == GOT_PATCH_CHOICE_QUIT)
8679 break;
8681 if (have_content || have_rejected_content)
8682 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8683 outfile, rejectfile);
8684 done:
8685 free(label1);
8686 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8687 err = got_error_from_errno("close");
8688 if (blob)
8689 got_object_blob_close(blob);
8690 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8691 err = got_error_from_errno("close");
8692 if (staged_blob)
8693 got_object_blob_close(staged_blob);
8694 free_err = got_diffreg_result_free(diffreg_result);
8695 if (free_err && err == NULL)
8696 err = free_err;
8697 if (f1 && fclose(f1) == EOF && err == NULL)
8698 err = got_error_from_errno2("fclose", path1);
8699 if (f2 && fclose(f2) == EOF && err == NULL)
8700 err = got_error_from_errno2("fclose", path2);
8701 if (outfile && fclose(outfile) == EOF && err == NULL)
8702 err = got_error_from_errno2("fclose", *path_unstaged_content);
8703 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8704 err = got_error_from_errno2("fclose", *path_new_staged_content);
8705 if (path1 && unlink(path1) == -1 && err == NULL)
8706 err = got_error_from_errno2("unlink", path1);
8707 if (path2 && unlink(path2) == -1 && err == NULL)
8708 err = got_error_from_errno2("unlink", path2);
8709 if (err || !have_content) {
8710 if (*path_unstaged_content &&
8711 unlink(*path_unstaged_content) == -1 && err == NULL)
8712 err = got_error_from_errno2("unlink",
8713 *path_unstaged_content);
8714 free(*path_unstaged_content);
8715 *path_unstaged_content = NULL;
8717 if (err || !have_content || !have_rejected_content) {
8718 if (*path_new_staged_content &&
8719 unlink(*path_new_staged_content) == -1 && err == NULL)
8720 err = got_error_from_errno2("unlink",
8721 *path_new_staged_content);
8722 free(*path_new_staged_content);
8723 *path_new_staged_content = NULL;
8725 free(path1);
8726 free(path2);
8727 return err;
8730 static const struct got_error *
8731 unstage_hunks(struct got_object_id *staged_blob_id,
8732 struct got_blob_object *blob_base,
8733 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8734 const char *ondisk_path, const char *label_orig,
8735 struct got_worktree *worktree, struct got_repository *repo,
8736 got_worktree_patch_cb patch_cb, void *patch_arg,
8737 got_worktree_checkout_cb progress_cb, void *progress_arg)
8739 const struct got_error *err = NULL;
8740 char *path_unstaged_content = NULL;
8741 char *path_new_staged_content = NULL;
8742 char *parent = NULL, *base_path = NULL;
8743 char *blob_base_path = NULL;
8744 struct got_object_id *new_staged_blob_id = NULL;
8745 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8746 struct stat sb;
8748 err = create_unstaged_content(&path_unstaged_content,
8749 &path_new_staged_content, blob_id, staged_blob_id,
8750 ie->path, repo, patch_cb, patch_arg);
8751 if (err)
8752 return err;
8754 if (path_unstaged_content == NULL)
8755 return NULL;
8757 if (path_new_staged_content) {
8758 err = got_object_blob_create(&new_staged_blob_id,
8759 path_new_staged_content, repo);
8760 if (err)
8761 goto done;
8764 f = fopen(path_unstaged_content, "re");
8765 if (f == NULL) {
8766 err = got_error_from_errno2("fopen",
8767 path_unstaged_content);
8768 goto done;
8770 if (fstat(fileno(f), &sb) == -1) {
8771 err = got_error_from_errno2("fstat", path_unstaged_content);
8772 goto done;
8774 if (got_fileindex_entry_staged_filetype_get(ie) ==
8775 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8776 char link_target[PATH_MAX];
8777 size_t r;
8778 r = fread(link_target, 1, sizeof(link_target), f);
8779 if (r == 0 && ferror(f)) {
8780 err = got_error_from_errno("fread");
8781 goto done;
8783 if (r >= sizeof(link_target)) { /* should not happen */
8784 err = got_error(GOT_ERR_NO_SPACE);
8785 goto done;
8787 link_target[r] = '\0';
8788 err = merge_symlink(worktree, blob_base,
8789 ondisk_path, ie->path, label_orig, link_target,
8790 worktree->base_commit_id, repo, progress_cb,
8791 progress_arg);
8792 } else {
8793 int local_changes_subsumed;
8795 err = got_path_dirname(&parent, ondisk_path);
8796 if (err)
8797 return err;
8799 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8800 parent) == -1) {
8801 err = got_error_from_errno("asprintf");
8802 base_path = NULL;
8803 goto done;
8806 err = got_opentemp_named(&blob_base_path, &f_base,
8807 base_path, "");
8808 if (err)
8809 goto done;
8810 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8811 blob_base);
8812 if (err)
8813 goto done;
8816 * In order the run a 3-way merge with a symlink we copy the symlink's
8817 * target path into a temporary file and use that file with diff3.
8819 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8820 err = dump_symlink_target_path_to_file(&f_deriv2,
8821 ondisk_path);
8822 if (err)
8823 goto done;
8824 } else {
8825 int fd;
8826 fd = open(ondisk_path,
8827 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8828 if (fd == -1) {
8829 err = got_error_from_errno2("open", ondisk_path);
8830 goto done;
8832 f_deriv2 = fdopen(fd, "r");
8833 if (f_deriv2 == NULL) {
8834 err = got_error_from_errno2("fdopen", ondisk_path);
8835 close(fd);
8836 goto done;
8840 err = merge_file(&local_changes_subsumed, worktree,
8841 f_base, f, f_deriv2, ondisk_path, ie->path,
8842 got_fileindex_perms_to_st(ie),
8843 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8844 repo, progress_cb, progress_arg);
8846 if (err)
8847 goto done;
8849 if (new_staged_blob_id) {
8850 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8851 SHA1_DIGEST_LENGTH);
8852 } else {
8853 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8854 got_fileindex_entry_staged_filetype_set(ie, 0);
8856 done:
8857 free(new_staged_blob_id);
8858 if (path_unstaged_content &&
8859 unlink(path_unstaged_content) == -1 && err == NULL)
8860 err = got_error_from_errno2("unlink", path_unstaged_content);
8861 if (path_new_staged_content &&
8862 unlink(path_new_staged_content) == -1 && err == NULL)
8863 err = got_error_from_errno2("unlink", path_new_staged_content);
8864 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8865 err = got_error_from_errno2("unlink", blob_base_path);
8866 if (f_base && fclose(f_base) == EOF && err == NULL)
8867 err = got_error_from_errno2("fclose", path_unstaged_content);
8868 if (f && fclose(f) == EOF && err == NULL)
8869 err = got_error_from_errno2("fclose", path_unstaged_content);
8870 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8871 err = got_error_from_errno2("fclose", ondisk_path);
8872 free(path_unstaged_content);
8873 free(path_new_staged_content);
8874 free(blob_base_path);
8875 free(parent);
8876 free(base_path);
8877 return err;
8880 static const struct got_error *
8881 unstage_path(void *arg, unsigned char status,
8882 unsigned char staged_status, const char *relpath,
8883 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8884 struct got_object_id *commit_id, int dirfd, const char *de_name)
8886 const struct got_error *err = NULL;
8887 struct unstage_path_arg *a = arg;
8888 struct got_fileindex_entry *ie;
8889 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8890 char *ondisk_path = NULL;
8891 char *id_str = NULL, *label_orig = NULL;
8892 int local_changes_subsumed;
8893 struct stat sb;
8894 int fd1 = -1, fd2 = -1;
8896 if (staged_status != GOT_STATUS_ADD &&
8897 staged_status != GOT_STATUS_MODIFY &&
8898 staged_status != GOT_STATUS_DELETE)
8899 return NULL;
8901 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8902 if (ie == NULL)
8903 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8905 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8906 == -1)
8907 return got_error_from_errno("asprintf");
8909 err = got_object_id_str(&id_str,
8910 commit_id ? commit_id : a->worktree->base_commit_id);
8911 if (err)
8912 goto done;
8913 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8914 id_str) == -1) {
8915 err = got_error_from_errno("asprintf");
8916 goto done;
8919 fd1 = got_opentempfd();
8920 if (fd1 == -1) {
8921 err = got_error_from_errno("got_opentempfd");
8922 goto done;
8924 fd2 = got_opentempfd();
8925 if (fd2 == -1) {
8926 err = got_error_from_errno("got_opentempfd");
8927 goto done;
8930 switch (staged_status) {
8931 case GOT_STATUS_MODIFY:
8932 err = got_object_open_as_blob(&blob_base, a->repo,
8933 blob_id, 8192, fd1);
8934 if (err)
8935 break;
8936 /* fall through */
8937 case GOT_STATUS_ADD:
8938 if (a->patch_cb) {
8939 if (staged_status == GOT_STATUS_ADD) {
8940 int choice = GOT_PATCH_CHOICE_NONE;
8941 err = (*a->patch_cb)(&choice, a->patch_arg,
8942 staged_status, ie->path, NULL, 1, 1);
8943 if (err)
8944 break;
8945 if (choice != GOT_PATCH_CHOICE_YES)
8946 break;
8947 } else {
8948 err = unstage_hunks(staged_blob_id,
8949 blob_base, blob_id, ie, ondisk_path,
8950 label_orig, a->worktree, a->repo,
8951 a->patch_cb, a->patch_arg,
8952 a->progress_cb, a->progress_arg);
8953 break; /* Done with this file. */
8956 err = got_object_open_as_blob(&blob_staged, a->repo,
8957 staged_blob_id, 8192, fd2);
8958 if (err)
8959 break;
8960 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8961 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8962 case GOT_FILEIDX_MODE_REGULAR_FILE:
8963 err = merge_blob(&local_changes_subsumed, a->worktree,
8964 blob_base, ondisk_path, relpath,
8965 got_fileindex_perms_to_st(ie), label_orig,
8966 blob_staged, commit_id ? commit_id :
8967 a->worktree->base_commit_id, a->repo,
8968 a->progress_cb, a->progress_arg);
8969 break;
8970 case GOT_FILEIDX_MODE_SYMLINK:
8971 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8972 char *staged_target;
8973 err = got_object_blob_read_to_str(
8974 &staged_target, blob_staged);
8975 if (err)
8976 goto done;
8977 err = merge_symlink(a->worktree, blob_base,
8978 ondisk_path, relpath, label_orig,
8979 staged_target, commit_id ? commit_id :
8980 a->worktree->base_commit_id,
8981 a->repo, a->progress_cb, a->progress_arg);
8982 free(staged_target);
8983 } else {
8984 err = merge_blob(&local_changes_subsumed,
8985 a->worktree, blob_base, ondisk_path,
8986 relpath, got_fileindex_perms_to_st(ie),
8987 label_orig, blob_staged,
8988 commit_id ? commit_id :
8989 a->worktree->base_commit_id, a->repo,
8990 a->progress_cb, a->progress_arg);
8992 break;
8993 default:
8994 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8995 break;
8997 if (err == NULL) {
8998 got_fileindex_entry_stage_set(ie,
8999 GOT_FILEIDX_STAGE_NONE);
9000 got_fileindex_entry_staged_filetype_set(ie, 0);
9002 break;
9003 case GOT_STATUS_DELETE:
9004 if (a->patch_cb) {
9005 int choice = GOT_PATCH_CHOICE_NONE;
9006 err = (*a->patch_cb)(&choice, a->patch_arg,
9007 staged_status, ie->path, NULL, 1, 1);
9008 if (err)
9009 break;
9010 if (choice == GOT_PATCH_CHOICE_NO)
9011 break;
9012 if (choice != GOT_PATCH_CHOICE_YES) {
9013 err = got_error(GOT_ERR_PATCH_CHOICE);
9014 break;
9017 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9018 got_fileindex_entry_staged_filetype_set(ie, 0);
9019 err = get_file_status(&status, &sb, ie, ondisk_path,
9020 dirfd, de_name, a->repo);
9021 if (err)
9022 break;
9023 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9024 break;
9026 done:
9027 free(ondisk_path);
9028 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9029 err = got_error_from_errno("close");
9030 if (blob_base)
9031 got_object_blob_close(blob_base);
9032 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9033 err = got_error_from_errno("close");
9034 if (blob_staged)
9035 got_object_blob_close(blob_staged);
9036 free(id_str);
9037 free(label_orig);
9038 return err;
9041 const struct got_error *
9042 got_worktree_unstage(struct got_worktree *worktree,
9043 struct got_pathlist_head *paths,
9044 got_worktree_checkout_cb progress_cb, void *progress_arg,
9045 got_worktree_patch_cb patch_cb, void *patch_arg,
9046 struct got_repository *repo)
9048 const struct got_error *err = NULL, *sync_err, *unlockerr;
9049 struct got_pathlist_entry *pe;
9050 struct got_fileindex *fileindex = NULL;
9051 char *fileindex_path = NULL;
9052 struct unstage_path_arg upa;
9054 err = lock_worktree(worktree, LOCK_EX);
9055 if (err)
9056 return err;
9058 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9059 if (err)
9060 goto done;
9062 upa.worktree = worktree;
9063 upa.fileindex = fileindex;
9064 upa.repo = repo;
9065 upa.progress_cb = progress_cb;
9066 upa.progress_arg = progress_arg;
9067 upa.patch_cb = patch_cb;
9068 upa.patch_arg = patch_arg;
9069 TAILQ_FOREACH(pe, paths, entry) {
9070 err = worktree_status(worktree, pe->path, fileindex, repo,
9071 unstage_path, &upa, NULL, NULL, 1, 0);
9072 if (err)
9073 goto done;
9076 sync_err = sync_fileindex(fileindex, fileindex_path);
9077 if (sync_err && err == NULL)
9078 err = sync_err;
9079 done:
9080 free(fileindex_path);
9081 if (fileindex)
9082 got_fileindex_free(fileindex);
9083 unlockerr = lock_worktree(worktree, LOCK_SH);
9084 if (unlockerr && err == NULL)
9085 err = unlockerr;
9086 return err;
9089 struct report_file_info_arg {
9090 struct got_worktree *worktree;
9091 got_worktree_path_info_cb info_cb;
9092 void *info_arg;
9093 struct got_pathlist_head *paths;
9094 got_cancel_cb cancel_cb;
9095 void *cancel_arg;
9098 static const struct got_error *
9099 report_file_info(void *arg, struct got_fileindex_entry *ie)
9101 struct report_file_info_arg *a = arg;
9102 struct got_pathlist_entry *pe;
9103 struct got_object_id blob_id, staged_blob_id, commit_id;
9104 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9105 struct got_object_id *commit_idp = NULL;
9106 int stage;
9108 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9109 return got_error(GOT_ERR_CANCELLED);
9111 TAILQ_FOREACH(pe, a->paths, entry) {
9112 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9113 got_path_is_child(ie->path, pe->path, pe->path_len))
9114 break;
9116 if (pe == NULL) /* not found */
9117 return NULL;
9119 if (got_fileindex_entry_has_blob(ie)) {
9120 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
9121 blob_idp = &blob_id;
9123 stage = got_fileindex_entry_stage_get(ie);
9124 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9125 stage == GOT_FILEIDX_STAGE_ADD) {
9126 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
9127 SHA1_DIGEST_LENGTH);
9128 staged_blob_idp = &staged_blob_id;
9131 if (got_fileindex_entry_has_commit(ie)) {
9132 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
9133 commit_idp = &commit_id;
9136 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9137 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9140 const struct got_error *
9141 got_worktree_path_info(struct got_worktree *worktree,
9142 struct got_pathlist_head *paths,
9143 got_worktree_path_info_cb info_cb, void *info_arg,
9144 got_cancel_cb cancel_cb, void *cancel_arg)
9147 const struct got_error *err = NULL, *unlockerr;
9148 struct got_fileindex *fileindex = NULL;
9149 char *fileindex_path = NULL;
9150 struct report_file_info_arg arg;
9152 err = lock_worktree(worktree, LOCK_SH);
9153 if (err)
9154 return err;
9156 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9157 if (err)
9158 goto done;
9160 arg.worktree = worktree;
9161 arg.info_cb = info_cb;
9162 arg.info_arg = info_arg;
9163 arg.paths = paths;
9164 arg.cancel_cb = cancel_cb;
9165 arg.cancel_arg = cancel_arg;
9166 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9167 &arg);
9168 done:
9169 free(fileindex_path);
9170 if (fileindex)
9171 got_fileindex_free(fileindex);
9172 unlockerr = lock_worktree(worktree, LOCK_UN);
9173 if (unlockerr && err == NULL)
9174 err = unlockerr;
9175 return err;
9178 static const struct got_error *
9179 patch_check_path(const char *p, char **path, unsigned char *status,
9180 unsigned char *staged_status, struct got_fileindex *fileindex,
9181 struct got_worktree *worktree, struct got_repository *repo)
9183 const struct got_error *err;
9184 struct got_fileindex_entry *ie;
9185 struct stat sb;
9186 char *ondisk_path = NULL;
9188 err = got_worktree_resolve_path(path, worktree, p);
9189 if (err)
9190 return err;
9192 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9193 *path[0] ? "/" : "", *path) == -1)
9194 return got_error_from_errno("asprintf");
9196 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9197 if (ie) {
9198 *staged_status = get_staged_status(ie);
9199 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9200 repo);
9201 if (err)
9202 goto done;
9203 } else {
9204 *staged_status = GOT_STATUS_NO_CHANGE;
9205 *status = GOT_STATUS_UNVERSIONED;
9206 if (lstat(ondisk_path, &sb) == -1) {
9207 if (errno != ENOENT) {
9208 err = got_error_from_errno2("lstat",
9209 ondisk_path);
9210 goto done;
9212 *status = GOT_STATUS_NONEXISTENT;
9216 done:
9217 free(ondisk_path);
9218 return err;
9221 static const struct got_error *
9222 patch_can_rm(const char *path, unsigned char status,
9223 unsigned char staged_status)
9225 if (status == GOT_STATUS_NONEXISTENT)
9226 return got_error_set_errno(ENOENT, path);
9227 if (status != GOT_STATUS_NO_CHANGE &&
9228 status != GOT_STATUS_ADD &&
9229 status != GOT_STATUS_MODIFY &&
9230 status != GOT_STATUS_MODE_CHANGE)
9231 return got_error_path(path, GOT_ERR_FILE_STATUS);
9232 if (staged_status == GOT_STATUS_DELETE)
9233 return got_error_path(path, GOT_ERR_FILE_STATUS);
9234 return NULL;
9237 static const struct got_error *
9238 patch_can_add(const char *path, unsigned char status)
9240 if (status != GOT_STATUS_NONEXISTENT)
9241 return got_error_path(path, GOT_ERR_FILE_STATUS);
9242 return NULL;
9245 static const struct got_error *
9246 patch_can_edit(const char *path, unsigned char status,
9247 unsigned char staged_status)
9249 if (status == GOT_STATUS_NONEXISTENT)
9250 return got_error_set_errno(ENOENT, path);
9251 if (status != GOT_STATUS_NO_CHANGE &&
9252 status != GOT_STATUS_ADD &&
9253 status != GOT_STATUS_MODIFY)
9254 return got_error_path(path, GOT_ERR_FILE_STATUS);
9255 if (staged_status == GOT_STATUS_DELETE)
9256 return got_error_path(path, GOT_ERR_FILE_STATUS);
9257 return NULL;
9260 const struct got_error *
9261 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9262 char **fileindex_path, struct got_worktree *worktree)
9264 return open_fileindex(fileindex, fileindex_path, worktree);
9267 const struct got_error *
9268 got_worktree_patch_check_path(const char *old, const char *new,
9269 char **oldpath, char **newpath, struct got_worktree *worktree,
9270 struct got_repository *repo, struct got_fileindex *fileindex)
9272 const struct got_error *err = NULL;
9273 int file_renamed = 0;
9274 unsigned char status_old, staged_status_old;
9275 unsigned char status_new, staged_status_new;
9277 *oldpath = NULL;
9278 *newpath = NULL;
9280 err = patch_check_path(old != NULL ? old : new, oldpath,
9281 &status_old, &staged_status_old, fileindex, worktree, repo);
9282 if (err)
9283 goto done;
9285 err = patch_check_path(new != NULL ? new : old, newpath,
9286 &status_new, &staged_status_new, fileindex, worktree, repo);
9287 if (err)
9288 goto done;
9290 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9291 file_renamed = 1;
9293 if (old != NULL && new == NULL)
9294 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9295 else if (file_renamed) {
9296 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9297 if (err == NULL)
9298 err = patch_can_add(*newpath, status_new);
9299 } else if (old == NULL)
9300 err = patch_can_add(*newpath, status_new);
9301 else
9302 err = patch_can_edit(*newpath, status_new, staged_status_new);
9304 done:
9305 if (err) {
9306 free(*oldpath);
9307 *oldpath = NULL;
9308 free(*newpath);
9309 *newpath = NULL;
9311 return err;
9314 const struct got_error *
9315 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9316 struct got_worktree *worktree, struct got_fileindex *fileindex,
9317 got_worktree_checkout_cb progress_cb, void *progress_arg)
9319 struct schedule_addition_args saa;
9321 memset(&saa, 0, sizeof(saa));
9322 saa.worktree = worktree;
9323 saa.fileindex = fileindex;
9324 saa.progress_cb = progress_cb;
9325 saa.progress_arg = progress_arg;
9326 saa.repo = repo;
9328 return worktree_status(worktree, path, fileindex, repo,
9329 schedule_addition, &saa, NULL, NULL, 1, 0);
9332 const struct got_error *
9333 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9334 struct got_worktree *worktree, struct got_fileindex *fileindex,
9335 got_worktree_delete_cb progress_cb, void *progress_arg)
9337 struct schedule_deletion_args sda;
9339 memset(&sda, 0, sizeof(sda));
9340 sda.worktree = worktree;
9341 sda.fileindex = fileindex;
9342 sda.progress_cb = progress_cb;
9343 sda.progress_arg = progress_arg;
9344 sda.repo = repo;
9345 sda.delete_local_mods = 0;
9346 sda.keep_on_disk = 0;
9347 sda.ignore_missing_paths = 0;
9348 sda.status_codes = NULL;
9350 return worktree_status(worktree, path, fileindex, repo,
9351 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9354 const struct got_error *
9355 got_worktree_patch_complete(struct got_fileindex *fileindex,
9356 const char *fileindex_path)
9358 const struct got_error *err = NULL;
9360 err = sync_fileindex(fileindex, fileindex_path);
9361 got_fileindex_free(fileindex);
9363 return err;