Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static mode_t apply_umask(mode_t);
69 static const struct got_error *
70 create_meta_file(const char *path_got, const char *name, const char *content)
71 {
72 const struct got_error *err = NULL;
73 char *path;
75 if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 return got_error_from_errno("asprintf");
78 err = got_path_create_file(path, content);
79 free(path);
80 return err;
81 }
83 static const struct got_error *
84 update_meta_file(const char *path_got, const char *name, const char *content)
85 {
86 const struct got_error *err = NULL;
87 FILE *tmpfile = NULL;
88 char *tmppath = NULL;
89 char *path = NULL;
91 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 err = got_error_from_errno("asprintf");
93 path = NULL;
94 goto done;
95 }
97 err = got_opentemp_named(&tmppath, &tmpfile, path);
98 if (err)
99 goto done;
101 if (content) {
102 int len = fprintf(tmpfile, "%s\n", content);
103 if (len != strlen(content) + 1) {
104 err = got_error_from_errno2("fprintf", tmppath);
105 goto done;
109 if (rename(tmppath, path) != 0) {
110 err = got_error_from_errno3("rename", tmppath, path);
111 unlink(tmppath);
112 goto done;
115 done:
116 if (fclose(tmpfile) == EOF && err == NULL)
117 err = got_error_from_errno2("fclose", tmppath);
118 free(tmppath);
119 return err;
122 static const struct got_error *
123 write_head_ref(const char *path_got, struct got_reference *head_ref)
125 const struct got_error *err = NULL;
126 char *refstr = NULL;
128 if (got_ref_is_symbolic(head_ref)) {
129 refstr = got_ref_to_str(head_ref);
130 if (refstr == NULL)
131 return got_error_from_errno("got_ref_to_str");
132 } else {
133 refstr = strdup(got_ref_get_name(head_ref));
134 if (refstr == NULL)
135 return got_error_from_errno("strdup");
137 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 free(refstr);
139 return err;
142 const struct got_error *
143 got_worktree_init(const char *path, struct got_reference *head_ref,
144 const char *prefix, struct got_repository *repo)
146 const struct got_error *err = NULL;
147 struct got_object_id *commit_id = NULL;
148 uuid_t uuid;
149 uint32_t uuid_status;
150 int obj_type;
151 char *path_got = NULL;
152 char *formatstr = NULL;
153 char *absprefix = NULL;
154 char *basestr = NULL;
155 char *uuidstr = NULL;
157 if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 err = got_error(GOT_ERR_WORKTREE_REPO);
159 goto done;
162 err = got_ref_resolve(&commit_id, repo, head_ref);
163 if (err)
164 return err;
165 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (err)
167 return err;
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error(GOT_ERR_OBJ_TYPE);
171 if (!got_path_is_absolute(prefix)) {
172 if (asprintf(&absprefix, "/%s", prefix) == -1)
173 return got_error_from_errno("asprintf");
176 /* Create top-level directory (may already exist). */
177 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 err = got_error_from_errno2("mkdir", path);
179 goto done;
182 /* Create .got directory (may already exist). */
183 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 err = got_error_from_errno("asprintf");
185 goto done;
187 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 err = got_error_from_errno2("mkdir", path_got);
189 goto done;
192 /* Create an empty lock file. */
193 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
194 if (err)
195 goto done;
197 /* Create an empty file index. */
198 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
199 if (err)
200 goto done;
202 /* Write the HEAD reference. */
203 err = write_head_ref(path_got, head_ref);
204 if (err)
205 goto done;
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
209 if (err)
210 goto done;
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 if (err)
213 goto done;
215 /* Store path to repository. */
216 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 got_repo_get_path(repo));
218 if (err)
219 goto done;
221 /* Store in-repository path prefix. */
222 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 absprefix ? absprefix : prefix);
224 if (err)
225 goto done;
227 /* Generate UUID. */
228 uuid_create(&uuid, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_create");
231 goto done;
233 uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 if (uuid_status != uuid_s_ok) {
235 err = got_error_uuid(uuid_status, "uuid_to_string");
236 goto done;
238 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
239 if (err)
240 goto done;
242 /* Stamp work tree with format file. */
243 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 err = got_error_from_errno("asprintf");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 if (err)
249 goto done;
251 done:
252 free(commit_id);
253 free(path_got);
254 free(formatstr);
255 free(absprefix);
256 free(basestr);
257 free(uuidstr);
258 return err;
261 const struct got_error *
262 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 const char *path_prefix)
265 char *absprefix = NULL;
267 if (!got_path_is_absolute(path_prefix)) {
268 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 return got_error_from_errno("asprintf");
271 *match = (strcmp(absprefix ? absprefix : path_prefix,
272 worktree->path_prefix) == 0);
273 free(absprefix);
274 return NULL;
277 const char *
278 got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 return worktree->head_ref_name;
283 const struct got_error *
284 got_worktree_set_head_ref(struct got_worktree *worktree,
285 struct got_reference *head_ref)
287 const struct got_error *err = NULL;
288 char *path_got = NULL, *head_ref_name = NULL;
290 if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 GOT_WORKTREE_GOT_DIR) == -1) {
292 err = got_error_from_errno("asprintf");
293 path_got = NULL;
294 goto done;
297 head_ref_name = strdup(got_ref_get_name(head_ref));
298 if (head_ref_name == NULL) {
299 err = got_error_from_errno("strdup");
300 goto done;
303 err = write_head_ref(path_got, head_ref);
304 if (err)
305 goto done;
307 free(worktree->head_ref_name);
308 worktree->head_ref_name = head_ref_name;
309 done:
310 free(path_got);
311 if (err)
312 free(head_ref_name);
313 return err;
316 struct got_object_id *
317 got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 return worktree->base_commit_id;
322 const struct got_error *
323 got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 struct got_repository *repo, struct got_object_id *commit_id)
326 const struct got_error *err;
327 struct got_object *obj = NULL;
328 char *id_str = NULL;
329 char *path_got = NULL;
331 if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 GOT_WORKTREE_GOT_DIR) == -1) {
333 err = got_error_from_errno("asprintf");
334 path_got = NULL;
335 goto done;
338 err = got_object_open(&obj, repo, commit_id);
339 if (err)
340 return err;
342 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 err = got_error(GOT_ERR_OBJ_TYPE);
344 goto done;
347 /* Record our base commit. */
348 err = got_object_id_str(&id_str, commit_id);
349 if (err)
350 goto done;
351 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
352 if (err)
353 goto done;
355 free(worktree->base_commit_id);
356 worktree->base_commit_id = got_object_id_dup(commit_id);
357 if (worktree->base_commit_id == NULL) {
358 err = got_error_from_errno("got_object_id_dup");
359 goto done;
361 done:
362 if (obj)
363 got_object_close(obj);
364 free(id_str);
365 free(path_got);
366 return err;
369 const struct got_gotconfig *
370 got_worktree_get_gotconfig(struct got_worktree *worktree)
372 return worktree->gotconfig;
375 static const struct got_error *
376 lock_worktree(struct got_worktree *worktree, int operation)
378 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 : got_error_from_errno2("flock",
381 got_worktree_get_root_path(worktree)));
382 return NULL;
385 static const struct got_error *
386 add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 const struct got_error *err = NULL;
389 char *abspath;
391 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 return got_error_from_errno("asprintf");
394 err = got_path_mkdir(abspath);
395 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 struct stat sb;
397 err = NULL;
398 if (lstat(abspath, &sb) == -1) {
399 err = got_error_from_errno2("lstat", abspath);
400 } else if (!S_ISDIR(sb.st_mode)) {
401 /* TODO directory is obstructed; do something */
402 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
405 free(abspath);
406 return err;
409 static const struct got_error *
410 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 const struct got_error *err = NULL;
413 uint8_t fbuf1[8192];
414 uint8_t fbuf2[8192];
415 size_t flen1 = 0, flen2 = 0;
417 *same = 1;
419 for (;;) {
420 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 if (flen1 == 0 && ferror(f1)) {
422 err = got_error_from_errno("fread");
423 break;
425 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 if (flen2 == 0 && ferror(f2)) {
427 err = got_error_from_errno("fread");
428 break;
430 if (flen1 == 0) {
431 if (flen2 != 0)
432 *same = 0;
433 break;
434 } else if (flen2 == 0) {
435 if (flen1 != 0)
436 *same = 0;
437 break;
438 } else if (flen1 == flen2) {
439 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
440 *same = 0;
441 break;
443 } else {
444 *same = 0;
445 break;
449 return err;
452 static const struct got_error *
453 check_files_equal(int *same, FILE *f1, FILE *f2)
455 struct stat sb;
456 size_t size1, size2;
458 *same = 1;
460 if (fstat(fileno(f1), &sb) != 0)
461 return got_error_from_errno("fstat");
462 size1 = sb.st_size;
464 if (fstat(fileno(f2), &sb) != 0)
465 return got_error_from_errno("fstat");
466 size2 = sb.st_size;
468 if (size1 != size2) {
469 *same = 0;
470 return NULL;
473 if (fseek(f1, 0L, SEEK_SET) == -1)
474 return got_ferror(f1, GOT_ERR_IO);
475 if (fseek(f2, 0L, SEEK_SET) == -1)
476 return got_ferror(f2, GOT_ERR_IO);
478 return check_file_contents_equal(same, f1, f2);
481 static const struct got_error *
482 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
484 uint8_t fbuf[65536];
485 size_t flen;
486 ssize_t outlen;
488 *outsize = 0;
490 if (fseek(f, 0L, SEEK_SET) == -1)
491 return got_ferror(f, GOT_ERR_IO);
493 for (;;) {
494 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 if (flen == 0) {
496 if (ferror(f))
497 return got_error_from_errno("fread");
498 if (feof(f))
499 break;
501 outlen = write(outfd, fbuf, flen);
502 if (outlen == -1)
503 return got_error_from_errno("write");
504 if (outlen != flen)
505 return got_error(GOT_ERR_IO);
506 *outsize += outlen;
509 return NULL;
512 static const struct got_error *
513 merge_binary_file(int *overlapcnt, int merged_fd,
514 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 const char *ondisk_path)
518 const struct got_error *err = NULL;
519 int same_content, changed_deriv, changed_deriv2;
520 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 char *base_path_orig = NULL, *base_path_deriv = NULL;
524 char *base_path_deriv2 = NULL;
526 *overlapcnt = 0;
528 err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 if (err)
530 return err;
532 if (same_content)
533 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 err = check_files_equal(&same_content, f_deriv, f_orig);
536 if (err)
537 return err;
538 changed_deriv = !same_content;
539 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 if (err)
541 return err;
542 changed_deriv2 = !same_content;
544 if (changed_deriv && changed_deriv2) {
545 *overlapcnt = 1;
546 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
550 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
552 goto done;
554 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 err = got_error_from_errno("asprintf");
556 goto done;
558 err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 base_path_orig);
560 if (err)
561 goto done;
562 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 base_path_deriv);
564 if (err)
565 goto done;
566 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 base_path_deriv2);
568 if (err)
569 goto done;
570 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 if (err)
572 goto done;
573 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 if (err)
575 goto done;
576 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (err)
578 goto done;
579 if (dprintf(merged_fd, "Binary files differ and cannot be "
580 "merged automatically:\n") < 0) {
581 err = got_error_from_errno("dprintf");
582 goto done;
584 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 label_deriv ? " " : "",
587 label_deriv ? label_deriv : "",
588 path_deriv) < 0) {
589 err = got_error_from_errno("dprintf");
590 goto done;
592 if (size_orig > 0) {
593 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 GOT_DIFF_CONFLICT_MARKER_ORIG,
595 label_orig ? " " : "",
596 label_orig ? label_orig : "",
597 path_orig) < 0) {
598 err = got_error_from_errno("dprintf");
599 goto done;
602 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 GOT_DIFF_CONFLICT_MARKER_SEP,
604 path_deriv2,
605 GOT_DIFF_CONFLICT_MARKER_END,
606 label_deriv2 ? " " : "",
607 label_deriv2 ? label_deriv2 : "") < 0) {
608 err = got_error_from_errno("dprintf");
609 goto done;
611 } else if (changed_deriv)
612 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 else if (changed_deriv2)
614 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 done:
616 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 err == NULL)
618 err = got_error_from_errno2("unlink", path_orig);
619 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_orig);
621 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv);
623 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 err = got_error_from_errno2("close", path_deriv2);
625 free(path_orig);
626 free(path_deriv);
627 free(path_deriv2);
628 free(base_path_orig);
629 free(base_path_deriv);
630 free(base_path_deriv2);
631 return err;
634 /*
635 * Perform a 3-way merge where the file f_orig acts as the common
636 * ancestor, the file f_deriv acts as the first derived version,
637 * and the file f_deriv2 acts as the second derived version.
638 * The merge result will be written to a new file at ondisk_path; any
639 * existing file at this path will be replaced.
640 */
641 static const struct got_error *
642 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 const char *path, uint16_t st_mode,
645 const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 got_worktree_checkout_cb progress_cb, void *progress_arg)
649 const struct got_error *err = NULL;
650 int merged_fd = -1;
651 FILE *f_merged = NULL;
652 char *merged_path = NULL, *base_path = NULL;
653 int overlapcnt = 0;
654 char *parent = NULL;
656 *local_changes_subsumed = 0;
658 err = got_path_dirname(&parent, ondisk_path);
659 if (err)
660 return err;
662 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 err = got_error_from_errno("asprintf");
664 goto done;
667 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
668 if (err)
669 goto done;
671 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 if (err) {
674 if (err->code != GOT_ERR_FILE_BINARY)
675 goto done;
676 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 ondisk_path);
679 if (err)
680 goto done;
683 err = (*progress_cb)(progress_arg,
684 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
685 if (err)
686 goto done;
688 if (fsync(merged_fd) != 0) {
689 err = got_error_from_errno("fsync");
690 goto done;
693 f_merged = fdopen(merged_fd, "r");
694 if (f_merged == NULL) {
695 err = got_error_from_errno("fdopen");
696 goto done;
698 merged_fd = -1;
700 /* Check if a clean merge has subsumed all local changes. */
701 if (overlapcnt == 0) {
702 err = check_files_equal(local_changes_subsumed, f_deriv,
703 f_merged);
704 if (err)
705 goto done;
708 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 err = got_error_from_errno2("fchmod", merged_path);
710 goto done;
713 if (rename(merged_path, ondisk_path) != 0) {
714 err = got_error_from_errno3("rename", merged_path,
715 ondisk_path);
716 goto done;
718 done:
719 if (err) {
720 if (merged_path)
721 unlink(merged_path);
723 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
727 free(merged_path);
728 free(base_path);
729 free(parent);
730 return err;
733 static const struct got_error *
734 update_symlink(const char *ondisk_path, const char *target_path,
735 size_t target_len)
737 /* This is not atomic but matches what 'ln -sf' does. */
738 if (unlink(ondisk_path) == -1)
739 return got_error_from_errno2("unlink", ondisk_path);
740 if (symlink(target_path, ondisk_path) == -1)
741 return got_error_from_errno3("symlink", target_path,
742 ondisk_path);
743 return NULL;
746 /*
747 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 * in the work tree with a file that contains conflict markers and the
749 * conflicting target paths of the original version, a "derived version"
750 * of a symlink from an incoming change, and a local version of the symlink.
752 * The original versions's target path can be NULL if it is not available,
753 * such as if both derived versions added a new symlink at the same path.
755 * The incoming derived symlink target is NULL in case the incoming change
756 * has deleted this symlink.
757 */
758 static const struct got_error *
759 install_symlink_conflict(const char *deriv_target,
760 struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 const char *label_orig, const char *local_target, const char *ondisk_path)
763 const struct got_error *err;
764 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 FILE *f = NULL;
767 err = got_object_id_str(&id_str, deriv_base_commit_id);
768 if (err)
769 return got_error_from_errno("asprintf");
771 if (asprintf(&label_deriv, "%s: commit %s",
772 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 err = got_error_from_errno("asprintf");
774 goto done;
777 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
778 if (err)
779 goto done;
781 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 err = got_error_from_errno2("fchmod", path);
783 goto done;
786 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 deriv_target ? deriv_target : "(symlink was deleted)",
789 orig_target ? label_orig : "",
790 orig_target ? "\n" : "",
791 orig_target ? orig_target : "",
792 orig_target ? "\n" : "",
793 GOT_DIFF_CONFLICT_MARKER_SEP,
794 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 err = got_error_from_errno2("fprintf", path);
796 goto done;
799 if (unlink(ondisk_path) == -1) {
800 err = got_error_from_errno2("unlink", ondisk_path);
801 goto done;
803 if (rename(path, ondisk_path) == -1) {
804 err = got_error_from_errno3("rename", path, ondisk_path);
805 goto done;
807 done:
808 if (f != NULL && fclose(f) == EOF && err == NULL)
809 err = got_error_from_errno2("fclose", path);
810 free(path);
811 free(id_str);
812 free(label_deriv);
813 return err;
816 /* forward declaration */
817 static const struct got_error *
818 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 const char *, const char *, uint16_t, const char *,
820 struct got_blob_object *, struct got_object_id *,
821 struct got_repository *, got_worktree_checkout_cb, void *);
823 /*
824 * Merge a symlink into the work tree, where blob_orig acts as the common
825 * ancestor, deriv_target is the link target of the first derived version,
826 * and the symlink on disk acts as the second derived version.
827 * Assume that contents of both blobs represent symlinks.
828 */
829 static const struct got_error *
830 merge_symlink(struct got_worktree *worktree,
831 struct got_blob_object *blob_orig, const char *ondisk_path,
832 const char *path, const char *label_orig, const char *deriv_target,
833 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 got_worktree_checkout_cb progress_cb, void *progress_arg)
836 const struct got_error *err = NULL;
837 char *ancestor_target = NULL;
838 struct stat sb;
839 ssize_t ondisk_len, deriv_len;
840 char ondisk_target[PATH_MAX];
841 int have_local_change = 0;
842 int have_incoming_change = 0;
844 if (lstat(ondisk_path, &sb) == -1)
845 return got_error_from_errno2("lstat", ondisk_path);
847 ondisk_len = readlink(ondisk_path, ondisk_target,
848 sizeof(ondisk_target));
849 if (ondisk_len == -1) {
850 err = got_error_from_errno2("readlink",
851 ondisk_path);
852 goto done;
854 ondisk_target[ondisk_len] = '\0';
856 if (blob_orig) {
857 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 if (err)
859 goto done;
862 if (ancestor_target == NULL ||
863 (ondisk_len != strlen(ancestor_target) ||
864 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 have_local_change = 1;
867 deriv_len = strlen(deriv_target);
868 if (ancestor_target == NULL ||
869 (deriv_len != strlen(ancestor_target) ||
870 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 have_incoming_change = 1;
873 if (!have_local_change && !have_incoming_change) {
874 if (ancestor_target) {
875 /* Both sides made the same change. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else if (deriv_len == ondisk_len &&
879 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 /* Both sides added the same symlink. */
881 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 path);
883 } else {
884 /* Both sides added symlinks which don't match. */
885 err = install_symlink_conflict(deriv_target,
886 deriv_base_commit_id, ancestor_target,
887 label_orig, ondisk_target, ondisk_path);
888 if (err)
889 goto done;
890 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 path);
893 } else if (!have_local_change && have_incoming_change) {
894 /* Apply the incoming change. */
895 err = update_symlink(ondisk_path, deriv_target,
896 strlen(deriv_target));
897 if (err)
898 goto done;
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 } else if (have_local_change && have_incoming_change) {
901 if (deriv_len == ondisk_len &&
902 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 /* Both sides made the same change. */
904 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 path);
906 } else {
907 err = install_symlink_conflict(deriv_target,
908 deriv_base_commit_id, ancestor_target, label_orig,
909 ondisk_target, ondisk_path);
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 path);
917 done:
918 free(ancestor_target);
919 return err;
922 static const struct got_error *
923 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 const struct got_error *err = NULL;
926 char target_path[PATH_MAX];
927 ssize_t target_len;
928 size_t n;
929 FILE *f;
931 *outfile = NULL;
933 f = got_opentemp();
934 if (f == NULL)
935 return got_error_from_errno("got_opentemp");
936 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 if (target_len == -1) {
938 err = got_error_from_errno2("readlink", ondisk_path);
939 goto done;
941 n = fwrite(target_path, 1, target_len, f);
942 if (n != target_len) {
943 err = got_ferror(f, GOT_ERR_IO);
944 goto done;
946 if (fflush(f) == EOF) {
947 err = got_error_from_errno("fflush");
948 goto done;
950 if (fseek(f, 0L, SEEK_SET) == -1) {
951 err = got_ferror(f, GOT_ERR_IO);
952 goto done;
954 done:
955 if (err)
956 fclose(f);
957 else
958 *outfile = f;
959 return err;
962 /*
963 * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 * blob_deriv acts as the first derived version, and the file on disk
965 * acts as the second derived version.
966 */
967 static const struct got_error *
968 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 struct got_blob_object *blob_orig, const char *ondisk_path,
970 const char *path, uint16_t st_mode, const char *label_orig,
971 struct got_blob_object *blob_deriv,
972 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 got_worktree_checkout_cb progress_cb, void *progress_arg)
975 const struct got_error *err = NULL;
976 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 char *blob_orig_path = NULL;
978 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 char *label_deriv = NULL, *parent = NULL;
981 *local_changes_subsumed = 0;
983 err = got_path_dirname(&parent, ondisk_path);
984 if (err)
985 return err;
987 if (blob_orig) {
988 if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 parent) == -1) {
990 err = got_error_from_errno("asprintf");
991 base_path = NULL;
992 goto done;
995 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
996 if (err)
997 goto done;
998 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
999 blob_orig);
1000 if (err)
1001 goto done;
1002 free(base_path);
1003 } else {
1005 * No common ancestor exists. This is an "add vs add" conflict
1006 * and we simply use an empty ancestor file to make both files
1007 * appear in the merged result in their entirety.
1009 f_orig = got_opentemp();
1010 if (f_orig == NULL) {
1011 err = got_error_from_errno("got_opentemp");
1012 goto done;
1016 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1017 err = got_error_from_errno("asprintf");
1018 base_path = NULL;
1019 goto done;
1022 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1023 if (err)
1024 goto done;
1025 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1026 blob_deriv);
1027 if (err)
1028 goto done;
1030 err = got_object_id_str(&id_str, deriv_base_commit_id);
1031 if (err)
1032 goto done;
1033 if (asprintf(&label_deriv, "%s: commit %s",
1034 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1035 err = got_error_from_errno("asprintf");
1036 goto done;
1040 * In order the run a 3-way merge with a symlink we copy the symlink's
1041 * target path into a temporary file and use that file with diff3.
1043 if (S_ISLNK(st_mode)) {
1044 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1045 if (err)
1046 goto done;
1047 } else {
1048 int fd;
1049 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1050 if (fd == -1) {
1051 err = got_error_from_errno2("open", ondisk_path);
1052 goto done;
1054 f_deriv2 = fdopen(fd, "r");
1055 if (f_deriv2 == NULL) {
1056 err = got_error_from_errno2("fdopen", ondisk_path);
1057 close(fd);
1058 goto done;
1062 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1063 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1064 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1065 done:
1066 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 free(base_path);
1073 if (blob_orig_path) {
1074 unlink(blob_orig_path);
1075 free(blob_orig_path);
1077 if (blob_deriv_path) {
1078 unlink(blob_deriv_path);
1079 free(blob_deriv_path);
1081 free(id_str);
1082 free(label_deriv);
1083 free(parent);
1084 return err;
1087 static const struct got_error *
1088 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1089 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1090 int wt_fd, const char *path, struct got_object_id *blob_id)
1092 const struct got_error *err = NULL;
1093 struct got_fileindex_entry *new_ie;
1095 *new_iep = NULL;
1097 err = got_fileindex_entry_alloc(&new_ie, path);
1098 if (err)
1099 return err;
1101 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1102 blob_id->sha1, base_commit_id->sha1, 1);
1103 if (err)
1104 goto done;
1106 err = got_fileindex_entry_add(fileindex, new_ie);
1107 done:
1108 if (err)
1109 got_fileindex_entry_free(new_ie);
1110 else
1111 *new_iep = new_ie;
1112 return err;
1115 static mode_t
1116 get_ondisk_perms(int executable, mode_t st_mode)
1118 mode_t xbits = S_IXUSR;
1120 if (executable) {
1121 /* Map read bits to execute bits. */
1122 if (st_mode & S_IRGRP)
1123 xbits |= S_IXGRP;
1124 if (st_mode & S_IROTH)
1125 xbits |= S_IXOTH;
1126 return st_mode | xbits;
1129 return st_mode;
1132 static mode_t
1133 apply_umask(mode_t mode)
1135 mode_t um;
1137 um = umask(000);
1138 umask(um);
1139 return mode & ~um;
1142 /* forward declaration */
1143 static const struct got_error *
1144 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 const char *path, mode_t te_mode, mode_t st_mode,
1146 struct got_blob_object *blob, int restoring_missing_file,
1147 int reverting_versioned_file, int installing_bad_symlink,
1148 int path_is_unversioned, struct got_repository *repo,
1149 got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 * This function assumes that the provided symlink target points at a
1153 * safe location in the work tree!
1155 static const struct got_error *
1156 replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 const char *target_path, size_t target_len)
1159 const struct got_error *err = NULL;
1160 ssize_t elen;
1161 char etarget[PATH_MAX];
1162 int fd;
1164 *did_something = 0;
1167 * "Bad" symlinks (those pointing outside the work tree or into the
1168 * .got directory) are installed in the work tree as a regular file
1169 * which contains the bad symlink target path.
1170 * The new symlink target has already been checked for safety by our
1171 * caller. If we can successfully open a regular file then we simply
1172 * replace this file with a symlink below.
1174 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 if (fd == -1) {
1176 if (!got_err_open_nofollow_on_symlink())
1177 return got_error_from_errno2("open", ondisk_path);
1179 /* We are updating an existing on-disk symlink. */
1180 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 if (elen == -1)
1182 return got_error_from_errno2("readlink", ondisk_path);
1184 if (elen == target_len &&
1185 memcmp(etarget, target_path, target_len) == 0)
1186 return NULL; /* nothing to do */
1189 *did_something = 1;
1190 err = update_symlink(ondisk_path, target_path, target_len);
1191 if (fd != -1 && close(fd) == -1 && err == NULL)
1192 err = got_error_from_errno2("close", ondisk_path);
1193 return err;
1196 static const struct got_error *
1197 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 const struct got_error *err = NULL;
1201 char canonpath[PATH_MAX];
1202 char *path_got = NULL;
1204 *is_bad_symlink = 0;
1206 if (target_len >= sizeof(canonpath)) {
1207 *is_bad_symlink = 1;
1208 return NULL;
1212 * We do not use realpath(3) to resolve the symlink's target
1213 * path because we don't want to resolve symlinks recursively.
1214 * Instead we make the path absolute and then canonicalize it.
1215 * Relative symlink target lookup should begin at the directory
1216 * in which the blob object is being installed.
1218 if (!got_path_is_absolute(target_path)) {
1219 char *abspath, *parent;
1220 err = got_path_dirname(&parent, ondisk_path);
1221 if (err)
1222 return err;
1223 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 free(parent);
1225 return got_error_from_errno("asprintf");
1227 free(parent);
1228 if (strlen(abspath) >= sizeof(canonpath)) {
1229 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1230 free(abspath);
1231 return err;
1233 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1234 free(abspath);
1235 if (err)
1236 return err;
1237 } else {
1238 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1239 if (err)
1240 return err;
1243 /* Only allow symlinks pointing at paths within the work tree. */
1244 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 *is_bad_symlink = 1;
1246 return NULL;
1249 /* Do not allow symlinks pointing into the .got directory. */
1250 if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 GOT_WORKTREE_GOT_DIR) == -1)
1252 return got_error_from_errno("asprintf");
1253 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 *is_bad_symlink = 1;
1256 free(path_got);
1257 return NULL;
1260 static const struct got_error *
1261 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 int restoring_missing_file, int reverting_versioned_file,
1264 int path_is_unversioned, int allow_bad_symlinks,
1265 struct got_repository *repo,
1266 got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 const struct got_error *err = NULL;
1269 char target_path[PATH_MAX];
1270 size_t len, target_len = 0;
1271 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 *is_bad_symlink = 0;
1277 * Blob object content specifies the target path of the link.
1278 * If a symbolic link cannot be installed we instead create
1279 * a regular file which contains the link target path stored
1280 * in the blob object.
1282 do {
1283 err = got_object_blob_read_block(&len, blob);
1284 if (err)
1285 return err;
1287 if (len + target_len >= sizeof(target_path)) {
1288 /* Path too long; install as a regular file. */
1289 *is_bad_symlink = 1;
1290 got_object_blob_rewind(blob);
1291 return install_blob(worktree, ondisk_path, path,
1292 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 restoring_missing_file, reverting_versioned_file,
1294 1, path_is_unversioned, repo, progress_cb,
1295 progress_arg);
1297 if (len > 0) {
1298 /* Skip blob object header first time around. */
1299 memcpy(target_path + target_len, buf + hdrlen,
1300 len - hdrlen);
1301 target_len += len - hdrlen;
1302 hdrlen = 0;
1304 } while (len != 0);
1305 target_path[target_len] = '\0';
1307 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 ondisk_path, worktree->root_path);
1309 if (err)
1310 return err;
1312 if (*is_bad_symlink && !allow_bad_symlinks) {
1313 /* install as a regular file */
1314 got_object_blob_rewind(blob);
1315 err = install_blob(worktree, ondisk_path, path,
1316 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 restoring_missing_file, reverting_versioned_file, 1,
1318 path_is_unversioned, repo, progress_cb, progress_arg);
1319 return err;
1322 if (symlink(target_path, ondisk_path) == -1) {
1323 if (errno == EEXIST) {
1324 int symlink_replaced;
1325 if (path_is_unversioned) {
1326 err = (*progress_cb)(progress_arg,
1327 GOT_STATUS_UNVERSIONED, path);
1328 return err;
1330 err = replace_existing_symlink(&symlink_replaced,
1331 ondisk_path, target_path, target_len);
1332 if (err)
1333 return err;
1334 if (progress_cb) {
1335 if (symlink_replaced) {
1336 err = (*progress_cb)(progress_arg,
1337 reverting_versioned_file ?
1338 GOT_STATUS_REVERT :
1339 GOT_STATUS_UPDATE, path);
1340 } else {
1341 err = (*progress_cb)(progress_arg,
1342 GOT_STATUS_EXISTS, path);
1345 return err; /* Nothing else to do. */
1348 if (errno == ENOENT) {
1349 char *parent;
1350 err = got_path_dirname(&parent, ondisk_path);
1351 if (err)
1352 return err;
1353 err = add_dir_on_disk(worktree, parent);
1354 free(parent);
1355 if (err)
1356 return err;
1358 * Retry, and fall through to error handling
1359 * below if this second attempt fails.
1361 if (symlink(target_path, ondisk_path) != -1) {
1362 err = NULL; /* success */
1363 return err;
1367 /* Handle errors from first or second creation attempt. */
1368 if (errno == ENAMETOOLONG) {
1369 /* bad target path; install as a regular file */
1370 *is_bad_symlink = 1;
1371 got_object_blob_rewind(blob);
1372 err = install_blob(worktree, ondisk_path, path,
1373 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 restoring_missing_file, reverting_versioned_file, 1,
1375 path_is_unversioned, repo,
1376 progress_cb, progress_arg);
1377 } else if (errno == ENOTDIR) {
1378 err = got_error_path(ondisk_path,
1379 GOT_ERR_FILE_OBSTRUCTED);
1380 } else {
1381 err = got_error_from_errno3("symlink",
1382 target_path, ondisk_path);
1384 } else if (progress_cb)
1385 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1387 return err;
1390 static const struct got_error *
1391 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 const char *path, mode_t te_mode, mode_t st_mode,
1393 struct got_blob_object *blob, int restoring_missing_file,
1394 int reverting_versioned_file, int installing_bad_symlink,
1395 int path_is_unversioned, struct got_repository *repo,
1396 got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 const struct got_error *err = NULL;
1399 int fd = -1;
1400 size_t len, hdrlen;
1401 int update = 0;
1402 char *tmppath = NULL;
1403 mode_t mode;
1405 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1407 O_CLOEXEC, mode);
1408 if (fd == -1) {
1409 if (errno == ENOENT) {
1410 char *parent;
1411 err = got_path_dirname(&parent, path);
1412 if (err)
1413 return err;
1414 err = add_dir_on_disk(worktree, parent);
1415 free(parent);
1416 if (err)
1417 return err;
1418 fd = open(ondisk_path,
1419 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1420 mode);
1421 if (fd == -1)
1422 return got_error_from_errno2("open",
1423 ondisk_path);
1424 } else if (errno == EEXIST) {
1425 if (path_is_unversioned) {
1426 err = (*progress_cb)(progress_arg,
1427 GOT_STATUS_UNVERSIONED, path);
1428 goto done;
1430 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1431 !S_ISREG(st_mode) && !installing_bad_symlink) {
1432 /* TODO file is obstructed; do something */
1433 err = got_error_path(ondisk_path,
1434 GOT_ERR_FILE_OBSTRUCTED);
1435 goto done;
1436 } else {
1437 err = got_opentemp_named_fd(&tmppath, &fd,
1438 ondisk_path);
1439 if (err)
1440 goto done;
1441 update = 1;
1443 if (fchmod(fd, apply_umask(mode)) == -1) {
1444 err = got_error_from_errno2("fchmod",
1445 tmppath);
1446 goto done;
1449 } else
1450 return got_error_from_errno2("open", ondisk_path);
1453 if (progress_cb) {
1454 if (restoring_missing_file)
1455 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1456 path);
1457 else if (reverting_versioned_file)
1458 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1459 path);
1460 else
1461 err = (*progress_cb)(progress_arg,
1462 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1463 if (err)
1464 goto done;
1467 hdrlen = got_object_blob_get_hdrlen(blob);
1468 do {
1469 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1470 err = got_object_blob_read_block(&len, blob);
1471 if (err)
1472 break;
1473 if (len > 0) {
1474 /* Skip blob object header first time around. */
1475 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1476 if (outlen == -1) {
1477 err = got_error_from_errno("write");
1478 goto done;
1479 } else if (outlen != len - hdrlen) {
1480 err = got_error(GOT_ERR_IO);
1481 goto done;
1483 hdrlen = 0;
1485 } while (len != 0);
1487 if (fsync(fd) != 0) {
1488 err = got_error_from_errno("fsync");
1489 goto done;
1492 if (update) {
1493 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1494 err = got_error_from_errno2("unlink", ondisk_path);
1495 goto done;
1497 if (rename(tmppath, ondisk_path) != 0) {
1498 err = got_error_from_errno3("rename", tmppath,
1499 ondisk_path);
1500 goto done;
1502 free(tmppath);
1503 tmppath = NULL;
1506 done:
1507 if (fd != -1 && close(fd) == -1 && err == NULL)
1508 err = got_error_from_errno("close");
1509 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1510 err = got_error_from_errno2("unlink", tmppath);
1511 free(tmppath);
1512 return err;
1515 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1516 static const struct got_error *
1517 get_modified_file_content_status(unsigned char *status, FILE *f)
1519 const struct got_error *err = NULL;
1520 const char *markers[3] = {
1521 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1522 GOT_DIFF_CONFLICT_MARKER_SEP,
1523 GOT_DIFF_CONFLICT_MARKER_END
1525 int i = 0;
1526 char *line = NULL;
1527 size_t linesize = 0;
1528 ssize_t linelen;
1530 while (*status == GOT_STATUS_MODIFY) {
1531 linelen = getline(&line, &linesize, f);
1532 if (linelen == -1) {
1533 if (feof(f))
1534 break;
1535 err = got_ferror(f, GOT_ERR_IO);
1536 break;
1539 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1540 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1541 == 0)
1542 *status = GOT_STATUS_CONFLICT;
1543 else
1544 i++;
1547 free(line);
1549 return err;
1552 static int
1553 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1555 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1556 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1559 static int
1560 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1562 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1563 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1564 ie->mtime_sec == sb->st_mtim.tv_sec &&
1565 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1566 ie->size == (sb->st_size & 0xffffffff) &&
1567 !xbit_differs(ie, sb->st_mode));
1570 static unsigned char
1571 get_staged_status(struct got_fileindex_entry *ie)
1573 switch (got_fileindex_entry_stage_get(ie)) {
1574 case GOT_FILEIDX_STAGE_ADD:
1575 return GOT_STATUS_ADD;
1576 case GOT_FILEIDX_STAGE_DELETE:
1577 return GOT_STATUS_DELETE;
1578 case GOT_FILEIDX_STAGE_MODIFY:
1579 return GOT_STATUS_MODIFY;
1580 default:
1581 return GOT_STATUS_NO_CHANGE;
1585 static const struct got_error *
1586 get_symlink_modification_status(unsigned char *status,
1587 struct got_fileindex_entry *ie, const char *abspath,
1588 int dirfd, const char *de_name, struct got_blob_object *blob)
1590 const struct got_error *err = NULL;
1591 char target_path[PATH_MAX];
1592 char etarget[PATH_MAX];
1593 ssize_t elen;
1594 size_t len, target_len = 0;
1595 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1596 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1598 *status = GOT_STATUS_NO_CHANGE;
1600 /* Blob object content specifies the target path of the link. */
1601 do {
1602 err = got_object_blob_read_block(&len, blob);
1603 if (err)
1604 return err;
1605 if (len + target_len >= sizeof(target_path)) {
1607 * Should not happen. The blob contents were OK
1608 * when this symlink was installed.
1610 return got_error(GOT_ERR_NO_SPACE);
1612 if (len > 0) {
1613 /* Skip blob object header first time around. */
1614 memcpy(target_path + target_len, buf + hdrlen,
1615 len - hdrlen);
1616 target_len += len - hdrlen;
1617 hdrlen = 0;
1619 } while (len != 0);
1620 target_path[target_len] = '\0';
1622 if (dirfd != -1) {
1623 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1624 if (elen == -1)
1625 return got_error_from_errno2("readlinkat", abspath);
1626 } else {
1627 elen = readlink(abspath, etarget, sizeof(etarget));
1628 if (elen == -1)
1629 return got_error_from_errno2("readlink", abspath);
1632 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1633 *status = GOT_STATUS_MODIFY;
1635 return NULL;
1638 static const struct got_error *
1639 get_file_status(unsigned char *status, struct stat *sb,
1640 struct got_fileindex_entry *ie, const char *abspath,
1641 int dirfd, const char *de_name, struct got_repository *repo)
1643 const struct got_error *err = NULL;
1644 struct got_object_id id;
1645 size_t hdrlen;
1646 int fd = -1, fd1 = -1;
1647 FILE *f = NULL;
1648 uint8_t fbuf[8192];
1649 struct got_blob_object *blob = NULL;
1650 size_t flen, blen;
1651 unsigned char staged_status = get_staged_status(ie);
1653 *status = GOT_STATUS_NO_CHANGE;
1654 memset(sb, 0, sizeof(*sb));
1657 * Whenever the caller provides a directory descriptor and a
1658 * directory entry name for the file, use them! This prevents
1659 * race conditions if filesystem paths change beneath our feet.
1661 if (dirfd != -1) {
1662 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1663 if (errno == ENOENT) {
1664 if (got_fileindex_entry_has_file_on_disk(ie))
1665 *status = GOT_STATUS_MISSING;
1666 else
1667 *status = GOT_STATUS_DELETE;
1668 goto done;
1670 err = got_error_from_errno2("fstatat", abspath);
1671 goto done;
1673 } else {
1674 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1675 if (fd == -1 && errno != ENOENT &&
1676 !got_err_open_nofollow_on_symlink())
1677 return got_error_from_errno2("open", abspath);
1678 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1679 if (lstat(abspath, sb) == -1)
1680 return got_error_from_errno2("lstat", abspath);
1681 } else if (fd == -1 || fstat(fd, sb) == -1) {
1682 if (errno == ENOENT) {
1683 if (got_fileindex_entry_has_file_on_disk(ie))
1684 *status = GOT_STATUS_MISSING;
1685 else
1686 *status = GOT_STATUS_DELETE;
1687 goto done;
1689 err = got_error_from_errno2("fstat", abspath);
1690 goto done;
1694 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1695 *status = GOT_STATUS_OBSTRUCTED;
1696 goto done;
1699 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1700 *status = GOT_STATUS_DELETE;
1701 goto done;
1702 } else if (!got_fileindex_entry_has_blob(ie) &&
1703 staged_status != GOT_STATUS_ADD) {
1704 *status = GOT_STATUS_ADD;
1705 goto done;
1708 if (!stat_info_differs(ie, sb))
1709 goto done;
1711 if (S_ISLNK(sb->st_mode) &&
1712 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1713 *status = GOT_STATUS_MODIFY;
1714 goto done;
1717 if (staged_status == GOT_STATUS_MODIFY ||
1718 staged_status == GOT_STATUS_ADD)
1719 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1720 else
1721 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1723 fd1 = got_opentempfd();
1724 if (fd1 == -1) {
1725 err = got_error_from_errno("got_opentempfd");
1726 goto done;
1728 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1729 if (err)
1730 goto done;
1732 if (S_ISLNK(sb->st_mode)) {
1733 err = get_symlink_modification_status(status, ie,
1734 abspath, dirfd, de_name, blob);
1735 goto done;
1738 if (dirfd != -1) {
1739 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 if (fd == -1) {
1741 err = got_error_from_errno2("openat", abspath);
1742 goto done;
1746 f = fdopen(fd, "r");
1747 if (f == NULL) {
1748 err = got_error_from_errno2("fdopen", abspath);
1749 goto done;
1751 fd = -1;
1752 hdrlen = got_object_blob_get_hdrlen(blob);
1753 for (;;) {
1754 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1755 err = got_object_blob_read_block(&blen, blob);
1756 if (err)
1757 goto done;
1758 /* Skip length of blob object header first time around. */
1759 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1760 if (flen == 0 && ferror(f)) {
1761 err = got_error_from_errno("fread");
1762 goto done;
1764 if (blen - hdrlen == 0) {
1765 if (flen != 0)
1766 *status = GOT_STATUS_MODIFY;
1767 break;
1768 } else if (flen == 0) {
1769 if (blen - hdrlen != 0)
1770 *status = GOT_STATUS_MODIFY;
1771 break;
1772 } else if (blen - hdrlen == flen) {
1773 /* Skip blob object header first time around. */
1774 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1775 *status = GOT_STATUS_MODIFY;
1776 break;
1778 } else {
1779 *status = GOT_STATUS_MODIFY;
1780 break;
1782 hdrlen = 0;
1785 if (*status == GOT_STATUS_MODIFY) {
1786 rewind(f);
1787 err = get_modified_file_content_status(status, f);
1788 } else if (xbit_differs(ie, sb->st_mode))
1789 *status = GOT_STATUS_MODE_CHANGE;
1790 done:
1791 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1792 err = got_error_from_errno("close");
1793 if (blob)
1794 got_object_blob_close(blob);
1795 if (f != NULL && fclose(f) == EOF && err == NULL)
1796 err = got_error_from_errno2("fclose", abspath);
1797 if (fd != -1 && close(fd) == -1 && err == NULL)
1798 err = got_error_from_errno2("close", abspath);
1799 return err;
1803 * Update timestamps in the file index if a file is unmodified and
1804 * we had to run a full content comparison to find out.
1806 static const struct got_error *
1807 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1808 struct got_fileindex_entry *ie, struct stat *sb)
1810 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1811 return got_fileindex_entry_update(ie, wt_fd, path,
1812 ie->blob_sha1, ie->commit_sha1, 1);
1814 return NULL;
1817 static const struct got_error *
1818 update_blob(struct got_worktree *worktree,
1819 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1820 struct got_tree_entry *te, const char *path,
1821 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1822 void *progress_arg)
1824 const struct got_error *err = NULL;
1825 struct got_blob_object *blob = NULL;
1826 char *ondisk_path = NULL;
1827 unsigned char status = GOT_STATUS_NO_CHANGE;
1828 struct stat sb;
1829 int fd1 = -1, fd2 = -1;
1831 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 return got_error_from_errno("asprintf");
1834 if (ie) {
1835 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1837 goto done;
1839 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1840 repo);
1841 if (err)
1842 goto done;
1843 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 sb.st_mode = got_fileindex_perms_to_st(ie);
1845 } else {
1846 if (stat(ondisk_path, &sb) == -1) {
1847 if (errno != ENOENT) {
1848 err = got_error_from_errno2("stat",
1849 ondisk_path);
1850 goto done;
1852 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 status = GOT_STATUS_UNVERSIONED;
1854 } else {
1855 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1856 status = GOT_STATUS_UNVERSIONED;
1857 else
1858 status = GOT_STATUS_OBSTRUCTED;
1862 if (status == GOT_STATUS_OBSTRUCTED) {
1863 if (ie)
1864 got_fileindex_entry_mark_skipped(ie);
1865 err = (*progress_cb)(progress_arg, status, path);
1866 goto done;
1868 if (status == GOT_STATUS_CONFLICT) {
1869 if (ie)
1870 got_fileindex_entry_mark_skipped(ie);
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1872 path);
1873 goto done;
1876 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1877 (S_ISLNK(te->mode) ||
1878 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1880 * This is a regular file or an installed bad symlink.
1881 * If the file index indicates that this file is already
1882 * up-to-date with respect to the repository we can skip
1883 * updating contents of this file.
1885 if (got_fileindex_entry_has_commit(ie) &&
1886 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1887 SHA1_DIGEST_LENGTH) == 0) {
1888 /* Same commit. */
1889 err = sync_timestamps(worktree->root_fd,
1890 path, status, ie, &sb);
1891 if (err)
1892 goto done;
1893 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1894 path);
1895 goto done;
1897 if (got_fileindex_entry_has_blob(ie) &&
1898 memcmp(ie->blob_sha1, te->id.sha1,
1899 SHA1_DIGEST_LENGTH) == 0) {
1900 /* Different commit but the same blob. */
1901 err = sync_timestamps(worktree->root_fd,
1902 path, status, ie, &sb);
1903 if (err)
1904 goto done;
1905 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1906 path);
1907 goto done;
1911 fd1 = got_opentempfd();
1912 if (fd1 == -1) {
1913 err = got_error_from_errno("got_opentempfd");
1914 goto done;
1916 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1917 if (err)
1918 goto done;
1920 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1921 int update_timestamps;
1922 struct got_blob_object *blob2 = NULL;
1923 char *label_orig = NULL;
1924 if (got_fileindex_entry_has_blob(ie)) {
1925 fd2 = got_opentempfd();
1926 if (fd2 == -1) {
1927 err = got_error_from_errno("got_opentempfd");
1928 goto done;
1930 struct got_object_id id2;
1931 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1932 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1933 fd2);
1934 if (err)
1935 goto done;
1937 if (got_fileindex_entry_has_commit(ie)) {
1938 char id_str[SHA1_DIGEST_STRING_LENGTH];
1939 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1940 sizeof(id_str)) == NULL) {
1941 err = got_error_path(id_str,
1942 GOT_ERR_BAD_OBJ_ID_STR);
1943 goto done;
1945 if (asprintf(&label_orig, "%s: commit %s",
1946 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1947 err = got_error_from_errno("asprintf");
1948 goto done;
1951 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1952 char *link_target;
1953 err = got_object_blob_read_to_str(&link_target, blob);
1954 if (err)
1955 goto done;
1956 err = merge_symlink(worktree, blob2, ondisk_path, path,
1957 label_orig, link_target, worktree->base_commit_id,
1958 repo, progress_cb, progress_arg);
1959 free(link_target);
1960 } else {
1961 err = merge_blob(&update_timestamps, worktree, blob2,
1962 ondisk_path, path, sb.st_mode, label_orig, blob,
1963 worktree->base_commit_id, repo,
1964 progress_cb, progress_arg);
1966 free(label_orig);
1967 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1968 err = got_error_from_errno("close");
1969 goto done;
1971 if (blob2)
1972 got_object_blob_close(blob2);
1973 if (err)
1974 goto done;
1976 * Do not update timestamps of files with local changes.
1977 * Otherwise, a future status walk would treat them as
1978 * unmodified files again.
1980 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1981 blob->id.sha1, worktree->base_commit_id->sha1,
1982 update_timestamps);
1983 } else if (status == GOT_STATUS_MODE_CHANGE) {
1984 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1985 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1986 } else if (status == GOT_STATUS_DELETE) {
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1988 if (err)
1989 goto done;
1990 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1991 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1992 if (err)
1993 goto done;
1994 } else {
1995 int is_bad_symlink = 0;
1996 if (S_ISLNK(te->mode)) {
1997 err = install_symlink(&is_bad_symlink, worktree,
1998 ondisk_path, path, blob,
1999 status == GOT_STATUS_MISSING, 0,
2000 status == GOT_STATUS_UNVERSIONED, 0,
2001 repo, progress_cb, progress_arg);
2002 } else {
2003 err = install_blob(worktree, ondisk_path, path,
2004 te->mode, sb.st_mode, blob,
2005 status == GOT_STATUS_MISSING, 0, 0,
2006 status == GOT_STATUS_UNVERSIONED, repo,
2007 progress_cb, progress_arg);
2009 if (err)
2010 goto done;
2012 if (ie) {
2013 err = got_fileindex_entry_update(ie,
2014 worktree->root_fd, path, blob->id.sha1,
2015 worktree->base_commit_id->sha1, 1);
2016 } else {
2017 err = create_fileindex_entry(&ie, fileindex,
2018 worktree->base_commit_id, worktree->root_fd, path,
2019 &blob->id);
2021 if (err)
2022 goto done;
2024 if (is_bad_symlink) {
2025 got_fileindex_entry_filetype_set(ie,
2026 GOT_FILEIDX_MODE_BAD_SYMLINK);
2030 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2031 err = got_error_from_errno("close");
2032 goto done;
2034 got_object_blob_close(blob);
2035 done:
2036 free(ondisk_path);
2037 return err;
2040 static const struct got_error *
2041 remove_ondisk_file(const char *root_path, const char *path)
2043 const struct got_error *err = NULL;
2044 char *ondisk_path = NULL, *parent = NULL;
2046 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2047 return got_error_from_errno("asprintf");
2049 if (unlink(ondisk_path) == -1) {
2050 if (errno != ENOENT)
2051 err = got_error_from_errno2("unlink", ondisk_path);
2052 } else {
2053 size_t root_len = strlen(root_path);
2054 err = got_path_dirname(&parent, ondisk_path);
2055 if (err)
2056 goto done;
2057 while (got_path_cmp(parent, root_path,
2058 strlen(parent), root_len) != 0) {
2059 free(ondisk_path);
2060 ondisk_path = parent;
2061 parent = NULL;
2062 if (rmdir(ondisk_path) == -1) {
2063 if (errno != ENOTEMPTY)
2064 err = got_error_from_errno2("rmdir",
2065 ondisk_path);
2066 break;
2068 err = got_path_dirname(&parent, ondisk_path);
2069 if (err)
2070 break;
2073 done:
2074 free(ondisk_path);
2075 free(parent);
2076 return err;
2079 static const struct got_error *
2080 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2081 struct got_fileindex_entry *ie, struct got_repository *repo,
2082 got_worktree_checkout_cb progress_cb, void *progress_arg)
2084 const struct got_error *err = NULL;
2085 unsigned char status;
2086 struct stat sb;
2087 char *ondisk_path;
2089 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2090 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2092 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2093 == -1)
2094 return got_error_from_errno("asprintf");
2096 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2097 if (err)
2098 goto done;
2100 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2101 char ondisk_target[PATH_MAX];
2102 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2103 sizeof(ondisk_target));
2104 if (ondisk_len == -1) {
2105 err = got_error_from_errno2("readlink", ondisk_path);
2106 goto done;
2108 ondisk_target[ondisk_len] = '\0';
2109 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2110 NULL, NULL, /* XXX pass common ancestor info? */
2111 ondisk_target, ondisk_path);
2112 if (err)
2113 goto done;
2114 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2115 ie->path);
2116 goto done;
2119 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2120 status == GOT_STATUS_ADD) {
2121 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2122 if (err)
2123 goto done;
2125 * Preserve the working file and change the deleted blob's
2126 * entry into a schedule-add entry.
2128 err = got_fileindex_entry_update(ie, worktree->root_fd,
2129 ie->path, NULL, NULL, 0);
2130 } else {
2131 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2132 if (err)
2133 goto done;
2134 if (status == GOT_STATUS_NO_CHANGE) {
2135 err = remove_ondisk_file(worktree->root_path, ie->path);
2136 if (err)
2137 goto done;
2139 got_fileindex_entry_remove(fileindex, ie);
2141 done:
2142 free(ondisk_path);
2143 return err;
2146 struct diff_cb_arg {
2147 struct got_fileindex *fileindex;
2148 struct got_worktree *worktree;
2149 struct got_repository *repo;
2150 got_worktree_checkout_cb progress_cb;
2151 void *progress_arg;
2152 got_cancel_cb cancel_cb;
2153 void *cancel_arg;
2156 static const struct got_error *
2157 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2158 struct got_tree_entry *te, const char *parent_path)
2160 struct diff_cb_arg *a = arg;
2162 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2163 return got_error(GOT_ERR_CANCELLED);
2165 return update_blob(a->worktree, a->fileindex, ie, te,
2166 ie->path, a->repo, a->progress_cb, a->progress_arg);
2169 static const struct got_error *
2170 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2172 struct diff_cb_arg *a = arg;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 return delete_blob(a->worktree, a->fileindex, ie,
2178 a->repo, a->progress_cb, a->progress_arg);
2181 static const struct got_error *
2182 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2184 struct diff_cb_arg *a = arg;
2185 const struct got_error *err;
2186 char *path;
2188 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2189 return got_error(GOT_ERR_CANCELLED);
2191 if (got_object_tree_entry_is_submodule(te))
2192 return NULL;
2194 if (asprintf(&path, "%s%s%s", parent_path,
2195 parent_path[0] ? "/" : "", te->name)
2196 == -1)
2197 return got_error_from_errno("asprintf");
2199 if (S_ISDIR(te->mode))
2200 err = add_dir_on_disk(a->worktree, path);
2201 else
2202 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2203 a->repo, a->progress_cb, a->progress_arg);
2205 free(path);
2206 return err;
2209 const struct got_error *
2210 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2212 uint32_t uuid_status;
2214 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2215 if (uuid_status != uuid_s_ok) {
2216 *uuidstr = NULL;
2217 return got_error_uuid(uuid_status, "uuid_to_string");
2220 return NULL;
2223 static const struct got_error *
2224 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2226 const struct got_error *err = NULL;
2227 char *uuidstr = NULL;
2229 *refname = NULL;
2231 err = got_worktree_get_uuid(&uuidstr, worktree);
2232 if (err)
2233 return err;
2235 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2236 err = got_error_from_errno("asprintf");
2237 *refname = NULL;
2239 free(uuidstr);
2240 return err;
2243 const struct got_error *
2244 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2246 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2249 static const struct got_error *
2250 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2252 return get_ref_name(refname, worktree,
2253 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2256 static const struct got_error *
2257 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2259 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2269 static const struct got_error *
2270 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2290 static const struct got_error *
2291 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2293 return get_ref_name(refname, worktree,
2294 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2297 static const struct got_error *
2298 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2300 return get_ref_name(refname, worktree,
2301 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2304 const struct got_error *
2305 got_worktree_get_histedit_script_path(char **path,
2306 struct got_worktree *worktree)
2308 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2309 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2310 *path = NULL;
2311 return got_error_from_errno("asprintf");
2313 return NULL;
2316 static const struct got_error *
2317 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree,
2320 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2323 static const struct got_error *
2324 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree,
2327 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2331 * Prevent Git's garbage collector from deleting our base commit by
2332 * setting a reference to our base commit's ID.
2334 static const struct got_error *
2335 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2337 const struct got_error *err = NULL;
2338 struct got_reference *ref = NULL;
2339 char *refname;
2341 err = got_worktree_get_base_ref_name(&refname, worktree);
2342 if (err)
2343 return err;
2345 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2346 if (err)
2347 goto done;
2349 err = got_ref_write(ref, repo);
2350 done:
2351 free(refname);
2352 if (ref)
2353 got_ref_close(ref);
2354 return err;
2357 static const struct got_error *
2358 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2360 const struct got_error *err = NULL;
2362 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2363 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2364 err = got_error_from_errno("asprintf");
2365 *fileindex_path = NULL;
2367 return err;
2371 static const struct got_error *
2372 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2373 struct got_worktree *worktree)
2375 const struct got_error *err = NULL;
2376 FILE *index = NULL;
2378 *fileindex_path = NULL;
2379 *fileindex = got_fileindex_alloc();
2380 if (*fileindex == NULL)
2381 return got_error_from_errno("got_fileindex_alloc");
2383 err = get_fileindex_path(fileindex_path, worktree);
2384 if (err)
2385 goto done;
2387 index = fopen(*fileindex_path, "rbe");
2388 if (index == NULL) {
2389 if (errno != ENOENT)
2390 err = got_error_from_errno2("fopen", *fileindex_path);
2391 } else {
2392 err = got_fileindex_read(*fileindex, index);
2393 if (fclose(index) == EOF && err == NULL)
2394 err = got_error_from_errno("fclose");
2396 done:
2397 if (err) {
2398 free(*fileindex_path);
2399 *fileindex_path = NULL;
2400 got_fileindex_free(*fileindex);
2401 *fileindex = NULL;
2403 return err;
2406 struct bump_base_commit_id_arg {
2407 struct got_object_id *base_commit_id;
2408 const char *path;
2409 size_t path_len;
2410 const char *entry_name;
2411 got_worktree_checkout_cb progress_cb;
2412 void *progress_arg;
2415 /* Bump base commit ID of all files within an updated part of the work tree. */
2416 static const struct got_error *
2417 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2419 const struct got_error *err;
2420 struct bump_base_commit_id_arg *a = arg;
2422 if (a->entry_name) {
2423 if (strcmp(ie->path, a->path) != 0)
2424 return NULL;
2425 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2426 return NULL;
2428 if (got_fileindex_entry_was_skipped(ie))
2429 return NULL;
2431 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2432 SHA1_DIGEST_LENGTH) == 0)
2433 return NULL;
2435 if (a->progress_cb) {
2436 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2437 ie->path);
2438 if (err)
2439 return err;
2441 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2442 return NULL;
2445 static const struct got_error *
2446 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2447 struct got_fileindex *fileindex,
2448 got_worktree_checkout_cb progress_cb, void *progress_arg)
2450 struct bump_base_commit_id_arg bbc_arg;
2452 bbc_arg.base_commit_id = worktree->base_commit_id;
2453 bbc_arg.entry_name = NULL;
2454 bbc_arg.path = "";
2455 bbc_arg.path_len = 0;
2456 bbc_arg.progress_cb = progress_cb;
2457 bbc_arg.progress_arg = progress_arg;
2459 return got_fileindex_for_each_entry_safe(fileindex,
2460 bump_base_commit_id, &bbc_arg);
2463 static const struct got_error *
2464 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2466 const struct got_error *err = NULL;
2467 char *new_fileindex_path = NULL;
2468 FILE *new_index = NULL;
2469 struct timespec timeout;
2471 err = got_opentemp_named(&new_fileindex_path, &new_index,
2472 fileindex_path);
2473 if (err)
2474 goto done;
2476 err = got_fileindex_write(fileindex, new_index);
2477 if (err)
2478 goto done;
2480 if (rename(new_fileindex_path, fileindex_path) != 0) {
2481 err = got_error_from_errno3("rename", new_fileindex_path,
2482 fileindex_path);
2483 unlink(new_fileindex_path);
2487 * Sleep for a short amount of time to ensure that files modified after
2488 * this program exits have a different time stamp from the one which
2489 * was recorded in the file index.
2491 timeout.tv_sec = 0;
2492 timeout.tv_nsec = 1;
2493 nanosleep(&timeout, NULL);
2494 done:
2495 if (new_index)
2496 fclose(new_index);
2497 free(new_fileindex_path);
2498 return err;
2501 static const struct got_error *
2502 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2503 struct got_object_id **tree_id, const char *wt_relpath,
2504 struct got_commit_object *base_commit, struct got_worktree *worktree,
2505 struct got_repository *repo)
2507 const struct got_error *err = NULL;
2508 struct got_object_id *id = NULL;
2509 char *in_repo_path = NULL;
2510 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2512 *entry_type = GOT_OBJ_TYPE_ANY;
2513 *tree_relpath = NULL;
2514 *tree_id = NULL;
2516 if (wt_relpath[0] == '\0') {
2517 /* Check out all files within the work tree. */
2518 *entry_type = GOT_OBJ_TYPE_TREE;
2519 *tree_relpath = strdup("");
2520 if (*tree_relpath == NULL) {
2521 err = got_error_from_errno("strdup");
2522 goto done;
2524 err = got_object_id_by_path(tree_id, repo, base_commit,
2525 worktree->path_prefix);
2526 if (err)
2527 goto done;
2528 return NULL;
2531 /* Check out a subset of files in the work tree. */
2533 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2534 is_root_wt ? "" : "/", wt_relpath) == -1) {
2535 err = got_error_from_errno("asprintf");
2536 goto done;
2539 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2540 if (err)
2541 goto done;
2543 free(in_repo_path);
2544 in_repo_path = NULL;
2546 err = got_object_get_type(entry_type, repo, id);
2547 if (err)
2548 goto done;
2550 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2551 /* Check out a single file. */
2552 if (strchr(wt_relpath, '/') == NULL) {
2553 /* Check out a single file in work tree's root dir. */
2554 in_repo_path = strdup(worktree->path_prefix);
2555 if (in_repo_path == NULL) {
2556 err = got_error_from_errno("strdup");
2557 goto done;
2559 *tree_relpath = strdup("");
2560 if (*tree_relpath == NULL) {
2561 err = got_error_from_errno("strdup");
2562 goto done;
2564 } else {
2565 /* Check out a single file in a subdirectory. */
2566 err = got_path_dirname(tree_relpath, wt_relpath);
2567 if (err)
2568 return err;
2569 if (asprintf(&in_repo_path, "%s%s%s",
2570 worktree->path_prefix, is_root_wt ? "" : "/",
2571 *tree_relpath) == -1) {
2572 err = got_error_from_errno("asprintf");
2573 goto done;
2576 err = got_object_id_by_path(tree_id, repo,
2577 base_commit, in_repo_path);
2578 } else {
2579 /* Check out all files within a subdirectory. */
2580 *tree_id = got_object_id_dup(id);
2581 if (*tree_id == NULL) {
2582 err = got_error_from_errno("got_object_id_dup");
2583 goto done;
2585 *tree_relpath = strdup(wt_relpath);
2586 if (*tree_relpath == NULL) {
2587 err = got_error_from_errno("strdup");
2588 goto done;
2591 done:
2592 free(id);
2593 free(in_repo_path);
2594 if (err) {
2595 *entry_type = GOT_OBJ_TYPE_ANY;
2596 free(*tree_relpath);
2597 *tree_relpath = NULL;
2598 free(*tree_id);
2599 *tree_id = NULL;
2601 return err;
2604 static const struct got_error *
2605 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2606 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2607 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2608 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2610 const struct got_error *err = NULL;
2611 struct got_commit_object *commit = NULL;
2612 struct got_tree_object *tree = NULL;
2613 struct got_fileindex_diff_tree_cb diff_cb;
2614 struct diff_cb_arg arg;
2616 err = ref_base_commit(worktree, repo);
2617 if (err) {
2618 if (!(err->code == GOT_ERR_ERRNO &&
2619 (errno == EACCES || errno == EROFS)))
2620 goto done;
2621 err = (*progress_cb)(progress_arg,
2622 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2623 if (err)
2624 return err;
2627 err = got_object_open_as_commit(&commit, repo,
2628 worktree->base_commit_id);
2629 if (err)
2630 goto done;
2632 err = got_object_open_as_tree(&tree, repo, tree_id);
2633 if (err)
2634 goto done;
2636 if (entry_name &&
2637 got_object_tree_find_entry(tree, entry_name) == NULL) {
2638 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2639 goto done;
2642 diff_cb.diff_old_new = diff_old_new;
2643 diff_cb.diff_old = diff_old;
2644 diff_cb.diff_new = diff_new;
2645 arg.fileindex = fileindex;
2646 arg.worktree = worktree;
2647 arg.repo = repo;
2648 arg.progress_cb = progress_cb;
2649 arg.progress_arg = progress_arg;
2650 arg.cancel_cb = cancel_cb;
2651 arg.cancel_arg = cancel_arg;
2652 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2653 entry_name, repo, &diff_cb, &arg);
2654 done:
2655 if (tree)
2656 got_object_tree_close(tree);
2657 if (commit)
2658 got_object_commit_close(commit);
2659 return err;
2662 const struct got_error *
2663 got_worktree_checkout_files(struct got_worktree *worktree,
2664 struct got_pathlist_head *paths, struct got_repository *repo,
2665 got_worktree_checkout_cb progress_cb, void *progress_arg,
2666 got_cancel_cb cancel_cb, void *cancel_arg)
2668 const struct got_error *err = NULL, *sync_err, *unlockerr;
2669 struct got_commit_object *commit = NULL;
2670 struct got_tree_object *tree = NULL;
2671 struct got_fileindex *fileindex = NULL;
2672 char *fileindex_path = NULL;
2673 struct got_pathlist_entry *pe;
2674 struct tree_path_data {
2675 STAILQ_ENTRY(tree_path_data) entry;
2676 struct got_object_id *tree_id;
2677 int entry_type;
2678 char *relpath;
2679 char *entry_name;
2680 } *tpd = NULL;
2681 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2683 STAILQ_INIT(&tree_paths);
2685 err = lock_worktree(worktree, LOCK_EX);
2686 if (err)
2687 return err;
2689 err = got_object_open_as_commit(&commit, repo,
2690 worktree->base_commit_id);
2691 if (err)
2692 goto done;
2694 /* Map all specified paths to in-repository trees. */
2695 TAILQ_FOREACH(pe, paths, entry) {
2696 tpd = malloc(sizeof(*tpd));
2697 if (tpd == NULL) {
2698 err = got_error_from_errno("malloc");
2699 goto done;
2702 err = find_tree_entry_for_checkout(&tpd->entry_type,
2703 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2704 worktree, repo);
2705 if (err) {
2706 free(tpd);
2707 goto done;
2710 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2711 err = got_path_basename(&tpd->entry_name, pe->path);
2712 if (err) {
2713 free(tpd->relpath);
2714 free(tpd->tree_id);
2715 free(tpd);
2716 goto done;
2718 } else
2719 tpd->entry_name = NULL;
2721 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2725 * Read the file index.
2726 * Checking out files is supposed to be an idempotent operation.
2727 * If the on-disk file index is incomplete we will try to complete it.
2729 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2730 if (err)
2731 goto done;
2733 tpd = STAILQ_FIRST(&tree_paths);
2734 TAILQ_FOREACH(pe, paths, entry) {
2735 struct bump_base_commit_id_arg bbc_arg;
2737 err = checkout_files(worktree, fileindex, tpd->relpath,
2738 tpd->tree_id, tpd->entry_name, repo,
2739 progress_cb, progress_arg, cancel_cb, cancel_arg);
2740 if (err)
2741 break;
2743 bbc_arg.base_commit_id = worktree->base_commit_id;
2744 bbc_arg.entry_name = tpd->entry_name;
2745 bbc_arg.path = pe->path;
2746 bbc_arg.path_len = pe->path_len;
2747 bbc_arg.progress_cb = progress_cb;
2748 bbc_arg.progress_arg = progress_arg;
2749 err = got_fileindex_for_each_entry_safe(fileindex,
2750 bump_base_commit_id, &bbc_arg);
2751 if (err)
2752 break;
2754 tpd = STAILQ_NEXT(tpd, entry);
2756 sync_err = sync_fileindex(fileindex, fileindex_path);
2757 if (sync_err && err == NULL)
2758 err = sync_err;
2759 done:
2760 free(fileindex_path);
2761 if (tree)
2762 got_object_tree_close(tree);
2763 if (commit)
2764 got_object_commit_close(commit);
2765 if (fileindex)
2766 got_fileindex_free(fileindex);
2767 while (!STAILQ_EMPTY(&tree_paths)) {
2768 tpd = STAILQ_FIRST(&tree_paths);
2769 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2770 free(tpd->relpath);
2771 free(tpd->tree_id);
2772 free(tpd);
2774 unlockerr = lock_worktree(worktree, LOCK_SH);
2775 if (unlockerr && err == NULL)
2776 err = unlockerr;
2777 return err;
2780 struct merge_file_cb_arg {
2781 struct got_worktree *worktree;
2782 struct got_fileindex *fileindex;
2783 got_worktree_checkout_cb progress_cb;
2784 void *progress_arg;
2785 got_cancel_cb cancel_cb;
2786 void *cancel_arg;
2787 const char *label_orig;
2788 struct got_object_id *commit_id2;
2789 int allow_bad_symlinks;
2792 static const struct got_error *
2793 merge_file_cb(void *arg, struct got_blob_object *blob1,
2794 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2795 struct got_object_id *id1, struct got_object_id *id2,
2796 const char *path1, const char *path2,
2797 mode_t mode1, mode_t mode2, struct got_repository *repo)
2799 static const struct got_error *err = NULL;
2800 struct merge_file_cb_arg *a = arg;
2801 struct got_fileindex_entry *ie;
2802 char *ondisk_path = NULL;
2803 struct stat sb;
2804 unsigned char status;
2805 int local_changes_subsumed;
2806 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2807 char *id_str = NULL, *label_deriv2 = NULL;
2809 if (blob1 && blob2) {
2810 ie = got_fileindex_entry_get(a->fileindex, path2,
2811 strlen(path2));
2812 if (ie == NULL)
2813 return (*a->progress_cb)(a->progress_arg,
2814 GOT_STATUS_MISSING, path2);
2816 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2817 path2) == -1)
2818 return got_error_from_errno("asprintf");
2820 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2821 repo);
2822 if (err)
2823 goto done;
2825 if (status == GOT_STATUS_DELETE) {
2826 err = (*a->progress_cb)(a->progress_arg,
2827 GOT_STATUS_MERGE, path2);
2828 goto done;
2830 if (status != GOT_STATUS_NO_CHANGE &&
2831 status != GOT_STATUS_MODIFY &&
2832 status != GOT_STATUS_CONFLICT &&
2833 status != GOT_STATUS_ADD) {
2834 err = (*a->progress_cb)(a->progress_arg, status, path2);
2835 goto done;
2838 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2839 char *link_target2;
2840 err = got_object_blob_read_to_str(&link_target2, blob2);
2841 if (err)
2842 goto done;
2843 err = merge_symlink(a->worktree, blob1, ondisk_path,
2844 path2, a->label_orig, link_target2, a->commit_id2,
2845 repo, a->progress_cb, a->progress_arg);
2846 free(link_target2);
2847 } else {
2848 int fd;
2850 f_orig = got_opentemp();
2851 if (f_orig == NULL) {
2852 err = got_error_from_errno("got_opentemp");
2853 goto done;
2855 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2856 f_orig, blob1);
2857 if (err)
2858 goto done;
2860 f_deriv2 = got_opentemp();
2861 if (f_deriv2 == NULL)
2862 goto done;
2863 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2864 f_deriv2, blob2);
2865 if (err)
2866 goto done;
2868 fd = open(ondisk_path,
2869 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2870 if (fd == -1) {
2871 err = got_error_from_errno2("open",
2872 ondisk_path);
2873 goto done;
2875 f_deriv = fdopen(fd, "r");
2876 if (f_deriv == NULL) {
2877 err = got_error_from_errno2("fdopen",
2878 ondisk_path);
2879 close(fd);
2880 goto done;
2882 err = got_object_id_str(&id_str, a->commit_id2);
2883 if (err)
2884 goto done;
2885 if (asprintf(&label_deriv2, "%s: commit %s",
2886 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2887 err = got_error_from_errno("asprintf");
2888 goto done;
2890 err = merge_file(&local_changes_subsumed, a->worktree,
2891 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2892 sb.st_mode, a->label_orig, NULL, label_deriv2,
2893 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2894 a->progress_cb, a->progress_arg);
2896 } else if (blob1) {
2897 ie = got_fileindex_entry_get(a->fileindex, path1,
2898 strlen(path1));
2899 if (ie == NULL)
2900 return (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_MISSING, path1);
2903 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2904 path1) == -1)
2905 return got_error_from_errno("asprintf");
2907 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2908 repo);
2909 if (err)
2910 goto done;
2912 switch (status) {
2913 case GOT_STATUS_NO_CHANGE:
2914 err = (*a->progress_cb)(a->progress_arg,
2915 GOT_STATUS_DELETE, path1);
2916 if (err)
2917 goto done;
2918 err = remove_ondisk_file(a->worktree->root_path, path1);
2919 if (err)
2920 goto done;
2921 if (ie)
2922 got_fileindex_entry_mark_deleted_from_disk(ie);
2923 break;
2924 case GOT_STATUS_DELETE:
2925 case GOT_STATUS_MISSING:
2926 err = (*a->progress_cb)(a->progress_arg,
2927 GOT_STATUS_DELETE, path1);
2928 if (err)
2929 goto done;
2930 if (ie)
2931 got_fileindex_entry_mark_deleted_from_disk(ie);
2932 break;
2933 case GOT_STATUS_ADD: {
2934 struct got_object_id *id;
2935 FILE *blob1_f;
2936 off_t blob1_size;
2938 * Delete the added file only if its content already
2939 * exists in the repository.
2941 err = got_object_blob_file_create(&id, &blob1_f,
2942 &blob1_size, path1);
2943 if (err)
2944 goto done;
2945 if (got_object_id_cmp(id, id1) == 0) {
2946 err = (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_DELETE, path1);
2948 if (err)
2949 goto done;
2950 err = remove_ondisk_file(a->worktree->root_path,
2951 path1);
2952 if (err)
2953 goto done;
2954 if (ie)
2955 got_fileindex_entry_remove(a->fileindex,
2956 ie);
2957 } else {
2958 err = (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_CANNOT_DELETE, path1);
2961 if (fclose(blob1_f) == EOF && err == NULL)
2962 err = got_error_from_errno("fclose");
2963 free(id);
2964 if (err)
2965 goto done;
2966 break;
2968 case GOT_STATUS_MODIFY:
2969 case GOT_STATUS_CONFLICT:
2970 err = (*a->progress_cb)(a->progress_arg,
2971 GOT_STATUS_CANNOT_DELETE, path1);
2972 if (err)
2973 goto done;
2974 break;
2975 case GOT_STATUS_OBSTRUCTED:
2976 err = (*a->progress_cb)(a->progress_arg, status, path1);
2977 if (err)
2978 goto done;
2979 break;
2980 default:
2981 break;
2983 } else if (blob2) {
2984 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2985 path2) == -1)
2986 return got_error_from_errno("asprintf");
2987 ie = got_fileindex_entry_get(a->fileindex, path2,
2988 strlen(path2));
2989 if (ie) {
2990 err = get_file_status(&status, &sb, ie, ondisk_path,
2991 -1, NULL, repo);
2992 if (err)
2993 goto done;
2994 if (status != GOT_STATUS_NO_CHANGE &&
2995 status != GOT_STATUS_MODIFY &&
2996 status != GOT_STATUS_CONFLICT &&
2997 status != GOT_STATUS_ADD) {
2998 err = (*a->progress_cb)(a->progress_arg,
2999 status, path2);
3000 goto done;
3002 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3003 char *link_target2;
3004 err = got_object_blob_read_to_str(&link_target2,
3005 blob2);
3006 if (err)
3007 goto done;
3008 err = merge_symlink(a->worktree, NULL,
3009 ondisk_path, path2, a->label_orig,
3010 link_target2, a->commit_id2, repo,
3011 a->progress_cb, a->progress_arg);
3012 free(link_target2);
3013 } else if (S_ISREG(sb.st_mode)) {
3014 err = merge_blob(&local_changes_subsumed,
3015 a->worktree, NULL, ondisk_path, path2,
3016 sb.st_mode, a->label_orig, blob2,
3017 a->commit_id2, repo, a->progress_cb,
3018 a->progress_arg);
3019 } else {
3020 err = got_error_path(ondisk_path,
3021 GOT_ERR_FILE_OBSTRUCTED);
3023 if (err)
3024 goto done;
3025 if (status == GOT_STATUS_DELETE) {
3026 err = got_fileindex_entry_update(ie,
3027 a->worktree->root_fd, path2, blob2->id.sha1,
3028 a->worktree->base_commit_id->sha1, 0);
3029 if (err)
3030 goto done;
3032 } else {
3033 int is_bad_symlink = 0;
3034 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3035 if (S_ISLNK(mode2)) {
3036 err = install_symlink(&is_bad_symlink,
3037 a->worktree, ondisk_path, path2, blob2, 0,
3038 0, 1, a->allow_bad_symlinks, repo,
3039 a->progress_cb, a->progress_arg);
3040 } else {
3041 err = install_blob(a->worktree, ondisk_path, path2,
3042 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3043 a->progress_cb, a->progress_arg);
3045 if (err)
3046 goto done;
3047 err = got_fileindex_entry_alloc(&ie, path2);
3048 if (err)
3049 goto done;
3050 err = got_fileindex_entry_update(ie,
3051 a->worktree->root_fd, path2, NULL, NULL, 1);
3052 if (err) {
3053 got_fileindex_entry_free(ie);
3054 goto done;
3056 err = got_fileindex_entry_add(a->fileindex, ie);
3057 if (err) {
3058 got_fileindex_entry_free(ie);
3059 goto done;
3061 if (is_bad_symlink) {
3062 got_fileindex_entry_filetype_set(ie,
3063 GOT_FILEIDX_MODE_BAD_SYMLINK);
3067 done:
3068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3069 err = got_error_from_errno("fclose");
3070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3071 err = got_error_from_errno("fclose");
3072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3073 err = got_error_from_errno("fclose");
3074 free(id_str);
3075 free(label_deriv2);
3076 free(ondisk_path);
3077 return err;
3080 static const struct got_error *
3081 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3083 struct got_worktree *worktree = arg;
3085 /* Reject merges into a work tree with mixed base commits. */
3086 if (got_fileindex_entry_has_commit(ie) &&
3087 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3088 SHA1_DIGEST_LENGTH) != 0)
3089 return got_error(GOT_ERR_MIXED_COMMITS);
3091 return NULL;
3094 struct check_merge_conflicts_arg {
3095 struct got_worktree *worktree;
3096 struct got_fileindex *fileindex;
3097 struct got_repository *repo;
3100 static const struct got_error *
3101 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3102 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3103 struct got_object_id *id1, struct got_object_id *id2,
3104 const char *path1, const char *path2,
3105 mode_t mode1, mode_t mode2, struct got_repository *repo)
3107 const struct got_error *err = NULL;
3108 struct check_merge_conflicts_arg *a = arg;
3109 unsigned char status;
3110 struct stat sb;
3111 struct got_fileindex_entry *ie;
3112 const char *path = path2 ? path2 : path1;
3113 struct got_object_id *id = id2 ? id2 : id1;
3114 char *ondisk_path;
3116 if (id == NULL)
3117 return NULL;
3119 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3120 if (ie == NULL)
3121 return NULL;
3123 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3124 == -1)
3125 return got_error_from_errno("asprintf");
3127 /* Reject merges into a work tree with conflicted files. */
3128 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3129 free(ondisk_path);
3130 if (err)
3131 return err;
3132 if (status == GOT_STATUS_CONFLICT)
3133 return got_error(GOT_ERR_CONFLICTS);
3135 return NULL;
3138 static const struct got_error *
3139 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3140 const char *fileindex_path, struct got_object_id *commit_id1,
3141 struct got_object_id *commit_id2, struct got_repository *repo,
3142 got_worktree_checkout_cb progress_cb, void *progress_arg,
3143 got_cancel_cb cancel_cb, void *cancel_arg)
3145 const struct got_error *err = NULL, *sync_err;
3146 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3147 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3148 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3149 struct check_merge_conflicts_arg cmc_arg;
3150 struct merge_file_cb_arg arg;
3151 char *label_orig = NULL;
3152 FILE *f1 = NULL, *f2 = NULL;
3153 int fd1 = -1, fd2 = -1;
3155 if (commit_id1) {
3156 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3157 if (err)
3158 goto done;
3159 err = got_object_id_by_path(&tree_id1, repo, commit1,
3160 worktree->path_prefix);
3161 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3162 goto done;
3164 if (tree_id1) {
3165 char *id_str;
3167 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3168 if (err)
3169 goto done;
3171 err = got_object_id_str(&id_str, commit_id1);
3172 if (err)
3173 goto done;
3175 if (asprintf(&label_orig, "%s: commit %s",
3176 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3177 err = got_error_from_errno("asprintf");
3178 free(id_str);
3179 goto done;
3181 free(id_str);
3183 f1 = got_opentemp();
3184 if (f1 == NULL) {
3185 err = got_error_from_errno("got_opentemp");
3186 goto done;
3190 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3191 if (err)
3192 goto done;
3194 err = got_object_id_by_path(&tree_id2, repo, commit2,
3195 worktree->path_prefix);
3196 if (err)
3197 goto done;
3199 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3200 if (err)
3201 goto done;
3203 f2 = got_opentemp();
3204 if (f2 == NULL) {
3205 err = got_error_from_errno("got_opentemp");
3206 goto done;
3209 fd1 = got_opentempfd();
3210 if (fd1 == -1) {
3211 err = got_error_from_errno("got_opentempfd");
3212 goto done;
3215 fd2 = got_opentempfd();
3216 if (fd2 == -1) {
3217 err = got_error_from_errno("got_opentempfd");
3218 goto done;
3221 cmc_arg.worktree = worktree;
3222 cmc_arg.fileindex = fileindex;
3223 cmc_arg.repo = repo;
3224 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 check_merge_conflicts, &cmc_arg, 0);
3226 if (err)
3227 goto done;
3229 arg.worktree = worktree;
3230 arg.fileindex = fileindex;
3231 arg.progress_cb = progress_cb;
3232 arg.progress_arg = progress_arg;
3233 arg.cancel_cb = cancel_cb;
3234 arg.cancel_arg = cancel_arg;
3235 arg.label_orig = label_orig;
3236 arg.commit_id2 = commit_id2;
3237 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3238 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3239 merge_file_cb, &arg, 1);
3240 sync_err = sync_fileindex(fileindex, fileindex_path);
3241 if (sync_err && err == NULL)
3242 err = sync_err;
3243 done:
3244 if (commit1)
3245 got_object_commit_close(commit1);
3246 if (commit2)
3247 got_object_commit_close(commit2);
3248 if (tree1)
3249 got_object_tree_close(tree1);
3250 if (tree2)
3251 got_object_tree_close(tree2);
3252 if (f1 && fclose(f1) == EOF && err == NULL)
3253 err = got_error_from_errno("fclose");
3254 if (f2 && fclose(f2) == EOF && err == NULL)
3255 err = got_error_from_errno("fclose");
3256 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3257 err = got_error_from_errno("close");
3258 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3259 err = got_error_from_errno("close");
3260 free(label_orig);
3261 return err;
3264 const struct got_error *
3265 got_worktree_merge_files(struct got_worktree *worktree,
3266 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3267 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3268 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3270 const struct got_error *err, *unlockerr;
3271 char *fileindex_path = NULL;
3272 struct got_fileindex *fileindex = NULL;
3274 err = lock_worktree(worktree, LOCK_EX);
3275 if (err)
3276 return err;
3278 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3279 if (err)
3280 goto done;
3282 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3283 worktree);
3284 if (err)
3285 goto done;
3287 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3288 commit_id2, repo, progress_cb, progress_arg,
3289 cancel_cb, cancel_arg);
3290 done:
3291 if (fileindex)
3292 got_fileindex_free(fileindex);
3293 free(fileindex_path);
3294 unlockerr = lock_worktree(worktree, LOCK_SH);
3295 if (unlockerr && err == NULL)
3296 err = unlockerr;
3297 return err;
3300 struct diff_dir_cb_arg {
3301 struct got_fileindex *fileindex;
3302 struct got_worktree *worktree;
3303 const char *status_path;
3304 size_t status_path_len;
3305 struct got_repository *repo;
3306 got_worktree_status_cb status_cb;
3307 void *status_arg;
3308 got_cancel_cb cancel_cb;
3309 void *cancel_arg;
3310 /* A pathlist containing per-directory pathlists of ignore patterns. */
3311 struct got_pathlist_head *ignores;
3312 int report_unchanged;
3313 int no_ignores;
3316 static const struct got_error *
3317 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3318 int dirfd, const char *de_name,
3319 got_worktree_status_cb status_cb, void *status_arg,
3320 struct got_repository *repo, int report_unchanged)
3322 const struct got_error *err = NULL;
3323 unsigned char status = GOT_STATUS_NO_CHANGE;
3324 unsigned char staged_status = get_staged_status(ie);
3325 struct stat sb;
3326 struct got_object_id blob_id, commit_id, staged_blob_id;
3327 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3328 struct got_object_id *staged_blob_idp = NULL;
3330 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3331 if (err)
3332 return err;
3334 if (status == GOT_STATUS_NO_CHANGE &&
3335 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3336 return NULL;
3338 if (got_fileindex_entry_has_blob(ie)) {
3339 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3340 blob_idp = &blob_id;
3342 if (got_fileindex_entry_has_commit(ie)) {
3343 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3344 commit_idp = &commit_id;
3346 if (staged_status == GOT_STATUS_ADD ||
3347 staged_status == GOT_STATUS_MODIFY) {
3348 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3349 SHA1_DIGEST_LENGTH);
3350 staged_blob_idp = &staged_blob_id;
3353 return (*status_cb)(status_arg, status, staged_status,
3354 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3357 static const struct got_error *
3358 status_old_new(void *arg, struct got_fileindex_entry *ie,
3359 struct dirent *de, const char *parent_path, int dirfd)
3361 const struct got_error *err = NULL;
3362 struct diff_dir_cb_arg *a = arg;
3363 char *abspath;
3365 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3366 return got_error(GOT_ERR_CANCELLED);
3368 if (got_path_cmp(parent_path, a->status_path,
3369 strlen(parent_path), a->status_path_len) != 0 &&
3370 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3371 return NULL;
3373 if (parent_path[0]) {
3374 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3375 parent_path, de->d_name) == -1)
3376 return got_error_from_errno("asprintf");
3377 } else {
3378 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3379 de->d_name) == -1)
3380 return got_error_from_errno("asprintf");
3383 err = report_file_status(ie, abspath, dirfd, de->d_name,
3384 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3385 free(abspath);
3386 return err;
3389 static const struct got_error *
3390 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3392 struct diff_dir_cb_arg *a = arg;
3393 struct got_object_id blob_id, commit_id;
3394 unsigned char status;
3396 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3397 return got_error(GOT_ERR_CANCELLED);
3399 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3400 return NULL;
3402 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3403 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3404 if (got_fileindex_entry_has_file_on_disk(ie))
3405 status = GOT_STATUS_MISSING;
3406 else
3407 status = GOT_STATUS_DELETE;
3408 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3409 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3412 static void
3413 free_ignorelist(struct got_pathlist_head *ignorelist)
3415 struct got_pathlist_entry *pe;
3417 TAILQ_FOREACH(pe, ignorelist, entry)
3418 free((char *)pe->path);
3419 got_pathlist_free(ignorelist);
3422 static void
3423 free_ignores(struct got_pathlist_head *ignores)
3425 struct got_pathlist_entry *pe;
3427 TAILQ_FOREACH(pe, ignores, entry) {
3428 struct got_pathlist_head *ignorelist = pe->data;
3429 free_ignorelist(ignorelist);
3430 free((char *)pe->path);
3432 got_pathlist_free(ignores);
3435 static const struct got_error *
3436 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3438 const struct got_error *err = NULL;
3439 struct got_pathlist_entry *pe = NULL;
3440 struct got_pathlist_head *ignorelist;
3441 char *line = NULL, *pattern, *dirpath = NULL;
3442 size_t linesize = 0;
3443 ssize_t linelen;
3445 ignorelist = calloc(1, sizeof(*ignorelist));
3446 if (ignorelist == NULL)
3447 return got_error_from_errno("calloc");
3448 TAILQ_INIT(ignorelist);
3450 while ((linelen = getline(&line, &linesize, f)) != -1) {
3451 if (linelen > 0 && line[linelen - 1] == '\n')
3452 line[linelen - 1] = '\0';
3454 /* Git's ignores may contain comments. */
3455 if (line[0] == '#')
3456 continue;
3458 /* Git's negated patterns are not (yet?) supported. */
3459 if (line[0] == '!')
3460 continue;
3462 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3463 line) == -1) {
3464 err = got_error_from_errno("asprintf");
3465 goto done;
3467 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3468 if (err)
3469 goto done;
3471 if (ferror(f)) {
3472 err = got_error_from_errno("getline");
3473 goto done;
3476 dirpath = strdup(path);
3477 if (dirpath == NULL) {
3478 err = got_error_from_errno("strdup");
3479 goto done;
3481 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3482 done:
3483 free(line);
3484 if (err || pe == NULL) {
3485 free(dirpath);
3486 free_ignorelist(ignorelist);
3488 return err;
3491 static int
3492 match_ignores(struct got_pathlist_head *ignores, const char *path)
3494 struct got_pathlist_entry *pe;
3496 /* Handle patterns which match in all directories. */
3497 TAILQ_FOREACH(pe, ignores, entry) {
3498 struct got_pathlist_head *ignorelist = pe->data;
3499 struct got_pathlist_entry *pi;
3501 TAILQ_FOREACH(pi, ignorelist, entry) {
3502 const char *p, *pattern = pi->path;
3504 if (strncmp(pattern, "**/", 3) != 0)
3505 continue;
3506 pattern += 3;
3507 p = path;
3508 while (*p) {
3509 if (fnmatch(pattern, p,
3510 FNM_PATHNAME | FNM_LEADING_DIR)) {
3511 /* Retry in next directory. */
3512 while (*p && *p != '/')
3513 p++;
3514 while (*p == '/')
3515 p++;
3516 continue;
3518 return 1;
3524 * The ignores pathlist contains ignore lists from children before
3525 * parents, so we can find the most specific ignorelist by walking
3526 * ignores backwards.
3528 pe = TAILQ_LAST(ignores, got_pathlist_head);
3529 while (pe) {
3530 if (got_path_is_child(path, pe->path, pe->path_len)) {
3531 struct got_pathlist_head *ignorelist = pe->data;
3532 struct got_pathlist_entry *pi;
3533 TAILQ_FOREACH(pi, ignorelist, entry) {
3534 const char *pattern = pi->path;
3535 int flags = FNM_LEADING_DIR;
3536 if (strstr(pattern, "/**/") == NULL)
3537 flags |= FNM_PATHNAME;
3538 if (fnmatch(pattern, path, flags))
3539 continue;
3540 return 1;
3543 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3546 return 0;
3549 static const struct got_error *
3550 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3551 const char *path, int dirfd, const char *ignores_filename)
3553 const struct got_error *err = NULL;
3554 char *ignorespath;
3555 int fd = -1;
3556 FILE *ignoresfile = NULL;
3558 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3559 path[0] ? "/" : "", ignores_filename) == -1)
3560 return got_error_from_errno("asprintf");
3562 if (dirfd != -1) {
3563 fd = openat(dirfd, ignores_filename,
3564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3565 if (fd == -1) {
3566 if (errno != ENOENT && errno != EACCES)
3567 err = got_error_from_errno2("openat",
3568 ignorespath);
3569 } else {
3570 ignoresfile = fdopen(fd, "r");
3571 if (ignoresfile == NULL)
3572 err = got_error_from_errno2("fdopen",
3573 ignorespath);
3574 else {
3575 fd = -1;
3576 err = read_ignores(ignores, path, ignoresfile);
3579 } else {
3580 ignoresfile = fopen(ignorespath, "re");
3581 if (ignoresfile == NULL) {
3582 if (errno != ENOENT && errno != EACCES)
3583 err = got_error_from_errno2("fopen",
3584 ignorespath);
3585 } else
3586 err = read_ignores(ignores, path, ignoresfile);
3589 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3590 err = got_error_from_errno2("fclose", path);
3591 if (fd != -1 && close(fd) == -1 && err == NULL)
3592 err = got_error_from_errno2("close", path);
3593 free(ignorespath);
3594 return err;
3597 static const struct got_error *
3598 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3599 int dirfd)
3601 const struct got_error *err = NULL;
3602 struct diff_dir_cb_arg *a = arg;
3603 char *path = NULL;
3605 if (ignore != NULL)
3606 *ignore = 0;
3608 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3609 return got_error(GOT_ERR_CANCELLED);
3611 if (parent_path[0]) {
3612 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3613 return got_error_from_errno("asprintf");
3614 } else {
3615 path = de->d_name;
3618 if (de->d_type == DT_DIR) {
3619 if (!a->no_ignores && ignore != NULL &&
3620 match_ignores(a->ignores, path))
3621 *ignore = 1;
3622 } else if (!match_ignores(a->ignores, path) &&
3623 got_path_is_child(path, a->status_path, a->status_path_len))
3624 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3626 if (parent_path[0])
3627 free(path);
3628 return err;
3631 static const struct got_error *
3632 status_traverse(void *arg, const char *path, int dirfd)
3634 const struct got_error *err = NULL;
3635 struct diff_dir_cb_arg *a = arg;
3637 if (a->no_ignores)
3638 return NULL;
3640 err = add_ignores(a->ignores, a->worktree->root_path,
3641 path, dirfd, ".cvsignore");
3642 if (err)
3643 return err;
3645 err = add_ignores(a->ignores, a->worktree->root_path, path,
3646 dirfd, ".gitignore");
3648 return err;
3651 static const struct got_error *
3652 report_single_file_status(const char *path, const char *ondisk_path,
3653 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3654 void *status_arg, struct got_repository *repo, int report_unchanged,
3655 struct got_pathlist_head *ignores, int no_ignores)
3657 struct got_fileindex_entry *ie;
3658 struct stat sb;
3660 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3661 if (ie)
3662 return report_file_status(ie, ondisk_path, -1, NULL,
3663 status_cb, status_arg, repo, report_unchanged);
3665 if (lstat(ondisk_path, &sb) == -1) {
3666 if (errno != ENOENT)
3667 return got_error_from_errno2("lstat", ondisk_path);
3668 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3669 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3672 if (!no_ignores && match_ignores(ignores, path))
3673 return NULL;
3675 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3676 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3677 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3679 return NULL;
3682 static const struct got_error *
3683 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3684 const char *root_path, const char *path)
3686 const struct got_error *err;
3687 char *parent_path, *next_parent_path = NULL;
3689 err = add_ignores(ignores, root_path, "", -1,
3690 ".cvsignore");
3691 if (err)
3692 return err;
3694 err = add_ignores(ignores, root_path, "", -1,
3695 ".gitignore");
3696 if (err)
3697 return err;
3699 err = got_path_dirname(&parent_path, path);
3700 if (err) {
3701 if (err->code == GOT_ERR_BAD_PATH)
3702 return NULL; /* cannot traverse parent */
3703 return err;
3705 for (;;) {
3706 err = add_ignores(ignores, root_path, parent_path, -1,
3707 ".cvsignore");
3708 if (err)
3709 break;
3710 err = add_ignores(ignores, root_path, parent_path, -1,
3711 ".gitignore");
3712 if (err)
3713 break;
3714 err = got_path_dirname(&next_parent_path, parent_path);
3715 if (err) {
3716 if (err->code == GOT_ERR_BAD_PATH)
3717 err = NULL; /* traversed everything */
3718 break;
3720 if (got_path_is_root_dir(parent_path))
3721 break;
3722 free(parent_path);
3723 parent_path = next_parent_path;
3724 next_parent_path = NULL;
3727 free(parent_path);
3728 free(next_parent_path);
3729 return err;
3732 static const struct got_error *
3733 worktree_status(struct got_worktree *worktree, const char *path,
3734 struct got_fileindex *fileindex, struct got_repository *repo,
3735 got_worktree_status_cb status_cb, void *status_arg,
3736 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3737 int report_unchanged)
3739 const struct got_error *err = NULL;
3740 int fd = -1;
3741 struct got_fileindex_diff_dir_cb fdiff_cb;
3742 struct diff_dir_cb_arg arg;
3743 char *ondisk_path = NULL;
3744 struct got_pathlist_head ignores;
3745 struct got_fileindex_entry *ie;
3747 TAILQ_INIT(&ignores);
3749 if (asprintf(&ondisk_path, "%s%s%s",
3750 worktree->root_path, path[0] ? "/" : "", path) == -1)
3751 return got_error_from_errno("asprintf");
3753 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3754 if (ie) {
3755 err = report_single_file_status(path, ondisk_path,
3756 fileindex, status_cb, status_arg, repo,
3757 report_unchanged, &ignores, no_ignores);
3758 goto done;
3761 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3762 if (fd == -1) {
3763 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3764 !got_err_open_nofollow_on_symlink())
3765 err = got_error_from_errno2("open", ondisk_path);
3766 else {
3767 if (!no_ignores) {
3768 err = add_ignores_from_parent_paths(&ignores,
3769 worktree->root_path, ondisk_path);
3770 if (err)
3771 goto done;
3773 err = report_single_file_status(path, ondisk_path,
3774 fileindex, status_cb, status_arg, repo,
3775 report_unchanged, &ignores, no_ignores);
3777 } else {
3778 fdiff_cb.diff_old_new = status_old_new;
3779 fdiff_cb.diff_old = status_old;
3780 fdiff_cb.diff_new = status_new;
3781 fdiff_cb.diff_traverse = status_traverse;
3782 arg.fileindex = fileindex;
3783 arg.worktree = worktree;
3784 arg.status_path = path;
3785 arg.status_path_len = strlen(path);
3786 arg.repo = repo;
3787 arg.status_cb = status_cb;
3788 arg.status_arg = status_arg;
3789 arg.cancel_cb = cancel_cb;
3790 arg.cancel_arg = cancel_arg;
3791 arg.report_unchanged = report_unchanged;
3792 arg.no_ignores = no_ignores;
3793 if (!no_ignores) {
3794 err = add_ignores_from_parent_paths(&ignores,
3795 worktree->root_path, path);
3796 if (err)
3797 goto done;
3799 arg.ignores = &ignores;
3800 err = got_fileindex_diff_dir(fileindex, fd,
3801 worktree->root_path, path, repo, &fdiff_cb, &arg);
3803 done:
3804 free_ignores(&ignores);
3805 if (fd != -1 && close(fd) == -1 && err == NULL)
3806 err = got_error_from_errno("close");
3807 free(ondisk_path);
3808 return err;
3811 const struct got_error *
3812 got_worktree_status(struct got_worktree *worktree,
3813 struct got_pathlist_head *paths, struct got_repository *repo,
3814 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3815 got_cancel_cb cancel_cb, void *cancel_arg)
3817 const struct got_error *err = NULL;
3818 char *fileindex_path = NULL;
3819 struct got_fileindex *fileindex = NULL;
3820 struct got_pathlist_entry *pe;
3822 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3823 if (err)
3824 return err;
3826 TAILQ_FOREACH(pe, paths, entry) {
3827 err = worktree_status(worktree, pe->path, fileindex, repo,
3828 status_cb, status_arg, cancel_cb, cancel_arg,
3829 no_ignores, 0);
3830 if (err)
3831 break;
3833 free(fileindex_path);
3834 got_fileindex_free(fileindex);
3835 return err;
3838 const struct got_error *
3839 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3840 const char *arg)
3842 const struct got_error *err = NULL;
3843 char *resolved = NULL, *cwd = NULL, *path = NULL;
3844 size_t len;
3845 struct stat sb;
3846 char *abspath = NULL;
3847 char canonpath[PATH_MAX];
3849 *wt_path = NULL;
3851 cwd = getcwd(NULL, 0);
3852 if (cwd == NULL)
3853 return got_error_from_errno("getcwd");
3855 if (lstat(arg, &sb) == -1) {
3856 if (errno != ENOENT) {
3857 err = got_error_from_errno2("lstat", arg);
3858 goto done;
3860 sb.st_mode = 0;
3862 if (S_ISLNK(sb.st_mode)) {
3864 * We cannot use realpath(3) with symlinks since we want to
3865 * operate on the symlink itself.
3866 * But we can make the path absolute, assuming it is relative
3867 * to the current working directory, and then canonicalize it.
3869 if (!got_path_is_absolute(arg)) {
3870 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3871 err = got_error_from_errno("asprintf");
3872 goto done;
3876 err = got_canonpath(abspath ? abspath : arg, canonpath,
3877 sizeof(canonpath));
3878 if (err)
3879 goto done;
3880 resolved = strdup(canonpath);
3881 if (resolved == NULL) {
3882 err = got_error_from_errno("strdup");
3883 goto done;
3885 } else {
3886 resolved = realpath(arg, NULL);
3887 if (resolved == NULL) {
3888 if (errno != ENOENT) {
3889 err = got_error_from_errno2("realpath", arg);
3890 goto done;
3892 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3893 err = got_error_from_errno("asprintf");
3894 goto done;
3896 err = got_canonpath(abspath, canonpath,
3897 sizeof(canonpath));
3898 if (err)
3899 goto done;
3900 resolved = strdup(canonpath);
3901 if (resolved == NULL) {
3902 err = got_error_from_errno("strdup");
3903 goto done;
3908 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3909 strlen(got_worktree_get_root_path(worktree)))) {
3910 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3911 goto done;
3914 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3915 err = got_path_skip_common_ancestor(&path,
3916 got_worktree_get_root_path(worktree), resolved);
3917 if (err)
3918 goto done;
3919 } else {
3920 path = strdup("");
3921 if (path == NULL) {
3922 err = got_error_from_errno("strdup");
3923 goto done;
3927 /* XXX status walk can't deal with trailing slash! */
3928 len = strlen(path);
3929 while (len > 0 && path[len - 1] == '/') {
3930 path[len - 1] = '\0';
3931 len--;
3933 done:
3934 free(abspath);
3935 free(resolved);
3936 free(cwd);
3937 if (err == NULL)
3938 *wt_path = path;
3939 else
3940 free(path);
3941 return err;
3944 struct schedule_addition_args {
3945 struct got_worktree *worktree;
3946 struct got_fileindex *fileindex;
3947 got_worktree_checkout_cb progress_cb;
3948 void *progress_arg;
3949 struct got_repository *repo;
3952 static const struct got_error *
3953 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3954 const char *relpath, struct got_object_id *blob_id,
3955 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3956 int dirfd, const char *de_name)
3958 struct schedule_addition_args *a = arg;
3959 const struct got_error *err = NULL;
3960 struct got_fileindex_entry *ie;
3961 struct stat sb;
3962 char *ondisk_path;
3964 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3965 relpath) == -1)
3966 return got_error_from_errno("asprintf");
3968 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3969 if (ie) {
3970 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3971 de_name, a->repo);
3972 if (err)
3973 goto done;
3974 /* Re-adding an existing entry is a no-op. */
3975 if (status == GOT_STATUS_ADD)
3976 goto done;
3977 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3978 if (err)
3979 goto done;
3982 if (status != GOT_STATUS_UNVERSIONED) {
3983 if (status == GOT_STATUS_NONEXISTENT)
3984 err = got_error_set_errno(ENOENT, ondisk_path);
3985 else
3986 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3987 goto done;
3990 err = got_fileindex_entry_alloc(&ie, relpath);
3991 if (err)
3992 goto done;
3993 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3994 relpath, NULL, NULL, 1);
3995 if (err) {
3996 got_fileindex_entry_free(ie);
3997 goto done;
3999 err = got_fileindex_entry_add(a->fileindex, ie);
4000 if (err) {
4001 got_fileindex_entry_free(ie);
4002 goto done;
4004 done:
4005 free(ondisk_path);
4006 if (err)
4007 return err;
4008 if (status == GOT_STATUS_ADD)
4009 return NULL;
4010 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4013 const struct got_error *
4014 got_worktree_schedule_add(struct got_worktree *worktree,
4015 struct got_pathlist_head *paths,
4016 got_worktree_checkout_cb progress_cb, void *progress_arg,
4017 struct got_repository *repo, int no_ignores)
4019 struct got_fileindex *fileindex = NULL;
4020 char *fileindex_path = NULL;
4021 const struct got_error *err = NULL, *sync_err, *unlockerr;
4022 struct got_pathlist_entry *pe;
4023 struct schedule_addition_args saa;
4025 err = lock_worktree(worktree, LOCK_EX);
4026 if (err)
4027 return err;
4029 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4030 if (err)
4031 goto done;
4033 saa.worktree = worktree;
4034 saa.fileindex = fileindex;
4035 saa.progress_cb = progress_cb;
4036 saa.progress_arg = progress_arg;
4037 saa.repo = repo;
4039 TAILQ_FOREACH(pe, paths, entry) {
4040 err = worktree_status(worktree, pe->path, fileindex, repo,
4041 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4042 if (err)
4043 break;
4045 sync_err = sync_fileindex(fileindex, fileindex_path);
4046 if (sync_err && err == NULL)
4047 err = sync_err;
4048 done:
4049 free(fileindex_path);
4050 if (fileindex)
4051 got_fileindex_free(fileindex);
4052 unlockerr = lock_worktree(worktree, LOCK_SH);
4053 if (unlockerr && err == NULL)
4054 err = unlockerr;
4055 return err;
4058 struct schedule_deletion_args {
4059 struct got_worktree *worktree;
4060 struct got_fileindex *fileindex;
4061 got_worktree_delete_cb progress_cb;
4062 void *progress_arg;
4063 struct got_repository *repo;
4064 int delete_local_mods;
4065 int keep_on_disk;
4066 int ignore_missing_paths;
4067 const char *status_codes;
4070 static const struct got_error *
4071 schedule_for_deletion(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct schedule_deletion_args *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_fileindex_entry *ie = NULL;
4079 struct stat sb;
4080 char *ondisk_path;
4082 if (status == GOT_STATUS_NONEXISTENT) {
4083 if (a->ignore_missing_paths)
4084 return NULL;
4085 return got_error_set_errno(ENOENT, relpath);
4088 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4089 if (ie == NULL)
4090 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4092 staged_status = get_staged_status(ie);
4093 if (staged_status != GOT_STATUS_NO_CHANGE) {
4094 if (staged_status == GOT_STATUS_DELETE)
4095 return NULL;
4096 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4099 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4100 relpath) == -1)
4101 return got_error_from_errno("asprintf");
4103 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4104 a->repo);
4105 if (err)
4106 goto done;
4108 if (a->status_codes) {
4109 size_t ncodes = strlen(a->status_codes);
4110 int i;
4111 for (i = 0; i < ncodes ; i++) {
4112 if (status == a->status_codes[i])
4113 break;
4115 if (i == ncodes) {
4116 /* Do not delete files in non-matching status. */
4117 free(ondisk_path);
4118 return NULL;
4120 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4121 a->status_codes[i] != GOT_STATUS_MISSING) {
4122 static char msg[64];
4123 snprintf(msg, sizeof(msg),
4124 "invalid status code '%c'", a->status_codes[i]);
4125 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4126 goto done;
4130 if (status != GOT_STATUS_NO_CHANGE) {
4131 if (status == GOT_STATUS_DELETE)
4132 goto done;
4133 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4134 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4135 goto done;
4137 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4138 err = got_error_set_errno(ENOENT, relpath);
4139 goto done;
4141 if (status != GOT_STATUS_MODIFY &&
4142 status != GOT_STATUS_MISSING) {
4143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4144 goto done;
4148 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4149 size_t root_len;
4151 if (dirfd != -1) {
4152 if (unlinkat(dirfd, de_name, 0) == -1) {
4153 err = got_error_from_errno2("unlinkat",
4154 ondisk_path);
4155 goto done;
4157 } else if (unlink(ondisk_path) == -1) {
4158 err = got_error_from_errno2("unlink", ondisk_path);
4159 goto done;
4162 root_len = strlen(a->worktree->root_path);
4163 do {
4164 char *parent;
4165 err = got_path_dirname(&parent, ondisk_path);
4166 if (err)
4167 goto done;
4168 free(ondisk_path);
4169 ondisk_path = parent;
4170 if (rmdir(ondisk_path) == -1) {
4171 if (errno != ENOTEMPTY)
4172 err = got_error_from_errno2("rmdir",
4173 ondisk_path);
4174 break;
4176 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 strlen(ondisk_path), root_len) != 0);
4180 got_fileindex_entry_mark_deleted_from_disk(ie);
4181 done:
4182 free(ondisk_path);
4183 if (err)
4184 return err;
4185 if (status == GOT_STATUS_DELETE)
4186 return NULL;
4187 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 staged_status, relpath);
4191 const struct got_error *
4192 got_worktree_schedule_delete(struct got_worktree *worktree,
4193 struct got_pathlist_head *paths, int delete_local_mods,
4194 const char *status_codes,
4195 got_worktree_delete_cb progress_cb, void *progress_arg,
4196 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4198 struct got_fileindex *fileindex = NULL;
4199 char *fileindex_path = NULL;
4200 const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 struct got_pathlist_entry *pe;
4202 struct schedule_deletion_args sda;
4204 err = lock_worktree(worktree, LOCK_EX);
4205 if (err)
4206 return err;
4208 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4209 if (err)
4210 goto done;
4212 sda.worktree = worktree;
4213 sda.fileindex = fileindex;
4214 sda.progress_cb = progress_cb;
4215 sda.progress_arg = progress_arg;
4216 sda.repo = repo;
4217 sda.delete_local_mods = delete_local_mods;
4218 sda.keep_on_disk = keep_on_disk;
4219 sda.ignore_missing_paths = ignore_missing_paths;
4220 sda.status_codes = status_codes;
4222 TAILQ_FOREACH(pe, paths, entry) {
4223 err = worktree_status(worktree, pe->path, fileindex, repo,
4224 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4225 if (err)
4226 break;
4228 sync_err = sync_fileindex(fileindex, fileindex_path);
4229 if (sync_err && err == NULL)
4230 err = sync_err;
4231 done:
4232 free(fileindex_path);
4233 if (fileindex)
4234 got_fileindex_free(fileindex);
4235 unlockerr = lock_worktree(worktree, LOCK_SH);
4236 if (unlockerr && err == NULL)
4237 err = unlockerr;
4238 return err;
4241 static const struct got_error *
4242 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4244 const struct got_error *err = NULL;
4245 char *line = NULL;
4246 size_t linesize = 0, n;
4247 ssize_t linelen;
4249 linelen = getline(&line, &linesize, infile);
4250 if (linelen == -1) {
4251 if (ferror(infile)) {
4252 err = got_error_from_errno("getline");
4253 goto done;
4255 return NULL;
4257 if (outfile) {
4258 n = fwrite(line, 1, linelen, outfile);
4259 if (n != linelen) {
4260 err = got_ferror(outfile, GOT_ERR_IO);
4261 goto done;
4264 if (rejectfile) {
4265 n = fwrite(line, 1, linelen, rejectfile);
4266 if (n != linelen)
4267 err = got_ferror(outfile, GOT_ERR_IO);
4269 done:
4270 free(line);
4271 return err;
4274 static const struct got_error *
4275 skip_one_line(FILE *f)
4277 char *line = NULL;
4278 size_t linesize = 0;
4279 ssize_t linelen;
4281 linelen = getline(&line, &linesize, f);
4282 if (linelen == -1) {
4283 if (ferror(f))
4284 return got_error_from_errno("getline");
4285 return NULL;
4287 free(line);
4288 return NULL;
4291 static const struct got_error *
4292 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4293 int start_old, int end_old, int start_new, int end_new,
4294 FILE *outfile, FILE *rejectfile)
4296 const struct got_error *err;
4298 /* Copy old file's lines leading up to patch. */
4299 while (!feof(f1) && *line_cur1 < start_old) {
4300 err = copy_one_line(f1, outfile, NULL);
4301 if (err)
4302 return err;
4303 (*line_cur1)++;
4305 /* Skip new file's lines leading up to patch. */
4306 while (!feof(f2) && *line_cur2 < start_new) {
4307 if (rejectfile)
4308 err = copy_one_line(f2, NULL, rejectfile);
4309 else
4310 err = skip_one_line(f2);
4311 if (err)
4312 return err;
4313 (*line_cur2)++;
4315 /* Copy patched lines. */
4316 while (!feof(f2) && *line_cur2 <= end_new) {
4317 err = copy_one_line(f2, outfile, NULL);
4318 if (err)
4319 return err;
4320 (*line_cur2)++;
4322 /* Skip over old file's replaced lines. */
4323 while (!feof(f1) && *line_cur1 <= end_old) {
4324 if (rejectfile)
4325 err = copy_one_line(f1, NULL, rejectfile);
4326 else
4327 err = skip_one_line(f1);
4328 if (err)
4329 return err;
4330 (*line_cur1)++;
4333 return NULL;
4336 static const struct got_error *
4337 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4338 FILE *outfile, FILE *rejectfile)
4340 const struct got_error *err;
4342 if (outfile) {
4343 /* Copy old file's lines until EOF. */
4344 while (!feof(f1)) {
4345 err = copy_one_line(f1, outfile, NULL);
4346 if (err)
4347 return err;
4348 (*line_cur1)++;
4351 if (rejectfile) {
4352 /* Copy new file's lines until EOF. */
4353 while (!feof(f2)) {
4354 err = copy_one_line(f2, NULL, rejectfile);
4355 if (err)
4356 return err;
4357 (*line_cur2)++;
4361 return NULL;
4364 static const struct got_error *
4365 apply_or_reject_change(int *choice, int *nchunks_used,
4366 struct diff_result *diff_result, int n,
4367 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4368 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4369 got_worktree_patch_cb patch_cb, void *patch_arg)
4371 const struct got_error *err = NULL;
4372 struct diff_chunk_context cc = {};
4373 int start_old, end_old, start_new, end_new;
4374 FILE *hunkfile;
4375 struct diff_output_unidiff_state *diff_state;
4376 struct diff_input_info diff_info;
4377 int rc;
4379 *choice = GOT_PATCH_CHOICE_NONE;
4381 /* Get changed line numbers without context lines for copy_change(). */
4382 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4383 start_old = cc.left.start;
4384 end_old = cc.left.end;
4385 start_new = cc.right.start;
4386 end_new = cc.right.end;
4388 /* Get the same change with context lines for display. */
4389 memset(&cc, 0, sizeof(cc));
4390 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4392 memset(&diff_info, 0, sizeof(diff_info));
4393 diff_info.left_path = relpath;
4394 diff_info.right_path = relpath;
4396 diff_state = diff_output_unidiff_state_alloc();
4397 if (diff_state == NULL)
4398 return got_error_set_errno(ENOMEM,
4399 "diff_output_unidiff_state_alloc");
4401 hunkfile = got_opentemp();
4402 if (hunkfile == NULL) {
4403 err = got_error_from_errno("got_opentemp");
4404 goto done;
4407 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4408 diff_result, &cc);
4409 if (rc != DIFF_RC_OK) {
4410 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4411 goto done;
4414 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4415 err = got_ferror(hunkfile, GOT_ERR_IO);
4416 goto done;
4419 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4420 hunkfile, changeno, nchanges);
4421 if (err)
4422 goto done;
4424 switch (*choice) {
4425 case GOT_PATCH_CHOICE_YES:
4426 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4427 end_old, start_new, end_new, outfile, rejectfile);
4428 break;
4429 case GOT_PATCH_CHOICE_NO:
4430 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4431 end_old, start_new, end_new, rejectfile, outfile);
4432 break;
4433 case GOT_PATCH_CHOICE_QUIT:
4434 break;
4435 default:
4436 err = got_error(GOT_ERR_PATCH_CHOICE);
4437 break;
4439 done:
4440 diff_output_unidiff_state_free(diff_state);
4441 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4442 err = got_error_from_errno("fclose");
4443 return err;
4446 struct revert_file_args {
4447 struct got_worktree *worktree;
4448 struct got_fileindex *fileindex;
4449 got_worktree_checkout_cb progress_cb;
4450 void *progress_arg;
4451 got_worktree_patch_cb patch_cb;
4452 void *patch_arg;
4453 struct got_repository *repo;
4454 int unlink_added_files;
4457 static const struct got_error *
4458 create_patched_content(char **path_outfile, int reverse_patch,
4459 struct got_object_id *blob_id, const char *path2,
4460 int dirfd2, const char *de_name2,
4461 const char *relpath, struct got_repository *repo,
4462 got_worktree_patch_cb patch_cb, void *patch_arg)
4464 const struct got_error *err, *free_err;
4465 struct got_blob_object *blob = NULL;
4466 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4467 int fd = -1, fd2 = -1;
4468 char link_target[PATH_MAX];
4469 ssize_t link_len = 0;
4470 char *path1 = NULL, *id_str = NULL;
4471 struct stat sb2;
4472 struct got_diffreg_result *diffreg_result = NULL;
4473 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4474 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4476 *path_outfile = NULL;
4478 err = got_object_id_str(&id_str, blob_id);
4479 if (err)
4480 return err;
4482 if (dirfd2 != -1) {
4483 fd2 = openat(dirfd2, de_name2,
4484 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4485 if (fd2 == -1) {
4486 if (!got_err_open_nofollow_on_symlink()) {
4487 err = got_error_from_errno2("openat", path2);
4488 goto done;
4490 link_len = readlinkat(dirfd2, de_name2,
4491 link_target, sizeof(link_target));
4492 if (link_len == -1) {
4493 return got_error_from_errno2("readlinkat",
4494 path2);
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4499 } else {
4500 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4501 if (fd2 == -1) {
4502 if (!got_err_open_nofollow_on_symlink()) {
4503 err = got_error_from_errno2("open", path2);
4504 goto done;
4506 link_len = readlink(path2, link_target,
4507 sizeof(link_target));
4508 if (link_len == -1)
4509 return got_error_from_errno2("readlink", path2);
4510 sb2.st_mode = S_IFLNK;
4511 sb2.st_size = link_len;
4514 if (fd2 != -1) {
4515 if (fstat(fd2, &sb2) == -1) {
4516 err = got_error_from_errno2("fstat", path2);
4517 goto done;
4520 f2 = fdopen(fd2, "r");
4521 if (f2 == NULL) {
4522 err = got_error_from_errno2("fdopen", path2);
4523 goto done;
4525 fd2 = -1;
4526 } else {
4527 size_t n;
4528 f2 = got_opentemp();
4529 if (f2 == NULL) {
4530 err = got_error_from_errno2("got_opentemp", path2);
4531 goto done;
4533 n = fwrite(link_target, 1, link_len, f2);
4534 if (n != link_len) {
4535 err = got_ferror(f2, GOT_ERR_IO);
4536 goto done;
4538 if (fflush(f2) == EOF) {
4539 err = got_error_from_errno("fflush");
4540 goto done;
4542 rewind(f2);
4545 fd = got_opentempfd();
4546 if (fd == -1) {
4547 err = got_error_from_errno("got_opentempfd");
4548 goto done;
4551 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4552 if (err)
4553 goto done;
4555 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4556 if (err)
4557 goto done;
4559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4560 if (err)
4561 goto done;
4563 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4564 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4565 if (err)
4566 goto done;
4568 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4569 if (err)
4570 goto done;
4572 if (fseek(f1, 0L, SEEK_SET) == -1)
4573 return got_ferror(f1, GOT_ERR_IO);
4574 if (fseek(f2, 0L, SEEK_SET) == -1)
4575 return got_ferror(f2, GOT_ERR_IO);
4577 /* Count the number of actual changes in the diff result. */
4578 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4579 struct diff_chunk_context cc = {};
4580 diff_chunk_context_load_change(&cc, &nchunks_used,
4581 diffreg_result->result, n, 0);
4582 nchanges++;
4584 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4585 int choice;
4586 err = apply_or_reject_change(&choice, &nchunks_used,
4587 diffreg_result->result, n, relpath, f1, f2,
4588 &line_cur1, &line_cur2,
4589 reverse_patch ? NULL : outfile,
4590 reverse_patch ? outfile : NULL,
4591 ++i, nchanges, patch_cb, patch_arg);
4592 if (err)
4593 goto done;
4594 if (choice == GOT_PATCH_CHOICE_YES)
4595 have_content = 1;
4596 else if (choice == GOT_PATCH_CHOICE_QUIT)
4597 break;
4599 if (have_content) {
4600 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4601 reverse_patch ? NULL : outfile,
4602 reverse_patch ? outfile : NULL);
4603 if (err)
4604 goto done;
4606 if (!S_ISLNK(sb2.st_mode)) {
4607 mode_t mode;
4609 mode = apply_umask(sb2.st_mode);
4610 if (fchmod(fileno(outfile), mode) == -1) {
4611 err = got_error_from_errno2("fchmod", path2);
4612 goto done;
4616 done:
4617 free(id_str);
4618 if (fd != -1 && close(fd) == -1 && err == NULL)
4619 err = got_error_from_errno("close");
4620 if (blob)
4621 got_object_blob_close(blob);
4622 free_err = got_diffreg_result_free(diffreg_result);
4623 if (err == NULL)
4624 err = free_err;
4625 if (f1 && fclose(f1) == EOF && err == NULL)
4626 err = got_error_from_errno2("fclose", path1);
4627 if (f2 && fclose(f2) == EOF && err == NULL)
4628 err = got_error_from_errno2("fclose", path2);
4629 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4630 err = got_error_from_errno2("close", path2);
4631 if (outfile && fclose(outfile) == EOF && err == NULL)
4632 err = got_error_from_errno2("fclose", *path_outfile);
4633 if (path1 && unlink(path1) == -1 && err == NULL)
4634 err = got_error_from_errno2("unlink", path1);
4635 if (err || !have_content) {
4636 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4637 err = got_error_from_errno2("unlink", *path_outfile);
4638 free(*path_outfile);
4639 *path_outfile = NULL;
4641 free(path1);
4642 return err;
4645 static const struct got_error *
4646 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4647 const char *relpath, struct got_object_id *blob_id,
4648 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4649 int dirfd, const char *de_name)
4651 struct revert_file_args *a = arg;
4652 const struct got_error *err = NULL;
4653 char *parent_path = NULL;
4654 struct got_fileindex_entry *ie;
4655 struct got_commit_object *base_commit = NULL;
4656 struct got_tree_object *tree = NULL;
4657 struct got_object_id *tree_id = NULL;
4658 const struct got_tree_entry *te = NULL;
4659 char *tree_path = NULL, *te_name;
4660 char *ondisk_path = NULL, *path_content = NULL;
4661 struct got_blob_object *blob = NULL;
4662 int fd = -1;
4664 /* Reverting a staged deletion is a no-op. */
4665 if (status == GOT_STATUS_DELETE &&
4666 staged_status != GOT_STATUS_NO_CHANGE)
4667 return NULL;
4669 if (status == GOT_STATUS_UNVERSIONED)
4670 return (*a->progress_cb)(a->progress_arg,
4671 GOT_STATUS_UNVERSIONED, relpath);
4673 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4674 if (ie == NULL)
4675 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4677 /* Construct in-repository path of tree which contains this blob. */
4678 err = got_path_dirname(&parent_path, ie->path);
4679 if (err) {
4680 if (err->code != GOT_ERR_BAD_PATH)
4681 goto done;
4682 parent_path = strdup("/");
4683 if (parent_path == NULL) {
4684 err = got_error_from_errno("strdup");
4685 goto done;
4688 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4689 tree_path = strdup(parent_path);
4690 if (tree_path == NULL) {
4691 err = got_error_from_errno("strdup");
4692 goto done;
4694 } else {
4695 if (got_path_is_root_dir(parent_path)) {
4696 tree_path = strdup(a->worktree->path_prefix);
4697 if (tree_path == NULL) {
4698 err = got_error_from_errno("strdup");
4699 goto done;
4701 } else {
4702 if (asprintf(&tree_path, "%s/%s",
4703 a->worktree->path_prefix, parent_path) == -1) {
4704 err = got_error_from_errno("asprintf");
4705 goto done;
4710 err = got_object_open_as_commit(&base_commit, a->repo,
4711 a->worktree->base_commit_id);
4712 if (err)
4713 goto done;
4715 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4716 if (err) {
4717 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4718 (status == GOT_STATUS_ADD ||
4719 staged_status == GOT_STATUS_ADD)))
4720 goto done;
4721 } else {
4722 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4723 if (err)
4724 goto done;
4726 err = got_path_basename(&te_name, ie->path);
4727 if (err)
4728 goto done;
4730 te = got_object_tree_find_entry(tree, te_name);
4731 free(te_name);
4732 if (te == NULL && status != GOT_STATUS_ADD &&
4733 staged_status != GOT_STATUS_ADD) {
4734 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4735 goto done;
4739 switch (status) {
4740 case GOT_STATUS_ADD:
4741 if (a->patch_cb) {
4742 int choice = GOT_PATCH_CHOICE_NONE;
4743 err = (*a->patch_cb)(&choice, a->patch_arg,
4744 status, ie->path, NULL, 1, 1);
4745 if (err)
4746 goto done;
4747 if (choice != GOT_PATCH_CHOICE_YES)
4748 break;
4750 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4751 ie->path);
4752 if (err)
4753 goto done;
4754 got_fileindex_entry_remove(a->fileindex, ie);
4755 if (a->unlink_added_files) {
4756 if (asprintf(&ondisk_path, "%s/%s",
4757 got_worktree_get_root_path(a->worktree),
4758 relpath) == -1) {
4759 err = got_error_from_errno("asprintf");
4760 goto done;
4762 if (unlink(ondisk_path) == -1) {
4763 err = got_error_from_errno2("unlink",
4764 ondisk_path);
4765 break;
4768 break;
4769 case GOT_STATUS_DELETE:
4770 if (a->patch_cb) {
4771 int choice = GOT_PATCH_CHOICE_NONE;
4772 err = (*a->patch_cb)(&choice, a->patch_arg,
4773 status, ie->path, NULL, 1, 1);
4774 if (err)
4775 goto done;
4776 if (choice != GOT_PATCH_CHOICE_YES)
4777 break;
4779 /* fall through */
4780 case GOT_STATUS_MODIFY:
4781 case GOT_STATUS_MODE_CHANGE:
4782 case GOT_STATUS_CONFLICT:
4783 case GOT_STATUS_MISSING: {
4784 struct got_object_id id;
4785 if (staged_status == GOT_STATUS_ADD ||
4786 staged_status == GOT_STATUS_MODIFY) {
4787 memcpy(id.sha1, ie->staged_blob_sha1,
4788 SHA1_DIGEST_LENGTH);
4789 } else
4790 memcpy(id.sha1, ie->blob_sha1,
4791 SHA1_DIGEST_LENGTH);
4792 fd = got_opentempfd();
4793 if (fd == -1) {
4794 err = got_error_from_errno("got_opentempfd");
4795 goto done;
4798 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4799 if (err)
4800 goto done;
4802 if (asprintf(&ondisk_path, "%s/%s",
4803 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4804 err = got_error_from_errno("asprintf");
4805 goto done;
4808 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4809 status == GOT_STATUS_CONFLICT)) {
4810 int is_bad_symlink = 0;
4811 err = create_patched_content(&path_content, 1, &id,
4812 ondisk_path, dirfd, de_name, ie->path, a->repo,
4813 a->patch_cb, a->patch_arg);
4814 if (err || path_content == NULL)
4815 break;
4816 if (te && S_ISLNK(te->mode)) {
4817 if (unlink(path_content) == -1) {
4818 err = got_error_from_errno2("unlink",
4819 path_content);
4820 break;
4822 err = install_symlink(&is_bad_symlink,
4823 a->worktree, ondisk_path, ie->path,
4824 blob, 0, 1, 0, 0, a->repo,
4825 a->progress_cb, a->progress_arg);
4826 } else {
4827 if (rename(path_content, ondisk_path) == -1) {
4828 err = got_error_from_errno3("rename",
4829 path_content, ondisk_path);
4830 goto done;
4833 } else {
4834 int is_bad_symlink = 0;
4835 if (te && S_ISLNK(te->mode)) {
4836 err = install_symlink(&is_bad_symlink,
4837 a->worktree, ondisk_path, ie->path,
4838 blob, 0, 1, 0, 0, a->repo,
4839 a->progress_cb, a->progress_arg);
4840 } else {
4841 err = install_blob(a->worktree, ondisk_path,
4842 ie->path,
4843 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4844 got_fileindex_perms_to_st(ie), blob,
4845 0, 1, 0, 0, a->repo,
4846 a->progress_cb, a->progress_arg);
4848 if (err)
4849 goto done;
4850 if (status == GOT_STATUS_DELETE ||
4851 status == GOT_STATUS_MODE_CHANGE) {
4852 err = got_fileindex_entry_update(ie,
4853 a->worktree->root_fd, relpath,
4854 blob->id.sha1,
4855 a->worktree->base_commit_id->sha1, 1);
4856 if (err)
4857 goto done;
4859 if (is_bad_symlink) {
4860 got_fileindex_entry_filetype_set(ie,
4861 GOT_FILEIDX_MODE_BAD_SYMLINK);
4864 break;
4866 default:
4867 break;
4869 done:
4870 free(ondisk_path);
4871 free(path_content);
4872 free(parent_path);
4873 free(tree_path);
4874 if (fd != -1 && close(fd) == -1 && err == NULL)
4875 err = got_error_from_errno("close");
4876 if (blob)
4877 got_object_blob_close(blob);
4878 if (tree)
4879 got_object_tree_close(tree);
4880 free(tree_id);
4881 if (base_commit)
4882 got_object_commit_close(base_commit);
4883 return err;
4886 const struct got_error *
4887 got_worktree_revert(struct got_worktree *worktree,
4888 struct got_pathlist_head *paths,
4889 got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 got_worktree_patch_cb patch_cb, void *patch_arg,
4891 struct got_repository *repo)
4893 struct got_fileindex *fileindex = NULL;
4894 char *fileindex_path = NULL;
4895 const struct got_error *err = NULL, *unlockerr = NULL;
4896 const struct got_error *sync_err = NULL;
4897 struct got_pathlist_entry *pe;
4898 struct revert_file_args rfa;
4900 err = lock_worktree(worktree, LOCK_EX);
4901 if (err)
4902 return err;
4904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4905 if (err)
4906 goto done;
4908 rfa.worktree = worktree;
4909 rfa.fileindex = fileindex;
4910 rfa.progress_cb = progress_cb;
4911 rfa.progress_arg = progress_arg;
4912 rfa.patch_cb = patch_cb;
4913 rfa.patch_arg = patch_arg;
4914 rfa.repo = repo;
4915 rfa.unlink_added_files = 0;
4916 TAILQ_FOREACH(pe, paths, entry) {
4917 err = worktree_status(worktree, pe->path, fileindex, repo,
4918 revert_file, &rfa, NULL, NULL, 1, 0);
4919 if (err)
4920 break;
4922 sync_err = sync_fileindex(fileindex, fileindex_path);
4923 if (sync_err && err == NULL)
4924 err = sync_err;
4925 done:
4926 free(fileindex_path);
4927 if (fileindex)
4928 got_fileindex_free(fileindex);
4929 unlockerr = lock_worktree(worktree, LOCK_SH);
4930 if (unlockerr && err == NULL)
4931 err = unlockerr;
4932 return err;
4935 static void
4936 free_commitable(struct got_commitable *ct)
4938 free(ct->path);
4939 free(ct->in_repo_path);
4940 free(ct->ondisk_path);
4941 free(ct->blob_id);
4942 free(ct->base_blob_id);
4943 free(ct->staged_blob_id);
4944 free(ct->base_commit_id);
4945 free(ct);
4948 struct collect_commitables_arg {
4949 struct got_pathlist_head *commitable_paths;
4950 struct got_repository *repo;
4951 struct got_worktree *worktree;
4952 struct got_fileindex *fileindex;
4953 int have_staged_files;
4954 int allow_bad_symlinks;
4957 static const struct got_error *
4958 collect_commitables(void *arg, unsigned char status,
4959 unsigned char staged_status, const char *relpath,
4960 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4961 struct got_object_id *commit_id, int dirfd, const char *de_name)
4963 struct collect_commitables_arg *a = arg;
4964 const struct got_error *err = NULL;
4965 struct got_commitable *ct = NULL;
4966 struct got_pathlist_entry *new = NULL;
4967 char *parent_path = NULL, *path = NULL;
4968 struct stat sb;
4970 if (a->have_staged_files) {
4971 if (staged_status != GOT_STATUS_MODIFY &&
4972 staged_status != GOT_STATUS_ADD &&
4973 staged_status != GOT_STATUS_DELETE)
4974 return NULL;
4975 } else {
4976 if (status == GOT_STATUS_CONFLICT)
4977 return got_error(GOT_ERR_COMMIT_CONFLICT);
4979 if (status != GOT_STATUS_MODIFY &&
4980 status != GOT_STATUS_MODE_CHANGE &&
4981 status != GOT_STATUS_ADD &&
4982 status != GOT_STATUS_DELETE)
4983 return NULL;
4986 if (asprintf(&path, "/%s", relpath) == -1) {
4987 err = got_error_from_errno("asprintf");
4988 goto done;
4990 if (strcmp(path, "/") == 0) {
4991 parent_path = strdup("");
4992 if (parent_path == NULL)
4993 return got_error_from_errno("strdup");
4994 } else {
4995 err = got_path_dirname(&parent_path, path);
4996 if (err)
4997 return err;
5000 ct = calloc(1, sizeof(*ct));
5001 if (ct == NULL) {
5002 err = got_error_from_errno("calloc");
5003 goto done;
5006 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5007 relpath) == -1) {
5008 err = got_error_from_errno("asprintf");
5009 goto done;
5012 if (staged_status == GOT_STATUS_ADD ||
5013 staged_status == GOT_STATUS_MODIFY) {
5014 struct got_fileindex_entry *ie;
5015 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5016 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5017 case GOT_FILEIDX_MODE_REGULAR_FILE:
5018 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5019 ct->mode = S_IFREG;
5020 break;
5021 case GOT_FILEIDX_MODE_SYMLINK:
5022 ct->mode = S_IFLNK;
5023 break;
5024 default:
5025 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5026 goto done;
5028 ct->mode |= got_fileindex_entry_perms_get(ie);
5029 } else if (status != GOT_STATUS_DELETE &&
5030 staged_status != GOT_STATUS_DELETE) {
5031 if (dirfd != -1) {
5032 if (fstatat(dirfd, de_name, &sb,
5033 AT_SYMLINK_NOFOLLOW) == -1) {
5034 err = got_error_from_errno2("fstatat",
5035 ct->ondisk_path);
5036 goto done;
5038 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5039 err = got_error_from_errno2("lstat", ct->ondisk_path);
5040 goto done;
5042 ct->mode = sb.st_mode;
5045 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5046 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5047 relpath) == -1) {
5048 err = got_error_from_errno("asprintf");
5049 goto done;
5052 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5053 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5054 int is_bad_symlink;
5055 char target_path[PATH_MAX];
5056 ssize_t target_len;
5057 target_len = readlink(ct->ondisk_path, target_path,
5058 sizeof(target_path));
5059 if (target_len == -1) {
5060 err = got_error_from_errno2("readlink",
5061 ct->ondisk_path);
5062 goto done;
5064 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5065 target_len, ct->ondisk_path, a->worktree->root_path);
5066 if (err)
5067 goto done;
5068 if (is_bad_symlink) {
5069 err = got_error_path(ct->ondisk_path,
5070 GOT_ERR_BAD_SYMLINK);
5071 goto done;
5076 ct->status = status;
5077 ct->staged_status = staged_status;
5078 ct->blob_id = NULL; /* will be filled in when blob gets created */
5079 if (ct->status != GOT_STATUS_ADD &&
5080 ct->staged_status != GOT_STATUS_ADD) {
5081 ct->base_blob_id = got_object_id_dup(blob_id);
5082 if (ct->base_blob_id == NULL) {
5083 err = got_error_from_errno("got_object_id_dup");
5084 goto done;
5086 ct->base_commit_id = got_object_id_dup(commit_id);
5087 if (ct->base_commit_id == NULL) {
5088 err = got_error_from_errno("got_object_id_dup");
5089 goto done;
5092 if (ct->staged_status == GOT_STATUS_ADD ||
5093 ct->staged_status == GOT_STATUS_MODIFY) {
5094 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5095 if (ct->staged_blob_id == NULL) {
5096 err = got_error_from_errno("got_object_id_dup");
5097 goto done;
5100 ct->path = strdup(path);
5101 if (ct->path == NULL) {
5102 err = got_error_from_errno("strdup");
5103 goto done;
5105 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5106 done:
5107 if (ct && (err || new == NULL))
5108 free_commitable(ct);
5109 free(parent_path);
5110 free(path);
5111 return err;
5114 static const struct got_error *write_tree(struct got_object_id **, int *,
5115 struct got_tree_object *, const char *, struct got_pathlist_head *,
5116 got_worktree_status_cb status_cb, void *status_arg,
5117 struct got_repository *);
5119 static const struct got_error *
5120 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5121 struct got_tree_entry *te, const char *parent_path,
5122 struct got_pathlist_head *commitable_paths,
5123 got_worktree_status_cb status_cb, void *status_arg,
5124 struct got_repository *repo)
5126 const struct got_error *err = NULL;
5127 struct got_tree_object *subtree;
5128 char *subpath;
5130 if (asprintf(&subpath, "%s%s%s", parent_path,
5131 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5132 return got_error_from_errno("asprintf");
5134 err = got_object_open_as_tree(&subtree, repo, &te->id);
5135 if (err)
5136 return err;
5138 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5139 commitable_paths, status_cb, status_arg, repo);
5140 got_object_tree_close(subtree);
5141 free(subpath);
5142 return err;
5145 static const struct got_error *
5146 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5148 const struct got_error *err = NULL;
5149 char *ct_parent_path = NULL;
5151 *match = 0;
5153 if (strchr(ct->in_repo_path, '/') == NULL) {
5154 *match = got_path_is_root_dir(path);
5155 return NULL;
5158 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5159 if (err)
5160 return err;
5161 *match = (strcmp(path, ct_parent_path) == 0);
5162 free(ct_parent_path);
5163 return err;
5166 static mode_t
5167 get_ct_file_mode(struct got_commitable *ct)
5169 if (S_ISLNK(ct->mode))
5170 return S_IFLNK;
5172 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5175 static const struct got_error *
5176 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5177 struct got_tree_entry *te, struct got_commitable *ct)
5179 const struct got_error *err = NULL;
5181 *new_te = NULL;
5183 err = got_object_tree_entry_dup(new_te, te);
5184 if (err)
5185 goto done;
5187 (*new_te)->mode = get_ct_file_mode(ct);
5189 if (ct->staged_status == GOT_STATUS_MODIFY)
5190 memcpy(&(*new_te)->id, ct->staged_blob_id,
5191 sizeof((*new_te)->id));
5192 else
5193 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5194 done:
5195 if (err && *new_te) {
5196 free(*new_te);
5197 *new_te = NULL;
5199 return err;
5202 static const struct got_error *
5203 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5204 struct got_commitable *ct)
5206 const struct got_error *err = NULL;
5207 char *ct_name = NULL;
5209 *new_te = NULL;
5211 *new_te = calloc(1, sizeof(**new_te));
5212 if (*new_te == NULL)
5213 return got_error_from_errno("calloc");
5215 err = got_path_basename(&ct_name, ct->path);
5216 if (err)
5217 goto done;
5218 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5219 sizeof((*new_te)->name)) {
5220 err = got_error(GOT_ERR_NO_SPACE);
5221 goto done;
5224 (*new_te)->mode = get_ct_file_mode(ct);
5226 if (ct->staged_status == GOT_STATUS_ADD)
5227 memcpy(&(*new_te)->id, ct->staged_blob_id,
5228 sizeof((*new_te)->id));
5229 else
5230 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5231 done:
5232 free(ct_name);
5233 if (err && *new_te) {
5234 free(*new_te);
5235 *new_te = NULL;
5237 return err;
5240 static const struct got_error *
5241 insert_tree_entry(struct got_tree_entry *new_te,
5242 struct got_pathlist_head *paths)
5244 const struct got_error *err = NULL;
5245 struct got_pathlist_entry *new_pe;
5247 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5248 if (err)
5249 return err;
5250 if (new_pe == NULL)
5251 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5252 return NULL;
5255 static const struct got_error *
5256 report_ct_status(struct got_commitable *ct,
5257 got_worktree_status_cb status_cb, void *status_arg)
5259 const char *ct_path = ct->path;
5260 unsigned char status;
5262 if (status_cb == NULL) /* no commit progress output desired */
5263 return NULL;
5265 while (ct_path[0] == '/')
5266 ct_path++;
5268 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5269 status = ct->staged_status;
5270 else
5271 status = ct->status;
5273 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5274 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5277 static const struct got_error *
5278 match_modified_subtree(int *modified, struct got_tree_entry *te,
5279 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5281 const struct got_error *err = NULL;
5282 struct got_pathlist_entry *pe;
5283 char *te_path;
5285 *modified = 0;
5287 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5288 got_path_is_root_dir(base_tree_path) ? "" : "/",
5289 te->name) == -1)
5290 return got_error_from_errno("asprintf");
5292 TAILQ_FOREACH(pe, commitable_paths, entry) {
5293 struct got_commitable *ct = pe->data;
5294 *modified = got_path_is_child(ct->in_repo_path, te_path,
5295 strlen(te_path));
5296 if (*modified)
5297 break;
5300 free(te_path);
5301 return err;
5304 static const struct got_error *
5305 match_deleted_or_modified_ct(struct got_commitable **ctp,
5306 struct got_tree_entry *te, const char *base_tree_path,
5307 struct got_pathlist_head *commitable_paths)
5309 const struct got_error *err = NULL;
5310 struct got_pathlist_entry *pe;
5312 *ctp = NULL;
5314 TAILQ_FOREACH(pe, commitable_paths, entry) {
5315 struct got_commitable *ct = pe->data;
5316 char *ct_name = NULL;
5317 int path_matches;
5319 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5320 if (ct->status != GOT_STATUS_MODIFY &&
5321 ct->status != GOT_STATUS_MODE_CHANGE &&
5322 ct->status != GOT_STATUS_DELETE)
5323 continue;
5324 } else {
5325 if (ct->staged_status != GOT_STATUS_MODIFY &&
5326 ct->staged_status != GOT_STATUS_DELETE)
5327 continue;
5330 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5331 continue;
5333 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5334 if (err)
5335 return err;
5336 if (!path_matches)
5337 continue;
5339 err = got_path_basename(&ct_name, pe->path);
5340 if (err)
5341 return err;
5343 if (strcmp(te->name, ct_name) != 0) {
5344 free(ct_name);
5345 continue;
5347 free(ct_name);
5349 *ctp = ct;
5350 break;
5353 return err;
5356 static const struct got_error *
5357 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5358 const char *child_path, const char *path_base_tree,
5359 struct got_pathlist_head *commitable_paths,
5360 got_worktree_status_cb status_cb, void *status_arg,
5361 struct got_repository *repo)
5363 const struct got_error *err = NULL;
5364 struct got_tree_entry *new_te;
5365 char *subtree_path;
5366 struct got_object_id *id = NULL;
5367 int nentries;
5369 *new_tep = NULL;
5371 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5372 got_path_is_root_dir(path_base_tree) ? "" : "/",
5373 child_path) == -1)
5374 return got_error_from_errno("asprintf");
5376 new_te = calloc(1, sizeof(*new_te));
5377 if (new_te == NULL)
5378 return got_error_from_errno("calloc");
5379 new_te->mode = S_IFDIR;
5381 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5382 sizeof(new_te->name)) {
5383 err = got_error(GOT_ERR_NO_SPACE);
5384 goto done;
5386 err = write_tree(&id, &nentries, NULL, subtree_path,
5387 commitable_paths, status_cb, status_arg, repo);
5388 if (err) {
5389 free(new_te);
5390 goto done;
5392 memcpy(&new_te->id, id, sizeof(new_te->id));
5393 done:
5394 free(id);
5395 free(subtree_path);
5396 if (err == NULL)
5397 *new_tep = new_te;
5398 return err;
5401 static const struct got_error *
5402 write_tree(struct got_object_id **new_tree_id, int *nentries,
5403 struct got_tree_object *base_tree, const char *path_base_tree,
5404 struct got_pathlist_head *commitable_paths,
5405 got_worktree_status_cb status_cb, void *status_arg,
5406 struct got_repository *repo)
5408 const struct got_error *err = NULL;
5409 struct got_pathlist_head paths;
5410 struct got_tree_entry *te, *new_te = NULL;
5411 struct got_pathlist_entry *pe;
5413 TAILQ_INIT(&paths);
5414 *nentries = 0;
5416 /* Insert, and recurse into, newly added entries first. */
5417 TAILQ_FOREACH(pe, commitable_paths, entry) {
5418 struct got_commitable *ct = pe->data;
5419 char *child_path = NULL, *slash;
5421 if ((ct->status != GOT_STATUS_ADD &&
5422 ct->staged_status != GOT_STATUS_ADD) ||
5423 (ct->flags & GOT_COMMITABLE_ADDED))
5424 continue;
5426 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5427 strlen(path_base_tree)))
5428 continue;
5430 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5431 ct->in_repo_path);
5432 if (err)
5433 goto done;
5435 slash = strchr(child_path, '/');
5436 if (slash == NULL) {
5437 err = alloc_added_blob_tree_entry(&new_te, ct);
5438 if (err)
5439 goto done;
5440 err = report_ct_status(ct, status_cb, status_arg);
5441 if (err)
5442 goto done;
5443 ct->flags |= GOT_COMMITABLE_ADDED;
5444 err = insert_tree_entry(new_te, &paths);
5445 if (err)
5446 goto done;
5447 (*nentries)++;
5448 } else {
5449 *slash = '\0'; /* trim trailing path components */
5450 if (base_tree == NULL ||
5451 got_object_tree_find_entry(base_tree, child_path)
5452 == NULL) {
5453 err = make_subtree_for_added_blob(&new_te,
5454 child_path, path_base_tree,
5455 commitable_paths, status_cb, status_arg,
5456 repo);
5457 if (err)
5458 goto done;
5459 err = insert_tree_entry(new_te, &paths);
5460 if (err)
5461 goto done;
5462 (*nentries)++;
5467 if (base_tree) {
5468 int i, nbase_entries;
5469 /* Handle modified and deleted entries. */
5470 nbase_entries = got_object_tree_get_nentries(base_tree);
5471 for (i = 0; i < nbase_entries; i++) {
5472 struct got_commitable *ct = NULL;
5474 te = got_object_tree_get_entry(base_tree, i);
5475 if (got_object_tree_entry_is_submodule(te)) {
5476 /* Entry is a submodule; just copy it. */
5477 err = got_object_tree_entry_dup(&new_te, te);
5478 if (err)
5479 goto done;
5480 err = insert_tree_entry(new_te, &paths);
5481 if (err)
5482 goto done;
5483 (*nentries)++;
5484 continue;
5487 if (S_ISDIR(te->mode)) {
5488 int modified;
5489 err = got_object_tree_entry_dup(&new_te, te);
5490 if (err)
5491 goto done;
5492 err = match_modified_subtree(&modified, te,
5493 path_base_tree, commitable_paths);
5494 if (err)
5495 goto done;
5496 /* Avoid recursion into unmodified subtrees. */
5497 if (modified) {
5498 struct got_object_id *new_id;
5499 int nsubentries;
5500 err = write_subtree(&new_id,
5501 &nsubentries, te,
5502 path_base_tree, commitable_paths,
5503 status_cb, status_arg, repo);
5504 if (err)
5505 goto done;
5506 if (nsubentries == 0) {
5507 /* All entries were deleted. */
5508 free(new_id);
5509 continue;
5511 memcpy(&new_te->id, new_id,
5512 sizeof(new_te->id));
5513 free(new_id);
5515 err = insert_tree_entry(new_te, &paths);
5516 if (err)
5517 goto done;
5518 (*nentries)++;
5519 continue;
5522 err = match_deleted_or_modified_ct(&ct, te,
5523 path_base_tree, commitable_paths);
5524 if (err)
5525 goto done;
5526 if (ct) {
5527 /* NB: Deleted entries get dropped here. */
5528 if (ct->status == GOT_STATUS_MODIFY ||
5529 ct->status == GOT_STATUS_MODE_CHANGE ||
5530 ct->staged_status == GOT_STATUS_MODIFY) {
5531 err = alloc_modified_blob_tree_entry(
5532 &new_te, te, ct);
5533 if (err)
5534 goto done;
5535 err = insert_tree_entry(new_te, &paths);
5536 if (err)
5537 goto done;
5538 (*nentries)++;
5540 err = report_ct_status(ct, status_cb,
5541 status_arg);
5542 if (err)
5543 goto done;
5544 } else {
5545 /* Entry is unchanged; just copy it. */
5546 err = got_object_tree_entry_dup(&new_te, te);
5547 if (err)
5548 goto done;
5549 err = insert_tree_entry(new_te, &paths);
5550 if (err)
5551 goto done;
5552 (*nentries)++;
5557 /* Write new list of entries; deleted entries have been dropped. */
5558 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5559 done:
5560 got_pathlist_free(&paths);
5561 return err;
5564 static const struct got_error *
5565 update_fileindex_after_commit(struct got_worktree *worktree,
5566 struct got_pathlist_head *commitable_paths,
5567 struct got_object_id *new_base_commit_id,
5568 struct got_fileindex *fileindex, int have_staged_files)
5570 const struct got_error *err = NULL;
5571 struct got_pathlist_entry *pe;
5572 char *relpath = NULL;
5574 TAILQ_FOREACH(pe, commitable_paths, entry) {
5575 struct got_fileindex_entry *ie;
5576 struct got_commitable *ct = pe->data;
5578 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5580 err = got_path_skip_common_ancestor(&relpath,
5581 worktree->root_path, ct->ondisk_path);
5582 if (err)
5583 goto done;
5585 if (ie) {
5586 if (ct->status == GOT_STATUS_DELETE ||
5587 ct->staged_status == GOT_STATUS_DELETE) {
5588 got_fileindex_entry_remove(fileindex, ie);
5589 } else if (ct->staged_status == GOT_STATUS_ADD ||
5590 ct->staged_status == GOT_STATUS_MODIFY) {
5591 got_fileindex_entry_stage_set(ie,
5592 GOT_FILEIDX_STAGE_NONE);
5593 got_fileindex_entry_staged_filetype_set(ie, 0);
5595 err = got_fileindex_entry_update(ie,
5596 worktree->root_fd, relpath,
5597 ct->staged_blob_id->sha1,
5598 new_base_commit_id->sha1,
5599 !have_staged_files);
5600 } else
5601 err = got_fileindex_entry_update(ie,
5602 worktree->root_fd, relpath,
5603 ct->blob_id->sha1,
5604 new_base_commit_id->sha1,
5605 !have_staged_files);
5606 } else {
5607 err = got_fileindex_entry_alloc(&ie, pe->path);
5608 if (err)
5609 goto done;
5610 err = got_fileindex_entry_update(ie,
5611 worktree->root_fd, relpath, ct->blob_id->sha1,
5612 new_base_commit_id->sha1, 1);
5613 if (err) {
5614 got_fileindex_entry_free(ie);
5615 goto done;
5617 err = got_fileindex_entry_add(fileindex, ie);
5618 if (err) {
5619 got_fileindex_entry_free(ie);
5620 goto done;
5623 free(relpath);
5624 relpath = NULL;
5626 done:
5627 free(relpath);
5628 return err;
5632 static const struct got_error *
5633 check_out_of_date(const char *in_repo_path, unsigned char status,
5634 unsigned char staged_status, struct got_object_id *base_blob_id,
5635 struct got_object_id *base_commit_id,
5636 struct got_object_id *head_commit_id, struct got_repository *repo,
5637 int ood_errcode)
5639 const struct got_error *err = NULL;
5640 struct got_commit_object *commit = NULL;
5641 struct got_object_id *id = NULL;
5643 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5644 /* Trivial case: base commit == head commit */
5645 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5646 return NULL;
5648 * Ensure file content which local changes were based
5649 * on matches file content in the branch head.
5651 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5652 if (err)
5653 goto done;
5654 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5655 if (err) {
5656 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5657 err = got_error(ood_errcode);
5658 goto done;
5659 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5660 err = got_error(ood_errcode);
5661 } else {
5662 /* Require that added files don't exist in the branch head. */
5663 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5664 if (err)
5665 goto done;
5666 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5667 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5668 goto done;
5669 err = id ? got_error(ood_errcode) : NULL;
5671 done:
5672 free(id);
5673 if (commit)
5674 got_object_commit_close(commit);
5675 return err;
5678 static const struct got_error *
5679 commit_worktree(struct got_object_id **new_commit_id,
5680 struct got_pathlist_head *commitable_paths,
5681 struct got_object_id *head_commit_id,
5682 struct got_object_id *parent_id2,
5683 struct got_worktree *worktree,
5684 const char *author, const char *committer,
5685 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5686 got_worktree_status_cb status_cb, void *status_arg,
5687 struct got_repository *repo)
5689 const struct got_error *err = NULL, *unlockerr = NULL;
5690 struct got_pathlist_entry *pe;
5691 const char *head_ref_name = NULL;
5692 struct got_commit_object *head_commit = NULL;
5693 struct got_reference *head_ref2 = NULL;
5694 struct got_object_id *head_commit_id2 = NULL;
5695 struct got_tree_object *head_tree = NULL;
5696 struct got_object_id *new_tree_id = NULL;
5697 int nentries, nparents = 0;
5698 struct got_object_id_queue parent_ids;
5699 struct got_object_qid *pid = NULL;
5700 char *logmsg = NULL;
5701 time_t timestamp;
5703 *new_commit_id = NULL;
5705 STAILQ_INIT(&parent_ids);
5707 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5708 if (err)
5709 goto done;
5711 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5712 if (err)
5713 goto done;
5715 if (commit_msg_cb != NULL) {
5716 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5717 if (err)
5718 goto done;
5721 if (logmsg == NULL || strlen(logmsg) == 0) {
5722 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5723 goto done;
5726 /* Create blobs from added and modified files and record their IDs. */
5727 TAILQ_FOREACH(pe, commitable_paths, entry) {
5728 struct got_commitable *ct = pe->data;
5729 char *ondisk_path;
5731 /* Blobs for staged files already exist. */
5732 if (ct->staged_status == GOT_STATUS_ADD ||
5733 ct->staged_status == GOT_STATUS_MODIFY)
5734 continue;
5736 if (ct->status != GOT_STATUS_ADD &&
5737 ct->status != GOT_STATUS_MODIFY &&
5738 ct->status != GOT_STATUS_MODE_CHANGE)
5739 continue;
5741 if (asprintf(&ondisk_path, "%s/%s",
5742 worktree->root_path, pe->path) == -1) {
5743 err = got_error_from_errno("asprintf");
5744 goto done;
5746 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5747 free(ondisk_path);
5748 if (err)
5749 goto done;
5752 /* Recursively write new tree objects. */
5753 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5754 commitable_paths, status_cb, status_arg, repo);
5755 if (err)
5756 goto done;
5758 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5759 if (err)
5760 goto done;
5761 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5762 nparents++;
5763 if (parent_id2) {
5764 err = got_object_qid_alloc(&pid, parent_id2);
5765 if (err)
5766 goto done;
5767 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5768 nparents++;
5770 timestamp = time(NULL);
5771 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5772 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5773 if (logmsg != NULL)
5774 free(logmsg);
5775 if (err)
5776 goto done;
5778 /* Check if a concurrent commit to our branch has occurred. */
5779 head_ref_name = got_worktree_get_head_ref_name(worktree);
5780 if (head_ref_name == NULL) {
5781 err = got_error_from_errno("got_worktree_get_head_ref_name");
5782 goto done;
5784 /* Lock the reference here to prevent concurrent modification. */
5785 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5786 if (err)
5787 goto done;
5788 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5789 if (err)
5790 goto done;
5791 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5792 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5793 goto done;
5795 /* Update branch head in repository. */
5796 err = got_ref_change_ref(head_ref2, *new_commit_id);
5797 if (err)
5798 goto done;
5799 err = got_ref_write(head_ref2, repo);
5800 if (err)
5801 goto done;
5803 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5804 if (err)
5805 goto done;
5807 err = ref_base_commit(worktree, repo);
5808 if (err)
5809 goto done;
5810 done:
5811 got_object_id_queue_free(&parent_ids);
5812 if (head_tree)
5813 got_object_tree_close(head_tree);
5814 if (head_commit)
5815 got_object_commit_close(head_commit);
5816 free(head_commit_id2);
5817 if (head_ref2) {
5818 unlockerr = got_ref_unlock(head_ref2);
5819 if (unlockerr && err == NULL)
5820 err = unlockerr;
5821 got_ref_close(head_ref2);
5823 return err;
5826 static const struct got_error *
5827 check_path_is_commitable(const char *path,
5828 struct got_pathlist_head *commitable_paths)
5830 struct got_pathlist_entry *cpe = NULL;
5831 size_t path_len = strlen(path);
5833 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5834 struct got_commitable *ct = cpe->data;
5835 const char *ct_path = ct->path;
5837 while (ct_path[0] == '/')
5838 ct_path++;
5840 if (strcmp(path, ct_path) == 0 ||
5841 got_path_is_child(ct_path, path, path_len))
5842 break;
5845 if (cpe == NULL)
5846 return got_error_path(path, GOT_ERR_BAD_PATH);
5848 return NULL;
5851 static const struct got_error *
5852 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5854 int *have_staged_files = arg;
5856 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5857 *have_staged_files = 1;
5858 return got_error(GOT_ERR_CANCELLED);
5861 return NULL;
5864 static const struct got_error *
5865 check_non_staged_files(struct got_fileindex *fileindex,
5866 struct got_pathlist_head *paths)
5868 struct got_pathlist_entry *pe;
5869 struct got_fileindex_entry *ie;
5871 TAILQ_FOREACH(pe, paths, entry) {
5872 if (pe->path[0] == '\0')
5873 continue;
5874 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5875 if (ie == NULL)
5876 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5877 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5878 return got_error_path(pe->path,
5879 GOT_ERR_FILE_NOT_STAGED);
5882 return NULL;
5885 const struct got_error *
5886 got_worktree_commit(struct got_object_id **new_commit_id,
5887 struct got_worktree *worktree, struct got_pathlist_head *paths,
5888 const char *author, const char *committer, int allow_bad_symlinks,
5889 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5890 got_worktree_status_cb status_cb, void *status_arg,
5891 struct got_repository *repo)
5893 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5894 struct got_fileindex *fileindex = NULL;
5895 char *fileindex_path = NULL;
5896 struct got_pathlist_head commitable_paths;
5897 struct collect_commitables_arg cc_arg;
5898 struct got_pathlist_entry *pe;
5899 struct got_reference *head_ref = NULL;
5900 struct got_object_id *head_commit_id = NULL;
5901 int have_staged_files = 0;
5903 *new_commit_id = NULL;
5905 TAILQ_INIT(&commitable_paths);
5907 err = lock_worktree(worktree, LOCK_EX);
5908 if (err)
5909 goto done;
5911 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5912 if (err)
5913 goto done;
5915 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5916 if (err)
5917 goto done;
5919 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5920 if (err)
5921 goto done;
5923 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5924 &have_staged_files);
5925 if (err && err->code != GOT_ERR_CANCELLED)
5926 goto done;
5927 if (have_staged_files) {
5928 err = check_non_staged_files(fileindex, paths);
5929 if (err)
5930 goto done;
5933 cc_arg.commitable_paths = &commitable_paths;
5934 cc_arg.worktree = worktree;
5935 cc_arg.fileindex = fileindex;
5936 cc_arg.repo = repo;
5937 cc_arg.have_staged_files = have_staged_files;
5938 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5939 TAILQ_FOREACH(pe, paths, entry) {
5940 err = worktree_status(worktree, pe->path, fileindex, repo,
5941 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5942 if (err)
5943 goto done;
5946 if (TAILQ_EMPTY(&commitable_paths)) {
5947 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5948 goto done;
5951 TAILQ_FOREACH(pe, paths, entry) {
5952 err = check_path_is_commitable(pe->path, &commitable_paths);
5953 if (err)
5954 goto done;
5957 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5958 struct got_commitable *ct = pe->data;
5959 const char *ct_path = ct->in_repo_path;
5961 while (ct_path[0] == '/')
5962 ct_path++;
5963 err = check_out_of_date(ct_path, ct->status,
5964 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5965 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5966 if (err)
5967 goto done;
5971 err = commit_worktree(new_commit_id, &commitable_paths,
5972 head_commit_id, NULL, worktree, author, committer,
5973 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5974 if (err)
5975 goto done;
5977 err = update_fileindex_after_commit(worktree, &commitable_paths,
5978 *new_commit_id, fileindex, have_staged_files);
5979 sync_err = sync_fileindex(fileindex, fileindex_path);
5980 if (sync_err && err == NULL)
5981 err = sync_err;
5982 done:
5983 if (fileindex)
5984 got_fileindex_free(fileindex);
5985 free(fileindex_path);
5986 unlockerr = lock_worktree(worktree, LOCK_SH);
5987 if (unlockerr && err == NULL)
5988 err = unlockerr;
5989 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5990 struct got_commitable *ct = pe->data;
5991 free_commitable(ct);
5993 got_pathlist_free(&commitable_paths);
5994 return err;
5997 const char *
5998 got_commitable_get_path(struct got_commitable *ct)
6000 return ct->path;
6003 unsigned int
6004 got_commitable_get_status(struct got_commitable *ct)
6006 return ct->status;
6009 struct check_rebase_ok_arg {
6010 struct got_worktree *worktree;
6011 struct got_repository *repo;
6014 static const struct got_error *
6015 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6017 const struct got_error *err = NULL;
6018 struct check_rebase_ok_arg *a = arg;
6019 unsigned char status;
6020 struct stat sb;
6021 char *ondisk_path;
6023 /* Reject rebase of a work tree with mixed base commits. */
6024 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6025 SHA1_DIGEST_LENGTH))
6026 return got_error(GOT_ERR_MIXED_COMMITS);
6028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6029 == -1)
6030 return got_error_from_errno("asprintf");
6032 /* Reject rebase of a work tree with modified or staged files. */
6033 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6034 free(ondisk_path);
6035 if (err)
6036 return err;
6038 if (status != GOT_STATUS_NO_CHANGE)
6039 return got_error(GOT_ERR_MODIFIED);
6040 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6041 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6043 return NULL;
6046 const struct got_error *
6047 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6048 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6049 struct got_worktree *worktree, struct got_reference *branch,
6050 struct got_repository *repo)
6052 const struct got_error *err = NULL;
6053 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6054 char *branch_ref_name = NULL;
6055 char *fileindex_path = NULL;
6056 struct check_rebase_ok_arg ok_arg;
6057 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6058 struct got_object_id *wt_branch_tip = NULL;
6060 *new_base_branch_ref = NULL;
6061 *tmp_branch = NULL;
6062 *fileindex = NULL;
6064 err = lock_worktree(worktree, LOCK_EX);
6065 if (err)
6066 return err;
6068 err = open_fileindex(fileindex, &fileindex_path, worktree);
6069 if (err)
6070 goto done;
6072 ok_arg.worktree = worktree;
6073 ok_arg.repo = repo;
6074 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6075 &ok_arg);
6076 if (err)
6077 goto done;
6079 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6080 if (err)
6081 goto done;
6083 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6084 if (err)
6085 goto done;
6087 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6088 if (err)
6089 goto done;
6091 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6092 0);
6093 if (err)
6094 goto done;
6096 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6097 if (err)
6098 goto done;
6099 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6100 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6101 goto done;
6104 err = got_ref_alloc_symref(new_base_branch_ref,
6105 new_base_branch_ref_name, wt_branch);
6106 if (err)
6107 goto done;
6108 err = got_ref_write(*new_base_branch_ref, repo);
6109 if (err)
6110 goto done;
6112 /* TODO Lock original branch's ref while rebasing? */
6114 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6115 if (err)
6116 goto done;
6118 err = got_ref_write(branch_ref, repo);
6119 if (err)
6120 goto done;
6122 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6123 worktree->base_commit_id);
6124 if (err)
6125 goto done;
6126 err = got_ref_write(*tmp_branch, repo);
6127 if (err)
6128 goto done;
6130 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6131 if (err)
6132 goto done;
6133 done:
6134 free(fileindex_path);
6135 free(tmp_branch_name);
6136 free(new_base_branch_ref_name);
6137 free(branch_ref_name);
6138 if (branch_ref)
6139 got_ref_close(branch_ref);
6140 if (wt_branch)
6141 got_ref_close(wt_branch);
6142 free(wt_branch_tip);
6143 if (err) {
6144 if (*new_base_branch_ref) {
6145 got_ref_close(*new_base_branch_ref);
6146 *new_base_branch_ref = NULL;
6148 if (*tmp_branch) {
6149 got_ref_close(*tmp_branch);
6150 *tmp_branch = NULL;
6152 if (*fileindex) {
6153 got_fileindex_free(*fileindex);
6154 *fileindex = NULL;
6156 lock_worktree(worktree, LOCK_SH);
6158 return err;
6161 const struct got_error *
6162 got_worktree_rebase_continue(struct got_object_id **commit_id,
6163 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6164 struct got_reference **branch, struct got_fileindex **fileindex,
6165 struct got_worktree *worktree, struct got_repository *repo)
6167 const struct got_error *err;
6168 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6169 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6170 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6171 char *fileindex_path = NULL;
6172 int have_staged_files = 0;
6174 *commit_id = NULL;
6175 *new_base_branch = NULL;
6176 *tmp_branch = NULL;
6177 *branch = NULL;
6178 *fileindex = NULL;
6180 err = lock_worktree(worktree, LOCK_EX);
6181 if (err)
6182 return err;
6184 err = open_fileindex(fileindex, &fileindex_path, worktree);
6185 if (err)
6186 goto done;
6188 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6189 &have_staged_files);
6190 if (err && err->code != GOT_ERR_CANCELLED)
6191 goto done;
6192 if (have_staged_files) {
6193 err = got_error(GOT_ERR_STAGED_PATHS);
6194 goto done;
6197 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6198 if (err)
6199 goto done;
6201 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6202 if (err)
6203 goto done;
6205 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6206 if (err)
6207 goto done;
6209 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6210 if (err)
6211 goto done;
6213 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6214 if (err)
6215 goto done;
6217 err = got_ref_open(branch, repo,
6218 got_ref_get_symref_target(branch_ref), 0);
6219 if (err)
6220 goto done;
6222 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6223 if (err)
6224 goto done;
6226 err = got_ref_resolve(commit_id, repo, commit_ref);
6227 if (err)
6228 goto done;
6230 err = got_ref_open(new_base_branch, repo,
6231 new_base_branch_ref_name, 0);
6232 if (err)
6233 goto done;
6235 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6236 if (err)
6237 goto done;
6238 done:
6239 free(commit_ref_name);
6240 free(branch_ref_name);
6241 free(fileindex_path);
6242 if (commit_ref)
6243 got_ref_close(commit_ref);
6244 if (branch_ref)
6245 got_ref_close(branch_ref);
6246 if (err) {
6247 free(*commit_id);
6248 *commit_id = NULL;
6249 if (*tmp_branch) {
6250 got_ref_close(*tmp_branch);
6251 *tmp_branch = NULL;
6253 if (*new_base_branch) {
6254 got_ref_close(*new_base_branch);
6255 *new_base_branch = NULL;
6257 if (*branch) {
6258 got_ref_close(*branch);
6259 *branch = NULL;
6261 if (*fileindex) {
6262 got_fileindex_free(*fileindex);
6263 *fileindex = NULL;
6265 lock_worktree(worktree, LOCK_SH);
6267 return err;
6270 const struct got_error *
6271 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6273 const struct got_error *err;
6274 char *tmp_branch_name = NULL;
6276 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6277 if (err)
6278 return err;
6280 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6281 free(tmp_branch_name);
6282 return NULL;
6285 static const struct got_error *
6286 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6287 char **logmsg, void *arg)
6289 *logmsg = arg;
6290 return NULL;
6293 static const struct got_error *
6294 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6295 const char *path, struct got_object_id *blob_id,
6296 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6297 int dirfd, const char *de_name)
6299 return NULL;
6302 struct collect_merged_paths_arg {
6303 got_worktree_checkout_cb progress_cb;
6304 void *progress_arg;
6305 struct got_pathlist_head *merged_paths;
6308 static const struct got_error *
6309 collect_merged_paths(void *arg, unsigned char status, const char *path)
6311 const struct got_error *err;
6312 struct collect_merged_paths_arg *a = arg;
6313 char *p;
6314 struct got_pathlist_entry *new;
6316 err = (*a->progress_cb)(a->progress_arg, status, path);
6317 if (err)
6318 return err;
6320 if (status != GOT_STATUS_MERGE &&
6321 status != GOT_STATUS_ADD &&
6322 status != GOT_STATUS_DELETE &&
6323 status != GOT_STATUS_CONFLICT)
6324 return NULL;
6326 p = strdup(path);
6327 if (p == NULL)
6328 return got_error_from_errno("strdup");
6330 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6331 if (err || new == NULL)
6332 free(p);
6333 return err;
6336 void
6337 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6339 struct got_pathlist_entry *pe;
6341 TAILQ_FOREACH(pe, merged_paths, entry)
6342 free((char *)pe->path);
6344 got_pathlist_free(merged_paths);
6347 static const struct got_error *
6348 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6349 int is_rebase, struct got_repository *repo)
6351 const struct got_error *err;
6352 struct got_reference *commit_ref = NULL;
6354 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6355 if (err) {
6356 if (err->code != GOT_ERR_NOT_REF)
6357 goto done;
6358 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6359 if (err)
6360 goto done;
6361 err = got_ref_write(commit_ref, repo);
6362 if (err)
6363 goto done;
6364 } else if (is_rebase) {
6365 struct got_object_id *stored_id;
6366 int cmp;
6368 err = got_ref_resolve(&stored_id, repo, commit_ref);
6369 if (err)
6370 goto done;
6371 cmp = got_object_id_cmp(commit_id, stored_id);
6372 free(stored_id);
6373 if (cmp != 0) {
6374 err = got_error(GOT_ERR_REBASE_COMMITID);
6375 goto done;
6378 done:
6379 if (commit_ref)
6380 got_ref_close(commit_ref);
6381 return err;
6384 static const struct got_error *
6385 rebase_merge_files(struct got_pathlist_head *merged_paths,
6386 const char *commit_ref_name, struct got_worktree *worktree,
6387 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6388 struct got_object_id *commit_id, struct got_repository *repo,
6389 got_worktree_checkout_cb progress_cb, void *progress_arg,
6390 got_cancel_cb cancel_cb, void *cancel_arg)
6392 const struct got_error *err;
6393 struct got_reference *commit_ref = NULL;
6394 struct collect_merged_paths_arg cmp_arg;
6395 char *fileindex_path;
6397 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6399 err = get_fileindex_path(&fileindex_path, worktree);
6400 if (err)
6401 return err;
6403 cmp_arg.progress_cb = progress_cb;
6404 cmp_arg.progress_arg = progress_arg;
6405 cmp_arg.merged_paths = merged_paths;
6406 err = merge_files(worktree, fileindex, fileindex_path,
6407 parent_commit_id, commit_id, repo, collect_merged_paths,
6408 &cmp_arg, cancel_cb, cancel_arg);
6409 if (commit_ref)
6410 got_ref_close(commit_ref);
6411 return err;
6414 const struct got_error *
6415 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6416 struct got_worktree *worktree, struct got_fileindex *fileindex,
6417 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6418 struct got_repository *repo,
6419 got_worktree_checkout_cb progress_cb, void *progress_arg,
6420 got_cancel_cb cancel_cb, void *cancel_arg)
6422 const struct got_error *err;
6423 char *commit_ref_name;
6425 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6426 if (err)
6427 return err;
6429 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6430 if (err)
6431 goto done;
6433 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6434 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6435 progress_arg, cancel_cb, cancel_arg);
6436 done:
6437 free(commit_ref_name);
6438 return err;
6441 const struct got_error *
6442 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6443 struct got_worktree *worktree, struct got_fileindex *fileindex,
6444 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6445 struct got_repository *repo,
6446 got_worktree_checkout_cb progress_cb, void *progress_arg,
6447 got_cancel_cb cancel_cb, void *cancel_arg)
6449 const struct got_error *err;
6450 char *commit_ref_name;
6452 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6453 if (err)
6454 return err;
6456 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6457 if (err)
6458 goto done;
6460 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6461 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6462 progress_arg, cancel_cb, cancel_arg);
6463 done:
6464 free(commit_ref_name);
6465 return err;
6468 static const struct got_error *
6469 rebase_commit(struct got_object_id **new_commit_id,
6470 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6471 struct got_worktree *worktree, struct got_fileindex *fileindex,
6472 struct got_reference *tmp_branch, const char *committer,
6473 struct got_commit_object *orig_commit, const char *new_logmsg,
6474 struct got_repository *repo)
6476 const struct got_error *err, *sync_err;
6477 struct got_pathlist_head commitable_paths;
6478 struct collect_commitables_arg cc_arg;
6479 char *fileindex_path = NULL;
6480 struct got_reference *head_ref = NULL;
6481 struct got_object_id *head_commit_id = NULL;
6482 char *logmsg = NULL;
6484 TAILQ_INIT(&commitable_paths);
6485 *new_commit_id = NULL;
6487 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6489 err = get_fileindex_path(&fileindex_path, worktree);
6490 if (err)
6491 return err;
6493 cc_arg.commitable_paths = &commitable_paths;
6494 cc_arg.worktree = worktree;
6495 cc_arg.repo = repo;
6496 cc_arg.have_staged_files = 0;
6498 * If possible get the status of individual files directly to
6499 * avoid crawling the entire work tree once per rebased commit.
6501 * Ideally, merged_paths would contain a list of commitables
6502 * we could use so we could skip worktree_status() entirely.
6503 * However, we would then need carefully keep track of cumulative
6504 * effects of operations such as file additions and deletions
6505 * in 'got histedit -f' (folding multiple commits into one),
6506 * and this extra complexity is not really worth it.
6508 if (merged_paths) {
6509 struct got_pathlist_entry *pe;
6510 TAILQ_FOREACH(pe, merged_paths, entry) {
6511 err = worktree_status(worktree, pe->path, fileindex,
6512 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6513 0);
6514 if (err)
6515 goto done;
6517 } else {
6518 err = worktree_status(worktree, "", fileindex, repo,
6519 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6520 if (err)
6521 goto done;
6524 if (TAILQ_EMPTY(&commitable_paths)) {
6525 /* No-op change; commit will be elided. */
6526 err = got_ref_delete(commit_ref, repo);
6527 if (err)
6528 goto done;
6529 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6530 goto done;
6533 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6534 if (err)
6535 goto done;
6537 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6538 if (err)
6539 goto done;
6541 if (new_logmsg) {
6542 logmsg = strdup(new_logmsg);
6543 if (logmsg == NULL) {
6544 err = got_error_from_errno("strdup");
6545 goto done;
6547 } else {
6548 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6549 if (err)
6550 goto done;
6553 /* NB: commit_worktree will call free(logmsg) */
6554 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6555 NULL, worktree, got_object_commit_get_author(orig_commit),
6556 committer ? committer :
6557 got_object_commit_get_committer(orig_commit),
6558 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6559 if (err)
6560 goto done;
6562 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6563 if (err)
6564 goto done;
6566 err = got_ref_delete(commit_ref, repo);
6567 if (err)
6568 goto done;
6570 err = update_fileindex_after_commit(worktree, &commitable_paths,
6571 *new_commit_id, fileindex, 0);
6572 sync_err = sync_fileindex(fileindex, fileindex_path);
6573 if (sync_err && err == NULL)
6574 err = sync_err;
6575 done:
6576 free(fileindex_path);
6577 free(head_commit_id);
6578 if (head_ref)
6579 got_ref_close(head_ref);
6580 if (err) {
6581 free(*new_commit_id);
6582 *new_commit_id = NULL;
6584 return err;
6587 const struct got_error *
6588 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6589 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6590 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6591 const char *committer, struct got_commit_object *orig_commit,
6592 struct got_object_id *orig_commit_id, struct got_repository *repo)
6594 const struct got_error *err;
6595 char *commit_ref_name;
6596 struct got_reference *commit_ref = NULL;
6597 struct got_object_id *commit_id = NULL;
6599 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6600 if (err)
6601 return err;
6603 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6604 if (err)
6605 goto done;
6606 err = got_ref_resolve(&commit_id, repo, commit_ref);
6607 if (err)
6608 goto done;
6609 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6610 err = got_error(GOT_ERR_REBASE_COMMITID);
6611 goto done;
6614 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6615 worktree, fileindex, tmp_branch, committer, orig_commit,
6616 NULL, repo);
6617 done:
6618 if (commit_ref)
6619 got_ref_close(commit_ref);
6620 free(commit_ref_name);
6621 free(commit_id);
6622 return err;
6625 const struct got_error *
6626 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6627 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6628 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6629 const char *committer, struct got_commit_object *orig_commit,
6630 struct got_object_id *orig_commit_id, const char *new_logmsg,
6631 struct got_repository *repo)
6633 const struct got_error *err;
6634 char *commit_ref_name;
6635 struct got_reference *commit_ref = NULL;
6637 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6638 if (err)
6639 return err;
6641 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6642 if (err)
6643 goto done;
6645 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6646 worktree, fileindex, tmp_branch, committer, orig_commit,
6647 new_logmsg, repo);
6648 done:
6649 if (commit_ref)
6650 got_ref_close(commit_ref);
6651 free(commit_ref_name);
6652 return err;
6655 const struct got_error *
6656 got_worktree_rebase_postpone(struct got_worktree *worktree,
6657 struct got_fileindex *fileindex)
6659 if (fileindex)
6660 got_fileindex_free(fileindex);
6661 return lock_worktree(worktree, LOCK_SH);
6664 static const struct got_error *
6665 delete_ref(const char *name, struct got_repository *repo)
6667 const struct got_error *err;
6668 struct got_reference *ref;
6670 err = got_ref_open(&ref, repo, name, 0);
6671 if (err) {
6672 if (err->code == GOT_ERR_NOT_REF)
6673 return NULL;
6674 return err;
6677 err = got_ref_delete(ref, repo);
6678 got_ref_close(ref);
6679 return err;
6682 static const struct got_error *
6683 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6685 const struct got_error *err;
6686 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6687 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6689 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6690 if (err)
6691 goto done;
6692 err = delete_ref(tmp_branch_name, repo);
6693 if (err)
6694 goto done;
6696 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6697 if (err)
6698 goto done;
6699 err = delete_ref(new_base_branch_ref_name, repo);
6700 if (err)
6701 goto done;
6703 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6704 if (err)
6705 goto done;
6706 err = delete_ref(branch_ref_name, repo);
6707 if (err)
6708 goto done;
6710 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6711 if (err)
6712 goto done;
6713 err = delete_ref(commit_ref_name, repo);
6714 if (err)
6715 goto done;
6717 done:
6718 free(tmp_branch_name);
6719 free(new_base_branch_ref_name);
6720 free(branch_ref_name);
6721 free(commit_ref_name);
6722 return err;
6725 static const struct got_error *
6726 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6727 struct got_object_id *new_commit_id, struct got_repository *repo)
6729 const struct got_error *err;
6730 struct got_reference *ref = NULL;
6731 struct got_object_id *old_commit_id = NULL;
6732 const char *branch_name = NULL;
6733 char *new_id_str = NULL;
6734 char *refname = NULL;
6736 branch_name = got_ref_get_name(branch);
6737 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6738 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6739 branch_name += 11;
6741 err = got_object_id_str(&new_id_str, new_commit_id);
6742 if (err)
6743 return err;
6745 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6746 new_id_str) == -1) {
6747 err = got_error_from_errno("asprintf");
6748 goto done;
6751 err = got_ref_resolve(&old_commit_id, repo, branch);
6752 if (err)
6753 goto done;
6755 err = got_ref_alloc(&ref, refname, old_commit_id);
6756 if (err)
6757 goto done;
6759 err = got_ref_write(ref, repo);
6760 done:
6761 free(new_id_str);
6762 free(refname);
6763 free(old_commit_id);
6764 if (ref)
6765 got_ref_close(ref);
6766 return err;
6769 const struct got_error *
6770 got_worktree_rebase_complete(struct got_worktree *worktree,
6771 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6772 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6773 struct got_repository *repo, int create_backup)
6775 const struct got_error *err, *unlockerr, *sync_err;
6776 struct got_object_id *new_head_commit_id = NULL;
6777 char *fileindex_path = NULL;
6779 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6780 if (err)
6781 return err;
6783 if (create_backup) {
6784 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6785 rebased_branch, new_head_commit_id, repo);
6786 if (err)
6787 goto done;
6790 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6791 if (err)
6792 goto done;
6794 err = got_ref_write(rebased_branch, repo);
6795 if (err)
6796 goto done;
6798 err = got_worktree_set_head_ref(worktree, rebased_branch);
6799 if (err)
6800 goto done;
6802 err = delete_rebase_refs(worktree, repo);
6803 if (err)
6804 goto done;
6806 err = get_fileindex_path(&fileindex_path, worktree);
6807 if (err)
6808 goto done;
6809 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6810 sync_err = sync_fileindex(fileindex, fileindex_path);
6811 if (sync_err && err == NULL)
6812 err = sync_err;
6813 done:
6814 got_fileindex_free(fileindex);
6815 free(fileindex_path);
6816 free(new_head_commit_id);
6817 unlockerr = lock_worktree(worktree, LOCK_SH);
6818 if (unlockerr && err == NULL)
6819 err = unlockerr;
6820 return err;
6823 const struct got_error *
6824 got_worktree_rebase_abort(struct got_worktree *worktree,
6825 struct got_fileindex *fileindex, struct got_repository *repo,
6826 struct got_reference *new_base_branch,
6827 got_worktree_checkout_cb progress_cb, void *progress_arg)
6829 const struct got_error *err, *unlockerr, *sync_err;
6830 struct got_reference *resolved = NULL;
6831 struct got_object_id *commit_id = NULL;
6832 struct got_commit_object *commit = NULL;
6833 char *fileindex_path = NULL;
6834 struct revert_file_args rfa;
6835 struct got_object_id *tree_id = NULL;
6837 err = lock_worktree(worktree, LOCK_EX);
6838 if (err)
6839 return err;
6841 err = got_object_open_as_commit(&commit, repo,
6842 worktree->base_commit_id);
6843 if (err)
6844 goto done;
6846 err = got_ref_open(&resolved, repo,
6847 got_ref_get_symref_target(new_base_branch), 0);
6848 if (err)
6849 goto done;
6851 err = got_worktree_set_head_ref(worktree, resolved);
6852 if (err)
6853 goto done;
6856 * XXX commits to the base branch could have happened while
6857 * we were busy rebasing; should we store the original commit ID
6858 * when rebase begins and read it back here?
6860 err = got_ref_resolve(&commit_id, repo, resolved);
6861 if (err)
6862 goto done;
6864 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6865 if (err)
6866 goto done;
6868 err = got_object_id_by_path(&tree_id, repo, commit,
6869 worktree->path_prefix);
6870 if (err)
6871 goto done;
6873 err = delete_rebase_refs(worktree, repo);
6874 if (err)
6875 goto done;
6877 err = get_fileindex_path(&fileindex_path, worktree);
6878 if (err)
6879 goto done;
6881 rfa.worktree = worktree;
6882 rfa.fileindex = fileindex;
6883 rfa.progress_cb = progress_cb;
6884 rfa.progress_arg = progress_arg;
6885 rfa.patch_cb = NULL;
6886 rfa.patch_arg = NULL;
6887 rfa.repo = repo;
6888 rfa.unlink_added_files = 0;
6889 err = worktree_status(worktree, "", fileindex, repo,
6890 revert_file, &rfa, NULL, NULL, 1, 0);
6891 if (err)
6892 goto sync;
6894 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6895 repo, progress_cb, progress_arg, NULL, NULL);
6896 sync:
6897 sync_err = sync_fileindex(fileindex, fileindex_path);
6898 if (sync_err && err == NULL)
6899 err = sync_err;
6900 done:
6901 got_ref_close(resolved);
6902 free(tree_id);
6903 free(commit_id);
6904 if (commit)
6905 got_object_commit_close(commit);
6906 if (fileindex)
6907 got_fileindex_free(fileindex);
6908 free(fileindex_path);
6910 unlockerr = lock_worktree(worktree, LOCK_SH);
6911 if (unlockerr && err == NULL)
6912 err = unlockerr;
6913 return err;
6916 const struct got_error *
6917 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6918 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6919 struct got_fileindex **fileindex, struct got_worktree *worktree,
6920 struct got_repository *repo)
6922 const struct got_error *err = NULL;
6923 char *tmp_branch_name = NULL;
6924 char *branch_ref_name = NULL;
6925 char *base_commit_ref_name = NULL;
6926 char *fileindex_path = NULL;
6927 struct check_rebase_ok_arg ok_arg;
6928 struct got_reference *wt_branch = NULL;
6929 struct got_reference *base_commit_ref = NULL;
6931 *tmp_branch = NULL;
6932 *branch_ref = NULL;
6933 *base_commit_id = NULL;
6934 *fileindex = NULL;
6936 err = lock_worktree(worktree, LOCK_EX);
6937 if (err)
6938 return err;
6940 err = open_fileindex(fileindex, &fileindex_path, worktree);
6941 if (err)
6942 goto done;
6944 ok_arg.worktree = worktree;
6945 ok_arg.repo = repo;
6946 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6947 &ok_arg);
6948 if (err)
6949 goto done;
6951 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6952 if (err)
6953 goto done;
6955 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6956 if (err)
6957 goto done;
6959 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6960 worktree);
6961 if (err)
6962 goto done;
6964 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6965 0);
6966 if (err)
6967 goto done;
6969 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6970 if (err)
6971 goto done;
6973 err = got_ref_write(*branch_ref, repo);
6974 if (err)
6975 goto done;
6977 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6978 worktree->base_commit_id);
6979 if (err)
6980 goto done;
6981 err = got_ref_write(base_commit_ref, repo);
6982 if (err)
6983 goto done;
6984 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6985 if (*base_commit_id == NULL) {
6986 err = got_error_from_errno("got_object_id_dup");
6987 goto done;
6990 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6991 worktree->base_commit_id);
6992 if (err)
6993 goto done;
6994 err = got_ref_write(*tmp_branch, repo);
6995 if (err)
6996 goto done;
6998 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6999 if (err)
7000 goto done;
7001 done:
7002 free(fileindex_path);
7003 free(tmp_branch_name);
7004 free(branch_ref_name);
7005 free(base_commit_ref_name);
7006 if (wt_branch)
7007 got_ref_close(wt_branch);
7008 if (err) {
7009 if (*branch_ref) {
7010 got_ref_close(*branch_ref);
7011 *branch_ref = NULL;
7013 if (*tmp_branch) {
7014 got_ref_close(*tmp_branch);
7015 *tmp_branch = NULL;
7017 free(*base_commit_id);
7018 if (*fileindex) {
7019 got_fileindex_free(*fileindex);
7020 *fileindex = NULL;
7022 lock_worktree(worktree, LOCK_SH);
7024 return err;
7027 const struct got_error *
7028 got_worktree_histedit_postpone(struct got_worktree *worktree,
7029 struct got_fileindex *fileindex)
7031 if (fileindex)
7032 got_fileindex_free(fileindex);
7033 return lock_worktree(worktree, LOCK_SH);
7036 const struct got_error *
7037 got_worktree_histedit_in_progress(int *in_progress,
7038 struct got_worktree *worktree)
7040 const struct got_error *err;
7041 char *tmp_branch_name = NULL;
7043 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7044 if (err)
7045 return err;
7047 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7048 free(tmp_branch_name);
7049 return NULL;
7052 const struct got_error *
7053 got_worktree_histedit_continue(struct got_object_id **commit_id,
7054 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7055 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7056 struct got_worktree *worktree, struct got_repository *repo)
7058 const struct got_error *err;
7059 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7060 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7061 struct got_reference *commit_ref = NULL;
7062 struct got_reference *base_commit_ref = NULL;
7063 char *fileindex_path = NULL;
7064 int have_staged_files = 0;
7066 *commit_id = NULL;
7067 *tmp_branch = NULL;
7068 *base_commit_id = NULL;
7069 *fileindex = NULL;
7071 err = lock_worktree(worktree, LOCK_EX);
7072 if (err)
7073 return err;
7075 err = open_fileindex(fileindex, &fileindex_path, worktree);
7076 if (err)
7077 goto done;
7079 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7080 &have_staged_files);
7081 if (err && err->code != GOT_ERR_CANCELLED)
7082 goto done;
7083 if (have_staged_files) {
7084 err = got_error(GOT_ERR_STAGED_PATHS);
7085 goto done;
7088 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7089 if (err)
7090 goto done;
7092 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7093 if (err)
7094 goto done;
7096 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7097 if (err)
7098 goto done;
7100 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7101 worktree);
7102 if (err)
7103 goto done;
7105 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7106 if (err)
7107 goto done;
7109 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7110 if (err)
7111 goto done;
7112 err = got_ref_resolve(commit_id, repo, commit_ref);
7113 if (err)
7114 goto done;
7116 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7117 if (err)
7118 goto done;
7119 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7120 if (err)
7121 goto done;
7123 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7124 if (err)
7125 goto done;
7126 done:
7127 free(commit_ref_name);
7128 free(branch_ref_name);
7129 free(fileindex_path);
7130 if (commit_ref)
7131 got_ref_close(commit_ref);
7132 if (base_commit_ref)
7133 got_ref_close(base_commit_ref);
7134 if (err) {
7135 free(*commit_id);
7136 *commit_id = NULL;
7137 free(*base_commit_id);
7138 *base_commit_id = NULL;
7139 if (*tmp_branch) {
7140 got_ref_close(*tmp_branch);
7141 *tmp_branch = NULL;
7143 if (*fileindex) {
7144 got_fileindex_free(*fileindex);
7145 *fileindex = NULL;
7147 lock_worktree(worktree, LOCK_EX);
7149 return err;
7152 static const struct got_error *
7153 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7155 const struct got_error *err;
7156 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7157 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7159 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7160 if (err)
7161 goto done;
7162 err = delete_ref(tmp_branch_name, repo);
7163 if (err)
7164 goto done;
7166 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7167 worktree);
7168 if (err)
7169 goto done;
7170 err = delete_ref(base_commit_ref_name, repo);
7171 if (err)
7172 goto done;
7174 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7175 if (err)
7176 goto done;
7177 err = delete_ref(branch_ref_name, repo);
7178 if (err)
7179 goto done;
7181 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7182 if (err)
7183 goto done;
7184 err = delete_ref(commit_ref_name, repo);
7185 if (err)
7186 goto done;
7187 done:
7188 free(tmp_branch_name);
7189 free(base_commit_ref_name);
7190 free(branch_ref_name);
7191 free(commit_ref_name);
7192 return err;
7195 const struct got_error *
7196 got_worktree_histedit_abort(struct got_worktree *worktree,
7197 struct got_fileindex *fileindex, struct got_repository *repo,
7198 struct got_reference *branch, struct got_object_id *base_commit_id,
7199 got_worktree_checkout_cb progress_cb, void *progress_arg)
7201 const struct got_error *err, *unlockerr, *sync_err;
7202 struct got_reference *resolved = NULL;
7203 char *fileindex_path = NULL;
7204 struct got_commit_object *commit = NULL;
7205 struct got_object_id *tree_id = NULL;
7206 struct revert_file_args rfa;
7208 err = lock_worktree(worktree, LOCK_EX);
7209 if (err)
7210 return err;
7212 err = got_object_open_as_commit(&commit, repo,
7213 worktree->base_commit_id);
7214 if (err)
7215 goto done;
7217 err = got_ref_open(&resolved, repo,
7218 got_ref_get_symref_target(branch), 0);
7219 if (err)
7220 goto done;
7222 err = got_worktree_set_head_ref(worktree, resolved);
7223 if (err)
7224 goto done;
7226 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7227 if (err)
7228 goto done;
7230 err = got_object_id_by_path(&tree_id, repo, commit,
7231 worktree->path_prefix);
7232 if (err)
7233 goto done;
7235 err = delete_histedit_refs(worktree, repo);
7236 if (err)
7237 goto done;
7239 err = get_fileindex_path(&fileindex_path, worktree);
7240 if (err)
7241 goto done;
7243 rfa.worktree = worktree;
7244 rfa.fileindex = fileindex;
7245 rfa.progress_cb = progress_cb;
7246 rfa.progress_arg = progress_arg;
7247 rfa.patch_cb = NULL;
7248 rfa.patch_arg = NULL;
7249 rfa.repo = repo;
7250 rfa.unlink_added_files = 0;
7251 err = worktree_status(worktree, "", fileindex, repo,
7252 revert_file, &rfa, NULL, NULL, 1, 0);
7253 if (err)
7254 goto sync;
7256 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7257 repo, progress_cb, progress_arg, NULL, NULL);
7258 sync:
7259 sync_err = sync_fileindex(fileindex, fileindex_path);
7260 if (sync_err && err == NULL)
7261 err = sync_err;
7262 done:
7263 got_ref_close(resolved);
7264 free(tree_id);
7265 free(fileindex_path);
7267 unlockerr = lock_worktree(worktree, LOCK_SH);
7268 if (unlockerr && err == NULL)
7269 err = unlockerr;
7270 return err;
7273 const struct got_error *
7274 got_worktree_histedit_complete(struct got_worktree *worktree,
7275 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7276 struct got_reference *edited_branch, struct got_repository *repo)
7278 const struct got_error *err, *unlockerr, *sync_err;
7279 struct got_object_id *new_head_commit_id = NULL;
7280 struct got_reference *resolved = NULL;
7281 char *fileindex_path = NULL;
7283 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7284 if (err)
7285 return err;
7287 err = got_ref_open(&resolved, repo,
7288 got_ref_get_symref_target(edited_branch), 0);
7289 if (err)
7290 goto done;
7292 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7293 resolved, new_head_commit_id, repo);
7294 if (err)
7295 goto done;
7297 err = got_ref_change_ref(resolved, new_head_commit_id);
7298 if (err)
7299 goto done;
7301 err = got_ref_write(resolved, repo);
7302 if (err)
7303 goto done;
7305 err = got_worktree_set_head_ref(worktree, resolved);
7306 if (err)
7307 goto done;
7309 err = delete_histedit_refs(worktree, repo);
7310 if (err)
7311 goto done;
7313 err = get_fileindex_path(&fileindex_path, worktree);
7314 if (err)
7315 goto done;
7316 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7317 sync_err = sync_fileindex(fileindex, fileindex_path);
7318 if (sync_err && err == NULL)
7319 err = sync_err;
7320 done:
7321 got_fileindex_free(fileindex);
7322 free(fileindex_path);
7323 free(new_head_commit_id);
7324 unlockerr = lock_worktree(worktree, LOCK_SH);
7325 if (unlockerr && err == NULL)
7326 err = unlockerr;
7327 return err;
7330 const struct got_error *
7331 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7332 struct got_object_id *commit_id, struct got_repository *repo)
7334 const struct got_error *err;
7335 char *commit_ref_name;
7337 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7338 if (err)
7339 return err;
7341 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7342 if (err)
7343 goto done;
7345 err = delete_ref(commit_ref_name, repo);
7346 done:
7347 free(commit_ref_name);
7348 return err;
7351 const struct got_error *
7352 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7353 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7354 struct got_worktree *worktree, const char *refname,
7355 struct got_repository *repo)
7357 const struct got_error *err = NULL;
7358 char *fileindex_path = NULL;
7359 struct check_rebase_ok_arg ok_arg;
7361 *fileindex = NULL;
7362 *branch_ref = NULL;
7363 *base_branch_ref = NULL;
7365 err = lock_worktree(worktree, LOCK_EX);
7366 if (err)
7367 return err;
7369 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7370 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7371 "cannot integrate a branch into itself; "
7372 "update -b or different branch name required");
7373 goto done;
7376 err = open_fileindex(fileindex, &fileindex_path, worktree);
7377 if (err)
7378 goto done;
7380 /* Preconditions are the same as for rebase. */
7381 ok_arg.worktree = worktree;
7382 ok_arg.repo = repo;
7383 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7384 &ok_arg);
7385 if (err)
7386 goto done;
7388 err = got_ref_open(branch_ref, repo, refname, 1);
7389 if (err)
7390 goto done;
7392 err = got_ref_open(base_branch_ref, repo,
7393 got_worktree_get_head_ref_name(worktree), 1);
7394 done:
7395 if (err) {
7396 if (*branch_ref) {
7397 got_ref_close(*branch_ref);
7398 *branch_ref = NULL;
7400 if (*base_branch_ref) {
7401 got_ref_close(*base_branch_ref);
7402 *base_branch_ref = NULL;
7404 if (*fileindex) {
7405 got_fileindex_free(*fileindex);
7406 *fileindex = NULL;
7408 lock_worktree(worktree, LOCK_SH);
7410 return err;
7413 const struct got_error *
7414 got_worktree_integrate_continue(struct got_worktree *worktree,
7415 struct got_fileindex *fileindex, struct got_repository *repo,
7416 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7417 got_worktree_checkout_cb progress_cb, void *progress_arg,
7418 got_cancel_cb cancel_cb, void *cancel_arg)
7420 const struct got_error *err = NULL, *sync_err, *unlockerr;
7421 char *fileindex_path = NULL;
7422 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7423 struct got_commit_object *commit = NULL;
7425 err = get_fileindex_path(&fileindex_path, worktree);
7426 if (err)
7427 goto done;
7429 err = got_ref_resolve(&commit_id, repo, branch_ref);
7430 if (err)
7431 goto done;
7433 err = got_object_open_as_commit(&commit, repo, commit_id);
7434 if (err)
7435 goto done;
7437 err = got_object_id_by_path(&tree_id, repo, commit,
7438 worktree->path_prefix);
7439 if (err)
7440 goto done;
7442 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7443 if (err)
7444 goto done;
7446 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7447 progress_cb, progress_arg, cancel_cb, cancel_arg);
7448 if (err)
7449 goto sync;
7451 err = got_ref_change_ref(base_branch_ref, commit_id);
7452 if (err)
7453 goto sync;
7455 err = got_ref_write(base_branch_ref, repo);
7456 if (err)
7457 goto sync;
7459 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7460 sync:
7461 sync_err = sync_fileindex(fileindex, fileindex_path);
7462 if (sync_err && err == NULL)
7463 err = sync_err;
7465 done:
7466 unlockerr = got_ref_unlock(branch_ref);
7467 if (unlockerr && err == NULL)
7468 err = unlockerr;
7469 got_ref_close(branch_ref);
7471 unlockerr = got_ref_unlock(base_branch_ref);
7472 if (unlockerr && err == NULL)
7473 err = unlockerr;
7474 got_ref_close(base_branch_ref);
7476 got_fileindex_free(fileindex);
7477 free(fileindex_path);
7478 free(tree_id);
7479 if (commit)
7480 got_object_commit_close(commit);
7482 unlockerr = lock_worktree(worktree, LOCK_SH);
7483 if (unlockerr && err == NULL)
7484 err = unlockerr;
7485 return err;
7488 const struct got_error *
7489 got_worktree_integrate_abort(struct got_worktree *worktree,
7490 struct got_fileindex *fileindex, struct got_repository *repo,
7491 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7493 const struct got_error *err = NULL, *unlockerr = NULL;
7495 got_fileindex_free(fileindex);
7497 err = lock_worktree(worktree, LOCK_SH);
7499 unlockerr = got_ref_unlock(branch_ref);
7500 if (unlockerr && err == NULL)
7501 err = unlockerr;
7502 got_ref_close(branch_ref);
7504 unlockerr = got_ref_unlock(base_branch_ref);
7505 if (unlockerr && err == NULL)
7506 err = unlockerr;
7507 got_ref_close(base_branch_ref);
7509 return err;
7512 const struct got_error *
7513 got_worktree_merge_postpone(struct got_worktree *worktree,
7514 struct got_fileindex *fileindex)
7516 const struct got_error *err, *sync_err;
7517 char *fileindex_path = NULL;
7519 err = get_fileindex_path(&fileindex_path, worktree);
7520 if (err)
7521 goto done;
7523 sync_err = sync_fileindex(fileindex, fileindex_path);
7525 err = lock_worktree(worktree, LOCK_SH);
7526 if (sync_err && err == NULL)
7527 err = sync_err;
7528 done:
7529 got_fileindex_free(fileindex);
7530 free(fileindex_path);
7531 return err;
7534 static const struct got_error *
7535 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7537 const struct got_error *err;
7538 char *branch_refname = NULL, *commit_refname = NULL;
7540 err = get_merge_branch_ref_name(&branch_refname, worktree);
7541 if (err)
7542 goto done;
7543 err = delete_ref(branch_refname, repo);
7544 if (err)
7545 goto done;
7547 err = get_merge_commit_ref_name(&commit_refname, worktree);
7548 if (err)
7549 goto done;
7550 err = delete_ref(commit_refname, repo);
7551 if (err)
7552 goto done;
7554 done:
7555 free(branch_refname);
7556 free(commit_refname);
7557 return err;
7560 struct merge_commit_msg_arg {
7561 struct got_worktree *worktree;
7562 const char *branch_name;
7565 static const struct got_error *
7566 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7567 void *arg)
7569 struct merge_commit_msg_arg *a = arg;
7571 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7572 got_worktree_get_head_ref_name(a->worktree)) == -1)
7573 return got_error_from_errno("asprintf");
7575 return NULL;
7579 const struct got_error *
7580 got_worktree_merge_branch(struct got_worktree *worktree,
7581 struct got_fileindex *fileindex,
7582 struct got_object_id *yca_commit_id,
7583 struct got_object_id *branch_tip,
7584 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7585 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7587 const struct got_error *err;
7588 char *fileindex_path = NULL;
7590 err = get_fileindex_path(&fileindex_path, worktree);
7591 if (err)
7592 goto done;
7594 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7595 worktree);
7596 if (err)
7597 goto done;
7599 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7600 branch_tip, repo, progress_cb, progress_arg,
7601 cancel_cb, cancel_arg);
7602 done:
7603 free(fileindex_path);
7604 return err;
7607 const struct got_error *
7608 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7609 struct got_worktree *worktree, struct got_fileindex *fileindex,
7610 const char *author, const char *committer, int allow_bad_symlinks,
7611 struct got_object_id *branch_tip, const char *branch_name,
7612 struct got_repository *repo,
7613 got_worktree_status_cb status_cb, void *status_arg)
7616 const struct got_error *err = NULL, *sync_err;
7617 struct got_pathlist_head commitable_paths;
7618 struct collect_commitables_arg cc_arg;
7619 struct got_pathlist_entry *pe;
7620 struct got_reference *head_ref = NULL;
7621 struct got_object_id *head_commit_id = NULL;
7622 int have_staged_files = 0;
7623 struct merge_commit_msg_arg mcm_arg;
7624 char *fileindex_path = NULL;
7626 *new_commit_id = NULL;
7628 TAILQ_INIT(&commitable_paths);
7630 err = get_fileindex_path(&fileindex_path, worktree);
7631 if (err)
7632 goto done;
7634 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7635 if (err)
7636 goto done;
7638 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7639 if (err)
7640 goto done;
7642 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7643 &have_staged_files);
7644 if (err && err->code != GOT_ERR_CANCELLED)
7645 goto done;
7646 if (have_staged_files) {
7647 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7648 goto done;
7651 cc_arg.commitable_paths = &commitable_paths;
7652 cc_arg.worktree = worktree;
7653 cc_arg.fileindex = fileindex;
7654 cc_arg.repo = repo;
7655 cc_arg.have_staged_files = have_staged_files;
7656 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7657 err = worktree_status(worktree, "", fileindex, repo,
7658 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7659 if (err)
7660 goto done;
7662 if (TAILQ_EMPTY(&commitable_paths)) {
7663 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7664 "merge of %s cannot proceed", branch_name);
7665 goto done;
7668 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7669 struct got_commitable *ct = pe->data;
7670 const char *ct_path = ct->in_repo_path;
7672 while (ct_path[0] == '/')
7673 ct_path++;
7674 err = check_out_of_date(ct_path, ct->status,
7675 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7676 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7677 if (err)
7678 goto done;
7682 mcm_arg.worktree = worktree;
7683 mcm_arg.branch_name = branch_name;
7684 err = commit_worktree(new_commit_id, &commitable_paths,
7685 head_commit_id, branch_tip, worktree, author, committer,
7686 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7687 if (err)
7688 goto done;
7690 err = update_fileindex_after_commit(worktree, &commitable_paths,
7691 *new_commit_id, fileindex, have_staged_files);
7692 sync_err = sync_fileindex(fileindex, fileindex_path);
7693 if (sync_err && err == NULL)
7694 err = sync_err;
7695 done:
7696 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7697 struct got_commitable *ct = pe->data;
7698 free_commitable(ct);
7700 got_pathlist_free(&commitable_paths);
7701 free(fileindex_path);
7702 return err;
7705 const struct got_error *
7706 got_worktree_merge_complete(struct got_worktree *worktree,
7707 struct got_fileindex *fileindex, struct got_repository *repo)
7709 const struct got_error *err, *unlockerr, *sync_err;
7710 char *fileindex_path = NULL;
7712 err = delete_merge_refs(worktree, repo);
7713 if (err)
7714 goto done;
7716 err = get_fileindex_path(&fileindex_path, worktree);
7717 if (err)
7718 goto done;
7719 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7720 sync_err = sync_fileindex(fileindex, fileindex_path);
7721 if (sync_err && err == NULL)
7722 err = sync_err;
7723 done:
7724 got_fileindex_free(fileindex);
7725 free(fileindex_path);
7726 unlockerr = lock_worktree(worktree, LOCK_SH);
7727 if (unlockerr && err == NULL)
7728 err = unlockerr;
7729 return err;
7732 const struct got_error *
7733 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7734 struct got_repository *repo)
7736 const struct got_error *err;
7737 char *branch_refname = NULL;
7738 struct got_reference *branch_ref = NULL;
7740 *in_progress = 0;
7742 err = get_merge_branch_ref_name(&branch_refname, worktree);
7743 if (err)
7744 return err;
7745 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7746 free(branch_refname);
7747 if (err) {
7748 if (err->code != GOT_ERR_NOT_REF)
7749 return err;
7750 } else
7751 *in_progress = 1;
7753 return NULL;
7756 const struct got_error *got_worktree_merge_prepare(
7757 struct got_fileindex **fileindex, struct got_worktree *worktree,
7758 struct got_reference *branch, struct got_repository *repo)
7760 const struct got_error *err = NULL;
7761 char *fileindex_path = NULL;
7762 char *branch_refname = NULL, *commit_refname = NULL;
7763 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7764 struct got_reference *commit_ref = NULL;
7765 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7766 struct check_rebase_ok_arg ok_arg;
7768 *fileindex = NULL;
7770 err = lock_worktree(worktree, LOCK_EX);
7771 if (err)
7772 return err;
7774 err = open_fileindex(fileindex, &fileindex_path, worktree);
7775 if (err)
7776 goto done;
7778 /* Preconditions are the same as for rebase. */
7779 ok_arg.worktree = worktree;
7780 ok_arg.repo = repo;
7781 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7782 &ok_arg);
7783 if (err)
7784 goto done;
7786 err = get_merge_branch_ref_name(&branch_refname, worktree);
7787 if (err)
7788 return err;
7790 err = get_merge_commit_ref_name(&commit_refname, worktree);
7791 if (err)
7792 return err;
7794 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7795 0);
7796 if (err)
7797 goto done;
7799 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7800 if (err)
7801 goto done;
7803 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7804 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7805 goto done;
7808 err = got_ref_resolve(&branch_tip, repo, branch);
7809 if (err)
7810 goto done;
7812 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7813 if (err)
7814 goto done;
7815 err = got_ref_write(branch_ref, repo);
7816 if (err)
7817 goto done;
7819 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7820 if (err)
7821 goto done;
7822 err = got_ref_write(commit_ref, repo);
7823 if (err)
7824 goto done;
7826 done:
7827 free(branch_refname);
7828 free(commit_refname);
7829 free(fileindex_path);
7830 if (branch_ref)
7831 got_ref_close(branch_ref);
7832 if (commit_ref)
7833 got_ref_close(commit_ref);
7834 if (wt_branch)
7835 got_ref_close(wt_branch);
7836 free(wt_branch_tip);
7837 if (err) {
7838 if (*fileindex) {
7839 got_fileindex_free(*fileindex);
7840 *fileindex = NULL;
7842 lock_worktree(worktree, LOCK_SH);
7844 return err;
7847 const struct got_error *
7848 got_worktree_merge_continue(char **branch_name,
7849 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7850 struct got_worktree *worktree, struct got_repository *repo)
7852 const struct got_error *err;
7853 char *commit_refname = NULL, *branch_refname = NULL;
7854 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7855 char *fileindex_path = NULL;
7856 int have_staged_files = 0;
7858 *branch_name = NULL;
7859 *branch_tip = NULL;
7860 *fileindex = NULL;
7862 err = lock_worktree(worktree, LOCK_EX);
7863 if (err)
7864 return err;
7866 err = open_fileindex(fileindex, &fileindex_path, worktree);
7867 if (err)
7868 goto done;
7870 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7871 &have_staged_files);
7872 if (err && err->code != GOT_ERR_CANCELLED)
7873 goto done;
7874 if (have_staged_files) {
7875 err = got_error(GOT_ERR_STAGED_PATHS);
7876 goto done;
7879 err = get_merge_branch_ref_name(&branch_refname, worktree);
7880 if (err)
7881 goto done;
7883 err = get_merge_commit_ref_name(&commit_refname, worktree);
7884 if (err)
7885 goto done;
7887 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7888 if (err)
7889 goto done;
7891 if (!got_ref_is_symbolic(branch_ref)) {
7892 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7893 "%s is not a symbolic reference",
7894 got_ref_get_name(branch_ref));
7895 goto done;
7897 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7898 if (*branch_name == NULL) {
7899 err = got_error_from_errno("strdup");
7900 goto done;
7903 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7904 if (err)
7905 goto done;
7907 err = got_ref_resolve(branch_tip, repo, commit_ref);
7908 if (err)
7909 goto done;
7910 done:
7911 free(commit_refname);
7912 free(branch_refname);
7913 free(fileindex_path);
7914 if (commit_ref)
7915 got_ref_close(commit_ref);
7916 if (branch_ref)
7917 got_ref_close(branch_ref);
7918 if (err) {
7919 if (*branch_name) {
7920 free(*branch_name);
7921 *branch_name = NULL;
7923 free(*branch_tip);
7924 *branch_tip = NULL;
7925 if (*fileindex) {
7926 got_fileindex_free(*fileindex);
7927 *fileindex = NULL;
7929 lock_worktree(worktree, LOCK_SH);
7931 return err;
7934 const struct got_error *
7935 got_worktree_merge_abort(struct got_worktree *worktree,
7936 struct got_fileindex *fileindex, struct got_repository *repo,
7937 got_worktree_checkout_cb progress_cb, void *progress_arg)
7939 const struct got_error *err, *unlockerr, *sync_err;
7940 struct got_object_id *commit_id = NULL;
7941 struct got_commit_object *commit = NULL;
7942 char *fileindex_path = NULL;
7943 struct revert_file_args rfa;
7944 struct got_object_id *tree_id = NULL;
7946 err = got_object_open_as_commit(&commit, repo,
7947 worktree->base_commit_id);
7948 if (err)
7949 goto done;
7951 err = got_object_id_by_path(&tree_id, repo, commit,
7952 worktree->path_prefix);
7953 if (err)
7954 goto done;
7956 err = delete_merge_refs(worktree, repo);
7957 if (err)
7958 goto done;
7960 err = get_fileindex_path(&fileindex_path, worktree);
7961 if (err)
7962 goto done;
7964 rfa.worktree = worktree;
7965 rfa.fileindex = fileindex;
7966 rfa.progress_cb = progress_cb;
7967 rfa.progress_arg = progress_arg;
7968 rfa.patch_cb = NULL;
7969 rfa.patch_arg = NULL;
7970 rfa.repo = repo;
7971 rfa.unlink_added_files = 1;
7972 err = worktree_status(worktree, "", fileindex, repo,
7973 revert_file, &rfa, NULL, NULL, 1, 0);
7974 if (err)
7975 goto sync;
7977 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7978 repo, progress_cb, progress_arg, NULL, NULL);
7979 sync:
7980 sync_err = sync_fileindex(fileindex, fileindex_path);
7981 if (sync_err && err == NULL)
7982 err = sync_err;
7983 done:
7984 free(tree_id);
7985 free(commit_id);
7986 if (commit)
7987 got_object_commit_close(commit);
7988 if (fileindex)
7989 got_fileindex_free(fileindex);
7990 free(fileindex_path);
7992 unlockerr = lock_worktree(worktree, LOCK_SH);
7993 if (unlockerr && err == NULL)
7994 err = unlockerr;
7995 return err;
7998 struct check_stage_ok_arg {
7999 struct got_object_id *head_commit_id;
8000 struct got_worktree *worktree;
8001 struct got_fileindex *fileindex;
8002 struct got_repository *repo;
8003 int have_changes;
8006 static const struct got_error *
8007 check_stage_ok(void *arg, unsigned char status,
8008 unsigned char staged_status, const char *relpath,
8009 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8010 struct got_object_id *commit_id, int dirfd, const char *de_name)
8012 struct check_stage_ok_arg *a = arg;
8013 const struct got_error *err = NULL;
8014 struct got_fileindex_entry *ie;
8015 struct got_object_id base_commit_id;
8016 struct got_object_id *base_commit_idp = NULL;
8017 char *in_repo_path = NULL, *p;
8019 if (status == GOT_STATUS_UNVERSIONED ||
8020 status == GOT_STATUS_NO_CHANGE)
8021 return NULL;
8022 if (status == GOT_STATUS_NONEXISTENT)
8023 return got_error_set_errno(ENOENT, relpath);
8025 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8026 if (ie == NULL)
8027 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8029 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8030 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8031 relpath) == -1)
8032 return got_error_from_errno("asprintf");
8034 if (got_fileindex_entry_has_commit(ie)) {
8035 memcpy(base_commit_id.sha1, ie->commit_sha1,
8036 SHA1_DIGEST_LENGTH);
8037 base_commit_idp = &base_commit_id;
8040 if (status == GOT_STATUS_CONFLICT) {
8041 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8042 goto done;
8043 } else if (status != GOT_STATUS_ADD &&
8044 status != GOT_STATUS_MODIFY &&
8045 status != GOT_STATUS_DELETE) {
8046 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8047 goto done;
8050 a->have_changes = 1;
8052 p = in_repo_path;
8053 while (p[0] == '/')
8054 p++;
8055 err = check_out_of_date(p, status, staged_status,
8056 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8057 GOT_ERR_STAGE_OUT_OF_DATE);
8058 done:
8059 free(in_repo_path);
8060 return err;
8063 struct stage_path_arg {
8064 struct got_worktree *worktree;
8065 struct got_fileindex *fileindex;
8066 struct got_repository *repo;
8067 got_worktree_status_cb status_cb;
8068 void *status_arg;
8069 got_worktree_patch_cb patch_cb;
8070 void *patch_arg;
8071 int staged_something;
8072 int allow_bad_symlinks;
8075 static const struct got_error *
8076 stage_path(void *arg, unsigned char status,
8077 unsigned char staged_status, const char *relpath,
8078 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8079 struct got_object_id *commit_id, int dirfd, const char *de_name)
8081 struct stage_path_arg *a = arg;
8082 const struct got_error *err = NULL;
8083 struct got_fileindex_entry *ie;
8084 char *ondisk_path = NULL, *path_content = NULL;
8085 uint32_t stage;
8086 struct got_object_id *new_staged_blob_id = NULL;
8087 struct stat sb;
8089 if (status == GOT_STATUS_UNVERSIONED)
8090 return NULL;
8092 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8093 if (ie == NULL)
8094 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8097 relpath)== -1)
8098 return got_error_from_errno("asprintf");
8100 switch (status) {
8101 case GOT_STATUS_ADD:
8102 case GOT_STATUS_MODIFY:
8103 /* XXX could sb.st_mode be passed in by our caller? */
8104 if (lstat(ondisk_path, &sb) == -1) {
8105 err = got_error_from_errno2("lstat", ondisk_path);
8106 break;
8108 if (a->patch_cb) {
8109 if (status == GOT_STATUS_ADD) {
8110 int choice = GOT_PATCH_CHOICE_NONE;
8111 err = (*a->patch_cb)(&choice, a->patch_arg,
8112 status, ie->path, NULL, 1, 1);
8113 if (err)
8114 break;
8115 if (choice != GOT_PATCH_CHOICE_YES)
8116 break;
8117 } else {
8118 err = create_patched_content(&path_content, 0,
8119 staged_blob_id ? staged_blob_id : blob_id,
8120 ondisk_path, dirfd, de_name, ie->path,
8121 a->repo, a->patch_cb, a->patch_arg);
8122 if (err || path_content == NULL)
8123 break;
8126 err = got_object_blob_create(&new_staged_blob_id,
8127 path_content ? path_content : ondisk_path, a->repo);
8128 if (err)
8129 break;
8130 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8131 SHA1_DIGEST_LENGTH);
8132 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8133 stage = GOT_FILEIDX_STAGE_ADD;
8134 else
8135 stage = GOT_FILEIDX_STAGE_MODIFY;
8136 got_fileindex_entry_stage_set(ie, stage);
8137 if (S_ISLNK(sb.st_mode)) {
8138 int is_bad_symlink = 0;
8139 if (!a->allow_bad_symlinks) {
8140 char target_path[PATH_MAX];
8141 ssize_t target_len;
8142 target_len = readlink(ondisk_path, target_path,
8143 sizeof(target_path));
8144 if (target_len == -1) {
8145 err = got_error_from_errno2("readlink",
8146 ondisk_path);
8147 break;
8149 err = is_bad_symlink_target(&is_bad_symlink,
8150 target_path, target_len, ondisk_path,
8151 a->worktree->root_path);
8152 if (err)
8153 break;
8154 if (is_bad_symlink) {
8155 err = got_error_path(ondisk_path,
8156 GOT_ERR_BAD_SYMLINK);
8157 break;
8160 if (is_bad_symlink)
8161 got_fileindex_entry_staged_filetype_set(ie,
8162 GOT_FILEIDX_MODE_BAD_SYMLINK);
8163 else
8164 got_fileindex_entry_staged_filetype_set(ie,
8165 GOT_FILEIDX_MODE_SYMLINK);
8166 } else {
8167 got_fileindex_entry_staged_filetype_set(ie,
8168 GOT_FILEIDX_MODE_REGULAR_FILE);
8170 a->staged_something = 1;
8171 if (a->status_cb == NULL)
8172 break;
8173 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8174 get_staged_status(ie), relpath, blob_id,
8175 new_staged_blob_id, NULL, dirfd, de_name);
8176 if (err)
8177 break;
8179 * When staging the reverse of the staged diff,
8180 * implicitly unstage the file.
8182 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8183 sizeof(ie->blob_sha1)) == 0) {
8184 got_fileindex_entry_stage_set(ie,
8185 GOT_FILEIDX_STAGE_NONE);
8187 break;
8188 case GOT_STATUS_DELETE:
8189 if (staged_status == GOT_STATUS_DELETE)
8190 break;
8191 if (a->patch_cb) {
8192 int choice = GOT_PATCH_CHOICE_NONE;
8193 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8194 ie->path, NULL, 1, 1);
8195 if (err)
8196 break;
8197 if (choice == GOT_PATCH_CHOICE_NO)
8198 break;
8199 if (choice != GOT_PATCH_CHOICE_YES) {
8200 err = got_error(GOT_ERR_PATCH_CHOICE);
8201 break;
8204 stage = GOT_FILEIDX_STAGE_DELETE;
8205 got_fileindex_entry_stage_set(ie, stage);
8206 a->staged_something = 1;
8207 if (a->status_cb == NULL)
8208 break;
8209 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8210 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8211 de_name);
8212 break;
8213 case GOT_STATUS_NO_CHANGE:
8214 break;
8215 case GOT_STATUS_CONFLICT:
8216 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8217 break;
8218 case GOT_STATUS_NONEXISTENT:
8219 err = got_error_set_errno(ENOENT, relpath);
8220 break;
8221 default:
8222 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8223 break;
8226 if (path_content && unlink(path_content) == -1 && err == NULL)
8227 err = got_error_from_errno2("unlink", path_content);
8228 free(path_content);
8229 free(ondisk_path);
8230 free(new_staged_blob_id);
8231 return err;
8234 const struct got_error *
8235 got_worktree_stage(struct got_worktree *worktree,
8236 struct got_pathlist_head *paths,
8237 got_worktree_status_cb status_cb, void *status_arg,
8238 got_worktree_patch_cb patch_cb, void *patch_arg,
8239 int allow_bad_symlinks, struct got_repository *repo)
8241 const struct got_error *err = NULL, *sync_err, *unlockerr;
8242 struct got_pathlist_entry *pe;
8243 struct got_fileindex *fileindex = NULL;
8244 char *fileindex_path = NULL;
8245 struct got_reference *head_ref = NULL;
8246 struct got_object_id *head_commit_id = NULL;
8247 struct check_stage_ok_arg oka;
8248 struct stage_path_arg spa;
8250 err = lock_worktree(worktree, LOCK_EX);
8251 if (err)
8252 return err;
8254 err = got_ref_open(&head_ref, repo,
8255 got_worktree_get_head_ref_name(worktree), 0);
8256 if (err)
8257 goto done;
8258 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8259 if (err)
8260 goto done;
8261 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8262 if (err)
8263 goto done;
8265 /* Check pre-conditions before staging anything. */
8266 oka.head_commit_id = head_commit_id;
8267 oka.worktree = worktree;
8268 oka.fileindex = fileindex;
8269 oka.repo = repo;
8270 oka.have_changes = 0;
8271 TAILQ_FOREACH(pe, paths, entry) {
8272 err = worktree_status(worktree, pe->path, fileindex, repo,
8273 check_stage_ok, &oka, NULL, NULL, 1, 0);
8274 if (err)
8275 goto done;
8277 if (!oka.have_changes) {
8278 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8279 goto done;
8282 spa.worktree = worktree;
8283 spa.fileindex = fileindex;
8284 spa.repo = repo;
8285 spa.patch_cb = patch_cb;
8286 spa.patch_arg = patch_arg;
8287 spa.status_cb = status_cb;
8288 spa.status_arg = status_arg;
8289 spa.staged_something = 0;
8290 spa.allow_bad_symlinks = allow_bad_symlinks;
8291 TAILQ_FOREACH(pe, paths, entry) {
8292 err = worktree_status(worktree, pe->path, fileindex, repo,
8293 stage_path, &spa, NULL, NULL, 1, 0);
8294 if (err)
8295 goto done;
8297 if (!spa.staged_something) {
8298 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8299 goto done;
8302 sync_err = sync_fileindex(fileindex, fileindex_path);
8303 if (sync_err && err == NULL)
8304 err = sync_err;
8305 done:
8306 if (head_ref)
8307 got_ref_close(head_ref);
8308 free(head_commit_id);
8309 free(fileindex_path);
8310 if (fileindex)
8311 got_fileindex_free(fileindex);
8312 unlockerr = lock_worktree(worktree, LOCK_SH);
8313 if (unlockerr && err == NULL)
8314 err = unlockerr;
8315 return err;
8318 struct unstage_path_arg {
8319 struct got_worktree *worktree;
8320 struct got_fileindex *fileindex;
8321 struct got_repository *repo;
8322 got_worktree_checkout_cb progress_cb;
8323 void *progress_arg;
8324 got_worktree_patch_cb patch_cb;
8325 void *patch_arg;
8328 static const struct got_error *
8329 create_unstaged_content(char **path_unstaged_content,
8330 char **path_new_staged_content, struct got_object_id *blob_id,
8331 struct got_object_id *staged_blob_id, const char *relpath,
8332 struct got_repository *repo,
8333 got_worktree_patch_cb patch_cb, void *patch_arg)
8335 const struct got_error *err, *free_err;
8336 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8337 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8338 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8339 struct got_diffreg_result *diffreg_result = NULL;
8340 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8341 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8342 int fd1 = -1, fd2 = -1;
8344 *path_unstaged_content = NULL;
8345 *path_new_staged_content = NULL;
8347 err = got_object_id_str(&label1, blob_id);
8348 if (err)
8349 return err;
8351 fd1 = got_opentempfd();
8352 if (fd1 == -1) {
8353 err = got_error_from_errno("got_opentempfd");
8354 goto done;
8356 fd2 = got_opentempfd();
8357 if (fd2 == -1) {
8358 err = got_error_from_errno("got_opentempfd");
8359 goto done;
8362 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8363 if (err)
8364 goto done;
8366 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8367 if (err)
8368 goto done;
8370 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8371 if (err)
8372 goto done;
8374 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8375 fd2);
8376 if (err)
8377 goto done;
8379 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8380 if (err)
8381 goto done;
8383 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8384 if (err)
8385 goto done;
8387 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8388 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8389 if (err)
8390 goto done;
8392 err = got_opentemp_named(path_unstaged_content, &outfile,
8393 "got-unstaged-content");
8394 if (err)
8395 goto done;
8396 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8397 "got-new-staged-content");
8398 if (err)
8399 goto done;
8401 if (fseek(f1, 0L, SEEK_SET) == -1) {
8402 err = got_ferror(f1, GOT_ERR_IO);
8403 goto done;
8405 if (fseek(f2, 0L, SEEK_SET) == -1) {
8406 err = got_ferror(f2, GOT_ERR_IO);
8407 goto done;
8409 /* Count the number of actual changes in the diff result. */
8410 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8411 struct diff_chunk_context cc = {};
8412 diff_chunk_context_load_change(&cc, &nchunks_used,
8413 diffreg_result->result, n, 0);
8414 nchanges++;
8416 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8417 int choice;
8418 err = apply_or_reject_change(&choice, &nchunks_used,
8419 diffreg_result->result, n, relpath, f1, f2,
8420 &line_cur1, &line_cur2,
8421 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8422 if (err)
8423 goto done;
8424 if (choice == GOT_PATCH_CHOICE_YES)
8425 have_content = 1;
8426 else
8427 have_rejected_content = 1;
8428 if (choice == GOT_PATCH_CHOICE_QUIT)
8429 break;
8431 if (have_content || have_rejected_content)
8432 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8433 outfile, rejectfile);
8434 done:
8435 free(label1);
8436 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8437 err = got_error_from_errno("close");
8438 if (blob)
8439 got_object_blob_close(blob);
8440 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8441 err = got_error_from_errno("close");
8442 if (staged_blob)
8443 got_object_blob_close(staged_blob);
8444 free_err = got_diffreg_result_free(diffreg_result);
8445 if (free_err && err == NULL)
8446 err = free_err;
8447 if (f1 && fclose(f1) == EOF && err == NULL)
8448 err = got_error_from_errno2("fclose", path1);
8449 if (f2 && fclose(f2) == EOF && err == NULL)
8450 err = got_error_from_errno2("fclose", path2);
8451 if (outfile && fclose(outfile) == EOF && err == NULL)
8452 err = got_error_from_errno2("fclose", *path_unstaged_content);
8453 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8454 err = got_error_from_errno2("fclose", *path_new_staged_content);
8455 if (path1 && unlink(path1) == -1 && err == NULL)
8456 err = got_error_from_errno2("unlink", path1);
8457 if (path2 && unlink(path2) == -1 && err == NULL)
8458 err = got_error_from_errno2("unlink", path2);
8459 if (err || !have_content) {
8460 if (*path_unstaged_content &&
8461 unlink(*path_unstaged_content) == -1 && err == NULL)
8462 err = got_error_from_errno2("unlink",
8463 *path_unstaged_content);
8464 free(*path_unstaged_content);
8465 *path_unstaged_content = NULL;
8467 if (err || !have_content || !have_rejected_content) {
8468 if (*path_new_staged_content &&
8469 unlink(*path_new_staged_content) == -1 && err == NULL)
8470 err = got_error_from_errno2("unlink",
8471 *path_new_staged_content);
8472 free(*path_new_staged_content);
8473 *path_new_staged_content = NULL;
8475 free(path1);
8476 free(path2);
8477 return err;
8480 static const struct got_error *
8481 unstage_hunks(struct got_object_id *staged_blob_id,
8482 struct got_blob_object *blob_base,
8483 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8484 const char *ondisk_path, const char *label_orig,
8485 struct got_worktree *worktree, struct got_repository *repo,
8486 got_worktree_patch_cb patch_cb, void *patch_arg,
8487 got_worktree_checkout_cb progress_cb, void *progress_arg)
8489 const struct got_error *err = NULL;
8490 char *path_unstaged_content = NULL;
8491 char *path_new_staged_content = NULL;
8492 char *parent = NULL, *base_path = NULL;
8493 char *blob_base_path = NULL;
8494 struct got_object_id *new_staged_blob_id = NULL;
8495 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8496 struct stat sb;
8498 err = create_unstaged_content(&path_unstaged_content,
8499 &path_new_staged_content, blob_id, staged_blob_id,
8500 ie->path, repo, patch_cb, patch_arg);
8501 if (err)
8502 return err;
8504 if (path_unstaged_content == NULL)
8505 return NULL;
8507 if (path_new_staged_content) {
8508 err = got_object_blob_create(&new_staged_blob_id,
8509 path_new_staged_content, repo);
8510 if (err)
8511 goto done;
8514 f = fopen(path_unstaged_content, "re");
8515 if (f == NULL) {
8516 err = got_error_from_errno2("fopen",
8517 path_unstaged_content);
8518 goto done;
8520 if (fstat(fileno(f), &sb) == -1) {
8521 err = got_error_from_errno2("fstat", path_unstaged_content);
8522 goto done;
8524 if (got_fileindex_entry_staged_filetype_get(ie) ==
8525 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8526 char link_target[PATH_MAX];
8527 size_t r;
8528 r = fread(link_target, 1, sizeof(link_target), f);
8529 if (r == 0 && ferror(f)) {
8530 err = got_error_from_errno("fread");
8531 goto done;
8533 if (r >= sizeof(link_target)) { /* should not happen */
8534 err = got_error(GOT_ERR_NO_SPACE);
8535 goto done;
8537 link_target[r] = '\0';
8538 err = merge_symlink(worktree, blob_base,
8539 ondisk_path, ie->path, label_orig, link_target,
8540 worktree->base_commit_id, repo, progress_cb,
8541 progress_arg);
8542 } else {
8543 int local_changes_subsumed;
8545 err = got_path_dirname(&parent, ondisk_path);
8546 if (err)
8547 return err;
8549 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8550 parent) == -1) {
8551 err = got_error_from_errno("asprintf");
8552 base_path = NULL;
8553 goto done;
8556 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8557 if (err)
8558 goto done;
8559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8560 blob_base);
8561 if (err)
8562 goto done;
8565 * In order the run a 3-way merge with a symlink we copy the symlink's
8566 * target path into a temporary file and use that file with diff3.
8568 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8569 err = dump_symlink_target_path_to_file(&f_deriv2,
8570 ondisk_path);
8571 if (err)
8572 goto done;
8573 } else {
8574 int fd;
8575 fd = open(ondisk_path,
8576 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8577 if (fd == -1) {
8578 err = got_error_from_errno2("open", ondisk_path);
8579 goto done;
8581 f_deriv2 = fdopen(fd, "r");
8582 if (f_deriv2 == NULL) {
8583 err = got_error_from_errno2("fdopen", ondisk_path);
8584 close(fd);
8585 goto done;
8589 err = merge_file(&local_changes_subsumed, worktree,
8590 f_base, f, f_deriv2, ondisk_path, ie->path,
8591 got_fileindex_perms_to_st(ie),
8592 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8593 repo, progress_cb, progress_arg);
8595 if (err)
8596 goto done;
8598 if (new_staged_blob_id) {
8599 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8600 SHA1_DIGEST_LENGTH);
8601 } else {
8602 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8603 got_fileindex_entry_staged_filetype_set(ie, 0);
8605 done:
8606 free(new_staged_blob_id);
8607 if (path_unstaged_content &&
8608 unlink(path_unstaged_content) == -1 && err == NULL)
8609 err = got_error_from_errno2("unlink", path_unstaged_content);
8610 if (path_new_staged_content &&
8611 unlink(path_new_staged_content) == -1 && err == NULL)
8612 err = got_error_from_errno2("unlink", path_new_staged_content);
8613 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8614 err = got_error_from_errno2("unlink", blob_base_path);
8615 if (f_base && fclose(f_base) == EOF && err == NULL)
8616 err = got_error_from_errno2("fclose", path_unstaged_content);
8617 if (f && fclose(f) == EOF && err == NULL)
8618 err = got_error_from_errno2("fclose", path_unstaged_content);
8619 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8620 err = got_error_from_errno2("fclose", ondisk_path);
8621 free(path_unstaged_content);
8622 free(path_new_staged_content);
8623 free(blob_base_path);
8624 free(parent);
8625 free(base_path);
8626 return err;
8629 static const struct got_error *
8630 unstage_path(void *arg, unsigned char status,
8631 unsigned char staged_status, const char *relpath,
8632 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8633 struct got_object_id *commit_id, int dirfd, const char *de_name)
8635 const struct got_error *err = NULL;
8636 struct unstage_path_arg *a = arg;
8637 struct got_fileindex_entry *ie;
8638 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8639 char *ondisk_path = NULL;
8640 char *id_str = NULL, *label_orig = NULL;
8641 int local_changes_subsumed;
8642 struct stat sb;
8643 int fd1 = -1, fd2 = -1;
8645 if (staged_status != GOT_STATUS_ADD &&
8646 staged_status != GOT_STATUS_MODIFY &&
8647 staged_status != GOT_STATUS_DELETE)
8648 return NULL;
8650 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8651 if (ie == NULL)
8652 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8654 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8655 == -1)
8656 return got_error_from_errno("asprintf");
8658 err = got_object_id_str(&id_str,
8659 commit_id ? commit_id : a->worktree->base_commit_id);
8660 if (err)
8661 goto done;
8662 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8663 id_str) == -1) {
8664 err = got_error_from_errno("asprintf");
8665 goto done;
8668 fd1 = got_opentempfd();
8669 if (fd1 == -1) {
8670 err = got_error_from_errno("got_opentempfd");
8671 goto done;
8673 fd2 = got_opentempfd();
8674 if (fd2 == -1) {
8675 err = got_error_from_errno("got_opentempfd");
8676 goto done;
8679 switch (staged_status) {
8680 case GOT_STATUS_MODIFY:
8681 err = got_object_open_as_blob(&blob_base, a->repo,
8682 blob_id, 8192, fd1);
8683 if (err)
8684 break;
8685 /* fall through */
8686 case GOT_STATUS_ADD:
8687 if (a->patch_cb) {
8688 if (staged_status == GOT_STATUS_ADD) {
8689 int choice = GOT_PATCH_CHOICE_NONE;
8690 err = (*a->patch_cb)(&choice, a->patch_arg,
8691 staged_status, ie->path, NULL, 1, 1);
8692 if (err)
8693 break;
8694 if (choice != GOT_PATCH_CHOICE_YES)
8695 break;
8696 } else {
8697 err = unstage_hunks(staged_blob_id,
8698 blob_base, blob_id, ie, ondisk_path,
8699 label_orig, a->worktree, a->repo,
8700 a->patch_cb, a->patch_arg,
8701 a->progress_cb, a->progress_arg);
8702 break; /* Done with this file. */
8705 err = got_object_open_as_blob(&blob_staged, a->repo,
8706 staged_blob_id, 8192, fd2);
8707 if (err)
8708 break;
8709 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8710 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8711 case GOT_FILEIDX_MODE_REGULAR_FILE:
8712 err = merge_blob(&local_changes_subsumed, a->worktree,
8713 blob_base, ondisk_path, relpath,
8714 got_fileindex_perms_to_st(ie), label_orig,
8715 blob_staged, commit_id ? commit_id :
8716 a->worktree->base_commit_id, a->repo,
8717 a->progress_cb, a->progress_arg);
8718 break;
8719 case GOT_FILEIDX_MODE_SYMLINK:
8720 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8721 char *staged_target;
8722 err = got_object_blob_read_to_str(
8723 &staged_target, blob_staged);
8724 if (err)
8725 goto done;
8726 err = merge_symlink(a->worktree, blob_base,
8727 ondisk_path, relpath, label_orig,
8728 staged_target, commit_id ? commit_id :
8729 a->worktree->base_commit_id,
8730 a->repo, a->progress_cb, a->progress_arg);
8731 free(staged_target);
8732 } else {
8733 err = merge_blob(&local_changes_subsumed,
8734 a->worktree, blob_base, ondisk_path,
8735 relpath, got_fileindex_perms_to_st(ie),
8736 label_orig, blob_staged,
8737 commit_id ? commit_id :
8738 a->worktree->base_commit_id, a->repo,
8739 a->progress_cb, a->progress_arg);
8741 break;
8742 default:
8743 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8744 break;
8746 if (err == NULL) {
8747 got_fileindex_entry_stage_set(ie,
8748 GOT_FILEIDX_STAGE_NONE);
8749 got_fileindex_entry_staged_filetype_set(ie, 0);
8751 break;
8752 case GOT_STATUS_DELETE:
8753 if (a->patch_cb) {
8754 int choice = GOT_PATCH_CHOICE_NONE;
8755 err = (*a->patch_cb)(&choice, a->patch_arg,
8756 staged_status, ie->path, NULL, 1, 1);
8757 if (err)
8758 break;
8759 if (choice == GOT_PATCH_CHOICE_NO)
8760 break;
8761 if (choice != GOT_PATCH_CHOICE_YES) {
8762 err = got_error(GOT_ERR_PATCH_CHOICE);
8763 break;
8766 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8767 got_fileindex_entry_staged_filetype_set(ie, 0);
8768 err = get_file_status(&status, &sb, ie, ondisk_path,
8769 dirfd, de_name, a->repo);
8770 if (err)
8771 break;
8772 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8773 break;
8775 done:
8776 free(ondisk_path);
8777 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8778 err = got_error_from_errno("close");
8779 if (blob_base)
8780 got_object_blob_close(blob_base);
8781 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8782 err = got_error_from_errno("close");
8783 if (blob_staged)
8784 got_object_blob_close(blob_staged);
8785 free(id_str);
8786 free(label_orig);
8787 return err;
8790 const struct got_error *
8791 got_worktree_unstage(struct got_worktree *worktree,
8792 struct got_pathlist_head *paths,
8793 got_worktree_checkout_cb progress_cb, void *progress_arg,
8794 got_worktree_patch_cb patch_cb, void *patch_arg,
8795 struct got_repository *repo)
8797 const struct got_error *err = NULL, *sync_err, *unlockerr;
8798 struct got_pathlist_entry *pe;
8799 struct got_fileindex *fileindex = NULL;
8800 char *fileindex_path = NULL;
8801 struct unstage_path_arg upa;
8803 err = lock_worktree(worktree, LOCK_EX);
8804 if (err)
8805 return err;
8807 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8808 if (err)
8809 goto done;
8811 upa.worktree = worktree;
8812 upa.fileindex = fileindex;
8813 upa.repo = repo;
8814 upa.progress_cb = progress_cb;
8815 upa.progress_arg = progress_arg;
8816 upa.patch_cb = patch_cb;
8817 upa.patch_arg = patch_arg;
8818 TAILQ_FOREACH(pe, paths, entry) {
8819 err = worktree_status(worktree, pe->path, fileindex, repo,
8820 unstage_path, &upa, NULL, NULL, 1, 0);
8821 if (err)
8822 goto done;
8825 sync_err = sync_fileindex(fileindex, fileindex_path);
8826 if (sync_err && err == NULL)
8827 err = sync_err;
8828 done:
8829 free(fileindex_path);
8830 if (fileindex)
8831 got_fileindex_free(fileindex);
8832 unlockerr = lock_worktree(worktree, LOCK_SH);
8833 if (unlockerr && err == NULL)
8834 err = unlockerr;
8835 return err;
8838 struct report_file_info_arg {
8839 struct got_worktree *worktree;
8840 got_worktree_path_info_cb info_cb;
8841 void *info_arg;
8842 struct got_pathlist_head *paths;
8843 got_cancel_cb cancel_cb;
8844 void *cancel_arg;
8847 static const struct got_error *
8848 report_file_info(void *arg, struct got_fileindex_entry *ie)
8850 struct report_file_info_arg *a = arg;
8851 struct got_pathlist_entry *pe;
8852 struct got_object_id blob_id, staged_blob_id, commit_id;
8853 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8854 struct got_object_id *commit_idp = NULL;
8855 int stage;
8857 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8858 return got_error(GOT_ERR_CANCELLED);
8860 TAILQ_FOREACH(pe, a->paths, entry) {
8861 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8862 got_path_is_child(ie->path, pe->path, pe->path_len))
8863 break;
8865 if (pe == NULL) /* not found */
8866 return NULL;
8868 if (got_fileindex_entry_has_blob(ie)) {
8869 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8870 blob_idp = &blob_id;
8872 stage = got_fileindex_entry_stage_get(ie);
8873 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8874 stage == GOT_FILEIDX_STAGE_ADD) {
8875 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8876 SHA1_DIGEST_LENGTH);
8877 staged_blob_idp = &staged_blob_id;
8880 if (got_fileindex_entry_has_commit(ie)) {
8881 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8882 commit_idp = &commit_id;
8885 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8886 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8889 const struct got_error *
8890 got_worktree_path_info(struct got_worktree *worktree,
8891 struct got_pathlist_head *paths,
8892 got_worktree_path_info_cb info_cb, void *info_arg,
8893 got_cancel_cb cancel_cb, void *cancel_arg)
8896 const struct got_error *err = NULL, *unlockerr;
8897 struct got_fileindex *fileindex = NULL;
8898 char *fileindex_path = NULL;
8899 struct report_file_info_arg arg;
8901 err = lock_worktree(worktree, LOCK_SH);
8902 if (err)
8903 return err;
8905 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8906 if (err)
8907 goto done;
8909 arg.worktree = worktree;
8910 arg.info_cb = info_cb;
8911 arg.info_arg = info_arg;
8912 arg.paths = paths;
8913 arg.cancel_cb = cancel_cb;
8914 arg.cancel_arg = cancel_arg;
8915 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8916 &arg);
8917 done:
8918 free(fileindex_path);
8919 if (fileindex)
8920 got_fileindex_free(fileindex);
8921 unlockerr = lock_worktree(worktree, LOCK_UN);
8922 if (unlockerr && err == NULL)
8923 err = unlockerr;
8924 return err;
8927 static const struct got_error *
8928 patch_check_path(const char *p, char **path, unsigned char *status,
8929 unsigned char *staged_status, struct got_fileindex *fileindex,
8930 struct got_worktree *worktree, struct got_repository *repo)
8932 const struct got_error *err;
8933 struct got_fileindex_entry *ie;
8934 struct stat sb;
8935 char *ondisk_path = NULL;
8937 err = got_worktree_resolve_path(path, worktree, p);
8938 if (err)
8939 return err;
8941 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8942 *path[0] ? "/" : "", *path) == -1)
8943 return got_error_from_errno("asprintf");
8945 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8946 if (ie) {
8947 *staged_status = get_staged_status(ie);
8948 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8949 repo);
8950 if (err)
8951 goto done;
8952 } else {
8953 *staged_status = GOT_STATUS_NO_CHANGE;
8954 *status = GOT_STATUS_UNVERSIONED;
8955 if (lstat(ondisk_path, &sb) == -1) {
8956 if (errno != ENOENT) {
8957 err = got_error_from_errno2("lstat",
8958 ondisk_path);
8959 goto done;
8961 *status = GOT_STATUS_NONEXISTENT;
8965 done:
8966 free(ondisk_path);
8967 return err;
8970 static const struct got_error *
8971 patch_can_rm(const char *path, unsigned char status,
8972 unsigned char staged_status)
8974 if (status == GOT_STATUS_NONEXISTENT)
8975 return got_error_set_errno(ENOENT, path);
8976 if (status != GOT_STATUS_NO_CHANGE &&
8977 status != GOT_STATUS_ADD &&
8978 status != GOT_STATUS_MODIFY &&
8979 status != GOT_STATUS_MODE_CHANGE)
8980 return got_error_path(path, GOT_ERR_FILE_STATUS);
8981 if (staged_status == GOT_STATUS_DELETE)
8982 return got_error_path(path, GOT_ERR_FILE_STATUS);
8983 return NULL;
8986 static const struct got_error *
8987 patch_can_add(const char *path, unsigned char status)
8989 if (status != GOT_STATUS_NONEXISTENT)
8990 return got_error_path(path, GOT_ERR_FILE_STATUS);
8991 return NULL;
8994 static const struct got_error *
8995 patch_can_edit(const char *path, unsigned char status,
8996 unsigned char staged_status)
8998 if (status == GOT_STATUS_NONEXISTENT)
8999 return got_error_set_errno(ENOENT, path);
9000 if (status != GOT_STATUS_NO_CHANGE &&
9001 status != GOT_STATUS_ADD &&
9002 status != GOT_STATUS_MODIFY)
9003 return got_error_path(path, GOT_ERR_FILE_STATUS);
9004 if (staged_status == GOT_STATUS_DELETE)
9005 return got_error_path(path, GOT_ERR_FILE_STATUS);
9006 return NULL;
9009 const struct got_error *
9010 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9011 char **fileindex_path, struct got_worktree *worktree)
9013 return open_fileindex(fileindex, fileindex_path, worktree);
9016 const struct got_error *
9017 got_worktree_patch_check_path(const char *old, const char *new,
9018 char **oldpath, char **newpath, struct got_worktree *worktree,
9019 struct got_repository *repo, struct got_fileindex *fileindex)
9021 const struct got_error *err = NULL;
9022 int file_renamed = 0;
9023 unsigned char status_old, staged_status_old;
9024 unsigned char status_new, staged_status_new;
9026 *oldpath = NULL;
9027 *newpath = NULL;
9029 err = patch_check_path(old != NULL ? old : new, oldpath,
9030 &status_old, &staged_status_old, fileindex, worktree, repo);
9031 if (err)
9032 goto done;
9034 err = patch_check_path(new != NULL ? new : old, newpath,
9035 &status_new, &staged_status_new, fileindex, worktree, repo);
9036 if (err)
9037 goto done;
9039 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9040 file_renamed = 1;
9042 if (old != NULL && new == NULL)
9043 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9044 else if (file_renamed) {
9045 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9046 if (err == NULL)
9047 err = patch_can_add(*newpath, status_new);
9048 } else if (old == NULL)
9049 err = patch_can_add(*newpath, status_new);
9050 else
9051 err = patch_can_edit(*newpath, status_new, staged_status_new);
9053 done:
9054 if (err) {
9055 free(*oldpath);
9056 *oldpath = NULL;
9057 free(*newpath);
9058 *newpath = NULL;
9060 return err;
9063 const struct got_error *
9064 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9065 struct got_worktree *worktree, struct got_fileindex *fileindex,
9066 got_worktree_checkout_cb progress_cb, void *progress_arg)
9068 struct schedule_addition_args saa;
9070 memset(&saa, 0, sizeof(saa));
9071 saa.worktree = worktree;
9072 saa.fileindex = fileindex;
9073 saa.progress_cb = progress_cb;
9074 saa.progress_arg = progress_arg;
9075 saa.repo = repo;
9077 return worktree_status(worktree, path, fileindex, repo,
9078 schedule_addition, &saa, NULL, NULL, 1, 0);
9081 const struct got_error *
9082 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9083 struct got_worktree *worktree, struct got_fileindex *fileindex,
9084 got_worktree_delete_cb progress_cb, void *progress_arg)
9086 struct schedule_deletion_args sda;
9088 memset(&sda, 0, sizeof(sda));
9089 sda.worktree = worktree;
9090 sda.fileindex = fileindex;
9091 sda.progress_cb = progress_cb;
9092 sda.progress_arg = progress_arg;
9093 sda.repo = repo;
9094 sda.delete_local_mods = 0;
9095 sda.keep_on_disk = 0;
9096 sda.ignore_missing_paths = 0;
9097 sda.status_codes = NULL;
9099 return worktree_status(worktree, path, fileindex, repo,
9100 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9103 const struct got_error *
9104 got_worktree_patch_complete(struct got_fileindex *fileindex,
9105 const char *fileindex_path)
9107 const struct got_error *err = NULL;
9109 err = sync_fileindex(fileindex, fileindex_path);
9110 got_fileindex_free(fileindex);
9112 return err;