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 | O_CLOEXEC);
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 | O_CLOEXEC);
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 O_CLOEXEC, 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 | O_CLOEXEC,
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 | O_CLOEXEC);
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 | O_CLOEXEC);
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, "rbe");
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,
2818 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2819 if (fd == -1) {
2820 err = got_error_from_errno2("open",
2821 ondisk_path);
2822 goto done;
2824 f_deriv = fdopen(fd, "r");
2825 if (f_deriv == NULL) {
2826 err = got_error_from_errno2("fdopen",
2827 ondisk_path);
2828 close(fd);
2829 goto done;
2831 err = got_object_id_str(&id_str, a->commit_id2);
2832 if (err)
2833 goto done;
2834 if (asprintf(&label_deriv2, "%s: commit %s",
2835 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2836 err = got_error_from_errno("asprintf");
2837 goto done;
2839 err = merge_file(&local_changes_subsumed, a->worktree,
2840 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2841 sb.st_mode, a->label_orig, NULL, label_deriv2,
2842 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2843 a->progress_cb, a->progress_arg);
2845 } else if (blob1) {
2846 ie = got_fileindex_entry_get(a->fileindex, path1,
2847 strlen(path1));
2848 if (ie == NULL)
2849 return (*a->progress_cb)(a->progress_arg,
2850 GOT_STATUS_MISSING, path1);
2852 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2853 path1) == -1)
2854 return got_error_from_errno("asprintf");
2856 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2857 repo);
2858 if (err)
2859 goto done;
2861 switch (status) {
2862 case GOT_STATUS_NO_CHANGE:
2863 err = (*a->progress_cb)(a->progress_arg,
2864 GOT_STATUS_DELETE, path1);
2865 if (err)
2866 goto done;
2867 err = remove_ondisk_file(a->worktree->root_path, path1);
2868 if (err)
2869 goto done;
2870 if (ie)
2871 got_fileindex_entry_mark_deleted_from_disk(ie);
2872 break;
2873 case GOT_STATUS_DELETE:
2874 case GOT_STATUS_MISSING:
2875 err = (*a->progress_cb)(a->progress_arg,
2876 GOT_STATUS_DELETE, path1);
2877 if (err)
2878 goto done;
2879 if (ie)
2880 got_fileindex_entry_mark_deleted_from_disk(ie);
2881 break;
2882 case GOT_STATUS_ADD: {
2883 struct got_object_id *id;
2884 FILE *blob1_f;
2885 off_t blob1_size;
2887 * Delete the added file only if its content already
2888 * exists in the repository.
2890 err = got_object_blob_file_create(&id, &blob1_f,
2891 &blob1_size, path1);
2892 if (err)
2893 goto done;
2894 if (got_object_id_cmp(id, id1) == 0) {
2895 err = (*a->progress_cb)(a->progress_arg,
2896 GOT_STATUS_DELETE, path1);
2897 if (err)
2898 goto done;
2899 err = remove_ondisk_file(a->worktree->root_path,
2900 path1);
2901 if (err)
2902 goto done;
2903 if (ie)
2904 got_fileindex_entry_remove(a->fileindex,
2905 ie);
2906 } else {
2907 err = (*a->progress_cb)(a->progress_arg,
2908 GOT_STATUS_CANNOT_DELETE, path1);
2910 if (fclose(blob1_f) == EOF && err == NULL)
2911 err = got_error_from_errno("fclose");
2912 free(id);
2913 if (err)
2914 goto done;
2915 break;
2917 case GOT_STATUS_MODIFY:
2918 case GOT_STATUS_CONFLICT:
2919 err = (*a->progress_cb)(a->progress_arg,
2920 GOT_STATUS_CANNOT_DELETE, path1);
2921 if (err)
2922 goto done;
2923 break;
2924 case GOT_STATUS_OBSTRUCTED:
2925 err = (*a->progress_cb)(a->progress_arg, status, path1);
2926 if (err)
2927 goto done;
2928 break;
2929 default:
2930 break;
2932 } else if (blob2) {
2933 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2934 path2) == -1)
2935 return got_error_from_errno("asprintf");
2936 ie = got_fileindex_entry_get(a->fileindex, path2,
2937 strlen(path2));
2938 if (ie) {
2939 err = get_file_status(&status, &sb, ie, ondisk_path,
2940 -1, NULL, repo);
2941 if (err)
2942 goto done;
2943 if (status != GOT_STATUS_NO_CHANGE &&
2944 status != GOT_STATUS_MODIFY &&
2945 status != GOT_STATUS_CONFLICT &&
2946 status != GOT_STATUS_ADD) {
2947 err = (*a->progress_cb)(a->progress_arg,
2948 status, path2);
2949 goto done;
2951 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2952 char *link_target2;
2953 err = got_object_blob_read_to_str(&link_target2,
2954 blob2);
2955 if (err)
2956 goto done;
2957 err = merge_symlink(a->worktree, NULL,
2958 ondisk_path, path2, a->label_orig,
2959 link_target2, a->commit_id2, repo,
2960 a->progress_cb, a->progress_arg);
2961 free(link_target2);
2962 } else if (S_ISREG(sb.st_mode)) {
2963 err = merge_blob(&local_changes_subsumed,
2964 a->worktree, NULL, ondisk_path, path2,
2965 sb.st_mode, a->label_orig, blob2,
2966 a->commit_id2, repo, a->progress_cb,
2967 a->progress_arg);
2968 } else {
2969 err = got_error_path(ondisk_path,
2970 GOT_ERR_FILE_OBSTRUCTED);
2972 if (err)
2973 goto done;
2974 if (status == GOT_STATUS_DELETE) {
2975 err = got_fileindex_entry_update(ie,
2976 a->worktree->root_fd, path2, blob2->id.sha1,
2977 a->worktree->base_commit_id->sha1, 0);
2978 if (err)
2979 goto done;
2981 } else {
2982 int is_bad_symlink = 0;
2983 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2984 if (S_ISLNK(mode2)) {
2985 err = install_symlink(&is_bad_symlink,
2986 a->worktree, ondisk_path, path2, blob2, 0,
2987 0, 1, a->allow_bad_symlinks, repo,
2988 a->progress_cb, a->progress_arg);
2989 } else {
2990 err = install_blob(a->worktree, ondisk_path, path2,
2991 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2992 a->progress_cb, a->progress_arg);
2994 if (err)
2995 goto done;
2996 err = got_fileindex_entry_alloc(&ie, path2);
2997 if (err)
2998 goto done;
2999 err = got_fileindex_entry_update(ie,
3000 a->worktree->root_fd, path2, NULL, NULL, 1);
3001 if (err) {
3002 got_fileindex_entry_free(ie);
3003 goto done;
3005 err = got_fileindex_entry_add(a->fileindex, ie);
3006 if (err) {
3007 got_fileindex_entry_free(ie);
3008 goto done;
3010 if (is_bad_symlink) {
3011 got_fileindex_entry_filetype_set(ie,
3012 GOT_FILEIDX_MODE_BAD_SYMLINK);
3016 done:
3017 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3018 err = got_error_from_errno("fclose");
3019 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3020 err = got_error_from_errno("fclose");
3021 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3022 err = got_error_from_errno("fclose");
3023 free(id_str);
3024 free(label_deriv2);
3025 free(ondisk_path);
3026 return err;
3029 static const struct got_error *
3030 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3032 struct got_worktree *worktree = arg;
3034 /* Reject merges into a work tree with mixed base commits. */
3035 if (got_fileindex_entry_has_commit(ie) &&
3036 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3037 SHA1_DIGEST_LENGTH) != 0)
3038 return got_error(GOT_ERR_MIXED_COMMITS);
3040 return NULL;
3043 struct check_merge_conflicts_arg {
3044 struct got_worktree *worktree;
3045 struct got_fileindex *fileindex;
3046 struct got_repository *repo;
3049 static const struct got_error *
3050 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3051 struct got_blob_object *blob2, struct got_object_id *id1,
3052 struct got_object_id *id2, const char *path1, const char *path2,
3053 mode_t mode1, mode_t mode2, struct got_repository *repo)
3055 const struct got_error *err = NULL;
3056 struct check_merge_conflicts_arg *a = arg;
3057 unsigned char status;
3058 struct stat sb;
3059 struct got_fileindex_entry *ie;
3060 const char *path = path2 ? path2 : path1;
3061 struct got_object_id *id = id2 ? id2 : id1;
3062 char *ondisk_path;
3064 if (id == NULL)
3065 return NULL;
3067 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3068 if (ie == NULL)
3069 return NULL;
3071 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3072 == -1)
3073 return got_error_from_errno("asprintf");
3075 /* Reject merges into a work tree with conflicted files. */
3076 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3077 free(ondisk_path);
3078 if (err)
3079 return err;
3080 if (status == GOT_STATUS_CONFLICT)
3081 return got_error(GOT_ERR_CONFLICTS);
3083 return NULL;
3086 static const struct got_error *
3087 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3088 const char *fileindex_path, struct got_object_id *commit_id1,
3089 struct got_object_id *commit_id2, struct got_repository *repo,
3090 got_worktree_checkout_cb progress_cb, void *progress_arg,
3091 got_cancel_cb cancel_cb, void *cancel_arg)
3093 const struct got_error *err = NULL, *sync_err;
3094 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3095 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3096 struct check_merge_conflicts_arg cmc_arg;
3097 struct merge_file_cb_arg arg;
3098 char *label_orig = NULL;
3100 if (commit_id1) {
3101 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3102 worktree->path_prefix);
3103 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3104 goto done;
3106 if (tree_id1) {
3107 char *id_str;
3109 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3110 if (err)
3111 goto done;
3113 err = got_object_id_str(&id_str, commit_id1);
3114 if (err)
3115 goto done;
3117 if (asprintf(&label_orig, "%s: commit %s",
3118 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3119 err = got_error_from_errno("asprintf");
3120 free(id_str);
3121 goto done;
3123 free(id_str);
3126 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3127 worktree->path_prefix);
3128 if (err)
3129 goto done;
3131 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3132 if (err)
3133 goto done;
3135 cmc_arg.worktree = worktree;
3136 cmc_arg.fileindex = fileindex;
3137 cmc_arg.repo = repo;
3138 err = got_diff_tree(tree1, tree2, "", "", repo,
3139 check_merge_conflicts, &cmc_arg, 0);
3140 if (err)
3141 goto done;
3143 arg.worktree = worktree;
3144 arg.fileindex = fileindex;
3145 arg.progress_cb = progress_cb;
3146 arg.progress_arg = progress_arg;
3147 arg.cancel_cb = cancel_cb;
3148 arg.cancel_arg = cancel_arg;
3149 arg.label_orig = label_orig;
3150 arg.commit_id2 = commit_id2;
3151 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3152 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3153 sync_err = sync_fileindex(fileindex, fileindex_path);
3154 if (sync_err && err == NULL)
3155 err = sync_err;
3156 done:
3157 if (tree1)
3158 got_object_tree_close(tree1);
3159 if (tree2)
3160 got_object_tree_close(tree2);
3161 free(label_orig);
3162 return err;
3165 const struct got_error *
3166 got_worktree_merge_files(struct got_worktree *worktree,
3167 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3168 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3169 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3171 const struct got_error *err, *unlockerr;
3172 char *fileindex_path = NULL;
3173 struct got_fileindex *fileindex = NULL;
3175 err = lock_worktree(worktree, LOCK_EX);
3176 if (err)
3177 return err;
3179 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3180 if (err)
3181 goto done;
3183 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3184 worktree);
3185 if (err)
3186 goto done;
3188 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3189 commit_id2, repo, progress_cb, progress_arg,
3190 cancel_cb, cancel_arg);
3191 done:
3192 if (fileindex)
3193 got_fileindex_free(fileindex);
3194 free(fileindex_path);
3195 unlockerr = lock_worktree(worktree, LOCK_SH);
3196 if (unlockerr && err == NULL)
3197 err = unlockerr;
3198 return err;
3201 struct diff_dir_cb_arg {
3202 struct got_fileindex *fileindex;
3203 struct got_worktree *worktree;
3204 const char *status_path;
3205 size_t status_path_len;
3206 struct got_repository *repo;
3207 got_worktree_status_cb status_cb;
3208 void *status_arg;
3209 got_cancel_cb cancel_cb;
3210 void *cancel_arg;
3211 /* A pathlist containing per-directory pathlists of ignore patterns. */
3212 struct got_pathlist_head *ignores;
3213 int report_unchanged;
3214 int no_ignores;
3217 static const struct got_error *
3218 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3219 int dirfd, const char *de_name,
3220 got_worktree_status_cb status_cb, void *status_arg,
3221 struct got_repository *repo, int report_unchanged)
3223 const struct got_error *err = NULL;
3224 unsigned char status = GOT_STATUS_NO_CHANGE;
3225 unsigned char staged_status = get_staged_status(ie);
3226 struct stat sb;
3227 struct got_object_id blob_id, commit_id, staged_blob_id;
3228 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3229 struct got_object_id *staged_blob_idp = NULL;
3231 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3232 if (err)
3233 return err;
3235 if (status == GOT_STATUS_NO_CHANGE &&
3236 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3237 return NULL;
3239 if (got_fileindex_entry_has_blob(ie)) {
3240 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3241 blob_idp = &blob_id;
3243 if (got_fileindex_entry_has_commit(ie)) {
3244 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3245 commit_idp = &commit_id;
3247 if (staged_status == GOT_STATUS_ADD ||
3248 staged_status == GOT_STATUS_MODIFY) {
3249 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3250 SHA1_DIGEST_LENGTH);
3251 staged_blob_idp = &staged_blob_id;
3254 return (*status_cb)(status_arg, status, staged_status,
3255 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3258 static const struct got_error *
3259 status_old_new(void *arg, struct got_fileindex_entry *ie,
3260 struct dirent *de, const char *parent_path, int dirfd)
3262 const struct got_error *err = NULL;
3263 struct diff_dir_cb_arg *a = arg;
3264 char *abspath;
3266 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3267 return got_error(GOT_ERR_CANCELLED);
3269 if (got_path_cmp(parent_path, a->status_path,
3270 strlen(parent_path), a->status_path_len) != 0 &&
3271 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3272 return NULL;
3274 if (parent_path[0]) {
3275 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3276 parent_path, de->d_name) == -1)
3277 return got_error_from_errno("asprintf");
3278 } else {
3279 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3280 de->d_name) == -1)
3281 return got_error_from_errno("asprintf");
3284 err = report_file_status(ie, abspath, dirfd, de->d_name,
3285 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3286 free(abspath);
3287 return err;
3290 static const struct got_error *
3291 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3293 struct diff_dir_cb_arg *a = arg;
3294 struct got_object_id blob_id, commit_id;
3295 unsigned char status;
3297 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3298 return got_error(GOT_ERR_CANCELLED);
3300 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3301 return NULL;
3303 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3304 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3305 if (got_fileindex_entry_has_file_on_disk(ie))
3306 status = GOT_STATUS_MISSING;
3307 else
3308 status = GOT_STATUS_DELETE;
3309 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3310 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3313 void
3314 free_ignorelist(struct got_pathlist_head *ignorelist)
3316 struct got_pathlist_entry *pe;
3318 TAILQ_FOREACH(pe, ignorelist, entry)
3319 free((char *)pe->path);
3320 got_pathlist_free(ignorelist);
3323 void
3324 free_ignores(struct got_pathlist_head *ignores)
3326 struct got_pathlist_entry *pe;
3328 TAILQ_FOREACH(pe, ignores, entry) {
3329 struct got_pathlist_head *ignorelist = pe->data;
3330 free_ignorelist(ignorelist);
3331 free((char *)pe->path);
3333 got_pathlist_free(ignores);
3336 static const struct got_error *
3337 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3339 const struct got_error *err = NULL;
3340 struct got_pathlist_entry *pe = NULL;
3341 struct got_pathlist_head *ignorelist;
3342 char *line = NULL, *pattern, *dirpath = NULL;
3343 size_t linesize = 0;
3344 ssize_t linelen;
3346 ignorelist = calloc(1, sizeof(*ignorelist));
3347 if (ignorelist == NULL)
3348 return got_error_from_errno("calloc");
3349 TAILQ_INIT(ignorelist);
3351 while ((linelen = getline(&line, &linesize, f)) != -1) {
3352 if (linelen > 0 && line[linelen - 1] == '\n')
3353 line[linelen - 1] = '\0';
3355 /* Git's ignores may contain comments. */
3356 if (line[0] == '#')
3357 continue;
3359 /* Git's negated patterns are not (yet?) supported. */
3360 if (line[0] == '!')
3361 continue;
3363 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3364 line) == -1) {
3365 err = got_error_from_errno("asprintf");
3366 goto done;
3368 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3369 if (err)
3370 goto done;
3372 if (ferror(f)) {
3373 err = got_error_from_errno("getline");
3374 goto done;
3377 dirpath = strdup(path);
3378 if (dirpath == NULL) {
3379 err = got_error_from_errno("strdup");
3380 goto done;
3382 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3383 done:
3384 free(line);
3385 if (err || pe == NULL) {
3386 free(dirpath);
3387 free_ignorelist(ignorelist);
3389 return err;
3392 int
3393 match_ignores(struct got_pathlist_head *ignores, const char *path)
3395 struct got_pathlist_entry *pe;
3397 /* Handle patterns which match in all directories. */
3398 TAILQ_FOREACH(pe, ignores, entry) {
3399 struct got_pathlist_head *ignorelist = pe->data;
3400 struct got_pathlist_entry *pi;
3402 TAILQ_FOREACH(pi, ignorelist, entry) {
3403 const char *p, *pattern = pi->path;
3405 if (strncmp(pattern, "**/", 3) != 0)
3406 continue;
3407 pattern += 3;
3408 p = path;
3409 while (*p) {
3410 if (fnmatch(pattern, p,
3411 FNM_PATHNAME | FNM_LEADING_DIR)) {
3412 /* Retry in next directory. */
3413 while (*p && *p != '/')
3414 p++;
3415 while (*p == '/')
3416 p++;
3417 continue;
3419 return 1;
3425 * The ignores pathlist contains ignore lists from children before
3426 * parents, so we can find the most specific ignorelist by walking
3427 * ignores backwards.
3429 pe = TAILQ_LAST(ignores, got_pathlist_head);
3430 while (pe) {
3431 if (got_path_is_child(path, pe->path, pe->path_len)) {
3432 struct got_pathlist_head *ignorelist = pe->data;
3433 struct got_pathlist_entry *pi;
3434 TAILQ_FOREACH(pi, ignorelist, entry) {
3435 const char *pattern = pi->path;
3436 int flags = FNM_LEADING_DIR;
3437 if (strstr(pattern, "/**/") == NULL)
3438 flags |= FNM_PATHNAME;
3439 if (fnmatch(pattern, path, flags))
3440 continue;
3441 return 1;
3444 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3447 return 0;
3450 static const struct got_error *
3451 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3452 const char *path, int dirfd, const char *ignores_filename)
3454 const struct got_error *err = NULL;
3455 char *ignorespath;
3456 int fd = -1;
3457 FILE *ignoresfile = NULL;
3459 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3460 path[0] ? "/" : "", ignores_filename) == -1)
3461 return got_error_from_errno("asprintf");
3463 if (dirfd != -1) {
3464 fd = openat(dirfd, ignores_filename,
3465 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3466 if (fd == -1) {
3467 if (errno != ENOENT && errno != EACCES)
3468 err = got_error_from_errno2("openat",
3469 ignorespath);
3470 } else {
3471 ignoresfile = fdopen(fd, "r");
3472 if (ignoresfile == NULL)
3473 err = got_error_from_errno2("fdopen",
3474 ignorespath);
3475 else {
3476 fd = -1;
3477 err = read_ignores(ignores, path, ignoresfile);
3480 } else {
3481 ignoresfile = fopen(ignorespath, "re");
3482 if (ignoresfile == NULL) {
3483 if (errno != ENOENT && errno != EACCES)
3484 err = got_error_from_errno2("fopen",
3485 ignorespath);
3486 } else
3487 err = read_ignores(ignores, path, ignoresfile);
3490 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3491 err = got_error_from_errno2("fclose", path);
3492 if (fd != -1 && close(fd) == -1 && err == NULL)
3493 err = got_error_from_errno2("close", path);
3494 free(ignorespath);
3495 return err;
3498 static const struct got_error *
3499 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3500 int dirfd)
3502 const struct got_error *err = NULL;
3503 struct diff_dir_cb_arg *a = arg;
3504 char *path = NULL;
3506 if (ignore != NULL)
3507 *ignore = 0;
3509 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3510 return got_error(GOT_ERR_CANCELLED);
3512 if (parent_path[0]) {
3513 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3514 return got_error_from_errno("asprintf");
3515 } else {
3516 path = de->d_name;
3519 if (de->d_type == DT_DIR) {
3520 if (!a->no_ignores && ignore != NULL &&
3521 match_ignores(a->ignores, path))
3522 *ignore = 1;
3523 } else if (!match_ignores(a->ignores, path) &&
3524 got_path_is_child(path, a->status_path, a->status_path_len))
3525 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3526 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3527 if (parent_path[0])
3528 free(path);
3529 return err;
3532 static const struct got_error *
3533 status_traverse(void *arg, const char *path, int dirfd)
3535 const struct got_error *err = NULL;
3536 struct diff_dir_cb_arg *a = arg;
3538 if (a->no_ignores)
3539 return NULL;
3541 err = add_ignores(a->ignores, a->worktree->root_path,
3542 path, dirfd, ".cvsignore");
3543 if (err)
3544 return err;
3546 err = add_ignores(a->ignores, a->worktree->root_path, path,
3547 dirfd, ".gitignore");
3549 return err;
3552 static const struct got_error *
3553 report_single_file_status(const char *path, const char *ondisk_path,
3554 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3555 void *status_arg, struct got_repository *repo, int report_unchanged,
3556 struct got_pathlist_head *ignores, int no_ignores)
3558 struct got_fileindex_entry *ie;
3559 struct stat sb;
3561 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3562 if (ie)
3563 return report_file_status(ie, ondisk_path, -1, NULL,
3564 status_cb, status_arg, repo, report_unchanged);
3566 if (lstat(ondisk_path, &sb) == -1) {
3567 if (errno != ENOENT)
3568 return got_error_from_errno2("lstat", ondisk_path);
3569 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3570 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3573 if (!no_ignores && match_ignores(ignores, path))
3574 return NULL;
3576 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3577 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3578 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3580 return NULL;
3583 static const struct got_error *
3584 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3585 const char *root_path, const char *path)
3587 const struct got_error *err;
3588 char *parent_path, *next_parent_path = NULL;
3590 err = add_ignores(ignores, root_path, "", -1,
3591 ".cvsignore");
3592 if (err)
3593 return err;
3595 err = add_ignores(ignores, root_path, "", -1,
3596 ".gitignore");
3597 if (err)
3598 return err;
3600 err = got_path_dirname(&parent_path, path);
3601 if (err) {
3602 if (err->code == GOT_ERR_BAD_PATH)
3603 return NULL; /* cannot traverse parent */
3604 return err;
3606 for (;;) {
3607 err = add_ignores(ignores, root_path, parent_path, -1,
3608 ".cvsignore");
3609 if (err)
3610 break;
3611 err = add_ignores(ignores, root_path, parent_path, -1,
3612 ".gitignore");
3613 if (err)
3614 break;
3615 err = got_path_dirname(&next_parent_path, parent_path);
3616 if (err) {
3617 if (err->code == GOT_ERR_BAD_PATH)
3618 err = NULL; /* traversed everything */
3619 break;
3621 if (got_path_is_root_dir(parent_path))
3622 break;
3623 free(parent_path);
3624 parent_path = next_parent_path;
3625 next_parent_path = NULL;
3628 free(parent_path);
3629 free(next_parent_path);
3630 return err;
3633 static const struct got_error *
3634 worktree_status(struct got_worktree *worktree, const char *path,
3635 struct got_fileindex *fileindex, struct got_repository *repo,
3636 got_worktree_status_cb status_cb, void *status_arg,
3637 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3638 int report_unchanged)
3640 const struct got_error *err = NULL;
3641 int fd = -1;
3642 struct got_fileindex_diff_dir_cb fdiff_cb;
3643 struct diff_dir_cb_arg arg;
3644 char *ondisk_path = NULL;
3645 struct got_pathlist_head ignores;
3647 TAILQ_INIT(&ignores);
3649 if (asprintf(&ondisk_path, "%s%s%s",
3650 worktree->root_path, path[0] ? "/" : "", path) == -1)
3651 return got_error_from_errno("asprintf");
3653 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3654 if (fd == -1) {
3655 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3656 !got_err_open_nofollow_on_symlink())
3657 err = got_error_from_errno2("open", ondisk_path);
3658 else {
3659 if (!no_ignores) {
3660 err = add_ignores_from_parent_paths(&ignores,
3661 worktree->root_path, ondisk_path);
3662 if (err)
3663 goto done;
3665 err = report_single_file_status(path, ondisk_path,
3666 fileindex, status_cb, status_arg, repo,
3667 report_unchanged, &ignores, no_ignores);
3669 } else {
3670 fdiff_cb.diff_old_new = status_old_new;
3671 fdiff_cb.diff_old = status_old;
3672 fdiff_cb.diff_new = status_new;
3673 fdiff_cb.diff_traverse = status_traverse;
3674 arg.fileindex = fileindex;
3675 arg.worktree = worktree;
3676 arg.status_path = path;
3677 arg.status_path_len = strlen(path);
3678 arg.repo = repo;
3679 arg.status_cb = status_cb;
3680 arg.status_arg = status_arg;
3681 arg.cancel_cb = cancel_cb;
3682 arg.cancel_arg = cancel_arg;
3683 arg.report_unchanged = report_unchanged;
3684 arg.no_ignores = no_ignores;
3685 if (!no_ignores) {
3686 err = add_ignores_from_parent_paths(&ignores,
3687 worktree->root_path, path);
3688 if (err)
3689 goto done;
3691 arg.ignores = &ignores;
3692 err = got_fileindex_diff_dir(fileindex, fd,
3693 worktree->root_path, path, repo, &fdiff_cb, &arg);
3695 done:
3696 free_ignores(&ignores);
3697 if (fd != -1 && close(fd) == -1 && err == NULL)
3698 err = got_error_from_errno("close");
3699 free(ondisk_path);
3700 return err;
3703 const struct got_error *
3704 got_worktree_status(struct got_worktree *worktree,
3705 struct got_pathlist_head *paths, struct got_repository *repo,
3706 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3707 got_cancel_cb cancel_cb, void *cancel_arg)
3709 const struct got_error *err = NULL;
3710 char *fileindex_path = NULL;
3711 struct got_fileindex *fileindex = NULL;
3712 struct got_pathlist_entry *pe;
3714 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3715 if (err)
3716 return err;
3718 TAILQ_FOREACH(pe, paths, entry) {
3719 err = worktree_status(worktree, pe->path, fileindex, repo,
3720 status_cb, status_arg, cancel_cb, cancel_arg,
3721 no_ignores, 0);
3722 if (err)
3723 break;
3725 free(fileindex_path);
3726 got_fileindex_free(fileindex);
3727 return err;
3730 const struct got_error *
3731 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3732 const char *arg)
3734 const struct got_error *err = NULL;
3735 char *resolved = NULL, *cwd = NULL, *path = NULL;
3736 size_t len;
3737 struct stat sb;
3738 char *abspath = NULL;
3739 char canonpath[PATH_MAX];
3741 *wt_path = NULL;
3743 cwd = getcwd(NULL, 0);
3744 if (cwd == NULL)
3745 return got_error_from_errno("getcwd");
3747 if (lstat(arg, &sb) == -1) {
3748 if (errno != ENOENT) {
3749 err = got_error_from_errno2("lstat", arg);
3750 goto done;
3752 sb.st_mode = 0;
3754 if (S_ISLNK(sb.st_mode)) {
3756 * We cannot use realpath(3) with symlinks since we want to
3757 * operate on the symlink itself.
3758 * But we can make the path absolute, assuming it is relative
3759 * to the current working directory, and then canonicalize it.
3761 if (!got_path_is_absolute(arg)) {
3762 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3763 err = got_error_from_errno("asprintf");
3764 goto done;
3768 err = got_canonpath(abspath ? abspath : arg, canonpath,
3769 sizeof(canonpath));
3770 if (err)
3771 goto done;
3772 resolved = strdup(canonpath);
3773 if (resolved == NULL) {
3774 err = got_error_from_errno("strdup");
3775 goto done;
3777 } else {
3778 resolved = realpath(arg, NULL);
3779 if (resolved == NULL) {
3780 if (errno != ENOENT) {
3781 err = got_error_from_errno2("realpath", arg);
3782 goto done;
3784 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3785 err = got_error_from_errno("asprintf");
3786 goto done;
3788 err = got_canonpath(abspath, canonpath,
3789 sizeof(canonpath));
3790 if (err)
3791 goto done;
3792 resolved = strdup(canonpath);
3793 if (resolved == NULL) {
3794 err = got_error_from_errno("strdup");
3795 goto done;
3800 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3801 strlen(got_worktree_get_root_path(worktree)))) {
3802 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3803 goto done;
3806 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3807 err = got_path_skip_common_ancestor(&path,
3808 got_worktree_get_root_path(worktree), resolved);
3809 if (err)
3810 goto done;
3811 } else {
3812 path = strdup("");
3813 if (path == NULL) {
3814 err = got_error_from_errno("strdup");
3815 goto done;
3819 /* XXX status walk can't deal with trailing slash! */
3820 len = strlen(path);
3821 while (len > 0 && path[len - 1] == '/') {
3822 path[len - 1] = '\0';
3823 len--;
3825 done:
3826 free(abspath);
3827 free(resolved);
3828 free(cwd);
3829 if (err == NULL)
3830 *wt_path = path;
3831 else
3832 free(path);
3833 return err;
3836 struct schedule_addition_args {
3837 struct got_worktree *worktree;
3838 struct got_fileindex *fileindex;
3839 got_worktree_checkout_cb progress_cb;
3840 void *progress_arg;
3841 struct got_repository *repo;
3844 static const struct got_error *
3845 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3846 const char *relpath, struct got_object_id *blob_id,
3847 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3848 int dirfd, const char *de_name)
3850 struct schedule_addition_args *a = arg;
3851 const struct got_error *err = NULL;
3852 struct got_fileindex_entry *ie;
3853 struct stat sb;
3854 char *ondisk_path;
3856 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3857 relpath) == -1)
3858 return got_error_from_errno("asprintf");
3860 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3861 if (ie) {
3862 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3863 de_name, a->repo);
3864 if (err)
3865 goto done;
3866 /* Re-adding an existing entry is a no-op. */
3867 if (status == GOT_STATUS_ADD)
3868 goto done;
3869 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3870 if (err)
3871 goto done;
3874 if (status != GOT_STATUS_UNVERSIONED) {
3875 if (status == GOT_STATUS_NONEXISTENT)
3876 err = got_error_set_errno(ENOENT, ondisk_path);
3877 else
3878 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3879 goto done;
3882 err = got_fileindex_entry_alloc(&ie, relpath);
3883 if (err)
3884 goto done;
3885 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3886 relpath, NULL, NULL, 1);
3887 if (err) {
3888 got_fileindex_entry_free(ie);
3889 goto done;
3891 err = got_fileindex_entry_add(a->fileindex, ie);
3892 if (err) {
3893 got_fileindex_entry_free(ie);
3894 goto done;
3896 done:
3897 free(ondisk_path);
3898 if (err)
3899 return err;
3900 if (status == GOT_STATUS_ADD)
3901 return NULL;
3902 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3905 const struct got_error *
3906 got_worktree_schedule_add(struct got_worktree *worktree,
3907 struct got_pathlist_head *paths,
3908 got_worktree_checkout_cb progress_cb, void *progress_arg,
3909 struct got_repository *repo, int no_ignores)
3911 struct got_fileindex *fileindex = NULL;
3912 char *fileindex_path = NULL;
3913 const struct got_error *err = NULL, *sync_err, *unlockerr;
3914 struct got_pathlist_entry *pe;
3915 struct schedule_addition_args saa;
3917 err = lock_worktree(worktree, LOCK_EX);
3918 if (err)
3919 return err;
3921 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3922 if (err)
3923 goto done;
3925 saa.worktree = worktree;
3926 saa.fileindex = fileindex;
3927 saa.progress_cb = progress_cb;
3928 saa.progress_arg = progress_arg;
3929 saa.repo = repo;
3931 TAILQ_FOREACH(pe, paths, entry) {
3932 err = worktree_status(worktree, pe->path, fileindex, repo,
3933 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3934 if (err)
3935 break;
3937 sync_err = sync_fileindex(fileindex, fileindex_path);
3938 if (sync_err && err == NULL)
3939 err = sync_err;
3940 done:
3941 free(fileindex_path);
3942 if (fileindex)
3943 got_fileindex_free(fileindex);
3944 unlockerr = lock_worktree(worktree, LOCK_SH);
3945 if (unlockerr && err == NULL)
3946 err = unlockerr;
3947 return err;
3950 struct schedule_deletion_args {
3951 struct got_worktree *worktree;
3952 struct got_fileindex *fileindex;
3953 got_worktree_delete_cb progress_cb;
3954 void *progress_arg;
3955 struct got_repository *repo;
3956 int delete_local_mods;
3957 int keep_on_disk;
3958 int ignore_missing_paths;
3959 const char *status_codes;
3962 static const struct got_error *
3963 schedule_for_deletion(void *arg, unsigned char status,
3964 unsigned char staged_status, const char *relpath,
3965 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3966 struct got_object_id *commit_id, int dirfd, const char *de_name)
3968 struct schedule_deletion_args *a = arg;
3969 const struct got_error *err = NULL;
3970 struct got_fileindex_entry *ie = NULL;
3971 struct stat sb;
3972 char *ondisk_path;
3974 if (status == GOT_STATUS_NONEXISTENT) {
3975 if (a->ignore_missing_paths)
3976 return NULL;
3977 return got_error_set_errno(ENOENT, relpath);
3980 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3981 if (ie == NULL)
3982 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
3984 staged_status = get_staged_status(ie);
3985 if (staged_status != GOT_STATUS_NO_CHANGE) {
3986 if (staged_status == GOT_STATUS_DELETE)
3987 return NULL;
3988 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3991 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3992 relpath) == -1)
3993 return got_error_from_errno("asprintf");
3995 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3996 a->repo);
3997 if (err)
3998 goto done;
4000 if (a->status_codes) {
4001 size_t ncodes = strlen(a->status_codes);
4002 int i;
4003 for (i = 0; i < ncodes ; i++) {
4004 if (status == a->status_codes[i])
4005 break;
4007 if (i == ncodes) {
4008 /* Do not delete files in non-matching status. */
4009 free(ondisk_path);
4010 return NULL;
4012 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4013 a->status_codes[i] != GOT_STATUS_MISSING) {
4014 static char msg[64];
4015 snprintf(msg, sizeof(msg),
4016 "invalid status code '%c'", a->status_codes[i]);
4017 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4018 goto done;
4022 if (status != GOT_STATUS_NO_CHANGE) {
4023 if (status == GOT_STATUS_DELETE)
4024 goto done;
4025 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4026 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4027 goto done;
4029 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4030 err = got_error_set_errno(ENOENT, relpath);
4031 goto done;
4033 if (status != GOT_STATUS_MODIFY &&
4034 status != GOT_STATUS_MISSING) {
4035 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4036 goto done;
4040 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4041 size_t root_len;
4043 if (dirfd != -1) {
4044 if (unlinkat(dirfd, de_name, 0) != 0) {
4045 err = got_error_from_errno2("unlinkat",
4046 ondisk_path);
4047 goto done;
4049 } else if (unlink(ondisk_path) != 0) {
4050 err = got_error_from_errno2("unlink", ondisk_path);
4051 goto done;
4054 root_len = strlen(a->worktree->root_path);
4055 do {
4056 char *parent;
4057 err = got_path_dirname(&parent, ondisk_path);
4058 if (err)
4059 goto done;
4060 free(ondisk_path);
4061 ondisk_path = parent;
4062 if (rmdir(ondisk_path) == -1) {
4063 if (errno != ENOTEMPTY)
4064 err = got_error_from_errno2("rmdir",
4065 ondisk_path);
4066 break;
4068 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4069 strlen(ondisk_path), root_len) != 0);
4072 got_fileindex_entry_mark_deleted_from_disk(ie);
4073 done:
4074 free(ondisk_path);
4075 if (err)
4076 return err;
4077 if (status == GOT_STATUS_DELETE)
4078 return NULL;
4079 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4080 staged_status, relpath);
4083 const struct got_error *
4084 got_worktree_schedule_delete(struct got_worktree *worktree,
4085 struct got_pathlist_head *paths, int delete_local_mods,
4086 const char *status_codes,
4087 got_worktree_delete_cb progress_cb, void *progress_arg,
4088 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4090 struct got_fileindex *fileindex = NULL;
4091 char *fileindex_path = NULL;
4092 const struct got_error *err = NULL, *sync_err, *unlockerr;
4093 struct got_pathlist_entry *pe;
4094 struct schedule_deletion_args sda;
4096 err = lock_worktree(worktree, LOCK_EX);
4097 if (err)
4098 return err;
4100 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4101 if (err)
4102 goto done;
4104 sda.worktree = worktree;
4105 sda.fileindex = fileindex;
4106 sda.progress_cb = progress_cb;
4107 sda.progress_arg = progress_arg;
4108 sda.repo = repo;
4109 sda.delete_local_mods = delete_local_mods;
4110 sda.keep_on_disk = keep_on_disk;
4111 sda.ignore_missing_paths = ignore_missing_paths;
4112 sda.status_codes = status_codes;
4114 TAILQ_FOREACH(pe, paths, entry) {
4115 err = worktree_status(worktree, pe->path, fileindex, repo,
4116 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4117 if (err)
4118 break;
4120 sync_err = sync_fileindex(fileindex, fileindex_path);
4121 if (sync_err && err == NULL)
4122 err = sync_err;
4123 done:
4124 free(fileindex_path);
4125 if (fileindex)
4126 got_fileindex_free(fileindex);
4127 unlockerr = lock_worktree(worktree, LOCK_SH);
4128 if (unlockerr && err == NULL)
4129 err = unlockerr;
4130 return err;
4133 static const struct got_error *
4134 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4136 const struct got_error *err = NULL;
4137 char *line = NULL;
4138 size_t linesize = 0, n;
4139 ssize_t linelen;
4141 linelen = getline(&line, &linesize, infile);
4142 if (linelen == -1) {
4143 if (ferror(infile)) {
4144 err = got_error_from_errno("getline");
4145 goto done;
4147 return NULL;
4149 if (outfile) {
4150 n = fwrite(line, 1, linelen, outfile);
4151 if (n != linelen) {
4152 err = got_ferror(outfile, GOT_ERR_IO);
4153 goto done;
4156 if (rejectfile) {
4157 n = fwrite(line, 1, linelen, rejectfile);
4158 if (n != linelen)
4159 err = got_ferror(outfile, GOT_ERR_IO);
4161 done:
4162 free(line);
4163 return err;
4166 static const struct got_error *
4167 skip_one_line(FILE *f)
4169 char *line = NULL;
4170 size_t linesize = 0;
4171 ssize_t linelen;
4173 linelen = getline(&line, &linesize, f);
4174 if (linelen == -1) {
4175 if (ferror(f))
4176 return got_error_from_errno("getline");
4177 return NULL;
4179 free(line);
4180 return NULL;
4183 static const struct got_error *
4184 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4185 int start_old, int end_old, int start_new, int end_new,
4186 FILE *outfile, FILE *rejectfile)
4188 const struct got_error *err;
4190 /* Copy old file's lines leading up to patch. */
4191 while (!feof(f1) && *line_cur1 < start_old) {
4192 err = copy_one_line(f1, outfile, NULL);
4193 if (err)
4194 return err;
4195 (*line_cur1)++;
4197 /* Skip new file's lines leading up to patch. */
4198 while (!feof(f2) && *line_cur2 < start_new) {
4199 if (rejectfile)
4200 err = copy_one_line(f2, NULL, rejectfile);
4201 else
4202 err = skip_one_line(f2);
4203 if (err)
4204 return err;
4205 (*line_cur2)++;
4207 /* Copy patched lines. */
4208 while (!feof(f2) && *line_cur2 <= end_new) {
4209 err = copy_one_line(f2, outfile, NULL);
4210 if (err)
4211 return err;
4212 (*line_cur2)++;
4214 /* Skip over old file's replaced lines. */
4215 while (!feof(f1) && *line_cur1 <= end_old) {
4216 if (rejectfile)
4217 err = copy_one_line(f1, NULL, rejectfile);
4218 else
4219 err = skip_one_line(f1);
4220 if (err)
4221 return err;
4222 (*line_cur1)++;
4225 return NULL;
4228 static const struct got_error *
4229 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4230 FILE *outfile, FILE *rejectfile)
4232 const struct got_error *err;
4234 if (outfile) {
4235 /* Copy old file's lines until EOF. */
4236 while (!feof(f1)) {
4237 err = copy_one_line(f1, outfile, NULL);
4238 if (err)
4239 return err;
4240 (*line_cur1)++;
4243 if (rejectfile) {
4244 /* Copy new file's lines until EOF. */
4245 while (!feof(f2)) {
4246 err = copy_one_line(f2, NULL, rejectfile);
4247 if (err)
4248 return err;
4249 (*line_cur2)++;
4253 return NULL;
4256 static const struct got_error *
4257 apply_or_reject_change(int *choice, int *nchunks_used,
4258 struct diff_result *diff_result, int n,
4259 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4260 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4261 got_worktree_patch_cb patch_cb, void *patch_arg)
4263 const struct got_error *err = NULL;
4264 struct diff_chunk_context cc = {};
4265 int start_old, end_old, start_new, end_new;
4266 FILE *hunkfile;
4267 struct diff_output_unidiff_state *diff_state;
4268 struct diff_input_info diff_info;
4269 int rc;
4271 *choice = GOT_PATCH_CHOICE_NONE;
4273 /* Get changed line numbers without context lines for copy_change(). */
4274 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4275 start_old = cc.left.start;
4276 end_old = cc.left.end;
4277 start_new = cc.right.start;
4278 end_new = cc.right.end;
4280 /* Get the same change with context lines for display. */
4281 memset(&cc, 0, sizeof(cc));
4282 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4284 memset(&diff_info, 0, sizeof(diff_info));
4285 diff_info.left_path = relpath;
4286 diff_info.right_path = relpath;
4288 diff_state = diff_output_unidiff_state_alloc();
4289 if (diff_state == NULL)
4290 return got_error_set_errno(ENOMEM,
4291 "diff_output_unidiff_state_alloc");
4293 hunkfile = got_opentemp();
4294 if (hunkfile == NULL) {
4295 err = got_error_from_errno("got_opentemp");
4296 goto done;
4299 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4300 diff_result, &cc);
4301 if (rc != DIFF_RC_OK) {
4302 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4303 goto done;
4306 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4307 err = got_ferror(hunkfile, GOT_ERR_IO);
4308 goto done;
4311 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4312 hunkfile, changeno, nchanges);
4313 if (err)
4314 goto done;
4316 switch (*choice) {
4317 case GOT_PATCH_CHOICE_YES:
4318 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4319 end_old, start_new, end_new, outfile, rejectfile);
4320 break;
4321 case GOT_PATCH_CHOICE_NO:
4322 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4323 end_old, start_new, end_new, rejectfile, outfile);
4324 break;
4325 case GOT_PATCH_CHOICE_QUIT:
4326 break;
4327 default:
4328 err = got_error(GOT_ERR_PATCH_CHOICE);
4329 break;
4331 done:
4332 diff_output_unidiff_state_free(diff_state);
4333 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4334 err = got_error_from_errno("fclose");
4335 return err;
4338 struct revert_file_args {
4339 struct got_worktree *worktree;
4340 struct got_fileindex *fileindex;
4341 got_worktree_checkout_cb progress_cb;
4342 void *progress_arg;
4343 got_worktree_patch_cb patch_cb;
4344 void *patch_arg;
4345 struct got_repository *repo;
4346 int unlink_added_files;
4349 static const struct got_error *
4350 create_patched_content(char **path_outfile, int reverse_patch,
4351 struct got_object_id *blob_id, const char *path2,
4352 int dirfd2, const char *de_name2,
4353 const char *relpath, struct got_repository *repo,
4354 got_worktree_patch_cb patch_cb, void *patch_arg)
4356 const struct got_error *err, *free_err;
4357 struct got_blob_object *blob = NULL;
4358 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4359 int fd2 = -1;
4360 char link_target[PATH_MAX];
4361 ssize_t link_len = 0;
4362 char *path1 = NULL, *id_str = NULL;
4363 struct stat sb2;
4364 struct got_diffreg_result *diffreg_result = NULL;
4365 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4366 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4368 *path_outfile = NULL;
4370 err = got_object_id_str(&id_str, blob_id);
4371 if (err)
4372 return err;
4374 if (dirfd2 != -1) {
4375 fd2 = openat(dirfd2, de_name2,
4376 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4377 if (fd2 == -1) {
4378 if (!got_err_open_nofollow_on_symlink()) {
4379 err = got_error_from_errno2("openat", path2);
4380 goto done;
4382 link_len = readlinkat(dirfd2, de_name2,
4383 link_target, sizeof(link_target));
4384 if (link_len == -1) {
4385 return got_error_from_errno2("readlinkat",
4386 path2);
4388 sb2.st_mode = S_IFLNK;
4389 sb2.st_size = link_len;
4391 } else {
4392 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4393 if (fd2 == -1) {
4394 if (!got_err_open_nofollow_on_symlink()) {
4395 err = got_error_from_errno2("open", path2);
4396 goto done;
4398 link_len = readlink(path2, link_target,
4399 sizeof(link_target));
4400 if (link_len == -1)
4401 return got_error_from_errno2("readlink", path2);
4402 sb2.st_mode = S_IFLNK;
4403 sb2.st_size = link_len;
4406 if (fd2 != -1) {
4407 if (fstat(fd2, &sb2) == -1) {
4408 err = got_error_from_errno2("fstat", path2);
4409 goto done;
4412 f2 = fdopen(fd2, "r");
4413 if (f2 == NULL) {
4414 err = got_error_from_errno2("fdopen", path2);
4415 goto done;
4417 fd2 = -1;
4418 } else {
4419 size_t n;
4420 f2 = got_opentemp();
4421 if (f2 == NULL) {
4422 err = got_error_from_errno2("got_opentemp", path2);
4423 goto done;
4425 n = fwrite(link_target, 1, link_len, f2);
4426 if (n != link_len) {
4427 err = got_ferror(f2, GOT_ERR_IO);
4428 goto done;
4430 if (fflush(f2) == EOF) {
4431 err = got_error_from_errno("fflush");
4432 goto done;
4434 rewind(f2);
4437 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4438 if (err)
4439 goto done;
4441 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4442 if (err)
4443 goto done;
4445 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4446 if (err)
4447 goto done;
4449 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4450 NULL);
4451 if (err)
4452 goto done;
4454 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4455 if (err)
4456 goto done;
4458 if (fseek(f1, 0L, SEEK_SET) == -1)
4459 return got_ferror(f1, GOT_ERR_IO);
4460 if (fseek(f2, 0L, SEEK_SET) == -1)
4461 return got_ferror(f2, GOT_ERR_IO);
4463 /* Count the number of actual changes in the diff result. */
4464 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4465 struct diff_chunk_context cc = {};
4466 diff_chunk_context_load_change(&cc, &nchunks_used,
4467 diffreg_result->result, n, 0);
4468 nchanges++;
4470 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4471 int choice;
4472 err = apply_or_reject_change(&choice, &nchunks_used,
4473 diffreg_result->result, n, relpath, f1, f2,
4474 &line_cur1, &line_cur2,
4475 reverse_patch ? NULL : outfile,
4476 reverse_patch ? outfile : NULL,
4477 ++i, nchanges, patch_cb, patch_arg);
4478 if (err)
4479 goto done;
4480 if (choice == GOT_PATCH_CHOICE_YES)
4481 have_content = 1;
4482 else if (choice == GOT_PATCH_CHOICE_QUIT)
4483 break;
4485 if (have_content) {
4486 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4487 reverse_patch ? NULL : outfile,
4488 reverse_patch ? outfile : NULL);
4489 if (err)
4490 goto done;
4492 if (!S_ISLNK(sb2.st_mode)) {
4493 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4494 err = got_error_from_errno2("fchmod", path2);
4495 goto done;
4499 done:
4500 free(id_str);
4501 if (blob)
4502 got_object_blob_close(blob);
4503 free_err = got_diffreg_result_free(diffreg_result);
4504 if (err == NULL)
4505 err = free_err;
4506 if (f1 && fclose(f1) == EOF && err == NULL)
4507 err = got_error_from_errno2("fclose", path1);
4508 if (f2 && fclose(f2) == EOF && err == NULL)
4509 err = got_error_from_errno2("fclose", path2);
4510 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4511 err = got_error_from_errno2("close", path2);
4512 if (outfile && fclose(outfile) == EOF && err == NULL)
4513 err = got_error_from_errno2("fclose", *path_outfile);
4514 if (path1 && unlink(path1) == -1 && err == NULL)
4515 err = got_error_from_errno2("unlink", path1);
4516 if (err || !have_content) {
4517 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4518 err = got_error_from_errno2("unlink", *path_outfile);
4519 free(*path_outfile);
4520 *path_outfile = NULL;
4522 free(path1);
4523 return err;
4526 static const struct got_error *
4527 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4528 const char *relpath, struct got_object_id *blob_id,
4529 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4530 int dirfd, const char *de_name)
4532 struct revert_file_args *a = arg;
4533 const struct got_error *err = NULL;
4534 char *parent_path = NULL;
4535 struct got_fileindex_entry *ie;
4536 struct got_tree_object *tree = NULL;
4537 struct got_object_id *tree_id = NULL;
4538 const struct got_tree_entry *te = NULL;
4539 char *tree_path = NULL, *te_name;
4540 char *ondisk_path = NULL, *path_content = NULL;
4541 struct got_blob_object *blob = NULL;
4543 /* Reverting a staged deletion is a no-op. */
4544 if (status == GOT_STATUS_DELETE &&
4545 staged_status != GOT_STATUS_NO_CHANGE)
4546 return NULL;
4548 if (status == GOT_STATUS_UNVERSIONED)
4549 return (*a->progress_cb)(a->progress_arg,
4550 GOT_STATUS_UNVERSIONED, relpath);
4552 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4553 if (ie == NULL)
4554 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4556 /* Construct in-repository path of tree which contains this blob. */
4557 err = got_path_dirname(&parent_path, ie->path);
4558 if (err) {
4559 if (err->code != GOT_ERR_BAD_PATH)
4560 goto done;
4561 parent_path = strdup("/");
4562 if (parent_path == NULL) {
4563 err = got_error_from_errno("strdup");
4564 goto done;
4567 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4568 tree_path = strdup(parent_path);
4569 if (tree_path == NULL) {
4570 err = got_error_from_errno("strdup");
4571 goto done;
4573 } else {
4574 if (got_path_is_root_dir(parent_path)) {
4575 tree_path = strdup(a->worktree->path_prefix);
4576 if (tree_path == NULL) {
4577 err = got_error_from_errno("strdup");
4578 goto done;
4580 } else {
4581 if (asprintf(&tree_path, "%s/%s",
4582 a->worktree->path_prefix, parent_path) == -1) {
4583 err = got_error_from_errno("asprintf");
4584 goto done;
4589 err = got_object_id_by_path(&tree_id, a->repo,
4590 a->worktree->base_commit_id, tree_path);
4591 if (err) {
4592 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4593 (status == GOT_STATUS_ADD ||
4594 staged_status == GOT_STATUS_ADD)))
4595 goto done;
4596 } else {
4597 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4598 if (err)
4599 goto done;
4601 err = got_path_basename(&te_name, ie->path);
4602 if (err)
4603 goto done;
4605 te = got_object_tree_find_entry(tree, te_name);
4606 free(te_name);
4607 if (te == NULL && status != GOT_STATUS_ADD &&
4608 staged_status != GOT_STATUS_ADD) {
4609 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4610 goto done;
4614 switch (status) {
4615 case GOT_STATUS_ADD:
4616 if (a->patch_cb) {
4617 int choice = GOT_PATCH_CHOICE_NONE;
4618 err = (*a->patch_cb)(&choice, a->patch_arg,
4619 status, ie->path, NULL, 1, 1);
4620 if (err)
4621 goto done;
4622 if (choice != GOT_PATCH_CHOICE_YES)
4623 break;
4625 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4626 ie->path);
4627 if (err)
4628 goto done;
4629 got_fileindex_entry_remove(a->fileindex, ie);
4630 if (a->unlink_added_files) {
4631 if (asprintf(&ondisk_path, "%s/%s",
4632 got_worktree_get_root_path(a->worktree),
4633 relpath) == -1) {
4634 err = got_error_from_errno("asprintf");
4635 goto done;
4637 if (unlink(ondisk_path) == -1) {
4638 err = got_error_from_errno2("unlink",
4639 ondisk_path);
4640 break;
4643 break;
4644 case GOT_STATUS_DELETE:
4645 if (a->patch_cb) {
4646 int choice = GOT_PATCH_CHOICE_NONE;
4647 err = (*a->patch_cb)(&choice, a->patch_arg,
4648 status, ie->path, NULL, 1, 1);
4649 if (err)
4650 goto done;
4651 if (choice != GOT_PATCH_CHOICE_YES)
4652 break;
4654 /* fall through */
4655 case GOT_STATUS_MODIFY:
4656 case GOT_STATUS_MODE_CHANGE:
4657 case GOT_STATUS_CONFLICT:
4658 case GOT_STATUS_MISSING: {
4659 struct got_object_id id;
4660 if (staged_status == GOT_STATUS_ADD ||
4661 staged_status == GOT_STATUS_MODIFY) {
4662 memcpy(id.sha1, ie->staged_blob_sha1,
4663 SHA1_DIGEST_LENGTH);
4664 } else
4665 memcpy(id.sha1, ie->blob_sha1,
4666 SHA1_DIGEST_LENGTH);
4667 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4668 if (err)
4669 goto done;
4671 if (asprintf(&ondisk_path, "%s/%s",
4672 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4673 err = got_error_from_errno("asprintf");
4674 goto done;
4677 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4678 status == GOT_STATUS_CONFLICT)) {
4679 int is_bad_symlink = 0;
4680 err = create_patched_content(&path_content, 1, &id,
4681 ondisk_path, dirfd, de_name, ie->path, a->repo,
4682 a->patch_cb, a->patch_arg);
4683 if (err || path_content == NULL)
4684 break;
4685 if (te && S_ISLNK(te->mode)) {
4686 if (unlink(path_content) == -1) {
4687 err = got_error_from_errno2("unlink",
4688 path_content);
4689 break;
4691 err = install_symlink(&is_bad_symlink,
4692 a->worktree, ondisk_path, ie->path,
4693 blob, 0, 1, 0, 0, a->repo,
4694 a->progress_cb, a->progress_arg);
4695 } else {
4696 if (rename(path_content, ondisk_path) == -1) {
4697 err = got_error_from_errno3("rename",
4698 path_content, ondisk_path);
4699 goto done;
4702 } else {
4703 int is_bad_symlink = 0;
4704 if (te && S_ISLNK(te->mode)) {
4705 err = install_symlink(&is_bad_symlink,
4706 a->worktree, ondisk_path, ie->path,
4707 blob, 0, 1, 0, 0, a->repo,
4708 a->progress_cb, a->progress_arg);
4709 } else {
4710 err = install_blob(a->worktree, ondisk_path,
4711 ie->path,
4712 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4713 got_fileindex_perms_to_st(ie), blob,
4714 0, 1, 0, 0, a->repo,
4715 a->progress_cb, a->progress_arg);
4717 if (err)
4718 goto done;
4719 if (status == GOT_STATUS_DELETE ||
4720 status == GOT_STATUS_MODE_CHANGE) {
4721 err = got_fileindex_entry_update(ie,
4722 a->worktree->root_fd, relpath,
4723 blob->id.sha1,
4724 a->worktree->base_commit_id->sha1, 1);
4725 if (err)
4726 goto done;
4728 if (is_bad_symlink) {
4729 got_fileindex_entry_filetype_set(ie,
4730 GOT_FILEIDX_MODE_BAD_SYMLINK);
4733 break;
4735 default:
4736 break;
4738 done:
4739 free(ondisk_path);
4740 free(path_content);
4741 free(parent_path);
4742 free(tree_path);
4743 if (blob)
4744 got_object_blob_close(blob);
4745 if (tree)
4746 got_object_tree_close(tree);
4747 free(tree_id);
4748 return err;
4751 const struct got_error *
4752 got_worktree_revert(struct got_worktree *worktree,
4753 struct got_pathlist_head *paths,
4754 got_worktree_checkout_cb progress_cb, void *progress_arg,
4755 got_worktree_patch_cb patch_cb, void *patch_arg,
4756 struct got_repository *repo)
4758 struct got_fileindex *fileindex = NULL;
4759 char *fileindex_path = NULL;
4760 const struct got_error *err = NULL, *unlockerr = NULL;
4761 const struct got_error *sync_err = NULL;
4762 struct got_pathlist_entry *pe;
4763 struct revert_file_args rfa;
4765 err = lock_worktree(worktree, LOCK_EX);
4766 if (err)
4767 return err;
4769 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4770 if (err)
4771 goto done;
4773 rfa.worktree = worktree;
4774 rfa.fileindex = fileindex;
4775 rfa.progress_cb = progress_cb;
4776 rfa.progress_arg = progress_arg;
4777 rfa.patch_cb = patch_cb;
4778 rfa.patch_arg = patch_arg;
4779 rfa.repo = repo;
4780 rfa.unlink_added_files = 0;
4781 TAILQ_FOREACH(pe, paths, entry) {
4782 err = worktree_status(worktree, pe->path, fileindex, repo,
4783 revert_file, &rfa, NULL, NULL, 1, 0);
4784 if (err)
4785 break;
4787 sync_err = sync_fileindex(fileindex, fileindex_path);
4788 if (sync_err && err == NULL)
4789 err = sync_err;
4790 done:
4791 free(fileindex_path);
4792 if (fileindex)
4793 got_fileindex_free(fileindex);
4794 unlockerr = lock_worktree(worktree, LOCK_SH);
4795 if (unlockerr && err == NULL)
4796 err = unlockerr;
4797 return err;
4800 static void
4801 free_commitable(struct got_commitable *ct)
4803 free(ct->path);
4804 free(ct->in_repo_path);
4805 free(ct->ondisk_path);
4806 free(ct->blob_id);
4807 free(ct->base_blob_id);
4808 free(ct->staged_blob_id);
4809 free(ct->base_commit_id);
4810 free(ct);
4813 struct collect_commitables_arg {
4814 struct got_pathlist_head *commitable_paths;
4815 struct got_repository *repo;
4816 struct got_worktree *worktree;
4817 struct got_fileindex *fileindex;
4818 int have_staged_files;
4819 int allow_bad_symlinks;
4822 static const struct got_error *
4823 collect_commitables(void *arg, unsigned char status,
4824 unsigned char staged_status, const char *relpath,
4825 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4826 struct got_object_id *commit_id, int dirfd, const char *de_name)
4828 struct collect_commitables_arg *a = arg;
4829 const struct got_error *err = NULL;
4830 struct got_commitable *ct = NULL;
4831 struct got_pathlist_entry *new = NULL;
4832 char *parent_path = NULL, *path = NULL;
4833 struct stat sb;
4835 if (a->have_staged_files) {
4836 if (staged_status != GOT_STATUS_MODIFY &&
4837 staged_status != GOT_STATUS_ADD &&
4838 staged_status != GOT_STATUS_DELETE)
4839 return NULL;
4840 } else {
4841 if (status == GOT_STATUS_CONFLICT)
4842 return got_error(GOT_ERR_COMMIT_CONFLICT);
4844 if (status != GOT_STATUS_MODIFY &&
4845 status != GOT_STATUS_MODE_CHANGE &&
4846 status != GOT_STATUS_ADD &&
4847 status != GOT_STATUS_DELETE)
4848 return NULL;
4851 if (asprintf(&path, "/%s", relpath) == -1) {
4852 err = got_error_from_errno("asprintf");
4853 goto done;
4855 if (strcmp(path, "/") == 0) {
4856 parent_path = strdup("");
4857 if (parent_path == NULL)
4858 return got_error_from_errno("strdup");
4859 } else {
4860 err = got_path_dirname(&parent_path, path);
4861 if (err)
4862 return err;
4865 ct = calloc(1, sizeof(*ct));
4866 if (ct == NULL) {
4867 err = got_error_from_errno("calloc");
4868 goto done;
4871 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4872 relpath) == -1) {
4873 err = got_error_from_errno("asprintf");
4874 goto done;
4877 if (staged_status == GOT_STATUS_ADD ||
4878 staged_status == GOT_STATUS_MODIFY) {
4879 struct got_fileindex_entry *ie;
4880 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4881 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4882 case GOT_FILEIDX_MODE_REGULAR_FILE:
4883 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4884 ct->mode = S_IFREG;
4885 break;
4886 case GOT_FILEIDX_MODE_SYMLINK:
4887 ct->mode = S_IFLNK;
4888 break;
4889 default:
4890 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4891 goto done;
4893 ct->mode |= got_fileindex_entry_perms_get(ie);
4894 } else if (status != GOT_STATUS_DELETE &&
4895 staged_status != GOT_STATUS_DELETE) {
4896 if (dirfd != -1) {
4897 if (fstatat(dirfd, de_name, &sb,
4898 AT_SYMLINK_NOFOLLOW) == -1) {
4899 err = got_error_from_errno2("fstatat",
4900 ct->ondisk_path);
4901 goto done;
4903 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4904 err = got_error_from_errno2("lstat", ct->ondisk_path);
4905 goto done;
4907 ct->mode = sb.st_mode;
4910 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4911 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4912 relpath) == -1) {
4913 err = got_error_from_errno("asprintf");
4914 goto done;
4917 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4918 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4919 int is_bad_symlink;
4920 char target_path[PATH_MAX];
4921 ssize_t target_len;
4922 target_len = readlink(ct->ondisk_path, target_path,
4923 sizeof(target_path));
4924 if (target_len == -1) {
4925 err = got_error_from_errno2("readlink",
4926 ct->ondisk_path);
4927 goto done;
4929 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4930 target_len, ct->ondisk_path, a->worktree->root_path);
4931 if (err)
4932 goto done;
4933 if (is_bad_symlink) {
4934 err = got_error_path(ct->ondisk_path,
4935 GOT_ERR_BAD_SYMLINK);
4936 goto done;
4941 ct->status = status;
4942 ct->staged_status = staged_status;
4943 ct->blob_id = NULL; /* will be filled in when blob gets created */
4944 if (ct->status != GOT_STATUS_ADD &&
4945 ct->staged_status != GOT_STATUS_ADD) {
4946 ct->base_blob_id = got_object_id_dup(blob_id);
4947 if (ct->base_blob_id == NULL) {
4948 err = got_error_from_errno("got_object_id_dup");
4949 goto done;
4951 ct->base_commit_id = got_object_id_dup(commit_id);
4952 if (ct->base_commit_id == NULL) {
4953 err = got_error_from_errno("got_object_id_dup");
4954 goto done;
4957 if (ct->staged_status == GOT_STATUS_ADD ||
4958 ct->staged_status == GOT_STATUS_MODIFY) {
4959 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4960 if (ct->staged_blob_id == NULL) {
4961 err = got_error_from_errno("got_object_id_dup");
4962 goto done;
4965 ct->path = strdup(path);
4966 if (ct->path == NULL) {
4967 err = got_error_from_errno("strdup");
4968 goto done;
4970 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4971 done:
4972 if (ct && (err || new == NULL))
4973 free_commitable(ct);
4974 free(parent_path);
4975 free(path);
4976 return err;
4979 static const struct got_error *write_tree(struct got_object_id **, int *,
4980 struct got_tree_object *, const char *, struct got_pathlist_head *,
4981 got_worktree_status_cb status_cb, void *status_arg,
4982 struct got_repository *);
4984 static const struct got_error *
4985 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4986 struct got_tree_entry *te, const char *parent_path,
4987 struct got_pathlist_head *commitable_paths,
4988 got_worktree_status_cb status_cb, void *status_arg,
4989 struct got_repository *repo)
4991 const struct got_error *err = NULL;
4992 struct got_tree_object *subtree;
4993 char *subpath;
4995 if (asprintf(&subpath, "%s%s%s", parent_path,
4996 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4997 return got_error_from_errno("asprintf");
4999 err = got_object_open_as_tree(&subtree, repo, &te->id);
5000 if (err)
5001 return err;
5003 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5004 commitable_paths, status_cb, status_arg, repo);
5005 got_object_tree_close(subtree);
5006 free(subpath);
5007 return err;
5010 static const struct got_error *
5011 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5013 const struct got_error *err = NULL;
5014 char *ct_parent_path = NULL;
5016 *match = 0;
5018 if (strchr(ct->in_repo_path, '/') == NULL) {
5019 *match = got_path_is_root_dir(path);
5020 return NULL;
5023 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5024 if (err)
5025 return err;
5026 *match = (strcmp(path, ct_parent_path) == 0);
5027 free(ct_parent_path);
5028 return err;
5031 static mode_t
5032 get_ct_file_mode(struct got_commitable *ct)
5034 if (S_ISLNK(ct->mode))
5035 return S_IFLNK;
5037 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5040 static const struct got_error *
5041 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5042 struct got_tree_entry *te, struct got_commitable *ct)
5044 const struct got_error *err = NULL;
5046 *new_te = NULL;
5048 err = got_object_tree_entry_dup(new_te, te);
5049 if (err)
5050 goto done;
5052 (*new_te)->mode = get_ct_file_mode(ct);
5054 if (ct->staged_status == GOT_STATUS_MODIFY)
5055 memcpy(&(*new_te)->id, ct->staged_blob_id,
5056 sizeof((*new_te)->id));
5057 else
5058 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5059 done:
5060 if (err && *new_te) {
5061 free(*new_te);
5062 *new_te = NULL;
5064 return err;
5067 static const struct got_error *
5068 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5069 struct got_commitable *ct)
5071 const struct got_error *err = NULL;
5072 char *ct_name = NULL;
5074 *new_te = NULL;
5076 *new_te = calloc(1, sizeof(**new_te));
5077 if (*new_te == NULL)
5078 return got_error_from_errno("calloc");
5080 err = got_path_basename(&ct_name, ct->path);
5081 if (err)
5082 goto done;
5083 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5084 sizeof((*new_te)->name)) {
5085 err = got_error(GOT_ERR_NO_SPACE);
5086 goto done;
5089 (*new_te)->mode = get_ct_file_mode(ct);
5091 if (ct->staged_status == GOT_STATUS_ADD)
5092 memcpy(&(*new_te)->id, ct->staged_blob_id,
5093 sizeof((*new_te)->id));
5094 else
5095 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5096 done:
5097 free(ct_name);
5098 if (err && *new_te) {
5099 free(*new_te);
5100 *new_te = NULL;
5102 return err;
5105 static const struct got_error *
5106 insert_tree_entry(struct got_tree_entry *new_te,
5107 struct got_pathlist_head *paths)
5109 const struct got_error *err = NULL;
5110 struct got_pathlist_entry *new_pe;
5112 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5113 if (err)
5114 return err;
5115 if (new_pe == NULL)
5116 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5117 return NULL;
5120 static const struct got_error *
5121 report_ct_status(struct got_commitable *ct,
5122 got_worktree_status_cb status_cb, void *status_arg)
5124 const char *ct_path = ct->path;
5125 unsigned char status;
5127 if (status_cb == NULL) /* no commit progress output desired */
5128 return NULL;
5130 while (ct_path[0] == '/')
5131 ct_path++;
5133 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5134 status = ct->staged_status;
5135 else
5136 status = ct->status;
5138 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5139 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5142 static const struct got_error *
5143 match_modified_subtree(int *modified, struct got_tree_entry *te,
5144 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5146 const struct got_error *err = NULL;
5147 struct got_pathlist_entry *pe;
5148 char *te_path;
5150 *modified = 0;
5152 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5153 got_path_is_root_dir(base_tree_path) ? "" : "/",
5154 te->name) == -1)
5155 return got_error_from_errno("asprintf");
5157 TAILQ_FOREACH(pe, commitable_paths, entry) {
5158 struct got_commitable *ct = pe->data;
5159 *modified = got_path_is_child(ct->in_repo_path, te_path,
5160 strlen(te_path));
5161 if (*modified)
5162 break;
5165 free(te_path);
5166 return err;
5169 static const struct got_error *
5170 match_deleted_or_modified_ct(struct got_commitable **ctp,
5171 struct got_tree_entry *te, const char *base_tree_path,
5172 struct got_pathlist_head *commitable_paths)
5174 const struct got_error *err = NULL;
5175 struct got_pathlist_entry *pe;
5177 *ctp = NULL;
5179 TAILQ_FOREACH(pe, commitable_paths, entry) {
5180 struct got_commitable *ct = pe->data;
5181 char *ct_name = NULL;
5182 int path_matches;
5184 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5185 if (ct->status != GOT_STATUS_MODIFY &&
5186 ct->status != GOT_STATUS_MODE_CHANGE &&
5187 ct->status != GOT_STATUS_DELETE)
5188 continue;
5189 } else {
5190 if (ct->staged_status != GOT_STATUS_MODIFY &&
5191 ct->staged_status != GOT_STATUS_DELETE)
5192 continue;
5195 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5196 continue;
5198 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5199 if (err)
5200 return err;
5201 if (!path_matches)
5202 continue;
5204 err = got_path_basename(&ct_name, pe->path);
5205 if (err)
5206 return err;
5208 if (strcmp(te->name, ct_name) != 0) {
5209 free(ct_name);
5210 continue;
5212 free(ct_name);
5214 *ctp = ct;
5215 break;
5218 return err;
5221 static const struct got_error *
5222 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5223 const char *child_path, const char *path_base_tree,
5224 struct got_pathlist_head *commitable_paths,
5225 got_worktree_status_cb status_cb, void *status_arg,
5226 struct got_repository *repo)
5228 const struct got_error *err = NULL;
5229 struct got_tree_entry *new_te;
5230 char *subtree_path;
5231 struct got_object_id *id = NULL;
5232 int nentries;
5234 *new_tep = NULL;
5236 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5237 got_path_is_root_dir(path_base_tree) ? "" : "/",
5238 child_path) == -1)
5239 return got_error_from_errno("asprintf");
5241 new_te = calloc(1, sizeof(*new_te));
5242 if (new_te == NULL)
5243 return got_error_from_errno("calloc");
5244 new_te->mode = S_IFDIR;
5246 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5247 sizeof(new_te->name)) {
5248 err = got_error(GOT_ERR_NO_SPACE);
5249 goto done;
5251 err = write_tree(&id, &nentries, NULL, subtree_path,
5252 commitable_paths, status_cb, status_arg, repo);
5253 if (err) {
5254 free(new_te);
5255 goto done;
5257 memcpy(&new_te->id, id, sizeof(new_te->id));
5258 done:
5259 free(id);
5260 free(subtree_path);
5261 if (err == NULL)
5262 *new_tep = new_te;
5263 return err;
5266 static const struct got_error *
5267 write_tree(struct got_object_id **new_tree_id, int *nentries,
5268 struct got_tree_object *base_tree, const char *path_base_tree,
5269 struct got_pathlist_head *commitable_paths,
5270 got_worktree_status_cb status_cb, void *status_arg,
5271 struct got_repository *repo)
5273 const struct got_error *err = NULL;
5274 struct got_pathlist_head paths;
5275 struct got_tree_entry *te, *new_te = NULL;
5276 struct got_pathlist_entry *pe;
5278 TAILQ_INIT(&paths);
5279 *nentries = 0;
5281 /* Insert, and recurse into, newly added entries first. */
5282 TAILQ_FOREACH(pe, commitable_paths, entry) {
5283 struct got_commitable *ct = pe->data;
5284 char *child_path = NULL, *slash;
5286 if ((ct->status != GOT_STATUS_ADD &&
5287 ct->staged_status != GOT_STATUS_ADD) ||
5288 (ct->flags & GOT_COMMITABLE_ADDED))
5289 continue;
5291 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5292 strlen(path_base_tree)))
5293 continue;
5295 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5296 ct->in_repo_path);
5297 if (err)
5298 goto done;
5300 slash = strchr(child_path, '/');
5301 if (slash == NULL) {
5302 err = alloc_added_blob_tree_entry(&new_te, ct);
5303 if (err)
5304 goto done;
5305 err = report_ct_status(ct, status_cb, status_arg);
5306 if (err)
5307 goto done;
5308 ct->flags |= GOT_COMMITABLE_ADDED;
5309 err = insert_tree_entry(new_te, &paths);
5310 if (err)
5311 goto done;
5312 (*nentries)++;
5313 } else {
5314 *slash = '\0'; /* trim trailing path components */
5315 if (base_tree == NULL ||
5316 got_object_tree_find_entry(base_tree, child_path)
5317 == NULL) {
5318 err = make_subtree_for_added_blob(&new_te,
5319 child_path, path_base_tree,
5320 commitable_paths, status_cb, status_arg,
5321 repo);
5322 if (err)
5323 goto done;
5324 err = insert_tree_entry(new_te, &paths);
5325 if (err)
5326 goto done;
5327 (*nentries)++;
5332 if (base_tree) {
5333 int i, nbase_entries;
5334 /* Handle modified and deleted entries. */
5335 nbase_entries = got_object_tree_get_nentries(base_tree);
5336 for (i = 0; i < nbase_entries; i++) {
5337 struct got_commitable *ct = NULL;
5339 te = got_object_tree_get_entry(base_tree, i);
5340 if (got_object_tree_entry_is_submodule(te)) {
5341 /* Entry is a submodule; just copy it. */
5342 err = got_object_tree_entry_dup(&new_te, te);
5343 if (err)
5344 goto done;
5345 err = insert_tree_entry(new_te, &paths);
5346 if (err)
5347 goto done;
5348 (*nentries)++;
5349 continue;
5352 if (S_ISDIR(te->mode)) {
5353 int modified;
5354 err = got_object_tree_entry_dup(&new_te, te);
5355 if (err)
5356 goto done;
5357 err = match_modified_subtree(&modified, te,
5358 path_base_tree, commitable_paths);
5359 if (err)
5360 goto done;
5361 /* Avoid recursion into unmodified subtrees. */
5362 if (modified) {
5363 struct got_object_id *new_id;
5364 int nsubentries;
5365 err = write_subtree(&new_id,
5366 &nsubentries, te,
5367 path_base_tree, commitable_paths,
5368 status_cb, status_arg, repo);
5369 if (err)
5370 goto done;
5371 if (nsubentries == 0) {
5372 /* All entries were deleted. */
5373 free(new_id);
5374 continue;
5376 memcpy(&new_te->id, new_id,
5377 sizeof(new_te->id));
5378 free(new_id);
5380 err = insert_tree_entry(new_te, &paths);
5381 if (err)
5382 goto done;
5383 (*nentries)++;
5384 continue;
5387 err = match_deleted_or_modified_ct(&ct, te,
5388 path_base_tree, commitable_paths);
5389 if (err)
5390 goto done;
5391 if (ct) {
5392 /* NB: Deleted entries get dropped here. */
5393 if (ct->status == GOT_STATUS_MODIFY ||
5394 ct->status == GOT_STATUS_MODE_CHANGE ||
5395 ct->staged_status == GOT_STATUS_MODIFY) {
5396 err = alloc_modified_blob_tree_entry(
5397 &new_te, te, ct);
5398 if (err)
5399 goto done;
5400 err = insert_tree_entry(new_te, &paths);
5401 if (err)
5402 goto done;
5403 (*nentries)++;
5405 err = report_ct_status(ct, status_cb,
5406 status_arg);
5407 if (err)
5408 goto done;
5409 } else {
5410 /* Entry is unchanged; just copy it. */
5411 err = got_object_tree_entry_dup(&new_te, te);
5412 if (err)
5413 goto done;
5414 err = insert_tree_entry(new_te, &paths);
5415 if (err)
5416 goto done;
5417 (*nentries)++;
5422 /* Write new list of entries; deleted entries have been dropped. */
5423 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5424 done:
5425 got_pathlist_free(&paths);
5426 return err;
5429 static const struct got_error *
5430 update_fileindex_after_commit(struct got_worktree *worktree,
5431 struct got_pathlist_head *commitable_paths,
5432 struct got_object_id *new_base_commit_id,
5433 struct got_fileindex *fileindex, int have_staged_files)
5435 const struct got_error *err = NULL;
5436 struct got_pathlist_entry *pe;
5437 char *relpath = NULL;
5439 TAILQ_FOREACH(pe, commitable_paths, entry) {
5440 struct got_fileindex_entry *ie;
5441 struct got_commitable *ct = pe->data;
5443 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5445 err = got_path_skip_common_ancestor(&relpath,
5446 worktree->root_path, ct->ondisk_path);
5447 if (err)
5448 goto done;
5450 if (ie) {
5451 if (ct->status == GOT_STATUS_DELETE ||
5452 ct->staged_status == GOT_STATUS_DELETE) {
5453 got_fileindex_entry_remove(fileindex, ie);
5454 } else if (ct->staged_status == GOT_STATUS_ADD ||
5455 ct->staged_status == GOT_STATUS_MODIFY) {
5456 got_fileindex_entry_stage_set(ie,
5457 GOT_FILEIDX_STAGE_NONE);
5458 got_fileindex_entry_staged_filetype_set(ie, 0);
5460 err = got_fileindex_entry_update(ie,
5461 worktree->root_fd, relpath,
5462 ct->staged_blob_id->sha1,
5463 new_base_commit_id->sha1,
5464 !have_staged_files);
5465 } else
5466 err = got_fileindex_entry_update(ie,
5467 worktree->root_fd, relpath,
5468 ct->blob_id->sha1,
5469 new_base_commit_id->sha1,
5470 !have_staged_files);
5471 } else {
5472 err = got_fileindex_entry_alloc(&ie, pe->path);
5473 if (err)
5474 goto done;
5475 err = got_fileindex_entry_update(ie,
5476 worktree->root_fd, relpath, ct->blob_id->sha1,
5477 new_base_commit_id->sha1, 1);
5478 if (err) {
5479 got_fileindex_entry_free(ie);
5480 goto done;
5482 err = got_fileindex_entry_add(fileindex, ie);
5483 if (err) {
5484 got_fileindex_entry_free(ie);
5485 goto done;
5488 free(relpath);
5489 relpath = NULL;
5491 done:
5492 free(relpath);
5493 return err;
5497 static const struct got_error *
5498 check_out_of_date(const char *in_repo_path, unsigned char status,
5499 unsigned char staged_status, struct got_object_id *base_blob_id,
5500 struct got_object_id *base_commit_id,
5501 struct got_object_id *head_commit_id, struct got_repository *repo,
5502 int ood_errcode)
5504 const struct got_error *err = NULL;
5505 struct got_object_id *id = NULL;
5507 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5508 /* Trivial case: base commit == head commit */
5509 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5510 return NULL;
5512 * Ensure file content which local changes were based
5513 * on matches file content in the branch head.
5515 err = got_object_id_by_path(&id, repo, head_commit_id,
5516 in_repo_path);
5517 if (err) {
5518 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5519 err = got_error(ood_errcode);
5520 goto done;
5521 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5522 err = got_error(ood_errcode);
5523 } else {
5524 /* Require that added files don't exist in the branch head. */
5525 err = got_object_id_by_path(&id, repo, head_commit_id,
5526 in_repo_path);
5527 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5528 goto done;
5529 err = id ? got_error(ood_errcode) : NULL;
5531 done:
5532 free(id);
5533 return err;
5536 const struct got_error *
5537 commit_worktree(struct got_object_id **new_commit_id,
5538 struct got_pathlist_head *commitable_paths,
5539 struct got_object_id *head_commit_id,
5540 struct got_object_id *parent_id2,
5541 struct got_worktree *worktree,
5542 const char *author, const char *committer,
5543 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5544 got_worktree_status_cb status_cb, void *status_arg,
5545 struct got_repository *repo)
5547 const struct got_error *err = NULL, *unlockerr = NULL;
5548 struct got_pathlist_entry *pe;
5549 const char *head_ref_name = NULL;
5550 struct got_commit_object *head_commit = NULL;
5551 struct got_reference *head_ref2 = NULL;
5552 struct got_object_id *head_commit_id2 = NULL;
5553 struct got_tree_object *head_tree = NULL;
5554 struct got_object_id *new_tree_id = NULL;
5555 int nentries, nparents = 0;
5556 struct got_object_id_queue parent_ids;
5557 struct got_object_qid *pid = NULL;
5558 char *logmsg = NULL;
5560 *new_commit_id = NULL;
5562 STAILQ_INIT(&parent_ids);
5564 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5565 if (err)
5566 goto done;
5568 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5569 if (err)
5570 goto done;
5572 if (commit_msg_cb != NULL) {
5573 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5574 if (err)
5575 goto done;
5578 if (logmsg == NULL || strlen(logmsg) == 0) {
5579 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5580 goto done;
5583 /* Create blobs from added and modified files and record their IDs. */
5584 TAILQ_FOREACH(pe, commitable_paths, entry) {
5585 struct got_commitable *ct = pe->data;
5586 char *ondisk_path;
5588 /* Blobs for staged files already exist. */
5589 if (ct->staged_status == GOT_STATUS_ADD ||
5590 ct->staged_status == GOT_STATUS_MODIFY)
5591 continue;
5593 if (ct->status != GOT_STATUS_ADD &&
5594 ct->status != GOT_STATUS_MODIFY &&
5595 ct->status != GOT_STATUS_MODE_CHANGE)
5596 continue;
5598 if (asprintf(&ondisk_path, "%s/%s",
5599 worktree->root_path, pe->path) == -1) {
5600 err = got_error_from_errno("asprintf");
5601 goto done;
5603 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5604 free(ondisk_path);
5605 if (err)
5606 goto done;
5609 /* Recursively write new tree objects. */
5610 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5611 commitable_paths, status_cb, status_arg, repo);
5612 if (err)
5613 goto done;
5615 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5616 if (err)
5617 goto done;
5618 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5619 nparents++;
5620 if (parent_id2) {
5621 err = got_object_qid_alloc(&pid, parent_id2);
5622 if (err)
5623 goto done;
5624 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5625 nparents++;
5627 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5628 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5629 if (logmsg != NULL)
5630 free(logmsg);
5631 if (err)
5632 goto done;
5634 /* Check if a concurrent commit to our branch has occurred. */
5635 head_ref_name = got_worktree_get_head_ref_name(worktree);
5636 if (head_ref_name == NULL) {
5637 err = got_error_from_errno("got_worktree_get_head_ref_name");
5638 goto done;
5640 /* Lock the reference here to prevent concurrent modification. */
5641 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5642 if (err)
5643 goto done;
5644 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5645 if (err)
5646 goto done;
5647 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5648 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5649 goto done;
5651 /* Update branch head in repository. */
5652 err = got_ref_change_ref(head_ref2, *new_commit_id);
5653 if (err)
5654 goto done;
5655 err = got_ref_write(head_ref2, repo);
5656 if (err)
5657 goto done;
5659 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5660 if (err)
5661 goto done;
5663 err = ref_base_commit(worktree, repo);
5664 if (err)
5665 goto done;
5666 done:
5667 got_object_id_queue_free(&parent_ids);
5668 if (head_tree)
5669 got_object_tree_close(head_tree);
5670 if (head_commit)
5671 got_object_commit_close(head_commit);
5672 free(head_commit_id2);
5673 if (head_ref2) {
5674 unlockerr = got_ref_unlock(head_ref2);
5675 if (unlockerr && err == NULL)
5676 err = unlockerr;
5677 got_ref_close(head_ref2);
5679 return err;
5682 static const struct got_error *
5683 check_path_is_commitable(const char *path,
5684 struct got_pathlist_head *commitable_paths)
5686 struct got_pathlist_entry *cpe = NULL;
5687 size_t path_len = strlen(path);
5689 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5690 struct got_commitable *ct = cpe->data;
5691 const char *ct_path = ct->path;
5693 while (ct_path[0] == '/')
5694 ct_path++;
5696 if (strcmp(path, ct_path) == 0 ||
5697 got_path_is_child(ct_path, path, path_len))
5698 break;
5701 if (cpe == NULL)
5702 return got_error_path(path, GOT_ERR_BAD_PATH);
5704 return NULL;
5707 static const struct got_error *
5708 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5710 int *have_staged_files = arg;
5712 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5713 *have_staged_files = 1;
5714 return got_error(GOT_ERR_CANCELLED);
5717 return NULL;
5720 static const struct got_error *
5721 check_non_staged_files(struct got_fileindex *fileindex,
5722 struct got_pathlist_head *paths)
5724 struct got_pathlist_entry *pe;
5725 struct got_fileindex_entry *ie;
5727 TAILQ_FOREACH(pe, paths, entry) {
5728 if (pe->path[0] == '\0')
5729 continue;
5730 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5731 if (ie == NULL)
5732 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5733 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5734 return got_error_path(pe->path,
5735 GOT_ERR_FILE_NOT_STAGED);
5738 return NULL;
5741 const struct got_error *
5742 got_worktree_commit(struct got_object_id **new_commit_id,
5743 struct got_worktree *worktree, struct got_pathlist_head *paths,
5744 const char *author, const char *committer, int allow_bad_symlinks,
5745 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5746 got_worktree_status_cb status_cb, void *status_arg,
5747 struct got_repository *repo)
5749 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5750 struct got_fileindex *fileindex = NULL;
5751 char *fileindex_path = NULL;
5752 struct got_pathlist_head commitable_paths;
5753 struct collect_commitables_arg cc_arg;
5754 struct got_pathlist_entry *pe;
5755 struct got_reference *head_ref = NULL;
5756 struct got_object_id *head_commit_id = NULL;
5757 int have_staged_files = 0;
5759 *new_commit_id = NULL;
5761 TAILQ_INIT(&commitable_paths);
5763 err = lock_worktree(worktree, LOCK_EX);
5764 if (err)
5765 goto done;
5767 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5768 if (err)
5769 goto done;
5771 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5772 if (err)
5773 goto done;
5775 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5776 if (err)
5777 goto done;
5779 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5780 &have_staged_files);
5781 if (err && err->code != GOT_ERR_CANCELLED)
5782 goto done;
5783 if (have_staged_files) {
5784 err = check_non_staged_files(fileindex, paths);
5785 if (err)
5786 goto done;
5789 cc_arg.commitable_paths = &commitable_paths;
5790 cc_arg.worktree = worktree;
5791 cc_arg.fileindex = fileindex;
5792 cc_arg.repo = repo;
5793 cc_arg.have_staged_files = have_staged_files;
5794 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5795 TAILQ_FOREACH(pe, paths, entry) {
5796 err = worktree_status(worktree, pe->path, fileindex, repo,
5797 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5798 if (err)
5799 goto done;
5802 if (TAILQ_EMPTY(&commitable_paths)) {
5803 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5804 goto done;
5807 TAILQ_FOREACH(pe, paths, entry) {
5808 err = check_path_is_commitable(pe->path, &commitable_paths);
5809 if (err)
5810 goto done;
5813 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5814 struct got_commitable *ct = pe->data;
5815 const char *ct_path = ct->in_repo_path;
5817 while (ct_path[0] == '/')
5818 ct_path++;
5819 err = check_out_of_date(ct_path, ct->status,
5820 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5821 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5822 if (err)
5823 goto done;
5827 err = commit_worktree(new_commit_id, &commitable_paths,
5828 head_commit_id, NULL, worktree, author, committer,
5829 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5830 if (err)
5831 goto done;
5833 err = update_fileindex_after_commit(worktree, &commitable_paths,
5834 *new_commit_id, fileindex, have_staged_files);
5835 sync_err = sync_fileindex(fileindex, fileindex_path);
5836 if (sync_err && err == NULL)
5837 err = sync_err;
5838 done:
5839 if (fileindex)
5840 got_fileindex_free(fileindex);
5841 free(fileindex_path);
5842 unlockerr = lock_worktree(worktree, LOCK_SH);
5843 if (unlockerr && err == NULL)
5844 err = unlockerr;
5845 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5846 struct got_commitable *ct = pe->data;
5847 free_commitable(ct);
5849 got_pathlist_free(&commitable_paths);
5850 return err;
5853 const char *
5854 got_commitable_get_path(struct got_commitable *ct)
5856 return ct->path;
5859 unsigned int
5860 got_commitable_get_status(struct got_commitable *ct)
5862 return ct->status;
5865 struct check_rebase_ok_arg {
5866 struct got_worktree *worktree;
5867 struct got_repository *repo;
5870 static const struct got_error *
5871 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5873 const struct got_error *err = NULL;
5874 struct check_rebase_ok_arg *a = arg;
5875 unsigned char status;
5876 struct stat sb;
5877 char *ondisk_path;
5879 /* Reject rebase of a work tree with mixed base commits. */
5880 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5881 SHA1_DIGEST_LENGTH))
5882 return got_error(GOT_ERR_MIXED_COMMITS);
5884 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5885 == -1)
5886 return got_error_from_errno("asprintf");
5888 /* Reject rebase of a work tree with modified or staged files. */
5889 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5890 free(ondisk_path);
5891 if (err)
5892 return err;
5894 if (status != GOT_STATUS_NO_CHANGE)
5895 return got_error(GOT_ERR_MODIFIED);
5896 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5897 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5899 return NULL;
5902 const struct got_error *
5903 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5904 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5905 struct got_worktree *worktree, struct got_reference *branch,
5906 struct got_repository *repo)
5908 const struct got_error *err = NULL;
5909 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5910 char *branch_ref_name = NULL;
5911 char *fileindex_path = NULL;
5912 struct check_rebase_ok_arg ok_arg;
5913 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5914 struct got_object_id *wt_branch_tip = NULL;
5916 *new_base_branch_ref = NULL;
5917 *tmp_branch = NULL;
5918 *fileindex = NULL;
5920 err = lock_worktree(worktree, LOCK_EX);
5921 if (err)
5922 return err;
5924 err = open_fileindex(fileindex, &fileindex_path, worktree);
5925 if (err)
5926 goto done;
5928 ok_arg.worktree = worktree;
5929 ok_arg.repo = repo;
5930 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5931 &ok_arg);
5932 if (err)
5933 goto done;
5935 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5936 if (err)
5937 goto done;
5939 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5940 if (err)
5941 goto done;
5943 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5944 if (err)
5945 goto done;
5947 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5948 0);
5949 if (err)
5950 goto done;
5952 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5953 if (err)
5954 goto done;
5955 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5956 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5957 goto done;
5960 err = got_ref_alloc_symref(new_base_branch_ref,
5961 new_base_branch_ref_name, wt_branch);
5962 if (err)
5963 goto done;
5964 err = got_ref_write(*new_base_branch_ref, repo);
5965 if (err)
5966 goto done;
5968 /* TODO Lock original branch's ref while rebasing? */
5970 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5971 if (err)
5972 goto done;
5974 err = got_ref_write(branch_ref, repo);
5975 if (err)
5976 goto done;
5978 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5979 worktree->base_commit_id);
5980 if (err)
5981 goto done;
5982 err = got_ref_write(*tmp_branch, repo);
5983 if (err)
5984 goto done;
5986 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5987 if (err)
5988 goto done;
5989 done:
5990 free(fileindex_path);
5991 free(tmp_branch_name);
5992 free(new_base_branch_ref_name);
5993 free(branch_ref_name);
5994 if (branch_ref)
5995 got_ref_close(branch_ref);
5996 if (wt_branch)
5997 got_ref_close(wt_branch);
5998 free(wt_branch_tip);
5999 if (err) {
6000 if (*new_base_branch_ref) {
6001 got_ref_close(*new_base_branch_ref);
6002 *new_base_branch_ref = NULL;
6004 if (*tmp_branch) {
6005 got_ref_close(*tmp_branch);
6006 *tmp_branch = NULL;
6008 if (*fileindex) {
6009 got_fileindex_free(*fileindex);
6010 *fileindex = NULL;
6012 lock_worktree(worktree, LOCK_SH);
6014 return err;
6017 const struct got_error *
6018 got_worktree_rebase_continue(struct got_object_id **commit_id,
6019 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6020 struct got_reference **branch, struct got_fileindex **fileindex,
6021 struct got_worktree *worktree, struct got_repository *repo)
6023 const struct got_error *err;
6024 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6025 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6026 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6027 char *fileindex_path = NULL;
6028 int have_staged_files = 0;
6030 *commit_id = NULL;
6031 *new_base_branch = NULL;
6032 *tmp_branch = NULL;
6033 *branch = NULL;
6034 *fileindex = NULL;
6036 err = lock_worktree(worktree, LOCK_EX);
6037 if (err)
6038 return err;
6040 err = open_fileindex(fileindex, &fileindex_path, worktree);
6041 if (err)
6042 goto done;
6044 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6045 &have_staged_files);
6046 if (err && err->code != GOT_ERR_CANCELLED)
6047 goto done;
6048 if (have_staged_files) {
6049 err = got_error(GOT_ERR_STAGED_PATHS);
6050 goto done;
6053 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6054 if (err)
6055 goto done;
6057 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6058 if (err)
6059 goto done;
6061 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6062 if (err)
6063 goto done;
6065 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6066 if (err)
6067 goto done;
6069 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6070 if (err)
6071 goto done;
6073 err = got_ref_open(branch, repo,
6074 got_ref_get_symref_target(branch_ref), 0);
6075 if (err)
6076 goto done;
6078 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6079 if (err)
6080 goto done;
6082 err = got_ref_resolve(commit_id, repo, commit_ref);
6083 if (err)
6084 goto done;
6086 err = got_ref_open(new_base_branch, repo,
6087 new_base_branch_ref_name, 0);
6088 if (err)
6089 goto done;
6091 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6092 if (err)
6093 goto done;
6094 done:
6095 free(commit_ref_name);
6096 free(branch_ref_name);
6097 free(fileindex_path);
6098 if (commit_ref)
6099 got_ref_close(commit_ref);
6100 if (branch_ref)
6101 got_ref_close(branch_ref);
6102 if (err) {
6103 free(*commit_id);
6104 *commit_id = NULL;
6105 if (*tmp_branch) {
6106 got_ref_close(*tmp_branch);
6107 *tmp_branch = NULL;
6109 if (*new_base_branch) {
6110 got_ref_close(*new_base_branch);
6111 *new_base_branch = NULL;
6113 if (*branch) {
6114 got_ref_close(*branch);
6115 *branch = NULL;
6117 if (*fileindex) {
6118 got_fileindex_free(*fileindex);
6119 *fileindex = NULL;
6121 lock_worktree(worktree, LOCK_SH);
6123 return err;
6126 const struct got_error *
6127 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6129 const struct got_error *err;
6130 char *tmp_branch_name = NULL;
6132 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6133 if (err)
6134 return err;
6136 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6137 free(tmp_branch_name);
6138 return NULL;
6141 static const struct got_error *
6142 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6143 char **logmsg, void *arg)
6145 *logmsg = arg;
6146 return NULL;
6149 static const struct got_error *
6150 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6151 const char *path, struct got_object_id *blob_id,
6152 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6153 int dirfd, const char *de_name)
6155 return NULL;
6158 struct collect_merged_paths_arg {
6159 got_worktree_checkout_cb progress_cb;
6160 void *progress_arg;
6161 struct got_pathlist_head *merged_paths;
6164 static const struct got_error *
6165 collect_merged_paths(void *arg, unsigned char status, const char *path)
6167 const struct got_error *err;
6168 struct collect_merged_paths_arg *a = arg;
6169 char *p;
6170 struct got_pathlist_entry *new;
6172 err = (*a->progress_cb)(a->progress_arg, status, path);
6173 if (err)
6174 return err;
6176 if (status != GOT_STATUS_MERGE &&
6177 status != GOT_STATUS_ADD &&
6178 status != GOT_STATUS_DELETE &&
6179 status != GOT_STATUS_CONFLICT)
6180 return NULL;
6182 p = strdup(path);
6183 if (p == NULL)
6184 return got_error_from_errno("strdup");
6186 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6187 if (err || new == NULL)
6188 free(p);
6189 return err;
6192 void
6193 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6195 struct got_pathlist_entry *pe;
6197 TAILQ_FOREACH(pe, merged_paths, entry)
6198 free((char *)pe->path);
6200 got_pathlist_free(merged_paths);
6203 static const struct got_error *
6204 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6205 int is_rebase, struct got_repository *repo)
6207 const struct got_error *err;
6208 struct got_reference *commit_ref = NULL;
6210 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6211 if (err) {
6212 if (err->code != GOT_ERR_NOT_REF)
6213 goto done;
6214 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6215 if (err)
6216 goto done;
6217 err = got_ref_write(commit_ref, repo);
6218 if (err)
6219 goto done;
6220 } else if (is_rebase) {
6221 struct got_object_id *stored_id;
6222 int cmp;
6224 err = got_ref_resolve(&stored_id, repo, commit_ref);
6225 if (err)
6226 goto done;
6227 cmp = got_object_id_cmp(commit_id, stored_id);
6228 free(stored_id);
6229 if (cmp != 0) {
6230 err = got_error(GOT_ERR_REBASE_COMMITID);
6231 goto done;
6234 done:
6235 if (commit_ref)
6236 got_ref_close(commit_ref);
6237 return err;
6240 static const struct got_error *
6241 rebase_merge_files(struct got_pathlist_head *merged_paths,
6242 const char *commit_ref_name, struct got_worktree *worktree,
6243 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6244 struct got_object_id *commit_id, struct got_repository *repo,
6245 got_worktree_checkout_cb progress_cb, void *progress_arg,
6246 got_cancel_cb cancel_cb, void *cancel_arg)
6248 const struct got_error *err;
6249 struct got_reference *commit_ref = NULL;
6250 struct collect_merged_paths_arg cmp_arg;
6251 char *fileindex_path;
6253 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6255 err = get_fileindex_path(&fileindex_path, worktree);
6256 if (err)
6257 return err;
6259 cmp_arg.progress_cb = progress_cb;
6260 cmp_arg.progress_arg = progress_arg;
6261 cmp_arg.merged_paths = merged_paths;
6262 err = merge_files(worktree, fileindex, fileindex_path,
6263 parent_commit_id, commit_id, repo, collect_merged_paths,
6264 &cmp_arg, cancel_cb, cancel_arg);
6265 if (commit_ref)
6266 got_ref_close(commit_ref);
6267 return err;
6270 const struct got_error *
6271 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6272 struct got_worktree *worktree, struct got_fileindex *fileindex,
6273 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6274 struct got_repository *repo,
6275 got_worktree_checkout_cb progress_cb, void *progress_arg,
6276 got_cancel_cb cancel_cb, void *cancel_arg)
6278 const struct got_error *err;
6279 char *commit_ref_name;
6281 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6282 if (err)
6283 return err;
6285 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6286 if (err)
6287 goto done;
6289 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6290 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6291 progress_arg, cancel_cb, cancel_arg);
6292 done:
6293 free(commit_ref_name);
6294 return err;
6297 const struct got_error *
6298 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6299 struct got_worktree *worktree, struct got_fileindex *fileindex,
6300 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6301 struct got_repository *repo,
6302 got_worktree_checkout_cb progress_cb, void *progress_arg,
6303 got_cancel_cb cancel_cb, void *cancel_arg)
6305 const struct got_error *err;
6306 char *commit_ref_name;
6308 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6309 if (err)
6310 return err;
6312 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6313 if (err)
6314 goto done;
6316 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6317 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6318 progress_arg, cancel_cb, cancel_arg);
6319 done:
6320 free(commit_ref_name);
6321 return err;
6324 static const struct got_error *
6325 rebase_commit(struct got_object_id **new_commit_id,
6326 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6327 struct got_worktree *worktree, struct got_fileindex *fileindex,
6328 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6329 const char *new_logmsg, struct got_repository *repo)
6331 const struct got_error *err, *sync_err;
6332 struct got_pathlist_head commitable_paths;
6333 struct collect_commitables_arg cc_arg;
6334 char *fileindex_path = NULL;
6335 struct got_reference *head_ref = NULL;
6336 struct got_object_id *head_commit_id = NULL;
6337 char *logmsg = NULL;
6339 TAILQ_INIT(&commitable_paths);
6340 *new_commit_id = NULL;
6342 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6344 err = get_fileindex_path(&fileindex_path, worktree);
6345 if (err)
6346 return err;
6348 cc_arg.commitable_paths = &commitable_paths;
6349 cc_arg.worktree = worktree;
6350 cc_arg.repo = repo;
6351 cc_arg.have_staged_files = 0;
6353 * If possible get the status of individual files directly to
6354 * avoid crawling the entire work tree once per rebased commit.
6356 * Ideally, merged_paths would contain a list of commitables
6357 * we could use so we could skip worktree_status() entirely.
6358 * However, we would then need carefully keep track of cumulative
6359 * effects of operations such as file additions and deletions
6360 * in 'got histedit -f' (folding multiple commits into one),
6361 * and this extra complexity is not really worth it.
6363 if (merged_paths) {
6364 struct got_pathlist_entry *pe;
6365 TAILQ_FOREACH(pe, merged_paths, entry) {
6366 err = worktree_status(worktree, pe->path, fileindex,
6367 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6368 0);
6369 if (err)
6370 goto done;
6372 } else {
6373 err = worktree_status(worktree, "", fileindex, repo,
6374 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6375 if (err)
6376 goto done;
6379 if (TAILQ_EMPTY(&commitable_paths)) {
6380 /* No-op change; commit will be elided. */
6381 err = got_ref_delete(commit_ref, repo);
6382 if (err)
6383 goto done;
6384 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6385 goto done;
6388 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6389 if (err)
6390 goto done;
6392 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6393 if (err)
6394 goto done;
6396 if (new_logmsg) {
6397 logmsg = strdup(new_logmsg);
6398 if (logmsg == NULL) {
6399 err = got_error_from_errno("strdup");
6400 goto done;
6402 } else {
6403 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6404 if (err)
6405 goto done;
6408 /* NB: commit_worktree will call free(logmsg) */
6409 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6410 NULL, worktree, got_object_commit_get_author(orig_commit),
6411 got_object_commit_get_committer(orig_commit),
6412 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6413 if (err)
6414 goto done;
6416 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6417 if (err)
6418 goto done;
6420 err = got_ref_delete(commit_ref, repo);
6421 if (err)
6422 goto done;
6424 err = update_fileindex_after_commit(worktree, &commitable_paths,
6425 *new_commit_id, fileindex, 0);
6426 sync_err = sync_fileindex(fileindex, fileindex_path);
6427 if (sync_err && err == NULL)
6428 err = sync_err;
6429 done:
6430 free(fileindex_path);
6431 free(head_commit_id);
6432 if (head_ref)
6433 got_ref_close(head_ref);
6434 if (err) {
6435 free(*new_commit_id);
6436 *new_commit_id = NULL;
6438 return err;
6441 const struct got_error *
6442 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6443 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6444 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6445 struct got_commit_object *orig_commit,
6446 struct got_object_id *orig_commit_id, struct got_repository *repo)
6448 const struct got_error *err;
6449 char *commit_ref_name;
6450 struct got_reference *commit_ref = NULL;
6451 struct got_object_id *commit_id = NULL;
6453 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6454 if (err)
6455 return err;
6457 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6458 if (err)
6459 goto done;
6460 err = got_ref_resolve(&commit_id, repo, commit_ref);
6461 if (err)
6462 goto done;
6463 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6464 err = got_error(GOT_ERR_REBASE_COMMITID);
6465 goto done;
6468 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6469 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6470 done:
6471 if (commit_ref)
6472 got_ref_close(commit_ref);
6473 free(commit_ref_name);
6474 free(commit_id);
6475 return err;
6478 const struct got_error *
6479 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6480 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6481 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6482 struct got_commit_object *orig_commit,
6483 struct got_object_id *orig_commit_id, const char *new_logmsg,
6484 struct got_repository *repo)
6486 const struct got_error *err;
6487 char *commit_ref_name;
6488 struct got_reference *commit_ref = NULL;
6490 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6491 if (err)
6492 return err;
6494 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6495 if (err)
6496 goto done;
6498 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6499 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6500 done:
6501 if (commit_ref)
6502 got_ref_close(commit_ref);
6503 free(commit_ref_name);
6504 return err;
6507 const struct got_error *
6508 got_worktree_rebase_postpone(struct got_worktree *worktree,
6509 struct got_fileindex *fileindex)
6511 if (fileindex)
6512 got_fileindex_free(fileindex);
6513 return lock_worktree(worktree, LOCK_SH);
6516 static const struct got_error *
6517 delete_ref(const char *name, struct got_repository *repo)
6519 const struct got_error *err;
6520 struct got_reference *ref;
6522 err = got_ref_open(&ref, repo, name, 0);
6523 if (err) {
6524 if (err->code == GOT_ERR_NOT_REF)
6525 return NULL;
6526 return err;
6529 err = got_ref_delete(ref, repo);
6530 got_ref_close(ref);
6531 return err;
6534 static const struct got_error *
6535 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6537 const struct got_error *err;
6538 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6539 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6541 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6542 if (err)
6543 goto done;
6544 err = delete_ref(tmp_branch_name, repo);
6545 if (err)
6546 goto done;
6548 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6549 if (err)
6550 goto done;
6551 err = delete_ref(new_base_branch_ref_name, repo);
6552 if (err)
6553 goto done;
6555 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6556 if (err)
6557 goto done;
6558 err = delete_ref(branch_ref_name, repo);
6559 if (err)
6560 goto done;
6562 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6563 if (err)
6564 goto done;
6565 err = delete_ref(commit_ref_name, repo);
6566 if (err)
6567 goto done;
6569 done:
6570 free(tmp_branch_name);
6571 free(new_base_branch_ref_name);
6572 free(branch_ref_name);
6573 free(commit_ref_name);
6574 return err;
6577 const struct got_error *
6578 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6579 struct got_object_id *new_commit_id, struct got_repository *repo)
6581 const struct got_error *err;
6582 struct got_reference *ref = NULL;
6583 struct got_object_id *old_commit_id = NULL;
6584 const char *branch_name = NULL;
6585 char *new_id_str = NULL;
6586 char *refname = NULL;
6588 branch_name = got_ref_get_name(branch);
6589 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6590 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6591 branch_name += 11;
6593 err = got_object_id_str(&new_id_str, new_commit_id);
6594 if (err)
6595 return err;
6597 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6598 new_id_str) == -1) {
6599 err = got_error_from_errno("asprintf");
6600 goto done;
6603 err = got_ref_resolve(&old_commit_id, repo, branch);
6604 if (err)
6605 goto done;
6607 err = got_ref_alloc(&ref, refname, old_commit_id);
6608 if (err)
6609 goto done;
6611 err = got_ref_write(ref, repo);
6612 done:
6613 free(new_id_str);
6614 free(refname);
6615 free(old_commit_id);
6616 if (ref)
6617 got_ref_close(ref);
6618 return err;
6621 const struct got_error *
6622 got_worktree_rebase_complete(struct got_worktree *worktree,
6623 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6624 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6625 struct got_repository *repo, int create_backup)
6627 const struct got_error *err, *unlockerr, *sync_err;
6628 struct got_object_id *new_head_commit_id = NULL;
6629 char *fileindex_path = NULL;
6631 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6632 if (err)
6633 return err;
6635 if (create_backup) {
6636 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6637 rebased_branch, new_head_commit_id, repo);
6638 if (err)
6639 goto done;
6642 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6643 if (err)
6644 goto done;
6646 err = got_ref_write(rebased_branch, repo);
6647 if (err)
6648 goto done;
6650 err = got_worktree_set_head_ref(worktree, rebased_branch);
6651 if (err)
6652 goto done;
6654 err = delete_rebase_refs(worktree, repo);
6655 if (err)
6656 goto done;
6658 err = get_fileindex_path(&fileindex_path, worktree);
6659 if (err)
6660 goto done;
6661 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6662 sync_err = sync_fileindex(fileindex, fileindex_path);
6663 if (sync_err && err == NULL)
6664 err = sync_err;
6665 done:
6666 got_fileindex_free(fileindex);
6667 free(fileindex_path);
6668 free(new_head_commit_id);
6669 unlockerr = lock_worktree(worktree, LOCK_SH);
6670 if (unlockerr && err == NULL)
6671 err = unlockerr;
6672 return err;
6675 const struct got_error *
6676 got_worktree_rebase_abort(struct got_worktree *worktree,
6677 struct got_fileindex *fileindex, struct got_repository *repo,
6678 struct got_reference *new_base_branch,
6679 got_worktree_checkout_cb progress_cb, void *progress_arg)
6681 const struct got_error *err, *unlockerr, *sync_err;
6682 struct got_reference *resolved = NULL;
6683 struct got_object_id *commit_id = NULL;
6684 char *fileindex_path = NULL;
6685 struct revert_file_args rfa;
6686 struct got_object_id *tree_id = NULL;
6688 err = lock_worktree(worktree, LOCK_EX);
6689 if (err)
6690 return err;
6692 err = got_ref_open(&resolved, repo,
6693 got_ref_get_symref_target(new_base_branch), 0);
6694 if (err)
6695 goto done;
6697 err = got_worktree_set_head_ref(worktree, resolved);
6698 if (err)
6699 goto done;
6702 * XXX commits to the base branch could have happened while
6703 * we were busy rebasing; should we store the original commit ID
6704 * when rebase begins and read it back here?
6706 err = got_ref_resolve(&commit_id, repo, resolved);
6707 if (err)
6708 goto done;
6710 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6711 if (err)
6712 goto done;
6714 err = got_object_id_by_path(&tree_id, repo,
6715 worktree->base_commit_id, worktree->path_prefix);
6716 if (err)
6717 goto done;
6719 err = delete_rebase_refs(worktree, repo);
6720 if (err)
6721 goto done;
6723 err = get_fileindex_path(&fileindex_path, worktree);
6724 if (err)
6725 goto done;
6727 rfa.worktree = worktree;
6728 rfa.fileindex = fileindex;
6729 rfa.progress_cb = progress_cb;
6730 rfa.progress_arg = progress_arg;
6731 rfa.patch_cb = NULL;
6732 rfa.patch_arg = NULL;
6733 rfa.repo = repo;
6734 rfa.unlink_added_files = 0;
6735 err = worktree_status(worktree, "", fileindex, repo,
6736 revert_file, &rfa, NULL, NULL, 1, 0);
6737 if (err)
6738 goto sync;
6740 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6741 repo, progress_cb, progress_arg, NULL, NULL);
6742 sync:
6743 sync_err = sync_fileindex(fileindex, fileindex_path);
6744 if (sync_err && err == NULL)
6745 err = sync_err;
6746 done:
6747 got_ref_close(resolved);
6748 free(tree_id);
6749 free(commit_id);
6750 if (fileindex)
6751 got_fileindex_free(fileindex);
6752 free(fileindex_path);
6754 unlockerr = lock_worktree(worktree, LOCK_SH);
6755 if (unlockerr && err == NULL)
6756 err = unlockerr;
6757 return err;
6760 const struct got_error *
6761 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6762 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6763 struct got_fileindex **fileindex, struct got_worktree *worktree,
6764 struct got_repository *repo)
6766 const struct got_error *err = NULL;
6767 char *tmp_branch_name = NULL;
6768 char *branch_ref_name = NULL;
6769 char *base_commit_ref_name = NULL;
6770 char *fileindex_path = NULL;
6771 struct check_rebase_ok_arg ok_arg;
6772 struct got_reference *wt_branch = NULL;
6773 struct got_reference *base_commit_ref = NULL;
6775 *tmp_branch = NULL;
6776 *branch_ref = NULL;
6777 *base_commit_id = NULL;
6778 *fileindex = NULL;
6780 err = lock_worktree(worktree, LOCK_EX);
6781 if (err)
6782 return err;
6784 err = open_fileindex(fileindex, &fileindex_path, worktree);
6785 if (err)
6786 goto done;
6788 ok_arg.worktree = worktree;
6789 ok_arg.repo = repo;
6790 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6791 &ok_arg);
6792 if (err)
6793 goto done;
6795 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6796 if (err)
6797 goto done;
6799 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6800 if (err)
6801 goto done;
6803 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6804 worktree);
6805 if (err)
6806 goto done;
6808 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6809 0);
6810 if (err)
6811 goto done;
6813 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6814 if (err)
6815 goto done;
6817 err = got_ref_write(*branch_ref, repo);
6818 if (err)
6819 goto done;
6821 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6822 worktree->base_commit_id);
6823 if (err)
6824 goto done;
6825 err = got_ref_write(base_commit_ref, repo);
6826 if (err)
6827 goto done;
6828 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6829 if (*base_commit_id == NULL) {
6830 err = got_error_from_errno("got_object_id_dup");
6831 goto done;
6834 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6835 worktree->base_commit_id);
6836 if (err)
6837 goto done;
6838 err = got_ref_write(*tmp_branch, repo);
6839 if (err)
6840 goto done;
6842 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6843 if (err)
6844 goto done;
6845 done:
6846 free(fileindex_path);
6847 free(tmp_branch_name);
6848 free(branch_ref_name);
6849 free(base_commit_ref_name);
6850 if (wt_branch)
6851 got_ref_close(wt_branch);
6852 if (err) {
6853 if (*branch_ref) {
6854 got_ref_close(*branch_ref);
6855 *branch_ref = NULL;
6857 if (*tmp_branch) {
6858 got_ref_close(*tmp_branch);
6859 *tmp_branch = NULL;
6861 free(*base_commit_id);
6862 if (*fileindex) {
6863 got_fileindex_free(*fileindex);
6864 *fileindex = NULL;
6866 lock_worktree(worktree, LOCK_SH);
6868 return err;
6871 const struct got_error *
6872 got_worktree_histedit_postpone(struct got_worktree *worktree,
6873 struct got_fileindex *fileindex)
6875 if (fileindex)
6876 got_fileindex_free(fileindex);
6877 return lock_worktree(worktree, LOCK_SH);
6880 const struct got_error *
6881 got_worktree_histedit_in_progress(int *in_progress,
6882 struct got_worktree *worktree)
6884 const struct got_error *err;
6885 char *tmp_branch_name = NULL;
6887 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6888 if (err)
6889 return err;
6891 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6892 free(tmp_branch_name);
6893 return NULL;
6896 const struct got_error *
6897 got_worktree_histedit_continue(struct got_object_id **commit_id,
6898 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6899 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6900 struct got_worktree *worktree, struct got_repository *repo)
6902 const struct got_error *err;
6903 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6904 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6905 struct got_reference *commit_ref = NULL;
6906 struct got_reference *base_commit_ref = NULL;
6907 char *fileindex_path = NULL;
6908 int have_staged_files = 0;
6910 *commit_id = NULL;
6911 *tmp_branch = NULL;
6912 *base_commit_id = NULL;
6913 *fileindex = NULL;
6915 err = lock_worktree(worktree, LOCK_EX);
6916 if (err)
6917 return err;
6919 err = open_fileindex(fileindex, &fileindex_path, worktree);
6920 if (err)
6921 goto done;
6923 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6924 &have_staged_files);
6925 if (err && err->code != GOT_ERR_CANCELLED)
6926 goto done;
6927 if (have_staged_files) {
6928 err = got_error(GOT_ERR_STAGED_PATHS);
6929 goto done;
6932 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6933 if (err)
6934 goto done;
6936 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6937 if (err)
6938 goto done;
6940 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6941 if (err)
6942 goto done;
6944 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6945 worktree);
6946 if (err)
6947 goto done;
6949 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6950 if (err)
6951 goto done;
6953 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6954 if (err)
6955 goto done;
6956 err = got_ref_resolve(commit_id, repo, commit_ref);
6957 if (err)
6958 goto done;
6960 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6961 if (err)
6962 goto done;
6963 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6964 if (err)
6965 goto done;
6967 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6968 if (err)
6969 goto done;
6970 done:
6971 free(commit_ref_name);
6972 free(branch_ref_name);
6973 free(fileindex_path);
6974 if (commit_ref)
6975 got_ref_close(commit_ref);
6976 if (base_commit_ref)
6977 got_ref_close(base_commit_ref);
6978 if (err) {
6979 free(*commit_id);
6980 *commit_id = NULL;
6981 free(*base_commit_id);
6982 *base_commit_id = NULL;
6983 if (*tmp_branch) {
6984 got_ref_close(*tmp_branch);
6985 *tmp_branch = NULL;
6987 if (*fileindex) {
6988 got_fileindex_free(*fileindex);
6989 *fileindex = NULL;
6991 lock_worktree(worktree, LOCK_EX);
6993 return err;
6996 static const struct got_error *
6997 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6999 const struct got_error *err;
7000 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7001 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7003 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7004 if (err)
7005 goto done;
7006 err = delete_ref(tmp_branch_name, repo);
7007 if (err)
7008 goto done;
7010 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7011 worktree);
7012 if (err)
7013 goto done;
7014 err = delete_ref(base_commit_ref_name, repo);
7015 if (err)
7016 goto done;
7018 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7019 if (err)
7020 goto done;
7021 err = delete_ref(branch_ref_name, repo);
7022 if (err)
7023 goto done;
7025 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7026 if (err)
7027 goto done;
7028 err = delete_ref(commit_ref_name, repo);
7029 if (err)
7030 goto done;
7031 done:
7032 free(tmp_branch_name);
7033 free(base_commit_ref_name);
7034 free(branch_ref_name);
7035 free(commit_ref_name);
7036 return err;
7039 const struct got_error *
7040 got_worktree_histedit_abort(struct got_worktree *worktree,
7041 struct got_fileindex *fileindex, struct got_repository *repo,
7042 struct got_reference *branch, struct got_object_id *base_commit_id,
7043 got_worktree_checkout_cb progress_cb, void *progress_arg)
7045 const struct got_error *err, *unlockerr, *sync_err;
7046 struct got_reference *resolved = NULL;
7047 char *fileindex_path = NULL;
7048 struct got_object_id *tree_id = NULL;
7049 struct revert_file_args rfa;
7051 err = lock_worktree(worktree, LOCK_EX);
7052 if (err)
7053 return err;
7055 err = got_ref_open(&resolved, repo,
7056 got_ref_get_symref_target(branch), 0);
7057 if (err)
7058 goto done;
7060 err = got_worktree_set_head_ref(worktree, resolved);
7061 if (err)
7062 goto done;
7064 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7065 if (err)
7066 goto done;
7068 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7069 worktree->path_prefix);
7070 if (err)
7071 goto done;
7073 err = delete_histedit_refs(worktree, repo);
7074 if (err)
7075 goto done;
7077 err = get_fileindex_path(&fileindex_path, worktree);
7078 if (err)
7079 goto done;
7081 rfa.worktree = worktree;
7082 rfa.fileindex = fileindex;
7083 rfa.progress_cb = progress_cb;
7084 rfa.progress_arg = progress_arg;
7085 rfa.patch_cb = NULL;
7086 rfa.patch_arg = NULL;
7087 rfa.repo = repo;
7088 rfa.unlink_added_files = 0;
7089 err = worktree_status(worktree, "", fileindex, repo,
7090 revert_file, &rfa, NULL, NULL, 1, 0);
7091 if (err)
7092 goto sync;
7094 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7095 repo, progress_cb, progress_arg, NULL, NULL);
7096 sync:
7097 sync_err = sync_fileindex(fileindex, fileindex_path);
7098 if (sync_err && err == NULL)
7099 err = sync_err;
7100 done:
7101 got_ref_close(resolved);
7102 free(tree_id);
7103 free(fileindex_path);
7105 unlockerr = lock_worktree(worktree, LOCK_SH);
7106 if (unlockerr && err == NULL)
7107 err = unlockerr;
7108 return err;
7111 const struct got_error *
7112 got_worktree_histedit_complete(struct got_worktree *worktree,
7113 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7114 struct got_reference *edited_branch, struct got_repository *repo)
7116 const struct got_error *err, *unlockerr, *sync_err;
7117 struct got_object_id *new_head_commit_id = NULL;
7118 struct got_reference *resolved = NULL;
7119 char *fileindex_path = NULL;
7121 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7122 if (err)
7123 return err;
7125 err = got_ref_open(&resolved, repo,
7126 got_ref_get_symref_target(edited_branch), 0);
7127 if (err)
7128 goto done;
7130 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7131 resolved, new_head_commit_id, repo);
7132 if (err)
7133 goto done;
7135 err = got_ref_change_ref(resolved, new_head_commit_id);
7136 if (err)
7137 goto done;
7139 err = got_ref_write(resolved, repo);
7140 if (err)
7141 goto done;
7143 err = got_worktree_set_head_ref(worktree, resolved);
7144 if (err)
7145 goto done;
7147 err = delete_histedit_refs(worktree, repo);
7148 if (err)
7149 goto done;
7151 err = get_fileindex_path(&fileindex_path, worktree);
7152 if (err)
7153 goto done;
7154 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7155 sync_err = sync_fileindex(fileindex, fileindex_path);
7156 if (sync_err && err == NULL)
7157 err = sync_err;
7158 done:
7159 got_fileindex_free(fileindex);
7160 free(fileindex_path);
7161 free(new_head_commit_id);
7162 unlockerr = lock_worktree(worktree, LOCK_SH);
7163 if (unlockerr && err == NULL)
7164 err = unlockerr;
7165 return err;
7168 const struct got_error *
7169 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7170 struct got_object_id *commit_id, struct got_repository *repo)
7172 const struct got_error *err;
7173 char *commit_ref_name;
7175 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7176 if (err)
7177 return err;
7179 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7180 if (err)
7181 goto done;
7183 err = delete_ref(commit_ref_name, repo);
7184 done:
7185 free(commit_ref_name);
7186 return err;
7189 const struct got_error *
7190 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7191 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7192 struct got_worktree *worktree, const char *refname,
7193 struct got_repository *repo)
7195 const struct got_error *err = NULL;
7196 char *fileindex_path = NULL;
7197 struct check_rebase_ok_arg ok_arg;
7199 *fileindex = NULL;
7200 *branch_ref = NULL;
7201 *base_branch_ref = NULL;
7203 err = lock_worktree(worktree, LOCK_EX);
7204 if (err)
7205 return err;
7207 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7208 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7209 "cannot integrate a branch into itself; "
7210 "update -b or different branch name required");
7211 goto done;
7214 err = open_fileindex(fileindex, &fileindex_path, worktree);
7215 if (err)
7216 goto done;
7218 /* Preconditions are the same as for rebase. */
7219 ok_arg.worktree = worktree;
7220 ok_arg.repo = repo;
7221 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7222 &ok_arg);
7223 if (err)
7224 goto done;
7226 err = got_ref_open(branch_ref, repo, refname, 1);
7227 if (err)
7228 goto done;
7230 err = got_ref_open(base_branch_ref, repo,
7231 got_worktree_get_head_ref_name(worktree), 1);
7232 done:
7233 if (err) {
7234 if (*branch_ref) {
7235 got_ref_close(*branch_ref);
7236 *branch_ref = NULL;
7238 if (*base_branch_ref) {
7239 got_ref_close(*base_branch_ref);
7240 *base_branch_ref = NULL;
7242 if (*fileindex) {
7243 got_fileindex_free(*fileindex);
7244 *fileindex = NULL;
7246 lock_worktree(worktree, LOCK_SH);
7248 return err;
7251 const struct got_error *
7252 got_worktree_integrate_continue(struct got_worktree *worktree,
7253 struct got_fileindex *fileindex, struct got_repository *repo,
7254 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7255 got_worktree_checkout_cb progress_cb, void *progress_arg,
7256 got_cancel_cb cancel_cb, void *cancel_arg)
7258 const struct got_error *err = NULL, *sync_err, *unlockerr;
7259 char *fileindex_path = NULL;
7260 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7262 err = get_fileindex_path(&fileindex_path, worktree);
7263 if (err)
7264 goto done;
7266 err = got_ref_resolve(&commit_id, repo, branch_ref);
7267 if (err)
7268 goto done;
7270 err = got_object_id_by_path(&tree_id, repo, commit_id,
7271 worktree->path_prefix);
7272 if (err)
7273 goto done;
7275 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7276 if (err)
7277 goto done;
7279 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7280 progress_cb, progress_arg, cancel_cb, cancel_arg);
7281 if (err)
7282 goto sync;
7284 err = got_ref_change_ref(base_branch_ref, commit_id);
7285 if (err)
7286 goto sync;
7288 err = got_ref_write(base_branch_ref, repo);
7289 if (err)
7290 goto sync;
7292 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7293 sync:
7294 sync_err = sync_fileindex(fileindex, fileindex_path);
7295 if (sync_err && err == NULL)
7296 err = sync_err;
7298 done:
7299 unlockerr = got_ref_unlock(branch_ref);
7300 if (unlockerr && err == NULL)
7301 err = unlockerr;
7302 got_ref_close(branch_ref);
7304 unlockerr = got_ref_unlock(base_branch_ref);
7305 if (unlockerr && err == NULL)
7306 err = unlockerr;
7307 got_ref_close(base_branch_ref);
7309 got_fileindex_free(fileindex);
7310 free(fileindex_path);
7311 free(tree_id);
7313 unlockerr = lock_worktree(worktree, LOCK_SH);
7314 if (unlockerr && err == NULL)
7315 err = unlockerr;
7316 return err;
7319 const struct got_error *
7320 got_worktree_integrate_abort(struct got_worktree *worktree,
7321 struct got_fileindex *fileindex, struct got_repository *repo,
7322 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7324 const struct got_error *err = NULL, *unlockerr = NULL;
7326 got_fileindex_free(fileindex);
7328 err = lock_worktree(worktree, LOCK_SH);
7330 unlockerr = got_ref_unlock(branch_ref);
7331 if (unlockerr && err == NULL)
7332 err = unlockerr;
7333 got_ref_close(branch_ref);
7335 unlockerr = got_ref_unlock(base_branch_ref);
7336 if (unlockerr && err == NULL)
7337 err = unlockerr;
7338 got_ref_close(base_branch_ref);
7340 return err;
7343 const struct got_error *
7344 got_worktree_merge_postpone(struct got_worktree *worktree,
7345 struct got_fileindex *fileindex)
7347 const struct got_error *err, *sync_err;
7348 char *fileindex_path = NULL;
7350 err = get_fileindex_path(&fileindex_path, worktree);
7351 if (err)
7352 goto done;
7354 sync_err = sync_fileindex(fileindex, fileindex_path);
7356 err = lock_worktree(worktree, LOCK_SH);
7357 if (sync_err && err == NULL)
7358 err = sync_err;
7359 done:
7360 got_fileindex_free(fileindex);
7361 free(fileindex_path);
7362 return err;
7365 static const struct got_error *
7366 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7368 const struct got_error *err;
7369 char *branch_refname = NULL, *commit_refname = NULL;
7371 err = get_merge_branch_ref_name(&branch_refname, worktree);
7372 if (err)
7373 goto done;
7374 err = delete_ref(branch_refname, repo);
7375 if (err)
7376 goto done;
7378 err = get_merge_commit_ref_name(&commit_refname, worktree);
7379 if (err)
7380 goto done;
7381 err = delete_ref(commit_refname, repo);
7382 if (err)
7383 goto done;
7385 done:
7386 free(branch_refname);
7387 free(commit_refname);
7388 return err;
7391 struct merge_commit_msg_arg {
7392 struct got_worktree *worktree;
7393 const char *branch_name;
7396 static const struct got_error *
7397 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7398 void *arg)
7400 struct merge_commit_msg_arg *a = arg;
7402 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7403 got_worktree_get_head_ref_name(a->worktree)) == -1)
7404 return got_error_from_errno("asprintf");
7406 return NULL;
7410 const struct got_error *
7411 got_worktree_merge_branch(struct got_worktree *worktree,
7412 struct got_fileindex *fileindex,
7413 struct got_object_id *yca_commit_id,
7414 struct got_object_id *branch_tip,
7415 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7416 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7418 const struct got_error *err;
7419 char *fileindex_path = NULL;
7421 err = get_fileindex_path(&fileindex_path, worktree);
7422 if (err)
7423 goto done;
7425 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7426 worktree);
7427 if (err)
7428 goto done;
7430 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7431 branch_tip, repo, progress_cb, progress_arg,
7432 cancel_cb, cancel_arg);
7433 done:
7434 free(fileindex_path);
7435 return err;
7438 const struct got_error *
7439 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7440 struct got_worktree *worktree, struct got_fileindex *fileindex,
7441 const char *author, const char *committer, int allow_bad_symlinks,
7442 struct got_object_id *branch_tip, const char *branch_name,
7443 struct got_repository *repo,
7444 got_worktree_status_cb status_cb, void *status_arg)
7447 const struct got_error *err = NULL, *sync_err;
7448 struct got_pathlist_head commitable_paths;
7449 struct collect_commitables_arg cc_arg;
7450 struct got_pathlist_entry *pe;
7451 struct got_reference *head_ref = NULL;
7452 struct got_object_id *head_commit_id = NULL;
7453 int have_staged_files = 0;
7454 struct merge_commit_msg_arg mcm_arg;
7455 char *fileindex_path = NULL;
7457 *new_commit_id = NULL;
7459 TAILQ_INIT(&commitable_paths);
7461 err = get_fileindex_path(&fileindex_path, worktree);
7462 if (err)
7463 goto done;
7465 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7466 if (err)
7467 goto done;
7469 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7470 if (err)
7471 goto done;
7473 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7474 &have_staged_files);
7475 if (err && err->code != GOT_ERR_CANCELLED)
7476 goto done;
7477 if (have_staged_files) {
7478 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7479 goto done;
7482 cc_arg.commitable_paths = &commitable_paths;
7483 cc_arg.worktree = worktree;
7484 cc_arg.fileindex = fileindex;
7485 cc_arg.repo = repo;
7486 cc_arg.have_staged_files = have_staged_files;
7487 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7488 err = worktree_status(worktree, "", fileindex, repo,
7489 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7490 if (err)
7491 goto done;
7493 if (TAILQ_EMPTY(&commitable_paths)) {
7494 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7495 "merge of %s cannot proceed", branch_name);
7496 goto done;
7499 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7500 struct got_commitable *ct = pe->data;
7501 const char *ct_path = ct->in_repo_path;
7503 while (ct_path[0] == '/')
7504 ct_path++;
7505 err = check_out_of_date(ct_path, ct->status,
7506 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7507 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7508 if (err)
7509 goto done;
7513 mcm_arg.worktree = worktree;
7514 mcm_arg.branch_name = branch_name;
7515 err = commit_worktree(new_commit_id, &commitable_paths,
7516 head_commit_id, branch_tip, worktree, author, committer,
7517 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7518 if (err)
7519 goto done;
7521 err = update_fileindex_after_commit(worktree, &commitable_paths,
7522 *new_commit_id, fileindex, have_staged_files);
7523 sync_err = sync_fileindex(fileindex, fileindex_path);
7524 if (sync_err && err == NULL)
7525 err = sync_err;
7526 done:
7527 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7528 struct got_commitable *ct = pe->data;
7529 free_commitable(ct);
7531 got_pathlist_free(&commitable_paths);
7532 free(fileindex_path);
7533 return err;
7536 const struct got_error *
7537 got_worktree_merge_complete(struct got_worktree *worktree,
7538 struct got_fileindex *fileindex, struct got_repository *repo)
7540 const struct got_error *err, *unlockerr, *sync_err;
7541 char *fileindex_path = NULL;
7543 err = delete_merge_refs(worktree, repo);
7544 if (err)
7545 goto done;
7547 err = get_fileindex_path(&fileindex_path, worktree);
7548 if (err)
7549 goto done;
7550 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7551 sync_err = sync_fileindex(fileindex, fileindex_path);
7552 if (sync_err && err == NULL)
7553 err = sync_err;
7554 done:
7555 got_fileindex_free(fileindex);
7556 free(fileindex_path);
7557 unlockerr = lock_worktree(worktree, LOCK_SH);
7558 if (unlockerr && err == NULL)
7559 err = unlockerr;
7560 return err;
7563 const struct got_error *
7564 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7565 struct got_repository *repo)
7567 const struct got_error *err;
7568 char *branch_refname = NULL;
7569 struct got_reference *branch_ref = NULL;
7571 *in_progress = 0;
7573 err = get_merge_branch_ref_name(&branch_refname, worktree);
7574 if (err)
7575 return err;
7576 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7577 free(branch_refname);
7578 if (err) {
7579 if (err->code != GOT_ERR_NOT_REF)
7580 return err;
7581 } else
7582 *in_progress = 1;
7584 return NULL;
7587 const struct got_error *got_worktree_merge_prepare(
7588 struct got_fileindex **fileindex, struct got_worktree *worktree,
7589 struct got_reference *branch, struct got_repository *repo)
7591 const struct got_error *err = NULL;
7592 char *fileindex_path = NULL;
7593 char *branch_refname = NULL, *commit_refname = NULL;
7594 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7595 struct got_reference *commit_ref = NULL;
7596 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7597 struct check_rebase_ok_arg ok_arg;
7599 *fileindex = NULL;
7601 err = lock_worktree(worktree, LOCK_EX);
7602 if (err)
7603 return err;
7605 err = open_fileindex(fileindex, &fileindex_path, worktree);
7606 if (err)
7607 goto done;
7609 /* Preconditions are the same as for rebase. */
7610 ok_arg.worktree = worktree;
7611 ok_arg.repo = repo;
7612 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7613 &ok_arg);
7614 if (err)
7615 goto done;
7617 err = get_merge_branch_ref_name(&branch_refname, worktree);
7618 if (err)
7619 return err;
7621 err = get_merge_commit_ref_name(&commit_refname, worktree);
7622 if (err)
7623 return err;
7625 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7626 0);
7627 if (err)
7628 goto done;
7630 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7631 if (err)
7632 goto done;
7634 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7635 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7636 goto done;
7639 err = got_ref_resolve(&branch_tip, repo, branch);
7640 if (err)
7641 goto done;
7643 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7644 if (err)
7645 goto done;
7646 err = got_ref_write(branch_ref, repo);
7647 if (err)
7648 goto done;
7650 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7651 if (err)
7652 goto done;
7653 err = got_ref_write(commit_ref, repo);
7654 if (err)
7655 goto done;
7657 done:
7658 free(branch_refname);
7659 free(commit_refname);
7660 free(fileindex_path);
7661 if (branch_ref)
7662 got_ref_close(branch_ref);
7663 if (commit_ref)
7664 got_ref_close(commit_ref);
7665 if (wt_branch)
7666 got_ref_close(wt_branch);
7667 free(wt_branch_tip);
7668 if (err) {
7669 if (*fileindex) {
7670 got_fileindex_free(*fileindex);
7671 *fileindex = NULL;
7673 lock_worktree(worktree, LOCK_SH);
7675 return err;
7678 const struct got_error *
7679 got_worktree_merge_continue(char **branch_name,
7680 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7681 struct got_worktree *worktree, struct got_repository *repo)
7683 const struct got_error *err;
7684 char *commit_refname = NULL, *branch_refname = NULL;
7685 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7686 char *fileindex_path = NULL;
7687 int have_staged_files = 0;
7689 *branch_name = NULL;
7690 *branch_tip = NULL;
7691 *fileindex = NULL;
7693 err = lock_worktree(worktree, LOCK_EX);
7694 if (err)
7695 return err;
7697 err = open_fileindex(fileindex, &fileindex_path, worktree);
7698 if (err)
7699 goto done;
7701 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7702 &have_staged_files);
7703 if (err && err->code != GOT_ERR_CANCELLED)
7704 goto done;
7705 if (have_staged_files) {
7706 err = got_error(GOT_ERR_STAGED_PATHS);
7707 goto done;
7710 err = get_merge_branch_ref_name(&branch_refname, worktree);
7711 if (err)
7712 goto done;
7714 err = get_merge_commit_ref_name(&commit_refname, worktree);
7715 if (err)
7716 goto done;
7718 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7719 if (err)
7720 goto done;
7722 if (!got_ref_is_symbolic(branch_ref)) {
7723 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7724 "%s is not a symbolic reference",
7725 got_ref_get_name(branch_ref));
7726 goto done;
7728 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7729 if (*branch_name == NULL) {
7730 err = got_error_from_errno("strdup");
7731 goto done;
7734 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7735 if (err)
7736 goto done;
7738 err = got_ref_resolve(branch_tip, repo, commit_ref);
7739 if (err)
7740 goto done;
7741 done:
7742 free(commit_refname);
7743 free(branch_refname);
7744 free(fileindex_path);
7745 if (commit_ref)
7746 got_ref_close(commit_ref);
7747 if (branch_ref)
7748 got_ref_close(branch_ref);
7749 if (err) {
7750 if (*branch_name) {
7751 free(*branch_name);
7752 *branch_name = NULL;
7754 free(*branch_tip);
7755 *branch_tip = NULL;
7756 if (*fileindex) {
7757 got_fileindex_free(*fileindex);
7758 *fileindex = NULL;
7760 lock_worktree(worktree, LOCK_SH);
7762 return err;
7765 const struct got_error *
7766 got_worktree_merge_abort(struct got_worktree *worktree,
7767 struct got_fileindex *fileindex, struct got_repository *repo,
7768 got_worktree_checkout_cb progress_cb, void *progress_arg)
7770 const struct got_error *err, *unlockerr, *sync_err;
7771 struct got_object_id *commit_id = NULL;
7772 char *fileindex_path = NULL;
7773 struct revert_file_args rfa;
7774 struct got_object_id *tree_id = NULL;
7776 err = got_object_id_by_path(&tree_id, repo,
7777 worktree->base_commit_id, worktree->path_prefix);
7778 if (err)
7779 goto done;
7781 err = delete_merge_refs(worktree, repo);
7782 if (err)
7783 goto done;
7785 err = get_fileindex_path(&fileindex_path, worktree);
7786 if (err)
7787 goto done;
7789 rfa.worktree = worktree;
7790 rfa.fileindex = fileindex;
7791 rfa.progress_cb = progress_cb;
7792 rfa.progress_arg = progress_arg;
7793 rfa.patch_cb = NULL;
7794 rfa.patch_arg = NULL;
7795 rfa.repo = repo;
7796 rfa.unlink_added_files = 1;
7797 err = worktree_status(worktree, "", fileindex, repo,
7798 revert_file, &rfa, NULL, NULL, 1, 0);
7799 if (err)
7800 goto sync;
7802 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7803 repo, progress_cb, progress_arg, NULL, NULL);
7804 sync:
7805 sync_err = sync_fileindex(fileindex, fileindex_path);
7806 if (sync_err && err == NULL)
7807 err = sync_err;
7808 done:
7809 free(tree_id);
7810 free(commit_id);
7811 if (fileindex)
7812 got_fileindex_free(fileindex);
7813 free(fileindex_path);
7815 unlockerr = lock_worktree(worktree, LOCK_SH);
7816 if (unlockerr && err == NULL)
7817 err = unlockerr;
7818 return err;
7821 struct check_stage_ok_arg {
7822 struct got_object_id *head_commit_id;
7823 struct got_worktree *worktree;
7824 struct got_fileindex *fileindex;
7825 struct got_repository *repo;
7826 int have_changes;
7829 const struct got_error *
7830 check_stage_ok(void *arg, unsigned char status,
7831 unsigned char staged_status, const char *relpath,
7832 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7833 struct got_object_id *commit_id, int dirfd, const char *de_name)
7835 struct check_stage_ok_arg *a = arg;
7836 const struct got_error *err = NULL;
7837 struct got_fileindex_entry *ie;
7838 struct got_object_id base_commit_id;
7839 struct got_object_id *base_commit_idp = NULL;
7840 char *in_repo_path = NULL, *p;
7842 if (status == GOT_STATUS_UNVERSIONED ||
7843 status == GOT_STATUS_NO_CHANGE)
7844 return NULL;
7845 if (status == GOT_STATUS_NONEXISTENT)
7846 return got_error_set_errno(ENOENT, relpath);
7848 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7849 if (ie == NULL)
7850 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7852 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7853 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7854 relpath) == -1)
7855 return got_error_from_errno("asprintf");
7857 if (got_fileindex_entry_has_commit(ie)) {
7858 memcpy(base_commit_id.sha1, ie->commit_sha1,
7859 SHA1_DIGEST_LENGTH);
7860 base_commit_idp = &base_commit_id;
7863 if (status == GOT_STATUS_CONFLICT) {
7864 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7865 goto done;
7866 } else if (status != GOT_STATUS_ADD &&
7867 status != GOT_STATUS_MODIFY &&
7868 status != GOT_STATUS_DELETE) {
7869 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7870 goto done;
7873 a->have_changes = 1;
7875 p = in_repo_path;
7876 while (p[0] == '/')
7877 p++;
7878 err = check_out_of_date(p, status, staged_status,
7879 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7880 GOT_ERR_STAGE_OUT_OF_DATE);
7881 done:
7882 free(in_repo_path);
7883 return err;
7886 struct stage_path_arg {
7887 struct got_worktree *worktree;
7888 struct got_fileindex *fileindex;
7889 struct got_repository *repo;
7890 got_worktree_status_cb status_cb;
7891 void *status_arg;
7892 got_worktree_patch_cb patch_cb;
7893 void *patch_arg;
7894 int staged_something;
7895 int allow_bad_symlinks;
7898 static const struct got_error *
7899 stage_path(void *arg, unsigned char status,
7900 unsigned char staged_status, const char *relpath,
7901 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7902 struct got_object_id *commit_id, int dirfd, const char *de_name)
7904 struct stage_path_arg *a = arg;
7905 const struct got_error *err = NULL;
7906 struct got_fileindex_entry *ie;
7907 char *ondisk_path = NULL, *path_content = NULL;
7908 uint32_t stage;
7909 struct got_object_id *new_staged_blob_id = NULL;
7910 struct stat sb;
7912 if (status == GOT_STATUS_UNVERSIONED)
7913 return NULL;
7915 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7916 if (ie == NULL)
7917 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7919 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7920 relpath)== -1)
7921 return got_error_from_errno("asprintf");
7923 switch (status) {
7924 case GOT_STATUS_ADD:
7925 case GOT_STATUS_MODIFY:
7926 /* XXX could sb.st_mode be passed in by our caller? */
7927 if (lstat(ondisk_path, &sb) == -1) {
7928 err = got_error_from_errno2("lstat", ondisk_path);
7929 break;
7931 if (a->patch_cb) {
7932 if (status == GOT_STATUS_ADD) {
7933 int choice = GOT_PATCH_CHOICE_NONE;
7934 err = (*a->patch_cb)(&choice, a->patch_arg,
7935 status, ie->path, NULL, 1, 1);
7936 if (err)
7937 break;
7938 if (choice != GOT_PATCH_CHOICE_YES)
7939 break;
7940 } else {
7941 err = create_patched_content(&path_content, 0,
7942 staged_blob_id ? staged_blob_id : blob_id,
7943 ondisk_path, dirfd, de_name, ie->path,
7944 a->repo, a->patch_cb, a->patch_arg);
7945 if (err || path_content == NULL)
7946 break;
7949 err = got_object_blob_create(&new_staged_blob_id,
7950 path_content ? path_content : ondisk_path, a->repo);
7951 if (err)
7952 break;
7953 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7954 SHA1_DIGEST_LENGTH);
7955 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7956 stage = GOT_FILEIDX_STAGE_ADD;
7957 else
7958 stage = GOT_FILEIDX_STAGE_MODIFY;
7959 got_fileindex_entry_stage_set(ie, stage);
7960 if (S_ISLNK(sb.st_mode)) {
7961 int is_bad_symlink = 0;
7962 if (!a->allow_bad_symlinks) {
7963 char target_path[PATH_MAX];
7964 ssize_t target_len;
7965 target_len = readlink(ondisk_path, target_path,
7966 sizeof(target_path));
7967 if (target_len == -1) {
7968 err = got_error_from_errno2("readlink",
7969 ondisk_path);
7970 break;
7972 err = is_bad_symlink_target(&is_bad_symlink,
7973 target_path, target_len, ondisk_path,
7974 a->worktree->root_path);
7975 if (err)
7976 break;
7977 if (is_bad_symlink) {
7978 err = got_error_path(ondisk_path,
7979 GOT_ERR_BAD_SYMLINK);
7980 break;
7983 if (is_bad_symlink)
7984 got_fileindex_entry_staged_filetype_set(ie,
7985 GOT_FILEIDX_MODE_BAD_SYMLINK);
7986 else
7987 got_fileindex_entry_staged_filetype_set(ie,
7988 GOT_FILEIDX_MODE_SYMLINK);
7989 } else {
7990 got_fileindex_entry_staged_filetype_set(ie,
7991 GOT_FILEIDX_MODE_REGULAR_FILE);
7993 a->staged_something = 1;
7994 if (a->status_cb == NULL)
7995 break;
7996 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7997 get_staged_status(ie), relpath, blob_id,
7998 new_staged_blob_id, NULL, dirfd, de_name);
7999 break;
8000 case GOT_STATUS_DELETE:
8001 if (staged_status == GOT_STATUS_DELETE)
8002 break;
8003 if (a->patch_cb) {
8004 int choice = GOT_PATCH_CHOICE_NONE;
8005 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8006 ie->path, NULL, 1, 1);
8007 if (err)
8008 break;
8009 if (choice == GOT_PATCH_CHOICE_NO)
8010 break;
8011 if (choice != GOT_PATCH_CHOICE_YES) {
8012 err = got_error(GOT_ERR_PATCH_CHOICE);
8013 break;
8016 stage = GOT_FILEIDX_STAGE_DELETE;
8017 got_fileindex_entry_stage_set(ie, stage);
8018 a->staged_something = 1;
8019 if (a->status_cb == NULL)
8020 break;
8021 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8022 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8023 de_name);
8024 break;
8025 case GOT_STATUS_NO_CHANGE:
8026 break;
8027 case GOT_STATUS_CONFLICT:
8028 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8029 break;
8030 case GOT_STATUS_NONEXISTENT:
8031 err = got_error_set_errno(ENOENT, relpath);
8032 break;
8033 default:
8034 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8035 break;
8038 if (path_content && unlink(path_content) == -1 && err == NULL)
8039 err = got_error_from_errno2("unlink", path_content);
8040 free(path_content);
8041 free(ondisk_path);
8042 free(new_staged_blob_id);
8043 return err;
8046 const struct got_error *
8047 got_worktree_stage(struct got_worktree *worktree,
8048 struct got_pathlist_head *paths,
8049 got_worktree_status_cb status_cb, void *status_arg,
8050 got_worktree_patch_cb patch_cb, void *patch_arg,
8051 int allow_bad_symlinks, struct got_repository *repo)
8053 const struct got_error *err = NULL, *sync_err, *unlockerr;
8054 struct got_pathlist_entry *pe;
8055 struct got_fileindex *fileindex = NULL;
8056 char *fileindex_path = NULL;
8057 struct got_reference *head_ref = NULL;
8058 struct got_object_id *head_commit_id = NULL;
8059 struct check_stage_ok_arg oka;
8060 struct stage_path_arg spa;
8062 err = lock_worktree(worktree, LOCK_EX);
8063 if (err)
8064 return err;
8066 err = got_ref_open(&head_ref, repo,
8067 got_worktree_get_head_ref_name(worktree), 0);
8068 if (err)
8069 goto done;
8070 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8071 if (err)
8072 goto done;
8073 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8074 if (err)
8075 goto done;
8077 /* Check pre-conditions before staging anything. */
8078 oka.head_commit_id = head_commit_id;
8079 oka.worktree = worktree;
8080 oka.fileindex = fileindex;
8081 oka.repo = repo;
8082 oka.have_changes = 0;
8083 TAILQ_FOREACH(pe, paths, entry) {
8084 err = worktree_status(worktree, pe->path, fileindex, repo,
8085 check_stage_ok, &oka, NULL, NULL, 1, 0);
8086 if (err)
8087 goto done;
8089 if (!oka.have_changes) {
8090 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8091 goto done;
8094 spa.worktree = worktree;
8095 spa.fileindex = fileindex;
8096 spa.repo = repo;
8097 spa.patch_cb = patch_cb;
8098 spa.patch_arg = patch_arg;
8099 spa.status_cb = status_cb;
8100 spa.status_arg = status_arg;
8101 spa.staged_something = 0;
8102 spa.allow_bad_symlinks = allow_bad_symlinks;
8103 TAILQ_FOREACH(pe, paths, entry) {
8104 err = worktree_status(worktree, pe->path, fileindex, repo,
8105 stage_path, &spa, NULL, NULL, 1, 0);
8106 if (err)
8107 goto done;
8109 if (!spa.staged_something) {
8110 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8111 goto done;
8114 sync_err = sync_fileindex(fileindex, fileindex_path);
8115 if (sync_err && err == NULL)
8116 err = sync_err;
8117 done:
8118 if (head_ref)
8119 got_ref_close(head_ref);
8120 free(head_commit_id);
8121 free(fileindex_path);
8122 if (fileindex)
8123 got_fileindex_free(fileindex);
8124 unlockerr = lock_worktree(worktree, LOCK_SH);
8125 if (unlockerr && err == NULL)
8126 err = unlockerr;
8127 return err;
8130 struct unstage_path_arg {
8131 struct got_worktree *worktree;
8132 struct got_fileindex *fileindex;
8133 struct got_repository *repo;
8134 got_worktree_checkout_cb progress_cb;
8135 void *progress_arg;
8136 got_worktree_patch_cb patch_cb;
8137 void *patch_arg;
8140 static const struct got_error *
8141 create_unstaged_content(char **path_unstaged_content,
8142 char **path_new_staged_content, struct got_object_id *blob_id,
8143 struct got_object_id *staged_blob_id, const char *relpath,
8144 struct got_repository *repo,
8145 got_worktree_patch_cb patch_cb, void *patch_arg)
8147 const struct got_error *err, *free_err;
8148 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8149 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8150 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8151 struct got_diffreg_result *diffreg_result = NULL;
8152 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8153 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8155 *path_unstaged_content = NULL;
8156 *path_new_staged_content = NULL;
8158 err = got_object_id_str(&label1, blob_id);
8159 if (err)
8160 return err;
8161 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8162 if (err)
8163 goto done;
8165 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8166 if (err)
8167 goto done;
8169 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8170 if (err)
8171 goto done;
8173 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8174 if (err)
8175 goto done;
8177 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8178 if (err)
8179 goto done;
8181 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8182 if (err)
8183 goto done;
8185 err = got_diff_files(&diffreg_result, f1, label1, f2,
8186 path2, 3, 0, 1, NULL);
8187 if (err)
8188 goto done;
8190 err = got_opentemp_named(path_unstaged_content, &outfile,
8191 "got-unstaged-content");
8192 if (err)
8193 goto done;
8194 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8195 "got-new-staged-content");
8196 if (err)
8197 goto done;
8199 if (fseek(f1, 0L, SEEK_SET) == -1) {
8200 err = got_ferror(f1, GOT_ERR_IO);
8201 goto done;
8203 if (fseek(f2, 0L, SEEK_SET) == -1) {
8204 err = got_ferror(f2, GOT_ERR_IO);
8205 goto done;
8207 /* Count the number of actual changes in the diff result. */
8208 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8209 struct diff_chunk_context cc = {};
8210 diff_chunk_context_load_change(&cc, &nchunks_used,
8211 diffreg_result->result, n, 0);
8212 nchanges++;
8214 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8215 int choice;
8216 err = apply_or_reject_change(&choice, &nchunks_used,
8217 diffreg_result->result, n, relpath, f1, f2,
8218 &line_cur1, &line_cur2,
8219 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8220 if (err)
8221 goto done;
8222 if (choice == GOT_PATCH_CHOICE_YES)
8223 have_content = 1;
8224 else
8225 have_rejected_content = 1;
8226 if (choice == GOT_PATCH_CHOICE_QUIT)
8227 break;
8229 if (have_content || have_rejected_content)
8230 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8231 outfile, rejectfile);
8232 done:
8233 free(label1);
8234 if (blob)
8235 got_object_blob_close(blob);
8236 if (staged_blob)
8237 got_object_blob_close(staged_blob);
8238 free_err = got_diffreg_result_free(diffreg_result);
8239 if (free_err && err == NULL)
8240 err = free_err;
8241 if (f1 && fclose(f1) == EOF && err == NULL)
8242 err = got_error_from_errno2("fclose", path1);
8243 if (f2 && fclose(f2) == EOF && err == NULL)
8244 err = got_error_from_errno2("fclose", path2);
8245 if (outfile && fclose(outfile) == EOF && err == NULL)
8246 err = got_error_from_errno2("fclose", *path_unstaged_content);
8247 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8248 err = got_error_from_errno2("fclose", *path_new_staged_content);
8249 if (path1 && unlink(path1) == -1 && err == NULL)
8250 err = got_error_from_errno2("unlink", path1);
8251 if (path2 && unlink(path2) == -1 && err == NULL)
8252 err = got_error_from_errno2("unlink", path2);
8253 if (err || !have_content) {
8254 if (*path_unstaged_content &&
8255 unlink(*path_unstaged_content) == -1 && err == NULL)
8256 err = got_error_from_errno2("unlink",
8257 *path_unstaged_content);
8258 free(*path_unstaged_content);
8259 *path_unstaged_content = NULL;
8261 if (err || !have_content || !have_rejected_content) {
8262 if (*path_new_staged_content &&
8263 unlink(*path_new_staged_content) == -1 && err == NULL)
8264 err = got_error_from_errno2("unlink",
8265 *path_new_staged_content);
8266 free(*path_new_staged_content);
8267 *path_new_staged_content = NULL;
8269 free(path1);
8270 free(path2);
8271 return err;
8274 static const struct got_error *
8275 unstage_hunks(struct got_object_id *staged_blob_id,
8276 struct got_blob_object *blob_base,
8277 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8278 const char *ondisk_path, const char *label_orig,
8279 struct got_worktree *worktree, struct got_repository *repo,
8280 got_worktree_patch_cb patch_cb, void *patch_arg,
8281 got_worktree_checkout_cb progress_cb, void *progress_arg)
8283 const struct got_error *err = NULL;
8284 char *path_unstaged_content = NULL;
8285 char *path_new_staged_content = NULL;
8286 char *parent = NULL, *base_path = NULL;
8287 char *blob_base_path = NULL;
8288 struct got_object_id *new_staged_blob_id = NULL;
8289 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8290 struct stat sb;
8292 err = create_unstaged_content(&path_unstaged_content,
8293 &path_new_staged_content, blob_id, staged_blob_id,
8294 ie->path, repo, patch_cb, patch_arg);
8295 if (err)
8296 return err;
8298 if (path_unstaged_content == NULL)
8299 return NULL;
8301 if (path_new_staged_content) {
8302 err = got_object_blob_create(&new_staged_blob_id,
8303 path_new_staged_content, repo);
8304 if (err)
8305 goto done;
8308 f = fopen(path_unstaged_content, "re");
8309 if (f == NULL) {
8310 err = got_error_from_errno2("fopen",
8311 path_unstaged_content);
8312 goto done;
8314 if (fstat(fileno(f), &sb) == -1) {
8315 err = got_error_from_errno2("fstat", path_unstaged_content);
8316 goto done;
8318 if (got_fileindex_entry_staged_filetype_get(ie) ==
8319 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8320 char link_target[PATH_MAX];
8321 size_t r;
8322 r = fread(link_target, 1, sizeof(link_target), f);
8323 if (r == 0 && ferror(f)) {
8324 err = got_error_from_errno("fread");
8325 goto done;
8327 if (r >= sizeof(link_target)) { /* should not happen */
8328 err = got_error(GOT_ERR_NO_SPACE);
8329 goto done;
8331 link_target[r] = '\0';
8332 err = merge_symlink(worktree, blob_base,
8333 ondisk_path, ie->path, label_orig, link_target,
8334 worktree->base_commit_id, repo, progress_cb,
8335 progress_arg);
8336 } else {
8337 int local_changes_subsumed;
8339 err = got_path_dirname(&parent, ondisk_path);
8340 if (err)
8341 return err;
8343 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8344 parent) == -1) {
8345 err = got_error_from_errno("asprintf");
8346 base_path = NULL;
8347 goto done;
8350 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8351 if (err)
8352 goto done;
8353 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8354 blob_base);
8355 if (err)
8356 goto done;
8359 * In order the run a 3-way merge with a symlink we copy the symlink's
8360 * target path into a temporary file and use that file with diff3.
8362 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8363 err = dump_symlink_target_path_to_file(&f_deriv2,
8364 ondisk_path);
8365 if (err)
8366 goto done;
8367 } else {
8368 int fd;
8369 fd = open(ondisk_path,
8370 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8371 if (fd == -1) {
8372 err = got_error_from_errno2("open", ondisk_path);
8373 goto done;
8375 f_deriv2 = fdopen(fd, "r");
8376 if (f_deriv2 == NULL) {
8377 err = got_error_from_errno2("fdopen", ondisk_path);
8378 close(fd);
8379 goto done;
8383 err = merge_file(&local_changes_subsumed, worktree,
8384 f_base, f, f_deriv2, ondisk_path, ie->path,
8385 got_fileindex_perms_to_st(ie),
8386 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8387 repo, progress_cb, progress_arg);
8389 if (err)
8390 goto done;
8392 if (new_staged_blob_id) {
8393 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8394 SHA1_DIGEST_LENGTH);
8395 } else {
8396 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8397 got_fileindex_entry_staged_filetype_set(ie, 0);
8399 done:
8400 free(new_staged_blob_id);
8401 if (path_unstaged_content &&
8402 unlink(path_unstaged_content) == -1 && err == NULL)
8403 err = got_error_from_errno2("unlink", path_unstaged_content);
8404 if (path_new_staged_content &&
8405 unlink(path_new_staged_content) == -1 && err == NULL)
8406 err = got_error_from_errno2("unlink", path_new_staged_content);
8407 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8408 err = got_error_from_errno2("unlink", blob_base_path);
8409 if (f_base && fclose(f_base) == EOF && err == NULL)
8410 err = got_error_from_errno2("fclose", path_unstaged_content);
8411 if (f && fclose(f) == EOF && err == NULL)
8412 err = got_error_from_errno2("fclose", path_unstaged_content);
8413 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8414 err = got_error_from_errno2("fclose", ondisk_path);
8415 free(path_unstaged_content);
8416 free(path_new_staged_content);
8417 free(blob_base_path);
8418 free(parent);
8419 free(base_path);
8420 return err;
8423 static const struct got_error *
8424 unstage_path(void *arg, unsigned char status,
8425 unsigned char staged_status, const char *relpath,
8426 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8427 struct got_object_id *commit_id, int dirfd, const char *de_name)
8429 const struct got_error *err = NULL;
8430 struct unstage_path_arg *a = arg;
8431 struct got_fileindex_entry *ie;
8432 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8433 char *ondisk_path = NULL;
8434 char *id_str = NULL, *label_orig = NULL;
8435 int local_changes_subsumed;
8436 struct stat sb;
8438 if (staged_status != GOT_STATUS_ADD &&
8439 staged_status != GOT_STATUS_MODIFY &&
8440 staged_status != GOT_STATUS_DELETE)
8441 return NULL;
8443 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8444 if (ie == NULL)
8445 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8447 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8448 == -1)
8449 return got_error_from_errno("asprintf");
8451 err = got_object_id_str(&id_str,
8452 commit_id ? commit_id : a->worktree->base_commit_id);
8453 if (err)
8454 goto done;
8455 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8456 id_str) == -1) {
8457 err = got_error_from_errno("asprintf");
8458 goto done;
8461 switch (staged_status) {
8462 case GOT_STATUS_MODIFY:
8463 err = got_object_open_as_blob(&blob_base, a->repo,
8464 blob_id, 8192);
8465 if (err)
8466 break;
8467 /* fall through */
8468 case GOT_STATUS_ADD:
8469 if (a->patch_cb) {
8470 if (staged_status == GOT_STATUS_ADD) {
8471 int choice = GOT_PATCH_CHOICE_NONE;
8472 err = (*a->patch_cb)(&choice, a->patch_arg,
8473 staged_status, ie->path, NULL, 1, 1);
8474 if (err)
8475 break;
8476 if (choice != GOT_PATCH_CHOICE_YES)
8477 break;
8478 } else {
8479 err = unstage_hunks(staged_blob_id,
8480 blob_base, blob_id, ie, ondisk_path,
8481 label_orig, a->worktree, a->repo,
8482 a->patch_cb, a->patch_arg,
8483 a->progress_cb, a->progress_arg);
8484 break; /* Done with this file. */
8487 err = got_object_open_as_blob(&blob_staged, a->repo,
8488 staged_blob_id, 8192);
8489 if (err)
8490 break;
8491 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8492 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8493 case GOT_FILEIDX_MODE_REGULAR_FILE:
8494 err = merge_blob(&local_changes_subsumed, a->worktree,
8495 blob_base, ondisk_path, relpath,
8496 got_fileindex_perms_to_st(ie), label_orig,
8497 blob_staged, commit_id ? commit_id :
8498 a->worktree->base_commit_id, a->repo,
8499 a->progress_cb, a->progress_arg);
8500 break;
8501 case GOT_FILEIDX_MODE_SYMLINK:
8502 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8503 char *staged_target;
8504 err = got_object_blob_read_to_str(
8505 &staged_target, blob_staged);
8506 if (err)
8507 goto done;
8508 err = merge_symlink(a->worktree, blob_base,
8509 ondisk_path, relpath, label_orig,
8510 staged_target, commit_id ? commit_id :
8511 a->worktree->base_commit_id,
8512 a->repo, a->progress_cb, a->progress_arg);
8513 free(staged_target);
8514 } else {
8515 err = merge_blob(&local_changes_subsumed,
8516 a->worktree, blob_base, ondisk_path,
8517 relpath, got_fileindex_perms_to_st(ie),
8518 label_orig, blob_staged,
8519 commit_id ? commit_id :
8520 a->worktree->base_commit_id, a->repo,
8521 a->progress_cb, a->progress_arg);
8523 break;
8524 default:
8525 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8526 break;
8528 if (err == NULL) {
8529 got_fileindex_entry_stage_set(ie,
8530 GOT_FILEIDX_STAGE_NONE);
8531 got_fileindex_entry_staged_filetype_set(ie, 0);
8533 break;
8534 case GOT_STATUS_DELETE:
8535 if (a->patch_cb) {
8536 int choice = GOT_PATCH_CHOICE_NONE;
8537 err = (*a->patch_cb)(&choice, a->patch_arg,
8538 staged_status, ie->path, NULL, 1, 1);
8539 if (err)
8540 break;
8541 if (choice == GOT_PATCH_CHOICE_NO)
8542 break;
8543 if (choice != GOT_PATCH_CHOICE_YES) {
8544 err = got_error(GOT_ERR_PATCH_CHOICE);
8545 break;
8548 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8549 got_fileindex_entry_staged_filetype_set(ie, 0);
8550 err = get_file_status(&status, &sb, ie, ondisk_path,
8551 dirfd, de_name, a->repo);
8552 if (err)
8553 break;
8554 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8555 break;
8557 done:
8558 free(ondisk_path);
8559 if (blob_base)
8560 got_object_blob_close(blob_base);
8561 if (blob_staged)
8562 got_object_blob_close(blob_staged);
8563 free(id_str);
8564 free(label_orig);
8565 return err;
8568 const struct got_error *
8569 got_worktree_unstage(struct got_worktree *worktree,
8570 struct got_pathlist_head *paths,
8571 got_worktree_checkout_cb progress_cb, void *progress_arg,
8572 got_worktree_patch_cb patch_cb, void *patch_arg,
8573 struct got_repository *repo)
8575 const struct got_error *err = NULL, *sync_err, *unlockerr;
8576 struct got_pathlist_entry *pe;
8577 struct got_fileindex *fileindex = NULL;
8578 char *fileindex_path = NULL;
8579 struct unstage_path_arg upa;
8581 err = lock_worktree(worktree, LOCK_EX);
8582 if (err)
8583 return err;
8585 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8586 if (err)
8587 goto done;
8589 upa.worktree = worktree;
8590 upa.fileindex = fileindex;
8591 upa.repo = repo;
8592 upa.progress_cb = progress_cb;
8593 upa.progress_arg = progress_arg;
8594 upa.patch_cb = patch_cb;
8595 upa.patch_arg = patch_arg;
8596 TAILQ_FOREACH(pe, paths, entry) {
8597 err = worktree_status(worktree, pe->path, fileindex, repo,
8598 unstage_path, &upa, NULL, NULL, 1, 0);
8599 if (err)
8600 goto done;
8603 sync_err = sync_fileindex(fileindex, fileindex_path);
8604 if (sync_err && err == NULL)
8605 err = sync_err;
8606 done:
8607 free(fileindex_path);
8608 if (fileindex)
8609 got_fileindex_free(fileindex);
8610 unlockerr = lock_worktree(worktree, LOCK_SH);
8611 if (unlockerr && err == NULL)
8612 err = unlockerr;
8613 return err;
8616 struct report_file_info_arg {
8617 struct got_worktree *worktree;
8618 got_worktree_path_info_cb info_cb;
8619 void *info_arg;
8620 struct got_pathlist_head *paths;
8621 got_cancel_cb cancel_cb;
8622 void *cancel_arg;
8625 static const struct got_error *
8626 report_file_info(void *arg, struct got_fileindex_entry *ie)
8628 struct report_file_info_arg *a = arg;
8629 struct got_pathlist_entry *pe;
8630 struct got_object_id blob_id, staged_blob_id, commit_id;
8631 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8632 struct got_object_id *commit_idp = NULL;
8633 int stage;
8635 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8636 return got_error(GOT_ERR_CANCELLED);
8638 TAILQ_FOREACH(pe, a->paths, entry) {
8639 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8640 got_path_is_child(ie->path, pe->path, pe->path_len))
8641 break;
8643 if (pe == NULL) /* not found */
8644 return NULL;
8646 if (got_fileindex_entry_has_blob(ie)) {
8647 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8648 blob_idp = &blob_id;
8650 stage = got_fileindex_entry_stage_get(ie);
8651 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8652 stage == GOT_FILEIDX_STAGE_ADD) {
8653 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8654 SHA1_DIGEST_LENGTH);
8655 staged_blob_idp = &staged_blob_id;
8658 if (got_fileindex_entry_has_commit(ie)) {
8659 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8660 commit_idp = &commit_id;
8663 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8664 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8667 const struct got_error *
8668 got_worktree_path_info(struct got_worktree *worktree,
8669 struct got_pathlist_head *paths,
8670 got_worktree_path_info_cb info_cb, void *info_arg,
8671 got_cancel_cb cancel_cb, void *cancel_arg)
8674 const struct got_error *err = NULL, *unlockerr;
8675 struct got_fileindex *fileindex = NULL;
8676 char *fileindex_path = NULL;
8677 struct report_file_info_arg arg;
8679 err = lock_worktree(worktree, LOCK_SH);
8680 if (err)
8681 return err;
8683 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8684 if (err)
8685 goto done;
8687 arg.worktree = worktree;
8688 arg.info_cb = info_cb;
8689 arg.info_arg = info_arg;
8690 arg.paths = paths;
8691 arg.cancel_cb = cancel_cb;
8692 arg.cancel_arg = cancel_arg;
8693 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8694 &arg);
8695 done:
8696 free(fileindex_path);
8697 if (fileindex)
8698 got_fileindex_free(fileindex);
8699 unlockerr = lock_worktree(worktree, LOCK_UN);
8700 if (unlockerr && err == NULL)
8701 err = unlockerr;
8702 return err;