Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
20 #include <dirent.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <zlib.h>
31 #include <fnmatch.h>
32 #include <libgen.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 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 *is_bad_symlink = 0;
1263 * Blob object content specifies the target path of the link.
1264 * If a symbolic link cannot be installed we instead create
1265 * a regular file which contains the link target path stored
1266 * in the blob object.
1268 do {
1269 err = got_object_blob_read_block(&len, blob);
1270 if (err)
1271 return err;
1273 if (len + target_len >= sizeof(target_path)) {
1274 /* Path too long; install as a regular file. */
1275 *is_bad_symlink = 1;
1276 got_object_blob_rewind(blob);
1277 return install_blob(worktree, ondisk_path, path,
1278 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 restoring_missing_file, reverting_versioned_file,
1280 1, path_is_unversioned, repo, progress_cb,
1281 progress_arg);
1283 if (len > 0) {
1284 /* Skip blob object header first time around. */
1285 memcpy(target_path + target_len, buf + hdrlen,
1286 len - hdrlen);
1287 target_len += len - hdrlen;
1288 hdrlen = 0;
1290 } while (len != 0);
1291 target_path[target_len] = '\0';
1293 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 ondisk_path, worktree->root_path);
1295 if (err)
1296 return err;
1298 if (*is_bad_symlink && !allow_bad_symlinks) {
1299 /* install as a regular file */
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 path_is_unversioned, repo, progress_cb, progress_arg);
1305 return err;
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 int symlink_replaced;
1311 if (path_is_unversioned) {
1312 err = (*progress_cb)(progress_arg,
1313 GOT_STATUS_UNVERSIONED, path);
1314 return err;
1316 err = replace_existing_symlink(&symlink_replaced,
1317 ondisk_path, target_path, target_len);
1318 if (err)
1319 return err;
1320 if (progress_cb) {
1321 if (symlink_replaced) {
1322 err = (*progress_cb)(progress_arg,
1323 reverting_versioned_file ?
1324 GOT_STATUS_REVERT :
1325 GOT_STATUS_UPDATE, path);
1326 } else {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_EXISTS, path);
1331 return err; /* Nothing else to do. */
1334 if (errno == ENOENT) {
1335 char *parent;
1336 err = got_path_dirname(&parent, ondisk_path);
1337 if (err)
1338 return err;
1339 err = add_dir_on_disk(worktree, parent);
1340 free(parent);
1341 if (err)
1342 return err;
1344 * Retry, and fall through to error handling
1345 * below if this second attempt fails.
1347 if (symlink(target_path, ondisk_path) != -1) {
1348 err = NULL; /* success */
1349 return err;
1353 /* Handle errors from first or second creation attempt. */
1354 if (errno == ENAMETOOLONG) {
1355 /* bad target path; install as a regular file */
1356 *is_bad_symlink = 1;
1357 got_object_blob_rewind(blob);
1358 err = install_blob(worktree, ondisk_path, path,
1359 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 restoring_missing_file, reverting_versioned_file, 1,
1361 path_is_unversioned, repo,
1362 progress_cb, progress_arg);
1363 } else if (errno == ENOTDIR) {
1364 err = got_error_path(ondisk_path,
1365 GOT_ERR_FILE_OBSTRUCTED);
1366 } else {
1367 err = got_error_from_errno3("symlink",
1368 target_path, ondisk_path);
1370 } else if (progress_cb)
1371 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
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, fd1 = -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 fd1 = got_opentempfd();
1708 if (fd1 == -1) {
1709 err = got_error_from_errno("got_opentempfd");
1710 goto done;
1712 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1713 if (err)
1714 goto done;
1716 if (S_ISLNK(sb->st_mode)) {
1717 err = get_symlink_modification_status(status, ie,
1718 abspath, dirfd, de_name, blob);
1719 goto done;
1722 if (dirfd != -1) {
1723 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1724 if (fd == -1) {
1725 err = got_error_from_errno2("openat", abspath);
1726 goto done;
1730 f = fdopen(fd, "r");
1731 if (f == NULL) {
1732 err = got_error_from_errno2("fdopen", abspath);
1733 goto done;
1735 fd = -1;
1736 hdrlen = got_object_blob_get_hdrlen(blob);
1737 for (;;) {
1738 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1739 err = got_object_blob_read_block(&blen, blob);
1740 if (err)
1741 goto done;
1742 /* Skip length of blob object header first time around. */
1743 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1744 if (flen == 0 && ferror(f)) {
1745 err = got_error_from_errno("fread");
1746 goto done;
1748 if (blen - hdrlen == 0) {
1749 if (flen != 0)
1750 *status = GOT_STATUS_MODIFY;
1751 break;
1752 } else if (flen == 0) {
1753 if (blen - hdrlen != 0)
1754 *status = GOT_STATUS_MODIFY;
1755 break;
1756 } else if (blen - hdrlen == flen) {
1757 /* Skip blob object header first time around. */
1758 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1759 *status = GOT_STATUS_MODIFY;
1760 break;
1762 } else {
1763 *status = GOT_STATUS_MODIFY;
1764 break;
1766 hdrlen = 0;
1769 if (*status == GOT_STATUS_MODIFY) {
1770 rewind(f);
1771 err = get_modified_file_content_status(status, f);
1772 } else if (xbit_differs(ie, sb->st_mode))
1773 *status = GOT_STATUS_MODE_CHANGE;
1774 done:
1775 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1776 err = got_error_from_errno("close");
1777 if (blob)
1778 got_object_blob_close(blob);
1779 if (f != NULL && fclose(f) == EOF && err == NULL)
1780 err = got_error_from_errno2("fclose", abspath);
1781 if (fd != -1 && close(fd) == -1 && err == NULL)
1782 err = got_error_from_errno2("close", abspath);
1783 return err;
1787 * Update timestamps in the file index if a file is unmodified and
1788 * we had to run a full content comparison to find out.
1790 static const struct got_error *
1791 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1792 struct got_fileindex_entry *ie, struct stat *sb)
1794 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1795 return got_fileindex_entry_update(ie, wt_fd, path,
1796 ie->blob_sha1, ie->commit_sha1, 1);
1798 return NULL;
1801 static const struct got_error *
1802 update_blob(struct got_worktree *worktree,
1803 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1804 struct got_tree_entry *te, const char *path,
1805 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1806 void *progress_arg)
1808 const struct got_error *err = NULL;
1809 struct got_blob_object *blob = NULL;
1810 char *ondisk_path = NULL;
1811 unsigned char status = GOT_STATUS_NO_CHANGE;
1812 struct stat sb;
1813 int fd1 = -1, fd2 = -1;
1815 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1816 return got_error_from_errno("asprintf");
1818 if (ie) {
1819 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1820 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1821 goto done;
1823 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1824 repo);
1825 if (err)
1826 goto done;
1827 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1828 sb.st_mode = got_fileindex_perms_to_st(ie);
1829 } else {
1830 if (stat(ondisk_path, &sb) == -1) {
1831 if (errno != ENOENT) {
1832 err = got_error_from_errno2("stat",
1833 ondisk_path);
1834 goto done;
1836 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1837 status = GOT_STATUS_UNVERSIONED;
1838 } else {
1839 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1840 status = GOT_STATUS_UNVERSIONED;
1841 else
1842 status = GOT_STATUS_OBSTRUCTED;
1846 if (status == GOT_STATUS_OBSTRUCTED) {
1847 if (ie)
1848 got_fileindex_entry_mark_skipped(ie);
1849 err = (*progress_cb)(progress_arg, status, path);
1850 goto done;
1852 if (status == GOT_STATUS_CONFLICT) {
1853 if (ie)
1854 got_fileindex_entry_mark_skipped(ie);
1855 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1856 path);
1857 goto done;
1860 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1861 (S_ISLNK(te->mode) ||
1862 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1864 * This is a regular file or an installed bad symlink.
1865 * If the file index indicates that this file is already
1866 * up-to-date with respect to the repository we can skip
1867 * updating contents of this file.
1869 if (got_fileindex_entry_has_commit(ie) &&
1870 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1871 SHA1_DIGEST_LENGTH) == 0) {
1872 /* Same commit. */
1873 err = sync_timestamps(worktree->root_fd,
1874 path, status, ie, &sb);
1875 if (err)
1876 goto done;
1877 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1878 path);
1879 goto done;
1881 if (got_fileindex_entry_has_blob(ie) &&
1882 memcmp(ie->blob_sha1, te->id.sha1,
1883 SHA1_DIGEST_LENGTH) == 0) {
1884 /* Different commit but the same blob. */
1885 err = sync_timestamps(worktree->root_fd,
1886 path, status, ie, &sb);
1887 if (err)
1888 goto done;
1889 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1890 path);
1891 goto done;
1895 fd1 = got_opentempfd();
1896 if (fd1 == -1) {
1897 err = got_error_from_errno("got_opentempfd");
1898 goto done;
1900 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1901 if (err)
1902 goto done;
1904 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1905 int update_timestamps;
1906 struct got_blob_object *blob2 = NULL;
1907 char *label_orig = NULL;
1908 if (got_fileindex_entry_has_blob(ie)) {
1909 fd2 = got_opentempfd();
1910 if (fd2 == -1) {
1911 err = got_error_from_errno("got_opentempfd");
1912 goto done;
1914 struct got_object_id id2;
1915 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1916 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1917 fd2);
1918 if (err)
1919 goto done;
1921 if (got_fileindex_entry_has_commit(ie)) {
1922 char id_str[SHA1_DIGEST_STRING_LENGTH];
1923 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1924 sizeof(id_str)) == NULL) {
1925 err = got_error_path(id_str,
1926 GOT_ERR_BAD_OBJ_ID_STR);
1927 goto done;
1929 if (asprintf(&label_orig, "%s: commit %s",
1930 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1931 err = got_error_from_errno("asprintf");
1932 goto done;
1935 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1936 char *link_target;
1937 err = got_object_blob_read_to_str(&link_target, blob);
1938 if (err)
1939 goto done;
1940 err = merge_symlink(worktree, blob2, ondisk_path, path,
1941 label_orig, link_target, worktree->base_commit_id,
1942 repo, progress_cb, progress_arg);
1943 free(link_target);
1944 } else {
1945 err = merge_blob(&update_timestamps, worktree, blob2,
1946 ondisk_path, path, sb.st_mode, label_orig, blob,
1947 worktree->base_commit_id, repo,
1948 progress_cb, progress_arg);
1950 free(label_orig);
1951 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1952 err = got_error_from_errno("close");
1953 goto done;
1955 if (blob2)
1956 got_object_blob_close(blob2);
1957 if (err)
1958 goto done;
1960 * Do not update timestamps of files with local changes.
1961 * Otherwise, a future status walk would treat them as
1962 * unmodified files again.
1964 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1965 blob->id.sha1, worktree->base_commit_id->sha1,
1966 update_timestamps);
1967 } else if (status == GOT_STATUS_MODE_CHANGE) {
1968 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1969 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1970 } else if (status == GOT_STATUS_DELETE) {
1971 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1972 if (err)
1973 goto done;
1974 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1975 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1976 if (err)
1977 goto done;
1978 } else {
1979 int is_bad_symlink = 0;
1980 if (S_ISLNK(te->mode)) {
1981 err = install_symlink(&is_bad_symlink, worktree,
1982 ondisk_path, path, blob,
1983 status == GOT_STATUS_MISSING, 0,
1984 status == GOT_STATUS_UNVERSIONED, 0,
1985 repo, progress_cb, progress_arg);
1986 } else {
1987 err = install_blob(worktree, ondisk_path, path,
1988 te->mode, sb.st_mode, blob,
1989 status == GOT_STATUS_MISSING, 0, 0,
1990 status == GOT_STATUS_UNVERSIONED, repo,
1991 progress_cb, progress_arg);
1993 if (err)
1994 goto done;
1996 if (ie) {
1997 err = got_fileindex_entry_update(ie,
1998 worktree->root_fd, path, blob->id.sha1,
1999 worktree->base_commit_id->sha1, 1);
2000 } else {
2001 err = create_fileindex_entry(&ie, fileindex,
2002 worktree->base_commit_id, worktree->root_fd, path,
2003 &blob->id);
2005 if (err)
2006 goto done;
2008 if (is_bad_symlink) {
2009 got_fileindex_entry_filetype_set(ie,
2010 GOT_FILEIDX_MODE_BAD_SYMLINK);
2014 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2015 err = got_error_from_errno("close");
2016 goto done;
2018 got_object_blob_close(blob);
2019 done:
2020 free(ondisk_path);
2021 return err;
2024 static const struct got_error *
2025 remove_ondisk_file(const char *root_path, const char *path)
2027 const struct got_error *err = NULL;
2028 char *ondisk_path = NULL, *parent = NULL;
2030 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2031 return got_error_from_errno("asprintf");
2033 if (unlink(ondisk_path) == -1) {
2034 if (errno != ENOENT)
2035 err = got_error_from_errno2("unlink", ondisk_path);
2036 } else {
2037 size_t root_len = strlen(root_path);
2038 err = got_path_dirname(&parent, ondisk_path);
2039 if (err)
2040 goto done;
2041 while (got_path_cmp(parent, root_path,
2042 strlen(parent), root_len) != 0) {
2043 free(ondisk_path);
2044 ondisk_path = parent;
2045 parent = NULL;
2046 if (rmdir(ondisk_path) == -1) {
2047 if (errno != ENOTEMPTY)
2048 err = got_error_from_errno2("rmdir",
2049 ondisk_path);
2050 break;
2052 err = got_path_dirname(&parent, ondisk_path);
2053 if (err)
2054 break;
2057 done:
2058 free(ondisk_path);
2059 free(parent);
2060 return err;
2063 static const struct got_error *
2064 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2065 struct got_fileindex_entry *ie, struct got_repository *repo,
2066 got_worktree_checkout_cb progress_cb, void *progress_arg)
2068 const struct got_error *err = NULL;
2069 unsigned char status;
2070 struct stat sb;
2071 char *ondisk_path;
2073 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2074 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2076 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2077 == -1)
2078 return got_error_from_errno("asprintf");
2080 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2081 if (err)
2082 goto done;
2084 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2085 char ondisk_target[PATH_MAX];
2086 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2087 sizeof(ondisk_target));
2088 if (ondisk_len == -1) {
2089 err = got_error_from_errno2("readlink", ondisk_path);
2090 goto done;
2092 ondisk_target[ondisk_len] = '\0';
2093 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2094 NULL, NULL, /* XXX pass common ancestor info? */
2095 ondisk_target, ondisk_path);
2096 if (err)
2097 goto done;
2098 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2099 ie->path);
2100 goto done;
2103 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2104 status == GOT_STATUS_ADD) {
2105 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2106 if (err)
2107 goto done;
2109 * Preserve the working file and change the deleted blob's
2110 * entry into a schedule-add entry.
2112 err = got_fileindex_entry_update(ie, worktree->root_fd,
2113 ie->path, NULL, NULL, 0);
2114 } else {
2115 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2116 if (err)
2117 goto done;
2118 if (status == GOT_STATUS_NO_CHANGE) {
2119 err = remove_ondisk_file(worktree->root_path, ie->path);
2120 if (err)
2121 goto done;
2123 got_fileindex_entry_remove(fileindex, ie);
2125 done:
2126 free(ondisk_path);
2127 return err;
2130 struct diff_cb_arg {
2131 struct got_fileindex *fileindex;
2132 struct got_worktree *worktree;
2133 struct got_repository *repo;
2134 got_worktree_checkout_cb progress_cb;
2135 void *progress_arg;
2136 got_cancel_cb cancel_cb;
2137 void *cancel_arg;
2140 static const struct got_error *
2141 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2142 struct got_tree_entry *te, const char *parent_path)
2144 struct diff_cb_arg *a = arg;
2146 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 return got_error(GOT_ERR_CANCELLED);
2149 return update_blob(a->worktree, a->fileindex, ie, te,
2150 ie->path, a->repo, a->progress_cb, a->progress_arg);
2153 static const struct got_error *
2154 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2156 struct diff_cb_arg *a = arg;
2158 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2159 return got_error(GOT_ERR_CANCELLED);
2161 return delete_blob(a->worktree, a->fileindex, ie,
2162 a->repo, a->progress_cb, a->progress_arg);
2165 static const struct got_error *
2166 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2168 struct diff_cb_arg *a = arg;
2169 const struct got_error *err;
2170 char *path;
2172 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2173 return got_error(GOT_ERR_CANCELLED);
2175 if (got_object_tree_entry_is_submodule(te))
2176 return NULL;
2178 if (asprintf(&path, "%s%s%s", parent_path,
2179 parent_path[0] ? "/" : "", te->name)
2180 == -1)
2181 return got_error_from_errno("asprintf");
2183 if (S_ISDIR(te->mode))
2184 err = add_dir_on_disk(a->worktree, path);
2185 else
2186 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2187 a->repo, a->progress_cb, a->progress_arg);
2189 free(path);
2190 return err;
2193 const struct got_error *
2194 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2196 uint32_t uuid_status;
2198 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2199 if (uuid_status != uuid_s_ok) {
2200 *uuidstr = NULL;
2201 return got_error_uuid(uuid_status, "uuid_to_string");
2204 return NULL;
2207 static const struct got_error *
2208 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2210 const struct got_error *err = NULL;
2211 char *uuidstr = NULL;
2213 *refname = NULL;
2215 err = got_worktree_get_uuid(&uuidstr, worktree);
2216 if (err)
2217 return err;
2219 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2220 err = got_error_from_errno("asprintf");
2221 *refname = NULL;
2223 free(uuidstr);
2224 return err;
2227 const struct got_error *
2228 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2233 static const struct got_error *
2234 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2236 return get_ref_name(refname, worktree,
2237 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2240 static const struct got_error *
2241 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2243 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2246 static const struct got_error *
2247 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2249 return get_ref_name(refname, worktree,
2250 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2253 static const struct got_error *
2254 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2256 return get_ref_name(refname, worktree,
2257 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2260 static const struct got_error *
2261 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2263 return get_ref_name(refname, worktree,
2264 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2267 static const struct got_error *
2268 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2270 return get_ref_name(refname, worktree,
2271 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2274 static const struct got_error *
2275 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2281 static const struct got_error *
2282 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2288 const struct got_error *
2289 got_worktree_get_histedit_script_path(char **path,
2290 struct got_worktree *worktree)
2292 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2293 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2294 *path = NULL;
2295 return got_error_from_errno("asprintf");
2297 return NULL;
2300 static const struct got_error *
2301 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2303 return get_ref_name(refname, worktree,
2304 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2307 static const struct got_error *
2308 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2310 return get_ref_name(refname, worktree,
2311 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2315 * Prevent Git's garbage collector from deleting our base commit by
2316 * setting a reference to our base commit's ID.
2318 static const struct got_error *
2319 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2321 const struct got_error *err = NULL;
2322 struct got_reference *ref = NULL;
2323 char *refname;
2325 err = got_worktree_get_base_ref_name(&refname, worktree);
2326 if (err)
2327 return err;
2329 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2330 if (err)
2331 goto done;
2333 err = got_ref_write(ref, repo);
2334 done:
2335 free(refname);
2336 if (ref)
2337 got_ref_close(ref);
2338 return err;
2341 static const struct got_error *
2342 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2344 const struct got_error *err = NULL;
2346 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2347 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2348 err = got_error_from_errno("asprintf");
2349 *fileindex_path = NULL;
2351 return err;
2355 static const struct got_error *
2356 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2357 struct got_worktree *worktree)
2359 const struct got_error *err = NULL;
2360 FILE *index = NULL;
2362 *fileindex_path = NULL;
2363 *fileindex = got_fileindex_alloc();
2364 if (*fileindex == NULL)
2365 return got_error_from_errno("got_fileindex_alloc");
2367 err = get_fileindex_path(fileindex_path, worktree);
2368 if (err)
2369 goto done;
2371 index = fopen(*fileindex_path, "rbe");
2372 if (index == NULL) {
2373 if (errno != ENOENT)
2374 err = got_error_from_errno2("fopen", *fileindex_path);
2375 } else {
2376 err = got_fileindex_read(*fileindex, index);
2377 if (fclose(index) == EOF && err == NULL)
2378 err = got_error_from_errno("fclose");
2380 done:
2381 if (err) {
2382 free(*fileindex_path);
2383 *fileindex_path = NULL;
2384 got_fileindex_free(*fileindex);
2385 *fileindex = NULL;
2387 return err;
2390 struct bump_base_commit_id_arg {
2391 struct got_object_id *base_commit_id;
2392 const char *path;
2393 size_t path_len;
2394 const char *entry_name;
2395 got_worktree_checkout_cb progress_cb;
2396 void *progress_arg;
2399 /* Bump base commit ID of all files within an updated part of the work tree. */
2400 static const struct got_error *
2401 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2403 const struct got_error *err;
2404 struct bump_base_commit_id_arg *a = arg;
2406 if (a->entry_name) {
2407 if (strcmp(ie->path, a->path) != 0)
2408 return NULL;
2409 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2410 return NULL;
2412 if (got_fileindex_entry_was_skipped(ie))
2413 return NULL;
2415 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2416 SHA1_DIGEST_LENGTH) == 0)
2417 return NULL;
2419 if (a->progress_cb) {
2420 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2421 ie->path);
2422 if (err)
2423 return err;
2425 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2426 return NULL;
2429 static const struct got_error *
2430 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2431 struct got_fileindex *fileindex,
2432 got_worktree_checkout_cb progress_cb, void *progress_arg)
2434 struct bump_base_commit_id_arg bbc_arg;
2436 bbc_arg.base_commit_id = worktree->base_commit_id;
2437 bbc_arg.entry_name = NULL;
2438 bbc_arg.path = "";
2439 bbc_arg.path_len = 0;
2440 bbc_arg.progress_cb = progress_cb;
2441 bbc_arg.progress_arg = progress_arg;
2443 return got_fileindex_for_each_entry_safe(fileindex,
2444 bump_base_commit_id, &bbc_arg);
2447 static const struct got_error *
2448 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2450 const struct got_error *err = NULL;
2451 char *new_fileindex_path = NULL;
2452 FILE *new_index = NULL;
2453 struct timespec timeout;
2455 err = got_opentemp_named(&new_fileindex_path, &new_index,
2456 fileindex_path);
2457 if (err)
2458 goto done;
2460 err = got_fileindex_write(fileindex, new_index);
2461 if (err)
2462 goto done;
2464 if (rename(new_fileindex_path, fileindex_path) != 0) {
2465 err = got_error_from_errno3("rename", new_fileindex_path,
2466 fileindex_path);
2467 unlink(new_fileindex_path);
2471 * Sleep for a short amount of time to ensure that files modified after
2472 * this program exits have a different time stamp from the one which
2473 * was recorded in the file index.
2475 timeout.tv_sec = 0;
2476 timeout.tv_nsec = 1;
2477 nanosleep(&timeout, NULL);
2478 done:
2479 if (new_index)
2480 fclose(new_index);
2481 free(new_fileindex_path);
2482 return err;
2485 static const struct got_error *
2486 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2487 struct got_object_id **tree_id, const char *wt_relpath,
2488 struct got_commit_object *base_commit, struct got_worktree *worktree,
2489 struct got_repository *repo)
2491 const struct got_error *err = NULL;
2492 struct got_object_id *id = NULL;
2493 char *in_repo_path = NULL;
2494 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2496 *entry_type = GOT_OBJ_TYPE_ANY;
2497 *tree_relpath = NULL;
2498 *tree_id = NULL;
2500 if (wt_relpath[0] == '\0') {
2501 /* Check out all files within the work tree. */
2502 *entry_type = GOT_OBJ_TYPE_TREE;
2503 *tree_relpath = strdup("");
2504 if (*tree_relpath == NULL) {
2505 err = got_error_from_errno("strdup");
2506 goto done;
2508 err = got_object_id_by_path(tree_id, repo, base_commit,
2509 worktree->path_prefix);
2510 if (err)
2511 goto done;
2512 return NULL;
2515 /* Check out a subset of files in the work tree. */
2517 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2518 is_root_wt ? "" : "/", wt_relpath) == -1) {
2519 err = got_error_from_errno("asprintf");
2520 goto done;
2523 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2524 if (err)
2525 goto done;
2527 free(in_repo_path);
2528 in_repo_path = NULL;
2530 err = got_object_get_type(entry_type, repo, id);
2531 if (err)
2532 goto done;
2534 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2535 /* Check out a single file. */
2536 if (strchr(wt_relpath, '/') == NULL) {
2537 /* Check out a single file in work tree's root dir. */
2538 in_repo_path = strdup(worktree->path_prefix);
2539 if (in_repo_path == NULL) {
2540 err = got_error_from_errno("strdup");
2541 goto done;
2543 *tree_relpath = strdup("");
2544 if (*tree_relpath == NULL) {
2545 err = got_error_from_errno("strdup");
2546 goto done;
2548 } else {
2549 /* Check out a single file in a subdirectory. */
2550 err = got_path_dirname(tree_relpath, wt_relpath);
2551 if (err)
2552 return err;
2553 if (asprintf(&in_repo_path, "%s%s%s",
2554 worktree->path_prefix, is_root_wt ? "" : "/",
2555 *tree_relpath) == -1) {
2556 err = got_error_from_errno("asprintf");
2557 goto done;
2560 err = got_object_id_by_path(tree_id, repo,
2561 base_commit, in_repo_path);
2562 } else {
2563 /* Check out all files within a subdirectory. */
2564 *tree_id = got_object_id_dup(id);
2565 if (*tree_id == NULL) {
2566 err = got_error_from_errno("got_object_id_dup");
2567 goto done;
2569 *tree_relpath = strdup(wt_relpath);
2570 if (*tree_relpath == NULL) {
2571 err = got_error_from_errno("strdup");
2572 goto done;
2575 done:
2576 free(id);
2577 free(in_repo_path);
2578 if (err) {
2579 *entry_type = GOT_OBJ_TYPE_ANY;
2580 free(*tree_relpath);
2581 *tree_relpath = NULL;
2582 free(*tree_id);
2583 *tree_id = NULL;
2585 return err;
2588 static const struct got_error *
2589 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2590 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2591 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2592 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2594 const struct got_error *err = NULL;
2595 struct got_commit_object *commit = NULL;
2596 struct got_tree_object *tree = NULL;
2597 struct got_fileindex_diff_tree_cb diff_cb;
2598 struct diff_cb_arg arg;
2600 err = ref_base_commit(worktree, repo);
2601 if (err) {
2602 if (!(err->code == GOT_ERR_ERRNO &&
2603 (errno == EACCES || errno == EROFS)))
2604 goto done;
2605 err = (*progress_cb)(progress_arg,
2606 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2607 if (err)
2608 return err;
2611 err = got_object_open_as_commit(&commit, repo,
2612 worktree->base_commit_id);
2613 if (err)
2614 goto done;
2616 err = got_object_open_as_tree(&tree, repo, tree_id);
2617 if (err)
2618 goto done;
2620 if (entry_name &&
2621 got_object_tree_find_entry(tree, entry_name) == NULL) {
2622 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2623 goto done;
2626 diff_cb.diff_old_new = diff_old_new;
2627 diff_cb.diff_old = diff_old;
2628 diff_cb.diff_new = diff_new;
2629 arg.fileindex = fileindex;
2630 arg.worktree = worktree;
2631 arg.repo = repo;
2632 arg.progress_cb = progress_cb;
2633 arg.progress_arg = progress_arg;
2634 arg.cancel_cb = cancel_cb;
2635 arg.cancel_arg = cancel_arg;
2636 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2637 entry_name, repo, &diff_cb, &arg);
2638 done:
2639 if (tree)
2640 got_object_tree_close(tree);
2641 if (commit)
2642 got_object_commit_close(commit);
2643 return err;
2646 const struct got_error *
2647 got_worktree_checkout_files(struct got_worktree *worktree,
2648 struct got_pathlist_head *paths, struct got_repository *repo,
2649 got_worktree_checkout_cb progress_cb, void *progress_arg,
2650 got_cancel_cb cancel_cb, void *cancel_arg)
2652 const struct got_error *err = NULL, *sync_err, *unlockerr;
2653 struct got_commit_object *commit = NULL;
2654 struct got_tree_object *tree = NULL;
2655 struct got_fileindex *fileindex = NULL;
2656 char *fileindex_path = NULL;
2657 struct got_pathlist_entry *pe;
2658 struct tree_path_data {
2659 STAILQ_ENTRY(tree_path_data) entry;
2660 struct got_object_id *tree_id;
2661 int entry_type;
2662 char *relpath;
2663 char *entry_name;
2664 } *tpd = NULL;
2665 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2667 STAILQ_INIT(&tree_paths);
2669 err = lock_worktree(worktree, LOCK_EX);
2670 if (err)
2671 return err;
2673 err = got_object_open_as_commit(&commit, repo,
2674 worktree->base_commit_id);
2675 if (err)
2676 goto done;
2678 /* Map all specified paths to in-repository trees. */
2679 TAILQ_FOREACH(pe, paths, entry) {
2680 tpd = malloc(sizeof(*tpd));
2681 if (tpd == NULL) {
2682 err = got_error_from_errno("malloc");
2683 goto done;
2686 err = find_tree_entry_for_checkout(&tpd->entry_type,
2687 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2688 worktree, repo);
2689 if (err) {
2690 free(tpd);
2691 goto done;
2694 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2695 err = got_path_basename(&tpd->entry_name, pe->path);
2696 if (err) {
2697 free(tpd->relpath);
2698 free(tpd->tree_id);
2699 free(tpd);
2700 goto done;
2702 } else
2703 tpd->entry_name = NULL;
2705 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2709 * Read the file index.
2710 * Checking out files is supposed to be an idempotent operation.
2711 * If the on-disk file index is incomplete we will try to complete it.
2713 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2714 if (err)
2715 goto done;
2717 tpd = STAILQ_FIRST(&tree_paths);
2718 TAILQ_FOREACH(pe, paths, entry) {
2719 struct bump_base_commit_id_arg bbc_arg;
2721 err = checkout_files(worktree, fileindex, tpd->relpath,
2722 tpd->tree_id, tpd->entry_name, repo,
2723 progress_cb, progress_arg, cancel_cb, cancel_arg);
2724 if (err)
2725 break;
2727 bbc_arg.base_commit_id = worktree->base_commit_id;
2728 bbc_arg.entry_name = tpd->entry_name;
2729 bbc_arg.path = pe->path;
2730 bbc_arg.path_len = pe->path_len;
2731 bbc_arg.progress_cb = progress_cb;
2732 bbc_arg.progress_arg = progress_arg;
2733 err = got_fileindex_for_each_entry_safe(fileindex,
2734 bump_base_commit_id, &bbc_arg);
2735 if (err)
2736 break;
2738 tpd = STAILQ_NEXT(tpd, entry);
2740 sync_err = sync_fileindex(fileindex, fileindex_path);
2741 if (sync_err && err == NULL)
2742 err = sync_err;
2743 done:
2744 free(fileindex_path);
2745 if (tree)
2746 got_object_tree_close(tree);
2747 if (commit)
2748 got_object_commit_close(commit);
2749 if (fileindex)
2750 got_fileindex_free(fileindex);
2751 while (!STAILQ_EMPTY(&tree_paths)) {
2752 tpd = STAILQ_FIRST(&tree_paths);
2753 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2754 free(tpd->relpath);
2755 free(tpd->tree_id);
2756 free(tpd);
2758 unlockerr = lock_worktree(worktree, LOCK_SH);
2759 if (unlockerr && err == NULL)
2760 err = unlockerr;
2761 return err;
2764 struct merge_file_cb_arg {
2765 struct got_worktree *worktree;
2766 struct got_fileindex *fileindex;
2767 got_worktree_checkout_cb progress_cb;
2768 void *progress_arg;
2769 got_cancel_cb cancel_cb;
2770 void *cancel_arg;
2771 const char *label_orig;
2772 struct got_object_id *commit_id2;
2773 int allow_bad_symlinks;
2776 static const struct got_error *
2777 merge_file_cb(void *arg, struct got_blob_object *blob1,
2778 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2779 struct got_object_id *id1, struct got_object_id *id2,
2780 const char *path1, const char *path2,
2781 mode_t mode1, mode_t mode2, struct got_repository *repo)
2783 static const struct got_error *err = NULL;
2784 struct merge_file_cb_arg *a = arg;
2785 struct got_fileindex_entry *ie;
2786 char *ondisk_path = NULL;
2787 struct stat sb;
2788 unsigned char status;
2789 int local_changes_subsumed;
2790 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2791 char *id_str = NULL, *label_deriv2 = NULL;
2793 if (blob1 && blob2) {
2794 ie = got_fileindex_entry_get(a->fileindex, path2,
2795 strlen(path2));
2796 if (ie == NULL)
2797 return (*a->progress_cb)(a->progress_arg,
2798 GOT_STATUS_MISSING, path2);
2800 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2801 path2) == -1)
2802 return got_error_from_errno("asprintf");
2804 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2805 repo);
2806 if (err)
2807 goto done;
2809 if (status == GOT_STATUS_DELETE) {
2810 err = (*a->progress_cb)(a->progress_arg,
2811 GOT_STATUS_MERGE, path2);
2812 goto done;
2814 if (status != GOT_STATUS_NO_CHANGE &&
2815 status != GOT_STATUS_MODIFY &&
2816 status != GOT_STATUS_CONFLICT &&
2817 status != GOT_STATUS_ADD) {
2818 err = (*a->progress_cb)(a->progress_arg, status, path2);
2819 goto done;
2822 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2823 char *link_target2;
2824 err = got_object_blob_read_to_str(&link_target2, blob2);
2825 if (err)
2826 goto done;
2827 err = merge_symlink(a->worktree, blob1, ondisk_path,
2828 path2, a->label_orig, link_target2, a->commit_id2,
2829 repo, a->progress_cb, a->progress_arg);
2830 free(link_target2);
2831 } else {
2832 int fd;
2834 f_orig = got_opentemp();
2835 if (f_orig == NULL) {
2836 err = got_error_from_errno("got_opentemp");
2837 goto done;
2839 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2840 f_orig, blob1);
2841 if (err)
2842 goto done;
2844 f_deriv2 = got_opentemp();
2845 if (f_deriv2 == NULL)
2846 goto done;
2847 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2848 f_deriv2, blob2);
2849 if (err)
2850 goto done;
2852 fd = open(ondisk_path,
2853 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2854 if (fd == -1) {
2855 err = got_error_from_errno2("open",
2856 ondisk_path);
2857 goto done;
2859 f_deriv = fdopen(fd, "r");
2860 if (f_deriv == NULL) {
2861 err = got_error_from_errno2("fdopen",
2862 ondisk_path);
2863 close(fd);
2864 goto done;
2866 err = got_object_id_str(&id_str, a->commit_id2);
2867 if (err)
2868 goto done;
2869 if (asprintf(&label_deriv2, "%s: commit %s",
2870 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2871 err = got_error_from_errno("asprintf");
2872 goto done;
2874 err = merge_file(&local_changes_subsumed, a->worktree,
2875 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2876 sb.st_mode, a->label_orig, NULL, label_deriv2,
2877 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2878 a->progress_cb, a->progress_arg);
2880 } else if (blob1) {
2881 ie = got_fileindex_entry_get(a->fileindex, path1,
2882 strlen(path1));
2883 if (ie == NULL)
2884 return (*a->progress_cb)(a->progress_arg,
2885 GOT_STATUS_MISSING, path1);
2887 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 path1) == -1)
2889 return got_error_from_errno("asprintf");
2891 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2892 repo);
2893 if (err)
2894 goto done;
2896 switch (status) {
2897 case GOT_STATUS_NO_CHANGE:
2898 err = (*a->progress_cb)(a->progress_arg,
2899 GOT_STATUS_DELETE, path1);
2900 if (err)
2901 goto done;
2902 err = remove_ondisk_file(a->worktree->root_path, path1);
2903 if (err)
2904 goto done;
2905 if (ie)
2906 got_fileindex_entry_mark_deleted_from_disk(ie);
2907 break;
2908 case GOT_STATUS_DELETE:
2909 case GOT_STATUS_MISSING:
2910 err = (*a->progress_cb)(a->progress_arg,
2911 GOT_STATUS_DELETE, path1);
2912 if (err)
2913 goto done;
2914 if (ie)
2915 got_fileindex_entry_mark_deleted_from_disk(ie);
2916 break;
2917 case GOT_STATUS_ADD: {
2918 struct got_object_id *id;
2919 FILE *blob1_f;
2920 off_t blob1_size;
2922 * Delete the added file only if its content already
2923 * exists in the repository.
2925 err = got_object_blob_file_create(&id, &blob1_f,
2926 &blob1_size, path1);
2927 if (err)
2928 goto done;
2929 if (got_object_id_cmp(id, id1) == 0) {
2930 err = (*a->progress_cb)(a->progress_arg,
2931 GOT_STATUS_DELETE, path1);
2932 if (err)
2933 goto done;
2934 err = remove_ondisk_file(a->worktree->root_path,
2935 path1);
2936 if (err)
2937 goto done;
2938 if (ie)
2939 got_fileindex_entry_remove(a->fileindex,
2940 ie);
2941 } else {
2942 err = (*a->progress_cb)(a->progress_arg,
2943 GOT_STATUS_CANNOT_DELETE, path1);
2945 if (fclose(blob1_f) == EOF && err == NULL)
2946 err = got_error_from_errno("fclose");
2947 free(id);
2948 if (err)
2949 goto done;
2950 break;
2952 case GOT_STATUS_MODIFY:
2953 case GOT_STATUS_CONFLICT:
2954 err = (*a->progress_cb)(a->progress_arg,
2955 GOT_STATUS_CANNOT_DELETE, path1);
2956 if (err)
2957 goto done;
2958 break;
2959 case GOT_STATUS_OBSTRUCTED:
2960 err = (*a->progress_cb)(a->progress_arg, status, path1);
2961 if (err)
2962 goto done;
2963 break;
2964 default:
2965 break;
2967 } else if (blob2) {
2968 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2969 path2) == -1)
2970 return got_error_from_errno("asprintf");
2971 ie = got_fileindex_entry_get(a->fileindex, path2,
2972 strlen(path2));
2973 if (ie) {
2974 err = get_file_status(&status, &sb, ie, ondisk_path,
2975 -1, NULL, repo);
2976 if (err)
2977 goto done;
2978 if (status != GOT_STATUS_NO_CHANGE &&
2979 status != GOT_STATUS_MODIFY &&
2980 status != GOT_STATUS_CONFLICT &&
2981 status != GOT_STATUS_ADD) {
2982 err = (*a->progress_cb)(a->progress_arg,
2983 status, path2);
2984 goto done;
2986 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2987 char *link_target2;
2988 err = got_object_blob_read_to_str(&link_target2,
2989 blob2);
2990 if (err)
2991 goto done;
2992 err = merge_symlink(a->worktree, NULL,
2993 ondisk_path, path2, a->label_orig,
2994 link_target2, a->commit_id2, repo,
2995 a->progress_cb, a->progress_arg);
2996 free(link_target2);
2997 } else if (S_ISREG(sb.st_mode)) {
2998 err = merge_blob(&local_changes_subsumed,
2999 a->worktree, NULL, ondisk_path, path2,
3000 sb.st_mode, a->label_orig, blob2,
3001 a->commit_id2, repo, a->progress_cb,
3002 a->progress_arg);
3003 } else {
3004 err = got_error_path(ondisk_path,
3005 GOT_ERR_FILE_OBSTRUCTED);
3007 if (err)
3008 goto done;
3009 if (status == GOT_STATUS_DELETE) {
3010 err = got_fileindex_entry_update(ie,
3011 a->worktree->root_fd, path2, blob2->id.sha1,
3012 a->worktree->base_commit_id->sha1, 0);
3013 if (err)
3014 goto done;
3016 } else {
3017 int is_bad_symlink = 0;
3018 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3019 if (S_ISLNK(mode2)) {
3020 err = install_symlink(&is_bad_symlink,
3021 a->worktree, ondisk_path, path2, blob2, 0,
3022 0, 1, a->allow_bad_symlinks, repo,
3023 a->progress_cb, a->progress_arg);
3024 } else {
3025 err = install_blob(a->worktree, ondisk_path, path2,
3026 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3027 a->progress_cb, a->progress_arg);
3029 if (err)
3030 goto done;
3031 err = got_fileindex_entry_alloc(&ie, path2);
3032 if (err)
3033 goto done;
3034 err = got_fileindex_entry_update(ie,
3035 a->worktree->root_fd, path2, NULL, NULL, 1);
3036 if (err) {
3037 got_fileindex_entry_free(ie);
3038 goto done;
3040 err = got_fileindex_entry_add(a->fileindex, ie);
3041 if (err) {
3042 got_fileindex_entry_free(ie);
3043 goto done;
3045 if (is_bad_symlink) {
3046 got_fileindex_entry_filetype_set(ie,
3047 GOT_FILEIDX_MODE_BAD_SYMLINK);
3051 done:
3052 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3053 err = got_error_from_errno("fclose");
3054 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3055 err = got_error_from_errno("fclose");
3056 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3057 err = got_error_from_errno("fclose");
3058 free(id_str);
3059 free(label_deriv2);
3060 free(ondisk_path);
3061 return err;
3064 static const struct got_error *
3065 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3067 struct got_worktree *worktree = arg;
3069 /* Reject merges into a work tree with mixed base commits. */
3070 if (got_fileindex_entry_has_commit(ie) &&
3071 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3072 SHA1_DIGEST_LENGTH) != 0)
3073 return got_error(GOT_ERR_MIXED_COMMITS);
3075 return NULL;
3078 struct check_merge_conflicts_arg {
3079 struct got_worktree *worktree;
3080 struct got_fileindex *fileindex;
3081 struct got_repository *repo;
3084 static const struct got_error *
3085 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3086 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3087 struct got_object_id *id1, struct got_object_id *id2,
3088 const char *path1, const char *path2,
3089 mode_t mode1, mode_t mode2, struct got_repository *repo)
3091 const struct got_error *err = NULL;
3092 struct check_merge_conflicts_arg *a = arg;
3093 unsigned char status;
3094 struct stat sb;
3095 struct got_fileindex_entry *ie;
3096 const char *path = path2 ? path2 : path1;
3097 struct got_object_id *id = id2 ? id2 : id1;
3098 char *ondisk_path;
3100 if (id == NULL)
3101 return NULL;
3103 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3104 if (ie == NULL)
3105 return NULL;
3107 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3108 == -1)
3109 return got_error_from_errno("asprintf");
3111 /* Reject merges into a work tree with conflicted files. */
3112 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3113 free(ondisk_path);
3114 if (err)
3115 return err;
3116 if (status == GOT_STATUS_CONFLICT)
3117 return got_error(GOT_ERR_CONFLICTS);
3119 return NULL;
3122 static const struct got_error *
3123 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3124 const char *fileindex_path, struct got_object_id *commit_id1,
3125 struct got_object_id *commit_id2, struct got_repository *repo,
3126 got_worktree_checkout_cb progress_cb, void *progress_arg,
3127 got_cancel_cb cancel_cb, void *cancel_arg)
3129 const struct got_error *err = NULL, *sync_err;
3130 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3131 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3132 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3133 struct check_merge_conflicts_arg cmc_arg;
3134 struct merge_file_cb_arg arg;
3135 char *label_orig = NULL;
3136 FILE *f1 = NULL, *f2 = NULL;
3137 int fd1 = -1, fd2 = -1;
3139 if (commit_id1) {
3140 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3141 if (err)
3142 goto done;
3143 err = got_object_id_by_path(&tree_id1, repo, commit1,
3144 worktree->path_prefix);
3145 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3146 goto done;
3148 if (tree_id1) {
3149 char *id_str;
3151 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3152 if (err)
3153 goto done;
3155 err = got_object_id_str(&id_str, commit_id1);
3156 if (err)
3157 goto done;
3159 if (asprintf(&label_orig, "%s: commit %s",
3160 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3161 err = got_error_from_errno("asprintf");
3162 free(id_str);
3163 goto done;
3165 free(id_str);
3167 f1 = got_opentemp();
3168 if (f1 == NULL) {
3169 err = got_error_from_errno("got_opentemp");
3170 goto done;
3174 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3175 if (err)
3176 goto done;
3178 err = got_object_id_by_path(&tree_id2, repo, commit2,
3179 worktree->path_prefix);
3180 if (err)
3181 goto done;
3183 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3184 if (err)
3185 goto done;
3187 f2 = got_opentemp();
3188 if (f2 == NULL) {
3189 err = got_error_from_errno("got_opentemp");
3190 goto done;
3193 fd1 = got_opentempfd();
3194 if (fd1 == -1) {
3195 err = got_error_from_errno("got_opentempfd");
3196 goto done;
3199 fd2 = got_opentempfd();
3200 if (fd2 == -1) {
3201 err = got_error_from_errno("got_opentempfd");
3202 goto done;
3205 cmc_arg.worktree = worktree;
3206 cmc_arg.fileindex = fileindex;
3207 cmc_arg.repo = repo;
3208 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3209 check_merge_conflicts, &cmc_arg, 0);
3210 if (err)
3211 goto done;
3213 arg.worktree = worktree;
3214 arg.fileindex = fileindex;
3215 arg.progress_cb = progress_cb;
3216 arg.progress_arg = progress_arg;
3217 arg.cancel_cb = cancel_cb;
3218 arg.cancel_arg = cancel_arg;
3219 arg.label_orig = label_orig;
3220 arg.commit_id2 = commit_id2;
3221 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3222 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3223 merge_file_cb, &arg, 1);
3224 sync_err = sync_fileindex(fileindex, fileindex_path);
3225 if (sync_err && err == NULL)
3226 err = sync_err;
3227 done:
3228 if (commit1)
3229 got_object_commit_close(commit1);
3230 if (commit2)
3231 got_object_commit_close(commit2);
3232 if (tree1)
3233 got_object_tree_close(tree1);
3234 if (tree2)
3235 got_object_tree_close(tree2);
3236 if (f1 && fclose(f1) == EOF && err == NULL)
3237 err = got_error_from_errno("fclose");
3238 if (f2 && fclose(f2) == EOF && err == NULL)
3239 err = got_error_from_errno("fclose");
3240 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3241 err = got_error_from_errno("close");
3242 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3243 err = got_error_from_errno("close");
3244 free(label_orig);
3245 return err;
3248 const struct got_error *
3249 got_worktree_merge_files(struct got_worktree *worktree,
3250 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3251 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3252 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3254 const struct got_error *err, *unlockerr;
3255 char *fileindex_path = NULL;
3256 struct got_fileindex *fileindex = NULL;
3258 err = lock_worktree(worktree, LOCK_EX);
3259 if (err)
3260 return err;
3262 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3263 if (err)
3264 goto done;
3266 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3267 worktree);
3268 if (err)
3269 goto done;
3271 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3272 commit_id2, repo, progress_cb, progress_arg,
3273 cancel_cb, cancel_arg);
3274 done:
3275 if (fileindex)
3276 got_fileindex_free(fileindex);
3277 free(fileindex_path);
3278 unlockerr = lock_worktree(worktree, LOCK_SH);
3279 if (unlockerr && err == NULL)
3280 err = unlockerr;
3281 return err;
3284 struct diff_dir_cb_arg {
3285 struct got_fileindex *fileindex;
3286 struct got_worktree *worktree;
3287 const char *status_path;
3288 size_t status_path_len;
3289 struct got_repository *repo;
3290 got_worktree_status_cb status_cb;
3291 void *status_arg;
3292 got_cancel_cb cancel_cb;
3293 void *cancel_arg;
3294 /* A pathlist containing per-directory pathlists of ignore patterns. */
3295 struct got_pathlist_head *ignores;
3296 int report_unchanged;
3297 int no_ignores;
3300 static const struct got_error *
3301 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3302 int dirfd, const char *de_name,
3303 got_worktree_status_cb status_cb, void *status_arg,
3304 struct got_repository *repo, int report_unchanged)
3306 const struct got_error *err = NULL;
3307 unsigned char status = GOT_STATUS_NO_CHANGE;
3308 unsigned char staged_status = get_staged_status(ie);
3309 struct stat sb;
3310 struct got_object_id blob_id, commit_id, staged_blob_id;
3311 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3312 struct got_object_id *staged_blob_idp = NULL;
3314 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3315 if (err)
3316 return err;
3318 if (status == GOT_STATUS_NO_CHANGE &&
3319 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3320 return NULL;
3322 if (got_fileindex_entry_has_blob(ie)) {
3323 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3324 blob_idp = &blob_id;
3326 if (got_fileindex_entry_has_commit(ie)) {
3327 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3328 commit_idp = &commit_id;
3330 if (staged_status == GOT_STATUS_ADD ||
3331 staged_status == GOT_STATUS_MODIFY) {
3332 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3333 SHA1_DIGEST_LENGTH);
3334 staged_blob_idp = &staged_blob_id;
3337 return (*status_cb)(status_arg, status, staged_status,
3338 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3341 static const struct got_error *
3342 status_old_new(void *arg, struct got_fileindex_entry *ie,
3343 struct dirent *de, const char *parent_path, int dirfd)
3345 const struct got_error *err = NULL;
3346 struct diff_dir_cb_arg *a = arg;
3347 char *abspath;
3349 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3350 return got_error(GOT_ERR_CANCELLED);
3352 if (got_path_cmp(parent_path, a->status_path,
3353 strlen(parent_path), a->status_path_len) != 0 &&
3354 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3355 return NULL;
3357 if (parent_path[0]) {
3358 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3359 parent_path, de->d_name) == -1)
3360 return got_error_from_errno("asprintf");
3361 } else {
3362 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3363 de->d_name) == -1)
3364 return got_error_from_errno("asprintf");
3367 err = report_file_status(ie, abspath, dirfd, de->d_name,
3368 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3369 free(abspath);
3370 return err;
3373 static const struct got_error *
3374 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3376 struct diff_dir_cb_arg *a = arg;
3377 struct got_object_id blob_id, commit_id;
3378 unsigned char status;
3380 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3381 return got_error(GOT_ERR_CANCELLED);
3383 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3384 return NULL;
3386 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3387 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3388 if (got_fileindex_entry_has_file_on_disk(ie))
3389 status = GOT_STATUS_MISSING;
3390 else
3391 status = GOT_STATUS_DELETE;
3392 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3393 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3396 static void
3397 free_ignorelist(struct got_pathlist_head *ignorelist)
3399 struct got_pathlist_entry *pe;
3401 TAILQ_FOREACH(pe, ignorelist, entry)
3402 free((char *)pe->path);
3403 got_pathlist_free(ignorelist);
3406 static void
3407 free_ignores(struct got_pathlist_head *ignores)
3409 struct got_pathlist_entry *pe;
3411 TAILQ_FOREACH(pe, ignores, entry) {
3412 struct got_pathlist_head *ignorelist = pe->data;
3413 free_ignorelist(ignorelist);
3414 free((char *)pe->path);
3416 got_pathlist_free(ignores);
3419 static const struct got_error *
3420 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3422 const struct got_error *err = NULL;
3423 struct got_pathlist_entry *pe = NULL;
3424 struct got_pathlist_head *ignorelist;
3425 char *line = NULL, *pattern, *dirpath = NULL;
3426 size_t linesize = 0;
3427 ssize_t linelen;
3429 ignorelist = calloc(1, sizeof(*ignorelist));
3430 if (ignorelist == NULL)
3431 return got_error_from_errno("calloc");
3432 TAILQ_INIT(ignorelist);
3434 while ((linelen = getline(&line, &linesize, f)) != -1) {
3435 if (linelen > 0 && line[linelen - 1] == '\n')
3436 line[linelen - 1] = '\0';
3438 /* Git's ignores may contain comments. */
3439 if (line[0] == '#')
3440 continue;
3442 /* Git's negated patterns are not (yet?) supported. */
3443 if (line[0] == '!')
3444 continue;
3446 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3447 line) == -1) {
3448 err = got_error_from_errno("asprintf");
3449 goto done;
3451 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3452 if (err)
3453 goto done;
3455 if (ferror(f)) {
3456 err = got_error_from_errno("getline");
3457 goto done;
3460 dirpath = strdup(path);
3461 if (dirpath == NULL) {
3462 err = got_error_from_errno("strdup");
3463 goto done;
3465 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3466 done:
3467 free(line);
3468 if (err || pe == NULL) {
3469 free(dirpath);
3470 free_ignorelist(ignorelist);
3472 return err;
3475 static int
3476 match_ignores(struct got_pathlist_head *ignores, const char *path)
3478 struct got_pathlist_entry *pe;
3480 /* Handle patterns which match in all directories. */
3481 TAILQ_FOREACH(pe, ignores, entry) {
3482 struct got_pathlist_head *ignorelist = pe->data;
3483 struct got_pathlist_entry *pi;
3485 TAILQ_FOREACH(pi, ignorelist, entry) {
3486 const char *p, *pattern = pi->path;
3488 if (strncmp(pattern, "**/", 3) != 0)
3489 continue;
3490 pattern += 3;
3491 p = path;
3492 while (*p) {
3493 if (fnmatch(pattern, p,
3494 FNM_PATHNAME | FNM_LEADING_DIR)) {
3495 /* Retry in next directory. */
3496 while (*p && *p != '/')
3497 p++;
3498 while (*p == '/')
3499 p++;
3500 continue;
3502 return 1;
3508 * The ignores pathlist contains ignore lists from children before
3509 * parents, so we can find the most specific ignorelist by walking
3510 * ignores backwards.
3512 pe = TAILQ_LAST(ignores, got_pathlist_head);
3513 while (pe) {
3514 if (got_path_is_child(path, pe->path, pe->path_len)) {
3515 struct got_pathlist_head *ignorelist = pe->data;
3516 struct got_pathlist_entry *pi;
3517 TAILQ_FOREACH(pi, ignorelist, entry) {
3518 const char *pattern = pi->path;
3519 int flags = FNM_LEADING_DIR;
3520 if (strstr(pattern, "/**/") == NULL)
3521 flags |= FNM_PATHNAME;
3522 if (fnmatch(pattern, path, flags))
3523 continue;
3524 return 1;
3527 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3530 return 0;
3533 static const struct got_error *
3534 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3535 const char *path, int dirfd, const char *ignores_filename)
3537 const struct got_error *err = NULL;
3538 char *ignorespath;
3539 int fd = -1;
3540 FILE *ignoresfile = NULL;
3542 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3543 path[0] ? "/" : "", ignores_filename) == -1)
3544 return got_error_from_errno("asprintf");
3546 if (dirfd != -1) {
3547 fd = openat(dirfd, ignores_filename,
3548 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3549 if (fd == -1) {
3550 if (errno != ENOENT && errno != EACCES)
3551 err = got_error_from_errno2("openat",
3552 ignorespath);
3553 } else {
3554 ignoresfile = fdopen(fd, "r");
3555 if (ignoresfile == NULL)
3556 err = got_error_from_errno2("fdopen",
3557 ignorespath);
3558 else {
3559 fd = -1;
3560 err = read_ignores(ignores, path, ignoresfile);
3563 } else {
3564 ignoresfile = fopen(ignorespath, "re");
3565 if (ignoresfile == NULL) {
3566 if (errno != ENOENT && errno != EACCES)
3567 err = got_error_from_errno2("fopen",
3568 ignorespath);
3569 } else
3570 err = read_ignores(ignores, path, ignoresfile);
3573 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3574 err = got_error_from_errno2("fclose", path);
3575 if (fd != -1 && close(fd) == -1 && err == NULL)
3576 err = got_error_from_errno2("close", path);
3577 free(ignorespath);
3578 return err;
3581 static const struct got_error *
3582 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3583 int dirfd)
3585 const struct got_error *err = NULL;
3586 struct diff_dir_cb_arg *a = arg;
3587 char *path = NULL;
3589 if (ignore != NULL)
3590 *ignore = 0;
3592 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3593 return got_error(GOT_ERR_CANCELLED);
3595 if (parent_path[0]) {
3596 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3597 return got_error_from_errno("asprintf");
3598 } else {
3599 path = de->d_name;
3602 if (de->d_type == DT_DIR) {
3603 if (!a->no_ignores && ignore != NULL &&
3604 match_ignores(a->ignores, path))
3605 *ignore = 1;
3606 } else if (!match_ignores(a->ignores, path) &&
3607 got_path_is_child(path, a->status_path, a->status_path_len))
3608 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3609 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3610 if (parent_path[0])
3611 free(path);
3612 return err;
3615 static const struct got_error *
3616 status_traverse(void *arg, const char *path, int dirfd)
3618 const struct got_error *err = NULL;
3619 struct diff_dir_cb_arg *a = arg;
3621 if (a->no_ignores)
3622 return NULL;
3624 err = add_ignores(a->ignores, a->worktree->root_path,
3625 path, dirfd, ".cvsignore");
3626 if (err)
3627 return err;
3629 err = add_ignores(a->ignores, a->worktree->root_path, path,
3630 dirfd, ".gitignore");
3632 return err;
3635 static const struct got_error *
3636 report_single_file_status(const char *path, const char *ondisk_path,
3637 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3638 void *status_arg, struct got_repository *repo, int report_unchanged,
3639 struct got_pathlist_head *ignores, int no_ignores)
3641 struct got_fileindex_entry *ie;
3642 struct stat sb;
3644 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3645 if (ie)
3646 return report_file_status(ie, ondisk_path, -1, NULL,
3647 status_cb, status_arg, repo, report_unchanged);
3649 if (lstat(ondisk_path, &sb) == -1) {
3650 if (errno != ENOENT)
3651 return got_error_from_errno2("lstat", ondisk_path);
3652 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3653 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3656 if (!no_ignores && match_ignores(ignores, path))
3657 return NULL;
3659 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3660 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3661 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3663 return NULL;
3666 static const struct got_error *
3667 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3668 const char *root_path, const char *path)
3670 const struct got_error *err;
3671 char *parent_path, *next_parent_path = NULL;
3673 err = add_ignores(ignores, root_path, "", -1,
3674 ".cvsignore");
3675 if (err)
3676 return err;
3678 err = add_ignores(ignores, root_path, "", -1,
3679 ".gitignore");
3680 if (err)
3681 return err;
3683 err = got_path_dirname(&parent_path, path);
3684 if (err) {
3685 if (err->code == GOT_ERR_BAD_PATH)
3686 return NULL; /* cannot traverse parent */
3687 return err;
3689 for (;;) {
3690 err = add_ignores(ignores, root_path, parent_path, -1,
3691 ".cvsignore");
3692 if (err)
3693 break;
3694 err = add_ignores(ignores, root_path, parent_path, -1,
3695 ".gitignore");
3696 if (err)
3697 break;
3698 err = got_path_dirname(&next_parent_path, parent_path);
3699 if (err) {
3700 if (err->code == GOT_ERR_BAD_PATH)
3701 err = NULL; /* traversed everything */
3702 break;
3704 if (got_path_is_root_dir(parent_path))
3705 break;
3706 free(parent_path);
3707 parent_path = next_parent_path;
3708 next_parent_path = NULL;
3711 free(parent_path);
3712 free(next_parent_path);
3713 return err;
3716 static const struct got_error *
3717 worktree_status(struct got_worktree *worktree, const char *path,
3718 struct got_fileindex *fileindex, struct got_repository *repo,
3719 got_worktree_status_cb status_cb, void *status_arg,
3720 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3721 int report_unchanged)
3723 const struct got_error *err = NULL;
3724 int fd = -1;
3725 struct got_fileindex_diff_dir_cb fdiff_cb;
3726 struct diff_dir_cb_arg arg;
3727 char *ondisk_path = NULL;
3728 struct got_pathlist_head ignores;
3729 struct got_fileindex_entry *ie;
3731 TAILQ_INIT(&ignores);
3733 if (asprintf(&ondisk_path, "%s%s%s",
3734 worktree->root_path, path[0] ? "/" : "", path) == -1)
3735 return got_error_from_errno("asprintf");
3737 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3738 if (ie) {
3739 err = report_single_file_status(path, ondisk_path,
3740 fileindex, status_cb, status_arg, repo,
3741 report_unchanged, &ignores, no_ignores);
3742 goto done;
3745 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3746 if (fd == -1) {
3747 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3748 !got_err_open_nofollow_on_symlink())
3749 err = got_error_from_errno2("open", ondisk_path);
3750 else {
3751 if (!no_ignores) {
3752 err = add_ignores_from_parent_paths(&ignores,
3753 worktree->root_path, ondisk_path);
3754 if (err)
3755 goto done;
3757 err = report_single_file_status(path, ondisk_path,
3758 fileindex, status_cb, status_arg, repo,
3759 report_unchanged, &ignores, no_ignores);
3761 } else {
3762 fdiff_cb.diff_old_new = status_old_new;
3763 fdiff_cb.diff_old = status_old;
3764 fdiff_cb.diff_new = status_new;
3765 fdiff_cb.diff_traverse = status_traverse;
3766 arg.fileindex = fileindex;
3767 arg.worktree = worktree;
3768 arg.status_path = path;
3769 arg.status_path_len = strlen(path);
3770 arg.repo = repo;
3771 arg.status_cb = status_cb;
3772 arg.status_arg = status_arg;
3773 arg.cancel_cb = cancel_cb;
3774 arg.cancel_arg = cancel_arg;
3775 arg.report_unchanged = report_unchanged;
3776 arg.no_ignores = no_ignores;
3777 if (!no_ignores) {
3778 err = add_ignores_from_parent_paths(&ignores,
3779 worktree->root_path, path);
3780 if (err)
3781 goto done;
3783 arg.ignores = &ignores;
3784 err = got_fileindex_diff_dir(fileindex, fd,
3785 worktree->root_path, path, repo, &fdiff_cb, &arg);
3787 done:
3788 free_ignores(&ignores);
3789 if (fd != -1 && close(fd) == -1 && err == NULL)
3790 err = got_error_from_errno("close");
3791 free(ondisk_path);
3792 return err;
3795 const struct got_error *
3796 got_worktree_status(struct got_worktree *worktree,
3797 struct got_pathlist_head *paths, struct got_repository *repo,
3798 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3799 got_cancel_cb cancel_cb, void *cancel_arg)
3801 const struct got_error *err = NULL;
3802 char *fileindex_path = NULL;
3803 struct got_fileindex *fileindex = NULL;
3804 struct got_pathlist_entry *pe;
3806 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3807 if (err)
3808 return err;
3810 TAILQ_FOREACH(pe, paths, entry) {
3811 err = worktree_status(worktree, pe->path, fileindex, repo,
3812 status_cb, status_arg, cancel_cb, cancel_arg,
3813 no_ignores, 0);
3814 if (err)
3815 break;
3817 free(fileindex_path);
3818 got_fileindex_free(fileindex);
3819 return err;
3822 const struct got_error *
3823 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3824 const char *arg)
3826 const struct got_error *err = NULL;
3827 char *resolved = NULL, *cwd = NULL, *path = NULL;
3828 size_t len;
3829 struct stat sb;
3830 char *abspath = NULL;
3831 char canonpath[PATH_MAX];
3833 *wt_path = NULL;
3835 cwd = getcwd(NULL, 0);
3836 if (cwd == NULL)
3837 return got_error_from_errno("getcwd");
3839 if (lstat(arg, &sb) == -1) {
3840 if (errno != ENOENT) {
3841 err = got_error_from_errno2("lstat", arg);
3842 goto done;
3844 sb.st_mode = 0;
3846 if (S_ISLNK(sb.st_mode)) {
3848 * We cannot use realpath(3) with symlinks since we want to
3849 * operate on the symlink itself.
3850 * But we can make the path absolute, assuming it is relative
3851 * to the current working directory, and then canonicalize it.
3853 if (!got_path_is_absolute(arg)) {
3854 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3855 err = got_error_from_errno("asprintf");
3856 goto done;
3860 err = got_canonpath(abspath ? abspath : arg, canonpath,
3861 sizeof(canonpath));
3862 if (err)
3863 goto done;
3864 resolved = strdup(canonpath);
3865 if (resolved == NULL) {
3866 err = got_error_from_errno("strdup");
3867 goto done;
3869 } else {
3870 resolved = realpath(arg, NULL);
3871 if (resolved == NULL) {
3872 if (errno != ENOENT) {
3873 err = got_error_from_errno2("realpath", arg);
3874 goto done;
3876 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3877 err = got_error_from_errno("asprintf");
3878 goto done;
3880 err = got_canonpath(abspath, canonpath,
3881 sizeof(canonpath));
3882 if (err)
3883 goto done;
3884 resolved = strdup(canonpath);
3885 if (resolved == NULL) {
3886 err = got_error_from_errno("strdup");
3887 goto done;
3892 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3893 strlen(got_worktree_get_root_path(worktree)))) {
3894 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3895 goto done;
3898 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3899 err = got_path_skip_common_ancestor(&path,
3900 got_worktree_get_root_path(worktree), resolved);
3901 if (err)
3902 goto done;
3903 } else {
3904 path = strdup("");
3905 if (path == NULL) {
3906 err = got_error_from_errno("strdup");
3907 goto done;
3911 /* XXX status walk can't deal with trailing slash! */
3912 len = strlen(path);
3913 while (len > 0 && path[len - 1] == '/') {
3914 path[len - 1] = '\0';
3915 len--;
3917 done:
3918 free(abspath);
3919 free(resolved);
3920 free(cwd);
3921 if (err == NULL)
3922 *wt_path = path;
3923 else
3924 free(path);
3925 return err;
3928 struct schedule_addition_args {
3929 struct got_worktree *worktree;
3930 struct got_fileindex *fileindex;
3931 got_worktree_checkout_cb progress_cb;
3932 void *progress_arg;
3933 struct got_repository *repo;
3936 static const struct got_error *
3937 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3938 const char *relpath, struct got_object_id *blob_id,
3939 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3940 int dirfd, const char *de_name)
3942 struct schedule_addition_args *a = arg;
3943 const struct got_error *err = NULL;
3944 struct got_fileindex_entry *ie;
3945 struct stat sb;
3946 char *ondisk_path;
3948 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3949 relpath) == -1)
3950 return got_error_from_errno("asprintf");
3952 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3953 if (ie) {
3954 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3955 de_name, a->repo);
3956 if (err)
3957 goto done;
3958 /* Re-adding an existing entry is a no-op. */
3959 if (status == GOT_STATUS_ADD)
3960 goto done;
3961 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3962 if (err)
3963 goto done;
3966 if (status != GOT_STATUS_UNVERSIONED) {
3967 if (status == GOT_STATUS_NONEXISTENT)
3968 err = got_error_set_errno(ENOENT, ondisk_path);
3969 else
3970 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3971 goto done;
3974 err = got_fileindex_entry_alloc(&ie, relpath);
3975 if (err)
3976 goto done;
3977 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3978 relpath, NULL, NULL, 1);
3979 if (err) {
3980 got_fileindex_entry_free(ie);
3981 goto done;
3983 err = got_fileindex_entry_add(a->fileindex, ie);
3984 if (err) {
3985 got_fileindex_entry_free(ie);
3986 goto done;
3988 done:
3989 free(ondisk_path);
3990 if (err)
3991 return err;
3992 if (status == GOT_STATUS_ADD)
3993 return NULL;
3994 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3997 const struct got_error *
3998 got_worktree_schedule_add(struct got_worktree *worktree,
3999 struct got_pathlist_head *paths,
4000 got_worktree_checkout_cb progress_cb, void *progress_arg,
4001 struct got_repository *repo, int no_ignores)
4003 struct got_fileindex *fileindex = NULL;
4004 char *fileindex_path = NULL;
4005 const struct got_error *err = NULL, *sync_err, *unlockerr;
4006 struct got_pathlist_entry *pe;
4007 struct schedule_addition_args saa;
4009 err = lock_worktree(worktree, LOCK_EX);
4010 if (err)
4011 return err;
4013 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4014 if (err)
4015 goto done;
4017 saa.worktree = worktree;
4018 saa.fileindex = fileindex;
4019 saa.progress_cb = progress_cb;
4020 saa.progress_arg = progress_arg;
4021 saa.repo = repo;
4023 TAILQ_FOREACH(pe, paths, entry) {
4024 err = worktree_status(worktree, pe->path, fileindex, repo,
4025 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4026 if (err)
4027 break;
4029 sync_err = sync_fileindex(fileindex, fileindex_path);
4030 if (sync_err && err == NULL)
4031 err = sync_err;
4032 done:
4033 free(fileindex_path);
4034 if (fileindex)
4035 got_fileindex_free(fileindex);
4036 unlockerr = lock_worktree(worktree, LOCK_SH);
4037 if (unlockerr && err == NULL)
4038 err = unlockerr;
4039 return err;
4042 struct schedule_deletion_args {
4043 struct got_worktree *worktree;
4044 struct got_fileindex *fileindex;
4045 got_worktree_delete_cb progress_cb;
4046 void *progress_arg;
4047 struct got_repository *repo;
4048 int delete_local_mods;
4049 int keep_on_disk;
4050 int ignore_missing_paths;
4051 const char *status_codes;
4054 static const struct got_error *
4055 schedule_for_deletion(void *arg, unsigned char status,
4056 unsigned char staged_status, const char *relpath,
4057 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4058 struct got_object_id *commit_id, int dirfd, const char *de_name)
4060 struct schedule_deletion_args *a = arg;
4061 const struct got_error *err = NULL;
4062 struct got_fileindex_entry *ie = NULL;
4063 struct stat sb;
4064 char *ondisk_path;
4066 if (status == GOT_STATUS_NONEXISTENT) {
4067 if (a->ignore_missing_paths)
4068 return NULL;
4069 return got_error_set_errno(ENOENT, relpath);
4072 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4073 if (ie == NULL)
4074 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4076 staged_status = get_staged_status(ie);
4077 if (staged_status != GOT_STATUS_NO_CHANGE) {
4078 if (staged_status == GOT_STATUS_DELETE)
4079 return NULL;
4080 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4083 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4084 relpath) == -1)
4085 return got_error_from_errno("asprintf");
4087 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4088 a->repo);
4089 if (err)
4090 goto done;
4092 if (a->status_codes) {
4093 size_t ncodes = strlen(a->status_codes);
4094 int i;
4095 for (i = 0; i < ncodes ; i++) {
4096 if (status == a->status_codes[i])
4097 break;
4099 if (i == ncodes) {
4100 /* Do not delete files in non-matching status. */
4101 free(ondisk_path);
4102 return NULL;
4104 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4105 a->status_codes[i] != GOT_STATUS_MISSING) {
4106 static char msg[64];
4107 snprintf(msg, sizeof(msg),
4108 "invalid status code '%c'", a->status_codes[i]);
4109 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4110 goto done;
4114 if (status != GOT_STATUS_NO_CHANGE) {
4115 if (status == GOT_STATUS_DELETE)
4116 goto done;
4117 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4118 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4119 goto done;
4121 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4122 err = got_error_set_errno(ENOENT, relpath);
4123 goto done;
4125 if (status != GOT_STATUS_MODIFY &&
4126 status != GOT_STATUS_MISSING) {
4127 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4128 goto done;
4132 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4133 size_t root_len;
4135 if (dirfd != -1) {
4136 if (unlinkat(dirfd, de_name, 0) != 0) {
4137 err = got_error_from_errno2("unlinkat",
4138 ondisk_path);
4139 goto done;
4141 } else if (unlink(ondisk_path) != 0) {
4142 err = got_error_from_errno2("unlink", ondisk_path);
4143 goto done;
4146 root_len = strlen(a->worktree->root_path);
4147 do {
4148 char *parent;
4149 err = got_path_dirname(&parent, ondisk_path);
4150 if (err)
4151 goto done;
4152 free(ondisk_path);
4153 ondisk_path = parent;
4154 if (rmdir(ondisk_path) == -1) {
4155 if (errno != ENOTEMPTY)
4156 err = got_error_from_errno2("rmdir",
4157 ondisk_path);
4158 break;
4160 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4161 strlen(ondisk_path), root_len) != 0);
4164 got_fileindex_entry_mark_deleted_from_disk(ie);
4165 done:
4166 free(ondisk_path);
4167 if (err)
4168 return err;
4169 if (status == GOT_STATUS_DELETE)
4170 return NULL;
4171 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4172 staged_status, relpath);
4175 const struct got_error *
4176 got_worktree_schedule_delete(struct got_worktree *worktree,
4177 struct got_pathlist_head *paths, int delete_local_mods,
4178 const char *status_codes,
4179 got_worktree_delete_cb progress_cb, void *progress_arg,
4180 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4182 struct got_fileindex *fileindex = NULL;
4183 char *fileindex_path = NULL;
4184 const struct got_error *err = NULL, *sync_err, *unlockerr;
4185 struct got_pathlist_entry *pe;
4186 struct schedule_deletion_args sda;
4188 err = lock_worktree(worktree, LOCK_EX);
4189 if (err)
4190 return err;
4192 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4193 if (err)
4194 goto done;
4196 sda.worktree = worktree;
4197 sda.fileindex = fileindex;
4198 sda.progress_cb = progress_cb;
4199 sda.progress_arg = progress_arg;
4200 sda.repo = repo;
4201 sda.delete_local_mods = delete_local_mods;
4202 sda.keep_on_disk = keep_on_disk;
4203 sda.ignore_missing_paths = ignore_missing_paths;
4204 sda.status_codes = status_codes;
4206 TAILQ_FOREACH(pe, paths, entry) {
4207 err = worktree_status(worktree, pe->path, fileindex, repo,
4208 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4209 if (err)
4210 break;
4212 sync_err = sync_fileindex(fileindex, fileindex_path);
4213 if (sync_err && err == NULL)
4214 err = sync_err;
4215 done:
4216 free(fileindex_path);
4217 if (fileindex)
4218 got_fileindex_free(fileindex);
4219 unlockerr = lock_worktree(worktree, LOCK_SH);
4220 if (unlockerr && err == NULL)
4221 err = unlockerr;
4222 return err;
4225 static const struct got_error *
4226 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4228 const struct got_error *err = NULL;
4229 char *line = NULL;
4230 size_t linesize = 0, n;
4231 ssize_t linelen;
4233 linelen = getline(&line, &linesize, infile);
4234 if (linelen == -1) {
4235 if (ferror(infile)) {
4236 err = got_error_from_errno("getline");
4237 goto done;
4239 return NULL;
4241 if (outfile) {
4242 n = fwrite(line, 1, linelen, outfile);
4243 if (n != linelen) {
4244 err = got_ferror(outfile, GOT_ERR_IO);
4245 goto done;
4248 if (rejectfile) {
4249 n = fwrite(line, 1, linelen, rejectfile);
4250 if (n != linelen)
4251 err = got_ferror(outfile, GOT_ERR_IO);
4253 done:
4254 free(line);
4255 return err;
4258 static const struct got_error *
4259 skip_one_line(FILE *f)
4261 char *line = NULL;
4262 size_t linesize = 0;
4263 ssize_t linelen;
4265 linelen = getline(&line, &linesize, f);
4266 if (linelen == -1) {
4267 if (ferror(f))
4268 return got_error_from_errno("getline");
4269 return NULL;
4271 free(line);
4272 return NULL;
4275 static const struct got_error *
4276 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4277 int start_old, int end_old, int start_new, int end_new,
4278 FILE *outfile, FILE *rejectfile)
4280 const struct got_error *err;
4282 /* Copy old file's lines leading up to patch. */
4283 while (!feof(f1) && *line_cur1 < start_old) {
4284 err = copy_one_line(f1, outfile, NULL);
4285 if (err)
4286 return err;
4287 (*line_cur1)++;
4289 /* Skip new file's lines leading up to patch. */
4290 while (!feof(f2) && *line_cur2 < start_new) {
4291 if (rejectfile)
4292 err = copy_one_line(f2, NULL, rejectfile);
4293 else
4294 err = skip_one_line(f2);
4295 if (err)
4296 return err;
4297 (*line_cur2)++;
4299 /* Copy patched lines. */
4300 while (!feof(f2) && *line_cur2 <= end_new) {
4301 err = copy_one_line(f2, outfile, NULL);
4302 if (err)
4303 return err;
4304 (*line_cur2)++;
4306 /* Skip over old file's replaced lines. */
4307 while (!feof(f1) && *line_cur1 <= end_old) {
4308 if (rejectfile)
4309 err = copy_one_line(f1, NULL, rejectfile);
4310 else
4311 err = skip_one_line(f1);
4312 if (err)
4313 return err;
4314 (*line_cur1)++;
4317 return NULL;
4320 static const struct got_error *
4321 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4322 FILE *outfile, FILE *rejectfile)
4324 const struct got_error *err;
4326 if (outfile) {
4327 /* Copy old file's lines until EOF. */
4328 while (!feof(f1)) {
4329 err = copy_one_line(f1, outfile, NULL);
4330 if (err)
4331 return err;
4332 (*line_cur1)++;
4335 if (rejectfile) {
4336 /* Copy new file's lines until EOF. */
4337 while (!feof(f2)) {
4338 err = copy_one_line(f2, NULL, rejectfile);
4339 if (err)
4340 return err;
4341 (*line_cur2)++;
4345 return NULL;
4348 static const struct got_error *
4349 apply_or_reject_change(int *choice, int *nchunks_used,
4350 struct diff_result *diff_result, int n,
4351 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4352 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4353 got_worktree_patch_cb patch_cb, void *patch_arg)
4355 const struct got_error *err = NULL;
4356 struct diff_chunk_context cc = {};
4357 int start_old, end_old, start_new, end_new;
4358 FILE *hunkfile;
4359 struct diff_output_unidiff_state *diff_state;
4360 struct diff_input_info diff_info;
4361 int rc;
4363 *choice = GOT_PATCH_CHOICE_NONE;
4365 /* Get changed line numbers without context lines for copy_change(). */
4366 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4367 start_old = cc.left.start;
4368 end_old = cc.left.end;
4369 start_new = cc.right.start;
4370 end_new = cc.right.end;
4372 /* Get the same change with context lines for display. */
4373 memset(&cc, 0, sizeof(cc));
4374 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4376 memset(&diff_info, 0, sizeof(diff_info));
4377 diff_info.left_path = relpath;
4378 diff_info.right_path = relpath;
4380 diff_state = diff_output_unidiff_state_alloc();
4381 if (diff_state == NULL)
4382 return got_error_set_errno(ENOMEM,
4383 "diff_output_unidiff_state_alloc");
4385 hunkfile = got_opentemp();
4386 if (hunkfile == NULL) {
4387 err = got_error_from_errno("got_opentemp");
4388 goto done;
4391 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4392 diff_result, &cc);
4393 if (rc != DIFF_RC_OK) {
4394 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4395 goto done;
4398 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4399 err = got_ferror(hunkfile, GOT_ERR_IO);
4400 goto done;
4403 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4404 hunkfile, changeno, nchanges);
4405 if (err)
4406 goto done;
4408 switch (*choice) {
4409 case GOT_PATCH_CHOICE_YES:
4410 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4411 end_old, start_new, end_new, outfile, rejectfile);
4412 break;
4413 case GOT_PATCH_CHOICE_NO:
4414 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4415 end_old, start_new, end_new, rejectfile, outfile);
4416 break;
4417 case GOT_PATCH_CHOICE_QUIT:
4418 break;
4419 default:
4420 err = got_error(GOT_ERR_PATCH_CHOICE);
4421 break;
4423 done:
4424 diff_output_unidiff_state_free(diff_state);
4425 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4426 err = got_error_from_errno("fclose");
4427 return err;
4430 struct revert_file_args {
4431 struct got_worktree *worktree;
4432 struct got_fileindex *fileindex;
4433 got_worktree_checkout_cb progress_cb;
4434 void *progress_arg;
4435 got_worktree_patch_cb patch_cb;
4436 void *patch_arg;
4437 struct got_repository *repo;
4438 int unlink_added_files;
4441 static const struct got_error *
4442 create_patched_content(char **path_outfile, int reverse_patch,
4443 struct got_object_id *blob_id, const char *path2,
4444 int dirfd2, const char *de_name2,
4445 const char *relpath, struct got_repository *repo,
4446 got_worktree_patch_cb patch_cb, void *patch_arg)
4448 const struct got_error *err, *free_err;
4449 struct got_blob_object *blob = NULL;
4450 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4451 int fd = -1, fd2 = -1;
4452 char link_target[PATH_MAX];
4453 ssize_t link_len = 0;
4454 char *path1 = NULL, *id_str = NULL;
4455 struct stat sb2;
4456 struct got_diffreg_result *diffreg_result = NULL;
4457 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4458 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4460 *path_outfile = NULL;
4462 err = got_object_id_str(&id_str, blob_id);
4463 if (err)
4464 return err;
4466 if (dirfd2 != -1) {
4467 fd2 = openat(dirfd2, de_name2,
4468 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4469 if (fd2 == -1) {
4470 if (!got_err_open_nofollow_on_symlink()) {
4471 err = got_error_from_errno2("openat", path2);
4472 goto done;
4474 link_len = readlinkat(dirfd2, de_name2,
4475 link_target, sizeof(link_target));
4476 if (link_len == -1) {
4477 return got_error_from_errno2("readlinkat",
4478 path2);
4480 sb2.st_mode = S_IFLNK;
4481 sb2.st_size = link_len;
4483 } else {
4484 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4485 if (fd2 == -1) {
4486 if (!got_err_open_nofollow_on_symlink()) {
4487 err = got_error_from_errno2("open", path2);
4488 goto done;
4490 link_len = readlink(path2, link_target,
4491 sizeof(link_target));
4492 if (link_len == -1)
4493 return got_error_from_errno2("readlink", path2);
4494 sb2.st_mode = S_IFLNK;
4495 sb2.st_size = link_len;
4498 if (fd2 != -1) {
4499 if (fstat(fd2, &sb2) == -1) {
4500 err = got_error_from_errno2("fstat", path2);
4501 goto done;
4504 f2 = fdopen(fd2, "r");
4505 if (f2 == NULL) {
4506 err = got_error_from_errno2("fdopen", path2);
4507 goto done;
4509 fd2 = -1;
4510 } else {
4511 size_t n;
4512 f2 = got_opentemp();
4513 if (f2 == NULL) {
4514 err = got_error_from_errno2("got_opentemp", path2);
4515 goto done;
4517 n = fwrite(link_target, 1, link_len, f2);
4518 if (n != link_len) {
4519 err = got_ferror(f2, GOT_ERR_IO);
4520 goto done;
4522 if (fflush(f2) == EOF) {
4523 err = got_error_from_errno("fflush");
4524 goto done;
4526 rewind(f2);
4529 fd = got_opentempfd();
4530 if (fd == -1) {
4531 err = got_error_from_errno("got_opentempfd");
4532 goto done;
4535 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4536 if (err)
4537 goto done;
4539 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4540 if (err)
4541 goto done;
4543 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4544 if (err)
4545 goto done;
4547 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4548 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4549 if (err)
4550 goto done;
4552 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4553 if (err)
4554 goto done;
4556 if (fseek(f1, 0L, SEEK_SET) == -1)
4557 return got_ferror(f1, GOT_ERR_IO);
4558 if (fseek(f2, 0L, SEEK_SET) == -1)
4559 return got_ferror(f2, GOT_ERR_IO);
4561 /* Count the number of actual changes in the diff result. */
4562 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4563 struct diff_chunk_context cc = {};
4564 diff_chunk_context_load_change(&cc, &nchunks_used,
4565 diffreg_result->result, n, 0);
4566 nchanges++;
4568 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4569 int choice;
4570 err = apply_or_reject_change(&choice, &nchunks_used,
4571 diffreg_result->result, n, relpath, f1, f2,
4572 &line_cur1, &line_cur2,
4573 reverse_patch ? NULL : outfile,
4574 reverse_patch ? outfile : NULL,
4575 ++i, nchanges, patch_cb, patch_arg);
4576 if (err)
4577 goto done;
4578 if (choice == GOT_PATCH_CHOICE_YES)
4579 have_content = 1;
4580 else if (choice == GOT_PATCH_CHOICE_QUIT)
4581 break;
4583 if (have_content) {
4584 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4585 reverse_patch ? NULL : outfile,
4586 reverse_patch ? outfile : NULL);
4587 if (err)
4588 goto done;
4590 if (!S_ISLNK(sb2.st_mode)) {
4591 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4592 err = got_error_from_errno2("fchmod", path2);
4593 goto done;
4597 done:
4598 free(id_str);
4599 if (fd != -1 && close(fd) == -1 && err == NULL)
4600 err = got_error_from_errno("close");
4601 if (blob)
4602 got_object_blob_close(blob);
4603 free_err = got_diffreg_result_free(diffreg_result);
4604 if (err == NULL)
4605 err = free_err;
4606 if (f1 && fclose(f1) == EOF && err == NULL)
4607 err = got_error_from_errno2("fclose", path1);
4608 if (f2 && fclose(f2) == EOF && err == NULL)
4609 err = got_error_from_errno2("fclose", path2);
4610 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4611 err = got_error_from_errno2("close", path2);
4612 if (outfile && fclose(outfile) == EOF && err == NULL)
4613 err = got_error_from_errno2("fclose", *path_outfile);
4614 if (path1 && unlink(path1) == -1 && err == NULL)
4615 err = got_error_from_errno2("unlink", path1);
4616 if (err || !have_content) {
4617 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4618 err = got_error_from_errno2("unlink", *path_outfile);
4619 free(*path_outfile);
4620 *path_outfile = NULL;
4622 free(path1);
4623 return err;
4626 static const struct got_error *
4627 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4628 const char *relpath, struct got_object_id *blob_id,
4629 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4630 int dirfd, const char *de_name)
4632 struct revert_file_args *a = arg;
4633 const struct got_error *err = NULL;
4634 char *parent_path = NULL;
4635 struct got_fileindex_entry *ie;
4636 struct got_commit_object *base_commit = NULL;
4637 struct got_tree_object *tree = NULL;
4638 struct got_object_id *tree_id = NULL;
4639 const struct got_tree_entry *te = NULL;
4640 char *tree_path = NULL, *te_name;
4641 char *ondisk_path = NULL, *path_content = NULL;
4642 struct got_blob_object *blob = NULL;
4643 int fd = -1;
4645 /* Reverting a staged deletion is a no-op. */
4646 if (status == GOT_STATUS_DELETE &&
4647 staged_status != GOT_STATUS_NO_CHANGE)
4648 return NULL;
4650 if (status == GOT_STATUS_UNVERSIONED)
4651 return (*a->progress_cb)(a->progress_arg,
4652 GOT_STATUS_UNVERSIONED, relpath);
4654 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4655 if (ie == NULL)
4656 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4658 /* Construct in-repository path of tree which contains this blob. */
4659 err = got_path_dirname(&parent_path, ie->path);
4660 if (err) {
4661 if (err->code != GOT_ERR_BAD_PATH)
4662 goto done;
4663 parent_path = strdup("/");
4664 if (parent_path == NULL) {
4665 err = got_error_from_errno("strdup");
4666 goto done;
4669 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4670 tree_path = strdup(parent_path);
4671 if (tree_path == NULL) {
4672 err = got_error_from_errno("strdup");
4673 goto done;
4675 } else {
4676 if (got_path_is_root_dir(parent_path)) {
4677 tree_path = strdup(a->worktree->path_prefix);
4678 if (tree_path == NULL) {
4679 err = got_error_from_errno("strdup");
4680 goto done;
4682 } else {
4683 if (asprintf(&tree_path, "%s/%s",
4684 a->worktree->path_prefix, parent_path) == -1) {
4685 err = got_error_from_errno("asprintf");
4686 goto done;
4691 err = got_object_open_as_commit(&base_commit, a->repo,
4692 a->worktree->base_commit_id);
4693 if (err)
4694 goto done;
4696 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4697 if (err) {
4698 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4699 (status == GOT_STATUS_ADD ||
4700 staged_status == GOT_STATUS_ADD)))
4701 goto done;
4702 } else {
4703 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4704 if (err)
4705 goto done;
4707 err = got_path_basename(&te_name, ie->path);
4708 if (err)
4709 goto done;
4711 te = got_object_tree_find_entry(tree, te_name);
4712 free(te_name);
4713 if (te == NULL && status != GOT_STATUS_ADD &&
4714 staged_status != GOT_STATUS_ADD) {
4715 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4716 goto done;
4720 switch (status) {
4721 case GOT_STATUS_ADD:
4722 if (a->patch_cb) {
4723 int choice = GOT_PATCH_CHOICE_NONE;
4724 err = (*a->patch_cb)(&choice, a->patch_arg,
4725 status, ie->path, NULL, 1, 1);
4726 if (err)
4727 goto done;
4728 if (choice != GOT_PATCH_CHOICE_YES)
4729 break;
4731 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4732 ie->path);
4733 if (err)
4734 goto done;
4735 got_fileindex_entry_remove(a->fileindex, ie);
4736 if (a->unlink_added_files) {
4737 if (asprintf(&ondisk_path, "%s/%s",
4738 got_worktree_get_root_path(a->worktree),
4739 relpath) == -1) {
4740 err = got_error_from_errno("asprintf");
4741 goto done;
4743 if (unlink(ondisk_path) == -1) {
4744 err = got_error_from_errno2("unlink",
4745 ondisk_path);
4746 break;
4749 break;
4750 case GOT_STATUS_DELETE:
4751 if (a->patch_cb) {
4752 int choice = GOT_PATCH_CHOICE_NONE;
4753 err = (*a->patch_cb)(&choice, a->patch_arg,
4754 status, ie->path, NULL, 1, 1);
4755 if (err)
4756 goto done;
4757 if (choice != GOT_PATCH_CHOICE_YES)
4758 break;
4760 /* fall through */
4761 case GOT_STATUS_MODIFY:
4762 case GOT_STATUS_MODE_CHANGE:
4763 case GOT_STATUS_CONFLICT:
4764 case GOT_STATUS_MISSING: {
4765 struct got_object_id id;
4766 if (staged_status == GOT_STATUS_ADD ||
4767 staged_status == GOT_STATUS_MODIFY) {
4768 memcpy(id.sha1, ie->staged_blob_sha1,
4769 SHA1_DIGEST_LENGTH);
4770 } else
4771 memcpy(id.sha1, ie->blob_sha1,
4772 SHA1_DIGEST_LENGTH);
4773 fd = got_opentempfd();
4774 if (fd == -1) {
4775 err = got_error_from_errno("got_opentempfd");
4776 goto done;
4779 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4780 if (err)
4781 goto done;
4783 if (asprintf(&ondisk_path, "%s/%s",
4784 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4785 err = got_error_from_errno("asprintf");
4786 goto done;
4789 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4790 status == GOT_STATUS_CONFLICT)) {
4791 int is_bad_symlink = 0;
4792 err = create_patched_content(&path_content, 1, &id,
4793 ondisk_path, dirfd, de_name, ie->path, a->repo,
4794 a->patch_cb, a->patch_arg);
4795 if (err || path_content == NULL)
4796 break;
4797 if (te && S_ISLNK(te->mode)) {
4798 if (unlink(path_content) == -1) {
4799 err = got_error_from_errno2("unlink",
4800 path_content);
4801 break;
4803 err = install_symlink(&is_bad_symlink,
4804 a->worktree, ondisk_path, ie->path,
4805 blob, 0, 1, 0, 0, a->repo,
4806 a->progress_cb, a->progress_arg);
4807 } else {
4808 if (rename(path_content, ondisk_path) == -1) {
4809 err = got_error_from_errno3("rename",
4810 path_content, ondisk_path);
4811 goto done;
4814 } else {
4815 int is_bad_symlink = 0;
4816 if (te && S_ISLNK(te->mode)) {
4817 err = install_symlink(&is_bad_symlink,
4818 a->worktree, ondisk_path, ie->path,
4819 blob, 0, 1, 0, 0, a->repo,
4820 a->progress_cb, a->progress_arg);
4821 } else {
4822 err = install_blob(a->worktree, ondisk_path,
4823 ie->path,
4824 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4825 got_fileindex_perms_to_st(ie), blob,
4826 0, 1, 0, 0, a->repo,
4827 a->progress_cb, a->progress_arg);
4829 if (err)
4830 goto done;
4831 if (status == GOT_STATUS_DELETE ||
4832 status == GOT_STATUS_MODE_CHANGE) {
4833 err = got_fileindex_entry_update(ie,
4834 a->worktree->root_fd, relpath,
4835 blob->id.sha1,
4836 a->worktree->base_commit_id->sha1, 1);
4837 if (err)
4838 goto done;
4840 if (is_bad_symlink) {
4841 got_fileindex_entry_filetype_set(ie,
4842 GOT_FILEIDX_MODE_BAD_SYMLINK);
4845 break;
4847 default:
4848 break;
4850 done:
4851 free(ondisk_path);
4852 free(path_content);
4853 free(parent_path);
4854 free(tree_path);
4855 if (fd != -1 && close(fd) == -1 && err == NULL)
4856 err = got_error_from_errno("close");
4857 if (blob)
4858 got_object_blob_close(blob);
4859 if (tree)
4860 got_object_tree_close(tree);
4861 free(tree_id);
4862 if (base_commit)
4863 got_object_commit_close(base_commit);
4864 return err;
4867 const struct got_error *
4868 got_worktree_revert(struct got_worktree *worktree,
4869 struct got_pathlist_head *paths,
4870 got_worktree_checkout_cb progress_cb, void *progress_arg,
4871 got_worktree_patch_cb patch_cb, void *patch_arg,
4872 struct got_repository *repo)
4874 struct got_fileindex *fileindex = NULL;
4875 char *fileindex_path = NULL;
4876 const struct got_error *err = NULL, *unlockerr = NULL;
4877 const struct got_error *sync_err = NULL;
4878 struct got_pathlist_entry *pe;
4879 struct revert_file_args rfa;
4881 err = lock_worktree(worktree, LOCK_EX);
4882 if (err)
4883 return err;
4885 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4886 if (err)
4887 goto done;
4889 rfa.worktree = worktree;
4890 rfa.fileindex = fileindex;
4891 rfa.progress_cb = progress_cb;
4892 rfa.progress_arg = progress_arg;
4893 rfa.patch_cb = patch_cb;
4894 rfa.patch_arg = patch_arg;
4895 rfa.repo = repo;
4896 rfa.unlink_added_files = 0;
4897 TAILQ_FOREACH(pe, paths, entry) {
4898 err = worktree_status(worktree, pe->path, fileindex, repo,
4899 revert_file, &rfa, NULL, NULL, 1, 0);
4900 if (err)
4901 break;
4903 sync_err = sync_fileindex(fileindex, fileindex_path);
4904 if (sync_err && err == NULL)
4905 err = sync_err;
4906 done:
4907 free(fileindex_path);
4908 if (fileindex)
4909 got_fileindex_free(fileindex);
4910 unlockerr = lock_worktree(worktree, LOCK_SH);
4911 if (unlockerr && err == NULL)
4912 err = unlockerr;
4913 return err;
4916 static void
4917 free_commitable(struct got_commitable *ct)
4919 free(ct->path);
4920 free(ct->in_repo_path);
4921 free(ct->ondisk_path);
4922 free(ct->blob_id);
4923 free(ct->base_blob_id);
4924 free(ct->staged_blob_id);
4925 free(ct->base_commit_id);
4926 free(ct);
4929 struct collect_commitables_arg {
4930 struct got_pathlist_head *commitable_paths;
4931 struct got_repository *repo;
4932 struct got_worktree *worktree;
4933 struct got_fileindex *fileindex;
4934 int have_staged_files;
4935 int allow_bad_symlinks;
4938 static const struct got_error *
4939 collect_commitables(void *arg, unsigned char status,
4940 unsigned char staged_status, const char *relpath,
4941 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4942 struct got_object_id *commit_id, int dirfd, const char *de_name)
4944 struct collect_commitables_arg *a = arg;
4945 const struct got_error *err = NULL;
4946 struct got_commitable *ct = NULL;
4947 struct got_pathlist_entry *new = NULL;
4948 char *parent_path = NULL, *path = NULL;
4949 struct stat sb;
4951 if (a->have_staged_files) {
4952 if (staged_status != GOT_STATUS_MODIFY &&
4953 staged_status != GOT_STATUS_ADD &&
4954 staged_status != GOT_STATUS_DELETE)
4955 return NULL;
4956 } else {
4957 if (status == GOT_STATUS_CONFLICT)
4958 return got_error(GOT_ERR_COMMIT_CONFLICT);
4960 if (status != GOT_STATUS_MODIFY &&
4961 status != GOT_STATUS_MODE_CHANGE &&
4962 status != GOT_STATUS_ADD &&
4963 status != GOT_STATUS_DELETE)
4964 return NULL;
4967 if (asprintf(&path, "/%s", relpath) == -1) {
4968 err = got_error_from_errno("asprintf");
4969 goto done;
4971 if (strcmp(path, "/") == 0) {
4972 parent_path = strdup("");
4973 if (parent_path == NULL)
4974 return got_error_from_errno("strdup");
4975 } else {
4976 err = got_path_dirname(&parent_path, path);
4977 if (err)
4978 return err;
4981 ct = calloc(1, sizeof(*ct));
4982 if (ct == NULL) {
4983 err = got_error_from_errno("calloc");
4984 goto done;
4987 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4988 relpath) == -1) {
4989 err = got_error_from_errno("asprintf");
4990 goto done;
4993 if (staged_status == GOT_STATUS_ADD ||
4994 staged_status == GOT_STATUS_MODIFY) {
4995 struct got_fileindex_entry *ie;
4996 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4997 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4998 case GOT_FILEIDX_MODE_REGULAR_FILE:
4999 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5000 ct->mode = S_IFREG;
5001 break;
5002 case GOT_FILEIDX_MODE_SYMLINK:
5003 ct->mode = S_IFLNK;
5004 break;
5005 default:
5006 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5007 goto done;
5009 ct->mode |= got_fileindex_entry_perms_get(ie);
5010 } else if (status != GOT_STATUS_DELETE &&
5011 staged_status != GOT_STATUS_DELETE) {
5012 if (dirfd != -1) {
5013 if (fstatat(dirfd, de_name, &sb,
5014 AT_SYMLINK_NOFOLLOW) == -1) {
5015 err = got_error_from_errno2("fstatat",
5016 ct->ondisk_path);
5017 goto done;
5019 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5020 err = got_error_from_errno2("lstat", ct->ondisk_path);
5021 goto done;
5023 ct->mode = sb.st_mode;
5026 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5027 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5028 relpath) == -1) {
5029 err = got_error_from_errno("asprintf");
5030 goto done;
5033 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5034 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5035 int is_bad_symlink;
5036 char target_path[PATH_MAX];
5037 ssize_t target_len;
5038 target_len = readlink(ct->ondisk_path, target_path,
5039 sizeof(target_path));
5040 if (target_len == -1) {
5041 err = got_error_from_errno2("readlink",
5042 ct->ondisk_path);
5043 goto done;
5045 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5046 target_len, ct->ondisk_path, a->worktree->root_path);
5047 if (err)
5048 goto done;
5049 if (is_bad_symlink) {
5050 err = got_error_path(ct->ondisk_path,
5051 GOT_ERR_BAD_SYMLINK);
5052 goto done;
5057 ct->status = status;
5058 ct->staged_status = staged_status;
5059 ct->blob_id = NULL; /* will be filled in when blob gets created */
5060 if (ct->status != GOT_STATUS_ADD &&
5061 ct->staged_status != GOT_STATUS_ADD) {
5062 ct->base_blob_id = got_object_id_dup(blob_id);
5063 if (ct->base_blob_id == NULL) {
5064 err = got_error_from_errno("got_object_id_dup");
5065 goto done;
5067 ct->base_commit_id = got_object_id_dup(commit_id);
5068 if (ct->base_commit_id == NULL) {
5069 err = got_error_from_errno("got_object_id_dup");
5070 goto done;
5073 if (ct->staged_status == GOT_STATUS_ADD ||
5074 ct->staged_status == GOT_STATUS_MODIFY) {
5075 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5076 if (ct->staged_blob_id == NULL) {
5077 err = got_error_from_errno("got_object_id_dup");
5078 goto done;
5081 ct->path = strdup(path);
5082 if (ct->path == NULL) {
5083 err = got_error_from_errno("strdup");
5084 goto done;
5086 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5087 done:
5088 if (ct && (err || new == NULL))
5089 free_commitable(ct);
5090 free(parent_path);
5091 free(path);
5092 return err;
5095 static const struct got_error *write_tree(struct got_object_id **, int *,
5096 struct got_tree_object *, const char *, struct got_pathlist_head *,
5097 got_worktree_status_cb status_cb, void *status_arg,
5098 struct got_repository *);
5100 static const struct got_error *
5101 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5102 struct got_tree_entry *te, const char *parent_path,
5103 struct got_pathlist_head *commitable_paths,
5104 got_worktree_status_cb status_cb, void *status_arg,
5105 struct got_repository *repo)
5107 const struct got_error *err = NULL;
5108 struct got_tree_object *subtree;
5109 char *subpath;
5111 if (asprintf(&subpath, "%s%s%s", parent_path,
5112 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5113 return got_error_from_errno("asprintf");
5115 err = got_object_open_as_tree(&subtree, repo, &te->id);
5116 if (err)
5117 return err;
5119 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5120 commitable_paths, status_cb, status_arg, repo);
5121 got_object_tree_close(subtree);
5122 free(subpath);
5123 return err;
5126 static const struct got_error *
5127 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5129 const struct got_error *err = NULL;
5130 char *ct_parent_path = NULL;
5132 *match = 0;
5134 if (strchr(ct->in_repo_path, '/') == NULL) {
5135 *match = got_path_is_root_dir(path);
5136 return NULL;
5139 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5140 if (err)
5141 return err;
5142 *match = (strcmp(path, ct_parent_path) == 0);
5143 free(ct_parent_path);
5144 return err;
5147 static mode_t
5148 get_ct_file_mode(struct got_commitable *ct)
5150 if (S_ISLNK(ct->mode))
5151 return S_IFLNK;
5153 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5156 static const struct got_error *
5157 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5158 struct got_tree_entry *te, struct got_commitable *ct)
5160 const struct got_error *err = NULL;
5162 *new_te = NULL;
5164 err = got_object_tree_entry_dup(new_te, te);
5165 if (err)
5166 goto done;
5168 (*new_te)->mode = get_ct_file_mode(ct);
5170 if (ct->staged_status == GOT_STATUS_MODIFY)
5171 memcpy(&(*new_te)->id, ct->staged_blob_id,
5172 sizeof((*new_te)->id));
5173 else
5174 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5175 done:
5176 if (err && *new_te) {
5177 free(*new_te);
5178 *new_te = NULL;
5180 return err;
5183 static const struct got_error *
5184 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5185 struct got_commitable *ct)
5187 const struct got_error *err = NULL;
5188 char *ct_name = NULL;
5190 *new_te = NULL;
5192 *new_te = calloc(1, sizeof(**new_te));
5193 if (*new_te == NULL)
5194 return got_error_from_errno("calloc");
5196 err = got_path_basename(&ct_name, ct->path);
5197 if (err)
5198 goto done;
5199 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5200 sizeof((*new_te)->name)) {
5201 err = got_error(GOT_ERR_NO_SPACE);
5202 goto done;
5205 (*new_te)->mode = get_ct_file_mode(ct);
5207 if (ct->staged_status == GOT_STATUS_ADD)
5208 memcpy(&(*new_te)->id, ct->staged_blob_id,
5209 sizeof((*new_te)->id));
5210 else
5211 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5212 done:
5213 free(ct_name);
5214 if (err && *new_te) {
5215 free(*new_te);
5216 *new_te = NULL;
5218 return err;
5221 static const struct got_error *
5222 insert_tree_entry(struct got_tree_entry *new_te,
5223 struct got_pathlist_head *paths)
5225 const struct got_error *err = NULL;
5226 struct got_pathlist_entry *new_pe;
5228 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5229 if (err)
5230 return err;
5231 if (new_pe == NULL)
5232 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5233 return NULL;
5236 static const struct got_error *
5237 report_ct_status(struct got_commitable *ct,
5238 got_worktree_status_cb status_cb, void *status_arg)
5240 const char *ct_path = ct->path;
5241 unsigned char status;
5243 if (status_cb == NULL) /* no commit progress output desired */
5244 return NULL;
5246 while (ct_path[0] == '/')
5247 ct_path++;
5249 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5250 status = ct->staged_status;
5251 else
5252 status = ct->status;
5254 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5255 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5258 static const struct got_error *
5259 match_modified_subtree(int *modified, struct got_tree_entry *te,
5260 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5262 const struct got_error *err = NULL;
5263 struct got_pathlist_entry *pe;
5264 char *te_path;
5266 *modified = 0;
5268 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5269 got_path_is_root_dir(base_tree_path) ? "" : "/",
5270 te->name) == -1)
5271 return got_error_from_errno("asprintf");
5273 TAILQ_FOREACH(pe, commitable_paths, entry) {
5274 struct got_commitable *ct = pe->data;
5275 *modified = got_path_is_child(ct->in_repo_path, te_path,
5276 strlen(te_path));
5277 if (*modified)
5278 break;
5281 free(te_path);
5282 return err;
5285 static const struct got_error *
5286 match_deleted_or_modified_ct(struct got_commitable **ctp,
5287 struct got_tree_entry *te, const char *base_tree_path,
5288 struct got_pathlist_head *commitable_paths)
5290 const struct got_error *err = NULL;
5291 struct got_pathlist_entry *pe;
5293 *ctp = NULL;
5295 TAILQ_FOREACH(pe, commitable_paths, entry) {
5296 struct got_commitable *ct = pe->data;
5297 char *ct_name = NULL;
5298 int path_matches;
5300 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5301 if (ct->status != GOT_STATUS_MODIFY &&
5302 ct->status != GOT_STATUS_MODE_CHANGE &&
5303 ct->status != GOT_STATUS_DELETE)
5304 continue;
5305 } else {
5306 if (ct->staged_status != GOT_STATUS_MODIFY &&
5307 ct->staged_status != GOT_STATUS_DELETE)
5308 continue;
5311 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5312 continue;
5314 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5315 if (err)
5316 return err;
5317 if (!path_matches)
5318 continue;
5320 err = got_path_basename(&ct_name, pe->path);
5321 if (err)
5322 return err;
5324 if (strcmp(te->name, ct_name) != 0) {
5325 free(ct_name);
5326 continue;
5328 free(ct_name);
5330 *ctp = ct;
5331 break;
5334 return err;
5337 static const struct got_error *
5338 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5339 const char *child_path, const char *path_base_tree,
5340 struct got_pathlist_head *commitable_paths,
5341 got_worktree_status_cb status_cb, void *status_arg,
5342 struct got_repository *repo)
5344 const struct got_error *err = NULL;
5345 struct got_tree_entry *new_te;
5346 char *subtree_path;
5347 struct got_object_id *id = NULL;
5348 int nentries;
5350 *new_tep = NULL;
5352 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5353 got_path_is_root_dir(path_base_tree) ? "" : "/",
5354 child_path) == -1)
5355 return got_error_from_errno("asprintf");
5357 new_te = calloc(1, sizeof(*new_te));
5358 if (new_te == NULL)
5359 return got_error_from_errno("calloc");
5360 new_te->mode = S_IFDIR;
5362 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5363 sizeof(new_te->name)) {
5364 err = got_error(GOT_ERR_NO_SPACE);
5365 goto done;
5367 err = write_tree(&id, &nentries, NULL, subtree_path,
5368 commitable_paths, status_cb, status_arg, repo);
5369 if (err) {
5370 free(new_te);
5371 goto done;
5373 memcpy(&new_te->id, id, sizeof(new_te->id));
5374 done:
5375 free(id);
5376 free(subtree_path);
5377 if (err == NULL)
5378 *new_tep = new_te;
5379 return err;
5382 static const struct got_error *
5383 write_tree(struct got_object_id **new_tree_id, int *nentries,
5384 struct got_tree_object *base_tree, const char *path_base_tree,
5385 struct got_pathlist_head *commitable_paths,
5386 got_worktree_status_cb status_cb, void *status_arg,
5387 struct got_repository *repo)
5389 const struct got_error *err = NULL;
5390 struct got_pathlist_head paths;
5391 struct got_tree_entry *te, *new_te = NULL;
5392 struct got_pathlist_entry *pe;
5394 TAILQ_INIT(&paths);
5395 *nentries = 0;
5397 /* Insert, and recurse into, newly added entries first. */
5398 TAILQ_FOREACH(pe, commitable_paths, entry) {
5399 struct got_commitable *ct = pe->data;
5400 char *child_path = NULL, *slash;
5402 if ((ct->status != GOT_STATUS_ADD &&
5403 ct->staged_status != GOT_STATUS_ADD) ||
5404 (ct->flags & GOT_COMMITABLE_ADDED))
5405 continue;
5407 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5408 strlen(path_base_tree)))
5409 continue;
5411 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5412 ct->in_repo_path);
5413 if (err)
5414 goto done;
5416 slash = strchr(child_path, '/');
5417 if (slash == NULL) {
5418 err = alloc_added_blob_tree_entry(&new_te, ct);
5419 if (err)
5420 goto done;
5421 err = report_ct_status(ct, status_cb, status_arg);
5422 if (err)
5423 goto done;
5424 ct->flags |= GOT_COMMITABLE_ADDED;
5425 err = insert_tree_entry(new_te, &paths);
5426 if (err)
5427 goto done;
5428 (*nentries)++;
5429 } else {
5430 *slash = '\0'; /* trim trailing path components */
5431 if (base_tree == NULL ||
5432 got_object_tree_find_entry(base_tree, child_path)
5433 == NULL) {
5434 err = make_subtree_for_added_blob(&new_te,
5435 child_path, path_base_tree,
5436 commitable_paths, status_cb, status_arg,
5437 repo);
5438 if (err)
5439 goto done;
5440 err = insert_tree_entry(new_te, &paths);
5441 if (err)
5442 goto done;
5443 (*nentries)++;
5448 if (base_tree) {
5449 int i, nbase_entries;
5450 /* Handle modified and deleted entries. */
5451 nbase_entries = got_object_tree_get_nentries(base_tree);
5452 for (i = 0; i < nbase_entries; i++) {
5453 struct got_commitable *ct = NULL;
5455 te = got_object_tree_get_entry(base_tree, i);
5456 if (got_object_tree_entry_is_submodule(te)) {
5457 /* Entry is a submodule; just copy it. */
5458 err = got_object_tree_entry_dup(&new_te, te);
5459 if (err)
5460 goto done;
5461 err = insert_tree_entry(new_te, &paths);
5462 if (err)
5463 goto done;
5464 (*nentries)++;
5465 continue;
5468 if (S_ISDIR(te->mode)) {
5469 int modified;
5470 err = got_object_tree_entry_dup(&new_te, te);
5471 if (err)
5472 goto done;
5473 err = match_modified_subtree(&modified, te,
5474 path_base_tree, commitable_paths);
5475 if (err)
5476 goto done;
5477 /* Avoid recursion into unmodified subtrees. */
5478 if (modified) {
5479 struct got_object_id *new_id;
5480 int nsubentries;
5481 err = write_subtree(&new_id,
5482 &nsubentries, te,
5483 path_base_tree, commitable_paths,
5484 status_cb, status_arg, repo);
5485 if (err)
5486 goto done;
5487 if (nsubentries == 0) {
5488 /* All entries were deleted. */
5489 free(new_id);
5490 continue;
5492 memcpy(&new_te->id, new_id,
5493 sizeof(new_te->id));
5494 free(new_id);
5496 err = insert_tree_entry(new_te, &paths);
5497 if (err)
5498 goto done;
5499 (*nentries)++;
5500 continue;
5503 err = match_deleted_or_modified_ct(&ct, te,
5504 path_base_tree, commitable_paths);
5505 if (err)
5506 goto done;
5507 if (ct) {
5508 /* NB: Deleted entries get dropped here. */
5509 if (ct->status == GOT_STATUS_MODIFY ||
5510 ct->status == GOT_STATUS_MODE_CHANGE ||
5511 ct->staged_status == GOT_STATUS_MODIFY) {
5512 err = alloc_modified_blob_tree_entry(
5513 &new_te, te, ct);
5514 if (err)
5515 goto done;
5516 err = insert_tree_entry(new_te, &paths);
5517 if (err)
5518 goto done;
5519 (*nentries)++;
5521 err = report_ct_status(ct, status_cb,
5522 status_arg);
5523 if (err)
5524 goto done;
5525 } else {
5526 /* Entry is unchanged; just copy it. */
5527 err = got_object_tree_entry_dup(&new_te, te);
5528 if (err)
5529 goto done;
5530 err = insert_tree_entry(new_te, &paths);
5531 if (err)
5532 goto done;
5533 (*nentries)++;
5538 /* Write new list of entries; deleted entries have been dropped. */
5539 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5540 done:
5541 got_pathlist_free(&paths);
5542 return err;
5545 static const struct got_error *
5546 update_fileindex_after_commit(struct got_worktree *worktree,
5547 struct got_pathlist_head *commitable_paths,
5548 struct got_object_id *new_base_commit_id,
5549 struct got_fileindex *fileindex, int have_staged_files)
5551 const struct got_error *err = NULL;
5552 struct got_pathlist_entry *pe;
5553 char *relpath = NULL;
5555 TAILQ_FOREACH(pe, commitable_paths, entry) {
5556 struct got_fileindex_entry *ie;
5557 struct got_commitable *ct = pe->data;
5559 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5561 err = got_path_skip_common_ancestor(&relpath,
5562 worktree->root_path, ct->ondisk_path);
5563 if (err)
5564 goto done;
5566 if (ie) {
5567 if (ct->status == GOT_STATUS_DELETE ||
5568 ct->staged_status == GOT_STATUS_DELETE) {
5569 got_fileindex_entry_remove(fileindex, ie);
5570 } else if (ct->staged_status == GOT_STATUS_ADD ||
5571 ct->staged_status == GOT_STATUS_MODIFY) {
5572 got_fileindex_entry_stage_set(ie,
5573 GOT_FILEIDX_STAGE_NONE);
5574 got_fileindex_entry_staged_filetype_set(ie, 0);
5576 err = got_fileindex_entry_update(ie,
5577 worktree->root_fd, relpath,
5578 ct->staged_blob_id->sha1,
5579 new_base_commit_id->sha1,
5580 !have_staged_files);
5581 } else
5582 err = got_fileindex_entry_update(ie,
5583 worktree->root_fd, relpath,
5584 ct->blob_id->sha1,
5585 new_base_commit_id->sha1,
5586 !have_staged_files);
5587 } else {
5588 err = got_fileindex_entry_alloc(&ie, pe->path);
5589 if (err)
5590 goto done;
5591 err = got_fileindex_entry_update(ie,
5592 worktree->root_fd, relpath, ct->blob_id->sha1,
5593 new_base_commit_id->sha1, 1);
5594 if (err) {
5595 got_fileindex_entry_free(ie);
5596 goto done;
5598 err = got_fileindex_entry_add(fileindex, ie);
5599 if (err) {
5600 got_fileindex_entry_free(ie);
5601 goto done;
5604 free(relpath);
5605 relpath = NULL;
5607 done:
5608 free(relpath);
5609 return err;
5613 static const struct got_error *
5614 check_out_of_date(const char *in_repo_path, unsigned char status,
5615 unsigned char staged_status, struct got_object_id *base_blob_id,
5616 struct got_object_id *base_commit_id,
5617 struct got_object_id *head_commit_id, struct got_repository *repo,
5618 int ood_errcode)
5620 const struct got_error *err = NULL;
5621 struct got_commit_object *commit = NULL;
5622 struct got_object_id *id = NULL;
5624 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5625 /* Trivial case: base commit == head commit */
5626 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5627 return NULL;
5629 * Ensure file content which local changes were based
5630 * on matches file content in the branch head.
5632 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5633 if (err)
5634 goto done;
5635 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5636 if (err) {
5637 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5638 err = got_error(ood_errcode);
5639 goto done;
5640 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5641 err = got_error(ood_errcode);
5642 } else {
5643 /* Require that added files don't exist in the branch head. */
5644 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5645 if (err)
5646 goto done;
5647 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5648 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5649 goto done;
5650 err = id ? got_error(ood_errcode) : NULL;
5652 done:
5653 free(id);
5654 if (commit)
5655 got_object_commit_close(commit);
5656 return err;
5659 static const struct got_error *
5660 commit_worktree(struct got_object_id **new_commit_id,
5661 struct got_pathlist_head *commitable_paths,
5662 struct got_object_id *head_commit_id,
5663 struct got_object_id *parent_id2,
5664 struct got_worktree *worktree,
5665 const char *author, const char *committer,
5666 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5667 got_worktree_status_cb status_cb, void *status_arg,
5668 struct got_repository *repo)
5670 const struct got_error *err = NULL, *unlockerr = NULL;
5671 struct got_pathlist_entry *pe;
5672 const char *head_ref_name = NULL;
5673 struct got_commit_object *head_commit = NULL;
5674 struct got_reference *head_ref2 = NULL;
5675 struct got_object_id *head_commit_id2 = NULL;
5676 struct got_tree_object *head_tree = NULL;
5677 struct got_object_id *new_tree_id = NULL;
5678 int nentries, nparents = 0;
5679 struct got_object_id_queue parent_ids;
5680 struct got_object_qid *pid = NULL;
5681 char *logmsg = NULL;
5682 time_t timestamp;
5684 *new_commit_id = NULL;
5686 STAILQ_INIT(&parent_ids);
5688 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5689 if (err)
5690 goto done;
5692 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5693 if (err)
5694 goto done;
5696 if (commit_msg_cb != NULL) {
5697 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5698 if (err)
5699 goto done;
5702 if (logmsg == NULL || strlen(logmsg) == 0) {
5703 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5704 goto done;
5707 /* Create blobs from added and modified files and record their IDs. */
5708 TAILQ_FOREACH(pe, commitable_paths, entry) {
5709 struct got_commitable *ct = pe->data;
5710 char *ondisk_path;
5712 /* Blobs for staged files already exist. */
5713 if (ct->staged_status == GOT_STATUS_ADD ||
5714 ct->staged_status == GOT_STATUS_MODIFY)
5715 continue;
5717 if (ct->status != GOT_STATUS_ADD &&
5718 ct->status != GOT_STATUS_MODIFY &&
5719 ct->status != GOT_STATUS_MODE_CHANGE)
5720 continue;
5722 if (asprintf(&ondisk_path, "%s/%s",
5723 worktree->root_path, pe->path) == -1) {
5724 err = got_error_from_errno("asprintf");
5725 goto done;
5727 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5728 free(ondisk_path);
5729 if (err)
5730 goto done;
5733 /* Recursively write new tree objects. */
5734 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5735 commitable_paths, status_cb, status_arg, repo);
5736 if (err)
5737 goto done;
5739 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5740 if (err)
5741 goto done;
5742 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5743 nparents++;
5744 if (parent_id2) {
5745 err = got_object_qid_alloc(&pid, parent_id2);
5746 if (err)
5747 goto done;
5748 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5749 nparents++;
5751 timestamp = time(NULL);
5752 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5753 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5754 if (logmsg != NULL)
5755 free(logmsg);
5756 if (err)
5757 goto done;
5759 /* Check if a concurrent commit to our branch has occurred. */
5760 head_ref_name = got_worktree_get_head_ref_name(worktree);
5761 if (head_ref_name == NULL) {
5762 err = got_error_from_errno("got_worktree_get_head_ref_name");
5763 goto done;
5765 /* Lock the reference here to prevent concurrent modification. */
5766 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5767 if (err)
5768 goto done;
5769 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5770 if (err)
5771 goto done;
5772 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5773 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5774 goto done;
5776 /* Update branch head in repository. */
5777 err = got_ref_change_ref(head_ref2, *new_commit_id);
5778 if (err)
5779 goto done;
5780 err = got_ref_write(head_ref2, repo);
5781 if (err)
5782 goto done;
5784 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5785 if (err)
5786 goto done;
5788 err = ref_base_commit(worktree, repo);
5789 if (err)
5790 goto done;
5791 done:
5792 got_object_id_queue_free(&parent_ids);
5793 if (head_tree)
5794 got_object_tree_close(head_tree);
5795 if (head_commit)
5796 got_object_commit_close(head_commit);
5797 free(head_commit_id2);
5798 if (head_ref2) {
5799 unlockerr = got_ref_unlock(head_ref2);
5800 if (unlockerr && err == NULL)
5801 err = unlockerr;
5802 got_ref_close(head_ref2);
5804 return err;
5807 static const struct got_error *
5808 check_path_is_commitable(const char *path,
5809 struct got_pathlist_head *commitable_paths)
5811 struct got_pathlist_entry *cpe = NULL;
5812 size_t path_len = strlen(path);
5814 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5815 struct got_commitable *ct = cpe->data;
5816 const char *ct_path = ct->path;
5818 while (ct_path[0] == '/')
5819 ct_path++;
5821 if (strcmp(path, ct_path) == 0 ||
5822 got_path_is_child(ct_path, path, path_len))
5823 break;
5826 if (cpe == NULL)
5827 return got_error_path(path, GOT_ERR_BAD_PATH);
5829 return NULL;
5832 static const struct got_error *
5833 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5835 int *have_staged_files = arg;
5837 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5838 *have_staged_files = 1;
5839 return got_error(GOT_ERR_CANCELLED);
5842 return NULL;
5845 static const struct got_error *
5846 check_non_staged_files(struct got_fileindex *fileindex,
5847 struct got_pathlist_head *paths)
5849 struct got_pathlist_entry *pe;
5850 struct got_fileindex_entry *ie;
5852 TAILQ_FOREACH(pe, paths, entry) {
5853 if (pe->path[0] == '\0')
5854 continue;
5855 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5856 if (ie == NULL)
5857 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5858 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5859 return got_error_path(pe->path,
5860 GOT_ERR_FILE_NOT_STAGED);
5863 return NULL;
5866 const struct got_error *
5867 got_worktree_commit(struct got_object_id **new_commit_id,
5868 struct got_worktree *worktree, struct got_pathlist_head *paths,
5869 const char *author, const char *committer, int allow_bad_symlinks,
5870 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5871 got_worktree_status_cb status_cb, void *status_arg,
5872 struct got_repository *repo)
5874 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5875 struct got_fileindex *fileindex = NULL;
5876 char *fileindex_path = NULL;
5877 struct got_pathlist_head commitable_paths;
5878 struct collect_commitables_arg cc_arg;
5879 struct got_pathlist_entry *pe;
5880 struct got_reference *head_ref = NULL;
5881 struct got_object_id *head_commit_id = NULL;
5882 int have_staged_files = 0;
5884 *new_commit_id = NULL;
5886 TAILQ_INIT(&commitable_paths);
5888 err = lock_worktree(worktree, LOCK_EX);
5889 if (err)
5890 goto done;
5892 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5893 if (err)
5894 goto done;
5896 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5897 if (err)
5898 goto done;
5900 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5901 if (err)
5902 goto done;
5904 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5905 &have_staged_files);
5906 if (err && err->code != GOT_ERR_CANCELLED)
5907 goto done;
5908 if (have_staged_files) {
5909 err = check_non_staged_files(fileindex, paths);
5910 if (err)
5911 goto done;
5914 cc_arg.commitable_paths = &commitable_paths;
5915 cc_arg.worktree = worktree;
5916 cc_arg.fileindex = fileindex;
5917 cc_arg.repo = repo;
5918 cc_arg.have_staged_files = have_staged_files;
5919 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5920 TAILQ_FOREACH(pe, paths, entry) {
5921 err = worktree_status(worktree, pe->path, fileindex, repo,
5922 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5923 if (err)
5924 goto done;
5927 if (TAILQ_EMPTY(&commitable_paths)) {
5928 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5929 goto done;
5932 TAILQ_FOREACH(pe, paths, entry) {
5933 err = check_path_is_commitable(pe->path, &commitable_paths);
5934 if (err)
5935 goto done;
5938 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5939 struct got_commitable *ct = pe->data;
5940 const char *ct_path = ct->in_repo_path;
5942 while (ct_path[0] == '/')
5943 ct_path++;
5944 err = check_out_of_date(ct_path, ct->status,
5945 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5946 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5947 if (err)
5948 goto done;
5952 err = commit_worktree(new_commit_id, &commitable_paths,
5953 head_commit_id, NULL, worktree, author, committer,
5954 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5955 if (err)
5956 goto done;
5958 err = update_fileindex_after_commit(worktree, &commitable_paths,
5959 *new_commit_id, fileindex, have_staged_files);
5960 sync_err = sync_fileindex(fileindex, fileindex_path);
5961 if (sync_err && err == NULL)
5962 err = sync_err;
5963 done:
5964 if (fileindex)
5965 got_fileindex_free(fileindex);
5966 free(fileindex_path);
5967 unlockerr = lock_worktree(worktree, LOCK_SH);
5968 if (unlockerr && err == NULL)
5969 err = unlockerr;
5970 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5971 struct got_commitable *ct = pe->data;
5972 free_commitable(ct);
5974 got_pathlist_free(&commitable_paths);
5975 return err;
5978 const char *
5979 got_commitable_get_path(struct got_commitable *ct)
5981 return ct->path;
5984 unsigned int
5985 got_commitable_get_status(struct got_commitable *ct)
5987 return ct->status;
5990 struct check_rebase_ok_arg {
5991 struct got_worktree *worktree;
5992 struct got_repository *repo;
5995 static const struct got_error *
5996 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5998 const struct got_error *err = NULL;
5999 struct check_rebase_ok_arg *a = arg;
6000 unsigned char status;
6001 struct stat sb;
6002 char *ondisk_path;
6004 /* Reject rebase of a work tree with mixed base commits. */
6005 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6006 SHA1_DIGEST_LENGTH))
6007 return got_error(GOT_ERR_MIXED_COMMITS);
6009 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6010 == -1)
6011 return got_error_from_errno("asprintf");
6013 /* Reject rebase of a work tree with modified or staged files. */
6014 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6015 free(ondisk_path);
6016 if (err)
6017 return err;
6019 if (status != GOT_STATUS_NO_CHANGE)
6020 return got_error(GOT_ERR_MODIFIED);
6021 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6022 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6024 return NULL;
6027 const struct got_error *
6028 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6029 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6030 struct got_worktree *worktree, struct got_reference *branch,
6031 struct got_repository *repo)
6033 const struct got_error *err = NULL;
6034 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6035 char *branch_ref_name = NULL;
6036 char *fileindex_path = NULL;
6037 struct check_rebase_ok_arg ok_arg;
6038 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6039 struct got_object_id *wt_branch_tip = NULL;
6041 *new_base_branch_ref = NULL;
6042 *tmp_branch = NULL;
6043 *fileindex = NULL;
6045 err = lock_worktree(worktree, LOCK_EX);
6046 if (err)
6047 return err;
6049 err = open_fileindex(fileindex, &fileindex_path, worktree);
6050 if (err)
6051 goto done;
6053 ok_arg.worktree = worktree;
6054 ok_arg.repo = repo;
6055 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6056 &ok_arg);
6057 if (err)
6058 goto done;
6060 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6061 if (err)
6062 goto done;
6064 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6065 if (err)
6066 goto done;
6068 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6069 if (err)
6070 goto done;
6072 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6073 0);
6074 if (err)
6075 goto done;
6077 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6078 if (err)
6079 goto done;
6080 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6081 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6082 goto done;
6085 err = got_ref_alloc_symref(new_base_branch_ref,
6086 new_base_branch_ref_name, wt_branch);
6087 if (err)
6088 goto done;
6089 err = got_ref_write(*new_base_branch_ref, repo);
6090 if (err)
6091 goto done;
6093 /* TODO Lock original branch's ref while rebasing? */
6095 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6096 if (err)
6097 goto done;
6099 err = got_ref_write(branch_ref, repo);
6100 if (err)
6101 goto done;
6103 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6104 worktree->base_commit_id);
6105 if (err)
6106 goto done;
6107 err = got_ref_write(*tmp_branch, repo);
6108 if (err)
6109 goto done;
6111 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6112 if (err)
6113 goto done;
6114 done:
6115 free(fileindex_path);
6116 free(tmp_branch_name);
6117 free(new_base_branch_ref_name);
6118 free(branch_ref_name);
6119 if (branch_ref)
6120 got_ref_close(branch_ref);
6121 if (wt_branch)
6122 got_ref_close(wt_branch);
6123 free(wt_branch_tip);
6124 if (err) {
6125 if (*new_base_branch_ref) {
6126 got_ref_close(*new_base_branch_ref);
6127 *new_base_branch_ref = NULL;
6129 if (*tmp_branch) {
6130 got_ref_close(*tmp_branch);
6131 *tmp_branch = NULL;
6133 if (*fileindex) {
6134 got_fileindex_free(*fileindex);
6135 *fileindex = NULL;
6137 lock_worktree(worktree, LOCK_SH);
6139 return err;
6142 const struct got_error *
6143 got_worktree_rebase_continue(struct got_object_id **commit_id,
6144 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6145 struct got_reference **branch, struct got_fileindex **fileindex,
6146 struct got_worktree *worktree, struct got_repository *repo)
6148 const struct got_error *err;
6149 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6150 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6151 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6152 char *fileindex_path = NULL;
6153 int have_staged_files = 0;
6155 *commit_id = NULL;
6156 *new_base_branch = NULL;
6157 *tmp_branch = NULL;
6158 *branch = NULL;
6159 *fileindex = NULL;
6161 err = lock_worktree(worktree, LOCK_EX);
6162 if (err)
6163 return err;
6165 err = open_fileindex(fileindex, &fileindex_path, worktree);
6166 if (err)
6167 goto done;
6169 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6170 &have_staged_files);
6171 if (err && err->code != GOT_ERR_CANCELLED)
6172 goto done;
6173 if (have_staged_files) {
6174 err = got_error(GOT_ERR_STAGED_PATHS);
6175 goto done;
6178 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6179 if (err)
6180 goto done;
6182 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6183 if (err)
6184 goto done;
6186 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6187 if (err)
6188 goto done;
6190 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6191 if (err)
6192 goto done;
6194 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6195 if (err)
6196 goto done;
6198 err = got_ref_open(branch, repo,
6199 got_ref_get_symref_target(branch_ref), 0);
6200 if (err)
6201 goto done;
6203 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6204 if (err)
6205 goto done;
6207 err = got_ref_resolve(commit_id, repo, commit_ref);
6208 if (err)
6209 goto done;
6211 err = got_ref_open(new_base_branch, repo,
6212 new_base_branch_ref_name, 0);
6213 if (err)
6214 goto done;
6216 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6217 if (err)
6218 goto done;
6219 done:
6220 free(commit_ref_name);
6221 free(branch_ref_name);
6222 free(fileindex_path);
6223 if (commit_ref)
6224 got_ref_close(commit_ref);
6225 if (branch_ref)
6226 got_ref_close(branch_ref);
6227 if (err) {
6228 free(*commit_id);
6229 *commit_id = NULL;
6230 if (*tmp_branch) {
6231 got_ref_close(*tmp_branch);
6232 *tmp_branch = NULL;
6234 if (*new_base_branch) {
6235 got_ref_close(*new_base_branch);
6236 *new_base_branch = NULL;
6238 if (*branch) {
6239 got_ref_close(*branch);
6240 *branch = NULL;
6242 if (*fileindex) {
6243 got_fileindex_free(*fileindex);
6244 *fileindex = NULL;
6246 lock_worktree(worktree, LOCK_SH);
6248 return err;
6251 const struct got_error *
6252 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6254 const struct got_error *err;
6255 char *tmp_branch_name = NULL;
6257 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6258 if (err)
6259 return err;
6261 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6262 free(tmp_branch_name);
6263 return NULL;
6266 static const struct got_error *
6267 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6268 char **logmsg, void *arg)
6270 *logmsg = arg;
6271 return NULL;
6274 static const struct got_error *
6275 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6276 const char *path, struct got_object_id *blob_id,
6277 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6278 int dirfd, const char *de_name)
6280 return NULL;
6283 struct collect_merged_paths_arg {
6284 got_worktree_checkout_cb progress_cb;
6285 void *progress_arg;
6286 struct got_pathlist_head *merged_paths;
6289 static const struct got_error *
6290 collect_merged_paths(void *arg, unsigned char status, const char *path)
6292 const struct got_error *err;
6293 struct collect_merged_paths_arg *a = arg;
6294 char *p;
6295 struct got_pathlist_entry *new;
6297 err = (*a->progress_cb)(a->progress_arg, status, path);
6298 if (err)
6299 return err;
6301 if (status != GOT_STATUS_MERGE &&
6302 status != GOT_STATUS_ADD &&
6303 status != GOT_STATUS_DELETE &&
6304 status != GOT_STATUS_CONFLICT)
6305 return NULL;
6307 p = strdup(path);
6308 if (p == NULL)
6309 return got_error_from_errno("strdup");
6311 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6312 if (err || new == NULL)
6313 free(p);
6314 return err;
6317 void
6318 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6320 struct got_pathlist_entry *pe;
6322 TAILQ_FOREACH(pe, merged_paths, entry)
6323 free((char *)pe->path);
6325 got_pathlist_free(merged_paths);
6328 static const struct got_error *
6329 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6330 int is_rebase, struct got_repository *repo)
6332 const struct got_error *err;
6333 struct got_reference *commit_ref = NULL;
6335 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6336 if (err) {
6337 if (err->code != GOT_ERR_NOT_REF)
6338 goto done;
6339 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6340 if (err)
6341 goto done;
6342 err = got_ref_write(commit_ref, repo);
6343 if (err)
6344 goto done;
6345 } else if (is_rebase) {
6346 struct got_object_id *stored_id;
6347 int cmp;
6349 err = got_ref_resolve(&stored_id, repo, commit_ref);
6350 if (err)
6351 goto done;
6352 cmp = got_object_id_cmp(commit_id, stored_id);
6353 free(stored_id);
6354 if (cmp != 0) {
6355 err = got_error(GOT_ERR_REBASE_COMMITID);
6356 goto done;
6359 done:
6360 if (commit_ref)
6361 got_ref_close(commit_ref);
6362 return err;
6365 static const struct got_error *
6366 rebase_merge_files(struct got_pathlist_head *merged_paths,
6367 const char *commit_ref_name, struct got_worktree *worktree,
6368 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6369 struct got_object_id *commit_id, struct got_repository *repo,
6370 got_worktree_checkout_cb progress_cb, void *progress_arg,
6371 got_cancel_cb cancel_cb, void *cancel_arg)
6373 const struct got_error *err;
6374 struct got_reference *commit_ref = NULL;
6375 struct collect_merged_paths_arg cmp_arg;
6376 char *fileindex_path;
6378 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6380 err = get_fileindex_path(&fileindex_path, worktree);
6381 if (err)
6382 return err;
6384 cmp_arg.progress_cb = progress_cb;
6385 cmp_arg.progress_arg = progress_arg;
6386 cmp_arg.merged_paths = merged_paths;
6387 err = merge_files(worktree, fileindex, fileindex_path,
6388 parent_commit_id, commit_id, repo, collect_merged_paths,
6389 &cmp_arg, cancel_cb, cancel_arg);
6390 if (commit_ref)
6391 got_ref_close(commit_ref);
6392 return err;
6395 const struct got_error *
6396 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6397 struct got_worktree *worktree, struct got_fileindex *fileindex,
6398 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6399 struct got_repository *repo,
6400 got_worktree_checkout_cb progress_cb, void *progress_arg,
6401 got_cancel_cb cancel_cb, void *cancel_arg)
6403 const struct got_error *err;
6404 char *commit_ref_name;
6406 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6407 if (err)
6408 return err;
6410 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6411 if (err)
6412 goto done;
6414 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6415 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6416 progress_arg, cancel_cb, cancel_arg);
6417 done:
6418 free(commit_ref_name);
6419 return err;
6422 const struct got_error *
6423 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6424 struct got_worktree *worktree, struct got_fileindex *fileindex,
6425 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6426 struct got_repository *repo,
6427 got_worktree_checkout_cb progress_cb, void *progress_arg,
6428 got_cancel_cb cancel_cb, void *cancel_arg)
6430 const struct got_error *err;
6431 char *commit_ref_name;
6433 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6434 if (err)
6435 return err;
6437 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6438 if (err)
6439 goto done;
6441 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6442 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6443 progress_arg, cancel_cb, cancel_arg);
6444 done:
6445 free(commit_ref_name);
6446 return err;
6449 static const struct got_error *
6450 rebase_commit(struct got_object_id **new_commit_id,
6451 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6452 struct got_worktree *worktree, struct got_fileindex *fileindex,
6453 struct got_reference *tmp_branch, const char *committer,
6454 struct got_commit_object *orig_commit, const char *new_logmsg,
6455 struct got_repository *repo)
6457 const struct got_error *err, *sync_err;
6458 struct got_pathlist_head commitable_paths;
6459 struct collect_commitables_arg cc_arg;
6460 char *fileindex_path = NULL;
6461 struct got_reference *head_ref = NULL;
6462 struct got_object_id *head_commit_id = NULL;
6463 char *logmsg = NULL;
6465 TAILQ_INIT(&commitable_paths);
6466 *new_commit_id = NULL;
6468 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6470 err = get_fileindex_path(&fileindex_path, worktree);
6471 if (err)
6472 return err;
6474 cc_arg.commitable_paths = &commitable_paths;
6475 cc_arg.worktree = worktree;
6476 cc_arg.repo = repo;
6477 cc_arg.have_staged_files = 0;
6479 * If possible get the status of individual files directly to
6480 * avoid crawling the entire work tree once per rebased commit.
6482 * Ideally, merged_paths would contain a list of commitables
6483 * we could use so we could skip worktree_status() entirely.
6484 * However, we would then need carefully keep track of cumulative
6485 * effects of operations such as file additions and deletions
6486 * in 'got histedit -f' (folding multiple commits into one),
6487 * and this extra complexity is not really worth it.
6489 if (merged_paths) {
6490 struct got_pathlist_entry *pe;
6491 TAILQ_FOREACH(pe, merged_paths, entry) {
6492 err = worktree_status(worktree, pe->path, fileindex,
6493 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6494 0);
6495 if (err)
6496 goto done;
6498 } else {
6499 err = worktree_status(worktree, "", fileindex, repo,
6500 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6501 if (err)
6502 goto done;
6505 if (TAILQ_EMPTY(&commitable_paths)) {
6506 /* No-op change; commit will be elided. */
6507 err = got_ref_delete(commit_ref, repo);
6508 if (err)
6509 goto done;
6510 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6511 goto done;
6514 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6515 if (err)
6516 goto done;
6518 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6519 if (err)
6520 goto done;
6522 if (new_logmsg) {
6523 logmsg = strdup(new_logmsg);
6524 if (logmsg == NULL) {
6525 err = got_error_from_errno("strdup");
6526 goto done;
6528 } else {
6529 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6530 if (err)
6531 goto done;
6534 /* NB: commit_worktree will call free(logmsg) */
6535 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6536 NULL, worktree, got_object_commit_get_author(orig_commit),
6537 committer, collect_rebase_commit_msg, logmsg, rebase_status, NULL,
6538 repo);
6539 if (err)
6540 goto done;
6542 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6543 if (err)
6544 goto done;
6546 err = got_ref_delete(commit_ref, repo);
6547 if (err)
6548 goto done;
6550 err = update_fileindex_after_commit(worktree, &commitable_paths,
6551 *new_commit_id, fileindex, 0);
6552 sync_err = sync_fileindex(fileindex, fileindex_path);
6553 if (sync_err && err == NULL)
6554 err = sync_err;
6555 done:
6556 free(fileindex_path);
6557 free(head_commit_id);
6558 if (head_ref)
6559 got_ref_close(head_ref);
6560 if (err) {
6561 free(*new_commit_id);
6562 *new_commit_id = NULL;
6564 return err;
6567 const struct got_error *
6568 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6569 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6570 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6571 const char *committer, struct got_commit_object *orig_commit,
6572 struct got_object_id *orig_commit_id, struct got_repository *repo)
6574 const struct got_error *err;
6575 char *commit_ref_name;
6576 struct got_reference *commit_ref = NULL;
6577 struct got_object_id *commit_id = NULL;
6579 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6580 if (err)
6581 return err;
6583 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6584 if (err)
6585 goto done;
6586 err = got_ref_resolve(&commit_id, repo, commit_ref);
6587 if (err)
6588 goto done;
6589 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6590 err = got_error(GOT_ERR_REBASE_COMMITID);
6591 goto done;
6594 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6595 worktree, fileindex, tmp_branch, committer, orig_commit,
6596 NULL, repo);
6597 done:
6598 if (commit_ref)
6599 got_ref_close(commit_ref);
6600 free(commit_ref_name);
6601 free(commit_id);
6602 return err;
6605 const struct got_error *
6606 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6607 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6608 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6609 const char *committer, struct got_commit_object *orig_commit,
6610 struct got_object_id *orig_commit_id, const char *new_logmsg,
6611 struct got_repository *repo)
6613 const struct got_error *err;
6614 char *commit_ref_name;
6615 struct got_reference *commit_ref = NULL;
6617 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6618 if (err)
6619 return err;
6621 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6622 if (err)
6623 goto done;
6625 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6626 worktree, fileindex, tmp_branch, committer, orig_commit,
6627 new_logmsg, repo);
6628 done:
6629 if (commit_ref)
6630 got_ref_close(commit_ref);
6631 free(commit_ref_name);
6632 return err;
6635 const struct got_error *
6636 got_worktree_rebase_postpone(struct got_worktree *worktree,
6637 struct got_fileindex *fileindex)
6639 if (fileindex)
6640 got_fileindex_free(fileindex);
6641 return lock_worktree(worktree, LOCK_SH);
6644 static const struct got_error *
6645 delete_ref(const char *name, struct got_repository *repo)
6647 const struct got_error *err;
6648 struct got_reference *ref;
6650 err = got_ref_open(&ref, repo, name, 0);
6651 if (err) {
6652 if (err->code == GOT_ERR_NOT_REF)
6653 return NULL;
6654 return err;
6657 err = got_ref_delete(ref, repo);
6658 got_ref_close(ref);
6659 return err;
6662 static const struct got_error *
6663 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6665 const struct got_error *err;
6666 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6667 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6669 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6670 if (err)
6671 goto done;
6672 err = delete_ref(tmp_branch_name, repo);
6673 if (err)
6674 goto done;
6676 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6677 if (err)
6678 goto done;
6679 err = delete_ref(new_base_branch_ref_name, repo);
6680 if (err)
6681 goto done;
6683 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6684 if (err)
6685 goto done;
6686 err = delete_ref(branch_ref_name, repo);
6687 if (err)
6688 goto done;
6690 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6691 if (err)
6692 goto done;
6693 err = delete_ref(commit_ref_name, repo);
6694 if (err)
6695 goto done;
6697 done:
6698 free(tmp_branch_name);
6699 free(new_base_branch_ref_name);
6700 free(branch_ref_name);
6701 free(commit_ref_name);
6702 return err;
6705 static const struct got_error *
6706 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6707 struct got_object_id *new_commit_id, struct got_repository *repo)
6709 const struct got_error *err;
6710 struct got_reference *ref = NULL;
6711 struct got_object_id *old_commit_id = NULL;
6712 const char *branch_name = NULL;
6713 char *new_id_str = NULL;
6714 char *refname = NULL;
6716 branch_name = got_ref_get_name(branch);
6717 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6718 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6719 branch_name += 11;
6721 err = got_object_id_str(&new_id_str, new_commit_id);
6722 if (err)
6723 return err;
6725 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6726 new_id_str) == -1) {
6727 err = got_error_from_errno("asprintf");
6728 goto done;
6731 err = got_ref_resolve(&old_commit_id, repo, branch);
6732 if (err)
6733 goto done;
6735 err = got_ref_alloc(&ref, refname, old_commit_id);
6736 if (err)
6737 goto done;
6739 err = got_ref_write(ref, repo);
6740 done:
6741 free(new_id_str);
6742 free(refname);
6743 free(old_commit_id);
6744 if (ref)
6745 got_ref_close(ref);
6746 return err;
6749 const struct got_error *
6750 got_worktree_rebase_complete(struct got_worktree *worktree,
6751 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6752 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6753 struct got_repository *repo, int create_backup)
6755 const struct got_error *err, *unlockerr, *sync_err;
6756 struct got_object_id *new_head_commit_id = NULL;
6757 char *fileindex_path = NULL;
6759 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6760 if (err)
6761 return err;
6763 if (create_backup) {
6764 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6765 rebased_branch, new_head_commit_id, repo);
6766 if (err)
6767 goto done;
6770 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6771 if (err)
6772 goto done;
6774 err = got_ref_write(rebased_branch, repo);
6775 if (err)
6776 goto done;
6778 err = got_worktree_set_head_ref(worktree, rebased_branch);
6779 if (err)
6780 goto done;
6782 err = delete_rebase_refs(worktree, repo);
6783 if (err)
6784 goto done;
6786 err = get_fileindex_path(&fileindex_path, worktree);
6787 if (err)
6788 goto done;
6789 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6790 sync_err = sync_fileindex(fileindex, fileindex_path);
6791 if (sync_err && err == NULL)
6792 err = sync_err;
6793 done:
6794 got_fileindex_free(fileindex);
6795 free(fileindex_path);
6796 free(new_head_commit_id);
6797 unlockerr = lock_worktree(worktree, LOCK_SH);
6798 if (unlockerr && err == NULL)
6799 err = unlockerr;
6800 return err;
6803 const struct got_error *
6804 got_worktree_rebase_abort(struct got_worktree *worktree,
6805 struct got_fileindex *fileindex, struct got_repository *repo,
6806 struct got_reference *new_base_branch,
6807 got_worktree_checkout_cb progress_cb, void *progress_arg)
6809 const struct got_error *err, *unlockerr, *sync_err;
6810 struct got_reference *resolved = NULL;
6811 struct got_object_id *commit_id = NULL;
6812 struct got_commit_object *commit = NULL;
6813 char *fileindex_path = NULL;
6814 struct revert_file_args rfa;
6815 struct got_object_id *tree_id = NULL;
6817 err = lock_worktree(worktree, LOCK_EX);
6818 if (err)
6819 return err;
6821 err = got_object_open_as_commit(&commit, repo,
6822 worktree->base_commit_id);
6823 if (err)
6824 goto done;
6826 err = got_ref_open(&resolved, repo,
6827 got_ref_get_symref_target(new_base_branch), 0);
6828 if (err)
6829 goto done;
6831 err = got_worktree_set_head_ref(worktree, resolved);
6832 if (err)
6833 goto done;
6836 * XXX commits to the base branch could have happened while
6837 * we were busy rebasing; should we store the original commit ID
6838 * when rebase begins and read it back here?
6840 err = got_ref_resolve(&commit_id, repo, resolved);
6841 if (err)
6842 goto done;
6844 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6845 if (err)
6846 goto done;
6848 err = got_object_id_by_path(&tree_id, repo, commit,
6849 worktree->path_prefix);
6850 if (err)
6851 goto done;
6853 err = delete_rebase_refs(worktree, repo);
6854 if (err)
6855 goto done;
6857 err = get_fileindex_path(&fileindex_path, worktree);
6858 if (err)
6859 goto done;
6861 rfa.worktree = worktree;
6862 rfa.fileindex = fileindex;
6863 rfa.progress_cb = progress_cb;
6864 rfa.progress_arg = progress_arg;
6865 rfa.patch_cb = NULL;
6866 rfa.patch_arg = NULL;
6867 rfa.repo = repo;
6868 rfa.unlink_added_files = 0;
6869 err = worktree_status(worktree, "", fileindex, repo,
6870 revert_file, &rfa, NULL, NULL, 1, 0);
6871 if (err)
6872 goto sync;
6874 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6875 repo, progress_cb, progress_arg, NULL, NULL);
6876 sync:
6877 sync_err = sync_fileindex(fileindex, fileindex_path);
6878 if (sync_err && err == NULL)
6879 err = sync_err;
6880 done:
6881 got_ref_close(resolved);
6882 free(tree_id);
6883 free(commit_id);
6884 if (commit)
6885 got_object_commit_close(commit);
6886 if (fileindex)
6887 got_fileindex_free(fileindex);
6888 free(fileindex_path);
6890 unlockerr = lock_worktree(worktree, LOCK_SH);
6891 if (unlockerr && err == NULL)
6892 err = unlockerr;
6893 return err;
6896 const struct got_error *
6897 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6898 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6899 struct got_fileindex **fileindex, struct got_worktree *worktree,
6900 struct got_repository *repo)
6902 const struct got_error *err = NULL;
6903 char *tmp_branch_name = NULL;
6904 char *branch_ref_name = NULL;
6905 char *base_commit_ref_name = NULL;
6906 char *fileindex_path = NULL;
6907 struct check_rebase_ok_arg ok_arg;
6908 struct got_reference *wt_branch = NULL;
6909 struct got_reference *base_commit_ref = NULL;
6911 *tmp_branch = NULL;
6912 *branch_ref = NULL;
6913 *base_commit_id = NULL;
6914 *fileindex = NULL;
6916 err = lock_worktree(worktree, LOCK_EX);
6917 if (err)
6918 return err;
6920 err = open_fileindex(fileindex, &fileindex_path, worktree);
6921 if (err)
6922 goto done;
6924 ok_arg.worktree = worktree;
6925 ok_arg.repo = repo;
6926 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6927 &ok_arg);
6928 if (err)
6929 goto done;
6931 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6932 if (err)
6933 goto done;
6935 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6936 if (err)
6937 goto done;
6939 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6940 worktree);
6941 if (err)
6942 goto done;
6944 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6945 0);
6946 if (err)
6947 goto done;
6949 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6950 if (err)
6951 goto done;
6953 err = got_ref_write(*branch_ref, repo);
6954 if (err)
6955 goto done;
6957 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6958 worktree->base_commit_id);
6959 if (err)
6960 goto done;
6961 err = got_ref_write(base_commit_ref, repo);
6962 if (err)
6963 goto done;
6964 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6965 if (*base_commit_id == NULL) {
6966 err = got_error_from_errno("got_object_id_dup");
6967 goto done;
6970 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6971 worktree->base_commit_id);
6972 if (err)
6973 goto done;
6974 err = got_ref_write(*tmp_branch, repo);
6975 if (err)
6976 goto done;
6978 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6979 if (err)
6980 goto done;
6981 done:
6982 free(fileindex_path);
6983 free(tmp_branch_name);
6984 free(branch_ref_name);
6985 free(base_commit_ref_name);
6986 if (wt_branch)
6987 got_ref_close(wt_branch);
6988 if (err) {
6989 if (*branch_ref) {
6990 got_ref_close(*branch_ref);
6991 *branch_ref = NULL;
6993 if (*tmp_branch) {
6994 got_ref_close(*tmp_branch);
6995 *tmp_branch = NULL;
6997 free(*base_commit_id);
6998 if (*fileindex) {
6999 got_fileindex_free(*fileindex);
7000 *fileindex = NULL;
7002 lock_worktree(worktree, LOCK_SH);
7004 return err;
7007 const struct got_error *
7008 got_worktree_histedit_postpone(struct got_worktree *worktree,
7009 struct got_fileindex *fileindex)
7011 if (fileindex)
7012 got_fileindex_free(fileindex);
7013 return lock_worktree(worktree, LOCK_SH);
7016 const struct got_error *
7017 got_worktree_histedit_in_progress(int *in_progress,
7018 struct got_worktree *worktree)
7020 const struct got_error *err;
7021 char *tmp_branch_name = NULL;
7023 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7024 if (err)
7025 return err;
7027 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7028 free(tmp_branch_name);
7029 return NULL;
7032 const struct got_error *
7033 got_worktree_histedit_continue(struct got_object_id **commit_id,
7034 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7035 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7036 struct got_worktree *worktree, struct got_repository *repo)
7038 const struct got_error *err;
7039 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7040 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7041 struct got_reference *commit_ref = NULL;
7042 struct got_reference *base_commit_ref = NULL;
7043 char *fileindex_path = NULL;
7044 int have_staged_files = 0;
7046 *commit_id = NULL;
7047 *tmp_branch = NULL;
7048 *base_commit_id = NULL;
7049 *fileindex = NULL;
7051 err = lock_worktree(worktree, LOCK_EX);
7052 if (err)
7053 return err;
7055 err = open_fileindex(fileindex, &fileindex_path, worktree);
7056 if (err)
7057 goto done;
7059 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7060 &have_staged_files);
7061 if (err && err->code != GOT_ERR_CANCELLED)
7062 goto done;
7063 if (have_staged_files) {
7064 err = got_error(GOT_ERR_STAGED_PATHS);
7065 goto done;
7068 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7069 if (err)
7070 goto done;
7072 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7073 if (err)
7074 goto done;
7076 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7077 if (err)
7078 goto done;
7080 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7081 worktree);
7082 if (err)
7083 goto done;
7085 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7086 if (err)
7087 goto done;
7089 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7090 if (err)
7091 goto done;
7092 err = got_ref_resolve(commit_id, repo, commit_ref);
7093 if (err)
7094 goto done;
7096 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7097 if (err)
7098 goto done;
7099 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7100 if (err)
7101 goto done;
7103 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7104 if (err)
7105 goto done;
7106 done:
7107 free(commit_ref_name);
7108 free(branch_ref_name);
7109 free(fileindex_path);
7110 if (commit_ref)
7111 got_ref_close(commit_ref);
7112 if (base_commit_ref)
7113 got_ref_close(base_commit_ref);
7114 if (err) {
7115 free(*commit_id);
7116 *commit_id = NULL;
7117 free(*base_commit_id);
7118 *base_commit_id = NULL;
7119 if (*tmp_branch) {
7120 got_ref_close(*tmp_branch);
7121 *tmp_branch = NULL;
7123 if (*fileindex) {
7124 got_fileindex_free(*fileindex);
7125 *fileindex = NULL;
7127 lock_worktree(worktree, LOCK_EX);
7129 return err;
7132 static const struct got_error *
7133 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7135 const struct got_error *err;
7136 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7137 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7139 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7140 if (err)
7141 goto done;
7142 err = delete_ref(tmp_branch_name, repo);
7143 if (err)
7144 goto done;
7146 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7147 worktree);
7148 if (err)
7149 goto done;
7150 err = delete_ref(base_commit_ref_name, repo);
7151 if (err)
7152 goto done;
7154 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7155 if (err)
7156 goto done;
7157 err = delete_ref(branch_ref_name, repo);
7158 if (err)
7159 goto done;
7161 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7162 if (err)
7163 goto done;
7164 err = delete_ref(commit_ref_name, repo);
7165 if (err)
7166 goto done;
7167 done:
7168 free(tmp_branch_name);
7169 free(base_commit_ref_name);
7170 free(branch_ref_name);
7171 free(commit_ref_name);
7172 return err;
7175 const struct got_error *
7176 got_worktree_histedit_abort(struct got_worktree *worktree,
7177 struct got_fileindex *fileindex, struct got_repository *repo,
7178 struct got_reference *branch, struct got_object_id *base_commit_id,
7179 got_worktree_checkout_cb progress_cb, void *progress_arg)
7181 const struct got_error *err, *unlockerr, *sync_err;
7182 struct got_reference *resolved = NULL;
7183 char *fileindex_path = NULL;
7184 struct got_commit_object *commit = NULL;
7185 struct got_object_id *tree_id = NULL;
7186 struct revert_file_args rfa;
7188 err = lock_worktree(worktree, LOCK_EX);
7189 if (err)
7190 return err;
7192 err = got_object_open_as_commit(&commit, repo,
7193 worktree->base_commit_id);
7194 if (err)
7195 goto done;
7197 err = got_ref_open(&resolved, repo,
7198 got_ref_get_symref_target(branch), 0);
7199 if (err)
7200 goto done;
7202 err = got_worktree_set_head_ref(worktree, resolved);
7203 if (err)
7204 goto done;
7206 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7207 if (err)
7208 goto done;
7210 err = got_object_id_by_path(&tree_id, repo, commit,
7211 worktree->path_prefix);
7212 if (err)
7213 goto done;
7215 err = delete_histedit_refs(worktree, repo);
7216 if (err)
7217 goto done;
7219 err = get_fileindex_path(&fileindex_path, worktree);
7220 if (err)
7221 goto done;
7223 rfa.worktree = worktree;
7224 rfa.fileindex = fileindex;
7225 rfa.progress_cb = progress_cb;
7226 rfa.progress_arg = progress_arg;
7227 rfa.patch_cb = NULL;
7228 rfa.patch_arg = NULL;
7229 rfa.repo = repo;
7230 rfa.unlink_added_files = 0;
7231 err = worktree_status(worktree, "", fileindex, repo,
7232 revert_file, &rfa, NULL, NULL, 1, 0);
7233 if (err)
7234 goto sync;
7236 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7237 repo, progress_cb, progress_arg, NULL, NULL);
7238 sync:
7239 sync_err = sync_fileindex(fileindex, fileindex_path);
7240 if (sync_err && err == NULL)
7241 err = sync_err;
7242 done:
7243 got_ref_close(resolved);
7244 free(tree_id);
7245 free(fileindex_path);
7247 unlockerr = lock_worktree(worktree, LOCK_SH);
7248 if (unlockerr && err == NULL)
7249 err = unlockerr;
7250 return err;
7253 const struct got_error *
7254 got_worktree_histedit_complete(struct got_worktree *worktree,
7255 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7256 struct got_reference *edited_branch, struct got_repository *repo)
7258 const struct got_error *err, *unlockerr, *sync_err;
7259 struct got_object_id *new_head_commit_id = NULL;
7260 struct got_reference *resolved = NULL;
7261 char *fileindex_path = NULL;
7263 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7264 if (err)
7265 return err;
7267 err = got_ref_open(&resolved, repo,
7268 got_ref_get_symref_target(edited_branch), 0);
7269 if (err)
7270 goto done;
7272 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7273 resolved, new_head_commit_id, repo);
7274 if (err)
7275 goto done;
7277 err = got_ref_change_ref(resolved, new_head_commit_id);
7278 if (err)
7279 goto done;
7281 err = got_ref_write(resolved, repo);
7282 if (err)
7283 goto done;
7285 err = got_worktree_set_head_ref(worktree, resolved);
7286 if (err)
7287 goto done;
7289 err = delete_histedit_refs(worktree, repo);
7290 if (err)
7291 goto done;
7293 err = get_fileindex_path(&fileindex_path, worktree);
7294 if (err)
7295 goto done;
7296 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7297 sync_err = sync_fileindex(fileindex, fileindex_path);
7298 if (sync_err && err == NULL)
7299 err = sync_err;
7300 done:
7301 got_fileindex_free(fileindex);
7302 free(fileindex_path);
7303 free(new_head_commit_id);
7304 unlockerr = lock_worktree(worktree, LOCK_SH);
7305 if (unlockerr && err == NULL)
7306 err = unlockerr;
7307 return err;
7310 const struct got_error *
7311 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7312 struct got_object_id *commit_id, struct got_repository *repo)
7314 const struct got_error *err;
7315 char *commit_ref_name;
7317 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7318 if (err)
7319 return err;
7321 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7322 if (err)
7323 goto done;
7325 err = delete_ref(commit_ref_name, repo);
7326 done:
7327 free(commit_ref_name);
7328 return err;
7331 const struct got_error *
7332 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7333 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7334 struct got_worktree *worktree, const char *refname,
7335 struct got_repository *repo)
7337 const struct got_error *err = NULL;
7338 char *fileindex_path = NULL;
7339 struct check_rebase_ok_arg ok_arg;
7341 *fileindex = NULL;
7342 *branch_ref = NULL;
7343 *base_branch_ref = NULL;
7345 err = lock_worktree(worktree, LOCK_EX);
7346 if (err)
7347 return err;
7349 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7350 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7351 "cannot integrate a branch into itself; "
7352 "update -b or different branch name required");
7353 goto done;
7356 err = open_fileindex(fileindex, &fileindex_path, worktree);
7357 if (err)
7358 goto done;
7360 /* Preconditions are the same as for rebase. */
7361 ok_arg.worktree = worktree;
7362 ok_arg.repo = repo;
7363 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7364 &ok_arg);
7365 if (err)
7366 goto done;
7368 err = got_ref_open(branch_ref, repo, refname, 1);
7369 if (err)
7370 goto done;
7372 err = got_ref_open(base_branch_ref, repo,
7373 got_worktree_get_head_ref_name(worktree), 1);
7374 done:
7375 if (err) {
7376 if (*branch_ref) {
7377 got_ref_close(*branch_ref);
7378 *branch_ref = NULL;
7380 if (*base_branch_ref) {
7381 got_ref_close(*base_branch_ref);
7382 *base_branch_ref = NULL;
7384 if (*fileindex) {
7385 got_fileindex_free(*fileindex);
7386 *fileindex = NULL;
7388 lock_worktree(worktree, LOCK_SH);
7390 return err;
7393 const struct got_error *
7394 got_worktree_integrate_continue(struct got_worktree *worktree,
7395 struct got_fileindex *fileindex, struct got_repository *repo,
7396 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7397 got_worktree_checkout_cb progress_cb, void *progress_arg,
7398 got_cancel_cb cancel_cb, void *cancel_arg)
7400 const struct got_error *err = NULL, *sync_err, *unlockerr;
7401 char *fileindex_path = NULL;
7402 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7403 struct got_commit_object *commit = NULL;
7405 err = get_fileindex_path(&fileindex_path, worktree);
7406 if (err)
7407 goto done;
7409 err = got_ref_resolve(&commit_id, repo, branch_ref);
7410 if (err)
7411 goto done;
7413 err = got_object_open_as_commit(&commit, repo, commit_id);
7414 if (err)
7415 goto done;
7417 err = got_object_id_by_path(&tree_id, repo, commit,
7418 worktree->path_prefix);
7419 if (err)
7420 goto done;
7422 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7423 if (err)
7424 goto done;
7426 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7427 progress_cb, progress_arg, cancel_cb, cancel_arg);
7428 if (err)
7429 goto sync;
7431 err = got_ref_change_ref(base_branch_ref, commit_id);
7432 if (err)
7433 goto sync;
7435 err = got_ref_write(base_branch_ref, repo);
7436 if (err)
7437 goto sync;
7439 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7440 sync:
7441 sync_err = sync_fileindex(fileindex, fileindex_path);
7442 if (sync_err && err == NULL)
7443 err = sync_err;
7445 done:
7446 unlockerr = got_ref_unlock(branch_ref);
7447 if (unlockerr && err == NULL)
7448 err = unlockerr;
7449 got_ref_close(branch_ref);
7451 unlockerr = got_ref_unlock(base_branch_ref);
7452 if (unlockerr && err == NULL)
7453 err = unlockerr;
7454 got_ref_close(base_branch_ref);
7456 got_fileindex_free(fileindex);
7457 free(fileindex_path);
7458 free(tree_id);
7459 if (commit)
7460 got_object_commit_close(commit);
7462 unlockerr = lock_worktree(worktree, LOCK_SH);
7463 if (unlockerr && err == NULL)
7464 err = unlockerr;
7465 return err;
7468 const struct got_error *
7469 got_worktree_integrate_abort(struct got_worktree *worktree,
7470 struct got_fileindex *fileindex, struct got_repository *repo,
7471 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7473 const struct got_error *err = NULL, *unlockerr = NULL;
7475 got_fileindex_free(fileindex);
7477 err = lock_worktree(worktree, LOCK_SH);
7479 unlockerr = got_ref_unlock(branch_ref);
7480 if (unlockerr && err == NULL)
7481 err = unlockerr;
7482 got_ref_close(branch_ref);
7484 unlockerr = got_ref_unlock(base_branch_ref);
7485 if (unlockerr && err == NULL)
7486 err = unlockerr;
7487 got_ref_close(base_branch_ref);
7489 return err;
7492 const struct got_error *
7493 got_worktree_merge_postpone(struct got_worktree *worktree,
7494 struct got_fileindex *fileindex)
7496 const struct got_error *err, *sync_err;
7497 char *fileindex_path = NULL;
7499 err = get_fileindex_path(&fileindex_path, worktree);
7500 if (err)
7501 goto done;
7503 sync_err = sync_fileindex(fileindex, fileindex_path);
7505 err = lock_worktree(worktree, LOCK_SH);
7506 if (sync_err && err == NULL)
7507 err = sync_err;
7508 done:
7509 got_fileindex_free(fileindex);
7510 free(fileindex_path);
7511 return err;
7514 static const struct got_error *
7515 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7517 const struct got_error *err;
7518 char *branch_refname = NULL, *commit_refname = NULL;
7520 err = get_merge_branch_ref_name(&branch_refname, worktree);
7521 if (err)
7522 goto done;
7523 err = delete_ref(branch_refname, repo);
7524 if (err)
7525 goto done;
7527 err = get_merge_commit_ref_name(&commit_refname, worktree);
7528 if (err)
7529 goto done;
7530 err = delete_ref(commit_refname, repo);
7531 if (err)
7532 goto done;
7534 done:
7535 free(branch_refname);
7536 free(commit_refname);
7537 return err;
7540 struct merge_commit_msg_arg {
7541 struct got_worktree *worktree;
7542 const char *branch_name;
7545 static const struct got_error *
7546 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7547 void *arg)
7549 struct merge_commit_msg_arg *a = arg;
7551 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7552 got_worktree_get_head_ref_name(a->worktree)) == -1)
7553 return got_error_from_errno("asprintf");
7555 return NULL;
7559 const struct got_error *
7560 got_worktree_merge_branch(struct got_worktree *worktree,
7561 struct got_fileindex *fileindex,
7562 struct got_object_id *yca_commit_id,
7563 struct got_object_id *branch_tip,
7564 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7565 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7567 const struct got_error *err;
7568 char *fileindex_path = NULL;
7570 err = get_fileindex_path(&fileindex_path, worktree);
7571 if (err)
7572 goto done;
7574 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7575 worktree);
7576 if (err)
7577 goto done;
7579 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7580 branch_tip, repo, progress_cb, progress_arg,
7581 cancel_cb, cancel_arg);
7582 done:
7583 free(fileindex_path);
7584 return err;
7587 const struct got_error *
7588 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7589 struct got_worktree *worktree, struct got_fileindex *fileindex,
7590 const char *author, const char *committer, int allow_bad_symlinks,
7591 struct got_object_id *branch_tip, const char *branch_name,
7592 struct got_repository *repo,
7593 got_worktree_status_cb status_cb, void *status_arg)
7596 const struct got_error *err = NULL, *sync_err;
7597 struct got_pathlist_head commitable_paths;
7598 struct collect_commitables_arg cc_arg;
7599 struct got_pathlist_entry *pe;
7600 struct got_reference *head_ref = NULL;
7601 struct got_object_id *head_commit_id = NULL;
7602 int have_staged_files = 0;
7603 struct merge_commit_msg_arg mcm_arg;
7604 char *fileindex_path = NULL;
7606 *new_commit_id = NULL;
7608 TAILQ_INIT(&commitable_paths);
7610 err = get_fileindex_path(&fileindex_path, worktree);
7611 if (err)
7612 goto done;
7614 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7615 if (err)
7616 goto done;
7618 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7619 if (err)
7620 goto done;
7622 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7623 &have_staged_files);
7624 if (err && err->code != GOT_ERR_CANCELLED)
7625 goto done;
7626 if (have_staged_files) {
7627 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7628 goto done;
7631 cc_arg.commitable_paths = &commitable_paths;
7632 cc_arg.worktree = worktree;
7633 cc_arg.fileindex = fileindex;
7634 cc_arg.repo = repo;
7635 cc_arg.have_staged_files = have_staged_files;
7636 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7637 err = worktree_status(worktree, "", fileindex, repo,
7638 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7639 if (err)
7640 goto done;
7642 if (TAILQ_EMPTY(&commitable_paths)) {
7643 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7644 "merge of %s cannot proceed", branch_name);
7645 goto done;
7648 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7649 struct got_commitable *ct = pe->data;
7650 const char *ct_path = ct->in_repo_path;
7652 while (ct_path[0] == '/')
7653 ct_path++;
7654 err = check_out_of_date(ct_path, ct->status,
7655 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7656 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7657 if (err)
7658 goto done;
7662 mcm_arg.worktree = worktree;
7663 mcm_arg.branch_name = branch_name;
7664 err = commit_worktree(new_commit_id, &commitable_paths,
7665 head_commit_id, branch_tip, worktree, author, committer,
7666 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7667 if (err)
7668 goto done;
7670 err = update_fileindex_after_commit(worktree, &commitable_paths,
7671 *new_commit_id, fileindex, have_staged_files);
7672 sync_err = sync_fileindex(fileindex, fileindex_path);
7673 if (sync_err && err == NULL)
7674 err = sync_err;
7675 done:
7676 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7677 struct got_commitable *ct = pe->data;
7678 free_commitable(ct);
7680 got_pathlist_free(&commitable_paths);
7681 free(fileindex_path);
7682 return err;
7685 const struct got_error *
7686 got_worktree_merge_complete(struct got_worktree *worktree,
7687 struct got_fileindex *fileindex, struct got_repository *repo)
7689 const struct got_error *err, *unlockerr, *sync_err;
7690 char *fileindex_path = NULL;
7692 err = delete_merge_refs(worktree, repo);
7693 if (err)
7694 goto done;
7696 err = get_fileindex_path(&fileindex_path, worktree);
7697 if (err)
7698 goto done;
7699 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7700 sync_err = sync_fileindex(fileindex, fileindex_path);
7701 if (sync_err && err == NULL)
7702 err = sync_err;
7703 done:
7704 got_fileindex_free(fileindex);
7705 free(fileindex_path);
7706 unlockerr = lock_worktree(worktree, LOCK_SH);
7707 if (unlockerr && err == NULL)
7708 err = unlockerr;
7709 return err;
7712 const struct got_error *
7713 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7714 struct got_repository *repo)
7716 const struct got_error *err;
7717 char *branch_refname = NULL;
7718 struct got_reference *branch_ref = NULL;
7720 *in_progress = 0;
7722 err = get_merge_branch_ref_name(&branch_refname, worktree);
7723 if (err)
7724 return err;
7725 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7726 free(branch_refname);
7727 if (err) {
7728 if (err->code != GOT_ERR_NOT_REF)
7729 return err;
7730 } else
7731 *in_progress = 1;
7733 return NULL;
7736 const struct got_error *got_worktree_merge_prepare(
7737 struct got_fileindex **fileindex, struct got_worktree *worktree,
7738 struct got_reference *branch, struct got_repository *repo)
7740 const struct got_error *err = NULL;
7741 char *fileindex_path = NULL;
7742 char *branch_refname = NULL, *commit_refname = NULL;
7743 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7744 struct got_reference *commit_ref = NULL;
7745 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7746 struct check_rebase_ok_arg ok_arg;
7748 *fileindex = NULL;
7750 err = lock_worktree(worktree, LOCK_EX);
7751 if (err)
7752 return err;
7754 err = open_fileindex(fileindex, &fileindex_path, worktree);
7755 if (err)
7756 goto done;
7758 /* Preconditions are the same as for rebase. */
7759 ok_arg.worktree = worktree;
7760 ok_arg.repo = repo;
7761 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7762 &ok_arg);
7763 if (err)
7764 goto done;
7766 err = get_merge_branch_ref_name(&branch_refname, worktree);
7767 if (err)
7768 return err;
7770 err = get_merge_commit_ref_name(&commit_refname, worktree);
7771 if (err)
7772 return err;
7774 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7775 0);
7776 if (err)
7777 goto done;
7779 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7780 if (err)
7781 goto done;
7783 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7784 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7785 goto done;
7788 err = got_ref_resolve(&branch_tip, repo, branch);
7789 if (err)
7790 goto done;
7792 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7793 if (err)
7794 goto done;
7795 err = got_ref_write(branch_ref, repo);
7796 if (err)
7797 goto done;
7799 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7800 if (err)
7801 goto done;
7802 err = got_ref_write(commit_ref, repo);
7803 if (err)
7804 goto done;
7806 done:
7807 free(branch_refname);
7808 free(commit_refname);
7809 free(fileindex_path);
7810 if (branch_ref)
7811 got_ref_close(branch_ref);
7812 if (commit_ref)
7813 got_ref_close(commit_ref);
7814 if (wt_branch)
7815 got_ref_close(wt_branch);
7816 free(wt_branch_tip);
7817 if (err) {
7818 if (*fileindex) {
7819 got_fileindex_free(*fileindex);
7820 *fileindex = NULL;
7822 lock_worktree(worktree, LOCK_SH);
7824 return err;
7827 const struct got_error *
7828 got_worktree_merge_continue(char **branch_name,
7829 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7830 struct got_worktree *worktree, struct got_repository *repo)
7832 const struct got_error *err;
7833 char *commit_refname = NULL, *branch_refname = NULL;
7834 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7835 char *fileindex_path = NULL;
7836 int have_staged_files = 0;
7838 *branch_name = NULL;
7839 *branch_tip = NULL;
7840 *fileindex = NULL;
7842 err = lock_worktree(worktree, LOCK_EX);
7843 if (err)
7844 return err;
7846 err = open_fileindex(fileindex, &fileindex_path, worktree);
7847 if (err)
7848 goto done;
7850 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7851 &have_staged_files);
7852 if (err && err->code != GOT_ERR_CANCELLED)
7853 goto done;
7854 if (have_staged_files) {
7855 err = got_error(GOT_ERR_STAGED_PATHS);
7856 goto done;
7859 err = get_merge_branch_ref_name(&branch_refname, worktree);
7860 if (err)
7861 goto done;
7863 err = get_merge_commit_ref_name(&commit_refname, worktree);
7864 if (err)
7865 goto done;
7867 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7868 if (err)
7869 goto done;
7871 if (!got_ref_is_symbolic(branch_ref)) {
7872 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7873 "%s is not a symbolic reference",
7874 got_ref_get_name(branch_ref));
7875 goto done;
7877 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7878 if (*branch_name == NULL) {
7879 err = got_error_from_errno("strdup");
7880 goto done;
7883 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7884 if (err)
7885 goto done;
7887 err = got_ref_resolve(branch_tip, repo, commit_ref);
7888 if (err)
7889 goto done;
7890 done:
7891 free(commit_refname);
7892 free(branch_refname);
7893 free(fileindex_path);
7894 if (commit_ref)
7895 got_ref_close(commit_ref);
7896 if (branch_ref)
7897 got_ref_close(branch_ref);
7898 if (err) {
7899 if (*branch_name) {
7900 free(*branch_name);
7901 *branch_name = NULL;
7903 free(*branch_tip);
7904 *branch_tip = NULL;
7905 if (*fileindex) {
7906 got_fileindex_free(*fileindex);
7907 *fileindex = NULL;
7909 lock_worktree(worktree, LOCK_SH);
7911 return err;
7914 const struct got_error *
7915 got_worktree_merge_abort(struct got_worktree *worktree,
7916 struct got_fileindex *fileindex, struct got_repository *repo,
7917 got_worktree_checkout_cb progress_cb, void *progress_arg)
7919 const struct got_error *err, *unlockerr, *sync_err;
7920 struct got_object_id *commit_id = NULL;
7921 struct got_commit_object *commit = NULL;
7922 char *fileindex_path = NULL;
7923 struct revert_file_args rfa;
7924 struct got_object_id *tree_id = NULL;
7926 err = got_object_open_as_commit(&commit, repo,
7927 worktree->base_commit_id);
7928 if (err)
7929 goto done;
7931 err = got_object_id_by_path(&tree_id, repo, commit,
7932 worktree->path_prefix);
7933 if (err)
7934 goto done;
7936 err = delete_merge_refs(worktree, repo);
7937 if (err)
7938 goto done;
7940 err = get_fileindex_path(&fileindex_path, worktree);
7941 if (err)
7942 goto done;
7944 rfa.worktree = worktree;
7945 rfa.fileindex = fileindex;
7946 rfa.progress_cb = progress_cb;
7947 rfa.progress_arg = progress_arg;
7948 rfa.patch_cb = NULL;
7949 rfa.patch_arg = NULL;
7950 rfa.repo = repo;
7951 rfa.unlink_added_files = 1;
7952 err = worktree_status(worktree, "", fileindex, repo,
7953 revert_file, &rfa, NULL, NULL, 1, 0);
7954 if (err)
7955 goto sync;
7957 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7958 repo, progress_cb, progress_arg, NULL, NULL);
7959 sync:
7960 sync_err = sync_fileindex(fileindex, fileindex_path);
7961 if (sync_err && err == NULL)
7962 err = sync_err;
7963 done:
7964 free(tree_id);
7965 free(commit_id);
7966 if (commit)
7967 got_object_commit_close(commit);
7968 if (fileindex)
7969 got_fileindex_free(fileindex);
7970 free(fileindex_path);
7972 unlockerr = lock_worktree(worktree, LOCK_SH);
7973 if (unlockerr && err == NULL)
7974 err = unlockerr;
7975 return err;
7978 struct check_stage_ok_arg {
7979 struct got_object_id *head_commit_id;
7980 struct got_worktree *worktree;
7981 struct got_fileindex *fileindex;
7982 struct got_repository *repo;
7983 int have_changes;
7986 static const struct got_error *
7987 check_stage_ok(void *arg, unsigned char status,
7988 unsigned char staged_status, const char *relpath,
7989 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7990 struct got_object_id *commit_id, int dirfd, const char *de_name)
7992 struct check_stage_ok_arg *a = arg;
7993 const struct got_error *err = NULL;
7994 struct got_fileindex_entry *ie;
7995 struct got_object_id base_commit_id;
7996 struct got_object_id *base_commit_idp = NULL;
7997 char *in_repo_path = NULL, *p;
7999 if (status == GOT_STATUS_UNVERSIONED ||
8000 status == GOT_STATUS_NO_CHANGE)
8001 return NULL;
8002 if (status == GOT_STATUS_NONEXISTENT)
8003 return got_error_set_errno(ENOENT, relpath);
8005 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8006 if (ie == NULL)
8007 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8009 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8010 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8011 relpath) == -1)
8012 return got_error_from_errno("asprintf");
8014 if (got_fileindex_entry_has_commit(ie)) {
8015 memcpy(base_commit_id.sha1, ie->commit_sha1,
8016 SHA1_DIGEST_LENGTH);
8017 base_commit_idp = &base_commit_id;
8020 if (status == GOT_STATUS_CONFLICT) {
8021 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8022 goto done;
8023 } else if (status != GOT_STATUS_ADD &&
8024 status != GOT_STATUS_MODIFY &&
8025 status != GOT_STATUS_DELETE) {
8026 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8027 goto done;
8030 a->have_changes = 1;
8032 p = in_repo_path;
8033 while (p[0] == '/')
8034 p++;
8035 err = check_out_of_date(p, status, staged_status,
8036 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8037 GOT_ERR_STAGE_OUT_OF_DATE);
8038 done:
8039 free(in_repo_path);
8040 return err;
8043 struct stage_path_arg {
8044 struct got_worktree *worktree;
8045 struct got_fileindex *fileindex;
8046 struct got_repository *repo;
8047 got_worktree_status_cb status_cb;
8048 void *status_arg;
8049 got_worktree_patch_cb patch_cb;
8050 void *patch_arg;
8051 int staged_something;
8052 int allow_bad_symlinks;
8055 static const struct got_error *
8056 stage_path(void *arg, unsigned char status,
8057 unsigned char staged_status, const char *relpath,
8058 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8059 struct got_object_id *commit_id, int dirfd, const char *de_name)
8061 struct stage_path_arg *a = arg;
8062 const struct got_error *err = NULL;
8063 struct got_fileindex_entry *ie;
8064 char *ondisk_path = NULL, *path_content = NULL;
8065 uint32_t stage;
8066 struct got_object_id *new_staged_blob_id = NULL;
8067 struct stat sb;
8069 if (status == GOT_STATUS_UNVERSIONED)
8070 return NULL;
8072 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8073 if (ie == NULL)
8074 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8076 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8077 relpath)== -1)
8078 return got_error_from_errno("asprintf");
8080 switch (status) {
8081 case GOT_STATUS_ADD:
8082 case GOT_STATUS_MODIFY:
8083 /* XXX could sb.st_mode be passed in by our caller? */
8084 if (lstat(ondisk_path, &sb) == -1) {
8085 err = got_error_from_errno2("lstat", ondisk_path);
8086 break;
8088 if (a->patch_cb) {
8089 if (status == GOT_STATUS_ADD) {
8090 int choice = GOT_PATCH_CHOICE_NONE;
8091 err = (*a->patch_cb)(&choice, a->patch_arg,
8092 status, ie->path, NULL, 1, 1);
8093 if (err)
8094 break;
8095 if (choice != GOT_PATCH_CHOICE_YES)
8096 break;
8097 } else {
8098 err = create_patched_content(&path_content, 0,
8099 staged_blob_id ? staged_blob_id : blob_id,
8100 ondisk_path, dirfd, de_name, ie->path,
8101 a->repo, a->patch_cb, a->patch_arg);
8102 if (err || path_content == NULL)
8103 break;
8106 err = got_object_blob_create(&new_staged_blob_id,
8107 path_content ? path_content : ondisk_path, a->repo);
8108 if (err)
8109 break;
8110 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8111 SHA1_DIGEST_LENGTH);
8112 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8113 stage = GOT_FILEIDX_STAGE_ADD;
8114 else
8115 stage = GOT_FILEIDX_STAGE_MODIFY;
8116 got_fileindex_entry_stage_set(ie, stage);
8117 if (S_ISLNK(sb.st_mode)) {
8118 int is_bad_symlink = 0;
8119 if (!a->allow_bad_symlinks) {
8120 char target_path[PATH_MAX];
8121 ssize_t target_len;
8122 target_len = readlink(ondisk_path, target_path,
8123 sizeof(target_path));
8124 if (target_len == -1) {
8125 err = got_error_from_errno2("readlink",
8126 ondisk_path);
8127 break;
8129 err = is_bad_symlink_target(&is_bad_symlink,
8130 target_path, target_len, ondisk_path,
8131 a->worktree->root_path);
8132 if (err)
8133 break;
8134 if (is_bad_symlink) {
8135 err = got_error_path(ondisk_path,
8136 GOT_ERR_BAD_SYMLINK);
8137 break;
8140 if (is_bad_symlink)
8141 got_fileindex_entry_staged_filetype_set(ie,
8142 GOT_FILEIDX_MODE_BAD_SYMLINK);
8143 else
8144 got_fileindex_entry_staged_filetype_set(ie,
8145 GOT_FILEIDX_MODE_SYMLINK);
8146 } else {
8147 got_fileindex_entry_staged_filetype_set(ie,
8148 GOT_FILEIDX_MODE_REGULAR_FILE);
8150 a->staged_something = 1;
8151 if (a->status_cb == NULL)
8152 break;
8153 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8154 get_staged_status(ie), relpath, blob_id,
8155 new_staged_blob_id, NULL, dirfd, de_name);
8156 if (err)
8157 break;
8159 * When staging the reverse of the staged diff,
8160 * implicitly unstage the file.
8162 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8163 sizeof(ie->blob_sha1)) == 0) {
8164 got_fileindex_entry_stage_set(ie,
8165 GOT_FILEIDX_STAGE_NONE);
8167 break;
8168 case GOT_STATUS_DELETE:
8169 if (staged_status == GOT_STATUS_DELETE)
8170 break;
8171 if (a->patch_cb) {
8172 int choice = GOT_PATCH_CHOICE_NONE;
8173 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8174 ie->path, NULL, 1, 1);
8175 if (err)
8176 break;
8177 if (choice == GOT_PATCH_CHOICE_NO)
8178 break;
8179 if (choice != GOT_PATCH_CHOICE_YES) {
8180 err = got_error(GOT_ERR_PATCH_CHOICE);
8181 break;
8184 stage = GOT_FILEIDX_STAGE_DELETE;
8185 got_fileindex_entry_stage_set(ie, stage);
8186 a->staged_something = 1;
8187 if (a->status_cb == NULL)
8188 break;
8189 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8190 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8191 de_name);
8192 break;
8193 case GOT_STATUS_NO_CHANGE:
8194 break;
8195 case GOT_STATUS_CONFLICT:
8196 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8197 break;
8198 case GOT_STATUS_NONEXISTENT:
8199 err = got_error_set_errno(ENOENT, relpath);
8200 break;
8201 default:
8202 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8203 break;
8206 if (path_content && unlink(path_content) == -1 && err == NULL)
8207 err = got_error_from_errno2("unlink", path_content);
8208 free(path_content);
8209 free(ondisk_path);
8210 free(new_staged_blob_id);
8211 return err;
8214 const struct got_error *
8215 got_worktree_stage(struct got_worktree *worktree,
8216 struct got_pathlist_head *paths,
8217 got_worktree_status_cb status_cb, void *status_arg,
8218 got_worktree_patch_cb patch_cb, void *patch_arg,
8219 int allow_bad_symlinks, struct got_repository *repo)
8221 const struct got_error *err = NULL, *sync_err, *unlockerr;
8222 struct got_pathlist_entry *pe;
8223 struct got_fileindex *fileindex = NULL;
8224 char *fileindex_path = NULL;
8225 struct got_reference *head_ref = NULL;
8226 struct got_object_id *head_commit_id = NULL;
8227 struct check_stage_ok_arg oka;
8228 struct stage_path_arg spa;
8230 err = lock_worktree(worktree, LOCK_EX);
8231 if (err)
8232 return err;
8234 err = got_ref_open(&head_ref, repo,
8235 got_worktree_get_head_ref_name(worktree), 0);
8236 if (err)
8237 goto done;
8238 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8239 if (err)
8240 goto done;
8241 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8242 if (err)
8243 goto done;
8245 /* Check pre-conditions before staging anything. */
8246 oka.head_commit_id = head_commit_id;
8247 oka.worktree = worktree;
8248 oka.fileindex = fileindex;
8249 oka.repo = repo;
8250 oka.have_changes = 0;
8251 TAILQ_FOREACH(pe, paths, entry) {
8252 err = worktree_status(worktree, pe->path, fileindex, repo,
8253 check_stage_ok, &oka, NULL, NULL, 1, 0);
8254 if (err)
8255 goto done;
8257 if (!oka.have_changes) {
8258 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8259 goto done;
8262 spa.worktree = worktree;
8263 spa.fileindex = fileindex;
8264 spa.repo = repo;
8265 spa.patch_cb = patch_cb;
8266 spa.patch_arg = patch_arg;
8267 spa.status_cb = status_cb;
8268 spa.status_arg = status_arg;
8269 spa.staged_something = 0;
8270 spa.allow_bad_symlinks = allow_bad_symlinks;
8271 TAILQ_FOREACH(pe, paths, entry) {
8272 err = worktree_status(worktree, pe->path, fileindex, repo,
8273 stage_path, &spa, NULL, NULL, 1, 0);
8274 if (err)
8275 goto done;
8277 if (!spa.staged_something) {
8278 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8279 goto done;
8282 sync_err = sync_fileindex(fileindex, fileindex_path);
8283 if (sync_err && err == NULL)
8284 err = sync_err;
8285 done:
8286 if (head_ref)
8287 got_ref_close(head_ref);
8288 free(head_commit_id);
8289 free(fileindex_path);
8290 if (fileindex)
8291 got_fileindex_free(fileindex);
8292 unlockerr = lock_worktree(worktree, LOCK_SH);
8293 if (unlockerr && err == NULL)
8294 err = unlockerr;
8295 return err;
8298 struct unstage_path_arg {
8299 struct got_worktree *worktree;
8300 struct got_fileindex *fileindex;
8301 struct got_repository *repo;
8302 got_worktree_checkout_cb progress_cb;
8303 void *progress_arg;
8304 got_worktree_patch_cb patch_cb;
8305 void *patch_arg;
8308 static const struct got_error *
8309 create_unstaged_content(char **path_unstaged_content,
8310 char **path_new_staged_content, struct got_object_id *blob_id,
8311 struct got_object_id *staged_blob_id, const char *relpath,
8312 struct got_repository *repo,
8313 got_worktree_patch_cb patch_cb, void *patch_arg)
8315 const struct got_error *err, *free_err;
8316 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8317 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8318 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8319 struct got_diffreg_result *diffreg_result = NULL;
8320 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8321 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8322 int fd1 = -1, fd2 = -1;
8324 *path_unstaged_content = NULL;
8325 *path_new_staged_content = NULL;
8327 err = got_object_id_str(&label1, blob_id);
8328 if (err)
8329 return err;
8331 fd1 = got_opentempfd();
8332 if (fd1 == -1) {
8333 err = got_error_from_errno("got_opentempfd");
8334 goto done;
8336 fd2 = got_opentempfd();
8337 if (fd2 == -1) {
8338 err = got_error_from_errno("got_opentempfd");
8339 goto done;
8342 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8343 if (err)
8344 goto done;
8346 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8347 if (err)
8348 goto done;
8350 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8351 if (err)
8352 goto done;
8354 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8355 fd2);
8356 if (err)
8357 goto done;
8359 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8360 if (err)
8361 goto done;
8363 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8364 if (err)
8365 goto done;
8367 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8368 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8369 if (err)
8370 goto done;
8372 err = got_opentemp_named(path_unstaged_content, &outfile,
8373 "got-unstaged-content");
8374 if (err)
8375 goto done;
8376 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8377 "got-new-staged-content");
8378 if (err)
8379 goto done;
8381 if (fseek(f1, 0L, SEEK_SET) == -1) {
8382 err = got_ferror(f1, GOT_ERR_IO);
8383 goto done;
8385 if (fseek(f2, 0L, SEEK_SET) == -1) {
8386 err = got_ferror(f2, GOT_ERR_IO);
8387 goto done;
8389 /* Count the number of actual changes in the diff result. */
8390 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8391 struct diff_chunk_context cc = {};
8392 diff_chunk_context_load_change(&cc, &nchunks_used,
8393 diffreg_result->result, n, 0);
8394 nchanges++;
8396 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8397 int choice;
8398 err = apply_or_reject_change(&choice, &nchunks_used,
8399 diffreg_result->result, n, relpath, f1, f2,
8400 &line_cur1, &line_cur2,
8401 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8402 if (err)
8403 goto done;
8404 if (choice == GOT_PATCH_CHOICE_YES)
8405 have_content = 1;
8406 else
8407 have_rejected_content = 1;
8408 if (choice == GOT_PATCH_CHOICE_QUIT)
8409 break;
8411 if (have_content || have_rejected_content)
8412 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8413 outfile, rejectfile);
8414 done:
8415 free(label1);
8416 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8417 err = got_error_from_errno("close");
8418 if (blob)
8419 got_object_blob_close(blob);
8420 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8421 err = got_error_from_errno("close");
8422 if (staged_blob)
8423 got_object_blob_close(staged_blob);
8424 free_err = got_diffreg_result_free(diffreg_result);
8425 if (free_err && err == NULL)
8426 err = free_err;
8427 if (f1 && fclose(f1) == EOF && err == NULL)
8428 err = got_error_from_errno2("fclose", path1);
8429 if (f2 && fclose(f2) == EOF && err == NULL)
8430 err = got_error_from_errno2("fclose", path2);
8431 if (outfile && fclose(outfile) == EOF && err == NULL)
8432 err = got_error_from_errno2("fclose", *path_unstaged_content);
8433 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8434 err = got_error_from_errno2("fclose", *path_new_staged_content);
8435 if (path1 && unlink(path1) == -1 && err == NULL)
8436 err = got_error_from_errno2("unlink", path1);
8437 if (path2 && unlink(path2) == -1 && err == NULL)
8438 err = got_error_from_errno2("unlink", path2);
8439 if (err || !have_content) {
8440 if (*path_unstaged_content &&
8441 unlink(*path_unstaged_content) == -1 && err == NULL)
8442 err = got_error_from_errno2("unlink",
8443 *path_unstaged_content);
8444 free(*path_unstaged_content);
8445 *path_unstaged_content = NULL;
8447 if (err || !have_content || !have_rejected_content) {
8448 if (*path_new_staged_content &&
8449 unlink(*path_new_staged_content) == -1 && err == NULL)
8450 err = got_error_from_errno2("unlink",
8451 *path_new_staged_content);
8452 free(*path_new_staged_content);
8453 *path_new_staged_content = NULL;
8455 free(path1);
8456 free(path2);
8457 return err;
8460 static const struct got_error *
8461 unstage_hunks(struct got_object_id *staged_blob_id,
8462 struct got_blob_object *blob_base,
8463 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8464 const char *ondisk_path, const char *label_orig,
8465 struct got_worktree *worktree, struct got_repository *repo,
8466 got_worktree_patch_cb patch_cb, void *patch_arg,
8467 got_worktree_checkout_cb progress_cb, void *progress_arg)
8469 const struct got_error *err = NULL;
8470 char *path_unstaged_content = NULL;
8471 char *path_new_staged_content = NULL;
8472 char *parent = NULL, *base_path = NULL;
8473 char *blob_base_path = NULL;
8474 struct got_object_id *new_staged_blob_id = NULL;
8475 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8476 struct stat sb;
8478 err = create_unstaged_content(&path_unstaged_content,
8479 &path_new_staged_content, blob_id, staged_blob_id,
8480 ie->path, repo, patch_cb, patch_arg);
8481 if (err)
8482 return err;
8484 if (path_unstaged_content == NULL)
8485 return NULL;
8487 if (path_new_staged_content) {
8488 err = got_object_blob_create(&new_staged_blob_id,
8489 path_new_staged_content, repo);
8490 if (err)
8491 goto done;
8494 f = fopen(path_unstaged_content, "re");
8495 if (f == NULL) {
8496 err = got_error_from_errno2("fopen",
8497 path_unstaged_content);
8498 goto done;
8500 if (fstat(fileno(f), &sb) == -1) {
8501 err = got_error_from_errno2("fstat", path_unstaged_content);
8502 goto done;
8504 if (got_fileindex_entry_staged_filetype_get(ie) ==
8505 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8506 char link_target[PATH_MAX];
8507 size_t r;
8508 r = fread(link_target, 1, sizeof(link_target), f);
8509 if (r == 0 && ferror(f)) {
8510 err = got_error_from_errno("fread");
8511 goto done;
8513 if (r >= sizeof(link_target)) { /* should not happen */
8514 err = got_error(GOT_ERR_NO_SPACE);
8515 goto done;
8517 link_target[r] = '\0';
8518 err = merge_symlink(worktree, blob_base,
8519 ondisk_path, ie->path, label_orig, link_target,
8520 worktree->base_commit_id, repo, progress_cb,
8521 progress_arg);
8522 } else {
8523 int local_changes_subsumed;
8525 err = got_path_dirname(&parent, ondisk_path);
8526 if (err)
8527 return err;
8529 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8530 parent) == -1) {
8531 err = got_error_from_errno("asprintf");
8532 base_path = NULL;
8533 goto done;
8536 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8537 if (err)
8538 goto done;
8539 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8540 blob_base);
8541 if (err)
8542 goto done;
8545 * In order the run a 3-way merge with a symlink we copy the symlink's
8546 * target path into a temporary file and use that file with diff3.
8548 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8549 err = dump_symlink_target_path_to_file(&f_deriv2,
8550 ondisk_path);
8551 if (err)
8552 goto done;
8553 } else {
8554 int fd;
8555 fd = open(ondisk_path,
8556 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8557 if (fd == -1) {
8558 err = got_error_from_errno2("open", ondisk_path);
8559 goto done;
8561 f_deriv2 = fdopen(fd, "r");
8562 if (f_deriv2 == NULL) {
8563 err = got_error_from_errno2("fdopen", ondisk_path);
8564 close(fd);
8565 goto done;
8569 err = merge_file(&local_changes_subsumed, worktree,
8570 f_base, f, f_deriv2, ondisk_path, ie->path,
8571 got_fileindex_perms_to_st(ie),
8572 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8573 repo, progress_cb, progress_arg);
8575 if (err)
8576 goto done;
8578 if (new_staged_blob_id) {
8579 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8580 SHA1_DIGEST_LENGTH);
8581 } else {
8582 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8583 got_fileindex_entry_staged_filetype_set(ie, 0);
8585 done:
8586 free(new_staged_blob_id);
8587 if (path_unstaged_content &&
8588 unlink(path_unstaged_content) == -1 && err == NULL)
8589 err = got_error_from_errno2("unlink", path_unstaged_content);
8590 if (path_new_staged_content &&
8591 unlink(path_new_staged_content) == -1 && err == NULL)
8592 err = got_error_from_errno2("unlink", path_new_staged_content);
8593 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8594 err = got_error_from_errno2("unlink", blob_base_path);
8595 if (f_base && fclose(f_base) == EOF && err == NULL)
8596 err = got_error_from_errno2("fclose", path_unstaged_content);
8597 if (f && fclose(f) == EOF && err == NULL)
8598 err = got_error_from_errno2("fclose", path_unstaged_content);
8599 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8600 err = got_error_from_errno2("fclose", ondisk_path);
8601 free(path_unstaged_content);
8602 free(path_new_staged_content);
8603 free(blob_base_path);
8604 free(parent);
8605 free(base_path);
8606 return err;
8609 static const struct got_error *
8610 unstage_path(void *arg, unsigned char status,
8611 unsigned char staged_status, const char *relpath,
8612 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8613 struct got_object_id *commit_id, int dirfd, const char *de_name)
8615 const struct got_error *err = NULL;
8616 struct unstage_path_arg *a = arg;
8617 struct got_fileindex_entry *ie;
8618 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8619 char *ondisk_path = NULL;
8620 char *id_str = NULL, *label_orig = NULL;
8621 int local_changes_subsumed;
8622 struct stat sb;
8623 int fd1 = -1, fd2 = -1;
8625 if (staged_status != GOT_STATUS_ADD &&
8626 staged_status != GOT_STATUS_MODIFY &&
8627 staged_status != GOT_STATUS_DELETE)
8628 return NULL;
8630 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8631 if (ie == NULL)
8632 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8634 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8635 == -1)
8636 return got_error_from_errno("asprintf");
8638 err = got_object_id_str(&id_str,
8639 commit_id ? commit_id : a->worktree->base_commit_id);
8640 if (err)
8641 goto done;
8642 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8643 id_str) == -1) {
8644 err = got_error_from_errno("asprintf");
8645 goto done;
8648 fd1 = got_opentempfd();
8649 if (fd1 == -1) {
8650 err = got_error_from_errno("got_opentempfd");
8651 goto done;
8653 fd2 = got_opentempfd();
8654 if (fd2 == -1) {
8655 err = got_error_from_errno("got_opentempfd");
8656 goto done;
8659 switch (staged_status) {
8660 case GOT_STATUS_MODIFY:
8661 err = got_object_open_as_blob(&blob_base, a->repo,
8662 blob_id, 8192, fd1);
8663 if (err)
8664 break;
8665 /* fall through */
8666 case GOT_STATUS_ADD:
8667 if (a->patch_cb) {
8668 if (staged_status == GOT_STATUS_ADD) {
8669 int choice = GOT_PATCH_CHOICE_NONE;
8670 err = (*a->patch_cb)(&choice, a->patch_arg,
8671 staged_status, ie->path, NULL, 1, 1);
8672 if (err)
8673 break;
8674 if (choice != GOT_PATCH_CHOICE_YES)
8675 break;
8676 } else {
8677 err = unstage_hunks(staged_blob_id,
8678 blob_base, blob_id, ie, ondisk_path,
8679 label_orig, a->worktree, a->repo,
8680 a->patch_cb, a->patch_arg,
8681 a->progress_cb, a->progress_arg);
8682 break; /* Done with this file. */
8685 err = got_object_open_as_blob(&blob_staged, a->repo,
8686 staged_blob_id, 8192, fd2);
8687 if (err)
8688 break;
8689 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8690 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8691 case GOT_FILEIDX_MODE_REGULAR_FILE:
8692 err = merge_blob(&local_changes_subsumed, a->worktree,
8693 blob_base, ondisk_path, relpath,
8694 got_fileindex_perms_to_st(ie), label_orig,
8695 blob_staged, commit_id ? commit_id :
8696 a->worktree->base_commit_id, a->repo,
8697 a->progress_cb, a->progress_arg);
8698 break;
8699 case GOT_FILEIDX_MODE_SYMLINK:
8700 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8701 char *staged_target;
8702 err = got_object_blob_read_to_str(
8703 &staged_target, blob_staged);
8704 if (err)
8705 goto done;
8706 err = merge_symlink(a->worktree, blob_base,
8707 ondisk_path, relpath, label_orig,
8708 staged_target, commit_id ? commit_id :
8709 a->worktree->base_commit_id,
8710 a->repo, a->progress_cb, a->progress_arg);
8711 free(staged_target);
8712 } else {
8713 err = merge_blob(&local_changes_subsumed,
8714 a->worktree, blob_base, ondisk_path,
8715 relpath, got_fileindex_perms_to_st(ie),
8716 label_orig, blob_staged,
8717 commit_id ? commit_id :
8718 a->worktree->base_commit_id, a->repo,
8719 a->progress_cb, a->progress_arg);
8721 break;
8722 default:
8723 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8724 break;
8726 if (err == NULL) {
8727 got_fileindex_entry_stage_set(ie,
8728 GOT_FILEIDX_STAGE_NONE);
8729 got_fileindex_entry_staged_filetype_set(ie, 0);
8731 break;
8732 case GOT_STATUS_DELETE:
8733 if (a->patch_cb) {
8734 int choice = GOT_PATCH_CHOICE_NONE;
8735 err = (*a->patch_cb)(&choice, a->patch_arg,
8736 staged_status, ie->path, NULL, 1, 1);
8737 if (err)
8738 break;
8739 if (choice == GOT_PATCH_CHOICE_NO)
8740 break;
8741 if (choice != GOT_PATCH_CHOICE_YES) {
8742 err = got_error(GOT_ERR_PATCH_CHOICE);
8743 break;
8746 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8747 got_fileindex_entry_staged_filetype_set(ie, 0);
8748 err = get_file_status(&status, &sb, ie, ondisk_path,
8749 dirfd, de_name, a->repo);
8750 if (err)
8751 break;
8752 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8753 break;
8755 done:
8756 free(ondisk_path);
8757 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8758 err = got_error_from_errno("close");
8759 if (blob_base)
8760 got_object_blob_close(blob_base);
8761 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8762 err = got_error_from_errno("close");
8763 if (blob_staged)
8764 got_object_blob_close(blob_staged);
8765 free(id_str);
8766 free(label_orig);
8767 return err;
8770 const struct got_error *
8771 got_worktree_unstage(struct got_worktree *worktree,
8772 struct got_pathlist_head *paths,
8773 got_worktree_checkout_cb progress_cb, void *progress_arg,
8774 got_worktree_patch_cb patch_cb, void *patch_arg,
8775 struct got_repository *repo)
8777 const struct got_error *err = NULL, *sync_err, *unlockerr;
8778 struct got_pathlist_entry *pe;
8779 struct got_fileindex *fileindex = NULL;
8780 char *fileindex_path = NULL;
8781 struct unstage_path_arg upa;
8783 err = lock_worktree(worktree, LOCK_EX);
8784 if (err)
8785 return err;
8787 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8788 if (err)
8789 goto done;
8791 upa.worktree = worktree;
8792 upa.fileindex = fileindex;
8793 upa.repo = repo;
8794 upa.progress_cb = progress_cb;
8795 upa.progress_arg = progress_arg;
8796 upa.patch_cb = patch_cb;
8797 upa.patch_arg = patch_arg;
8798 TAILQ_FOREACH(pe, paths, entry) {
8799 err = worktree_status(worktree, pe->path, fileindex, repo,
8800 unstage_path, &upa, NULL, NULL, 1, 0);
8801 if (err)
8802 goto done;
8805 sync_err = sync_fileindex(fileindex, fileindex_path);
8806 if (sync_err && err == NULL)
8807 err = sync_err;
8808 done:
8809 free(fileindex_path);
8810 if (fileindex)
8811 got_fileindex_free(fileindex);
8812 unlockerr = lock_worktree(worktree, LOCK_SH);
8813 if (unlockerr && err == NULL)
8814 err = unlockerr;
8815 return err;
8818 struct report_file_info_arg {
8819 struct got_worktree *worktree;
8820 got_worktree_path_info_cb info_cb;
8821 void *info_arg;
8822 struct got_pathlist_head *paths;
8823 got_cancel_cb cancel_cb;
8824 void *cancel_arg;
8827 static const struct got_error *
8828 report_file_info(void *arg, struct got_fileindex_entry *ie)
8830 struct report_file_info_arg *a = arg;
8831 struct got_pathlist_entry *pe;
8832 struct got_object_id blob_id, staged_blob_id, commit_id;
8833 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8834 struct got_object_id *commit_idp = NULL;
8835 int stage;
8837 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8838 return got_error(GOT_ERR_CANCELLED);
8840 TAILQ_FOREACH(pe, a->paths, entry) {
8841 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8842 got_path_is_child(ie->path, pe->path, pe->path_len))
8843 break;
8845 if (pe == NULL) /* not found */
8846 return NULL;
8848 if (got_fileindex_entry_has_blob(ie)) {
8849 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8850 blob_idp = &blob_id;
8852 stage = got_fileindex_entry_stage_get(ie);
8853 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8854 stage == GOT_FILEIDX_STAGE_ADD) {
8855 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8856 SHA1_DIGEST_LENGTH);
8857 staged_blob_idp = &staged_blob_id;
8860 if (got_fileindex_entry_has_commit(ie)) {
8861 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8862 commit_idp = &commit_id;
8865 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8866 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8869 const struct got_error *
8870 got_worktree_path_info(struct got_worktree *worktree,
8871 struct got_pathlist_head *paths,
8872 got_worktree_path_info_cb info_cb, void *info_arg,
8873 got_cancel_cb cancel_cb, void *cancel_arg)
8876 const struct got_error *err = NULL, *unlockerr;
8877 struct got_fileindex *fileindex = NULL;
8878 char *fileindex_path = NULL;
8879 struct report_file_info_arg arg;
8881 err = lock_worktree(worktree, LOCK_SH);
8882 if (err)
8883 return err;
8885 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8886 if (err)
8887 goto done;
8889 arg.worktree = worktree;
8890 arg.info_cb = info_cb;
8891 arg.info_arg = info_arg;
8892 arg.paths = paths;
8893 arg.cancel_cb = cancel_cb;
8894 arg.cancel_arg = cancel_arg;
8895 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8896 &arg);
8897 done:
8898 free(fileindex_path);
8899 if (fileindex)
8900 got_fileindex_free(fileindex);
8901 unlockerr = lock_worktree(worktree, LOCK_UN);
8902 if (unlockerr && err == NULL)
8903 err = unlockerr;
8904 return err;
8907 static const struct got_error *
8908 patch_check_path(const char *p, char **path, unsigned char *status,
8909 unsigned char *staged_status, struct got_fileindex *fileindex,
8910 struct got_worktree *worktree, struct got_repository *repo)
8912 const struct got_error *err;
8913 struct got_fileindex_entry *ie;
8914 struct stat sb;
8915 char *ondisk_path = NULL;
8917 err = got_worktree_resolve_path(path, worktree, p);
8918 if (err)
8919 return err;
8921 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8922 *path[0] ? "/" : "", *path) == -1)
8923 return got_error_from_errno("asprintf");
8925 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8926 if (ie) {
8927 *staged_status = get_staged_status(ie);
8928 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8929 repo);
8930 if (err)
8931 goto done;
8932 } else {
8933 *staged_status = GOT_STATUS_NO_CHANGE;
8934 *status = GOT_STATUS_UNVERSIONED;
8935 if (lstat(ondisk_path, &sb) == -1) {
8936 if (errno != ENOENT) {
8937 err = got_error_from_errno2("lstat",
8938 ondisk_path);
8939 goto done;
8941 *status = GOT_STATUS_NONEXISTENT;
8945 done:
8946 free(ondisk_path);
8947 return err;
8950 static const struct got_error *
8951 patch_can_rm(const char *path, unsigned char status,
8952 unsigned char staged_status)
8954 if (status == GOT_STATUS_NONEXISTENT)
8955 return got_error_set_errno(ENOENT, path);
8956 if (status != GOT_STATUS_NO_CHANGE &&
8957 status != GOT_STATUS_ADD &&
8958 status != GOT_STATUS_MODIFY &&
8959 status != GOT_STATUS_MODE_CHANGE)
8960 return got_error_path(path, GOT_ERR_FILE_STATUS);
8961 if (staged_status == GOT_STATUS_DELETE)
8962 return got_error_path(path, GOT_ERR_FILE_STATUS);
8963 return NULL;
8966 static const struct got_error *
8967 patch_can_add(const char *path, unsigned char status)
8969 if (status != GOT_STATUS_NONEXISTENT)
8970 return got_error_path(path, GOT_ERR_FILE_STATUS);
8971 return NULL;
8974 static const struct got_error *
8975 patch_can_edit(const char *path, unsigned char status,
8976 unsigned char staged_status)
8978 if (status == GOT_STATUS_NONEXISTENT)
8979 return got_error_set_errno(ENOENT, path);
8980 if (status != GOT_STATUS_NO_CHANGE &&
8981 status != GOT_STATUS_ADD &&
8982 status != GOT_STATUS_MODIFY)
8983 return got_error_path(path, GOT_ERR_FILE_STATUS);
8984 if (staged_status == GOT_STATUS_DELETE)
8985 return got_error_path(path, GOT_ERR_FILE_STATUS);
8986 return NULL;
8989 const struct got_error *
8990 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8991 char **fileindex_path, struct got_worktree *worktree)
8993 return open_fileindex(fileindex, fileindex_path, worktree);
8996 const struct got_error *
8997 got_worktree_patch_check_path(const char *old, const char *new,
8998 char **oldpath, char **newpath, struct got_worktree *worktree,
8999 struct got_repository *repo, struct got_fileindex *fileindex)
9001 const struct got_error *err = NULL;
9002 int file_renamed = 0;
9003 unsigned char status_old, staged_status_old;
9004 unsigned char status_new, staged_status_new;
9006 *oldpath = NULL;
9007 *newpath = NULL;
9009 err = patch_check_path(old != NULL ? old : new, oldpath,
9010 &status_old, &staged_status_old, fileindex, worktree, repo);
9011 if (err)
9012 goto done;
9014 err = patch_check_path(new != NULL ? new : old, newpath,
9015 &status_new, &staged_status_new, fileindex, worktree, repo);
9016 if (err)
9017 goto done;
9019 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9020 file_renamed = 1;
9022 if (old != NULL && new == NULL)
9023 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9024 else if (file_renamed) {
9025 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9026 if (err == NULL)
9027 err = patch_can_add(*newpath, status_new);
9028 } else if (old == NULL)
9029 err = patch_can_add(*newpath, status_new);
9030 else
9031 err = patch_can_edit(*newpath, status_new, staged_status_new);
9033 done:
9034 if (err) {
9035 free(*oldpath);
9036 *oldpath = NULL;
9037 free(*newpath);
9038 *newpath = NULL;
9040 return err;
9043 const struct got_error *
9044 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9045 struct got_worktree *worktree, struct got_fileindex *fileindex,
9046 got_worktree_checkout_cb progress_cb, void *progress_arg)
9048 struct schedule_addition_args saa;
9050 memset(&saa, 0, sizeof(saa));
9051 saa.worktree = worktree;
9052 saa.fileindex = fileindex;
9053 saa.progress_cb = progress_cb;
9054 saa.progress_arg = progress_arg;
9055 saa.repo = repo;
9057 return worktree_status(worktree, path, fileindex, repo,
9058 schedule_addition, &saa, NULL, NULL, 1, 0);
9061 const struct got_error *
9062 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9063 struct got_worktree *worktree, struct got_fileindex *fileindex,
9064 got_worktree_delete_cb progress_cb, void *progress_arg)
9066 struct schedule_deletion_args sda;
9068 memset(&sda, 0, sizeof(sda));
9069 sda.worktree = worktree;
9070 sda.fileindex = fileindex;
9071 sda.progress_cb = progress_cb;
9072 sda.progress_arg = progress_arg;
9073 sda.repo = repo;
9074 sda.delete_local_mods = 0;
9075 sda.keep_on_disk = 0;
9076 sda.ignore_missing_paths = 0;
9077 sda.status_codes = NULL;
9079 return worktree_status(worktree, path, fileindex, repo,
9080 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9083 const struct got_error *
9084 got_worktree_patch_complete(struct got_fileindex *fileindex,
9085 const char *fileindex_path)
9087 const struct got_error *err = NULL;
9089 err = sync_fileindex(fileindex, fileindex_path);
9090 got_fileindex_free(fileindex);
9092 return err;