Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
19 #include <dirent.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <zlib.h>
30 #include <fnmatch.h>
31 #include <libgen.h>
32 #include <uuid.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static const struct got_error *
66 create_meta_file(const char *path_got, const char *name, const char *content)
67 {
68 const struct got_error *err = NULL;
69 char *path;
71 if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 return got_error_from_errno("asprintf");
74 err = got_path_create_file(path, content);
75 free(path);
76 return err;
77 }
79 static const struct got_error *
80 update_meta_file(const char *path_got, const char *name, const char *content)
81 {
82 const struct got_error *err = NULL;
83 FILE *tmpfile = NULL;
84 char *tmppath = NULL;
85 char *path = NULL;
87 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 err = got_error_from_errno("asprintf");
89 path = NULL;
90 goto done;
91 }
93 err = got_opentemp_named(&tmppath, &tmpfile, path);
94 if (err)
95 goto done;
97 if (content) {
98 int len = fprintf(tmpfile, "%s\n", content);
99 if (len != strlen(content) + 1) {
100 err = got_error_from_errno2("fprintf", tmppath);
101 goto done;
105 if (rename(tmppath, path) != 0) {
106 err = got_error_from_errno3("rename", tmppath, path);
107 unlink(tmppath);
108 goto done;
111 done:
112 if (fclose(tmpfile) == EOF && err == NULL)
113 err = got_error_from_errno2("fclose", tmppath);
114 free(tmppath);
115 return err;
118 static const struct got_error *
119 write_head_ref(const char *path_got, struct got_reference *head_ref)
121 const struct got_error *err = NULL;
122 char *refstr = NULL;
124 if (got_ref_is_symbolic(head_ref)) {
125 refstr = got_ref_to_str(head_ref);
126 if (refstr == NULL)
127 return got_error_from_errno("got_ref_to_str");
128 } else {
129 refstr = strdup(got_ref_get_name(head_ref));
130 if (refstr == NULL)
131 return got_error_from_errno("strdup");
133 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
134 free(refstr);
135 return err;
138 const struct got_error *
139 got_worktree_init(const char *path, struct got_reference *head_ref,
140 const char *prefix, struct got_repository *repo)
142 const struct got_error *err = NULL;
143 struct got_object_id *commit_id = NULL;
144 uuid_t uuid;
145 uint32_t uuid_status;
146 int obj_type;
147 char *path_got = NULL;
148 char *formatstr = NULL;
149 char *absprefix = NULL;
150 char *basestr = NULL;
151 char *uuidstr = NULL;
153 if (strcmp(path, got_repo_get_path(repo)) == 0) {
154 err = got_error(GOT_ERR_WORKTREE_REPO);
155 goto done;
158 err = got_ref_resolve(&commit_id, repo, head_ref);
159 if (err)
160 return err;
161 err = got_object_get_type(&obj_type, repo, commit_id);
162 if (err)
163 return err;
164 if (obj_type != GOT_OBJ_TYPE_COMMIT)
165 return got_error(GOT_ERR_OBJ_TYPE);
167 if (!got_path_is_absolute(prefix)) {
168 if (asprintf(&absprefix, "/%s", prefix) == -1)
169 return got_error_from_errno("asprintf");
172 /* Create top-level directory (may already exist). */
173 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
174 err = got_error_from_errno2("mkdir", path);
175 goto done;
178 /* Create .got directory (may already exist). */
179 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
180 err = got_error_from_errno("asprintf");
181 goto done;
183 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path_got);
185 goto done;
188 /* Create an empty lock file. */
189 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
190 if (err)
191 goto done;
193 /* Create an empty file index. */
194 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
195 if (err)
196 goto done;
198 /* Write the HEAD reference. */
199 err = write_head_ref(path_got, head_ref);
200 if (err)
201 goto done;
203 /* Record our base commit. */
204 err = got_object_id_str(&basestr, commit_id);
205 if (err)
206 goto done;
207 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
208 if (err)
209 goto done;
211 /* Store path to repository. */
212 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
213 got_repo_get_path(repo));
214 if (err)
215 goto done;
217 /* Store in-repository path prefix. */
218 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
219 absprefix ? absprefix : prefix);
220 if (err)
221 goto done;
223 /* Generate UUID. */
224 uuid_create(&uuid, &uuid_status);
225 if (uuid_status != uuid_s_ok) {
226 err = got_error_uuid(uuid_status, "uuid_create");
227 goto done;
229 uuid_to_string(&uuid, &uuidstr, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_to_string");
232 goto done;
234 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
235 if (err)
236 goto done;
238 /* Stamp work tree with format file. */
239 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
240 err = got_error_from_errno("asprintf");
241 goto done;
243 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
244 if (err)
245 goto done;
247 done:
248 free(commit_id);
249 free(path_got);
250 free(formatstr);
251 free(absprefix);
252 free(basestr);
253 free(uuidstr);
254 return err;
257 const struct got_error *
258 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
259 const char *path_prefix)
261 char *absprefix = NULL;
263 if (!got_path_is_absolute(path_prefix)) {
264 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
265 return got_error_from_errno("asprintf");
267 *match = (strcmp(absprefix ? absprefix : path_prefix,
268 worktree->path_prefix) == 0);
269 free(absprefix);
270 return NULL;
273 const char *
274 got_worktree_get_head_ref_name(struct got_worktree *worktree)
276 return worktree->head_ref_name;
279 const struct got_error *
280 got_worktree_set_head_ref(struct got_worktree *worktree,
281 struct got_reference *head_ref)
283 const struct got_error *err = NULL;
284 char *path_got = NULL, *head_ref_name = NULL;
286 if (asprintf(&path_got, "%s/%s", worktree->root_path,
287 GOT_WORKTREE_GOT_DIR) == -1) {
288 err = got_error_from_errno("asprintf");
289 path_got = NULL;
290 goto done;
293 head_ref_name = strdup(got_ref_get_name(head_ref));
294 if (head_ref_name == NULL) {
295 err = got_error_from_errno("strdup");
296 goto done;
299 err = write_head_ref(path_got, head_ref);
300 if (err)
301 goto done;
303 free(worktree->head_ref_name);
304 worktree->head_ref_name = head_ref_name;
305 done:
306 free(path_got);
307 if (err)
308 free(head_ref_name);
309 return err;
312 struct got_object_id *
313 got_worktree_get_base_commit_id(struct got_worktree *worktree)
315 return worktree->base_commit_id;
318 const struct got_error *
319 got_worktree_set_base_commit_id(struct got_worktree *worktree,
320 struct got_repository *repo, struct got_object_id *commit_id)
322 const struct got_error *err;
323 struct got_object *obj = NULL;
324 char *id_str = NULL;
325 char *path_got = NULL;
327 if (asprintf(&path_got, "%s/%s", worktree->root_path,
328 GOT_WORKTREE_GOT_DIR) == -1) {
329 err = got_error_from_errno("asprintf");
330 path_got = NULL;
331 goto done;
334 err = got_object_open(&obj, repo, commit_id);
335 if (err)
336 return err;
338 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
339 err = got_error(GOT_ERR_OBJ_TYPE);
340 goto done;
343 /* Record our base commit. */
344 err = got_object_id_str(&id_str, commit_id);
345 if (err)
346 goto done;
347 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
348 if (err)
349 goto done;
351 free(worktree->base_commit_id);
352 worktree->base_commit_id = got_object_id_dup(commit_id);
353 if (worktree->base_commit_id == NULL) {
354 err = got_error_from_errno("got_object_id_dup");
355 goto done;
357 done:
358 if (obj)
359 got_object_close(obj);
360 free(id_str);
361 free(path_got);
362 return err;
365 const struct got_gotconfig *
366 got_worktree_get_gotconfig(struct got_worktree *worktree)
368 return worktree->gotconfig;
371 static const struct got_error *
372 lock_worktree(struct got_worktree *worktree, int operation)
374 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
375 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
376 : got_error_from_errno2("flock",
377 got_worktree_get_root_path(worktree)));
378 return NULL;
381 static const struct got_error *
382 add_dir_on_disk(struct got_worktree *worktree, const char *path)
384 const struct got_error *err = NULL;
385 char *abspath;
387 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
388 return got_error_from_errno("asprintf");
390 err = got_path_mkdir(abspath);
391 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
392 struct stat sb;
393 err = NULL;
394 if (lstat(abspath, &sb) == -1) {
395 err = got_error_from_errno2("lstat", abspath);
396 } else if (!S_ISDIR(sb.st_mode)) {
397 /* TODO directory is obstructed; do something */
398 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 free(abspath);
402 return err;
405 static const struct got_error *
406 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
408 const struct got_error *err = NULL;
409 uint8_t fbuf1[8192];
410 uint8_t fbuf2[8192];
411 size_t flen1 = 0, flen2 = 0;
413 *same = 1;
415 for (;;) {
416 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
417 if (flen1 == 0 && ferror(f1)) {
418 err = got_error_from_errno("fread");
419 break;
421 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
422 if (flen2 == 0 && ferror(f2)) {
423 err = got_error_from_errno("fread");
424 break;
426 if (flen1 == 0) {
427 if (flen2 != 0)
428 *same = 0;
429 break;
430 } else if (flen2 == 0) {
431 if (flen1 != 0)
432 *same = 0;
433 break;
434 } else if (flen1 == flen2) {
435 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
436 *same = 0;
437 break;
439 } else {
440 *same = 0;
441 break;
445 return err;
448 static const struct got_error *
449 check_files_equal(int *same, FILE *f1, FILE *f2)
451 struct stat sb;
452 size_t size1, size2;
454 *same = 1;
456 if (fstat(fileno(f1), &sb) != 0)
457 return got_error_from_errno("fstat");
458 size1 = sb.st_size;
460 if (fstat(fileno(f2), &sb) != 0)
461 return got_error_from_errno("fstat");
462 size2 = sb.st_size;
464 if (size1 != size2) {
465 *same = 0;
466 return NULL;
469 if (fseek(f1, 0L, SEEK_SET) == -1)
470 return got_ferror(f1, GOT_ERR_IO);
471 if (fseek(f2, 0L, SEEK_SET) == -1)
472 return got_ferror(f2, GOT_ERR_IO);
474 return check_file_contents_equal(same, f1, f2);
477 static const struct got_error *
478 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
480 uint8_t fbuf[65536];
481 size_t flen;
482 ssize_t outlen;
484 *outsize = 0;
486 if (fseek(f, 0L, SEEK_SET) == -1)
487 return got_ferror(f, GOT_ERR_IO);
489 for (;;) {
490 flen = fread(fbuf, 1, sizeof(fbuf), f);
491 if (flen == 0) {
492 if (ferror(f))
493 return got_error_from_errno("fread");
494 if (feof(f))
495 break;
497 outlen = write(outfd, fbuf, flen);
498 if (outlen == -1)
499 return got_error_from_errno("write");
500 if (outlen != flen)
501 return got_error(GOT_ERR_IO);
502 *outsize += outlen;
505 return NULL;
508 static const struct got_error *
509 merge_binary_file(int *overlapcnt, int merged_fd,
510 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
511 const char *label_deriv, const char *label_orig, const char *label_deriv2,
512 const char *ondisk_path)
514 const struct got_error *err = NULL;
515 int same_content, changed_deriv, changed_deriv2;
516 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
517 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
518 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
519 char *base_path_orig = NULL, *base_path_deriv = NULL;
520 char *base_path_deriv2 = NULL;
522 *overlapcnt = 0;
524 err = check_files_equal(&same_content, f_deriv, f_deriv2);
525 if (err)
526 return err;
528 if (same_content)
529 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
531 err = check_files_equal(&same_content, f_deriv, f_orig);
532 if (err)
533 return err;
534 changed_deriv = !same_content;
535 err = check_files_equal(&same_content, f_deriv2, f_orig);
536 if (err)
537 return err;
538 changed_deriv2 = !same_content;
540 if (changed_deriv && changed_deriv2) {
541 *overlapcnt = 1;
542 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
543 err = got_error_from_errno("asprintf");
544 goto done;
546 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
550 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
551 err = got_error_from_errno("asprintf");
552 goto done;
554 err = got_opentemp_named_fd(&path_orig, &fd_orig,
555 base_path_orig);
556 if (err)
557 goto done;
558 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
559 base_path_deriv);
560 if (err)
561 goto done;
562 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
563 base_path_deriv2);
564 if (err)
565 goto done;
566 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
567 if (err)
568 goto done;
569 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
570 if (err)
571 goto done;
572 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
573 if (err)
574 goto done;
575 if (dprintf(merged_fd, "Binary files differ and cannot be "
576 "merged automatically:\n") < 0) {
577 err = got_error_from_errno("dprintf");
578 goto done;
580 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
581 GOT_DIFF_CONFLICT_MARKER_BEGIN,
582 label_deriv ? " " : "",
583 label_deriv ? label_deriv : "",
584 path_deriv) < 0) {
585 err = got_error_from_errno("dprintf");
586 goto done;
588 if (size_orig > 0) {
589 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
590 GOT_DIFF_CONFLICT_MARKER_ORIG,
591 label_orig ? " " : "",
592 label_orig ? label_orig : "",
593 path_orig) < 0) {
594 err = got_error_from_errno("dprintf");
595 goto done;
598 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
599 GOT_DIFF_CONFLICT_MARKER_SEP,
600 path_deriv2,
601 GOT_DIFF_CONFLICT_MARKER_END,
602 label_deriv2 ? " " : "",
603 label_deriv2 ? label_deriv2 : "") < 0) {
604 err = got_error_from_errno("dprintf");
605 goto done;
607 } else if (changed_deriv)
608 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
609 else if (changed_deriv2)
610 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
611 done:
612 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
613 err == NULL)
614 err = got_error_from_errno2("unlink", path_orig);
615 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
616 err = got_error_from_errno2("close", path_orig);
617 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_deriv);
619 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv2);
621 free(path_orig);
622 free(path_deriv);
623 free(path_deriv2);
624 free(base_path_orig);
625 free(base_path_deriv);
626 free(base_path_deriv2);
627 return err;
630 /*
631 * Perform a 3-way merge where the file f_orig acts as the common
632 * ancestor, the file f_deriv acts as the first derived version,
633 * and the file f_deriv2 acts as the second derived version.
634 * The merge result will be written to a new file at ondisk_path; any
635 * existing file at this path will be replaced.
636 */
637 static const struct got_error *
638 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
639 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
640 const char *path, uint16_t st_mode,
641 const char *label_orig, const char *label_deriv, const char *label_deriv2,
642 enum got_diff_algorithm diff_algo, struct got_repository *repo,
643 got_worktree_checkout_cb progress_cb, void *progress_arg)
645 const struct got_error *err = NULL;
646 int merged_fd = -1;
647 FILE *f_merged = NULL;
648 char *merged_path = NULL, *base_path = NULL;
649 int overlapcnt = 0;
650 char *parent = NULL;
652 *local_changes_subsumed = 0;
654 err = got_path_dirname(&parent, ondisk_path);
655 if (err)
656 return err;
658 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
659 err = got_error_from_errno("asprintf");
660 goto done;
663 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
664 if (err)
665 goto done;
667 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
668 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
669 if (err) {
670 if (err->code != GOT_ERR_FILE_BINARY)
671 goto done;
672 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
673 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
674 ondisk_path);
675 if (err)
676 goto done;
679 err = (*progress_cb)(progress_arg,
680 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
681 if (err)
682 goto done;
684 if (fsync(merged_fd) != 0) {
685 err = got_error_from_errno("fsync");
686 goto done;
689 f_merged = fdopen(merged_fd, "r");
690 if (f_merged == NULL) {
691 err = got_error_from_errno("fdopen");
692 goto done;
694 merged_fd = -1;
696 /* Check if a clean merge has subsumed all local changes. */
697 if (overlapcnt == 0) {
698 err = check_files_equal(local_changes_subsumed, f_deriv,
699 f_merged);
700 if (err)
701 goto done;
704 if (fchmod(fileno(f_merged), st_mode) != 0) {
705 err = got_error_from_errno2("fchmod", merged_path);
706 goto done;
709 if (rename(merged_path, ondisk_path) != 0) {
710 err = got_error_from_errno3("rename", merged_path,
711 ondisk_path);
712 goto done;
714 done:
715 if (err) {
716 if (merged_path)
717 unlink(merged_path);
719 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
720 err = got_error_from_errno("close");
721 if (f_merged && fclose(f_merged) == EOF && err == NULL)
722 err = got_error_from_errno("fclose");
723 free(merged_path);
724 free(base_path);
725 free(parent);
726 return err;
729 static const struct got_error *
730 update_symlink(const char *ondisk_path, const char *target_path,
731 size_t target_len)
733 /* This is not atomic but matches what 'ln -sf' does. */
734 if (unlink(ondisk_path) == -1)
735 return got_error_from_errno2("unlink", ondisk_path);
736 if (symlink(target_path, ondisk_path) == -1)
737 return got_error_from_errno3("symlink", target_path,
738 ondisk_path);
739 return NULL;
742 /*
743 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
744 * in the work tree with a file that contains conflict markers and the
745 * conflicting target paths of the original version, a "derived version"
746 * of a symlink from an incoming change, and a local version of the symlink.
748 * The original versions's target path can be NULL if it is not available,
749 * such as if both derived versions added a new symlink at the same path.
751 * The incoming derived symlink target is NULL in case the incoming change
752 * has deleted this symlink.
753 */
754 static const struct got_error *
755 install_symlink_conflict(const char *deriv_target,
756 struct got_object_id *deriv_base_commit_id, const char *orig_target,
757 const char *label_orig, const char *local_target, const char *ondisk_path)
759 const struct got_error *err;
760 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
761 FILE *f = NULL;
763 err = got_object_id_str(&id_str, deriv_base_commit_id);
764 if (err)
765 return got_error_from_errno("asprintf");
767 if (asprintf(&label_deriv, "%s: commit %s",
768 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
769 err = got_error_from_errno("asprintf");
770 goto done;
773 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
774 if (err)
775 goto done;
777 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
778 err = got_error_from_errno2("fchmod", path);
779 goto done;
782 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
783 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
784 deriv_target ? deriv_target : "(symlink was deleted)",
785 orig_target ? label_orig : "",
786 orig_target ? "\n" : "",
787 orig_target ? orig_target : "",
788 orig_target ? "\n" : "",
789 GOT_DIFF_CONFLICT_MARKER_SEP,
790 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
791 err = got_error_from_errno2("fprintf", path);
792 goto done;
795 if (unlink(ondisk_path) == -1) {
796 err = got_error_from_errno2("unlink", ondisk_path);
797 goto done;
799 if (rename(path, ondisk_path) == -1) {
800 err = got_error_from_errno3("rename", path, ondisk_path);
801 goto done;
803 done:
804 if (f != NULL && fclose(f) == EOF && err == NULL)
805 err = got_error_from_errno2("fclose", path);
806 free(path);
807 free(id_str);
808 free(label_deriv);
809 return err;
812 /* forward declaration */
813 static const struct got_error *
814 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
815 const char *, const char *, uint16_t, const char *,
816 struct got_blob_object *, struct got_object_id *,
817 struct got_repository *, got_worktree_checkout_cb, void *);
819 /*
820 * Merge a symlink into the work tree, where blob_orig acts as the common
821 * ancestor, deriv_target is the link target of the first derived version,
822 * and the symlink on disk acts as the second derived version.
823 * Assume that contents of both blobs represent symlinks.
824 */
825 static const struct got_error *
826 merge_symlink(struct got_worktree *worktree,
827 struct got_blob_object *blob_orig, const char *ondisk_path,
828 const char *path, const char *label_orig, const char *deriv_target,
829 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
830 got_worktree_checkout_cb progress_cb, void *progress_arg)
832 const struct got_error *err = NULL;
833 char *ancestor_target = NULL;
834 struct stat sb;
835 ssize_t ondisk_len, deriv_len;
836 char ondisk_target[PATH_MAX];
837 int have_local_change = 0;
838 int have_incoming_change = 0;
840 if (lstat(ondisk_path, &sb) == -1)
841 return got_error_from_errno2("lstat", ondisk_path);
843 ondisk_len = readlink(ondisk_path, ondisk_target,
844 sizeof(ondisk_target));
845 if (ondisk_len == -1) {
846 err = got_error_from_errno2("readlink",
847 ondisk_path);
848 goto done;
850 ondisk_target[ondisk_len] = '\0';
852 if (blob_orig) {
853 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
854 if (err)
855 goto done;
858 if (ancestor_target == NULL ||
859 (ondisk_len != strlen(ancestor_target) ||
860 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
861 have_local_change = 1;
863 deriv_len = strlen(deriv_target);
864 if (ancestor_target == NULL ||
865 (deriv_len != strlen(ancestor_target) ||
866 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
867 have_incoming_change = 1;
869 if (!have_local_change && !have_incoming_change) {
870 if (ancestor_target) {
871 /* Both sides made the same change. */
872 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
873 path);
874 } else if (deriv_len == ondisk_len &&
875 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
876 /* Both sides added the same symlink. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 path);
879 } else {
880 /* Both sides added symlinks which don't match. */
881 err = install_symlink_conflict(deriv_target,
882 deriv_base_commit_id, ancestor_target,
883 label_orig, ondisk_target, ondisk_path);
884 if (err)
885 goto done;
886 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
887 path);
889 } else if (!have_local_change && have_incoming_change) {
890 /* Apply the incoming change. */
891 err = update_symlink(ondisk_path, deriv_target,
892 strlen(deriv_target));
893 if (err)
894 goto done;
895 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
896 } else if (have_local_change && have_incoming_change) {
897 if (deriv_len == ondisk_len &&
898 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
899 /* Both sides made the same change. */
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
901 path);
902 } else {
903 err = install_symlink_conflict(deriv_target,
904 deriv_base_commit_id, ancestor_target, label_orig,
905 ondisk_target, ondisk_path);
906 if (err)
907 goto done;
908 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
909 path);
913 done:
914 free(ancestor_target);
915 return err;
918 static const struct got_error *
919 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
921 const struct got_error *err = NULL;
922 char target_path[PATH_MAX];
923 ssize_t target_len;
924 size_t n;
925 FILE *f;
927 *outfile = NULL;
929 f = got_opentemp();
930 if (f == NULL)
931 return got_error_from_errno("got_opentemp");
932 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
933 if (target_len == -1) {
934 err = got_error_from_errno2("readlink", ondisk_path);
935 goto done;
937 n = fwrite(target_path, 1, target_len, f);
938 if (n != target_len) {
939 err = got_ferror(f, GOT_ERR_IO);
940 goto done;
942 if (fflush(f) == EOF) {
943 err = got_error_from_errno("fflush");
944 goto done;
946 if (fseek(f, 0L, SEEK_SET) == -1) {
947 err = got_ferror(f, GOT_ERR_IO);
948 goto done;
950 done:
951 if (err)
952 fclose(f);
953 else
954 *outfile = f;
955 return err;
958 /*
959 * Perform a 3-way merge where blob_orig acts as the common ancestor,
960 * blob_deriv acts as the first derived version, and the file on disk
961 * acts as the second derived version.
962 */
963 static const struct got_error *
964 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
965 struct got_blob_object *blob_orig, const char *ondisk_path,
966 const char *path, uint16_t st_mode, const char *label_orig,
967 struct got_blob_object *blob_deriv,
968 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
969 got_worktree_checkout_cb progress_cb, void *progress_arg)
971 const struct got_error *err = NULL;
972 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
973 char *blob_orig_path = NULL;
974 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
975 char *label_deriv = NULL, *parent = NULL;
977 *local_changes_subsumed = 0;
979 err = got_path_dirname(&parent, ondisk_path);
980 if (err)
981 return err;
983 if (blob_orig) {
984 if (asprintf(&base_path, "%s/got-merge-blob-orig",
985 parent) == -1) {
986 err = got_error_from_errno("asprintf");
987 base_path = NULL;
988 goto done;
991 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
992 if (err)
993 goto done;
994 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
995 blob_orig);
996 if (err)
997 goto done;
998 free(base_path);
999 } else {
1001 * No common ancestor exists. This is an "add vs add" conflict
1002 * and we simply use an empty ancestor file to make both files
1003 * appear in the merged result in their entirety.
1005 f_orig = got_opentemp();
1006 if (f_orig == NULL) {
1007 err = got_error_from_errno("got_opentemp");
1008 goto done;
1012 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1013 err = got_error_from_errno("asprintf");
1014 base_path = NULL;
1015 goto done;
1018 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1019 if (err)
1020 goto done;
1021 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1022 blob_deriv);
1023 if (err)
1024 goto done;
1026 err = got_object_id_str(&id_str, deriv_base_commit_id);
1027 if (err)
1028 goto done;
1029 if (asprintf(&label_deriv, "%s: commit %s",
1030 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1031 err = got_error_from_errno("asprintf");
1032 goto done;
1036 * In order the run a 3-way merge with a symlink we copy the symlink's
1037 * target path into a temporary file and use that file with diff3.
1039 if (S_ISLNK(st_mode)) {
1040 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1041 if (err)
1042 goto done;
1043 } else {
1044 int fd;
1045 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1046 if (fd == -1) {
1047 err = got_error_from_errno2("open", ondisk_path);
1048 goto done;
1050 f_deriv2 = fdopen(fd, "r");
1051 if (f_deriv2 == NULL) {
1052 err = got_error_from_errno2("fdopen", ondisk_path);
1053 close(fd);
1054 goto done;
1058 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1059 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1060 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1061 done:
1062 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1063 err = got_error_from_errno("fclose");
1064 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 free(base_path);
1069 if (blob_orig_path) {
1070 unlink(blob_orig_path);
1071 free(blob_orig_path);
1073 if (blob_deriv_path) {
1074 unlink(blob_deriv_path);
1075 free(blob_deriv_path);
1077 free(id_str);
1078 free(label_deriv);
1079 free(parent);
1080 return err;
1083 static const struct got_error *
1084 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1085 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1086 int wt_fd, const char *path, struct got_object_id *blob_id)
1088 const struct got_error *err = NULL;
1089 struct got_fileindex_entry *new_ie;
1091 *new_iep = NULL;
1093 err = got_fileindex_entry_alloc(&new_ie, path);
1094 if (err)
1095 return err;
1097 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1098 blob_id->sha1, base_commit_id->sha1, 1);
1099 if (err)
1100 goto done;
1102 err = got_fileindex_entry_add(fileindex, new_ie);
1103 done:
1104 if (err)
1105 got_fileindex_entry_free(new_ie);
1106 else
1107 *new_iep = new_ie;
1108 return err;
1111 static mode_t
1112 get_ondisk_perms(int executable, mode_t st_mode)
1114 mode_t xbits = S_IXUSR;
1116 if (executable) {
1117 /* Map read bits to execute bits. */
1118 if (st_mode & S_IRGRP)
1119 xbits |= S_IXGRP;
1120 if (st_mode & S_IROTH)
1121 xbits |= S_IXOTH;
1122 return st_mode | xbits;
1125 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 /* forward declaration */
1129 static const struct got_error *
1130 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 const char *path, mode_t te_mode, mode_t st_mode,
1132 struct got_blob_object *blob, int restoring_missing_file,
1133 int reverting_versioned_file, int installing_bad_symlink,
1134 int path_is_unversioned, struct got_repository *repo,
1135 got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 * This function assumes that the provided symlink target points at a
1139 * safe location in the work tree!
1141 static const struct got_error *
1142 replace_existing_symlink(int *did_something, const char *ondisk_path,
1143 const char *target_path, size_t target_len)
1145 const struct got_error *err = NULL;
1146 ssize_t elen;
1147 char etarget[PATH_MAX];
1148 int fd;
1150 *did_something = 0;
1153 * "Bad" symlinks (those pointing outside the work tree or into the
1154 * .got directory) are installed in the work tree as a regular file
1155 * which contains the bad symlink target path.
1156 * The new symlink target has already been checked for safety by our
1157 * caller. If we can successfully open a regular file then we simply
1158 * replace this file with a symlink below.
1160 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1161 if (fd == -1) {
1162 if (!got_err_open_nofollow_on_symlink())
1163 return got_error_from_errno2("open", ondisk_path);
1165 /* We are updating an existing on-disk symlink. */
1166 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1167 if (elen == -1)
1168 return got_error_from_errno2("readlink", ondisk_path);
1170 if (elen == target_len &&
1171 memcmp(etarget, target_path, target_len) == 0)
1172 return NULL; /* nothing to do */
1175 *did_something = 1;
1176 err = update_symlink(ondisk_path, target_path, target_len);
1177 if (fd != -1 && close(fd) == -1 && err == NULL)
1178 err = got_error_from_errno2("close", ondisk_path);
1179 return err;
1182 static const struct got_error *
1183 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1184 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1186 const struct got_error *err = NULL;
1187 char canonpath[PATH_MAX];
1188 char *path_got = NULL;
1190 *is_bad_symlink = 0;
1192 if (target_len >= sizeof(canonpath)) {
1193 *is_bad_symlink = 1;
1194 return NULL;
1198 * We do not use realpath(3) to resolve the symlink's target
1199 * path because we don't want to resolve symlinks recursively.
1200 * Instead we make the path absolute and then canonicalize it.
1201 * Relative symlink target lookup should begin at the directory
1202 * in which the blob object is being installed.
1204 if (!got_path_is_absolute(target_path)) {
1205 char *abspath, *parent;
1206 err = got_path_dirname(&parent, ondisk_path);
1207 if (err)
1208 return err;
1209 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1210 free(parent);
1211 return got_error_from_errno("asprintf");
1213 free(parent);
1214 if (strlen(abspath) >= sizeof(canonpath)) {
1215 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1216 free(abspath);
1217 return err;
1219 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1220 free(abspath);
1221 if (err)
1222 return err;
1223 } else {
1224 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1225 if (err)
1226 return err;
1229 /* Only allow symlinks pointing at paths within the work tree. */
1230 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1231 *is_bad_symlink = 1;
1232 return NULL;
1235 /* Do not allow symlinks pointing into the .got directory. */
1236 if (asprintf(&path_got, "%s/%s", wtroot_path,
1237 GOT_WORKTREE_GOT_DIR) == -1)
1238 return got_error_from_errno("asprintf");
1239 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1240 *is_bad_symlink = 1;
1242 free(path_got);
1243 return NULL;
1246 static const struct got_error *
1247 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1248 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1249 int restoring_missing_file, int reverting_versioned_file,
1250 int path_is_unversioned, int allow_bad_symlinks,
1251 struct got_repository *repo,
1252 got_worktree_checkout_cb progress_cb, void *progress_arg)
1254 const struct got_error *err = NULL;
1255 char target_path[PATH_MAX];
1256 size_t len, target_len = 0;
1257 char *path_got = NULL;
1258 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1259 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1261 *is_bad_symlink = 0;
1264 * Blob object content specifies the target path of the link.
1265 * If a symbolic link cannot be installed we instead create
1266 * a regular file which contains the link target path stored
1267 * in the blob object.
1269 do {
1270 err = got_object_blob_read_block(&len, blob);
1271 if (len + target_len >= sizeof(target_path)) {
1272 /* Path too long; install as a regular file. */
1273 *is_bad_symlink = 1;
1274 got_object_blob_rewind(blob);
1275 return install_blob(worktree, ondisk_path, path,
1276 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1277 restoring_missing_file, reverting_versioned_file,
1278 1, path_is_unversioned, repo, progress_cb,
1279 progress_arg);
1281 if (len > 0) {
1282 /* Skip blob object header first time around. */
1283 memcpy(target_path + target_len, buf + hdrlen,
1284 len - hdrlen);
1285 target_len += len - hdrlen;
1286 hdrlen = 0;
1288 } while (len != 0);
1289 target_path[target_len] = '\0';
1291 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1292 ondisk_path, worktree->root_path);
1293 if (err)
1294 return err;
1296 if (*is_bad_symlink && !allow_bad_symlinks) {
1297 /* install as a regular file */
1298 got_object_blob_rewind(blob);
1299 err = install_blob(worktree, ondisk_path, path,
1300 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1301 restoring_missing_file, reverting_versioned_file, 1,
1302 path_is_unversioned, repo, progress_cb, progress_arg);
1303 goto done;
1306 if (symlink(target_path, ondisk_path) == -1) {
1307 if (errno == EEXIST) {
1308 int symlink_replaced;
1309 if (path_is_unversioned) {
1310 err = (*progress_cb)(progress_arg,
1311 GOT_STATUS_UNVERSIONED, path);
1312 goto done;
1314 err = replace_existing_symlink(&symlink_replaced,
1315 ondisk_path, target_path, target_len);
1316 if (err)
1317 goto done;
1318 if (progress_cb) {
1319 if (symlink_replaced) {
1320 err = (*progress_cb)(progress_arg,
1321 reverting_versioned_file ?
1322 GOT_STATUS_REVERT :
1323 GOT_STATUS_UPDATE, path);
1324 } else {
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_EXISTS, path);
1329 goto done; /* Nothing else to do. */
1332 if (errno == ENOENT) {
1333 char *parent;
1334 err = got_path_dirname(&parent, ondisk_path);
1335 if (err)
1336 goto done;
1337 err = add_dir_on_disk(worktree, parent);
1338 free(parent);
1339 if (err)
1340 goto done;
1342 * Retry, and fall through to error handling
1343 * below if this second attempt fails.
1345 if (symlink(target_path, ondisk_path) != -1) {
1346 err = NULL; /* success */
1347 goto done;
1351 /* Handle errors from first or second creation attempt. */
1352 if (errno == ENAMETOOLONG) {
1353 /* bad target path; install as a regular file */
1354 *is_bad_symlink = 1;
1355 got_object_blob_rewind(blob);
1356 err = install_blob(worktree, ondisk_path, path,
1357 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1358 restoring_missing_file, reverting_versioned_file, 1,
1359 path_is_unversioned, repo,
1360 progress_cb, progress_arg);
1361 } else if (errno == ENOTDIR) {
1362 err = got_error_path(ondisk_path,
1363 GOT_ERR_FILE_OBSTRUCTED);
1364 } else {
1365 err = got_error_from_errno3("symlink",
1366 target_path, ondisk_path);
1368 } else if (progress_cb)
1369 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1370 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1371 done:
1372 free(path_got);
1373 return err;
1376 static const struct got_error *
1377 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1378 const char *path, mode_t te_mode, mode_t st_mode,
1379 struct got_blob_object *blob, int restoring_missing_file,
1380 int reverting_versioned_file, int installing_bad_symlink,
1381 int path_is_unversioned, struct got_repository *repo,
1382 got_worktree_checkout_cb progress_cb, void *progress_arg)
1384 const struct got_error *err = NULL;
1385 int fd = -1;
1386 size_t len, hdrlen;
1387 int update = 0;
1388 char *tmppath = NULL;
1390 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1391 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1392 if (fd == -1) {
1393 if (errno == ENOENT) {
1394 char *parent;
1395 err = got_path_dirname(&parent, path);
1396 if (err)
1397 return err;
1398 err = add_dir_on_disk(worktree, parent);
1399 free(parent);
1400 if (err)
1401 return err;
1402 fd = open(ondisk_path,
1403 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1404 GOT_DEFAULT_FILE_MODE);
1405 if (fd == -1)
1406 return got_error_from_errno2("open",
1407 ondisk_path);
1408 } else if (errno == EEXIST) {
1409 if (path_is_unversioned) {
1410 err = (*progress_cb)(progress_arg,
1411 GOT_STATUS_UNVERSIONED, path);
1412 goto done;
1414 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1415 !S_ISREG(st_mode) && !installing_bad_symlink) {
1416 /* TODO file is obstructed; do something */
1417 err = got_error_path(ondisk_path,
1418 GOT_ERR_FILE_OBSTRUCTED);
1419 goto done;
1420 } else {
1421 err = got_opentemp_named_fd(&tmppath, &fd,
1422 ondisk_path);
1423 if (err)
1424 goto done;
1425 update = 1;
1427 } else
1428 return got_error_from_errno2("open", ondisk_path);
1431 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1432 err = got_error_from_errno2("fchmod",
1433 update ? tmppath : ondisk_path);
1434 goto done;
1437 if (progress_cb) {
1438 if (restoring_missing_file)
1439 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1440 path);
1441 else if (reverting_versioned_file)
1442 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1443 path);
1444 else
1445 err = (*progress_cb)(progress_arg,
1446 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1447 if (err)
1448 goto done;
1451 hdrlen = got_object_blob_get_hdrlen(blob);
1452 do {
1453 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1454 err = got_object_blob_read_block(&len, blob);
1455 if (err)
1456 break;
1457 if (len > 0) {
1458 /* Skip blob object header first time around. */
1459 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1460 if (outlen == -1) {
1461 err = got_error_from_errno("write");
1462 goto done;
1463 } else if (outlen != len - hdrlen) {
1464 err = got_error(GOT_ERR_IO);
1465 goto done;
1467 hdrlen = 0;
1469 } while (len != 0);
1471 if (fsync(fd) != 0) {
1472 err = got_error_from_errno("fsync");
1473 goto done;
1476 if (update) {
1477 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1478 err = got_error_from_errno2("unlink", ondisk_path);
1479 goto done;
1481 if (rename(tmppath, ondisk_path) != 0) {
1482 err = got_error_from_errno3("rename", tmppath,
1483 ondisk_path);
1484 goto done;
1486 free(tmppath);
1487 tmppath = NULL;
1490 done:
1491 if (fd != -1 && close(fd) == -1 && err == NULL)
1492 err = got_error_from_errno("close");
1493 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1494 err = got_error_from_errno2("unlink", tmppath);
1495 free(tmppath);
1496 return err;
1499 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1500 static const struct got_error *
1501 get_modified_file_content_status(unsigned char *status, FILE *f)
1503 const struct got_error *err = NULL;
1504 const char *markers[3] = {
1505 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1506 GOT_DIFF_CONFLICT_MARKER_SEP,
1507 GOT_DIFF_CONFLICT_MARKER_END
1509 int i = 0;
1510 char *line = NULL;
1511 size_t linesize = 0;
1512 ssize_t linelen;
1514 while (*status == GOT_STATUS_MODIFY) {
1515 linelen = getline(&line, &linesize, f);
1516 if (linelen == -1) {
1517 if (feof(f))
1518 break;
1519 err = got_ferror(f, GOT_ERR_IO);
1520 break;
1523 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1524 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1525 == 0)
1526 *status = GOT_STATUS_CONFLICT;
1527 else
1528 i++;
1531 free(line);
1533 return err;
1536 static int
1537 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1539 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1540 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 static int
1544 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1546 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1547 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1548 ie->mtime_sec == sb->st_mtim.tv_sec &&
1549 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1550 ie->size == (sb->st_size & 0xffffffff) &&
1551 !xbit_differs(ie, sb->st_mode));
1554 static unsigned char
1555 get_staged_status(struct got_fileindex_entry *ie)
1557 switch (got_fileindex_entry_stage_get(ie)) {
1558 case GOT_FILEIDX_STAGE_ADD:
1559 return GOT_STATUS_ADD;
1560 case GOT_FILEIDX_STAGE_DELETE:
1561 return GOT_STATUS_DELETE;
1562 case GOT_FILEIDX_STAGE_MODIFY:
1563 return GOT_STATUS_MODIFY;
1564 default:
1565 return GOT_STATUS_NO_CHANGE;
1569 static const struct got_error *
1570 get_symlink_modification_status(unsigned char *status,
1571 struct got_fileindex_entry *ie, const char *abspath,
1572 int dirfd, const char *de_name, struct got_blob_object *blob)
1574 const struct got_error *err = NULL;
1575 char target_path[PATH_MAX];
1576 char etarget[PATH_MAX];
1577 ssize_t elen;
1578 size_t len, target_len = 0;
1579 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1582 *status = GOT_STATUS_NO_CHANGE;
1584 /* Blob object content specifies the target path of the link. */
1585 do {
1586 err = got_object_blob_read_block(&len, blob);
1587 if (err)
1588 return err;
1589 if (len + target_len >= sizeof(target_path)) {
1591 * Should not happen. The blob contents were OK
1592 * when this symlink was installed.
1594 return got_error(GOT_ERR_NO_SPACE);
1596 if (len > 0) {
1597 /* Skip blob object header first time around. */
1598 memcpy(target_path + target_len, buf + hdrlen,
1599 len - hdrlen);
1600 target_len += len - hdrlen;
1601 hdrlen = 0;
1603 } while (len != 0);
1604 target_path[target_len] = '\0';
1606 if (dirfd != -1) {
1607 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1608 if (elen == -1)
1609 return got_error_from_errno2("readlinkat", abspath);
1610 } else {
1611 elen = readlink(abspath, etarget, sizeof(etarget));
1612 if (elen == -1)
1613 return got_error_from_errno2("readlink", abspath);
1616 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1617 *status = GOT_STATUS_MODIFY;
1619 return NULL;
1622 static const struct got_error *
1623 get_file_status(unsigned char *status, struct stat *sb,
1624 struct got_fileindex_entry *ie, const char *abspath,
1625 int dirfd, const char *de_name, struct got_repository *repo)
1627 const struct got_error *err = NULL;
1628 struct got_object_id id;
1629 size_t hdrlen;
1630 int fd = -1;
1631 FILE *f = NULL;
1632 uint8_t fbuf[8192];
1633 struct got_blob_object *blob = NULL;
1634 size_t flen, blen;
1635 unsigned char staged_status = get_staged_status(ie);
1637 *status = GOT_STATUS_NO_CHANGE;
1638 memset(sb, 0, sizeof(*sb));
1641 * Whenever the caller provides a directory descriptor and a
1642 * directory entry name for the file, use them! This prevents
1643 * race conditions if filesystem paths change beneath our feet.
1645 if (dirfd != -1) {
1646 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1647 if (errno == ENOENT) {
1648 if (got_fileindex_entry_has_file_on_disk(ie))
1649 *status = GOT_STATUS_MISSING;
1650 else
1651 *status = GOT_STATUS_DELETE;
1652 goto done;
1654 err = got_error_from_errno2("fstatat", abspath);
1655 goto done;
1657 } else {
1658 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1659 if (fd == -1 && errno != ENOENT &&
1660 !got_err_open_nofollow_on_symlink())
1661 return got_error_from_errno2("open", abspath);
1662 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1663 if (lstat(abspath, sb) == -1)
1664 return got_error_from_errno2("lstat", abspath);
1665 } else if (fd == -1 || fstat(fd, sb) == -1) {
1666 if (errno == ENOENT) {
1667 if (got_fileindex_entry_has_file_on_disk(ie))
1668 *status = GOT_STATUS_MISSING;
1669 else
1670 *status = GOT_STATUS_DELETE;
1671 goto done;
1673 err = got_error_from_errno2("fstat", abspath);
1674 goto done;
1678 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1679 *status = GOT_STATUS_OBSTRUCTED;
1680 goto done;
1683 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1684 *status = GOT_STATUS_DELETE;
1685 goto done;
1686 } else if (!got_fileindex_entry_has_blob(ie) &&
1687 staged_status != GOT_STATUS_ADD) {
1688 *status = GOT_STATUS_ADD;
1689 goto done;
1692 if (!stat_info_differs(ie, sb))
1693 goto done;
1695 if (S_ISLNK(sb->st_mode) &&
1696 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1697 *status = GOT_STATUS_MODIFY;
1698 goto done;
1701 if (staged_status == GOT_STATUS_MODIFY ||
1702 staged_status == GOT_STATUS_ADD)
1703 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1704 else
1705 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1707 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1708 if (err)
1709 goto done;
1711 if (S_ISLNK(sb->st_mode)) {
1712 err = get_symlink_modification_status(status, ie,
1713 abspath, dirfd, de_name, blob);
1714 goto done;
1717 if (dirfd != -1) {
1718 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1719 if (fd == -1) {
1720 err = got_error_from_errno2("openat", abspath);
1721 goto done;
1725 f = fdopen(fd, "r");
1726 if (f == NULL) {
1727 err = got_error_from_errno2("fdopen", abspath);
1728 goto done;
1730 fd = -1;
1731 hdrlen = got_object_blob_get_hdrlen(blob);
1732 for (;;) {
1733 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1734 err = got_object_blob_read_block(&blen, blob);
1735 if (err)
1736 goto done;
1737 /* Skip length of blob object header first time around. */
1738 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1739 if (flen == 0 && ferror(f)) {
1740 err = got_error_from_errno("fread");
1741 goto done;
1743 if (blen - hdrlen == 0) {
1744 if (flen != 0)
1745 *status = GOT_STATUS_MODIFY;
1746 break;
1747 } else if (flen == 0) {
1748 if (blen - hdrlen != 0)
1749 *status = GOT_STATUS_MODIFY;
1750 break;
1751 } else if (blen - hdrlen == flen) {
1752 /* Skip blob object header first time around. */
1753 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1754 *status = GOT_STATUS_MODIFY;
1755 break;
1757 } else {
1758 *status = GOT_STATUS_MODIFY;
1759 break;
1761 hdrlen = 0;
1764 if (*status == GOT_STATUS_MODIFY) {
1765 rewind(f);
1766 err = get_modified_file_content_status(status, f);
1767 } else if (xbit_differs(ie, sb->st_mode))
1768 *status = GOT_STATUS_MODE_CHANGE;
1769 done:
1770 if (blob)
1771 got_object_blob_close(blob);
1772 if (f != NULL && fclose(f) == EOF && err == NULL)
1773 err = got_error_from_errno2("fclose", abspath);
1774 if (fd != -1 && close(fd) == -1 && err == NULL)
1775 err = got_error_from_errno2("close", abspath);
1776 return err;
1780 * Update timestamps in the file index if a file is unmodified and
1781 * we had to run a full content comparison to find out.
1783 static const struct got_error *
1784 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1785 struct got_fileindex_entry *ie, struct stat *sb)
1787 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1788 return got_fileindex_entry_update(ie, wt_fd, path,
1789 ie->blob_sha1, ie->commit_sha1, 1);
1791 return NULL;
1794 static const struct got_error *
1795 update_blob(struct got_worktree *worktree,
1796 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1797 struct got_tree_entry *te, const char *path,
1798 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1799 void *progress_arg)
1801 const struct got_error *err = NULL;
1802 struct got_blob_object *blob = NULL;
1803 char *ondisk_path;
1804 unsigned char status = GOT_STATUS_NO_CHANGE;
1805 struct stat sb;
1807 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1808 return got_error_from_errno("asprintf");
1810 if (ie) {
1811 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1812 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1813 goto done;
1815 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1816 repo);
1817 if (err)
1818 goto done;
1819 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1820 sb.st_mode = got_fileindex_perms_to_st(ie);
1821 } else {
1822 if (stat(ondisk_path, &sb) == -1) {
1823 if (errno != ENOENT) {
1824 err = got_error_from_errno2("stat",
1825 ondisk_path);
1826 goto done;
1828 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1829 status = GOT_STATUS_UNVERSIONED;
1830 } else {
1831 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1832 status = GOT_STATUS_UNVERSIONED;
1833 else
1834 status = GOT_STATUS_OBSTRUCTED;
1838 if (status == GOT_STATUS_OBSTRUCTED) {
1839 if (ie)
1840 got_fileindex_entry_mark_skipped(ie);
1841 err = (*progress_cb)(progress_arg, status, path);
1842 goto done;
1844 if (status == GOT_STATUS_CONFLICT) {
1845 if (ie)
1846 got_fileindex_entry_mark_skipped(ie);
1847 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1848 path);
1849 goto done;
1852 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1853 (S_ISLNK(te->mode) ||
1854 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1856 * This is a regular file or an installed bad symlink.
1857 * If the file index indicates that this file is already
1858 * up-to-date with respect to the repository we can skip
1859 * updating contents of this file.
1861 if (got_fileindex_entry_has_commit(ie) &&
1862 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1863 SHA1_DIGEST_LENGTH) == 0) {
1864 /* Same commit. */
1865 err = sync_timestamps(worktree->root_fd,
1866 path, status, ie, &sb);
1867 if (err)
1868 goto done;
1869 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1870 path);
1871 goto done;
1873 if (got_fileindex_entry_has_blob(ie) &&
1874 memcmp(ie->blob_sha1, te->id.sha1,
1875 SHA1_DIGEST_LENGTH) == 0) {
1876 /* Different commit but the same blob. */
1877 err = sync_timestamps(worktree->root_fd,
1878 path, status, ie, &sb);
1879 if (err)
1880 goto done;
1881 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1882 path);
1883 goto done;
1887 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1888 if (err)
1889 goto done;
1891 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1892 int update_timestamps;
1893 struct got_blob_object *blob2 = NULL;
1894 char *label_orig = NULL;
1895 if (got_fileindex_entry_has_blob(ie)) {
1896 struct got_object_id id2;
1897 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1898 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1899 if (err)
1900 goto done;
1902 if (got_fileindex_entry_has_commit(ie)) {
1903 char id_str[SHA1_DIGEST_STRING_LENGTH];
1904 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1905 sizeof(id_str)) == NULL) {
1906 err = got_error_path(id_str,
1907 GOT_ERR_BAD_OBJ_ID_STR);
1908 goto done;
1910 if (asprintf(&label_orig, "%s: commit %s",
1911 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1912 err = got_error_from_errno("asprintf");
1913 goto done;
1916 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1917 char *link_target;
1918 err = got_object_blob_read_to_str(&link_target, blob);
1919 if (err)
1920 goto done;
1921 err = merge_symlink(worktree, blob2, ondisk_path, path,
1922 label_orig, link_target, worktree->base_commit_id,
1923 repo, progress_cb, progress_arg);
1924 free(link_target);
1925 } else {
1926 err = merge_blob(&update_timestamps, worktree, blob2,
1927 ondisk_path, path, sb.st_mode, label_orig, blob,
1928 worktree->base_commit_id, repo,
1929 progress_cb, progress_arg);
1931 free(label_orig);
1932 if (blob2)
1933 got_object_blob_close(blob2);
1934 if (err)
1935 goto done;
1937 * Do not update timestamps of files with local changes.
1938 * Otherwise, a future status walk would treat them as
1939 * unmodified files again.
1941 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1942 blob->id.sha1, worktree->base_commit_id->sha1,
1943 update_timestamps);
1944 } else if (status == GOT_STATUS_MODE_CHANGE) {
1945 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1946 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1947 } else if (status == GOT_STATUS_DELETE) {
1948 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1949 if (err)
1950 goto done;
1951 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1952 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1953 if (err)
1954 goto done;
1955 } else {
1956 int is_bad_symlink = 0;
1957 if (S_ISLNK(te->mode)) {
1958 err = install_symlink(&is_bad_symlink, worktree,
1959 ondisk_path, path, blob,
1960 status == GOT_STATUS_MISSING, 0,
1961 status == GOT_STATUS_UNVERSIONED, 0,
1962 repo, progress_cb, progress_arg);
1963 } else {
1964 err = install_blob(worktree, ondisk_path, path,
1965 te->mode, sb.st_mode, blob,
1966 status == GOT_STATUS_MISSING, 0, 0,
1967 status == GOT_STATUS_UNVERSIONED, repo,
1968 progress_cb, progress_arg);
1970 if (err)
1971 goto done;
1973 if (ie) {
1974 err = got_fileindex_entry_update(ie,
1975 worktree->root_fd, path, blob->id.sha1,
1976 worktree->base_commit_id->sha1, 1);
1977 } else {
1978 err = create_fileindex_entry(&ie, fileindex,
1979 worktree->base_commit_id, worktree->root_fd, path,
1980 &blob->id);
1982 if (err)
1983 goto done;
1985 if (is_bad_symlink) {
1986 got_fileindex_entry_filetype_set(ie,
1987 GOT_FILEIDX_MODE_BAD_SYMLINK);
1990 got_object_blob_close(blob);
1991 done:
1992 free(ondisk_path);
1993 return err;
1996 static const struct got_error *
1997 remove_ondisk_file(const char *root_path, const char *path)
1999 const struct got_error *err = NULL;
2000 char *ondisk_path = NULL, *parent = NULL;
2002 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2003 return got_error_from_errno("asprintf");
2005 if (unlink(ondisk_path) == -1) {
2006 if (errno != ENOENT)
2007 err = got_error_from_errno2("unlink", ondisk_path);
2008 } else {
2009 size_t root_len = strlen(root_path);
2010 err = got_path_dirname(&parent, ondisk_path);
2011 if (err)
2012 goto done;
2013 while (got_path_cmp(parent, root_path,
2014 strlen(parent), root_len) != 0) {
2015 free(ondisk_path);
2016 ondisk_path = parent;
2017 parent = NULL;
2018 if (rmdir(ondisk_path) == -1) {
2019 if (errno != ENOTEMPTY)
2020 err = got_error_from_errno2("rmdir",
2021 ondisk_path);
2022 break;
2024 err = got_path_dirname(&parent, ondisk_path);
2025 if (err)
2026 break;
2029 done:
2030 free(ondisk_path);
2031 free(parent);
2032 return err;
2035 static const struct got_error *
2036 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2037 struct got_fileindex_entry *ie, struct got_repository *repo,
2038 got_worktree_checkout_cb progress_cb, void *progress_arg)
2040 const struct got_error *err = NULL;
2041 unsigned char status;
2042 struct stat sb;
2043 char *ondisk_path;
2045 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2046 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2048 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2049 == -1)
2050 return got_error_from_errno("asprintf");
2052 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2053 if (err)
2054 goto done;
2056 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2057 char ondisk_target[PATH_MAX];
2058 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2059 sizeof(ondisk_target));
2060 if (ondisk_len == -1) {
2061 err = got_error_from_errno2("readlink", ondisk_path);
2062 goto done;
2064 ondisk_target[ondisk_len] = '\0';
2065 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2066 NULL, NULL, /* XXX pass common ancestor info? */
2067 ondisk_target, ondisk_path);
2068 if (err)
2069 goto done;
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2071 ie->path);
2072 goto done;
2075 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2076 status == GOT_STATUS_ADD) {
2077 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2078 if (err)
2079 goto done;
2081 * Preserve the working file and change the deleted blob's
2082 * entry into a schedule-add entry.
2084 err = got_fileindex_entry_update(ie, worktree->root_fd,
2085 ie->path, NULL, NULL, 0);
2086 } else {
2087 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2088 if (err)
2089 goto done;
2090 if (status == GOT_STATUS_NO_CHANGE) {
2091 err = remove_ondisk_file(worktree->root_path, ie->path);
2092 if (err)
2093 goto done;
2095 got_fileindex_entry_remove(fileindex, ie);
2097 done:
2098 free(ondisk_path);
2099 return err;
2102 struct diff_cb_arg {
2103 struct got_fileindex *fileindex;
2104 struct got_worktree *worktree;
2105 struct got_repository *repo;
2106 got_worktree_checkout_cb progress_cb;
2107 void *progress_arg;
2108 got_cancel_cb cancel_cb;
2109 void *cancel_arg;
2112 static const struct got_error *
2113 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2114 struct got_tree_entry *te, const char *parent_path)
2116 struct diff_cb_arg *a = arg;
2118 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2119 return got_error(GOT_ERR_CANCELLED);
2121 return update_blob(a->worktree, a->fileindex, ie, te,
2122 ie->path, a->repo, a->progress_cb, a->progress_arg);
2125 static const struct got_error *
2126 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2128 struct diff_cb_arg *a = arg;
2130 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2131 return got_error(GOT_ERR_CANCELLED);
2133 return delete_blob(a->worktree, a->fileindex, ie,
2134 a->repo, a->progress_cb, a->progress_arg);
2137 static const struct got_error *
2138 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2140 struct diff_cb_arg *a = arg;
2141 const struct got_error *err;
2142 char *path;
2144 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2145 return got_error(GOT_ERR_CANCELLED);
2147 if (got_object_tree_entry_is_submodule(te))
2148 return NULL;
2150 if (asprintf(&path, "%s%s%s", parent_path,
2151 parent_path[0] ? "/" : "", te->name)
2152 == -1)
2153 return got_error_from_errno("asprintf");
2155 if (S_ISDIR(te->mode))
2156 err = add_dir_on_disk(a->worktree, path);
2157 else
2158 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2159 a->repo, a->progress_cb, a->progress_arg);
2161 free(path);
2162 return err;
2165 const struct got_error *
2166 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2168 uint32_t uuid_status;
2170 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2171 if (uuid_status != uuid_s_ok) {
2172 *uuidstr = NULL;
2173 return got_error_uuid(uuid_status, "uuid_to_string");
2176 return NULL;
2179 static const struct got_error *
2180 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2182 const struct got_error *err = NULL;
2183 char *uuidstr = NULL;
2185 *refname = NULL;
2187 err = got_worktree_get_uuid(&uuidstr, worktree);
2188 if (err)
2189 return err;
2191 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2192 err = got_error_from_errno("asprintf");
2193 *refname = NULL;
2195 free(uuidstr);
2196 return err;
2199 const struct got_error *
2200 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2205 static const struct got_error *
2206 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2208 return get_ref_name(refname, worktree,
2209 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2212 static const struct got_error *
2213 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2215 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2218 static const struct got_error *
2219 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2221 return get_ref_name(refname, worktree,
2222 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2225 static const struct got_error *
2226 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2228 return get_ref_name(refname, worktree,
2229 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2232 static const struct got_error *
2233 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2235 return get_ref_name(refname, worktree,
2236 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2239 static const struct got_error *
2240 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2242 return get_ref_name(refname, worktree,
2243 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2246 static const struct got_error *
2247 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2249 return get_ref_name(refname, worktree,
2250 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2253 static const struct got_error *
2254 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2256 return get_ref_name(refname, worktree,
2257 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2260 const struct got_error *
2261 got_worktree_get_histedit_script_path(char **path,
2262 struct got_worktree *worktree)
2264 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2265 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2266 *path = NULL;
2267 return got_error_from_errno("asprintf");
2269 return NULL;
2272 static const struct got_error *
2273 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2279 static const struct got_error *
2280 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2287 * Prevent Git's garbage collector from deleting our base commit by
2288 * setting a reference to our base commit's ID.
2290 static const struct got_error *
2291 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2293 const struct got_error *err = NULL;
2294 struct got_reference *ref = NULL;
2295 char *refname;
2297 err = got_worktree_get_base_ref_name(&refname, worktree);
2298 if (err)
2299 return err;
2301 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2302 if (err)
2303 goto done;
2305 err = got_ref_write(ref, repo);
2306 done:
2307 free(refname);
2308 if (ref)
2309 got_ref_close(ref);
2310 return err;
2313 static const struct got_error *
2314 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2316 const struct got_error *err = NULL;
2318 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2319 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2320 err = got_error_from_errno("asprintf");
2321 *fileindex_path = NULL;
2323 return err;
2327 static const struct got_error *
2328 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2329 struct got_worktree *worktree)
2331 const struct got_error *err = NULL;
2332 FILE *index = NULL;
2334 *fileindex_path = NULL;
2335 *fileindex = got_fileindex_alloc();
2336 if (*fileindex == NULL)
2337 return got_error_from_errno("got_fileindex_alloc");
2339 err = get_fileindex_path(fileindex_path, worktree);
2340 if (err)
2341 goto done;
2343 index = fopen(*fileindex_path, "rbe");
2344 if (index == NULL) {
2345 if (errno != ENOENT)
2346 err = got_error_from_errno2("fopen", *fileindex_path);
2347 } else {
2348 err = got_fileindex_read(*fileindex, index);
2349 if (fclose(index) == EOF && err == NULL)
2350 err = got_error_from_errno("fclose");
2352 done:
2353 if (err) {
2354 free(*fileindex_path);
2355 *fileindex_path = NULL;
2356 got_fileindex_free(*fileindex);
2357 *fileindex = NULL;
2359 return err;
2362 struct bump_base_commit_id_arg {
2363 struct got_object_id *base_commit_id;
2364 const char *path;
2365 size_t path_len;
2366 const char *entry_name;
2367 got_worktree_checkout_cb progress_cb;
2368 void *progress_arg;
2371 /* Bump base commit ID of all files within an updated part of the work tree. */
2372 static const struct got_error *
2373 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2375 const struct got_error *err;
2376 struct bump_base_commit_id_arg *a = arg;
2378 if (a->entry_name) {
2379 if (strcmp(ie->path, a->path) != 0)
2380 return NULL;
2381 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2382 return NULL;
2384 if (got_fileindex_entry_was_skipped(ie))
2385 return NULL;
2387 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2388 SHA1_DIGEST_LENGTH) == 0)
2389 return NULL;
2391 if (a->progress_cb) {
2392 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2393 ie->path);
2394 if (err)
2395 return err;
2397 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2398 return NULL;
2401 static const struct got_error *
2402 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2403 struct got_fileindex *fileindex,
2404 got_worktree_checkout_cb progress_cb, void *progress_arg)
2406 struct bump_base_commit_id_arg bbc_arg;
2408 bbc_arg.base_commit_id = worktree->base_commit_id;
2409 bbc_arg.entry_name = NULL;
2410 bbc_arg.path = "";
2411 bbc_arg.path_len = 0;
2412 bbc_arg.progress_cb = progress_cb;
2413 bbc_arg.progress_arg = progress_arg;
2415 return got_fileindex_for_each_entry_safe(fileindex,
2416 bump_base_commit_id, &bbc_arg);
2419 static const struct got_error *
2420 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2422 const struct got_error *err = NULL;
2423 char *new_fileindex_path = NULL;
2424 FILE *new_index = NULL;
2425 struct timespec timeout;
2427 err = got_opentemp_named(&new_fileindex_path, &new_index,
2428 fileindex_path);
2429 if (err)
2430 goto done;
2432 err = got_fileindex_write(fileindex, new_index);
2433 if (err)
2434 goto done;
2436 if (rename(new_fileindex_path, fileindex_path) != 0) {
2437 err = got_error_from_errno3("rename", new_fileindex_path,
2438 fileindex_path);
2439 unlink(new_fileindex_path);
2443 * Sleep for a short amount of time to ensure that files modified after
2444 * this program exits have a different time stamp from the one which
2445 * was recorded in the file index.
2447 timeout.tv_sec = 0;
2448 timeout.tv_nsec = 1;
2449 nanosleep(&timeout, NULL);
2450 done:
2451 if (new_index)
2452 fclose(new_index);
2453 free(new_fileindex_path);
2454 return err;
2457 static const struct got_error *
2458 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2459 struct got_object_id **tree_id, const char *wt_relpath,
2460 struct got_worktree *worktree, struct got_repository *repo)
2462 const struct got_error *err = NULL;
2463 struct got_object_id *id = NULL;
2464 char *in_repo_path = NULL;
2465 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2467 *entry_type = GOT_OBJ_TYPE_ANY;
2468 *tree_relpath = NULL;
2469 *tree_id = NULL;
2471 if (wt_relpath[0] == '\0') {
2472 /* Check out all files within the work tree. */
2473 *entry_type = GOT_OBJ_TYPE_TREE;
2474 *tree_relpath = strdup("");
2475 if (*tree_relpath == NULL) {
2476 err = got_error_from_errno("strdup");
2477 goto done;
2479 err = got_object_id_by_path(tree_id, repo,
2480 worktree->base_commit_id, worktree->path_prefix);
2481 if (err)
2482 goto done;
2483 return NULL;
2486 /* Check out a subset of files in the work tree. */
2488 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2489 is_root_wt ? "" : "/", wt_relpath) == -1) {
2490 err = got_error_from_errno("asprintf");
2491 goto done;
2494 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2495 in_repo_path);
2496 if (err)
2497 goto done;
2499 free(in_repo_path);
2500 in_repo_path = NULL;
2502 err = got_object_get_type(entry_type, repo, id);
2503 if (err)
2504 goto done;
2506 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2507 /* Check out a single file. */
2508 if (strchr(wt_relpath, '/') == NULL) {
2509 /* Check out a single file in work tree's root dir. */
2510 in_repo_path = strdup(worktree->path_prefix);
2511 if (in_repo_path == NULL) {
2512 err = got_error_from_errno("strdup");
2513 goto done;
2515 *tree_relpath = strdup("");
2516 if (*tree_relpath == NULL) {
2517 err = got_error_from_errno("strdup");
2518 goto done;
2520 } else {
2521 /* Check out a single file in a subdirectory. */
2522 err = got_path_dirname(tree_relpath, wt_relpath);
2523 if (err)
2524 return err;
2525 if (asprintf(&in_repo_path, "%s%s%s",
2526 worktree->path_prefix, is_root_wt ? "" : "/",
2527 *tree_relpath) == -1) {
2528 err = got_error_from_errno("asprintf");
2529 goto done;
2532 err = got_object_id_by_path(tree_id, repo,
2533 worktree->base_commit_id, in_repo_path);
2534 } else {
2535 /* Check out all files within a subdirectory. */
2536 *tree_id = got_object_id_dup(id);
2537 if (*tree_id == NULL) {
2538 err = got_error_from_errno("got_object_id_dup");
2539 goto done;
2541 *tree_relpath = strdup(wt_relpath);
2542 if (*tree_relpath == NULL) {
2543 err = got_error_from_errno("strdup");
2544 goto done;
2547 done:
2548 free(id);
2549 free(in_repo_path);
2550 if (err) {
2551 *entry_type = GOT_OBJ_TYPE_ANY;
2552 free(*tree_relpath);
2553 *tree_relpath = NULL;
2554 free(*tree_id);
2555 *tree_id = NULL;
2557 return err;
2560 static const struct got_error *
2561 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2562 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2563 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2564 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2566 const struct got_error *err = NULL;
2567 struct got_commit_object *commit = NULL;
2568 struct got_tree_object *tree = NULL;
2569 struct got_fileindex_diff_tree_cb diff_cb;
2570 struct diff_cb_arg arg;
2572 err = ref_base_commit(worktree, repo);
2573 if (err) {
2574 if (!(err->code == GOT_ERR_ERRNO &&
2575 (errno == EACCES || errno == EROFS)))
2576 goto done;
2577 err = (*progress_cb)(progress_arg,
2578 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2579 if (err)
2580 return err;
2583 err = got_object_open_as_commit(&commit, repo,
2584 worktree->base_commit_id);
2585 if (err)
2586 goto done;
2588 err = got_object_open_as_tree(&tree, repo, tree_id);
2589 if (err)
2590 goto done;
2592 if (entry_name &&
2593 got_object_tree_find_entry(tree, entry_name) == NULL) {
2594 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2595 goto done;
2598 diff_cb.diff_old_new = diff_old_new;
2599 diff_cb.diff_old = diff_old;
2600 diff_cb.diff_new = diff_new;
2601 arg.fileindex = fileindex;
2602 arg.worktree = worktree;
2603 arg.repo = repo;
2604 arg.progress_cb = progress_cb;
2605 arg.progress_arg = progress_arg;
2606 arg.cancel_cb = cancel_cb;
2607 arg.cancel_arg = cancel_arg;
2608 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2609 entry_name, repo, &diff_cb, &arg);
2610 done:
2611 if (tree)
2612 got_object_tree_close(tree);
2613 if (commit)
2614 got_object_commit_close(commit);
2615 return err;
2618 const struct got_error *
2619 got_worktree_checkout_files(struct got_worktree *worktree,
2620 struct got_pathlist_head *paths, struct got_repository *repo,
2621 got_worktree_checkout_cb progress_cb, void *progress_arg,
2622 got_cancel_cb cancel_cb, void *cancel_arg)
2624 const struct got_error *err = NULL, *sync_err, *unlockerr;
2625 struct got_commit_object *commit = NULL;
2626 struct got_tree_object *tree = NULL;
2627 struct got_fileindex *fileindex = NULL;
2628 char *fileindex_path = NULL;
2629 struct got_pathlist_entry *pe;
2630 struct tree_path_data {
2631 STAILQ_ENTRY(tree_path_data) entry;
2632 struct got_object_id *tree_id;
2633 int entry_type;
2634 char *relpath;
2635 char *entry_name;
2636 } *tpd = NULL;
2637 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2639 STAILQ_INIT(&tree_paths);
2641 err = lock_worktree(worktree, LOCK_EX);
2642 if (err)
2643 return err;
2645 /* Map all specified paths to in-repository trees. */
2646 TAILQ_FOREACH(pe, paths, entry) {
2647 tpd = malloc(sizeof(*tpd));
2648 if (tpd == NULL) {
2649 err = got_error_from_errno("malloc");
2650 goto done;
2653 err = find_tree_entry_for_checkout(&tpd->entry_type,
2654 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2655 if (err) {
2656 free(tpd);
2657 goto done;
2660 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2661 err = got_path_basename(&tpd->entry_name, pe->path);
2662 if (err) {
2663 free(tpd->relpath);
2664 free(tpd->tree_id);
2665 free(tpd);
2666 goto done;
2668 } else
2669 tpd->entry_name = NULL;
2671 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2675 * Read the file index.
2676 * Checking out files is supposed to be an idempotent operation.
2677 * If the on-disk file index is incomplete we will try to complete it.
2679 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2680 if (err)
2681 goto done;
2683 tpd = STAILQ_FIRST(&tree_paths);
2684 TAILQ_FOREACH(pe, paths, entry) {
2685 struct bump_base_commit_id_arg bbc_arg;
2687 err = checkout_files(worktree, fileindex, tpd->relpath,
2688 tpd->tree_id, tpd->entry_name, repo,
2689 progress_cb, progress_arg, cancel_cb, cancel_arg);
2690 if (err)
2691 break;
2693 bbc_arg.base_commit_id = worktree->base_commit_id;
2694 bbc_arg.entry_name = tpd->entry_name;
2695 bbc_arg.path = pe->path;
2696 bbc_arg.path_len = pe->path_len;
2697 bbc_arg.progress_cb = progress_cb;
2698 bbc_arg.progress_arg = progress_arg;
2699 err = got_fileindex_for_each_entry_safe(fileindex,
2700 bump_base_commit_id, &bbc_arg);
2701 if (err)
2702 break;
2704 tpd = STAILQ_NEXT(tpd, entry);
2706 sync_err = sync_fileindex(fileindex, fileindex_path);
2707 if (sync_err && err == NULL)
2708 err = sync_err;
2709 done:
2710 free(fileindex_path);
2711 if (tree)
2712 got_object_tree_close(tree);
2713 if (commit)
2714 got_object_commit_close(commit);
2715 if (fileindex)
2716 got_fileindex_free(fileindex);
2717 while (!STAILQ_EMPTY(&tree_paths)) {
2718 tpd = STAILQ_FIRST(&tree_paths);
2719 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2720 free(tpd->relpath);
2721 free(tpd->tree_id);
2722 free(tpd);
2724 unlockerr = lock_worktree(worktree, LOCK_SH);
2725 if (unlockerr && err == NULL)
2726 err = unlockerr;
2727 return err;
2730 struct merge_file_cb_arg {
2731 struct got_worktree *worktree;
2732 struct got_fileindex *fileindex;
2733 got_worktree_checkout_cb progress_cb;
2734 void *progress_arg;
2735 got_cancel_cb cancel_cb;
2736 void *cancel_arg;
2737 const char *label_orig;
2738 struct got_object_id *commit_id2;
2739 int allow_bad_symlinks;
2742 static const struct got_error *
2743 merge_file_cb(void *arg, struct got_blob_object *blob1,
2744 struct got_blob_object *blob2, struct got_object_id *id1,
2745 struct got_object_id *id2, const char *path1, const char *path2,
2746 mode_t mode1, mode_t mode2, struct got_repository *repo)
2748 static const struct got_error *err = NULL;
2749 struct merge_file_cb_arg *a = arg;
2750 struct got_fileindex_entry *ie;
2751 char *ondisk_path = NULL;
2752 struct stat sb;
2753 unsigned char status;
2754 int local_changes_subsumed;
2755 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2756 char *id_str = NULL, *label_deriv2 = NULL;
2758 if (blob1 && blob2) {
2759 ie = got_fileindex_entry_get(a->fileindex, path2,
2760 strlen(path2));
2761 if (ie == NULL)
2762 return (*a->progress_cb)(a->progress_arg,
2763 GOT_STATUS_MISSING, path2);
2765 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2766 path2) == -1)
2767 return got_error_from_errno("asprintf");
2769 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2770 repo);
2771 if (err)
2772 goto done;
2774 if (status == GOT_STATUS_DELETE) {
2775 err = (*a->progress_cb)(a->progress_arg,
2776 GOT_STATUS_MERGE, path2);
2777 goto done;
2779 if (status != GOT_STATUS_NO_CHANGE &&
2780 status != GOT_STATUS_MODIFY &&
2781 status != GOT_STATUS_CONFLICT &&
2782 status != GOT_STATUS_ADD) {
2783 err = (*a->progress_cb)(a->progress_arg, status, path2);
2784 goto done;
2787 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2788 char *link_target2;
2789 err = got_object_blob_read_to_str(&link_target2, blob2);
2790 if (err)
2791 goto done;
2792 err = merge_symlink(a->worktree, blob1, ondisk_path,
2793 path2, a->label_orig, link_target2, a->commit_id2,
2794 repo, a->progress_cb, a->progress_arg);
2795 free(link_target2);
2796 } else {
2797 int fd;
2799 f_orig = got_opentemp();
2800 if (f_orig == NULL) {
2801 err = got_error_from_errno("got_opentemp");
2802 goto done;
2804 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2805 f_orig, blob1);
2806 if (err)
2807 goto done;
2809 f_deriv2 = got_opentemp();
2810 if (f_deriv2 == NULL)
2811 goto done;
2812 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2813 f_deriv2, blob2);
2814 if (err)
2815 goto done;
2817 fd = open(ondisk_path,
2818 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2819 if (fd == -1) {
2820 err = got_error_from_errno2("open",
2821 ondisk_path);
2822 goto done;
2824 f_deriv = fdopen(fd, "r");
2825 if (f_deriv == NULL) {
2826 err = got_error_from_errno2("fdopen",
2827 ondisk_path);
2828 close(fd);
2829 goto done;
2831 err = got_object_id_str(&id_str, a->commit_id2);
2832 if (err)
2833 goto done;
2834 if (asprintf(&label_deriv2, "%s: commit %s",
2835 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2836 err = got_error_from_errno("asprintf");
2837 goto done;
2839 err = merge_file(&local_changes_subsumed, a->worktree,
2840 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2841 sb.st_mode, a->label_orig, NULL, label_deriv2,
2842 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2843 a->progress_cb, a->progress_arg);
2845 } else if (blob1) {
2846 ie = got_fileindex_entry_get(a->fileindex, path1,
2847 strlen(path1));
2848 if (ie == NULL)
2849 return (*a->progress_cb)(a->progress_arg,
2850 GOT_STATUS_MISSING, path1);
2852 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2853 path1) == -1)
2854 return got_error_from_errno("asprintf");
2856 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2857 repo);
2858 if (err)
2859 goto done;
2861 switch (status) {
2862 case GOT_STATUS_NO_CHANGE:
2863 err = (*a->progress_cb)(a->progress_arg,
2864 GOT_STATUS_DELETE, path1);
2865 if (err)
2866 goto done;
2867 err = remove_ondisk_file(a->worktree->root_path, path1);
2868 if (err)
2869 goto done;
2870 if (ie)
2871 got_fileindex_entry_mark_deleted_from_disk(ie);
2872 break;
2873 case GOT_STATUS_DELETE:
2874 case GOT_STATUS_MISSING:
2875 err = (*a->progress_cb)(a->progress_arg,
2876 GOT_STATUS_DELETE, path1);
2877 if (err)
2878 goto done;
2879 if (ie)
2880 got_fileindex_entry_mark_deleted_from_disk(ie);
2881 break;
2882 case GOT_STATUS_ADD: {
2883 struct got_object_id *id;
2884 FILE *blob1_f;
2885 off_t blob1_size;
2887 * Delete the added file only if its content already
2888 * exists in the repository.
2890 err = got_object_blob_file_create(&id, &blob1_f,
2891 &blob1_size, path1);
2892 if (err)
2893 goto done;
2894 if (got_object_id_cmp(id, id1) == 0) {
2895 err = (*a->progress_cb)(a->progress_arg,
2896 GOT_STATUS_DELETE, path1);
2897 if (err)
2898 goto done;
2899 err = remove_ondisk_file(a->worktree->root_path,
2900 path1);
2901 if (err)
2902 goto done;
2903 if (ie)
2904 got_fileindex_entry_remove(a->fileindex,
2905 ie);
2906 } else {
2907 err = (*a->progress_cb)(a->progress_arg,
2908 GOT_STATUS_CANNOT_DELETE, path1);
2910 if (fclose(blob1_f) == EOF && err == NULL)
2911 err = got_error_from_errno("fclose");
2912 free(id);
2913 if (err)
2914 goto done;
2915 break;
2917 case GOT_STATUS_MODIFY:
2918 case GOT_STATUS_CONFLICT:
2919 err = (*a->progress_cb)(a->progress_arg,
2920 GOT_STATUS_CANNOT_DELETE, path1);
2921 if (err)
2922 goto done;
2923 break;
2924 case GOT_STATUS_OBSTRUCTED:
2925 err = (*a->progress_cb)(a->progress_arg, status, path1);
2926 if (err)
2927 goto done;
2928 break;
2929 default:
2930 break;
2932 } else if (blob2) {
2933 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2934 path2) == -1)
2935 return got_error_from_errno("asprintf");
2936 ie = got_fileindex_entry_get(a->fileindex, path2,
2937 strlen(path2));
2938 if (ie) {
2939 err = get_file_status(&status, &sb, ie, ondisk_path,
2940 -1, NULL, repo);
2941 if (err)
2942 goto done;
2943 if (status != GOT_STATUS_NO_CHANGE &&
2944 status != GOT_STATUS_MODIFY &&
2945 status != GOT_STATUS_CONFLICT &&
2946 status != GOT_STATUS_ADD) {
2947 err = (*a->progress_cb)(a->progress_arg,
2948 status, path2);
2949 goto done;
2951 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2952 char *link_target2;
2953 err = got_object_blob_read_to_str(&link_target2,
2954 blob2);
2955 if (err)
2956 goto done;
2957 err = merge_symlink(a->worktree, NULL,
2958 ondisk_path, path2, a->label_orig,
2959 link_target2, a->commit_id2, repo,
2960 a->progress_cb, a->progress_arg);
2961 free(link_target2);
2962 } else if (S_ISREG(sb.st_mode)) {
2963 err = merge_blob(&local_changes_subsumed,
2964 a->worktree, NULL, ondisk_path, path2,
2965 sb.st_mode, a->label_orig, blob2,
2966 a->commit_id2, repo, a->progress_cb,
2967 a->progress_arg);
2968 } else {
2969 err = got_error_path(ondisk_path,
2970 GOT_ERR_FILE_OBSTRUCTED);
2972 if (err)
2973 goto done;
2974 if (status == GOT_STATUS_DELETE) {
2975 err = got_fileindex_entry_update(ie,
2976 a->worktree->root_fd, path2, blob2->id.sha1,
2977 a->worktree->base_commit_id->sha1, 0);
2978 if (err)
2979 goto done;
2981 } else {
2982 int is_bad_symlink = 0;
2983 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2984 if (S_ISLNK(mode2)) {
2985 err = install_symlink(&is_bad_symlink,
2986 a->worktree, ondisk_path, path2, blob2, 0,
2987 0, 1, a->allow_bad_symlinks, repo,
2988 a->progress_cb, a->progress_arg);
2989 } else {
2990 err = install_blob(a->worktree, ondisk_path, path2,
2991 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2992 a->progress_cb, a->progress_arg);
2994 if (err)
2995 goto done;
2996 err = got_fileindex_entry_alloc(&ie, path2);
2997 if (err)
2998 goto done;
2999 err = got_fileindex_entry_update(ie,
3000 a->worktree->root_fd, path2, NULL, NULL, 1);
3001 if (err) {
3002 got_fileindex_entry_free(ie);
3003 goto done;
3005 err = got_fileindex_entry_add(a->fileindex, ie);
3006 if (err) {
3007 got_fileindex_entry_free(ie);
3008 goto done;
3010 if (is_bad_symlink) {
3011 got_fileindex_entry_filetype_set(ie,
3012 GOT_FILEIDX_MODE_BAD_SYMLINK);
3016 done:
3017 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3018 err = got_error_from_errno("fclose");
3019 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3020 err = got_error_from_errno("fclose");
3021 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3022 err = got_error_from_errno("fclose");
3023 free(id_str);
3024 free(label_deriv2);
3025 free(ondisk_path);
3026 return err;
3029 static const struct got_error *
3030 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3032 struct got_worktree *worktree = arg;
3034 /* Reject merges into a work tree with mixed base commits. */
3035 if (got_fileindex_entry_has_commit(ie) &&
3036 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3037 SHA1_DIGEST_LENGTH) != 0)
3038 return got_error(GOT_ERR_MIXED_COMMITS);
3040 return NULL;
3043 struct check_merge_conflicts_arg {
3044 struct got_worktree *worktree;
3045 struct got_fileindex *fileindex;
3046 struct got_repository *repo;
3049 static const struct got_error *
3050 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3051 struct got_blob_object *blob2, struct got_object_id *id1,
3052 struct got_object_id *id2, const char *path1, const char *path2,
3053 mode_t mode1, mode_t mode2, struct got_repository *repo)
3055 const struct got_error *err = NULL;
3056 struct check_merge_conflicts_arg *a = arg;
3057 unsigned char status;
3058 struct stat sb;
3059 struct got_fileindex_entry *ie;
3060 const char *path = path2 ? path2 : path1;
3061 struct got_object_id *id = id2 ? id2 : id1;
3062 char *ondisk_path;
3064 if (id == NULL)
3065 return NULL;
3067 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3068 if (ie == NULL)
3069 return NULL;
3071 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3072 == -1)
3073 return got_error_from_errno("asprintf");
3075 /* Reject merges into a work tree with conflicted files. */
3076 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3077 free(ondisk_path);
3078 if (err)
3079 return err;
3080 if (status == GOT_STATUS_CONFLICT)
3081 return got_error(GOT_ERR_CONFLICTS);
3083 return NULL;
3086 static const struct got_error *
3087 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3088 const char *fileindex_path, struct got_object_id *commit_id1,
3089 struct got_object_id *commit_id2, struct got_repository *repo,
3090 got_worktree_checkout_cb progress_cb, void *progress_arg,
3091 got_cancel_cb cancel_cb, void *cancel_arg)
3093 const struct got_error *err = NULL, *sync_err;
3094 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3095 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3096 struct check_merge_conflicts_arg cmc_arg;
3097 struct merge_file_cb_arg arg;
3098 char *label_orig = NULL;
3100 if (commit_id1) {
3101 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3102 worktree->path_prefix);
3103 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3104 goto done;
3106 if (tree_id1) {
3107 char *id_str;
3109 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3110 if (err)
3111 goto done;
3113 err = got_object_id_str(&id_str, commit_id1);
3114 if (err)
3115 goto done;
3117 if (asprintf(&label_orig, "%s: commit %s",
3118 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3119 err = got_error_from_errno("asprintf");
3120 free(id_str);
3121 goto done;
3123 free(id_str);
3126 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3127 worktree->path_prefix);
3128 if (err)
3129 goto done;
3131 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3132 if (err)
3133 goto done;
3135 cmc_arg.worktree = worktree;
3136 cmc_arg.fileindex = fileindex;
3137 cmc_arg.repo = repo;
3138 err = got_diff_tree(tree1, tree2, "", "", repo,
3139 check_merge_conflicts, &cmc_arg, 0);
3140 if (err)
3141 goto done;
3143 arg.worktree = worktree;
3144 arg.fileindex = fileindex;
3145 arg.progress_cb = progress_cb;
3146 arg.progress_arg = progress_arg;
3147 arg.cancel_cb = cancel_cb;
3148 arg.cancel_arg = cancel_arg;
3149 arg.label_orig = label_orig;
3150 arg.commit_id2 = commit_id2;
3151 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3152 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3153 sync_err = sync_fileindex(fileindex, fileindex_path);
3154 if (sync_err && err == NULL)
3155 err = sync_err;
3156 done:
3157 if (tree1)
3158 got_object_tree_close(tree1);
3159 if (tree2)
3160 got_object_tree_close(tree2);
3161 free(label_orig);
3162 return err;
3165 const struct got_error *
3166 got_worktree_merge_files(struct got_worktree *worktree,
3167 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3168 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3169 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3171 const struct got_error *err, *unlockerr;
3172 char *fileindex_path = NULL;
3173 struct got_fileindex *fileindex = NULL;
3175 err = lock_worktree(worktree, LOCK_EX);
3176 if (err)
3177 return err;
3179 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3180 if (err)
3181 goto done;
3183 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3184 worktree);
3185 if (err)
3186 goto done;
3188 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3189 commit_id2, repo, progress_cb, progress_arg,
3190 cancel_cb, cancel_arg);
3191 done:
3192 if (fileindex)
3193 got_fileindex_free(fileindex);
3194 free(fileindex_path);
3195 unlockerr = lock_worktree(worktree, LOCK_SH);
3196 if (unlockerr && err == NULL)
3197 err = unlockerr;
3198 return err;
3201 struct diff_dir_cb_arg {
3202 struct got_fileindex *fileindex;
3203 struct got_worktree *worktree;
3204 const char *status_path;
3205 size_t status_path_len;
3206 struct got_repository *repo;
3207 got_worktree_status_cb status_cb;
3208 void *status_arg;
3209 got_cancel_cb cancel_cb;
3210 void *cancel_arg;
3211 /* A pathlist containing per-directory pathlists of ignore patterns. */
3212 struct got_pathlist_head *ignores;
3213 int report_unchanged;
3214 int no_ignores;
3217 static const struct got_error *
3218 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3219 int dirfd, const char *de_name,
3220 got_worktree_status_cb status_cb, void *status_arg,
3221 struct got_repository *repo, int report_unchanged)
3223 const struct got_error *err = NULL;
3224 unsigned char status = GOT_STATUS_NO_CHANGE;
3225 unsigned char staged_status = get_staged_status(ie);
3226 struct stat sb;
3227 struct got_object_id blob_id, commit_id, staged_blob_id;
3228 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3229 struct got_object_id *staged_blob_idp = NULL;
3231 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3232 if (err)
3233 return err;
3235 if (status == GOT_STATUS_NO_CHANGE &&
3236 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3237 return NULL;
3239 if (got_fileindex_entry_has_blob(ie)) {
3240 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3241 blob_idp = &blob_id;
3243 if (got_fileindex_entry_has_commit(ie)) {
3244 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3245 commit_idp = &commit_id;
3247 if (staged_status == GOT_STATUS_ADD ||
3248 staged_status == GOT_STATUS_MODIFY) {
3249 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3250 SHA1_DIGEST_LENGTH);
3251 staged_blob_idp = &staged_blob_id;
3254 return (*status_cb)(status_arg, status, staged_status,
3255 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3258 static const struct got_error *
3259 status_old_new(void *arg, struct got_fileindex_entry *ie,
3260 struct dirent *de, const char *parent_path, int dirfd)
3262 const struct got_error *err = NULL;
3263 struct diff_dir_cb_arg *a = arg;
3264 char *abspath;
3266 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3267 return got_error(GOT_ERR_CANCELLED);
3269 if (got_path_cmp(parent_path, a->status_path,
3270 strlen(parent_path), a->status_path_len) != 0 &&
3271 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3272 return NULL;
3274 if (parent_path[0]) {
3275 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3276 parent_path, de->d_name) == -1)
3277 return got_error_from_errno("asprintf");
3278 } else {
3279 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3280 de->d_name) == -1)
3281 return got_error_from_errno("asprintf");
3284 err = report_file_status(ie, abspath, dirfd, de->d_name,
3285 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3286 free(abspath);
3287 return err;
3290 static const struct got_error *
3291 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3293 struct diff_dir_cb_arg *a = arg;
3294 struct got_object_id blob_id, commit_id;
3295 unsigned char status;
3297 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3298 return got_error(GOT_ERR_CANCELLED);
3300 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3301 return NULL;
3303 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3304 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3305 if (got_fileindex_entry_has_file_on_disk(ie))
3306 status = GOT_STATUS_MISSING;
3307 else
3308 status = GOT_STATUS_DELETE;
3309 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3310 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3313 void
3314 free_ignorelist(struct got_pathlist_head *ignorelist)
3316 struct got_pathlist_entry *pe;
3318 TAILQ_FOREACH(pe, ignorelist, entry)
3319 free((char *)pe->path);
3320 got_pathlist_free(ignorelist);
3323 void
3324 free_ignores(struct got_pathlist_head *ignores)
3326 struct got_pathlist_entry *pe;
3328 TAILQ_FOREACH(pe, ignores, entry) {
3329 struct got_pathlist_head *ignorelist = pe->data;
3330 free_ignorelist(ignorelist);
3331 free((char *)pe->path);
3333 got_pathlist_free(ignores);
3336 static const struct got_error *
3337 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3339 const struct got_error *err = NULL;
3340 struct got_pathlist_entry *pe = NULL;
3341 struct got_pathlist_head *ignorelist;
3342 char *line = NULL, *pattern, *dirpath = NULL;
3343 size_t linesize = 0;
3344 ssize_t linelen;
3346 ignorelist = calloc(1, sizeof(*ignorelist));
3347 if (ignorelist == NULL)
3348 return got_error_from_errno("calloc");
3349 TAILQ_INIT(ignorelist);
3351 while ((linelen = getline(&line, &linesize, f)) != -1) {
3352 if (linelen > 0 && line[linelen - 1] == '\n')
3353 line[linelen - 1] = '\0';
3355 /* Git's ignores may contain comments. */
3356 if (line[0] == '#')
3357 continue;
3359 /* Git's negated patterns are not (yet?) supported. */
3360 if (line[0] == '!')
3361 continue;
3363 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3364 line) == -1) {
3365 err = got_error_from_errno("asprintf");
3366 goto done;
3368 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3369 if (err)
3370 goto done;
3372 if (ferror(f)) {
3373 err = got_error_from_errno("getline");
3374 goto done;
3377 dirpath = strdup(path);
3378 if (dirpath == NULL) {
3379 err = got_error_from_errno("strdup");
3380 goto done;
3382 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3383 done:
3384 free(line);
3385 if (err || pe == NULL) {
3386 free(dirpath);
3387 free_ignorelist(ignorelist);
3389 return err;
3392 int
3393 match_ignores(struct got_pathlist_head *ignores, const char *path)
3395 struct got_pathlist_entry *pe;
3397 /* Handle patterns which match in all directories. */
3398 TAILQ_FOREACH(pe, ignores, entry) {
3399 struct got_pathlist_head *ignorelist = pe->data;
3400 struct got_pathlist_entry *pi;
3402 TAILQ_FOREACH(pi, ignorelist, entry) {
3403 const char *p, *pattern = pi->path;
3405 if (strncmp(pattern, "**/", 3) != 0)
3406 continue;
3407 pattern += 3;
3408 p = path;
3409 while (*p) {
3410 if (fnmatch(pattern, p,
3411 FNM_PATHNAME | FNM_LEADING_DIR)) {
3412 /* Retry in next directory. */
3413 while (*p && *p != '/')
3414 p++;
3415 while (*p == '/')
3416 p++;
3417 continue;
3419 return 1;
3425 * The ignores pathlist contains ignore lists from children before
3426 * parents, so we can find the most specific ignorelist by walking
3427 * ignores backwards.
3429 pe = TAILQ_LAST(ignores, got_pathlist_head);
3430 while (pe) {
3431 if (got_path_is_child(path, pe->path, pe->path_len)) {
3432 struct got_pathlist_head *ignorelist = pe->data;
3433 struct got_pathlist_entry *pi;
3434 TAILQ_FOREACH(pi, ignorelist, entry) {
3435 const char *pattern = pi->path;
3436 int flags = FNM_LEADING_DIR;
3437 if (strstr(pattern, "/**/") == NULL)
3438 flags |= FNM_PATHNAME;
3439 if (fnmatch(pattern, path, flags))
3440 continue;
3441 return 1;
3444 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3447 return 0;
3450 static const struct got_error *
3451 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3452 const char *path, int dirfd, const char *ignores_filename)
3454 const struct got_error *err = NULL;
3455 char *ignorespath;
3456 int fd = -1;
3457 FILE *ignoresfile = NULL;
3459 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3460 path[0] ? "/" : "", ignores_filename) == -1)
3461 return got_error_from_errno("asprintf");
3463 if (dirfd != -1) {
3464 fd = openat(dirfd, ignores_filename,
3465 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3466 if (fd == -1) {
3467 if (errno != ENOENT && errno != EACCES)
3468 err = got_error_from_errno2("openat",
3469 ignorespath);
3470 } else {
3471 ignoresfile = fdopen(fd, "r");
3472 if (ignoresfile == NULL)
3473 err = got_error_from_errno2("fdopen",
3474 ignorespath);
3475 else {
3476 fd = -1;
3477 err = read_ignores(ignores, path, ignoresfile);
3480 } else {
3481 ignoresfile = fopen(ignorespath, "re");
3482 if (ignoresfile == NULL) {
3483 if (errno != ENOENT && errno != EACCES)
3484 err = got_error_from_errno2("fopen",
3485 ignorespath);
3486 } else
3487 err = read_ignores(ignores, path, ignoresfile);
3490 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3491 err = got_error_from_errno2("fclose", path);
3492 if (fd != -1 && close(fd) == -1 && err == NULL)
3493 err = got_error_from_errno2("close", path);
3494 free(ignorespath);
3495 return err;
3498 static const struct got_error *
3499 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3500 int dirfd)
3502 const struct got_error *err = NULL;
3503 struct diff_dir_cb_arg *a = arg;
3504 char *path = NULL;
3506 if (ignore != NULL)
3507 *ignore = 0;
3509 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3510 return got_error(GOT_ERR_CANCELLED);
3512 if (parent_path[0]) {
3513 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3514 return got_error_from_errno("asprintf");
3515 } else {
3516 path = de->d_name;
3519 if (de->d_type == DT_DIR) {
3520 if (!a->no_ignores && ignore != NULL &&
3521 match_ignores(a->ignores, path))
3522 *ignore = 1;
3523 } else if (!match_ignores(a->ignores, path) &&
3524 got_path_is_child(path, a->status_path, a->status_path_len))
3525 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3526 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3527 if (parent_path[0])
3528 free(path);
3529 return err;
3532 static const struct got_error *
3533 status_traverse(void *arg, const char *path, int dirfd)
3535 const struct got_error *err = NULL;
3536 struct diff_dir_cb_arg *a = arg;
3538 if (a->no_ignores)
3539 return NULL;
3541 err = add_ignores(a->ignores, a->worktree->root_path,
3542 path, dirfd, ".cvsignore");
3543 if (err)
3544 return err;
3546 err = add_ignores(a->ignores, a->worktree->root_path, path,
3547 dirfd, ".gitignore");
3549 return err;
3552 static const struct got_error *
3553 report_single_file_status(const char *path, const char *ondisk_path,
3554 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3555 void *status_arg, struct got_repository *repo, int report_unchanged,
3556 struct got_pathlist_head *ignores, int no_ignores)
3558 struct got_fileindex_entry *ie;
3559 struct stat sb;
3561 if (!no_ignores && match_ignores(ignores, path))
3562 return NULL;
3564 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3565 if (ie)
3566 return report_file_status(ie, ondisk_path, -1, NULL,
3567 status_cb, status_arg, repo, report_unchanged);
3569 if (lstat(ondisk_path, &sb) == -1) {
3570 if (errno != ENOENT)
3571 return got_error_from_errno2("lstat", ondisk_path);
3572 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3573 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3576 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3577 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3578 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3580 return NULL;
3583 static const struct got_error *
3584 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3585 const char *root_path, const char *path)
3587 const struct got_error *err;
3588 char *parent_path, *next_parent_path = NULL;
3590 err = add_ignores(ignores, root_path, "", -1,
3591 ".cvsignore");
3592 if (err)
3593 return err;
3595 err = add_ignores(ignores, root_path, "", -1,
3596 ".gitignore");
3597 if (err)
3598 return err;
3600 err = got_path_dirname(&parent_path, path);
3601 if (err) {
3602 if (err->code == GOT_ERR_BAD_PATH)
3603 return NULL; /* cannot traverse parent */
3604 return err;
3606 for (;;) {
3607 err = add_ignores(ignores, root_path, parent_path, -1,
3608 ".cvsignore");
3609 if (err)
3610 break;
3611 err = add_ignores(ignores, root_path, parent_path, -1,
3612 ".gitignore");
3613 if (err)
3614 break;
3615 err = got_path_dirname(&next_parent_path, parent_path);
3616 if (err) {
3617 if (err->code == GOT_ERR_BAD_PATH)
3618 err = NULL; /* traversed everything */
3619 break;
3621 if (got_path_is_root_dir(parent_path))
3622 break;
3623 free(parent_path);
3624 parent_path = next_parent_path;
3625 next_parent_path = NULL;
3628 free(parent_path);
3629 free(next_parent_path);
3630 return err;
3633 static const struct got_error *
3634 worktree_status(struct got_worktree *worktree, const char *path,
3635 struct got_fileindex *fileindex, struct got_repository *repo,
3636 got_worktree_status_cb status_cb, void *status_arg,
3637 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3638 int report_unchanged)
3640 const struct got_error *err = NULL;
3641 int fd = -1;
3642 struct got_fileindex_diff_dir_cb fdiff_cb;
3643 struct diff_dir_cb_arg arg;
3644 char *ondisk_path = NULL;
3645 struct got_pathlist_head ignores;
3647 TAILQ_INIT(&ignores);
3649 if (asprintf(&ondisk_path, "%s%s%s",
3650 worktree->root_path, path[0] ? "/" : "", path) == -1)
3651 return got_error_from_errno("asprintf");
3653 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3654 if (fd == -1) {
3655 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3656 !got_err_open_nofollow_on_symlink())
3657 err = got_error_from_errno2("open", ondisk_path);
3658 else {
3659 if (!no_ignores) {
3660 err = add_ignores_from_parent_paths(&ignores,
3661 worktree->root_path, ondisk_path);
3662 if (err)
3663 goto done;
3665 err = report_single_file_status(path, ondisk_path,
3666 fileindex, status_cb, status_arg, repo,
3667 report_unchanged, &ignores, no_ignores);
3669 } else {
3670 fdiff_cb.diff_old_new = status_old_new;
3671 fdiff_cb.diff_old = status_old;
3672 fdiff_cb.diff_new = status_new;
3673 fdiff_cb.diff_traverse = status_traverse;
3674 arg.fileindex = fileindex;
3675 arg.worktree = worktree;
3676 arg.status_path = path;
3677 arg.status_path_len = strlen(path);
3678 arg.repo = repo;
3679 arg.status_cb = status_cb;
3680 arg.status_arg = status_arg;
3681 arg.cancel_cb = cancel_cb;
3682 arg.cancel_arg = cancel_arg;
3683 arg.report_unchanged = report_unchanged;
3684 arg.no_ignores = no_ignores;
3685 if (!no_ignores) {
3686 err = add_ignores_from_parent_paths(&ignores,
3687 worktree->root_path, path);
3688 if (err)
3689 goto done;
3691 arg.ignores = &ignores;
3692 err = got_fileindex_diff_dir(fileindex, fd,
3693 worktree->root_path, path, repo, &fdiff_cb, &arg);
3695 done:
3696 free_ignores(&ignores);
3697 if (fd != -1 && close(fd) == -1 && err == NULL)
3698 err = got_error_from_errno("close");
3699 free(ondisk_path);
3700 return err;
3703 const struct got_error *
3704 got_worktree_status(struct got_worktree *worktree,
3705 struct got_pathlist_head *paths, struct got_repository *repo,
3706 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3707 got_cancel_cb cancel_cb, void *cancel_arg)
3709 const struct got_error *err = NULL;
3710 char *fileindex_path = NULL;
3711 struct got_fileindex *fileindex = NULL;
3712 struct got_pathlist_entry *pe;
3714 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3715 if (err)
3716 return err;
3718 TAILQ_FOREACH(pe, paths, entry) {
3719 err = worktree_status(worktree, pe->path, fileindex, repo,
3720 status_cb, status_arg, cancel_cb, cancel_arg,
3721 no_ignores, 0);
3722 if (err)
3723 break;
3725 free(fileindex_path);
3726 got_fileindex_free(fileindex);
3727 return err;
3730 const struct got_error *
3731 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3732 const char *arg)
3734 const struct got_error *err = NULL;
3735 char *resolved = NULL, *cwd = NULL, *path = NULL;
3736 size_t len;
3737 struct stat sb;
3738 char *abspath = NULL;
3739 char canonpath[PATH_MAX];
3741 *wt_path = NULL;
3743 cwd = getcwd(NULL, 0);
3744 if (cwd == NULL)
3745 return got_error_from_errno("getcwd");
3747 if (lstat(arg, &sb) == -1) {
3748 if (errno != ENOENT) {
3749 err = got_error_from_errno2("lstat", arg);
3750 goto done;
3752 sb.st_mode = 0;
3754 if (S_ISLNK(sb.st_mode)) {
3756 * We cannot use realpath(3) with symlinks since we want to
3757 * operate on the symlink itself.
3758 * But we can make the path absolute, assuming it is relative
3759 * to the current working directory, and then canonicalize it.
3761 if (!got_path_is_absolute(arg)) {
3762 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3763 err = got_error_from_errno("asprintf");
3764 goto done;
3768 err = got_canonpath(abspath ? abspath : arg, canonpath,
3769 sizeof(canonpath));
3770 if (err)
3771 goto done;
3772 resolved = strdup(canonpath);
3773 if (resolved == NULL) {
3774 err = got_error_from_errno("strdup");
3775 goto done;
3777 } else {
3778 resolved = realpath(arg, NULL);
3779 if (resolved == NULL) {
3780 if (errno != ENOENT) {
3781 err = got_error_from_errno2("realpath", arg);
3782 goto done;
3784 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3785 err = got_error_from_errno("asprintf");
3786 goto done;
3788 err = got_canonpath(abspath, canonpath,
3789 sizeof(canonpath));
3790 if (err)
3791 goto done;
3792 resolved = strdup(canonpath);
3793 if (resolved == NULL) {
3794 err = got_error_from_errno("strdup");
3795 goto done;
3800 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3801 strlen(got_worktree_get_root_path(worktree)))) {
3802 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3803 goto done;
3806 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3807 err = got_path_skip_common_ancestor(&path,
3808 got_worktree_get_root_path(worktree), resolved);
3809 if (err)
3810 goto done;
3811 } else {
3812 path = strdup("");
3813 if (path == NULL) {
3814 err = got_error_from_errno("strdup");
3815 goto done;
3819 /* XXX status walk can't deal with trailing slash! */
3820 len = strlen(path);
3821 while (len > 0 && path[len - 1] == '/') {
3822 path[len - 1] = '\0';
3823 len--;
3825 done:
3826 free(abspath);
3827 free(resolved);
3828 free(cwd);
3829 if (err == NULL)
3830 *wt_path = path;
3831 else
3832 free(path);
3833 return err;
3836 struct schedule_addition_args {
3837 struct got_worktree *worktree;
3838 struct got_fileindex *fileindex;
3839 got_worktree_checkout_cb progress_cb;
3840 void *progress_arg;
3841 struct got_repository *repo;
3844 static const struct got_error *
3845 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3846 const char *relpath, struct got_object_id *blob_id,
3847 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3848 int dirfd, const char *de_name)
3850 struct schedule_addition_args *a = arg;
3851 const struct got_error *err = NULL;
3852 struct got_fileindex_entry *ie;
3853 struct stat sb;
3854 char *ondisk_path;
3856 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3857 relpath) == -1)
3858 return got_error_from_errno("asprintf");
3860 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3861 if (ie) {
3862 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3863 de_name, a->repo);
3864 if (err)
3865 goto done;
3866 /* Re-adding an existing entry is a no-op. */
3867 if (status == GOT_STATUS_ADD)
3868 goto done;
3869 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3870 if (err)
3871 goto done;
3874 if (status != GOT_STATUS_UNVERSIONED) {
3875 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3876 goto done;
3879 err = got_fileindex_entry_alloc(&ie, relpath);
3880 if (err)
3881 goto done;
3882 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3883 relpath, NULL, NULL, 1);
3884 if (err) {
3885 got_fileindex_entry_free(ie);
3886 goto done;
3888 err = got_fileindex_entry_add(a->fileindex, ie);
3889 if (err) {
3890 got_fileindex_entry_free(ie);
3891 goto done;
3893 done:
3894 free(ondisk_path);
3895 if (err)
3896 return err;
3897 if (status == GOT_STATUS_ADD)
3898 return NULL;
3899 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3902 const struct got_error *
3903 got_worktree_schedule_add(struct got_worktree *worktree,
3904 struct got_pathlist_head *paths,
3905 got_worktree_checkout_cb progress_cb, void *progress_arg,
3906 struct got_repository *repo, int no_ignores)
3908 struct got_fileindex *fileindex = NULL;
3909 char *fileindex_path = NULL;
3910 const struct got_error *err = NULL, *sync_err, *unlockerr;
3911 struct got_pathlist_entry *pe;
3912 struct schedule_addition_args saa;
3914 err = lock_worktree(worktree, LOCK_EX);
3915 if (err)
3916 return err;
3918 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3919 if (err)
3920 goto done;
3922 saa.worktree = worktree;
3923 saa.fileindex = fileindex;
3924 saa.progress_cb = progress_cb;
3925 saa.progress_arg = progress_arg;
3926 saa.repo = repo;
3928 TAILQ_FOREACH(pe, paths, entry) {
3929 err = worktree_status(worktree, pe->path, fileindex, repo,
3930 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3931 if (err)
3932 break;
3934 sync_err = sync_fileindex(fileindex, fileindex_path);
3935 if (sync_err && err == NULL)
3936 err = sync_err;
3937 done:
3938 free(fileindex_path);
3939 if (fileindex)
3940 got_fileindex_free(fileindex);
3941 unlockerr = lock_worktree(worktree, LOCK_SH);
3942 if (unlockerr && err == NULL)
3943 err = unlockerr;
3944 return err;
3947 struct schedule_deletion_args {
3948 struct got_worktree *worktree;
3949 struct got_fileindex *fileindex;
3950 got_worktree_delete_cb progress_cb;
3951 void *progress_arg;
3952 struct got_repository *repo;
3953 int delete_local_mods;
3954 int keep_on_disk;
3955 int ignore_missing_paths;
3956 const char *status_codes;
3959 static const struct got_error *
3960 schedule_for_deletion(void *arg, unsigned char status,
3961 unsigned char staged_status, const char *relpath,
3962 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3963 struct got_object_id *commit_id, int dirfd, const char *de_name)
3965 struct schedule_deletion_args *a = arg;
3966 const struct got_error *err = NULL;
3967 struct got_fileindex_entry *ie = NULL;
3968 struct stat sb;
3969 char *ondisk_path;
3971 if (status == GOT_STATUS_NONEXISTENT) {
3972 if (a->ignore_missing_paths)
3973 return NULL;
3974 return got_error_set_errno(ENOENT, relpath);
3977 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3978 if (ie == NULL)
3979 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
3981 staged_status = get_staged_status(ie);
3982 if (staged_status != GOT_STATUS_NO_CHANGE) {
3983 if (staged_status == GOT_STATUS_DELETE)
3984 return NULL;
3985 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3988 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3989 relpath) == -1)
3990 return got_error_from_errno("asprintf");
3992 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3993 a->repo);
3994 if (err)
3995 goto done;
3997 if (a->status_codes) {
3998 size_t ncodes = strlen(a->status_codes);
3999 int i;
4000 for (i = 0; i < ncodes ; i++) {
4001 if (status == a->status_codes[i])
4002 break;
4004 if (i == ncodes) {
4005 /* Do not delete files in non-matching status. */
4006 free(ondisk_path);
4007 return NULL;
4009 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4010 a->status_codes[i] != GOT_STATUS_MISSING) {
4011 static char msg[64];
4012 snprintf(msg, sizeof(msg),
4013 "invalid status code '%c'", a->status_codes[i]);
4014 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4015 goto done;
4019 if (status != GOT_STATUS_NO_CHANGE) {
4020 if (status == GOT_STATUS_DELETE)
4021 goto done;
4022 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4023 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4024 goto done;
4026 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4027 err = got_error_set_errno(ENOENT, relpath);
4028 goto done;
4030 if (status != GOT_STATUS_MODIFY &&
4031 status != GOT_STATUS_MISSING) {
4032 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4033 goto done;
4037 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4038 size_t root_len;
4040 if (dirfd != -1) {
4041 if (unlinkat(dirfd, de_name, 0) != 0) {
4042 err = got_error_from_errno2("unlinkat",
4043 ondisk_path);
4044 goto done;
4046 } else if (unlink(ondisk_path) != 0) {
4047 err = got_error_from_errno2("unlink", ondisk_path);
4048 goto done;
4051 root_len = strlen(a->worktree->root_path);
4052 do {
4053 char *parent;
4054 err = got_path_dirname(&parent, ondisk_path);
4055 if (err)
4056 goto done;
4057 free(ondisk_path);
4058 ondisk_path = parent;
4059 if (rmdir(ondisk_path) == -1) {
4060 if (errno != ENOTEMPTY)
4061 err = got_error_from_errno2("rmdir",
4062 ondisk_path);
4063 break;
4065 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4066 strlen(ondisk_path), root_len) != 0);
4069 got_fileindex_entry_mark_deleted_from_disk(ie);
4070 done:
4071 free(ondisk_path);
4072 if (err)
4073 return err;
4074 if (status == GOT_STATUS_DELETE)
4075 return NULL;
4076 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4077 staged_status, relpath);
4080 const struct got_error *
4081 got_worktree_schedule_delete(struct got_worktree *worktree,
4082 struct got_pathlist_head *paths, int delete_local_mods,
4083 const char *status_codes,
4084 got_worktree_delete_cb progress_cb, void *progress_arg,
4085 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4087 struct got_fileindex *fileindex = NULL;
4088 char *fileindex_path = NULL;
4089 const struct got_error *err = NULL, *sync_err, *unlockerr;
4090 struct got_pathlist_entry *pe;
4091 struct schedule_deletion_args sda;
4093 err = lock_worktree(worktree, LOCK_EX);
4094 if (err)
4095 return err;
4097 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4098 if (err)
4099 goto done;
4101 sda.worktree = worktree;
4102 sda.fileindex = fileindex;
4103 sda.progress_cb = progress_cb;
4104 sda.progress_arg = progress_arg;
4105 sda.repo = repo;
4106 sda.delete_local_mods = delete_local_mods;
4107 sda.keep_on_disk = keep_on_disk;
4108 sda.ignore_missing_paths = ignore_missing_paths;
4109 sda.status_codes = status_codes;
4111 TAILQ_FOREACH(pe, paths, entry) {
4112 err = worktree_status(worktree, pe->path, fileindex, repo,
4113 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4114 if (err)
4115 break;
4117 sync_err = sync_fileindex(fileindex, fileindex_path);
4118 if (sync_err && err == NULL)
4119 err = sync_err;
4120 done:
4121 free(fileindex_path);
4122 if (fileindex)
4123 got_fileindex_free(fileindex);
4124 unlockerr = lock_worktree(worktree, LOCK_SH);
4125 if (unlockerr && err == NULL)
4126 err = unlockerr;
4127 return err;
4130 static const struct got_error *
4131 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4133 const struct got_error *err = NULL;
4134 char *line = NULL;
4135 size_t linesize = 0, n;
4136 ssize_t linelen;
4138 linelen = getline(&line, &linesize, infile);
4139 if (linelen == -1) {
4140 if (ferror(infile)) {
4141 err = got_error_from_errno("getline");
4142 goto done;
4144 return NULL;
4146 if (outfile) {
4147 n = fwrite(line, 1, linelen, outfile);
4148 if (n != linelen) {
4149 err = got_ferror(outfile, GOT_ERR_IO);
4150 goto done;
4153 if (rejectfile) {
4154 n = fwrite(line, 1, linelen, rejectfile);
4155 if (n != linelen)
4156 err = got_ferror(outfile, GOT_ERR_IO);
4158 done:
4159 free(line);
4160 return err;
4163 static const struct got_error *
4164 skip_one_line(FILE *f)
4166 char *line = NULL;
4167 size_t linesize = 0;
4168 ssize_t linelen;
4170 linelen = getline(&line, &linesize, f);
4171 if (linelen == -1) {
4172 if (ferror(f))
4173 return got_error_from_errno("getline");
4174 return NULL;
4176 free(line);
4177 return NULL;
4180 static const struct got_error *
4181 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4182 int start_old, int end_old, int start_new, int end_new,
4183 FILE *outfile, FILE *rejectfile)
4185 const struct got_error *err;
4187 /* Copy old file's lines leading up to patch. */
4188 while (!feof(f1) && *line_cur1 < start_old) {
4189 err = copy_one_line(f1, outfile, NULL);
4190 if (err)
4191 return err;
4192 (*line_cur1)++;
4194 /* Skip new file's lines leading up to patch. */
4195 while (!feof(f2) && *line_cur2 < start_new) {
4196 if (rejectfile)
4197 err = copy_one_line(f2, NULL, rejectfile);
4198 else
4199 err = skip_one_line(f2);
4200 if (err)
4201 return err;
4202 (*line_cur2)++;
4204 /* Copy patched lines. */
4205 while (!feof(f2) && *line_cur2 <= end_new) {
4206 err = copy_one_line(f2, outfile, NULL);
4207 if (err)
4208 return err;
4209 (*line_cur2)++;
4211 /* Skip over old file's replaced lines. */
4212 while (!feof(f1) && *line_cur1 <= end_old) {
4213 if (rejectfile)
4214 err = copy_one_line(f1, NULL, rejectfile);
4215 else
4216 err = skip_one_line(f1);
4217 if (err)
4218 return err;
4219 (*line_cur1)++;
4222 return NULL;
4225 static const struct got_error *
4226 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4227 FILE *outfile, FILE *rejectfile)
4229 const struct got_error *err;
4231 if (outfile) {
4232 /* Copy old file's lines until EOF. */
4233 while (!feof(f1)) {
4234 err = copy_one_line(f1, outfile, NULL);
4235 if (err)
4236 return err;
4237 (*line_cur1)++;
4240 if (rejectfile) {
4241 /* Copy new file's lines until EOF. */
4242 while (!feof(f2)) {
4243 err = copy_one_line(f2, NULL, rejectfile);
4244 if (err)
4245 return err;
4246 (*line_cur2)++;
4250 return NULL;
4253 static const struct got_error *
4254 apply_or_reject_change(int *choice, int *nchunks_used,
4255 struct diff_result *diff_result, int n,
4256 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4257 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4258 got_worktree_patch_cb patch_cb, void *patch_arg)
4260 const struct got_error *err = NULL;
4261 struct diff_chunk_context cc = {};
4262 int start_old, end_old, start_new, end_new;
4263 FILE *hunkfile;
4264 struct diff_output_unidiff_state *diff_state;
4265 struct diff_input_info diff_info;
4266 int rc;
4268 *choice = GOT_PATCH_CHOICE_NONE;
4270 /* Get changed line numbers without context lines for copy_change(). */
4271 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4272 start_old = cc.left.start;
4273 end_old = cc.left.end;
4274 start_new = cc.right.start;
4275 end_new = cc.right.end;
4277 /* Get the same change with context lines for display. */
4278 memset(&cc, 0, sizeof(cc));
4279 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4281 memset(&diff_info, 0, sizeof(diff_info));
4282 diff_info.left_path = relpath;
4283 diff_info.right_path = relpath;
4285 diff_state = diff_output_unidiff_state_alloc();
4286 if (diff_state == NULL)
4287 return got_error_set_errno(ENOMEM,
4288 "diff_output_unidiff_state_alloc");
4290 hunkfile = got_opentemp();
4291 if (hunkfile == NULL) {
4292 err = got_error_from_errno("got_opentemp");
4293 goto done;
4296 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4297 diff_result, &cc);
4298 if (rc != DIFF_RC_OK) {
4299 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4300 goto done;
4303 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4304 err = got_ferror(hunkfile, GOT_ERR_IO);
4305 goto done;
4308 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4309 hunkfile, changeno, nchanges);
4310 if (err)
4311 goto done;
4313 switch (*choice) {
4314 case GOT_PATCH_CHOICE_YES:
4315 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4316 end_old, start_new, end_new, outfile, rejectfile);
4317 break;
4318 case GOT_PATCH_CHOICE_NO:
4319 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4320 end_old, start_new, end_new, rejectfile, outfile);
4321 break;
4322 case GOT_PATCH_CHOICE_QUIT:
4323 break;
4324 default:
4325 err = got_error(GOT_ERR_PATCH_CHOICE);
4326 break;
4328 done:
4329 diff_output_unidiff_state_free(diff_state);
4330 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4331 err = got_error_from_errno("fclose");
4332 return err;
4335 struct revert_file_args {
4336 struct got_worktree *worktree;
4337 struct got_fileindex *fileindex;
4338 got_worktree_checkout_cb progress_cb;
4339 void *progress_arg;
4340 got_worktree_patch_cb patch_cb;
4341 void *patch_arg;
4342 struct got_repository *repo;
4343 int unlink_added_files;
4346 static const struct got_error *
4347 create_patched_content(char **path_outfile, int reverse_patch,
4348 struct got_object_id *blob_id, const char *path2,
4349 int dirfd2, const char *de_name2,
4350 const char *relpath, struct got_repository *repo,
4351 got_worktree_patch_cb patch_cb, void *patch_arg)
4353 const struct got_error *err, *free_err;
4354 struct got_blob_object *blob = NULL;
4355 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4356 int fd2 = -1;
4357 char link_target[PATH_MAX];
4358 ssize_t link_len = 0;
4359 char *path1 = NULL, *id_str = NULL;
4360 struct stat sb2;
4361 struct got_diffreg_result *diffreg_result = NULL;
4362 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4363 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4365 *path_outfile = NULL;
4367 err = got_object_id_str(&id_str, blob_id);
4368 if (err)
4369 return err;
4371 if (dirfd2 != -1) {
4372 fd2 = openat(dirfd2, de_name2,
4373 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4374 if (fd2 == -1) {
4375 if (!got_err_open_nofollow_on_symlink()) {
4376 err = got_error_from_errno2("openat", path2);
4377 goto done;
4379 link_len = readlinkat(dirfd2, de_name2,
4380 link_target, sizeof(link_target));
4381 if (link_len == -1) {
4382 return got_error_from_errno2("readlinkat",
4383 path2);
4385 sb2.st_mode = S_IFLNK;
4386 sb2.st_size = link_len;
4388 } else {
4389 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4390 if (fd2 == -1) {
4391 if (!got_err_open_nofollow_on_symlink()) {
4392 err = got_error_from_errno2("open", path2);
4393 goto done;
4395 link_len = readlink(path2, link_target,
4396 sizeof(link_target));
4397 if (link_len == -1)
4398 return got_error_from_errno2("readlink", path2);
4399 sb2.st_mode = S_IFLNK;
4400 sb2.st_size = link_len;
4403 if (fd2 != -1) {
4404 if (fstat(fd2, &sb2) == -1) {
4405 err = got_error_from_errno2("fstat", path2);
4406 goto done;
4409 f2 = fdopen(fd2, "r");
4410 if (f2 == NULL) {
4411 err = got_error_from_errno2("fdopen", path2);
4412 goto done;
4414 fd2 = -1;
4415 } else {
4416 size_t n;
4417 f2 = got_opentemp();
4418 if (f2 == NULL) {
4419 err = got_error_from_errno2("got_opentemp", path2);
4420 goto done;
4422 n = fwrite(link_target, 1, link_len, f2);
4423 if (n != link_len) {
4424 err = got_ferror(f2, GOT_ERR_IO);
4425 goto done;
4427 if (fflush(f2) == EOF) {
4428 err = got_error_from_errno("fflush");
4429 goto done;
4431 rewind(f2);
4434 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4435 if (err)
4436 goto done;
4438 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4439 if (err)
4440 goto done;
4442 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4443 if (err)
4444 goto done;
4446 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4447 NULL);
4448 if (err)
4449 goto done;
4451 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4452 if (err)
4453 goto done;
4455 if (fseek(f1, 0L, SEEK_SET) == -1)
4456 return got_ferror(f1, GOT_ERR_IO);
4457 if (fseek(f2, 0L, SEEK_SET) == -1)
4458 return got_ferror(f2, GOT_ERR_IO);
4460 /* Count the number of actual changes in the diff result. */
4461 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4462 struct diff_chunk_context cc = {};
4463 diff_chunk_context_load_change(&cc, &nchunks_used,
4464 diffreg_result->result, n, 0);
4465 nchanges++;
4467 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4468 int choice;
4469 err = apply_or_reject_change(&choice, &nchunks_used,
4470 diffreg_result->result, n, relpath, f1, f2,
4471 &line_cur1, &line_cur2,
4472 reverse_patch ? NULL : outfile,
4473 reverse_patch ? outfile : NULL,
4474 ++i, nchanges, patch_cb, patch_arg);
4475 if (err)
4476 goto done;
4477 if (choice == GOT_PATCH_CHOICE_YES)
4478 have_content = 1;
4479 else if (choice == GOT_PATCH_CHOICE_QUIT)
4480 break;
4482 if (have_content) {
4483 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4484 reverse_patch ? NULL : outfile,
4485 reverse_patch ? outfile : NULL);
4486 if (err)
4487 goto done;
4489 if (!S_ISLNK(sb2.st_mode)) {
4490 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4491 err = got_error_from_errno2("fchmod", path2);
4492 goto done;
4496 done:
4497 free(id_str);
4498 if (blob)
4499 got_object_blob_close(blob);
4500 free_err = got_diffreg_result_free(diffreg_result);
4501 if (err == NULL)
4502 err = free_err;
4503 if (f1 && fclose(f1) == EOF && err == NULL)
4504 err = got_error_from_errno2("fclose", path1);
4505 if (f2 && fclose(f2) == EOF && err == NULL)
4506 err = got_error_from_errno2("fclose", path2);
4507 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4508 err = got_error_from_errno2("close", path2);
4509 if (outfile && fclose(outfile) == EOF && err == NULL)
4510 err = got_error_from_errno2("fclose", *path_outfile);
4511 if (path1 && unlink(path1) == -1 && err == NULL)
4512 err = got_error_from_errno2("unlink", path1);
4513 if (err || !have_content) {
4514 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4515 err = got_error_from_errno2("unlink", *path_outfile);
4516 free(*path_outfile);
4517 *path_outfile = NULL;
4519 free(path1);
4520 return err;
4523 static const struct got_error *
4524 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4525 const char *relpath, struct got_object_id *blob_id,
4526 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4527 int dirfd, const char *de_name)
4529 struct revert_file_args *a = arg;
4530 const struct got_error *err = NULL;
4531 char *parent_path = NULL;
4532 struct got_fileindex_entry *ie;
4533 struct got_tree_object *tree = NULL;
4534 struct got_object_id *tree_id = NULL;
4535 const struct got_tree_entry *te = NULL;
4536 char *tree_path = NULL, *te_name;
4537 char *ondisk_path = NULL, *path_content = NULL;
4538 struct got_blob_object *blob = NULL;
4540 /* Reverting a staged deletion is a no-op. */
4541 if (status == GOT_STATUS_DELETE &&
4542 staged_status != GOT_STATUS_NO_CHANGE)
4543 return NULL;
4545 if (status == GOT_STATUS_UNVERSIONED)
4546 return (*a->progress_cb)(a->progress_arg,
4547 GOT_STATUS_UNVERSIONED, relpath);
4549 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4550 if (ie == NULL)
4551 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4553 /* Construct in-repository path of tree which contains this blob. */
4554 err = got_path_dirname(&parent_path, ie->path);
4555 if (err) {
4556 if (err->code != GOT_ERR_BAD_PATH)
4557 goto done;
4558 parent_path = strdup("/");
4559 if (parent_path == NULL) {
4560 err = got_error_from_errno("strdup");
4561 goto done;
4564 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4565 tree_path = strdup(parent_path);
4566 if (tree_path == NULL) {
4567 err = got_error_from_errno("strdup");
4568 goto done;
4570 } else {
4571 if (got_path_is_root_dir(parent_path)) {
4572 tree_path = strdup(a->worktree->path_prefix);
4573 if (tree_path == NULL) {
4574 err = got_error_from_errno("strdup");
4575 goto done;
4577 } else {
4578 if (asprintf(&tree_path, "%s/%s",
4579 a->worktree->path_prefix, parent_path) == -1) {
4580 err = got_error_from_errno("asprintf");
4581 goto done;
4586 err = got_object_id_by_path(&tree_id, a->repo,
4587 a->worktree->base_commit_id, tree_path);
4588 if (err) {
4589 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4590 (status == GOT_STATUS_ADD ||
4591 staged_status == GOT_STATUS_ADD)))
4592 goto done;
4593 } else {
4594 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4595 if (err)
4596 goto done;
4598 err = got_path_basename(&te_name, ie->path);
4599 if (err)
4600 goto done;
4602 te = got_object_tree_find_entry(tree, te_name);
4603 free(te_name);
4604 if (te == NULL && status != GOT_STATUS_ADD &&
4605 staged_status != GOT_STATUS_ADD) {
4606 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4607 goto done;
4611 switch (status) {
4612 case GOT_STATUS_ADD:
4613 if (a->patch_cb) {
4614 int choice = GOT_PATCH_CHOICE_NONE;
4615 err = (*a->patch_cb)(&choice, a->patch_arg,
4616 status, ie->path, NULL, 1, 1);
4617 if (err)
4618 goto done;
4619 if (choice != GOT_PATCH_CHOICE_YES)
4620 break;
4622 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4623 ie->path);
4624 if (err)
4625 goto done;
4626 got_fileindex_entry_remove(a->fileindex, ie);
4627 if (a->unlink_added_files) {
4628 if (asprintf(&ondisk_path, "%s/%s",
4629 got_worktree_get_root_path(a->worktree),
4630 relpath) == -1) {
4631 err = got_error_from_errno("asprintf");
4632 goto done;
4634 if (unlink(ondisk_path) == -1) {
4635 err = got_error_from_errno2("unlink",
4636 ondisk_path);
4637 break;
4640 break;
4641 case GOT_STATUS_DELETE:
4642 if (a->patch_cb) {
4643 int choice = GOT_PATCH_CHOICE_NONE;
4644 err = (*a->patch_cb)(&choice, a->patch_arg,
4645 status, ie->path, NULL, 1, 1);
4646 if (err)
4647 goto done;
4648 if (choice != GOT_PATCH_CHOICE_YES)
4649 break;
4651 /* fall through */
4652 case GOT_STATUS_MODIFY:
4653 case GOT_STATUS_MODE_CHANGE:
4654 case GOT_STATUS_CONFLICT:
4655 case GOT_STATUS_MISSING: {
4656 struct got_object_id id;
4657 if (staged_status == GOT_STATUS_ADD ||
4658 staged_status == GOT_STATUS_MODIFY) {
4659 memcpy(id.sha1, ie->staged_blob_sha1,
4660 SHA1_DIGEST_LENGTH);
4661 } else
4662 memcpy(id.sha1, ie->blob_sha1,
4663 SHA1_DIGEST_LENGTH);
4664 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4665 if (err)
4666 goto done;
4668 if (asprintf(&ondisk_path, "%s/%s",
4669 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4670 err = got_error_from_errno("asprintf");
4671 goto done;
4674 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4675 status == GOT_STATUS_CONFLICT)) {
4676 int is_bad_symlink = 0;
4677 err = create_patched_content(&path_content, 1, &id,
4678 ondisk_path, dirfd, de_name, ie->path, a->repo,
4679 a->patch_cb, a->patch_arg);
4680 if (err || path_content == NULL)
4681 break;
4682 if (te && S_ISLNK(te->mode)) {
4683 if (unlink(path_content) == -1) {
4684 err = got_error_from_errno2("unlink",
4685 path_content);
4686 break;
4688 err = install_symlink(&is_bad_symlink,
4689 a->worktree, ondisk_path, ie->path,
4690 blob, 0, 1, 0, 0, a->repo,
4691 a->progress_cb, a->progress_arg);
4692 } else {
4693 if (rename(path_content, ondisk_path) == -1) {
4694 err = got_error_from_errno3("rename",
4695 path_content, ondisk_path);
4696 goto done;
4699 } else {
4700 int is_bad_symlink = 0;
4701 if (te && S_ISLNK(te->mode)) {
4702 err = install_symlink(&is_bad_symlink,
4703 a->worktree, ondisk_path, ie->path,
4704 blob, 0, 1, 0, 0, a->repo,
4705 a->progress_cb, a->progress_arg);
4706 } else {
4707 err = install_blob(a->worktree, ondisk_path,
4708 ie->path,
4709 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4710 got_fileindex_perms_to_st(ie), blob,
4711 0, 1, 0, 0, a->repo,
4712 a->progress_cb, a->progress_arg);
4714 if (err)
4715 goto done;
4716 if (status == GOT_STATUS_DELETE ||
4717 status == GOT_STATUS_MODE_CHANGE) {
4718 err = got_fileindex_entry_update(ie,
4719 a->worktree->root_fd, relpath,
4720 blob->id.sha1,
4721 a->worktree->base_commit_id->sha1, 1);
4722 if (err)
4723 goto done;
4725 if (is_bad_symlink) {
4726 got_fileindex_entry_filetype_set(ie,
4727 GOT_FILEIDX_MODE_BAD_SYMLINK);
4730 break;
4732 default:
4733 break;
4735 done:
4736 free(ondisk_path);
4737 free(path_content);
4738 free(parent_path);
4739 free(tree_path);
4740 if (blob)
4741 got_object_blob_close(blob);
4742 if (tree)
4743 got_object_tree_close(tree);
4744 free(tree_id);
4745 return err;
4748 const struct got_error *
4749 got_worktree_revert(struct got_worktree *worktree,
4750 struct got_pathlist_head *paths,
4751 got_worktree_checkout_cb progress_cb, void *progress_arg,
4752 got_worktree_patch_cb patch_cb, void *patch_arg,
4753 struct got_repository *repo)
4755 struct got_fileindex *fileindex = NULL;
4756 char *fileindex_path = NULL;
4757 const struct got_error *err = NULL, *unlockerr = NULL;
4758 const struct got_error *sync_err = NULL;
4759 struct got_pathlist_entry *pe;
4760 struct revert_file_args rfa;
4762 err = lock_worktree(worktree, LOCK_EX);
4763 if (err)
4764 return err;
4766 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4767 if (err)
4768 goto done;
4770 rfa.worktree = worktree;
4771 rfa.fileindex = fileindex;
4772 rfa.progress_cb = progress_cb;
4773 rfa.progress_arg = progress_arg;
4774 rfa.patch_cb = patch_cb;
4775 rfa.patch_arg = patch_arg;
4776 rfa.repo = repo;
4777 rfa.unlink_added_files = 0;
4778 TAILQ_FOREACH(pe, paths, entry) {
4779 err = worktree_status(worktree, pe->path, fileindex, repo,
4780 revert_file, &rfa, NULL, NULL, 1, 0);
4781 if (err)
4782 break;
4784 sync_err = sync_fileindex(fileindex, fileindex_path);
4785 if (sync_err && err == NULL)
4786 err = sync_err;
4787 done:
4788 free(fileindex_path);
4789 if (fileindex)
4790 got_fileindex_free(fileindex);
4791 unlockerr = lock_worktree(worktree, LOCK_SH);
4792 if (unlockerr && err == NULL)
4793 err = unlockerr;
4794 return err;
4797 static void
4798 free_commitable(struct got_commitable *ct)
4800 free(ct->path);
4801 free(ct->in_repo_path);
4802 free(ct->ondisk_path);
4803 free(ct->blob_id);
4804 free(ct->base_blob_id);
4805 free(ct->staged_blob_id);
4806 free(ct->base_commit_id);
4807 free(ct);
4810 struct collect_commitables_arg {
4811 struct got_pathlist_head *commitable_paths;
4812 struct got_repository *repo;
4813 struct got_worktree *worktree;
4814 struct got_fileindex *fileindex;
4815 int have_staged_files;
4816 int allow_bad_symlinks;
4819 static const struct got_error *
4820 collect_commitables(void *arg, unsigned char status,
4821 unsigned char staged_status, const char *relpath,
4822 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4823 struct got_object_id *commit_id, int dirfd, const char *de_name)
4825 struct collect_commitables_arg *a = arg;
4826 const struct got_error *err = NULL;
4827 struct got_commitable *ct = NULL;
4828 struct got_pathlist_entry *new = NULL;
4829 char *parent_path = NULL, *path = NULL;
4830 struct stat sb;
4832 if (a->have_staged_files) {
4833 if (staged_status != GOT_STATUS_MODIFY &&
4834 staged_status != GOT_STATUS_ADD &&
4835 staged_status != GOT_STATUS_DELETE)
4836 return NULL;
4837 } else {
4838 if (status == GOT_STATUS_CONFLICT)
4839 return got_error(GOT_ERR_COMMIT_CONFLICT);
4841 if (status != GOT_STATUS_MODIFY &&
4842 status != GOT_STATUS_MODE_CHANGE &&
4843 status != GOT_STATUS_ADD &&
4844 status != GOT_STATUS_DELETE)
4845 return NULL;
4848 if (asprintf(&path, "/%s", relpath) == -1) {
4849 err = got_error_from_errno("asprintf");
4850 goto done;
4852 if (strcmp(path, "/") == 0) {
4853 parent_path = strdup("");
4854 if (parent_path == NULL)
4855 return got_error_from_errno("strdup");
4856 } else {
4857 err = got_path_dirname(&parent_path, path);
4858 if (err)
4859 return err;
4862 ct = calloc(1, sizeof(*ct));
4863 if (ct == NULL) {
4864 err = got_error_from_errno("calloc");
4865 goto done;
4868 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4869 relpath) == -1) {
4870 err = got_error_from_errno("asprintf");
4871 goto done;
4874 if (staged_status == GOT_STATUS_ADD ||
4875 staged_status == GOT_STATUS_MODIFY) {
4876 struct got_fileindex_entry *ie;
4877 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4878 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4879 case GOT_FILEIDX_MODE_REGULAR_FILE:
4880 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4881 ct->mode = S_IFREG;
4882 break;
4883 case GOT_FILEIDX_MODE_SYMLINK:
4884 ct->mode = S_IFLNK;
4885 break;
4886 default:
4887 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4888 goto done;
4890 ct->mode |= got_fileindex_entry_perms_get(ie);
4891 } else if (status != GOT_STATUS_DELETE &&
4892 staged_status != GOT_STATUS_DELETE) {
4893 if (dirfd != -1) {
4894 if (fstatat(dirfd, de_name, &sb,
4895 AT_SYMLINK_NOFOLLOW) == -1) {
4896 err = got_error_from_errno2("fstatat",
4897 ct->ondisk_path);
4898 goto done;
4900 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4901 err = got_error_from_errno2("lstat", ct->ondisk_path);
4902 goto done;
4904 ct->mode = sb.st_mode;
4907 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4908 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4909 relpath) == -1) {
4910 err = got_error_from_errno("asprintf");
4911 goto done;
4914 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4915 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4916 int is_bad_symlink;
4917 char target_path[PATH_MAX];
4918 ssize_t target_len;
4919 target_len = readlink(ct->ondisk_path, target_path,
4920 sizeof(target_path));
4921 if (target_len == -1) {
4922 err = got_error_from_errno2("readlink",
4923 ct->ondisk_path);
4924 goto done;
4926 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4927 target_len, ct->ondisk_path, a->worktree->root_path);
4928 if (err)
4929 goto done;
4930 if (is_bad_symlink) {
4931 err = got_error_path(ct->ondisk_path,
4932 GOT_ERR_BAD_SYMLINK);
4933 goto done;
4938 ct->status = status;
4939 ct->staged_status = staged_status;
4940 ct->blob_id = NULL; /* will be filled in when blob gets created */
4941 if (ct->status != GOT_STATUS_ADD &&
4942 ct->staged_status != GOT_STATUS_ADD) {
4943 ct->base_blob_id = got_object_id_dup(blob_id);
4944 if (ct->base_blob_id == NULL) {
4945 err = got_error_from_errno("got_object_id_dup");
4946 goto done;
4948 ct->base_commit_id = got_object_id_dup(commit_id);
4949 if (ct->base_commit_id == NULL) {
4950 err = got_error_from_errno("got_object_id_dup");
4951 goto done;
4954 if (ct->staged_status == GOT_STATUS_ADD ||
4955 ct->staged_status == GOT_STATUS_MODIFY) {
4956 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4957 if (ct->staged_blob_id == NULL) {
4958 err = got_error_from_errno("got_object_id_dup");
4959 goto done;
4962 ct->path = strdup(path);
4963 if (ct->path == NULL) {
4964 err = got_error_from_errno("strdup");
4965 goto done;
4967 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4968 done:
4969 if (ct && (err || new == NULL))
4970 free_commitable(ct);
4971 free(parent_path);
4972 free(path);
4973 return err;
4976 static const struct got_error *write_tree(struct got_object_id **, int *,
4977 struct got_tree_object *, const char *, struct got_pathlist_head *,
4978 got_worktree_status_cb status_cb, void *status_arg,
4979 struct got_repository *);
4981 static const struct got_error *
4982 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4983 struct got_tree_entry *te, const char *parent_path,
4984 struct got_pathlist_head *commitable_paths,
4985 got_worktree_status_cb status_cb, void *status_arg,
4986 struct got_repository *repo)
4988 const struct got_error *err = NULL;
4989 struct got_tree_object *subtree;
4990 char *subpath;
4992 if (asprintf(&subpath, "%s%s%s", parent_path,
4993 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4994 return got_error_from_errno("asprintf");
4996 err = got_object_open_as_tree(&subtree, repo, &te->id);
4997 if (err)
4998 return err;
5000 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5001 commitable_paths, status_cb, status_arg, repo);
5002 got_object_tree_close(subtree);
5003 free(subpath);
5004 return err;
5007 static const struct got_error *
5008 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5010 const struct got_error *err = NULL;
5011 char *ct_parent_path = NULL;
5013 *match = 0;
5015 if (strchr(ct->in_repo_path, '/') == NULL) {
5016 *match = got_path_is_root_dir(path);
5017 return NULL;
5020 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5021 if (err)
5022 return err;
5023 *match = (strcmp(path, ct_parent_path) == 0);
5024 free(ct_parent_path);
5025 return err;
5028 static mode_t
5029 get_ct_file_mode(struct got_commitable *ct)
5031 if (S_ISLNK(ct->mode))
5032 return S_IFLNK;
5034 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5037 static const struct got_error *
5038 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5039 struct got_tree_entry *te, struct got_commitable *ct)
5041 const struct got_error *err = NULL;
5043 *new_te = NULL;
5045 err = got_object_tree_entry_dup(new_te, te);
5046 if (err)
5047 goto done;
5049 (*new_te)->mode = get_ct_file_mode(ct);
5051 if (ct->staged_status == GOT_STATUS_MODIFY)
5052 memcpy(&(*new_te)->id, ct->staged_blob_id,
5053 sizeof((*new_te)->id));
5054 else
5055 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5056 done:
5057 if (err && *new_te) {
5058 free(*new_te);
5059 *new_te = NULL;
5061 return err;
5064 static const struct got_error *
5065 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5066 struct got_commitable *ct)
5068 const struct got_error *err = NULL;
5069 char *ct_name = NULL;
5071 *new_te = NULL;
5073 *new_te = calloc(1, sizeof(**new_te));
5074 if (*new_te == NULL)
5075 return got_error_from_errno("calloc");
5077 err = got_path_basename(&ct_name, ct->path);
5078 if (err)
5079 goto done;
5080 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5081 sizeof((*new_te)->name)) {
5082 err = got_error(GOT_ERR_NO_SPACE);
5083 goto done;
5086 (*new_te)->mode = get_ct_file_mode(ct);
5088 if (ct->staged_status == GOT_STATUS_ADD)
5089 memcpy(&(*new_te)->id, ct->staged_blob_id,
5090 sizeof((*new_te)->id));
5091 else
5092 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5093 done:
5094 free(ct_name);
5095 if (err && *new_te) {
5096 free(*new_te);
5097 *new_te = NULL;
5099 return err;
5102 static const struct got_error *
5103 insert_tree_entry(struct got_tree_entry *new_te,
5104 struct got_pathlist_head *paths)
5106 const struct got_error *err = NULL;
5107 struct got_pathlist_entry *new_pe;
5109 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5110 if (err)
5111 return err;
5112 if (new_pe == NULL)
5113 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5114 return NULL;
5117 static const struct got_error *
5118 report_ct_status(struct got_commitable *ct,
5119 got_worktree_status_cb status_cb, void *status_arg)
5121 const char *ct_path = ct->path;
5122 unsigned char status;
5124 if (status_cb == NULL) /* no commit progress output desired */
5125 return NULL;
5127 while (ct_path[0] == '/')
5128 ct_path++;
5130 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5131 status = ct->staged_status;
5132 else
5133 status = ct->status;
5135 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5136 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5139 static const struct got_error *
5140 match_modified_subtree(int *modified, struct got_tree_entry *te,
5141 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5143 const struct got_error *err = NULL;
5144 struct got_pathlist_entry *pe;
5145 char *te_path;
5147 *modified = 0;
5149 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5150 got_path_is_root_dir(base_tree_path) ? "" : "/",
5151 te->name) == -1)
5152 return got_error_from_errno("asprintf");
5154 TAILQ_FOREACH(pe, commitable_paths, entry) {
5155 struct got_commitable *ct = pe->data;
5156 *modified = got_path_is_child(ct->in_repo_path, te_path,
5157 strlen(te_path));
5158 if (*modified)
5159 break;
5162 free(te_path);
5163 return err;
5166 static const struct got_error *
5167 match_deleted_or_modified_ct(struct got_commitable **ctp,
5168 struct got_tree_entry *te, const char *base_tree_path,
5169 struct got_pathlist_head *commitable_paths)
5171 const struct got_error *err = NULL;
5172 struct got_pathlist_entry *pe;
5174 *ctp = NULL;
5176 TAILQ_FOREACH(pe, commitable_paths, entry) {
5177 struct got_commitable *ct = pe->data;
5178 char *ct_name = NULL;
5179 int path_matches;
5181 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5182 if (ct->status != GOT_STATUS_MODIFY &&
5183 ct->status != GOT_STATUS_MODE_CHANGE &&
5184 ct->status != GOT_STATUS_DELETE)
5185 continue;
5186 } else {
5187 if (ct->staged_status != GOT_STATUS_MODIFY &&
5188 ct->staged_status != GOT_STATUS_DELETE)
5189 continue;
5192 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5193 continue;
5195 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5196 if (err)
5197 return err;
5198 if (!path_matches)
5199 continue;
5201 err = got_path_basename(&ct_name, pe->path);
5202 if (err)
5203 return err;
5205 if (strcmp(te->name, ct_name) != 0) {
5206 free(ct_name);
5207 continue;
5209 free(ct_name);
5211 *ctp = ct;
5212 break;
5215 return err;
5218 static const struct got_error *
5219 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5220 const char *child_path, const char *path_base_tree,
5221 struct got_pathlist_head *commitable_paths,
5222 got_worktree_status_cb status_cb, void *status_arg,
5223 struct got_repository *repo)
5225 const struct got_error *err = NULL;
5226 struct got_tree_entry *new_te;
5227 char *subtree_path;
5228 struct got_object_id *id = NULL;
5229 int nentries;
5231 *new_tep = NULL;
5233 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5234 got_path_is_root_dir(path_base_tree) ? "" : "/",
5235 child_path) == -1)
5236 return got_error_from_errno("asprintf");
5238 new_te = calloc(1, sizeof(*new_te));
5239 if (new_te == NULL)
5240 return got_error_from_errno("calloc");
5241 new_te->mode = S_IFDIR;
5243 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5244 sizeof(new_te->name)) {
5245 err = got_error(GOT_ERR_NO_SPACE);
5246 goto done;
5248 err = write_tree(&id, &nentries, NULL, subtree_path,
5249 commitable_paths, status_cb, status_arg, repo);
5250 if (err) {
5251 free(new_te);
5252 goto done;
5254 memcpy(&new_te->id, id, sizeof(new_te->id));
5255 done:
5256 free(id);
5257 free(subtree_path);
5258 if (err == NULL)
5259 *new_tep = new_te;
5260 return err;
5263 static const struct got_error *
5264 write_tree(struct got_object_id **new_tree_id, int *nentries,
5265 struct got_tree_object *base_tree, const char *path_base_tree,
5266 struct got_pathlist_head *commitable_paths,
5267 got_worktree_status_cb status_cb, void *status_arg,
5268 struct got_repository *repo)
5270 const struct got_error *err = NULL;
5271 struct got_pathlist_head paths;
5272 struct got_tree_entry *te, *new_te = NULL;
5273 struct got_pathlist_entry *pe;
5275 TAILQ_INIT(&paths);
5276 *nentries = 0;
5278 /* Insert, and recurse into, newly added entries first. */
5279 TAILQ_FOREACH(pe, commitable_paths, entry) {
5280 struct got_commitable *ct = pe->data;
5281 char *child_path = NULL, *slash;
5283 if ((ct->status != GOT_STATUS_ADD &&
5284 ct->staged_status != GOT_STATUS_ADD) ||
5285 (ct->flags & GOT_COMMITABLE_ADDED))
5286 continue;
5288 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5289 strlen(path_base_tree)))
5290 continue;
5292 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5293 ct->in_repo_path);
5294 if (err)
5295 goto done;
5297 slash = strchr(child_path, '/');
5298 if (slash == NULL) {
5299 err = alloc_added_blob_tree_entry(&new_te, ct);
5300 if (err)
5301 goto done;
5302 err = report_ct_status(ct, status_cb, status_arg);
5303 if (err)
5304 goto done;
5305 ct->flags |= GOT_COMMITABLE_ADDED;
5306 err = insert_tree_entry(new_te, &paths);
5307 if (err)
5308 goto done;
5309 (*nentries)++;
5310 } else {
5311 *slash = '\0'; /* trim trailing path components */
5312 if (base_tree == NULL ||
5313 got_object_tree_find_entry(base_tree, child_path)
5314 == NULL) {
5315 err = make_subtree_for_added_blob(&new_te,
5316 child_path, path_base_tree,
5317 commitable_paths, status_cb, status_arg,
5318 repo);
5319 if (err)
5320 goto done;
5321 err = insert_tree_entry(new_te, &paths);
5322 if (err)
5323 goto done;
5324 (*nentries)++;
5329 if (base_tree) {
5330 int i, nbase_entries;
5331 /* Handle modified and deleted entries. */
5332 nbase_entries = got_object_tree_get_nentries(base_tree);
5333 for (i = 0; i < nbase_entries; i++) {
5334 struct got_commitable *ct = NULL;
5336 te = got_object_tree_get_entry(base_tree, i);
5337 if (got_object_tree_entry_is_submodule(te)) {
5338 /* Entry is a submodule; just copy it. */
5339 err = got_object_tree_entry_dup(&new_te, te);
5340 if (err)
5341 goto done;
5342 err = insert_tree_entry(new_te, &paths);
5343 if (err)
5344 goto done;
5345 (*nentries)++;
5346 continue;
5349 if (S_ISDIR(te->mode)) {
5350 int modified;
5351 err = got_object_tree_entry_dup(&new_te, te);
5352 if (err)
5353 goto done;
5354 err = match_modified_subtree(&modified, te,
5355 path_base_tree, commitable_paths);
5356 if (err)
5357 goto done;
5358 /* Avoid recursion into unmodified subtrees. */
5359 if (modified) {
5360 struct got_object_id *new_id;
5361 int nsubentries;
5362 err = write_subtree(&new_id,
5363 &nsubentries, te,
5364 path_base_tree, commitable_paths,
5365 status_cb, status_arg, repo);
5366 if (err)
5367 goto done;
5368 if (nsubentries == 0) {
5369 /* All entries were deleted. */
5370 free(new_id);
5371 continue;
5373 memcpy(&new_te->id, new_id,
5374 sizeof(new_te->id));
5375 free(new_id);
5377 err = insert_tree_entry(new_te, &paths);
5378 if (err)
5379 goto done;
5380 (*nentries)++;
5381 continue;
5384 err = match_deleted_or_modified_ct(&ct, te,
5385 path_base_tree, commitable_paths);
5386 if (err)
5387 goto done;
5388 if (ct) {
5389 /* NB: Deleted entries get dropped here. */
5390 if (ct->status == GOT_STATUS_MODIFY ||
5391 ct->status == GOT_STATUS_MODE_CHANGE ||
5392 ct->staged_status == GOT_STATUS_MODIFY) {
5393 err = alloc_modified_blob_tree_entry(
5394 &new_te, te, ct);
5395 if (err)
5396 goto done;
5397 err = insert_tree_entry(new_te, &paths);
5398 if (err)
5399 goto done;
5400 (*nentries)++;
5402 err = report_ct_status(ct, status_cb,
5403 status_arg);
5404 if (err)
5405 goto done;
5406 } else {
5407 /* Entry is unchanged; just copy it. */
5408 err = got_object_tree_entry_dup(&new_te, te);
5409 if (err)
5410 goto done;
5411 err = insert_tree_entry(new_te, &paths);
5412 if (err)
5413 goto done;
5414 (*nentries)++;
5419 /* Write new list of entries; deleted entries have been dropped. */
5420 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5421 done:
5422 got_pathlist_free(&paths);
5423 return err;
5426 static const struct got_error *
5427 update_fileindex_after_commit(struct got_worktree *worktree,
5428 struct got_pathlist_head *commitable_paths,
5429 struct got_object_id *new_base_commit_id,
5430 struct got_fileindex *fileindex, int have_staged_files)
5432 const struct got_error *err = NULL;
5433 struct got_pathlist_entry *pe;
5434 char *relpath = NULL;
5436 TAILQ_FOREACH(pe, commitable_paths, entry) {
5437 struct got_fileindex_entry *ie;
5438 struct got_commitable *ct = pe->data;
5440 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5442 err = got_path_skip_common_ancestor(&relpath,
5443 worktree->root_path, ct->ondisk_path);
5444 if (err)
5445 goto done;
5447 if (ie) {
5448 if (ct->status == GOT_STATUS_DELETE ||
5449 ct->staged_status == GOT_STATUS_DELETE) {
5450 got_fileindex_entry_remove(fileindex, ie);
5451 } else if (ct->staged_status == GOT_STATUS_ADD ||
5452 ct->staged_status == GOT_STATUS_MODIFY) {
5453 got_fileindex_entry_stage_set(ie,
5454 GOT_FILEIDX_STAGE_NONE);
5455 got_fileindex_entry_staged_filetype_set(ie, 0);
5457 err = got_fileindex_entry_update(ie,
5458 worktree->root_fd, relpath,
5459 ct->staged_blob_id->sha1,
5460 new_base_commit_id->sha1,
5461 !have_staged_files);
5462 } else
5463 err = got_fileindex_entry_update(ie,
5464 worktree->root_fd, relpath,
5465 ct->blob_id->sha1,
5466 new_base_commit_id->sha1,
5467 !have_staged_files);
5468 } else {
5469 err = got_fileindex_entry_alloc(&ie, pe->path);
5470 if (err)
5471 goto done;
5472 err = got_fileindex_entry_update(ie,
5473 worktree->root_fd, relpath, ct->blob_id->sha1,
5474 new_base_commit_id->sha1, 1);
5475 if (err) {
5476 got_fileindex_entry_free(ie);
5477 goto done;
5479 err = got_fileindex_entry_add(fileindex, ie);
5480 if (err) {
5481 got_fileindex_entry_free(ie);
5482 goto done;
5485 free(relpath);
5486 relpath = NULL;
5488 done:
5489 free(relpath);
5490 return err;
5494 static const struct got_error *
5495 check_out_of_date(const char *in_repo_path, unsigned char status,
5496 unsigned char staged_status, struct got_object_id *base_blob_id,
5497 struct got_object_id *base_commit_id,
5498 struct got_object_id *head_commit_id, struct got_repository *repo,
5499 int ood_errcode)
5501 const struct got_error *err = NULL;
5502 struct got_object_id *id = NULL;
5504 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5505 /* Trivial case: base commit == head commit */
5506 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5507 return NULL;
5509 * Ensure file content which local changes were based
5510 * on matches file content in the branch head.
5512 err = got_object_id_by_path(&id, repo, head_commit_id,
5513 in_repo_path);
5514 if (err) {
5515 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5516 err = got_error(ood_errcode);
5517 goto done;
5518 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5519 err = got_error(ood_errcode);
5520 } else {
5521 /* Require that added files don't exist in the branch head. */
5522 err = got_object_id_by_path(&id, repo, head_commit_id,
5523 in_repo_path);
5524 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5525 goto done;
5526 err = id ? got_error(ood_errcode) : NULL;
5528 done:
5529 free(id);
5530 return err;
5533 const struct got_error *
5534 commit_worktree(struct got_object_id **new_commit_id,
5535 struct got_pathlist_head *commitable_paths,
5536 struct got_object_id *head_commit_id,
5537 struct got_object_id *parent_id2,
5538 struct got_worktree *worktree,
5539 const char *author, const char *committer,
5540 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5541 got_worktree_status_cb status_cb, void *status_arg,
5542 struct got_repository *repo)
5544 const struct got_error *err = NULL, *unlockerr = NULL;
5545 struct got_pathlist_entry *pe;
5546 const char *head_ref_name = NULL;
5547 struct got_commit_object *head_commit = NULL;
5548 struct got_reference *head_ref2 = NULL;
5549 struct got_object_id *head_commit_id2 = NULL;
5550 struct got_tree_object *head_tree = NULL;
5551 struct got_object_id *new_tree_id = NULL;
5552 int nentries, nparents = 0;
5553 struct got_object_id_queue parent_ids;
5554 struct got_object_qid *pid = NULL;
5555 char *logmsg = NULL;
5557 *new_commit_id = NULL;
5559 STAILQ_INIT(&parent_ids);
5561 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5562 if (err)
5563 goto done;
5565 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5566 if (err)
5567 goto done;
5569 if (commit_msg_cb != NULL) {
5570 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5571 if (err)
5572 goto done;
5575 if (logmsg == NULL || strlen(logmsg) == 0) {
5576 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5577 goto done;
5580 /* Create blobs from added and modified files and record their IDs. */
5581 TAILQ_FOREACH(pe, commitable_paths, entry) {
5582 struct got_commitable *ct = pe->data;
5583 char *ondisk_path;
5585 /* Blobs for staged files already exist. */
5586 if (ct->staged_status == GOT_STATUS_ADD ||
5587 ct->staged_status == GOT_STATUS_MODIFY)
5588 continue;
5590 if (ct->status != GOT_STATUS_ADD &&
5591 ct->status != GOT_STATUS_MODIFY &&
5592 ct->status != GOT_STATUS_MODE_CHANGE)
5593 continue;
5595 if (asprintf(&ondisk_path, "%s/%s",
5596 worktree->root_path, pe->path) == -1) {
5597 err = got_error_from_errno("asprintf");
5598 goto done;
5600 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5601 free(ondisk_path);
5602 if (err)
5603 goto done;
5606 /* Recursively write new tree objects. */
5607 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5608 commitable_paths, status_cb, status_arg, repo);
5609 if (err)
5610 goto done;
5612 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5613 if (err)
5614 goto done;
5615 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5616 nparents++;
5617 if (parent_id2) {
5618 err = got_object_qid_alloc(&pid, parent_id2);
5619 if (err)
5620 goto done;
5621 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5622 nparents++;
5624 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5625 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5626 if (logmsg != NULL)
5627 free(logmsg);
5628 if (err)
5629 goto done;
5631 /* Check if a concurrent commit to our branch has occurred. */
5632 head_ref_name = got_worktree_get_head_ref_name(worktree);
5633 if (head_ref_name == NULL) {
5634 err = got_error_from_errno("got_worktree_get_head_ref_name");
5635 goto done;
5637 /* Lock the reference here to prevent concurrent modification. */
5638 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5639 if (err)
5640 goto done;
5641 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5642 if (err)
5643 goto done;
5644 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5645 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5646 goto done;
5648 /* Update branch head in repository. */
5649 err = got_ref_change_ref(head_ref2, *new_commit_id);
5650 if (err)
5651 goto done;
5652 err = got_ref_write(head_ref2, repo);
5653 if (err)
5654 goto done;
5656 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5657 if (err)
5658 goto done;
5660 err = ref_base_commit(worktree, repo);
5661 if (err)
5662 goto done;
5663 done:
5664 got_object_id_queue_free(&parent_ids);
5665 if (head_tree)
5666 got_object_tree_close(head_tree);
5667 if (head_commit)
5668 got_object_commit_close(head_commit);
5669 free(head_commit_id2);
5670 if (head_ref2) {
5671 unlockerr = got_ref_unlock(head_ref2);
5672 if (unlockerr && err == NULL)
5673 err = unlockerr;
5674 got_ref_close(head_ref2);
5676 return err;
5679 static const struct got_error *
5680 check_path_is_commitable(const char *path,
5681 struct got_pathlist_head *commitable_paths)
5683 struct got_pathlist_entry *cpe = NULL;
5684 size_t path_len = strlen(path);
5686 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5687 struct got_commitable *ct = cpe->data;
5688 const char *ct_path = ct->path;
5690 while (ct_path[0] == '/')
5691 ct_path++;
5693 if (strcmp(path, ct_path) == 0 ||
5694 got_path_is_child(ct_path, path, path_len))
5695 break;
5698 if (cpe == NULL)
5699 return got_error_path(path, GOT_ERR_BAD_PATH);
5701 return NULL;
5704 static const struct got_error *
5705 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5707 int *have_staged_files = arg;
5709 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5710 *have_staged_files = 1;
5711 return got_error(GOT_ERR_CANCELLED);
5714 return NULL;
5717 static const struct got_error *
5718 check_non_staged_files(struct got_fileindex *fileindex,
5719 struct got_pathlist_head *paths)
5721 struct got_pathlist_entry *pe;
5722 struct got_fileindex_entry *ie;
5724 TAILQ_FOREACH(pe, paths, entry) {
5725 if (pe->path[0] == '\0')
5726 continue;
5727 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5728 if (ie == NULL)
5729 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5730 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5731 return got_error_path(pe->path,
5732 GOT_ERR_FILE_NOT_STAGED);
5735 return NULL;
5738 const struct got_error *
5739 got_worktree_commit(struct got_object_id **new_commit_id,
5740 struct got_worktree *worktree, struct got_pathlist_head *paths,
5741 const char *author, const char *committer, int allow_bad_symlinks,
5742 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5743 got_worktree_status_cb status_cb, void *status_arg,
5744 struct got_repository *repo)
5746 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5747 struct got_fileindex *fileindex = NULL;
5748 char *fileindex_path = NULL;
5749 struct got_pathlist_head commitable_paths;
5750 struct collect_commitables_arg cc_arg;
5751 struct got_pathlist_entry *pe;
5752 struct got_reference *head_ref = NULL;
5753 struct got_object_id *head_commit_id = NULL;
5754 int have_staged_files = 0;
5756 *new_commit_id = NULL;
5758 TAILQ_INIT(&commitable_paths);
5760 err = lock_worktree(worktree, LOCK_EX);
5761 if (err)
5762 goto done;
5764 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5765 if (err)
5766 goto done;
5768 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5769 if (err)
5770 goto done;
5772 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5773 if (err)
5774 goto done;
5776 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5777 &have_staged_files);
5778 if (err && err->code != GOT_ERR_CANCELLED)
5779 goto done;
5780 if (have_staged_files) {
5781 err = check_non_staged_files(fileindex, paths);
5782 if (err)
5783 goto done;
5786 cc_arg.commitable_paths = &commitable_paths;
5787 cc_arg.worktree = worktree;
5788 cc_arg.fileindex = fileindex;
5789 cc_arg.repo = repo;
5790 cc_arg.have_staged_files = have_staged_files;
5791 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5792 TAILQ_FOREACH(pe, paths, entry) {
5793 err = worktree_status(worktree, pe->path, fileindex, repo,
5794 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5795 if (err)
5796 goto done;
5799 if (TAILQ_EMPTY(&commitable_paths)) {
5800 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5801 goto done;
5804 TAILQ_FOREACH(pe, paths, entry) {
5805 err = check_path_is_commitable(pe->path, &commitable_paths);
5806 if (err)
5807 goto done;
5810 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5811 struct got_commitable *ct = pe->data;
5812 const char *ct_path = ct->in_repo_path;
5814 while (ct_path[0] == '/')
5815 ct_path++;
5816 err = check_out_of_date(ct_path, ct->status,
5817 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5818 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5819 if (err)
5820 goto done;
5824 err = commit_worktree(new_commit_id, &commitable_paths,
5825 head_commit_id, NULL, worktree, author, committer,
5826 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5827 if (err)
5828 goto done;
5830 err = update_fileindex_after_commit(worktree, &commitable_paths,
5831 *new_commit_id, fileindex, have_staged_files);
5832 sync_err = sync_fileindex(fileindex, fileindex_path);
5833 if (sync_err && err == NULL)
5834 err = sync_err;
5835 done:
5836 if (fileindex)
5837 got_fileindex_free(fileindex);
5838 free(fileindex_path);
5839 unlockerr = lock_worktree(worktree, LOCK_SH);
5840 if (unlockerr && err == NULL)
5841 err = unlockerr;
5842 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5843 struct got_commitable *ct = pe->data;
5844 free_commitable(ct);
5846 got_pathlist_free(&commitable_paths);
5847 return err;
5850 const char *
5851 got_commitable_get_path(struct got_commitable *ct)
5853 return ct->path;
5856 unsigned int
5857 got_commitable_get_status(struct got_commitable *ct)
5859 return ct->status;
5862 struct check_rebase_ok_arg {
5863 struct got_worktree *worktree;
5864 struct got_repository *repo;
5867 static const struct got_error *
5868 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5870 const struct got_error *err = NULL;
5871 struct check_rebase_ok_arg *a = arg;
5872 unsigned char status;
5873 struct stat sb;
5874 char *ondisk_path;
5876 /* Reject rebase of a work tree with mixed base commits. */
5877 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5878 SHA1_DIGEST_LENGTH))
5879 return got_error(GOT_ERR_MIXED_COMMITS);
5881 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5882 == -1)
5883 return got_error_from_errno("asprintf");
5885 /* Reject rebase of a work tree with modified or staged files. */
5886 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5887 free(ondisk_path);
5888 if (err)
5889 return err;
5891 if (status != GOT_STATUS_NO_CHANGE)
5892 return got_error(GOT_ERR_MODIFIED);
5893 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5894 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5896 return NULL;
5899 const struct got_error *
5900 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5901 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5902 struct got_worktree *worktree, struct got_reference *branch,
5903 struct got_repository *repo)
5905 const struct got_error *err = NULL;
5906 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5907 char *branch_ref_name = NULL;
5908 char *fileindex_path = NULL;
5909 struct check_rebase_ok_arg ok_arg;
5910 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5911 struct got_object_id *wt_branch_tip = NULL;
5913 *new_base_branch_ref = NULL;
5914 *tmp_branch = NULL;
5915 *fileindex = NULL;
5917 err = lock_worktree(worktree, LOCK_EX);
5918 if (err)
5919 return err;
5921 err = open_fileindex(fileindex, &fileindex_path, worktree);
5922 if (err)
5923 goto done;
5925 ok_arg.worktree = worktree;
5926 ok_arg.repo = repo;
5927 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5928 &ok_arg);
5929 if (err)
5930 goto done;
5932 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5933 if (err)
5934 goto done;
5936 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5937 if (err)
5938 goto done;
5940 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5941 if (err)
5942 goto done;
5944 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5945 0);
5946 if (err)
5947 goto done;
5949 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5950 if (err)
5951 goto done;
5952 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5953 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5954 goto done;
5957 err = got_ref_alloc_symref(new_base_branch_ref,
5958 new_base_branch_ref_name, wt_branch);
5959 if (err)
5960 goto done;
5961 err = got_ref_write(*new_base_branch_ref, repo);
5962 if (err)
5963 goto done;
5965 /* TODO Lock original branch's ref while rebasing? */
5967 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5968 if (err)
5969 goto done;
5971 err = got_ref_write(branch_ref, repo);
5972 if (err)
5973 goto done;
5975 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5976 worktree->base_commit_id);
5977 if (err)
5978 goto done;
5979 err = got_ref_write(*tmp_branch, repo);
5980 if (err)
5981 goto done;
5983 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5984 if (err)
5985 goto done;
5986 done:
5987 free(fileindex_path);
5988 free(tmp_branch_name);
5989 free(new_base_branch_ref_name);
5990 free(branch_ref_name);
5991 if (branch_ref)
5992 got_ref_close(branch_ref);
5993 if (wt_branch)
5994 got_ref_close(wt_branch);
5995 free(wt_branch_tip);
5996 if (err) {
5997 if (*new_base_branch_ref) {
5998 got_ref_close(*new_base_branch_ref);
5999 *new_base_branch_ref = NULL;
6001 if (*tmp_branch) {
6002 got_ref_close(*tmp_branch);
6003 *tmp_branch = NULL;
6005 if (*fileindex) {
6006 got_fileindex_free(*fileindex);
6007 *fileindex = NULL;
6009 lock_worktree(worktree, LOCK_SH);
6011 return err;
6014 const struct got_error *
6015 got_worktree_rebase_continue(struct got_object_id **commit_id,
6016 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6017 struct got_reference **branch, struct got_fileindex **fileindex,
6018 struct got_worktree *worktree, struct got_repository *repo)
6020 const struct got_error *err;
6021 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6022 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6023 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6024 char *fileindex_path = NULL;
6025 int have_staged_files = 0;
6027 *commit_id = NULL;
6028 *new_base_branch = NULL;
6029 *tmp_branch = NULL;
6030 *branch = NULL;
6031 *fileindex = NULL;
6033 err = lock_worktree(worktree, LOCK_EX);
6034 if (err)
6035 return err;
6037 err = open_fileindex(fileindex, &fileindex_path, worktree);
6038 if (err)
6039 goto done;
6041 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6042 &have_staged_files);
6043 if (err && err->code != GOT_ERR_CANCELLED)
6044 goto done;
6045 if (have_staged_files) {
6046 err = got_error(GOT_ERR_STAGED_PATHS);
6047 goto done;
6050 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6051 if (err)
6052 goto done;
6054 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6055 if (err)
6056 goto done;
6058 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6059 if (err)
6060 goto done;
6062 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6063 if (err)
6064 goto done;
6066 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6067 if (err)
6068 goto done;
6070 err = got_ref_open(branch, repo,
6071 got_ref_get_symref_target(branch_ref), 0);
6072 if (err)
6073 goto done;
6075 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6076 if (err)
6077 goto done;
6079 err = got_ref_resolve(commit_id, repo, commit_ref);
6080 if (err)
6081 goto done;
6083 err = got_ref_open(new_base_branch, repo,
6084 new_base_branch_ref_name, 0);
6085 if (err)
6086 goto done;
6088 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6089 if (err)
6090 goto done;
6091 done:
6092 free(commit_ref_name);
6093 free(branch_ref_name);
6094 free(fileindex_path);
6095 if (commit_ref)
6096 got_ref_close(commit_ref);
6097 if (branch_ref)
6098 got_ref_close(branch_ref);
6099 if (err) {
6100 free(*commit_id);
6101 *commit_id = NULL;
6102 if (*tmp_branch) {
6103 got_ref_close(*tmp_branch);
6104 *tmp_branch = NULL;
6106 if (*new_base_branch) {
6107 got_ref_close(*new_base_branch);
6108 *new_base_branch = NULL;
6110 if (*branch) {
6111 got_ref_close(*branch);
6112 *branch = NULL;
6114 if (*fileindex) {
6115 got_fileindex_free(*fileindex);
6116 *fileindex = NULL;
6118 lock_worktree(worktree, LOCK_SH);
6120 return err;
6123 const struct got_error *
6124 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6126 const struct got_error *err;
6127 char *tmp_branch_name = NULL;
6129 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6130 if (err)
6131 return err;
6133 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6134 free(tmp_branch_name);
6135 return NULL;
6138 static const struct got_error *
6139 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6140 char **logmsg, void *arg)
6142 *logmsg = arg;
6143 return NULL;
6146 static const struct got_error *
6147 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6148 const char *path, struct got_object_id *blob_id,
6149 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6150 int dirfd, const char *de_name)
6152 return NULL;
6155 struct collect_merged_paths_arg {
6156 got_worktree_checkout_cb progress_cb;
6157 void *progress_arg;
6158 struct got_pathlist_head *merged_paths;
6161 static const struct got_error *
6162 collect_merged_paths(void *arg, unsigned char status, const char *path)
6164 const struct got_error *err;
6165 struct collect_merged_paths_arg *a = arg;
6166 char *p;
6167 struct got_pathlist_entry *new;
6169 err = (*a->progress_cb)(a->progress_arg, status, path);
6170 if (err)
6171 return err;
6173 if (status != GOT_STATUS_MERGE &&
6174 status != GOT_STATUS_ADD &&
6175 status != GOT_STATUS_DELETE &&
6176 status != GOT_STATUS_CONFLICT)
6177 return NULL;
6179 p = strdup(path);
6180 if (p == NULL)
6181 return got_error_from_errno("strdup");
6183 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6184 if (err || new == NULL)
6185 free(p);
6186 return err;
6189 void
6190 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6192 struct got_pathlist_entry *pe;
6194 TAILQ_FOREACH(pe, merged_paths, entry)
6195 free((char *)pe->path);
6197 got_pathlist_free(merged_paths);
6200 static const struct got_error *
6201 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6202 int is_rebase, struct got_repository *repo)
6204 const struct got_error *err;
6205 struct got_reference *commit_ref = NULL;
6207 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6208 if (err) {
6209 if (err->code != GOT_ERR_NOT_REF)
6210 goto done;
6211 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6212 if (err)
6213 goto done;
6214 err = got_ref_write(commit_ref, repo);
6215 if (err)
6216 goto done;
6217 } else if (is_rebase) {
6218 struct got_object_id *stored_id;
6219 int cmp;
6221 err = got_ref_resolve(&stored_id, repo, commit_ref);
6222 if (err)
6223 goto done;
6224 cmp = got_object_id_cmp(commit_id, stored_id);
6225 free(stored_id);
6226 if (cmp != 0) {
6227 err = got_error(GOT_ERR_REBASE_COMMITID);
6228 goto done;
6231 done:
6232 if (commit_ref)
6233 got_ref_close(commit_ref);
6234 return err;
6237 static const struct got_error *
6238 rebase_merge_files(struct got_pathlist_head *merged_paths,
6239 const char *commit_ref_name, struct got_worktree *worktree,
6240 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6241 struct got_object_id *commit_id, struct got_repository *repo,
6242 got_worktree_checkout_cb progress_cb, void *progress_arg,
6243 got_cancel_cb cancel_cb, void *cancel_arg)
6245 const struct got_error *err;
6246 struct got_reference *commit_ref = NULL;
6247 struct collect_merged_paths_arg cmp_arg;
6248 char *fileindex_path;
6250 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6252 err = get_fileindex_path(&fileindex_path, worktree);
6253 if (err)
6254 return err;
6256 cmp_arg.progress_cb = progress_cb;
6257 cmp_arg.progress_arg = progress_arg;
6258 cmp_arg.merged_paths = merged_paths;
6259 err = merge_files(worktree, fileindex, fileindex_path,
6260 parent_commit_id, commit_id, repo, collect_merged_paths,
6261 &cmp_arg, cancel_cb, cancel_arg);
6262 if (commit_ref)
6263 got_ref_close(commit_ref);
6264 return err;
6267 const struct got_error *
6268 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6269 struct got_worktree *worktree, struct got_fileindex *fileindex,
6270 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6271 struct got_repository *repo,
6272 got_worktree_checkout_cb progress_cb, void *progress_arg,
6273 got_cancel_cb cancel_cb, void *cancel_arg)
6275 const struct got_error *err;
6276 char *commit_ref_name;
6278 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6279 if (err)
6280 return err;
6282 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6283 if (err)
6284 goto done;
6286 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6287 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6288 progress_arg, cancel_cb, cancel_arg);
6289 done:
6290 free(commit_ref_name);
6291 return err;
6294 const struct got_error *
6295 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6296 struct got_worktree *worktree, struct got_fileindex *fileindex,
6297 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6298 struct got_repository *repo,
6299 got_worktree_checkout_cb progress_cb, void *progress_arg,
6300 got_cancel_cb cancel_cb, void *cancel_arg)
6302 const struct got_error *err;
6303 char *commit_ref_name;
6305 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6306 if (err)
6307 return err;
6309 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6310 if (err)
6311 goto done;
6313 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6314 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6315 progress_arg, cancel_cb, cancel_arg);
6316 done:
6317 free(commit_ref_name);
6318 return err;
6321 static const struct got_error *
6322 rebase_commit(struct got_object_id **new_commit_id,
6323 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6324 struct got_worktree *worktree, struct got_fileindex *fileindex,
6325 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6326 const char *new_logmsg, struct got_repository *repo)
6328 const struct got_error *err, *sync_err;
6329 struct got_pathlist_head commitable_paths;
6330 struct collect_commitables_arg cc_arg;
6331 char *fileindex_path = NULL;
6332 struct got_reference *head_ref = NULL;
6333 struct got_object_id *head_commit_id = NULL;
6334 char *logmsg = NULL;
6336 TAILQ_INIT(&commitable_paths);
6337 *new_commit_id = NULL;
6339 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6341 err = get_fileindex_path(&fileindex_path, worktree);
6342 if (err)
6343 return err;
6345 cc_arg.commitable_paths = &commitable_paths;
6346 cc_arg.worktree = worktree;
6347 cc_arg.repo = repo;
6348 cc_arg.have_staged_files = 0;
6350 * If possible get the status of individual files directly to
6351 * avoid crawling the entire work tree once per rebased commit.
6353 * Ideally, merged_paths would contain a list of commitables
6354 * we could use so we could skip worktree_status() entirely.
6355 * However, we would then need carefully keep track of cumulative
6356 * effects of operations such as file additions and deletions
6357 * in 'got histedit -f' (folding multiple commits into one),
6358 * and this extra complexity is not really worth it.
6360 if (merged_paths) {
6361 struct got_pathlist_entry *pe;
6362 TAILQ_FOREACH(pe, merged_paths, entry) {
6363 err = worktree_status(worktree, pe->path, fileindex,
6364 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6365 0);
6366 if (err)
6367 goto done;
6369 } else {
6370 err = worktree_status(worktree, "", fileindex, repo,
6371 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6372 if (err)
6373 goto done;
6376 if (TAILQ_EMPTY(&commitable_paths)) {
6377 /* No-op change; commit will be elided. */
6378 err = got_ref_delete(commit_ref, repo);
6379 if (err)
6380 goto done;
6381 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6382 goto done;
6385 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6386 if (err)
6387 goto done;
6389 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6390 if (err)
6391 goto done;
6393 if (new_logmsg) {
6394 logmsg = strdup(new_logmsg);
6395 if (logmsg == NULL) {
6396 err = got_error_from_errno("strdup");
6397 goto done;
6399 } else {
6400 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6401 if (err)
6402 goto done;
6405 /* NB: commit_worktree will call free(logmsg) */
6406 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6407 NULL, worktree, got_object_commit_get_author(orig_commit),
6408 got_object_commit_get_committer(orig_commit),
6409 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6410 if (err)
6411 goto done;
6413 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6414 if (err)
6415 goto done;
6417 err = got_ref_delete(commit_ref, repo);
6418 if (err)
6419 goto done;
6421 err = update_fileindex_after_commit(worktree, &commitable_paths,
6422 *new_commit_id, fileindex, 0);
6423 sync_err = sync_fileindex(fileindex, fileindex_path);
6424 if (sync_err && err == NULL)
6425 err = sync_err;
6426 done:
6427 free(fileindex_path);
6428 free(head_commit_id);
6429 if (head_ref)
6430 got_ref_close(head_ref);
6431 if (err) {
6432 free(*new_commit_id);
6433 *new_commit_id = NULL;
6435 return err;
6438 const struct got_error *
6439 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6440 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6441 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6442 struct got_commit_object *orig_commit,
6443 struct got_object_id *orig_commit_id, struct got_repository *repo)
6445 const struct got_error *err;
6446 char *commit_ref_name;
6447 struct got_reference *commit_ref = NULL;
6448 struct got_object_id *commit_id = NULL;
6450 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6451 if (err)
6452 return err;
6454 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6455 if (err)
6456 goto done;
6457 err = got_ref_resolve(&commit_id, repo, commit_ref);
6458 if (err)
6459 goto done;
6460 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6461 err = got_error(GOT_ERR_REBASE_COMMITID);
6462 goto done;
6465 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6466 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6467 done:
6468 if (commit_ref)
6469 got_ref_close(commit_ref);
6470 free(commit_ref_name);
6471 free(commit_id);
6472 return err;
6475 const struct got_error *
6476 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6477 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6478 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6479 struct got_commit_object *orig_commit,
6480 struct got_object_id *orig_commit_id, const char *new_logmsg,
6481 struct got_repository *repo)
6483 const struct got_error *err;
6484 char *commit_ref_name;
6485 struct got_reference *commit_ref = NULL;
6487 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6488 if (err)
6489 return err;
6491 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6492 if (err)
6493 goto done;
6495 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6496 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6497 done:
6498 if (commit_ref)
6499 got_ref_close(commit_ref);
6500 free(commit_ref_name);
6501 return err;
6504 const struct got_error *
6505 got_worktree_rebase_postpone(struct got_worktree *worktree,
6506 struct got_fileindex *fileindex)
6508 if (fileindex)
6509 got_fileindex_free(fileindex);
6510 return lock_worktree(worktree, LOCK_SH);
6513 static const struct got_error *
6514 delete_ref(const char *name, struct got_repository *repo)
6516 const struct got_error *err;
6517 struct got_reference *ref;
6519 err = got_ref_open(&ref, repo, name, 0);
6520 if (err) {
6521 if (err->code == GOT_ERR_NOT_REF)
6522 return NULL;
6523 return err;
6526 err = got_ref_delete(ref, repo);
6527 got_ref_close(ref);
6528 return err;
6531 static const struct got_error *
6532 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6534 const struct got_error *err;
6535 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6536 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6538 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6539 if (err)
6540 goto done;
6541 err = delete_ref(tmp_branch_name, repo);
6542 if (err)
6543 goto done;
6545 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6546 if (err)
6547 goto done;
6548 err = delete_ref(new_base_branch_ref_name, repo);
6549 if (err)
6550 goto done;
6552 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6553 if (err)
6554 goto done;
6555 err = delete_ref(branch_ref_name, repo);
6556 if (err)
6557 goto done;
6559 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6560 if (err)
6561 goto done;
6562 err = delete_ref(commit_ref_name, repo);
6563 if (err)
6564 goto done;
6566 done:
6567 free(tmp_branch_name);
6568 free(new_base_branch_ref_name);
6569 free(branch_ref_name);
6570 free(commit_ref_name);
6571 return err;
6574 const struct got_error *
6575 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6576 struct got_object_id *new_commit_id, struct got_repository *repo)
6578 const struct got_error *err;
6579 struct got_reference *ref = NULL;
6580 struct got_object_id *old_commit_id = NULL;
6581 const char *branch_name = NULL;
6582 char *new_id_str = NULL;
6583 char *refname = NULL;
6585 branch_name = got_ref_get_name(branch);
6586 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6587 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6588 branch_name += 11;
6590 err = got_object_id_str(&new_id_str, new_commit_id);
6591 if (err)
6592 return err;
6594 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6595 new_id_str) == -1) {
6596 err = got_error_from_errno("asprintf");
6597 goto done;
6600 err = got_ref_resolve(&old_commit_id, repo, branch);
6601 if (err)
6602 goto done;
6604 err = got_ref_alloc(&ref, refname, old_commit_id);
6605 if (err)
6606 goto done;
6608 err = got_ref_write(ref, repo);
6609 done:
6610 free(new_id_str);
6611 free(refname);
6612 free(old_commit_id);
6613 if (ref)
6614 got_ref_close(ref);
6615 return err;
6618 const struct got_error *
6619 got_worktree_rebase_complete(struct got_worktree *worktree,
6620 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6621 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6622 struct got_repository *repo, int create_backup)
6624 const struct got_error *err, *unlockerr, *sync_err;
6625 struct got_object_id *new_head_commit_id = NULL;
6626 char *fileindex_path = NULL;
6628 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6629 if (err)
6630 return err;
6632 if (create_backup) {
6633 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6634 rebased_branch, new_head_commit_id, repo);
6635 if (err)
6636 goto done;
6639 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6640 if (err)
6641 goto done;
6643 err = got_ref_write(rebased_branch, repo);
6644 if (err)
6645 goto done;
6647 err = got_worktree_set_head_ref(worktree, rebased_branch);
6648 if (err)
6649 goto done;
6651 err = delete_rebase_refs(worktree, repo);
6652 if (err)
6653 goto done;
6655 err = get_fileindex_path(&fileindex_path, worktree);
6656 if (err)
6657 goto done;
6658 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6659 sync_err = sync_fileindex(fileindex, fileindex_path);
6660 if (sync_err && err == NULL)
6661 err = sync_err;
6662 done:
6663 got_fileindex_free(fileindex);
6664 free(fileindex_path);
6665 free(new_head_commit_id);
6666 unlockerr = lock_worktree(worktree, LOCK_SH);
6667 if (unlockerr && err == NULL)
6668 err = unlockerr;
6669 return err;
6672 const struct got_error *
6673 got_worktree_rebase_abort(struct got_worktree *worktree,
6674 struct got_fileindex *fileindex, struct got_repository *repo,
6675 struct got_reference *new_base_branch,
6676 got_worktree_checkout_cb progress_cb, void *progress_arg)
6678 const struct got_error *err, *unlockerr, *sync_err;
6679 struct got_reference *resolved = NULL;
6680 struct got_object_id *commit_id = NULL;
6681 char *fileindex_path = NULL;
6682 struct revert_file_args rfa;
6683 struct got_object_id *tree_id = NULL;
6685 err = lock_worktree(worktree, LOCK_EX);
6686 if (err)
6687 return err;
6689 err = got_ref_open(&resolved, repo,
6690 got_ref_get_symref_target(new_base_branch), 0);
6691 if (err)
6692 goto done;
6694 err = got_worktree_set_head_ref(worktree, resolved);
6695 if (err)
6696 goto done;
6699 * XXX commits to the base branch could have happened while
6700 * we were busy rebasing; should we store the original commit ID
6701 * when rebase begins and read it back here?
6703 err = got_ref_resolve(&commit_id, repo, resolved);
6704 if (err)
6705 goto done;
6707 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6708 if (err)
6709 goto done;
6711 err = got_object_id_by_path(&tree_id, repo,
6712 worktree->base_commit_id, worktree->path_prefix);
6713 if (err)
6714 goto done;
6716 err = delete_rebase_refs(worktree, repo);
6717 if (err)
6718 goto done;
6720 err = get_fileindex_path(&fileindex_path, worktree);
6721 if (err)
6722 goto done;
6724 rfa.worktree = worktree;
6725 rfa.fileindex = fileindex;
6726 rfa.progress_cb = progress_cb;
6727 rfa.progress_arg = progress_arg;
6728 rfa.patch_cb = NULL;
6729 rfa.patch_arg = NULL;
6730 rfa.repo = repo;
6731 rfa.unlink_added_files = 0;
6732 err = worktree_status(worktree, "", fileindex, repo,
6733 revert_file, &rfa, NULL, NULL, 1, 0);
6734 if (err)
6735 goto sync;
6737 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6738 repo, progress_cb, progress_arg, NULL, NULL);
6739 sync:
6740 sync_err = sync_fileindex(fileindex, fileindex_path);
6741 if (sync_err && err == NULL)
6742 err = sync_err;
6743 done:
6744 got_ref_close(resolved);
6745 free(tree_id);
6746 free(commit_id);
6747 if (fileindex)
6748 got_fileindex_free(fileindex);
6749 free(fileindex_path);
6751 unlockerr = lock_worktree(worktree, LOCK_SH);
6752 if (unlockerr && err == NULL)
6753 err = unlockerr;
6754 return err;
6757 const struct got_error *
6758 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6759 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6760 struct got_fileindex **fileindex, struct got_worktree *worktree,
6761 struct got_repository *repo)
6763 const struct got_error *err = NULL;
6764 char *tmp_branch_name = NULL;
6765 char *branch_ref_name = NULL;
6766 char *base_commit_ref_name = NULL;
6767 char *fileindex_path = NULL;
6768 struct check_rebase_ok_arg ok_arg;
6769 struct got_reference *wt_branch = NULL;
6770 struct got_reference *base_commit_ref = NULL;
6772 *tmp_branch = NULL;
6773 *branch_ref = NULL;
6774 *base_commit_id = NULL;
6775 *fileindex = NULL;
6777 err = lock_worktree(worktree, LOCK_EX);
6778 if (err)
6779 return err;
6781 err = open_fileindex(fileindex, &fileindex_path, worktree);
6782 if (err)
6783 goto done;
6785 ok_arg.worktree = worktree;
6786 ok_arg.repo = repo;
6787 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6788 &ok_arg);
6789 if (err)
6790 goto done;
6792 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6793 if (err)
6794 goto done;
6796 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6797 if (err)
6798 goto done;
6800 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6801 worktree);
6802 if (err)
6803 goto done;
6805 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6806 0);
6807 if (err)
6808 goto done;
6810 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6811 if (err)
6812 goto done;
6814 err = got_ref_write(*branch_ref, repo);
6815 if (err)
6816 goto done;
6818 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6819 worktree->base_commit_id);
6820 if (err)
6821 goto done;
6822 err = got_ref_write(base_commit_ref, repo);
6823 if (err)
6824 goto done;
6825 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6826 if (*base_commit_id == NULL) {
6827 err = got_error_from_errno("got_object_id_dup");
6828 goto done;
6831 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6832 worktree->base_commit_id);
6833 if (err)
6834 goto done;
6835 err = got_ref_write(*tmp_branch, repo);
6836 if (err)
6837 goto done;
6839 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6840 if (err)
6841 goto done;
6842 done:
6843 free(fileindex_path);
6844 free(tmp_branch_name);
6845 free(branch_ref_name);
6846 free(base_commit_ref_name);
6847 if (wt_branch)
6848 got_ref_close(wt_branch);
6849 if (err) {
6850 if (*branch_ref) {
6851 got_ref_close(*branch_ref);
6852 *branch_ref = NULL;
6854 if (*tmp_branch) {
6855 got_ref_close(*tmp_branch);
6856 *tmp_branch = NULL;
6858 free(*base_commit_id);
6859 if (*fileindex) {
6860 got_fileindex_free(*fileindex);
6861 *fileindex = NULL;
6863 lock_worktree(worktree, LOCK_SH);
6865 return err;
6868 const struct got_error *
6869 got_worktree_histedit_postpone(struct got_worktree *worktree,
6870 struct got_fileindex *fileindex)
6872 if (fileindex)
6873 got_fileindex_free(fileindex);
6874 return lock_worktree(worktree, LOCK_SH);
6877 const struct got_error *
6878 got_worktree_histedit_in_progress(int *in_progress,
6879 struct got_worktree *worktree)
6881 const struct got_error *err;
6882 char *tmp_branch_name = NULL;
6884 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6885 if (err)
6886 return err;
6888 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6889 free(tmp_branch_name);
6890 return NULL;
6893 const struct got_error *
6894 got_worktree_histedit_continue(struct got_object_id **commit_id,
6895 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6896 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6897 struct got_worktree *worktree, struct got_repository *repo)
6899 const struct got_error *err;
6900 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6901 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6902 struct got_reference *commit_ref = NULL;
6903 struct got_reference *base_commit_ref = NULL;
6904 char *fileindex_path = NULL;
6905 int have_staged_files = 0;
6907 *commit_id = NULL;
6908 *tmp_branch = NULL;
6909 *base_commit_id = NULL;
6910 *fileindex = NULL;
6912 err = lock_worktree(worktree, LOCK_EX);
6913 if (err)
6914 return err;
6916 err = open_fileindex(fileindex, &fileindex_path, worktree);
6917 if (err)
6918 goto done;
6920 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6921 &have_staged_files);
6922 if (err && err->code != GOT_ERR_CANCELLED)
6923 goto done;
6924 if (have_staged_files) {
6925 err = got_error(GOT_ERR_STAGED_PATHS);
6926 goto done;
6929 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6930 if (err)
6931 goto done;
6933 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6934 if (err)
6935 goto done;
6937 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6938 if (err)
6939 goto done;
6941 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6942 worktree);
6943 if (err)
6944 goto done;
6946 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6947 if (err)
6948 goto done;
6950 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6951 if (err)
6952 goto done;
6953 err = got_ref_resolve(commit_id, repo, commit_ref);
6954 if (err)
6955 goto done;
6957 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6958 if (err)
6959 goto done;
6960 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6961 if (err)
6962 goto done;
6964 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6965 if (err)
6966 goto done;
6967 done:
6968 free(commit_ref_name);
6969 free(branch_ref_name);
6970 free(fileindex_path);
6971 if (commit_ref)
6972 got_ref_close(commit_ref);
6973 if (base_commit_ref)
6974 got_ref_close(base_commit_ref);
6975 if (err) {
6976 free(*commit_id);
6977 *commit_id = NULL;
6978 free(*base_commit_id);
6979 *base_commit_id = NULL;
6980 if (*tmp_branch) {
6981 got_ref_close(*tmp_branch);
6982 *tmp_branch = NULL;
6984 if (*fileindex) {
6985 got_fileindex_free(*fileindex);
6986 *fileindex = NULL;
6988 lock_worktree(worktree, LOCK_EX);
6990 return err;
6993 static const struct got_error *
6994 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6996 const struct got_error *err;
6997 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6998 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7000 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7001 if (err)
7002 goto done;
7003 err = delete_ref(tmp_branch_name, repo);
7004 if (err)
7005 goto done;
7007 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7008 worktree);
7009 if (err)
7010 goto done;
7011 err = delete_ref(base_commit_ref_name, repo);
7012 if (err)
7013 goto done;
7015 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7016 if (err)
7017 goto done;
7018 err = delete_ref(branch_ref_name, repo);
7019 if (err)
7020 goto done;
7022 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7023 if (err)
7024 goto done;
7025 err = delete_ref(commit_ref_name, repo);
7026 if (err)
7027 goto done;
7028 done:
7029 free(tmp_branch_name);
7030 free(base_commit_ref_name);
7031 free(branch_ref_name);
7032 free(commit_ref_name);
7033 return err;
7036 const struct got_error *
7037 got_worktree_histedit_abort(struct got_worktree *worktree,
7038 struct got_fileindex *fileindex, struct got_repository *repo,
7039 struct got_reference *branch, struct got_object_id *base_commit_id,
7040 got_worktree_checkout_cb progress_cb, void *progress_arg)
7042 const struct got_error *err, *unlockerr, *sync_err;
7043 struct got_reference *resolved = NULL;
7044 char *fileindex_path = NULL;
7045 struct got_object_id *tree_id = NULL;
7046 struct revert_file_args rfa;
7048 err = lock_worktree(worktree, LOCK_EX);
7049 if (err)
7050 return err;
7052 err = got_ref_open(&resolved, repo,
7053 got_ref_get_symref_target(branch), 0);
7054 if (err)
7055 goto done;
7057 err = got_worktree_set_head_ref(worktree, resolved);
7058 if (err)
7059 goto done;
7061 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7062 if (err)
7063 goto done;
7065 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7066 worktree->path_prefix);
7067 if (err)
7068 goto done;
7070 err = delete_histedit_refs(worktree, repo);
7071 if (err)
7072 goto done;
7074 err = get_fileindex_path(&fileindex_path, worktree);
7075 if (err)
7076 goto done;
7078 rfa.worktree = worktree;
7079 rfa.fileindex = fileindex;
7080 rfa.progress_cb = progress_cb;
7081 rfa.progress_arg = progress_arg;
7082 rfa.patch_cb = NULL;
7083 rfa.patch_arg = NULL;
7084 rfa.repo = repo;
7085 rfa.unlink_added_files = 0;
7086 err = worktree_status(worktree, "", fileindex, repo,
7087 revert_file, &rfa, NULL, NULL, 1, 0);
7088 if (err)
7089 goto sync;
7091 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7092 repo, progress_cb, progress_arg, NULL, NULL);
7093 sync:
7094 sync_err = sync_fileindex(fileindex, fileindex_path);
7095 if (sync_err && err == NULL)
7096 err = sync_err;
7097 done:
7098 got_ref_close(resolved);
7099 free(tree_id);
7100 free(fileindex_path);
7102 unlockerr = lock_worktree(worktree, LOCK_SH);
7103 if (unlockerr && err == NULL)
7104 err = unlockerr;
7105 return err;
7108 const struct got_error *
7109 got_worktree_histedit_complete(struct got_worktree *worktree,
7110 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7111 struct got_reference *edited_branch, struct got_repository *repo)
7113 const struct got_error *err, *unlockerr, *sync_err;
7114 struct got_object_id *new_head_commit_id = NULL;
7115 struct got_reference *resolved = NULL;
7116 char *fileindex_path = NULL;
7118 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7119 if (err)
7120 return err;
7122 err = got_ref_open(&resolved, repo,
7123 got_ref_get_symref_target(edited_branch), 0);
7124 if (err)
7125 goto done;
7127 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7128 resolved, new_head_commit_id, repo);
7129 if (err)
7130 goto done;
7132 err = got_ref_change_ref(resolved, new_head_commit_id);
7133 if (err)
7134 goto done;
7136 err = got_ref_write(resolved, repo);
7137 if (err)
7138 goto done;
7140 err = got_worktree_set_head_ref(worktree, resolved);
7141 if (err)
7142 goto done;
7144 err = delete_histedit_refs(worktree, repo);
7145 if (err)
7146 goto done;
7148 err = get_fileindex_path(&fileindex_path, worktree);
7149 if (err)
7150 goto done;
7151 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7152 sync_err = sync_fileindex(fileindex, fileindex_path);
7153 if (sync_err && err == NULL)
7154 err = sync_err;
7155 done:
7156 got_fileindex_free(fileindex);
7157 free(fileindex_path);
7158 free(new_head_commit_id);
7159 unlockerr = lock_worktree(worktree, LOCK_SH);
7160 if (unlockerr && err == NULL)
7161 err = unlockerr;
7162 return err;
7165 const struct got_error *
7166 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7167 struct got_object_id *commit_id, struct got_repository *repo)
7169 const struct got_error *err;
7170 char *commit_ref_name;
7172 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7173 if (err)
7174 return err;
7176 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7177 if (err)
7178 goto done;
7180 err = delete_ref(commit_ref_name, repo);
7181 done:
7182 free(commit_ref_name);
7183 return err;
7186 const struct got_error *
7187 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7188 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7189 struct got_worktree *worktree, const char *refname,
7190 struct got_repository *repo)
7192 const struct got_error *err = NULL;
7193 char *fileindex_path = NULL;
7194 struct check_rebase_ok_arg ok_arg;
7196 *fileindex = NULL;
7197 *branch_ref = NULL;
7198 *base_branch_ref = NULL;
7200 err = lock_worktree(worktree, LOCK_EX);
7201 if (err)
7202 return err;
7204 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7205 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7206 "cannot integrate a branch into itself; "
7207 "update -b or different branch name required");
7208 goto done;
7211 err = open_fileindex(fileindex, &fileindex_path, worktree);
7212 if (err)
7213 goto done;
7215 /* Preconditions are the same as for rebase. */
7216 ok_arg.worktree = worktree;
7217 ok_arg.repo = repo;
7218 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7219 &ok_arg);
7220 if (err)
7221 goto done;
7223 err = got_ref_open(branch_ref, repo, refname, 1);
7224 if (err)
7225 goto done;
7227 err = got_ref_open(base_branch_ref, repo,
7228 got_worktree_get_head_ref_name(worktree), 1);
7229 done:
7230 if (err) {
7231 if (*branch_ref) {
7232 got_ref_close(*branch_ref);
7233 *branch_ref = NULL;
7235 if (*base_branch_ref) {
7236 got_ref_close(*base_branch_ref);
7237 *base_branch_ref = NULL;
7239 if (*fileindex) {
7240 got_fileindex_free(*fileindex);
7241 *fileindex = NULL;
7243 lock_worktree(worktree, LOCK_SH);
7245 return err;
7248 const struct got_error *
7249 got_worktree_integrate_continue(struct got_worktree *worktree,
7250 struct got_fileindex *fileindex, struct got_repository *repo,
7251 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7252 got_worktree_checkout_cb progress_cb, void *progress_arg,
7253 got_cancel_cb cancel_cb, void *cancel_arg)
7255 const struct got_error *err = NULL, *sync_err, *unlockerr;
7256 char *fileindex_path = NULL;
7257 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7259 err = get_fileindex_path(&fileindex_path, worktree);
7260 if (err)
7261 goto done;
7263 err = got_ref_resolve(&commit_id, repo, branch_ref);
7264 if (err)
7265 goto done;
7267 err = got_object_id_by_path(&tree_id, repo, commit_id,
7268 worktree->path_prefix);
7269 if (err)
7270 goto done;
7272 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7273 if (err)
7274 goto done;
7276 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7277 progress_cb, progress_arg, cancel_cb, cancel_arg);
7278 if (err)
7279 goto sync;
7281 err = got_ref_change_ref(base_branch_ref, commit_id);
7282 if (err)
7283 goto sync;
7285 err = got_ref_write(base_branch_ref, repo);
7286 if (err)
7287 goto sync;
7289 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7290 sync:
7291 sync_err = sync_fileindex(fileindex, fileindex_path);
7292 if (sync_err && err == NULL)
7293 err = sync_err;
7295 done:
7296 unlockerr = got_ref_unlock(branch_ref);
7297 if (unlockerr && err == NULL)
7298 err = unlockerr;
7299 got_ref_close(branch_ref);
7301 unlockerr = got_ref_unlock(base_branch_ref);
7302 if (unlockerr && err == NULL)
7303 err = unlockerr;
7304 got_ref_close(base_branch_ref);
7306 got_fileindex_free(fileindex);
7307 free(fileindex_path);
7308 free(tree_id);
7310 unlockerr = lock_worktree(worktree, LOCK_SH);
7311 if (unlockerr && err == NULL)
7312 err = unlockerr;
7313 return err;
7316 const struct got_error *
7317 got_worktree_integrate_abort(struct got_worktree *worktree,
7318 struct got_fileindex *fileindex, struct got_repository *repo,
7319 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7321 const struct got_error *err = NULL, *unlockerr = NULL;
7323 got_fileindex_free(fileindex);
7325 err = lock_worktree(worktree, LOCK_SH);
7327 unlockerr = got_ref_unlock(branch_ref);
7328 if (unlockerr && err == NULL)
7329 err = unlockerr;
7330 got_ref_close(branch_ref);
7332 unlockerr = got_ref_unlock(base_branch_ref);
7333 if (unlockerr && err == NULL)
7334 err = unlockerr;
7335 got_ref_close(base_branch_ref);
7337 return err;
7340 const struct got_error *
7341 got_worktree_merge_postpone(struct got_worktree *worktree,
7342 struct got_fileindex *fileindex)
7344 const struct got_error *err, *sync_err;
7345 char *fileindex_path = NULL;
7347 err = get_fileindex_path(&fileindex_path, worktree);
7348 if (err)
7349 goto done;
7351 sync_err = sync_fileindex(fileindex, fileindex_path);
7353 err = lock_worktree(worktree, LOCK_SH);
7354 if (sync_err && err == NULL)
7355 err = sync_err;
7356 done:
7357 got_fileindex_free(fileindex);
7358 free(fileindex_path);
7359 return err;
7362 static const struct got_error *
7363 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7365 const struct got_error *err;
7366 char *branch_refname = NULL, *commit_refname = NULL;
7368 err = get_merge_branch_ref_name(&branch_refname, worktree);
7369 if (err)
7370 goto done;
7371 err = delete_ref(branch_refname, repo);
7372 if (err)
7373 goto done;
7375 err = get_merge_commit_ref_name(&commit_refname, worktree);
7376 if (err)
7377 goto done;
7378 err = delete_ref(commit_refname, repo);
7379 if (err)
7380 goto done;
7382 done:
7383 free(branch_refname);
7384 free(commit_refname);
7385 return err;
7388 struct merge_commit_msg_arg {
7389 struct got_worktree *worktree;
7390 const char *branch_name;
7393 static const struct got_error *
7394 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7395 void *arg)
7397 struct merge_commit_msg_arg *a = arg;
7399 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7400 got_worktree_get_head_ref_name(a->worktree)) == -1)
7401 return got_error_from_errno("asprintf");
7403 return NULL;
7407 const struct got_error *
7408 got_worktree_merge_branch(struct got_worktree *worktree,
7409 struct got_fileindex *fileindex,
7410 struct got_object_id *yca_commit_id,
7411 struct got_object_id *branch_tip,
7412 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7413 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7415 const struct got_error *err;
7416 char *fileindex_path = NULL;
7418 err = get_fileindex_path(&fileindex_path, worktree);
7419 if (err)
7420 goto done;
7422 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7423 worktree);
7424 if (err)
7425 goto done;
7427 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7428 branch_tip, repo, progress_cb, progress_arg,
7429 cancel_cb, cancel_arg);
7430 done:
7431 free(fileindex_path);
7432 return err;
7435 const struct got_error *
7436 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7437 struct got_worktree *worktree, struct got_fileindex *fileindex,
7438 const char *author, const char *committer, int allow_bad_symlinks,
7439 struct got_object_id *branch_tip, const char *branch_name,
7440 struct got_repository *repo,
7441 got_worktree_status_cb status_cb, void *status_arg)
7444 const struct got_error *err = NULL, *sync_err;
7445 struct got_pathlist_head commitable_paths;
7446 struct collect_commitables_arg cc_arg;
7447 struct got_pathlist_entry *pe;
7448 struct got_reference *head_ref = NULL;
7449 struct got_object_id *head_commit_id = NULL;
7450 int have_staged_files = 0;
7451 struct merge_commit_msg_arg mcm_arg;
7452 char *fileindex_path = NULL;
7454 *new_commit_id = NULL;
7456 TAILQ_INIT(&commitable_paths);
7458 err = get_fileindex_path(&fileindex_path, worktree);
7459 if (err)
7460 goto done;
7462 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7463 if (err)
7464 goto done;
7466 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7467 if (err)
7468 goto done;
7470 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7471 &have_staged_files);
7472 if (err && err->code != GOT_ERR_CANCELLED)
7473 goto done;
7474 if (have_staged_files) {
7475 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7476 goto done;
7479 cc_arg.commitable_paths = &commitable_paths;
7480 cc_arg.worktree = worktree;
7481 cc_arg.fileindex = fileindex;
7482 cc_arg.repo = repo;
7483 cc_arg.have_staged_files = have_staged_files;
7484 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7485 err = worktree_status(worktree, "", fileindex, repo,
7486 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7487 if (err)
7488 goto done;
7490 if (TAILQ_EMPTY(&commitable_paths)) {
7491 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7492 "merge of %s cannot proceed", branch_name);
7493 goto done;
7496 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7497 struct got_commitable *ct = pe->data;
7498 const char *ct_path = ct->in_repo_path;
7500 while (ct_path[0] == '/')
7501 ct_path++;
7502 err = check_out_of_date(ct_path, ct->status,
7503 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7504 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7505 if (err)
7506 goto done;
7510 mcm_arg.worktree = worktree;
7511 mcm_arg.branch_name = branch_name;
7512 err = commit_worktree(new_commit_id, &commitable_paths,
7513 head_commit_id, branch_tip, worktree, author, committer,
7514 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7515 if (err)
7516 goto done;
7518 err = update_fileindex_after_commit(worktree, &commitable_paths,
7519 *new_commit_id, fileindex, have_staged_files);
7520 sync_err = sync_fileindex(fileindex, fileindex_path);
7521 if (sync_err && err == NULL)
7522 err = sync_err;
7523 done:
7524 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7525 struct got_commitable *ct = pe->data;
7526 free_commitable(ct);
7528 got_pathlist_free(&commitable_paths);
7529 free(fileindex_path);
7530 return err;
7533 const struct got_error *
7534 got_worktree_merge_complete(struct got_worktree *worktree,
7535 struct got_fileindex *fileindex, struct got_repository *repo)
7537 const struct got_error *err, *unlockerr, *sync_err;
7538 char *fileindex_path = NULL;
7540 err = delete_merge_refs(worktree, repo);
7541 if (err)
7542 goto done;
7544 err = get_fileindex_path(&fileindex_path, worktree);
7545 if (err)
7546 goto done;
7547 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7548 sync_err = sync_fileindex(fileindex, fileindex_path);
7549 if (sync_err && err == NULL)
7550 err = sync_err;
7551 done:
7552 got_fileindex_free(fileindex);
7553 free(fileindex_path);
7554 unlockerr = lock_worktree(worktree, LOCK_SH);
7555 if (unlockerr && err == NULL)
7556 err = unlockerr;
7557 return err;
7560 const struct got_error *
7561 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7562 struct got_repository *repo)
7564 const struct got_error *err;
7565 char *branch_refname = NULL;
7566 struct got_reference *branch_ref = NULL;
7568 *in_progress = 0;
7570 err = get_merge_branch_ref_name(&branch_refname, worktree);
7571 if (err)
7572 return err;
7573 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7574 free(branch_refname);
7575 if (err) {
7576 if (err->code != GOT_ERR_NOT_REF)
7577 return err;
7578 } else
7579 *in_progress = 1;
7581 return NULL;
7584 const struct got_error *got_worktree_merge_prepare(
7585 struct got_fileindex **fileindex, struct got_worktree *worktree,
7586 struct got_reference *branch, struct got_repository *repo)
7588 const struct got_error *err = NULL;
7589 char *fileindex_path = NULL;
7590 char *branch_refname = NULL, *commit_refname = NULL;
7591 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7592 struct got_reference *commit_ref = NULL;
7593 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7594 struct check_rebase_ok_arg ok_arg;
7596 *fileindex = NULL;
7598 err = lock_worktree(worktree, LOCK_EX);
7599 if (err)
7600 return err;
7602 err = open_fileindex(fileindex, &fileindex_path, worktree);
7603 if (err)
7604 goto done;
7606 /* Preconditions are the same as for rebase. */
7607 ok_arg.worktree = worktree;
7608 ok_arg.repo = repo;
7609 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7610 &ok_arg);
7611 if (err)
7612 goto done;
7614 err = get_merge_branch_ref_name(&branch_refname, worktree);
7615 if (err)
7616 return err;
7618 err = get_merge_commit_ref_name(&commit_refname, worktree);
7619 if (err)
7620 return err;
7622 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7623 0);
7624 if (err)
7625 goto done;
7627 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7628 if (err)
7629 goto done;
7631 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7632 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7633 goto done;
7636 err = got_ref_resolve(&branch_tip, repo, branch);
7637 if (err)
7638 goto done;
7640 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7641 if (err)
7642 goto done;
7643 err = got_ref_write(branch_ref, repo);
7644 if (err)
7645 goto done;
7647 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7648 if (err)
7649 goto done;
7650 err = got_ref_write(commit_ref, repo);
7651 if (err)
7652 goto done;
7654 done:
7655 free(branch_refname);
7656 free(commit_refname);
7657 free(fileindex_path);
7658 if (branch_ref)
7659 got_ref_close(branch_ref);
7660 if (commit_ref)
7661 got_ref_close(commit_ref);
7662 if (wt_branch)
7663 got_ref_close(wt_branch);
7664 free(wt_branch_tip);
7665 if (err) {
7666 if (*fileindex) {
7667 got_fileindex_free(*fileindex);
7668 *fileindex = NULL;
7670 lock_worktree(worktree, LOCK_SH);
7672 return err;
7675 const struct got_error *
7676 got_worktree_merge_continue(char **branch_name,
7677 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7678 struct got_worktree *worktree, struct got_repository *repo)
7680 const struct got_error *err;
7681 char *commit_refname = NULL, *branch_refname = NULL;
7682 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7683 char *fileindex_path = NULL;
7684 int have_staged_files = 0;
7686 *branch_name = NULL;
7687 *branch_tip = NULL;
7688 *fileindex = NULL;
7690 err = lock_worktree(worktree, LOCK_EX);
7691 if (err)
7692 return err;
7694 err = open_fileindex(fileindex, &fileindex_path, worktree);
7695 if (err)
7696 goto done;
7698 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7699 &have_staged_files);
7700 if (err && err->code != GOT_ERR_CANCELLED)
7701 goto done;
7702 if (have_staged_files) {
7703 err = got_error(GOT_ERR_STAGED_PATHS);
7704 goto done;
7707 err = get_merge_branch_ref_name(&branch_refname, worktree);
7708 if (err)
7709 goto done;
7711 err = get_merge_commit_ref_name(&commit_refname, worktree);
7712 if (err)
7713 goto done;
7715 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7716 if (err)
7717 goto done;
7719 if (!got_ref_is_symbolic(branch_ref)) {
7720 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7721 "%s is not a symbolic reference",
7722 got_ref_get_name(branch_ref));
7723 goto done;
7725 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7726 if (*branch_name == NULL) {
7727 err = got_error_from_errno("strdup");
7728 goto done;
7731 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7732 if (err)
7733 goto done;
7735 err = got_ref_resolve(branch_tip, repo, commit_ref);
7736 if (err)
7737 goto done;
7738 done:
7739 free(commit_refname);
7740 free(branch_refname);
7741 free(fileindex_path);
7742 if (commit_ref)
7743 got_ref_close(commit_ref);
7744 if (branch_ref)
7745 got_ref_close(branch_ref);
7746 if (err) {
7747 if (*branch_name) {
7748 free(*branch_name);
7749 *branch_name = NULL;
7751 free(*branch_tip);
7752 *branch_tip = NULL;
7753 if (*fileindex) {
7754 got_fileindex_free(*fileindex);
7755 *fileindex = NULL;
7757 lock_worktree(worktree, LOCK_SH);
7759 return err;
7762 const struct got_error *
7763 got_worktree_merge_abort(struct got_worktree *worktree,
7764 struct got_fileindex *fileindex, struct got_repository *repo,
7765 got_worktree_checkout_cb progress_cb, void *progress_arg)
7767 const struct got_error *err, *unlockerr, *sync_err;
7768 struct got_object_id *commit_id = NULL;
7769 char *fileindex_path = NULL;
7770 struct revert_file_args rfa;
7771 struct got_object_id *tree_id = NULL;
7773 err = got_object_id_by_path(&tree_id, repo,
7774 worktree->base_commit_id, worktree->path_prefix);
7775 if (err)
7776 goto done;
7778 err = delete_merge_refs(worktree, repo);
7779 if (err)
7780 goto done;
7782 err = get_fileindex_path(&fileindex_path, worktree);
7783 if (err)
7784 goto done;
7786 rfa.worktree = worktree;
7787 rfa.fileindex = fileindex;
7788 rfa.progress_cb = progress_cb;
7789 rfa.progress_arg = progress_arg;
7790 rfa.patch_cb = NULL;
7791 rfa.patch_arg = NULL;
7792 rfa.repo = repo;
7793 rfa.unlink_added_files = 1;
7794 err = worktree_status(worktree, "", fileindex, repo,
7795 revert_file, &rfa, NULL, NULL, 1, 0);
7796 if (err)
7797 goto sync;
7799 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7800 repo, progress_cb, progress_arg, NULL, NULL);
7801 sync:
7802 sync_err = sync_fileindex(fileindex, fileindex_path);
7803 if (sync_err && err == NULL)
7804 err = sync_err;
7805 done:
7806 free(tree_id);
7807 free(commit_id);
7808 if (fileindex)
7809 got_fileindex_free(fileindex);
7810 free(fileindex_path);
7812 unlockerr = lock_worktree(worktree, LOCK_SH);
7813 if (unlockerr && err == NULL)
7814 err = unlockerr;
7815 return err;
7818 struct check_stage_ok_arg {
7819 struct got_object_id *head_commit_id;
7820 struct got_worktree *worktree;
7821 struct got_fileindex *fileindex;
7822 struct got_repository *repo;
7823 int have_changes;
7826 const struct got_error *
7827 check_stage_ok(void *arg, unsigned char status,
7828 unsigned char staged_status, const char *relpath,
7829 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7830 struct got_object_id *commit_id, int dirfd, const char *de_name)
7832 struct check_stage_ok_arg *a = arg;
7833 const struct got_error *err = NULL;
7834 struct got_fileindex_entry *ie;
7835 struct got_object_id base_commit_id;
7836 struct got_object_id *base_commit_idp = NULL;
7837 char *in_repo_path = NULL, *p;
7839 if (status == GOT_STATUS_UNVERSIONED ||
7840 status == GOT_STATUS_NO_CHANGE)
7841 return NULL;
7842 if (status == GOT_STATUS_NONEXISTENT)
7843 return got_error_set_errno(ENOENT, relpath);
7845 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7846 if (ie == NULL)
7847 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7849 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7850 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7851 relpath) == -1)
7852 return got_error_from_errno("asprintf");
7854 if (got_fileindex_entry_has_commit(ie)) {
7855 memcpy(base_commit_id.sha1, ie->commit_sha1,
7856 SHA1_DIGEST_LENGTH);
7857 base_commit_idp = &base_commit_id;
7860 if (status == GOT_STATUS_CONFLICT) {
7861 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7862 goto done;
7863 } else if (status != GOT_STATUS_ADD &&
7864 status != GOT_STATUS_MODIFY &&
7865 status != GOT_STATUS_DELETE) {
7866 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7867 goto done;
7870 a->have_changes = 1;
7872 p = in_repo_path;
7873 while (p[0] == '/')
7874 p++;
7875 err = check_out_of_date(p, status, staged_status,
7876 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7877 GOT_ERR_STAGE_OUT_OF_DATE);
7878 done:
7879 free(in_repo_path);
7880 return err;
7883 struct stage_path_arg {
7884 struct got_worktree *worktree;
7885 struct got_fileindex *fileindex;
7886 struct got_repository *repo;
7887 got_worktree_status_cb status_cb;
7888 void *status_arg;
7889 got_worktree_patch_cb patch_cb;
7890 void *patch_arg;
7891 int staged_something;
7892 int allow_bad_symlinks;
7895 static const struct got_error *
7896 stage_path(void *arg, unsigned char status,
7897 unsigned char staged_status, const char *relpath,
7898 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7899 struct got_object_id *commit_id, int dirfd, const char *de_name)
7901 struct stage_path_arg *a = arg;
7902 const struct got_error *err = NULL;
7903 struct got_fileindex_entry *ie;
7904 char *ondisk_path = NULL, *path_content = NULL;
7905 uint32_t stage;
7906 struct got_object_id *new_staged_blob_id = NULL;
7907 struct stat sb;
7909 if (status == GOT_STATUS_UNVERSIONED)
7910 return NULL;
7912 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7913 if (ie == NULL)
7914 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7916 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7917 relpath)== -1)
7918 return got_error_from_errno("asprintf");
7920 switch (status) {
7921 case GOT_STATUS_ADD:
7922 case GOT_STATUS_MODIFY:
7923 /* XXX could sb.st_mode be passed in by our caller? */
7924 if (lstat(ondisk_path, &sb) == -1) {
7925 err = got_error_from_errno2("lstat", ondisk_path);
7926 break;
7928 if (a->patch_cb) {
7929 if (status == GOT_STATUS_ADD) {
7930 int choice = GOT_PATCH_CHOICE_NONE;
7931 err = (*a->patch_cb)(&choice, a->patch_arg,
7932 status, ie->path, NULL, 1, 1);
7933 if (err)
7934 break;
7935 if (choice != GOT_PATCH_CHOICE_YES)
7936 break;
7937 } else {
7938 err = create_patched_content(&path_content, 0,
7939 staged_blob_id ? staged_blob_id : blob_id,
7940 ondisk_path, dirfd, de_name, ie->path,
7941 a->repo, a->patch_cb, a->patch_arg);
7942 if (err || path_content == NULL)
7943 break;
7946 err = got_object_blob_create(&new_staged_blob_id,
7947 path_content ? path_content : ondisk_path, a->repo);
7948 if (err)
7949 break;
7950 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7951 SHA1_DIGEST_LENGTH);
7952 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7953 stage = GOT_FILEIDX_STAGE_ADD;
7954 else
7955 stage = GOT_FILEIDX_STAGE_MODIFY;
7956 got_fileindex_entry_stage_set(ie, stage);
7957 if (S_ISLNK(sb.st_mode)) {
7958 int is_bad_symlink = 0;
7959 if (!a->allow_bad_symlinks) {
7960 char target_path[PATH_MAX];
7961 ssize_t target_len;
7962 target_len = readlink(ondisk_path, target_path,
7963 sizeof(target_path));
7964 if (target_len == -1) {
7965 err = got_error_from_errno2("readlink",
7966 ondisk_path);
7967 break;
7969 err = is_bad_symlink_target(&is_bad_symlink,
7970 target_path, target_len, ondisk_path,
7971 a->worktree->root_path);
7972 if (err)
7973 break;
7974 if (is_bad_symlink) {
7975 err = got_error_path(ondisk_path,
7976 GOT_ERR_BAD_SYMLINK);
7977 break;
7980 if (is_bad_symlink)
7981 got_fileindex_entry_staged_filetype_set(ie,
7982 GOT_FILEIDX_MODE_BAD_SYMLINK);
7983 else
7984 got_fileindex_entry_staged_filetype_set(ie,
7985 GOT_FILEIDX_MODE_SYMLINK);
7986 } else {
7987 got_fileindex_entry_staged_filetype_set(ie,
7988 GOT_FILEIDX_MODE_REGULAR_FILE);
7990 a->staged_something = 1;
7991 if (a->status_cb == NULL)
7992 break;
7993 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7994 get_staged_status(ie), relpath, blob_id,
7995 new_staged_blob_id, NULL, dirfd, de_name);
7996 break;
7997 case GOT_STATUS_DELETE:
7998 if (staged_status == GOT_STATUS_DELETE)
7999 break;
8000 if (a->patch_cb) {
8001 int choice = GOT_PATCH_CHOICE_NONE;
8002 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8003 ie->path, NULL, 1, 1);
8004 if (err)
8005 break;
8006 if (choice == GOT_PATCH_CHOICE_NO)
8007 break;
8008 if (choice != GOT_PATCH_CHOICE_YES) {
8009 err = got_error(GOT_ERR_PATCH_CHOICE);
8010 break;
8013 stage = GOT_FILEIDX_STAGE_DELETE;
8014 got_fileindex_entry_stage_set(ie, stage);
8015 a->staged_something = 1;
8016 if (a->status_cb == NULL)
8017 break;
8018 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8019 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8020 de_name);
8021 break;
8022 case GOT_STATUS_NO_CHANGE:
8023 break;
8024 case GOT_STATUS_CONFLICT:
8025 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8026 break;
8027 case GOT_STATUS_NONEXISTENT:
8028 err = got_error_set_errno(ENOENT, relpath);
8029 break;
8030 default:
8031 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8032 break;
8035 if (path_content && unlink(path_content) == -1 && err == NULL)
8036 err = got_error_from_errno2("unlink", path_content);
8037 free(path_content);
8038 free(ondisk_path);
8039 free(new_staged_blob_id);
8040 return err;
8043 const struct got_error *
8044 got_worktree_stage(struct got_worktree *worktree,
8045 struct got_pathlist_head *paths,
8046 got_worktree_status_cb status_cb, void *status_arg,
8047 got_worktree_patch_cb patch_cb, void *patch_arg,
8048 int allow_bad_symlinks, struct got_repository *repo)
8050 const struct got_error *err = NULL, *sync_err, *unlockerr;
8051 struct got_pathlist_entry *pe;
8052 struct got_fileindex *fileindex = NULL;
8053 char *fileindex_path = NULL;
8054 struct got_reference *head_ref = NULL;
8055 struct got_object_id *head_commit_id = NULL;
8056 struct check_stage_ok_arg oka;
8057 struct stage_path_arg spa;
8059 err = lock_worktree(worktree, LOCK_EX);
8060 if (err)
8061 return err;
8063 err = got_ref_open(&head_ref, repo,
8064 got_worktree_get_head_ref_name(worktree), 0);
8065 if (err)
8066 goto done;
8067 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8068 if (err)
8069 goto done;
8070 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8071 if (err)
8072 goto done;
8074 /* Check pre-conditions before staging anything. */
8075 oka.head_commit_id = head_commit_id;
8076 oka.worktree = worktree;
8077 oka.fileindex = fileindex;
8078 oka.repo = repo;
8079 oka.have_changes = 0;
8080 TAILQ_FOREACH(pe, paths, entry) {
8081 err = worktree_status(worktree, pe->path, fileindex, repo,
8082 check_stage_ok, &oka, NULL, NULL, 1, 0);
8083 if (err)
8084 goto done;
8086 if (!oka.have_changes) {
8087 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8088 goto done;
8091 spa.worktree = worktree;
8092 spa.fileindex = fileindex;
8093 spa.repo = repo;
8094 spa.patch_cb = patch_cb;
8095 spa.patch_arg = patch_arg;
8096 spa.status_cb = status_cb;
8097 spa.status_arg = status_arg;
8098 spa.staged_something = 0;
8099 spa.allow_bad_symlinks = allow_bad_symlinks;
8100 TAILQ_FOREACH(pe, paths, entry) {
8101 err = worktree_status(worktree, pe->path, fileindex, repo,
8102 stage_path, &spa, NULL, NULL, 1, 0);
8103 if (err)
8104 goto done;
8106 if (!spa.staged_something) {
8107 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8108 goto done;
8111 sync_err = sync_fileindex(fileindex, fileindex_path);
8112 if (sync_err && err == NULL)
8113 err = sync_err;
8114 done:
8115 if (head_ref)
8116 got_ref_close(head_ref);
8117 free(head_commit_id);
8118 free(fileindex_path);
8119 if (fileindex)
8120 got_fileindex_free(fileindex);
8121 unlockerr = lock_worktree(worktree, LOCK_SH);
8122 if (unlockerr && err == NULL)
8123 err = unlockerr;
8124 return err;
8127 struct unstage_path_arg {
8128 struct got_worktree *worktree;
8129 struct got_fileindex *fileindex;
8130 struct got_repository *repo;
8131 got_worktree_checkout_cb progress_cb;
8132 void *progress_arg;
8133 got_worktree_patch_cb patch_cb;
8134 void *patch_arg;
8137 static const struct got_error *
8138 create_unstaged_content(char **path_unstaged_content,
8139 char **path_new_staged_content, struct got_object_id *blob_id,
8140 struct got_object_id *staged_blob_id, const char *relpath,
8141 struct got_repository *repo,
8142 got_worktree_patch_cb patch_cb, void *patch_arg)
8144 const struct got_error *err, *free_err;
8145 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8146 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8147 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8148 struct got_diffreg_result *diffreg_result = NULL;
8149 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8150 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8152 *path_unstaged_content = NULL;
8153 *path_new_staged_content = NULL;
8155 err = got_object_id_str(&label1, blob_id);
8156 if (err)
8157 return err;
8158 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8159 if (err)
8160 goto done;
8162 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8163 if (err)
8164 goto done;
8166 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8167 if (err)
8168 goto done;
8170 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8171 if (err)
8172 goto done;
8174 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8175 if (err)
8176 goto done;
8178 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8179 if (err)
8180 goto done;
8182 err = got_diff_files(&diffreg_result, f1, label1, f2,
8183 path2, 3, 0, 1, NULL);
8184 if (err)
8185 goto done;
8187 err = got_opentemp_named(path_unstaged_content, &outfile,
8188 "got-unstaged-content");
8189 if (err)
8190 goto done;
8191 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8192 "got-new-staged-content");
8193 if (err)
8194 goto done;
8196 if (fseek(f1, 0L, SEEK_SET) == -1) {
8197 err = got_ferror(f1, GOT_ERR_IO);
8198 goto done;
8200 if (fseek(f2, 0L, SEEK_SET) == -1) {
8201 err = got_ferror(f2, GOT_ERR_IO);
8202 goto done;
8204 /* Count the number of actual changes in the diff result. */
8205 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8206 struct diff_chunk_context cc = {};
8207 diff_chunk_context_load_change(&cc, &nchunks_used,
8208 diffreg_result->result, n, 0);
8209 nchanges++;
8211 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8212 int choice;
8213 err = apply_or_reject_change(&choice, &nchunks_used,
8214 diffreg_result->result, n, relpath, f1, f2,
8215 &line_cur1, &line_cur2,
8216 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8217 if (err)
8218 goto done;
8219 if (choice == GOT_PATCH_CHOICE_YES)
8220 have_content = 1;
8221 else
8222 have_rejected_content = 1;
8223 if (choice == GOT_PATCH_CHOICE_QUIT)
8224 break;
8226 if (have_content || have_rejected_content)
8227 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8228 outfile, rejectfile);
8229 done:
8230 free(label1);
8231 if (blob)
8232 got_object_blob_close(blob);
8233 if (staged_blob)
8234 got_object_blob_close(staged_blob);
8235 free_err = got_diffreg_result_free(diffreg_result);
8236 if (free_err && err == NULL)
8237 err = free_err;
8238 if (f1 && fclose(f1) == EOF && err == NULL)
8239 err = got_error_from_errno2("fclose", path1);
8240 if (f2 && fclose(f2) == EOF && err == NULL)
8241 err = got_error_from_errno2("fclose", path2);
8242 if (outfile && fclose(outfile) == EOF && err == NULL)
8243 err = got_error_from_errno2("fclose", *path_unstaged_content);
8244 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8245 err = got_error_from_errno2("fclose", *path_new_staged_content);
8246 if (path1 && unlink(path1) == -1 && err == NULL)
8247 err = got_error_from_errno2("unlink", path1);
8248 if (path2 && unlink(path2) == -1 && err == NULL)
8249 err = got_error_from_errno2("unlink", path2);
8250 if (err || !have_content) {
8251 if (*path_unstaged_content &&
8252 unlink(*path_unstaged_content) == -1 && err == NULL)
8253 err = got_error_from_errno2("unlink",
8254 *path_unstaged_content);
8255 free(*path_unstaged_content);
8256 *path_unstaged_content = NULL;
8258 if (err || !have_content || !have_rejected_content) {
8259 if (*path_new_staged_content &&
8260 unlink(*path_new_staged_content) == -1 && err == NULL)
8261 err = got_error_from_errno2("unlink",
8262 *path_new_staged_content);
8263 free(*path_new_staged_content);
8264 *path_new_staged_content = NULL;
8266 free(path1);
8267 free(path2);
8268 return err;
8271 static const struct got_error *
8272 unstage_hunks(struct got_object_id *staged_blob_id,
8273 struct got_blob_object *blob_base,
8274 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8275 const char *ondisk_path, const char *label_orig,
8276 struct got_worktree *worktree, struct got_repository *repo,
8277 got_worktree_patch_cb patch_cb, void *patch_arg,
8278 got_worktree_checkout_cb progress_cb, void *progress_arg)
8280 const struct got_error *err = NULL;
8281 char *path_unstaged_content = NULL;
8282 char *path_new_staged_content = NULL;
8283 char *parent = NULL, *base_path = NULL;
8284 char *blob_base_path = NULL;
8285 struct got_object_id *new_staged_blob_id = NULL;
8286 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8287 struct stat sb;
8289 err = create_unstaged_content(&path_unstaged_content,
8290 &path_new_staged_content, blob_id, staged_blob_id,
8291 ie->path, repo, patch_cb, patch_arg);
8292 if (err)
8293 return err;
8295 if (path_unstaged_content == NULL)
8296 return NULL;
8298 if (path_new_staged_content) {
8299 err = got_object_blob_create(&new_staged_blob_id,
8300 path_new_staged_content, repo);
8301 if (err)
8302 goto done;
8305 f = fopen(path_unstaged_content, "re");
8306 if (f == NULL) {
8307 err = got_error_from_errno2("fopen",
8308 path_unstaged_content);
8309 goto done;
8311 if (fstat(fileno(f), &sb) == -1) {
8312 err = got_error_from_errno2("fstat", path_unstaged_content);
8313 goto done;
8315 if (got_fileindex_entry_staged_filetype_get(ie) ==
8316 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8317 char link_target[PATH_MAX];
8318 size_t r;
8319 r = fread(link_target, 1, sizeof(link_target), f);
8320 if (r == 0 && ferror(f)) {
8321 err = got_error_from_errno("fread");
8322 goto done;
8324 if (r >= sizeof(link_target)) { /* should not happen */
8325 err = got_error(GOT_ERR_NO_SPACE);
8326 goto done;
8328 link_target[r] = '\0';
8329 err = merge_symlink(worktree, blob_base,
8330 ondisk_path, ie->path, label_orig, link_target,
8331 worktree->base_commit_id, repo, progress_cb,
8332 progress_arg);
8333 } else {
8334 int local_changes_subsumed;
8336 err = got_path_dirname(&parent, ondisk_path);
8337 if (err)
8338 return err;
8340 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8341 parent) == -1) {
8342 err = got_error_from_errno("asprintf");
8343 base_path = NULL;
8344 goto done;
8347 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8348 if (err)
8349 goto done;
8350 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8351 blob_base);
8352 if (err)
8353 goto done;
8356 * In order the run a 3-way merge with a symlink we copy the symlink's
8357 * target path into a temporary file and use that file with diff3.
8359 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8360 err = dump_symlink_target_path_to_file(&f_deriv2,
8361 ondisk_path);
8362 if (err)
8363 goto done;
8364 } else {
8365 int fd;
8366 fd = open(ondisk_path,
8367 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8368 if (fd == -1) {
8369 err = got_error_from_errno2("open", ondisk_path);
8370 goto done;
8372 f_deriv2 = fdopen(fd, "r");
8373 if (f_deriv2 == NULL) {
8374 err = got_error_from_errno2("fdopen", ondisk_path);
8375 close(fd);
8376 goto done;
8380 err = merge_file(&local_changes_subsumed, worktree,
8381 f_base, f, f_deriv2, ondisk_path, ie->path,
8382 got_fileindex_perms_to_st(ie),
8383 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8384 repo, progress_cb, progress_arg);
8386 if (err)
8387 goto done;
8389 if (new_staged_blob_id) {
8390 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8391 SHA1_DIGEST_LENGTH);
8392 } else {
8393 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8394 got_fileindex_entry_staged_filetype_set(ie, 0);
8396 done:
8397 free(new_staged_blob_id);
8398 if (path_unstaged_content &&
8399 unlink(path_unstaged_content) == -1 && err == NULL)
8400 err = got_error_from_errno2("unlink", path_unstaged_content);
8401 if (path_new_staged_content &&
8402 unlink(path_new_staged_content) == -1 && err == NULL)
8403 err = got_error_from_errno2("unlink", path_new_staged_content);
8404 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8405 err = got_error_from_errno2("unlink", blob_base_path);
8406 if (f_base && fclose(f_base) == EOF && err == NULL)
8407 err = got_error_from_errno2("fclose", path_unstaged_content);
8408 if (f && fclose(f) == EOF && err == NULL)
8409 err = got_error_from_errno2("fclose", path_unstaged_content);
8410 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8411 err = got_error_from_errno2("fclose", ondisk_path);
8412 free(path_unstaged_content);
8413 free(path_new_staged_content);
8414 free(blob_base_path);
8415 free(parent);
8416 free(base_path);
8417 return err;
8420 static const struct got_error *
8421 unstage_path(void *arg, unsigned char status,
8422 unsigned char staged_status, const char *relpath,
8423 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8424 struct got_object_id *commit_id, int dirfd, const char *de_name)
8426 const struct got_error *err = NULL;
8427 struct unstage_path_arg *a = arg;
8428 struct got_fileindex_entry *ie;
8429 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8430 char *ondisk_path = NULL;
8431 char *id_str = NULL, *label_orig = NULL;
8432 int local_changes_subsumed;
8433 struct stat sb;
8435 if (staged_status != GOT_STATUS_ADD &&
8436 staged_status != GOT_STATUS_MODIFY &&
8437 staged_status != GOT_STATUS_DELETE)
8438 return NULL;
8440 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8441 if (ie == NULL)
8442 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8444 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8445 == -1)
8446 return got_error_from_errno("asprintf");
8448 err = got_object_id_str(&id_str,
8449 commit_id ? commit_id : a->worktree->base_commit_id);
8450 if (err)
8451 goto done;
8452 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8453 id_str) == -1) {
8454 err = got_error_from_errno("asprintf");
8455 goto done;
8458 switch (staged_status) {
8459 case GOT_STATUS_MODIFY:
8460 err = got_object_open_as_blob(&blob_base, a->repo,
8461 blob_id, 8192);
8462 if (err)
8463 break;
8464 /* fall through */
8465 case GOT_STATUS_ADD:
8466 if (a->patch_cb) {
8467 if (staged_status == GOT_STATUS_ADD) {
8468 int choice = GOT_PATCH_CHOICE_NONE;
8469 err = (*a->patch_cb)(&choice, a->patch_arg,
8470 staged_status, ie->path, NULL, 1, 1);
8471 if (err)
8472 break;
8473 if (choice != GOT_PATCH_CHOICE_YES)
8474 break;
8475 } else {
8476 err = unstage_hunks(staged_blob_id,
8477 blob_base, blob_id, ie, ondisk_path,
8478 label_orig, a->worktree, a->repo,
8479 a->patch_cb, a->patch_arg,
8480 a->progress_cb, a->progress_arg);
8481 break; /* Done with this file. */
8484 err = got_object_open_as_blob(&blob_staged, a->repo,
8485 staged_blob_id, 8192);
8486 if (err)
8487 break;
8488 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8489 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8490 case GOT_FILEIDX_MODE_REGULAR_FILE:
8491 err = merge_blob(&local_changes_subsumed, a->worktree,
8492 blob_base, ondisk_path, relpath,
8493 got_fileindex_perms_to_st(ie), label_orig,
8494 blob_staged, commit_id ? commit_id :
8495 a->worktree->base_commit_id, a->repo,
8496 a->progress_cb, a->progress_arg);
8497 break;
8498 case GOT_FILEIDX_MODE_SYMLINK:
8499 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8500 char *staged_target;
8501 err = got_object_blob_read_to_str(
8502 &staged_target, blob_staged);
8503 if (err)
8504 goto done;
8505 err = merge_symlink(a->worktree, blob_base,
8506 ondisk_path, relpath, label_orig,
8507 staged_target, commit_id ? commit_id :
8508 a->worktree->base_commit_id,
8509 a->repo, a->progress_cb, a->progress_arg);
8510 free(staged_target);
8511 } else {
8512 err = merge_blob(&local_changes_subsumed,
8513 a->worktree, blob_base, ondisk_path,
8514 relpath, got_fileindex_perms_to_st(ie),
8515 label_orig, blob_staged,
8516 commit_id ? commit_id :
8517 a->worktree->base_commit_id, a->repo,
8518 a->progress_cb, a->progress_arg);
8520 break;
8521 default:
8522 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8523 break;
8525 if (err == NULL) {
8526 got_fileindex_entry_stage_set(ie,
8527 GOT_FILEIDX_STAGE_NONE);
8528 got_fileindex_entry_staged_filetype_set(ie, 0);
8530 break;
8531 case GOT_STATUS_DELETE:
8532 if (a->patch_cb) {
8533 int choice = GOT_PATCH_CHOICE_NONE;
8534 err = (*a->patch_cb)(&choice, a->patch_arg,
8535 staged_status, ie->path, NULL, 1, 1);
8536 if (err)
8537 break;
8538 if (choice == GOT_PATCH_CHOICE_NO)
8539 break;
8540 if (choice != GOT_PATCH_CHOICE_YES) {
8541 err = got_error(GOT_ERR_PATCH_CHOICE);
8542 break;
8545 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8546 got_fileindex_entry_staged_filetype_set(ie, 0);
8547 err = get_file_status(&status, &sb, ie, ondisk_path,
8548 dirfd, de_name, a->repo);
8549 if (err)
8550 break;
8551 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8552 break;
8554 done:
8555 free(ondisk_path);
8556 if (blob_base)
8557 got_object_blob_close(blob_base);
8558 if (blob_staged)
8559 got_object_blob_close(blob_staged);
8560 free(id_str);
8561 free(label_orig);
8562 return err;
8565 const struct got_error *
8566 got_worktree_unstage(struct got_worktree *worktree,
8567 struct got_pathlist_head *paths,
8568 got_worktree_checkout_cb progress_cb, void *progress_arg,
8569 got_worktree_patch_cb patch_cb, void *patch_arg,
8570 struct got_repository *repo)
8572 const struct got_error *err = NULL, *sync_err, *unlockerr;
8573 struct got_pathlist_entry *pe;
8574 struct got_fileindex *fileindex = NULL;
8575 char *fileindex_path = NULL;
8576 struct unstage_path_arg upa;
8578 err = lock_worktree(worktree, LOCK_EX);
8579 if (err)
8580 return err;
8582 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8583 if (err)
8584 goto done;
8586 upa.worktree = worktree;
8587 upa.fileindex = fileindex;
8588 upa.repo = repo;
8589 upa.progress_cb = progress_cb;
8590 upa.progress_arg = progress_arg;
8591 upa.patch_cb = patch_cb;
8592 upa.patch_arg = patch_arg;
8593 TAILQ_FOREACH(pe, paths, entry) {
8594 err = worktree_status(worktree, pe->path, fileindex, repo,
8595 unstage_path, &upa, NULL, NULL, 1, 0);
8596 if (err)
8597 goto done;
8600 sync_err = sync_fileindex(fileindex, fileindex_path);
8601 if (sync_err && err == NULL)
8602 err = sync_err;
8603 done:
8604 free(fileindex_path);
8605 if (fileindex)
8606 got_fileindex_free(fileindex);
8607 unlockerr = lock_worktree(worktree, LOCK_SH);
8608 if (unlockerr && err == NULL)
8609 err = unlockerr;
8610 return err;
8613 struct report_file_info_arg {
8614 struct got_worktree *worktree;
8615 got_worktree_path_info_cb info_cb;
8616 void *info_arg;
8617 struct got_pathlist_head *paths;
8618 got_cancel_cb cancel_cb;
8619 void *cancel_arg;
8622 static const struct got_error *
8623 report_file_info(void *arg, struct got_fileindex_entry *ie)
8625 struct report_file_info_arg *a = arg;
8626 struct got_pathlist_entry *pe;
8627 struct got_object_id blob_id, staged_blob_id, commit_id;
8628 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8629 struct got_object_id *commit_idp = NULL;
8630 int stage;
8632 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8633 return got_error(GOT_ERR_CANCELLED);
8635 TAILQ_FOREACH(pe, a->paths, entry) {
8636 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8637 got_path_is_child(ie->path, pe->path, pe->path_len))
8638 break;
8640 if (pe == NULL) /* not found */
8641 return NULL;
8643 if (got_fileindex_entry_has_blob(ie)) {
8644 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8645 blob_idp = &blob_id;
8647 stage = got_fileindex_entry_stage_get(ie);
8648 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8649 stage == GOT_FILEIDX_STAGE_ADD) {
8650 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8651 SHA1_DIGEST_LENGTH);
8652 staged_blob_idp = &staged_blob_id;
8655 if (got_fileindex_entry_has_commit(ie)) {
8656 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8657 commit_idp = &commit_id;
8660 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8661 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8664 const struct got_error *
8665 got_worktree_path_info(struct got_worktree *worktree,
8666 struct got_pathlist_head *paths,
8667 got_worktree_path_info_cb info_cb, void *info_arg,
8668 got_cancel_cb cancel_cb, void *cancel_arg)
8671 const struct got_error *err = NULL, *unlockerr;
8672 struct got_fileindex *fileindex = NULL;
8673 char *fileindex_path = NULL;
8674 struct report_file_info_arg arg;
8676 err = lock_worktree(worktree, LOCK_SH);
8677 if (err)
8678 return err;
8680 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8681 if (err)
8682 goto done;
8684 arg.worktree = worktree;
8685 arg.info_cb = info_cb;
8686 arg.info_arg = info_arg;
8687 arg.paths = paths;
8688 arg.cancel_cb = cancel_cb;
8689 arg.cancel_arg = cancel_arg;
8690 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8691 &arg);
8692 done:
8693 free(fileindex_path);
8694 if (fileindex)
8695 got_fileindex_free(fileindex);
8696 unlockerr = lock_worktree(worktree, LOCK_UN);
8697 if (unlockerr && err == NULL)
8698 err = unlockerr;
8699 return err;