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>
19 #include <dirent.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <zlib.h>
30 #include <fnmatch.h>
31 #include <libgen.h>
32 #include <uuid.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static const struct got_error *
66 create_meta_file(const char *path_got, const char *name, const char *content)
67 {
68 const struct got_error *err = NULL;
69 char *path;
71 if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 return got_error_from_errno("asprintf");
74 err = got_path_create_file(path, content);
75 free(path);
76 return err;
77 }
79 static const struct got_error *
80 update_meta_file(const char *path_got, const char *name, const char *content)
81 {
82 const struct got_error *err = NULL;
83 FILE *tmpfile = NULL;
84 char *tmppath = NULL;
85 char *path = NULL;
87 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 err = got_error_from_errno("asprintf");
89 path = NULL;
90 goto done;
91 }
93 err = got_opentemp_named(&tmppath, &tmpfile, path);
94 if (err)
95 goto done;
97 if (content) {
98 int len = fprintf(tmpfile, "%s\n", content);
99 if (len != strlen(content) + 1) {
100 err = got_error_from_errno2("fprintf", tmppath);
101 goto done;
105 if (rename(tmppath, path) != 0) {
106 err = got_error_from_errno3("rename", tmppath, path);
107 unlink(tmppath);
108 goto done;
111 done:
112 if (fclose(tmpfile) == EOF && err == NULL)
113 err = got_error_from_errno2("fclose", tmppath);
114 free(tmppath);
115 return err;
118 static const struct got_error *
119 write_head_ref(const char *path_got, struct got_reference *head_ref)
121 const struct got_error *err = NULL;
122 char *refstr = NULL;
124 if (got_ref_is_symbolic(head_ref)) {
125 refstr = got_ref_to_str(head_ref);
126 if (refstr == NULL)
127 return got_error_from_errno("got_ref_to_str");
128 } else {
129 refstr = strdup(got_ref_get_name(head_ref));
130 if (refstr == NULL)
131 return got_error_from_errno("strdup");
133 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
134 free(refstr);
135 return err;
138 const struct got_error *
139 got_worktree_init(const char *path, struct got_reference *head_ref,
140 const char *prefix, struct got_repository *repo)
142 const struct got_error *err = NULL;
143 struct got_object_id *commit_id = NULL;
144 uuid_t uuid;
145 uint32_t uuid_status;
146 int obj_type;
147 char *path_got = NULL;
148 char *formatstr = NULL;
149 char *absprefix = NULL;
150 char *basestr = NULL;
151 char *uuidstr = NULL;
153 if (strcmp(path, got_repo_get_path(repo)) == 0) {
154 err = got_error(GOT_ERR_WORKTREE_REPO);
155 goto done;
158 err = got_ref_resolve(&commit_id, repo, head_ref);
159 if (err)
160 return err;
161 err = got_object_get_type(&obj_type, repo, commit_id);
162 if (err)
163 return err;
164 if (obj_type != GOT_OBJ_TYPE_COMMIT)
165 return got_error(GOT_ERR_OBJ_TYPE);
167 if (!got_path_is_absolute(prefix)) {
168 if (asprintf(&absprefix, "/%s", prefix) == -1)
169 return got_error_from_errno("asprintf");
172 /* Create top-level directory (may already exist). */
173 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
174 err = got_error_from_errno2("mkdir", path);
175 goto done;
178 /* Create .got directory (may already exist). */
179 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
180 err = got_error_from_errno("asprintf");
181 goto done;
183 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path_got);
185 goto done;
188 /* Create an empty lock file. */
189 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
190 if (err)
191 goto done;
193 /* Create an empty file index. */
194 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
195 if (err)
196 goto done;
198 /* Write the HEAD reference. */
199 err = write_head_ref(path_got, head_ref);
200 if (err)
201 goto done;
203 /* Record our base commit. */
204 err = got_object_id_str(&basestr, commit_id);
205 if (err)
206 goto done;
207 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
208 if (err)
209 goto done;
211 /* Store path to repository. */
212 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
213 got_repo_get_path(repo));
214 if (err)
215 goto done;
217 /* Store in-repository path prefix. */
218 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
219 absprefix ? absprefix : prefix);
220 if (err)
221 goto done;
223 /* Generate UUID. */
224 uuid_create(&uuid, &uuid_status);
225 if (uuid_status != uuid_s_ok) {
226 err = got_error_uuid(uuid_status, "uuid_create");
227 goto done;
229 uuid_to_string(&uuid, &uuidstr, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_to_string");
232 goto done;
234 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
235 if (err)
236 goto done;
238 /* Stamp work tree with format file. */
239 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
240 err = got_error_from_errno("asprintf");
241 goto done;
243 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
244 if (err)
245 goto done;
247 done:
248 free(commit_id);
249 free(path_got);
250 free(formatstr);
251 free(absprefix);
252 free(basestr);
253 free(uuidstr);
254 return err;
257 const struct got_error *
258 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
259 const char *path_prefix)
261 char *absprefix = NULL;
263 if (!got_path_is_absolute(path_prefix)) {
264 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
265 return got_error_from_errno("asprintf");
267 *match = (strcmp(absprefix ? absprefix : path_prefix,
268 worktree->path_prefix) == 0);
269 free(absprefix);
270 return NULL;
273 const char *
274 got_worktree_get_head_ref_name(struct got_worktree *worktree)
276 return worktree->head_ref_name;
279 const struct got_error *
280 got_worktree_set_head_ref(struct got_worktree *worktree,
281 struct got_reference *head_ref)
283 const struct got_error *err = NULL;
284 char *path_got = NULL, *head_ref_name = NULL;
286 if (asprintf(&path_got, "%s/%s", worktree->root_path,
287 GOT_WORKTREE_GOT_DIR) == -1) {
288 err = got_error_from_errno("asprintf");
289 path_got = NULL;
290 goto done;
293 head_ref_name = strdup(got_ref_get_name(head_ref));
294 if (head_ref_name == NULL) {
295 err = got_error_from_errno("strdup");
296 goto done;
299 err = write_head_ref(path_got, head_ref);
300 if (err)
301 goto done;
303 free(worktree->head_ref_name);
304 worktree->head_ref_name = head_ref_name;
305 done:
306 free(path_got);
307 if (err)
308 free(head_ref_name);
309 return err;
312 struct got_object_id *
313 got_worktree_get_base_commit_id(struct got_worktree *worktree)
315 return worktree->base_commit_id;
318 const struct got_error *
319 got_worktree_set_base_commit_id(struct got_worktree *worktree,
320 struct got_repository *repo, struct got_object_id *commit_id)
322 const struct got_error *err;
323 struct got_object *obj = NULL;
324 char *id_str = NULL;
325 char *path_got = NULL;
327 if (asprintf(&path_got, "%s/%s", worktree->root_path,
328 GOT_WORKTREE_GOT_DIR) == -1) {
329 err = got_error_from_errno("asprintf");
330 path_got = NULL;
331 goto done;
334 err = got_object_open(&obj, repo, commit_id);
335 if (err)
336 return err;
338 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
339 err = got_error(GOT_ERR_OBJ_TYPE);
340 goto done;
343 /* Record our base commit. */
344 err = got_object_id_str(&id_str, commit_id);
345 if (err)
346 goto done;
347 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
348 if (err)
349 goto done;
351 free(worktree->base_commit_id);
352 worktree->base_commit_id = got_object_id_dup(commit_id);
353 if (worktree->base_commit_id == NULL) {
354 err = got_error_from_errno("got_object_id_dup");
355 goto done;
357 done:
358 if (obj)
359 got_object_close(obj);
360 free(id_str);
361 free(path_got);
362 return err;
365 const struct got_gotconfig *
366 got_worktree_get_gotconfig(struct got_worktree *worktree)
368 return worktree->gotconfig;
371 static const struct got_error *
372 lock_worktree(struct got_worktree *worktree, int operation)
374 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
375 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
376 : got_error_from_errno2("flock",
377 got_worktree_get_root_path(worktree)));
378 return NULL;
381 static const struct got_error *
382 add_dir_on_disk(struct got_worktree *worktree, const char *path)
384 const struct got_error *err = NULL;
385 char *abspath;
387 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
388 return got_error_from_errno("asprintf");
390 err = got_path_mkdir(abspath);
391 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
392 struct stat sb;
393 err = NULL;
394 if (lstat(abspath, &sb) == -1) {
395 err = got_error_from_errno2("lstat", abspath);
396 } else if (!S_ISDIR(sb.st_mode)) {
397 /* TODO directory is obstructed; do something */
398 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 free(abspath);
402 return err;
405 static const struct got_error *
406 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
408 const struct got_error *err = NULL;
409 uint8_t fbuf1[8192];
410 uint8_t fbuf2[8192];
411 size_t flen1 = 0, flen2 = 0;
413 *same = 1;
415 for (;;) {
416 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
417 if (flen1 == 0 && ferror(f1)) {
418 err = got_error_from_errno("fread");
419 break;
421 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
422 if (flen2 == 0 && ferror(f2)) {
423 err = got_error_from_errno("fread");
424 break;
426 if (flen1 == 0) {
427 if (flen2 != 0)
428 *same = 0;
429 break;
430 } else if (flen2 == 0) {
431 if (flen1 != 0)
432 *same = 0;
433 break;
434 } else if (flen1 == flen2) {
435 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
436 *same = 0;
437 break;
439 } else {
440 *same = 0;
441 break;
445 return err;
448 static const struct got_error *
449 check_files_equal(int *same, FILE *f1, FILE *f2)
451 struct stat sb;
452 size_t size1, size2;
454 *same = 1;
456 if (fstat(fileno(f1), &sb) != 0)
457 return got_error_from_errno("fstat");
458 size1 = sb.st_size;
460 if (fstat(fileno(f2), &sb) != 0)
461 return got_error_from_errno("fstat");
462 size2 = sb.st_size;
464 if (size1 != size2) {
465 *same = 0;
466 return NULL;
469 if (fseek(f1, 0L, SEEK_SET) == -1)
470 return got_ferror(f1, GOT_ERR_IO);
471 if (fseek(f2, 0L, SEEK_SET) == -1)
472 return got_ferror(f2, GOT_ERR_IO);
474 return check_file_contents_equal(same, f1, f2);
477 static const struct got_error *
478 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
480 uint8_t fbuf[65536];
481 size_t flen;
482 ssize_t outlen;
484 *outsize = 0;
486 if (fseek(f, 0L, SEEK_SET) == -1)
487 return got_ferror(f, GOT_ERR_IO);
489 for (;;) {
490 flen = fread(fbuf, 1, sizeof(fbuf), f);
491 if (flen == 0) {
492 if (ferror(f))
493 return got_error_from_errno("fread");
494 if (feof(f))
495 break;
497 outlen = write(outfd, fbuf, flen);
498 if (outlen == -1)
499 return got_error_from_errno("write");
500 if (outlen != flen)
501 return got_error(GOT_ERR_IO);
502 *outsize += outlen;
505 return NULL;
508 static const struct got_error *
509 merge_binary_file(int *overlapcnt, int merged_fd,
510 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
511 const char *label_deriv, const char *label_orig, const char *label_deriv2,
512 const char *ondisk_path)
514 const struct got_error *err = NULL;
515 int same_content, changed_deriv, changed_deriv2;
516 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
517 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
518 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
519 char *base_path_orig = NULL, *base_path_deriv = NULL;
520 char *base_path_deriv2 = NULL;
522 *overlapcnt = 0;
524 err = check_files_equal(&same_content, f_deriv, f_deriv2);
525 if (err)
526 return err;
528 if (same_content)
529 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
531 err = check_files_equal(&same_content, f_deriv, f_orig);
532 if (err)
533 return err;
534 changed_deriv = !same_content;
535 err = check_files_equal(&same_content, f_deriv2, f_orig);
536 if (err)
537 return err;
538 changed_deriv2 = !same_content;
540 if (changed_deriv && changed_deriv2) {
541 *overlapcnt = 1;
542 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
543 err = got_error_from_errno("asprintf");
544 goto done;
546 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
550 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
552 goto done;
554 err = got_opentemp_named_fd(&path_orig, &fd_orig,
555 base_path_orig);
556 if (err)
557 goto done;
558 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
559 base_path_deriv);
560 if (err)
561 goto done;
562 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
563 base_path_deriv2);
564 if (err)
565 goto done;
566 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
567 if (err)
568 goto done;
569 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
570 if (err)
571 goto done;
572 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
573 if (err)
574 goto done;
575 if (dprintf(merged_fd, "Binary files differ and cannot be "
576 "merged automatically:\n") < 0) {
577 err = got_error_from_errno("dprintf");
578 goto done;
580 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
581 GOT_DIFF_CONFLICT_MARKER_BEGIN,
582 label_deriv ? " " : "",
583 label_deriv ? label_deriv : "",
584 path_deriv) < 0) {
585 err = got_error_from_errno("dprintf");
586 goto done;
588 if (size_orig > 0) {
589 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
590 GOT_DIFF_CONFLICT_MARKER_ORIG,
591 label_orig ? " " : "",
592 label_orig ? label_orig : "",
593 path_orig) < 0) {
594 err = got_error_from_errno("dprintf");
595 goto done;
598 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
599 GOT_DIFF_CONFLICT_MARKER_SEP,
600 path_deriv2,
601 GOT_DIFF_CONFLICT_MARKER_END,
602 label_deriv2 ? " " : "",
603 label_deriv2 ? label_deriv2 : "") < 0) {
604 err = got_error_from_errno("dprintf");
605 goto done;
607 } else if (changed_deriv)
608 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
609 else if (changed_deriv2)
610 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
611 done:
612 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
613 err == NULL)
614 err = got_error_from_errno2("unlink", path_orig);
615 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
616 err = got_error_from_errno2("close", path_orig);
617 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_deriv);
619 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv2);
621 free(path_orig);
622 free(path_deriv);
623 free(path_deriv2);
624 free(base_path_orig);
625 free(base_path_deriv);
626 free(base_path_deriv2);
627 return err;
630 /*
631 * Perform a 3-way merge where the file f_orig acts as the common
632 * ancestor, the file f_deriv acts as the first derived version,
633 * and the file f_deriv2 acts as the second derived version.
634 * The merge result will be written to a new file at ondisk_path; any
635 * existing file at this path will be replaced.
636 */
637 static const struct got_error *
638 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
639 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
640 const char *path, uint16_t st_mode,
641 const char *label_orig, const char *label_deriv, const char *label_deriv2,
642 enum got_diff_algorithm diff_algo, struct got_repository *repo,
643 got_worktree_checkout_cb progress_cb, void *progress_arg)
645 const struct got_error *err = NULL;
646 int merged_fd = -1;
647 FILE *f_merged = NULL;
648 char *merged_path = NULL, *base_path = NULL;
649 int overlapcnt = 0;
650 char *parent = NULL;
652 *local_changes_subsumed = 0;
654 err = got_path_dirname(&parent, ondisk_path);
655 if (err)
656 return err;
658 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
659 err = got_error_from_errno("asprintf");
660 goto done;
663 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
664 if (err)
665 goto done;
667 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
668 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
669 if (err) {
670 if (err->code != GOT_ERR_FILE_BINARY)
671 goto done;
672 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
673 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
674 ondisk_path);
675 if (err)
676 goto done;
679 err = (*progress_cb)(progress_arg,
680 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
681 if (err)
682 goto done;
684 if (fsync(merged_fd) != 0) {
685 err = got_error_from_errno("fsync");
686 goto done;
689 f_merged = fdopen(merged_fd, "r");
690 if (f_merged == NULL) {
691 err = got_error_from_errno("fdopen");
692 goto done;
694 merged_fd = -1;
696 /* Check if a clean merge has subsumed all local changes. */
697 if (overlapcnt == 0) {
698 err = check_files_equal(local_changes_subsumed, f_deriv,
699 f_merged);
700 if (err)
701 goto done;
704 if (fchmod(fileno(f_merged), st_mode) != 0) {
705 err = got_error_from_errno2("fchmod", merged_path);
706 goto done;
709 if (rename(merged_path, ondisk_path) != 0) {
710 err = got_error_from_errno3("rename", merged_path,
711 ondisk_path);
712 goto done;
714 done:
715 if (err) {
716 if (merged_path)
717 unlink(merged_path);
719 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
720 err = got_error_from_errno("close");
721 if (f_merged && fclose(f_merged) == EOF && err == NULL)
722 err = got_error_from_errno("fclose");
723 free(merged_path);
724 free(base_path);
725 free(parent);
726 return err;
729 static const struct got_error *
730 update_symlink(const char *ondisk_path, const char *target_path,
731 size_t target_len)
733 /* This is not atomic but matches what 'ln -sf' does. */
734 if (unlink(ondisk_path) == -1)
735 return got_error_from_errno2("unlink", ondisk_path);
736 if (symlink(target_path, ondisk_path) == -1)
737 return got_error_from_errno3("symlink", target_path,
738 ondisk_path);
739 return NULL;
742 /*
743 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
744 * in the work tree with a file that contains conflict markers and the
745 * conflicting target paths of the original version, a "derived version"
746 * of a symlink from an incoming change, and a local version of the symlink.
748 * The original versions's target path can be NULL if it is not available,
749 * such as if both derived versions added a new symlink at the same path.
751 * The incoming derived symlink target is NULL in case the incoming change
752 * has deleted this symlink.
753 */
754 static const struct got_error *
755 install_symlink_conflict(const char *deriv_target,
756 struct got_object_id *deriv_base_commit_id, const char *orig_target,
757 const char *label_orig, const char *local_target, const char *ondisk_path)
759 const struct got_error *err;
760 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
761 FILE *f = NULL;
763 err = got_object_id_str(&id_str, deriv_base_commit_id);
764 if (err)
765 return got_error_from_errno("asprintf");
767 if (asprintf(&label_deriv, "%s: commit %s",
768 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
769 err = got_error_from_errno("asprintf");
770 goto done;
773 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
774 if (err)
775 goto done;
777 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
778 err = got_error_from_errno2("fchmod", path);
779 goto done;
782 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
783 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
784 deriv_target ? deriv_target : "(symlink was deleted)",
785 orig_target ? label_orig : "",
786 orig_target ? "\n" : "",
787 orig_target ? orig_target : "",
788 orig_target ? "\n" : "",
789 GOT_DIFF_CONFLICT_MARKER_SEP,
790 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
791 err = got_error_from_errno2("fprintf", path);
792 goto done;
795 if (unlink(ondisk_path) == -1) {
796 err = got_error_from_errno2("unlink", ondisk_path);
797 goto done;
799 if (rename(path, ondisk_path) == -1) {
800 err = got_error_from_errno3("rename", path, ondisk_path);
801 goto done;
803 done:
804 if (f != NULL && fclose(f) == EOF && err == NULL)
805 err = got_error_from_errno2("fclose", path);
806 free(path);
807 free(id_str);
808 free(label_deriv);
809 return err;
812 /* forward declaration */
813 static const struct got_error *
814 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
815 const char *, const char *, uint16_t, const char *,
816 struct got_blob_object *, struct got_object_id *,
817 struct got_repository *, got_worktree_checkout_cb, void *);
819 /*
820 * Merge a symlink into the work tree, where blob_orig acts as the common
821 * ancestor, deriv_target is the link target of the first derived version,
822 * and the symlink on disk acts as the second derived version.
823 * Assume that contents of both blobs represent symlinks.
824 */
825 static const struct got_error *
826 merge_symlink(struct got_worktree *worktree,
827 struct got_blob_object *blob_orig, const char *ondisk_path,
828 const char *path, const char *label_orig, const char *deriv_target,
829 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
830 got_worktree_checkout_cb progress_cb, void *progress_arg)
832 const struct got_error *err = NULL;
833 char *ancestor_target = NULL;
834 struct stat sb;
835 ssize_t ondisk_len, deriv_len;
836 char ondisk_target[PATH_MAX];
837 int have_local_change = 0;
838 int have_incoming_change = 0;
840 if (lstat(ondisk_path, &sb) == -1)
841 return got_error_from_errno2("lstat", ondisk_path);
843 ondisk_len = readlink(ondisk_path, ondisk_target,
844 sizeof(ondisk_target));
845 if (ondisk_len == -1) {
846 err = got_error_from_errno2("readlink",
847 ondisk_path);
848 goto done;
850 ondisk_target[ondisk_len] = '\0';
852 if (blob_orig) {
853 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
854 if (err)
855 goto done;
858 if (ancestor_target == NULL ||
859 (ondisk_len != strlen(ancestor_target) ||
860 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
861 have_local_change = 1;
863 deriv_len = strlen(deriv_target);
864 if (ancestor_target == NULL ||
865 (deriv_len != strlen(ancestor_target) ||
866 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
867 have_incoming_change = 1;
869 if (!have_local_change && !have_incoming_change) {
870 if (ancestor_target) {
871 /* Both sides made the same change. */
872 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
873 path);
874 } else if (deriv_len == ondisk_len &&
875 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
876 /* Both sides added the same symlink. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 path);
879 } else {
880 /* Both sides added symlinks which don't match. */
881 err = install_symlink_conflict(deriv_target,
882 deriv_base_commit_id, ancestor_target,
883 label_orig, ondisk_target, ondisk_path);
884 if (err)
885 goto done;
886 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
887 path);
889 } else if (!have_local_change && have_incoming_change) {
890 /* Apply the incoming change. */
891 err = update_symlink(ondisk_path, deriv_target,
892 strlen(deriv_target));
893 if (err)
894 goto done;
895 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
896 } else if (have_local_change && have_incoming_change) {
897 if (deriv_len == ondisk_len &&
898 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
899 /* Both sides made the same change. */
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
901 path);
902 } else {
903 err = install_symlink_conflict(deriv_target,
904 deriv_base_commit_id, ancestor_target, label_orig,
905 ondisk_target, ondisk_path);
906 if (err)
907 goto done;
908 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
909 path);
913 done:
914 free(ancestor_target);
915 return err;
918 static const struct got_error *
919 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
921 const struct got_error *err = NULL;
922 char target_path[PATH_MAX];
923 ssize_t target_len;
924 size_t n;
925 FILE *f;
927 *outfile = NULL;
929 f = got_opentemp();
930 if (f == NULL)
931 return got_error_from_errno("got_opentemp");
932 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
933 if (target_len == -1) {
934 err = got_error_from_errno2("readlink", ondisk_path);
935 goto done;
937 n = fwrite(target_path, 1, target_len, f);
938 if (n != target_len) {
939 err = got_ferror(f, GOT_ERR_IO);
940 goto done;
942 if (fflush(f) == EOF) {
943 err = got_error_from_errno("fflush");
944 goto done;
946 if (fseek(f, 0L, SEEK_SET) == -1) {
947 err = got_ferror(f, GOT_ERR_IO);
948 goto done;
950 done:
951 if (err)
952 fclose(f);
953 else
954 *outfile = f;
955 return err;
958 /*
959 * Perform a 3-way merge where blob_orig acts as the common ancestor,
960 * blob_deriv acts as the first derived version, and the file on disk
961 * acts as the second derived version.
962 */
963 static const struct got_error *
964 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
965 struct got_blob_object *blob_orig, const char *ondisk_path,
966 const char *path, uint16_t st_mode, const char *label_orig,
967 struct got_blob_object *blob_deriv,
968 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
969 got_worktree_checkout_cb progress_cb, void *progress_arg)
971 const struct got_error *err = NULL;
972 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
973 char *blob_orig_path = NULL;
974 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
975 char *label_deriv = NULL, *parent = NULL;
977 *local_changes_subsumed = 0;
979 err = got_path_dirname(&parent, ondisk_path);
980 if (err)
981 return err;
983 if (blob_orig) {
984 if (asprintf(&base_path, "%s/got-merge-blob-orig",
985 parent) == -1) {
986 err = got_error_from_errno("asprintf");
987 base_path = NULL;
988 goto done;
991 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
992 if (err)
993 goto done;
994 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
995 blob_orig);
996 if (err)
997 goto done;
998 free(base_path);
999 } else {
1001 * No common ancestor exists. This is an "add vs add" conflict
1002 * and we simply use an empty ancestor file to make both files
1003 * appear in the merged result in their entirety.
1005 f_orig = got_opentemp();
1006 if (f_orig == NULL) {
1007 err = got_error_from_errno("got_opentemp");
1008 goto done;
1012 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1013 err = got_error_from_errno("asprintf");
1014 base_path = NULL;
1015 goto done;
1018 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1019 if (err)
1020 goto done;
1021 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1022 blob_deriv);
1023 if (err)
1024 goto done;
1026 err = got_object_id_str(&id_str, deriv_base_commit_id);
1027 if (err)
1028 goto done;
1029 if (asprintf(&label_deriv, "%s: commit %s",
1030 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1031 err = got_error_from_errno("asprintf");
1032 goto done;
1036 * In order the run a 3-way merge with a symlink we copy the symlink's
1037 * target path into a temporary file and use that file with diff3.
1039 if (S_ISLNK(st_mode)) {
1040 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1041 if (err)
1042 goto done;
1043 } else {
1044 int fd;
1045 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1046 if (fd == -1) {
1047 err = got_error_from_errno2("open", ondisk_path);
1048 goto done;
1050 f_deriv2 = fdopen(fd, "r");
1051 if (f_deriv2 == NULL) {
1052 err = got_error_from_errno2("fdopen", ondisk_path);
1053 close(fd);
1054 goto done;
1058 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1059 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1060 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1061 done:
1062 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1063 err = got_error_from_errno("fclose");
1064 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 free(base_path);
1069 if (blob_orig_path) {
1070 unlink(blob_orig_path);
1071 free(blob_orig_path);
1073 if (blob_deriv_path) {
1074 unlink(blob_deriv_path);
1075 free(blob_deriv_path);
1077 free(id_str);
1078 free(label_deriv);
1079 free(parent);
1080 return err;
1083 static const struct got_error *
1084 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1085 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1086 int wt_fd, const char *path, struct got_object_id *blob_id)
1088 const struct got_error *err = NULL;
1089 struct got_fileindex_entry *new_ie;
1091 *new_iep = NULL;
1093 err = got_fileindex_entry_alloc(&new_ie, path);
1094 if (err)
1095 return err;
1097 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1098 blob_id->sha1, base_commit_id->sha1, 1);
1099 if (err)
1100 goto done;
1102 err = got_fileindex_entry_add(fileindex, new_ie);
1103 done:
1104 if (err)
1105 got_fileindex_entry_free(new_ie);
1106 else
1107 *new_iep = new_ie;
1108 return err;
1111 static mode_t
1112 get_ondisk_perms(int executable, mode_t st_mode)
1114 mode_t xbits = S_IXUSR;
1116 if (executable) {
1117 /* Map read bits to execute bits. */
1118 if (st_mode & S_IRGRP)
1119 xbits |= S_IXGRP;
1120 if (st_mode & S_IROTH)
1121 xbits |= S_IXOTH;
1122 return st_mode | xbits;
1125 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 /* forward declaration */
1129 static const struct got_error *
1130 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 const char *path, mode_t te_mode, mode_t st_mode,
1132 struct got_blob_object *blob, int restoring_missing_file,
1133 int reverting_versioned_file, int installing_bad_symlink,
1134 int path_is_unversioned, struct got_repository *repo,
1135 got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 * This function assumes that the provided symlink target points at a
1139 * safe location in the work tree!
1141 static const struct got_error *
1142 replace_existing_symlink(int *did_something, const char *ondisk_path,
1143 const char *target_path, size_t target_len)
1145 const struct got_error *err = NULL;
1146 ssize_t elen;
1147 char etarget[PATH_MAX];
1148 int fd;
1150 *did_something = 0;
1153 * "Bad" symlinks (those pointing outside the work tree or into the
1154 * .got directory) are installed in the work tree as a regular file
1155 * which contains the bad symlink target path.
1156 * The new symlink target has already been checked for safety by our
1157 * caller. If we can successfully open a regular file then we simply
1158 * replace this file with a symlink below.
1160 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1161 if (fd == -1) {
1162 if (!got_err_open_nofollow_on_symlink())
1163 return got_error_from_errno2("open", ondisk_path);
1165 /* We are updating an existing on-disk symlink. */
1166 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1167 if (elen == -1)
1168 return got_error_from_errno2("readlink", ondisk_path);
1170 if (elen == target_len &&
1171 memcmp(etarget, target_path, target_len) == 0)
1172 return NULL; /* nothing to do */
1175 *did_something = 1;
1176 err = update_symlink(ondisk_path, target_path, target_len);
1177 if (fd != -1 && close(fd) == -1 && err == NULL)
1178 err = got_error_from_errno2("close", ondisk_path);
1179 return err;
1182 static const struct got_error *
1183 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1184 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1186 const struct got_error *err = NULL;
1187 char canonpath[PATH_MAX];
1188 char *path_got = NULL;
1190 *is_bad_symlink = 0;
1192 if (target_len >= sizeof(canonpath)) {
1193 *is_bad_symlink = 1;
1194 return NULL;
1198 * We do not use realpath(3) to resolve the symlink's target
1199 * path because we don't want to resolve symlinks recursively.
1200 * Instead we make the path absolute and then canonicalize it.
1201 * Relative symlink target lookup should begin at the directory
1202 * in which the blob object is being installed.
1204 if (!got_path_is_absolute(target_path)) {
1205 char *abspath, *parent;
1206 err = got_path_dirname(&parent, ondisk_path);
1207 if (err)
1208 return err;
1209 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1210 free(parent);
1211 return got_error_from_errno("asprintf");
1213 free(parent);
1214 if (strlen(abspath) >= sizeof(canonpath)) {
1215 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1216 free(abspath);
1217 return err;
1219 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1220 free(abspath);
1221 if (err)
1222 return err;
1223 } else {
1224 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1225 if (err)
1226 return err;
1229 /* Only allow symlinks pointing at paths within the work tree. */
1230 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1231 *is_bad_symlink = 1;
1232 return NULL;
1235 /* Do not allow symlinks pointing into the .got directory. */
1236 if (asprintf(&path_got, "%s/%s", wtroot_path,
1237 GOT_WORKTREE_GOT_DIR) == -1)
1238 return got_error_from_errno("asprintf");
1239 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1240 *is_bad_symlink = 1;
1242 free(path_got);
1243 return NULL;
1246 static const struct got_error *
1247 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1248 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1249 int restoring_missing_file, int reverting_versioned_file,
1250 int path_is_unversioned, int allow_bad_symlinks,
1251 struct got_repository *repo,
1252 got_worktree_checkout_cb progress_cb, void *progress_arg)
1254 const struct got_error *err = NULL;
1255 char target_path[PATH_MAX];
1256 size_t len, target_len = 0;
1257 char *path_got = NULL;
1258 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1259 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1261 *is_bad_symlink = 0;
1264 * Blob object content specifies the target path of the link.
1265 * If a symbolic link cannot be installed we instead create
1266 * a regular file which contains the link target path stored
1267 * in the blob object.
1269 do {
1270 err = got_object_blob_read_block(&len, blob);
1271 if (len + target_len >= sizeof(target_path)) {
1272 /* Path too long; install as a regular file. */
1273 *is_bad_symlink = 1;
1274 got_object_blob_rewind(blob);
1275 return install_blob(worktree, ondisk_path, path,
1276 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1277 restoring_missing_file, reverting_versioned_file,
1278 1, path_is_unversioned, repo, progress_cb,
1279 progress_arg);
1281 if (len > 0) {
1282 /* Skip blob object header first time around. */
1283 memcpy(target_path + target_len, buf + hdrlen,
1284 len - hdrlen);
1285 target_len += len - hdrlen;
1286 hdrlen = 0;
1288 } while (len != 0);
1289 target_path[target_len] = '\0';
1291 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1292 ondisk_path, worktree->root_path);
1293 if (err)
1294 return err;
1296 if (*is_bad_symlink && !allow_bad_symlinks) {
1297 /* install as a regular file */
1298 got_object_blob_rewind(blob);
1299 err = install_blob(worktree, ondisk_path, path,
1300 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1301 restoring_missing_file, reverting_versioned_file, 1,
1302 path_is_unversioned, repo, progress_cb, progress_arg);
1303 goto done;
1306 if (symlink(target_path, ondisk_path) == -1) {
1307 if (errno == EEXIST) {
1308 int symlink_replaced;
1309 if (path_is_unversioned) {
1310 err = (*progress_cb)(progress_arg,
1311 GOT_STATUS_UNVERSIONED, path);
1312 goto done;
1314 err = replace_existing_symlink(&symlink_replaced,
1315 ondisk_path, target_path, target_len);
1316 if (err)
1317 goto done;
1318 if (progress_cb) {
1319 if (symlink_replaced) {
1320 err = (*progress_cb)(progress_arg,
1321 reverting_versioned_file ?
1322 GOT_STATUS_REVERT :
1323 GOT_STATUS_UPDATE, path);
1324 } else {
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_EXISTS, path);
1329 goto done; /* Nothing else to do. */
1332 if (errno == ENOENT) {
1333 char *parent;
1334 err = got_path_dirname(&parent, ondisk_path);
1335 if (err)
1336 goto done;
1337 err = add_dir_on_disk(worktree, parent);
1338 free(parent);
1339 if (err)
1340 goto done;
1342 * Retry, and fall through to error handling
1343 * below if this second attempt fails.
1345 if (symlink(target_path, ondisk_path) != -1) {
1346 err = NULL; /* success */
1347 goto done;
1351 /* Handle errors from first or second creation attempt. */
1352 if (errno == ENAMETOOLONG) {
1353 /* bad target path; install as a regular file */
1354 *is_bad_symlink = 1;
1355 got_object_blob_rewind(blob);
1356 err = install_blob(worktree, ondisk_path, path,
1357 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1358 restoring_missing_file, reverting_versioned_file, 1,
1359 path_is_unversioned, repo,
1360 progress_cb, progress_arg);
1361 } else if (errno == ENOTDIR) {
1362 err = got_error_path(ondisk_path,
1363 GOT_ERR_FILE_OBSTRUCTED);
1364 } else {
1365 err = got_error_from_errno3("symlink",
1366 target_path, ondisk_path);
1368 } else if (progress_cb)
1369 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1370 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1371 done:
1372 free(path_got);
1373 return err;
1376 static const struct got_error *
1377 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1378 const char *path, mode_t te_mode, mode_t st_mode,
1379 struct got_blob_object *blob, int restoring_missing_file,
1380 int reverting_versioned_file, int installing_bad_symlink,
1381 int path_is_unversioned, struct got_repository *repo,
1382 got_worktree_checkout_cb progress_cb, void *progress_arg)
1384 const struct got_error *err = NULL;
1385 int fd = -1;
1386 size_t len, hdrlen;
1387 int update = 0;
1388 char *tmppath = NULL;
1390 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1391 GOT_DEFAULT_FILE_MODE);
1392 if (fd == -1) {
1393 if (errno == ENOENT) {
1394 char *parent;
1395 err = got_path_dirname(&parent, path);
1396 if (err)
1397 return err;
1398 err = add_dir_on_disk(worktree, parent);
1399 free(parent);
1400 if (err)
1401 return err;
1402 fd = open(ondisk_path,
1403 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1404 GOT_DEFAULT_FILE_MODE);
1405 if (fd == -1)
1406 return got_error_from_errno2("open",
1407 ondisk_path);
1408 } else if (errno == EEXIST) {
1409 if (path_is_unversioned) {
1410 err = (*progress_cb)(progress_arg,
1411 GOT_STATUS_UNVERSIONED, path);
1412 goto done;
1414 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1415 !S_ISREG(st_mode) && !installing_bad_symlink) {
1416 /* TODO file is obstructed; do something */
1417 err = got_error_path(ondisk_path,
1418 GOT_ERR_FILE_OBSTRUCTED);
1419 goto done;
1420 } else {
1421 err = got_opentemp_named_fd(&tmppath, &fd,
1422 ondisk_path);
1423 if (err)
1424 goto done;
1425 update = 1;
1427 } else
1428 return got_error_from_errno2("open", ondisk_path);
1431 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1432 err = got_error_from_errno2("fchmod",
1433 update ? tmppath : ondisk_path);
1434 goto done;
1437 if (progress_cb) {
1438 if (restoring_missing_file)
1439 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1440 path);
1441 else if (reverting_versioned_file)
1442 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1443 path);
1444 else
1445 err = (*progress_cb)(progress_arg,
1446 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1447 if (err)
1448 goto done;
1451 hdrlen = got_object_blob_get_hdrlen(blob);
1452 do {
1453 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1454 err = got_object_blob_read_block(&len, blob);
1455 if (err)
1456 break;
1457 if (len > 0) {
1458 /* Skip blob object header first time around. */
1459 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1460 if (outlen == -1) {
1461 err = got_error_from_errno("write");
1462 goto done;
1463 } else if (outlen != len - hdrlen) {
1464 err = got_error(GOT_ERR_IO);
1465 goto done;
1467 hdrlen = 0;
1469 } while (len != 0);
1471 if (fsync(fd) != 0) {
1472 err = got_error_from_errno("fsync");
1473 goto done;
1476 if (update) {
1477 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1478 err = got_error_from_errno2("unlink", ondisk_path);
1479 goto done;
1481 if (rename(tmppath, ondisk_path) != 0) {
1482 err = got_error_from_errno3("rename", tmppath,
1483 ondisk_path);
1484 goto done;
1486 free(tmppath);
1487 tmppath = NULL;
1490 done:
1491 if (fd != -1 && close(fd) == -1 && err == NULL)
1492 err = got_error_from_errno("close");
1493 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1494 err = got_error_from_errno2("unlink", tmppath);
1495 free(tmppath);
1496 return err;
1499 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1500 static const struct got_error *
1501 get_modified_file_content_status(unsigned char *status, FILE *f)
1503 const struct got_error *err = NULL;
1504 const char *markers[3] = {
1505 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1506 GOT_DIFF_CONFLICT_MARKER_SEP,
1507 GOT_DIFF_CONFLICT_MARKER_END
1509 int i = 0;
1510 char *line = NULL;
1511 size_t linesize = 0;
1512 ssize_t linelen;
1514 while (*status == GOT_STATUS_MODIFY) {
1515 linelen = getline(&line, &linesize, f);
1516 if (linelen == -1) {
1517 if (feof(f))
1518 break;
1519 err = got_ferror(f, GOT_ERR_IO);
1520 break;
1523 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1524 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1525 == 0)
1526 *status = GOT_STATUS_CONFLICT;
1527 else
1528 i++;
1531 free(line);
1533 return err;
1536 static int
1537 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1539 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1540 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 static int
1544 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1546 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1547 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1548 ie->mtime_sec == sb->st_mtim.tv_sec &&
1549 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1550 ie->size == (sb->st_size & 0xffffffff) &&
1551 !xbit_differs(ie, sb->st_mode));
1554 static unsigned char
1555 get_staged_status(struct got_fileindex_entry *ie)
1557 switch (got_fileindex_entry_stage_get(ie)) {
1558 case GOT_FILEIDX_STAGE_ADD:
1559 return GOT_STATUS_ADD;
1560 case GOT_FILEIDX_STAGE_DELETE:
1561 return GOT_STATUS_DELETE;
1562 case GOT_FILEIDX_STAGE_MODIFY:
1563 return GOT_STATUS_MODIFY;
1564 default:
1565 return GOT_STATUS_NO_CHANGE;
1569 static const struct got_error *
1570 get_symlink_modification_status(unsigned char *status,
1571 struct got_fileindex_entry *ie, const char *abspath,
1572 int dirfd, const char *de_name, struct got_blob_object *blob)
1574 const struct got_error *err = NULL;
1575 char target_path[PATH_MAX];
1576 char etarget[PATH_MAX];
1577 ssize_t elen;
1578 size_t len, target_len = 0;
1579 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1582 *status = GOT_STATUS_NO_CHANGE;
1584 /* Blob object content specifies the target path of the link. */
1585 do {
1586 err = got_object_blob_read_block(&len, blob);
1587 if (err)
1588 return err;
1589 if (len + target_len >= sizeof(target_path)) {
1591 * Should not happen. The blob contents were OK
1592 * when this symlink was installed.
1594 return got_error(GOT_ERR_NO_SPACE);
1596 if (len > 0) {
1597 /* Skip blob object header first time around. */
1598 memcpy(target_path + target_len, buf + hdrlen,
1599 len - hdrlen);
1600 target_len += len - hdrlen;
1601 hdrlen = 0;
1603 } while (len != 0);
1604 target_path[target_len] = '\0';
1606 if (dirfd != -1) {
1607 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1608 if (elen == -1)
1609 return got_error_from_errno2("readlinkat", abspath);
1610 } else {
1611 elen = readlink(abspath, etarget, sizeof(etarget));
1612 if (elen == -1)
1613 return got_error_from_errno2("readlink", abspath);
1616 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1617 *status = GOT_STATUS_MODIFY;
1619 return NULL;
1622 static const struct got_error *
1623 get_file_status(unsigned char *status, struct stat *sb,
1624 struct got_fileindex_entry *ie, const char *abspath,
1625 int dirfd, const char *de_name, struct got_repository *repo)
1627 const struct got_error *err = NULL;
1628 struct got_object_id id;
1629 size_t hdrlen;
1630 int fd = -1;
1631 FILE *f = NULL;
1632 uint8_t fbuf[8192];
1633 struct got_blob_object *blob = NULL;
1634 size_t flen, blen;
1635 unsigned char staged_status = get_staged_status(ie);
1637 *status = GOT_STATUS_NO_CHANGE;
1638 memset(sb, 0, sizeof(*sb));
1641 * Whenever the caller provides a directory descriptor and a
1642 * directory entry name for the file, use them! This prevents
1643 * race conditions if filesystem paths change beneath our feet.
1645 if (dirfd != -1) {
1646 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1647 if (errno == ENOENT) {
1648 if (got_fileindex_entry_has_file_on_disk(ie))
1649 *status = GOT_STATUS_MISSING;
1650 else
1651 *status = GOT_STATUS_DELETE;
1652 goto done;
1654 err = got_error_from_errno2("fstatat", abspath);
1655 goto done;
1657 } else {
1658 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1659 if (fd == -1 && errno != ENOENT &&
1660 !got_err_open_nofollow_on_symlink())
1661 return got_error_from_errno2("open", abspath);
1662 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1663 if (lstat(abspath, sb) == -1)
1664 return got_error_from_errno2("lstat", abspath);
1665 } else if (fd == -1 || fstat(fd, sb) == -1) {
1666 if (errno == ENOENT) {
1667 if (got_fileindex_entry_has_file_on_disk(ie))
1668 *status = GOT_STATUS_MISSING;
1669 else
1670 *status = GOT_STATUS_DELETE;
1671 goto done;
1673 err = got_error_from_errno2("fstat", abspath);
1674 goto done;
1678 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1679 *status = GOT_STATUS_OBSTRUCTED;
1680 goto done;
1683 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1684 *status = GOT_STATUS_DELETE;
1685 goto done;
1686 } else if (!got_fileindex_entry_has_blob(ie) &&
1687 staged_status != GOT_STATUS_ADD) {
1688 *status = GOT_STATUS_ADD;
1689 goto done;
1692 if (!stat_info_differs(ie, sb))
1693 goto done;
1695 if (S_ISLNK(sb->st_mode) &&
1696 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1697 *status = GOT_STATUS_MODIFY;
1698 goto done;
1701 if (staged_status == GOT_STATUS_MODIFY ||
1702 staged_status == GOT_STATUS_ADD)
1703 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1704 else
1705 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1707 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1708 if (err)
1709 goto done;
1711 if (S_ISLNK(sb->st_mode)) {
1712 err = get_symlink_modification_status(status, ie,
1713 abspath, dirfd, de_name, blob);
1714 goto done;
1717 if (dirfd != -1) {
1718 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1719 if (fd == -1) {
1720 err = got_error_from_errno2("openat", abspath);
1721 goto done;
1725 f = fdopen(fd, "r");
1726 if (f == NULL) {
1727 err = got_error_from_errno2("fdopen", abspath);
1728 goto done;
1730 fd = -1;
1731 hdrlen = got_object_blob_get_hdrlen(blob);
1732 for (;;) {
1733 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1734 err = got_object_blob_read_block(&blen, blob);
1735 if (err)
1736 goto done;
1737 /* Skip length of blob object header first time around. */
1738 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1739 if (flen == 0 && ferror(f)) {
1740 err = got_error_from_errno("fread");
1741 goto done;
1743 if (blen - hdrlen == 0) {
1744 if (flen != 0)
1745 *status = GOT_STATUS_MODIFY;
1746 break;
1747 } else if (flen == 0) {
1748 if (blen - hdrlen != 0)
1749 *status = GOT_STATUS_MODIFY;
1750 break;
1751 } else if (blen - hdrlen == flen) {
1752 /* Skip blob object header first time around. */
1753 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1754 *status = GOT_STATUS_MODIFY;
1755 break;
1757 } else {
1758 *status = GOT_STATUS_MODIFY;
1759 break;
1761 hdrlen = 0;
1764 if (*status == GOT_STATUS_MODIFY) {
1765 rewind(f);
1766 err = get_modified_file_content_status(status, f);
1767 } else if (xbit_differs(ie, sb->st_mode))
1768 *status = GOT_STATUS_MODE_CHANGE;
1769 done:
1770 if (blob)
1771 got_object_blob_close(blob);
1772 if (f != NULL && fclose(f) == EOF && err == NULL)
1773 err = got_error_from_errno2("fclose", abspath);
1774 if (fd != -1 && close(fd) == -1 && err == NULL)
1775 err = got_error_from_errno2("close", abspath);
1776 return err;
1780 * Update timestamps in the file index if a file is unmodified and
1781 * we had to run a full content comparison to find out.
1783 static const struct got_error *
1784 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1785 struct got_fileindex_entry *ie, struct stat *sb)
1787 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1788 return got_fileindex_entry_update(ie, wt_fd, path,
1789 ie->blob_sha1, ie->commit_sha1, 1);
1791 return NULL;
1794 static const struct got_error *
1795 update_blob(struct got_worktree *worktree,
1796 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1797 struct got_tree_entry *te, const char *path,
1798 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1799 void *progress_arg)
1801 const struct got_error *err = NULL;
1802 struct got_blob_object *blob = NULL;
1803 char *ondisk_path;
1804 unsigned char status = GOT_STATUS_NO_CHANGE;
1805 struct stat sb;
1807 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1808 return got_error_from_errno("asprintf");
1810 if (ie) {
1811 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1812 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1813 goto done;
1815 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1816 repo);
1817 if (err)
1818 goto done;
1819 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1820 sb.st_mode = got_fileindex_perms_to_st(ie);
1821 } else {
1822 if (stat(ondisk_path, &sb) == -1) {
1823 if (errno != ENOENT) {
1824 err = got_error_from_errno2("stat",
1825 ondisk_path);
1826 goto done;
1828 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1829 status = GOT_STATUS_UNVERSIONED;
1830 } else {
1831 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1832 status = GOT_STATUS_UNVERSIONED;
1833 else
1834 status = GOT_STATUS_OBSTRUCTED;
1838 if (status == GOT_STATUS_OBSTRUCTED) {
1839 if (ie)
1840 got_fileindex_entry_mark_skipped(ie);
1841 err = (*progress_cb)(progress_arg, status, path);
1842 goto done;
1844 if (status == GOT_STATUS_CONFLICT) {
1845 if (ie)
1846 got_fileindex_entry_mark_skipped(ie);
1847 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1848 path);
1849 goto done;
1852 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1853 (S_ISLNK(te->mode) ||
1854 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1856 * This is a regular file or an installed bad symlink.
1857 * If the file index indicates that this file is already
1858 * up-to-date with respect to the repository we can skip
1859 * updating contents of this file.
1861 if (got_fileindex_entry_has_commit(ie) &&
1862 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1863 SHA1_DIGEST_LENGTH) == 0) {
1864 /* Same commit. */
1865 err = sync_timestamps(worktree->root_fd,
1866 path, status, ie, &sb);
1867 if (err)
1868 goto done;
1869 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1870 path);
1871 goto done;
1873 if (got_fileindex_entry_has_blob(ie) &&
1874 memcmp(ie->blob_sha1, te->id.sha1,
1875 SHA1_DIGEST_LENGTH) == 0) {
1876 /* Different commit but the same blob. */
1877 err = sync_timestamps(worktree->root_fd,
1878 path, status, ie, &sb);
1879 if (err)
1880 goto done;
1881 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1882 path);
1883 goto done;
1887 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1888 if (err)
1889 goto done;
1891 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1892 int update_timestamps;
1893 struct got_blob_object *blob2 = NULL;
1894 char *label_orig = NULL;
1895 if (got_fileindex_entry_has_blob(ie)) {
1896 struct got_object_id id2;
1897 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1898 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1899 if (err)
1900 goto done;
1902 if (got_fileindex_entry_has_commit(ie)) {
1903 char id_str[SHA1_DIGEST_STRING_LENGTH];
1904 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1905 sizeof(id_str)) == NULL) {
1906 err = got_error_path(id_str,
1907 GOT_ERR_BAD_OBJ_ID_STR);
1908 goto done;
1910 if (asprintf(&label_orig, "%s: commit %s",
1911 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1912 err = got_error_from_errno("asprintf");
1913 goto done;
1916 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1917 char *link_target;
1918 err = got_object_blob_read_to_str(&link_target, blob);
1919 if (err)
1920 goto done;
1921 err = merge_symlink(worktree, blob2, ondisk_path, path,
1922 label_orig, link_target, worktree->base_commit_id,
1923 repo, progress_cb, progress_arg);
1924 free(link_target);
1925 } else {
1926 err = merge_blob(&update_timestamps, worktree, blob2,
1927 ondisk_path, path, sb.st_mode, label_orig, blob,
1928 worktree->base_commit_id, repo,
1929 progress_cb, progress_arg);
1931 free(label_orig);
1932 if (blob2)
1933 got_object_blob_close(blob2);
1934 if (err)
1935 goto done;
1937 * Do not update timestamps of files with local changes.
1938 * Otherwise, a future status walk would treat them as
1939 * unmodified files again.
1941 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1942 blob->id.sha1, worktree->base_commit_id->sha1,
1943 update_timestamps);
1944 } else if (status == GOT_STATUS_MODE_CHANGE) {
1945 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1946 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1947 } else if (status == GOT_STATUS_DELETE) {
1948 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1949 if (err)
1950 goto done;
1951 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1952 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1953 if (err)
1954 goto done;
1955 } else {
1956 int is_bad_symlink = 0;
1957 if (S_ISLNK(te->mode)) {
1958 err = install_symlink(&is_bad_symlink, worktree,
1959 ondisk_path, path, blob,
1960 status == GOT_STATUS_MISSING, 0,
1961 status == GOT_STATUS_UNVERSIONED, 0,
1962 repo, progress_cb, progress_arg);
1963 } else {
1964 err = install_blob(worktree, ondisk_path, path,
1965 te->mode, sb.st_mode, blob,
1966 status == GOT_STATUS_MISSING, 0, 0,
1967 status == GOT_STATUS_UNVERSIONED, repo,
1968 progress_cb, progress_arg);
1970 if (err)
1971 goto done;
1973 if (ie) {
1974 err = got_fileindex_entry_update(ie,
1975 worktree->root_fd, path, blob->id.sha1,
1976 worktree->base_commit_id->sha1, 1);
1977 } else {
1978 err = create_fileindex_entry(&ie, fileindex,
1979 worktree->base_commit_id, worktree->root_fd, path,
1980 &blob->id);
1982 if (err)
1983 goto done;
1985 if (is_bad_symlink) {
1986 got_fileindex_entry_filetype_set(ie,
1987 GOT_FILEIDX_MODE_BAD_SYMLINK);
1990 got_object_blob_close(blob);
1991 done:
1992 free(ondisk_path);
1993 return err;
1996 static const struct got_error *
1997 remove_ondisk_file(const char *root_path, const char *path)
1999 const struct got_error *err = NULL;
2000 char *ondisk_path = NULL, *parent = NULL;
2002 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2003 return got_error_from_errno("asprintf");
2005 if (unlink(ondisk_path) == -1) {
2006 if (errno != ENOENT)
2007 err = got_error_from_errno2("unlink", ondisk_path);
2008 } else {
2009 size_t root_len = strlen(root_path);
2010 err = got_path_dirname(&parent, ondisk_path);
2011 if (err)
2012 goto done;
2013 while (got_path_cmp(parent, root_path,
2014 strlen(parent), root_len) != 0) {
2015 free(ondisk_path);
2016 ondisk_path = parent;
2017 parent = NULL;
2018 if (rmdir(ondisk_path) == -1) {
2019 if (errno != ENOTEMPTY)
2020 err = got_error_from_errno2("rmdir",
2021 ondisk_path);
2022 break;
2024 err = got_path_dirname(&parent, ondisk_path);
2025 if (err)
2026 break;
2029 done:
2030 free(ondisk_path);
2031 free(parent);
2032 return err;
2035 static const struct got_error *
2036 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2037 struct got_fileindex_entry *ie, struct got_repository *repo,
2038 got_worktree_checkout_cb progress_cb, void *progress_arg)
2040 const struct got_error *err = NULL;
2041 unsigned char status;
2042 struct stat sb;
2043 char *ondisk_path;
2045 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2046 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2048 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2049 == -1)
2050 return got_error_from_errno("asprintf");
2052 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2053 if (err)
2054 goto done;
2056 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2057 char ondisk_target[PATH_MAX];
2058 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2059 sizeof(ondisk_target));
2060 if (ondisk_len == -1) {
2061 err = got_error_from_errno2("readlink", ondisk_path);
2062 goto done;
2064 ondisk_target[ondisk_len] = '\0';
2065 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2066 NULL, NULL, /* XXX pass common ancestor info? */
2067 ondisk_target, ondisk_path);
2068 if (err)
2069 goto done;
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2071 ie->path);
2072 goto done;
2075 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2076 status == GOT_STATUS_ADD) {
2077 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2078 if (err)
2079 goto done;
2081 * Preserve the working file and change the deleted blob's
2082 * entry into a schedule-add entry.
2084 err = got_fileindex_entry_update(ie, worktree->root_fd,
2085 ie->path, NULL, NULL, 0);
2086 } else {
2087 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2088 if (err)
2089 goto done;
2090 if (status == GOT_STATUS_NO_CHANGE) {
2091 err = remove_ondisk_file(worktree->root_path, ie->path);
2092 if (err)
2093 goto done;
2095 got_fileindex_entry_remove(fileindex, ie);
2097 done:
2098 free(ondisk_path);
2099 return err;
2102 struct diff_cb_arg {
2103 struct got_fileindex *fileindex;
2104 struct got_worktree *worktree;
2105 struct got_repository *repo;
2106 got_worktree_checkout_cb progress_cb;
2107 void *progress_arg;
2108 got_cancel_cb cancel_cb;
2109 void *cancel_arg;
2112 static const struct got_error *
2113 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2114 struct got_tree_entry *te, const char *parent_path)
2116 struct diff_cb_arg *a = arg;
2118 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2119 return got_error(GOT_ERR_CANCELLED);
2121 return update_blob(a->worktree, a->fileindex, ie, te,
2122 ie->path, a->repo, a->progress_cb, a->progress_arg);
2125 static const struct got_error *
2126 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2128 struct diff_cb_arg *a = arg;
2130 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2131 return got_error(GOT_ERR_CANCELLED);
2133 return delete_blob(a->worktree, a->fileindex, ie,
2134 a->repo, a->progress_cb, a->progress_arg);
2137 static const struct got_error *
2138 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2140 struct diff_cb_arg *a = arg;
2141 const struct got_error *err;
2142 char *path;
2144 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2145 return got_error(GOT_ERR_CANCELLED);
2147 if (got_object_tree_entry_is_submodule(te))
2148 return NULL;
2150 if (asprintf(&path, "%s%s%s", parent_path,
2151 parent_path[0] ? "/" : "", te->name)
2152 == -1)
2153 return got_error_from_errno("asprintf");
2155 if (S_ISDIR(te->mode))
2156 err = add_dir_on_disk(a->worktree, path);
2157 else
2158 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2159 a->repo, a->progress_cb, a->progress_arg);
2161 free(path);
2162 return err;
2165 const struct got_error *
2166 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2168 uint32_t uuid_status;
2170 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2171 if (uuid_status != uuid_s_ok) {
2172 *uuidstr = NULL;
2173 return got_error_uuid(uuid_status, "uuid_to_string");
2176 return NULL;
2179 static const struct got_error *
2180 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2182 const struct got_error *err = NULL;
2183 char *uuidstr = NULL;
2185 *refname = NULL;
2187 err = got_worktree_get_uuid(&uuidstr, worktree);
2188 if (err)
2189 return err;
2191 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2192 err = got_error_from_errno("asprintf");
2193 *refname = NULL;
2195 free(uuidstr);
2196 return err;
2199 const struct got_error *
2200 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2205 static const struct got_error *
2206 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2208 return get_ref_name(refname, worktree,
2209 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2212 static const struct got_error *
2213 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2215 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2218 static const struct got_error *
2219 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2221 return get_ref_name(refname, worktree,
2222 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2225 static const struct got_error *
2226 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2228 return get_ref_name(refname, worktree,
2229 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2232 static const struct got_error *
2233 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2235 return get_ref_name(refname, worktree,
2236 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2239 static const struct got_error *
2240 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2242 return get_ref_name(refname, worktree,
2243 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2246 static const struct got_error *
2247 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2249 return get_ref_name(refname, worktree,
2250 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2253 static const struct got_error *
2254 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2256 return get_ref_name(refname, worktree,
2257 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2260 const struct got_error *
2261 got_worktree_get_histedit_script_path(char **path,
2262 struct got_worktree *worktree)
2264 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2265 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2266 *path = NULL;
2267 return got_error_from_errno("asprintf");
2269 return NULL;
2272 static const struct got_error *
2273 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2279 static const struct got_error *
2280 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2287 * Prevent Git's garbage collector from deleting our base commit by
2288 * setting a reference to our base commit's ID.
2290 static const struct got_error *
2291 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2293 const struct got_error *err = NULL;
2294 struct got_reference *ref = NULL;
2295 char *refname;
2297 err = got_worktree_get_base_ref_name(&refname, worktree);
2298 if (err)
2299 return err;
2301 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2302 if (err)
2303 goto done;
2305 err = got_ref_write(ref, repo);
2306 done:
2307 free(refname);
2308 if (ref)
2309 got_ref_close(ref);
2310 return err;
2313 static const struct got_error *
2314 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2316 const struct got_error *err = NULL;
2318 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2319 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2320 err = got_error_from_errno("asprintf");
2321 *fileindex_path = NULL;
2323 return err;
2327 static const struct got_error *
2328 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2329 struct got_worktree *worktree)
2331 const struct got_error *err = NULL;
2332 FILE *index = NULL;
2334 *fileindex_path = NULL;
2335 *fileindex = got_fileindex_alloc();
2336 if (*fileindex == NULL)
2337 return got_error_from_errno("got_fileindex_alloc");
2339 err = get_fileindex_path(fileindex_path, worktree);
2340 if (err)
2341 goto done;
2343 index = fopen(*fileindex_path, "rb");
2344 if (index == NULL) {
2345 if (errno != ENOENT)
2346 err = got_error_from_errno2("fopen", *fileindex_path);
2347 } else {
2348 err = got_fileindex_read(*fileindex, index);
2349 if (fclose(index) == EOF && err == NULL)
2350 err = got_error_from_errno("fclose");
2352 done:
2353 if (err) {
2354 free(*fileindex_path);
2355 *fileindex_path = NULL;
2356 got_fileindex_free(*fileindex);
2357 *fileindex = NULL;
2359 return err;
2362 struct bump_base_commit_id_arg {
2363 struct got_object_id *base_commit_id;
2364 const char *path;
2365 size_t path_len;
2366 const char *entry_name;
2367 got_worktree_checkout_cb progress_cb;
2368 void *progress_arg;
2371 /* Bump base commit ID of all files within an updated part of the work tree. */
2372 static const struct got_error *
2373 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2375 const struct got_error *err;
2376 struct bump_base_commit_id_arg *a = arg;
2378 if (a->entry_name) {
2379 if (strcmp(ie->path, a->path) != 0)
2380 return NULL;
2381 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2382 return NULL;
2384 if (got_fileindex_entry_was_skipped(ie))
2385 return NULL;
2387 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2388 SHA1_DIGEST_LENGTH) == 0)
2389 return NULL;
2391 if (a->progress_cb) {
2392 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2393 ie->path);
2394 if (err)
2395 return err;
2397 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2398 return NULL;
2401 static const struct got_error *
2402 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2403 struct got_fileindex *fileindex,
2404 got_worktree_checkout_cb progress_cb, void *progress_arg)
2406 struct bump_base_commit_id_arg bbc_arg;
2408 bbc_arg.base_commit_id = worktree->base_commit_id;
2409 bbc_arg.entry_name = NULL;
2410 bbc_arg.path = "";
2411 bbc_arg.path_len = 0;
2412 bbc_arg.progress_cb = progress_cb;
2413 bbc_arg.progress_arg = progress_arg;
2415 return got_fileindex_for_each_entry_safe(fileindex,
2416 bump_base_commit_id, &bbc_arg);
2419 static const struct got_error *
2420 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2422 const struct got_error *err = NULL;
2423 char *new_fileindex_path = NULL;
2424 FILE *new_index = NULL;
2425 struct timespec timeout;
2427 err = got_opentemp_named(&new_fileindex_path, &new_index,
2428 fileindex_path);
2429 if (err)
2430 goto done;
2432 err = got_fileindex_write(fileindex, new_index);
2433 if (err)
2434 goto done;
2436 if (rename(new_fileindex_path, fileindex_path) != 0) {
2437 err = got_error_from_errno3("rename", new_fileindex_path,
2438 fileindex_path);
2439 unlink(new_fileindex_path);
2443 * Sleep for a short amount of time to ensure that files modified after
2444 * this program exits have a different time stamp from the one which
2445 * was recorded in the file index.
2447 timeout.tv_sec = 0;
2448 timeout.tv_nsec = 1;
2449 nanosleep(&timeout, NULL);
2450 done:
2451 if (new_index)
2452 fclose(new_index);
2453 free(new_fileindex_path);
2454 return err;
2457 static const struct got_error *
2458 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2459 struct got_object_id **tree_id, const char *wt_relpath,
2460 struct got_worktree *worktree, struct got_repository *repo)
2462 const struct got_error *err = NULL;
2463 struct got_object_id *id = NULL;
2464 char *in_repo_path = NULL;
2465 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2467 *entry_type = GOT_OBJ_TYPE_ANY;
2468 *tree_relpath = NULL;
2469 *tree_id = NULL;
2471 if (wt_relpath[0] == '\0') {
2472 /* Check out all files within the work tree. */
2473 *entry_type = GOT_OBJ_TYPE_TREE;
2474 *tree_relpath = strdup("");
2475 if (*tree_relpath == NULL) {
2476 err = got_error_from_errno("strdup");
2477 goto done;
2479 err = got_object_id_by_path(tree_id, repo,
2480 worktree->base_commit_id, worktree->path_prefix);
2481 if (err)
2482 goto done;
2483 return NULL;
2486 /* Check out a subset of files in the work tree. */
2488 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2489 is_root_wt ? "" : "/", wt_relpath) == -1) {
2490 err = got_error_from_errno("asprintf");
2491 goto done;
2494 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2495 in_repo_path);
2496 if (err)
2497 goto done;
2499 free(in_repo_path);
2500 in_repo_path = NULL;
2502 err = got_object_get_type(entry_type, repo, id);
2503 if (err)
2504 goto done;
2506 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2507 /* Check out a single file. */
2508 if (strchr(wt_relpath, '/') == NULL) {
2509 /* Check out a single file in work tree's root dir. */
2510 in_repo_path = strdup(worktree->path_prefix);
2511 if (in_repo_path == NULL) {
2512 err = got_error_from_errno("strdup");
2513 goto done;
2515 *tree_relpath = strdup("");
2516 if (*tree_relpath == NULL) {
2517 err = got_error_from_errno("strdup");
2518 goto done;
2520 } else {
2521 /* Check out a single file in a subdirectory. */
2522 err = got_path_dirname(tree_relpath, wt_relpath);
2523 if (err)
2524 return err;
2525 if (asprintf(&in_repo_path, "%s%s%s",
2526 worktree->path_prefix, is_root_wt ? "" : "/",
2527 *tree_relpath) == -1) {
2528 err = got_error_from_errno("asprintf");
2529 goto done;
2532 err = got_object_id_by_path(tree_id, repo,
2533 worktree->base_commit_id, in_repo_path);
2534 } else {
2535 /* Check out all files within a subdirectory. */
2536 *tree_id = got_object_id_dup(id);
2537 if (*tree_id == NULL) {
2538 err = got_error_from_errno("got_object_id_dup");
2539 goto done;
2541 *tree_relpath = strdup(wt_relpath);
2542 if (*tree_relpath == NULL) {
2543 err = got_error_from_errno("strdup");
2544 goto done;
2547 done:
2548 free(id);
2549 free(in_repo_path);
2550 if (err) {
2551 *entry_type = GOT_OBJ_TYPE_ANY;
2552 free(*tree_relpath);
2553 *tree_relpath = NULL;
2554 free(*tree_id);
2555 *tree_id = NULL;
2557 return err;
2560 static const struct got_error *
2561 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2562 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2563 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2564 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2566 const struct got_error *err = NULL;
2567 struct got_commit_object *commit = NULL;
2568 struct got_tree_object *tree = NULL;
2569 struct got_fileindex_diff_tree_cb diff_cb;
2570 struct diff_cb_arg arg;
2572 err = ref_base_commit(worktree, repo);
2573 if (err) {
2574 if (!(err->code == GOT_ERR_ERRNO &&
2575 (errno == EACCES || errno == EROFS)))
2576 goto done;
2577 err = (*progress_cb)(progress_arg,
2578 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2579 if (err)
2580 return err;
2583 err = got_object_open_as_commit(&commit, repo,
2584 worktree->base_commit_id);
2585 if (err)
2586 goto done;
2588 err = got_object_open_as_tree(&tree, repo, tree_id);
2589 if (err)
2590 goto done;
2592 if (entry_name &&
2593 got_object_tree_find_entry(tree, entry_name) == NULL) {
2594 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2595 goto done;
2598 diff_cb.diff_old_new = diff_old_new;
2599 diff_cb.diff_old = diff_old;
2600 diff_cb.diff_new = diff_new;
2601 arg.fileindex = fileindex;
2602 arg.worktree = worktree;
2603 arg.repo = repo;
2604 arg.progress_cb = progress_cb;
2605 arg.progress_arg = progress_arg;
2606 arg.cancel_cb = cancel_cb;
2607 arg.cancel_arg = cancel_arg;
2608 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2609 entry_name, repo, &diff_cb, &arg);
2610 done:
2611 if (tree)
2612 got_object_tree_close(tree);
2613 if (commit)
2614 got_object_commit_close(commit);
2615 return err;
2618 const struct got_error *
2619 got_worktree_checkout_files(struct got_worktree *worktree,
2620 struct got_pathlist_head *paths, struct got_repository *repo,
2621 got_worktree_checkout_cb progress_cb, void *progress_arg,
2622 got_cancel_cb cancel_cb, void *cancel_arg)
2624 const struct got_error *err = NULL, *sync_err, *unlockerr;
2625 struct got_commit_object *commit = NULL;
2626 struct got_tree_object *tree = NULL;
2627 struct got_fileindex *fileindex = NULL;
2628 char *fileindex_path = NULL;
2629 struct got_pathlist_entry *pe;
2630 struct tree_path_data {
2631 STAILQ_ENTRY(tree_path_data) entry;
2632 struct got_object_id *tree_id;
2633 int entry_type;
2634 char *relpath;
2635 char *entry_name;
2636 } *tpd = NULL;
2637 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2639 STAILQ_INIT(&tree_paths);
2641 err = lock_worktree(worktree, LOCK_EX);
2642 if (err)
2643 return err;
2645 /* Map all specified paths to in-repository trees. */
2646 TAILQ_FOREACH(pe, paths, entry) {
2647 tpd = malloc(sizeof(*tpd));
2648 if (tpd == NULL) {
2649 err = got_error_from_errno("malloc");
2650 goto done;
2653 err = find_tree_entry_for_checkout(&tpd->entry_type,
2654 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2655 if (err) {
2656 free(tpd);
2657 goto done;
2660 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2661 err = got_path_basename(&tpd->entry_name, pe->path);
2662 if (err) {
2663 free(tpd->relpath);
2664 free(tpd->tree_id);
2665 free(tpd);
2666 goto done;
2668 } else
2669 tpd->entry_name = NULL;
2671 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2675 * Read the file index.
2676 * Checking out files is supposed to be an idempotent operation.
2677 * If the on-disk file index is incomplete we will try to complete it.
2679 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2680 if (err)
2681 goto done;
2683 tpd = STAILQ_FIRST(&tree_paths);
2684 TAILQ_FOREACH(pe, paths, entry) {
2685 struct bump_base_commit_id_arg bbc_arg;
2687 err = checkout_files(worktree, fileindex, tpd->relpath,
2688 tpd->tree_id, tpd->entry_name, repo,
2689 progress_cb, progress_arg, cancel_cb, cancel_arg);
2690 if (err)
2691 break;
2693 bbc_arg.base_commit_id = worktree->base_commit_id;
2694 bbc_arg.entry_name = tpd->entry_name;
2695 bbc_arg.path = pe->path;
2696 bbc_arg.path_len = pe->path_len;
2697 bbc_arg.progress_cb = progress_cb;
2698 bbc_arg.progress_arg = progress_arg;
2699 err = got_fileindex_for_each_entry_safe(fileindex,
2700 bump_base_commit_id, &bbc_arg);
2701 if (err)
2702 break;
2704 tpd = STAILQ_NEXT(tpd, entry);
2706 sync_err = sync_fileindex(fileindex, fileindex_path);
2707 if (sync_err && err == NULL)
2708 err = sync_err;
2709 done:
2710 free(fileindex_path);
2711 if (tree)
2712 got_object_tree_close(tree);
2713 if (commit)
2714 got_object_commit_close(commit);
2715 if (fileindex)
2716 got_fileindex_free(fileindex);
2717 while (!STAILQ_EMPTY(&tree_paths)) {
2718 tpd = STAILQ_FIRST(&tree_paths);
2719 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2720 free(tpd->relpath);
2721 free(tpd->tree_id);
2722 free(tpd);
2724 unlockerr = lock_worktree(worktree, LOCK_SH);
2725 if (unlockerr && err == NULL)
2726 err = unlockerr;
2727 return err;
2730 struct merge_file_cb_arg {
2731 struct got_worktree *worktree;
2732 struct got_fileindex *fileindex;
2733 got_worktree_checkout_cb progress_cb;
2734 void *progress_arg;
2735 got_cancel_cb cancel_cb;
2736 void *cancel_arg;
2737 const char *label_orig;
2738 struct got_object_id *commit_id2;
2739 int allow_bad_symlinks;
2742 static const struct got_error *
2743 merge_file_cb(void *arg, struct got_blob_object *blob1,
2744 struct got_blob_object *blob2, struct got_object_id *id1,
2745 struct got_object_id *id2, const char *path1, const char *path2,
2746 mode_t mode1, mode_t mode2, struct got_repository *repo)
2748 static const struct got_error *err = NULL;
2749 struct merge_file_cb_arg *a = arg;
2750 struct got_fileindex_entry *ie;
2751 char *ondisk_path = NULL;
2752 struct stat sb;
2753 unsigned char status;
2754 int local_changes_subsumed;
2755 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2756 char *id_str = NULL, *label_deriv2 = NULL;
2758 if (blob1 && blob2) {
2759 ie = got_fileindex_entry_get(a->fileindex, path2,
2760 strlen(path2));
2761 if (ie == NULL)
2762 return (*a->progress_cb)(a->progress_arg,
2763 GOT_STATUS_MISSING, path2);
2765 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2766 path2) == -1)
2767 return got_error_from_errno("asprintf");
2769 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2770 repo);
2771 if (err)
2772 goto done;
2774 if (status == GOT_STATUS_DELETE) {
2775 err = (*a->progress_cb)(a->progress_arg,
2776 GOT_STATUS_MERGE, path2);
2777 goto done;
2779 if (status != GOT_STATUS_NO_CHANGE &&
2780 status != GOT_STATUS_MODIFY &&
2781 status != GOT_STATUS_CONFLICT &&
2782 status != GOT_STATUS_ADD) {
2783 err = (*a->progress_cb)(a->progress_arg, status, path2);
2784 goto done;
2787 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2788 char *link_target2;
2789 err = got_object_blob_read_to_str(&link_target2, blob2);
2790 if (err)
2791 goto done;
2792 err = merge_symlink(a->worktree, blob1, ondisk_path,
2793 path2, a->label_orig, link_target2, a->commit_id2,
2794 repo, a->progress_cb, a->progress_arg);
2795 free(link_target2);
2796 } else {
2797 int fd;
2799 f_orig = got_opentemp();
2800 if (f_orig == NULL) {
2801 err = got_error_from_errno("got_opentemp");
2802 goto done;
2804 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2805 f_orig, blob1);
2806 if (err)
2807 goto done;
2809 f_deriv2 = got_opentemp();
2810 if (f_deriv2 == NULL)
2811 goto done;
2812 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2813 f_deriv2, blob2);
2814 if (err)
2815 goto done;
2817 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2818 if (fd == -1) {
2819 err = got_error_from_errno2("open",
2820 ondisk_path);
2821 goto done;
2823 f_deriv = fdopen(fd, "r");
2824 if (f_deriv == NULL) {
2825 err = got_error_from_errno2("fdopen",
2826 ondisk_path);
2827 close(fd);
2828 goto done;
2830 err = got_object_id_str(&id_str, a->commit_id2);
2831 if (err)
2832 goto done;
2833 if (asprintf(&label_deriv2, "%s: commit %s",
2834 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2835 err = got_error_from_errno("asprintf");
2836 goto done;
2838 err = merge_file(&local_changes_subsumed, a->worktree,
2839 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2840 sb.st_mode, a->label_orig, NULL, label_deriv2,
2841 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2842 a->progress_cb, a->progress_arg);
2844 } else if (blob1) {
2845 ie = got_fileindex_entry_get(a->fileindex, path1,
2846 strlen(path1));
2847 if (ie == NULL)
2848 return (*a->progress_cb)(a->progress_arg,
2849 GOT_STATUS_MISSING, path1);
2851 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2852 path1) == -1)
2853 return got_error_from_errno("asprintf");
2855 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2856 repo);
2857 if (err)
2858 goto done;
2860 switch (status) {
2861 case GOT_STATUS_NO_CHANGE:
2862 err = (*a->progress_cb)(a->progress_arg,
2863 GOT_STATUS_DELETE, path1);
2864 if (err)
2865 goto done;
2866 err = remove_ondisk_file(a->worktree->root_path, path1);
2867 if (err)
2868 goto done;
2869 if (ie)
2870 got_fileindex_entry_mark_deleted_from_disk(ie);
2871 break;
2872 case GOT_STATUS_DELETE:
2873 case GOT_STATUS_MISSING:
2874 err = (*a->progress_cb)(a->progress_arg,
2875 GOT_STATUS_DELETE, path1);
2876 if (err)
2877 goto done;
2878 if (ie)
2879 got_fileindex_entry_mark_deleted_from_disk(ie);
2880 break;
2881 case GOT_STATUS_ADD: {
2882 struct got_object_id *id;
2883 FILE *blob1_f;
2885 * Delete the added file only if its content already
2886 * exists in the repository.
2888 err = got_object_blob_file_create(&id, &blob1_f, path1);
2889 if (err)
2890 goto done;
2891 if (got_object_id_cmp(id, id1) == 0) {
2892 err = (*a->progress_cb)(a->progress_arg,
2893 GOT_STATUS_DELETE, path1);
2894 if (err)
2895 goto done;
2896 err = remove_ondisk_file(a->worktree->root_path,
2897 path1);
2898 if (err)
2899 goto done;
2900 if (ie)
2901 got_fileindex_entry_remove(a->fileindex,
2902 ie);
2903 } else {
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_CANNOT_DELETE, path1);
2907 if (fclose(blob1_f) == EOF && err == NULL)
2908 err = got_error_from_errno("fclose");
2909 free(id);
2910 if (err)
2911 goto done;
2912 break;
2914 case GOT_STATUS_MODIFY:
2915 case GOT_STATUS_CONFLICT:
2916 err = (*a->progress_cb)(a->progress_arg,
2917 GOT_STATUS_CANNOT_DELETE, path1);
2918 if (err)
2919 goto done;
2920 break;
2921 case GOT_STATUS_OBSTRUCTED:
2922 err = (*a->progress_cb)(a->progress_arg, status, path1);
2923 if (err)
2924 goto done;
2925 break;
2926 default:
2927 break;
2929 } else if (blob2) {
2930 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2931 path2) == -1)
2932 return got_error_from_errno("asprintf");
2933 ie = got_fileindex_entry_get(a->fileindex, path2,
2934 strlen(path2));
2935 if (ie) {
2936 err = get_file_status(&status, &sb, ie, ondisk_path,
2937 -1, NULL, repo);
2938 if (err)
2939 goto done;
2940 if (status != GOT_STATUS_NO_CHANGE &&
2941 status != GOT_STATUS_MODIFY &&
2942 status != GOT_STATUS_CONFLICT &&
2943 status != GOT_STATUS_ADD) {
2944 err = (*a->progress_cb)(a->progress_arg,
2945 status, path2);
2946 goto done;
2948 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2949 char *link_target2;
2950 err = got_object_blob_read_to_str(&link_target2,
2951 blob2);
2952 if (err)
2953 goto done;
2954 err = merge_symlink(a->worktree, NULL,
2955 ondisk_path, path2, a->label_orig,
2956 link_target2, a->commit_id2, repo,
2957 a->progress_cb, a->progress_arg);
2958 free(link_target2);
2959 } else if (S_ISREG(sb.st_mode)) {
2960 err = merge_blob(&local_changes_subsumed,
2961 a->worktree, NULL, ondisk_path, path2,
2962 sb.st_mode, a->label_orig, blob2,
2963 a->commit_id2, repo, a->progress_cb,
2964 a->progress_arg);
2965 } else {
2966 err = got_error_path(ondisk_path,
2967 GOT_ERR_FILE_OBSTRUCTED);
2969 if (err)
2970 goto done;
2971 if (status == GOT_STATUS_DELETE) {
2972 err = got_fileindex_entry_update(ie,
2973 a->worktree->root_fd, path2, blob2->id.sha1,
2974 a->worktree->base_commit_id->sha1, 0);
2975 if (err)
2976 goto done;
2978 } else {
2979 int is_bad_symlink = 0;
2980 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2981 if (S_ISLNK(mode2)) {
2982 err = install_symlink(&is_bad_symlink,
2983 a->worktree, ondisk_path, path2, blob2, 0,
2984 0, 1, a->allow_bad_symlinks, repo,
2985 a->progress_cb, a->progress_arg);
2986 } else {
2987 err = install_blob(a->worktree, ondisk_path, path2,
2988 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2989 a->progress_cb, a->progress_arg);
2991 if (err)
2992 goto done;
2993 err = got_fileindex_entry_alloc(&ie, path2);
2994 if (err)
2995 goto done;
2996 err = got_fileindex_entry_update(ie,
2997 a->worktree->root_fd, path2, NULL, NULL, 1);
2998 if (err) {
2999 got_fileindex_entry_free(ie);
3000 goto done;
3002 err = got_fileindex_entry_add(a->fileindex, ie);
3003 if (err) {
3004 got_fileindex_entry_free(ie);
3005 goto done;
3007 if (is_bad_symlink) {
3008 got_fileindex_entry_filetype_set(ie,
3009 GOT_FILEIDX_MODE_BAD_SYMLINK);
3013 done:
3014 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3015 err = got_error_from_errno("fclose");
3016 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3017 err = got_error_from_errno("fclose");
3018 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3019 err = got_error_from_errno("fclose");
3020 free(id_str);
3021 free(label_deriv2);
3022 free(ondisk_path);
3023 return err;
3026 static const struct got_error *
3027 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3029 struct got_worktree *worktree = arg;
3031 /* Reject merges into a work tree with mixed base commits. */
3032 if (got_fileindex_entry_has_commit(ie) &&
3033 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3034 SHA1_DIGEST_LENGTH) != 0)
3035 return got_error(GOT_ERR_MIXED_COMMITS);
3037 return NULL;
3040 struct check_merge_conflicts_arg {
3041 struct got_worktree *worktree;
3042 struct got_fileindex *fileindex;
3043 struct got_repository *repo;
3046 static const struct got_error *
3047 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3048 struct got_blob_object *blob2, struct got_object_id *id1,
3049 struct got_object_id *id2, const char *path1, const char *path2,
3050 mode_t mode1, mode_t mode2, struct got_repository *repo)
3052 const struct got_error *err = NULL;
3053 struct check_merge_conflicts_arg *a = arg;
3054 unsigned char status;
3055 struct stat sb;
3056 struct got_fileindex_entry *ie;
3057 const char *path = path2 ? path2 : path1;
3058 struct got_object_id *id = id2 ? id2 : id1;
3059 char *ondisk_path;
3061 if (id == NULL)
3062 return NULL;
3064 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3065 if (ie == NULL)
3066 return NULL;
3068 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3069 == -1)
3070 return got_error_from_errno("asprintf");
3072 /* Reject merges into a work tree with conflicted files. */
3073 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3074 free(ondisk_path);
3075 if (err)
3076 return err;
3077 if (status == GOT_STATUS_CONFLICT)
3078 return got_error(GOT_ERR_CONFLICTS);
3080 return NULL;
3083 static const struct got_error *
3084 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3085 const char *fileindex_path, struct got_object_id *commit_id1,
3086 struct got_object_id *commit_id2, struct got_repository *repo,
3087 got_worktree_checkout_cb progress_cb, void *progress_arg,
3088 got_cancel_cb cancel_cb, void *cancel_arg)
3090 const struct got_error *err = NULL, *sync_err;
3091 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3092 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3093 struct check_merge_conflicts_arg cmc_arg;
3094 struct merge_file_cb_arg arg;
3095 char *label_orig = NULL;
3097 if (commit_id1) {
3098 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3099 worktree->path_prefix);
3100 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3101 goto done;
3103 if (tree_id1) {
3104 char *id_str;
3106 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3107 if (err)
3108 goto done;
3110 err = got_object_id_str(&id_str, commit_id1);
3111 if (err)
3112 goto done;
3114 if (asprintf(&label_orig, "%s: commit %s",
3115 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3116 err = got_error_from_errno("asprintf");
3117 free(id_str);
3118 goto done;
3120 free(id_str);
3123 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3124 worktree->path_prefix);
3125 if (err)
3126 goto done;
3128 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3129 if (err)
3130 goto done;
3132 cmc_arg.worktree = worktree;
3133 cmc_arg.fileindex = fileindex;
3134 cmc_arg.repo = repo;
3135 err = got_diff_tree(tree1, tree2, "", "", repo,
3136 check_merge_conflicts, &cmc_arg, 0);
3137 if (err)
3138 goto done;
3140 arg.worktree = worktree;
3141 arg.fileindex = fileindex;
3142 arg.progress_cb = progress_cb;
3143 arg.progress_arg = progress_arg;
3144 arg.cancel_cb = cancel_cb;
3145 arg.cancel_arg = cancel_arg;
3146 arg.label_orig = label_orig;
3147 arg.commit_id2 = commit_id2;
3148 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3149 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3150 sync_err = sync_fileindex(fileindex, fileindex_path);
3151 if (sync_err && err == NULL)
3152 err = sync_err;
3153 done:
3154 if (tree1)
3155 got_object_tree_close(tree1);
3156 if (tree2)
3157 got_object_tree_close(tree2);
3158 free(label_orig);
3159 return err;
3162 const struct got_error *
3163 got_worktree_merge_files(struct got_worktree *worktree,
3164 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3165 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3166 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3168 const struct got_error *err, *unlockerr;
3169 char *fileindex_path = NULL;
3170 struct got_fileindex *fileindex = NULL;
3172 err = lock_worktree(worktree, LOCK_EX);
3173 if (err)
3174 return err;
3176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3177 if (err)
3178 goto done;
3180 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3181 worktree);
3182 if (err)
3183 goto done;
3185 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3186 commit_id2, repo, progress_cb, progress_arg,
3187 cancel_cb, cancel_arg);
3188 done:
3189 if (fileindex)
3190 got_fileindex_free(fileindex);
3191 free(fileindex_path);
3192 unlockerr = lock_worktree(worktree, LOCK_SH);
3193 if (unlockerr && err == NULL)
3194 err = unlockerr;
3195 return err;
3198 struct diff_dir_cb_arg {
3199 struct got_fileindex *fileindex;
3200 struct got_worktree *worktree;
3201 const char *status_path;
3202 size_t status_path_len;
3203 struct got_repository *repo;
3204 got_worktree_status_cb status_cb;
3205 void *status_arg;
3206 got_cancel_cb cancel_cb;
3207 void *cancel_arg;
3208 /* A pathlist containing per-directory pathlists of ignore patterns. */
3209 struct got_pathlist_head *ignores;
3210 int report_unchanged;
3211 int no_ignores;
3214 static const struct got_error *
3215 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3216 int dirfd, const char *de_name,
3217 got_worktree_status_cb status_cb, void *status_arg,
3218 struct got_repository *repo, int report_unchanged)
3220 const struct got_error *err = NULL;
3221 unsigned char status = GOT_STATUS_NO_CHANGE;
3222 unsigned char staged_status = get_staged_status(ie);
3223 struct stat sb;
3224 struct got_object_id blob_id, commit_id, staged_blob_id;
3225 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3226 struct got_object_id *staged_blob_idp = NULL;
3228 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3229 if (err)
3230 return err;
3232 if (status == GOT_STATUS_NO_CHANGE &&
3233 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3234 return NULL;
3236 if (got_fileindex_entry_has_blob(ie)) {
3237 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3238 blob_idp = &blob_id;
3240 if (got_fileindex_entry_has_commit(ie)) {
3241 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3242 commit_idp = &commit_id;
3244 if (staged_status == GOT_STATUS_ADD ||
3245 staged_status == GOT_STATUS_MODIFY) {
3246 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3247 SHA1_DIGEST_LENGTH);
3248 staged_blob_idp = &staged_blob_id;
3251 return (*status_cb)(status_arg, status, staged_status,
3252 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3255 static const struct got_error *
3256 status_old_new(void *arg, struct got_fileindex_entry *ie,
3257 struct dirent *de, const char *parent_path, int dirfd)
3259 const struct got_error *err = NULL;
3260 struct diff_dir_cb_arg *a = arg;
3261 char *abspath;
3263 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3264 return got_error(GOT_ERR_CANCELLED);
3266 if (got_path_cmp(parent_path, a->status_path,
3267 strlen(parent_path), a->status_path_len) != 0 &&
3268 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3269 return NULL;
3271 if (parent_path[0]) {
3272 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3273 parent_path, de->d_name) == -1)
3274 return got_error_from_errno("asprintf");
3275 } else {
3276 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3277 de->d_name) == -1)
3278 return got_error_from_errno("asprintf");
3281 err = report_file_status(ie, abspath, dirfd, de->d_name,
3282 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3283 free(abspath);
3284 return err;
3287 static const struct got_error *
3288 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3290 struct diff_dir_cb_arg *a = arg;
3291 struct got_object_id blob_id, commit_id;
3292 unsigned char status;
3294 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3295 return got_error(GOT_ERR_CANCELLED);
3297 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3298 return NULL;
3300 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3301 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3302 if (got_fileindex_entry_has_file_on_disk(ie))
3303 status = GOT_STATUS_MISSING;
3304 else
3305 status = GOT_STATUS_DELETE;
3306 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3307 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3310 void
3311 free_ignorelist(struct got_pathlist_head *ignorelist)
3313 struct got_pathlist_entry *pe;
3315 TAILQ_FOREACH(pe, ignorelist, entry)
3316 free((char *)pe->path);
3317 got_pathlist_free(ignorelist);
3320 void
3321 free_ignores(struct got_pathlist_head *ignores)
3323 struct got_pathlist_entry *pe;
3325 TAILQ_FOREACH(pe, ignores, entry) {
3326 struct got_pathlist_head *ignorelist = pe->data;
3327 free_ignorelist(ignorelist);
3328 free((char *)pe->path);
3330 got_pathlist_free(ignores);
3333 static const struct got_error *
3334 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3336 const struct got_error *err = NULL;
3337 struct got_pathlist_entry *pe = NULL;
3338 struct got_pathlist_head *ignorelist;
3339 char *line = NULL, *pattern, *dirpath = NULL;
3340 size_t linesize = 0;
3341 ssize_t linelen;
3343 ignorelist = calloc(1, sizeof(*ignorelist));
3344 if (ignorelist == NULL)
3345 return got_error_from_errno("calloc");
3346 TAILQ_INIT(ignorelist);
3348 while ((linelen = getline(&line, &linesize, f)) != -1) {
3349 if (linelen > 0 && line[linelen - 1] == '\n')
3350 line[linelen - 1] = '\0';
3352 /* Git's ignores may contain comments. */
3353 if (line[0] == '#')
3354 continue;
3356 /* Git's negated patterns are not (yet?) supported. */
3357 if (line[0] == '!')
3358 continue;
3360 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3361 line) == -1) {
3362 err = got_error_from_errno("asprintf");
3363 goto done;
3365 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3366 if (err)
3367 goto done;
3369 if (ferror(f)) {
3370 err = got_error_from_errno("getline");
3371 goto done;
3374 dirpath = strdup(path);
3375 if (dirpath == NULL) {
3376 err = got_error_from_errno("strdup");
3377 goto done;
3379 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3380 done:
3381 free(line);
3382 if (err || pe == NULL) {
3383 free(dirpath);
3384 free_ignorelist(ignorelist);
3386 return err;
3389 int
3390 match_ignores(struct got_pathlist_head *ignores, const char *path)
3392 struct got_pathlist_entry *pe;
3394 /* Handle patterns which match in all directories. */
3395 TAILQ_FOREACH(pe, ignores, entry) {
3396 struct got_pathlist_head *ignorelist = pe->data;
3397 struct got_pathlist_entry *pi;
3399 TAILQ_FOREACH(pi, ignorelist, entry) {
3400 const char *p, *pattern = pi->path;
3402 if (strncmp(pattern, "**/", 3) != 0)
3403 continue;
3404 pattern += 3;
3405 p = path;
3406 while (*p) {
3407 if (fnmatch(pattern, p,
3408 FNM_PATHNAME | FNM_LEADING_DIR)) {
3409 /* Retry in next directory. */
3410 while (*p && *p != '/')
3411 p++;
3412 while (*p == '/')
3413 p++;
3414 continue;
3416 return 1;
3422 * The ignores pathlist contains ignore lists from children before
3423 * parents, so we can find the most specific ignorelist by walking
3424 * ignores backwards.
3426 pe = TAILQ_LAST(ignores, got_pathlist_head);
3427 while (pe) {
3428 if (got_path_is_child(path, pe->path, pe->path_len)) {
3429 struct got_pathlist_head *ignorelist = pe->data;
3430 struct got_pathlist_entry *pi;
3431 TAILQ_FOREACH(pi, ignorelist, entry) {
3432 const char *pattern = pi->path;
3433 int flags = FNM_LEADING_DIR;
3434 if (strstr(pattern, "/**/") == NULL)
3435 flags |= FNM_PATHNAME;
3436 if (fnmatch(pattern, path, flags))
3437 continue;
3438 return 1;
3441 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3444 return 0;
3447 static const struct got_error *
3448 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3449 const char *path, int dirfd, const char *ignores_filename)
3451 const struct got_error *err = NULL;
3452 char *ignorespath;
3453 int fd = -1;
3454 FILE *ignoresfile = NULL;
3456 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3457 path[0] ? "/" : "", ignores_filename) == -1)
3458 return got_error_from_errno("asprintf");
3460 if (dirfd != -1) {
3461 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3462 if (fd == -1) {
3463 if (errno != ENOENT && errno != EACCES)
3464 err = got_error_from_errno2("openat",
3465 ignorespath);
3466 } else {
3467 ignoresfile = fdopen(fd, "r");
3468 if (ignoresfile == NULL)
3469 err = got_error_from_errno2("fdopen",
3470 ignorespath);
3471 else {
3472 fd = -1;
3473 err = read_ignores(ignores, path, ignoresfile);
3476 } else {
3477 ignoresfile = fopen(ignorespath, "r");
3478 if (ignoresfile == NULL) {
3479 if (errno != ENOENT && errno != EACCES)
3480 err = got_error_from_errno2("fopen",
3481 ignorespath);
3482 } else
3483 err = read_ignores(ignores, path, ignoresfile);
3486 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3487 err = got_error_from_errno2("fclose", path);
3488 if (fd != -1 && close(fd) == -1 && err == NULL)
3489 err = got_error_from_errno2("close", path);
3490 free(ignorespath);
3491 return err;
3494 static const struct got_error *
3495 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3496 int dirfd)
3498 const struct got_error *err = NULL;
3499 struct diff_dir_cb_arg *a = arg;
3500 char *path = NULL;
3502 if (ignore != NULL)
3503 *ignore = 0;
3505 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3506 return got_error(GOT_ERR_CANCELLED);
3508 if (parent_path[0]) {
3509 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3510 return got_error_from_errno("asprintf");
3511 } else {
3512 path = de->d_name;
3515 if (de->d_type == DT_DIR) {
3516 if (!a->no_ignores && ignore != NULL &&
3517 match_ignores(a->ignores, path))
3518 *ignore = 1;
3519 } else if (!match_ignores(a->ignores, path) &&
3520 got_path_is_child(path, a->status_path, a->status_path_len))
3521 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3522 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3523 if (parent_path[0])
3524 free(path);
3525 return err;
3528 static const struct got_error *
3529 status_traverse(void *arg, const char *path, int dirfd)
3531 const struct got_error *err = NULL;
3532 struct diff_dir_cb_arg *a = arg;
3534 if (a->no_ignores)
3535 return NULL;
3537 err = add_ignores(a->ignores, a->worktree->root_path,
3538 path, dirfd, ".cvsignore");
3539 if (err)
3540 return err;
3542 err = add_ignores(a->ignores, a->worktree->root_path, path,
3543 dirfd, ".gitignore");
3545 return err;
3548 static const struct got_error *
3549 report_single_file_status(const char *path, const char *ondisk_path,
3550 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3551 void *status_arg, struct got_repository *repo, int report_unchanged,
3552 struct got_pathlist_head *ignores, int no_ignores)
3554 struct got_fileindex_entry *ie;
3555 struct stat sb;
3557 if (!no_ignores && match_ignores(ignores, path))
3558 return NULL;
3560 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3561 if (ie)
3562 return report_file_status(ie, ondisk_path, -1, NULL,
3563 status_cb, status_arg, repo, report_unchanged);
3565 if (lstat(ondisk_path, &sb) == -1) {
3566 if (errno != ENOENT)
3567 return got_error_from_errno2("lstat", ondisk_path);
3568 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3569 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3570 return NULL;
3573 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3574 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3575 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3577 return NULL;
3580 static const struct got_error *
3581 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3582 const char *root_path, const char *path)
3584 const struct got_error *err;
3585 char *parent_path, *next_parent_path = NULL;
3587 err = add_ignores(ignores, root_path, "", -1,
3588 ".cvsignore");
3589 if (err)
3590 return err;
3592 err = add_ignores(ignores, root_path, "", -1,
3593 ".gitignore");
3594 if (err)
3595 return err;
3597 err = got_path_dirname(&parent_path, path);
3598 if (err) {
3599 if (err->code == GOT_ERR_BAD_PATH)
3600 return NULL; /* cannot traverse parent */
3601 return err;
3603 for (;;) {
3604 err = add_ignores(ignores, root_path, parent_path, -1,
3605 ".cvsignore");
3606 if (err)
3607 break;
3608 err = add_ignores(ignores, root_path, parent_path, -1,
3609 ".gitignore");
3610 if (err)
3611 break;
3612 err = got_path_dirname(&next_parent_path, parent_path);
3613 if (err) {
3614 if (err->code == GOT_ERR_BAD_PATH)
3615 err = NULL; /* traversed everything */
3616 break;
3618 if (got_path_is_root_dir(parent_path))
3619 break;
3620 free(parent_path);
3621 parent_path = next_parent_path;
3622 next_parent_path = NULL;
3625 free(parent_path);
3626 free(next_parent_path);
3627 return err;
3630 static const struct got_error *
3631 worktree_status(struct got_worktree *worktree, const char *path,
3632 struct got_fileindex *fileindex, struct got_repository *repo,
3633 got_worktree_status_cb status_cb, void *status_arg,
3634 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3635 int report_unchanged)
3637 const struct got_error *err = NULL;
3638 int fd = -1;
3639 struct got_fileindex_diff_dir_cb fdiff_cb;
3640 struct diff_dir_cb_arg arg;
3641 char *ondisk_path = NULL;
3642 struct got_pathlist_head ignores;
3644 TAILQ_INIT(&ignores);
3646 if (asprintf(&ondisk_path, "%s%s%s",
3647 worktree->root_path, path[0] ? "/" : "", path) == -1)
3648 return got_error_from_errno("asprintf");
3650 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3651 if (fd == -1) {
3652 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3653 !got_err_open_nofollow_on_symlink())
3654 err = got_error_from_errno2("open", ondisk_path);
3655 else {
3656 if (!no_ignores) {
3657 err = add_ignores_from_parent_paths(&ignores,
3658 worktree->root_path, ondisk_path);
3659 if (err)
3660 goto done;
3662 err = report_single_file_status(path, ondisk_path,
3663 fileindex, status_cb, status_arg, repo,
3664 report_unchanged, &ignores, no_ignores);
3666 } else {
3667 fdiff_cb.diff_old_new = status_old_new;
3668 fdiff_cb.diff_old = status_old;
3669 fdiff_cb.diff_new = status_new;
3670 fdiff_cb.diff_traverse = status_traverse;
3671 arg.fileindex = fileindex;
3672 arg.worktree = worktree;
3673 arg.status_path = path;
3674 arg.status_path_len = strlen(path);
3675 arg.repo = repo;
3676 arg.status_cb = status_cb;
3677 arg.status_arg = status_arg;
3678 arg.cancel_cb = cancel_cb;
3679 arg.cancel_arg = cancel_arg;
3680 arg.report_unchanged = report_unchanged;
3681 arg.no_ignores = no_ignores;
3682 if (!no_ignores) {
3683 err = add_ignores_from_parent_paths(&ignores,
3684 worktree->root_path, path);
3685 if (err)
3686 goto done;
3688 arg.ignores = &ignores;
3689 err = got_fileindex_diff_dir(fileindex, fd,
3690 worktree->root_path, path, repo, &fdiff_cb, &arg);
3692 done:
3693 free_ignores(&ignores);
3694 if (fd != -1 && close(fd) == -1 && err == NULL)
3695 err = got_error_from_errno("close");
3696 free(ondisk_path);
3697 return err;
3700 const struct got_error *
3701 got_worktree_status(struct got_worktree *worktree,
3702 struct got_pathlist_head *paths, struct got_repository *repo,
3703 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3704 got_cancel_cb cancel_cb, void *cancel_arg)
3706 const struct got_error *err = NULL;
3707 char *fileindex_path = NULL;
3708 struct got_fileindex *fileindex = NULL;
3709 struct got_pathlist_entry *pe;
3711 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3712 if (err)
3713 return err;
3715 TAILQ_FOREACH(pe, paths, entry) {
3716 err = worktree_status(worktree, pe->path, fileindex, repo,
3717 status_cb, status_arg, cancel_cb, cancel_arg,
3718 no_ignores, 0);
3719 if (err)
3720 break;
3722 free(fileindex_path);
3723 got_fileindex_free(fileindex);
3724 return err;
3727 const struct got_error *
3728 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3729 const char *arg)
3731 const struct got_error *err = NULL;
3732 char *resolved = NULL, *cwd = NULL, *path = NULL;
3733 size_t len;
3734 struct stat sb;
3735 char *abspath = NULL;
3736 char canonpath[PATH_MAX];
3738 *wt_path = NULL;
3740 cwd = getcwd(NULL, 0);
3741 if (cwd == NULL)
3742 return got_error_from_errno("getcwd");
3744 if (lstat(arg, &sb) == -1) {
3745 if (errno != ENOENT) {
3746 err = got_error_from_errno2("lstat", arg);
3747 goto done;
3749 sb.st_mode = 0;
3751 if (S_ISLNK(sb.st_mode)) {
3753 * We cannot use realpath(3) with symlinks since we want to
3754 * operate on the symlink itself.
3755 * But we can make the path absolute, assuming it is relative
3756 * to the current working directory, and then canonicalize it.
3758 if (!got_path_is_absolute(arg)) {
3759 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3760 err = got_error_from_errno("asprintf");
3761 goto done;
3765 err = got_canonpath(abspath ? abspath : arg, canonpath,
3766 sizeof(canonpath));
3767 if (err)
3768 goto done;
3769 resolved = strdup(canonpath);
3770 if (resolved == NULL) {
3771 err = got_error_from_errno("strdup");
3772 goto done;
3774 } else {
3775 resolved = realpath(arg, NULL);
3776 if (resolved == NULL) {
3777 if (errno != ENOENT) {
3778 err = got_error_from_errno2("realpath", arg);
3779 goto done;
3781 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3782 err = got_error_from_errno("asprintf");
3783 goto done;
3785 err = got_canonpath(abspath, canonpath,
3786 sizeof(canonpath));
3787 if (err)
3788 goto done;
3789 resolved = strdup(canonpath);
3790 if (resolved == NULL) {
3791 err = got_error_from_errno("strdup");
3792 goto done;
3797 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3798 strlen(got_worktree_get_root_path(worktree)))) {
3799 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3800 goto done;
3803 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3804 err = got_path_skip_common_ancestor(&path,
3805 got_worktree_get_root_path(worktree), resolved);
3806 if (err)
3807 goto done;
3808 } else {
3809 path = strdup("");
3810 if (path == NULL) {
3811 err = got_error_from_errno("strdup");
3812 goto done;
3816 /* XXX status walk can't deal with trailing slash! */
3817 len = strlen(path);
3818 while (len > 0 && path[len - 1] == '/') {
3819 path[len - 1] = '\0';
3820 len--;
3822 done:
3823 free(abspath);
3824 free(resolved);
3825 free(cwd);
3826 if (err == NULL)
3827 *wt_path = path;
3828 else
3829 free(path);
3830 return err;
3833 struct schedule_addition_args {
3834 struct got_worktree *worktree;
3835 struct got_fileindex *fileindex;
3836 got_worktree_checkout_cb progress_cb;
3837 void *progress_arg;
3838 struct got_repository *repo;
3841 static const struct got_error *
3842 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3843 const char *relpath, struct got_object_id *blob_id,
3844 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3845 int dirfd, const char *de_name)
3847 struct schedule_addition_args *a = arg;
3848 const struct got_error *err = NULL;
3849 struct got_fileindex_entry *ie;
3850 struct stat sb;
3851 char *ondisk_path;
3853 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3854 relpath) == -1)
3855 return got_error_from_errno("asprintf");
3857 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3858 if (ie) {
3859 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3860 de_name, a->repo);
3861 if (err)
3862 goto done;
3863 /* Re-adding an existing entry is a no-op. */
3864 if (status == GOT_STATUS_ADD)
3865 goto done;
3866 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3867 if (err)
3868 goto done;
3871 if (status != GOT_STATUS_UNVERSIONED) {
3872 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3873 goto done;
3876 err = got_fileindex_entry_alloc(&ie, relpath);
3877 if (err)
3878 goto done;
3879 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3880 relpath, NULL, NULL, 1);
3881 if (err) {
3882 got_fileindex_entry_free(ie);
3883 goto done;
3885 err = got_fileindex_entry_add(a->fileindex, ie);
3886 if (err) {
3887 got_fileindex_entry_free(ie);
3888 goto done;
3890 done:
3891 free(ondisk_path);
3892 if (err)
3893 return err;
3894 if (status == GOT_STATUS_ADD)
3895 return NULL;
3896 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3899 const struct got_error *
3900 got_worktree_schedule_add(struct got_worktree *worktree,
3901 struct got_pathlist_head *paths,
3902 got_worktree_checkout_cb progress_cb, void *progress_arg,
3903 struct got_repository *repo, int no_ignores)
3905 struct got_fileindex *fileindex = NULL;
3906 char *fileindex_path = NULL;
3907 const struct got_error *err = NULL, *sync_err, *unlockerr;
3908 struct got_pathlist_entry *pe;
3909 struct schedule_addition_args saa;
3911 err = lock_worktree(worktree, LOCK_EX);
3912 if (err)
3913 return err;
3915 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3916 if (err)
3917 goto done;
3919 saa.worktree = worktree;
3920 saa.fileindex = fileindex;
3921 saa.progress_cb = progress_cb;
3922 saa.progress_arg = progress_arg;
3923 saa.repo = repo;
3925 TAILQ_FOREACH(pe, paths, entry) {
3926 err = worktree_status(worktree, pe->path, fileindex, repo,
3927 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3928 if (err)
3929 break;
3931 sync_err = sync_fileindex(fileindex, fileindex_path);
3932 if (sync_err && err == NULL)
3933 err = sync_err;
3934 done:
3935 free(fileindex_path);
3936 if (fileindex)
3937 got_fileindex_free(fileindex);
3938 unlockerr = lock_worktree(worktree, LOCK_SH);
3939 if (unlockerr && err == NULL)
3940 err = unlockerr;
3941 return err;
3944 struct schedule_deletion_args {
3945 struct got_worktree *worktree;
3946 struct got_fileindex *fileindex;
3947 got_worktree_delete_cb progress_cb;
3948 void *progress_arg;
3949 struct got_repository *repo;
3950 int delete_local_mods;
3951 int keep_on_disk;
3952 const char *status_codes;
3955 static const struct got_error *
3956 schedule_for_deletion(void *arg, unsigned char status,
3957 unsigned char staged_status, const char *relpath,
3958 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3959 struct got_object_id *commit_id, int dirfd, const char *de_name)
3961 struct schedule_deletion_args *a = arg;
3962 const struct got_error *err = NULL;
3963 struct got_fileindex_entry *ie = NULL;
3964 struct stat sb;
3965 char *ondisk_path;
3967 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3968 if (ie == NULL)
3969 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3971 staged_status = get_staged_status(ie);
3972 if (staged_status != GOT_STATUS_NO_CHANGE) {
3973 if (staged_status == GOT_STATUS_DELETE)
3974 return NULL;
3975 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3979 relpath) == -1)
3980 return got_error_from_errno("asprintf");
3982 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3983 a->repo);
3984 if (err)
3985 goto done;
3987 if (a->status_codes) {
3988 size_t ncodes = strlen(a->status_codes);
3989 int i;
3990 for (i = 0; i < ncodes ; i++) {
3991 if (status == a->status_codes[i])
3992 break;
3994 if (i == ncodes) {
3995 /* Do not delete files in non-matching status. */
3996 free(ondisk_path);
3997 return NULL;
3999 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4000 a->status_codes[i] != GOT_STATUS_MISSING) {
4001 static char msg[64];
4002 snprintf(msg, sizeof(msg),
4003 "invalid status code '%c'", a->status_codes[i]);
4004 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4005 goto done;
4009 if (status != GOT_STATUS_NO_CHANGE) {
4010 if (status == GOT_STATUS_DELETE)
4011 goto done;
4012 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4013 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4014 goto done;
4016 if (status != GOT_STATUS_MODIFY &&
4017 status != GOT_STATUS_MISSING) {
4018 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4019 goto done;
4023 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4024 size_t root_len;
4026 if (dirfd != -1) {
4027 if (unlinkat(dirfd, de_name, 0) != 0) {
4028 err = got_error_from_errno2("unlinkat",
4029 ondisk_path);
4030 goto done;
4032 } else if (unlink(ondisk_path) != 0) {
4033 err = got_error_from_errno2("unlink", ondisk_path);
4034 goto done;
4037 root_len = strlen(a->worktree->root_path);
4038 do {
4039 char *parent;
4040 err = got_path_dirname(&parent, ondisk_path);
4041 if (err)
4042 goto done;
4043 free(ondisk_path);
4044 ondisk_path = parent;
4045 if (rmdir(ondisk_path) == -1) {
4046 if (errno != ENOTEMPTY)
4047 err = got_error_from_errno2("rmdir",
4048 ondisk_path);
4049 break;
4051 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4052 strlen(ondisk_path), root_len) != 0);
4055 got_fileindex_entry_mark_deleted_from_disk(ie);
4056 done:
4057 free(ondisk_path);
4058 if (err)
4059 return err;
4060 if (status == GOT_STATUS_DELETE)
4061 return NULL;
4062 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4063 staged_status, relpath);
4066 const struct got_error *
4067 got_worktree_schedule_delete(struct got_worktree *worktree,
4068 struct got_pathlist_head *paths, int delete_local_mods,
4069 const char *status_codes,
4070 got_worktree_delete_cb progress_cb, void *progress_arg,
4071 struct got_repository *repo, int keep_on_disk)
4073 struct got_fileindex *fileindex = NULL;
4074 char *fileindex_path = NULL;
4075 const struct got_error *err = NULL, *sync_err, *unlockerr;
4076 struct got_pathlist_entry *pe;
4077 struct schedule_deletion_args sda;
4079 err = lock_worktree(worktree, LOCK_EX);
4080 if (err)
4081 return err;
4083 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4084 if (err)
4085 goto done;
4087 sda.worktree = worktree;
4088 sda.fileindex = fileindex;
4089 sda.progress_cb = progress_cb;
4090 sda.progress_arg = progress_arg;
4091 sda.repo = repo;
4092 sda.delete_local_mods = delete_local_mods;
4093 sda.keep_on_disk = keep_on_disk;
4094 sda.status_codes = status_codes;
4096 TAILQ_FOREACH(pe, paths, entry) {
4097 err = worktree_status(worktree, pe->path, fileindex, repo,
4098 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4099 if (err)
4100 break;
4102 sync_err = sync_fileindex(fileindex, fileindex_path);
4103 if (sync_err && err == NULL)
4104 err = sync_err;
4105 done:
4106 free(fileindex_path);
4107 if (fileindex)
4108 got_fileindex_free(fileindex);
4109 unlockerr = lock_worktree(worktree, LOCK_SH);
4110 if (unlockerr && err == NULL)
4111 err = unlockerr;
4112 return err;
4115 static const struct got_error *
4116 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4118 const struct got_error *err = NULL;
4119 char *line = NULL;
4120 size_t linesize = 0, n;
4121 ssize_t linelen;
4123 linelen = getline(&line, &linesize, infile);
4124 if (linelen == -1) {
4125 if (ferror(infile)) {
4126 err = got_error_from_errno("getline");
4127 goto done;
4129 return NULL;
4131 if (outfile) {
4132 n = fwrite(line, 1, linelen, outfile);
4133 if (n != linelen) {
4134 err = got_ferror(outfile, GOT_ERR_IO);
4135 goto done;
4138 if (rejectfile) {
4139 n = fwrite(line, 1, linelen, rejectfile);
4140 if (n != linelen)
4141 err = got_ferror(outfile, GOT_ERR_IO);
4143 done:
4144 free(line);
4145 return err;
4148 static const struct got_error *
4149 skip_one_line(FILE *f)
4151 char *line = NULL;
4152 size_t linesize = 0;
4153 ssize_t linelen;
4155 linelen = getline(&line, &linesize, f);
4156 if (linelen == -1) {
4157 if (ferror(f))
4158 return got_error_from_errno("getline");
4159 return NULL;
4161 free(line);
4162 return NULL;
4165 static const struct got_error *
4166 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4167 int start_old, int end_old, int start_new, int end_new,
4168 FILE *outfile, FILE *rejectfile)
4170 const struct got_error *err;
4172 /* Copy old file's lines leading up to patch. */
4173 while (!feof(f1) && *line_cur1 < start_old) {
4174 err = copy_one_line(f1, outfile, NULL);
4175 if (err)
4176 return err;
4177 (*line_cur1)++;
4179 /* Skip new file's lines leading up to patch. */
4180 while (!feof(f2) && *line_cur2 < start_new) {
4181 if (rejectfile)
4182 err = copy_one_line(f2, NULL, rejectfile);
4183 else
4184 err = skip_one_line(f2);
4185 if (err)
4186 return err;
4187 (*line_cur2)++;
4189 /* Copy patched lines. */
4190 while (!feof(f2) && *line_cur2 <= end_new) {
4191 err = copy_one_line(f2, outfile, NULL);
4192 if (err)
4193 return err;
4194 (*line_cur2)++;
4196 /* Skip over old file's replaced lines. */
4197 while (!feof(f1) && *line_cur1 <= end_old) {
4198 if (rejectfile)
4199 err = copy_one_line(f1, NULL, rejectfile);
4200 else
4201 err = skip_one_line(f1);
4202 if (err)
4203 return err;
4204 (*line_cur1)++;
4207 return NULL;
4210 static const struct got_error *
4211 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4212 FILE *outfile, FILE *rejectfile)
4214 const struct got_error *err;
4216 if (outfile) {
4217 /* Copy old file's lines until EOF. */
4218 while (!feof(f1)) {
4219 err = copy_one_line(f1, outfile, NULL);
4220 if (err)
4221 return err;
4222 (*line_cur1)++;
4225 if (rejectfile) {
4226 /* Copy new file's lines until EOF. */
4227 while (!feof(f2)) {
4228 err = copy_one_line(f2, NULL, rejectfile);
4229 if (err)
4230 return err;
4231 (*line_cur2)++;
4235 return NULL;
4238 static const struct got_error *
4239 apply_or_reject_change(int *choice, int *nchunks_used,
4240 struct diff_result *diff_result, int n,
4241 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4242 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4243 got_worktree_patch_cb patch_cb, void *patch_arg)
4245 const struct got_error *err = NULL;
4246 struct diff_chunk_context cc = {};
4247 int start_old, end_old, start_new, end_new;
4248 FILE *hunkfile;
4249 struct diff_output_unidiff_state *diff_state;
4250 struct diff_input_info diff_info;
4251 int rc;
4253 *choice = GOT_PATCH_CHOICE_NONE;
4255 /* Get changed line numbers without context lines for copy_change(). */
4256 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4257 start_old = cc.left.start;
4258 end_old = cc.left.end;
4259 start_new = cc.right.start;
4260 end_new = cc.right.end;
4262 /* Get the same change with context lines for display. */
4263 memset(&cc, 0, sizeof(cc));
4264 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4266 memset(&diff_info, 0, sizeof(diff_info));
4267 diff_info.left_path = relpath;
4268 diff_info.right_path = relpath;
4270 diff_state = diff_output_unidiff_state_alloc();
4271 if (diff_state == NULL)
4272 return got_error_set_errno(ENOMEM,
4273 "diff_output_unidiff_state_alloc");
4275 hunkfile = got_opentemp();
4276 if (hunkfile == NULL) {
4277 err = got_error_from_errno("got_opentemp");
4278 goto done;
4281 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4282 diff_result, &cc);
4283 if (rc != DIFF_RC_OK) {
4284 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4285 goto done;
4288 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4289 err = got_ferror(hunkfile, GOT_ERR_IO);
4290 goto done;
4293 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4294 hunkfile, changeno, nchanges);
4295 if (err)
4296 goto done;
4298 switch (*choice) {
4299 case GOT_PATCH_CHOICE_YES:
4300 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4301 end_old, start_new, end_new, outfile, rejectfile);
4302 break;
4303 case GOT_PATCH_CHOICE_NO:
4304 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4305 end_old, start_new, end_new, rejectfile, outfile);
4306 break;
4307 case GOT_PATCH_CHOICE_QUIT:
4308 break;
4309 default:
4310 err = got_error(GOT_ERR_PATCH_CHOICE);
4311 break;
4313 done:
4314 diff_output_unidiff_state_free(diff_state);
4315 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4316 err = got_error_from_errno("fclose");
4317 return err;
4320 struct revert_file_args {
4321 struct got_worktree *worktree;
4322 struct got_fileindex *fileindex;
4323 got_worktree_checkout_cb progress_cb;
4324 void *progress_arg;
4325 got_worktree_patch_cb patch_cb;
4326 void *patch_arg;
4327 struct got_repository *repo;
4328 int unlink_added_files;
4331 static const struct got_error *
4332 create_patched_content(char **path_outfile, int reverse_patch,
4333 struct got_object_id *blob_id, const char *path2,
4334 int dirfd2, const char *de_name2,
4335 const char *relpath, struct got_repository *repo,
4336 got_worktree_patch_cb patch_cb, void *patch_arg)
4338 const struct got_error *err, *free_err;
4339 struct got_blob_object *blob = NULL;
4340 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4341 int fd2 = -1;
4342 char link_target[PATH_MAX];
4343 ssize_t link_len = 0;
4344 char *path1 = NULL, *id_str = NULL;
4345 struct stat sb2;
4346 struct got_diffreg_result *diffreg_result = NULL;
4347 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4348 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4350 *path_outfile = NULL;
4352 err = got_object_id_str(&id_str, blob_id);
4353 if (err)
4354 return err;
4356 if (dirfd2 != -1) {
4357 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4358 if (fd2 == -1) {
4359 if (!got_err_open_nofollow_on_symlink()) {
4360 err = got_error_from_errno2("openat", path2);
4361 goto done;
4363 link_len = readlinkat(dirfd2, de_name2,
4364 link_target, sizeof(link_target));
4365 if (link_len == -1)
4366 return got_error_from_errno2("readlinkat", path2);
4367 sb2.st_mode = S_IFLNK;
4368 sb2.st_size = link_len;
4370 } else {
4371 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4372 if (fd2 == -1) {
4373 if (!got_err_open_nofollow_on_symlink()) {
4374 err = got_error_from_errno2("open", path2);
4375 goto done;
4377 link_len = readlink(path2, link_target,
4378 sizeof(link_target));
4379 if (link_len == -1)
4380 return got_error_from_errno2("readlink", path2);
4381 sb2.st_mode = S_IFLNK;
4382 sb2.st_size = link_len;
4385 if (fd2 != -1) {
4386 if (fstat(fd2, &sb2) == -1) {
4387 err = got_error_from_errno2("fstat", path2);
4388 goto done;
4391 f2 = fdopen(fd2, "r");
4392 if (f2 == NULL) {
4393 err = got_error_from_errno2("fdopen", path2);
4394 goto done;
4396 fd2 = -1;
4397 } else {
4398 size_t n;
4399 f2 = got_opentemp();
4400 if (f2 == NULL) {
4401 err = got_error_from_errno2("got_opentemp", path2);
4402 goto done;
4404 n = fwrite(link_target, 1, link_len, f2);
4405 if (n != link_len) {
4406 err = got_ferror(f2, GOT_ERR_IO);
4407 goto done;
4409 if (fflush(f2) == EOF) {
4410 err = got_error_from_errno("fflush");
4411 goto done;
4413 rewind(f2);
4416 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4417 if (err)
4418 goto done;
4420 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4421 if (err)
4422 goto done;
4424 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4425 if (err)
4426 goto done;
4428 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4429 NULL);
4430 if (err)
4431 goto done;
4433 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4434 if (err)
4435 goto done;
4437 if (fseek(f1, 0L, SEEK_SET) == -1)
4438 return got_ferror(f1, GOT_ERR_IO);
4439 if (fseek(f2, 0L, SEEK_SET) == -1)
4440 return got_ferror(f2, GOT_ERR_IO);
4442 /* Count the number of actual changes in the diff result. */
4443 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4444 struct diff_chunk_context cc = {};
4445 diff_chunk_context_load_change(&cc, &nchunks_used,
4446 diffreg_result->result, n, 0);
4447 nchanges++;
4449 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4450 int choice;
4451 err = apply_or_reject_change(&choice, &nchunks_used,
4452 diffreg_result->result, n, relpath, f1, f2,
4453 &line_cur1, &line_cur2,
4454 reverse_patch ? NULL : outfile,
4455 reverse_patch ? outfile : NULL,
4456 ++i, nchanges, patch_cb, patch_arg);
4457 if (err)
4458 goto done;
4459 if (choice == GOT_PATCH_CHOICE_YES)
4460 have_content = 1;
4461 else if (choice == GOT_PATCH_CHOICE_QUIT)
4462 break;
4464 if (have_content) {
4465 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4466 reverse_patch ? NULL : outfile,
4467 reverse_patch ? outfile : NULL);
4468 if (err)
4469 goto done;
4471 if (!S_ISLNK(sb2.st_mode)) {
4472 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4473 err = got_error_from_errno2("fchmod", path2);
4474 goto done;
4478 done:
4479 free(id_str);
4480 if (blob)
4481 got_object_blob_close(blob);
4482 free_err = got_diffreg_result_free(diffreg_result);
4483 if (err == NULL)
4484 err = free_err;
4485 if (f1 && fclose(f1) == EOF && err == NULL)
4486 err = got_error_from_errno2("fclose", path1);
4487 if (f2 && fclose(f2) == EOF && err == NULL)
4488 err = got_error_from_errno2("fclose", path2);
4489 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4490 err = got_error_from_errno2("close", path2);
4491 if (outfile && fclose(outfile) == EOF && err == NULL)
4492 err = got_error_from_errno2("fclose", *path_outfile);
4493 if (path1 && unlink(path1) == -1 && err == NULL)
4494 err = got_error_from_errno2("unlink", path1);
4495 if (err || !have_content) {
4496 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4497 err = got_error_from_errno2("unlink", *path_outfile);
4498 free(*path_outfile);
4499 *path_outfile = NULL;
4501 free(path1);
4502 return err;
4505 static const struct got_error *
4506 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4507 const char *relpath, struct got_object_id *blob_id,
4508 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4509 int dirfd, const char *de_name)
4511 struct revert_file_args *a = arg;
4512 const struct got_error *err = NULL;
4513 char *parent_path = NULL;
4514 struct got_fileindex_entry *ie;
4515 struct got_tree_object *tree = NULL;
4516 struct got_object_id *tree_id = NULL;
4517 const struct got_tree_entry *te = NULL;
4518 char *tree_path = NULL, *te_name;
4519 char *ondisk_path = NULL, *path_content = NULL;
4520 struct got_blob_object *blob = NULL;
4522 /* Reverting a staged deletion is a no-op. */
4523 if (status == GOT_STATUS_DELETE &&
4524 staged_status != GOT_STATUS_NO_CHANGE)
4525 return NULL;
4527 if (status == GOT_STATUS_UNVERSIONED)
4528 return (*a->progress_cb)(a->progress_arg,
4529 GOT_STATUS_UNVERSIONED, relpath);
4531 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4532 if (ie == NULL)
4533 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4535 /* Construct in-repository path of tree which contains this blob. */
4536 err = got_path_dirname(&parent_path, ie->path);
4537 if (err) {
4538 if (err->code != GOT_ERR_BAD_PATH)
4539 goto done;
4540 parent_path = strdup("/");
4541 if (parent_path == NULL) {
4542 err = got_error_from_errno("strdup");
4543 goto done;
4546 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4547 tree_path = strdup(parent_path);
4548 if (tree_path == NULL) {
4549 err = got_error_from_errno("strdup");
4550 goto done;
4552 } else {
4553 if (got_path_is_root_dir(parent_path)) {
4554 tree_path = strdup(a->worktree->path_prefix);
4555 if (tree_path == NULL) {
4556 err = got_error_from_errno("strdup");
4557 goto done;
4559 } else {
4560 if (asprintf(&tree_path, "%s/%s",
4561 a->worktree->path_prefix, parent_path) == -1) {
4562 err = got_error_from_errno("asprintf");
4563 goto done;
4568 err = got_object_id_by_path(&tree_id, a->repo,
4569 a->worktree->base_commit_id, tree_path);
4570 if (err) {
4571 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4572 (status == GOT_STATUS_ADD ||
4573 staged_status == GOT_STATUS_ADD)))
4574 goto done;
4575 } else {
4576 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4577 if (err)
4578 goto done;
4580 err = got_path_basename(&te_name, ie->path);
4581 if (err)
4582 goto done;
4584 te = got_object_tree_find_entry(tree, te_name);
4585 free(te_name);
4586 if (te == NULL && status != GOT_STATUS_ADD &&
4587 staged_status != GOT_STATUS_ADD) {
4588 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4589 goto done;
4593 switch (status) {
4594 case GOT_STATUS_ADD:
4595 if (a->patch_cb) {
4596 int choice = GOT_PATCH_CHOICE_NONE;
4597 err = (*a->patch_cb)(&choice, a->patch_arg,
4598 status, ie->path, NULL, 1, 1);
4599 if (err)
4600 goto done;
4601 if (choice != GOT_PATCH_CHOICE_YES)
4602 break;
4604 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4605 ie->path);
4606 if (err)
4607 goto done;
4608 got_fileindex_entry_remove(a->fileindex, ie);
4609 if (a->unlink_added_files) {
4610 if (asprintf(&ondisk_path, "%s/%s",
4611 got_worktree_get_root_path(a->worktree),
4612 relpath) == -1) {
4613 err = got_error_from_errno("asprintf");
4614 goto done;
4616 if (unlink(ondisk_path) == -1) {
4617 err = got_error_from_errno2("unlink",
4618 ondisk_path);
4619 break;
4622 break;
4623 case GOT_STATUS_DELETE:
4624 if (a->patch_cb) {
4625 int choice = GOT_PATCH_CHOICE_NONE;
4626 err = (*a->patch_cb)(&choice, a->patch_arg,
4627 status, ie->path, NULL, 1, 1);
4628 if (err)
4629 goto done;
4630 if (choice != GOT_PATCH_CHOICE_YES)
4631 break;
4633 /* fall through */
4634 case GOT_STATUS_MODIFY:
4635 case GOT_STATUS_MODE_CHANGE:
4636 case GOT_STATUS_CONFLICT:
4637 case GOT_STATUS_MISSING: {
4638 struct got_object_id id;
4639 if (staged_status == GOT_STATUS_ADD ||
4640 staged_status == GOT_STATUS_MODIFY) {
4641 memcpy(id.sha1, ie->staged_blob_sha1,
4642 SHA1_DIGEST_LENGTH);
4643 } else
4644 memcpy(id.sha1, ie->blob_sha1,
4645 SHA1_DIGEST_LENGTH);
4646 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4647 if (err)
4648 goto done;
4650 if (asprintf(&ondisk_path, "%s/%s",
4651 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4652 err = got_error_from_errno("asprintf");
4653 goto done;
4656 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4657 status == GOT_STATUS_CONFLICT)) {
4658 int is_bad_symlink = 0;
4659 err = create_patched_content(&path_content, 1, &id,
4660 ondisk_path, dirfd, de_name, ie->path, a->repo,
4661 a->patch_cb, a->patch_arg);
4662 if (err || path_content == NULL)
4663 break;
4664 if (te && S_ISLNK(te->mode)) {
4665 if (unlink(path_content) == -1) {
4666 err = got_error_from_errno2("unlink",
4667 path_content);
4668 break;
4670 err = install_symlink(&is_bad_symlink,
4671 a->worktree, ondisk_path, ie->path,
4672 blob, 0, 1, 0, 0, a->repo,
4673 a->progress_cb, a->progress_arg);
4674 } else {
4675 if (rename(path_content, ondisk_path) == -1) {
4676 err = got_error_from_errno3("rename",
4677 path_content, ondisk_path);
4678 goto done;
4681 } else {
4682 int is_bad_symlink = 0;
4683 if (te && S_ISLNK(te->mode)) {
4684 err = install_symlink(&is_bad_symlink,
4685 a->worktree, ondisk_path, ie->path,
4686 blob, 0, 1, 0, 0, a->repo,
4687 a->progress_cb, a->progress_arg);
4688 } else {
4689 err = install_blob(a->worktree, ondisk_path,
4690 ie->path,
4691 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4692 got_fileindex_perms_to_st(ie), blob,
4693 0, 1, 0, 0, a->repo,
4694 a->progress_cb, a->progress_arg);
4696 if (err)
4697 goto done;
4698 if (status == GOT_STATUS_DELETE ||
4699 status == GOT_STATUS_MODE_CHANGE) {
4700 err = got_fileindex_entry_update(ie,
4701 a->worktree->root_fd, relpath,
4702 blob->id.sha1,
4703 a->worktree->base_commit_id->sha1, 1);
4704 if (err)
4705 goto done;
4707 if (is_bad_symlink) {
4708 got_fileindex_entry_filetype_set(ie,
4709 GOT_FILEIDX_MODE_BAD_SYMLINK);
4712 break;
4714 default:
4715 break;
4717 done:
4718 free(ondisk_path);
4719 free(path_content);
4720 free(parent_path);
4721 free(tree_path);
4722 if (blob)
4723 got_object_blob_close(blob);
4724 if (tree)
4725 got_object_tree_close(tree);
4726 free(tree_id);
4727 return err;
4730 const struct got_error *
4731 got_worktree_revert(struct got_worktree *worktree,
4732 struct got_pathlist_head *paths,
4733 got_worktree_checkout_cb progress_cb, void *progress_arg,
4734 got_worktree_patch_cb patch_cb, void *patch_arg,
4735 struct got_repository *repo)
4737 struct got_fileindex *fileindex = NULL;
4738 char *fileindex_path = NULL;
4739 const struct got_error *err = NULL, *unlockerr = NULL;
4740 const struct got_error *sync_err = NULL;
4741 struct got_pathlist_entry *pe;
4742 struct revert_file_args rfa;
4744 err = lock_worktree(worktree, LOCK_EX);
4745 if (err)
4746 return err;
4748 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4749 if (err)
4750 goto done;
4752 rfa.worktree = worktree;
4753 rfa.fileindex = fileindex;
4754 rfa.progress_cb = progress_cb;
4755 rfa.progress_arg = progress_arg;
4756 rfa.patch_cb = patch_cb;
4757 rfa.patch_arg = patch_arg;
4758 rfa.repo = repo;
4759 rfa.unlink_added_files = 0;
4760 TAILQ_FOREACH(pe, paths, entry) {
4761 err = worktree_status(worktree, pe->path, fileindex, repo,
4762 revert_file, &rfa, NULL, NULL, 1, 0);
4763 if (err)
4764 break;
4766 sync_err = sync_fileindex(fileindex, fileindex_path);
4767 if (sync_err && err == NULL)
4768 err = sync_err;
4769 done:
4770 free(fileindex_path);
4771 if (fileindex)
4772 got_fileindex_free(fileindex);
4773 unlockerr = lock_worktree(worktree, LOCK_SH);
4774 if (unlockerr && err == NULL)
4775 err = unlockerr;
4776 return err;
4779 static void
4780 free_commitable(struct got_commitable *ct)
4782 free(ct->path);
4783 free(ct->in_repo_path);
4784 free(ct->ondisk_path);
4785 free(ct->blob_id);
4786 free(ct->base_blob_id);
4787 free(ct->staged_blob_id);
4788 free(ct->base_commit_id);
4789 free(ct);
4792 struct collect_commitables_arg {
4793 struct got_pathlist_head *commitable_paths;
4794 struct got_repository *repo;
4795 struct got_worktree *worktree;
4796 struct got_fileindex *fileindex;
4797 int have_staged_files;
4798 int allow_bad_symlinks;
4801 static const struct got_error *
4802 collect_commitables(void *arg, unsigned char status,
4803 unsigned char staged_status, const char *relpath,
4804 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4805 struct got_object_id *commit_id, int dirfd, const char *de_name)
4807 struct collect_commitables_arg *a = arg;
4808 const struct got_error *err = NULL;
4809 struct got_commitable *ct = NULL;
4810 struct got_pathlist_entry *new = NULL;
4811 char *parent_path = NULL, *path = NULL;
4812 struct stat sb;
4814 if (a->have_staged_files) {
4815 if (staged_status != GOT_STATUS_MODIFY &&
4816 staged_status != GOT_STATUS_ADD &&
4817 staged_status != GOT_STATUS_DELETE)
4818 return NULL;
4819 } else {
4820 if (status == GOT_STATUS_CONFLICT)
4821 return got_error(GOT_ERR_COMMIT_CONFLICT);
4823 if (status != GOT_STATUS_MODIFY &&
4824 status != GOT_STATUS_MODE_CHANGE &&
4825 status != GOT_STATUS_ADD &&
4826 status != GOT_STATUS_DELETE)
4827 return NULL;
4830 if (asprintf(&path, "/%s", relpath) == -1) {
4831 err = got_error_from_errno("asprintf");
4832 goto done;
4834 if (strcmp(path, "/") == 0) {
4835 parent_path = strdup("");
4836 if (parent_path == NULL)
4837 return got_error_from_errno("strdup");
4838 } else {
4839 err = got_path_dirname(&parent_path, path);
4840 if (err)
4841 return err;
4844 ct = calloc(1, sizeof(*ct));
4845 if (ct == NULL) {
4846 err = got_error_from_errno("calloc");
4847 goto done;
4850 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4851 relpath) == -1) {
4852 err = got_error_from_errno("asprintf");
4853 goto done;
4856 if (staged_status == GOT_STATUS_ADD ||
4857 staged_status == GOT_STATUS_MODIFY) {
4858 struct got_fileindex_entry *ie;
4859 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4860 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4861 case GOT_FILEIDX_MODE_REGULAR_FILE:
4862 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4863 ct->mode = S_IFREG;
4864 break;
4865 case GOT_FILEIDX_MODE_SYMLINK:
4866 ct->mode = S_IFLNK;
4867 break;
4868 default:
4869 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4870 goto done;
4872 ct->mode |= got_fileindex_entry_perms_get(ie);
4873 } else if (status != GOT_STATUS_DELETE &&
4874 staged_status != GOT_STATUS_DELETE) {
4875 if (dirfd != -1) {
4876 if (fstatat(dirfd, de_name, &sb,
4877 AT_SYMLINK_NOFOLLOW) == -1) {
4878 err = got_error_from_errno2("fstatat",
4879 ct->ondisk_path);
4880 goto done;
4882 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4883 err = got_error_from_errno2("lstat", ct->ondisk_path);
4884 goto done;
4886 ct->mode = sb.st_mode;
4889 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4890 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4891 relpath) == -1) {
4892 err = got_error_from_errno("asprintf");
4893 goto done;
4896 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4897 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4898 int is_bad_symlink;
4899 char target_path[PATH_MAX];
4900 ssize_t target_len;
4901 target_len = readlink(ct->ondisk_path, target_path,
4902 sizeof(target_path));
4903 if (target_len == -1) {
4904 err = got_error_from_errno2("readlink",
4905 ct->ondisk_path);
4906 goto done;
4908 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4909 target_len, ct->ondisk_path, a->worktree->root_path);
4910 if (err)
4911 goto done;
4912 if (is_bad_symlink) {
4913 err = got_error_path(ct->ondisk_path,
4914 GOT_ERR_BAD_SYMLINK);
4915 goto done;
4920 ct->status = status;
4921 ct->staged_status = staged_status;
4922 ct->blob_id = NULL; /* will be filled in when blob gets created */
4923 if (ct->status != GOT_STATUS_ADD &&
4924 ct->staged_status != GOT_STATUS_ADD) {
4925 ct->base_blob_id = got_object_id_dup(blob_id);
4926 if (ct->base_blob_id == NULL) {
4927 err = got_error_from_errno("got_object_id_dup");
4928 goto done;
4930 ct->base_commit_id = got_object_id_dup(commit_id);
4931 if (ct->base_commit_id == NULL) {
4932 err = got_error_from_errno("got_object_id_dup");
4933 goto done;
4936 if (ct->staged_status == GOT_STATUS_ADD ||
4937 ct->staged_status == GOT_STATUS_MODIFY) {
4938 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4939 if (ct->staged_blob_id == NULL) {
4940 err = got_error_from_errno("got_object_id_dup");
4941 goto done;
4944 ct->path = strdup(path);
4945 if (ct->path == NULL) {
4946 err = got_error_from_errno("strdup");
4947 goto done;
4949 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4950 done:
4951 if (ct && (err || new == NULL))
4952 free_commitable(ct);
4953 free(parent_path);
4954 free(path);
4955 return err;
4958 static const struct got_error *write_tree(struct got_object_id **, int *,
4959 struct got_tree_object *, const char *, struct got_pathlist_head *,
4960 got_worktree_status_cb status_cb, void *status_arg,
4961 struct got_repository *);
4963 static const struct got_error *
4964 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4965 struct got_tree_entry *te, const char *parent_path,
4966 struct got_pathlist_head *commitable_paths,
4967 got_worktree_status_cb status_cb, void *status_arg,
4968 struct got_repository *repo)
4970 const struct got_error *err = NULL;
4971 struct got_tree_object *subtree;
4972 char *subpath;
4974 if (asprintf(&subpath, "%s%s%s", parent_path,
4975 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4976 return got_error_from_errno("asprintf");
4978 err = got_object_open_as_tree(&subtree, repo, &te->id);
4979 if (err)
4980 return err;
4982 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4983 commitable_paths, status_cb, status_arg, repo);
4984 got_object_tree_close(subtree);
4985 free(subpath);
4986 return err;
4989 static const struct got_error *
4990 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4992 const struct got_error *err = NULL;
4993 char *ct_parent_path = NULL;
4995 *match = 0;
4997 if (strchr(ct->in_repo_path, '/') == NULL) {
4998 *match = got_path_is_root_dir(path);
4999 return NULL;
5002 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5003 if (err)
5004 return err;
5005 *match = (strcmp(path, ct_parent_path) == 0);
5006 free(ct_parent_path);
5007 return err;
5010 static mode_t
5011 get_ct_file_mode(struct got_commitable *ct)
5013 if (S_ISLNK(ct->mode))
5014 return S_IFLNK;
5016 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5019 static const struct got_error *
5020 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5021 struct got_tree_entry *te, struct got_commitable *ct)
5023 const struct got_error *err = NULL;
5025 *new_te = NULL;
5027 err = got_object_tree_entry_dup(new_te, te);
5028 if (err)
5029 goto done;
5031 (*new_te)->mode = get_ct_file_mode(ct);
5033 if (ct->staged_status == GOT_STATUS_MODIFY)
5034 memcpy(&(*new_te)->id, ct->staged_blob_id,
5035 sizeof((*new_te)->id));
5036 else
5037 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5038 done:
5039 if (err && *new_te) {
5040 free(*new_te);
5041 *new_te = NULL;
5043 return err;
5046 static const struct got_error *
5047 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5048 struct got_commitable *ct)
5050 const struct got_error *err = NULL;
5051 char *ct_name = NULL;
5053 *new_te = NULL;
5055 *new_te = calloc(1, sizeof(**new_te));
5056 if (*new_te == NULL)
5057 return got_error_from_errno("calloc");
5059 err = got_path_basename(&ct_name, ct->path);
5060 if (err)
5061 goto done;
5062 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5063 sizeof((*new_te)->name)) {
5064 err = got_error(GOT_ERR_NO_SPACE);
5065 goto done;
5068 (*new_te)->mode = get_ct_file_mode(ct);
5070 if (ct->staged_status == GOT_STATUS_ADD)
5071 memcpy(&(*new_te)->id, ct->staged_blob_id,
5072 sizeof((*new_te)->id));
5073 else
5074 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5075 done:
5076 free(ct_name);
5077 if (err && *new_te) {
5078 free(*new_te);
5079 *new_te = NULL;
5081 return err;
5084 static const struct got_error *
5085 insert_tree_entry(struct got_tree_entry *new_te,
5086 struct got_pathlist_head *paths)
5088 const struct got_error *err = NULL;
5089 struct got_pathlist_entry *new_pe;
5091 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5092 if (err)
5093 return err;
5094 if (new_pe == NULL)
5095 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5096 return NULL;
5099 static const struct got_error *
5100 report_ct_status(struct got_commitable *ct,
5101 got_worktree_status_cb status_cb, void *status_arg)
5103 const char *ct_path = ct->path;
5104 unsigned char status;
5106 if (status_cb == NULL) /* no commit progress output desired */
5107 return NULL;
5109 while (ct_path[0] == '/')
5110 ct_path++;
5112 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5113 status = ct->staged_status;
5114 else
5115 status = ct->status;
5117 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5118 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5121 static const struct got_error *
5122 match_modified_subtree(int *modified, struct got_tree_entry *te,
5123 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5125 const struct got_error *err = NULL;
5126 struct got_pathlist_entry *pe;
5127 char *te_path;
5129 *modified = 0;
5131 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5132 got_path_is_root_dir(base_tree_path) ? "" : "/",
5133 te->name) == -1)
5134 return got_error_from_errno("asprintf");
5136 TAILQ_FOREACH(pe, commitable_paths, entry) {
5137 struct got_commitable *ct = pe->data;
5138 *modified = got_path_is_child(ct->in_repo_path, te_path,
5139 strlen(te_path));
5140 if (*modified)
5141 break;
5144 free(te_path);
5145 return err;
5148 static const struct got_error *
5149 match_deleted_or_modified_ct(struct got_commitable **ctp,
5150 struct got_tree_entry *te, const char *base_tree_path,
5151 struct got_pathlist_head *commitable_paths)
5153 const struct got_error *err = NULL;
5154 struct got_pathlist_entry *pe;
5156 *ctp = NULL;
5158 TAILQ_FOREACH(pe, commitable_paths, entry) {
5159 struct got_commitable *ct = pe->data;
5160 char *ct_name = NULL;
5161 int path_matches;
5163 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5164 if (ct->status != GOT_STATUS_MODIFY &&
5165 ct->status != GOT_STATUS_MODE_CHANGE &&
5166 ct->status != GOT_STATUS_DELETE)
5167 continue;
5168 } else {
5169 if (ct->staged_status != GOT_STATUS_MODIFY &&
5170 ct->staged_status != GOT_STATUS_DELETE)
5171 continue;
5174 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5175 continue;
5177 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5178 if (err)
5179 return err;
5180 if (!path_matches)
5181 continue;
5183 err = got_path_basename(&ct_name, pe->path);
5184 if (err)
5185 return err;
5187 if (strcmp(te->name, ct_name) != 0) {
5188 free(ct_name);
5189 continue;
5191 free(ct_name);
5193 *ctp = ct;
5194 break;
5197 return err;
5200 static const struct got_error *
5201 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5202 const char *child_path, const char *path_base_tree,
5203 struct got_pathlist_head *commitable_paths,
5204 got_worktree_status_cb status_cb, void *status_arg,
5205 struct got_repository *repo)
5207 const struct got_error *err = NULL;
5208 struct got_tree_entry *new_te;
5209 char *subtree_path;
5210 struct got_object_id *id = NULL;
5211 int nentries;
5213 *new_tep = NULL;
5215 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5216 got_path_is_root_dir(path_base_tree) ? "" : "/",
5217 child_path) == -1)
5218 return got_error_from_errno("asprintf");
5220 new_te = calloc(1, sizeof(*new_te));
5221 if (new_te == NULL)
5222 return got_error_from_errno("calloc");
5223 new_te->mode = S_IFDIR;
5225 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5226 sizeof(new_te->name)) {
5227 err = got_error(GOT_ERR_NO_SPACE);
5228 goto done;
5230 err = write_tree(&id, &nentries, NULL, subtree_path,
5231 commitable_paths, status_cb, status_arg, repo);
5232 if (err) {
5233 free(new_te);
5234 goto done;
5236 memcpy(&new_te->id, id, sizeof(new_te->id));
5237 done:
5238 free(id);
5239 free(subtree_path);
5240 if (err == NULL)
5241 *new_tep = new_te;
5242 return err;
5245 static const struct got_error *
5246 write_tree(struct got_object_id **new_tree_id, int *nentries,
5247 struct got_tree_object *base_tree, const char *path_base_tree,
5248 struct got_pathlist_head *commitable_paths,
5249 got_worktree_status_cb status_cb, void *status_arg,
5250 struct got_repository *repo)
5252 const struct got_error *err = NULL;
5253 struct got_pathlist_head paths;
5254 struct got_tree_entry *te, *new_te = NULL;
5255 struct got_pathlist_entry *pe;
5257 TAILQ_INIT(&paths);
5258 *nentries = 0;
5260 /* Insert, and recurse into, newly added entries first. */
5261 TAILQ_FOREACH(pe, commitable_paths, entry) {
5262 struct got_commitable *ct = pe->data;
5263 char *child_path = NULL, *slash;
5265 if ((ct->status != GOT_STATUS_ADD &&
5266 ct->staged_status != GOT_STATUS_ADD) ||
5267 (ct->flags & GOT_COMMITABLE_ADDED))
5268 continue;
5270 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5271 strlen(path_base_tree)))
5272 continue;
5274 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5275 ct->in_repo_path);
5276 if (err)
5277 goto done;
5279 slash = strchr(child_path, '/');
5280 if (slash == NULL) {
5281 err = alloc_added_blob_tree_entry(&new_te, ct);
5282 if (err)
5283 goto done;
5284 err = report_ct_status(ct, status_cb, status_arg);
5285 if (err)
5286 goto done;
5287 ct->flags |= GOT_COMMITABLE_ADDED;
5288 err = insert_tree_entry(new_te, &paths);
5289 if (err)
5290 goto done;
5291 (*nentries)++;
5292 } else {
5293 *slash = '\0'; /* trim trailing path components */
5294 if (base_tree == NULL ||
5295 got_object_tree_find_entry(base_tree, child_path)
5296 == NULL) {
5297 err = make_subtree_for_added_blob(&new_te,
5298 child_path, path_base_tree,
5299 commitable_paths, status_cb, status_arg,
5300 repo);
5301 if (err)
5302 goto done;
5303 err = insert_tree_entry(new_te, &paths);
5304 if (err)
5305 goto done;
5306 (*nentries)++;
5311 if (base_tree) {
5312 int i, nbase_entries;
5313 /* Handle modified and deleted entries. */
5314 nbase_entries = got_object_tree_get_nentries(base_tree);
5315 for (i = 0; i < nbase_entries; i++) {
5316 struct got_commitable *ct = NULL;
5318 te = got_object_tree_get_entry(base_tree, i);
5319 if (got_object_tree_entry_is_submodule(te)) {
5320 /* Entry is a submodule; just copy it. */
5321 err = got_object_tree_entry_dup(&new_te, te);
5322 if (err)
5323 goto done;
5324 err = insert_tree_entry(new_te, &paths);
5325 if (err)
5326 goto done;
5327 (*nentries)++;
5328 continue;
5331 if (S_ISDIR(te->mode)) {
5332 int modified;
5333 err = got_object_tree_entry_dup(&new_te, te);
5334 if (err)
5335 goto done;
5336 err = match_modified_subtree(&modified, te,
5337 path_base_tree, commitable_paths);
5338 if (err)
5339 goto done;
5340 /* Avoid recursion into unmodified subtrees. */
5341 if (modified) {
5342 struct got_object_id *new_id;
5343 int nsubentries;
5344 err = write_subtree(&new_id,
5345 &nsubentries, te,
5346 path_base_tree, commitable_paths,
5347 status_cb, status_arg, repo);
5348 if (err)
5349 goto done;
5350 if (nsubentries == 0) {
5351 /* All entries were deleted. */
5352 free(new_id);
5353 continue;
5355 memcpy(&new_te->id, new_id,
5356 sizeof(new_te->id));
5357 free(new_id);
5359 err = insert_tree_entry(new_te, &paths);
5360 if (err)
5361 goto done;
5362 (*nentries)++;
5363 continue;
5366 err = match_deleted_or_modified_ct(&ct, te,
5367 path_base_tree, commitable_paths);
5368 if (err)
5369 goto done;
5370 if (ct) {
5371 /* NB: Deleted entries get dropped here. */
5372 if (ct->status == GOT_STATUS_MODIFY ||
5373 ct->status == GOT_STATUS_MODE_CHANGE ||
5374 ct->staged_status == GOT_STATUS_MODIFY) {
5375 err = alloc_modified_blob_tree_entry(
5376 &new_te, te, ct);
5377 if (err)
5378 goto done;
5379 err = insert_tree_entry(new_te, &paths);
5380 if (err)
5381 goto done;
5382 (*nentries)++;
5384 err = report_ct_status(ct, status_cb,
5385 status_arg);
5386 if (err)
5387 goto done;
5388 } else {
5389 /* Entry is unchanged; just copy it. */
5390 err = got_object_tree_entry_dup(&new_te, te);
5391 if (err)
5392 goto done;
5393 err = insert_tree_entry(new_te, &paths);
5394 if (err)
5395 goto done;
5396 (*nentries)++;
5401 /* Write new list of entries; deleted entries have been dropped. */
5402 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5403 done:
5404 got_pathlist_free(&paths);
5405 return err;
5408 static const struct got_error *
5409 update_fileindex_after_commit(struct got_worktree *worktree,
5410 struct got_pathlist_head *commitable_paths,
5411 struct got_object_id *new_base_commit_id,
5412 struct got_fileindex *fileindex, int have_staged_files)
5414 const struct got_error *err = NULL;
5415 struct got_pathlist_entry *pe;
5416 char *relpath = NULL;
5418 TAILQ_FOREACH(pe, commitable_paths, entry) {
5419 struct got_fileindex_entry *ie;
5420 struct got_commitable *ct = pe->data;
5422 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5424 err = got_path_skip_common_ancestor(&relpath,
5425 worktree->root_path, ct->ondisk_path);
5426 if (err)
5427 goto done;
5429 if (ie) {
5430 if (ct->status == GOT_STATUS_DELETE ||
5431 ct->staged_status == GOT_STATUS_DELETE) {
5432 got_fileindex_entry_remove(fileindex, ie);
5433 } else if (ct->staged_status == GOT_STATUS_ADD ||
5434 ct->staged_status == GOT_STATUS_MODIFY) {
5435 got_fileindex_entry_stage_set(ie,
5436 GOT_FILEIDX_STAGE_NONE);
5437 got_fileindex_entry_staged_filetype_set(ie, 0);
5439 err = got_fileindex_entry_update(ie,
5440 worktree->root_fd, relpath,
5441 ct->staged_blob_id->sha1,
5442 new_base_commit_id->sha1,
5443 !have_staged_files);
5444 } else
5445 err = got_fileindex_entry_update(ie,
5446 worktree->root_fd, relpath,
5447 ct->blob_id->sha1,
5448 new_base_commit_id->sha1,
5449 !have_staged_files);
5450 } else {
5451 err = got_fileindex_entry_alloc(&ie, pe->path);
5452 if (err)
5453 goto done;
5454 err = got_fileindex_entry_update(ie,
5455 worktree->root_fd, relpath, ct->blob_id->sha1,
5456 new_base_commit_id->sha1, 1);
5457 if (err) {
5458 got_fileindex_entry_free(ie);
5459 goto done;
5461 err = got_fileindex_entry_add(fileindex, ie);
5462 if (err) {
5463 got_fileindex_entry_free(ie);
5464 goto done;
5467 free(relpath);
5468 relpath = NULL;
5470 done:
5471 free(relpath);
5472 return err;
5476 static const struct got_error *
5477 check_out_of_date(const char *in_repo_path, unsigned char status,
5478 unsigned char staged_status, struct got_object_id *base_blob_id,
5479 struct got_object_id *base_commit_id,
5480 struct got_object_id *head_commit_id, struct got_repository *repo,
5481 int ood_errcode)
5483 const struct got_error *err = NULL;
5484 struct got_object_id *id = NULL;
5486 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5487 /* Trivial case: base commit == head commit */
5488 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5489 return NULL;
5491 * Ensure file content which local changes were based
5492 * on matches file content in the branch head.
5494 err = got_object_id_by_path(&id, repo, head_commit_id,
5495 in_repo_path);
5496 if (err) {
5497 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5498 err = got_error(ood_errcode);
5499 goto done;
5500 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5501 err = got_error(ood_errcode);
5502 } else {
5503 /* Require that added files don't exist in the branch head. */
5504 err = got_object_id_by_path(&id, repo, head_commit_id,
5505 in_repo_path);
5506 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5507 goto done;
5508 err = id ? got_error(ood_errcode) : NULL;
5510 done:
5511 free(id);
5512 return err;
5515 const struct got_error *
5516 commit_worktree(struct got_object_id **new_commit_id,
5517 struct got_pathlist_head *commitable_paths,
5518 struct got_object_id *head_commit_id,
5519 struct got_object_id *parent_id2,
5520 struct got_worktree *worktree,
5521 const char *author, const char *committer,
5522 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5523 got_worktree_status_cb status_cb, void *status_arg,
5524 struct got_repository *repo)
5526 const struct got_error *err = NULL, *unlockerr = NULL;
5527 struct got_pathlist_entry *pe;
5528 const char *head_ref_name = NULL;
5529 struct got_commit_object *head_commit = NULL;
5530 struct got_reference *head_ref2 = NULL;
5531 struct got_object_id *head_commit_id2 = NULL;
5532 struct got_tree_object *head_tree = NULL;
5533 struct got_object_id *new_tree_id = NULL;
5534 int nentries, nparents = 0;
5535 struct got_object_id_queue parent_ids;
5536 struct got_object_qid *pid = NULL;
5537 char *logmsg = NULL;
5539 *new_commit_id = NULL;
5541 STAILQ_INIT(&parent_ids);
5543 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5544 if (err)
5545 goto done;
5547 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5548 if (err)
5549 goto done;
5551 if (commit_msg_cb != NULL) {
5552 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5553 if (err)
5554 goto done;
5557 if (logmsg == NULL || strlen(logmsg) == 0) {
5558 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5559 goto done;
5562 /* Create blobs from added and modified files and record their IDs. */
5563 TAILQ_FOREACH(pe, commitable_paths, entry) {
5564 struct got_commitable *ct = pe->data;
5565 char *ondisk_path;
5567 /* Blobs for staged files already exist. */
5568 if (ct->staged_status == GOT_STATUS_ADD ||
5569 ct->staged_status == GOT_STATUS_MODIFY)
5570 continue;
5572 if (ct->status != GOT_STATUS_ADD &&
5573 ct->status != GOT_STATUS_MODIFY &&
5574 ct->status != GOT_STATUS_MODE_CHANGE)
5575 continue;
5577 if (asprintf(&ondisk_path, "%s/%s",
5578 worktree->root_path, pe->path) == -1) {
5579 err = got_error_from_errno("asprintf");
5580 goto done;
5582 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5583 free(ondisk_path);
5584 if (err)
5585 goto done;
5588 /* Recursively write new tree objects. */
5589 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5590 commitable_paths, status_cb, status_arg, repo);
5591 if (err)
5592 goto done;
5594 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5595 if (err)
5596 goto done;
5597 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5598 nparents++;
5599 if (parent_id2) {
5600 err = got_object_qid_alloc(&pid, parent_id2);
5601 if (err)
5602 goto done;
5603 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5604 nparents++;
5606 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5607 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5608 if (logmsg != NULL)
5609 free(logmsg);
5610 if (err)
5611 goto done;
5613 /* Check if a concurrent commit to our branch has occurred. */
5614 head_ref_name = got_worktree_get_head_ref_name(worktree);
5615 if (head_ref_name == NULL) {
5616 err = got_error_from_errno("got_worktree_get_head_ref_name");
5617 goto done;
5619 /* Lock the reference here to prevent concurrent modification. */
5620 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5621 if (err)
5622 goto done;
5623 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5624 if (err)
5625 goto done;
5626 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5627 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5628 goto done;
5630 /* Update branch head in repository. */
5631 err = got_ref_change_ref(head_ref2, *new_commit_id);
5632 if (err)
5633 goto done;
5634 err = got_ref_write(head_ref2, repo);
5635 if (err)
5636 goto done;
5638 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5639 if (err)
5640 goto done;
5642 err = ref_base_commit(worktree, repo);
5643 if (err)
5644 goto done;
5645 done:
5646 got_object_id_queue_free(&parent_ids);
5647 if (head_tree)
5648 got_object_tree_close(head_tree);
5649 if (head_commit)
5650 got_object_commit_close(head_commit);
5651 free(head_commit_id2);
5652 if (head_ref2) {
5653 unlockerr = got_ref_unlock(head_ref2);
5654 if (unlockerr && err == NULL)
5655 err = unlockerr;
5656 got_ref_close(head_ref2);
5658 return err;
5661 static const struct got_error *
5662 check_path_is_commitable(const char *path,
5663 struct got_pathlist_head *commitable_paths)
5665 struct got_pathlist_entry *cpe = NULL;
5666 size_t path_len = strlen(path);
5668 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5669 struct got_commitable *ct = cpe->data;
5670 const char *ct_path = ct->path;
5672 while (ct_path[0] == '/')
5673 ct_path++;
5675 if (strcmp(path, ct_path) == 0 ||
5676 got_path_is_child(ct_path, path, path_len))
5677 break;
5680 if (cpe == NULL)
5681 return got_error_path(path, GOT_ERR_BAD_PATH);
5683 return NULL;
5686 static const struct got_error *
5687 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5689 int *have_staged_files = arg;
5691 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5692 *have_staged_files = 1;
5693 return got_error(GOT_ERR_CANCELLED);
5696 return NULL;
5699 static const struct got_error *
5700 check_non_staged_files(struct got_fileindex *fileindex,
5701 struct got_pathlist_head *paths)
5703 struct got_pathlist_entry *pe;
5704 struct got_fileindex_entry *ie;
5706 TAILQ_FOREACH(pe, paths, entry) {
5707 if (pe->path[0] == '\0')
5708 continue;
5709 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5710 if (ie == NULL)
5711 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5712 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5713 return got_error_path(pe->path,
5714 GOT_ERR_FILE_NOT_STAGED);
5717 return NULL;
5720 const struct got_error *
5721 got_worktree_commit(struct got_object_id **new_commit_id,
5722 struct got_worktree *worktree, struct got_pathlist_head *paths,
5723 const char *author, const char *committer, int allow_bad_symlinks,
5724 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5725 got_worktree_status_cb status_cb, void *status_arg,
5726 struct got_repository *repo)
5728 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5729 struct got_fileindex *fileindex = NULL;
5730 char *fileindex_path = NULL;
5731 struct got_pathlist_head commitable_paths;
5732 struct collect_commitables_arg cc_arg;
5733 struct got_pathlist_entry *pe;
5734 struct got_reference *head_ref = NULL;
5735 struct got_object_id *head_commit_id = NULL;
5736 int have_staged_files = 0;
5738 *new_commit_id = NULL;
5740 TAILQ_INIT(&commitable_paths);
5742 err = lock_worktree(worktree, LOCK_EX);
5743 if (err)
5744 goto done;
5746 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5747 if (err)
5748 goto done;
5750 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5751 if (err)
5752 goto done;
5754 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5755 if (err)
5756 goto done;
5758 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5759 &have_staged_files);
5760 if (err && err->code != GOT_ERR_CANCELLED)
5761 goto done;
5762 if (have_staged_files) {
5763 err = check_non_staged_files(fileindex, paths);
5764 if (err)
5765 goto done;
5768 cc_arg.commitable_paths = &commitable_paths;
5769 cc_arg.worktree = worktree;
5770 cc_arg.fileindex = fileindex;
5771 cc_arg.repo = repo;
5772 cc_arg.have_staged_files = have_staged_files;
5773 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5774 TAILQ_FOREACH(pe, paths, entry) {
5775 err = worktree_status(worktree, pe->path, fileindex, repo,
5776 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5777 if (err)
5778 goto done;
5781 if (TAILQ_EMPTY(&commitable_paths)) {
5782 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5783 goto done;
5786 TAILQ_FOREACH(pe, paths, entry) {
5787 err = check_path_is_commitable(pe->path, &commitable_paths);
5788 if (err)
5789 goto done;
5792 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5793 struct got_commitable *ct = pe->data;
5794 const char *ct_path = ct->in_repo_path;
5796 while (ct_path[0] == '/')
5797 ct_path++;
5798 err = check_out_of_date(ct_path, ct->status,
5799 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5800 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5801 if (err)
5802 goto done;
5806 err = commit_worktree(new_commit_id, &commitable_paths,
5807 head_commit_id, NULL, worktree, author, committer,
5808 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5809 if (err)
5810 goto done;
5812 err = update_fileindex_after_commit(worktree, &commitable_paths,
5813 *new_commit_id, fileindex, have_staged_files);
5814 sync_err = sync_fileindex(fileindex, fileindex_path);
5815 if (sync_err && err == NULL)
5816 err = sync_err;
5817 done:
5818 if (fileindex)
5819 got_fileindex_free(fileindex);
5820 free(fileindex_path);
5821 unlockerr = lock_worktree(worktree, LOCK_SH);
5822 if (unlockerr && err == NULL)
5823 err = unlockerr;
5824 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5825 struct got_commitable *ct = pe->data;
5826 free_commitable(ct);
5828 got_pathlist_free(&commitable_paths);
5829 return err;
5832 const char *
5833 got_commitable_get_path(struct got_commitable *ct)
5835 return ct->path;
5838 unsigned int
5839 got_commitable_get_status(struct got_commitable *ct)
5841 return ct->status;
5844 struct check_rebase_ok_arg {
5845 struct got_worktree *worktree;
5846 struct got_repository *repo;
5849 static const struct got_error *
5850 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5852 const struct got_error *err = NULL;
5853 struct check_rebase_ok_arg *a = arg;
5854 unsigned char status;
5855 struct stat sb;
5856 char *ondisk_path;
5858 /* Reject rebase of a work tree with mixed base commits. */
5859 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5860 SHA1_DIGEST_LENGTH))
5861 return got_error(GOT_ERR_MIXED_COMMITS);
5863 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5864 == -1)
5865 return got_error_from_errno("asprintf");
5867 /* Reject rebase of a work tree with modified or staged files. */
5868 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5869 free(ondisk_path);
5870 if (err)
5871 return err;
5873 if (status != GOT_STATUS_NO_CHANGE)
5874 return got_error(GOT_ERR_MODIFIED);
5875 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5876 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5878 return NULL;
5881 const struct got_error *
5882 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5883 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5884 struct got_worktree *worktree, struct got_reference *branch,
5885 struct got_repository *repo)
5887 const struct got_error *err = NULL;
5888 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5889 char *branch_ref_name = NULL;
5890 char *fileindex_path = NULL;
5891 struct check_rebase_ok_arg ok_arg;
5892 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5893 struct got_object_id *wt_branch_tip = NULL;
5895 *new_base_branch_ref = NULL;
5896 *tmp_branch = NULL;
5897 *fileindex = NULL;
5899 err = lock_worktree(worktree, LOCK_EX);
5900 if (err)
5901 return err;
5903 err = open_fileindex(fileindex, &fileindex_path, worktree);
5904 if (err)
5905 goto done;
5907 ok_arg.worktree = worktree;
5908 ok_arg.repo = repo;
5909 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5910 &ok_arg);
5911 if (err)
5912 goto done;
5914 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5915 if (err)
5916 goto done;
5918 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5919 if (err)
5920 goto done;
5922 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5923 if (err)
5924 goto done;
5926 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5927 0);
5928 if (err)
5929 goto done;
5931 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5932 if (err)
5933 goto done;
5934 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5935 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5936 goto done;
5939 err = got_ref_alloc_symref(new_base_branch_ref,
5940 new_base_branch_ref_name, wt_branch);
5941 if (err)
5942 goto done;
5943 err = got_ref_write(*new_base_branch_ref, repo);
5944 if (err)
5945 goto done;
5947 /* TODO Lock original branch's ref while rebasing? */
5949 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5950 if (err)
5951 goto done;
5953 err = got_ref_write(branch_ref, repo);
5954 if (err)
5955 goto done;
5957 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5958 worktree->base_commit_id);
5959 if (err)
5960 goto done;
5961 err = got_ref_write(*tmp_branch, repo);
5962 if (err)
5963 goto done;
5965 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5966 if (err)
5967 goto done;
5968 done:
5969 free(fileindex_path);
5970 free(tmp_branch_name);
5971 free(new_base_branch_ref_name);
5972 free(branch_ref_name);
5973 if (branch_ref)
5974 got_ref_close(branch_ref);
5975 if (wt_branch)
5976 got_ref_close(wt_branch);
5977 free(wt_branch_tip);
5978 if (err) {
5979 if (*new_base_branch_ref) {
5980 got_ref_close(*new_base_branch_ref);
5981 *new_base_branch_ref = NULL;
5983 if (*tmp_branch) {
5984 got_ref_close(*tmp_branch);
5985 *tmp_branch = NULL;
5987 if (*fileindex) {
5988 got_fileindex_free(*fileindex);
5989 *fileindex = NULL;
5991 lock_worktree(worktree, LOCK_SH);
5993 return err;
5996 const struct got_error *
5997 got_worktree_rebase_continue(struct got_object_id **commit_id,
5998 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5999 struct got_reference **branch, struct got_fileindex **fileindex,
6000 struct got_worktree *worktree, struct got_repository *repo)
6002 const struct got_error *err;
6003 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6004 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6005 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6006 char *fileindex_path = NULL;
6007 int have_staged_files = 0;
6009 *commit_id = NULL;
6010 *new_base_branch = NULL;
6011 *tmp_branch = NULL;
6012 *branch = NULL;
6013 *fileindex = NULL;
6015 err = lock_worktree(worktree, LOCK_EX);
6016 if (err)
6017 return err;
6019 err = open_fileindex(fileindex, &fileindex_path, worktree);
6020 if (err)
6021 goto done;
6023 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6024 &have_staged_files);
6025 if (err && err->code != GOT_ERR_CANCELLED)
6026 goto done;
6027 if (have_staged_files) {
6028 err = got_error(GOT_ERR_STAGED_PATHS);
6029 goto done;
6032 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6033 if (err)
6034 goto done;
6036 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6037 if (err)
6038 goto done;
6040 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6041 if (err)
6042 goto done;
6044 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6045 if (err)
6046 goto done;
6048 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6049 if (err)
6050 goto done;
6052 err = got_ref_open(branch, repo,
6053 got_ref_get_symref_target(branch_ref), 0);
6054 if (err)
6055 goto done;
6057 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6058 if (err)
6059 goto done;
6061 err = got_ref_resolve(commit_id, repo, commit_ref);
6062 if (err)
6063 goto done;
6065 err = got_ref_open(new_base_branch, repo,
6066 new_base_branch_ref_name, 0);
6067 if (err)
6068 goto done;
6070 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6071 if (err)
6072 goto done;
6073 done:
6074 free(commit_ref_name);
6075 free(branch_ref_name);
6076 free(fileindex_path);
6077 if (commit_ref)
6078 got_ref_close(commit_ref);
6079 if (branch_ref)
6080 got_ref_close(branch_ref);
6081 if (err) {
6082 free(*commit_id);
6083 *commit_id = NULL;
6084 if (*tmp_branch) {
6085 got_ref_close(*tmp_branch);
6086 *tmp_branch = NULL;
6088 if (*new_base_branch) {
6089 got_ref_close(*new_base_branch);
6090 *new_base_branch = NULL;
6092 if (*branch) {
6093 got_ref_close(*branch);
6094 *branch = NULL;
6096 if (*fileindex) {
6097 got_fileindex_free(*fileindex);
6098 *fileindex = NULL;
6100 lock_worktree(worktree, LOCK_SH);
6102 return err;
6105 const struct got_error *
6106 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6108 const struct got_error *err;
6109 char *tmp_branch_name = NULL;
6111 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6112 if (err)
6113 return err;
6115 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6116 free(tmp_branch_name);
6117 return NULL;
6120 static const struct got_error *
6121 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6122 char **logmsg, void *arg)
6124 *logmsg = arg;
6125 return NULL;
6128 static const struct got_error *
6129 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6130 const char *path, struct got_object_id *blob_id,
6131 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6132 int dirfd, const char *de_name)
6134 return NULL;
6137 struct collect_merged_paths_arg {
6138 got_worktree_checkout_cb progress_cb;
6139 void *progress_arg;
6140 struct got_pathlist_head *merged_paths;
6143 static const struct got_error *
6144 collect_merged_paths(void *arg, unsigned char status, const char *path)
6146 const struct got_error *err;
6147 struct collect_merged_paths_arg *a = arg;
6148 char *p;
6149 struct got_pathlist_entry *new;
6151 err = (*a->progress_cb)(a->progress_arg, status, path);
6152 if (err)
6153 return err;
6155 if (status != GOT_STATUS_MERGE &&
6156 status != GOT_STATUS_ADD &&
6157 status != GOT_STATUS_DELETE &&
6158 status != GOT_STATUS_CONFLICT)
6159 return NULL;
6161 p = strdup(path);
6162 if (p == NULL)
6163 return got_error_from_errno("strdup");
6165 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6166 if (err || new == NULL)
6167 free(p);
6168 return err;
6171 void
6172 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6174 struct got_pathlist_entry *pe;
6176 TAILQ_FOREACH(pe, merged_paths, entry)
6177 free((char *)pe->path);
6179 got_pathlist_free(merged_paths);
6182 static const struct got_error *
6183 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6184 int is_rebase, struct got_repository *repo)
6186 const struct got_error *err;
6187 struct got_reference *commit_ref = NULL;
6189 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6190 if (err) {
6191 if (err->code != GOT_ERR_NOT_REF)
6192 goto done;
6193 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6194 if (err)
6195 goto done;
6196 err = got_ref_write(commit_ref, repo);
6197 if (err)
6198 goto done;
6199 } else if (is_rebase) {
6200 struct got_object_id *stored_id;
6201 int cmp;
6203 err = got_ref_resolve(&stored_id, repo, commit_ref);
6204 if (err)
6205 goto done;
6206 cmp = got_object_id_cmp(commit_id, stored_id);
6207 free(stored_id);
6208 if (cmp != 0) {
6209 err = got_error(GOT_ERR_REBASE_COMMITID);
6210 goto done;
6213 done:
6214 if (commit_ref)
6215 got_ref_close(commit_ref);
6216 return err;
6219 static const struct got_error *
6220 rebase_merge_files(struct got_pathlist_head *merged_paths,
6221 const char *commit_ref_name, struct got_worktree *worktree,
6222 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6223 struct got_object_id *commit_id, struct got_repository *repo,
6224 got_worktree_checkout_cb progress_cb, void *progress_arg,
6225 got_cancel_cb cancel_cb, void *cancel_arg)
6227 const struct got_error *err;
6228 struct got_reference *commit_ref = NULL;
6229 struct collect_merged_paths_arg cmp_arg;
6230 char *fileindex_path;
6232 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6234 err = get_fileindex_path(&fileindex_path, worktree);
6235 if (err)
6236 return err;
6238 cmp_arg.progress_cb = progress_cb;
6239 cmp_arg.progress_arg = progress_arg;
6240 cmp_arg.merged_paths = merged_paths;
6241 err = merge_files(worktree, fileindex, fileindex_path,
6242 parent_commit_id, commit_id, repo, collect_merged_paths,
6243 &cmp_arg, cancel_cb, cancel_arg);
6244 if (commit_ref)
6245 got_ref_close(commit_ref);
6246 return err;
6249 const struct got_error *
6250 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6251 struct got_worktree *worktree, struct got_fileindex *fileindex,
6252 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6253 struct got_repository *repo,
6254 got_worktree_checkout_cb progress_cb, void *progress_arg,
6255 got_cancel_cb cancel_cb, void *cancel_arg)
6257 const struct got_error *err;
6258 char *commit_ref_name;
6260 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6261 if (err)
6262 return err;
6264 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6265 if (err)
6266 goto done;
6268 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6269 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6270 progress_arg, cancel_cb, cancel_arg);
6271 done:
6272 free(commit_ref_name);
6273 return err;
6276 const struct got_error *
6277 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6278 struct got_worktree *worktree, struct got_fileindex *fileindex,
6279 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6280 struct got_repository *repo,
6281 got_worktree_checkout_cb progress_cb, void *progress_arg,
6282 got_cancel_cb cancel_cb, void *cancel_arg)
6284 const struct got_error *err;
6285 char *commit_ref_name;
6287 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6288 if (err)
6289 return err;
6291 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6292 if (err)
6293 goto done;
6295 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6296 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6297 progress_arg, cancel_cb, cancel_arg);
6298 done:
6299 free(commit_ref_name);
6300 return err;
6303 static const struct got_error *
6304 rebase_commit(struct got_object_id **new_commit_id,
6305 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6306 struct got_worktree *worktree, struct got_fileindex *fileindex,
6307 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6308 const char *new_logmsg, struct got_repository *repo)
6310 const struct got_error *err, *sync_err;
6311 struct got_pathlist_head commitable_paths;
6312 struct collect_commitables_arg cc_arg;
6313 char *fileindex_path = NULL;
6314 struct got_reference *head_ref = NULL;
6315 struct got_object_id *head_commit_id = NULL;
6316 char *logmsg = NULL;
6318 TAILQ_INIT(&commitable_paths);
6319 *new_commit_id = NULL;
6321 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6323 err = get_fileindex_path(&fileindex_path, worktree);
6324 if (err)
6325 return err;
6327 cc_arg.commitable_paths = &commitable_paths;
6328 cc_arg.worktree = worktree;
6329 cc_arg.repo = repo;
6330 cc_arg.have_staged_files = 0;
6332 * If possible get the status of individual files directly to
6333 * avoid crawling the entire work tree once per rebased commit.
6335 * Ideally, merged_paths would contain a list of commitables
6336 * we could use so we could skip worktree_status() entirely.
6337 * However, we would then need carefully keep track of cumulative
6338 * effects of operations such as file additions and deletions
6339 * in 'got histedit -f' (folding multiple commits into one),
6340 * and this extra complexity is not really worth it.
6342 if (merged_paths) {
6343 struct got_pathlist_entry *pe;
6344 TAILQ_FOREACH(pe, merged_paths, entry) {
6345 err = worktree_status(worktree, pe->path, fileindex,
6346 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6347 0);
6348 if (err)
6349 goto done;
6351 } else {
6352 err = worktree_status(worktree, "", fileindex, repo,
6353 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6354 if (err)
6355 goto done;
6358 if (TAILQ_EMPTY(&commitable_paths)) {
6359 /* No-op change; commit will be elided. */
6360 err = got_ref_delete(commit_ref, repo);
6361 if (err)
6362 goto done;
6363 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6364 goto done;
6367 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6368 if (err)
6369 goto done;
6371 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6372 if (err)
6373 goto done;
6375 if (new_logmsg) {
6376 logmsg = strdup(new_logmsg);
6377 if (logmsg == NULL) {
6378 err = got_error_from_errno("strdup");
6379 goto done;
6381 } else {
6382 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6383 if (err)
6384 goto done;
6387 /* NB: commit_worktree will call free(logmsg) */
6388 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6389 NULL, worktree, got_object_commit_get_author(orig_commit),
6390 got_object_commit_get_committer(orig_commit),
6391 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6392 if (err)
6393 goto done;
6395 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6396 if (err)
6397 goto done;
6399 err = got_ref_delete(commit_ref, repo);
6400 if (err)
6401 goto done;
6403 err = update_fileindex_after_commit(worktree, &commitable_paths,
6404 *new_commit_id, fileindex, 0);
6405 sync_err = sync_fileindex(fileindex, fileindex_path);
6406 if (sync_err && err == NULL)
6407 err = sync_err;
6408 done:
6409 free(fileindex_path);
6410 free(head_commit_id);
6411 if (head_ref)
6412 got_ref_close(head_ref);
6413 if (err) {
6414 free(*new_commit_id);
6415 *new_commit_id = NULL;
6417 return err;
6420 const struct got_error *
6421 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6422 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6423 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6424 struct got_commit_object *orig_commit,
6425 struct got_object_id *orig_commit_id, struct got_repository *repo)
6427 const struct got_error *err;
6428 char *commit_ref_name;
6429 struct got_reference *commit_ref = NULL;
6430 struct got_object_id *commit_id = NULL;
6432 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6433 if (err)
6434 return err;
6436 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6437 if (err)
6438 goto done;
6439 err = got_ref_resolve(&commit_id, repo, commit_ref);
6440 if (err)
6441 goto done;
6442 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6443 err = got_error(GOT_ERR_REBASE_COMMITID);
6444 goto done;
6447 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6448 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6449 done:
6450 if (commit_ref)
6451 got_ref_close(commit_ref);
6452 free(commit_ref_name);
6453 free(commit_id);
6454 return err;
6457 const struct got_error *
6458 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6459 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6460 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6461 struct got_commit_object *orig_commit,
6462 struct got_object_id *orig_commit_id, const char *new_logmsg,
6463 struct got_repository *repo)
6465 const struct got_error *err;
6466 char *commit_ref_name;
6467 struct got_reference *commit_ref = NULL;
6469 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6470 if (err)
6471 return err;
6473 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6474 if (err)
6475 goto done;
6477 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6478 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6479 done:
6480 if (commit_ref)
6481 got_ref_close(commit_ref);
6482 free(commit_ref_name);
6483 return err;
6486 const struct got_error *
6487 got_worktree_rebase_postpone(struct got_worktree *worktree,
6488 struct got_fileindex *fileindex)
6490 if (fileindex)
6491 got_fileindex_free(fileindex);
6492 return lock_worktree(worktree, LOCK_SH);
6495 static const struct got_error *
6496 delete_ref(const char *name, struct got_repository *repo)
6498 const struct got_error *err;
6499 struct got_reference *ref;
6501 err = got_ref_open(&ref, repo, name, 0);
6502 if (err) {
6503 if (err->code == GOT_ERR_NOT_REF)
6504 return NULL;
6505 return err;
6508 err = got_ref_delete(ref, repo);
6509 got_ref_close(ref);
6510 return err;
6513 static const struct got_error *
6514 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6516 const struct got_error *err;
6517 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6518 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6520 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6521 if (err)
6522 goto done;
6523 err = delete_ref(tmp_branch_name, repo);
6524 if (err)
6525 goto done;
6527 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6528 if (err)
6529 goto done;
6530 err = delete_ref(new_base_branch_ref_name, repo);
6531 if (err)
6532 goto done;
6534 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6535 if (err)
6536 goto done;
6537 err = delete_ref(branch_ref_name, repo);
6538 if (err)
6539 goto done;
6541 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6542 if (err)
6543 goto done;
6544 err = delete_ref(commit_ref_name, repo);
6545 if (err)
6546 goto done;
6548 done:
6549 free(tmp_branch_name);
6550 free(new_base_branch_ref_name);
6551 free(branch_ref_name);
6552 free(commit_ref_name);
6553 return err;
6556 const struct got_error *
6557 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6558 struct got_object_id *new_commit_id, struct got_repository *repo)
6560 const struct got_error *err;
6561 struct got_reference *ref = NULL;
6562 struct got_object_id *old_commit_id = NULL;
6563 const char *branch_name = NULL;
6564 char *new_id_str = NULL;
6565 char *refname = NULL;
6567 branch_name = got_ref_get_name(branch);
6568 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6569 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6570 branch_name += 11;
6572 err = got_object_id_str(&new_id_str, new_commit_id);
6573 if (err)
6574 return err;
6576 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6577 new_id_str) == -1) {
6578 err = got_error_from_errno("asprintf");
6579 goto done;
6582 err = got_ref_resolve(&old_commit_id, repo, branch);
6583 if (err)
6584 goto done;
6586 err = got_ref_alloc(&ref, refname, old_commit_id);
6587 if (err)
6588 goto done;
6590 err = got_ref_write(ref, repo);
6591 done:
6592 free(new_id_str);
6593 free(refname);
6594 free(old_commit_id);
6595 if (ref)
6596 got_ref_close(ref);
6597 return err;
6600 const struct got_error *
6601 got_worktree_rebase_complete(struct got_worktree *worktree,
6602 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6603 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6604 struct got_repository *repo, int create_backup)
6606 const struct got_error *err, *unlockerr, *sync_err;
6607 struct got_object_id *new_head_commit_id = NULL;
6608 char *fileindex_path = NULL;
6610 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6611 if (err)
6612 return err;
6614 if (create_backup) {
6615 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6616 rebased_branch, new_head_commit_id, repo);
6617 if (err)
6618 goto done;
6621 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6622 if (err)
6623 goto done;
6625 err = got_ref_write(rebased_branch, repo);
6626 if (err)
6627 goto done;
6629 err = got_worktree_set_head_ref(worktree, rebased_branch);
6630 if (err)
6631 goto done;
6633 err = delete_rebase_refs(worktree, repo);
6634 if (err)
6635 goto done;
6637 err = get_fileindex_path(&fileindex_path, worktree);
6638 if (err)
6639 goto done;
6640 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6641 sync_err = sync_fileindex(fileindex, fileindex_path);
6642 if (sync_err && err == NULL)
6643 err = sync_err;
6644 done:
6645 got_fileindex_free(fileindex);
6646 free(fileindex_path);
6647 free(new_head_commit_id);
6648 unlockerr = lock_worktree(worktree, LOCK_SH);
6649 if (unlockerr && err == NULL)
6650 err = unlockerr;
6651 return err;
6654 const struct got_error *
6655 got_worktree_rebase_abort(struct got_worktree *worktree,
6656 struct got_fileindex *fileindex, struct got_repository *repo,
6657 struct got_reference *new_base_branch,
6658 got_worktree_checkout_cb progress_cb, void *progress_arg)
6660 const struct got_error *err, *unlockerr, *sync_err;
6661 struct got_reference *resolved = NULL;
6662 struct got_object_id *commit_id = NULL;
6663 char *fileindex_path = NULL;
6664 struct revert_file_args rfa;
6665 struct got_object_id *tree_id = NULL;
6667 err = lock_worktree(worktree, LOCK_EX);
6668 if (err)
6669 return err;
6671 err = got_ref_open(&resolved, repo,
6672 got_ref_get_symref_target(new_base_branch), 0);
6673 if (err)
6674 goto done;
6676 err = got_worktree_set_head_ref(worktree, resolved);
6677 if (err)
6678 goto done;
6681 * XXX commits to the base branch could have happened while
6682 * we were busy rebasing; should we store the original commit ID
6683 * when rebase begins and read it back here?
6685 err = got_ref_resolve(&commit_id, repo, resolved);
6686 if (err)
6687 goto done;
6689 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6690 if (err)
6691 goto done;
6693 err = got_object_id_by_path(&tree_id, repo,
6694 worktree->base_commit_id, worktree->path_prefix);
6695 if (err)
6696 goto done;
6698 err = delete_rebase_refs(worktree, repo);
6699 if (err)
6700 goto done;
6702 err = get_fileindex_path(&fileindex_path, worktree);
6703 if (err)
6704 goto done;
6706 rfa.worktree = worktree;
6707 rfa.fileindex = fileindex;
6708 rfa.progress_cb = progress_cb;
6709 rfa.progress_arg = progress_arg;
6710 rfa.patch_cb = NULL;
6711 rfa.patch_arg = NULL;
6712 rfa.repo = repo;
6713 rfa.unlink_added_files = 0;
6714 err = worktree_status(worktree, "", fileindex, repo,
6715 revert_file, &rfa, NULL, NULL, 1, 0);
6716 if (err)
6717 goto sync;
6719 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6720 repo, progress_cb, progress_arg, NULL, NULL);
6721 sync:
6722 sync_err = sync_fileindex(fileindex, fileindex_path);
6723 if (sync_err && err == NULL)
6724 err = sync_err;
6725 done:
6726 got_ref_close(resolved);
6727 free(tree_id);
6728 free(commit_id);
6729 if (fileindex)
6730 got_fileindex_free(fileindex);
6731 free(fileindex_path);
6733 unlockerr = lock_worktree(worktree, LOCK_SH);
6734 if (unlockerr && err == NULL)
6735 err = unlockerr;
6736 return err;
6739 const struct got_error *
6740 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6741 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6742 struct got_fileindex **fileindex, struct got_worktree *worktree,
6743 struct got_repository *repo)
6745 const struct got_error *err = NULL;
6746 char *tmp_branch_name = NULL;
6747 char *branch_ref_name = NULL;
6748 char *base_commit_ref_name = NULL;
6749 char *fileindex_path = NULL;
6750 struct check_rebase_ok_arg ok_arg;
6751 struct got_reference *wt_branch = NULL;
6752 struct got_reference *base_commit_ref = NULL;
6754 *tmp_branch = NULL;
6755 *branch_ref = NULL;
6756 *base_commit_id = NULL;
6757 *fileindex = NULL;
6759 err = lock_worktree(worktree, LOCK_EX);
6760 if (err)
6761 return err;
6763 err = open_fileindex(fileindex, &fileindex_path, worktree);
6764 if (err)
6765 goto done;
6767 ok_arg.worktree = worktree;
6768 ok_arg.repo = repo;
6769 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6770 &ok_arg);
6771 if (err)
6772 goto done;
6774 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6775 if (err)
6776 goto done;
6778 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6779 if (err)
6780 goto done;
6782 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6783 worktree);
6784 if (err)
6785 goto done;
6787 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6788 0);
6789 if (err)
6790 goto done;
6792 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6793 if (err)
6794 goto done;
6796 err = got_ref_write(*branch_ref, repo);
6797 if (err)
6798 goto done;
6800 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6801 worktree->base_commit_id);
6802 if (err)
6803 goto done;
6804 err = got_ref_write(base_commit_ref, repo);
6805 if (err)
6806 goto done;
6807 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6808 if (*base_commit_id == NULL) {
6809 err = got_error_from_errno("got_object_id_dup");
6810 goto done;
6813 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6814 worktree->base_commit_id);
6815 if (err)
6816 goto done;
6817 err = got_ref_write(*tmp_branch, repo);
6818 if (err)
6819 goto done;
6821 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6822 if (err)
6823 goto done;
6824 done:
6825 free(fileindex_path);
6826 free(tmp_branch_name);
6827 free(branch_ref_name);
6828 free(base_commit_ref_name);
6829 if (wt_branch)
6830 got_ref_close(wt_branch);
6831 if (err) {
6832 if (*branch_ref) {
6833 got_ref_close(*branch_ref);
6834 *branch_ref = NULL;
6836 if (*tmp_branch) {
6837 got_ref_close(*tmp_branch);
6838 *tmp_branch = NULL;
6840 free(*base_commit_id);
6841 if (*fileindex) {
6842 got_fileindex_free(*fileindex);
6843 *fileindex = NULL;
6845 lock_worktree(worktree, LOCK_SH);
6847 return err;
6850 const struct got_error *
6851 got_worktree_histedit_postpone(struct got_worktree *worktree,
6852 struct got_fileindex *fileindex)
6854 if (fileindex)
6855 got_fileindex_free(fileindex);
6856 return lock_worktree(worktree, LOCK_SH);
6859 const struct got_error *
6860 got_worktree_histedit_in_progress(int *in_progress,
6861 struct got_worktree *worktree)
6863 const struct got_error *err;
6864 char *tmp_branch_name = NULL;
6866 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6867 if (err)
6868 return err;
6870 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6871 free(tmp_branch_name);
6872 return NULL;
6875 const struct got_error *
6876 got_worktree_histedit_continue(struct got_object_id **commit_id,
6877 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6878 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6879 struct got_worktree *worktree, struct got_repository *repo)
6881 const struct got_error *err;
6882 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6883 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6884 struct got_reference *commit_ref = NULL;
6885 struct got_reference *base_commit_ref = NULL;
6886 char *fileindex_path = NULL;
6887 int have_staged_files = 0;
6889 *commit_id = NULL;
6890 *tmp_branch = NULL;
6891 *base_commit_id = NULL;
6892 *fileindex = NULL;
6894 err = lock_worktree(worktree, LOCK_EX);
6895 if (err)
6896 return err;
6898 err = open_fileindex(fileindex, &fileindex_path, worktree);
6899 if (err)
6900 goto done;
6902 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6903 &have_staged_files);
6904 if (err && err->code != GOT_ERR_CANCELLED)
6905 goto done;
6906 if (have_staged_files) {
6907 err = got_error(GOT_ERR_STAGED_PATHS);
6908 goto done;
6911 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6912 if (err)
6913 goto done;
6915 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6916 if (err)
6917 goto done;
6919 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6920 if (err)
6921 goto done;
6923 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6924 worktree);
6925 if (err)
6926 goto done;
6928 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6929 if (err)
6930 goto done;
6932 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6933 if (err)
6934 goto done;
6935 err = got_ref_resolve(commit_id, repo, commit_ref);
6936 if (err)
6937 goto done;
6939 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6940 if (err)
6941 goto done;
6942 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6943 if (err)
6944 goto done;
6946 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6947 if (err)
6948 goto done;
6949 done:
6950 free(commit_ref_name);
6951 free(branch_ref_name);
6952 free(fileindex_path);
6953 if (commit_ref)
6954 got_ref_close(commit_ref);
6955 if (base_commit_ref)
6956 got_ref_close(base_commit_ref);
6957 if (err) {
6958 free(*commit_id);
6959 *commit_id = NULL;
6960 free(*base_commit_id);
6961 *base_commit_id = NULL;
6962 if (*tmp_branch) {
6963 got_ref_close(*tmp_branch);
6964 *tmp_branch = NULL;
6966 if (*fileindex) {
6967 got_fileindex_free(*fileindex);
6968 *fileindex = NULL;
6970 lock_worktree(worktree, LOCK_EX);
6972 return err;
6975 static const struct got_error *
6976 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6978 const struct got_error *err;
6979 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6980 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6982 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6983 if (err)
6984 goto done;
6985 err = delete_ref(tmp_branch_name, repo);
6986 if (err)
6987 goto done;
6989 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6990 worktree);
6991 if (err)
6992 goto done;
6993 err = delete_ref(base_commit_ref_name, repo);
6994 if (err)
6995 goto done;
6997 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6998 if (err)
6999 goto done;
7000 err = delete_ref(branch_ref_name, repo);
7001 if (err)
7002 goto done;
7004 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7005 if (err)
7006 goto done;
7007 err = delete_ref(commit_ref_name, repo);
7008 if (err)
7009 goto done;
7010 done:
7011 free(tmp_branch_name);
7012 free(base_commit_ref_name);
7013 free(branch_ref_name);
7014 free(commit_ref_name);
7015 return err;
7018 const struct got_error *
7019 got_worktree_histedit_abort(struct got_worktree *worktree,
7020 struct got_fileindex *fileindex, struct got_repository *repo,
7021 struct got_reference *branch, struct got_object_id *base_commit_id,
7022 got_worktree_checkout_cb progress_cb, void *progress_arg)
7024 const struct got_error *err, *unlockerr, *sync_err;
7025 struct got_reference *resolved = NULL;
7026 char *fileindex_path = NULL;
7027 struct got_object_id *tree_id = NULL;
7028 struct revert_file_args rfa;
7030 err = lock_worktree(worktree, LOCK_EX);
7031 if (err)
7032 return err;
7034 err = got_ref_open(&resolved, repo,
7035 got_ref_get_symref_target(branch), 0);
7036 if (err)
7037 goto done;
7039 err = got_worktree_set_head_ref(worktree, resolved);
7040 if (err)
7041 goto done;
7043 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7044 if (err)
7045 goto done;
7047 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7048 worktree->path_prefix);
7049 if (err)
7050 goto done;
7052 err = delete_histedit_refs(worktree, repo);
7053 if (err)
7054 goto done;
7056 err = get_fileindex_path(&fileindex_path, worktree);
7057 if (err)
7058 goto done;
7060 rfa.worktree = worktree;
7061 rfa.fileindex = fileindex;
7062 rfa.progress_cb = progress_cb;
7063 rfa.progress_arg = progress_arg;
7064 rfa.patch_cb = NULL;
7065 rfa.patch_arg = NULL;
7066 rfa.repo = repo;
7067 rfa.unlink_added_files = 0;
7068 err = worktree_status(worktree, "", fileindex, repo,
7069 revert_file, &rfa, NULL, NULL, 1, 0);
7070 if (err)
7071 goto sync;
7073 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7074 repo, progress_cb, progress_arg, NULL, NULL);
7075 sync:
7076 sync_err = sync_fileindex(fileindex, fileindex_path);
7077 if (sync_err && err == NULL)
7078 err = sync_err;
7079 done:
7080 got_ref_close(resolved);
7081 free(tree_id);
7082 free(fileindex_path);
7084 unlockerr = lock_worktree(worktree, LOCK_SH);
7085 if (unlockerr && err == NULL)
7086 err = unlockerr;
7087 return err;
7090 const struct got_error *
7091 got_worktree_histedit_complete(struct got_worktree *worktree,
7092 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7093 struct got_reference *edited_branch, struct got_repository *repo)
7095 const struct got_error *err, *unlockerr, *sync_err;
7096 struct got_object_id *new_head_commit_id = NULL;
7097 struct got_reference *resolved = NULL;
7098 char *fileindex_path = NULL;
7100 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7101 if (err)
7102 return err;
7104 err = got_ref_open(&resolved, repo,
7105 got_ref_get_symref_target(edited_branch), 0);
7106 if (err)
7107 goto done;
7109 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7110 resolved, new_head_commit_id, repo);
7111 if (err)
7112 goto done;
7114 err = got_ref_change_ref(resolved, new_head_commit_id);
7115 if (err)
7116 goto done;
7118 err = got_ref_write(resolved, repo);
7119 if (err)
7120 goto done;
7122 err = got_worktree_set_head_ref(worktree, resolved);
7123 if (err)
7124 goto done;
7126 err = delete_histedit_refs(worktree, repo);
7127 if (err)
7128 goto done;
7130 err = get_fileindex_path(&fileindex_path, worktree);
7131 if (err)
7132 goto done;
7133 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7134 sync_err = sync_fileindex(fileindex, fileindex_path);
7135 if (sync_err && err == NULL)
7136 err = sync_err;
7137 done:
7138 got_fileindex_free(fileindex);
7139 free(fileindex_path);
7140 free(new_head_commit_id);
7141 unlockerr = lock_worktree(worktree, LOCK_SH);
7142 if (unlockerr && err == NULL)
7143 err = unlockerr;
7144 return err;
7147 const struct got_error *
7148 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7149 struct got_object_id *commit_id, struct got_repository *repo)
7151 const struct got_error *err;
7152 char *commit_ref_name;
7154 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7155 if (err)
7156 return err;
7158 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7159 if (err)
7160 goto done;
7162 err = delete_ref(commit_ref_name, repo);
7163 done:
7164 free(commit_ref_name);
7165 return err;
7168 const struct got_error *
7169 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7170 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7171 struct got_worktree *worktree, const char *refname,
7172 struct got_repository *repo)
7174 const struct got_error *err = NULL;
7175 char *fileindex_path = NULL;
7176 struct check_rebase_ok_arg ok_arg;
7178 *fileindex = NULL;
7179 *branch_ref = NULL;
7180 *base_branch_ref = NULL;
7182 err = lock_worktree(worktree, LOCK_EX);
7183 if (err)
7184 return err;
7186 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7187 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7188 "cannot integrate a branch into itself; "
7189 "update -b or different branch name required");
7190 goto done;
7193 err = open_fileindex(fileindex, &fileindex_path, worktree);
7194 if (err)
7195 goto done;
7197 /* Preconditions are the same as for rebase. */
7198 ok_arg.worktree = worktree;
7199 ok_arg.repo = repo;
7200 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7201 &ok_arg);
7202 if (err)
7203 goto done;
7205 err = got_ref_open(branch_ref, repo, refname, 1);
7206 if (err)
7207 goto done;
7209 err = got_ref_open(base_branch_ref, repo,
7210 got_worktree_get_head_ref_name(worktree), 1);
7211 done:
7212 if (err) {
7213 if (*branch_ref) {
7214 got_ref_close(*branch_ref);
7215 *branch_ref = NULL;
7217 if (*base_branch_ref) {
7218 got_ref_close(*base_branch_ref);
7219 *base_branch_ref = NULL;
7221 if (*fileindex) {
7222 got_fileindex_free(*fileindex);
7223 *fileindex = NULL;
7225 lock_worktree(worktree, LOCK_SH);
7227 return err;
7230 const struct got_error *
7231 got_worktree_integrate_continue(struct got_worktree *worktree,
7232 struct got_fileindex *fileindex, struct got_repository *repo,
7233 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7234 got_worktree_checkout_cb progress_cb, void *progress_arg,
7235 got_cancel_cb cancel_cb, void *cancel_arg)
7237 const struct got_error *err = NULL, *sync_err, *unlockerr;
7238 char *fileindex_path = NULL;
7239 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7241 err = get_fileindex_path(&fileindex_path, worktree);
7242 if (err)
7243 goto done;
7245 err = got_ref_resolve(&commit_id, repo, branch_ref);
7246 if (err)
7247 goto done;
7249 err = got_object_id_by_path(&tree_id, repo, commit_id,
7250 worktree->path_prefix);
7251 if (err)
7252 goto done;
7254 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7255 if (err)
7256 goto done;
7258 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7259 progress_cb, progress_arg, cancel_cb, cancel_arg);
7260 if (err)
7261 goto sync;
7263 err = got_ref_change_ref(base_branch_ref, commit_id);
7264 if (err)
7265 goto sync;
7267 err = got_ref_write(base_branch_ref, repo);
7268 if (err)
7269 goto sync;
7271 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7272 sync:
7273 sync_err = sync_fileindex(fileindex, fileindex_path);
7274 if (sync_err && err == NULL)
7275 err = sync_err;
7277 done:
7278 unlockerr = got_ref_unlock(branch_ref);
7279 if (unlockerr && err == NULL)
7280 err = unlockerr;
7281 got_ref_close(branch_ref);
7283 unlockerr = got_ref_unlock(base_branch_ref);
7284 if (unlockerr && err == NULL)
7285 err = unlockerr;
7286 got_ref_close(base_branch_ref);
7288 got_fileindex_free(fileindex);
7289 free(fileindex_path);
7290 free(tree_id);
7292 unlockerr = lock_worktree(worktree, LOCK_SH);
7293 if (unlockerr && err == NULL)
7294 err = unlockerr;
7295 return err;
7298 const struct got_error *
7299 got_worktree_integrate_abort(struct got_worktree *worktree,
7300 struct got_fileindex *fileindex, struct got_repository *repo,
7301 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7303 const struct got_error *err = NULL, *unlockerr = NULL;
7305 got_fileindex_free(fileindex);
7307 err = lock_worktree(worktree, LOCK_SH);
7309 unlockerr = got_ref_unlock(branch_ref);
7310 if (unlockerr && err == NULL)
7311 err = unlockerr;
7312 got_ref_close(branch_ref);
7314 unlockerr = got_ref_unlock(base_branch_ref);
7315 if (unlockerr && err == NULL)
7316 err = unlockerr;
7317 got_ref_close(base_branch_ref);
7319 return err;
7322 const struct got_error *
7323 got_worktree_merge_postpone(struct got_worktree *worktree,
7324 struct got_fileindex *fileindex)
7326 const struct got_error *err, *sync_err;
7327 char *fileindex_path = NULL;
7329 err = get_fileindex_path(&fileindex_path, worktree);
7330 if (err)
7331 goto done;
7333 sync_err = sync_fileindex(fileindex, fileindex_path);
7335 err = lock_worktree(worktree, LOCK_SH);
7336 if (sync_err && err == NULL)
7337 err = sync_err;
7338 done:
7339 got_fileindex_free(fileindex);
7340 free(fileindex_path);
7341 return err;
7344 static const struct got_error *
7345 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7347 const struct got_error *err;
7348 char *branch_refname = NULL, *commit_refname = NULL;
7350 err = get_merge_branch_ref_name(&branch_refname, worktree);
7351 if (err)
7352 goto done;
7353 err = delete_ref(branch_refname, repo);
7354 if (err)
7355 goto done;
7357 err = get_merge_commit_ref_name(&commit_refname, worktree);
7358 if (err)
7359 goto done;
7360 err = delete_ref(commit_refname, repo);
7361 if (err)
7362 goto done;
7364 done:
7365 free(branch_refname);
7366 free(commit_refname);
7367 return err;
7370 struct merge_commit_msg_arg {
7371 struct got_worktree *worktree;
7372 const char *branch_name;
7375 static const struct got_error *
7376 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7377 void *arg)
7379 struct merge_commit_msg_arg *a = arg;
7381 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7382 got_worktree_get_head_ref_name(a->worktree)) == -1)
7383 return got_error_from_errno("asprintf");
7385 return NULL;
7389 const struct got_error *
7390 got_worktree_merge_branch(struct got_worktree *worktree,
7391 struct got_fileindex *fileindex,
7392 struct got_object_id *yca_commit_id,
7393 struct got_object_id *branch_tip,
7394 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7395 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7397 const struct got_error *err;
7398 char *fileindex_path = NULL;
7400 err = get_fileindex_path(&fileindex_path, worktree);
7401 if (err)
7402 goto done;
7404 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7405 worktree);
7406 if (err)
7407 goto done;
7409 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7410 branch_tip, repo, progress_cb, progress_arg,
7411 cancel_cb, cancel_arg);
7412 done:
7413 free(fileindex_path);
7414 return err;
7417 const struct got_error *
7418 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7419 struct got_worktree *worktree, struct got_fileindex *fileindex,
7420 const char *author, const char *committer, int allow_bad_symlinks,
7421 struct got_object_id *branch_tip, const char *branch_name,
7422 struct got_repository *repo,
7423 got_worktree_status_cb status_cb, void *status_arg)
7426 const struct got_error *err = NULL, *sync_err;
7427 struct got_pathlist_head commitable_paths;
7428 struct collect_commitables_arg cc_arg;
7429 struct got_pathlist_entry *pe;
7430 struct got_reference *head_ref = NULL;
7431 struct got_object_id *head_commit_id = NULL;
7432 int have_staged_files = 0;
7433 struct merge_commit_msg_arg mcm_arg;
7434 char *fileindex_path = NULL;
7436 *new_commit_id = NULL;
7438 TAILQ_INIT(&commitable_paths);
7440 err = get_fileindex_path(&fileindex_path, worktree);
7441 if (err)
7442 goto done;
7444 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7445 if (err)
7446 goto done;
7448 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7449 if (err)
7450 goto done;
7452 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7453 &have_staged_files);
7454 if (err && err->code != GOT_ERR_CANCELLED)
7455 goto done;
7456 if (have_staged_files) {
7457 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7458 goto done;
7461 cc_arg.commitable_paths = &commitable_paths;
7462 cc_arg.worktree = worktree;
7463 cc_arg.fileindex = fileindex;
7464 cc_arg.repo = repo;
7465 cc_arg.have_staged_files = have_staged_files;
7466 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7467 err = worktree_status(worktree, "", fileindex, repo,
7468 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7469 if (err)
7470 goto done;
7472 if (TAILQ_EMPTY(&commitable_paths)) {
7473 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7474 "merge of %s cannot proceed", branch_name);
7475 goto done;
7478 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7479 struct got_commitable *ct = pe->data;
7480 const char *ct_path = ct->in_repo_path;
7482 while (ct_path[0] == '/')
7483 ct_path++;
7484 err = check_out_of_date(ct_path, ct->status,
7485 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7486 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7487 if (err)
7488 goto done;
7492 mcm_arg.worktree = worktree;
7493 mcm_arg.branch_name = branch_name;
7494 err = commit_worktree(new_commit_id, &commitable_paths,
7495 head_commit_id, branch_tip, worktree, author, committer,
7496 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7497 if (err)
7498 goto done;
7500 err = update_fileindex_after_commit(worktree, &commitable_paths,
7501 *new_commit_id, fileindex, have_staged_files);
7502 sync_err = sync_fileindex(fileindex, fileindex_path);
7503 if (sync_err && err == NULL)
7504 err = sync_err;
7505 done:
7506 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7507 struct got_commitable *ct = pe->data;
7508 free_commitable(ct);
7510 got_pathlist_free(&commitable_paths);
7511 free(fileindex_path);
7512 return err;
7515 const struct got_error *
7516 got_worktree_merge_complete(struct got_worktree *worktree,
7517 struct got_fileindex *fileindex, struct got_repository *repo)
7519 const struct got_error *err, *unlockerr, *sync_err;
7520 char *fileindex_path = NULL;
7522 err = delete_merge_refs(worktree, repo);
7523 if (err)
7524 goto done;
7526 err = get_fileindex_path(&fileindex_path, worktree);
7527 if (err)
7528 goto done;
7529 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7530 sync_err = sync_fileindex(fileindex, fileindex_path);
7531 if (sync_err && err == NULL)
7532 err = sync_err;
7533 done:
7534 got_fileindex_free(fileindex);
7535 free(fileindex_path);
7536 unlockerr = lock_worktree(worktree, LOCK_SH);
7537 if (unlockerr && err == NULL)
7538 err = unlockerr;
7539 return err;
7542 const struct got_error *
7543 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7544 struct got_repository *repo)
7546 const struct got_error *err;
7547 char *branch_refname = NULL;
7548 struct got_reference *branch_ref = NULL;
7550 *in_progress = 0;
7552 err = get_merge_branch_ref_name(&branch_refname, worktree);
7553 if (err)
7554 return err;
7555 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7556 free(branch_refname);
7557 if (err) {
7558 if (err->code != GOT_ERR_NOT_REF)
7559 return err;
7560 } else
7561 *in_progress = 1;
7563 return NULL;
7566 const struct got_error *got_worktree_merge_prepare(
7567 struct got_fileindex **fileindex, struct got_worktree *worktree,
7568 struct got_reference *branch, struct got_repository *repo)
7570 const struct got_error *err = NULL;
7571 char *fileindex_path = NULL;
7572 char *branch_refname = NULL, *commit_refname = NULL;
7573 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7574 struct got_reference *commit_ref = NULL;
7575 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7576 struct check_rebase_ok_arg ok_arg;
7578 *fileindex = NULL;
7580 err = lock_worktree(worktree, LOCK_EX);
7581 if (err)
7582 return err;
7584 err = open_fileindex(fileindex, &fileindex_path, worktree);
7585 if (err)
7586 goto done;
7588 /* Preconditions are the same as for rebase. */
7589 ok_arg.worktree = worktree;
7590 ok_arg.repo = repo;
7591 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7592 &ok_arg);
7593 if (err)
7594 goto done;
7596 err = get_merge_branch_ref_name(&branch_refname, worktree);
7597 if (err)
7598 return err;
7600 err = get_merge_commit_ref_name(&commit_refname, worktree);
7601 if (err)
7602 return err;
7604 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7605 0);
7606 if (err)
7607 goto done;
7609 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7610 if (err)
7611 goto done;
7613 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7614 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7615 goto done;
7618 err = got_ref_resolve(&branch_tip, repo, branch);
7619 if (err)
7620 goto done;
7622 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7623 if (err)
7624 goto done;
7625 err = got_ref_write(branch_ref, repo);
7626 if (err)
7627 goto done;
7629 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7630 if (err)
7631 goto done;
7632 err = got_ref_write(commit_ref, repo);
7633 if (err)
7634 goto done;
7636 done:
7637 free(branch_refname);
7638 free(commit_refname);
7639 free(fileindex_path);
7640 if (branch_ref)
7641 got_ref_close(branch_ref);
7642 if (commit_ref)
7643 got_ref_close(commit_ref);
7644 if (wt_branch)
7645 got_ref_close(wt_branch);
7646 free(wt_branch_tip);
7647 if (err) {
7648 if (*fileindex) {
7649 got_fileindex_free(*fileindex);
7650 *fileindex = NULL;
7652 lock_worktree(worktree, LOCK_SH);
7654 return err;
7657 const struct got_error *
7658 got_worktree_merge_continue(char **branch_name,
7659 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7660 struct got_worktree *worktree, struct got_repository *repo)
7662 const struct got_error *err;
7663 char *commit_refname = NULL, *branch_refname = NULL;
7664 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7665 char *fileindex_path = NULL;
7666 int have_staged_files = 0;
7668 *branch_name = NULL;
7669 *branch_tip = NULL;
7670 *fileindex = NULL;
7672 err = lock_worktree(worktree, LOCK_EX);
7673 if (err)
7674 return err;
7676 err = open_fileindex(fileindex, &fileindex_path, worktree);
7677 if (err)
7678 goto done;
7680 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7681 &have_staged_files);
7682 if (err && err->code != GOT_ERR_CANCELLED)
7683 goto done;
7684 if (have_staged_files) {
7685 err = got_error(GOT_ERR_STAGED_PATHS);
7686 goto done;
7689 err = get_merge_branch_ref_name(&branch_refname, worktree);
7690 if (err)
7691 goto done;
7693 err = get_merge_commit_ref_name(&commit_refname, worktree);
7694 if (err)
7695 goto done;
7697 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7698 if (err)
7699 goto done;
7701 if (!got_ref_is_symbolic(branch_ref)) {
7702 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7703 "%s is not a symbolic reference",
7704 got_ref_get_name(branch_ref));
7705 goto done;
7707 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7708 if (*branch_name == NULL) {
7709 err = got_error_from_errno("strdup");
7710 goto done;
7713 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7714 if (err)
7715 goto done;
7717 err = got_ref_resolve(branch_tip, repo, commit_ref);
7718 if (err)
7719 goto done;
7720 done:
7721 free(commit_refname);
7722 free(branch_refname);
7723 free(fileindex_path);
7724 if (commit_ref)
7725 got_ref_close(commit_ref);
7726 if (branch_ref)
7727 got_ref_close(branch_ref);
7728 if (err) {
7729 if (*branch_name) {
7730 free(*branch_name);
7731 *branch_name = NULL;
7733 free(*branch_tip);
7734 *branch_tip = NULL;
7735 if (*fileindex) {
7736 got_fileindex_free(*fileindex);
7737 *fileindex = NULL;
7739 lock_worktree(worktree, LOCK_SH);
7741 return err;
7744 const struct got_error *
7745 got_worktree_merge_abort(struct got_worktree *worktree,
7746 struct got_fileindex *fileindex, struct got_repository *repo,
7747 got_worktree_checkout_cb progress_cb, void *progress_arg)
7749 const struct got_error *err, *unlockerr, *sync_err;
7750 struct got_object_id *commit_id = NULL;
7751 char *fileindex_path = NULL;
7752 struct revert_file_args rfa;
7753 struct got_object_id *tree_id = NULL;
7755 err = got_object_id_by_path(&tree_id, repo,
7756 worktree->base_commit_id, worktree->path_prefix);
7757 if (err)
7758 goto done;
7760 err = delete_merge_refs(worktree, repo);
7761 if (err)
7762 goto done;
7764 err = get_fileindex_path(&fileindex_path, worktree);
7765 if (err)
7766 goto done;
7768 rfa.worktree = worktree;
7769 rfa.fileindex = fileindex;
7770 rfa.progress_cb = progress_cb;
7771 rfa.progress_arg = progress_arg;
7772 rfa.patch_cb = NULL;
7773 rfa.patch_arg = NULL;
7774 rfa.repo = repo;
7775 rfa.unlink_added_files = 1;
7776 err = worktree_status(worktree, "", fileindex, repo,
7777 revert_file, &rfa, NULL, NULL, 1, 0);
7778 if (err)
7779 goto sync;
7781 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7782 repo, progress_cb, progress_arg, NULL, NULL);
7783 sync:
7784 sync_err = sync_fileindex(fileindex, fileindex_path);
7785 if (sync_err && err == NULL)
7786 err = sync_err;
7787 done:
7788 free(tree_id);
7789 free(commit_id);
7790 if (fileindex)
7791 got_fileindex_free(fileindex);
7792 free(fileindex_path);
7794 unlockerr = lock_worktree(worktree, LOCK_SH);
7795 if (unlockerr && err == NULL)
7796 err = unlockerr;
7797 return err;
7800 struct check_stage_ok_arg {
7801 struct got_object_id *head_commit_id;
7802 struct got_worktree *worktree;
7803 struct got_fileindex *fileindex;
7804 struct got_repository *repo;
7805 int have_changes;
7808 const struct got_error *
7809 check_stage_ok(void *arg, unsigned char status,
7810 unsigned char staged_status, const char *relpath,
7811 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7812 struct got_object_id *commit_id, int dirfd, const char *de_name)
7814 struct check_stage_ok_arg *a = arg;
7815 const struct got_error *err = NULL;
7816 struct got_fileindex_entry *ie;
7817 struct got_object_id base_commit_id;
7818 struct got_object_id *base_commit_idp = NULL;
7819 char *in_repo_path = NULL, *p;
7821 if (status == GOT_STATUS_UNVERSIONED ||
7822 status == GOT_STATUS_NO_CHANGE)
7823 return NULL;
7824 if (status == GOT_STATUS_NONEXISTENT)
7825 return got_error_set_errno(ENOENT, relpath);
7827 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7828 if (ie == NULL)
7829 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7831 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7832 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7833 relpath) == -1)
7834 return got_error_from_errno("asprintf");
7836 if (got_fileindex_entry_has_commit(ie)) {
7837 memcpy(base_commit_id.sha1, ie->commit_sha1,
7838 SHA1_DIGEST_LENGTH);
7839 base_commit_idp = &base_commit_id;
7842 if (status == GOT_STATUS_CONFLICT) {
7843 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7844 goto done;
7845 } else if (status != GOT_STATUS_ADD &&
7846 status != GOT_STATUS_MODIFY &&
7847 status != GOT_STATUS_DELETE) {
7848 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7849 goto done;
7852 a->have_changes = 1;
7854 p = in_repo_path;
7855 while (p[0] == '/')
7856 p++;
7857 err = check_out_of_date(p, status, staged_status,
7858 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7859 GOT_ERR_STAGE_OUT_OF_DATE);
7860 done:
7861 free(in_repo_path);
7862 return err;
7865 struct stage_path_arg {
7866 struct got_worktree *worktree;
7867 struct got_fileindex *fileindex;
7868 struct got_repository *repo;
7869 got_worktree_status_cb status_cb;
7870 void *status_arg;
7871 got_worktree_patch_cb patch_cb;
7872 void *patch_arg;
7873 int staged_something;
7874 int allow_bad_symlinks;
7877 static const struct got_error *
7878 stage_path(void *arg, unsigned char status,
7879 unsigned char staged_status, const char *relpath,
7880 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7881 struct got_object_id *commit_id, int dirfd, const char *de_name)
7883 struct stage_path_arg *a = arg;
7884 const struct got_error *err = NULL;
7885 struct got_fileindex_entry *ie;
7886 char *ondisk_path = NULL, *path_content = NULL;
7887 uint32_t stage;
7888 struct got_object_id *new_staged_blob_id = NULL;
7889 struct stat sb;
7891 if (status == GOT_STATUS_UNVERSIONED)
7892 return NULL;
7894 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7895 if (ie == NULL)
7896 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7898 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7899 relpath)== -1)
7900 return got_error_from_errno("asprintf");
7902 switch (status) {
7903 case GOT_STATUS_ADD:
7904 case GOT_STATUS_MODIFY:
7905 /* XXX could sb.st_mode be passed in by our caller? */
7906 if (lstat(ondisk_path, &sb) == -1) {
7907 err = got_error_from_errno2("lstat", ondisk_path);
7908 break;
7910 if (a->patch_cb) {
7911 if (status == GOT_STATUS_ADD) {
7912 int choice = GOT_PATCH_CHOICE_NONE;
7913 err = (*a->patch_cb)(&choice, a->patch_arg,
7914 status, ie->path, NULL, 1, 1);
7915 if (err)
7916 break;
7917 if (choice != GOT_PATCH_CHOICE_YES)
7918 break;
7919 } else {
7920 err = create_patched_content(&path_content, 0,
7921 staged_blob_id ? staged_blob_id : blob_id,
7922 ondisk_path, dirfd, de_name, ie->path,
7923 a->repo, a->patch_cb, a->patch_arg);
7924 if (err || path_content == NULL)
7925 break;
7928 err = got_object_blob_create(&new_staged_blob_id,
7929 path_content ? path_content : ondisk_path, a->repo);
7930 if (err)
7931 break;
7932 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7933 SHA1_DIGEST_LENGTH);
7934 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7935 stage = GOT_FILEIDX_STAGE_ADD;
7936 else
7937 stage = GOT_FILEIDX_STAGE_MODIFY;
7938 got_fileindex_entry_stage_set(ie, stage);
7939 if (S_ISLNK(sb.st_mode)) {
7940 int is_bad_symlink = 0;
7941 if (!a->allow_bad_symlinks) {
7942 char target_path[PATH_MAX];
7943 ssize_t target_len;
7944 target_len = readlink(ondisk_path, target_path,
7945 sizeof(target_path));
7946 if (target_len == -1) {
7947 err = got_error_from_errno2("readlink",
7948 ondisk_path);
7949 break;
7951 err = is_bad_symlink_target(&is_bad_symlink,
7952 target_path, target_len, ondisk_path,
7953 a->worktree->root_path);
7954 if (err)
7955 break;
7956 if (is_bad_symlink) {
7957 err = got_error_path(ondisk_path,
7958 GOT_ERR_BAD_SYMLINK);
7959 break;
7962 if (is_bad_symlink)
7963 got_fileindex_entry_staged_filetype_set(ie,
7964 GOT_FILEIDX_MODE_BAD_SYMLINK);
7965 else
7966 got_fileindex_entry_staged_filetype_set(ie,
7967 GOT_FILEIDX_MODE_SYMLINK);
7968 } else {
7969 got_fileindex_entry_staged_filetype_set(ie,
7970 GOT_FILEIDX_MODE_REGULAR_FILE);
7972 a->staged_something = 1;
7973 if (a->status_cb == NULL)
7974 break;
7975 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7976 get_staged_status(ie), relpath, blob_id,
7977 new_staged_blob_id, NULL, dirfd, de_name);
7978 break;
7979 case GOT_STATUS_DELETE:
7980 if (staged_status == GOT_STATUS_DELETE)
7981 break;
7982 if (a->patch_cb) {
7983 int choice = GOT_PATCH_CHOICE_NONE;
7984 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7985 ie->path, NULL, 1, 1);
7986 if (err)
7987 break;
7988 if (choice == GOT_PATCH_CHOICE_NO)
7989 break;
7990 if (choice != GOT_PATCH_CHOICE_YES) {
7991 err = got_error(GOT_ERR_PATCH_CHOICE);
7992 break;
7995 stage = GOT_FILEIDX_STAGE_DELETE;
7996 got_fileindex_entry_stage_set(ie, stage);
7997 a->staged_something = 1;
7998 if (a->status_cb == NULL)
7999 break;
8000 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8001 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8002 de_name);
8003 break;
8004 case GOT_STATUS_NO_CHANGE:
8005 break;
8006 case GOT_STATUS_CONFLICT:
8007 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8008 break;
8009 case GOT_STATUS_NONEXISTENT:
8010 err = got_error_set_errno(ENOENT, relpath);
8011 break;
8012 default:
8013 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8014 break;
8017 if (path_content && unlink(path_content) == -1 && err == NULL)
8018 err = got_error_from_errno2("unlink", path_content);
8019 free(path_content);
8020 free(ondisk_path);
8021 free(new_staged_blob_id);
8022 return err;
8025 const struct got_error *
8026 got_worktree_stage(struct got_worktree *worktree,
8027 struct got_pathlist_head *paths,
8028 got_worktree_status_cb status_cb, void *status_arg,
8029 got_worktree_patch_cb patch_cb, void *patch_arg,
8030 int allow_bad_symlinks, struct got_repository *repo)
8032 const struct got_error *err = NULL, *sync_err, *unlockerr;
8033 struct got_pathlist_entry *pe;
8034 struct got_fileindex *fileindex = NULL;
8035 char *fileindex_path = NULL;
8036 struct got_reference *head_ref = NULL;
8037 struct got_object_id *head_commit_id = NULL;
8038 struct check_stage_ok_arg oka;
8039 struct stage_path_arg spa;
8041 err = lock_worktree(worktree, LOCK_EX);
8042 if (err)
8043 return err;
8045 err = got_ref_open(&head_ref, repo,
8046 got_worktree_get_head_ref_name(worktree), 0);
8047 if (err)
8048 goto done;
8049 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8050 if (err)
8051 goto done;
8052 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8053 if (err)
8054 goto done;
8056 /* Check pre-conditions before staging anything. */
8057 oka.head_commit_id = head_commit_id;
8058 oka.worktree = worktree;
8059 oka.fileindex = fileindex;
8060 oka.repo = repo;
8061 oka.have_changes = 0;
8062 TAILQ_FOREACH(pe, paths, entry) {
8063 err = worktree_status(worktree, pe->path, fileindex, repo,
8064 check_stage_ok, &oka, NULL, NULL, 1, 0);
8065 if (err)
8066 goto done;
8068 if (!oka.have_changes) {
8069 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8070 goto done;
8073 spa.worktree = worktree;
8074 spa.fileindex = fileindex;
8075 spa.repo = repo;
8076 spa.patch_cb = patch_cb;
8077 spa.patch_arg = patch_arg;
8078 spa.status_cb = status_cb;
8079 spa.status_arg = status_arg;
8080 spa.staged_something = 0;
8081 spa.allow_bad_symlinks = allow_bad_symlinks;
8082 TAILQ_FOREACH(pe, paths, entry) {
8083 err = worktree_status(worktree, pe->path, fileindex, repo,
8084 stage_path, &spa, NULL, NULL, 1, 0);
8085 if (err)
8086 goto done;
8088 if (!spa.staged_something) {
8089 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8090 goto done;
8093 sync_err = sync_fileindex(fileindex, fileindex_path);
8094 if (sync_err && err == NULL)
8095 err = sync_err;
8096 done:
8097 if (head_ref)
8098 got_ref_close(head_ref);
8099 free(head_commit_id);
8100 free(fileindex_path);
8101 if (fileindex)
8102 got_fileindex_free(fileindex);
8103 unlockerr = lock_worktree(worktree, LOCK_SH);
8104 if (unlockerr && err == NULL)
8105 err = unlockerr;
8106 return err;
8109 struct unstage_path_arg {
8110 struct got_worktree *worktree;
8111 struct got_fileindex *fileindex;
8112 struct got_repository *repo;
8113 got_worktree_checkout_cb progress_cb;
8114 void *progress_arg;
8115 got_worktree_patch_cb patch_cb;
8116 void *patch_arg;
8119 static const struct got_error *
8120 create_unstaged_content(char **path_unstaged_content,
8121 char **path_new_staged_content, struct got_object_id *blob_id,
8122 struct got_object_id *staged_blob_id, const char *relpath,
8123 struct got_repository *repo,
8124 got_worktree_patch_cb patch_cb, void *patch_arg)
8126 const struct got_error *err, *free_err;
8127 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8128 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8129 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8130 struct got_diffreg_result *diffreg_result = NULL;
8131 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8132 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8134 *path_unstaged_content = NULL;
8135 *path_new_staged_content = NULL;
8137 err = got_object_id_str(&label1, blob_id);
8138 if (err)
8139 return err;
8140 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8141 if (err)
8142 goto done;
8144 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8145 if (err)
8146 goto done;
8148 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8149 if (err)
8150 goto done;
8152 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8153 if (err)
8154 goto done;
8156 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8157 if (err)
8158 goto done;
8160 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8161 if (err)
8162 goto done;
8164 err = got_diff_files(&diffreg_result, f1, label1, f2,
8165 path2, 3, 0, 1, NULL);
8166 if (err)
8167 goto done;
8169 err = got_opentemp_named(path_unstaged_content, &outfile,
8170 "got-unstaged-content");
8171 if (err)
8172 goto done;
8173 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8174 "got-new-staged-content");
8175 if (err)
8176 goto done;
8178 if (fseek(f1, 0L, SEEK_SET) == -1) {
8179 err = got_ferror(f1, GOT_ERR_IO);
8180 goto done;
8182 if (fseek(f2, 0L, SEEK_SET) == -1) {
8183 err = got_ferror(f2, GOT_ERR_IO);
8184 goto done;
8186 /* Count the number of actual changes in the diff result. */
8187 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8188 struct diff_chunk_context cc = {};
8189 diff_chunk_context_load_change(&cc, &nchunks_used,
8190 diffreg_result->result, n, 0);
8191 nchanges++;
8193 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8194 int choice;
8195 err = apply_or_reject_change(&choice, &nchunks_used,
8196 diffreg_result->result, n, relpath, f1, f2,
8197 &line_cur1, &line_cur2,
8198 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8199 if (err)
8200 goto done;
8201 if (choice == GOT_PATCH_CHOICE_YES)
8202 have_content = 1;
8203 else
8204 have_rejected_content = 1;
8205 if (choice == GOT_PATCH_CHOICE_QUIT)
8206 break;
8208 if (have_content || have_rejected_content)
8209 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8210 outfile, rejectfile);
8211 done:
8212 free(label1);
8213 if (blob)
8214 got_object_blob_close(blob);
8215 if (staged_blob)
8216 got_object_blob_close(staged_blob);
8217 free_err = got_diffreg_result_free(diffreg_result);
8218 if (free_err && err == NULL)
8219 err = free_err;
8220 if (f1 && fclose(f1) == EOF && err == NULL)
8221 err = got_error_from_errno2("fclose", path1);
8222 if (f2 && fclose(f2) == EOF && err == NULL)
8223 err = got_error_from_errno2("fclose", path2);
8224 if (outfile && fclose(outfile) == EOF && err == NULL)
8225 err = got_error_from_errno2("fclose", *path_unstaged_content);
8226 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8227 err = got_error_from_errno2("fclose", *path_new_staged_content);
8228 if (path1 && unlink(path1) == -1 && err == NULL)
8229 err = got_error_from_errno2("unlink", path1);
8230 if (path2 && unlink(path2) == -1 && err == NULL)
8231 err = got_error_from_errno2("unlink", path2);
8232 if (err || !have_content) {
8233 if (*path_unstaged_content &&
8234 unlink(*path_unstaged_content) == -1 && err == NULL)
8235 err = got_error_from_errno2("unlink",
8236 *path_unstaged_content);
8237 free(*path_unstaged_content);
8238 *path_unstaged_content = NULL;
8240 if (err || !have_content || !have_rejected_content) {
8241 if (*path_new_staged_content &&
8242 unlink(*path_new_staged_content) == -1 && err == NULL)
8243 err = got_error_from_errno2("unlink",
8244 *path_new_staged_content);
8245 free(*path_new_staged_content);
8246 *path_new_staged_content = NULL;
8248 free(path1);
8249 free(path2);
8250 return err;
8253 static const struct got_error *
8254 unstage_hunks(struct got_object_id *staged_blob_id,
8255 struct got_blob_object *blob_base,
8256 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8257 const char *ondisk_path, const char *label_orig,
8258 struct got_worktree *worktree, struct got_repository *repo,
8259 got_worktree_patch_cb patch_cb, void *patch_arg,
8260 got_worktree_checkout_cb progress_cb, void *progress_arg)
8262 const struct got_error *err = NULL;
8263 char *path_unstaged_content = NULL;
8264 char *path_new_staged_content = NULL;
8265 char *parent = NULL, *base_path = NULL;
8266 char *blob_base_path = NULL;
8267 struct got_object_id *new_staged_blob_id = NULL;
8268 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8269 struct stat sb;
8271 err = create_unstaged_content(&path_unstaged_content,
8272 &path_new_staged_content, blob_id, staged_blob_id,
8273 ie->path, repo, patch_cb, patch_arg);
8274 if (err)
8275 return err;
8277 if (path_unstaged_content == NULL)
8278 return NULL;
8280 if (path_new_staged_content) {
8281 err = got_object_blob_create(&new_staged_blob_id,
8282 path_new_staged_content, repo);
8283 if (err)
8284 goto done;
8287 f = fopen(path_unstaged_content, "r");
8288 if (f == NULL) {
8289 err = got_error_from_errno2("fopen",
8290 path_unstaged_content);
8291 goto done;
8293 if (fstat(fileno(f), &sb) == -1) {
8294 err = got_error_from_errno2("fstat", path_unstaged_content);
8295 goto done;
8297 if (got_fileindex_entry_staged_filetype_get(ie) ==
8298 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8299 char link_target[PATH_MAX];
8300 size_t r;
8301 r = fread(link_target, 1, sizeof(link_target), f);
8302 if (r == 0 && ferror(f)) {
8303 err = got_error_from_errno("fread");
8304 goto done;
8306 if (r >= sizeof(link_target)) { /* should not happen */
8307 err = got_error(GOT_ERR_NO_SPACE);
8308 goto done;
8310 link_target[r] = '\0';
8311 err = merge_symlink(worktree, blob_base,
8312 ondisk_path, ie->path, label_orig, link_target,
8313 worktree->base_commit_id, repo, progress_cb,
8314 progress_arg);
8315 } else {
8316 int local_changes_subsumed;
8318 err = got_path_dirname(&parent, ondisk_path);
8319 if (err)
8320 return err;
8322 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8323 parent) == -1) {
8324 err = got_error_from_errno("asprintf");
8325 base_path = NULL;
8326 goto done;
8329 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8330 if (err)
8331 goto done;
8332 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8333 blob_base);
8334 if (err)
8335 goto done;
8338 * In order the run a 3-way merge with a symlink we copy the symlink's
8339 * target path into a temporary file and use that file with diff3.
8341 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8342 err = dump_symlink_target_path_to_file(&f_deriv2,
8343 ondisk_path);
8344 if (err)
8345 goto done;
8346 } else {
8347 int fd;
8348 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8349 if (fd == -1) {
8350 err = got_error_from_errno2("open", ondisk_path);
8351 goto done;
8353 f_deriv2 = fdopen(fd, "r");
8354 if (f_deriv2 == NULL) {
8355 err = got_error_from_errno2("fdopen", ondisk_path);
8356 close(fd);
8357 goto done;
8361 err = merge_file(&local_changes_subsumed, worktree,
8362 f_base, f, f_deriv2, ondisk_path, ie->path,
8363 got_fileindex_perms_to_st(ie),
8364 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8365 repo, progress_cb, progress_arg);
8367 if (err)
8368 goto done;
8370 if (new_staged_blob_id) {
8371 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8372 SHA1_DIGEST_LENGTH);
8373 } else {
8374 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8375 got_fileindex_entry_staged_filetype_set(ie, 0);
8377 done:
8378 free(new_staged_blob_id);
8379 if (path_unstaged_content &&
8380 unlink(path_unstaged_content) == -1 && err == NULL)
8381 err = got_error_from_errno2("unlink", path_unstaged_content);
8382 if (path_new_staged_content &&
8383 unlink(path_new_staged_content) == -1 && err == NULL)
8384 err = got_error_from_errno2("unlink", path_new_staged_content);
8385 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8386 err = got_error_from_errno2("unlink", blob_base_path);
8387 if (f_base && fclose(f_base) == EOF && err == NULL)
8388 err = got_error_from_errno2("fclose", path_unstaged_content);
8389 if (f && fclose(f) == EOF && err == NULL)
8390 err = got_error_from_errno2("fclose", path_unstaged_content);
8391 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8392 err = got_error_from_errno2("fclose", ondisk_path);
8393 free(path_unstaged_content);
8394 free(path_new_staged_content);
8395 free(blob_base_path);
8396 free(parent);
8397 free(base_path);
8398 return err;
8401 static const struct got_error *
8402 unstage_path(void *arg, unsigned char status,
8403 unsigned char staged_status, const char *relpath,
8404 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8405 struct got_object_id *commit_id, int dirfd, const char *de_name)
8407 const struct got_error *err = NULL;
8408 struct unstage_path_arg *a = arg;
8409 struct got_fileindex_entry *ie;
8410 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8411 char *ondisk_path = NULL;
8412 char *id_str = NULL, *label_orig = NULL;
8413 int local_changes_subsumed;
8414 struct stat sb;
8416 if (staged_status != GOT_STATUS_ADD &&
8417 staged_status != GOT_STATUS_MODIFY &&
8418 staged_status != GOT_STATUS_DELETE)
8419 return NULL;
8421 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8422 if (ie == NULL)
8423 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8425 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8426 == -1)
8427 return got_error_from_errno("asprintf");
8429 err = got_object_id_str(&id_str,
8430 commit_id ? commit_id : a->worktree->base_commit_id);
8431 if (err)
8432 goto done;
8433 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8434 id_str) == -1) {
8435 err = got_error_from_errno("asprintf");
8436 goto done;
8439 switch (staged_status) {
8440 case GOT_STATUS_MODIFY:
8441 err = got_object_open_as_blob(&blob_base, a->repo,
8442 blob_id, 8192);
8443 if (err)
8444 break;
8445 /* fall through */
8446 case GOT_STATUS_ADD:
8447 if (a->patch_cb) {
8448 if (staged_status == GOT_STATUS_ADD) {
8449 int choice = GOT_PATCH_CHOICE_NONE;
8450 err = (*a->patch_cb)(&choice, a->patch_arg,
8451 staged_status, ie->path, NULL, 1, 1);
8452 if (err)
8453 break;
8454 if (choice != GOT_PATCH_CHOICE_YES)
8455 break;
8456 } else {
8457 err = unstage_hunks(staged_blob_id,
8458 blob_base, blob_id, ie, ondisk_path,
8459 label_orig, a->worktree, a->repo,
8460 a->patch_cb, a->patch_arg,
8461 a->progress_cb, a->progress_arg);
8462 break; /* Done with this file. */
8465 err = got_object_open_as_blob(&blob_staged, a->repo,
8466 staged_blob_id, 8192);
8467 if (err)
8468 break;
8469 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8470 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8471 case GOT_FILEIDX_MODE_REGULAR_FILE:
8472 err = merge_blob(&local_changes_subsumed, a->worktree,
8473 blob_base, ondisk_path, relpath,
8474 got_fileindex_perms_to_st(ie), label_orig,
8475 blob_staged, commit_id ? commit_id :
8476 a->worktree->base_commit_id, a->repo,
8477 a->progress_cb, a->progress_arg);
8478 break;
8479 case GOT_FILEIDX_MODE_SYMLINK:
8480 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8481 char *staged_target;
8482 err = got_object_blob_read_to_str(
8483 &staged_target, blob_staged);
8484 if (err)
8485 goto done;
8486 err = merge_symlink(a->worktree, blob_base,
8487 ondisk_path, relpath, label_orig,
8488 staged_target, commit_id ? commit_id :
8489 a->worktree->base_commit_id,
8490 a->repo, a->progress_cb, a->progress_arg);
8491 free(staged_target);
8492 } else {
8493 err = merge_blob(&local_changes_subsumed,
8494 a->worktree, blob_base, ondisk_path,
8495 relpath, got_fileindex_perms_to_st(ie),
8496 label_orig, blob_staged,
8497 commit_id ? commit_id :
8498 a->worktree->base_commit_id, a->repo,
8499 a->progress_cb, a->progress_arg);
8501 break;
8502 default:
8503 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8504 break;
8506 if (err == NULL) {
8507 got_fileindex_entry_stage_set(ie,
8508 GOT_FILEIDX_STAGE_NONE);
8509 got_fileindex_entry_staged_filetype_set(ie, 0);
8511 break;
8512 case GOT_STATUS_DELETE:
8513 if (a->patch_cb) {
8514 int choice = GOT_PATCH_CHOICE_NONE;
8515 err = (*a->patch_cb)(&choice, a->patch_arg,
8516 staged_status, ie->path, NULL, 1, 1);
8517 if (err)
8518 break;
8519 if (choice == GOT_PATCH_CHOICE_NO)
8520 break;
8521 if (choice != GOT_PATCH_CHOICE_YES) {
8522 err = got_error(GOT_ERR_PATCH_CHOICE);
8523 break;
8526 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8527 got_fileindex_entry_staged_filetype_set(ie, 0);
8528 err = get_file_status(&status, &sb, ie, ondisk_path,
8529 dirfd, de_name, a->repo);
8530 if (err)
8531 break;
8532 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8533 break;
8535 done:
8536 free(ondisk_path);
8537 if (blob_base)
8538 got_object_blob_close(blob_base);
8539 if (blob_staged)
8540 got_object_blob_close(blob_staged);
8541 free(id_str);
8542 free(label_orig);
8543 return err;
8546 const struct got_error *
8547 got_worktree_unstage(struct got_worktree *worktree,
8548 struct got_pathlist_head *paths,
8549 got_worktree_checkout_cb progress_cb, void *progress_arg,
8550 got_worktree_patch_cb patch_cb, void *patch_arg,
8551 struct got_repository *repo)
8553 const struct got_error *err = NULL, *sync_err, *unlockerr;
8554 struct got_pathlist_entry *pe;
8555 struct got_fileindex *fileindex = NULL;
8556 char *fileindex_path = NULL;
8557 struct unstage_path_arg upa;
8559 err = lock_worktree(worktree, LOCK_EX);
8560 if (err)
8561 return err;
8563 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8564 if (err)
8565 goto done;
8567 upa.worktree = worktree;
8568 upa.fileindex = fileindex;
8569 upa.repo = repo;
8570 upa.progress_cb = progress_cb;
8571 upa.progress_arg = progress_arg;
8572 upa.patch_cb = patch_cb;
8573 upa.patch_arg = patch_arg;
8574 TAILQ_FOREACH(pe, paths, entry) {
8575 err = worktree_status(worktree, pe->path, fileindex, repo,
8576 unstage_path, &upa, NULL, NULL, 1, 0);
8577 if (err)
8578 goto done;
8581 sync_err = sync_fileindex(fileindex, fileindex_path);
8582 if (sync_err && err == NULL)
8583 err = sync_err;
8584 done:
8585 free(fileindex_path);
8586 if (fileindex)
8587 got_fileindex_free(fileindex);
8588 unlockerr = lock_worktree(worktree, LOCK_SH);
8589 if (unlockerr && err == NULL)
8590 err = unlockerr;
8591 return err;
8594 struct report_file_info_arg {
8595 struct got_worktree *worktree;
8596 got_worktree_path_info_cb info_cb;
8597 void *info_arg;
8598 struct got_pathlist_head *paths;
8599 got_cancel_cb cancel_cb;
8600 void *cancel_arg;
8603 static const struct got_error *
8604 report_file_info(void *arg, struct got_fileindex_entry *ie)
8606 struct report_file_info_arg *a = arg;
8607 struct got_pathlist_entry *pe;
8608 struct got_object_id blob_id, staged_blob_id, commit_id;
8609 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8610 struct got_object_id *commit_idp = NULL;
8611 int stage;
8613 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8614 return got_error(GOT_ERR_CANCELLED);
8616 TAILQ_FOREACH(pe, a->paths, entry) {
8617 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8618 got_path_is_child(ie->path, pe->path, pe->path_len))
8619 break;
8621 if (pe == NULL) /* not found */
8622 return NULL;
8624 if (got_fileindex_entry_has_blob(ie)) {
8625 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8626 blob_idp = &blob_id;
8628 stage = got_fileindex_entry_stage_get(ie);
8629 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8630 stage == GOT_FILEIDX_STAGE_ADD) {
8631 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8632 SHA1_DIGEST_LENGTH);
8633 staged_blob_idp = &staged_blob_id;
8636 if (got_fileindex_entry_has_commit(ie)) {
8637 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8638 commit_idp = &commit_id;
8641 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8642 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8645 const struct got_error *
8646 got_worktree_path_info(struct got_worktree *worktree,
8647 struct got_pathlist_head *paths,
8648 got_worktree_path_info_cb info_cb, void *info_arg,
8649 got_cancel_cb cancel_cb, void *cancel_arg)
8652 const struct got_error *err = NULL, *unlockerr;
8653 struct got_fileindex *fileindex = NULL;
8654 char *fileindex_path = NULL;
8655 struct report_file_info_arg arg;
8657 err = lock_worktree(worktree, LOCK_SH);
8658 if (err)
8659 return err;
8661 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8662 if (err)
8663 goto done;
8665 arg.worktree = worktree;
8666 arg.info_cb = info_cb;
8667 arg.info_arg = info_arg;
8668 arg.paths = paths;
8669 arg.cancel_cb = cancel_cb;
8670 arg.cancel_arg = cancel_arg;
8671 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8672 &arg);
8673 done:
8674 free(fileindex_path);
8675 if (fileindex)
8676 got_fileindex_free(fileindex);
8677 unlockerr = lock_worktree(worktree, LOCK_UN);
8678 if (unlockerr && err == NULL)
8679 err = unlockerr;
8680 return err;