portable: add back sys/queue.h
[got-portable.git] / lib / worktree.c
blob553e6bf2e7d7eb1a347e0a5a1542621d2124c6bb
1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
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)
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;
79 static const struct got_error *
80 update_meta_file(const char *path_got, const char *name, const char *content)
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;
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;
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.
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;
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.
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 *);
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.
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;
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.
963 static const struct got_error *
964 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
965 struct got_blob_object *blob_orig, const char *ondisk_path,
966 const char *path, uint16_t st_mode, const char *label_orig,
967 struct got_blob_object *blob_deriv,
968 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
969 got_worktree_checkout_cb progress_cb, void *progress_arg)
971 const struct got_error *err = NULL;
972 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
973 char *blob_orig_path = NULL;
974 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
975 char *label_deriv = NULL, *parent = NULL;
977 *local_changes_subsumed = 0;
979 err = got_path_dirname(&parent, ondisk_path);
980 if (err)
981 return err;
983 if (blob_orig) {
984 if (asprintf(&base_path, "%s/got-merge-blob-orig",
985 parent) == -1) {
986 err = got_error_from_errno("asprintf");
987 base_path = NULL;
988 goto done;
991 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
992 if (err)
993 goto done;
994 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
995 blob_orig);
996 if (err)
997 goto done;
998 free(base_path);
999 } else {
1001 * No common ancestor exists. This is an "add vs add" conflict
1002 * and we simply use an empty ancestor file to make both files
1003 * appear in the merged result in their entirety.
1005 f_orig = got_opentemp();
1006 if (f_orig == NULL) {
1007 err = got_error_from_errno("got_opentemp");
1008 goto done;
1012 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1013 err = got_error_from_errno("asprintf");
1014 base_path = NULL;
1015 goto done;
1018 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1019 if (err)
1020 goto done;
1021 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1022 blob_deriv);
1023 if (err)
1024 goto done;
1026 err = got_object_id_str(&id_str, deriv_base_commit_id);
1027 if (err)
1028 goto done;
1029 if (asprintf(&label_deriv, "%s: commit %s",
1030 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1031 err = got_error_from_errno("asprintf");
1032 goto done;
1036 * In order the run a 3-way merge with a symlink we copy the symlink's
1037 * target path into a temporary file and use that file with diff3.
1039 if (S_ISLNK(st_mode)) {
1040 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1041 if (err)
1042 goto done;
1043 } else {
1044 int fd;
1045 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1046 if (fd == -1) {
1047 err = got_error_from_errno2("open", ondisk_path);
1048 goto done;
1050 f_deriv2 = fdopen(fd, "r");
1051 if (f_deriv2 == NULL) {
1052 err = got_error_from_errno2("fdopen", ondisk_path);
1053 close(fd);
1054 goto done;
1058 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1059 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1060 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1061 done:
1062 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1063 err = got_error_from_errno("fclose");
1064 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 free(base_path);
1069 if (blob_orig_path) {
1070 unlink(blob_orig_path);
1071 free(blob_orig_path);
1073 if (blob_deriv_path) {
1074 unlink(blob_deriv_path);
1075 free(blob_deriv_path);
1077 free(id_str);
1078 free(label_deriv);
1079 free(parent);
1080 return err;
1083 static const struct got_error *
1084 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1085 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1086 int wt_fd, const char *path, struct got_object_id *blob_id)
1088 const struct got_error *err = NULL;
1089 struct got_fileindex_entry *new_ie;
1091 *new_iep = NULL;
1093 err = got_fileindex_entry_alloc(&new_ie, path);
1094 if (err)
1095 return err;
1097 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1098 blob_id->sha1, base_commit_id->sha1, 1);
1099 if (err)
1100 goto done;
1102 err = got_fileindex_entry_add(fileindex, new_ie);
1103 done:
1104 if (err)
1105 got_fileindex_entry_free(new_ie);
1106 else
1107 *new_iep = new_ie;
1108 return err;
1111 static mode_t
1112 get_ondisk_perms(int executable, mode_t st_mode)
1114 mode_t xbits = S_IXUSR;
1116 if (executable) {
1117 /* Map read bits to execute bits. */
1118 if (st_mode & S_IRGRP)
1119 xbits |= S_IXGRP;
1120 if (st_mode & S_IROTH)
1121 xbits |= S_IXOTH;
1122 return st_mode | xbits;
1125 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 /* forward declaration */
1129 static const struct got_error *
1130 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 const char *path, mode_t te_mode, mode_t st_mode,
1132 struct got_blob_object *blob, int restoring_missing_file,
1133 int reverting_versioned_file, int installing_bad_symlink,
1134 int path_is_unversioned, struct got_repository *repo,
1135 got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 * This function assumes that the provided symlink target points at a
1139 * safe location in the work tree!
1141 static const struct got_error *
1142 replace_existing_symlink(int *did_something, const char *ondisk_path,
1143 const char *target_path, size_t target_len)
1145 const struct got_error *err = NULL;
1146 ssize_t elen;
1147 char etarget[PATH_MAX];
1148 int fd;
1150 *did_something = 0;
1153 * "Bad" symlinks (those pointing outside the work tree or into the
1154 * .got directory) are installed in the work tree as a regular file
1155 * which contains the bad symlink target path.
1156 * The new symlink target has already been checked for safety by our
1157 * caller. If we can successfully open a regular file then we simply
1158 * replace this file with a symlink below.
1160 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1161 if (fd == -1) {
1162 if (!got_err_open_nofollow_on_symlink())
1163 return got_error_from_errno2("open", ondisk_path);
1165 /* We are updating an existing on-disk symlink. */
1166 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1167 if (elen == -1)
1168 return got_error_from_errno2("readlink", ondisk_path);
1170 if (elen == target_len &&
1171 memcmp(etarget, target_path, target_len) == 0)
1172 return NULL; /* nothing to do */
1175 *did_something = 1;
1176 err = update_symlink(ondisk_path, target_path, target_len);
1177 if (fd != -1 && close(fd) == -1 && err == NULL)
1178 err = got_error_from_errno2("close", ondisk_path);
1179 return err;
1182 static const struct got_error *
1183 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1184 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1186 const struct got_error *err = NULL;
1187 char canonpath[PATH_MAX];
1188 char *path_got = NULL;
1190 *is_bad_symlink = 0;
1192 if (target_len >= sizeof(canonpath)) {
1193 *is_bad_symlink = 1;
1194 return NULL;
1198 * We do not use realpath(3) to resolve the symlink's target
1199 * path because we don't want to resolve symlinks recursively.
1200 * Instead we make the path absolute and then canonicalize it.
1201 * Relative symlink target lookup should begin at the directory
1202 * in which the blob object is being installed.
1204 if (!got_path_is_absolute(target_path)) {
1205 char *abspath, *parent;
1206 err = got_path_dirname(&parent, ondisk_path);
1207 if (err)
1208 return err;
1209 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1210 free(parent);
1211 return got_error_from_errno("asprintf");
1213 free(parent);
1214 if (strlen(abspath) >= sizeof(canonpath)) {
1215 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1216 free(abspath);
1217 return err;
1219 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1220 free(abspath);
1221 if (err)
1222 return err;
1223 } else {
1224 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1225 if (err)
1226 return err;
1229 /* Only allow symlinks pointing at paths within the work tree. */
1230 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1231 *is_bad_symlink = 1;
1232 return NULL;
1235 /* Do not allow symlinks pointing into the .got directory. */
1236 if (asprintf(&path_got, "%s/%s", wtroot_path,
1237 GOT_WORKTREE_GOT_DIR) == -1)
1238 return got_error_from_errno("asprintf");
1239 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1240 *is_bad_symlink = 1;
1242 free(path_got);
1243 return NULL;
1246 static const struct got_error *
1247 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1248 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1249 int restoring_missing_file, int reverting_versioned_file,
1250 int path_is_unversioned, int allow_bad_symlinks,
1251 struct got_repository *repo,
1252 got_worktree_checkout_cb progress_cb, void *progress_arg)
1254 const struct got_error *err = NULL;
1255 char target_path[PATH_MAX];
1256 size_t len, target_len = 0;
1257 char *path_got = NULL;
1258 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1259 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1261 *is_bad_symlink = 0;
1264 * Blob object content specifies the target path of the link.
1265 * If a symbolic link cannot be installed we instead create
1266 * a regular file which contains the link target path stored
1267 * in the blob object.
1269 do {
1270 err = got_object_blob_read_block(&len, blob);
1271 if (len + target_len >= sizeof(target_path)) {
1272 /* Path too long; install as a regular file. */
1273 *is_bad_symlink = 1;
1274 got_object_blob_rewind(blob);
1275 return install_blob(worktree, ondisk_path, path,
1276 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1277 restoring_missing_file, reverting_versioned_file,
1278 1, path_is_unversioned, repo, progress_cb,
1279 progress_arg);
1281 if (len > 0) {
1282 /* Skip blob object header first time around. */
1283 memcpy(target_path + target_len, buf + hdrlen,
1284 len - hdrlen);
1285 target_len += len - hdrlen;
1286 hdrlen = 0;
1288 } while (len != 0);
1289 target_path[target_len] = '\0';
1291 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1292 ondisk_path, worktree->root_path);
1293 if (err)
1294 return err;
1296 if (*is_bad_symlink && !allow_bad_symlinks) {
1297 /* install as a regular file */
1298 got_object_blob_rewind(blob);
1299 err = install_blob(worktree, ondisk_path, path,
1300 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1301 restoring_missing_file, reverting_versioned_file, 1,
1302 path_is_unversioned, repo, progress_cb, progress_arg);
1303 goto done;
1306 if (symlink(target_path, ondisk_path) == -1) {
1307 if (errno == EEXIST) {
1308 int symlink_replaced;
1309 if (path_is_unversioned) {
1310 err = (*progress_cb)(progress_arg,
1311 GOT_STATUS_UNVERSIONED, path);
1312 goto done;
1314 err = replace_existing_symlink(&symlink_replaced,
1315 ondisk_path, target_path, target_len);
1316 if (err)
1317 goto done;
1318 if (progress_cb) {
1319 if (symlink_replaced) {
1320 err = (*progress_cb)(progress_arg,
1321 reverting_versioned_file ?
1322 GOT_STATUS_REVERT :
1323 GOT_STATUS_UPDATE, path);
1324 } else {
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_EXISTS, path);
1329 goto done; /* Nothing else to do. */
1332 if (errno == ENOENT) {
1333 char *parent;
1334 err = got_path_dirname(&parent, ondisk_path);
1335 if (err)
1336 goto done;
1337 err = add_dir_on_disk(worktree, parent);
1338 free(parent);
1339 if (err)
1340 goto done;
1342 * Retry, and fall through to error handling
1343 * below if this second attempt fails.
1345 if (symlink(target_path, ondisk_path) != -1) {
1346 err = NULL; /* success */
1347 goto done;
1351 /* Handle errors from first or second creation attempt. */
1352 if (errno == ENAMETOOLONG) {
1353 /* bad target path; install as a regular file */
1354 *is_bad_symlink = 1;
1355 got_object_blob_rewind(blob);
1356 err = install_blob(worktree, ondisk_path, path,
1357 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1358 restoring_missing_file, reverting_versioned_file, 1,
1359 path_is_unversioned, repo,
1360 progress_cb, progress_arg);
1361 } else if (errno == ENOTDIR) {
1362 err = got_error_path(ondisk_path,
1363 GOT_ERR_FILE_OBSTRUCTED);
1364 } else {
1365 err = got_error_from_errno3("symlink",
1366 target_path, ondisk_path);
1368 } else if (progress_cb)
1369 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1370 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1371 done:
1372 free(path_got);
1373 return err;
1376 static const struct got_error *
1377 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1378 const char *path, mode_t te_mode, mode_t st_mode,
1379 struct got_blob_object *blob, int restoring_missing_file,
1380 int reverting_versioned_file, int installing_bad_symlink,
1381 int path_is_unversioned, struct got_repository *repo,
1382 got_worktree_checkout_cb progress_cb, void *progress_arg)
1384 const struct got_error *err = NULL;
1385 int fd = -1;
1386 size_t len, hdrlen;
1387 int update = 0;
1388 char *tmppath = NULL;
1390 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1391 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1392 if (fd == -1) {
1393 if (errno == ENOENT) {
1394 char *parent;
1395 err = got_path_dirname(&parent, path);
1396 if (err)
1397 return err;
1398 err = add_dir_on_disk(worktree, parent);
1399 free(parent);
1400 if (err)
1401 return err;
1402 fd = open(ondisk_path,
1403 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1404 GOT_DEFAULT_FILE_MODE);
1405 if (fd == -1)
1406 return got_error_from_errno2("open",
1407 ondisk_path);
1408 } else if (errno == EEXIST) {
1409 if (path_is_unversioned) {
1410 err = (*progress_cb)(progress_arg,
1411 GOT_STATUS_UNVERSIONED, path);
1412 goto done;
1414 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1415 !S_ISREG(st_mode) && !installing_bad_symlink) {
1416 /* TODO file is obstructed; do something */
1417 err = got_error_path(ondisk_path,
1418 GOT_ERR_FILE_OBSTRUCTED);
1419 goto done;
1420 } else {
1421 err = got_opentemp_named_fd(&tmppath, &fd,
1422 ondisk_path);
1423 if (err)
1424 goto done;
1425 update = 1;
1427 } else
1428 return got_error_from_errno2("open", ondisk_path);
1431 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1432 err = got_error_from_errno2("fchmod",
1433 update ? tmppath : ondisk_path);
1434 goto done;
1437 if (progress_cb) {
1438 if (restoring_missing_file)
1439 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1440 path);
1441 else if (reverting_versioned_file)
1442 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1443 path);
1444 else
1445 err = (*progress_cb)(progress_arg,
1446 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1447 if (err)
1448 goto done;
1451 hdrlen = got_object_blob_get_hdrlen(blob);
1452 do {
1453 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1454 err = got_object_blob_read_block(&len, blob);
1455 if (err)
1456 break;
1457 if (len > 0) {
1458 /* Skip blob object header first time around. */
1459 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1460 if (outlen == -1) {
1461 err = got_error_from_errno("write");
1462 goto done;
1463 } else if (outlen != len - hdrlen) {
1464 err = got_error(GOT_ERR_IO);
1465 goto done;
1467 hdrlen = 0;
1469 } while (len != 0);
1471 if (fsync(fd) != 0) {
1472 err = got_error_from_errno("fsync");
1473 goto done;
1476 if (update) {
1477 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1478 err = got_error_from_errno2("unlink", ondisk_path);
1479 goto done;
1481 if (rename(tmppath, ondisk_path) != 0) {
1482 err = got_error_from_errno3("rename", tmppath,
1483 ondisk_path);
1484 goto done;
1486 free(tmppath);
1487 tmppath = NULL;
1490 done:
1491 if (fd != -1 && close(fd) == -1 && err == NULL)
1492 err = got_error_from_errno("close");
1493 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1494 err = got_error_from_errno2("unlink", tmppath);
1495 free(tmppath);
1496 return err;
1499 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1500 static const struct got_error *
1501 get_modified_file_content_status(unsigned char *status, FILE *f)
1503 const struct got_error *err = NULL;
1504 const char *markers[3] = {
1505 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1506 GOT_DIFF_CONFLICT_MARKER_SEP,
1507 GOT_DIFF_CONFLICT_MARKER_END
1509 int i = 0;
1510 char *line = NULL;
1511 size_t linesize = 0;
1512 ssize_t linelen;
1514 while (*status == GOT_STATUS_MODIFY) {
1515 linelen = getline(&line, &linesize, f);
1516 if (linelen == -1) {
1517 if (feof(f))
1518 break;
1519 err = got_ferror(f, GOT_ERR_IO);
1520 break;
1523 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1524 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1525 == 0)
1526 *status = GOT_STATUS_CONFLICT;
1527 else
1528 i++;
1531 free(line);
1533 return err;
1536 static int
1537 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1539 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1540 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 static int
1544 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1546 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1547 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1548 ie->mtime_sec == sb->st_mtim.tv_sec &&
1549 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1550 ie->size == (sb->st_size & 0xffffffff) &&
1551 !xbit_differs(ie, sb->st_mode));
1554 static unsigned char
1555 get_staged_status(struct got_fileindex_entry *ie)
1557 switch (got_fileindex_entry_stage_get(ie)) {
1558 case GOT_FILEIDX_STAGE_ADD:
1559 return GOT_STATUS_ADD;
1560 case GOT_FILEIDX_STAGE_DELETE:
1561 return GOT_STATUS_DELETE;
1562 case GOT_FILEIDX_STAGE_MODIFY:
1563 return GOT_STATUS_MODIFY;
1564 default:
1565 return GOT_STATUS_NO_CHANGE;
1569 static const struct got_error *
1570 get_symlink_modification_status(unsigned char *status,
1571 struct got_fileindex_entry *ie, const char *abspath,
1572 int dirfd, const char *de_name, struct got_blob_object *blob)
1574 const struct got_error *err = NULL;
1575 char target_path[PATH_MAX];
1576 char etarget[PATH_MAX];
1577 ssize_t elen;
1578 size_t len, target_len = 0;
1579 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1582 *status = GOT_STATUS_NO_CHANGE;
1584 /* Blob object content specifies the target path of the link. */
1585 do {
1586 err = got_object_blob_read_block(&len, blob);
1587 if (err)
1588 return err;
1589 if (len + target_len >= sizeof(target_path)) {
1591 * Should not happen. The blob contents were OK
1592 * when this symlink was installed.
1594 return got_error(GOT_ERR_NO_SPACE);
1596 if (len > 0) {
1597 /* Skip blob object header first time around. */
1598 memcpy(target_path + target_len, buf + hdrlen,
1599 len - hdrlen);
1600 target_len += len - hdrlen;
1601 hdrlen = 0;
1603 } while (len != 0);
1604 target_path[target_len] = '\0';
1606 if (dirfd != -1) {
1607 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1608 if (elen == -1)
1609 return got_error_from_errno2("readlinkat", abspath);
1610 } else {
1611 elen = readlink(abspath, etarget, sizeof(etarget));
1612 if (elen == -1)
1613 return got_error_from_errno2("readlink", abspath);
1616 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1617 *status = GOT_STATUS_MODIFY;
1619 return NULL;
1622 static const struct got_error *
1623 get_file_status(unsigned char *status, struct stat *sb,
1624 struct got_fileindex_entry *ie, const char *abspath,
1625 int dirfd, const char *de_name, struct got_repository *repo)
1627 const struct got_error *err = NULL;
1628 struct got_object_id id;
1629 size_t hdrlen;
1630 int fd = -1, 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;
5683 *new_commit_id = NULL;
5685 STAILQ_INIT(&parent_ids);
5687 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5688 if (err)
5689 goto done;
5691 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5692 if (err)
5693 goto done;
5695 if (commit_msg_cb != NULL) {
5696 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5697 if (err)
5698 goto done;
5701 if (logmsg == NULL || strlen(logmsg) == 0) {
5702 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5703 goto done;
5706 /* Create blobs from added and modified files and record their IDs. */
5707 TAILQ_FOREACH(pe, commitable_paths, entry) {
5708 struct got_commitable *ct = pe->data;
5709 char *ondisk_path;
5711 /* Blobs for staged files already exist. */
5712 if (ct->staged_status == GOT_STATUS_ADD ||
5713 ct->staged_status == GOT_STATUS_MODIFY)
5714 continue;
5716 if (ct->status != GOT_STATUS_ADD &&
5717 ct->status != GOT_STATUS_MODIFY &&
5718 ct->status != GOT_STATUS_MODE_CHANGE)
5719 continue;
5721 if (asprintf(&ondisk_path, "%s/%s",
5722 worktree->root_path, pe->path) == -1) {
5723 err = got_error_from_errno("asprintf");
5724 goto done;
5726 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5727 free(ondisk_path);
5728 if (err)
5729 goto done;
5732 /* Recursively write new tree objects. */
5733 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5734 commitable_paths, status_cb, status_arg, repo);
5735 if (err)
5736 goto done;
5738 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5739 if (err)
5740 goto done;
5741 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5742 nparents++;
5743 if (parent_id2) {
5744 err = got_object_qid_alloc(&pid, parent_id2);
5745 if (err)
5746 goto done;
5747 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5748 nparents++;
5750 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5751 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5752 if (logmsg != NULL)
5753 free(logmsg);
5754 if (err)
5755 goto done;
5757 /* Check if a concurrent commit to our branch has occurred. */
5758 head_ref_name = got_worktree_get_head_ref_name(worktree);
5759 if (head_ref_name == NULL) {
5760 err = got_error_from_errno("got_worktree_get_head_ref_name");
5761 goto done;
5763 /* Lock the reference here to prevent concurrent modification. */
5764 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5765 if (err)
5766 goto done;
5767 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5768 if (err)
5769 goto done;
5770 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5771 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5772 goto done;
5774 /* Update branch head in repository. */
5775 err = got_ref_change_ref(head_ref2, *new_commit_id);
5776 if (err)
5777 goto done;
5778 err = got_ref_write(head_ref2, repo);
5779 if (err)
5780 goto done;
5782 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5783 if (err)
5784 goto done;
5786 err = ref_base_commit(worktree, repo);
5787 if (err)
5788 goto done;
5789 done:
5790 got_object_id_queue_free(&parent_ids);
5791 if (head_tree)
5792 got_object_tree_close(head_tree);
5793 if (head_commit)
5794 got_object_commit_close(head_commit);
5795 free(head_commit_id2);
5796 if (head_ref2) {
5797 unlockerr = got_ref_unlock(head_ref2);
5798 if (unlockerr && err == NULL)
5799 err = unlockerr;
5800 got_ref_close(head_ref2);
5802 return err;
5805 static const struct got_error *
5806 check_path_is_commitable(const char *path,
5807 struct got_pathlist_head *commitable_paths)
5809 struct got_pathlist_entry *cpe = NULL;
5810 size_t path_len = strlen(path);
5812 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5813 struct got_commitable *ct = cpe->data;
5814 const char *ct_path = ct->path;
5816 while (ct_path[0] == '/')
5817 ct_path++;
5819 if (strcmp(path, ct_path) == 0 ||
5820 got_path_is_child(ct_path, path, path_len))
5821 break;
5824 if (cpe == NULL)
5825 return got_error_path(path, GOT_ERR_BAD_PATH);
5827 return NULL;
5830 static const struct got_error *
5831 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5833 int *have_staged_files = arg;
5835 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5836 *have_staged_files = 1;
5837 return got_error(GOT_ERR_CANCELLED);
5840 return NULL;
5843 static const struct got_error *
5844 check_non_staged_files(struct got_fileindex *fileindex,
5845 struct got_pathlist_head *paths)
5847 struct got_pathlist_entry *pe;
5848 struct got_fileindex_entry *ie;
5850 TAILQ_FOREACH(pe, paths, entry) {
5851 if (pe->path[0] == '\0')
5852 continue;
5853 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5854 if (ie == NULL)
5855 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5856 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5857 return got_error_path(pe->path,
5858 GOT_ERR_FILE_NOT_STAGED);
5861 return NULL;
5864 const struct got_error *
5865 got_worktree_commit(struct got_object_id **new_commit_id,
5866 struct got_worktree *worktree, struct got_pathlist_head *paths,
5867 const char *author, const char *committer, int allow_bad_symlinks,
5868 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5869 got_worktree_status_cb status_cb, void *status_arg,
5870 struct got_repository *repo)
5872 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5873 struct got_fileindex *fileindex = NULL;
5874 char *fileindex_path = NULL;
5875 struct got_pathlist_head commitable_paths;
5876 struct collect_commitables_arg cc_arg;
5877 struct got_pathlist_entry *pe;
5878 struct got_reference *head_ref = NULL;
5879 struct got_object_id *head_commit_id = NULL;
5880 int have_staged_files = 0;
5882 *new_commit_id = NULL;
5884 TAILQ_INIT(&commitable_paths);
5886 err = lock_worktree(worktree, LOCK_EX);
5887 if (err)
5888 goto done;
5890 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5891 if (err)
5892 goto done;
5894 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5895 if (err)
5896 goto done;
5898 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5899 if (err)
5900 goto done;
5902 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5903 &have_staged_files);
5904 if (err && err->code != GOT_ERR_CANCELLED)
5905 goto done;
5906 if (have_staged_files) {
5907 err = check_non_staged_files(fileindex, paths);
5908 if (err)
5909 goto done;
5912 cc_arg.commitable_paths = &commitable_paths;
5913 cc_arg.worktree = worktree;
5914 cc_arg.fileindex = fileindex;
5915 cc_arg.repo = repo;
5916 cc_arg.have_staged_files = have_staged_files;
5917 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5918 TAILQ_FOREACH(pe, paths, entry) {
5919 err = worktree_status(worktree, pe->path, fileindex, repo,
5920 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5921 if (err)
5922 goto done;
5925 if (TAILQ_EMPTY(&commitable_paths)) {
5926 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5927 goto done;
5930 TAILQ_FOREACH(pe, paths, entry) {
5931 err = check_path_is_commitable(pe->path, &commitable_paths);
5932 if (err)
5933 goto done;
5936 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5937 struct got_commitable *ct = pe->data;
5938 const char *ct_path = ct->in_repo_path;
5940 while (ct_path[0] == '/')
5941 ct_path++;
5942 err = check_out_of_date(ct_path, ct->status,
5943 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5944 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5945 if (err)
5946 goto done;
5950 err = commit_worktree(new_commit_id, &commitable_paths,
5951 head_commit_id, NULL, worktree, author, committer,
5952 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5953 if (err)
5954 goto done;
5956 err = update_fileindex_after_commit(worktree, &commitable_paths,
5957 *new_commit_id, fileindex, have_staged_files);
5958 sync_err = sync_fileindex(fileindex, fileindex_path);
5959 if (sync_err && err == NULL)
5960 err = sync_err;
5961 done:
5962 if (fileindex)
5963 got_fileindex_free(fileindex);
5964 free(fileindex_path);
5965 unlockerr = lock_worktree(worktree, LOCK_SH);
5966 if (unlockerr && err == NULL)
5967 err = unlockerr;
5968 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5969 struct got_commitable *ct = pe->data;
5970 free_commitable(ct);
5972 got_pathlist_free(&commitable_paths);
5973 return err;
5976 const char *
5977 got_commitable_get_path(struct got_commitable *ct)
5979 return ct->path;
5982 unsigned int
5983 got_commitable_get_status(struct got_commitable *ct)
5985 return ct->status;
5988 struct check_rebase_ok_arg {
5989 struct got_worktree *worktree;
5990 struct got_repository *repo;
5993 static const struct got_error *
5994 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5996 const struct got_error *err = NULL;
5997 struct check_rebase_ok_arg *a = arg;
5998 unsigned char status;
5999 struct stat sb;
6000 char *ondisk_path;
6002 /* Reject rebase of a work tree with mixed base commits. */
6003 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6004 SHA1_DIGEST_LENGTH))
6005 return got_error(GOT_ERR_MIXED_COMMITS);
6007 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6008 == -1)
6009 return got_error_from_errno("asprintf");
6011 /* Reject rebase of a work tree with modified or staged files. */
6012 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6013 free(ondisk_path);
6014 if (err)
6015 return err;
6017 if (status != GOT_STATUS_NO_CHANGE)
6018 return got_error(GOT_ERR_MODIFIED);
6019 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6020 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6022 return NULL;
6025 const struct got_error *
6026 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6027 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6028 struct got_worktree *worktree, struct got_reference *branch,
6029 struct got_repository *repo)
6031 const struct got_error *err = NULL;
6032 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6033 char *branch_ref_name = NULL;
6034 char *fileindex_path = NULL;
6035 struct check_rebase_ok_arg ok_arg;
6036 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6037 struct got_object_id *wt_branch_tip = NULL;
6039 *new_base_branch_ref = NULL;
6040 *tmp_branch = NULL;
6041 *fileindex = NULL;
6043 err = lock_worktree(worktree, LOCK_EX);
6044 if (err)
6045 return err;
6047 err = open_fileindex(fileindex, &fileindex_path, worktree);
6048 if (err)
6049 goto done;
6051 ok_arg.worktree = worktree;
6052 ok_arg.repo = repo;
6053 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6054 &ok_arg);
6055 if (err)
6056 goto done;
6058 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6059 if (err)
6060 goto done;
6062 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6063 if (err)
6064 goto done;
6066 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6067 if (err)
6068 goto done;
6070 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6072 if (err)
6073 goto done;
6075 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6076 if (err)
6077 goto done;
6078 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6079 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6080 goto done;
6083 err = got_ref_alloc_symref(new_base_branch_ref,
6084 new_base_branch_ref_name, wt_branch);
6085 if (err)
6086 goto done;
6087 err = got_ref_write(*new_base_branch_ref, repo);
6088 if (err)
6089 goto done;
6091 /* TODO Lock original branch's ref while rebasing? */
6093 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6094 if (err)
6095 goto done;
6097 err = got_ref_write(branch_ref, repo);
6098 if (err)
6099 goto done;
6101 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6102 worktree->base_commit_id);
6103 if (err)
6104 goto done;
6105 err = got_ref_write(*tmp_branch, repo);
6106 if (err)
6107 goto done;
6109 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6110 if (err)
6111 goto done;
6112 done:
6113 free(fileindex_path);
6114 free(tmp_branch_name);
6115 free(new_base_branch_ref_name);
6116 free(branch_ref_name);
6117 if (branch_ref)
6118 got_ref_close(branch_ref);
6119 if (wt_branch)
6120 got_ref_close(wt_branch);
6121 free(wt_branch_tip);
6122 if (err) {
6123 if (*new_base_branch_ref) {
6124 got_ref_close(*new_base_branch_ref);
6125 *new_base_branch_ref = NULL;
6127 if (*tmp_branch) {
6128 got_ref_close(*tmp_branch);
6129 *tmp_branch = NULL;
6131 if (*fileindex) {
6132 got_fileindex_free(*fileindex);
6133 *fileindex = NULL;
6135 lock_worktree(worktree, LOCK_SH);
6137 return err;
6140 const struct got_error *
6141 got_worktree_rebase_continue(struct got_object_id **commit_id,
6142 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6143 struct got_reference **branch, struct got_fileindex **fileindex,
6144 struct got_worktree *worktree, struct got_repository *repo)
6146 const struct got_error *err;
6147 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6148 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6149 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6150 char *fileindex_path = NULL;
6151 int have_staged_files = 0;
6153 *commit_id = NULL;
6154 *new_base_branch = NULL;
6155 *tmp_branch = NULL;
6156 *branch = NULL;
6157 *fileindex = NULL;
6159 err = lock_worktree(worktree, LOCK_EX);
6160 if (err)
6161 return err;
6163 err = open_fileindex(fileindex, &fileindex_path, worktree);
6164 if (err)
6165 goto done;
6167 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6168 &have_staged_files);
6169 if (err && err->code != GOT_ERR_CANCELLED)
6170 goto done;
6171 if (have_staged_files) {
6172 err = got_error(GOT_ERR_STAGED_PATHS);
6173 goto done;
6176 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6177 if (err)
6178 goto done;
6180 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6181 if (err)
6182 goto done;
6184 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6185 if (err)
6186 goto done;
6188 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6189 if (err)
6190 goto done;
6192 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6193 if (err)
6194 goto done;
6196 err = got_ref_open(branch, repo,
6197 got_ref_get_symref_target(branch_ref), 0);
6198 if (err)
6199 goto done;
6201 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6202 if (err)
6203 goto done;
6205 err = got_ref_resolve(commit_id, repo, commit_ref);
6206 if (err)
6207 goto done;
6209 err = got_ref_open(new_base_branch, repo,
6210 new_base_branch_ref_name, 0);
6211 if (err)
6212 goto done;
6214 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6215 if (err)
6216 goto done;
6217 done:
6218 free(commit_ref_name);
6219 free(branch_ref_name);
6220 free(fileindex_path);
6221 if (commit_ref)
6222 got_ref_close(commit_ref);
6223 if (branch_ref)
6224 got_ref_close(branch_ref);
6225 if (err) {
6226 free(*commit_id);
6227 *commit_id = NULL;
6228 if (*tmp_branch) {
6229 got_ref_close(*tmp_branch);
6230 *tmp_branch = NULL;
6232 if (*new_base_branch) {
6233 got_ref_close(*new_base_branch);
6234 *new_base_branch = NULL;
6236 if (*branch) {
6237 got_ref_close(*branch);
6238 *branch = NULL;
6240 if (*fileindex) {
6241 got_fileindex_free(*fileindex);
6242 *fileindex = NULL;
6244 lock_worktree(worktree, LOCK_SH);
6246 return err;
6249 const struct got_error *
6250 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6252 const struct got_error *err;
6253 char *tmp_branch_name = NULL;
6255 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6256 if (err)
6257 return err;
6259 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6260 free(tmp_branch_name);
6261 return NULL;
6264 static const struct got_error *
6265 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6266 char **logmsg, void *arg)
6268 *logmsg = arg;
6269 return NULL;
6272 static const struct got_error *
6273 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6274 const char *path, struct got_object_id *blob_id,
6275 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6276 int dirfd, const char *de_name)
6278 return NULL;
6281 struct collect_merged_paths_arg {
6282 got_worktree_checkout_cb progress_cb;
6283 void *progress_arg;
6284 struct got_pathlist_head *merged_paths;
6287 static const struct got_error *
6288 collect_merged_paths(void *arg, unsigned char status, const char *path)
6290 const struct got_error *err;
6291 struct collect_merged_paths_arg *a = arg;
6292 char *p;
6293 struct got_pathlist_entry *new;
6295 err = (*a->progress_cb)(a->progress_arg, status, path);
6296 if (err)
6297 return err;
6299 if (status != GOT_STATUS_MERGE &&
6300 status != GOT_STATUS_ADD &&
6301 status != GOT_STATUS_DELETE &&
6302 status != GOT_STATUS_CONFLICT)
6303 return NULL;
6305 p = strdup(path);
6306 if (p == NULL)
6307 return got_error_from_errno("strdup");
6309 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6310 if (err || new == NULL)
6311 free(p);
6312 return err;
6315 void
6316 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6318 struct got_pathlist_entry *pe;
6320 TAILQ_FOREACH(pe, merged_paths, entry)
6321 free((char *)pe->path);
6323 got_pathlist_free(merged_paths);
6326 static const struct got_error *
6327 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6328 int is_rebase, struct got_repository *repo)
6330 const struct got_error *err;
6331 struct got_reference *commit_ref = NULL;
6333 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6334 if (err) {
6335 if (err->code != GOT_ERR_NOT_REF)
6336 goto done;
6337 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6338 if (err)
6339 goto done;
6340 err = got_ref_write(commit_ref, repo);
6341 if (err)
6342 goto done;
6343 } else if (is_rebase) {
6344 struct got_object_id *stored_id;
6345 int cmp;
6347 err = got_ref_resolve(&stored_id, repo, commit_ref);
6348 if (err)
6349 goto done;
6350 cmp = got_object_id_cmp(commit_id, stored_id);
6351 free(stored_id);
6352 if (cmp != 0) {
6353 err = got_error(GOT_ERR_REBASE_COMMITID);
6354 goto done;
6357 done:
6358 if (commit_ref)
6359 got_ref_close(commit_ref);
6360 return err;
6363 static const struct got_error *
6364 rebase_merge_files(struct got_pathlist_head *merged_paths,
6365 const char *commit_ref_name, struct got_worktree *worktree,
6366 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6367 struct got_object_id *commit_id, struct got_repository *repo,
6368 got_worktree_checkout_cb progress_cb, void *progress_arg,
6369 got_cancel_cb cancel_cb, void *cancel_arg)
6371 const struct got_error *err;
6372 struct got_reference *commit_ref = NULL;
6373 struct collect_merged_paths_arg cmp_arg;
6374 char *fileindex_path;
6376 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6378 err = get_fileindex_path(&fileindex_path, worktree);
6379 if (err)
6380 return err;
6382 cmp_arg.progress_cb = progress_cb;
6383 cmp_arg.progress_arg = progress_arg;
6384 cmp_arg.merged_paths = merged_paths;
6385 err = merge_files(worktree, fileindex, fileindex_path,
6386 parent_commit_id, commit_id, repo, collect_merged_paths,
6387 &cmp_arg, cancel_cb, cancel_arg);
6388 if (commit_ref)
6389 got_ref_close(commit_ref);
6390 return err;
6393 const struct got_error *
6394 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6395 struct got_worktree *worktree, struct got_fileindex *fileindex,
6396 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6397 struct got_repository *repo,
6398 got_worktree_checkout_cb progress_cb, void *progress_arg,
6399 got_cancel_cb cancel_cb, void *cancel_arg)
6401 const struct got_error *err;
6402 char *commit_ref_name;
6404 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6405 if (err)
6406 return err;
6408 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6409 if (err)
6410 goto done;
6412 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6413 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6414 progress_arg, cancel_cb, cancel_arg);
6415 done:
6416 free(commit_ref_name);
6417 return err;
6420 const struct got_error *
6421 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6422 struct got_worktree *worktree, struct got_fileindex *fileindex,
6423 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6424 struct got_repository *repo,
6425 got_worktree_checkout_cb progress_cb, void *progress_arg,
6426 got_cancel_cb cancel_cb, void *cancel_arg)
6428 const struct got_error *err;
6429 char *commit_ref_name;
6431 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6432 if (err)
6433 return err;
6435 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6436 if (err)
6437 goto done;
6439 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6440 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6441 progress_arg, cancel_cb, cancel_arg);
6442 done:
6443 free(commit_ref_name);
6444 return err;
6447 static const struct got_error *
6448 rebase_commit(struct got_object_id **new_commit_id,
6449 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6450 struct got_worktree *worktree, struct got_fileindex *fileindex,
6451 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6452 const char *new_logmsg, struct got_repository *repo)
6454 const struct got_error *err, *sync_err;
6455 struct got_pathlist_head commitable_paths;
6456 struct collect_commitables_arg cc_arg;
6457 char *fileindex_path = NULL;
6458 struct got_reference *head_ref = NULL;
6459 struct got_object_id *head_commit_id = NULL;
6460 char *logmsg = NULL;
6462 TAILQ_INIT(&commitable_paths);
6463 *new_commit_id = NULL;
6465 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6467 err = get_fileindex_path(&fileindex_path, worktree);
6468 if (err)
6469 return err;
6471 cc_arg.commitable_paths = &commitable_paths;
6472 cc_arg.worktree = worktree;
6473 cc_arg.repo = repo;
6474 cc_arg.have_staged_files = 0;
6476 * If possible get the status of individual files directly to
6477 * avoid crawling the entire work tree once per rebased commit.
6479 * Ideally, merged_paths would contain a list of commitables
6480 * we could use so we could skip worktree_status() entirely.
6481 * However, we would then need carefully keep track of cumulative
6482 * effects of operations such as file additions and deletions
6483 * in 'got histedit -f' (folding multiple commits into one),
6484 * and this extra complexity is not really worth it.
6486 if (merged_paths) {
6487 struct got_pathlist_entry *pe;
6488 TAILQ_FOREACH(pe, merged_paths, entry) {
6489 err = worktree_status(worktree, pe->path, fileindex,
6490 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6492 if (err)
6493 goto done;
6495 } else {
6496 err = worktree_status(worktree, "", fileindex, repo,
6497 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6498 if (err)
6499 goto done;
6502 if (TAILQ_EMPTY(&commitable_paths)) {
6503 /* No-op change; commit will be elided. */
6504 err = got_ref_delete(commit_ref, repo);
6505 if (err)
6506 goto done;
6507 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6508 goto done;
6511 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6512 if (err)
6513 goto done;
6515 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6516 if (err)
6517 goto done;
6519 if (new_logmsg) {
6520 logmsg = strdup(new_logmsg);
6521 if (logmsg == NULL) {
6522 err = got_error_from_errno("strdup");
6523 goto done;
6525 } else {
6526 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6527 if (err)
6528 goto done;
6531 /* NB: commit_worktree will call free(logmsg) */
6532 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6533 NULL, worktree, got_object_commit_get_author(orig_commit),
6534 got_object_commit_get_committer(orig_commit),
6535 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6536 if (err)
6537 goto done;
6539 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6540 if (err)
6541 goto done;
6543 err = got_ref_delete(commit_ref, repo);
6544 if (err)
6545 goto done;
6547 err = update_fileindex_after_commit(worktree, &commitable_paths,
6548 *new_commit_id, fileindex, 0);
6549 sync_err = sync_fileindex(fileindex, fileindex_path);
6550 if (sync_err && err == NULL)
6551 err = sync_err;
6552 done:
6553 free(fileindex_path);
6554 free(head_commit_id);
6555 if (head_ref)
6556 got_ref_close(head_ref);
6557 if (err) {
6558 free(*new_commit_id);
6559 *new_commit_id = NULL;
6561 return err;
6564 const struct got_error *
6565 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6566 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6567 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6568 struct got_commit_object *orig_commit,
6569 struct got_object_id *orig_commit_id, struct got_repository *repo)
6571 const struct got_error *err;
6572 char *commit_ref_name;
6573 struct got_reference *commit_ref = NULL;
6574 struct got_object_id *commit_id = NULL;
6576 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6577 if (err)
6578 return err;
6580 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6581 if (err)
6582 goto done;
6583 err = got_ref_resolve(&commit_id, repo, commit_ref);
6584 if (err)
6585 goto done;
6586 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6587 err = got_error(GOT_ERR_REBASE_COMMITID);
6588 goto done;
6591 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6592 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6593 done:
6594 if (commit_ref)
6595 got_ref_close(commit_ref);
6596 free(commit_ref_name);
6597 free(commit_id);
6598 return err;
6601 const struct got_error *
6602 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6603 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6604 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6605 struct got_commit_object *orig_commit,
6606 struct got_object_id *orig_commit_id, const char *new_logmsg,
6607 struct got_repository *repo)
6609 const struct got_error *err;
6610 char *commit_ref_name;
6611 struct got_reference *commit_ref = NULL;
6613 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6614 if (err)
6615 return err;
6617 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6618 if (err)
6619 goto done;
6621 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6622 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6623 done:
6624 if (commit_ref)
6625 got_ref_close(commit_ref);
6626 free(commit_ref_name);
6627 return err;
6630 const struct got_error *
6631 got_worktree_rebase_postpone(struct got_worktree *worktree,
6632 struct got_fileindex *fileindex)
6634 if (fileindex)
6635 got_fileindex_free(fileindex);
6636 return lock_worktree(worktree, LOCK_SH);
6639 static const struct got_error *
6640 delete_ref(const char *name, struct got_repository *repo)
6642 const struct got_error *err;
6643 struct got_reference *ref;
6645 err = got_ref_open(&ref, repo, name, 0);
6646 if (err) {
6647 if (err->code == GOT_ERR_NOT_REF)
6648 return NULL;
6649 return err;
6652 err = got_ref_delete(ref, repo);
6653 got_ref_close(ref);
6654 return err;
6657 static const struct got_error *
6658 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6660 const struct got_error *err;
6661 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6662 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6664 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6665 if (err)
6666 goto done;
6667 err = delete_ref(tmp_branch_name, repo);
6668 if (err)
6669 goto done;
6671 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6672 if (err)
6673 goto done;
6674 err = delete_ref(new_base_branch_ref_name, repo);
6675 if (err)
6676 goto done;
6678 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6679 if (err)
6680 goto done;
6681 err = delete_ref(branch_ref_name, repo);
6682 if (err)
6683 goto done;
6685 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6686 if (err)
6687 goto done;
6688 err = delete_ref(commit_ref_name, repo);
6689 if (err)
6690 goto done;
6692 done:
6693 free(tmp_branch_name);
6694 free(new_base_branch_ref_name);
6695 free(branch_ref_name);
6696 free(commit_ref_name);
6697 return err;
6700 static const struct got_error *
6701 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6702 struct got_object_id *new_commit_id, struct got_repository *repo)
6704 const struct got_error *err;
6705 struct got_reference *ref = NULL;
6706 struct got_object_id *old_commit_id = NULL;
6707 const char *branch_name = NULL;
6708 char *new_id_str = NULL;
6709 char *refname = NULL;
6711 branch_name = got_ref_get_name(branch);
6712 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6713 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6714 branch_name += 11;
6716 err = got_object_id_str(&new_id_str, new_commit_id);
6717 if (err)
6718 return err;
6720 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6721 new_id_str) == -1) {
6722 err = got_error_from_errno("asprintf");
6723 goto done;
6726 err = got_ref_resolve(&old_commit_id, repo, branch);
6727 if (err)
6728 goto done;
6730 err = got_ref_alloc(&ref, refname, old_commit_id);
6731 if (err)
6732 goto done;
6734 err = got_ref_write(ref, repo);
6735 done:
6736 free(new_id_str);
6737 free(refname);
6738 free(old_commit_id);
6739 if (ref)
6740 got_ref_close(ref);
6741 return err;
6744 const struct got_error *
6745 got_worktree_rebase_complete(struct got_worktree *worktree,
6746 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6747 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6748 struct got_repository *repo, int create_backup)
6750 const struct got_error *err, *unlockerr, *sync_err;
6751 struct got_object_id *new_head_commit_id = NULL;
6752 char *fileindex_path = NULL;
6754 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6755 if (err)
6756 return err;
6758 if (create_backup) {
6759 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6760 rebased_branch, new_head_commit_id, repo);
6761 if (err)
6762 goto done;
6765 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6766 if (err)
6767 goto done;
6769 err = got_ref_write(rebased_branch, repo);
6770 if (err)
6771 goto done;
6773 err = got_worktree_set_head_ref(worktree, rebased_branch);
6774 if (err)
6775 goto done;
6777 err = delete_rebase_refs(worktree, repo);
6778 if (err)
6779 goto done;
6781 err = get_fileindex_path(&fileindex_path, worktree);
6782 if (err)
6783 goto done;
6784 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6785 sync_err = sync_fileindex(fileindex, fileindex_path);
6786 if (sync_err && err == NULL)
6787 err = sync_err;
6788 done:
6789 got_fileindex_free(fileindex);
6790 free(fileindex_path);
6791 free(new_head_commit_id);
6792 unlockerr = lock_worktree(worktree, LOCK_SH);
6793 if (unlockerr && err == NULL)
6794 err = unlockerr;
6795 return err;
6798 const struct got_error *
6799 got_worktree_rebase_abort(struct got_worktree *worktree,
6800 struct got_fileindex *fileindex, struct got_repository *repo,
6801 struct got_reference *new_base_branch,
6802 got_worktree_checkout_cb progress_cb, void *progress_arg)
6804 const struct got_error *err, *unlockerr, *sync_err;
6805 struct got_reference *resolved = NULL;
6806 struct got_object_id *commit_id = NULL;
6807 struct got_commit_object *commit = NULL;
6808 char *fileindex_path = NULL;
6809 struct revert_file_args rfa;
6810 struct got_object_id *tree_id = NULL;
6812 err = lock_worktree(worktree, LOCK_EX);
6813 if (err)
6814 return err;
6816 err = got_object_open_as_commit(&commit, repo,
6817 worktree->base_commit_id);
6818 if (err)
6819 goto done;
6821 err = got_ref_open(&resolved, repo,
6822 got_ref_get_symref_target(new_base_branch), 0);
6823 if (err)
6824 goto done;
6826 err = got_worktree_set_head_ref(worktree, resolved);
6827 if (err)
6828 goto done;
6831 * XXX commits to the base branch could have happened while
6832 * we were busy rebasing; should we store the original commit ID
6833 * when rebase begins and read it back here?
6835 err = got_ref_resolve(&commit_id, repo, resolved);
6836 if (err)
6837 goto done;
6839 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6840 if (err)
6841 goto done;
6843 err = got_object_id_by_path(&tree_id, repo, commit,
6844 worktree->path_prefix);
6845 if (err)
6846 goto done;
6848 err = delete_rebase_refs(worktree, repo);
6849 if (err)
6850 goto done;
6852 err = get_fileindex_path(&fileindex_path, worktree);
6853 if (err)
6854 goto done;
6856 rfa.worktree = worktree;
6857 rfa.fileindex = fileindex;
6858 rfa.progress_cb = progress_cb;
6859 rfa.progress_arg = progress_arg;
6860 rfa.patch_cb = NULL;
6861 rfa.patch_arg = NULL;
6862 rfa.repo = repo;
6863 rfa.unlink_added_files = 0;
6864 err = worktree_status(worktree, "", fileindex, repo,
6865 revert_file, &rfa, NULL, NULL, 1, 0);
6866 if (err)
6867 goto sync;
6869 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6870 repo, progress_cb, progress_arg, NULL, NULL);
6871 sync:
6872 sync_err = sync_fileindex(fileindex, fileindex_path);
6873 if (sync_err && err == NULL)
6874 err = sync_err;
6875 done:
6876 got_ref_close(resolved);
6877 free(tree_id);
6878 free(commit_id);
6879 if (commit)
6880 got_object_commit_close(commit);
6881 if (fileindex)
6882 got_fileindex_free(fileindex);
6883 free(fileindex_path);
6885 unlockerr = lock_worktree(worktree, LOCK_SH);
6886 if (unlockerr && err == NULL)
6887 err = unlockerr;
6888 return err;
6891 const struct got_error *
6892 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6893 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6894 struct got_fileindex **fileindex, struct got_worktree *worktree,
6895 struct got_repository *repo)
6897 const struct got_error *err = NULL;
6898 char *tmp_branch_name = NULL;
6899 char *branch_ref_name = NULL;
6900 char *base_commit_ref_name = NULL;
6901 char *fileindex_path = NULL;
6902 struct check_rebase_ok_arg ok_arg;
6903 struct got_reference *wt_branch = NULL;
6904 struct got_reference *base_commit_ref = NULL;
6906 *tmp_branch = NULL;
6907 *branch_ref = NULL;
6908 *base_commit_id = NULL;
6909 *fileindex = NULL;
6911 err = lock_worktree(worktree, LOCK_EX);
6912 if (err)
6913 return err;
6915 err = open_fileindex(fileindex, &fileindex_path, worktree);
6916 if (err)
6917 goto done;
6919 ok_arg.worktree = worktree;
6920 ok_arg.repo = repo;
6921 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6922 &ok_arg);
6923 if (err)
6924 goto done;
6926 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6927 if (err)
6928 goto done;
6930 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6931 if (err)
6932 goto done;
6934 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6935 worktree);
6936 if (err)
6937 goto done;
6939 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6941 if (err)
6942 goto done;
6944 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6945 if (err)
6946 goto done;
6948 err = got_ref_write(*branch_ref, repo);
6949 if (err)
6950 goto done;
6952 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6953 worktree->base_commit_id);
6954 if (err)
6955 goto done;
6956 err = got_ref_write(base_commit_ref, repo);
6957 if (err)
6958 goto done;
6959 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6960 if (*base_commit_id == NULL) {
6961 err = got_error_from_errno("got_object_id_dup");
6962 goto done;
6965 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6966 worktree->base_commit_id);
6967 if (err)
6968 goto done;
6969 err = got_ref_write(*tmp_branch, repo);
6970 if (err)
6971 goto done;
6973 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6974 if (err)
6975 goto done;
6976 done:
6977 free(fileindex_path);
6978 free(tmp_branch_name);
6979 free(branch_ref_name);
6980 free(base_commit_ref_name);
6981 if (wt_branch)
6982 got_ref_close(wt_branch);
6983 if (err) {
6984 if (*branch_ref) {
6985 got_ref_close(*branch_ref);
6986 *branch_ref = NULL;
6988 if (*tmp_branch) {
6989 got_ref_close(*tmp_branch);
6990 *tmp_branch = NULL;
6992 free(*base_commit_id);
6993 if (*fileindex) {
6994 got_fileindex_free(*fileindex);
6995 *fileindex = NULL;
6997 lock_worktree(worktree, LOCK_SH);
6999 return err;
7002 const struct got_error *
7003 got_worktree_histedit_postpone(struct got_worktree *worktree,
7004 struct got_fileindex *fileindex)
7006 if (fileindex)
7007 got_fileindex_free(fileindex);
7008 return lock_worktree(worktree, LOCK_SH);
7011 const struct got_error *
7012 got_worktree_histedit_in_progress(int *in_progress,
7013 struct got_worktree *worktree)
7015 const struct got_error *err;
7016 char *tmp_branch_name = NULL;
7018 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7019 if (err)
7020 return err;
7022 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7023 free(tmp_branch_name);
7024 return NULL;
7027 const struct got_error *
7028 got_worktree_histedit_continue(struct got_object_id **commit_id,
7029 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7030 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7031 struct got_worktree *worktree, struct got_repository *repo)
7033 const struct got_error *err;
7034 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7035 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7036 struct got_reference *commit_ref = NULL;
7037 struct got_reference *base_commit_ref = NULL;
7038 char *fileindex_path = NULL;
7039 int have_staged_files = 0;
7041 *commit_id = NULL;
7042 *tmp_branch = NULL;
7043 *base_commit_id = NULL;
7044 *fileindex = NULL;
7046 err = lock_worktree(worktree, LOCK_EX);
7047 if (err)
7048 return err;
7050 err = open_fileindex(fileindex, &fileindex_path, worktree);
7051 if (err)
7052 goto done;
7054 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7055 &have_staged_files);
7056 if (err && err->code != GOT_ERR_CANCELLED)
7057 goto done;
7058 if (have_staged_files) {
7059 err = got_error(GOT_ERR_STAGED_PATHS);
7060 goto done;
7063 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7064 if (err)
7065 goto done;
7067 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7068 if (err)
7069 goto done;
7071 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7072 if (err)
7073 goto done;
7075 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7076 worktree);
7077 if (err)
7078 goto done;
7080 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7081 if (err)
7082 goto done;
7084 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7085 if (err)
7086 goto done;
7087 err = got_ref_resolve(commit_id, repo, commit_ref);
7088 if (err)
7089 goto done;
7091 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7092 if (err)
7093 goto done;
7094 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7095 if (err)
7096 goto done;
7098 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7099 if (err)
7100 goto done;
7101 done:
7102 free(commit_ref_name);
7103 free(branch_ref_name);
7104 free(fileindex_path);
7105 if (commit_ref)
7106 got_ref_close(commit_ref);
7107 if (base_commit_ref)
7108 got_ref_close(base_commit_ref);
7109 if (err) {
7110 free(*commit_id);
7111 *commit_id = NULL;
7112 free(*base_commit_id);
7113 *base_commit_id = NULL;
7114 if (*tmp_branch) {
7115 got_ref_close(*tmp_branch);
7116 *tmp_branch = NULL;
7118 if (*fileindex) {
7119 got_fileindex_free(*fileindex);
7120 *fileindex = NULL;
7122 lock_worktree(worktree, LOCK_EX);
7124 return err;
7127 static const struct got_error *
7128 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7130 const struct got_error *err;
7131 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7132 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7134 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7135 if (err)
7136 goto done;
7137 err = delete_ref(tmp_branch_name, repo);
7138 if (err)
7139 goto done;
7141 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7142 worktree);
7143 if (err)
7144 goto done;
7145 err = delete_ref(base_commit_ref_name, repo);
7146 if (err)
7147 goto done;
7149 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7150 if (err)
7151 goto done;
7152 err = delete_ref(branch_ref_name, repo);
7153 if (err)
7154 goto done;
7156 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7157 if (err)
7158 goto done;
7159 err = delete_ref(commit_ref_name, repo);
7160 if (err)
7161 goto done;
7162 done:
7163 free(tmp_branch_name);
7164 free(base_commit_ref_name);
7165 free(branch_ref_name);
7166 free(commit_ref_name);
7167 return err;
7170 const struct got_error *
7171 got_worktree_histedit_abort(struct got_worktree *worktree,
7172 struct got_fileindex *fileindex, struct got_repository *repo,
7173 struct got_reference *branch, struct got_object_id *base_commit_id,
7174 got_worktree_checkout_cb progress_cb, void *progress_arg)
7176 const struct got_error *err, *unlockerr, *sync_err;
7177 struct got_reference *resolved = NULL;
7178 char *fileindex_path = NULL;
7179 struct got_commit_object *commit = NULL;
7180 struct got_object_id *tree_id = NULL;
7181 struct revert_file_args rfa;
7183 err = lock_worktree(worktree, LOCK_EX);
7184 if (err)
7185 return err;
7187 err = got_object_open_as_commit(&commit, repo,
7188 worktree->base_commit_id);
7189 if (err)
7190 goto done;
7192 err = got_ref_open(&resolved, repo,
7193 got_ref_get_symref_target(branch), 0);
7194 if (err)
7195 goto done;
7197 err = got_worktree_set_head_ref(worktree, resolved);
7198 if (err)
7199 goto done;
7201 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7202 if (err)
7203 goto done;
7205 err = got_object_id_by_path(&tree_id, repo, commit,
7206 worktree->path_prefix);
7207 if (err)
7208 goto done;
7210 err = delete_histedit_refs(worktree, repo);
7211 if (err)
7212 goto done;
7214 err = get_fileindex_path(&fileindex_path, worktree);
7215 if (err)
7216 goto done;
7218 rfa.worktree = worktree;
7219 rfa.fileindex = fileindex;
7220 rfa.progress_cb = progress_cb;
7221 rfa.progress_arg = progress_arg;
7222 rfa.patch_cb = NULL;
7223 rfa.patch_arg = NULL;
7224 rfa.repo = repo;
7225 rfa.unlink_added_files = 0;
7226 err = worktree_status(worktree, "", fileindex, repo,
7227 revert_file, &rfa, NULL, NULL, 1, 0);
7228 if (err)
7229 goto sync;
7231 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7232 repo, progress_cb, progress_arg, NULL, NULL);
7233 sync:
7234 sync_err = sync_fileindex(fileindex, fileindex_path);
7235 if (sync_err && err == NULL)
7236 err = sync_err;
7237 done:
7238 got_ref_close(resolved);
7239 free(tree_id);
7240 free(fileindex_path);
7242 unlockerr = lock_worktree(worktree, LOCK_SH);
7243 if (unlockerr && err == NULL)
7244 err = unlockerr;
7245 return err;
7248 const struct got_error *
7249 got_worktree_histedit_complete(struct got_worktree *worktree,
7250 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7251 struct got_reference *edited_branch, struct got_repository *repo)
7253 const struct got_error *err, *unlockerr, *sync_err;
7254 struct got_object_id *new_head_commit_id = NULL;
7255 struct got_reference *resolved = NULL;
7256 char *fileindex_path = NULL;
7258 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7259 if (err)
7260 return err;
7262 err = got_ref_open(&resolved, repo,
7263 got_ref_get_symref_target(edited_branch), 0);
7264 if (err)
7265 goto done;
7267 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7268 resolved, new_head_commit_id, repo);
7269 if (err)
7270 goto done;
7272 err = got_ref_change_ref(resolved, new_head_commit_id);
7273 if (err)
7274 goto done;
7276 err = got_ref_write(resolved, repo);
7277 if (err)
7278 goto done;
7280 err = got_worktree_set_head_ref(worktree, resolved);
7281 if (err)
7282 goto done;
7284 err = delete_histedit_refs(worktree, repo);
7285 if (err)
7286 goto done;
7288 err = get_fileindex_path(&fileindex_path, worktree);
7289 if (err)
7290 goto done;
7291 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7292 sync_err = sync_fileindex(fileindex, fileindex_path);
7293 if (sync_err && err == NULL)
7294 err = sync_err;
7295 done:
7296 got_fileindex_free(fileindex);
7297 free(fileindex_path);
7298 free(new_head_commit_id);
7299 unlockerr = lock_worktree(worktree, LOCK_SH);
7300 if (unlockerr && err == NULL)
7301 err = unlockerr;
7302 return err;
7305 const struct got_error *
7306 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7307 struct got_object_id *commit_id, struct got_repository *repo)
7309 const struct got_error *err;
7310 char *commit_ref_name;
7312 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7313 if (err)
7314 return err;
7316 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7317 if (err)
7318 goto done;
7320 err = delete_ref(commit_ref_name, repo);
7321 done:
7322 free(commit_ref_name);
7323 return err;
7326 const struct got_error *
7327 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7328 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7329 struct got_worktree *worktree, const char *refname,
7330 struct got_repository *repo)
7332 const struct got_error *err = NULL;
7333 char *fileindex_path = NULL;
7334 struct check_rebase_ok_arg ok_arg;
7336 *fileindex = NULL;
7337 *branch_ref = NULL;
7338 *base_branch_ref = NULL;
7340 err = lock_worktree(worktree, LOCK_EX);
7341 if (err)
7342 return err;
7344 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7345 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7346 "cannot integrate a branch into itself; "
7347 "update -b or different branch name required");
7348 goto done;
7351 err = open_fileindex(fileindex, &fileindex_path, worktree);
7352 if (err)
7353 goto done;
7355 /* Preconditions are the same as for rebase. */
7356 ok_arg.worktree = worktree;
7357 ok_arg.repo = repo;
7358 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7359 &ok_arg);
7360 if (err)
7361 goto done;
7363 err = got_ref_open(branch_ref, repo, refname, 1);
7364 if (err)
7365 goto done;
7367 err = got_ref_open(base_branch_ref, repo,
7368 got_worktree_get_head_ref_name(worktree), 1);
7369 done:
7370 if (err) {
7371 if (*branch_ref) {
7372 got_ref_close(*branch_ref);
7373 *branch_ref = NULL;
7375 if (*base_branch_ref) {
7376 got_ref_close(*base_branch_ref);
7377 *base_branch_ref = NULL;
7379 if (*fileindex) {
7380 got_fileindex_free(*fileindex);
7381 *fileindex = NULL;
7383 lock_worktree(worktree, LOCK_SH);
7385 return err;
7388 const struct got_error *
7389 got_worktree_integrate_continue(struct got_worktree *worktree,
7390 struct got_fileindex *fileindex, struct got_repository *repo,
7391 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7392 got_worktree_checkout_cb progress_cb, void *progress_arg,
7393 got_cancel_cb cancel_cb, void *cancel_arg)
7395 const struct got_error *err = NULL, *sync_err, *unlockerr;
7396 char *fileindex_path = NULL;
7397 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7398 struct got_commit_object *commit = NULL;
7400 err = get_fileindex_path(&fileindex_path, worktree);
7401 if (err)
7402 goto done;
7404 err = got_ref_resolve(&commit_id, repo, branch_ref);
7405 if (err)
7406 goto done;
7408 err = got_object_open_as_commit(&commit, repo, commit_id);
7409 if (err)
7410 goto done;
7412 err = got_object_id_by_path(&tree_id, repo, commit,
7413 worktree->path_prefix);
7414 if (err)
7415 goto done;
7417 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7418 if (err)
7419 goto done;
7421 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7422 progress_cb, progress_arg, cancel_cb, cancel_arg);
7423 if (err)
7424 goto sync;
7426 err = got_ref_change_ref(base_branch_ref, commit_id);
7427 if (err)
7428 goto sync;
7430 err = got_ref_write(base_branch_ref, repo);
7431 if (err)
7432 goto sync;
7434 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7435 sync:
7436 sync_err = sync_fileindex(fileindex, fileindex_path);
7437 if (sync_err && err == NULL)
7438 err = sync_err;
7440 done:
7441 unlockerr = got_ref_unlock(branch_ref);
7442 if (unlockerr && err == NULL)
7443 err = unlockerr;
7444 got_ref_close(branch_ref);
7446 unlockerr = got_ref_unlock(base_branch_ref);
7447 if (unlockerr && err == NULL)
7448 err = unlockerr;
7449 got_ref_close(base_branch_ref);
7451 got_fileindex_free(fileindex);
7452 free(fileindex_path);
7453 free(tree_id);
7454 if (commit)
7455 got_object_commit_close(commit);
7457 unlockerr = lock_worktree(worktree, LOCK_SH);
7458 if (unlockerr && err == NULL)
7459 err = unlockerr;
7460 return err;
7463 const struct got_error *
7464 got_worktree_integrate_abort(struct got_worktree *worktree,
7465 struct got_fileindex *fileindex, struct got_repository *repo,
7466 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7468 const struct got_error *err = NULL, *unlockerr = NULL;
7470 got_fileindex_free(fileindex);
7472 err = lock_worktree(worktree, LOCK_SH);
7474 unlockerr = got_ref_unlock(branch_ref);
7475 if (unlockerr && err == NULL)
7476 err = unlockerr;
7477 got_ref_close(branch_ref);
7479 unlockerr = got_ref_unlock(base_branch_ref);
7480 if (unlockerr && err == NULL)
7481 err = unlockerr;
7482 got_ref_close(base_branch_ref);
7484 return err;
7487 const struct got_error *
7488 got_worktree_merge_postpone(struct got_worktree *worktree,
7489 struct got_fileindex *fileindex)
7491 const struct got_error *err, *sync_err;
7492 char *fileindex_path = NULL;
7494 err = get_fileindex_path(&fileindex_path, worktree);
7495 if (err)
7496 goto done;
7498 sync_err = sync_fileindex(fileindex, fileindex_path);
7500 err = lock_worktree(worktree, LOCK_SH);
7501 if (sync_err && err == NULL)
7502 err = sync_err;
7503 done:
7504 got_fileindex_free(fileindex);
7505 free(fileindex_path);
7506 return err;
7509 static const struct got_error *
7510 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7512 const struct got_error *err;
7513 char *branch_refname = NULL, *commit_refname = NULL;
7515 err = get_merge_branch_ref_name(&branch_refname, worktree);
7516 if (err)
7517 goto done;
7518 err = delete_ref(branch_refname, repo);
7519 if (err)
7520 goto done;
7522 err = get_merge_commit_ref_name(&commit_refname, worktree);
7523 if (err)
7524 goto done;
7525 err = delete_ref(commit_refname, repo);
7526 if (err)
7527 goto done;
7529 done:
7530 free(branch_refname);
7531 free(commit_refname);
7532 return err;
7535 struct merge_commit_msg_arg {
7536 struct got_worktree *worktree;
7537 const char *branch_name;
7540 static const struct got_error *
7541 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7542 void *arg)
7544 struct merge_commit_msg_arg *a = arg;
7546 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7547 got_worktree_get_head_ref_name(a->worktree)) == -1)
7548 return got_error_from_errno("asprintf");
7550 return NULL;
7554 const struct got_error *
7555 got_worktree_merge_branch(struct got_worktree *worktree,
7556 struct got_fileindex *fileindex,
7557 struct got_object_id *yca_commit_id,
7558 struct got_object_id *branch_tip,
7559 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7560 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7562 const struct got_error *err;
7563 char *fileindex_path = NULL;
7565 err = get_fileindex_path(&fileindex_path, worktree);
7566 if (err)
7567 goto done;
7569 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7570 worktree);
7571 if (err)
7572 goto done;
7574 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7575 branch_tip, repo, progress_cb, progress_arg,
7576 cancel_cb, cancel_arg);
7577 done:
7578 free(fileindex_path);
7579 return err;
7582 const struct got_error *
7583 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7584 struct got_worktree *worktree, struct got_fileindex *fileindex,
7585 const char *author, const char *committer, int allow_bad_symlinks,
7586 struct got_object_id *branch_tip, const char *branch_name,
7587 struct got_repository *repo,
7588 got_worktree_status_cb status_cb, void *status_arg)
7591 const struct got_error *err = NULL, *sync_err;
7592 struct got_pathlist_head commitable_paths;
7593 struct collect_commitables_arg cc_arg;
7594 struct got_pathlist_entry *pe;
7595 struct got_reference *head_ref = NULL;
7596 struct got_object_id *head_commit_id = NULL;
7597 int have_staged_files = 0;
7598 struct merge_commit_msg_arg mcm_arg;
7599 char *fileindex_path = NULL;
7601 *new_commit_id = NULL;
7603 TAILQ_INIT(&commitable_paths);
7605 err = get_fileindex_path(&fileindex_path, worktree);
7606 if (err)
7607 goto done;
7609 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7610 if (err)
7611 goto done;
7613 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7614 if (err)
7615 goto done;
7617 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7618 &have_staged_files);
7619 if (err && err->code != GOT_ERR_CANCELLED)
7620 goto done;
7621 if (have_staged_files) {
7622 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7623 goto done;
7626 cc_arg.commitable_paths = &commitable_paths;
7627 cc_arg.worktree = worktree;
7628 cc_arg.fileindex = fileindex;
7629 cc_arg.repo = repo;
7630 cc_arg.have_staged_files = have_staged_files;
7631 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7632 err = worktree_status(worktree, "", fileindex, repo,
7633 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7634 if (err)
7635 goto done;
7637 if (TAILQ_EMPTY(&commitable_paths)) {
7638 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7639 "merge of %s cannot proceed", branch_name);
7640 goto done;
7643 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7644 struct got_commitable *ct = pe->data;
7645 const char *ct_path = ct->in_repo_path;
7647 while (ct_path[0] == '/')
7648 ct_path++;
7649 err = check_out_of_date(ct_path, ct->status,
7650 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7651 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7652 if (err)
7653 goto done;
7657 mcm_arg.worktree = worktree;
7658 mcm_arg.branch_name = branch_name;
7659 err = commit_worktree(new_commit_id, &commitable_paths,
7660 head_commit_id, branch_tip, worktree, author, committer,
7661 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7662 if (err)
7663 goto done;
7665 err = update_fileindex_after_commit(worktree, &commitable_paths,
7666 *new_commit_id, fileindex, have_staged_files);
7667 sync_err = sync_fileindex(fileindex, fileindex_path);
7668 if (sync_err && err == NULL)
7669 err = sync_err;
7670 done:
7671 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7672 struct got_commitable *ct = pe->data;
7673 free_commitable(ct);
7675 got_pathlist_free(&commitable_paths);
7676 free(fileindex_path);
7677 return err;
7680 const struct got_error *
7681 got_worktree_merge_complete(struct got_worktree *worktree,
7682 struct got_fileindex *fileindex, struct got_repository *repo)
7684 const struct got_error *err, *unlockerr, *sync_err;
7685 char *fileindex_path = NULL;
7687 err = delete_merge_refs(worktree, repo);
7688 if (err)
7689 goto done;
7691 err = get_fileindex_path(&fileindex_path, worktree);
7692 if (err)
7693 goto done;
7694 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7695 sync_err = sync_fileindex(fileindex, fileindex_path);
7696 if (sync_err && err == NULL)
7697 err = sync_err;
7698 done:
7699 got_fileindex_free(fileindex);
7700 free(fileindex_path);
7701 unlockerr = lock_worktree(worktree, LOCK_SH);
7702 if (unlockerr && err == NULL)
7703 err = unlockerr;
7704 return err;
7707 const struct got_error *
7708 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7709 struct got_repository *repo)
7711 const struct got_error *err;
7712 char *branch_refname = NULL;
7713 struct got_reference *branch_ref = NULL;
7715 *in_progress = 0;
7717 err = get_merge_branch_ref_name(&branch_refname, worktree);
7718 if (err)
7719 return err;
7720 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7721 free(branch_refname);
7722 if (err) {
7723 if (err->code != GOT_ERR_NOT_REF)
7724 return err;
7725 } else
7726 *in_progress = 1;
7728 return NULL;
7731 const struct got_error *got_worktree_merge_prepare(
7732 struct got_fileindex **fileindex, struct got_worktree *worktree,
7733 struct got_reference *branch, struct got_repository *repo)
7735 const struct got_error *err = NULL;
7736 char *fileindex_path = NULL;
7737 char *branch_refname = NULL, *commit_refname = NULL;
7738 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7739 struct got_reference *commit_ref = NULL;
7740 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7741 struct check_rebase_ok_arg ok_arg;
7743 *fileindex = NULL;
7745 err = lock_worktree(worktree, LOCK_EX);
7746 if (err)
7747 return err;
7749 err = open_fileindex(fileindex, &fileindex_path, worktree);
7750 if (err)
7751 goto done;
7753 /* Preconditions are the same as for rebase. */
7754 ok_arg.worktree = worktree;
7755 ok_arg.repo = repo;
7756 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7757 &ok_arg);
7758 if (err)
7759 goto done;
7761 err = get_merge_branch_ref_name(&branch_refname, worktree);
7762 if (err)
7763 return err;
7765 err = get_merge_commit_ref_name(&commit_refname, worktree);
7766 if (err)
7767 return err;
7769 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7771 if (err)
7772 goto done;
7774 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7775 if (err)
7776 goto done;
7778 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7779 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7780 goto done;
7783 err = got_ref_resolve(&branch_tip, repo, branch);
7784 if (err)
7785 goto done;
7787 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7788 if (err)
7789 goto done;
7790 err = got_ref_write(branch_ref, repo);
7791 if (err)
7792 goto done;
7794 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7795 if (err)
7796 goto done;
7797 err = got_ref_write(commit_ref, repo);
7798 if (err)
7799 goto done;
7801 done:
7802 free(branch_refname);
7803 free(commit_refname);
7804 free(fileindex_path);
7805 if (branch_ref)
7806 got_ref_close(branch_ref);
7807 if (commit_ref)
7808 got_ref_close(commit_ref);
7809 if (wt_branch)
7810 got_ref_close(wt_branch);
7811 free(wt_branch_tip);
7812 if (err) {
7813 if (*fileindex) {
7814 got_fileindex_free(*fileindex);
7815 *fileindex = NULL;
7817 lock_worktree(worktree, LOCK_SH);
7819 return err;
7822 const struct got_error *
7823 got_worktree_merge_continue(char **branch_name,
7824 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7825 struct got_worktree *worktree, struct got_repository *repo)
7827 const struct got_error *err;
7828 char *commit_refname = NULL, *branch_refname = NULL;
7829 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7830 char *fileindex_path = NULL;
7831 int have_staged_files = 0;
7833 *branch_name = NULL;
7834 *branch_tip = NULL;
7835 *fileindex = NULL;
7837 err = lock_worktree(worktree, LOCK_EX);
7838 if (err)
7839 return err;
7841 err = open_fileindex(fileindex, &fileindex_path, worktree);
7842 if (err)
7843 goto done;
7845 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7846 &have_staged_files);
7847 if (err && err->code != GOT_ERR_CANCELLED)
7848 goto done;
7849 if (have_staged_files) {
7850 err = got_error(GOT_ERR_STAGED_PATHS);
7851 goto done;
7854 err = get_merge_branch_ref_name(&branch_refname, worktree);
7855 if (err)
7856 goto done;
7858 err = get_merge_commit_ref_name(&commit_refname, worktree);
7859 if (err)
7860 goto done;
7862 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7863 if (err)
7864 goto done;
7866 if (!got_ref_is_symbolic(branch_ref)) {
7867 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7868 "%s is not a symbolic reference",
7869 got_ref_get_name(branch_ref));
7870 goto done;
7872 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7873 if (*branch_name == NULL) {
7874 err = got_error_from_errno("strdup");
7875 goto done;
7878 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7879 if (err)
7880 goto done;
7882 err = got_ref_resolve(branch_tip, repo, commit_ref);
7883 if (err)
7884 goto done;
7885 done:
7886 free(commit_refname);
7887 free(branch_refname);
7888 free(fileindex_path);
7889 if (commit_ref)
7890 got_ref_close(commit_ref);
7891 if (branch_ref)
7892 got_ref_close(branch_ref);
7893 if (err) {
7894 if (*branch_name) {
7895 free(*branch_name);
7896 *branch_name = NULL;
7898 free(*branch_tip);
7899 *branch_tip = NULL;
7900 if (*fileindex) {
7901 got_fileindex_free(*fileindex);
7902 *fileindex = NULL;
7904 lock_worktree(worktree, LOCK_SH);
7906 return err;
7909 const struct got_error *
7910 got_worktree_merge_abort(struct got_worktree *worktree,
7911 struct got_fileindex *fileindex, struct got_repository *repo,
7912 got_worktree_checkout_cb progress_cb, void *progress_arg)
7914 const struct got_error *err, *unlockerr, *sync_err;
7915 struct got_object_id *commit_id = NULL;
7916 struct got_commit_object *commit = NULL;
7917 char *fileindex_path = NULL;
7918 struct revert_file_args rfa;
7919 struct got_object_id *tree_id = NULL;
7921 err = got_object_open_as_commit(&commit, repo,
7922 worktree->base_commit_id);
7923 if (err)
7924 goto done;
7926 err = got_object_id_by_path(&tree_id, repo, commit,
7927 worktree->path_prefix);
7928 if (err)
7929 goto done;
7931 err = delete_merge_refs(worktree, repo);
7932 if (err)
7933 goto done;
7935 err = get_fileindex_path(&fileindex_path, worktree);
7936 if (err)
7937 goto done;
7939 rfa.worktree = worktree;
7940 rfa.fileindex = fileindex;
7941 rfa.progress_cb = progress_cb;
7942 rfa.progress_arg = progress_arg;
7943 rfa.patch_cb = NULL;
7944 rfa.patch_arg = NULL;
7945 rfa.repo = repo;
7946 rfa.unlink_added_files = 1;
7947 err = worktree_status(worktree, "", fileindex, repo,
7948 revert_file, &rfa, NULL, NULL, 1, 0);
7949 if (err)
7950 goto sync;
7952 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7953 repo, progress_cb, progress_arg, NULL, NULL);
7954 sync:
7955 sync_err = sync_fileindex(fileindex, fileindex_path);
7956 if (sync_err && err == NULL)
7957 err = sync_err;
7958 done:
7959 free(tree_id);
7960 free(commit_id);
7961 if (commit)
7962 got_object_commit_close(commit);
7963 if (fileindex)
7964 got_fileindex_free(fileindex);
7965 free(fileindex_path);
7967 unlockerr = lock_worktree(worktree, LOCK_SH);
7968 if (unlockerr && err == NULL)
7969 err = unlockerr;
7970 return err;
7973 struct check_stage_ok_arg {
7974 struct got_object_id *head_commit_id;
7975 struct got_worktree *worktree;
7976 struct got_fileindex *fileindex;
7977 struct got_repository *repo;
7978 int have_changes;
7981 static const struct got_error *
7982 check_stage_ok(void *arg, unsigned char status,
7983 unsigned char staged_status, const char *relpath,
7984 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7985 struct got_object_id *commit_id, int dirfd, const char *de_name)
7987 struct check_stage_ok_arg *a = arg;
7988 const struct got_error *err = NULL;
7989 struct got_fileindex_entry *ie;
7990 struct got_object_id base_commit_id;
7991 struct got_object_id *base_commit_idp = NULL;
7992 char *in_repo_path = NULL, *p;
7994 if (status == GOT_STATUS_UNVERSIONED ||
7995 status == GOT_STATUS_NO_CHANGE)
7996 return NULL;
7997 if (status == GOT_STATUS_NONEXISTENT)
7998 return got_error_set_errno(ENOENT, relpath);
8000 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8001 if (ie == NULL)
8002 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8004 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8005 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8006 relpath) == -1)
8007 return got_error_from_errno("asprintf");
8009 if (got_fileindex_entry_has_commit(ie)) {
8010 memcpy(base_commit_id.sha1, ie->commit_sha1,
8011 SHA1_DIGEST_LENGTH);
8012 base_commit_idp = &base_commit_id;
8015 if (status == GOT_STATUS_CONFLICT) {
8016 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8017 goto done;
8018 } else if (status != GOT_STATUS_ADD &&
8019 status != GOT_STATUS_MODIFY &&
8020 status != GOT_STATUS_DELETE) {
8021 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8022 goto done;
8025 a->have_changes = 1;
8027 p = in_repo_path;
8028 while (p[0] == '/')
8029 p++;
8030 err = check_out_of_date(p, status, staged_status,
8031 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8032 GOT_ERR_STAGE_OUT_OF_DATE);
8033 done:
8034 free(in_repo_path);
8035 return err;
8038 struct stage_path_arg {
8039 struct got_worktree *worktree;
8040 struct got_fileindex *fileindex;
8041 struct got_repository *repo;
8042 got_worktree_status_cb status_cb;
8043 void *status_arg;
8044 got_worktree_patch_cb patch_cb;
8045 void *patch_arg;
8046 int staged_something;
8047 int allow_bad_symlinks;
8050 static const struct got_error *
8051 stage_path(void *arg, unsigned char status,
8052 unsigned char staged_status, const char *relpath,
8053 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8054 struct got_object_id *commit_id, int dirfd, const char *de_name)
8056 struct stage_path_arg *a = arg;
8057 const struct got_error *err = NULL;
8058 struct got_fileindex_entry *ie;
8059 char *ondisk_path = NULL, *path_content = NULL;
8060 uint32_t stage;
8061 struct got_object_id *new_staged_blob_id = NULL;
8062 struct stat sb;
8064 if (status == GOT_STATUS_UNVERSIONED)
8065 return NULL;
8067 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8068 if (ie == NULL)
8069 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8071 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8072 relpath)== -1)
8073 return got_error_from_errno("asprintf");
8075 switch (status) {
8076 case GOT_STATUS_ADD:
8077 case GOT_STATUS_MODIFY:
8078 /* XXX could sb.st_mode be passed in by our caller? */
8079 if (lstat(ondisk_path, &sb) == -1) {
8080 err = got_error_from_errno2("lstat", ondisk_path);
8081 break;
8083 if (a->patch_cb) {
8084 if (status == GOT_STATUS_ADD) {
8085 int choice = GOT_PATCH_CHOICE_NONE;
8086 err = (*a->patch_cb)(&choice, a->patch_arg,
8087 status, ie->path, NULL, 1, 1);
8088 if (err)
8089 break;
8090 if (choice != GOT_PATCH_CHOICE_YES)
8091 break;
8092 } else {
8093 err = create_patched_content(&path_content, 0,
8094 staged_blob_id ? staged_blob_id : blob_id,
8095 ondisk_path, dirfd, de_name, ie->path,
8096 a->repo, a->patch_cb, a->patch_arg);
8097 if (err || path_content == NULL)
8098 break;
8101 err = got_object_blob_create(&new_staged_blob_id,
8102 path_content ? path_content : ondisk_path, a->repo);
8103 if (err)
8104 break;
8105 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8106 SHA1_DIGEST_LENGTH);
8107 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8108 stage = GOT_FILEIDX_STAGE_ADD;
8109 else
8110 stage = GOT_FILEIDX_STAGE_MODIFY;
8111 got_fileindex_entry_stage_set(ie, stage);
8112 if (S_ISLNK(sb.st_mode)) {
8113 int is_bad_symlink = 0;
8114 if (!a->allow_bad_symlinks) {
8115 char target_path[PATH_MAX];
8116 ssize_t target_len;
8117 target_len = readlink(ondisk_path, target_path,
8118 sizeof(target_path));
8119 if (target_len == -1) {
8120 err = got_error_from_errno2("readlink",
8121 ondisk_path);
8122 break;
8124 err = is_bad_symlink_target(&is_bad_symlink,
8125 target_path, target_len, ondisk_path,
8126 a->worktree->root_path);
8127 if (err)
8128 break;
8129 if (is_bad_symlink) {
8130 err = got_error_path(ondisk_path,
8131 GOT_ERR_BAD_SYMLINK);
8132 break;
8135 if (is_bad_symlink)
8136 got_fileindex_entry_staged_filetype_set(ie,
8137 GOT_FILEIDX_MODE_BAD_SYMLINK);
8138 else
8139 got_fileindex_entry_staged_filetype_set(ie,
8140 GOT_FILEIDX_MODE_SYMLINK);
8141 } else {
8142 got_fileindex_entry_staged_filetype_set(ie,
8143 GOT_FILEIDX_MODE_REGULAR_FILE);
8145 a->staged_something = 1;
8146 if (a->status_cb == NULL)
8147 break;
8148 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8149 get_staged_status(ie), relpath, blob_id,
8150 new_staged_blob_id, NULL, dirfd, de_name);
8151 if (err)
8152 break;
8154 * When staging the reverse of the staged diff,
8155 * implicitly unstage the file.
8157 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8158 sizeof(ie->blob_sha1)) == 0) {
8159 got_fileindex_entry_stage_set(ie,
8160 GOT_FILEIDX_STAGE_NONE);
8162 break;
8163 case GOT_STATUS_DELETE:
8164 if (staged_status == GOT_STATUS_DELETE)
8165 break;
8166 if (a->patch_cb) {
8167 int choice = GOT_PATCH_CHOICE_NONE;
8168 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8169 ie->path, NULL, 1, 1);
8170 if (err)
8171 break;
8172 if (choice == GOT_PATCH_CHOICE_NO)
8173 break;
8174 if (choice != GOT_PATCH_CHOICE_YES) {
8175 err = got_error(GOT_ERR_PATCH_CHOICE);
8176 break;
8179 stage = GOT_FILEIDX_STAGE_DELETE;
8180 got_fileindex_entry_stage_set(ie, stage);
8181 a->staged_something = 1;
8182 if (a->status_cb == NULL)
8183 break;
8184 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8185 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8186 de_name);
8187 break;
8188 case GOT_STATUS_NO_CHANGE:
8189 break;
8190 case GOT_STATUS_CONFLICT:
8191 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8192 break;
8193 case GOT_STATUS_NONEXISTENT:
8194 err = got_error_set_errno(ENOENT, relpath);
8195 break;
8196 default:
8197 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8198 break;
8201 if (path_content && unlink(path_content) == -1 && err == NULL)
8202 err = got_error_from_errno2("unlink", path_content);
8203 free(path_content);
8204 free(ondisk_path);
8205 free(new_staged_blob_id);
8206 return err;
8209 const struct got_error *
8210 got_worktree_stage(struct got_worktree *worktree,
8211 struct got_pathlist_head *paths,
8212 got_worktree_status_cb status_cb, void *status_arg,
8213 got_worktree_patch_cb patch_cb, void *patch_arg,
8214 int allow_bad_symlinks, struct got_repository *repo)
8216 const struct got_error *err = NULL, *sync_err, *unlockerr;
8217 struct got_pathlist_entry *pe;
8218 struct got_fileindex *fileindex = NULL;
8219 char *fileindex_path = NULL;
8220 struct got_reference *head_ref = NULL;
8221 struct got_object_id *head_commit_id = NULL;
8222 struct check_stage_ok_arg oka;
8223 struct stage_path_arg spa;
8225 err = lock_worktree(worktree, LOCK_EX);
8226 if (err)
8227 return err;
8229 err = got_ref_open(&head_ref, repo,
8230 got_worktree_get_head_ref_name(worktree), 0);
8231 if (err)
8232 goto done;
8233 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8234 if (err)
8235 goto done;
8236 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8237 if (err)
8238 goto done;
8240 /* Check pre-conditions before staging anything. */
8241 oka.head_commit_id = head_commit_id;
8242 oka.worktree = worktree;
8243 oka.fileindex = fileindex;
8244 oka.repo = repo;
8245 oka.have_changes = 0;
8246 TAILQ_FOREACH(pe, paths, entry) {
8247 err = worktree_status(worktree, pe->path, fileindex, repo,
8248 check_stage_ok, &oka, NULL, NULL, 1, 0);
8249 if (err)
8250 goto done;
8252 if (!oka.have_changes) {
8253 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8254 goto done;
8257 spa.worktree = worktree;
8258 spa.fileindex = fileindex;
8259 spa.repo = repo;
8260 spa.patch_cb = patch_cb;
8261 spa.patch_arg = patch_arg;
8262 spa.status_cb = status_cb;
8263 spa.status_arg = status_arg;
8264 spa.staged_something = 0;
8265 spa.allow_bad_symlinks = allow_bad_symlinks;
8266 TAILQ_FOREACH(pe, paths, entry) {
8267 err = worktree_status(worktree, pe->path, fileindex, repo,
8268 stage_path, &spa, NULL, NULL, 1, 0);
8269 if (err)
8270 goto done;
8272 if (!spa.staged_something) {
8273 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8274 goto done;
8277 sync_err = sync_fileindex(fileindex, fileindex_path);
8278 if (sync_err && err == NULL)
8279 err = sync_err;
8280 done:
8281 if (head_ref)
8282 got_ref_close(head_ref);
8283 free(head_commit_id);
8284 free(fileindex_path);
8285 if (fileindex)
8286 got_fileindex_free(fileindex);
8287 unlockerr = lock_worktree(worktree, LOCK_SH);
8288 if (unlockerr && err == NULL)
8289 err = unlockerr;
8290 return err;
8293 struct unstage_path_arg {
8294 struct got_worktree *worktree;
8295 struct got_fileindex *fileindex;
8296 struct got_repository *repo;
8297 got_worktree_checkout_cb progress_cb;
8298 void *progress_arg;
8299 got_worktree_patch_cb patch_cb;
8300 void *patch_arg;
8303 static const struct got_error *
8304 create_unstaged_content(char **path_unstaged_content,
8305 char **path_new_staged_content, struct got_object_id *blob_id,
8306 struct got_object_id *staged_blob_id, const char *relpath,
8307 struct got_repository *repo,
8308 got_worktree_patch_cb patch_cb, void *patch_arg)
8310 const struct got_error *err, *free_err;
8311 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8312 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8313 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8314 struct got_diffreg_result *diffreg_result = NULL;
8315 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8316 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8317 int fd1 = -1, fd2 = -1;
8319 *path_unstaged_content = NULL;
8320 *path_new_staged_content = NULL;
8322 err = got_object_id_str(&label1, blob_id);
8323 if (err)
8324 return err;
8326 fd1 = got_opentempfd();
8327 if (fd1 == -1) {
8328 err = got_error_from_errno("got_opentempfd");
8329 goto done;
8331 fd2 = got_opentempfd();
8332 if (fd2 == -1) {
8333 err = got_error_from_errno("got_opentempfd");
8334 goto done;
8337 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8338 if (err)
8339 goto done;
8341 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8342 if (err)
8343 goto done;
8345 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8346 if (err)
8347 goto done;
8349 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8350 fd2);
8351 if (err)
8352 goto done;
8354 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8355 if (err)
8356 goto done;
8358 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8359 if (err)
8360 goto done;
8362 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8363 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8364 if (err)
8365 goto done;
8367 err = got_opentemp_named(path_unstaged_content, &outfile,
8368 "got-unstaged-content");
8369 if (err)
8370 goto done;
8371 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8372 "got-new-staged-content");
8373 if (err)
8374 goto done;
8376 if (fseek(f1, 0L, SEEK_SET) == -1) {
8377 err = got_ferror(f1, GOT_ERR_IO);
8378 goto done;
8380 if (fseek(f2, 0L, SEEK_SET) == -1) {
8381 err = got_ferror(f2, GOT_ERR_IO);
8382 goto done;
8384 /* Count the number of actual changes in the diff result. */
8385 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8386 struct diff_chunk_context cc = {};
8387 diff_chunk_context_load_change(&cc, &nchunks_used,
8388 diffreg_result->result, n, 0);
8389 nchanges++;
8391 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8392 int choice;
8393 err = apply_or_reject_change(&choice, &nchunks_used,
8394 diffreg_result->result, n, relpath, f1, f2,
8395 &line_cur1, &line_cur2,
8396 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8397 if (err)
8398 goto done;
8399 if (choice == GOT_PATCH_CHOICE_YES)
8400 have_content = 1;
8401 else
8402 have_rejected_content = 1;
8403 if (choice == GOT_PATCH_CHOICE_QUIT)
8404 break;
8406 if (have_content || have_rejected_content)
8407 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8408 outfile, rejectfile);
8409 done:
8410 free(label1);
8411 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8412 err = got_error_from_errno("close");
8413 if (blob)
8414 got_object_blob_close(blob);
8415 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8416 err = got_error_from_errno("close");
8417 if (staged_blob)
8418 got_object_blob_close(staged_blob);
8419 free_err = got_diffreg_result_free(diffreg_result);
8420 if (free_err && err == NULL)
8421 err = free_err;
8422 if (f1 && fclose(f1) == EOF && err == NULL)
8423 err = got_error_from_errno2("fclose", path1);
8424 if (f2 && fclose(f2) == EOF && err == NULL)
8425 err = got_error_from_errno2("fclose", path2);
8426 if (outfile && fclose(outfile) == EOF && err == NULL)
8427 err = got_error_from_errno2("fclose", *path_unstaged_content);
8428 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8429 err = got_error_from_errno2("fclose", *path_new_staged_content);
8430 if (path1 && unlink(path1) == -1 && err == NULL)
8431 err = got_error_from_errno2("unlink", path1);
8432 if (path2 && unlink(path2) == -1 && err == NULL)
8433 err = got_error_from_errno2("unlink", path2);
8434 if (err || !have_content) {
8435 if (*path_unstaged_content &&
8436 unlink(*path_unstaged_content) == -1 && err == NULL)
8437 err = got_error_from_errno2("unlink",
8438 *path_unstaged_content);
8439 free(*path_unstaged_content);
8440 *path_unstaged_content = NULL;
8442 if (err || !have_content || !have_rejected_content) {
8443 if (*path_new_staged_content &&
8444 unlink(*path_new_staged_content) == -1 && err == NULL)
8445 err = got_error_from_errno2("unlink",
8446 *path_new_staged_content);
8447 free(*path_new_staged_content);
8448 *path_new_staged_content = NULL;
8450 free(path1);
8451 free(path2);
8452 return err;
8455 static const struct got_error *
8456 unstage_hunks(struct got_object_id *staged_blob_id,
8457 struct got_blob_object *blob_base,
8458 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8459 const char *ondisk_path, const char *label_orig,
8460 struct got_worktree *worktree, struct got_repository *repo,
8461 got_worktree_patch_cb patch_cb, void *patch_arg,
8462 got_worktree_checkout_cb progress_cb, void *progress_arg)
8464 const struct got_error *err = NULL;
8465 char *path_unstaged_content = NULL;
8466 char *path_new_staged_content = NULL;
8467 char *parent = NULL, *base_path = NULL;
8468 char *blob_base_path = NULL;
8469 struct got_object_id *new_staged_blob_id = NULL;
8470 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8471 struct stat sb;
8473 err = create_unstaged_content(&path_unstaged_content,
8474 &path_new_staged_content, blob_id, staged_blob_id,
8475 ie->path, repo, patch_cb, patch_arg);
8476 if (err)
8477 return err;
8479 if (path_unstaged_content == NULL)
8480 return NULL;
8482 if (path_new_staged_content) {
8483 err = got_object_blob_create(&new_staged_blob_id,
8484 path_new_staged_content, repo);
8485 if (err)
8486 goto done;
8489 f = fopen(path_unstaged_content, "re");
8490 if (f == NULL) {
8491 err = got_error_from_errno2("fopen",
8492 path_unstaged_content);
8493 goto done;
8495 if (fstat(fileno(f), &sb) == -1) {
8496 err = got_error_from_errno2("fstat", path_unstaged_content);
8497 goto done;
8499 if (got_fileindex_entry_staged_filetype_get(ie) ==
8500 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8501 char link_target[PATH_MAX];
8502 size_t r;
8503 r = fread(link_target, 1, sizeof(link_target), f);
8504 if (r == 0 && ferror(f)) {
8505 err = got_error_from_errno("fread");
8506 goto done;
8508 if (r >= sizeof(link_target)) { /* should not happen */
8509 err = got_error(GOT_ERR_NO_SPACE);
8510 goto done;
8512 link_target[r] = '\0';
8513 err = merge_symlink(worktree, blob_base,
8514 ondisk_path, ie->path, label_orig, link_target,
8515 worktree->base_commit_id, repo, progress_cb,
8516 progress_arg);
8517 } else {
8518 int local_changes_subsumed;
8520 err = got_path_dirname(&parent, ondisk_path);
8521 if (err)
8522 return err;
8524 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8525 parent) == -1) {
8526 err = got_error_from_errno("asprintf");
8527 base_path = NULL;
8528 goto done;
8531 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8532 if (err)
8533 goto done;
8534 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8535 blob_base);
8536 if (err)
8537 goto done;
8540 * In order the run a 3-way merge with a symlink we copy the symlink's
8541 * target path into a temporary file and use that file with diff3.
8543 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8544 err = dump_symlink_target_path_to_file(&f_deriv2,
8545 ondisk_path);
8546 if (err)
8547 goto done;
8548 } else {
8549 int fd;
8550 fd = open(ondisk_path,
8551 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8552 if (fd == -1) {
8553 err = got_error_from_errno2("open", ondisk_path);
8554 goto done;
8556 f_deriv2 = fdopen(fd, "r");
8557 if (f_deriv2 == NULL) {
8558 err = got_error_from_errno2("fdopen", ondisk_path);
8559 close(fd);
8560 goto done;
8564 err = merge_file(&local_changes_subsumed, worktree,
8565 f_base, f, f_deriv2, ondisk_path, ie->path,
8566 got_fileindex_perms_to_st(ie),
8567 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8568 repo, progress_cb, progress_arg);
8570 if (err)
8571 goto done;
8573 if (new_staged_blob_id) {
8574 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8575 SHA1_DIGEST_LENGTH);
8576 } else {
8577 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8578 got_fileindex_entry_staged_filetype_set(ie, 0);
8580 done:
8581 free(new_staged_blob_id);
8582 if (path_unstaged_content &&
8583 unlink(path_unstaged_content) == -1 && err == NULL)
8584 err = got_error_from_errno2("unlink", path_unstaged_content);
8585 if (path_new_staged_content &&
8586 unlink(path_new_staged_content) == -1 && err == NULL)
8587 err = got_error_from_errno2("unlink", path_new_staged_content);
8588 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8589 err = got_error_from_errno2("unlink", blob_base_path);
8590 if (f_base && fclose(f_base) == EOF && err == NULL)
8591 err = got_error_from_errno2("fclose", path_unstaged_content);
8592 if (f && fclose(f) == EOF && err == NULL)
8593 err = got_error_from_errno2("fclose", path_unstaged_content);
8594 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8595 err = got_error_from_errno2("fclose", ondisk_path);
8596 free(path_unstaged_content);
8597 free(path_new_staged_content);
8598 free(blob_base_path);
8599 free(parent);
8600 free(base_path);
8601 return err;
8604 static const struct got_error *
8605 unstage_path(void *arg, unsigned char status,
8606 unsigned char staged_status, const char *relpath,
8607 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8608 struct got_object_id *commit_id, int dirfd, const char *de_name)
8610 const struct got_error *err = NULL;
8611 struct unstage_path_arg *a = arg;
8612 struct got_fileindex_entry *ie;
8613 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8614 char *ondisk_path = NULL;
8615 char *id_str = NULL, *label_orig = NULL;
8616 int local_changes_subsumed;
8617 struct stat sb;
8618 int fd1 = -1, fd2 = -1;
8620 if (staged_status != GOT_STATUS_ADD &&
8621 staged_status != GOT_STATUS_MODIFY &&
8622 staged_status != GOT_STATUS_DELETE)
8623 return NULL;
8625 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8626 if (ie == NULL)
8627 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8629 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8630 == -1)
8631 return got_error_from_errno("asprintf");
8633 err = got_object_id_str(&id_str,
8634 commit_id ? commit_id : a->worktree->base_commit_id);
8635 if (err)
8636 goto done;
8637 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8638 id_str) == -1) {
8639 err = got_error_from_errno("asprintf");
8640 goto done;
8643 fd1 = got_opentempfd();
8644 if (fd1 == -1) {
8645 err = got_error_from_errno("got_opentempfd");
8646 goto done;
8648 fd2 = got_opentempfd();
8649 if (fd2 == -1) {
8650 err = got_error_from_errno("got_opentempfd");
8651 goto done;
8654 switch (staged_status) {
8655 case GOT_STATUS_MODIFY:
8656 err = got_object_open_as_blob(&blob_base, a->repo,
8657 blob_id, 8192, fd1);
8658 if (err)
8659 break;
8660 /* fall through */
8661 case GOT_STATUS_ADD:
8662 if (a->patch_cb) {
8663 if (staged_status == GOT_STATUS_ADD) {
8664 int choice = GOT_PATCH_CHOICE_NONE;
8665 err = (*a->patch_cb)(&choice, a->patch_arg,
8666 staged_status, ie->path, NULL, 1, 1);
8667 if (err)
8668 break;
8669 if (choice != GOT_PATCH_CHOICE_YES)
8670 break;
8671 } else {
8672 err = unstage_hunks(staged_blob_id,
8673 blob_base, blob_id, ie, ondisk_path,
8674 label_orig, a->worktree, a->repo,
8675 a->patch_cb, a->patch_arg,
8676 a->progress_cb, a->progress_arg);
8677 break; /* Done with this file. */
8680 err = got_object_open_as_blob(&blob_staged, a->repo,
8681 staged_blob_id, 8192, fd2);
8682 if (err)
8683 break;
8684 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8685 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8686 case GOT_FILEIDX_MODE_REGULAR_FILE:
8687 err = merge_blob(&local_changes_subsumed, a->worktree,
8688 blob_base, ondisk_path, relpath,
8689 got_fileindex_perms_to_st(ie), label_orig,
8690 blob_staged, commit_id ? commit_id :
8691 a->worktree->base_commit_id, a->repo,
8692 a->progress_cb, a->progress_arg);
8693 break;
8694 case GOT_FILEIDX_MODE_SYMLINK:
8695 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8696 char *staged_target;
8697 err = got_object_blob_read_to_str(
8698 &staged_target, blob_staged);
8699 if (err)
8700 goto done;
8701 err = merge_symlink(a->worktree, blob_base,
8702 ondisk_path, relpath, label_orig,
8703 staged_target, commit_id ? commit_id :
8704 a->worktree->base_commit_id,
8705 a->repo, a->progress_cb, a->progress_arg);
8706 free(staged_target);
8707 } else {
8708 err = merge_blob(&local_changes_subsumed,
8709 a->worktree, blob_base, ondisk_path,
8710 relpath, got_fileindex_perms_to_st(ie),
8711 label_orig, blob_staged,
8712 commit_id ? commit_id :
8713 a->worktree->base_commit_id, a->repo,
8714 a->progress_cb, a->progress_arg);
8716 break;
8717 default:
8718 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8719 break;
8721 if (err == NULL) {
8722 got_fileindex_entry_stage_set(ie,
8723 GOT_FILEIDX_STAGE_NONE);
8724 got_fileindex_entry_staged_filetype_set(ie, 0);
8726 break;
8727 case GOT_STATUS_DELETE:
8728 if (a->patch_cb) {
8729 int choice = GOT_PATCH_CHOICE_NONE;
8730 err = (*a->patch_cb)(&choice, a->patch_arg,
8731 staged_status, ie->path, NULL, 1, 1);
8732 if (err)
8733 break;
8734 if (choice == GOT_PATCH_CHOICE_NO)
8735 break;
8736 if (choice != GOT_PATCH_CHOICE_YES) {
8737 err = got_error(GOT_ERR_PATCH_CHOICE);
8738 break;
8741 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8742 got_fileindex_entry_staged_filetype_set(ie, 0);
8743 err = get_file_status(&status, &sb, ie, ondisk_path,
8744 dirfd, de_name, a->repo);
8745 if (err)
8746 break;
8747 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8748 break;
8750 done:
8751 free(ondisk_path);
8752 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8753 err = got_error_from_errno("close");
8754 if (blob_base)
8755 got_object_blob_close(blob_base);
8756 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8757 err = got_error_from_errno("close");
8758 if (blob_staged)
8759 got_object_blob_close(blob_staged);
8760 free(id_str);
8761 free(label_orig);
8762 return err;
8765 const struct got_error *
8766 got_worktree_unstage(struct got_worktree *worktree,
8767 struct got_pathlist_head *paths,
8768 got_worktree_checkout_cb progress_cb, void *progress_arg,
8769 got_worktree_patch_cb patch_cb, void *patch_arg,
8770 struct got_repository *repo)
8772 const struct got_error *err = NULL, *sync_err, *unlockerr;
8773 struct got_pathlist_entry *pe;
8774 struct got_fileindex *fileindex = NULL;
8775 char *fileindex_path = NULL;
8776 struct unstage_path_arg upa;
8778 err = lock_worktree(worktree, LOCK_EX);
8779 if (err)
8780 return err;
8782 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8783 if (err)
8784 goto done;
8786 upa.worktree = worktree;
8787 upa.fileindex = fileindex;
8788 upa.repo = repo;
8789 upa.progress_cb = progress_cb;
8790 upa.progress_arg = progress_arg;
8791 upa.patch_cb = patch_cb;
8792 upa.patch_arg = patch_arg;
8793 TAILQ_FOREACH(pe, paths, entry) {
8794 err = worktree_status(worktree, pe->path, fileindex, repo,
8795 unstage_path, &upa, NULL, NULL, 1, 0);
8796 if (err)
8797 goto done;
8800 sync_err = sync_fileindex(fileindex, fileindex_path);
8801 if (sync_err && err == NULL)
8802 err = sync_err;
8803 done:
8804 free(fileindex_path);
8805 if (fileindex)
8806 got_fileindex_free(fileindex);
8807 unlockerr = lock_worktree(worktree, LOCK_SH);
8808 if (unlockerr && err == NULL)
8809 err = unlockerr;
8810 return err;
8813 struct report_file_info_arg {
8814 struct got_worktree *worktree;
8815 got_worktree_path_info_cb info_cb;
8816 void *info_arg;
8817 struct got_pathlist_head *paths;
8818 got_cancel_cb cancel_cb;
8819 void *cancel_arg;
8822 static const struct got_error *
8823 report_file_info(void *arg, struct got_fileindex_entry *ie)
8825 struct report_file_info_arg *a = arg;
8826 struct got_pathlist_entry *pe;
8827 struct got_object_id blob_id, staged_blob_id, commit_id;
8828 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8829 struct got_object_id *commit_idp = NULL;
8830 int stage;
8832 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8833 return got_error(GOT_ERR_CANCELLED);
8835 TAILQ_FOREACH(pe, a->paths, entry) {
8836 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8837 got_path_is_child(ie->path, pe->path, pe->path_len))
8838 break;
8840 if (pe == NULL) /* not found */
8841 return NULL;
8843 if (got_fileindex_entry_has_blob(ie)) {
8844 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8845 blob_idp = &blob_id;
8847 stage = got_fileindex_entry_stage_get(ie);
8848 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8849 stage == GOT_FILEIDX_STAGE_ADD) {
8850 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8851 SHA1_DIGEST_LENGTH);
8852 staged_blob_idp = &staged_blob_id;
8855 if (got_fileindex_entry_has_commit(ie)) {
8856 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8857 commit_idp = &commit_id;
8860 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8861 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8864 const struct got_error *
8865 got_worktree_path_info(struct got_worktree *worktree,
8866 struct got_pathlist_head *paths,
8867 got_worktree_path_info_cb info_cb, void *info_arg,
8868 got_cancel_cb cancel_cb, void *cancel_arg)
8871 const struct got_error *err = NULL, *unlockerr;
8872 struct got_fileindex *fileindex = NULL;
8873 char *fileindex_path = NULL;
8874 struct report_file_info_arg arg;
8876 err = lock_worktree(worktree, LOCK_SH);
8877 if (err)
8878 return err;
8880 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8881 if (err)
8882 goto done;
8884 arg.worktree = worktree;
8885 arg.info_cb = info_cb;
8886 arg.info_arg = info_arg;
8887 arg.paths = paths;
8888 arg.cancel_cb = cancel_cb;
8889 arg.cancel_arg = cancel_arg;
8890 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8891 &arg);
8892 done:
8893 free(fileindex_path);
8894 if (fileindex)
8895 got_fileindex_free(fileindex);
8896 unlockerr = lock_worktree(worktree, LOCK_UN);
8897 if (unlockerr && err == NULL)
8898 err = unlockerr;
8899 return err;
8902 static const struct got_error *
8903 patch_check_path(const char *p, char **path, unsigned char *status,
8904 unsigned char *staged_status, struct got_fileindex *fileindex,
8905 struct got_worktree *worktree, struct got_repository *repo)
8907 const struct got_error *err;
8908 struct got_fileindex_entry *ie;
8909 struct stat sb;
8910 char *ondisk_path = NULL;
8912 err = got_worktree_resolve_path(path, worktree, p);
8913 if (err)
8914 return err;
8916 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8917 *path[0] ? "/" : "", *path) == -1)
8918 return got_error_from_errno("asprintf");
8920 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8921 if (ie) {
8922 *staged_status = get_staged_status(ie);
8923 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8924 repo);
8925 if (err)
8926 goto done;
8927 } else {
8928 *staged_status = GOT_STATUS_NO_CHANGE;
8929 *status = GOT_STATUS_UNVERSIONED;
8930 if (lstat(ondisk_path, &sb) == -1) {
8931 if (errno != ENOENT) {
8932 err = got_error_from_errno2("lstat",
8933 ondisk_path);
8934 goto done;
8936 *status = GOT_STATUS_NONEXISTENT;
8940 done:
8941 free(ondisk_path);
8942 return err;
8945 static const struct got_error *
8946 patch_can_rm(const char *path, unsigned char status,
8947 unsigned char staged_status)
8949 if (status == GOT_STATUS_NONEXISTENT)
8950 return got_error_set_errno(ENOENT, path);
8951 if (status != GOT_STATUS_NO_CHANGE &&
8952 status != GOT_STATUS_ADD &&
8953 status != GOT_STATUS_MODIFY &&
8954 status != GOT_STATUS_MODE_CHANGE)
8955 return got_error_path(path, GOT_ERR_FILE_STATUS);
8956 if (staged_status == GOT_STATUS_DELETE)
8957 return got_error_path(path, GOT_ERR_FILE_STATUS);
8958 return NULL;
8961 static const struct got_error *
8962 patch_can_add(const char *path, unsigned char status)
8964 if (status != GOT_STATUS_NONEXISTENT)
8965 return got_error_path(path, GOT_ERR_FILE_STATUS);
8966 return NULL;
8969 static const struct got_error *
8970 patch_can_edit(const char *path, unsigned char status,
8971 unsigned char staged_status)
8973 if (status == GOT_STATUS_NONEXISTENT)
8974 return got_error_set_errno(ENOENT, path);
8975 if (status != GOT_STATUS_NO_CHANGE &&
8976 status != GOT_STATUS_ADD &&
8977 status != GOT_STATUS_MODIFY)
8978 return got_error_path(path, GOT_ERR_FILE_STATUS);
8979 if (staged_status == GOT_STATUS_DELETE)
8980 return got_error_path(path, GOT_ERR_FILE_STATUS);
8981 return NULL;
8984 const struct got_error *
8985 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8986 char **fileindex_path, struct got_worktree *worktree)
8988 return open_fileindex(fileindex, fileindex_path, worktree);
8991 const struct got_error *
8992 got_worktree_patch_check_path(const char *old, const char *new,
8993 char **oldpath, char **newpath, struct got_worktree *worktree,
8994 struct got_repository *repo, struct got_fileindex *fileindex)
8996 const struct got_error *err = NULL;
8997 int file_renamed = 0;
8998 unsigned char status_old, staged_status_old;
8999 unsigned char status_new, staged_status_new;
9001 *oldpath = NULL;
9002 *newpath = NULL;
9004 err = patch_check_path(old != NULL ? old : new, oldpath,
9005 &status_old, &staged_status_old, fileindex, worktree, repo);
9006 if (err)
9007 goto done;
9009 err = patch_check_path(new != NULL ? new : old, newpath,
9010 &status_new, &staged_status_new, fileindex, worktree, repo);
9011 if (err)
9012 goto done;
9014 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9015 file_renamed = 1;
9017 if (old != NULL && new == NULL)
9018 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9019 else if (file_renamed) {
9020 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9021 if (err == NULL)
9022 err = patch_can_add(*newpath, status_new);
9023 } else if (old == NULL)
9024 err = patch_can_add(*newpath, status_new);
9025 else
9026 err = patch_can_edit(*newpath, status_new, staged_status_new);
9028 done:
9029 if (err) {
9030 free(*oldpath);
9031 *oldpath = NULL;
9032 free(*newpath);
9033 *newpath = NULL;
9035 return err;
9038 const struct got_error *
9039 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9040 struct got_worktree *worktree, struct got_fileindex *fileindex,
9041 got_worktree_checkout_cb progress_cb, void *progress_arg)
9043 struct schedule_addition_args saa;
9045 memset(&saa, 0, sizeof(saa));
9046 saa.worktree = worktree;
9047 saa.fileindex = fileindex;
9048 saa.progress_cb = progress_cb;
9049 saa.progress_arg = progress_arg;
9050 saa.repo = repo;
9052 return worktree_status(worktree, path, fileindex, repo,
9053 schedule_addition, &saa, NULL, NULL, 1, 0);
9056 const struct got_error *
9057 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9058 struct got_worktree *worktree, struct got_fileindex *fileindex,
9059 got_worktree_delete_cb progress_cb, void *progress_arg)
9061 struct schedule_deletion_args sda;
9063 memset(&sda, 0, sizeof(sda));
9064 sda.worktree = worktree;
9065 sda.fileindex = fileindex;
9066 sda.progress_cb = progress_cb;
9067 sda.progress_arg = progress_arg;
9068 sda.repo = repo;
9069 sda.delete_local_mods = 0;
9070 sda.keep_on_disk = 0;
9071 sda.ignore_missing_paths = 0;
9072 sda.status_codes = NULL;
9074 return worktree_status(worktree, path, fileindex, repo,
9075 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9078 const struct got_error *
9079 got_worktree_patch_complete(struct got_fileindex *fileindex,
9080 const char *fileindex_path)
9082 const struct got_error *err = NULL;
9084 err = sync_fileindex(fileindex, fileindex_path);
9085 got_fileindex_free(fileindex);
9087 return err;