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, 0, 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 ? committer :
6538 got_object_commit_get_committer(orig_commit),
6539 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6540 if (err)
6541 goto done;
6543 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6544 if (err)
6545 goto done;
6547 err = got_ref_delete(commit_ref, repo);
6548 if (err)
6549 goto done;
6551 err = update_fileindex_after_commit(worktree, &commitable_paths,
6552 *new_commit_id, fileindex, 0);
6553 sync_err = sync_fileindex(fileindex, fileindex_path);
6554 if (sync_err && err == NULL)
6555 err = sync_err;
6556 done:
6557 free(fileindex_path);
6558 free(head_commit_id);
6559 if (head_ref)
6560 got_ref_close(head_ref);
6561 if (err) {
6562 free(*new_commit_id);
6563 *new_commit_id = NULL;
6565 return err;
6568 const struct got_error *
6569 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6570 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6571 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6572 const char *committer, struct got_commit_object *orig_commit,
6573 struct got_object_id *orig_commit_id, struct got_repository *repo)
6575 const struct got_error *err;
6576 char *commit_ref_name;
6577 struct got_reference *commit_ref = NULL;
6578 struct got_object_id *commit_id = NULL;
6580 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6581 if (err)
6582 return err;
6584 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6585 if (err)
6586 goto done;
6587 err = got_ref_resolve(&commit_id, repo, commit_ref);
6588 if (err)
6589 goto done;
6590 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6591 err = got_error(GOT_ERR_REBASE_COMMITID);
6592 goto done;
6595 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6596 worktree, fileindex, tmp_branch, committer, orig_commit,
6597 NULL, repo);
6598 done:
6599 if (commit_ref)
6600 got_ref_close(commit_ref);
6601 free(commit_ref_name);
6602 free(commit_id);
6603 return err;
6606 const struct got_error *
6607 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6608 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6609 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6610 const char *committer, struct got_commit_object *orig_commit,
6611 struct got_object_id *orig_commit_id, const char *new_logmsg,
6612 struct got_repository *repo)
6614 const struct got_error *err;
6615 char *commit_ref_name;
6616 struct got_reference *commit_ref = NULL;
6618 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6619 if (err)
6620 return err;
6622 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6623 if (err)
6624 goto done;
6626 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6627 worktree, fileindex, tmp_branch, committer, orig_commit,
6628 new_logmsg, repo);
6629 done:
6630 if (commit_ref)
6631 got_ref_close(commit_ref);
6632 free(commit_ref_name);
6633 return err;
6636 const struct got_error *
6637 got_worktree_rebase_postpone(struct got_worktree *worktree,
6638 struct got_fileindex *fileindex)
6640 if (fileindex)
6641 got_fileindex_free(fileindex);
6642 return lock_worktree(worktree, LOCK_SH);
6645 static const struct got_error *
6646 delete_ref(const char *name, struct got_repository *repo)
6648 const struct got_error *err;
6649 struct got_reference *ref;
6651 err = got_ref_open(&ref, repo, name, 0);
6652 if (err) {
6653 if (err->code == GOT_ERR_NOT_REF)
6654 return NULL;
6655 return err;
6658 err = got_ref_delete(ref, repo);
6659 got_ref_close(ref);
6660 return err;
6663 static const struct got_error *
6664 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6666 const struct got_error *err;
6667 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6668 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6670 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6671 if (err)
6672 goto done;
6673 err = delete_ref(tmp_branch_name, repo);
6674 if (err)
6675 goto done;
6677 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6678 if (err)
6679 goto done;
6680 err = delete_ref(new_base_branch_ref_name, repo);
6681 if (err)
6682 goto done;
6684 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6685 if (err)
6686 goto done;
6687 err = delete_ref(branch_ref_name, repo);
6688 if (err)
6689 goto done;
6691 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6692 if (err)
6693 goto done;
6694 err = delete_ref(commit_ref_name, repo);
6695 if (err)
6696 goto done;
6698 done:
6699 free(tmp_branch_name);
6700 free(new_base_branch_ref_name);
6701 free(branch_ref_name);
6702 free(commit_ref_name);
6703 return err;
6706 static const struct got_error *
6707 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6708 struct got_object_id *new_commit_id, struct got_repository *repo)
6710 const struct got_error *err;
6711 struct got_reference *ref = NULL;
6712 struct got_object_id *old_commit_id = NULL;
6713 const char *branch_name = NULL;
6714 char *new_id_str = NULL;
6715 char *refname = NULL;
6717 branch_name = got_ref_get_name(branch);
6718 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6719 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6720 branch_name += 11;
6722 err = got_object_id_str(&new_id_str, new_commit_id);
6723 if (err)
6724 return err;
6726 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6727 new_id_str) == -1) {
6728 err = got_error_from_errno("asprintf");
6729 goto done;
6732 err = got_ref_resolve(&old_commit_id, repo, branch);
6733 if (err)
6734 goto done;
6736 err = got_ref_alloc(&ref, refname, old_commit_id);
6737 if (err)
6738 goto done;
6740 err = got_ref_write(ref, repo);
6741 done:
6742 free(new_id_str);
6743 free(refname);
6744 free(old_commit_id);
6745 if (ref)
6746 got_ref_close(ref);
6747 return err;
6750 const struct got_error *
6751 got_worktree_rebase_complete(struct got_worktree *worktree,
6752 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6753 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6754 struct got_repository *repo, int create_backup)
6756 const struct got_error *err, *unlockerr, *sync_err;
6757 struct got_object_id *new_head_commit_id = NULL;
6758 char *fileindex_path = NULL;
6760 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6761 if (err)
6762 return err;
6764 if (create_backup) {
6765 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6766 rebased_branch, new_head_commit_id, repo);
6767 if (err)
6768 goto done;
6771 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6772 if (err)
6773 goto done;
6775 err = got_ref_write(rebased_branch, repo);
6776 if (err)
6777 goto done;
6779 err = got_worktree_set_head_ref(worktree, rebased_branch);
6780 if (err)
6781 goto done;
6783 err = delete_rebase_refs(worktree, repo);
6784 if (err)
6785 goto done;
6787 err = get_fileindex_path(&fileindex_path, worktree);
6788 if (err)
6789 goto done;
6790 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6791 sync_err = sync_fileindex(fileindex, fileindex_path);
6792 if (sync_err && err == NULL)
6793 err = sync_err;
6794 done:
6795 got_fileindex_free(fileindex);
6796 free(fileindex_path);
6797 free(new_head_commit_id);
6798 unlockerr = lock_worktree(worktree, LOCK_SH);
6799 if (unlockerr && err == NULL)
6800 err = unlockerr;
6801 return err;
6804 const struct got_error *
6805 got_worktree_rebase_abort(struct got_worktree *worktree,
6806 struct got_fileindex *fileindex, struct got_repository *repo,
6807 struct got_reference *new_base_branch,
6808 got_worktree_checkout_cb progress_cb, void *progress_arg)
6810 const struct got_error *err, *unlockerr, *sync_err;
6811 struct got_reference *resolved = NULL;
6812 struct got_object_id *commit_id = NULL;
6813 struct got_commit_object *commit = NULL;
6814 char *fileindex_path = NULL;
6815 struct revert_file_args rfa;
6816 struct got_object_id *tree_id = NULL;
6818 err = lock_worktree(worktree, LOCK_EX);
6819 if (err)
6820 return err;
6822 err = got_object_open_as_commit(&commit, repo,
6823 worktree->base_commit_id);
6824 if (err)
6825 goto done;
6827 err = got_ref_open(&resolved, repo,
6828 got_ref_get_symref_target(new_base_branch), 0);
6829 if (err)
6830 goto done;
6832 err = got_worktree_set_head_ref(worktree, resolved);
6833 if (err)
6834 goto done;
6837 * XXX commits to the base branch could have happened while
6838 * we were busy rebasing; should we store the original commit ID
6839 * when rebase begins and read it back here?
6841 err = got_ref_resolve(&commit_id, repo, resolved);
6842 if (err)
6843 goto done;
6845 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6846 if (err)
6847 goto done;
6849 err = got_object_id_by_path(&tree_id, repo, commit,
6850 worktree->path_prefix);
6851 if (err)
6852 goto done;
6854 err = delete_rebase_refs(worktree, repo);
6855 if (err)
6856 goto done;
6858 err = get_fileindex_path(&fileindex_path, worktree);
6859 if (err)
6860 goto done;
6862 rfa.worktree = worktree;
6863 rfa.fileindex = fileindex;
6864 rfa.progress_cb = progress_cb;
6865 rfa.progress_arg = progress_arg;
6866 rfa.patch_cb = NULL;
6867 rfa.patch_arg = NULL;
6868 rfa.repo = repo;
6869 rfa.unlink_added_files = 0;
6870 err = worktree_status(worktree, "", fileindex, repo,
6871 revert_file, &rfa, NULL, NULL, 1, 0);
6872 if (err)
6873 goto sync;
6875 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6876 repo, progress_cb, progress_arg, NULL, NULL);
6877 sync:
6878 sync_err = sync_fileindex(fileindex, fileindex_path);
6879 if (sync_err && err == NULL)
6880 err = sync_err;
6881 done:
6882 got_ref_close(resolved);
6883 free(tree_id);
6884 free(commit_id);
6885 if (commit)
6886 got_object_commit_close(commit);
6887 if (fileindex)
6888 got_fileindex_free(fileindex);
6889 free(fileindex_path);
6891 unlockerr = lock_worktree(worktree, LOCK_SH);
6892 if (unlockerr && err == NULL)
6893 err = unlockerr;
6894 return err;
6897 const struct got_error *
6898 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6899 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6900 struct got_fileindex **fileindex, struct got_worktree *worktree,
6901 struct got_repository *repo)
6903 const struct got_error *err = NULL;
6904 char *tmp_branch_name = NULL;
6905 char *branch_ref_name = NULL;
6906 char *base_commit_ref_name = NULL;
6907 char *fileindex_path = NULL;
6908 struct check_rebase_ok_arg ok_arg;
6909 struct got_reference *wt_branch = NULL;
6910 struct got_reference *base_commit_ref = NULL;
6912 *tmp_branch = NULL;
6913 *branch_ref = NULL;
6914 *base_commit_id = NULL;
6915 *fileindex = NULL;
6917 err = lock_worktree(worktree, LOCK_EX);
6918 if (err)
6919 return err;
6921 err = open_fileindex(fileindex, &fileindex_path, worktree);
6922 if (err)
6923 goto done;
6925 ok_arg.worktree = worktree;
6926 ok_arg.repo = repo;
6927 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6928 &ok_arg);
6929 if (err)
6930 goto done;
6932 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6933 if (err)
6934 goto done;
6936 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6937 if (err)
6938 goto done;
6940 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6941 worktree);
6942 if (err)
6943 goto done;
6945 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6946 0);
6947 if (err)
6948 goto done;
6950 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6951 if (err)
6952 goto done;
6954 err = got_ref_write(*branch_ref, repo);
6955 if (err)
6956 goto done;
6958 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6959 worktree->base_commit_id);
6960 if (err)
6961 goto done;
6962 err = got_ref_write(base_commit_ref, repo);
6963 if (err)
6964 goto done;
6965 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6966 if (*base_commit_id == NULL) {
6967 err = got_error_from_errno("got_object_id_dup");
6968 goto done;
6971 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6972 worktree->base_commit_id);
6973 if (err)
6974 goto done;
6975 err = got_ref_write(*tmp_branch, repo);
6976 if (err)
6977 goto done;
6979 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6980 if (err)
6981 goto done;
6982 done:
6983 free(fileindex_path);
6984 free(tmp_branch_name);
6985 free(branch_ref_name);
6986 free(base_commit_ref_name);
6987 if (wt_branch)
6988 got_ref_close(wt_branch);
6989 if (err) {
6990 if (*branch_ref) {
6991 got_ref_close(*branch_ref);
6992 *branch_ref = NULL;
6994 if (*tmp_branch) {
6995 got_ref_close(*tmp_branch);
6996 *tmp_branch = NULL;
6998 free(*base_commit_id);
6999 if (*fileindex) {
7000 got_fileindex_free(*fileindex);
7001 *fileindex = NULL;
7003 lock_worktree(worktree, LOCK_SH);
7005 return err;
7008 const struct got_error *
7009 got_worktree_histedit_postpone(struct got_worktree *worktree,
7010 struct got_fileindex *fileindex)
7012 if (fileindex)
7013 got_fileindex_free(fileindex);
7014 return lock_worktree(worktree, LOCK_SH);
7017 const struct got_error *
7018 got_worktree_histedit_in_progress(int *in_progress,
7019 struct got_worktree *worktree)
7021 const struct got_error *err;
7022 char *tmp_branch_name = NULL;
7024 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7025 if (err)
7026 return err;
7028 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7029 free(tmp_branch_name);
7030 return NULL;
7033 const struct got_error *
7034 got_worktree_histedit_continue(struct got_object_id **commit_id,
7035 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7036 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7037 struct got_worktree *worktree, struct got_repository *repo)
7039 const struct got_error *err;
7040 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7041 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7042 struct got_reference *commit_ref = NULL;
7043 struct got_reference *base_commit_ref = NULL;
7044 char *fileindex_path = NULL;
7045 int have_staged_files = 0;
7047 *commit_id = NULL;
7048 *tmp_branch = NULL;
7049 *base_commit_id = NULL;
7050 *fileindex = NULL;
7052 err = lock_worktree(worktree, LOCK_EX);
7053 if (err)
7054 return err;
7056 err = open_fileindex(fileindex, &fileindex_path, worktree);
7057 if (err)
7058 goto done;
7060 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7061 &have_staged_files);
7062 if (err && err->code != GOT_ERR_CANCELLED)
7063 goto done;
7064 if (have_staged_files) {
7065 err = got_error(GOT_ERR_STAGED_PATHS);
7066 goto done;
7069 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7070 if (err)
7071 goto done;
7073 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7074 if (err)
7075 goto done;
7077 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7078 if (err)
7079 goto done;
7081 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7082 worktree);
7083 if (err)
7084 goto done;
7086 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7087 if (err)
7088 goto done;
7090 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7091 if (err)
7092 goto done;
7093 err = got_ref_resolve(commit_id, repo, commit_ref);
7094 if (err)
7095 goto done;
7097 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7098 if (err)
7099 goto done;
7100 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7101 if (err)
7102 goto done;
7104 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7105 if (err)
7106 goto done;
7107 done:
7108 free(commit_ref_name);
7109 free(branch_ref_name);
7110 free(fileindex_path);
7111 if (commit_ref)
7112 got_ref_close(commit_ref);
7113 if (base_commit_ref)
7114 got_ref_close(base_commit_ref);
7115 if (err) {
7116 free(*commit_id);
7117 *commit_id = NULL;
7118 free(*base_commit_id);
7119 *base_commit_id = NULL;
7120 if (*tmp_branch) {
7121 got_ref_close(*tmp_branch);
7122 *tmp_branch = NULL;
7124 if (*fileindex) {
7125 got_fileindex_free(*fileindex);
7126 *fileindex = NULL;
7128 lock_worktree(worktree, LOCK_EX);
7130 return err;
7133 static const struct got_error *
7134 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7136 const struct got_error *err;
7137 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7138 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7140 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7141 if (err)
7142 goto done;
7143 err = delete_ref(tmp_branch_name, repo);
7144 if (err)
7145 goto done;
7147 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7148 worktree);
7149 if (err)
7150 goto done;
7151 err = delete_ref(base_commit_ref_name, repo);
7152 if (err)
7153 goto done;
7155 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7156 if (err)
7157 goto done;
7158 err = delete_ref(branch_ref_name, repo);
7159 if (err)
7160 goto done;
7162 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7163 if (err)
7164 goto done;
7165 err = delete_ref(commit_ref_name, repo);
7166 if (err)
7167 goto done;
7168 done:
7169 free(tmp_branch_name);
7170 free(base_commit_ref_name);
7171 free(branch_ref_name);
7172 free(commit_ref_name);
7173 return err;
7176 const struct got_error *
7177 got_worktree_histedit_abort(struct got_worktree *worktree,
7178 struct got_fileindex *fileindex, struct got_repository *repo,
7179 struct got_reference *branch, struct got_object_id *base_commit_id,
7180 got_worktree_checkout_cb progress_cb, void *progress_arg)
7182 const struct got_error *err, *unlockerr, *sync_err;
7183 struct got_reference *resolved = NULL;
7184 char *fileindex_path = NULL;
7185 struct got_commit_object *commit = NULL;
7186 struct got_object_id *tree_id = NULL;
7187 struct revert_file_args rfa;
7189 err = lock_worktree(worktree, LOCK_EX);
7190 if (err)
7191 return err;
7193 err = got_object_open_as_commit(&commit, repo,
7194 worktree->base_commit_id);
7195 if (err)
7196 goto done;
7198 err = got_ref_open(&resolved, repo,
7199 got_ref_get_symref_target(branch), 0);
7200 if (err)
7201 goto done;
7203 err = got_worktree_set_head_ref(worktree, resolved);
7204 if (err)
7205 goto done;
7207 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7208 if (err)
7209 goto done;
7211 err = got_object_id_by_path(&tree_id, repo, commit,
7212 worktree->path_prefix);
7213 if (err)
7214 goto done;
7216 err = delete_histedit_refs(worktree, repo);
7217 if (err)
7218 goto done;
7220 err = get_fileindex_path(&fileindex_path, worktree);
7221 if (err)
7222 goto done;
7224 rfa.worktree = worktree;
7225 rfa.fileindex = fileindex;
7226 rfa.progress_cb = progress_cb;
7227 rfa.progress_arg = progress_arg;
7228 rfa.patch_cb = NULL;
7229 rfa.patch_arg = NULL;
7230 rfa.repo = repo;
7231 rfa.unlink_added_files = 0;
7232 err = worktree_status(worktree, "", fileindex, repo,
7233 revert_file, &rfa, NULL, NULL, 1, 0);
7234 if (err)
7235 goto sync;
7237 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7238 repo, progress_cb, progress_arg, NULL, NULL);
7239 sync:
7240 sync_err = sync_fileindex(fileindex, fileindex_path);
7241 if (sync_err && err == NULL)
7242 err = sync_err;
7243 done:
7244 got_ref_close(resolved);
7245 free(tree_id);
7246 free(fileindex_path);
7248 unlockerr = lock_worktree(worktree, LOCK_SH);
7249 if (unlockerr && err == NULL)
7250 err = unlockerr;
7251 return err;
7254 const struct got_error *
7255 got_worktree_histedit_complete(struct got_worktree *worktree,
7256 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7257 struct got_reference *edited_branch, struct got_repository *repo)
7259 const struct got_error *err, *unlockerr, *sync_err;
7260 struct got_object_id *new_head_commit_id = NULL;
7261 struct got_reference *resolved = NULL;
7262 char *fileindex_path = NULL;
7264 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7265 if (err)
7266 return err;
7268 err = got_ref_open(&resolved, repo,
7269 got_ref_get_symref_target(edited_branch), 0);
7270 if (err)
7271 goto done;
7273 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7274 resolved, new_head_commit_id, repo);
7275 if (err)
7276 goto done;
7278 err = got_ref_change_ref(resolved, new_head_commit_id);
7279 if (err)
7280 goto done;
7282 err = got_ref_write(resolved, repo);
7283 if (err)
7284 goto done;
7286 err = got_worktree_set_head_ref(worktree, resolved);
7287 if (err)
7288 goto done;
7290 err = delete_histedit_refs(worktree, repo);
7291 if (err)
7292 goto done;
7294 err = get_fileindex_path(&fileindex_path, worktree);
7295 if (err)
7296 goto done;
7297 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7298 sync_err = sync_fileindex(fileindex, fileindex_path);
7299 if (sync_err && err == NULL)
7300 err = sync_err;
7301 done:
7302 got_fileindex_free(fileindex);
7303 free(fileindex_path);
7304 free(new_head_commit_id);
7305 unlockerr = lock_worktree(worktree, LOCK_SH);
7306 if (unlockerr && err == NULL)
7307 err = unlockerr;
7308 return err;
7311 const struct got_error *
7312 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7313 struct got_object_id *commit_id, struct got_repository *repo)
7315 const struct got_error *err;
7316 char *commit_ref_name;
7318 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7319 if (err)
7320 return err;
7322 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7323 if (err)
7324 goto done;
7326 err = delete_ref(commit_ref_name, repo);
7327 done:
7328 free(commit_ref_name);
7329 return err;
7332 const struct got_error *
7333 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7334 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7335 struct got_worktree *worktree, const char *refname,
7336 struct got_repository *repo)
7338 const struct got_error *err = NULL;
7339 char *fileindex_path = NULL;
7340 struct check_rebase_ok_arg ok_arg;
7342 *fileindex = NULL;
7343 *branch_ref = NULL;
7344 *base_branch_ref = NULL;
7346 err = lock_worktree(worktree, LOCK_EX);
7347 if (err)
7348 return err;
7350 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7351 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7352 "cannot integrate a branch into itself; "
7353 "update -b or different branch name required");
7354 goto done;
7357 err = open_fileindex(fileindex, &fileindex_path, worktree);
7358 if (err)
7359 goto done;
7361 /* Preconditions are the same as for rebase. */
7362 ok_arg.worktree = worktree;
7363 ok_arg.repo = repo;
7364 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7365 &ok_arg);
7366 if (err)
7367 goto done;
7369 err = got_ref_open(branch_ref, repo, refname, 1);
7370 if (err)
7371 goto done;
7373 err = got_ref_open(base_branch_ref, repo,
7374 got_worktree_get_head_ref_name(worktree), 1);
7375 done:
7376 if (err) {
7377 if (*branch_ref) {
7378 got_ref_close(*branch_ref);
7379 *branch_ref = NULL;
7381 if (*base_branch_ref) {
7382 got_ref_close(*base_branch_ref);
7383 *base_branch_ref = NULL;
7385 if (*fileindex) {
7386 got_fileindex_free(*fileindex);
7387 *fileindex = NULL;
7389 lock_worktree(worktree, LOCK_SH);
7391 return err;
7394 const struct got_error *
7395 got_worktree_integrate_continue(struct got_worktree *worktree,
7396 struct got_fileindex *fileindex, struct got_repository *repo,
7397 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7398 got_worktree_checkout_cb progress_cb, void *progress_arg,
7399 got_cancel_cb cancel_cb, void *cancel_arg)
7401 const struct got_error *err = NULL, *sync_err, *unlockerr;
7402 char *fileindex_path = NULL;
7403 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7404 struct got_commit_object *commit = NULL;
7406 err = get_fileindex_path(&fileindex_path, worktree);
7407 if (err)
7408 goto done;
7410 err = got_ref_resolve(&commit_id, repo, branch_ref);
7411 if (err)
7412 goto done;
7414 err = got_object_open_as_commit(&commit, repo, commit_id);
7415 if (err)
7416 goto done;
7418 err = got_object_id_by_path(&tree_id, repo, commit,
7419 worktree->path_prefix);
7420 if (err)
7421 goto done;
7423 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7424 if (err)
7425 goto done;
7427 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7428 progress_cb, progress_arg, cancel_cb, cancel_arg);
7429 if (err)
7430 goto sync;
7432 err = got_ref_change_ref(base_branch_ref, commit_id);
7433 if (err)
7434 goto sync;
7436 err = got_ref_write(base_branch_ref, repo);
7437 if (err)
7438 goto sync;
7440 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7441 sync:
7442 sync_err = sync_fileindex(fileindex, fileindex_path);
7443 if (sync_err && err == NULL)
7444 err = sync_err;
7446 done:
7447 unlockerr = got_ref_unlock(branch_ref);
7448 if (unlockerr && err == NULL)
7449 err = unlockerr;
7450 got_ref_close(branch_ref);
7452 unlockerr = got_ref_unlock(base_branch_ref);
7453 if (unlockerr && err == NULL)
7454 err = unlockerr;
7455 got_ref_close(base_branch_ref);
7457 got_fileindex_free(fileindex);
7458 free(fileindex_path);
7459 free(tree_id);
7460 if (commit)
7461 got_object_commit_close(commit);
7463 unlockerr = lock_worktree(worktree, LOCK_SH);
7464 if (unlockerr && err == NULL)
7465 err = unlockerr;
7466 return err;
7469 const struct got_error *
7470 got_worktree_integrate_abort(struct got_worktree *worktree,
7471 struct got_fileindex *fileindex, struct got_repository *repo,
7472 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7474 const struct got_error *err = NULL, *unlockerr = NULL;
7476 got_fileindex_free(fileindex);
7478 err = lock_worktree(worktree, LOCK_SH);
7480 unlockerr = got_ref_unlock(branch_ref);
7481 if (unlockerr && err == NULL)
7482 err = unlockerr;
7483 got_ref_close(branch_ref);
7485 unlockerr = got_ref_unlock(base_branch_ref);
7486 if (unlockerr && err == NULL)
7487 err = unlockerr;
7488 got_ref_close(base_branch_ref);
7490 return err;
7493 const struct got_error *
7494 got_worktree_merge_postpone(struct got_worktree *worktree,
7495 struct got_fileindex *fileindex)
7497 const struct got_error *err, *sync_err;
7498 char *fileindex_path = NULL;
7500 err = get_fileindex_path(&fileindex_path, worktree);
7501 if (err)
7502 goto done;
7504 sync_err = sync_fileindex(fileindex, fileindex_path);
7506 err = lock_worktree(worktree, LOCK_SH);
7507 if (sync_err && err == NULL)
7508 err = sync_err;
7509 done:
7510 got_fileindex_free(fileindex);
7511 free(fileindex_path);
7512 return err;
7515 static const struct got_error *
7516 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7518 const struct got_error *err;
7519 char *branch_refname = NULL, *commit_refname = NULL;
7521 err = get_merge_branch_ref_name(&branch_refname, worktree);
7522 if (err)
7523 goto done;
7524 err = delete_ref(branch_refname, repo);
7525 if (err)
7526 goto done;
7528 err = get_merge_commit_ref_name(&commit_refname, worktree);
7529 if (err)
7530 goto done;
7531 err = delete_ref(commit_refname, repo);
7532 if (err)
7533 goto done;
7535 done:
7536 free(branch_refname);
7537 free(commit_refname);
7538 return err;
7541 struct merge_commit_msg_arg {
7542 struct got_worktree *worktree;
7543 const char *branch_name;
7546 static const struct got_error *
7547 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7548 void *arg)
7550 struct merge_commit_msg_arg *a = arg;
7552 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7553 got_worktree_get_head_ref_name(a->worktree)) == -1)
7554 return got_error_from_errno("asprintf");
7556 return NULL;
7560 const struct got_error *
7561 got_worktree_merge_branch(struct got_worktree *worktree,
7562 struct got_fileindex *fileindex,
7563 struct got_object_id *yca_commit_id,
7564 struct got_object_id *branch_tip,
7565 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7566 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7568 const struct got_error *err;
7569 char *fileindex_path = NULL;
7571 err = get_fileindex_path(&fileindex_path, worktree);
7572 if (err)
7573 goto done;
7575 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7576 worktree);
7577 if (err)
7578 goto done;
7580 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7581 branch_tip, repo, progress_cb, progress_arg,
7582 cancel_cb, cancel_arg);
7583 done:
7584 free(fileindex_path);
7585 return err;
7588 const struct got_error *
7589 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7590 struct got_worktree *worktree, struct got_fileindex *fileindex,
7591 const char *author, const char *committer, int allow_bad_symlinks,
7592 struct got_object_id *branch_tip, const char *branch_name,
7593 struct got_repository *repo,
7594 got_worktree_status_cb status_cb, void *status_arg)
7597 const struct got_error *err = NULL, *sync_err;
7598 struct got_pathlist_head commitable_paths;
7599 struct collect_commitables_arg cc_arg;
7600 struct got_pathlist_entry *pe;
7601 struct got_reference *head_ref = NULL;
7602 struct got_object_id *head_commit_id = NULL;
7603 int have_staged_files = 0;
7604 struct merge_commit_msg_arg mcm_arg;
7605 char *fileindex_path = NULL;
7607 *new_commit_id = NULL;
7609 TAILQ_INIT(&commitable_paths);
7611 err = get_fileindex_path(&fileindex_path, worktree);
7612 if (err)
7613 goto done;
7615 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7616 if (err)
7617 goto done;
7619 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7620 if (err)
7621 goto done;
7623 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7624 &have_staged_files);
7625 if (err && err->code != GOT_ERR_CANCELLED)
7626 goto done;
7627 if (have_staged_files) {
7628 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7629 goto done;
7632 cc_arg.commitable_paths = &commitable_paths;
7633 cc_arg.worktree = worktree;
7634 cc_arg.fileindex = fileindex;
7635 cc_arg.repo = repo;
7636 cc_arg.have_staged_files = have_staged_files;
7637 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7638 err = worktree_status(worktree, "", fileindex, repo,
7639 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7640 if (err)
7641 goto done;
7643 if (TAILQ_EMPTY(&commitable_paths)) {
7644 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7645 "merge of %s cannot proceed", branch_name);
7646 goto done;
7649 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7650 struct got_commitable *ct = pe->data;
7651 const char *ct_path = ct->in_repo_path;
7653 while (ct_path[0] == '/')
7654 ct_path++;
7655 err = check_out_of_date(ct_path, ct->status,
7656 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7657 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7658 if (err)
7659 goto done;
7663 mcm_arg.worktree = worktree;
7664 mcm_arg.branch_name = branch_name;
7665 err = commit_worktree(new_commit_id, &commitable_paths,
7666 head_commit_id, branch_tip, worktree, author, committer,
7667 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7668 if (err)
7669 goto done;
7671 err = update_fileindex_after_commit(worktree, &commitable_paths,
7672 *new_commit_id, fileindex, have_staged_files);
7673 sync_err = sync_fileindex(fileindex, fileindex_path);
7674 if (sync_err && err == NULL)
7675 err = sync_err;
7676 done:
7677 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7678 struct got_commitable *ct = pe->data;
7679 free_commitable(ct);
7681 got_pathlist_free(&commitable_paths);
7682 free(fileindex_path);
7683 return err;
7686 const struct got_error *
7687 got_worktree_merge_complete(struct got_worktree *worktree,
7688 struct got_fileindex *fileindex, struct got_repository *repo)
7690 const struct got_error *err, *unlockerr, *sync_err;
7691 char *fileindex_path = NULL;
7693 err = delete_merge_refs(worktree, repo);
7694 if (err)
7695 goto done;
7697 err = get_fileindex_path(&fileindex_path, worktree);
7698 if (err)
7699 goto done;
7700 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7701 sync_err = sync_fileindex(fileindex, fileindex_path);
7702 if (sync_err && err == NULL)
7703 err = sync_err;
7704 done:
7705 got_fileindex_free(fileindex);
7706 free(fileindex_path);
7707 unlockerr = lock_worktree(worktree, LOCK_SH);
7708 if (unlockerr && err == NULL)
7709 err = unlockerr;
7710 return err;
7713 const struct got_error *
7714 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7715 struct got_repository *repo)
7717 const struct got_error *err;
7718 char *branch_refname = NULL;
7719 struct got_reference *branch_ref = NULL;
7721 *in_progress = 0;
7723 err = get_merge_branch_ref_name(&branch_refname, worktree);
7724 if (err)
7725 return err;
7726 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7727 free(branch_refname);
7728 if (err) {
7729 if (err->code != GOT_ERR_NOT_REF)
7730 return err;
7731 } else
7732 *in_progress = 1;
7734 return NULL;
7737 const struct got_error *got_worktree_merge_prepare(
7738 struct got_fileindex **fileindex, struct got_worktree *worktree,
7739 struct got_reference *branch, struct got_repository *repo)
7741 const struct got_error *err = NULL;
7742 char *fileindex_path = NULL;
7743 char *branch_refname = NULL, *commit_refname = NULL;
7744 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7745 struct got_reference *commit_ref = NULL;
7746 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7747 struct check_rebase_ok_arg ok_arg;
7749 *fileindex = NULL;
7751 err = lock_worktree(worktree, LOCK_EX);
7752 if (err)
7753 return err;
7755 err = open_fileindex(fileindex, &fileindex_path, worktree);
7756 if (err)
7757 goto done;
7759 /* Preconditions are the same as for rebase. */
7760 ok_arg.worktree = worktree;
7761 ok_arg.repo = repo;
7762 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7763 &ok_arg);
7764 if (err)
7765 goto done;
7767 err = get_merge_branch_ref_name(&branch_refname, worktree);
7768 if (err)
7769 return err;
7771 err = get_merge_commit_ref_name(&commit_refname, worktree);
7772 if (err)
7773 return err;
7775 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7776 0);
7777 if (err)
7778 goto done;
7780 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7781 if (err)
7782 goto done;
7784 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7785 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7786 goto done;
7789 err = got_ref_resolve(&branch_tip, repo, branch);
7790 if (err)
7791 goto done;
7793 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7794 if (err)
7795 goto done;
7796 err = got_ref_write(branch_ref, repo);
7797 if (err)
7798 goto done;
7800 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7801 if (err)
7802 goto done;
7803 err = got_ref_write(commit_ref, repo);
7804 if (err)
7805 goto done;
7807 done:
7808 free(branch_refname);
7809 free(commit_refname);
7810 free(fileindex_path);
7811 if (branch_ref)
7812 got_ref_close(branch_ref);
7813 if (commit_ref)
7814 got_ref_close(commit_ref);
7815 if (wt_branch)
7816 got_ref_close(wt_branch);
7817 free(wt_branch_tip);
7818 if (err) {
7819 if (*fileindex) {
7820 got_fileindex_free(*fileindex);
7821 *fileindex = NULL;
7823 lock_worktree(worktree, LOCK_SH);
7825 return err;
7828 const struct got_error *
7829 got_worktree_merge_continue(char **branch_name,
7830 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7831 struct got_worktree *worktree, struct got_repository *repo)
7833 const struct got_error *err;
7834 char *commit_refname = NULL, *branch_refname = NULL;
7835 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7836 char *fileindex_path = NULL;
7837 int have_staged_files = 0;
7839 *branch_name = NULL;
7840 *branch_tip = NULL;
7841 *fileindex = NULL;
7843 err = lock_worktree(worktree, LOCK_EX);
7844 if (err)
7845 return err;
7847 err = open_fileindex(fileindex, &fileindex_path, worktree);
7848 if (err)
7849 goto done;
7851 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7852 &have_staged_files);
7853 if (err && err->code != GOT_ERR_CANCELLED)
7854 goto done;
7855 if (have_staged_files) {
7856 err = got_error(GOT_ERR_STAGED_PATHS);
7857 goto done;
7860 err = get_merge_branch_ref_name(&branch_refname, worktree);
7861 if (err)
7862 goto done;
7864 err = get_merge_commit_ref_name(&commit_refname, worktree);
7865 if (err)
7866 goto done;
7868 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7869 if (err)
7870 goto done;
7872 if (!got_ref_is_symbolic(branch_ref)) {
7873 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7874 "%s is not a symbolic reference",
7875 got_ref_get_name(branch_ref));
7876 goto done;
7878 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7879 if (*branch_name == NULL) {
7880 err = got_error_from_errno("strdup");
7881 goto done;
7884 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7885 if (err)
7886 goto done;
7888 err = got_ref_resolve(branch_tip, repo, commit_ref);
7889 if (err)
7890 goto done;
7891 done:
7892 free(commit_refname);
7893 free(branch_refname);
7894 free(fileindex_path);
7895 if (commit_ref)
7896 got_ref_close(commit_ref);
7897 if (branch_ref)
7898 got_ref_close(branch_ref);
7899 if (err) {
7900 if (*branch_name) {
7901 free(*branch_name);
7902 *branch_name = NULL;
7904 free(*branch_tip);
7905 *branch_tip = NULL;
7906 if (*fileindex) {
7907 got_fileindex_free(*fileindex);
7908 *fileindex = NULL;
7910 lock_worktree(worktree, LOCK_SH);
7912 return err;
7915 const struct got_error *
7916 got_worktree_merge_abort(struct got_worktree *worktree,
7917 struct got_fileindex *fileindex, struct got_repository *repo,
7918 got_worktree_checkout_cb progress_cb, void *progress_arg)
7920 const struct got_error *err, *unlockerr, *sync_err;
7921 struct got_object_id *commit_id = NULL;
7922 struct got_commit_object *commit = NULL;
7923 char *fileindex_path = NULL;
7924 struct revert_file_args rfa;
7925 struct got_object_id *tree_id = NULL;
7927 err = got_object_open_as_commit(&commit, repo,
7928 worktree->base_commit_id);
7929 if (err)
7930 goto done;
7932 err = got_object_id_by_path(&tree_id, repo, commit,
7933 worktree->path_prefix);
7934 if (err)
7935 goto done;
7937 err = delete_merge_refs(worktree, repo);
7938 if (err)
7939 goto done;
7941 err = get_fileindex_path(&fileindex_path, worktree);
7942 if (err)
7943 goto done;
7945 rfa.worktree = worktree;
7946 rfa.fileindex = fileindex;
7947 rfa.progress_cb = progress_cb;
7948 rfa.progress_arg = progress_arg;
7949 rfa.patch_cb = NULL;
7950 rfa.patch_arg = NULL;
7951 rfa.repo = repo;
7952 rfa.unlink_added_files = 1;
7953 err = worktree_status(worktree, "", fileindex, repo,
7954 revert_file, &rfa, NULL, NULL, 1, 0);
7955 if (err)
7956 goto sync;
7958 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7959 repo, progress_cb, progress_arg, NULL, NULL);
7960 sync:
7961 sync_err = sync_fileindex(fileindex, fileindex_path);
7962 if (sync_err && err == NULL)
7963 err = sync_err;
7964 done:
7965 free(tree_id);
7966 free(commit_id);
7967 if (commit)
7968 got_object_commit_close(commit);
7969 if (fileindex)
7970 got_fileindex_free(fileindex);
7971 free(fileindex_path);
7973 unlockerr = lock_worktree(worktree, LOCK_SH);
7974 if (unlockerr && err == NULL)
7975 err = unlockerr;
7976 return err;
7979 struct check_stage_ok_arg {
7980 struct got_object_id *head_commit_id;
7981 struct got_worktree *worktree;
7982 struct got_fileindex *fileindex;
7983 struct got_repository *repo;
7984 int have_changes;
7987 static const struct got_error *
7988 check_stage_ok(void *arg, unsigned char status,
7989 unsigned char staged_status, const char *relpath,
7990 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7991 struct got_object_id *commit_id, int dirfd, const char *de_name)
7993 struct check_stage_ok_arg *a = arg;
7994 const struct got_error *err = NULL;
7995 struct got_fileindex_entry *ie;
7996 struct got_object_id base_commit_id;
7997 struct got_object_id *base_commit_idp = NULL;
7998 char *in_repo_path = NULL, *p;
8000 if (status == GOT_STATUS_UNVERSIONED ||
8001 status == GOT_STATUS_NO_CHANGE)
8002 return NULL;
8003 if (status == GOT_STATUS_NONEXISTENT)
8004 return got_error_set_errno(ENOENT, relpath);
8006 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8007 if (ie == NULL)
8008 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8010 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8011 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8012 relpath) == -1)
8013 return got_error_from_errno("asprintf");
8015 if (got_fileindex_entry_has_commit(ie)) {
8016 memcpy(base_commit_id.sha1, ie->commit_sha1,
8017 SHA1_DIGEST_LENGTH);
8018 base_commit_idp = &base_commit_id;
8021 if (status == GOT_STATUS_CONFLICT) {
8022 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8023 goto done;
8024 } else if (status != GOT_STATUS_ADD &&
8025 status != GOT_STATUS_MODIFY &&
8026 status != GOT_STATUS_DELETE) {
8027 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8028 goto done;
8031 a->have_changes = 1;
8033 p = in_repo_path;
8034 while (p[0] == '/')
8035 p++;
8036 err = check_out_of_date(p, status, staged_status,
8037 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8038 GOT_ERR_STAGE_OUT_OF_DATE);
8039 done:
8040 free(in_repo_path);
8041 return err;
8044 struct stage_path_arg {
8045 struct got_worktree *worktree;
8046 struct got_fileindex *fileindex;
8047 struct got_repository *repo;
8048 got_worktree_status_cb status_cb;
8049 void *status_arg;
8050 got_worktree_patch_cb patch_cb;
8051 void *patch_arg;
8052 int staged_something;
8053 int allow_bad_symlinks;
8056 static const struct got_error *
8057 stage_path(void *arg, unsigned char status,
8058 unsigned char staged_status, const char *relpath,
8059 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8060 struct got_object_id *commit_id, int dirfd, const char *de_name)
8062 struct stage_path_arg *a = arg;
8063 const struct got_error *err = NULL;
8064 struct got_fileindex_entry *ie;
8065 char *ondisk_path = NULL, *path_content = NULL;
8066 uint32_t stage;
8067 struct got_object_id *new_staged_blob_id = NULL;
8068 struct stat sb;
8070 if (status == GOT_STATUS_UNVERSIONED)
8071 return NULL;
8073 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8074 if (ie == NULL)
8075 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8077 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8078 relpath)== -1)
8079 return got_error_from_errno("asprintf");
8081 switch (status) {
8082 case GOT_STATUS_ADD:
8083 case GOT_STATUS_MODIFY:
8084 /* XXX could sb.st_mode be passed in by our caller? */
8085 if (lstat(ondisk_path, &sb) == -1) {
8086 err = got_error_from_errno2("lstat", ondisk_path);
8087 break;
8089 if (a->patch_cb) {
8090 if (status == GOT_STATUS_ADD) {
8091 int choice = GOT_PATCH_CHOICE_NONE;
8092 err = (*a->patch_cb)(&choice, a->patch_arg,
8093 status, ie->path, NULL, 1, 1);
8094 if (err)
8095 break;
8096 if (choice != GOT_PATCH_CHOICE_YES)
8097 break;
8098 } else {
8099 err = create_patched_content(&path_content, 0,
8100 staged_blob_id ? staged_blob_id : blob_id,
8101 ondisk_path, dirfd, de_name, ie->path,
8102 a->repo, a->patch_cb, a->patch_arg);
8103 if (err || path_content == NULL)
8104 break;
8107 err = got_object_blob_create(&new_staged_blob_id,
8108 path_content ? path_content : ondisk_path, a->repo);
8109 if (err)
8110 break;
8111 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8112 SHA1_DIGEST_LENGTH);
8113 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8114 stage = GOT_FILEIDX_STAGE_ADD;
8115 else
8116 stage = GOT_FILEIDX_STAGE_MODIFY;
8117 got_fileindex_entry_stage_set(ie, stage);
8118 if (S_ISLNK(sb.st_mode)) {
8119 int is_bad_symlink = 0;
8120 if (!a->allow_bad_symlinks) {
8121 char target_path[PATH_MAX];
8122 ssize_t target_len;
8123 target_len = readlink(ondisk_path, target_path,
8124 sizeof(target_path));
8125 if (target_len == -1) {
8126 err = got_error_from_errno2("readlink",
8127 ondisk_path);
8128 break;
8130 err = is_bad_symlink_target(&is_bad_symlink,
8131 target_path, target_len, ondisk_path,
8132 a->worktree->root_path);
8133 if (err)
8134 break;
8135 if (is_bad_symlink) {
8136 err = got_error_path(ondisk_path,
8137 GOT_ERR_BAD_SYMLINK);
8138 break;
8141 if (is_bad_symlink)
8142 got_fileindex_entry_staged_filetype_set(ie,
8143 GOT_FILEIDX_MODE_BAD_SYMLINK);
8144 else
8145 got_fileindex_entry_staged_filetype_set(ie,
8146 GOT_FILEIDX_MODE_SYMLINK);
8147 } else {
8148 got_fileindex_entry_staged_filetype_set(ie,
8149 GOT_FILEIDX_MODE_REGULAR_FILE);
8151 a->staged_something = 1;
8152 if (a->status_cb == NULL)
8153 break;
8154 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8155 get_staged_status(ie), relpath, blob_id,
8156 new_staged_blob_id, NULL, dirfd, de_name);
8157 if (err)
8158 break;
8160 * When staging the reverse of the staged diff,
8161 * implicitly unstage the file.
8163 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8164 sizeof(ie->blob_sha1)) == 0) {
8165 got_fileindex_entry_stage_set(ie,
8166 GOT_FILEIDX_STAGE_NONE);
8168 break;
8169 case GOT_STATUS_DELETE:
8170 if (staged_status == GOT_STATUS_DELETE)
8171 break;
8172 if (a->patch_cb) {
8173 int choice = GOT_PATCH_CHOICE_NONE;
8174 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8175 ie->path, NULL, 1, 1);
8176 if (err)
8177 break;
8178 if (choice == GOT_PATCH_CHOICE_NO)
8179 break;
8180 if (choice != GOT_PATCH_CHOICE_YES) {
8181 err = got_error(GOT_ERR_PATCH_CHOICE);
8182 break;
8185 stage = GOT_FILEIDX_STAGE_DELETE;
8186 got_fileindex_entry_stage_set(ie, stage);
8187 a->staged_something = 1;
8188 if (a->status_cb == NULL)
8189 break;
8190 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8191 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8192 de_name);
8193 break;
8194 case GOT_STATUS_NO_CHANGE:
8195 break;
8196 case GOT_STATUS_CONFLICT:
8197 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8198 break;
8199 case GOT_STATUS_NONEXISTENT:
8200 err = got_error_set_errno(ENOENT, relpath);
8201 break;
8202 default:
8203 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8204 break;
8207 if (path_content && unlink(path_content) == -1 && err == NULL)
8208 err = got_error_from_errno2("unlink", path_content);
8209 free(path_content);
8210 free(ondisk_path);
8211 free(new_staged_blob_id);
8212 return err;
8215 const struct got_error *
8216 got_worktree_stage(struct got_worktree *worktree,
8217 struct got_pathlist_head *paths,
8218 got_worktree_status_cb status_cb, void *status_arg,
8219 got_worktree_patch_cb patch_cb, void *patch_arg,
8220 int allow_bad_symlinks, struct got_repository *repo)
8222 const struct got_error *err = NULL, *sync_err, *unlockerr;
8223 struct got_pathlist_entry *pe;
8224 struct got_fileindex *fileindex = NULL;
8225 char *fileindex_path = NULL;
8226 struct got_reference *head_ref = NULL;
8227 struct got_object_id *head_commit_id = NULL;
8228 struct check_stage_ok_arg oka;
8229 struct stage_path_arg spa;
8231 err = lock_worktree(worktree, LOCK_EX);
8232 if (err)
8233 return err;
8235 err = got_ref_open(&head_ref, repo,
8236 got_worktree_get_head_ref_name(worktree), 0);
8237 if (err)
8238 goto done;
8239 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8240 if (err)
8241 goto done;
8242 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8243 if (err)
8244 goto done;
8246 /* Check pre-conditions before staging anything. */
8247 oka.head_commit_id = head_commit_id;
8248 oka.worktree = worktree;
8249 oka.fileindex = fileindex;
8250 oka.repo = repo;
8251 oka.have_changes = 0;
8252 TAILQ_FOREACH(pe, paths, entry) {
8253 err = worktree_status(worktree, pe->path, fileindex, repo,
8254 check_stage_ok, &oka, NULL, NULL, 1, 0);
8255 if (err)
8256 goto done;
8258 if (!oka.have_changes) {
8259 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8260 goto done;
8263 spa.worktree = worktree;
8264 spa.fileindex = fileindex;
8265 spa.repo = repo;
8266 spa.patch_cb = patch_cb;
8267 spa.patch_arg = patch_arg;
8268 spa.status_cb = status_cb;
8269 spa.status_arg = status_arg;
8270 spa.staged_something = 0;
8271 spa.allow_bad_symlinks = allow_bad_symlinks;
8272 TAILQ_FOREACH(pe, paths, entry) {
8273 err = worktree_status(worktree, pe->path, fileindex, repo,
8274 stage_path, &spa, NULL, NULL, 1, 0);
8275 if (err)
8276 goto done;
8278 if (!spa.staged_something) {
8279 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8280 goto done;
8283 sync_err = sync_fileindex(fileindex, fileindex_path);
8284 if (sync_err && err == NULL)
8285 err = sync_err;
8286 done:
8287 if (head_ref)
8288 got_ref_close(head_ref);
8289 free(head_commit_id);
8290 free(fileindex_path);
8291 if (fileindex)
8292 got_fileindex_free(fileindex);
8293 unlockerr = lock_worktree(worktree, LOCK_SH);
8294 if (unlockerr && err == NULL)
8295 err = unlockerr;
8296 return err;
8299 struct unstage_path_arg {
8300 struct got_worktree *worktree;
8301 struct got_fileindex *fileindex;
8302 struct got_repository *repo;
8303 got_worktree_checkout_cb progress_cb;
8304 void *progress_arg;
8305 got_worktree_patch_cb patch_cb;
8306 void *patch_arg;
8309 static const struct got_error *
8310 create_unstaged_content(char **path_unstaged_content,
8311 char **path_new_staged_content, struct got_object_id *blob_id,
8312 struct got_object_id *staged_blob_id, const char *relpath,
8313 struct got_repository *repo,
8314 got_worktree_patch_cb patch_cb, void *patch_arg)
8316 const struct got_error *err, *free_err;
8317 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8318 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8319 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8320 struct got_diffreg_result *diffreg_result = NULL;
8321 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8322 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8323 int fd1 = -1, fd2 = -1;
8325 *path_unstaged_content = NULL;
8326 *path_new_staged_content = NULL;
8328 err = got_object_id_str(&label1, blob_id);
8329 if (err)
8330 return err;
8332 fd1 = got_opentempfd();
8333 if (fd1 == -1) {
8334 err = got_error_from_errno("got_opentempfd");
8335 goto done;
8337 fd2 = got_opentempfd();
8338 if (fd2 == -1) {
8339 err = got_error_from_errno("got_opentempfd");
8340 goto done;
8343 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8344 if (err)
8345 goto done;
8347 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8348 if (err)
8349 goto done;
8351 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8352 if (err)
8353 goto done;
8355 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8356 fd2);
8357 if (err)
8358 goto done;
8360 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8361 if (err)
8362 goto done;
8364 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8365 if (err)
8366 goto done;
8368 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8369 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8370 if (err)
8371 goto done;
8373 err = got_opentemp_named(path_unstaged_content, &outfile,
8374 "got-unstaged-content");
8375 if (err)
8376 goto done;
8377 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8378 "got-new-staged-content");
8379 if (err)
8380 goto done;
8382 if (fseek(f1, 0L, SEEK_SET) == -1) {
8383 err = got_ferror(f1, GOT_ERR_IO);
8384 goto done;
8386 if (fseek(f2, 0L, SEEK_SET) == -1) {
8387 err = got_ferror(f2, GOT_ERR_IO);
8388 goto done;
8390 /* Count the number of actual changes in the diff result. */
8391 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8392 struct diff_chunk_context cc = {};
8393 diff_chunk_context_load_change(&cc, &nchunks_used,
8394 diffreg_result->result, n, 0);
8395 nchanges++;
8397 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8398 int choice;
8399 err = apply_or_reject_change(&choice, &nchunks_used,
8400 diffreg_result->result, n, relpath, f1, f2,
8401 &line_cur1, &line_cur2,
8402 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8403 if (err)
8404 goto done;
8405 if (choice == GOT_PATCH_CHOICE_YES)
8406 have_content = 1;
8407 else
8408 have_rejected_content = 1;
8409 if (choice == GOT_PATCH_CHOICE_QUIT)
8410 break;
8412 if (have_content || have_rejected_content)
8413 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8414 outfile, rejectfile);
8415 done:
8416 free(label1);
8417 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8418 err = got_error_from_errno("close");
8419 if (blob)
8420 got_object_blob_close(blob);
8421 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8422 err = got_error_from_errno("close");
8423 if (staged_blob)
8424 got_object_blob_close(staged_blob);
8425 free_err = got_diffreg_result_free(diffreg_result);
8426 if (free_err && err == NULL)
8427 err = free_err;
8428 if (f1 && fclose(f1) == EOF && err == NULL)
8429 err = got_error_from_errno2("fclose", path1);
8430 if (f2 && fclose(f2) == EOF && err == NULL)
8431 err = got_error_from_errno2("fclose", path2);
8432 if (outfile && fclose(outfile) == EOF && err == NULL)
8433 err = got_error_from_errno2("fclose", *path_unstaged_content);
8434 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8435 err = got_error_from_errno2("fclose", *path_new_staged_content);
8436 if (path1 && unlink(path1) == -1 && err == NULL)
8437 err = got_error_from_errno2("unlink", path1);
8438 if (path2 && unlink(path2) == -1 && err == NULL)
8439 err = got_error_from_errno2("unlink", path2);
8440 if (err || !have_content) {
8441 if (*path_unstaged_content &&
8442 unlink(*path_unstaged_content) == -1 && err == NULL)
8443 err = got_error_from_errno2("unlink",
8444 *path_unstaged_content);
8445 free(*path_unstaged_content);
8446 *path_unstaged_content = NULL;
8448 if (err || !have_content || !have_rejected_content) {
8449 if (*path_new_staged_content &&
8450 unlink(*path_new_staged_content) == -1 && err == NULL)
8451 err = got_error_from_errno2("unlink",
8452 *path_new_staged_content);
8453 free(*path_new_staged_content);
8454 *path_new_staged_content = NULL;
8456 free(path1);
8457 free(path2);
8458 return err;
8461 static const struct got_error *
8462 unstage_hunks(struct got_object_id *staged_blob_id,
8463 struct got_blob_object *blob_base,
8464 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8465 const char *ondisk_path, const char *label_orig,
8466 struct got_worktree *worktree, struct got_repository *repo,
8467 got_worktree_patch_cb patch_cb, void *patch_arg,
8468 got_worktree_checkout_cb progress_cb, void *progress_arg)
8470 const struct got_error *err = NULL;
8471 char *path_unstaged_content = NULL;
8472 char *path_new_staged_content = NULL;
8473 char *parent = NULL, *base_path = NULL;
8474 char *blob_base_path = NULL;
8475 struct got_object_id *new_staged_blob_id = NULL;
8476 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8477 struct stat sb;
8479 err = create_unstaged_content(&path_unstaged_content,
8480 &path_new_staged_content, blob_id, staged_blob_id,
8481 ie->path, repo, patch_cb, patch_arg);
8482 if (err)
8483 return err;
8485 if (path_unstaged_content == NULL)
8486 return NULL;
8488 if (path_new_staged_content) {
8489 err = got_object_blob_create(&new_staged_blob_id,
8490 path_new_staged_content, repo);
8491 if (err)
8492 goto done;
8495 f = fopen(path_unstaged_content, "re");
8496 if (f == NULL) {
8497 err = got_error_from_errno2("fopen",
8498 path_unstaged_content);
8499 goto done;
8501 if (fstat(fileno(f), &sb) == -1) {
8502 err = got_error_from_errno2("fstat", path_unstaged_content);
8503 goto done;
8505 if (got_fileindex_entry_staged_filetype_get(ie) ==
8506 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8507 char link_target[PATH_MAX];
8508 size_t r;
8509 r = fread(link_target, 1, sizeof(link_target), f);
8510 if (r == 0 && ferror(f)) {
8511 err = got_error_from_errno("fread");
8512 goto done;
8514 if (r >= sizeof(link_target)) { /* should not happen */
8515 err = got_error(GOT_ERR_NO_SPACE);
8516 goto done;
8518 link_target[r] = '\0';
8519 err = merge_symlink(worktree, blob_base,
8520 ondisk_path, ie->path, label_orig, link_target,
8521 worktree->base_commit_id, repo, progress_cb,
8522 progress_arg);
8523 } else {
8524 int local_changes_subsumed;
8526 err = got_path_dirname(&parent, ondisk_path);
8527 if (err)
8528 return err;
8530 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8531 parent) == -1) {
8532 err = got_error_from_errno("asprintf");
8533 base_path = NULL;
8534 goto done;
8537 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8538 if (err)
8539 goto done;
8540 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8541 blob_base);
8542 if (err)
8543 goto done;
8546 * In order the run a 3-way merge with a symlink we copy the symlink's
8547 * target path into a temporary file and use that file with diff3.
8549 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8550 err = dump_symlink_target_path_to_file(&f_deriv2,
8551 ondisk_path);
8552 if (err)
8553 goto done;
8554 } else {
8555 int fd;
8556 fd = open(ondisk_path,
8557 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8558 if (fd == -1) {
8559 err = got_error_from_errno2("open", ondisk_path);
8560 goto done;
8562 f_deriv2 = fdopen(fd, "r");
8563 if (f_deriv2 == NULL) {
8564 err = got_error_from_errno2("fdopen", ondisk_path);
8565 close(fd);
8566 goto done;
8570 err = merge_file(&local_changes_subsumed, worktree,
8571 f_base, f, f_deriv2, ondisk_path, ie->path,
8572 got_fileindex_perms_to_st(ie),
8573 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8574 repo, progress_cb, progress_arg);
8576 if (err)
8577 goto done;
8579 if (new_staged_blob_id) {
8580 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8581 SHA1_DIGEST_LENGTH);
8582 } else {
8583 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8584 got_fileindex_entry_staged_filetype_set(ie, 0);
8586 done:
8587 free(new_staged_blob_id);
8588 if (path_unstaged_content &&
8589 unlink(path_unstaged_content) == -1 && err == NULL)
8590 err = got_error_from_errno2("unlink", path_unstaged_content);
8591 if (path_new_staged_content &&
8592 unlink(path_new_staged_content) == -1 && err == NULL)
8593 err = got_error_from_errno2("unlink", path_new_staged_content);
8594 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8595 err = got_error_from_errno2("unlink", blob_base_path);
8596 if (f_base && fclose(f_base) == EOF && err == NULL)
8597 err = got_error_from_errno2("fclose", path_unstaged_content);
8598 if (f && fclose(f) == EOF && err == NULL)
8599 err = got_error_from_errno2("fclose", path_unstaged_content);
8600 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8601 err = got_error_from_errno2("fclose", ondisk_path);
8602 free(path_unstaged_content);
8603 free(path_new_staged_content);
8604 free(blob_base_path);
8605 free(parent);
8606 free(base_path);
8607 return err;
8610 static const struct got_error *
8611 unstage_path(void *arg, unsigned char status,
8612 unsigned char staged_status, const char *relpath,
8613 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8614 struct got_object_id *commit_id, int dirfd, const char *de_name)
8616 const struct got_error *err = NULL;
8617 struct unstage_path_arg *a = arg;
8618 struct got_fileindex_entry *ie;
8619 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8620 char *ondisk_path = NULL;
8621 char *id_str = NULL, *label_orig = NULL;
8622 int local_changes_subsumed;
8623 struct stat sb;
8624 int fd1 = -1, fd2 = -1;
8626 if (staged_status != GOT_STATUS_ADD &&
8627 staged_status != GOT_STATUS_MODIFY &&
8628 staged_status != GOT_STATUS_DELETE)
8629 return NULL;
8631 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8632 if (ie == NULL)
8633 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8635 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8636 == -1)
8637 return got_error_from_errno("asprintf");
8639 err = got_object_id_str(&id_str,
8640 commit_id ? commit_id : a->worktree->base_commit_id);
8641 if (err)
8642 goto done;
8643 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8644 id_str) == -1) {
8645 err = got_error_from_errno("asprintf");
8646 goto done;
8649 fd1 = got_opentempfd();
8650 if (fd1 == -1) {
8651 err = got_error_from_errno("got_opentempfd");
8652 goto done;
8654 fd2 = got_opentempfd();
8655 if (fd2 == -1) {
8656 err = got_error_from_errno("got_opentempfd");
8657 goto done;
8660 switch (staged_status) {
8661 case GOT_STATUS_MODIFY:
8662 err = got_object_open_as_blob(&blob_base, a->repo,
8663 blob_id, 8192, fd1);
8664 if (err)
8665 break;
8666 /* fall through */
8667 case GOT_STATUS_ADD:
8668 if (a->patch_cb) {
8669 if (staged_status == GOT_STATUS_ADD) {
8670 int choice = GOT_PATCH_CHOICE_NONE;
8671 err = (*a->patch_cb)(&choice, a->patch_arg,
8672 staged_status, ie->path, NULL, 1, 1);
8673 if (err)
8674 break;
8675 if (choice != GOT_PATCH_CHOICE_YES)
8676 break;
8677 } else {
8678 err = unstage_hunks(staged_blob_id,
8679 blob_base, blob_id, ie, ondisk_path,
8680 label_orig, a->worktree, a->repo,
8681 a->patch_cb, a->patch_arg,
8682 a->progress_cb, a->progress_arg);
8683 break; /* Done with this file. */
8686 err = got_object_open_as_blob(&blob_staged, a->repo,
8687 staged_blob_id, 8192, fd2);
8688 if (err)
8689 break;
8690 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8691 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8692 case GOT_FILEIDX_MODE_REGULAR_FILE:
8693 err = merge_blob(&local_changes_subsumed, a->worktree,
8694 blob_base, ondisk_path, relpath,
8695 got_fileindex_perms_to_st(ie), label_orig,
8696 blob_staged, commit_id ? commit_id :
8697 a->worktree->base_commit_id, a->repo,
8698 a->progress_cb, a->progress_arg);
8699 break;
8700 case GOT_FILEIDX_MODE_SYMLINK:
8701 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8702 char *staged_target;
8703 err = got_object_blob_read_to_str(
8704 &staged_target, blob_staged);
8705 if (err)
8706 goto done;
8707 err = merge_symlink(a->worktree, blob_base,
8708 ondisk_path, relpath, label_orig,
8709 staged_target, commit_id ? commit_id :
8710 a->worktree->base_commit_id,
8711 a->repo, a->progress_cb, a->progress_arg);
8712 free(staged_target);
8713 } else {
8714 err = merge_blob(&local_changes_subsumed,
8715 a->worktree, blob_base, ondisk_path,
8716 relpath, got_fileindex_perms_to_st(ie),
8717 label_orig, blob_staged,
8718 commit_id ? commit_id :
8719 a->worktree->base_commit_id, a->repo,
8720 a->progress_cb, a->progress_arg);
8722 break;
8723 default:
8724 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8725 break;
8727 if (err == NULL) {
8728 got_fileindex_entry_stage_set(ie,
8729 GOT_FILEIDX_STAGE_NONE);
8730 got_fileindex_entry_staged_filetype_set(ie, 0);
8732 break;
8733 case GOT_STATUS_DELETE:
8734 if (a->patch_cb) {
8735 int choice = GOT_PATCH_CHOICE_NONE;
8736 err = (*a->patch_cb)(&choice, a->patch_arg,
8737 staged_status, ie->path, NULL, 1, 1);
8738 if (err)
8739 break;
8740 if (choice == GOT_PATCH_CHOICE_NO)
8741 break;
8742 if (choice != GOT_PATCH_CHOICE_YES) {
8743 err = got_error(GOT_ERR_PATCH_CHOICE);
8744 break;
8747 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8748 got_fileindex_entry_staged_filetype_set(ie, 0);
8749 err = get_file_status(&status, &sb, ie, ondisk_path,
8750 dirfd, de_name, a->repo);
8751 if (err)
8752 break;
8753 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8754 break;
8756 done:
8757 free(ondisk_path);
8758 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8759 err = got_error_from_errno("close");
8760 if (blob_base)
8761 got_object_blob_close(blob_base);
8762 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8763 err = got_error_from_errno("close");
8764 if (blob_staged)
8765 got_object_blob_close(blob_staged);
8766 free(id_str);
8767 free(label_orig);
8768 return err;
8771 const struct got_error *
8772 got_worktree_unstage(struct got_worktree *worktree,
8773 struct got_pathlist_head *paths,
8774 got_worktree_checkout_cb progress_cb, void *progress_arg,
8775 got_worktree_patch_cb patch_cb, void *patch_arg,
8776 struct got_repository *repo)
8778 const struct got_error *err = NULL, *sync_err, *unlockerr;
8779 struct got_pathlist_entry *pe;
8780 struct got_fileindex *fileindex = NULL;
8781 char *fileindex_path = NULL;
8782 struct unstage_path_arg upa;
8784 err = lock_worktree(worktree, LOCK_EX);
8785 if (err)
8786 return err;
8788 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8789 if (err)
8790 goto done;
8792 upa.worktree = worktree;
8793 upa.fileindex = fileindex;
8794 upa.repo = repo;
8795 upa.progress_cb = progress_cb;
8796 upa.progress_arg = progress_arg;
8797 upa.patch_cb = patch_cb;
8798 upa.patch_arg = patch_arg;
8799 TAILQ_FOREACH(pe, paths, entry) {
8800 err = worktree_status(worktree, pe->path, fileindex, repo,
8801 unstage_path, &upa, NULL, NULL, 1, 0);
8802 if (err)
8803 goto done;
8806 sync_err = sync_fileindex(fileindex, fileindex_path);
8807 if (sync_err && err == NULL)
8808 err = sync_err;
8809 done:
8810 free(fileindex_path);
8811 if (fileindex)
8812 got_fileindex_free(fileindex);
8813 unlockerr = lock_worktree(worktree, LOCK_SH);
8814 if (unlockerr && err == NULL)
8815 err = unlockerr;
8816 return err;
8819 struct report_file_info_arg {
8820 struct got_worktree *worktree;
8821 got_worktree_path_info_cb info_cb;
8822 void *info_arg;
8823 struct got_pathlist_head *paths;
8824 got_cancel_cb cancel_cb;
8825 void *cancel_arg;
8828 static const struct got_error *
8829 report_file_info(void *arg, struct got_fileindex_entry *ie)
8831 struct report_file_info_arg *a = arg;
8832 struct got_pathlist_entry *pe;
8833 struct got_object_id blob_id, staged_blob_id, commit_id;
8834 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8835 struct got_object_id *commit_idp = NULL;
8836 int stage;
8838 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8839 return got_error(GOT_ERR_CANCELLED);
8841 TAILQ_FOREACH(pe, a->paths, entry) {
8842 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8843 got_path_is_child(ie->path, pe->path, pe->path_len))
8844 break;
8846 if (pe == NULL) /* not found */
8847 return NULL;
8849 if (got_fileindex_entry_has_blob(ie)) {
8850 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8851 blob_idp = &blob_id;
8853 stage = got_fileindex_entry_stage_get(ie);
8854 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8855 stage == GOT_FILEIDX_STAGE_ADD) {
8856 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8857 SHA1_DIGEST_LENGTH);
8858 staged_blob_idp = &staged_blob_id;
8861 if (got_fileindex_entry_has_commit(ie)) {
8862 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8863 commit_idp = &commit_id;
8866 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8867 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8870 const struct got_error *
8871 got_worktree_path_info(struct got_worktree *worktree,
8872 struct got_pathlist_head *paths,
8873 got_worktree_path_info_cb info_cb, void *info_arg,
8874 got_cancel_cb cancel_cb, void *cancel_arg)
8877 const struct got_error *err = NULL, *unlockerr;
8878 struct got_fileindex *fileindex = NULL;
8879 char *fileindex_path = NULL;
8880 struct report_file_info_arg arg;
8882 err = lock_worktree(worktree, LOCK_SH);
8883 if (err)
8884 return err;
8886 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8887 if (err)
8888 goto done;
8890 arg.worktree = worktree;
8891 arg.info_cb = info_cb;
8892 arg.info_arg = info_arg;
8893 arg.paths = paths;
8894 arg.cancel_cb = cancel_cb;
8895 arg.cancel_arg = cancel_arg;
8896 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8897 &arg);
8898 done:
8899 free(fileindex_path);
8900 if (fileindex)
8901 got_fileindex_free(fileindex);
8902 unlockerr = lock_worktree(worktree, LOCK_UN);
8903 if (unlockerr && err == NULL)
8904 err = unlockerr;
8905 return err;
8908 static const struct got_error *
8909 patch_check_path(const char *p, char **path, unsigned char *status,
8910 unsigned char *staged_status, struct got_fileindex *fileindex,
8911 struct got_worktree *worktree, struct got_repository *repo)
8913 const struct got_error *err;
8914 struct got_fileindex_entry *ie;
8915 struct stat sb;
8916 char *ondisk_path = NULL;
8918 err = got_worktree_resolve_path(path, worktree, p);
8919 if (err)
8920 return err;
8922 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8923 *path[0] ? "/" : "", *path) == -1)
8924 return got_error_from_errno("asprintf");
8926 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8927 if (ie) {
8928 *staged_status = get_staged_status(ie);
8929 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8930 repo);
8931 if (err)
8932 goto done;
8933 } else {
8934 *staged_status = GOT_STATUS_NO_CHANGE;
8935 *status = GOT_STATUS_UNVERSIONED;
8936 if (lstat(ondisk_path, &sb) == -1) {
8937 if (errno != ENOENT) {
8938 err = got_error_from_errno2("lstat",
8939 ondisk_path);
8940 goto done;
8942 *status = GOT_STATUS_NONEXISTENT;
8946 done:
8947 free(ondisk_path);
8948 return err;
8951 static const struct got_error *
8952 patch_can_rm(const char *path, unsigned char status,
8953 unsigned char staged_status)
8955 if (status == GOT_STATUS_NONEXISTENT)
8956 return got_error_set_errno(ENOENT, path);
8957 if (status != GOT_STATUS_NO_CHANGE &&
8958 status != GOT_STATUS_ADD &&
8959 status != GOT_STATUS_MODIFY &&
8960 status != GOT_STATUS_MODE_CHANGE)
8961 return got_error_path(path, GOT_ERR_FILE_STATUS);
8962 if (staged_status == GOT_STATUS_DELETE)
8963 return got_error_path(path, GOT_ERR_FILE_STATUS);
8964 return NULL;
8967 static const struct got_error *
8968 patch_can_add(const char *path, unsigned char status)
8970 if (status != GOT_STATUS_NONEXISTENT)
8971 return got_error_path(path, GOT_ERR_FILE_STATUS);
8972 return NULL;
8975 static const struct got_error *
8976 patch_can_edit(const char *path, unsigned char status,
8977 unsigned char staged_status)
8979 if (status == GOT_STATUS_NONEXISTENT)
8980 return got_error_set_errno(ENOENT, path);
8981 if (status != GOT_STATUS_NO_CHANGE &&
8982 status != GOT_STATUS_ADD &&
8983 status != GOT_STATUS_MODIFY)
8984 return got_error_path(path, GOT_ERR_FILE_STATUS);
8985 if (staged_status == GOT_STATUS_DELETE)
8986 return got_error_path(path, GOT_ERR_FILE_STATUS);
8987 return NULL;
8990 const struct got_error *
8991 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8992 char **fileindex_path, struct got_worktree *worktree)
8994 return open_fileindex(fileindex, fileindex_path, worktree);
8997 const struct got_error *
8998 got_worktree_patch_check_path(const char *old, const char *new,
8999 char **oldpath, char **newpath, struct got_worktree *worktree,
9000 struct got_repository *repo, struct got_fileindex *fileindex)
9002 const struct got_error *err = NULL;
9003 int file_renamed = 0;
9004 unsigned char status_old, staged_status_old;
9005 unsigned char status_new, staged_status_new;
9007 *oldpath = NULL;
9008 *newpath = NULL;
9010 err = patch_check_path(old != NULL ? old : new, oldpath,
9011 &status_old, &staged_status_old, fileindex, worktree, repo);
9012 if (err)
9013 goto done;
9015 err = patch_check_path(new != NULL ? new : old, newpath,
9016 &status_new, &staged_status_new, fileindex, worktree, repo);
9017 if (err)
9018 goto done;
9020 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9021 file_renamed = 1;
9023 if (old != NULL && new == NULL)
9024 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9025 else if (file_renamed) {
9026 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9027 if (err == NULL)
9028 err = patch_can_add(*newpath, status_new);
9029 } else if (old == NULL)
9030 err = patch_can_add(*newpath, status_new);
9031 else
9032 err = patch_can_edit(*newpath, status_new, staged_status_new);
9034 done:
9035 if (err) {
9036 free(*oldpath);
9037 *oldpath = NULL;
9038 free(*newpath);
9039 *newpath = NULL;
9041 return err;
9044 const struct got_error *
9045 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9046 struct got_worktree *worktree, struct got_fileindex *fileindex,
9047 got_worktree_checkout_cb progress_cb, void *progress_arg)
9049 struct schedule_addition_args saa;
9051 memset(&saa, 0, sizeof(saa));
9052 saa.worktree = worktree;
9053 saa.fileindex = fileindex;
9054 saa.progress_cb = progress_cb;
9055 saa.progress_arg = progress_arg;
9056 saa.repo = repo;
9058 return worktree_status(worktree, path, fileindex, repo,
9059 schedule_addition, &saa, NULL, NULL, 1, 0);
9062 const struct got_error *
9063 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9064 struct got_worktree *worktree, struct got_fileindex *fileindex,
9065 got_worktree_delete_cb progress_cb, void *progress_arg)
9067 struct schedule_deletion_args sda;
9069 memset(&sda, 0, sizeof(sda));
9070 sda.worktree = worktree;
9071 sda.fileindex = fileindex;
9072 sda.progress_cb = progress_cb;
9073 sda.progress_arg = progress_arg;
9074 sda.repo = repo;
9075 sda.delete_local_mods = 0;
9076 sda.keep_on_disk = 0;
9077 sda.ignore_missing_paths = 0;
9078 sda.status_codes = NULL;
9080 return worktree_status(worktree, path, fileindex, repo,
9081 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9084 const struct got_error *
9085 got_worktree_patch_complete(struct got_fileindex *fileindex,
9086 const char *fileindex_path)
9088 const struct got_error *err = NULL;
9090 err = sync_fileindex(fileindex, fileindex_path);
9091 got_fileindex_free(fileindex);
9093 return err;