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;
2886 * Delete the added file only if its content already
2887 * exists in the repository.
2889 err = got_object_blob_file_create(&id, &blob1_f, path1);
2890 if (err)
2891 goto done;
2892 if (got_object_id_cmp(id, id1) == 0) {
2893 err = (*a->progress_cb)(a->progress_arg,
2894 GOT_STATUS_DELETE, path1);
2895 if (err)
2896 goto done;
2897 err = remove_ondisk_file(a->worktree->root_path,
2898 path1);
2899 if (err)
2900 goto done;
2901 if (ie)
2902 got_fileindex_entry_remove(a->fileindex,
2903 ie);
2904 } else {
2905 err = (*a->progress_cb)(a->progress_arg,
2906 GOT_STATUS_CANNOT_DELETE, path1);
2908 if (fclose(blob1_f) == EOF && err == NULL)
2909 err = got_error_from_errno("fclose");
2910 free(id);
2911 if (err)
2912 goto done;
2913 break;
2915 case GOT_STATUS_MODIFY:
2916 case GOT_STATUS_CONFLICT:
2917 err = (*a->progress_cb)(a->progress_arg,
2918 GOT_STATUS_CANNOT_DELETE, path1);
2919 if (err)
2920 goto done;
2921 break;
2922 case GOT_STATUS_OBSTRUCTED:
2923 err = (*a->progress_cb)(a->progress_arg, status, path1);
2924 if (err)
2925 goto done;
2926 break;
2927 default:
2928 break;
2930 } else if (blob2) {
2931 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2932 path2) == -1)
2933 return got_error_from_errno("asprintf");
2934 ie = got_fileindex_entry_get(a->fileindex, path2,
2935 strlen(path2));
2936 if (ie) {
2937 err = get_file_status(&status, &sb, ie, ondisk_path,
2938 -1, NULL, repo);
2939 if (err)
2940 goto done;
2941 if (status != GOT_STATUS_NO_CHANGE &&
2942 status != GOT_STATUS_MODIFY &&
2943 status != GOT_STATUS_CONFLICT &&
2944 status != GOT_STATUS_ADD) {
2945 err = (*a->progress_cb)(a->progress_arg,
2946 status, path2);
2947 goto done;
2949 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2950 char *link_target2;
2951 err = got_object_blob_read_to_str(&link_target2,
2952 blob2);
2953 if (err)
2954 goto done;
2955 err = merge_symlink(a->worktree, NULL,
2956 ondisk_path, path2, a->label_orig,
2957 link_target2, a->commit_id2, repo,
2958 a->progress_cb, a->progress_arg);
2959 free(link_target2);
2960 } else if (S_ISREG(sb.st_mode)) {
2961 err = merge_blob(&local_changes_subsumed,
2962 a->worktree, NULL, ondisk_path, path2,
2963 sb.st_mode, a->label_orig, blob2,
2964 a->commit_id2, repo, a->progress_cb,
2965 a->progress_arg);
2966 } else {
2967 err = got_error_path(ondisk_path,
2968 GOT_ERR_FILE_OBSTRUCTED);
2970 if (err)
2971 goto done;
2972 if (status == GOT_STATUS_DELETE) {
2973 err = got_fileindex_entry_update(ie,
2974 a->worktree->root_fd, path2, blob2->id.sha1,
2975 a->worktree->base_commit_id->sha1, 0);
2976 if (err)
2977 goto done;
2979 } else {
2980 int is_bad_symlink = 0;
2981 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2982 if (S_ISLNK(mode2)) {
2983 err = install_symlink(&is_bad_symlink,
2984 a->worktree, ondisk_path, path2, blob2, 0,
2985 0, 1, a->allow_bad_symlinks, repo,
2986 a->progress_cb, a->progress_arg);
2987 } else {
2988 err = install_blob(a->worktree, ondisk_path, path2,
2989 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2990 a->progress_cb, a->progress_arg);
2992 if (err)
2993 goto done;
2994 err = got_fileindex_entry_alloc(&ie, path2);
2995 if (err)
2996 goto done;
2997 err = got_fileindex_entry_update(ie,
2998 a->worktree->root_fd, path2, NULL, NULL, 1);
2999 if (err) {
3000 got_fileindex_entry_free(ie);
3001 goto done;
3003 err = got_fileindex_entry_add(a->fileindex, ie);
3004 if (err) {
3005 got_fileindex_entry_free(ie);
3006 goto done;
3008 if (is_bad_symlink) {
3009 got_fileindex_entry_filetype_set(ie,
3010 GOT_FILEIDX_MODE_BAD_SYMLINK);
3014 done:
3015 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3016 err = got_error_from_errno("fclose");
3017 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3018 err = got_error_from_errno("fclose");
3019 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3020 err = got_error_from_errno("fclose");
3021 free(id_str);
3022 free(label_deriv2);
3023 free(ondisk_path);
3024 return err;
3027 static const struct got_error *
3028 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3030 struct got_worktree *worktree = arg;
3032 /* Reject merges into a work tree with mixed base commits. */
3033 if (got_fileindex_entry_has_commit(ie) &&
3034 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3035 SHA1_DIGEST_LENGTH) != 0)
3036 return got_error(GOT_ERR_MIXED_COMMITS);
3038 return NULL;
3041 struct check_merge_conflicts_arg {
3042 struct got_worktree *worktree;
3043 struct got_fileindex *fileindex;
3044 struct got_repository *repo;
3047 static const struct got_error *
3048 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3049 struct got_blob_object *blob2, struct got_object_id *id1,
3050 struct got_object_id *id2, const char *path1, const char *path2,
3051 mode_t mode1, mode_t mode2, struct got_repository *repo)
3053 const struct got_error *err = NULL;
3054 struct check_merge_conflicts_arg *a = arg;
3055 unsigned char status;
3056 struct stat sb;
3057 struct got_fileindex_entry *ie;
3058 const char *path = path2 ? path2 : path1;
3059 struct got_object_id *id = id2 ? id2 : id1;
3060 char *ondisk_path;
3062 if (id == NULL)
3063 return NULL;
3065 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3066 if (ie == NULL)
3067 return NULL;
3069 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3070 == -1)
3071 return got_error_from_errno("asprintf");
3073 /* Reject merges into a work tree with conflicted files. */
3074 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3075 free(ondisk_path);
3076 if (err)
3077 return err;
3078 if (status == GOT_STATUS_CONFLICT)
3079 return got_error(GOT_ERR_CONFLICTS);
3081 return NULL;
3084 static const struct got_error *
3085 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3086 const char *fileindex_path, struct got_object_id *commit_id1,
3087 struct got_object_id *commit_id2, struct got_repository *repo,
3088 got_worktree_checkout_cb progress_cb, void *progress_arg,
3089 got_cancel_cb cancel_cb, void *cancel_arg)
3091 const struct got_error *err = NULL, *sync_err;
3092 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3093 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3094 struct check_merge_conflicts_arg cmc_arg;
3095 struct merge_file_cb_arg arg;
3096 char *label_orig = NULL;
3098 if (commit_id1) {
3099 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3100 worktree->path_prefix);
3101 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3102 goto done;
3104 if (tree_id1) {
3105 char *id_str;
3107 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3108 if (err)
3109 goto done;
3111 err = got_object_id_str(&id_str, commit_id1);
3112 if (err)
3113 goto done;
3115 if (asprintf(&label_orig, "%s: commit %s",
3116 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3117 err = got_error_from_errno("asprintf");
3118 free(id_str);
3119 goto done;
3121 free(id_str);
3124 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3125 worktree->path_prefix);
3126 if (err)
3127 goto done;
3129 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3130 if (err)
3131 goto done;
3133 cmc_arg.worktree = worktree;
3134 cmc_arg.fileindex = fileindex;
3135 cmc_arg.repo = repo;
3136 err = got_diff_tree(tree1, tree2, "", "", repo,
3137 check_merge_conflicts, &cmc_arg, 0);
3138 if (err)
3139 goto done;
3141 arg.worktree = worktree;
3142 arg.fileindex = fileindex;
3143 arg.progress_cb = progress_cb;
3144 arg.progress_arg = progress_arg;
3145 arg.cancel_cb = cancel_cb;
3146 arg.cancel_arg = cancel_arg;
3147 arg.label_orig = label_orig;
3148 arg.commit_id2 = commit_id2;
3149 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3150 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3151 sync_err = sync_fileindex(fileindex, fileindex_path);
3152 if (sync_err && err == NULL)
3153 err = sync_err;
3154 done:
3155 if (tree1)
3156 got_object_tree_close(tree1);
3157 if (tree2)
3158 got_object_tree_close(tree2);
3159 free(label_orig);
3160 return err;
3163 const struct got_error *
3164 got_worktree_merge_files(struct got_worktree *worktree,
3165 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3166 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3167 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3169 const struct got_error *err, *unlockerr;
3170 char *fileindex_path = NULL;
3171 struct got_fileindex *fileindex = NULL;
3173 err = lock_worktree(worktree, LOCK_EX);
3174 if (err)
3175 return err;
3177 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3178 if (err)
3179 goto done;
3181 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3182 worktree);
3183 if (err)
3184 goto done;
3186 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3187 commit_id2, repo, progress_cb, progress_arg,
3188 cancel_cb, cancel_arg);
3189 done:
3190 if (fileindex)
3191 got_fileindex_free(fileindex);
3192 free(fileindex_path);
3193 unlockerr = lock_worktree(worktree, LOCK_SH);
3194 if (unlockerr && err == NULL)
3195 err = unlockerr;
3196 return err;
3199 struct diff_dir_cb_arg {
3200 struct got_fileindex *fileindex;
3201 struct got_worktree *worktree;
3202 const char *status_path;
3203 size_t status_path_len;
3204 struct got_repository *repo;
3205 got_worktree_status_cb status_cb;
3206 void *status_arg;
3207 got_cancel_cb cancel_cb;
3208 void *cancel_arg;
3209 /* A pathlist containing per-directory pathlists of ignore patterns. */
3210 struct got_pathlist_head *ignores;
3211 int report_unchanged;
3212 int no_ignores;
3215 static const struct got_error *
3216 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3217 int dirfd, const char *de_name,
3218 got_worktree_status_cb status_cb, void *status_arg,
3219 struct got_repository *repo, int report_unchanged)
3221 const struct got_error *err = NULL;
3222 unsigned char status = GOT_STATUS_NO_CHANGE;
3223 unsigned char staged_status = get_staged_status(ie);
3224 struct stat sb;
3225 struct got_object_id blob_id, commit_id, staged_blob_id;
3226 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3227 struct got_object_id *staged_blob_idp = NULL;
3229 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3230 if (err)
3231 return err;
3233 if (status == GOT_STATUS_NO_CHANGE &&
3234 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3235 return NULL;
3237 if (got_fileindex_entry_has_blob(ie)) {
3238 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3239 blob_idp = &blob_id;
3241 if (got_fileindex_entry_has_commit(ie)) {
3242 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3243 commit_idp = &commit_id;
3245 if (staged_status == GOT_STATUS_ADD ||
3246 staged_status == GOT_STATUS_MODIFY) {
3247 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3248 SHA1_DIGEST_LENGTH);
3249 staged_blob_idp = &staged_blob_id;
3252 return (*status_cb)(status_arg, status, staged_status,
3253 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3256 static const struct got_error *
3257 status_old_new(void *arg, struct got_fileindex_entry *ie,
3258 struct dirent *de, const char *parent_path, int dirfd)
3260 const struct got_error *err = NULL;
3261 struct diff_dir_cb_arg *a = arg;
3262 char *abspath;
3264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3265 return got_error(GOT_ERR_CANCELLED);
3267 if (got_path_cmp(parent_path, a->status_path,
3268 strlen(parent_path), a->status_path_len) != 0 &&
3269 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3270 return NULL;
3272 if (parent_path[0]) {
3273 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3274 parent_path, de->d_name) == -1)
3275 return got_error_from_errno("asprintf");
3276 } else {
3277 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3278 de->d_name) == -1)
3279 return got_error_from_errno("asprintf");
3282 err = report_file_status(ie, abspath, dirfd, de->d_name,
3283 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3284 free(abspath);
3285 return err;
3288 static const struct got_error *
3289 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3291 struct diff_dir_cb_arg *a = arg;
3292 struct got_object_id blob_id, commit_id;
3293 unsigned char status;
3295 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3296 return got_error(GOT_ERR_CANCELLED);
3298 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3299 return NULL;
3301 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3302 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3303 if (got_fileindex_entry_has_file_on_disk(ie))
3304 status = GOT_STATUS_MISSING;
3305 else
3306 status = GOT_STATUS_DELETE;
3307 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3308 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3311 void
3312 free_ignorelist(struct got_pathlist_head *ignorelist)
3314 struct got_pathlist_entry *pe;
3316 TAILQ_FOREACH(pe, ignorelist, entry)
3317 free((char *)pe->path);
3318 got_pathlist_free(ignorelist);
3321 void
3322 free_ignores(struct got_pathlist_head *ignores)
3324 struct got_pathlist_entry *pe;
3326 TAILQ_FOREACH(pe, ignores, entry) {
3327 struct got_pathlist_head *ignorelist = pe->data;
3328 free_ignorelist(ignorelist);
3329 free((char *)pe->path);
3331 got_pathlist_free(ignores);
3334 static const struct got_error *
3335 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3337 const struct got_error *err = NULL;
3338 struct got_pathlist_entry *pe = NULL;
3339 struct got_pathlist_head *ignorelist;
3340 char *line = NULL, *pattern, *dirpath = NULL;
3341 size_t linesize = 0;
3342 ssize_t linelen;
3344 ignorelist = calloc(1, sizeof(*ignorelist));
3345 if (ignorelist == NULL)
3346 return got_error_from_errno("calloc");
3347 TAILQ_INIT(ignorelist);
3349 while ((linelen = getline(&line, &linesize, f)) != -1) {
3350 if (linelen > 0 && line[linelen - 1] == '\n')
3351 line[linelen - 1] = '\0';
3353 /* Git's ignores may contain comments. */
3354 if (line[0] == '#')
3355 continue;
3357 /* Git's negated patterns are not (yet?) supported. */
3358 if (line[0] == '!')
3359 continue;
3361 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3362 line) == -1) {
3363 err = got_error_from_errno("asprintf");
3364 goto done;
3366 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3367 if (err)
3368 goto done;
3370 if (ferror(f)) {
3371 err = got_error_from_errno("getline");
3372 goto done;
3375 dirpath = strdup(path);
3376 if (dirpath == NULL) {
3377 err = got_error_from_errno("strdup");
3378 goto done;
3380 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3381 done:
3382 free(line);
3383 if (err || pe == NULL) {
3384 free(dirpath);
3385 free_ignorelist(ignorelist);
3387 return err;
3390 int
3391 match_ignores(struct got_pathlist_head *ignores, const char *path)
3393 struct got_pathlist_entry *pe;
3395 /* Handle patterns which match in all directories. */
3396 TAILQ_FOREACH(pe, ignores, entry) {
3397 struct got_pathlist_head *ignorelist = pe->data;
3398 struct got_pathlist_entry *pi;
3400 TAILQ_FOREACH(pi, ignorelist, entry) {
3401 const char *p, *pattern = pi->path;
3403 if (strncmp(pattern, "**/", 3) != 0)
3404 continue;
3405 pattern += 3;
3406 p = path;
3407 while (*p) {
3408 if (fnmatch(pattern, p,
3409 FNM_PATHNAME | FNM_LEADING_DIR)) {
3410 /* Retry in next directory. */
3411 while (*p && *p != '/')
3412 p++;
3413 while (*p == '/')
3414 p++;
3415 continue;
3417 return 1;
3423 * The ignores pathlist contains ignore lists from children before
3424 * parents, so we can find the most specific ignorelist by walking
3425 * ignores backwards.
3427 pe = TAILQ_LAST(ignores, got_pathlist_head);
3428 while (pe) {
3429 if (got_path_is_child(path, pe->path, pe->path_len)) {
3430 struct got_pathlist_head *ignorelist = pe->data;
3431 struct got_pathlist_entry *pi;
3432 TAILQ_FOREACH(pi, ignorelist, entry) {
3433 const char *pattern = pi->path;
3434 int flags = FNM_LEADING_DIR;
3435 if (strstr(pattern, "/**/") == NULL)
3436 flags |= FNM_PATHNAME;
3437 if (fnmatch(pattern, path, flags))
3438 continue;
3439 return 1;
3442 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3445 return 0;
3448 static const struct got_error *
3449 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3450 const char *path, int dirfd, const char *ignores_filename)
3452 const struct got_error *err = NULL;
3453 char *ignorespath;
3454 int fd = -1;
3455 FILE *ignoresfile = NULL;
3457 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3458 path[0] ? "/" : "", ignores_filename) == -1)
3459 return got_error_from_errno("asprintf");
3461 if (dirfd != -1) {
3462 fd = openat(dirfd, ignores_filename,
3463 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3464 if (fd == -1) {
3465 if (errno != ENOENT && errno != EACCES)
3466 err = got_error_from_errno2("openat",
3467 ignorespath);
3468 } else {
3469 ignoresfile = fdopen(fd, "r");
3470 if (ignoresfile == NULL)
3471 err = got_error_from_errno2("fdopen",
3472 ignorespath);
3473 else {
3474 fd = -1;
3475 err = read_ignores(ignores, path, ignoresfile);
3478 } else {
3479 ignoresfile = fopen(ignorespath, "re");
3480 if (ignoresfile == NULL) {
3481 if (errno != ENOENT && errno != EACCES)
3482 err = got_error_from_errno2("fopen",
3483 ignorespath);
3484 } else
3485 err = read_ignores(ignores, path, ignoresfile);
3488 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3489 err = got_error_from_errno2("fclose", path);
3490 if (fd != -1 && close(fd) == -1 && err == NULL)
3491 err = got_error_from_errno2("close", path);
3492 free(ignorespath);
3493 return err;
3496 static const struct got_error *
3497 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3498 int dirfd)
3500 const struct got_error *err = NULL;
3501 struct diff_dir_cb_arg *a = arg;
3502 char *path = NULL;
3504 if (ignore != NULL)
3505 *ignore = 0;
3507 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 return got_error(GOT_ERR_CANCELLED);
3510 if (parent_path[0]) {
3511 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3512 return got_error_from_errno("asprintf");
3513 } else {
3514 path = de->d_name;
3517 if (de->d_type == DT_DIR) {
3518 if (!a->no_ignores && ignore != NULL &&
3519 match_ignores(a->ignores, path))
3520 *ignore = 1;
3521 } else if (!match_ignores(a->ignores, path) &&
3522 got_path_is_child(path, a->status_path, a->status_path_len))
3523 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3524 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3525 if (parent_path[0])
3526 free(path);
3527 return err;
3530 static const struct got_error *
3531 status_traverse(void *arg, const char *path, int dirfd)
3533 const struct got_error *err = NULL;
3534 struct diff_dir_cb_arg *a = arg;
3536 if (a->no_ignores)
3537 return NULL;
3539 err = add_ignores(a->ignores, a->worktree->root_path,
3540 path, dirfd, ".cvsignore");
3541 if (err)
3542 return err;
3544 err = add_ignores(a->ignores, a->worktree->root_path, path,
3545 dirfd, ".gitignore");
3547 return err;
3550 static const struct got_error *
3551 report_single_file_status(const char *path, const char *ondisk_path,
3552 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3553 void *status_arg, struct got_repository *repo, int report_unchanged,
3554 struct got_pathlist_head *ignores, int no_ignores)
3556 struct got_fileindex_entry *ie;
3557 struct stat sb;
3559 if (!no_ignores && match_ignores(ignores, path))
3560 return NULL;
3562 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3563 if (ie)
3564 return report_file_status(ie, ondisk_path, -1, NULL,
3565 status_cb, status_arg, repo, report_unchanged);
3567 if (lstat(ondisk_path, &sb) == -1) {
3568 if (errno != ENOENT)
3569 return got_error_from_errno2("lstat", ondisk_path);
3570 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3571 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3572 return NULL;
3575 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3576 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3577 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3579 return NULL;
3582 static const struct got_error *
3583 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3584 const char *root_path, const char *path)
3586 const struct got_error *err;
3587 char *parent_path, *next_parent_path = NULL;
3589 err = add_ignores(ignores, root_path, "", -1,
3590 ".cvsignore");
3591 if (err)
3592 return err;
3594 err = add_ignores(ignores, root_path, "", -1,
3595 ".gitignore");
3596 if (err)
3597 return err;
3599 err = got_path_dirname(&parent_path, path);
3600 if (err) {
3601 if (err->code == GOT_ERR_BAD_PATH)
3602 return NULL; /* cannot traverse parent */
3603 return err;
3605 for (;;) {
3606 err = add_ignores(ignores, root_path, parent_path, -1,
3607 ".cvsignore");
3608 if (err)
3609 break;
3610 err = add_ignores(ignores, root_path, parent_path, -1,
3611 ".gitignore");
3612 if (err)
3613 break;
3614 err = got_path_dirname(&next_parent_path, parent_path);
3615 if (err) {
3616 if (err->code == GOT_ERR_BAD_PATH)
3617 err = NULL; /* traversed everything */
3618 break;
3620 if (got_path_is_root_dir(parent_path))
3621 break;
3622 free(parent_path);
3623 parent_path = next_parent_path;
3624 next_parent_path = NULL;
3627 free(parent_path);
3628 free(next_parent_path);
3629 return err;
3632 static const struct got_error *
3633 worktree_status(struct got_worktree *worktree, const char *path,
3634 struct got_fileindex *fileindex, struct got_repository *repo,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3637 int report_unchanged)
3639 const struct got_error *err = NULL;
3640 int fd = -1;
3641 struct got_fileindex_diff_dir_cb fdiff_cb;
3642 struct diff_dir_cb_arg arg;
3643 char *ondisk_path = NULL;
3644 struct got_pathlist_head ignores;
3646 TAILQ_INIT(&ignores);
3648 if (asprintf(&ondisk_path, "%s%s%s",
3649 worktree->root_path, path[0] ? "/" : "", path) == -1)
3650 return got_error_from_errno("asprintf");
3652 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3653 if (fd == -1) {
3654 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3655 !got_err_open_nofollow_on_symlink())
3656 err = got_error_from_errno2("open", ondisk_path);
3657 else {
3658 if (!no_ignores) {
3659 err = add_ignores_from_parent_paths(&ignores,
3660 worktree->root_path, ondisk_path);
3661 if (err)
3662 goto done;
3664 err = report_single_file_status(path, ondisk_path,
3665 fileindex, status_cb, status_arg, repo,
3666 report_unchanged, &ignores, no_ignores);
3668 } else {
3669 fdiff_cb.diff_old_new = status_old_new;
3670 fdiff_cb.diff_old = status_old;
3671 fdiff_cb.diff_new = status_new;
3672 fdiff_cb.diff_traverse = status_traverse;
3673 arg.fileindex = fileindex;
3674 arg.worktree = worktree;
3675 arg.status_path = path;
3676 arg.status_path_len = strlen(path);
3677 arg.repo = repo;
3678 arg.status_cb = status_cb;
3679 arg.status_arg = status_arg;
3680 arg.cancel_cb = cancel_cb;
3681 arg.cancel_arg = cancel_arg;
3682 arg.report_unchanged = report_unchanged;
3683 arg.no_ignores = no_ignores;
3684 if (!no_ignores) {
3685 err = add_ignores_from_parent_paths(&ignores,
3686 worktree->root_path, path);
3687 if (err)
3688 goto done;
3690 arg.ignores = &ignores;
3691 err = got_fileindex_diff_dir(fileindex, fd,
3692 worktree->root_path, path, repo, &fdiff_cb, &arg);
3694 done:
3695 free_ignores(&ignores);
3696 if (fd != -1 && close(fd) == -1 && err == NULL)
3697 err = got_error_from_errno("close");
3698 free(ondisk_path);
3699 return err;
3702 const struct got_error *
3703 got_worktree_status(struct got_worktree *worktree,
3704 struct got_pathlist_head *paths, struct got_repository *repo,
3705 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3706 got_cancel_cb cancel_cb, void *cancel_arg)
3708 const struct got_error *err = NULL;
3709 char *fileindex_path = NULL;
3710 struct got_fileindex *fileindex = NULL;
3711 struct got_pathlist_entry *pe;
3713 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3714 if (err)
3715 return err;
3717 TAILQ_FOREACH(pe, paths, entry) {
3718 err = worktree_status(worktree, pe->path, fileindex, repo,
3719 status_cb, status_arg, cancel_cb, cancel_arg,
3720 no_ignores, 0);
3721 if (err)
3722 break;
3724 free(fileindex_path);
3725 got_fileindex_free(fileindex);
3726 return err;
3729 const struct got_error *
3730 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3731 const char *arg)
3733 const struct got_error *err = NULL;
3734 char *resolved = NULL, *cwd = NULL, *path = NULL;
3735 size_t len;
3736 struct stat sb;
3737 char *abspath = NULL;
3738 char canonpath[PATH_MAX];
3740 *wt_path = NULL;
3742 cwd = getcwd(NULL, 0);
3743 if (cwd == NULL)
3744 return got_error_from_errno("getcwd");
3746 if (lstat(arg, &sb) == -1) {
3747 if (errno != ENOENT) {
3748 err = got_error_from_errno2("lstat", arg);
3749 goto done;
3751 sb.st_mode = 0;
3753 if (S_ISLNK(sb.st_mode)) {
3755 * We cannot use realpath(3) with symlinks since we want to
3756 * operate on the symlink itself.
3757 * But we can make the path absolute, assuming it is relative
3758 * to the current working directory, and then canonicalize it.
3760 if (!got_path_is_absolute(arg)) {
3761 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3762 err = got_error_from_errno("asprintf");
3763 goto done;
3767 err = got_canonpath(abspath ? abspath : arg, canonpath,
3768 sizeof(canonpath));
3769 if (err)
3770 goto done;
3771 resolved = strdup(canonpath);
3772 if (resolved == NULL) {
3773 err = got_error_from_errno("strdup");
3774 goto done;
3776 } else {
3777 resolved = realpath(arg, NULL);
3778 if (resolved == NULL) {
3779 if (errno != ENOENT) {
3780 err = got_error_from_errno2("realpath", arg);
3781 goto done;
3783 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3784 err = got_error_from_errno("asprintf");
3785 goto done;
3787 err = got_canonpath(abspath, canonpath,
3788 sizeof(canonpath));
3789 if (err)
3790 goto done;
3791 resolved = strdup(canonpath);
3792 if (resolved == NULL) {
3793 err = got_error_from_errno("strdup");
3794 goto done;
3799 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3800 strlen(got_worktree_get_root_path(worktree)))) {
3801 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3802 goto done;
3805 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3806 err = got_path_skip_common_ancestor(&path,
3807 got_worktree_get_root_path(worktree), resolved);
3808 if (err)
3809 goto done;
3810 } else {
3811 path = strdup("");
3812 if (path == NULL) {
3813 err = got_error_from_errno("strdup");
3814 goto done;
3818 /* XXX status walk can't deal with trailing slash! */
3819 len = strlen(path);
3820 while (len > 0 && path[len - 1] == '/') {
3821 path[len - 1] = '\0';
3822 len--;
3824 done:
3825 free(abspath);
3826 free(resolved);
3827 free(cwd);
3828 if (err == NULL)
3829 *wt_path = path;
3830 else
3831 free(path);
3832 return err;
3835 struct schedule_addition_args {
3836 struct got_worktree *worktree;
3837 struct got_fileindex *fileindex;
3838 got_worktree_checkout_cb progress_cb;
3839 void *progress_arg;
3840 struct got_repository *repo;
3843 static const struct got_error *
3844 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3845 const char *relpath, struct got_object_id *blob_id,
3846 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3847 int dirfd, const char *de_name)
3849 struct schedule_addition_args *a = arg;
3850 const struct got_error *err = NULL;
3851 struct got_fileindex_entry *ie;
3852 struct stat sb;
3853 char *ondisk_path;
3855 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3856 relpath) == -1)
3857 return got_error_from_errno("asprintf");
3859 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3860 if (ie) {
3861 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3862 de_name, a->repo);
3863 if (err)
3864 goto done;
3865 /* Re-adding an existing entry is a no-op. */
3866 if (status == GOT_STATUS_ADD)
3867 goto done;
3868 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3869 if (err)
3870 goto done;
3873 if (status != GOT_STATUS_UNVERSIONED) {
3874 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3875 goto done;
3878 err = got_fileindex_entry_alloc(&ie, relpath);
3879 if (err)
3880 goto done;
3881 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3882 relpath, NULL, NULL, 1);
3883 if (err) {
3884 got_fileindex_entry_free(ie);
3885 goto done;
3887 err = got_fileindex_entry_add(a->fileindex, ie);
3888 if (err) {
3889 got_fileindex_entry_free(ie);
3890 goto done;
3892 done:
3893 free(ondisk_path);
3894 if (err)
3895 return err;
3896 if (status == GOT_STATUS_ADD)
3897 return NULL;
3898 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3901 const struct got_error *
3902 got_worktree_schedule_add(struct got_worktree *worktree,
3903 struct got_pathlist_head *paths,
3904 got_worktree_checkout_cb progress_cb, void *progress_arg,
3905 struct got_repository *repo, int no_ignores)
3907 struct got_fileindex *fileindex = NULL;
3908 char *fileindex_path = NULL;
3909 const struct got_error *err = NULL, *sync_err, *unlockerr;
3910 struct got_pathlist_entry *pe;
3911 struct schedule_addition_args saa;
3913 err = lock_worktree(worktree, LOCK_EX);
3914 if (err)
3915 return err;
3917 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3918 if (err)
3919 goto done;
3921 saa.worktree = worktree;
3922 saa.fileindex = fileindex;
3923 saa.progress_cb = progress_cb;
3924 saa.progress_arg = progress_arg;
3925 saa.repo = repo;
3927 TAILQ_FOREACH(pe, paths, entry) {
3928 err = worktree_status(worktree, pe->path, fileindex, repo,
3929 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3930 if (err)
3931 break;
3933 sync_err = sync_fileindex(fileindex, fileindex_path);
3934 if (sync_err && err == NULL)
3935 err = sync_err;
3936 done:
3937 free(fileindex_path);
3938 if (fileindex)
3939 got_fileindex_free(fileindex);
3940 unlockerr = lock_worktree(worktree, LOCK_SH);
3941 if (unlockerr && err == NULL)
3942 err = unlockerr;
3943 return err;
3946 struct schedule_deletion_args {
3947 struct got_worktree *worktree;
3948 struct got_fileindex *fileindex;
3949 got_worktree_delete_cb progress_cb;
3950 void *progress_arg;
3951 struct got_repository *repo;
3952 int delete_local_mods;
3953 int keep_on_disk;
3954 const char *status_codes;
3957 static const struct got_error *
3958 schedule_for_deletion(void *arg, unsigned char status,
3959 unsigned char staged_status, const char *relpath,
3960 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3961 struct got_object_id *commit_id, int dirfd, const char *de_name)
3963 struct schedule_deletion_args *a = arg;
3964 const struct got_error *err = NULL;
3965 struct got_fileindex_entry *ie = NULL;
3966 struct stat sb;
3967 char *ondisk_path;
3969 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3970 if (ie == NULL)
3971 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3973 staged_status = get_staged_status(ie);
3974 if (staged_status != GOT_STATUS_NO_CHANGE) {
3975 if (staged_status == GOT_STATUS_DELETE)
3976 return NULL;
3977 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3980 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3981 relpath) == -1)
3982 return got_error_from_errno("asprintf");
3984 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3985 a->repo);
3986 if (err)
3987 goto done;
3989 if (a->status_codes) {
3990 size_t ncodes = strlen(a->status_codes);
3991 int i;
3992 for (i = 0; i < ncodes ; i++) {
3993 if (status == a->status_codes[i])
3994 break;
3996 if (i == ncodes) {
3997 /* Do not delete files in non-matching status. */
3998 free(ondisk_path);
3999 return NULL;
4001 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4002 a->status_codes[i] != GOT_STATUS_MISSING) {
4003 static char msg[64];
4004 snprintf(msg, sizeof(msg),
4005 "invalid status code '%c'", a->status_codes[i]);
4006 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4007 goto done;
4011 if (status != GOT_STATUS_NO_CHANGE) {
4012 if (status == GOT_STATUS_DELETE)
4013 goto done;
4014 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4015 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4016 goto done;
4018 if (status != GOT_STATUS_MODIFY &&
4019 status != GOT_STATUS_MISSING) {
4020 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4021 goto done;
4025 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4026 size_t root_len;
4028 if (dirfd != -1) {
4029 if (unlinkat(dirfd, de_name, 0) != 0) {
4030 err = got_error_from_errno2("unlinkat",
4031 ondisk_path);
4032 goto done;
4034 } else if (unlink(ondisk_path) != 0) {
4035 err = got_error_from_errno2("unlink", ondisk_path);
4036 goto done;
4039 root_len = strlen(a->worktree->root_path);
4040 do {
4041 char *parent;
4042 err = got_path_dirname(&parent, ondisk_path);
4043 if (err)
4044 goto done;
4045 free(ondisk_path);
4046 ondisk_path = parent;
4047 if (rmdir(ondisk_path) == -1) {
4048 if (errno != ENOTEMPTY)
4049 err = got_error_from_errno2("rmdir",
4050 ondisk_path);
4051 break;
4053 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4054 strlen(ondisk_path), root_len) != 0);
4057 got_fileindex_entry_mark_deleted_from_disk(ie);
4058 done:
4059 free(ondisk_path);
4060 if (err)
4061 return err;
4062 if (status == GOT_STATUS_DELETE)
4063 return NULL;
4064 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4065 staged_status, relpath);
4068 const struct got_error *
4069 got_worktree_schedule_delete(struct got_worktree *worktree,
4070 struct got_pathlist_head *paths, int delete_local_mods,
4071 const char *status_codes,
4072 got_worktree_delete_cb progress_cb, void *progress_arg,
4073 struct got_repository *repo, int keep_on_disk)
4075 struct got_fileindex *fileindex = NULL;
4076 char *fileindex_path = NULL;
4077 const struct got_error *err = NULL, *sync_err, *unlockerr;
4078 struct got_pathlist_entry *pe;
4079 struct schedule_deletion_args sda;
4081 err = lock_worktree(worktree, LOCK_EX);
4082 if (err)
4083 return err;
4085 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4086 if (err)
4087 goto done;
4089 sda.worktree = worktree;
4090 sda.fileindex = fileindex;
4091 sda.progress_cb = progress_cb;
4092 sda.progress_arg = progress_arg;
4093 sda.repo = repo;
4094 sda.delete_local_mods = delete_local_mods;
4095 sda.keep_on_disk = keep_on_disk;
4096 sda.status_codes = status_codes;
4098 TAILQ_FOREACH(pe, paths, entry) {
4099 err = worktree_status(worktree, pe->path, fileindex, repo,
4100 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4101 if (err)
4102 break;
4104 sync_err = sync_fileindex(fileindex, fileindex_path);
4105 if (sync_err && err == NULL)
4106 err = sync_err;
4107 done:
4108 free(fileindex_path);
4109 if (fileindex)
4110 got_fileindex_free(fileindex);
4111 unlockerr = lock_worktree(worktree, LOCK_SH);
4112 if (unlockerr && err == NULL)
4113 err = unlockerr;
4114 return err;
4117 static const struct got_error *
4118 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4120 const struct got_error *err = NULL;
4121 char *line = NULL;
4122 size_t linesize = 0, n;
4123 ssize_t linelen;
4125 linelen = getline(&line, &linesize, infile);
4126 if (linelen == -1) {
4127 if (ferror(infile)) {
4128 err = got_error_from_errno("getline");
4129 goto done;
4131 return NULL;
4133 if (outfile) {
4134 n = fwrite(line, 1, linelen, outfile);
4135 if (n != linelen) {
4136 err = got_ferror(outfile, GOT_ERR_IO);
4137 goto done;
4140 if (rejectfile) {
4141 n = fwrite(line, 1, linelen, rejectfile);
4142 if (n != linelen)
4143 err = got_ferror(outfile, GOT_ERR_IO);
4145 done:
4146 free(line);
4147 return err;
4150 static const struct got_error *
4151 skip_one_line(FILE *f)
4153 char *line = NULL;
4154 size_t linesize = 0;
4155 ssize_t linelen;
4157 linelen = getline(&line, &linesize, f);
4158 if (linelen == -1) {
4159 if (ferror(f))
4160 return got_error_from_errno("getline");
4161 return NULL;
4163 free(line);
4164 return NULL;
4167 static const struct got_error *
4168 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4169 int start_old, int end_old, int start_new, int end_new,
4170 FILE *outfile, FILE *rejectfile)
4172 const struct got_error *err;
4174 /* Copy old file's lines leading up to patch. */
4175 while (!feof(f1) && *line_cur1 < start_old) {
4176 err = copy_one_line(f1, outfile, NULL);
4177 if (err)
4178 return err;
4179 (*line_cur1)++;
4181 /* Skip new file's lines leading up to patch. */
4182 while (!feof(f2) && *line_cur2 < start_new) {
4183 if (rejectfile)
4184 err = copy_one_line(f2, NULL, rejectfile);
4185 else
4186 err = skip_one_line(f2);
4187 if (err)
4188 return err;
4189 (*line_cur2)++;
4191 /* Copy patched lines. */
4192 while (!feof(f2) && *line_cur2 <= end_new) {
4193 err = copy_one_line(f2, outfile, NULL);
4194 if (err)
4195 return err;
4196 (*line_cur2)++;
4198 /* Skip over old file's replaced lines. */
4199 while (!feof(f1) && *line_cur1 <= end_old) {
4200 if (rejectfile)
4201 err = copy_one_line(f1, NULL, rejectfile);
4202 else
4203 err = skip_one_line(f1);
4204 if (err)
4205 return err;
4206 (*line_cur1)++;
4209 return NULL;
4212 static const struct got_error *
4213 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4214 FILE *outfile, FILE *rejectfile)
4216 const struct got_error *err;
4218 if (outfile) {
4219 /* Copy old file's lines until EOF. */
4220 while (!feof(f1)) {
4221 err = copy_one_line(f1, outfile, NULL);
4222 if (err)
4223 return err;
4224 (*line_cur1)++;
4227 if (rejectfile) {
4228 /* Copy new file's lines until EOF. */
4229 while (!feof(f2)) {
4230 err = copy_one_line(f2, NULL, rejectfile);
4231 if (err)
4232 return err;
4233 (*line_cur2)++;
4237 return NULL;
4240 static const struct got_error *
4241 apply_or_reject_change(int *choice, int *nchunks_used,
4242 struct diff_result *diff_result, int n,
4243 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4244 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4245 got_worktree_patch_cb patch_cb, void *patch_arg)
4247 const struct got_error *err = NULL;
4248 struct diff_chunk_context cc = {};
4249 int start_old, end_old, start_new, end_new;
4250 FILE *hunkfile;
4251 struct diff_output_unidiff_state *diff_state;
4252 struct diff_input_info diff_info;
4253 int rc;
4255 *choice = GOT_PATCH_CHOICE_NONE;
4257 /* Get changed line numbers without context lines for copy_change(). */
4258 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4259 start_old = cc.left.start;
4260 end_old = cc.left.end;
4261 start_new = cc.right.start;
4262 end_new = cc.right.end;
4264 /* Get the same change with context lines for display. */
4265 memset(&cc, 0, sizeof(cc));
4266 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4268 memset(&diff_info, 0, sizeof(diff_info));
4269 diff_info.left_path = relpath;
4270 diff_info.right_path = relpath;
4272 diff_state = diff_output_unidiff_state_alloc();
4273 if (diff_state == NULL)
4274 return got_error_set_errno(ENOMEM,
4275 "diff_output_unidiff_state_alloc");
4277 hunkfile = got_opentemp();
4278 if (hunkfile == NULL) {
4279 err = got_error_from_errno("got_opentemp");
4280 goto done;
4283 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4284 diff_result, &cc);
4285 if (rc != DIFF_RC_OK) {
4286 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4287 goto done;
4290 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4291 err = got_ferror(hunkfile, GOT_ERR_IO);
4292 goto done;
4295 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4296 hunkfile, changeno, nchanges);
4297 if (err)
4298 goto done;
4300 switch (*choice) {
4301 case GOT_PATCH_CHOICE_YES:
4302 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4303 end_old, start_new, end_new, outfile, rejectfile);
4304 break;
4305 case GOT_PATCH_CHOICE_NO:
4306 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4307 end_old, start_new, end_new, rejectfile, outfile);
4308 break;
4309 case GOT_PATCH_CHOICE_QUIT:
4310 break;
4311 default:
4312 err = got_error(GOT_ERR_PATCH_CHOICE);
4313 break;
4315 done:
4316 diff_output_unidiff_state_free(diff_state);
4317 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4318 err = got_error_from_errno("fclose");
4319 return err;
4322 struct revert_file_args {
4323 struct got_worktree *worktree;
4324 struct got_fileindex *fileindex;
4325 got_worktree_checkout_cb progress_cb;
4326 void *progress_arg;
4327 got_worktree_patch_cb patch_cb;
4328 void *patch_arg;
4329 struct got_repository *repo;
4330 int unlink_added_files;
4333 static const struct got_error *
4334 create_patched_content(char **path_outfile, int reverse_patch,
4335 struct got_object_id *blob_id, const char *path2,
4336 int dirfd2, const char *de_name2,
4337 const char *relpath, struct got_repository *repo,
4338 got_worktree_patch_cb patch_cb, void *patch_arg)
4340 const struct got_error *err, *free_err;
4341 struct got_blob_object *blob = NULL;
4342 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4343 int fd2 = -1;
4344 char link_target[PATH_MAX];
4345 ssize_t link_len = 0;
4346 char *path1 = NULL, *id_str = NULL;
4347 struct stat sb2;
4348 struct got_diffreg_result *diffreg_result = NULL;
4349 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4350 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4352 *path_outfile = NULL;
4354 err = got_object_id_str(&id_str, blob_id);
4355 if (err)
4356 return err;
4358 if (dirfd2 != -1) {
4359 fd2 = openat(dirfd2, de_name2,
4360 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4361 if (fd2 == -1) {
4362 if (!got_err_open_nofollow_on_symlink()) {
4363 err = got_error_from_errno2("openat", path2);
4364 goto done;
4366 link_len = readlinkat(dirfd2, de_name2,
4367 link_target, sizeof(link_target));
4368 if (link_len == -1) {
4369 return got_error_from_errno2("readlinkat",
4370 path2);
4372 sb2.st_mode = S_IFLNK;
4373 sb2.st_size = link_len;
4375 } else {
4376 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4377 if (fd2 == -1) {
4378 if (!got_err_open_nofollow_on_symlink()) {
4379 err = got_error_from_errno2("open", path2);
4380 goto done;
4382 link_len = readlink(path2, link_target,
4383 sizeof(link_target));
4384 if (link_len == -1)
4385 return got_error_from_errno2("readlink", path2);
4386 sb2.st_mode = S_IFLNK;
4387 sb2.st_size = link_len;
4390 if (fd2 != -1) {
4391 if (fstat(fd2, &sb2) == -1) {
4392 err = got_error_from_errno2("fstat", path2);
4393 goto done;
4396 f2 = fdopen(fd2, "r");
4397 if (f2 == NULL) {
4398 err = got_error_from_errno2("fdopen", path2);
4399 goto done;
4401 fd2 = -1;
4402 } else {
4403 size_t n;
4404 f2 = got_opentemp();
4405 if (f2 == NULL) {
4406 err = got_error_from_errno2("got_opentemp", path2);
4407 goto done;
4409 n = fwrite(link_target, 1, link_len, f2);
4410 if (n != link_len) {
4411 err = got_ferror(f2, GOT_ERR_IO);
4412 goto done;
4414 if (fflush(f2) == EOF) {
4415 err = got_error_from_errno("fflush");
4416 goto done;
4418 rewind(f2);
4421 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4422 if (err)
4423 goto done;
4425 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4426 if (err)
4427 goto done;
4429 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4430 if (err)
4431 goto done;
4433 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4434 NULL);
4435 if (err)
4436 goto done;
4438 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4439 if (err)
4440 goto done;
4442 if (fseek(f1, 0L, SEEK_SET) == -1)
4443 return got_ferror(f1, GOT_ERR_IO);
4444 if (fseek(f2, 0L, SEEK_SET) == -1)
4445 return got_ferror(f2, GOT_ERR_IO);
4447 /* Count the number of actual changes in the diff result. */
4448 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4449 struct diff_chunk_context cc = {};
4450 diff_chunk_context_load_change(&cc, &nchunks_used,
4451 diffreg_result->result, n, 0);
4452 nchanges++;
4454 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4455 int choice;
4456 err = apply_or_reject_change(&choice, &nchunks_used,
4457 diffreg_result->result, n, relpath, f1, f2,
4458 &line_cur1, &line_cur2,
4459 reverse_patch ? NULL : outfile,
4460 reverse_patch ? outfile : NULL,
4461 ++i, nchanges, patch_cb, patch_arg);
4462 if (err)
4463 goto done;
4464 if (choice == GOT_PATCH_CHOICE_YES)
4465 have_content = 1;
4466 else if (choice == GOT_PATCH_CHOICE_QUIT)
4467 break;
4469 if (have_content) {
4470 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4471 reverse_patch ? NULL : outfile,
4472 reverse_patch ? outfile : NULL);
4473 if (err)
4474 goto done;
4476 if (!S_ISLNK(sb2.st_mode)) {
4477 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4478 err = got_error_from_errno2("fchmod", path2);
4479 goto done;
4483 done:
4484 free(id_str);
4485 if (blob)
4486 got_object_blob_close(blob);
4487 free_err = got_diffreg_result_free(diffreg_result);
4488 if (err == NULL)
4489 err = free_err;
4490 if (f1 && fclose(f1) == EOF && err == NULL)
4491 err = got_error_from_errno2("fclose", path1);
4492 if (f2 && fclose(f2) == EOF && err == NULL)
4493 err = got_error_from_errno2("fclose", path2);
4494 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4495 err = got_error_from_errno2("close", path2);
4496 if (outfile && fclose(outfile) == EOF && err == NULL)
4497 err = got_error_from_errno2("fclose", *path_outfile);
4498 if (path1 && unlink(path1) == -1 && err == NULL)
4499 err = got_error_from_errno2("unlink", path1);
4500 if (err || !have_content) {
4501 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4502 err = got_error_from_errno2("unlink", *path_outfile);
4503 free(*path_outfile);
4504 *path_outfile = NULL;
4506 free(path1);
4507 return err;
4510 static const struct got_error *
4511 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4512 const char *relpath, struct got_object_id *blob_id,
4513 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4514 int dirfd, const char *de_name)
4516 struct revert_file_args *a = arg;
4517 const struct got_error *err = NULL;
4518 char *parent_path = NULL;
4519 struct got_fileindex_entry *ie;
4520 struct got_tree_object *tree = NULL;
4521 struct got_object_id *tree_id = NULL;
4522 const struct got_tree_entry *te = NULL;
4523 char *tree_path = NULL, *te_name;
4524 char *ondisk_path = NULL, *path_content = NULL;
4525 struct got_blob_object *blob = NULL;
4527 /* Reverting a staged deletion is a no-op. */
4528 if (status == GOT_STATUS_DELETE &&
4529 staged_status != GOT_STATUS_NO_CHANGE)
4530 return NULL;
4532 if (status == GOT_STATUS_UNVERSIONED)
4533 return (*a->progress_cb)(a->progress_arg,
4534 GOT_STATUS_UNVERSIONED, relpath);
4536 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4537 if (ie == NULL)
4538 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4540 /* Construct in-repository path of tree which contains this blob. */
4541 err = got_path_dirname(&parent_path, ie->path);
4542 if (err) {
4543 if (err->code != GOT_ERR_BAD_PATH)
4544 goto done;
4545 parent_path = strdup("/");
4546 if (parent_path == NULL) {
4547 err = got_error_from_errno("strdup");
4548 goto done;
4551 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4552 tree_path = strdup(parent_path);
4553 if (tree_path == NULL) {
4554 err = got_error_from_errno("strdup");
4555 goto done;
4557 } else {
4558 if (got_path_is_root_dir(parent_path)) {
4559 tree_path = strdup(a->worktree->path_prefix);
4560 if (tree_path == NULL) {
4561 err = got_error_from_errno("strdup");
4562 goto done;
4564 } else {
4565 if (asprintf(&tree_path, "%s/%s",
4566 a->worktree->path_prefix, parent_path) == -1) {
4567 err = got_error_from_errno("asprintf");
4568 goto done;
4573 err = got_object_id_by_path(&tree_id, a->repo,
4574 a->worktree->base_commit_id, tree_path);
4575 if (err) {
4576 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4577 (status == GOT_STATUS_ADD ||
4578 staged_status == GOT_STATUS_ADD)))
4579 goto done;
4580 } else {
4581 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4582 if (err)
4583 goto done;
4585 err = got_path_basename(&te_name, ie->path);
4586 if (err)
4587 goto done;
4589 te = got_object_tree_find_entry(tree, te_name);
4590 free(te_name);
4591 if (te == NULL && status != GOT_STATUS_ADD &&
4592 staged_status != GOT_STATUS_ADD) {
4593 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4594 goto done;
4598 switch (status) {
4599 case GOT_STATUS_ADD:
4600 if (a->patch_cb) {
4601 int choice = GOT_PATCH_CHOICE_NONE;
4602 err = (*a->patch_cb)(&choice, a->patch_arg,
4603 status, ie->path, NULL, 1, 1);
4604 if (err)
4605 goto done;
4606 if (choice != GOT_PATCH_CHOICE_YES)
4607 break;
4609 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4610 ie->path);
4611 if (err)
4612 goto done;
4613 got_fileindex_entry_remove(a->fileindex, ie);
4614 if (a->unlink_added_files) {
4615 if (asprintf(&ondisk_path, "%s/%s",
4616 got_worktree_get_root_path(a->worktree),
4617 relpath) == -1) {
4618 err = got_error_from_errno("asprintf");
4619 goto done;
4621 if (unlink(ondisk_path) == -1) {
4622 err = got_error_from_errno2("unlink",
4623 ondisk_path);
4624 break;
4627 break;
4628 case GOT_STATUS_DELETE:
4629 if (a->patch_cb) {
4630 int choice = GOT_PATCH_CHOICE_NONE;
4631 err = (*a->patch_cb)(&choice, a->patch_arg,
4632 status, ie->path, NULL, 1, 1);
4633 if (err)
4634 goto done;
4635 if (choice != GOT_PATCH_CHOICE_YES)
4636 break;
4638 /* fall through */
4639 case GOT_STATUS_MODIFY:
4640 case GOT_STATUS_MODE_CHANGE:
4641 case GOT_STATUS_CONFLICT:
4642 case GOT_STATUS_MISSING: {
4643 struct got_object_id id;
4644 if (staged_status == GOT_STATUS_ADD ||
4645 staged_status == GOT_STATUS_MODIFY) {
4646 memcpy(id.sha1, ie->staged_blob_sha1,
4647 SHA1_DIGEST_LENGTH);
4648 } else
4649 memcpy(id.sha1, ie->blob_sha1,
4650 SHA1_DIGEST_LENGTH);
4651 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4652 if (err)
4653 goto done;
4655 if (asprintf(&ondisk_path, "%s/%s",
4656 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4657 err = got_error_from_errno("asprintf");
4658 goto done;
4661 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4662 status == GOT_STATUS_CONFLICT)) {
4663 int is_bad_symlink = 0;
4664 err = create_patched_content(&path_content, 1, &id,
4665 ondisk_path, dirfd, de_name, ie->path, a->repo,
4666 a->patch_cb, a->patch_arg);
4667 if (err || path_content == NULL)
4668 break;
4669 if (te && S_ISLNK(te->mode)) {
4670 if (unlink(path_content) == -1) {
4671 err = got_error_from_errno2("unlink",
4672 path_content);
4673 break;
4675 err = install_symlink(&is_bad_symlink,
4676 a->worktree, ondisk_path, ie->path,
4677 blob, 0, 1, 0, 0, a->repo,
4678 a->progress_cb, a->progress_arg);
4679 } else {
4680 if (rename(path_content, ondisk_path) == -1) {
4681 err = got_error_from_errno3("rename",
4682 path_content, ondisk_path);
4683 goto done;
4686 } else {
4687 int is_bad_symlink = 0;
4688 if (te && S_ISLNK(te->mode)) {
4689 err = install_symlink(&is_bad_symlink,
4690 a->worktree, ondisk_path, ie->path,
4691 blob, 0, 1, 0, 0, a->repo,
4692 a->progress_cb, a->progress_arg);
4693 } else {
4694 err = install_blob(a->worktree, ondisk_path,
4695 ie->path,
4696 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4697 got_fileindex_perms_to_st(ie), blob,
4698 0, 1, 0, 0, a->repo,
4699 a->progress_cb, a->progress_arg);
4701 if (err)
4702 goto done;
4703 if (status == GOT_STATUS_DELETE ||
4704 status == GOT_STATUS_MODE_CHANGE) {
4705 err = got_fileindex_entry_update(ie,
4706 a->worktree->root_fd, relpath,
4707 blob->id.sha1,
4708 a->worktree->base_commit_id->sha1, 1);
4709 if (err)
4710 goto done;
4712 if (is_bad_symlink) {
4713 got_fileindex_entry_filetype_set(ie,
4714 GOT_FILEIDX_MODE_BAD_SYMLINK);
4717 break;
4719 default:
4720 break;
4722 done:
4723 free(ondisk_path);
4724 free(path_content);
4725 free(parent_path);
4726 free(tree_path);
4727 if (blob)
4728 got_object_blob_close(blob);
4729 if (tree)
4730 got_object_tree_close(tree);
4731 free(tree_id);
4732 return err;
4735 const struct got_error *
4736 got_worktree_revert(struct got_worktree *worktree,
4737 struct got_pathlist_head *paths,
4738 got_worktree_checkout_cb progress_cb, void *progress_arg,
4739 got_worktree_patch_cb patch_cb, void *patch_arg,
4740 struct got_repository *repo)
4742 struct got_fileindex *fileindex = NULL;
4743 char *fileindex_path = NULL;
4744 const struct got_error *err = NULL, *unlockerr = NULL;
4745 const struct got_error *sync_err = NULL;
4746 struct got_pathlist_entry *pe;
4747 struct revert_file_args rfa;
4749 err = lock_worktree(worktree, LOCK_EX);
4750 if (err)
4751 return err;
4753 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4754 if (err)
4755 goto done;
4757 rfa.worktree = worktree;
4758 rfa.fileindex = fileindex;
4759 rfa.progress_cb = progress_cb;
4760 rfa.progress_arg = progress_arg;
4761 rfa.patch_cb = patch_cb;
4762 rfa.patch_arg = patch_arg;
4763 rfa.repo = repo;
4764 rfa.unlink_added_files = 0;
4765 TAILQ_FOREACH(pe, paths, entry) {
4766 err = worktree_status(worktree, pe->path, fileindex, repo,
4767 revert_file, &rfa, NULL, NULL, 1, 0);
4768 if (err)
4769 break;
4771 sync_err = sync_fileindex(fileindex, fileindex_path);
4772 if (sync_err && err == NULL)
4773 err = sync_err;
4774 done:
4775 free(fileindex_path);
4776 if (fileindex)
4777 got_fileindex_free(fileindex);
4778 unlockerr = lock_worktree(worktree, LOCK_SH);
4779 if (unlockerr && err == NULL)
4780 err = unlockerr;
4781 return err;
4784 static void
4785 free_commitable(struct got_commitable *ct)
4787 free(ct->path);
4788 free(ct->in_repo_path);
4789 free(ct->ondisk_path);
4790 free(ct->blob_id);
4791 free(ct->base_blob_id);
4792 free(ct->staged_blob_id);
4793 free(ct->base_commit_id);
4794 free(ct);
4797 struct collect_commitables_arg {
4798 struct got_pathlist_head *commitable_paths;
4799 struct got_repository *repo;
4800 struct got_worktree *worktree;
4801 struct got_fileindex *fileindex;
4802 int have_staged_files;
4803 int allow_bad_symlinks;
4806 static const struct got_error *
4807 collect_commitables(void *arg, unsigned char status,
4808 unsigned char staged_status, const char *relpath,
4809 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4810 struct got_object_id *commit_id, int dirfd, const char *de_name)
4812 struct collect_commitables_arg *a = arg;
4813 const struct got_error *err = NULL;
4814 struct got_commitable *ct = NULL;
4815 struct got_pathlist_entry *new = NULL;
4816 char *parent_path = NULL, *path = NULL;
4817 struct stat sb;
4819 if (a->have_staged_files) {
4820 if (staged_status != GOT_STATUS_MODIFY &&
4821 staged_status != GOT_STATUS_ADD &&
4822 staged_status != GOT_STATUS_DELETE)
4823 return NULL;
4824 } else {
4825 if (status == GOT_STATUS_CONFLICT)
4826 return got_error(GOT_ERR_COMMIT_CONFLICT);
4828 if (status != GOT_STATUS_MODIFY &&
4829 status != GOT_STATUS_MODE_CHANGE &&
4830 status != GOT_STATUS_ADD &&
4831 status != GOT_STATUS_DELETE)
4832 return NULL;
4835 if (asprintf(&path, "/%s", relpath) == -1) {
4836 err = got_error_from_errno("asprintf");
4837 goto done;
4839 if (strcmp(path, "/") == 0) {
4840 parent_path = strdup("");
4841 if (parent_path == NULL)
4842 return got_error_from_errno("strdup");
4843 } else {
4844 err = got_path_dirname(&parent_path, path);
4845 if (err)
4846 return err;
4849 ct = calloc(1, sizeof(*ct));
4850 if (ct == NULL) {
4851 err = got_error_from_errno("calloc");
4852 goto done;
4855 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4856 relpath) == -1) {
4857 err = got_error_from_errno("asprintf");
4858 goto done;
4861 if (staged_status == GOT_STATUS_ADD ||
4862 staged_status == GOT_STATUS_MODIFY) {
4863 struct got_fileindex_entry *ie;
4864 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4865 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4866 case GOT_FILEIDX_MODE_REGULAR_FILE:
4867 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4868 ct->mode = S_IFREG;
4869 break;
4870 case GOT_FILEIDX_MODE_SYMLINK:
4871 ct->mode = S_IFLNK;
4872 break;
4873 default:
4874 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4875 goto done;
4877 ct->mode |= got_fileindex_entry_perms_get(ie);
4878 } else if (status != GOT_STATUS_DELETE &&
4879 staged_status != GOT_STATUS_DELETE) {
4880 if (dirfd != -1) {
4881 if (fstatat(dirfd, de_name, &sb,
4882 AT_SYMLINK_NOFOLLOW) == -1) {
4883 err = got_error_from_errno2("fstatat",
4884 ct->ondisk_path);
4885 goto done;
4887 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4888 err = got_error_from_errno2("lstat", ct->ondisk_path);
4889 goto done;
4891 ct->mode = sb.st_mode;
4894 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4895 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4896 relpath) == -1) {
4897 err = got_error_from_errno("asprintf");
4898 goto done;
4901 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4902 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4903 int is_bad_symlink;
4904 char target_path[PATH_MAX];
4905 ssize_t target_len;
4906 target_len = readlink(ct->ondisk_path, target_path,
4907 sizeof(target_path));
4908 if (target_len == -1) {
4909 err = got_error_from_errno2("readlink",
4910 ct->ondisk_path);
4911 goto done;
4913 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4914 target_len, ct->ondisk_path, a->worktree->root_path);
4915 if (err)
4916 goto done;
4917 if (is_bad_symlink) {
4918 err = got_error_path(ct->ondisk_path,
4919 GOT_ERR_BAD_SYMLINK);
4920 goto done;
4925 ct->status = status;
4926 ct->staged_status = staged_status;
4927 ct->blob_id = NULL; /* will be filled in when blob gets created */
4928 if (ct->status != GOT_STATUS_ADD &&
4929 ct->staged_status != GOT_STATUS_ADD) {
4930 ct->base_blob_id = got_object_id_dup(blob_id);
4931 if (ct->base_blob_id == NULL) {
4932 err = got_error_from_errno("got_object_id_dup");
4933 goto done;
4935 ct->base_commit_id = got_object_id_dup(commit_id);
4936 if (ct->base_commit_id == NULL) {
4937 err = got_error_from_errno("got_object_id_dup");
4938 goto done;
4941 if (ct->staged_status == GOT_STATUS_ADD ||
4942 ct->staged_status == GOT_STATUS_MODIFY) {
4943 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4944 if (ct->staged_blob_id == NULL) {
4945 err = got_error_from_errno("got_object_id_dup");
4946 goto done;
4949 ct->path = strdup(path);
4950 if (ct->path == NULL) {
4951 err = got_error_from_errno("strdup");
4952 goto done;
4954 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4955 done:
4956 if (ct && (err || new == NULL))
4957 free_commitable(ct);
4958 free(parent_path);
4959 free(path);
4960 return err;
4963 static const struct got_error *write_tree(struct got_object_id **, int *,
4964 struct got_tree_object *, const char *, struct got_pathlist_head *,
4965 got_worktree_status_cb status_cb, void *status_arg,
4966 struct got_repository *);
4968 static const struct got_error *
4969 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4970 struct got_tree_entry *te, const char *parent_path,
4971 struct got_pathlist_head *commitable_paths,
4972 got_worktree_status_cb status_cb, void *status_arg,
4973 struct got_repository *repo)
4975 const struct got_error *err = NULL;
4976 struct got_tree_object *subtree;
4977 char *subpath;
4979 if (asprintf(&subpath, "%s%s%s", parent_path,
4980 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4981 return got_error_from_errno("asprintf");
4983 err = got_object_open_as_tree(&subtree, repo, &te->id);
4984 if (err)
4985 return err;
4987 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4988 commitable_paths, status_cb, status_arg, repo);
4989 got_object_tree_close(subtree);
4990 free(subpath);
4991 return err;
4994 static const struct got_error *
4995 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4997 const struct got_error *err = NULL;
4998 char *ct_parent_path = NULL;
5000 *match = 0;
5002 if (strchr(ct->in_repo_path, '/') == NULL) {
5003 *match = got_path_is_root_dir(path);
5004 return NULL;
5007 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5008 if (err)
5009 return err;
5010 *match = (strcmp(path, ct_parent_path) == 0);
5011 free(ct_parent_path);
5012 return err;
5015 static mode_t
5016 get_ct_file_mode(struct got_commitable *ct)
5018 if (S_ISLNK(ct->mode))
5019 return S_IFLNK;
5021 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5024 static const struct got_error *
5025 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5026 struct got_tree_entry *te, struct got_commitable *ct)
5028 const struct got_error *err = NULL;
5030 *new_te = NULL;
5032 err = got_object_tree_entry_dup(new_te, te);
5033 if (err)
5034 goto done;
5036 (*new_te)->mode = get_ct_file_mode(ct);
5038 if (ct->staged_status == GOT_STATUS_MODIFY)
5039 memcpy(&(*new_te)->id, ct->staged_blob_id,
5040 sizeof((*new_te)->id));
5041 else
5042 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5043 done:
5044 if (err && *new_te) {
5045 free(*new_te);
5046 *new_te = NULL;
5048 return err;
5051 static const struct got_error *
5052 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5053 struct got_commitable *ct)
5055 const struct got_error *err = NULL;
5056 char *ct_name = NULL;
5058 *new_te = NULL;
5060 *new_te = calloc(1, sizeof(**new_te));
5061 if (*new_te == NULL)
5062 return got_error_from_errno("calloc");
5064 err = got_path_basename(&ct_name, ct->path);
5065 if (err)
5066 goto done;
5067 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5068 sizeof((*new_te)->name)) {
5069 err = got_error(GOT_ERR_NO_SPACE);
5070 goto done;
5073 (*new_te)->mode = get_ct_file_mode(ct);
5075 if (ct->staged_status == GOT_STATUS_ADD)
5076 memcpy(&(*new_te)->id, ct->staged_blob_id,
5077 sizeof((*new_te)->id));
5078 else
5079 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5080 done:
5081 free(ct_name);
5082 if (err && *new_te) {
5083 free(*new_te);
5084 *new_te = NULL;
5086 return err;
5089 static const struct got_error *
5090 insert_tree_entry(struct got_tree_entry *new_te,
5091 struct got_pathlist_head *paths)
5093 const struct got_error *err = NULL;
5094 struct got_pathlist_entry *new_pe;
5096 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5097 if (err)
5098 return err;
5099 if (new_pe == NULL)
5100 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5101 return NULL;
5104 static const struct got_error *
5105 report_ct_status(struct got_commitable *ct,
5106 got_worktree_status_cb status_cb, void *status_arg)
5108 const char *ct_path = ct->path;
5109 unsigned char status;
5111 if (status_cb == NULL) /* no commit progress output desired */
5112 return NULL;
5114 while (ct_path[0] == '/')
5115 ct_path++;
5117 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5118 status = ct->staged_status;
5119 else
5120 status = ct->status;
5122 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5123 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5126 static const struct got_error *
5127 match_modified_subtree(int *modified, struct got_tree_entry *te,
5128 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5130 const struct got_error *err = NULL;
5131 struct got_pathlist_entry *pe;
5132 char *te_path;
5134 *modified = 0;
5136 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5137 got_path_is_root_dir(base_tree_path) ? "" : "/",
5138 te->name) == -1)
5139 return got_error_from_errno("asprintf");
5141 TAILQ_FOREACH(pe, commitable_paths, entry) {
5142 struct got_commitable *ct = pe->data;
5143 *modified = got_path_is_child(ct->in_repo_path, te_path,
5144 strlen(te_path));
5145 if (*modified)
5146 break;
5149 free(te_path);
5150 return err;
5153 static const struct got_error *
5154 match_deleted_or_modified_ct(struct got_commitable **ctp,
5155 struct got_tree_entry *te, const char *base_tree_path,
5156 struct got_pathlist_head *commitable_paths)
5158 const struct got_error *err = NULL;
5159 struct got_pathlist_entry *pe;
5161 *ctp = NULL;
5163 TAILQ_FOREACH(pe, commitable_paths, entry) {
5164 struct got_commitable *ct = pe->data;
5165 char *ct_name = NULL;
5166 int path_matches;
5168 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5169 if (ct->status != GOT_STATUS_MODIFY &&
5170 ct->status != GOT_STATUS_MODE_CHANGE &&
5171 ct->status != GOT_STATUS_DELETE)
5172 continue;
5173 } else {
5174 if (ct->staged_status != GOT_STATUS_MODIFY &&
5175 ct->staged_status != GOT_STATUS_DELETE)
5176 continue;
5179 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5180 continue;
5182 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5183 if (err)
5184 return err;
5185 if (!path_matches)
5186 continue;
5188 err = got_path_basename(&ct_name, pe->path);
5189 if (err)
5190 return err;
5192 if (strcmp(te->name, ct_name) != 0) {
5193 free(ct_name);
5194 continue;
5196 free(ct_name);
5198 *ctp = ct;
5199 break;
5202 return err;
5205 static const struct got_error *
5206 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5207 const char *child_path, const char *path_base_tree,
5208 struct got_pathlist_head *commitable_paths,
5209 got_worktree_status_cb status_cb, void *status_arg,
5210 struct got_repository *repo)
5212 const struct got_error *err = NULL;
5213 struct got_tree_entry *new_te;
5214 char *subtree_path;
5215 struct got_object_id *id = NULL;
5216 int nentries;
5218 *new_tep = NULL;
5220 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5221 got_path_is_root_dir(path_base_tree) ? "" : "/",
5222 child_path) == -1)
5223 return got_error_from_errno("asprintf");
5225 new_te = calloc(1, sizeof(*new_te));
5226 if (new_te == NULL)
5227 return got_error_from_errno("calloc");
5228 new_te->mode = S_IFDIR;
5230 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5231 sizeof(new_te->name)) {
5232 err = got_error(GOT_ERR_NO_SPACE);
5233 goto done;
5235 err = write_tree(&id, &nentries, NULL, subtree_path,
5236 commitable_paths, status_cb, status_arg, repo);
5237 if (err) {
5238 free(new_te);
5239 goto done;
5241 memcpy(&new_te->id, id, sizeof(new_te->id));
5242 done:
5243 free(id);
5244 free(subtree_path);
5245 if (err == NULL)
5246 *new_tep = new_te;
5247 return err;
5250 static const struct got_error *
5251 write_tree(struct got_object_id **new_tree_id, int *nentries,
5252 struct got_tree_object *base_tree, const char *path_base_tree,
5253 struct got_pathlist_head *commitable_paths,
5254 got_worktree_status_cb status_cb, void *status_arg,
5255 struct got_repository *repo)
5257 const struct got_error *err = NULL;
5258 struct got_pathlist_head paths;
5259 struct got_tree_entry *te, *new_te = NULL;
5260 struct got_pathlist_entry *pe;
5262 TAILQ_INIT(&paths);
5263 *nentries = 0;
5265 /* Insert, and recurse into, newly added entries first. */
5266 TAILQ_FOREACH(pe, commitable_paths, entry) {
5267 struct got_commitable *ct = pe->data;
5268 char *child_path = NULL, *slash;
5270 if ((ct->status != GOT_STATUS_ADD &&
5271 ct->staged_status != GOT_STATUS_ADD) ||
5272 (ct->flags & GOT_COMMITABLE_ADDED))
5273 continue;
5275 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5276 strlen(path_base_tree)))
5277 continue;
5279 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5280 ct->in_repo_path);
5281 if (err)
5282 goto done;
5284 slash = strchr(child_path, '/');
5285 if (slash == NULL) {
5286 err = alloc_added_blob_tree_entry(&new_te, ct);
5287 if (err)
5288 goto done;
5289 err = report_ct_status(ct, status_cb, status_arg);
5290 if (err)
5291 goto done;
5292 ct->flags |= GOT_COMMITABLE_ADDED;
5293 err = insert_tree_entry(new_te, &paths);
5294 if (err)
5295 goto done;
5296 (*nentries)++;
5297 } else {
5298 *slash = '\0'; /* trim trailing path components */
5299 if (base_tree == NULL ||
5300 got_object_tree_find_entry(base_tree, child_path)
5301 == NULL) {
5302 err = make_subtree_for_added_blob(&new_te,
5303 child_path, path_base_tree,
5304 commitable_paths, status_cb, status_arg,
5305 repo);
5306 if (err)
5307 goto done;
5308 err = insert_tree_entry(new_te, &paths);
5309 if (err)
5310 goto done;
5311 (*nentries)++;
5316 if (base_tree) {
5317 int i, nbase_entries;
5318 /* Handle modified and deleted entries. */
5319 nbase_entries = got_object_tree_get_nentries(base_tree);
5320 for (i = 0; i < nbase_entries; i++) {
5321 struct got_commitable *ct = NULL;
5323 te = got_object_tree_get_entry(base_tree, i);
5324 if (got_object_tree_entry_is_submodule(te)) {
5325 /* Entry is a submodule; just copy it. */
5326 err = got_object_tree_entry_dup(&new_te, te);
5327 if (err)
5328 goto done;
5329 err = insert_tree_entry(new_te, &paths);
5330 if (err)
5331 goto done;
5332 (*nentries)++;
5333 continue;
5336 if (S_ISDIR(te->mode)) {
5337 int modified;
5338 err = got_object_tree_entry_dup(&new_te, te);
5339 if (err)
5340 goto done;
5341 err = match_modified_subtree(&modified, te,
5342 path_base_tree, commitable_paths);
5343 if (err)
5344 goto done;
5345 /* Avoid recursion into unmodified subtrees. */
5346 if (modified) {
5347 struct got_object_id *new_id;
5348 int nsubentries;
5349 err = write_subtree(&new_id,
5350 &nsubentries, te,
5351 path_base_tree, commitable_paths,
5352 status_cb, status_arg, repo);
5353 if (err)
5354 goto done;
5355 if (nsubentries == 0) {
5356 /* All entries were deleted. */
5357 free(new_id);
5358 continue;
5360 memcpy(&new_te->id, new_id,
5361 sizeof(new_te->id));
5362 free(new_id);
5364 err = insert_tree_entry(new_te, &paths);
5365 if (err)
5366 goto done;
5367 (*nentries)++;
5368 continue;
5371 err = match_deleted_or_modified_ct(&ct, te,
5372 path_base_tree, commitable_paths);
5373 if (err)
5374 goto done;
5375 if (ct) {
5376 /* NB: Deleted entries get dropped here. */
5377 if (ct->status == GOT_STATUS_MODIFY ||
5378 ct->status == GOT_STATUS_MODE_CHANGE ||
5379 ct->staged_status == GOT_STATUS_MODIFY) {
5380 err = alloc_modified_blob_tree_entry(
5381 &new_te, te, ct);
5382 if (err)
5383 goto done;
5384 err = insert_tree_entry(new_te, &paths);
5385 if (err)
5386 goto done;
5387 (*nentries)++;
5389 err = report_ct_status(ct, status_cb,
5390 status_arg);
5391 if (err)
5392 goto done;
5393 } else {
5394 /* Entry is unchanged; just copy it. */
5395 err = got_object_tree_entry_dup(&new_te, te);
5396 if (err)
5397 goto done;
5398 err = insert_tree_entry(new_te, &paths);
5399 if (err)
5400 goto done;
5401 (*nentries)++;
5406 /* Write new list of entries; deleted entries have been dropped. */
5407 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5408 done:
5409 got_pathlist_free(&paths);
5410 return err;
5413 static const struct got_error *
5414 update_fileindex_after_commit(struct got_worktree *worktree,
5415 struct got_pathlist_head *commitable_paths,
5416 struct got_object_id *new_base_commit_id,
5417 struct got_fileindex *fileindex, int have_staged_files)
5419 const struct got_error *err = NULL;
5420 struct got_pathlist_entry *pe;
5421 char *relpath = NULL;
5423 TAILQ_FOREACH(pe, commitable_paths, entry) {
5424 struct got_fileindex_entry *ie;
5425 struct got_commitable *ct = pe->data;
5427 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5429 err = got_path_skip_common_ancestor(&relpath,
5430 worktree->root_path, ct->ondisk_path);
5431 if (err)
5432 goto done;
5434 if (ie) {
5435 if (ct->status == GOT_STATUS_DELETE ||
5436 ct->staged_status == GOT_STATUS_DELETE) {
5437 got_fileindex_entry_remove(fileindex, ie);
5438 } else if (ct->staged_status == GOT_STATUS_ADD ||
5439 ct->staged_status == GOT_STATUS_MODIFY) {
5440 got_fileindex_entry_stage_set(ie,
5441 GOT_FILEIDX_STAGE_NONE);
5442 got_fileindex_entry_staged_filetype_set(ie, 0);
5444 err = got_fileindex_entry_update(ie,
5445 worktree->root_fd, relpath,
5446 ct->staged_blob_id->sha1,
5447 new_base_commit_id->sha1,
5448 !have_staged_files);
5449 } else
5450 err = got_fileindex_entry_update(ie,
5451 worktree->root_fd, relpath,
5452 ct->blob_id->sha1,
5453 new_base_commit_id->sha1,
5454 !have_staged_files);
5455 } else {
5456 err = got_fileindex_entry_alloc(&ie, pe->path);
5457 if (err)
5458 goto done;
5459 err = got_fileindex_entry_update(ie,
5460 worktree->root_fd, relpath, ct->blob_id->sha1,
5461 new_base_commit_id->sha1, 1);
5462 if (err) {
5463 got_fileindex_entry_free(ie);
5464 goto done;
5466 err = got_fileindex_entry_add(fileindex, ie);
5467 if (err) {
5468 got_fileindex_entry_free(ie);
5469 goto done;
5472 free(relpath);
5473 relpath = NULL;
5475 done:
5476 free(relpath);
5477 return err;
5481 static const struct got_error *
5482 check_out_of_date(const char *in_repo_path, unsigned char status,
5483 unsigned char staged_status, struct got_object_id *base_blob_id,
5484 struct got_object_id *base_commit_id,
5485 struct got_object_id *head_commit_id, struct got_repository *repo,
5486 int ood_errcode)
5488 const struct got_error *err = NULL;
5489 struct got_object_id *id = NULL;
5491 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5492 /* Trivial case: base commit == head commit */
5493 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5494 return NULL;
5496 * Ensure file content which local changes were based
5497 * on matches file content in the branch head.
5499 err = got_object_id_by_path(&id, repo, head_commit_id,
5500 in_repo_path);
5501 if (err) {
5502 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5503 err = got_error(ood_errcode);
5504 goto done;
5505 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5506 err = got_error(ood_errcode);
5507 } else {
5508 /* Require that added files don't exist in the branch head. */
5509 err = got_object_id_by_path(&id, repo, head_commit_id,
5510 in_repo_path);
5511 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5512 goto done;
5513 err = id ? got_error(ood_errcode) : NULL;
5515 done:
5516 free(id);
5517 return err;
5520 const struct got_error *
5521 commit_worktree(struct got_object_id **new_commit_id,
5522 struct got_pathlist_head *commitable_paths,
5523 struct got_object_id *head_commit_id,
5524 struct got_object_id *parent_id2,
5525 struct got_worktree *worktree,
5526 const char *author, const char *committer,
5527 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5528 got_worktree_status_cb status_cb, void *status_arg,
5529 struct got_repository *repo)
5531 const struct got_error *err = NULL, *unlockerr = NULL;
5532 struct got_pathlist_entry *pe;
5533 const char *head_ref_name = NULL;
5534 struct got_commit_object *head_commit = NULL;
5535 struct got_reference *head_ref2 = NULL;
5536 struct got_object_id *head_commit_id2 = NULL;
5537 struct got_tree_object *head_tree = NULL;
5538 struct got_object_id *new_tree_id = NULL;
5539 int nentries, nparents = 0;
5540 struct got_object_id_queue parent_ids;
5541 struct got_object_qid *pid = NULL;
5542 char *logmsg = NULL;
5544 *new_commit_id = NULL;
5546 STAILQ_INIT(&parent_ids);
5548 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5549 if (err)
5550 goto done;
5552 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5553 if (err)
5554 goto done;
5556 if (commit_msg_cb != NULL) {
5557 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5558 if (err)
5559 goto done;
5562 if (logmsg == NULL || strlen(logmsg) == 0) {
5563 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5564 goto done;
5567 /* Create blobs from added and modified files and record their IDs. */
5568 TAILQ_FOREACH(pe, commitable_paths, entry) {
5569 struct got_commitable *ct = pe->data;
5570 char *ondisk_path;
5572 /* Blobs for staged files already exist. */
5573 if (ct->staged_status == GOT_STATUS_ADD ||
5574 ct->staged_status == GOT_STATUS_MODIFY)
5575 continue;
5577 if (ct->status != GOT_STATUS_ADD &&
5578 ct->status != GOT_STATUS_MODIFY &&
5579 ct->status != GOT_STATUS_MODE_CHANGE)
5580 continue;
5582 if (asprintf(&ondisk_path, "%s/%s",
5583 worktree->root_path, pe->path) == -1) {
5584 err = got_error_from_errno("asprintf");
5585 goto done;
5587 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5588 free(ondisk_path);
5589 if (err)
5590 goto done;
5593 /* Recursively write new tree objects. */
5594 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5595 commitable_paths, status_cb, status_arg, repo);
5596 if (err)
5597 goto done;
5599 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5600 if (err)
5601 goto done;
5602 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5603 nparents++;
5604 if (parent_id2) {
5605 err = got_object_qid_alloc(&pid, parent_id2);
5606 if (err)
5607 goto done;
5608 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5609 nparents++;
5611 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5612 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5613 if (logmsg != NULL)
5614 free(logmsg);
5615 if (err)
5616 goto done;
5618 /* Check if a concurrent commit to our branch has occurred. */
5619 head_ref_name = got_worktree_get_head_ref_name(worktree);
5620 if (head_ref_name == NULL) {
5621 err = got_error_from_errno("got_worktree_get_head_ref_name");
5622 goto done;
5624 /* Lock the reference here to prevent concurrent modification. */
5625 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5626 if (err)
5627 goto done;
5628 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5629 if (err)
5630 goto done;
5631 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5632 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5633 goto done;
5635 /* Update branch head in repository. */
5636 err = got_ref_change_ref(head_ref2, *new_commit_id);
5637 if (err)
5638 goto done;
5639 err = got_ref_write(head_ref2, repo);
5640 if (err)
5641 goto done;
5643 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5644 if (err)
5645 goto done;
5647 err = ref_base_commit(worktree, repo);
5648 if (err)
5649 goto done;
5650 done:
5651 got_object_id_queue_free(&parent_ids);
5652 if (head_tree)
5653 got_object_tree_close(head_tree);
5654 if (head_commit)
5655 got_object_commit_close(head_commit);
5656 free(head_commit_id2);
5657 if (head_ref2) {
5658 unlockerr = got_ref_unlock(head_ref2);
5659 if (unlockerr && err == NULL)
5660 err = unlockerr;
5661 got_ref_close(head_ref2);
5663 return err;
5666 static const struct got_error *
5667 check_path_is_commitable(const char *path,
5668 struct got_pathlist_head *commitable_paths)
5670 struct got_pathlist_entry *cpe = NULL;
5671 size_t path_len = strlen(path);
5673 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5674 struct got_commitable *ct = cpe->data;
5675 const char *ct_path = ct->path;
5677 while (ct_path[0] == '/')
5678 ct_path++;
5680 if (strcmp(path, ct_path) == 0 ||
5681 got_path_is_child(ct_path, path, path_len))
5682 break;
5685 if (cpe == NULL)
5686 return got_error_path(path, GOT_ERR_BAD_PATH);
5688 return NULL;
5691 static const struct got_error *
5692 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5694 int *have_staged_files = arg;
5696 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5697 *have_staged_files = 1;
5698 return got_error(GOT_ERR_CANCELLED);
5701 return NULL;
5704 static const struct got_error *
5705 check_non_staged_files(struct got_fileindex *fileindex,
5706 struct got_pathlist_head *paths)
5708 struct got_pathlist_entry *pe;
5709 struct got_fileindex_entry *ie;
5711 TAILQ_FOREACH(pe, paths, entry) {
5712 if (pe->path[0] == '\0')
5713 continue;
5714 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5715 if (ie == NULL)
5716 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5717 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5718 return got_error_path(pe->path,
5719 GOT_ERR_FILE_NOT_STAGED);
5722 return NULL;
5725 const struct got_error *
5726 got_worktree_commit(struct got_object_id **new_commit_id,
5727 struct got_worktree *worktree, struct got_pathlist_head *paths,
5728 const char *author, const char *committer, int allow_bad_symlinks,
5729 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5730 got_worktree_status_cb status_cb, void *status_arg,
5731 struct got_repository *repo)
5733 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5734 struct got_fileindex *fileindex = NULL;
5735 char *fileindex_path = NULL;
5736 struct got_pathlist_head commitable_paths;
5737 struct collect_commitables_arg cc_arg;
5738 struct got_pathlist_entry *pe;
5739 struct got_reference *head_ref = NULL;
5740 struct got_object_id *head_commit_id = NULL;
5741 int have_staged_files = 0;
5743 *new_commit_id = NULL;
5745 TAILQ_INIT(&commitable_paths);
5747 err = lock_worktree(worktree, LOCK_EX);
5748 if (err)
5749 goto done;
5751 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5752 if (err)
5753 goto done;
5755 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5756 if (err)
5757 goto done;
5759 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5760 if (err)
5761 goto done;
5763 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5764 &have_staged_files);
5765 if (err && err->code != GOT_ERR_CANCELLED)
5766 goto done;
5767 if (have_staged_files) {
5768 err = check_non_staged_files(fileindex, paths);
5769 if (err)
5770 goto done;
5773 cc_arg.commitable_paths = &commitable_paths;
5774 cc_arg.worktree = worktree;
5775 cc_arg.fileindex = fileindex;
5776 cc_arg.repo = repo;
5777 cc_arg.have_staged_files = have_staged_files;
5778 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5779 TAILQ_FOREACH(pe, paths, entry) {
5780 err = worktree_status(worktree, pe->path, fileindex, repo,
5781 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5782 if (err)
5783 goto done;
5786 if (TAILQ_EMPTY(&commitable_paths)) {
5787 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5788 goto done;
5791 TAILQ_FOREACH(pe, paths, entry) {
5792 err = check_path_is_commitable(pe->path, &commitable_paths);
5793 if (err)
5794 goto done;
5797 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5798 struct got_commitable *ct = pe->data;
5799 const char *ct_path = ct->in_repo_path;
5801 while (ct_path[0] == '/')
5802 ct_path++;
5803 err = check_out_of_date(ct_path, ct->status,
5804 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5805 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5806 if (err)
5807 goto done;
5811 err = commit_worktree(new_commit_id, &commitable_paths,
5812 head_commit_id, NULL, worktree, author, committer,
5813 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5814 if (err)
5815 goto done;
5817 err = update_fileindex_after_commit(worktree, &commitable_paths,
5818 *new_commit_id, fileindex, have_staged_files);
5819 sync_err = sync_fileindex(fileindex, fileindex_path);
5820 if (sync_err && err == NULL)
5821 err = sync_err;
5822 done:
5823 if (fileindex)
5824 got_fileindex_free(fileindex);
5825 free(fileindex_path);
5826 unlockerr = lock_worktree(worktree, LOCK_SH);
5827 if (unlockerr && err == NULL)
5828 err = unlockerr;
5829 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5830 struct got_commitable *ct = pe->data;
5831 free_commitable(ct);
5833 got_pathlist_free(&commitable_paths);
5834 return err;
5837 const char *
5838 got_commitable_get_path(struct got_commitable *ct)
5840 return ct->path;
5843 unsigned int
5844 got_commitable_get_status(struct got_commitable *ct)
5846 return ct->status;
5849 struct check_rebase_ok_arg {
5850 struct got_worktree *worktree;
5851 struct got_repository *repo;
5854 static const struct got_error *
5855 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5857 const struct got_error *err = NULL;
5858 struct check_rebase_ok_arg *a = arg;
5859 unsigned char status;
5860 struct stat sb;
5861 char *ondisk_path;
5863 /* Reject rebase of a work tree with mixed base commits. */
5864 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5865 SHA1_DIGEST_LENGTH))
5866 return got_error(GOT_ERR_MIXED_COMMITS);
5868 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5869 == -1)
5870 return got_error_from_errno("asprintf");
5872 /* Reject rebase of a work tree with modified or staged files. */
5873 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5874 free(ondisk_path);
5875 if (err)
5876 return err;
5878 if (status != GOT_STATUS_NO_CHANGE)
5879 return got_error(GOT_ERR_MODIFIED);
5880 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5881 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5883 return NULL;
5886 const struct got_error *
5887 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5888 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5889 struct got_worktree *worktree, struct got_reference *branch,
5890 struct got_repository *repo)
5892 const struct got_error *err = NULL;
5893 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5894 char *branch_ref_name = NULL;
5895 char *fileindex_path = NULL;
5896 struct check_rebase_ok_arg ok_arg;
5897 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5898 struct got_object_id *wt_branch_tip = NULL;
5900 *new_base_branch_ref = NULL;
5901 *tmp_branch = NULL;
5902 *fileindex = NULL;
5904 err = lock_worktree(worktree, LOCK_EX);
5905 if (err)
5906 return err;
5908 err = open_fileindex(fileindex, &fileindex_path, worktree);
5909 if (err)
5910 goto done;
5912 ok_arg.worktree = worktree;
5913 ok_arg.repo = repo;
5914 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5915 &ok_arg);
5916 if (err)
5917 goto done;
5919 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5920 if (err)
5921 goto done;
5923 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5924 if (err)
5925 goto done;
5927 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5928 if (err)
5929 goto done;
5931 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5932 0);
5933 if (err)
5934 goto done;
5936 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5937 if (err)
5938 goto done;
5939 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5940 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5941 goto done;
5944 err = got_ref_alloc_symref(new_base_branch_ref,
5945 new_base_branch_ref_name, wt_branch);
5946 if (err)
5947 goto done;
5948 err = got_ref_write(*new_base_branch_ref, repo);
5949 if (err)
5950 goto done;
5952 /* TODO Lock original branch's ref while rebasing? */
5954 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5955 if (err)
5956 goto done;
5958 err = got_ref_write(branch_ref, repo);
5959 if (err)
5960 goto done;
5962 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5963 worktree->base_commit_id);
5964 if (err)
5965 goto done;
5966 err = got_ref_write(*tmp_branch, repo);
5967 if (err)
5968 goto done;
5970 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5971 if (err)
5972 goto done;
5973 done:
5974 free(fileindex_path);
5975 free(tmp_branch_name);
5976 free(new_base_branch_ref_name);
5977 free(branch_ref_name);
5978 if (branch_ref)
5979 got_ref_close(branch_ref);
5980 if (wt_branch)
5981 got_ref_close(wt_branch);
5982 free(wt_branch_tip);
5983 if (err) {
5984 if (*new_base_branch_ref) {
5985 got_ref_close(*new_base_branch_ref);
5986 *new_base_branch_ref = NULL;
5988 if (*tmp_branch) {
5989 got_ref_close(*tmp_branch);
5990 *tmp_branch = NULL;
5992 if (*fileindex) {
5993 got_fileindex_free(*fileindex);
5994 *fileindex = NULL;
5996 lock_worktree(worktree, LOCK_SH);
5998 return err;
6001 const struct got_error *
6002 got_worktree_rebase_continue(struct got_object_id **commit_id,
6003 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6004 struct got_reference **branch, struct got_fileindex **fileindex,
6005 struct got_worktree *worktree, struct got_repository *repo)
6007 const struct got_error *err;
6008 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6009 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6010 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6011 char *fileindex_path = NULL;
6012 int have_staged_files = 0;
6014 *commit_id = NULL;
6015 *new_base_branch = NULL;
6016 *tmp_branch = NULL;
6017 *branch = NULL;
6018 *fileindex = NULL;
6020 err = lock_worktree(worktree, LOCK_EX);
6021 if (err)
6022 return err;
6024 err = open_fileindex(fileindex, &fileindex_path, worktree);
6025 if (err)
6026 goto done;
6028 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6029 &have_staged_files);
6030 if (err && err->code != GOT_ERR_CANCELLED)
6031 goto done;
6032 if (have_staged_files) {
6033 err = got_error(GOT_ERR_STAGED_PATHS);
6034 goto done;
6037 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6038 if (err)
6039 goto done;
6041 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6042 if (err)
6043 goto done;
6045 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6046 if (err)
6047 goto done;
6049 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6050 if (err)
6051 goto done;
6053 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6054 if (err)
6055 goto done;
6057 err = got_ref_open(branch, repo,
6058 got_ref_get_symref_target(branch_ref), 0);
6059 if (err)
6060 goto done;
6062 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6063 if (err)
6064 goto done;
6066 err = got_ref_resolve(commit_id, repo, commit_ref);
6067 if (err)
6068 goto done;
6070 err = got_ref_open(new_base_branch, repo,
6071 new_base_branch_ref_name, 0);
6072 if (err)
6073 goto done;
6075 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6076 if (err)
6077 goto done;
6078 done:
6079 free(commit_ref_name);
6080 free(branch_ref_name);
6081 free(fileindex_path);
6082 if (commit_ref)
6083 got_ref_close(commit_ref);
6084 if (branch_ref)
6085 got_ref_close(branch_ref);
6086 if (err) {
6087 free(*commit_id);
6088 *commit_id = NULL;
6089 if (*tmp_branch) {
6090 got_ref_close(*tmp_branch);
6091 *tmp_branch = NULL;
6093 if (*new_base_branch) {
6094 got_ref_close(*new_base_branch);
6095 *new_base_branch = NULL;
6097 if (*branch) {
6098 got_ref_close(*branch);
6099 *branch = NULL;
6101 if (*fileindex) {
6102 got_fileindex_free(*fileindex);
6103 *fileindex = NULL;
6105 lock_worktree(worktree, LOCK_SH);
6107 return err;
6110 const struct got_error *
6111 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6113 const struct got_error *err;
6114 char *tmp_branch_name = NULL;
6116 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6117 if (err)
6118 return err;
6120 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6121 free(tmp_branch_name);
6122 return NULL;
6125 static const struct got_error *
6126 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6127 char **logmsg, void *arg)
6129 *logmsg = arg;
6130 return NULL;
6133 static const struct got_error *
6134 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6135 const char *path, struct got_object_id *blob_id,
6136 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6137 int dirfd, const char *de_name)
6139 return NULL;
6142 struct collect_merged_paths_arg {
6143 got_worktree_checkout_cb progress_cb;
6144 void *progress_arg;
6145 struct got_pathlist_head *merged_paths;
6148 static const struct got_error *
6149 collect_merged_paths(void *arg, unsigned char status, const char *path)
6151 const struct got_error *err;
6152 struct collect_merged_paths_arg *a = arg;
6153 char *p;
6154 struct got_pathlist_entry *new;
6156 err = (*a->progress_cb)(a->progress_arg, status, path);
6157 if (err)
6158 return err;
6160 if (status != GOT_STATUS_MERGE &&
6161 status != GOT_STATUS_ADD &&
6162 status != GOT_STATUS_DELETE &&
6163 status != GOT_STATUS_CONFLICT)
6164 return NULL;
6166 p = strdup(path);
6167 if (p == NULL)
6168 return got_error_from_errno("strdup");
6170 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6171 if (err || new == NULL)
6172 free(p);
6173 return err;
6176 void
6177 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6179 struct got_pathlist_entry *pe;
6181 TAILQ_FOREACH(pe, merged_paths, entry)
6182 free((char *)pe->path);
6184 got_pathlist_free(merged_paths);
6187 static const struct got_error *
6188 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6189 int is_rebase, struct got_repository *repo)
6191 const struct got_error *err;
6192 struct got_reference *commit_ref = NULL;
6194 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6195 if (err) {
6196 if (err->code != GOT_ERR_NOT_REF)
6197 goto done;
6198 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6199 if (err)
6200 goto done;
6201 err = got_ref_write(commit_ref, repo);
6202 if (err)
6203 goto done;
6204 } else if (is_rebase) {
6205 struct got_object_id *stored_id;
6206 int cmp;
6208 err = got_ref_resolve(&stored_id, repo, commit_ref);
6209 if (err)
6210 goto done;
6211 cmp = got_object_id_cmp(commit_id, stored_id);
6212 free(stored_id);
6213 if (cmp != 0) {
6214 err = got_error(GOT_ERR_REBASE_COMMITID);
6215 goto done;
6218 done:
6219 if (commit_ref)
6220 got_ref_close(commit_ref);
6221 return err;
6224 static const struct got_error *
6225 rebase_merge_files(struct got_pathlist_head *merged_paths,
6226 const char *commit_ref_name, struct got_worktree *worktree,
6227 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6228 struct got_object_id *commit_id, struct got_repository *repo,
6229 got_worktree_checkout_cb progress_cb, void *progress_arg,
6230 got_cancel_cb cancel_cb, void *cancel_arg)
6232 const struct got_error *err;
6233 struct got_reference *commit_ref = NULL;
6234 struct collect_merged_paths_arg cmp_arg;
6235 char *fileindex_path;
6237 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6239 err = get_fileindex_path(&fileindex_path, worktree);
6240 if (err)
6241 return err;
6243 cmp_arg.progress_cb = progress_cb;
6244 cmp_arg.progress_arg = progress_arg;
6245 cmp_arg.merged_paths = merged_paths;
6246 err = merge_files(worktree, fileindex, fileindex_path,
6247 parent_commit_id, commit_id, repo, collect_merged_paths,
6248 &cmp_arg, cancel_cb, cancel_arg);
6249 if (commit_ref)
6250 got_ref_close(commit_ref);
6251 return err;
6254 const struct got_error *
6255 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6256 struct got_worktree *worktree, struct got_fileindex *fileindex,
6257 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6258 struct got_repository *repo,
6259 got_worktree_checkout_cb progress_cb, void *progress_arg,
6260 got_cancel_cb cancel_cb, void *cancel_arg)
6262 const struct got_error *err;
6263 char *commit_ref_name;
6265 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6266 if (err)
6267 return err;
6269 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6270 if (err)
6271 goto done;
6273 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6274 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6275 progress_arg, cancel_cb, cancel_arg);
6276 done:
6277 free(commit_ref_name);
6278 return err;
6281 const struct got_error *
6282 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6283 struct got_worktree *worktree, struct got_fileindex *fileindex,
6284 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6285 struct got_repository *repo,
6286 got_worktree_checkout_cb progress_cb, void *progress_arg,
6287 got_cancel_cb cancel_cb, void *cancel_arg)
6289 const struct got_error *err;
6290 char *commit_ref_name;
6292 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6293 if (err)
6294 return err;
6296 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6297 if (err)
6298 goto done;
6300 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6301 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6302 progress_arg, cancel_cb, cancel_arg);
6303 done:
6304 free(commit_ref_name);
6305 return err;
6308 static const struct got_error *
6309 rebase_commit(struct got_object_id **new_commit_id,
6310 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6311 struct got_worktree *worktree, struct got_fileindex *fileindex,
6312 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6313 const char *new_logmsg, struct got_repository *repo)
6315 const struct got_error *err, *sync_err;
6316 struct got_pathlist_head commitable_paths;
6317 struct collect_commitables_arg cc_arg;
6318 char *fileindex_path = NULL;
6319 struct got_reference *head_ref = NULL;
6320 struct got_object_id *head_commit_id = NULL;
6321 char *logmsg = NULL;
6323 TAILQ_INIT(&commitable_paths);
6324 *new_commit_id = NULL;
6326 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6328 err = get_fileindex_path(&fileindex_path, worktree);
6329 if (err)
6330 return err;
6332 cc_arg.commitable_paths = &commitable_paths;
6333 cc_arg.worktree = worktree;
6334 cc_arg.repo = repo;
6335 cc_arg.have_staged_files = 0;
6337 * If possible get the status of individual files directly to
6338 * avoid crawling the entire work tree once per rebased commit.
6340 * Ideally, merged_paths would contain a list of commitables
6341 * we could use so we could skip worktree_status() entirely.
6342 * However, we would then need carefully keep track of cumulative
6343 * effects of operations such as file additions and deletions
6344 * in 'got histedit -f' (folding multiple commits into one),
6345 * and this extra complexity is not really worth it.
6347 if (merged_paths) {
6348 struct got_pathlist_entry *pe;
6349 TAILQ_FOREACH(pe, merged_paths, entry) {
6350 err = worktree_status(worktree, pe->path, fileindex,
6351 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6352 0);
6353 if (err)
6354 goto done;
6356 } else {
6357 err = worktree_status(worktree, "", fileindex, repo,
6358 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6359 if (err)
6360 goto done;
6363 if (TAILQ_EMPTY(&commitable_paths)) {
6364 /* No-op change; commit will be elided. */
6365 err = got_ref_delete(commit_ref, repo);
6366 if (err)
6367 goto done;
6368 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6369 goto done;
6372 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6373 if (err)
6374 goto done;
6376 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6377 if (err)
6378 goto done;
6380 if (new_logmsg) {
6381 logmsg = strdup(new_logmsg);
6382 if (logmsg == NULL) {
6383 err = got_error_from_errno("strdup");
6384 goto done;
6386 } else {
6387 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6388 if (err)
6389 goto done;
6392 /* NB: commit_worktree will call free(logmsg) */
6393 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6394 NULL, worktree, got_object_commit_get_author(orig_commit),
6395 got_object_commit_get_committer(orig_commit),
6396 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6397 if (err)
6398 goto done;
6400 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6401 if (err)
6402 goto done;
6404 err = got_ref_delete(commit_ref, repo);
6405 if (err)
6406 goto done;
6408 err = update_fileindex_after_commit(worktree, &commitable_paths,
6409 *new_commit_id, fileindex, 0);
6410 sync_err = sync_fileindex(fileindex, fileindex_path);
6411 if (sync_err && err == NULL)
6412 err = sync_err;
6413 done:
6414 free(fileindex_path);
6415 free(head_commit_id);
6416 if (head_ref)
6417 got_ref_close(head_ref);
6418 if (err) {
6419 free(*new_commit_id);
6420 *new_commit_id = NULL;
6422 return err;
6425 const struct got_error *
6426 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6427 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6428 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6429 struct got_commit_object *orig_commit,
6430 struct got_object_id *orig_commit_id, struct got_repository *repo)
6432 const struct got_error *err;
6433 char *commit_ref_name;
6434 struct got_reference *commit_ref = NULL;
6435 struct got_object_id *commit_id = NULL;
6437 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6438 if (err)
6439 return err;
6441 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6442 if (err)
6443 goto done;
6444 err = got_ref_resolve(&commit_id, repo, commit_ref);
6445 if (err)
6446 goto done;
6447 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6448 err = got_error(GOT_ERR_REBASE_COMMITID);
6449 goto done;
6452 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6453 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6454 done:
6455 if (commit_ref)
6456 got_ref_close(commit_ref);
6457 free(commit_ref_name);
6458 free(commit_id);
6459 return err;
6462 const struct got_error *
6463 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6464 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6465 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6466 struct got_commit_object *orig_commit,
6467 struct got_object_id *orig_commit_id, const char *new_logmsg,
6468 struct got_repository *repo)
6470 const struct got_error *err;
6471 char *commit_ref_name;
6472 struct got_reference *commit_ref = NULL;
6474 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6475 if (err)
6476 return err;
6478 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6479 if (err)
6480 goto done;
6482 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6483 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6484 done:
6485 if (commit_ref)
6486 got_ref_close(commit_ref);
6487 free(commit_ref_name);
6488 return err;
6491 const struct got_error *
6492 got_worktree_rebase_postpone(struct got_worktree *worktree,
6493 struct got_fileindex *fileindex)
6495 if (fileindex)
6496 got_fileindex_free(fileindex);
6497 return lock_worktree(worktree, LOCK_SH);
6500 static const struct got_error *
6501 delete_ref(const char *name, struct got_repository *repo)
6503 const struct got_error *err;
6504 struct got_reference *ref;
6506 err = got_ref_open(&ref, repo, name, 0);
6507 if (err) {
6508 if (err->code == GOT_ERR_NOT_REF)
6509 return NULL;
6510 return err;
6513 err = got_ref_delete(ref, repo);
6514 got_ref_close(ref);
6515 return err;
6518 static const struct got_error *
6519 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6521 const struct got_error *err;
6522 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6523 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6525 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6526 if (err)
6527 goto done;
6528 err = delete_ref(tmp_branch_name, repo);
6529 if (err)
6530 goto done;
6532 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6533 if (err)
6534 goto done;
6535 err = delete_ref(new_base_branch_ref_name, repo);
6536 if (err)
6537 goto done;
6539 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6540 if (err)
6541 goto done;
6542 err = delete_ref(branch_ref_name, repo);
6543 if (err)
6544 goto done;
6546 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6547 if (err)
6548 goto done;
6549 err = delete_ref(commit_ref_name, repo);
6550 if (err)
6551 goto done;
6553 done:
6554 free(tmp_branch_name);
6555 free(new_base_branch_ref_name);
6556 free(branch_ref_name);
6557 free(commit_ref_name);
6558 return err;
6561 const struct got_error *
6562 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6563 struct got_object_id *new_commit_id, struct got_repository *repo)
6565 const struct got_error *err;
6566 struct got_reference *ref = NULL;
6567 struct got_object_id *old_commit_id = NULL;
6568 const char *branch_name = NULL;
6569 char *new_id_str = NULL;
6570 char *refname = NULL;
6572 branch_name = got_ref_get_name(branch);
6573 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6574 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6575 branch_name += 11;
6577 err = got_object_id_str(&new_id_str, new_commit_id);
6578 if (err)
6579 return err;
6581 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6582 new_id_str) == -1) {
6583 err = got_error_from_errno("asprintf");
6584 goto done;
6587 err = got_ref_resolve(&old_commit_id, repo, branch);
6588 if (err)
6589 goto done;
6591 err = got_ref_alloc(&ref, refname, old_commit_id);
6592 if (err)
6593 goto done;
6595 err = got_ref_write(ref, repo);
6596 done:
6597 free(new_id_str);
6598 free(refname);
6599 free(old_commit_id);
6600 if (ref)
6601 got_ref_close(ref);
6602 return err;
6605 const struct got_error *
6606 got_worktree_rebase_complete(struct got_worktree *worktree,
6607 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6608 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6609 struct got_repository *repo, int create_backup)
6611 const struct got_error *err, *unlockerr, *sync_err;
6612 struct got_object_id *new_head_commit_id = NULL;
6613 char *fileindex_path = NULL;
6615 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6616 if (err)
6617 return err;
6619 if (create_backup) {
6620 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6621 rebased_branch, new_head_commit_id, repo);
6622 if (err)
6623 goto done;
6626 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6627 if (err)
6628 goto done;
6630 err = got_ref_write(rebased_branch, repo);
6631 if (err)
6632 goto done;
6634 err = got_worktree_set_head_ref(worktree, rebased_branch);
6635 if (err)
6636 goto done;
6638 err = delete_rebase_refs(worktree, repo);
6639 if (err)
6640 goto done;
6642 err = get_fileindex_path(&fileindex_path, worktree);
6643 if (err)
6644 goto done;
6645 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6646 sync_err = sync_fileindex(fileindex, fileindex_path);
6647 if (sync_err && err == NULL)
6648 err = sync_err;
6649 done:
6650 got_fileindex_free(fileindex);
6651 free(fileindex_path);
6652 free(new_head_commit_id);
6653 unlockerr = lock_worktree(worktree, LOCK_SH);
6654 if (unlockerr && err == NULL)
6655 err = unlockerr;
6656 return err;
6659 const struct got_error *
6660 got_worktree_rebase_abort(struct got_worktree *worktree,
6661 struct got_fileindex *fileindex, struct got_repository *repo,
6662 struct got_reference *new_base_branch,
6663 got_worktree_checkout_cb progress_cb, void *progress_arg)
6665 const struct got_error *err, *unlockerr, *sync_err;
6666 struct got_reference *resolved = NULL;
6667 struct got_object_id *commit_id = NULL;
6668 char *fileindex_path = NULL;
6669 struct revert_file_args rfa;
6670 struct got_object_id *tree_id = NULL;
6672 err = lock_worktree(worktree, LOCK_EX);
6673 if (err)
6674 return err;
6676 err = got_ref_open(&resolved, repo,
6677 got_ref_get_symref_target(new_base_branch), 0);
6678 if (err)
6679 goto done;
6681 err = got_worktree_set_head_ref(worktree, resolved);
6682 if (err)
6683 goto done;
6686 * XXX commits to the base branch could have happened while
6687 * we were busy rebasing; should we store the original commit ID
6688 * when rebase begins and read it back here?
6690 err = got_ref_resolve(&commit_id, repo, resolved);
6691 if (err)
6692 goto done;
6694 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6695 if (err)
6696 goto done;
6698 err = got_object_id_by_path(&tree_id, repo,
6699 worktree->base_commit_id, worktree->path_prefix);
6700 if (err)
6701 goto done;
6703 err = delete_rebase_refs(worktree, repo);
6704 if (err)
6705 goto done;
6707 err = get_fileindex_path(&fileindex_path, worktree);
6708 if (err)
6709 goto done;
6711 rfa.worktree = worktree;
6712 rfa.fileindex = fileindex;
6713 rfa.progress_cb = progress_cb;
6714 rfa.progress_arg = progress_arg;
6715 rfa.patch_cb = NULL;
6716 rfa.patch_arg = NULL;
6717 rfa.repo = repo;
6718 rfa.unlink_added_files = 0;
6719 err = worktree_status(worktree, "", fileindex, repo,
6720 revert_file, &rfa, NULL, NULL, 1, 0);
6721 if (err)
6722 goto sync;
6724 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6725 repo, progress_cb, progress_arg, NULL, NULL);
6726 sync:
6727 sync_err = sync_fileindex(fileindex, fileindex_path);
6728 if (sync_err && err == NULL)
6729 err = sync_err;
6730 done:
6731 got_ref_close(resolved);
6732 free(tree_id);
6733 free(commit_id);
6734 if (fileindex)
6735 got_fileindex_free(fileindex);
6736 free(fileindex_path);
6738 unlockerr = lock_worktree(worktree, LOCK_SH);
6739 if (unlockerr && err == NULL)
6740 err = unlockerr;
6741 return err;
6744 const struct got_error *
6745 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6746 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6747 struct got_fileindex **fileindex, struct got_worktree *worktree,
6748 struct got_repository *repo)
6750 const struct got_error *err = NULL;
6751 char *tmp_branch_name = NULL;
6752 char *branch_ref_name = NULL;
6753 char *base_commit_ref_name = NULL;
6754 char *fileindex_path = NULL;
6755 struct check_rebase_ok_arg ok_arg;
6756 struct got_reference *wt_branch = NULL;
6757 struct got_reference *base_commit_ref = NULL;
6759 *tmp_branch = NULL;
6760 *branch_ref = NULL;
6761 *base_commit_id = NULL;
6762 *fileindex = NULL;
6764 err = lock_worktree(worktree, LOCK_EX);
6765 if (err)
6766 return err;
6768 err = open_fileindex(fileindex, &fileindex_path, worktree);
6769 if (err)
6770 goto done;
6772 ok_arg.worktree = worktree;
6773 ok_arg.repo = repo;
6774 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6775 &ok_arg);
6776 if (err)
6777 goto done;
6779 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6780 if (err)
6781 goto done;
6783 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6784 if (err)
6785 goto done;
6787 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6788 worktree);
6789 if (err)
6790 goto done;
6792 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6793 0);
6794 if (err)
6795 goto done;
6797 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6798 if (err)
6799 goto done;
6801 err = got_ref_write(*branch_ref, repo);
6802 if (err)
6803 goto done;
6805 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6806 worktree->base_commit_id);
6807 if (err)
6808 goto done;
6809 err = got_ref_write(base_commit_ref, repo);
6810 if (err)
6811 goto done;
6812 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6813 if (*base_commit_id == NULL) {
6814 err = got_error_from_errno("got_object_id_dup");
6815 goto done;
6818 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6819 worktree->base_commit_id);
6820 if (err)
6821 goto done;
6822 err = got_ref_write(*tmp_branch, repo);
6823 if (err)
6824 goto done;
6826 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6827 if (err)
6828 goto done;
6829 done:
6830 free(fileindex_path);
6831 free(tmp_branch_name);
6832 free(branch_ref_name);
6833 free(base_commit_ref_name);
6834 if (wt_branch)
6835 got_ref_close(wt_branch);
6836 if (err) {
6837 if (*branch_ref) {
6838 got_ref_close(*branch_ref);
6839 *branch_ref = NULL;
6841 if (*tmp_branch) {
6842 got_ref_close(*tmp_branch);
6843 *tmp_branch = NULL;
6845 free(*base_commit_id);
6846 if (*fileindex) {
6847 got_fileindex_free(*fileindex);
6848 *fileindex = NULL;
6850 lock_worktree(worktree, LOCK_SH);
6852 return err;
6855 const struct got_error *
6856 got_worktree_histedit_postpone(struct got_worktree *worktree,
6857 struct got_fileindex *fileindex)
6859 if (fileindex)
6860 got_fileindex_free(fileindex);
6861 return lock_worktree(worktree, LOCK_SH);
6864 const struct got_error *
6865 got_worktree_histedit_in_progress(int *in_progress,
6866 struct got_worktree *worktree)
6868 const struct got_error *err;
6869 char *tmp_branch_name = NULL;
6871 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6872 if (err)
6873 return err;
6875 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6876 free(tmp_branch_name);
6877 return NULL;
6880 const struct got_error *
6881 got_worktree_histedit_continue(struct got_object_id **commit_id,
6882 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6883 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6884 struct got_worktree *worktree, struct got_repository *repo)
6886 const struct got_error *err;
6887 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6888 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6889 struct got_reference *commit_ref = NULL;
6890 struct got_reference *base_commit_ref = NULL;
6891 char *fileindex_path = NULL;
6892 int have_staged_files = 0;
6894 *commit_id = NULL;
6895 *tmp_branch = NULL;
6896 *base_commit_id = NULL;
6897 *fileindex = NULL;
6899 err = lock_worktree(worktree, LOCK_EX);
6900 if (err)
6901 return err;
6903 err = open_fileindex(fileindex, &fileindex_path, worktree);
6904 if (err)
6905 goto done;
6907 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6908 &have_staged_files);
6909 if (err && err->code != GOT_ERR_CANCELLED)
6910 goto done;
6911 if (have_staged_files) {
6912 err = got_error(GOT_ERR_STAGED_PATHS);
6913 goto done;
6916 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6917 if (err)
6918 goto done;
6920 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6921 if (err)
6922 goto done;
6924 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6925 if (err)
6926 goto done;
6928 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6929 worktree);
6930 if (err)
6931 goto done;
6933 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6934 if (err)
6935 goto done;
6937 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6938 if (err)
6939 goto done;
6940 err = got_ref_resolve(commit_id, repo, commit_ref);
6941 if (err)
6942 goto done;
6944 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6945 if (err)
6946 goto done;
6947 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6948 if (err)
6949 goto done;
6951 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6952 if (err)
6953 goto done;
6954 done:
6955 free(commit_ref_name);
6956 free(branch_ref_name);
6957 free(fileindex_path);
6958 if (commit_ref)
6959 got_ref_close(commit_ref);
6960 if (base_commit_ref)
6961 got_ref_close(base_commit_ref);
6962 if (err) {
6963 free(*commit_id);
6964 *commit_id = NULL;
6965 free(*base_commit_id);
6966 *base_commit_id = NULL;
6967 if (*tmp_branch) {
6968 got_ref_close(*tmp_branch);
6969 *tmp_branch = NULL;
6971 if (*fileindex) {
6972 got_fileindex_free(*fileindex);
6973 *fileindex = NULL;
6975 lock_worktree(worktree, LOCK_EX);
6977 return err;
6980 static const struct got_error *
6981 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6983 const struct got_error *err;
6984 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6985 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6987 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6988 if (err)
6989 goto done;
6990 err = delete_ref(tmp_branch_name, repo);
6991 if (err)
6992 goto done;
6994 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6995 worktree);
6996 if (err)
6997 goto done;
6998 err = delete_ref(base_commit_ref_name, repo);
6999 if (err)
7000 goto done;
7002 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7003 if (err)
7004 goto done;
7005 err = delete_ref(branch_ref_name, repo);
7006 if (err)
7007 goto done;
7009 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7010 if (err)
7011 goto done;
7012 err = delete_ref(commit_ref_name, repo);
7013 if (err)
7014 goto done;
7015 done:
7016 free(tmp_branch_name);
7017 free(base_commit_ref_name);
7018 free(branch_ref_name);
7019 free(commit_ref_name);
7020 return err;
7023 const struct got_error *
7024 got_worktree_histedit_abort(struct got_worktree *worktree,
7025 struct got_fileindex *fileindex, struct got_repository *repo,
7026 struct got_reference *branch, struct got_object_id *base_commit_id,
7027 got_worktree_checkout_cb progress_cb, void *progress_arg)
7029 const struct got_error *err, *unlockerr, *sync_err;
7030 struct got_reference *resolved = NULL;
7031 char *fileindex_path = NULL;
7032 struct got_object_id *tree_id = NULL;
7033 struct revert_file_args rfa;
7035 err = lock_worktree(worktree, LOCK_EX);
7036 if (err)
7037 return err;
7039 err = got_ref_open(&resolved, repo,
7040 got_ref_get_symref_target(branch), 0);
7041 if (err)
7042 goto done;
7044 err = got_worktree_set_head_ref(worktree, resolved);
7045 if (err)
7046 goto done;
7048 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7049 if (err)
7050 goto done;
7052 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7053 worktree->path_prefix);
7054 if (err)
7055 goto done;
7057 err = delete_histedit_refs(worktree, repo);
7058 if (err)
7059 goto done;
7061 err = get_fileindex_path(&fileindex_path, worktree);
7062 if (err)
7063 goto done;
7065 rfa.worktree = worktree;
7066 rfa.fileindex = fileindex;
7067 rfa.progress_cb = progress_cb;
7068 rfa.progress_arg = progress_arg;
7069 rfa.patch_cb = NULL;
7070 rfa.patch_arg = NULL;
7071 rfa.repo = repo;
7072 rfa.unlink_added_files = 0;
7073 err = worktree_status(worktree, "", fileindex, repo,
7074 revert_file, &rfa, NULL, NULL, 1, 0);
7075 if (err)
7076 goto sync;
7078 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7079 repo, progress_cb, progress_arg, NULL, NULL);
7080 sync:
7081 sync_err = sync_fileindex(fileindex, fileindex_path);
7082 if (sync_err && err == NULL)
7083 err = sync_err;
7084 done:
7085 got_ref_close(resolved);
7086 free(tree_id);
7087 free(fileindex_path);
7089 unlockerr = lock_worktree(worktree, LOCK_SH);
7090 if (unlockerr && err == NULL)
7091 err = unlockerr;
7092 return err;
7095 const struct got_error *
7096 got_worktree_histedit_complete(struct got_worktree *worktree,
7097 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7098 struct got_reference *edited_branch, struct got_repository *repo)
7100 const struct got_error *err, *unlockerr, *sync_err;
7101 struct got_object_id *new_head_commit_id = NULL;
7102 struct got_reference *resolved = NULL;
7103 char *fileindex_path = NULL;
7105 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7106 if (err)
7107 return err;
7109 err = got_ref_open(&resolved, repo,
7110 got_ref_get_symref_target(edited_branch), 0);
7111 if (err)
7112 goto done;
7114 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7115 resolved, new_head_commit_id, repo);
7116 if (err)
7117 goto done;
7119 err = got_ref_change_ref(resolved, new_head_commit_id);
7120 if (err)
7121 goto done;
7123 err = got_ref_write(resolved, repo);
7124 if (err)
7125 goto done;
7127 err = got_worktree_set_head_ref(worktree, resolved);
7128 if (err)
7129 goto done;
7131 err = delete_histedit_refs(worktree, repo);
7132 if (err)
7133 goto done;
7135 err = get_fileindex_path(&fileindex_path, worktree);
7136 if (err)
7137 goto done;
7138 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7139 sync_err = sync_fileindex(fileindex, fileindex_path);
7140 if (sync_err && err == NULL)
7141 err = sync_err;
7142 done:
7143 got_fileindex_free(fileindex);
7144 free(fileindex_path);
7145 free(new_head_commit_id);
7146 unlockerr = lock_worktree(worktree, LOCK_SH);
7147 if (unlockerr && err == NULL)
7148 err = unlockerr;
7149 return err;
7152 const struct got_error *
7153 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7154 struct got_object_id *commit_id, struct got_repository *repo)
7156 const struct got_error *err;
7157 char *commit_ref_name;
7159 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7160 if (err)
7161 return err;
7163 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7164 if (err)
7165 goto done;
7167 err = delete_ref(commit_ref_name, repo);
7168 done:
7169 free(commit_ref_name);
7170 return err;
7173 const struct got_error *
7174 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7175 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7176 struct got_worktree *worktree, const char *refname,
7177 struct got_repository *repo)
7179 const struct got_error *err = NULL;
7180 char *fileindex_path = NULL;
7181 struct check_rebase_ok_arg ok_arg;
7183 *fileindex = NULL;
7184 *branch_ref = NULL;
7185 *base_branch_ref = NULL;
7187 err = lock_worktree(worktree, LOCK_EX);
7188 if (err)
7189 return err;
7191 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7192 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7193 "cannot integrate a branch into itself; "
7194 "update -b or different branch name required");
7195 goto done;
7198 err = open_fileindex(fileindex, &fileindex_path, worktree);
7199 if (err)
7200 goto done;
7202 /* Preconditions are the same as for rebase. */
7203 ok_arg.worktree = worktree;
7204 ok_arg.repo = repo;
7205 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7206 &ok_arg);
7207 if (err)
7208 goto done;
7210 err = got_ref_open(branch_ref, repo, refname, 1);
7211 if (err)
7212 goto done;
7214 err = got_ref_open(base_branch_ref, repo,
7215 got_worktree_get_head_ref_name(worktree), 1);
7216 done:
7217 if (err) {
7218 if (*branch_ref) {
7219 got_ref_close(*branch_ref);
7220 *branch_ref = NULL;
7222 if (*base_branch_ref) {
7223 got_ref_close(*base_branch_ref);
7224 *base_branch_ref = NULL;
7226 if (*fileindex) {
7227 got_fileindex_free(*fileindex);
7228 *fileindex = NULL;
7230 lock_worktree(worktree, LOCK_SH);
7232 return err;
7235 const struct got_error *
7236 got_worktree_integrate_continue(struct got_worktree *worktree,
7237 struct got_fileindex *fileindex, struct got_repository *repo,
7238 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7239 got_worktree_checkout_cb progress_cb, void *progress_arg,
7240 got_cancel_cb cancel_cb, void *cancel_arg)
7242 const struct got_error *err = NULL, *sync_err, *unlockerr;
7243 char *fileindex_path = NULL;
7244 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7246 err = get_fileindex_path(&fileindex_path, worktree);
7247 if (err)
7248 goto done;
7250 err = got_ref_resolve(&commit_id, repo, branch_ref);
7251 if (err)
7252 goto done;
7254 err = got_object_id_by_path(&tree_id, repo, commit_id,
7255 worktree->path_prefix);
7256 if (err)
7257 goto done;
7259 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7260 if (err)
7261 goto done;
7263 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7264 progress_cb, progress_arg, cancel_cb, cancel_arg);
7265 if (err)
7266 goto sync;
7268 err = got_ref_change_ref(base_branch_ref, commit_id);
7269 if (err)
7270 goto sync;
7272 err = got_ref_write(base_branch_ref, repo);
7273 if (err)
7274 goto sync;
7276 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7277 sync:
7278 sync_err = sync_fileindex(fileindex, fileindex_path);
7279 if (sync_err && err == NULL)
7280 err = sync_err;
7282 done:
7283 unlockerr = got_ref_unlock(branch_ref);
7284 if (unlockerr && err == NULL)
7285 err = unlockerr;
7286 got_ref_close(branch_ref);
7288 unlockerr = got_ref_unlock(base_branch_ref);
7289 if (unlockerr && err == NULL)
7290 err = unlockerr;
7291 got_ref_close(base_branch_ref);
7293 got_fileindex_free(fileindex);
7294 free(fileindex_path);
7295 free(tree_id);
7297 unlockerr = lock_worktree(worktree, LOCK_SH);
7298 if (unlockerr && err == NULL)
7299 err = unlockerr;
7300 return err;
7303 const struct got_error *
7304 got_worktree_integrate_abort(struct got_worktree *worktree,
7305 struct got_fileindex *fileindex, struct got_repository *repo,
7306 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7308 const struct got_error *err = NULL, *unlockerr = NULL;
7310 got_fileindex_free(fileindex);
7312 err = lock_worktree(worktree, LOCK_SH);
7314 unlockerr = got_ref_unlock(branch_ref);
7315 if (unlockerr && err == NULL)
7316 err = unlockerr;
7317 got_ref_close(branch_ref);
7319 unlockerr = got_ref_unlock(base_branch_ref);
7320 if (unlockerr && err == NULL)
7321 err = unlockerr;
7322 got_ref_close(base_branch_ref);
7324 return err;
7327 const struct got_error *
7328 got_worktree_merge_postpone(struct got_worktree *worktree,
7329 struct got_fileindex *fileindex)
7331 const struct got_error *err, *sync_err;
7332 char *fileindex_path = NULL;
7334 err = get_fileindex_path(&fileindex_path, worktree);
7335 if (err)
7336 goto done;
7338 sync_err = sync_fileindex(fileindex, fileindex_path);
7340 err = lock_worktree(worktree, LOCK_SH);
7341 if (sync_err && err == NULL)
7342 err = sync_err;
7343 done:
7344 got_fileindex_free(fileindex);
7345 free(fileindex_path);
7346 return err;
7349 static const struct got_error *
7350 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7352 const struct got_error *err;
7353 char *branch_refname = NULL, *commit_refname = NULL;
7355 err = get_merge_branch_ref_name(&branch_refname, worktree);
7356 if (err)
7357 goto done;
7358 err = delete_ref(branch_refname, repo);
7359 if (err)
7360 goto done;
7362 err = get_merge_commit_ref_name(&commit_refname, worktree);
7363 if (err)
7364 goto done;
7365 err = delete_ref(commit_refname, repo);
7366 if (err)
7367 goto done;
7369 done:
7370 free(branch_refname);
7371 free(commit_refname);
7372 return err;
7375 struct merge_commit_msg_arg {
7376 struct got_worktree *worktree;
7377 const char *branch_name;
7380 static const struct got_error *
7381 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7382 void *arg)
7384 struct merge_commit_msg_arg *a = arg;
7386 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7387 got_worktree_get_head_ref_name(a->worktree)) == -1)
7388 return got_error_from_errno("asprintf");
7390 return NULL;
7394 const struct got_error *
7395 got_worktree_merge_branch(struct got_worktree *worktree,
7396 struct got_fileindex *fileindex,
7397 struct got_object_id *yca_commit_id,
7398 struct got_object_id *branch_tip,
7399 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7400 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7402 const struct got_error *err;
7403 char *fileindex_path = NULL;
7405 err = get_fileindex_path(&fileindex_path, worktree);
7406 if (err)
7407 goto done;
7409 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7410 worktree);
7411 if (err)
7412 goto done;
7414 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7415 branch_tip, repo, progress_cb, progress_arg,
7416 cancel_cb, cancel_arg);
7417 done:
7418 free(fileindex_path);
7419 return err;
7422 const struct got_error *
7423 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7424 struct got_worktree *worktree, struct got_fileindex *fileindex,
7425 const char *author, const char *committer, int allow_bad_symlinks,
7426 struct got_object_id *branch_tip, const char *branch_name,
7427 struct got_repository *repo,
7428 got_worktree_status_cb status_cb, void *status_arg)
7431 const struct got_error *err = NULL, *sync_err;
7432 struct got_pathlist_head commitable_paths;
7433 struct collect_commitables_arg cc_arg;
7434 struct got_pathlist_entry *pe;
7435 struct got_reference *head_ref = NULL;
7436 struct got_object_id *head_commit_id = NULL;
7437 int have_staged_files = 0;
7438 struct merge_commit_msg_arg mcm_arg;
7439 char *fileindex_path = NULL;
7441 *new_commit_id = NULL;
7443 TAILQ_INIT(&commitable_paths);
7445 err = get_fileindex_path(&fileindex_path, worktree);
7446 if (err)
7447 goto done;
7449 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7450 if (err)
7451 goto done;
7453 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7454 if (err)
7455 goto done;
7457 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7458 &have_staged_files);
7459 if (err && err->code != GOT_ERR_CANCELLED)
7460 goto done;
7461 if (have_staged_files) {
7462 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7463 goto done;
7466 cc_arg.commitable_paths = &commitable_paths;
7467 cc_arg.worktree = worktree;
7468 cc_arg.fileindex = fileindex;
7469 cc_arg.repo = repo;
7470 cc_arg.have_staged_files = have_staged_files;
7471 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7472 err = worktree_status(worktree, "", fileindex, repo,
7473 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7474 if (err)
7475 goto done;
7477 if (TAILQ_EMPTY(&commitable_paths)) {
7478 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7479 "merge of %s cannot proceed", branch_name);
7480 goto done;
7483 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7484 struct got_commitable *ct = pe->data;
7485 const char *ct_path = ct->in_repo_path;
7487 while (ct_path[0] == '/')
7488 ct_path++;
7489 err = check_out_of_date(ct_path, ct->status,
7490 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7491 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7492 if (err)
7493 goto done;
7497 mcm_arg.worktree = worktree;
7498 mcm_arg.branch_name = branch_name;
7499 err = commit_worktree(new_commit_id, &commitable_paths,
7500 head_commit_id, branch_tip, worktree, author, committer,
7501 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7502 if (err)
7503 goto done;
7505 err = update_fileindex_after_commit(worktree, &commitable_paths,
7506 *new_commit_id, fileindex, have_staged_files);
7507 sync_err = sync_fileindex(fileindex, fileindex_path);
7508 if (sync_err && err == NULL)
7509 err = sync_err;
7510 done:
7511 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7512 struct got_commitable *ct = pe->data;
7513 free_commitable(ct);
7515 got_pathlist_free(&commitable_paths);
7516 free(fileindex_path);
7517 return err;
7520 const struct got_error *
7521 got_worktree_merge_complete(struct got_worktree *worktree,
7522 struct got_fileindex *fileindex, struct got_repository *repo)
7524 const struct got_error *err, *unlockerr, *sync_err;
7525 char *fileindex_path = NULL;
7527 err = delete_merge_refs(worktree, repo);
7528 if (err)
7529 goto done;
7531 err = get_fileindex_path(&fileindex_path, worktree);
7532 if (err)
7533 goto done;
7534 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7535 sync_err = sync_fileindex(fileindex, fileindex_path);
7536 if (sync_err && err == NULL)
7537 err = sync_err;
7538 done:
7539 got_fileindex_free(fileindex);
7540 free(fileindex_path);
7541 unlockerr = lock_worktree(worktree, LOCK_SH);
7542 if (unlockerr && err == NULL)
7543 err = unlockerr;
7544 return err;
7547 const struct got_error *
7548 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7549 struct got_repository *repo)
7551 const struct got_error *err;
7552 char *branch_refname = NULL;
7553 struct got_reference *branch_ref = NULL;
7555 *in_progress = 0;
7557 err = get_merge_branch_ref_name(&branch_refname, worktree);
7558 if (err)
7559 return err;
7560 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7561 free(branch_refname);
7562 if (err) {
7563 if (err->code != GOT_ERR_NOT_REF)
7564 return err;
7565 } else
7566 *in_progress = 1;
7568 return NULL;
7571 const struct got_error *got_worktree_merge_prepare(
7572 struct got_fileindex **fileindex, struct got_worktree *worktree,
7573 struct got_reference *branch, struct got_repository *repo)
7575 const struct got_error *err = NULL;
7576 char *fileindex_path = NULL;
7577 char *branch_refname = NULL, *commit_refname = NULL;
7578 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7579 struct got_reference *commit_ref = NULL;
7580 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7581 struct check_rebase_ok_arg ok_arg;
7583 *fileindex = NULL;
7585 err = lock_worktree(worktree, LOCK_EX);
7586 if (err)
7587 return err;
7589 err = open_fileindex(fileindex, &fileindex_path, worktree);
7590 if (err)
7591 goto done;
7593 /* Preconditions are the same as for rebase. */
7594 ok_arg.worktree = worktree;
7595 ok_arg.repo = repo;
7596 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7597 &ok_arg);
7598 if (err)
7599 goto done;
7601 err = get_merge_branch_ref_name(&branch_refname, worktree);
7602 if (err)
7603 return err;
7605 err = get_merge_commit_ref_name(&commit_refname, worktree);
7606 if (err)
7607 return err;
7609 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7610 0);
7611 if (err)
7612 goto done;
7614 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7615 if (err)
7616 goto done;
7618 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7619 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7620 goto done;
7623 err = got_ref_resolve(&branch_tip, repo, branch);
7624 if (err)
7625 goto done;
7627 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7628 if (err)
7629 goto done;
7630 err = got_ref_write(branch_ref, repo);
7631 if (err)
7632 goto done;
7634 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7635 if (err)
7636 goto done;
7637 err = got_ref_write(commit_ref, repo);
7638 if (err)
7639 goto done;
7641 done:
7642 free(branch_refname);
7643 free(commit_refname);
7644 free(fileindex_path);
7645 if (branch_ref)
7646 got_ref_close(branch_ref);
7647 if (commit_ref)
7648 got_ref_close(commit_ref);
7649 if (wt_branch)
7650 got_ref_close(wt_branch);
7651 free(wt_branch_tip);
7652 if (err) {
7653 if (*fileindex) {
7654 got_fileindex_free(*fileindex);
7655 *fileindex = NULL;
7657 lock_worktree(worktree, LOCK_SH);
7659 return err;
7662 const struct got_error *
7663 got_worktree_merge_continue(char **branch_name,
7664 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7665 struct got_worktree *worktree, struct got_repository *repo)
7667 const struct got_error *err;
7668 char *commit_refname = NULL, *branch_refname = NULL;
7669 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7670 char *fileindex_path = NULL;
7671 int have_staged_files = 0;
7673 *branch_name = NULL;
7674 *branch_tip = NULL;
7675 *fileindex = NULL;
7677 err = lock_worktree(worktree, LOCK_EX);
7678 if (err)
7679 return err;
7681 err = open_fileindex(fileindex, &fileindex_path, worktree);
7682 if (err)
7683 goto done;
7685 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7686 &have_staged_files);
7687 if (err && err->code != GOT_ERR_CANCELLED)
7688 goto done;
7689 if (have_staged_files) {
7690 err = got_error(GOT_ERR_STAGED_PATHS);
7691 goto done;
7694 err = get_merge_branch_ref_name(&branch_refname, worktree);
7695 if (err)
7696 goto done;
7698 err = get_merge_commit_ref_name(&commit_refname, worktree);
7699 if (err)
7700 goto done;
7702 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7703 if (err)
7704 goto done;
7706 if (!got_ref_is_symbolic(branch_ref)) {
7707 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7708 "%s is not a symbolic reference",
7709 got_ref_get_name(branch_ref));
7710 goto done;
7712 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7713 if (*branch_name == NULL) {
7714 err = got_error_from_errno("strdup");
7715 goto done;
7718 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7719 if (err)
7720 goto done;
7722 err = got_ref_resolve(branch_tip, repo, commit_ref);
7723 if (err)
7724 goto done;
7725 done:
7726 free(commit_refname);
7727 free(branch_refname);
7728 free(fileindex_path);
7729 if (commit_ref)
7730 got_ref_close(commit_ref);
7731 if (branch_ref)
7732 got_ref_close(branch_ref);
7733 if (err) {
7734 if (*branch_name) {
7735 free(*branch_name);
7736 *branch_name = NULL;
7738 free(*branch_tip);
7739 *branch_tip = NULL;
7740 if (*fileindex) {
7741 got_fileindex_free(*fileindex);
7742 *fileindex = NULL;
7744 lock_worktree(worktree, LOCK_SH);
7746 return err;
7749 const struct got_error *
7750 got_worktree_merge_abort(struct got_worktree *worktree,
7751 struct got_fileindex *fileindex, struct got_repository *repo,
7752 got_worktree_checkout_cb progress_cb, void *progress_arg)
7754 const struct got_error *err, *unlockerr, *sync_err;
7755 struct got_object_id *commit_id = NULL;
7756 char *fileindex_path = NULL;
7757 struct revert_file_args rfa;
7758 struct got_object_id *tree_id = NULL;
7760 err = got_object_id_by_path(&tree_id, repo,
7761 worktree->base_commit_id, worktree->path_prefix);
7762 if (err)
7763 goto done;
7765 err = delete_merge_refs(worktree, repo);
7766 if (err)
7767 goto done;
7769 err = get_fileindex_path(&fileindex_path, worktree);
7770 if (err)
7771 goto done;
7773 rfa.worktree = worktree;
7774 rfa.fileindex = fileindex;
7775 rfa.progress_cb = progress_cb;
7776 rfa.progress_arg = progress_arg;
7777 rfa.patch_cb = NULL;
7778 rfa.patch_arg = NULL;
7779 rfa.repo = repo;
7780 rfa.unlink_added_files = 1;
7781 err = worktree_status(worktree, "", fileindex, repo,
7782 revert_file, &rfa, NULL, NULL, 1, 0);
7783 if (err)
7784 goto sync;
7786 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7787 repo, progress_cb, progress_arg, NULL, NULL);
7788 sync:
7789 sync_err = sync_fileindex(fileindex, fileindex_path);
7790 if (sync_err && err == NULL)
7791 err = sync_err;
7792 done:
7793 free(tree_id);
7794 free(commit_id);
7795 if (fileindex)
7796 got_fileindex_free(fileindex);
7797 free(fileindex_path);
7799 unlockerr = lock_worktree(worktree, LOCK_SH);
7800 if (unlockerr && err == NULL)
7801 err = unlockerr;
7802 return err;
7805 struct check_stage_ok_arg {
7806 struct got_object_id *head_commit_id;
7807 struct got_worktree *worktree;
7808 struct got_fileindex *fileindex;
7809 struct got_repository *repo;
7810 int have_changes;
7813 const struct got_error *
7814 check_stage_ok(void *arg, unsigned char status,
7815 unsigned char staged_status, const char *relpath,
7816 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7817 struct got_object_id *commit_id, int dirfd, const char *de_name)
7819 struct check_stage_ok_arg *a = arg;
7820 const struct got_error *err = NULL;
7821 struct got_fileindex_entry *ie;
7822 struct got_object_id base_commit_id;
7823 struct got_object_id *base_commit_idp = NULL;
7824 char *in_repo_path = NULL, *p;
7826 if (status == GOT_STATUS_UNVERSIONED ||
7827 status == GOT_STATUS_NO_CHANGE)
7828 return NULL;
7829 if (status == GOT_STATUS_NONEXISTENT)
7830 return got_error_set_errno(ENOENT, relpath);
7832 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7833 if (ie == NULL)
7834 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7836 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7837 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7838 relpath) == -1)
7839 return got_error_from_errno("asprintf");
7841 if (got_fileindex_entry_has_commit(ie)) {
7842 memcpy(base_commit_id.sha1, ie->commit_sha1,
7843 SHA1_DIGEST_LENGTH);
7844 base_commit_idp = &base_commit_id;
7847 if (status == GOT_STATUS_CONFLICT) {
7848 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7849 goto done;
7850 } else if (status != GOT_STATUS_ADD &&
7851 status != GOT_STATUS_MODIFY &&
7852 status != GOT_STATUS_DELETE) {
7853 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7854 goto done;
7857 a->have_changes = 1;
7859 p = in_repo_path;
7860 while (p[0] == '/')
7861 p++;
7862 err = check_out_of_date(p, status, staged_status,
7863 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7864 GOT_ERR_STAGE_OUT_OF_DATE);
7865 done:
7866 free(in_repo_path);
7867 return err;
7870 struct stage_path_arg {
7871 struct got_worktree *worktree;
7872 struct got_fileindex *fileindex;
7873 struct got_repository *repo;
7874 got_worktree_status_cb status_cb;
7875 void *status_arg;
7876 got_worktree_patch_cb patch_cb;
7877 void *patch_arg;
7878 int staged_something;
7879 int allow_bad_symlinks;
7882 static const struct got_error *
7883 stage_path(void *arg, unsigned char status,
7884 unsigned char staged_status, const char *relpath,
7885 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7886 struct got_object_id *commit_id, int dirfd, const char *de_name)
7888 struct stage_path_arg *a = arg;
7889 const struct got_error *err = NULL;
7890 struct got_fileindex_entry *ie;
7891 char *ondisk_path = NULL, *path_content = NULL;
7892 uint32_t stage;
7893 struct got_object_id *new_staged_blob_id = NULL;
7894 struct stat sb;
7896 if (status == GOT_STATUS_UNVERSIONED)
7897 return NULL;
7899 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7900 if (ie == NULL)
7901 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7903 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7904 relpath)== -1)
7905 return got_error_from_errno("asprintf");
7907 switch (status) {
7908 case GOT_STATUS_ADD:
7909 case GOT_STATUS_MODIFY:
7910 /* XXX could sb.st_mode be passed in by our caller? */
7911 if (lstat(ondisk_path, &sb) == -1) {
7912 err = got_error_from_errno2("lstat", ondisk_path);
7913 break;
7915 if (a->patch_cb) {
7916 if (status == GOT_STATUS_ADD) {
7917 int choice = GOT_PATCH_CHOICE_NONE;
7918 err = (*a->patch_cb)(&choice, a->patch_arg,
7919 status, ie->path, NULL, 1, 1);
7920 if (err)
7921 break;
7922 if (choice != GOT_PATCH_CHOICE_YES)
7923 break;
7924 } else {
7925 err = create_patched_content(&path_content, 0,
7926 staged_blob_id ? staged_blob_id : blob_id,
7927 ondisk_path, dirfd, de_name, ie->path,
7928 a->repo, a->patch_cb, a->patch_arg);
7929 if (err || path_content == NULL)
7930 break;
7933 err = got_object_blob_create(&new_staged_blob_id,
7934 path_content ? path_content : ondisk_path, a->repo);
7935 if (err)
7936 break;
7937 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7938 SHA1_DIGEST_LENGTH);
7939 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7940 stage = GOT_FILEIDX_STAGE_ADD;
7941 else
7942 stage = GOT_FILEIDX_STAGE_MODIFY;
7943 got_fileindex_entry_stage_set(ie, stage);
7944 if (S_ISLNK(sb.st_mode)) {
7945 int is_bad_symlink = 0;
7946 if (!a->allow_bad_symlinks) {
7947 char target_path[PATH_MAX];
7948 ssize_t target_len;
7949 target_len = readlink(ondisk_path, target_path,
7950 sizeof(target_path));
7951 if (target_len == -1) {
7952 err = got_error_from_errno2("readlink",
7953 ondisk_path);
7954 break;
7956 err = is_bad_symlink_target(&is_bad_symlink,
7957 target_path, target_len, ondisk_path,
7958 a->worktree->root_path);
7959 if (err)
7960 break;
7961 if (is_bad_symlink) {
7962 err = got_error_path(ondisk_path,
7963 GOT_ERR_BAD_SYMLINK);
7964 break;
7967 if (is_bad_symlink)
7968 got_fileindex_entry_staged_filetype_set(ie,
7969 GOT_FILEIDX_MODE_BAD_SYMLINK);
7970 else
7971 got_fileindex_entry_staged_filetype_set(ie,
7972 GOT_FILEIDX_MODE_SYMLINK);
7973 } else {
7974 got_fileindex_entry_staged_filetype_set(ie,
7975 GOT_FILEIDX_MODE_REGULAR_FILE);
7977 a->staged_something = 1;
7978 if (a->status_cb == NULL)
7979 break;
7980 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7981 get_staged_status(ie), relpath, blob_id,
7982 new_staged_blob_id, NULL, dirfd, de_name);
7983 break;
7984 case GOT_STATUS_DELETE:
7985 if (staged_status == GOT_STATUS_DELETE)
7986 break;
7987 if (a->patch_cb) {
7988 int choice = GOT_PATCH_CHOICE_NONE;
7989 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7990 ie->path, NULL, 1, 1);
7991 if (err)
7992 break;
7993 if (choice == GOT_PATCH_CHOICE_NO)
7994 break;
7995 if (choice != GOT_PATCH_CHOICE_YES) {
7996 err = got_error(GOT_ERR_PATCH_CHOICE);
7997 break;
8000 stage = GOT_FILEIDX_STAGE_DELETE;
8001 got_fileindex_entry_stage_set(ie, stage);
8002 a->staged_something = 1;
8003 if (a->status_cb == NULL)
8004 break;
8005 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8006 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8007 de_name);
8008 break;
8009 case GOT_STATUS_NO_CHANGE:
8010 break;
8011 case GOT_STATUS_CONFLICT:
8012 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8013 break;
8014 case GOT_STATUS_NONEXISTENT:
8015 err = got_error_set_errno(ENOENT, relpath);
8016 break;
8017 default:
8018 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8019 break;
8022 if (path_content && unlink(path_content) == -1 && err == NULL)
8023 err = got_error_from_errno2("unlink", path_content);
8024 free(path_content);
8025 free(ondisk_path);
8026 free(new_staged_blob_id);
8027 return err;
8030 const struct got_error *
8031 got_worktree_stage(struct got_worktree *worktree,
8032 struct got_pathlist_head *paths,
8033 got_worktree_status_cb status_cb, void *status_arg,
8034 got_worktree_patch_cb patch_cb, void *patch_arg,
8035 int allow_bad_symlinks, struct got_repository *repo)
8037 const struct got_error *err = NULL, *sync_err, *unlockerr;
8038 struct got_pathlist_entry *pe;
8039 struct got_fileindex *fileindex = NULL;
8040 char *fileindex_path = NULL;
8041 struct got_reference *head_ref = NULL;
8042 struct got_object_id *head_commit_id = NULL;
8043 struct check_stage_ok_arg oka;
8044 struct stage_path_arg spa;
8046 err = lock_worktree(worktree, LOCK_EX);
8047 if (err)
8048 return err;
8050 err = got_ref_open(&head_ref, repo,
8051 got_worktree_get_head_ref_name(worktree), 0);
8052 if (err)
8053 goto done;
8054 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8055 if (err)
8056 goto done;
8057 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8058 if (err)
8059 goto done;
8061 /* Check pre-conditions before staging anything. */
8062 oka.head_commit_id = head_commit_id;
8063 oka.worktree = worktree;
8064 oka.fileindex = fileindex;
8065 oka.repo = repo;
8066 oka.have_changes = 0;
8067 TAILQ_FOREACH(pe, paths, entry) {
8068 err = worktree_status(worktree, pe->path, fileindex, repo,
8069 check_stage_ok, &oka, NULL, NULL, 1, 0);
8070 if (err)
8071 goto done;
8073 if (!oka.have_changes) {
8074 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8075 goto done;
8078 spa.worktree = worktree;
8079 spa.fileindex = fileindex;
8080 spa.repo = repo;
8081 spa.patch_cb = patch_cb;
8082 spa.patch_arg = patch_arg;
8083 spa.status_cb = status_cb;
8084 spa.status_arg = status_arg;
8085 spa.staged_something = 0;
8086 spa.allow_bad_symlinks = allow_bad_symlinks;
8087 TAILQ_FOREACH(pe, paths, entry) {
8088 err = worktree_status(worktree, pe->path, fileindex, repo,
8089 stage_path, &spa, NULL, NULL, 1, 0);
8090 if (err)
8091 goto done;
8093 if (!spa.staged_something) {
8094 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8095 goto done;
8098 sync_err = sync_fileindex(fileindex, fileindex_path);
8099 if (sync_err && err == NULL)
8100 err = sync_err;
8101 done:
8102 if (head_ref)
8103 got_ref_close(head_ref);
8104 free(head_commit_id);
8105 free(fileindex_path);
8106 if (fileindex)
8107 got_fileindex_free(fileindex);
8108 unlockerr = lock_worktree(worktree, LOCK_SH);
8109 if (unlockerr && err == NULL)
8110 err = unlockerr;
8111 return err;
8114 struct unstage_path_arg {
8115 struct got_worktree *worktree;
8116 struct got_fileindex *fileindex;
8117 struct got_repository *repo;
8118 got_worktree_checkout_cb progress_cb;
8119 void *progress_arg;
8120 got_worktree_patch_cb patch_cb;
8121 void *patch_arg;
8124 static const struct got_error *
8125 create_unstaged_content(char **path_unstaged_content,
8126 char **path_new_staged_content, struct got_object_id *blob_id,
8127 struct got_object_id *staged_blob_id, const char *relpath,
8128 struct got_repository *repo,
8129 got_worktree_patch_cb patch_cb, void *patch_arg)
8131 const struct got_error *err, *free_err;
8132 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8133 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8134 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8135 struct got_diffreg_result *diffreg_result = NULL;
8136 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8137 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8139 *path_unstaged_content = NULL;
8140 *path_new_staged_content = NULL;
8142 err = got_object_id_str(&label1, blob_id);
8143 if (err)
8144 return err;
8145 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8146 if (err)
8147 goto done;
8149 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8150 if (err)
8151 goto done;
8153 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8154 if (err)
8155 goto done;
8157 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8158 if (err)
8159 goto done;
8161 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8162 if (err)
8163 goto done;
8165 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8166 if (err)
8167 goto done;
8169 err = got_diff_files(&diffreg_result, f1, label1, f2,
8170 path2, 3, 0, 1, NULL);
8171 if (err)
8172 goto done;
8174 err = got_opentemp_named(path_unstaged_content, &outfile,
8175 "got-unstaged-content");
8176 if (err)
8177 goto done;
8178 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8179 "got-new-staged-content");
8180 if (err)
8181 goto done;
8183 if (fseek(f1, 0L, SEEK_SET) == -1) {
8184 err = got_ferror(f1, GOT_ERR_IO);
8185 goto done;
8187 if (fseek(f2, 0L, SEEK_SET) == -1) {
8188 err = got_ferror(f2, GOT_ERR_IO);
8189 goto done;
8191 /* Count the number of actual changes in the diff result. */
8192 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8193 struct diff_chunk_context cc = {};
8194 diff_chunk_context_load_change(&cc, &nchunks_used,
8195 diffreg_result->result, n, 0);
8196 nchanges++;
8198 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8199 int choice;
8200 err = apply_or_reject_change(&choice, &nchunks_used,
8201 diffreg_result->result, n, relpath, f1, f2,
8202 &line_cur1, &line_cur2,
8203 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8204 if (err)
8205 goto done;
8206 if (choice == GOT_PATCH_CHOICE_YES)
8207 have_content = 1;
8208 else
8209 have_rejected_content = 1;
8210 if (choice == GOT_PATCH_CHOICE_QUIT)
8211 break;
8213 if (have_content || have_rejected_content)
8214 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8215 outfile, rejectfile);
8216 done:
8217 free(label1);
8218 if (blob)
8219 got_object_blob_close(blob);
8220 if (staged_blob)
8221 got_object_blob_close(staged_blob);
8222 free_err = got_diffreg_result_free(diffreg_result);
8223 if (free_err && err == NULL)
8224 err = free_err;
8225 if (f1 && fclose(f1) == EOF && err == NULL)
8226 err = got_error_from_errno2("fclose", path1);
8227 if (f2 && fclose(f2) == EOF && err == NULL)
8228 err = got_error_from_errno2("fclose", path2);
8229 if (outfile && fclose(outfile) == EOF && err == NULL)
8230 err = got_error_from_errno2("fclose", *path_unstaged_content);
8231 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8232 err = got_error_from_errno2("fclose", *path_new_staged_content);
8233 if (path1 && unlink(path1) == -1 && err == NULL)
8234 err = got_error_from_errno2("unlink", path1);
8235 if (path2 && unlink(path2) == -1 && err == NULL)
8236 err = got_error_from_errno2("unlink", path2);
8237 if (err || !have_content) {
8238 if (*path_unstaged_content &&
8239 unlink(*path_unstaged_content) == -1 && err == NULL)
8240 err = got_error_from_errno2("unlink",
8241 *path_unstaged_content);
8242 free(*path_unstaged_content);
8243 *path_unstaged_content = NULL;
8245 if (err || !have_content || !have_rejected_content) {
8246 if (*path_new_staged_content &&
8247 unlink(*path_new_staged_content) == -1 && err == NULL)
8248 err = got_error_from_errno2("unlink",
8249 *path_new_staged_content);
8250 free(*path_new_staged_content);
8251 *path_new_staged_content = NULL;
8253 free(path1);
8254 free(path2);
8255 return err;
8258 static const struct got_error *
8259 unstage_hunks(struct got_object_id *staged_blob_id,
8260 struct got_blob_object *blob_base,
8261 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8262 const char *ondisk_path, const char *label_orig,
8263 struct got_worktree *worktree, struct got_repository *repo,
8264 got_worktree_patch_cb patch_cb, void *patch_arg,
8265 got_worktree_checkout_cb progress_cb, void *progress_arg)
8267 const struct got_error *err = NULL;
8268 char *path_unstaged_content = NULL;
8269 char *path_new_staged_content = NULL;
8270 char *parent = NULL, *base_path = NULL;
8271 char *blob_base_path = NULL;
8272 struct got_object_id *new_staged_blob_id = NULL;
8273 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8274 struct stat sb;
8276 err = create_unstaged_content(&path_unstaged_content,
8277 &path_new_staged_content, blob_id, staged_blob_id,
8278 ie->path, repo, patch_cb, patch_arg);
8279 if (err)
8280 return err;
8282 if (path_unstaged_content == NULL)
8283 return NULL;
8285 if (path_new_staged_content) {
8286 err = got_object_blob_create(&new_staged_blob_id,
8287 path_new_staged_content, repo);
8288 if (err)
8289 goto done;
8292 f = fopen(path_unstaged_content, "re");
8293 if (f == NULL) {
8294 err = got_error_from_errno2("fopen",
8295 path_unstaged_content);
8296 goto done;
8298 if (fstat(fileno(f), &sb) == -1) {
8299 err = got_error_from_errno2("fstat", path_unstaged_content);
8300 goto done;
8302 if (got_fileindex_entry_staged_filetype_get(ie) ==
8303 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8304 char link_target[PATH_MAX];
8305 size_t r;
8306 r = fread(link_target, 1, sizeof(link_target), f);
8307 if (r == 0 && ferror(f)) {
8308 err = got_error_from_errno("fread");
8309 goto done;
8311 if (r >= sizeof(link_target)) { /* should not happen */
8312 err = got_error(GOT_ERR_NO_SPACE);
8313 goto done;
8315 link_target[r] = '\0';
8316 err = merge_symlink(worktree, blob_base,
8317 ondisk_path, ie->path, label_orig, link_target,
8318 worktree->base_commit_id, repo, progress_cb,
8319 progress_arg);
8320 } else {
8321 int local_changes_subsumed;
8323 err = got_path_dirname(&parent, ondisk_path);
8324 if (err)
8325 return err;
8327 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8328 parent) == -1) {
8329 err = got_error_from_errno("asprintf");
8330 base_path = NULL;
8331 goto done;
8334 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8335 if (err)
8336 goto done;
8337 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8338 blob_base);
8339 if (err)
8340 goto done;
8343 * In order the run a 3-way merge with a symlink we copy the symlink's
8344 * target path into a temporary file and use that file with diff3.
8346 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8347 err = dump_symlink_target_path_to_file(&f_deriv2,
8348 ondisk_path);
8349 if (err)
8350 goto done;
8351 } else {
8352 int fd;
8353 fd = open(ondisk_path,
8354 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8355 if (fd == -1) {
8356 err = got_error_from_errno2("open", ondisk_path);
8357 goto done;
8359 f_deriv2 = fdopen(fd, "r");
8360 if (f_deriv2 == NULL) {
8361 err = got_error_from_errno2("fdopen", ondisk_path);
8362 close(fd);
8363 goto done;
8367 err = merge_file(&local_changes_subsumed, worktree,
8368 f_base, f, f_deriv2, ondisk_path, ie->path,
8369 got_fileindex_perms_to_st(ie),
8370 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8371 repo, progress_cb, progress_arg);
8373 if (err)
8374 goto done;
8376 if (new_staged_blob_id) {
8377 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8378 SHA1_DIGEST_LENGTH);
8379 } else {
8380 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8381 got_fileindex_entry_staged_filetype_set(ie, 0);
8383 done:
8384 free(new_staged_blob_id);
8385 if (path_unstaged_content &&
8386 unlink(path_unstaged_content) == -1 && err == NULL)
8387 err = got_error_from_errno2("unlink", path_unstaged_content);
8388 if (path_new_staged_content &&
8389 unlink(path_new_staged_content) == -1 && err == NULL)
8390 err = got_error_from_errno2("unlink", path_new_staged_content);
8391 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8392 err = got_error_from_errno2("unlink", blob_base_path);
8393 if (f_base && fclose(f_base) == EOF && err == NULL)
8394 err = got_error_from_errno2("fclose", path_unstaged_content);
8395 if (f && fclose(f) == EOF && err == NULL)
8396 err = got_error_from_errno2("fclose", path_unstaged_content);
8397 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8398 err = got_error_from_errno2("fclose", ondisk_path);
8399 free(path_unstaged_content);
8400 free(path_new_staged_content);
8401 free(blob_base_path);
8402 free(parent);
8403 free(base_path);
8404 return err;
8407 static const struct got_error *
8408 unstage_path(void *arg, unsigned char status,
8409 unsigned char staged_status, const char *relpath,
8410 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8411 struct got_object_id *commit_id, int dirfd, const char *de_name)
8413 const struct got_error *err = NULL;
8414 struct unstage_path_arg *a = arg;
8415 struct got_fileindex_entry *ie;
8416 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8417 char *ondisk_path = NULL;
8418 char *id_str = NULL, *label_orig = NULL;
8419 int local_changes_subsumed;
8420 struct stat sb;
8422 if (staged_status != GOT_STATUS_ADD &&
8423 staged_status != GOT_STATUS_MODIFY &&
8424 staged_status != GOT_STATUS_DELETE)
8425 return NULL;
8427 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8428 if (ie == NULL)
8429 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8431 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8432 == -1)
8433 return got_error_from_errno("asprintf");
8435 err = got_object_id_str(&id_str,
8436 commit_id ? commit_id : a->worktree->base_commit_id);
8437 if (err)
8438 goto done;
8439 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8440 id_str) == -1) {
8441 err = got_error_from_errno("asprintf");
8442 goto done;
8445 switch (staged_status) {
8446 case GOT_STATUS_MODIFY:
8447 err = got_object_open_as_blob(&blob_base, a->repo,
8448 blob_id, 8192);
8449 if (err)
8450 break;
8451 /* fall through */
8452 case GOT_STATUS_ADD:
8453 if (a->patch_cb) {
8454 if (staged_status == GOT_STATUS_ADD) {
8455 int choice = GOT_PATCH_CHOICE_NONE;
8456 err = (*a->patch_cb)(&choice, a->patch_arg,
8457 staged_status, ie->path, NULL, 1, 1);
8458 if (err)
8459 break;
8460 if (choice != GOT_PATCH_CHOICE_YES)
8461 break;
8462 } else {
8463 err = unstage_hunks(staged_blob_id,
8464 blob_base, blob_id, ie, ondisk_path,
8465 label_orig, a->worktree, a->repo,
8466 a->patch_cb, a->patch_arg,
8467 a->progress_cb, a->progress_arg);
8468 break; /* Done with this file. */
8471 err = got_object_open_as_blob(&blob_staged, a->repo,
8472 staged_blob_id, 8192);
8473 if (err)
8474 break;
8475 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8476 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8477 case GOT_FILEIDX_MODE_REGULAR_FILE:
8478 err = merge_blob(&local_changes_subsumed, a->worktree,
8479 blob_base, ondisk_path, relpath,
8480 got_fileindex_perms_to_st(ie), label_orig,
8481 blob_staged, commit_id ? commit_id :
8482 a->worktree->base_commit_id, a->repo,
8483 a->progress_cb, a->progress_arg);
8484 break;
8485 case GOT_FILEIDX_MODE_SYMLINK:
8486 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8487 char *staged_target;
8488 err = got_object_blob_read_to_str(
8489 &staged_target, blob_staged);
8490 if (err)
8491 goto done;
8492 err = merge_symlink(a->worktree, blob_base,
8493 ondisk_path, relpath, label_orig,
8494 staged_target, commit_id ? commit_id :
8495 a->worktree->base_commit_id,
8496 a->repo, a->progress_cb, a->progress_arg);
8497 free(staged_target);
8498 } else {
8499 err = merge_blob(&local_changes_subsumed,
8500 a->worktree, blob_base, ondisk_path,
8501 relpath, got_fileindex_perms_to_st(ie),
8502 label_orig, blob_staged,
8503 commit_id ? commit_id :
8504 a->worktree->base_commit_id, a->repo,
8505 a->progress_cb, a->progress_arg);
8507 break;
8508 default:
8509 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8510 break;
8512 if (err == NULL) {
8513 got_fileindex_entry_stage_set(ie,
8514 GOT_FILEIDX_STAGE_NONE);
8515 got_fileindex_entry_staged_filetype_set(ie, 0);
8517 break;
8518 case GOT_STATUS_DELETE:
8519 if (a->patch_cb) {
8520 int choice = GOT_PATCH_CHOICE_NONE;
8521 err = (*a->patch_cb)(&choice, a->patch_arg,
8522 staged_status, ie->path, NULL, 1, 1);
8523 if (err)
8524 break;
8525 if (choice == GOT_PATCH_CHOICE_NO)
8526 break;
8527 if (choice != GOT_PATCH_CHOICE_YES) {
8528 err = got_error(GOT_ERR_PATCH_CHOICE);
8529 break;
8532 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8533 got_fileindex_entry_staged_filetype_set(ie, 0);
8534 err = get_file_status(&status, &sb, ie, ondisk_path,
8535 dirfd, de_name, a->repo);
8536 if (err)
8537 break;
8538 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8539 break;
8541 done:
8542 free(ondisk_path);
8543 if (blob_base)
8544 got_object_blob_close(blob_base);
8545 if (blob_staged)
8546 got_object_blob_close(blob_staged);
8547 free(id_str);
8548 free(label_orig);
8549 return err;
8552 const struct got_error *
8553 got_worktree_unstage(struct got_worktree *worktree,
8554 struct got_pathlist_head *paths,
8555 got_worktree_checkout_cb progress_cb, void *progress_arg,
8556 got_worktree_patch_cb patch_cb, void *patch_arg,
8557 struct got_repository *repo)
8559 const struct got_error *err = NULL, *sync_err, *unlockerr;
8560 struct got_pathlist_entry *pe;
8561 struct got_fileindex *fileindex = NULL;
8562 char *fileindex_path = NULL;
8563 struct unstage_path_arg upa;
8565 err = lock_worktree(worktree, LOCK_EX);
8566 if (err)
8567 return err;
8569 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8570 if (err)
8571 goto done;
8573 upa.worktree = worktree;
8574 upa.fileindex = fileindex;
8575 upa.repo = repo;
8576 upa.progress_cb = progress_cb;
8577 upa.progress_arg = progress_arg;
8578 upa.patch_cb = patch_cb;
8579 upa.patch_arg = patch_arg;
8580 TAILQ_FOREACH(pe, paths, entry) {
8581 err = worktree_status(worktree, pe->path, fileindex, repo,
8582 unstage_path, &upa, NULL, NULL, 1, 0);
8583 if (err)
8584 goto done;
8587 sync_err = sync_fileindex(fileindex, fileindex_path);
8588 if (sync_err && err == NULL)
8589 err = sync_err;
8590 done:
8591 free(fileindex_path);
8592 if (fileindex)
8593 got_fileindex_free(fileindex);
8594 unlockerr = lock_worktree(worktree, LOCK_SH);
8595 if (unlockerr && err == NULL)
8596 err = unlockerr;
8597 return err;
8600 struct report_file_info_arg {
8601 struct got_worktree *worktree;
8602 got_worktree_path_info_cb info_cb;
8603 void *info_arg;
8604 struct got_pathlist_head *paths;
8605 got_cancel_cb cancel_cb;
8606 void *cancel_arg;
8609 static const struct got_error *
8610 report_file_info(void *arg, struct got_fileindex_entry *ie)
8612 struct report_file_info_arg *a = arg;
8613 struct got_pathlist_entry *pe;
8614 struct got_object_id blob_id, staged_blob_id, commit_id;
8615 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8616 struct got_object_id *commit_idp = NULL;
8617 int stage;
8619 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8620 return got_error(GOT_ERR_CANCELLED);
8622 TAILQ_FOREACH(pe, a->paths, entry) {
8623 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8624 got_path_is_child(ie->path, pe->path, pe->path_len))
8625 break;
8627 if (pe == NULL) /* not found */
8628 return NULL;
8630 if (got_fileindex_entry_has_blob(ie)) {
8631 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8632 blob_idp = &blob_id;
8634 stage = got_fileindex_entry_stage_get(ie);
8635 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8636 stage == GOT_FILEIDX_STAGE_ADD) {
8637 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8638 SHA1_DIGEST_LENGTH);
8639 staged_blob_idp = &staged_blob_id;
8642 if (got_fileindex_entry_has_commit(ie)) {
8643 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8644 commit_idp = &commit_id;
8647 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8648 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8651 const struct got_error *
8652 got_worktree_path_info(struct got_worktree *worktree,
8653 struct got_pathlist_head *paths,
8654 got_worktree_path_info_cb info_cb, void *info_arg,
8655 got_cancel_cb cancel_cb, void *cancel_arg)
8658 const struct got_error *err = NULL, *unlockerr;
8659 struct got_fileindex *fileindex = NULL;
8660 char *fileindex_path = NULL;
8661 struct report_file_info_arg arg;
8663 err = lock_worktree(worktree, LOCK_SH);
8664 if (err)
8665 return err;
8667 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8668 if (err)
8669 goto done;
8671 arg.worktree = worktree;
8672 arg.info_cb = info_cb;
8673 arg.info_arg = info_arg;
8674 arg.paths = paths;
8675 arg.cancel_cb = cancel_cb;
8676 arg.cancel_arg = cancel_arg;
8677 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8678 &arg);
8679 done:
8680 free(fileindex_path);
8681 if (fileindex)
8682 got_fileindex_free(fileindex);
8683 unlockerr = lock_worktree(worktree, LOCK_UN);
8684 if (unlockerr && err == NULL)
8685 err = unlockerr;
8686 return err;