Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_diff.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 #define WORKTREE_ALGO(wt) ((wt)->base_commit_id->algo)
70 static mode_t apply_umask(mode_t);
72 static const struct got_error *
73 create_meta_file(const char *path_got, const char *name, const char *content)
74 {
75 const struct got_error *err = NULL;
76 char *path;
78 if (asprintf(&path, "%s/%s", path_got, name) == -1)
79 return got_error_from_errno("asprintf");
81 err = got_path_create_file(path, content);
82 free(path);
83 return err;
84 }
86 static const struct got_error *
87 update_meta_file(const char *path_got, const char *name, const char *content)
88 {
89 const struct got_error *err = NULL;
90 FILE *tmpfile = NULL;
91 char *tmppath = NULL;
92 char *path = NULL;
94 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
95 err = got_error_from_errno("asprintf");
96 path = NULL;
97 goto done;
98 }
100 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
101 if (err)
102 goto done;
104 if (content) {
105 int len = fprintf(tmpfile, "%s\n", content);
106 if (len != strlen(content) + 1) {
107 err = got_error_from_errno2("fprintf", tmppath);
108 goto done;
112 if (rename(tmppath, path) != 0) {
113 err = got_error_from_errno3("rename", tmppath, path);
114 unlink(tmppath);
115 goto done;
118 done:
119 if (fclose(tmpfile) == EOF && err == NULL)
120 err = got_error_from_errno2("fclose", tmppath);
121 free(tmppath);
122 return err;
125 static const struct got_error *
126 write_head_ref(const char *path_got, struct got_reference *head_ref)
128 const struct got_error *err = NULL;
129 char *refstr = NULL;
131 if (got_ref_is_symbolic(head_ref)) {
132 refstr = got_ref_to_str(head_ref);
133 if (refstr == NULL)
134 return got_error_from_errno("got_ref_to_str");
135 } else {
136 refstr = strdup(got_ref_get_name(head_ref));
137 if (refstr == NULL)
138 return got_error_from_errno("strdup");
140 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
141 free(refstr);
142 return err;
145 const struct got_error *
146 got_worktree_init(const char *path, struct got_reference *head_ref,
147 const char *prefix, struct got_repository *repo)
149 const struct got_error *err = NULL;
150 struct got_object_id *commit_id = NULL;
151 uuid_t uuid;
152 uint32_t uuid_status;
153 int obj_type;
154 char *path_got = NULL;
155 char *formatstr = NULL;
156 char *absprefix = NULL;
157 char *basestr = NULL;
158 char *uuidstr = NULL;
160 if (strcmp(path, got_repo_get_path(repo)) == 0) {
161 err = got_error(GOT_ERR_WORKTREE_REPO);
162 goto done;
165 err = got_ref_resolve(&commit_id, repo, head_ref);
166 if (err)
167 return err;
168 err = got_object_get_type(&obj_type, repo, commit_id);
169 if (err)
170 return err;
171 if (obj_type != GOT_OBJ_TYPE_COMMIT)
172 return got_error(GOT_ERR_OBJ_TYPE);
174 if (!got_path_is_absolute(prefix)) {
175 if (asprintf(&absprefix, "/%s", prefix) == -1)
176 return got_error_from_errno("asprintf");
179 /* Create top-level directory (may already exist). */
180 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
181 err = got_error_from_errno2("mkdir", path);
182 goto done;
185 /* Create .got directory (may already exist). */
186 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
187 err = got_error_from_errno("asprintf");
188 goto done;
190 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
191 err = got_error_from_errno2("mkdir", path_got);
192 goto done;
195 /* Create an empty lock file. */
196 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
197 if (err)
198 goto done;
200 /* Create an empty file index. */
201 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
202 if (err)
203 goto done;
205 /* Write the HEAD reference. */
206 err = write_head_ref(path_got, head_ref);
207 if (err)
208 goto done;
210 /* Record our base commit. */
211 err = got_object_id_str(&basestr, commit_id);
212 if (err)
213 goto done;
214 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
215 if (err)
216 goto done;
218 /* Store path to repository. */
219 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
220 got_repo_get_path(repo));
221 if (err)
222 goto done;
224 /* Store in-repository path prefix. */
225 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
226 absprefix ? absprefix : prefix);
227 if (err)
228 goto done;
230 /* Generate UUID. */
231 uuid_create(&uuid, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_create");
234 goto done;
236 uuid_to_string(&uuid, &uuidstr, &uuid_status);
237 if (uuid_status != uuid_s_ok) {
238 err = got_error_uuid(uuid_status, "uuid_to_string");
239 goto done;
241 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
242 if (err)
243 goto done;
245 /* Stamp work tree with format file. */
246 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
247 err = got_error_from_errno("asprintf");
248 goto done;
250 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
251 if (err)
252 goto done;
254 done:
255 free(commit_id);
256 free(path_got);
257 free(formatstr);
258 free(absprefix);
259 free(basestr);
260 free(uuidstr);
261 return err;
264 const struct got_error *
265 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
266 const char *path_prefix)
268 char *absprefix = NULL;
270 if (!got_path_is_absolute(path_prefix)) {
271 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
272 return got_error_from_errno("asprintf");
274 *match = (strcmp(absprefix ? absprefix : path_prefix,
275 worktree->path_prefix) == 0);
276 free(absprefix);
277 return NULL;
280 const char *
281 got_worktree_get_head_ref_name(struct got_worktree *worktree)
283 return worktree->head_ref_name;
286 const struct got_error *
287 got_worktree_set_head_ref(struct got_worktree *worktree,
288 struct got_reference *head_ref)
290 const struct got_error *err = NULL;
291 char *path_got = NULL, *head_ref_name = NULL;
293 if (asprintf(&path_got, "%s/%s", worktree->root_path,
294 GOT_WORKTREE_GOT_DIR) == -1) {
295 err = got_error_from_errno("asprintf");
296 path_got = NULL;
297 goto done;
300 head_ref_name = strdup(got_ref_get_name(head_ref));
301 if (head_ref_name == NULL) {
302 err = got_error_from_errno("strdup");
303 goto done;
306 err = write_head_ref(path_got, head_ref);
307 if (err)
308 goto done;
310 free(worktree->head_ref_name);
311 worktree->head_ref_name = head_ref_name;
312 done:
313 free(path_got);
314 if (err)
315 free(head_ref_name);
316 return err;
319 struct got_object_id *
320 got_worktree_get_base_commit_id(struct got_worktree *worktree)
322 return worktree->base_commit_id;
325 const struct got_error *
326 got_worktree_set_base_commit_id(struct got_worktree *worktree,
327 struct got_repository *repo, struct got_object_id *commit_id)
329 const struct got_error *err;
330 struct got_object *obj = NULL;
331 char *id_str = NULL;
332 char *path_got = NULL;
334 if (asprintf(&path_got, "%s/%s", worktree->root_path,
335 GOT_WORKTREE_GOT_DIR) == -1) {
336 err = got_error_from_errno("asprintf");
337 path_got = NULL;
338 goto done;
341 err = got_object_open(&obj, repo, commit_id);
342 if (err)
343 return err;
345 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
346 err = got_error(GOT_ERR_OBJ_TYPE);
347 goto done;
350 /* Record our base commit. */
351 err = got_object_id_str(&id_str, commit_id);
352 if (err)
353 goto done;
354 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
355 if (err)
356 goto done;
358 free(worktree->base_commit_id);
359 worktree->base_commit_id = got_object_id_dup(commit_id);
360 if (worktree->base_commit_id == NULL) {
361 err = got_error_from_errno("got_object_id_dup");
362 goto done;
364 done:
365 if (obj)
366 got_object_close(obj);
367 free(id_str);
368 free(path_got);
369 return err;
372 const struct got_gotconfig *
373 got_worktree_get_gotconfig(struct got_worktree *worktree)
375 return worktree->gotconfig;
378 static const struct got_error *
379 lock_worktree(struct got_worktree *worktree, int operation)
381 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
382 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
383 : got_error_from_errno2("flock",
384 got_worktree_get_root_path(worktree)));
385 return NULL;
388 static const struct got_error *
389 add_dir_on_disk(struct got_worktree *worktree, const char *path)
391 const struct got_error *err = NULL;
392 char *abspath;
394 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
395 return got_error_from_errno("asprintf");
397 err = got_path_mkdir(abspath);
398 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
399 struct stat sb;
400 err = NULL;
401 if (lstat(abspath, &sb) == -1) {
402 err = got_error_from_errno2("lstat", abspath);
403 } else if (!S_ISDIR(sb.st_mode)) {
404 /* TODO directory is obstructed; do something */
405 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
408 free(abspath);
409 return err;
412 static const struct got_error *
413 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
415 const struct got_error *err = NULL;
416 uint8_t fbuf1[8192];
417 uint8_t fbuf2[8192];
418 size_t flen1 = 0, flen2 = 0;
420 *same = 1;
422 for (;;) {
423 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
424 if (flen1 == 0 && ferror(f1)) {
425 err = got_error_from_errno("fread");
426 break;
428 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
429 if (flen2 == 0 && ferror(f2)) {
430 err = got_error_from_errno("fread");
431 break;
433 if (flen1 == 0) {
434 if (flen2 != 0)
435 *same = 0;
436 break;
437 } else if (flen2 == 0) {
438 if (flen1 != 0)
439 *same = 0;
440 break;
441 } else if (flen1 == flen2) {
442 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
443 *same = 0;
444 break;
446 } else {
447 *same = 0;
448 break;
452 return err;
455 static const struct got_error *
456 check_files_equal(int *same, FILE *f1, FILE *f2)
458 struct stat sb;
459 size_t size1, size2;
461 *same = 1;
463 if (fstat(fileno(f1), &sb) != 0)
464 return got_error_from_errno("fstat");
465 size1 = sb.st_size;
467 if (fstat(fileno(f2), &sb) != 0)
468 return got_error_from_errno("fstat");
469 size2 = sb.st_size;
471 if (size1 != size2) {
472 *same = 0;
473 return NULL;
476 if (fseek(f1, 0L, SEEK_SET) == -1)
477 return got_ferror(f1, GOT_ERR_IO);
478 if (fseek(f2, 0L, SEEK_SET) == -1)
479 return got_ferror(f2, GOT_ERR_IO);
481 return check_file_contents_equal(same, f1, f2);
484 static const struct got_error *
485 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
487 uint8_t fbuf[65536];
488 size_t flen;
489 ssize_t outlen;
491 *outsize = 0;
493 if (fseek(f, 0L, SEEK_SET) == -1)
494 return got_ferror(f, GOT_ERR_IO);
496 for (;;) {
497 flen = fread(fbuf, 1, sizeof(fbuf), f);
498 if (flen == 0) {
499 if (ferror(f))
500 return got_error_from_errno("fread");
501 if (feof(f))
502 break;
504 outlen = write(outfd, fbuf, flen);
505 if (outlen == -1)
506 return got_error_from_errno("write");
507 if (outlen != flen)
508 return got_error(GOT_ERR_IO);
509 *outsize += outlen;
512 return NULL;
515 static const struct got_error *
516 merge_binary_file(int *overlapcnt, int merged_fd,
517 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
518 const char *label_deriv, const char *label_orig, const char *label_deriv2,
519 const char *ondisk_path)
521 const struct got_error *err = NULL;
522 int same_content, changed_deriv, changed_deriv2;
523 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
524 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
525 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
526 char *base_path_orig = NULL, *base_path_deriv = NULL;
527 char *base_path_deriv2 = NULL;
529 *overlapcnt = 0;
531 err = check_files_equal(&same_content, f_deriv, f_deriv2);
532 if (err)
533 return err;
535 if (same_content)
536 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
538 err = check_files_equal(&same_content, f_deriv, f_orig);
539 if (err)
540 return err;
541 changed_deriv = !same_content;
542 err = check_files_equal(&same_content, f_deriv2, f_orig);
543 if (err)
544 return err;
545 changed_deriv2 = !same_content;
547 if (changed_deriv && changed_deriv2) {
548 *overlapcnt = 1;
549 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
551 goto done;
553 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
554 err = got_error_from_errno("asprintf");
555 goto done;
557 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
558 err = got_error_from_errno("asprintf");
559 goto done;
561 err = got_opentemp_named_fd(&path_orig, &fd_orig,
562 base_path_orig, "");
563 if (err)
564 goto done;
565 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
566 base_path_deriv, "");
567 if (err)
568 goto done;
569 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
570 base_path_deriv2, "");
571 if (err)
572 goto done;
573 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
574 if (err)
575 goto done;
576 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
577 if (err)
578 goto done;
579 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
580 if (err)
581 goto done;
582 if (dprintf(merged_fd, "Binary files differ and cannot be "
583 "merged automatically:\n") < 0) {
584 err = got_error_from_errno("dprintf");
585 goto done;
587 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
588 GOT_DIFF_CONFLICT_MARKER_BEGIN,
589 label_deriv ? " " : "",
590 label_deriv ? label_deriv : "",
591 path_deriv) < 0) {
592 err = got_error_from_errno("dprintf");
593 goto done;
595 if (size_orig > 0) {
596 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
597 GOT_DIFF_CONFLICT_MARKER_ORIG,
598 label_orig ? " " : "",
599 label_orig ? label_orig : "",
600 path_orig) < 0) {
601 err = got_error_from_errno("dprintf");
602 goto done;
605 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
606 GOT_DIFF_CONFLICT_MARKER_SEP,
607 path_deriv2,
608 GOT_DIFF_CONFLICT_MARKER_END,
609 label_deriv2 ? " " : "",
610 label_deriv2 ? label_deriv2 : "") < 0) {
611 err = got_error_from_errno("dprintf");
612 goto done;
614 } else if (changed_deriv)
615 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
616 else if (changed_deriv2)
617 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
618 done:
619 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
620 err == NULL)
621 err = got_error_from_errno2("unlink", path_orig);
622 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_orig);
624 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
625 err = got_error_from_errno2("close", path_deriv);
626 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
627 err = got_error_from_errno2("close", path_deriv2);
628 free(path_orig);
629 free(path_deriv);
630 free(path_deriv2);
631 free(base_path_orig);
632 free(base_path_deriv);
633 free(base_path_deriv2);
634 return err;
637 /*
638 * Perform a 3-way merge where the file f_orig acts as the common
639 * ancestor, the file f_deriv acts as the first derived version,
640 * and the file f_deriv2 acts as the second derived version.
641 * The merge result will be written to a new file at ondisk_path; any
642 * existing file at this path will be replaced.
643 */
644 static const struct got_error *
645 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
646 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
647 const char *path, uint16_t st_mode,
648 const char *label_orig, const char *label_deriv, const char *label_deriv2,
649 enum got_diff_algorithm diff_algo, struct got_repository *repo,
650 got_worktree_checkout_cb progress_cb, void *progress_arg)
652 const struct got_error *err = NULL;
653 int merged_fd = -1;
654 FILE *f_merged = NULL;
655 char *merged_path = NULL, *base_path = NULL;
656 int overlapcnt = 0;
657 char *parent = NULL;
659 *local_changes_subsumed = 0;
661 err = got_path_dirname(&parent, ondisk_path);
662 if (err)
663 return err;
665 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
666 err = got_error_from_errno("asprintf");
667 goto done;
670 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
671 if (err)
672 goto done;
674 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
675 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
676 if (err) {
677 if (err->code != GOT_ERR_FILE_BINARY)
678 goto done;
679 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
680 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
681 ondisk_path);
682 if (err)
683 goto done;
686 err = (*progress_cb)(progress_arg,
687 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
688 if (err)
689 goto done;
691 if (fsync(merged_fd) != 0) {
692 err = got_error_from_errno("fsync");
693 goto done;
696 f_merged = fdopen(merged_fd, "r");
697 if (f_merged == NULL) {
698 err = got_error_from_errno("fdopen");
699 goto done;
701 merged_fd = -1;
703 /* Check if a clean merge has subsumed all local changes. */
704 if (overlapcnt == 0) {
705 err = check_files_equal(local_changes_subsumed, f_deriv,
706 f_merged);
707 if (err)
708 goto done;
711 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
712 err = got_error_from_errno2("fchmod", merged_path);
713 goto done;
716 if (rename(merged_path, ondisk_path) != 0) {
717 err = got_error_from_errno3("rename", merged_path,
718 ondisk_path);
719 goto done;
721 done:
722 if (err) {
723 if (merged_path)
724 unlink(merged_path);
726 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
727 err = got_error_from_errno("close");
728 if (f_merged && fclose(f_merged) == EOF && err == NULL)
729 err = got_error_from_errno("fclose");
730 free(merged_path);
731 free(base_path);
732 free(parent);
733 return err;
736 static const struct got_error *
737 update_symlink(const char *ondisk_path, const char *target_path,
738 size_t target_len)
740 /* This is not atomic but matches what 'ln -sf' does. */
741 if (unlink(ondisk_path) == -1)
742 return got_error_from_errno2("unlink", ondisk_path);
743 if (symlink(target_path, ondisk_path) == -1)
744 return got_error_from_errno3("symlink", target_path,
745 ondisk_path);
746 return NULL;
749 /*
750 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
751 * in the work tree with a file that contains conflict markers and the
752 * conflicting target paths of the original version, a "derived version"
753 * of a symlink from an incoming change, and a local version of the symlink.
755 * The original versions's target path can be NULL if it is not available,
756 * such as if both derived versions added a new symlink at the same path.
758 * The incoming derived symlink target is NULL in case the incoming change
759 * has deleted this symlink.
760 */
761 static const struct got_error *
762 install_symlink_conflict(const char *deriv_target,
763 struct got_object_id *deriv_base_commit_id, const char *orig_target,
764 const char *label_orig, const char *local_target, const char *ondisk_path)
766 const struct got_error *err;
767 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
768 FILE *f = NULL;
770 err = got_object_id_str(&id_str, deriv_base_commit_id);
771 if (err)
772 return got_error_from_errno("asprintf");
774 if (asprintf(&label_deriv, "%s: commit %s",
775 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
776 err = got_error_from_errno("asprintf");
777 goto done;
780 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
781 if (err)
782 goto done;
784 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
785 err = got_error_from_errno2("fchmod", path);
786 goto done;
789 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
790 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
791 deriv_target ? deriv_target : "(symlink was deleted)",
792 orig_target ? label_orig : "",
793 orig_target ? "\n" : "",
794 orig_target ? orig_target : "",
795 orig_target ? "\n" : "",
796 GOT_DIFF_CONFLICT_MARKER_SEP,
797 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
798 err = got_error_from_errno2("fprintf", path);
799 goto done;
802 if (unlink(ondisk_path) == -1) {
803 err = got_error_from_errno2("unlink", ondisk_path);
804 goto done;
806 if (rename(path, ondisk_path) == -1) {
807 err = got_error_from_errno3("rename", path, ondisk_path);
808 goto done;
810 done:
811 if (f != NULL && fclose(f) == EOF && err == NULL)
812 err = got_error_from_errno2("fclose", path);
813 free(path);
814 free(id_str);
815 free(label_deriv);
816 return err;
819 /* forward declaration */
820 static const struct got_error *
821 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
822 const char *, const char *, uint16_t, const char *,
823 struct got_blob_object *, struct got_object_id *,
824 struct got_repository *, got_worktree_checkout_cb, void *);
826 /*
827 * Merge a symlink into the work tree, where blob_orig acts as the common
828 * ancestor, deriv_target is the link target of the first derived version,
829 * and the symlink on disk acts as the second derived version.
830 * Assume that contents of both blobs represent symlinks.
831 */
832 static const struct got_error *
833 merge_symlink(struct got_worktree *worktree,
834 struct got_blob_object *blob_orig, const char *ondisk_path,
835 const char *path, const char *label_orig, const char *deriv_target,
836 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
837 got_worktree_checkout_cb progress_cb, void *progress_arg)
839 const struct got_error *err = NULL;
840 char *ancestor_target = NULL;
841 struct stat sb;
842 ssize_t ondisk_len, deriv_len;
843 char ondisk_target[PATH_MAX];
844 int have_local_change = 0;
845 int have_incoming_change = 0;
847 if (lstat(ondisk_path, &sb) == -1)
848 return got_error_from_errno2("lstat", ondisk_path);
850 ondisk_len = readlink(ondisk_path, ondisk_target,
851 sizeof(ondisk_target));
852 if (ondisk_len == -1) {
853 err = got_error_from_errno2("readlink",
854 ondisk_path);
855 goto done;
857 ondisk_target[ondisk_len] = '\0';
859 if (blob_orig) {
860 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
861 if (err)
862 goto done;
865 if (ancestor_target == NULL ||
866 (ondisk_len != strlen(ancestor_target) ||
867 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
868 have_local_change = 1;
870 deriv_len = strlen(deriv_target);
871 if (ancestor_target == NULL ||
872 (deriv_len != strlen(ancestor_target) ||
873 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
874 have_incoming_change = 1;
876 if (!have_local_change && !have_incoming_change) {
877 if (ancestor_target) {
878 /* Both sides made the same change. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 path);
881 } else if (deriv_len == ondisk_len &&
882 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
883 /* Both sides added the same symlink. */
884 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
885 path);
886 } else {
887 /* Both sides added symlinks which don't match. */
888 err = install_symlink_conflict(deriv_target,
889 deriv_base_commit_id, ancestor_target,
890 label_orig, ondisk_target, ondisk_path);
891 if (err)
892 goto done;
893 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
894 path);
896 } else if (!have_local_change && have_incoming_change) {
897 /* Apply the incoming change. */
898 err = update_symlink(ondisk_path, deriv_target,
899 strlen(deriv_target));
900 if (err)
901 goto done;
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
903 } else if (have_local_change && have_incoming_change) {
904 if (deriv_len == ondisk_len &&
905 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
906 /* Both sides made the same change. */
907 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
908 path);
909 } else {
910 err = install_symlink_conflict(deriv_target,
911 deriv_base_commit_id, ancestor_target, label_orig,
912 ondisk_target, ondisk_path);
913 if (err)
914 goto done;
915 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
916 path);
920 done:
921 free(ancestor_target);
922 return err;
925 static const struct got_error *
926 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
928 const struct got_error *err = NULL;
929 char target_path[PATH_MAX];
930 ssize_t target_len;
931 size_t n;
932 FILE *f;
934 *outfile = NULL;
936 f = got_opentemp();
937 if (f == NULL)
938 return got_error_from_errno("got_opentemp");
939 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
940 if (target_len == -1) {
941 err = got_error_from_errno2("readlink", ondisk_path);
942 goto done;
944 n = fwrite(target_path, 1, target_len, f);
945 if (n != target_len) {
946 err = got_ferror(f, GOT_ERR_IO);
947 goto done;
949 if (fflush(f) == EOF) {
950 err = got_error_from_errno("fflush");
951 goto done;
953 if (fseek(f, 0L, SEEK_SET) == -1) {
954 err = got_ferror(f, GOT_ERR_IO);
955 goto done;
957 done:
958 if (err)
959 fclose(f);
960 else
961 *outfile = f;
962 return err;
965 /*
966 * Perform a 3-way merge where blob_orig acts as the common ancestor,
967 * blob_deriv acts as the first derived version, and the file on disk
968 * acts as the second derived version.
969 */
970 static const struct got_error *
971 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
972 struct got_blob_object *blob_orig, const char *ondisk_path,
973 const char *path, uint16_t st_mode, const char *label_orig,
974 struct got_blob_object *blob_deriv,
975 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
976 got_worktree_checkout_cb progress_cb, void *progress_arg)
978 const struct got_error *err = NULL;
979 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
980 char *blob_orig_path = NULL;
981 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
982 char *label_deriv = NULL, *parent = NULL;
984 *local_changes_subsumed = 0;
986 err = got_path_dirname(&parent, ondisk_path);
987 if (err)
988 return err;
990 if (blob_orig) {
991 if (asprintf(&base_path, "%s/got-merge-blob-orig",
992 parent) == -1) {
993 err = got_error_from_errno("asprintf");
994 base_path = NULL;
995 goto done;
998 err = got_opentemp_named(&blob_orig_path, &f_orig,
999 base_path, "");
1000 if (err)
1001 goto done;
1002 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1003 blob_orig);
1004 if (err)
1005 goto done;
1006 free(base_path);
1007 } else {
1009 * No common ancestor exists. This is an "add vs add" conflict
1010 * and we simply use an empty ancestor file to make both files
1011 * appear in the merged result in their entirety.
1013 f_orig = got_opentemp();
1014 if (f_orig == NULL) {
1015 err = got_error_from_errno("got_opentemp");
1016 goto done;
1020 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1021 err = got_error_from_errno("asprintf");
1022 base_path = NULL;
1023 goto done;
1026 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1027 if (err)
1028 goto done;
1029 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1030 blob_deriv);
1031 if (err)
1032 goto done;
1034 err = got_object_id_str(&id_str, deriv_base_commit_id);
1035 if (err)
1036 goto done;
1037 if (asprintf(&label_deriv, "%s: commit %s",
1038 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1039 err = got_error_from_errno("asprintf");
1040 goto done;
1044 * In order the run a 3-way merge with a symlink we copy the symlink's
1045 * target path into a temporary file and use that file with diff3.
1047 if (S_ISLNK(st_mode)) {
1048 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1049 if (err)
1050 goto done;
1051 } else {
1052 int fd;
1053 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1054 if (fd == -1) {
1055 err = got_error_from_errno2("open", ondisk_path);
1056 goto done;
1058 f_deriv2 = fdopen(fd, "r");
1059 if (f_deriv2 == NULL) {
1060 err = got_error_from_errno2("fdopen", ondisk_path);
1061 close(fd);
1062 goto done;
1066 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1067 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1068 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1069 done:
1070 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1073 err = got_error_from_errno("fclose");
1074 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1075 err = got_error_from_errno("fclose");
1076 free(base_path);
1077 if (blob_orig_path) {
1078 unlink(blob_orig_path);
1079 free(blob_orig_path);
1081 if (blob_deriv_path) {
1082 unlink(blob_deriv_path);
1083 free(blob_deriv_path);
1085 free(id_str);
1086 free(label_deriv);
1087 free(parent);
1088 return err;
1091 static const struct got_error *
1092 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1093 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1094 int wt_fd, const char *path, struct got_object_id *blob_id)
1096 const struct got_error *err = NULL;
1097 struct got_fileindex_entry *new_ie;
1099 *new_iep = NULL;
1101 err = got_fileindex_entry_alloc(&new_ie, path);
1102 if (err)
1103 return err;
1105 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1106 blob_id->hash, base_commit_id->hash, 1);
1107 if (err)
1108 goto done;
1110 err = got_fileindex_entry_add(fileindex, new_ie);
1111 done:
1112 if (err)
1113 got_fileindex_entry_free(new_ie);
1114 else
1115 *new_iep = new_ie;
1116 return err;
1119 static mode_t
1120 get_ondisk_perms(int executable, mode_t st_mode)
1122 mode_t xbits = S_IXUSR;
1124 if (executable) {
1125 /* Map read bits to execute bits. */
1126 if (st_mode & S_IRGRP)
1127 xbits |= S_IXGRP;
1128 if (st_mode & S_IROTH)
1129 xbits |= S_IXOTH;
1130 return st_mode | xbits;
1133 return st_mode;
1136 static mode_t
1137 apply_umask(mode_t mode)
1139 mode_t um;
1141 um = umask(000);
1142 umask(um);
1143 return mode & ~um;
1146 /* forward declaration */
1147 static const struct got_error *
1148 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1149 const char *path, mode_t te_mode, mode_t st_mode,
1150 struct got_blob_object *blob, int restoring_missing_file,
1151 int reverting_versioned_file, int installing_bad_symlink,
1152 int path_is_unversioned, struct got_repository *repo,
1153 got_worktree_checkout_cb progress_cb, void *progress_arg);
1156 * This function assumes that the provided symlink target points at a
1157 * safe location in the work tree!
1159 static const struct got_error *
1160 replace_existing_symlink(int *did_something, const char *ondisk_path,
1161 const char *target_path, size_t target_len)
1163 const struct got_error *err = NULL;
1164 ssize_t elen;
1165 char etarget[PATH_MAX];
1166 int fd;
1168 *did_something = 0;
1171 * "Bad" symlinks (those pointing outside the work tree or into the
1172 * .got directory) are installed in the work tree as a regular file
1173 * which contains the bad symlink target path.
1174 * The new symlink target has already been checked for safety by our
1175 * caller. If we can successfully open a regular file then we simply
1176 * replace this file with a symlink below.
1178 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1179 if (fd == -1) {
1180 if (!got_err_open_nofollow_on_symlink())
1181 return got_error_from_errno2("open", ondisk_path);
1183 /* We are updating an existing on-disk symlink. */
1184 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 if (elen == -1)
1186 return got_error_from_errno2("readlink", ondisk_path);
1188 if (elen == target_len &&
1189 memcmp(etarget, target_path, target_len) == 0)
1190 return NULL; /* nothing to do */
1193 *did_something = 1;
1194 err = update_symlink(ondisk_path, target_path, target_len);
1195 if (fd != -1 && close(fd) == -1 && err == NULL)
1196 err = got_error_from_errno2("close", ondisk_path);
1197 return err;
1200 static const struct got_error *
1201 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1202 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1204 const struct got_error *err = NULL;
1205 char canonpath[PATH_MAX];
1206 char *path_got = NULL;
1208 *is_bad_symlink = 0;
1210 if (target_len >= sizeof(canonpath)) {
1211 *is_bad_symlink = 1;
1212 return NULL;
1216 * We do not use realpath(3) to resolve the symlink's target
1217 * path because we don't want to resolve symlinks recursively.
1218 * Instead we make the path absolute and then canonicalize it.
1219 * Relative symlink target lookup should begin at the directory
1220 * in which the blob object is being installed.
1222 if (!got_path_is_absolute(target_path)) {
1223 char *abspath, *parent;
1224 err = got_path_dirname(&parent, ondisk_path);
1225 if (err)
1226 return err;
1227 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1228 free(parent);
1229 return got_error_from_errno("asprintf");
1231 free(parent);
1232 if (strlen(abspath) >= sizeof(canonpath)) {
1233 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1234 free(abspath);
1235 return err;
1237 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1238 free(abspath);
1239 if (err)
1240 return err;
1241 } else {
1242 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1243 if (err)
1244 return err;
1247 /* Only allow symlinks pointing at paths within the work tree. */
1248 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1249 *is_bad_symlink = 1;
1250 return NULL;
1253 /* Do not allow symlinks pointing into the .got directory. */
1254 if (asprintf(&path_got, "%s/%s", wtroot_path,
1255 GOT_WORKTREE_GOT_DIR) == -1)
1256 return got_error_from_errno("asprintf");
1257 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1258 *is_bad_symlink = 1;
1260 free(path_got);
1261 return NULL;
1264 static const struct got_error *
1265 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1266 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1267 int restoring_missing_file, int reverting_versioned_file,
1268 int path_is_unversioned, int allow_bad_symlinks,
1269 struct got_repository *repo,
1270 got_worktree_checkout_cb progress_cb, void *progress_arg)
1272 const struct got_error *err = NULL;
1273 char target_path[PATH_MAX];
1274 size_t len, target_len = 0;
1275 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1276 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1278 *is_bad_symlink = 0;
1281 * Blob object content specifies the target path of the link.
1282 * If a symbolic link cannot be installed we instead create
1283 * a regular file which contains the link target path stored
1284 * in the blob object.
1286 do {
1287 err = got_object_blob_read_block(&len, blob);
1288 if (err)
1289 return err;
1291 if (len + target_len >= sizeof(target_path)) {
1292 /* Path too long; install as a regular file. */
1293 *is_bad_symlink = 1;
1294 got_object_blob_rewind(blob);
1295 return install_blob(worktree, ondisk_path, path,
1296 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1297 restoring_missing_file, reverting_versioned_file,
1298 1, path_is_unversioned, repo, progress_cb,
1299 progress_arg);
1301 if (len > 0) {
1302 /* Skip blob object header first time around. */
1303 memcpy(target_path + target_len, buf + hdrlen,
1304 len - hdrlen);
1305 target_len += len - hdrlen;
1306 hdrlen = 0;
1308 } while (len != 0);
1309 target_path[target_len] = '\0';
1311 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1312 ondisk_path, worktree->root_path);
1313 if (err)
1314 return err;
1316 if (*is_bad_symlink && !allow_bad_symlinks) {
1317 /* install as a regular file */
1318 got_object_blob_rewind(blob);
1319 err = install_blob(worktree, ondisk_path, path,
1320 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1321 restoring_missing_file, reverting_versioned_file, 1,
1322 path_is_unversioned, repo, progress_cb, progress_arg);
1323 return err;
1326 if (symlink(target_path, ondisk_path) == -1) {
1327 if (errno == EEXIST) {
1328 int symlink_replaced;
1329 if (path_is_unversioned) {
1330 err = (*progress_cb)(progress_arg,
1331 GOT_STATUS_UNVERSIONED, path);
1332 return err;
1334 err = replace_existing_symlink(&symlink_replaced,
1335 ondisk_path, target_path, target_len);
1336 if (err)
1337 return err;
1338 if (progress_cb) {
1339 if (symlink_replaced) {
1340 err = (*progress_cb)(progress_arg,
1341 reverting_versioned_file ?
1342 GOT_STATUS_REVERT :
1343 GOT_STATUS_UPDATE, path);
1344 } else {
1345 err = (*progress_cb)(progress_arg,
1346 GOT_STATUS_EXISTS, path);
1349 return err; /* Nothing else to do. */
1352 if (errno == ENOENT) {
1353 char *parent;
1354 err = got_path_dirname(&parent, ondisk_path);
1355 if (err)
1356 return err;
1357 err = add_dir_on_disk(worktree, parent);
1358 free(parent);
1359 if (err)
1360 return err;
1362 * Retry, and fall through to error handling
1363 * below if this second attempt fails.
1365 if (symlink(target_path, ondisk_path) != -1) {
1366 err = NULL; /* success */
1367 return err;
1371 /* Handle errors from first or second creation attempt. */
1372 if (errno == ENAMETOOLONG) {
1373 /* bad target path; install as a regular file */
1374 *is_bad_symlink = 1;
1375 got_object_blob_rewind(blob);
1376 err = install_blob(worktree, ondisk_path, path,
1377 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1378 restoring_missing_file, reverting_versioned_file, 1,
1379 path_is_unversioned, repo,
1380 progress_cb, progress_arg);
1381 } else if (errno == ENOTDIR) {
1382 err = got_error_path(ondisk_path,
1383 GOT_ERR_FILE_OBSTRUCTED);
1384 } else {
1385 err = got_error_from_errno3("symlink",
1386 target_path, ondisk_path);
1388 } else if (progress_cb)
1389 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1390 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1391 return err;
1394 static const struct got_error *
1395 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1396 const char *path, mode_t te_mode, mode_t st_mode,
1397 struct got_blob_object *blob, int restoring_missing_file,
1398 int reverting_versioned_file, int installing_bad_symlink,
1399 int path_is_unversioned, struct got_repository *repo,
1400 got_worktree_checkout_cb progress_cb, void *progress_arg)
1402 const struct got_error *err = NULL;
1403 int fd = -1;
1404 size_t len, hdrlen;
1405 int update = 0;
1406 char *tmppath = NULL;
1407 mode_t mode;
1409 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1410 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1411 O_CLOEXEC, mode);
1412 if (fd == -1) {
1413 if (errno == ENOENT) {
1414 char *parent;
1415 err = got_path_dirname(&parent, path);
1416 if (err)
1417 return err;
1418 err = add_dir_on_disk(worktree, parent);
1419 free(parent);
1420 if (err)
1421 return err;
1422 fd = open(ondisk_path,
1423 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1424 mode);
1425 if (fd == -1)
1426 return got_error_from_errno2("open",
1427 ondisk_path);
1428 } else if (errno == EEXIST) {
1429 if (path_is_unversioned) {
1430 err = (*progress_cb)(progress_arg,
1431 GOT_STATUS_UNVERSIONED, path);
1432 goto done;
1434 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1435 !S_ISREG(st_mode) && !installing_bad_symlink) {
1436 /* TODO file is obstructed; do something */
1437 err = got_error_path(ondisk_path,
1438 GOT_ERR_FILE_OBSTRUCTED);
1439 goto done;
1440 } else {
1441 err = got_opentemp_named_fd(&tmppath, &fd,
1442 ondisk_path, "");
1443 if (err)
1444 goto done;
1445 update = 1;
1447 if (fchmod(fd, apply_umask(mode)) == -1) {
1448 err = got_error_from_errno2("fchmod",
1449 tmppath);
1450 goto done;
1453 } else
1454 return got_error_from_errno2("open", ondisk_path);
1457 if (progress_cb) {
1458 if (restoring_missing_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1460 path);
1461 else if (reverting_versioned_file)
1462 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1463 path);
1464 else
1465 err = (*progress_cb)(progress_arg,
1466 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1467 if (err)
1468 goto done;
1471 hdrlen = got_object_blob_get_hdrlen(blob);
1472 do {
1473 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1474 err = got_object_blob_read_block(&len, blob);
1475 if (err)
1476 break;
1477 if (len > 0) {
1478 /* Skip blob object header first time around. */
1479 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1480 if (outlen == -1) {
1481 err = got_error_from_errno("write");
1482 goto done;
1483 } else if (outlen != len - hdrlen) {
1484 err = got_error(GOT_ERR_IO);
1485 goto done;
1487 hdrlen = 0;
1489 } while (len != 0);
1491 if (fsync(fd) != 0) {
1492 err = got_error_from_errno("fsync");
1493 goto done;
1496 if (update) {
1497 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1498 err = got_error_from_errno2("unlink", ondisk_path);
1499 goto done;
1501 if (rename(tmppath, ondisk_path) != 0) {
1502 err = got_error_from_errno3("rename", tmppath,
1503 ondisk_path);
1504 goto done;
1506 free(tmppath);
1507 tmppath = NULL;
1510 done:
1511 if (fd != -1 && close(fd) == -1 && err == NULL)
1512 err = got_error_from_errno("close");
1513 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1514 err = got_error_from_errno2("unlink", tmppath);
1515 free(tmppath);
1516 return err;
1519 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1520 static const struct got_error *
1521 get_modified_file_content_status(unsigned char *status, FILE *f)
1523 const struct got_error *err = NULL;
1524 const char *markers[3] = {
1525 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 GOT_DIFF_CONFLICT_MARKER_SEP,
1527 GOT_DIFF_CONFLICT_MARKER_END
1529 int i = 0;
1530 char *line = NULL;
1531 size_t linesize = 0;
1532 ssize_t linelen;
1534 while (*status == GOT_STATUS_MODIFY) {
1535 linelen = getline(&line, &linesize, f);
1536 if (linelen == -1) {
1537 if (feof(f))
1538 break;
1539 err = got_ferror(f, GOT_ERR_IO);
1540 break;
1543 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1544 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1545 == 0)
1546 *status = GOT_STATUS_CONFLICT;
1547 else
1548 i++;
1551 free(line);
1553 return err;
1556 static int
1557 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1559 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1560 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1563 static int
1564 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1566 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1567 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1568 ie->mtime_sec == sb->st_mtim.tv_sec &&
1569 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1570 ie->size == (sb->st_size & 0xffffffff) &&
1571 !xbit_differs(ie, sb->st_mode));
1574 static unsigned char
1575 get_staged_status(struct got_fileindex_entry *ie)
1577 switch (got_fileindex_entry_stage_get(ie)) {
1578 case GOT_FILEIDX_STAGE_ADD:
1579 return GOT_STATUS_ADD;
1580 case GOT_FILEIDX_STAGE_DELETE:
1581 return GOT_STATUS_DELETE;
1582 case GOT_FILEIDX_STAGE_MODIFY:
1583 return GOT_STATUS_MODIFY;
1584 default:
1585 return GOT_STATUS_NO_CHANGE;
1589 static const struct got_error *
1590 get_symlink_modification_status(unsigned char *status,
1591 struct got_fileindex_entry *ie, const char *abspath,
1592 int dirfd, const char *de_name, struct got_blob_object *blob)
1594 const struct got_error *err = NULL;
1595 char target_path[PATH_MAX];
1596 char etarget[PATH_MAX];
1597 ssize_t elen;
1598 size_t len, target_len = 0;
1599 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1600 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1602 *status = GOT_STATUS_NO_CHANGE;
1604 /* Blob object content specifies the target path of the link. */
1605 do {
1606 err = got_object_blob_read_block(&len, blob);
1607 if (err)
1608 return err;
1609 if (len + target_len >= sizeof(target_path)) {
1611 * Should not happen. The blob contents were OK
1612 * when this symlink was installed.
1614 return got_error(GOT_ERR_NO_SPACE);
1616 if (len > 0) {
1617 /* Skip blob object header first time around. */
1618 memcpy(target_path + target_len, buf + hdrlen,
1619 len - hdrlen);
1620 target_len += len - hdrlen;
1621 hdrlen = 0;
1623 } while (len != 0);
1624 target_path[target_len] = '\0';
1626 if (dirfd != -1) {
1627 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1628 if (elen == -1)
1629 return got_error_from_errno2("readlinkat", abspath);
1630 } else {
1631 elen = readlink(abspath, etarget, sizeof(etarget));
1632 if (elen == -1)
1633 return got_error_from_errno2("readlink", abspath);
1636 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1637 *status = GOT_STATUS_MODIFY;
1639 return NULL;
1642 static const struct got_error *
1643 get_file_status(unsigned char *status, struct stat *sb,
1644 struct got_fileindex_entry *ie, const char *abspath,
1645 int dirfd, const char *de_name, struct got_repository *repo)
1647 const struct got_error *err = NULL;
1648 struct got_object_id id;
1649 size_t hdrlen;
1650 int fd = -1, fd1 = -1;
1651 FILE *f = NULL;
1652 uint8_t fbuf[8192];
1653 struct got_blob_object *blob = NULL;
1654 size_t flen, blen;
1655 unsigned char staged_status = get_staged_status(ie);
1657 *status = GOT_STATUS_NO_CHANGE;
1658 memset(sb, 0, sizeof(*sb));
1661 * Whenever the caller provides a directory descriptor and a
1662 * directory entry name for the file, use them! This prevents
1663 * race conditions if filesystem paths change beneath our feet.
1665 if (dirfd != -1) {
1666 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1667 if (errno == ENOENT) {
1668 if (got_fileindex_entry_has_file_on_disk(ie))
1669 *status = GOT_STATUS_MISSING;
1670 else
1671 *status = GOT_STATUS_DELETE;
1672 goto done;
1674 err = got_error_from_errno2("fstatat", abspath);
1675 goto done;
1677 } else {
1678 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1679 if (fd == -1 && errno != ENOENT &&
1680 !got_err_open_nofollow_on_symlink())
1681 return got_error_from_errno2("open", abspath);
1682 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1683 if (lstat(abspath, sb) == -1)
1684 return got_error_from_errno2("lstat", abspath);
1685 } else if (fd == -1 || fstat(fd, sb) == -1) {
1686 if (errno == ENOENT) {
1687 if (got_fileindex_entry_has_file_on_disk(ie))
1688 *status = GOT_STATUS_MISSING;
1689 else
1690 *status = GOT_STATUS_DELETE;
1691 goto done;
1693 err = got_error_from_errno2("fstat", abspath);
1694 goto done;
1698 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1699 *status = GOT_STATUS_OBSTRUCTED;
1700 goto done;
1703 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1704 *status = GOT_STATUS_DELETE;
1705 goto done;
1706 } else if (!got_fileindex_entry_has_blob(ie) &&
1707 staged_status != GOT_STATUS_ADD) {
1708 *status = GOT_STATUS_ADD;
1709 goto done;
1712 if (!stat_info_differs(ie, sb))
1713 goto done;
1715 if (S_ISLNK(sb->st_mode) &&
1716 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1717 *status = GOT_STATUS_MODIFY;
1718 goto done;
1721 if (staged_status == GOT_STATUS_MODIFY ||
1722 staged_status == GOT_STATUS_ADD)
1723 memcpy(id.hash, ie->staged_blob_hash, sizeof(id.hash));
1724 else
1725 memcpy(id.hash, ie->blob_hash, sizeof(id.hash));
1726 id.algo = got_repo_get_hash_algorithm(repo);
1728 fd1 = got_opentempfd();
1729 if (fd1 == -1) {
1730 err = got_error_from_errno("got_opentempfd");
1731 goto done;
1733 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1734 if (err)
1735 goto done;
1737 if (S_ISLNK(sb->st_mode)) {
1738 err = get_symlink_modification_status(status, ie,
1739 abspath, dirfd, de_name, blob);
1740 goto done;
1743 if (dirfd != -1) {
1744 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 if (fd == -1) {
1746 err = got_error_from_errno2("openat", abspath);
1747 goto done;
1751 f = fdopen(fd, "r");
1752 if (f == NULL) {
1753 err = got_error_from_errno2("fdopen", abspath);
1754 goto done;
1756 fd = -1;
1757 hdrlen = got_object_blob_get_hdrlen(blob);
1758 for (;;) {
1759 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1760 err = got_object_blob_read_block(&blen, blob);
1761 if (err)
1762 goto done;
1763 /* Skip length of blob object header first time around. */
1764 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1765 if (flen == 0 && ferror(f)) {
1766 err = got_error_from_errno("fread");
1767 goto done;
1769 if (blen - hdrlen == 0) {
1770 if (flen != 0)
1771 *status = GOT_STATUS_MODIFY;
1772 break;
1773 } else if (flen == 0) {
1774 if (blen - hdrlen != 0)
1775 *status = GOT_STATUS_MODIFY;
1776 break;
1777 } else if (blen - hdrlen == flen) {
1778 /* Skip blob object header first time around. */
1779 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1780 *status = GOT_STATUS_MODIFY;
1781 break;
1783 } else {
1784 *status = GOT_STATUS_MODIFY;
1785 break;
1787 hdrlen = 0;
1790 if (*status == GOT_STATUS_MODIFY) {
1791 rewind(f);
1792 err = get_modified_file_content_status(status, f);
1793 } else if (xbit_differs(ie, sb->st_mode))
1794 *status = GOT_STATUS_MODE_CHANGE;
1795 done:
1796 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1797 err = got_error_from_errno("close");
1798 if (blob)
1799 got_object_blob_close(blob);
1800 if (f != NULL && fclose(f) == EOF && err == NULL)
1801 err = got_error_from_errno2("fclose", abspath);
1802 if (fd != -1 && close(fd) == -1 && err == NULL)
1803 err = got_error_from_errno2("close", abspath);
1804 return err;
1808 * Update timestamps in the file index if a file is unmodified and
1809 * we had to run a full content comparison to find out.
1811 static const struct got_error *
1812 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1813 struct got_fileindex_entry *ie, struct stat *sb)
1815 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1816 return got_fileindex_entry_update(ie, wt_fd, path,
1817 ie->blob_hash, ie->commit_hash, 1);
1819 return NULL;
1822 static const struct got_error *
1823 update_blob(struct got_worktree *worktree,
1824 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1825 struct got_tree_entry *te, const char *path,
1826 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1827 void *progress_arg)
1829 const struct got_error *err = NULL;
1830 struct got_blob_object *blob = NULL;
1831 char *ondisk_path = NULL;
1832 unsigned char status = GOT_STATUS_NO_CHANGE;
1833 struct stat sb;
1834 int fd1 = -1, fd2 = -1;
1836 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1837 return got_error_from_errno("asprintf");
1839 if (ie) {
1840 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1841 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1842 goto done;
1844 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1845 repo);
1846 if (err)
1847 goto done;
1848 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1849 sb.st_mode = got_fileindex_perms_to_st(ie);
1850 } else {
1851 if (stat(ondisk_path, &sb) == -1) {
1852 if (errno != ENOENT) {
1853 err = got_error_from_errno2("stat",
1854 ondisk_path);
1855 goto done;
1857 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1858 status = GOT_STATUS_UNVERSIONED;
1859 } else {
1860 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1861 status = GOT_STATUS_UNVERSIONED;
1862 else
1863 status = GOT_STATUS_OBSTRUCTED;
1867 if (status == GOT_STATUS_OBSTRUCTED) {
1868 if (ie)
1869 got_fileindex_entry_mark_skipped(ie);
1870 err = (*progress_cb)(progress_arg, status, path);
1871 goto done;
1873 if (status == GOT_STATUS_CONFLICT) {
1874 if (ie)
1875 got_fileindex_entry_mark_skipped(ie);
1876 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1877 path);
1878 goto done;
1881 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1882 (S_ISLNK(te->mode) ||
1883 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1885 * This is a regular file or an installed bad symlink.
1886 * If the file index indicates that this file is already
1887 * up-to-date with respect to the repository we can skip
1888 * updating contents of this file.
1890 if (got_fileindex_entry_has_commit(ie) &&
1891 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
1892 SHA1_DIGEST_LENGTH) == 0) {
1893 /* Same commit. */
1894 err = sync_timestamps(worktree->root_fd,
1895 path, status, ie, &sb);
1896 if (err)
1897 goto done;
1898 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1899 path);
1900 goto done;
1902 if (got_fileindex_entry_has_blob(ie) &&
1903 memcmp(ie->blob_hash, te->id.hash,
1904 SHA1_DIGEST_LENGTH) == 0) {
1905 /* Different commit but the same blob. */
1906 err = sync_timestamps(worktree->root_fd,
1907 path, status, ie, &sb);
1908 if (err)
1909 goto done;
1910 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1911 path);
1912 goto done;
1916 fd1 = got_opentempfd();
1917 if (fd1 == -1) {
1918 err = got_error_from_errno("got_opentempfd");
1919 goto done;
1921 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1922 if (err)
1923 goto done;
1925 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1926 int update_timestamps;
1927 struct got_blob_object *blob2 = NULL;
1928 char *label_orig = NULL;
1929 if (got_fileindex_entry_has_blob(ie)) {
1930 fd2 = got_opentempfd();
1931 if (fd2 == -1) {
1932 err = got_error_from_errno("got_opentempfd");
1933 goto done;
1935 struct got_object_id id2;
1936 memcpy(id2.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
1937 id2.algo = got_repo_get_hash_algorithm(repo);
1938 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1939 fd2);
1940 if (err)
1941 goto done;
1943 if (got_fileindex_entry_has_commit(ie)) {
1944 char id_str[SHA1_DIGEST_STRING_LENGTH];
1945 if (got_sha1_digest_to_str(ie->commit_hash, id_str,
1946 sizeof(id_str)) == NULL) {
1947 err = got_error_path(id_str,
1948 GOT_ERR_BAD_OBJ_ID_STR);
1949 goto done;
1951 if (asprintf(&label_orig, "%s: commit %s",
1952 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1953 err = got_error_from_errno("asprintf");
1954 goto done;
1957 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1958 char *link_target;
1959 err = got_object_blob_read_to_str(&link_target, blob);
1960 if (err)
1961 goto done;
1962 err = merge_symlink(worktree, blob2, ondisk_path, path,
1963 label_orig, link_target, worktree->base_commit_id,
1964 repo, progress_cb, progress_arg);
1965 free(link_target);
1966 } else {
1967 err = merge_blob(&update_timestamps, worktree, blob2,
1968 ondisk_path, path, sb.st_mode, label_orig, blob,
1969 worktree->base_commit_id, repo,
1970 progress_cb, progress_arg);
1972 free(label_orig);
1973 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1974 err = got_error_from_errno("close");
1975 goto done;
1977 if (blob2)
1978 got_object_blob_close(blob2);
1979 if (err)
1980 goto done;
1982 * Do not update timestamps of files with local changes.
1983 * Otherwise, a future status walk would treat them as
1984 * unmodified files again.
1986 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1987 blob->id.hash, worktree->base_commit_id->hash,
1988 update_timestamps);
1989 } else if (status == GOT_STATUS_MODE_CHANGE) {
1990 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1991 blob->id.hash, worktree->base_commit_id->hash, 0);
1992 } else if (status == GOT_STATUS_DELETE) {
1993 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1994 if (err)
1995 goto done;
1996 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1997 blob->id.hash, worktree->base_commit_id->hash, 0);
1998 if (err)
1999 goto done;
2000 } else {
2001 int is_bad_symlink = 0;
2002 if (S_ISLNK(te->mode)) {
2003 err = install_symlink(&is_bad_symlink, worktree,
2004 ondisk_path, path, blob,
2005 status == GOT_STATUS_MISSING, 0,
2006 status == GOT_STATUS_UNVERSIONED, 0,
2007 repo, progress_cb, progress_arg);
2008 } else {
2009 err = install_blob(worktree, ondisk_path, path,
2010 te->mode, sb.st_mode, blob,
2011 status == GOT_STATUS_MISSING, 0, 0,
2012 status == GOT_STATUS_UNVERSIONED, repo,
2013 progress_cb, progress_arg);
2015 if (err)
2016 goto done;
2018 if (ie) {
2019 err = got_fileindex_entry_update(ie,
2020 worktree->root_fd, path, blob->id.hash,
2021 worktree->base_commit_id->hash, 1);
2022 } else {
2023 err = create_fileindex_entry(&ie, fileindex,
2024 worktree->base_commit_id, worktree->root_fd, path,
2025 &blob->id);
2027 if (err)
2028 goto done;
2030 if (is_bad_symlink) {
2031 got_fileindex_entry_filetype_set(ie,
2032 GOT_FILEIDX_MODE_BAD_SYMLINK);
2036 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2037 err = got_error_from_errno("close");
2038 goto done;
2040 got_object_blob_close(blob);
2041 done:
2042 free(ondisk_path);
2043 return err;
2046 static const struct got_error *
2047 remove_ondisk_file(const char *root_path, const char *path)
2049 const struct got_error *err = NULL;
2050 char *ondisk_path = NULL, *parent = NULL;
2052 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2053 return got_error_from_errno("asprintf");
2055 if (unlink(ondisk_path) == -1) {
2056 if (errno != ENOENT)
2057 err = got_error_from_errno2("unlink", ondisk_path);
2058 } else {
2059 size_t root_len = strlen(root_path);
2060 err = got_path_dirname(&parent, ondisk_path);
2061 if (err)
2062 goto done;
2063 while (got_path_cmp(parent, root_path,
2064 strlen(parent), root_len) != 0) {
2065 free(ondisk_path);
2066 ondisk_path = parent;
2067 parent = NULL;
2068 if (rmdir(ondisk_path) == -1) {
2069 if (errno != ENOTEMPTY)
2070 err = got_error_from_errno2("rmdir",
2071 ondisk_path);
2072 break;
2074 err = got_path_dirname(&parent, ondisk_path);
2075 if (err)
2076 break;
2079 done:
2080 free(ondisk_path);
2081 free(parent);
2082 return err;
2085 static const struct got_error *
2086 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2087 struct got_fileindex_entry *ie, struct got_repository *repo,
2088 got_worktree_checkout_cb progress_cb, void *progress_arg)
2090 const struct got_error *err = NULL;
2091 unsigned char status;
2092 struct stat sb;
2093 char *ondisk_path;
2095 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2096 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2098 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2099 == -1)
2100 return got_error_from_errno("asprintf");
2102 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2103 if (err)
2104 goto done;
2106 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2107 char ondisk_target[PATH_MAX];
2108 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2109 sizeof(ondisk_target));
2110 if (ondisk_len == -1) {
2111 err = got_error_from_errno2("readlink", ondisk_path);
2112 goto done;
2114 ondisk_target[ondisk_len] = '\0';
2115 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2116 NULL, NULL, /* XXX pass common ancestor info? */
2117 ondisk_target, ondisk_path);
2118 if (err)
2119 goto done;
2120 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2121 ie->path);
2122 goto done;
2125 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2126 status == GOT_STATUS_ADD) {
2127 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2128 if (err)
2129 goto done;
2131 * Preserve the working file and change the deleted blob's
2132 * entry into a schedule-add entry.
2134 err = got_fileindex_entry_update(ie, worktree->root_fd,
2135 ie->path, NULL, NULL, 0);
2136 } else {
2137 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2138 if (err)
2139 goto done;
2140 if (status == GOT_STATUS_NO_CHANGE) {
2141 err = remove_ondisk_file(worktree->root_path, ie->path);
2142 if (err)
2143 goto done;
2145 got_fileindex_entry_remove(fileindex, ie);
2147 done:
2148 free(ondisk_path);
2149 return err;
2152 struct diff_cb_arg {
2153 struct got_fileindex *fileindex;
2154 struct got_worktree *worktree;
2155 struct got_repository *repo;
2156 got_worktree_checkout_cb progress_cb;
2157 void *progress_arg;
2158 got_cancel_cb cancel_cb;
2159 void *cancel_arg;
2162 static const struct got_error *
2163 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2164 struct got_tree_entry *te, const char *parent_path)
2166 struct diff_cb_arg *a = arg;
2168 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2169 return got_error(GOT_ERR_CANCELLED);
2171 return update_blob(a->worktree, a->fileindex, ie, te,
2172 ie->path, a->repo, a->progress_cb, a->progress_arg);
2175 static const struct got_error *
2176 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2178 struct diff_cb_arg *a = arg;
2180 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2181 return got_error(GOT_ERR_CANCELLED);
2183 return delete_blob(a->worktree, a->fileindex, ie,
2184 a->repo, a->progress_cb, a->progress_arg);
2187 static const struct got_error *
2188 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2190 struct diff_cb_arg *a = arg;
2191 const struct got_error *err;
2192 char *path;
2194 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2195 return got_error(GOT_ERR_CANCELLED);
2197 if (got_object_tree_entry_is_submodule(te))
2198 return NULL;
2200 if (asprintf(&path, "%s%s%s", parent_path,
2201 parent_path[0] ? "/" : "", te->name)
2202 == -1)
2203 return got_error_from_errno("asprintf");
2205 if (S_ISDIR(te->mode))
2206 err = add_dir_on_disk(a->worktree, path);
2207 else
2208 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2209 a->repo, a->progress_cb, a->progress_arg);
2211 free(path);
2212 return err;
2215 const struct got_error *
2216 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2218 uint32_t uuid_status;
2220 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2221 if (uuid_status != uuid_s_ok) {
2222 *uuidstr = NULL;
2223 return got_error_uuid(uuid_status, "uuid_to_string");
2226 return NULL;
2229 static const struct got_error *
2230 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2232 const struct got_error *err = NULL;
2233 char *uuidstr = NULL;
2235 *refname = NULL;
2237 err = got_worktree_get_uuid(&uuidstr, worktree);
2238 if (err)
2239 return err;
2241 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2242 err = got_error_from_errno("asprintf");
2243 *refname = NULL;
2245 free(uuidstr);
2246 return err;
2249 const struct got_error *
2250 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2251 const char *prefix)
2253 return get_ref_name(refname, worktree, prefix);
2256 const struct got_error *
2257 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2259 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2275 static const struct got_error *
2276 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2282 static const struct got_error *
2283 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2285 return get_ref_name(refname, worktree,
2286 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2289 static const struct got_error *
2290 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2292 return get_ref_name(refname, worktree,
2293 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2296 static const struct got_error *
2297 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2299 return get_ref_name(refname, worktree,
2300 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2303 static const struct got_error *
2304 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2306 return get_ref_name(refname, worktree,
2307 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2310 static const struct got_error *
2311 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2313 return get_ref_name(refname, worktree,
2314 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2317 const struct got_error *
2318 got_worktree_get_histedit_script_path(char **path,
2319 struct got_worktree *worktree)
2321 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2322 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2323 *path = NULL;
2324 return got_error_from_errno("asprintf");
2326 return NULL;
2329 static const struct got_error *
2330 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree,
2333 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2336 static const struct got_error *
2337 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree,
2340 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2344 * Prevent Git's garbage collector from deleting our base commit by
2345 * setting a reference to our base commit's ID.
2347 static const struct got_error *
2348 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2350 const struct got_error *err = NULL;
2351 struct got_reference *ref = NULL;
2352 char *refname;
2354 err = got_worktree_get_base_ref_name(&refname, worktree);
2355 if (err)
2356 return err;
2358 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2359 if (err)
2360 goto done;
2362 err = got_ref_write(ref, repo);
2363 done:
2364 free(refname);
2365 if (ref)
2366 got_ref_close(ref);
2367 return err;
2370 static const struct got_error *
2371 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2373 const struct got_error *err = NULL;
2375 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2376 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2377 err = got_error_from_errno("asprintf");
2378 *fileindex_path = NULL;
2380 return err;
2384 static const struct got_error *
2385 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2386 struct got_worktree *worktree)
2388 const struct got_error *err = NULL;
2389 FILE *index = NULL;
2391 *fileindex_path = NULL;
2392 *fileindex = got_fileindex_alloc();
2393 if (*fileindex == NULL)
2394 return got_error_from_errno("got_fileindex_alloc");
2396 err = get_fileindex_path(fileindex_path, worktree);
2397 if (err)
2398 goto done;
2400 index = fopen(*fileindex_path, "rbe");
2401 if (index == NULL) {
2402 if (errno != ENOENT)
2403 err = got_error_from_errno2("fopen", *fileindex_path);
2404 } else {
2405 err = got_fileindex_read(*fileindex, index);
2406 if (fclose(index) == EOF && err == NULL)
2407 err = got_error_from_errno("fclose");
2409 done:
2410 if (err) {
2411 free(*fileindex_path);
2412 *fileindex_path = NULL;
2413 got_fileindex_free(*fileindex);
2414 *fileindex = NULL;
2416 return err;
2419 struct bump_base_commit_id_arg {
2420 struct got_object_id *base_commit_id;
2421 const char *path;
2422 size_t path_len;
2423 const char *entry_name;
2424 got_worktree_checkout_cb progress_cb;
2425 void *progress_arg;
2428 /* Bump base commit ID of all files within an updated part of the work tree. */
2429 static const struct got_error *
2430 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2432 const struct got_error *err;
2433 struct bump_base_commit_id_arg *a = arg;
2435 if (a->entry_name) {
2436 if (strcmp(ie->path, a->path) != 0)
2437 return NULL;
2438 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2439 return NULL;
2441 if (got_fileindex_entry_was_skipped(ie))
2442 return NULL;
2444 if (memcmp(ie->commit_hash, a->base_commit_id->hash,
2445 SHA1_DIGEST_LENGTH) == 0)
2446 return NULL;
2448 if (a->progress_cb) {
2449 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2450 ie->path);
2451 if (err)
2452 return err;
2454 memcpy(ie->commit_hash, a->base_commit_id->hash, SHA1_DIGEST_LENGTH);
2455 return NULL;
2458 static const struct got_error *
2459 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2460 struct got_fileindex *fileindex,
2461 got_worktree_checkout_cb progress_cb, void *progress_arg)
2463 struct bump_base_commit_id_arg bbc_arg;
2465 bbc_arg.base_commit_id = worktree->base_commit_id;
2466 bbc_arg.entry_name = NULL;
2467 bbc_arg.path = "";
2468 bbc_arg.path_len = 0;
2469 bbc_arg.progress_cb = progress_cb;
2470 bbc_arg.progress_arg = progress_arg;
2472 return got_fileindex_for_each_entry_safe(fileindex,
2473 bump_base_commit_id, &bbc_arg);
2476 static const struct got_error *
2477 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2479 const struct got_error *err = NULL;
2480 char *new_fileindex_path = NULL;
2481 FILE *new_index = NULL;
2482 struct timespec timeout;
2484 err = got_opentemp_named(&new_fileindex_path, &new_index,
2485 fileindex_path, "");
2486 if (err)
2487 goto done;
2489 err = got_fileindex_write(fileindex, new_index);
2490 if (err)
2491 goto done;
2493 if (rename(new_fileindex_path, fileindex_path) != 0) {
2494 err = got_error_from_errno3("rename", new_fileindex_path,
2495 fileindex_path);
2496 unlink(new_fileindex_path);
2500 * Sleep for a short amount of time to ensure that files modified after
2501 * this program exits have a different time stamp from the one which
2502 * was recorded in the file index.
2504 timeout.tv_sec = 0;
2505 timeout.tv_nsec = 1;
2506 nanosleep(&timeout, NULL);
2507 done:
2508 if (new_index)
2509 fclose(new_index);
2510 free(new_fileindex_path);
2511 return err;
2514 static const struct got_error *
2515 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2516 struct got_object_id **tree_id, const char *wt_relpath,
2517 struct got_commit_object *base_commit, struct got_worktree *worktree,
2518 struct got_repository *repo)
2520 const struct got_error *err = NULL;
2521 struct got_object_id *id = NULL;
2522 char *in_repo_path = NULL;
2523 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2525 *entry_type = GOT_OBJ_TYPE_ANY;
2526 *tree_relpath = NULL;
2527 *tree_id = NULL;
2529 if (wt_relpath[0] == '\0') {
2530 /* Check out all files within the work tree. */
2531 *entry_type = GOT_OBJ_TYPE_TREE;
2532 *tree_relpath = strdup("");
2533 if (*tree_relpath == NULL) {
2534 err = got_error_from_errno("strdup");
2535 goto done;
2537 err = got_object_id_by_path(tree_id, repo, base_commit,
2538 worktree->path_prefix);
2539 if (err)
2540 goto done;
2541 return NULL;
2544 /* Check out a subset of files in the work tree. */
2546 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2547 is_root_wt ? "" : "/", wt_relpath) == -1) {
2548 err = got_error_from_errno("asprintf");
2549 goto done;
2552 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2553 if (err)
2554 goto done;
2556 free(in_repo_path);
2557 in_repo_path = NULL;
2559 err = got_object_get_type(entry_type, repo, id);
2560 if (err)
2561 goto done;
2563 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2564 /* Check out a single file. */
2565 if (strchr(wt_relpath, '/') == NULL) {
2566 /* Check out a single file in work tree's root dir. */
2567 in_repo_path = strdup(worktree->path_prefix);
2568 if (in_repo_path == NULL) {
2569 err = got_error_from_errno("strdup");
2570 goto done;
2572 *tree_relpath = strdup("");
2573 if (*tree_relpath == NULL) {
2574 err = got_error_from_errno("strdup");
2575 goto done;
2577 } else {
2578 /* Check out a single file in a subdirectory. */
2579 err = got_path_dirname(tree_relpath, wt_relpath);
2580 if (err)
2581 return err;
2582 if (asprintf(&in_repo_path, "%s%s%s",
2583 worktree->path_prefix, is_root_wt ? "" : "/",
2584 *tree_relpath) == -1) {
2585 err = got_error_from_errno("asprintf");
2586 goto done;
2589 err = got_object_id_by_path(tree_id, repo,
2590 base_commit, in_repo_path);
2591 } else {
2592 /* Check out all files within a subdirectory. */
2593 *tree_id = got_object_id_dup(id);
2594 if (*tree_id == NULL) {
2595 err = got_error_from_errno("got_object_id_dup");
2596 goto done;
2598 *tree_relpath = strdup(wt_relpath);
2599 if (*tree_relpath == NULL) {
2600 err = got_error_from_errno("strdup");
2601 goto done;
2604 done:
2605 free(id);
2606 free(in_repo_path);
2607 if (err) {
2608 *entry_type = GOT_OBJ_TYPE_ANY;
2609 free(*tree_relpath);
2610 *tree_relpath = NULL;
2611 free(*tree_id);
2612 *tree_id = NULL;
2614 return err;
2617 static const struct got_error *
2618 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2619 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2620 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2621 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2623 const struct got_error *err = NULL;
2624 struct got_commit_object *commit = NULL;
2625 struct got_tree_object *tree = NULL;
2626 struct got_fileindex_diff_tree_cb diff_cb;
2627 struct diff_cb_arg arg;
2629 err = ref_base_commit(worktree, repo);
2630 if (err) {
2631 if (!(err->code == GOT_ERR_ERRNO &&
2632 (errno == EACCES || errno == EROFS)))
2633 goto done;
2634 err = (*progress_cb)(progress_arg,
2635 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2636 if (err)
2637 return err;
2640 err = got_object_open_as_commit(&commit, repo,
2641 worktree->base_commit_id);
2642 if (err)
2643 goto done;
2645 err = got_object_open_as_tree(&tree, repo, tree_id);
2646 if (err)
2647 goto done;
2649 if (entry_name &&
2650 got_object_tree_find_entry(tree, entry_name) == NULL) {
2651 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2652 goto done;
2655 diff_cb.diff_old_new = diff_old_new;
2656 diff_cb.diff_old = diff_old;
2657 diff_cb.diff_new = diff_new;
2658 arg.fileindex = fileindex;
2659 arg.worktree = worktree;
2660 arg.repo = repo;
2661 arg.progress_cb = progress_cb;
2662 arg.progress_arg = progress_arg;
2663 arg.cancel_cb = cancel_cb;
2664 arg.cancel_arg = cancel_arg;
2665 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2666 entry_name, repo, &diff_cb, &arg);
2667 done:
2668 if (tree)
2669 got_object_tree_close(tree);
2670 if (commit)
2671 got_object_commit_close(commit);
2672 return err;
2675 const struct got_error *
2676 got_worktree_checkout_files(struct got_worktree *worktree,
2677 struct got_pathlist_head *paths, struct got_repository *repo,
2678 got_worktree_checkout_cb progress_cb, void *progress_arg,
2679 got_cancel_cb cancel_cb, void *cancel_arg)
2681 const struct got_error *err = NULL, *sync_err, *unlockerr;
2682 struct got_commit_object *commit = NULL;
2683 struct got_tree_object *tree = NULL;
2684 struct got_fileindex *fileindex = NULL;
2685 char *fileindex_path = NULL;
2686 struct got_pathlist_entry *pe;
2687 struct tree_path_data {
2688 STAILQ_ENTRY(tree_path_data) entry;
2689 struct got_object_id *tree_id;
2690 int entry_type;
2691 char *relpath;
2692 char *entry_name;
2693 } *tpd = NULL;
2694 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2696 STAILQ_INIT(&tree_paths);
2698 err = lock_worktree(worktree, LOCK_EX);
2699 if (err)
2700 return err;
2702 err = got_object_open_as_commit(&commit, repo,
2703 worktree->base_commit_id);
2704 if (err)
2705 goto done;
2707 /* Map all specified paths to in-repository trees. */
2708 TAILQ_FOREACH(pe, paths, entry) {
2709 tpd = malloc(sizeof(*tpd));
2710 if (tpd == NULL) {
2711 err = got_error_from_errno("malloc");
2712 goto done;
2715 err = find_tree_entry_for_checkout(&tpd->entry_type,
2716 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2717 worktree, repo);
2718 if (err) {
2719 free(tpd);
2720 goto done;
2723 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2724 err = got_path_basename(&tpd->entry_name, pe->path);
2725 if (err) {
2726 free(tpd->relpath);
2727 free(tpd->tree_id);
2728 free(tpd);
2729 goto done;
2731 } else
2732 tpd->entry_name = NULL;
2734 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2738 * Read the file index.
2739 * Checking out files is supposed to be an idempotent operation.
2740 * If the on-disk file index is incomplete we will try to complete it.
2742 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2743 if (err)
2744 goto done;
2746 tpd = STAILQ_FIRST(&tree_paths);
2747 TAILQ_FOREACH(pe, paths, entry) {
2748 struct bump_base_commit_id_arg bbc_arg;
2750 err = checkout_files(worktree, fileindex, tpd->relpath,
2751 tpd->tree_id, tpd->entry_name, repo,
2752 progress_cb, progress_arg, cancel_cb, cancel_arg);
2753 if (err)
2754 break;
2756 bbc_arg.base_commit_id = worktree->base_commit_id;
2757 bbc_arg.entry_name = tpd->entry_name;
2758 bbc_arg.path = pe->path;
2759 bbc_arg.path_len = pe->path_len;
2760 bbc_arg.progress_cb = progress_cb;
2761 bbc_arg.progress_arg = progress_arg;
2762 err = got_fileindex_for_each_entry_safe(fileindex,
2763 bump_base_commit_id, &bbc_arg);
2764 if (err)
2765 break;
2767 tpd = STAILQ_NEXT(tpd, entry);
2769 sync_err = sync_fileindex(fileindex, fileindex_path);
2770 if (sync_err && err == NULL)
2771 err = sync_err;
2772 done:
2773 free(fileindex_path);
2774 if (tree)
2775 got_object_tree_close(tree);
2776 if (commit)
2777 got_object_commit_close(commit);
2778 if (fileindex)
2779 got_fileindex_free(fileindex);
2780 while (!STAILQ_EMPTY(&tree_paths)) {
2781 tpd = STAILQ_FIRST(&tree_paths);
2782 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2783 free(tpd->relpath);
2784 free(tpd->tree_id);
2785 free(tpd);
2787 unlockerr = lock_worktree(worktree, LOCK_SH);
2788 if (unlockerr && err == NULL)
2789 err = unlockerr;
2790 return err;
2793 struct merge_file_cb_arg {
2794 struct got_worktree *worktree;
2795 struct got_fileindex *fileindex;
2796 got_worktree_checkout_cb progress_cb;
2797 void *progress_arg;
2798 got_cancel_cb cancel_cb;
2799 void *cancel_arg;
2800 const char *label_orig;
2801 struct got_object_id *commit_id2;
2802 int allow_bad_symlinks;
2805 static const struct got_error *
2806 merge_file_cb(void *arg, struct got_blob_object *blob1,
2807 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2808 struct got_object_id *id1, struct got_object_id *id2,
2809 const char *path1, const char *path2,
2810 mode_t mode1, mode_t mode2, struct got_repository *repo)
2812 static const struct got_error *err = NULL;
2813 struct merge_file_cb_arg *a = arg;
2814 struct got_fileindex_entry *ie;
2815 char *ondisk_path = NULL;
2816 struct stat sb;
2817 unsigned char status;
2818 int local_changes_subsumed;
2819 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2820 char *id_str = NULL, *label_deriv2 = NULL;
2822 if (blob1 && blob2) {
2823 ie = got_fileindex_entry_get(a->fileindex, path2,
2824 strlen(path2));
2825 if (ie == NULL)
2826 return (*a->progress_cb)(a->progress_arg,
2827 GOT_STATUS_MISSING, path2);
2829 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2830 path2) == -1)
2831 return got_error_from_errno("asprintf");
2833 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2834 repo);
2835 if (err)
2836 goto done;
2838 if (status == GOT_STATUS_DELETE) {
2839 err = (*a->progress_cb)(a->progress_arg,
2840 GOT_STATUS_MERGE, path2);
2841 goto done;
2843 if (status != GOT_STATUS_NO_CHANGE &&
2844 status != GOT_STATUS_MODIFY &&
2845 status != GOT_STATUS_CONFLICT &&
2846 status != GOT_STATUS_ADD) {
2847 err = (*a->progress_cb)(a->progress_arg, status, path2);
2848 goto done;
2851 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2852 char *link_target2;
2853 err = got_object_blob_read_to_str(&link_target2, blob2);
2854 if (err)
2855 goto done;
2856 err = merge_symlink(a->worktree, blob1, ondisk_path,
2857 path2, a->label_orig, link_target2, a->commit_id2,
2858 repo, a->progress_cb, a->progress_arg);
2859 free(link_target2);
2860 } else {
2861 int fd;
2863 f_orig = got_opentemp();
2864 if (f_orig == NULL) {
2865 err = got_error_from_errno("got_opentemp");
2866 goto done;
2868 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2869 f_orig, blob1);
2870 if (err)
2871 goto done;
2873 f_deriv2 = got_opentemp();
2874 if (f_deriv2 == NULL)
2875 goto done;
2876 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2877 f_deriv2, blob2);
2878 if (err)
2879 goto done;
2881 fd = open(ondisk_path,
2882 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2883 if (fd == -1) {
2884 err = got_error_from_errno2("open",
2885 ondisk_path);
2886 goto done;
2888 f_deriv = fdopen(fd, "r");
2889 if (f_deriv == NULL) {
2890 err = got_error_from_errno2("fdopen",
2891 ondisk_path);
2892 close(fd);
2893 goto done;
2895 err = got_object_id_str(&id_str, a->commit_id2);
2896 if (err)
2897 goto done;
2898 if (asprintf(&label_deriv2, "%s: commit %s",
2899 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2900 err = got_error_from_errno("asprintf");
2901 goto done;
2903 err = merge_file(&local_changes_subsumed, a->worktree,
2904 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2905 mode2, a->label_orig, NULL, label_deriv2,
2906 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2907 a->progress_cb, a->progress_arg);
2909 } else if (blob1) {
2910 ie = got_fileindex_entry_get(a->fileindex, path1,
2911 strlen(path1));
2912 if (ie == NULL)
2913 return (*a->progress_cb)(a->progress_arg,
2914 GOT_STATUS_MISSING, path1);
2916 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2917 path1) == -1)
2918 return got_error_from_errno("asprintf");
2920 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2921 repo);
2922 if (err)
2923 goto done;
2925 switch (status) {
2926 case GOT_STATUS_NO_CHANGE:
2927 err = (*a->progress_cb)(a->progress_arg,
2928 GOT_STATUS_DELETE, path1);
2929 if (err)
2930 goto done;
2931 err = remove_ondisk_file(a->worktree->root_path, path1);
2932 if (err)
2933 goto done;
2934 if (ie)
2935 got_fileindex_entry_mark_deleted_from_disk(ie);
2936 break;
2937 case GOT_STATUS_DELETE:
2938 case GOT_STATUS_MISSING:
2939 err = (*a->progress_cb)(a->progress_arg,
2940 GOT_STATUS_DELETE, path1);
2941 if (err)
2942 goto done;
2943 if (ie)
2944 got_fileindex_entry_mark_deleted_from_disk(ie);
2945 break;
2946 case GOT_STATUS_ADD: {
2947 struct got_object_id *id;
2948 FILE *blob1_f;
2949 off_t blob1_size;
2951 * Delete the added file only if its content already
2952 * exists in the repository.
2954 err = got_object_blob_file_create(&id, &blob1_f,
2955 &blob1_size, path1);
2956 if (err)
2957 goto done;
2958 if (got_object_id_cmp(id, id1) == 0) {
2959 err = (*a->progress_cb)(a->progress_arg,
2960 GOT_STATUS_DELETE, path1);
2961 if (err)
2962 goto done;
2963 err = remove_ondisk_file(a->worktree->root_path,
2964 path1);
2965 if (err)
2966 goto done;
2967 if (ie)
2968 got_fileindex_entry_remove(a->fileindex,
2969 ie);
2970 } else {
2971 err = (*a->progress_cb)(a->progress_arg,
2972 GOT_STATUS_CANNOT_DELETE, path1);
2974 if (fclose(blob1_f) == EOF && err == NULL)
2975 err = got_error_from_errno("fclose");
2976 free(id);
2977 if (err)
2978 goto done;
2979 break;
2981 case GOT_STATUS_MODIFY:
2982 case GOT_STATUS_CONFLICT:
2983 err = (*a->progress_cb)(a->progress_arg,
2984 GOT_STATUS_CANNOT_DELETE, path1);
2985 if (err)
2986 goto done;
2987 break;
2988 case GOT_STATUS_OBSTRUCTED:
2989 err = (*a->progress_cb)(a->progress_arg, status, path1);
2990 if (err)
2991 goto done;
2992 break;
2993 default:
2994 break;
2996 } else if (blob2) {
2997 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2998 path2) == -1)
2999 return got_error_from_errno("asprintf");
3000 ie = got_fileindex_entry_get(a->fileindex, path2,
3001 strlen(path2));
3002 if (ie) {
3003 err = get_file_status(&status, &sb, ie, ondisk_path,
3004 -1, NULL, repo);
3005 if (err)
3006 goto done;
3007 if (status != GOT_STATUS_NO_CHANGE &&
3008 status != GOT_STATUS_MODIFY &&
3009 status != GOT_STATUS_CONFLICT &&
3010 status != GOT_STATUS_ADD) {
3011 err = (*a->progress_cb)(a->progress_arg,
3012 status, path2);
3013 goto done;
3015 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3016 char *link_target2;
3017 err = got_object_blob_read_to_str(&link_target2,
3018 blob2);
3019 if (err)
3020 goto done;
3021 err = merge_symlink(a->worktree, NULL,
3022 ondisk_path, path2, a->label_orig,
3023 link_target2, a->commit_id2, repo,
3024 a->progress_cb, a->progress_arg);
3025 free(link_target2);
3026 } else if (S_ISREG(sb.st_mode)) {
3027 err = merge_blob(&local_changes_subsumed,
3028 a->worktree, NULL, ondisk_path, path2,
3029 sb.st_mode, a->label_orig, blob2,
3030 a->commit_id2, repo, a->progress_cb,
3031 a->progress_arg);
3032 } else {
3033 err = got_error_path(ondisk_path,
3034 GOT_ERR_FILE_OBSTRUCTED);
3036 if (err)
3037 goto done;
3038 if (status == GOT_STATUS_DELETE) {
3039 err = got_fileindex_entry_update(ie,
3040 a->worktree->root_fd, path2, blob2->id.hash,
3041 a->worktree->base_commit_id->hash, 0);
3042 if (err)
3043 goto done;
3045 } else {
3046 int is_bad_symlink = 0;
3047 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3048 if (S_ISLNK(mode2)) {
3049 err = install_symlink(&is_bad_symlink,
3050 a->worktree, ondisk_path, path2, blob2, 0,
3051 0, 1, a->allow_bad_symlinks, repo,
3052 a->progress_cb, a->progress_arg);
3053 } else {
3054 err = install_blob(a->worktree, ondisk_path, path2,
3055 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3056 a->progress_cb, a->progress_arg);
3058 if (err)
3059 goto done;
3060 err = got_fileindex_entry_alloc(&ie, path2);
3061 if (err)
3062 goto done;
3063 err = got_fileindex_entry_update(ie,
3064 a->worktree->root_fd, path2, NULL, NULL, 1);
3065 if (err) {
3066 got_fileindex_entry_free(ie);
3067 goto done;
3069 err = got_fileindex_entry_add(a->fileindex, ie);
3070 if (err) {
3071 got_fileindex_entry_free(ie);
3072 goto done;
3074 if (is_bad_symlink) {
3075 got_fileindex_entry_filetype_set(ie,
3076 GOT_FILEIDX_MODE_BAD_SYMLINK);
3080 done:
3081 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3082 err = got_error_from_errno("fclose");
3083 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3084 err = got_error_from_errno("fclose");
3085 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3086 err = got_error_from_errno("fclose");
3087 free(id_str);
3088 free(label_deriv2);
3089 free(ondisk_path);
3090 return err;
3093 static const struct got_error *
3094 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3096 struct got_worktree *worktree = arg;
3098 /* Reject merges into a work tree with mixed base commits. */
3099 if (got_fileindex_entry_has_commit(ie) &&
3100 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
3101 SHA1_DIGEST_LENGTH) != 0)
3102 return got_error(GOT_ERR_MIXED_COMMITS);
3104 return NULL;
3107 struct check_merge_conflicts_arg {
3108 struct got_worktree *worktree;
3109 struct got_fileindex *fileindex;
3110 struct got_repository *repo;
3113 static const struct got_error *
3114 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3115 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3116 struct got_object_id *id1, struct got_object_id *id2,
3117 const char *path1, const char *path2,
3118 mode_t mode1, mode_t mode2, struct got_repository *repo)
3120 const struct got_error *err = NULL;
3121 struct check_merge_conflicts_arg *a = arg;
3122 unsigned char status;
3123 struct stat sb;
3124 struct got_fileindex_entry *ie;
3125 const char *path = path2 ? path2 : path1;
3126 struct got_object_id *id = id2 ? id2 : id1;
3127 char *ondisk_path;
3129 if (id == NULL)
3130 return NULL;
3132 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3133 if (ie == NULL)
3134 return NULL;
3136 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3137 == -1)
3138 return got_error_from_errno("asprintf");
3140 /* Reject merges into a work tree with conflicted files. */
3141 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3142 free(ondisk_path);
3143 if (err)
3144 return err;
3145 if (status == GOT_STATUS_CONFLICT)
3146 return got_error(GOT_ERR_CONFLICTS);
3148 return NULL;
3151 static const struct got_error *
3152 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3153 const char *fileindex_path, struct got_object_id *commit_id1,
3154 struct got_object_id *commit_id2, struct got_repository *repo,
3155 got_worktree_checkout_cb progress_cb, void *progress_arg,
3156 got_cancel_cb cancel_cb, void *cancel_arg)
3158 const struct got_error *err = NULL, *sync_err;
3159 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3160 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3161 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3162 struct check_merge_conflicts_arg cmc_arg;
3163 struct merge_file_cb_arg arg;
3164 char *label_orig = NULL;
3165 FILE *f1 = NULL, *f2 = NULL;
3166 int fd1 = -1, fd2 = -1;
3168 if (commit_id1) {
3169 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3170 if (err)
3171 goto done;
3172 err = got_object_id_by_path(&tree_id1, repo, commit1,
3173 worktree->path_prefix);
3174 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3175 goto done;
3177 if (tree_id1) {
3178 char *id_str;
3180 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3181 if (err)
3182 goto done;
3184 err = got_object_id_str(&id_str, commit_id1);
3185 if (err)
3186 goto done;
3188 if (asprintf(&label_orig, "%s: commit %s",
3189 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3190 err = got_error_from_errno("asprintf");
3191 free(id_str);
3192 goto done;
3194 free(id_str);
3196 f1 = got_opentemp();
3197 if (f1 == NULL) {
3198 err = got_error_from_errno("got_opentemp");
3199 goto done;
3203 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3204 if (err)
3205 goto done;
3207 err = got_object_id_by_path(&tree_id2, repo, commit2,
3208 worktree->path_prefix);
3209 if (err)
3210 goto done;
3212 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3213 if (err)
3214 goto done;
3216 f2 = got_opentemp();
3217 if (f2 == NULL) {
3218 err = got_error_from_errno("got_opentemp");
3219 goto done;
3222 fd1 = got_opentempfd();
3223 if (fd1 == -1) {
3224 err = got_error_from_errno("got_opentempfd");
3225 goto done;
3228 fd2 = got_opentempfd();
3229 if (fd2 == -1) {
3230 err = got_error_from_errno("got_opentempfd");
3231 goto done;
3234 cmc_arg.worktree = worktree;
3235 cmc_arg.fileindex = fileindex;
3236 cmc_arg.repo = repo;
3237 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3238 check_merge_conflicts, &cmc_arg, 0);
3239 if (err)
3240 goto done;
3242 arg.worktree = worktree;
3243 arg.fileindex = fileindex;
3244 arg.progress_cb = progress_cb;
3245 arg.progress_arg = progress_arg;
3246 arg.cancel_cb = cancel_cb;
3247 arg.cancel_arg = cancel_arg;
3248 arg.label_orig = label_orig;
3249 arg.commit_id2 = commit_id2;
3250 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3251 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3252 merge_file_cb, &arg, 1);
3253 sync_err = sync_fileindex(fileindex, fileindex_path);
3254 if (sync_err && err == NULL)
3255 err = sync_err;
3256 done:
3257 if (commit1)
3258 got_object_commit_close(commit1);
3259 if (commit2)
3260 got_object_commit_close(commit2);
3261 if (tree1)
3262 got_object_tree_close(tree1);
3263 if (tree2)
3264 got_object_tree_close(tree2);
3265 if (f1 && fclose(f1) == EOF && err == NULL)
3266 err = got_error_from_errno("fclose");
3267 if (f2 && fclose(f2) == EOF && err == NULL)
3268 err = got_error_from_errno("fclose");
3269 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3270 err = got_error_from_errno("close");
3271 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3272 err = got_error_from_errno("close");
3273 free(label_orig);
3274 return err;
3277 const struct got_error *
3278 got_worktree_merge_files(struct got_worktree *worktree,
3279 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3280 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3281 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3283 const struct got_error *err, *unlockerr;
3284 char *fileindex_path = NULL;
3285 struct got_fileindex *fileindex = NULL;
3287 err = lock_worktree(worktree, LOCK_EX);
3288 if (err)
3289 return err;
3291 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3292 if (err)
3293 goto done;
3295 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3296 worktree);
3297 if (err)
3298 goto done;
3300 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3301 commit_id2, repo, progress_cb, progress_arg,
3302 cancel_cb, cancel_arg);
3303 done:
3304 if (fileindex)
3305 got_fileindex_free(fileindex);
3306 free(fileindex_path);
3307 unlockerr = lock_worktree(worktree, LOCK_SH);
3308 if (unlockerr && err == NULL)
3309 err = unlockerr;
3310 return err;
3313 struct diff_dir_cb_arg {
3314 struct got_fileindex *fileindex;
3315 struct got_worktree *worktree;
3316 const char *status_path;
3317 size_t status_path_len;
3318 struct got_repository *repo;
3319 got_worktree_status_cb status_cb;
3320 void *status_arg;
3321 got_cancel_cb cancel_cb;
3322 void *cancel_arg;
3323 /* A pathlist containing per-directory pathlists of ignore patterns. */
3324 struct got_pathlist_head *ignores;
3325 int report_unchanged;
3326 int no_ignores;
3329 static const struct got_error *
3330 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3331 int dirfd, const char *de_name,
3332 got_worktree_status_cb status_cb, void *status_arg,
3333 struct got_repository *repo, int report_unchanged)
3335 const struct got_error *err = NULL;
3336 unsigned char status = GOT_STATUS_NO_CHANGE;
3337 unsigned char staged_status = get_staged_status(ie);
3338 struct stat sb;
3339 struct got_object_id blob_id, commit_id, staged_blob_id;
3340 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3341 struct got_object_id *staged_blob_idp = NULL;
3343 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3344 if (err)
3345 return err;
3347 if (status == GOT_STATUS_NO_CHANGE &&
3348 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3349 return NULL;
3351 if (got_fileindex_entry_has_blob(ie)) {
3352 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3353 blob_id.algo = got_repo_get_hash_algorithm(repo);
3354 blob_idp = &blob_id;
3356 if (got_fileindex_entry_has_commit(ie)) {
3357 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3358 commit_id.algo = got_repo_get_hash_algorithm(repo);
3359 commit_idp = &commit_id;
3361 if (staged_status == GOT_STATUS_ADD ||
3362 staged_status == GOT_STATUS_MODIFY) {
3363 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
3364 SHA1_DIGEST_LENGTH);
3365 staged_blob_id.algo = got_repo_get_hash_algorithm(repo);
3366 staged_blob_idp = &staged_blob_id;
3369 return (*status_cb)(status_arg, status, staged_status,
3370 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3373 static const struct got_error *
3374 status_old_new(void *arg, struct got_fileindex_entry *ie,
3375 struct dirent *de, const char *parent_path, int dirfd)
3377 const struct got_error *err = NULL;
3378 struct diff_dir_cb_arg *a = arg;
3379 char *abspath;
3381 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3382 return got_error(GOT_ERR_CANCELLED);
3384 if (got_path_cmp(parent_path, a->status_path,
3385 strlen(parent_path), a->status_path_len) != 0 &&
3386 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3387 return NULL;
3389 if (parent_path[0]) {
3390 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3391 parent_path, de->d_name) == -1)
3392 return got_error_from_errno("asprintf");
3393 } else {
3394 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3395 de->d_name) == -1)
3396 return got_error_from_errno("asprintf");
3399 err = report_file_status(ie, abspath, dirfd, de->d_name,
3400 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3401 free(abspath);
3402 return err;
3405 static const struct got_error *
3406 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3408 struct diff_dir_cb_arg *a = arg;
3409 struct got_object_id blob_id, commit_id;
3410 unsigned char status;
3412 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3413 return got_error(GOT_ERR_CANCELLED);
3415 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3416 return NULL;
3418 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3419 blob_id.algo = got_repo_get_hash_algorithm(a->repo);
3420 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3421 commit_id.algo = got_repo_get_hash_algorithm(a->repo);
3422 if (got_fileindex_entry_has_file_on_disk(ie))
3423 status = GOT_STATUS_MISSING;
3424 else
3425 status = GOT_STATUS_DELETE;
3426 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3427 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3430 static void
3431 free_ignores(struct got_pathlist_head *ignores)
3433 struct got_pathlist_entry *pe;
3435 TAILQ_FOREACH(pe, ignores, entry) {
3436 struct got_pathlist_head *ignorelist = pe->data;
3438 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3440 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3443 static const struct got_error *
3444 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3446 const struct got_error *err = NULL;
3447 struct got_pathlist_entry *pe = NULL;
3448 struct got_pathlist_head *ignorelist;
3449 char *line = NULL, *pattern, *dirpath = NULL;
3450 size_t linesize = 0;
3451 ssize_t linelen;
3453 ignorelist = calloc(1, sizeof(*ignorelist));
3454 if (ignorelist == NULL)
3455 return got_error_from_errno("calloc");
3456 TAILQ_INIT(ignorelist);
3458 while ((linelen = getline(&line, &linesize, f)) != -1) {
3459 if (linelen > 0 && line[linelen - 1] == '\n')
3460 line[linelen - 1] = '\0';
3462 /* Git's ignores may contain comments. */
3463 if (line[0] == '#')
3464 continue;
3466 /* Git's negated patterns are not (yet?) supported. */
3467 if (line[0] == '!')
3468 continue;
3470 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3471 line) == -1) {
3472 err = got_error_from_errno("asprintf");
3473 goto done;
3475 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3476 if (err)
3477 goto done;
3479 if (ferror(f)) {
3480 err = got_error_from_errno("getline");
3481 goto done;
3484 dirpath = strdup(path);
3485 if (dirpath == NULL) {
3486 err = got_error_from_errno("strdup");
3487 goto done;
3489 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3490 done:
3491 free(line);
3492 if (err || pe == NULL) {
3493 free(dirpath);
3494 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3496 return err;
3499 static int
3500 match_ignores(struct got_pathlist_head *ignores, const char *path)
3502 struct got_pathlist_entry *pe;
3504 /* Handle patterns which match in all directories. */
3505 TAILQ_FOREACH(pe, ignores, entry) {
3506 struct got_pathlist_head *ignorelist = pe->data;
3507 struct got_pathlist_entry *pi;
3509 TAILQ_FOREACH(pi, ignorelist, entry) {
3510 const char *p, *pattern = pi->path;
3512 if (strncmp(pattern, "**/", 3) != 0)
3513 continue;
3514 pattern += 3;
3515 p = path;
3516 while (*p) {
3517 if (fnmatch(pattern, p,
3518 FNM_PATHNAME | FNM_LEADING_DIR)) {
3519 /* Retry in next directory. */
3520 while (*p && *p != '/')
3521 p++;
3522 while (*p == '/')
3523 p++;
3524 continue;
3526 return 1;
3532 * The ignores pathlist contains ignore lists from children before
3533 * parents, so we can find the most specific ignorelist by walking
3534 * ignores backwards.
3536 pe = TAILQ_LAST(ignores, got_pathlist_head);
3537 while (pe) {
3538 if (got_path_is_child(path, pe->path, pe->path_len)) {
3539 struct got_pathlist_head *ignorelist = pe->data;
3540 struct got_pathlist_entry *pi;
3541 TAILQ_FOREACH(pi, ignorelist, entry) {
3542 const char *pattern = pi->path;
3543 int flags = FNM_LEADING_DIR;
3544 if (strstr(pattern, "/**/") == NULL)
3545 flags |= FNM_PATHNAME;
3546 if (fnmatch(pattern, path, flags))
3547 continue;
3548 return 1;
3551 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3554 return 0;
3557 static const struct got_error *
3558 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3559 const char *path, int dirfd, const char *ignores_filename)
3561 const struct got_error *err = NULL;
3562 char *ignorespath;
3563 int fd = -1;
3564 FILE *ignoresfile = NULL;
3566 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3567 path[0] ? "/" : "", ignores_filename) == -1)
3568 return got_error_from_errno("asprintf");
3570 if (dirfd != -1) {
3571 fd = openat(dirfd, ignores_filename,
3572 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3573 if (fd == -1) {
3574 if (errno != ENOENT && errno != EACCES)
3575 err = got_error_from_errno2("openat",
3576 ignorespath);
3577 } else {
3578 ignoresfile = fdopen(fd, "r");
3579 if (ignoresfile == NULL)
3580 err = got_error_from_errno2("fdopen",
3581 ignorespath);
3582 else {
3583 fd = -1;
3584 err = read_ignores(ignores, path, ignoresfile);
3587 } else {
3588 ignoresfile = fopen(ignorespath, "re");
3589 if (ignoresfile == NULL) {
3590 if (errno != ENOENT && errno != EACCES)
3591 err = got_error_from_errno2("fopen",
3592 ignorespath);
3593 } else
3594 err = read_ignores(ignores, path, ignoresfile);
3597 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3598 err = got_error_from_errno2("fclose", path);
3599 if (fd != -1 && close(fd) == -1 && err == NULL)
3600 err = got_error_from_errno2("close", path);
3601 free(ignorespath);
3602 return err;
3605 static const struct got_error *
3606 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3607 int dirfd)
3609 const struct got_error *err = NULL;
3610 struct diff_dir_cb_arg *a = arg;
3611 char *path = NULL;
3613 if (ignore != NULL)
3614 *ignore = 0;
3616 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3617 return got_error(GOT_ERR_CANCELLED);
3619 if (parent_path[0]) {
3620 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3621 return got_error_from_errno("asprintf");
3622 } else {
3623 path = de->d_name;
3626 if (de->d_type == DT_DIR) {
3627 if (!a->no_ignores && ignore != NULL &&
3628 match_ignores(a->ignores, path))
3629 *ignore = 1;
3630 } else if (!match_ignores(a->ignores, path) &&
3631 got_path_is_child(path, a->status_path, a->status_path_len))
3632 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3633 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3634 if (parent_path[0])
3635 free(path);
3636 return err;
3639 static const struct got_error *
3640 status_traverse(void *arg, const char *path, int dirfd)
3642 const struct got_error *err = NULL;
3643 struct diff_dir_cb_arg *a = arg;
3645 if (a->no_ignores)
3646 return NULL;
3648 err = add_ignores(a->ignores, a->worktree->root_path,
3649 path, dirfd, ".cvsignore");
3650 if (err)
3651 return err;
3653 err = add_ignores(a->ignores, a->worktree->root_path, path,
3654 dirfd, ".gitignore");
3656 return err;
3659 static const struct got_error *
3660 report_single_file_status(const char *path, const char *ondisk_path,
3661 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3662 void *status_arg, struct got_repository *repo, int report_unchanged,
3663 struct got_pathlist_head *ignores, int no_ignores)
3665 struct got_fileindex_entry *ie;
3666 struct stat sb;
3668 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3669 if (ie)
3670 return report_file_status(ie, ondisk_path, -1, NULL,
3671 status_cb, status_arg, repo, report_unchanged);
3673 if (lstat(ondisk_path, &sb) == -1) {
3674 if (errno != ENOENT)
3675 return got_error_from_errno2("lstat", ondisk_path);
3676 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3677 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3680 if (!no_ignores && match_ignores(ignores, path))
3681 return NULL;
3683 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3684 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3685 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3687 return NULL;
3690 static const struct got_error *
3691 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3692 const char *root_path, const char *path)
3694 const struct got_error *err;
3695 char *parent_path, *next_parent_path = NULL;
3697 err = add_ignores(ignores, root_path, "", -1,
3698 ".cvsignore");
3699 if (err)
3700 return err;
3702 err = add_ignores(ignores, root_path, "", -1,
3703 ".gitignore");
3704 if (err)
3705 return err;
3707 err = got_path_dirname(&parent_path, path);
3708 if (err) {
3709 if (err->code == GOT_ERR_BAD_PATH)
3710 return NULL; /* cannot traverse parent */
3711 return err;
3713 for (;;) {
3714 err = add_ignores(ignores, root_path, parent_path, -1,
3715 ".cvsignore");
3716 if (err)
3717 break;
3718 err = add_ignores(ignores, root_path, parent_path, -1,
3719 ".gitignore");
3720 if (err)
3721 break;
3722 err = got_path_dirname(&next_parent_path, parent_path);
3723 if (err) {
3724 if (err->code == GOT_ERR_BAD_PATH)
3725 err = NULL; /* traversed everything */
3726 break;
3728 if (got_path_is_root_dir(parent_path))
3729 break;
3730 free(parent_path);
3731 parent_path = next_parent_path;
3732 next_parent_path = NULL;
3735 free(parent_path);
3736 free(next_parent_path);
3737 return err;
3740 static const struct got_error *
3741 worktree_status(struct got_worktree *worktree, const char *path,
3742 struct got_fileindex *fileindex, struct got_repository *repo,
3743 got_worktree_status_cb status_cb, void *status_arg,
3744 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3745 int report_unchanged)
3747 const struct got_error *err = NULL;
3748 int fd = -1;
3749 struct got_fileindex_diff_dir_cb fdiff_cb;
3750 struct diff_dir_cb_arg arg;
3751 char *ondisk_path = NULL;
3752 struct got_pathlist_head ignores;
3753 struct got_fileindex_entry *ie;
3755 TAILQ_INIT(&ignores);
3757 if (asprintf(&ondisk_path, "%s%s%s",
3758 worktree->root_path, path[0] ? "/" : "", path) == -1)
3759 return got_error_from_errno("asprintf");
3761 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3762 if (ie) {
3763 err = report_single_file_status(path, ondisk_path,
3764 fileindex, status_cb, status_arg, repo,
3765 report_unchanged, &ignores, no_ignores);
3766 goto done;
3769 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3770 if (fd == -1) {
3771 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3772 !got_err_open_nofollow_on_symlink())
3773 err = got_error_from_errno2("open", ondisk_path);
3774 else {
3775 if (!no_ignores) {
3776 err = add_ignores_from_parent_paths(&ignores,
3777 worktree->root_path, ondisk_path);
3778 if (err)
3779 goto done;
3781 err = report_single_file_status(path, ondisk_path,
3782 fileindex, status_cb, status_arg, repo,
3783 report_unchanged, &ignores, no_ignores);
3785 } else {
3786 fdiff_cb.diff_old_new = status_old_new;
3787 fdiff_cb.diff_old = status_old;
3788 fdiff_cb.diff_new = status_new;
3789 fdiff_cb.diff_traverse = status_traverse;
3790 arg.fileindex = fileindex;
3791 arg.worktree = worktree;
3792 arg.status_path = path;
3793 arg.status_path_len = strlen(path);
3794 arg.repo = repo;
3795 arg.status_cb = status_cb;
3796 arg.status_arg = status_arg;
3797 arg.cancel_cb = cancel_cb;
3798 arg.cancel_arg = cancel_arg;
3799 arg.report_unchanged = report_unchanged;
3800 arg.no_ignores = no_ignores;
3801 if (!no_ignores) {
3802 err = add_ignores_from_parent_paths(&ignores,
3803 worktree->root_path, path);
3804 if (err)
3805 goto done;
3807 arg.ignores = &ignores;
3808 err = got_fileindex_diff_dir(fileindex, fd,
3809 worktree->root_path, path, repo, &fdiff_cb, &arg);
3811 done:
3812 free_ignores(&ignores);
3813 if (fd != -1 && close(fd) == -1 && err == NULL)
3814 err = got_error_from_errno("close");
3815 free(ondisk_path);
3816 return err;
3819 const struct got_error *
3820 got_worktree_status(struct got_worktree *worktree,
3821 struct got_pathlist_head *paths, struct got_repository *repo,
3822 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3823 got_cancel_cb cancel_cb, void *cancel_arg)
3825 const struct got_error *err = NULL;
3826 char *fileindex_path = NULL;
3827 struct got_fileindex *fileindex = NULL;
3828 struct got_pathlist_entry *pe;
3830 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3831 if (err)
3832 return err;
3834 TAILQ_FOREACH(pe, paths, entry) {
3835 err = worktree_status(worktree, pe->path, fileindex, repo,
3836 status_cb, status_arg, cancel_cb, cancel_arg,
3837 no_ignores, 0);
3838 if (err)
3839 break;
3841 free(fileindex_path);
3842 got_fileindex_free(fileindex);
3843 return err;
3846 const struct got_error *
3847 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3848 const char *arg)
3850 const struct got_error *err = NULL;
3851 char *resolved = NULL, *cwd = NULL, *path = NULL;
3852 size_t len;
3853 struct stat sb;
3854 char *abspath = NULL;
3855 char canonpath[PATH_MAX];
3857 *wt_path = NULL;
3859 cwd = getcwd(NULL, 0);
3860 if (cwd == NULL)
3861 return got_error_from_errno("getcwd");
3863 if (lstat(arg, &sb) == -1) {
3864 if (errno != ENOENT) {
3865 err = got_error_from_errno2("lstat", arg);
3866 goto done;
3868 sb.st_mode = 0;
3870 if (S_ISLNK(sb.st_mode)) {
3872 * We cannot use realpath(3) with symlinks since we want to
3873 * operate on the symlink itself.
3874 * But we can make the path absolute, assuming it is relative
3875 * to the current working directory, and then canonicalize it.
3877 if (!got_path_is_absolute(arg)) {
3878 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3879 err = got_error_from_errno("asprintf");
3880 goto done;
3884 err = got_canonpath(abspath ? abspath : arg, canonpath,
3885 sizeof(canonpath));
3886 if (err)
3887 goto done;
3888 resolved = strdup(canonpath);
3889 if (resolved == NULL) {
3890 err = got_error_from_errno("strdup");
3891 goto done;
3893 } else {
3894 resolved = realpath(arg, NULL);
3895 if (resolved == NULL) {
3896 if (errno != ENOENT) {
3897 err = got_error_from_errno2("realpath", arg);
3898 goto done;
3900 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3901 err = got_error_from_errno("asprintf");
3902 goto done;
3904 err = got_canonpath(abspath, canonpath,
3905 sizeof(canonpath));
3906 if (err)
3907 goto done;
3908 resolved = strdup(canonpath);
3909 if (resolved == NULL) {
3910 err = got_error_from_errno("strdup");
3911 goto done;
3916 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3917 strlen(got_worktree_get_root_path(worktree)))) {
3918 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3919 goto done;
3922 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3923 err = got_path_skip_common_ancestor(&path,
3924 got_worktree_get_root_path(worktree), resolved);
3925 if (err)
3926 goto done;
3927 } else {
3928 path = strdup("");
3929 if (path == NULL) {
3930 err = got_error_from_errno("strdup");
3931 goto done;
3935 /* XXX status walk can't deal with trailing slash! */
3936 len = strlen(path);
3937 while (len > 0 && path[len - 1] == '/') {
3938 path[len - 1] = '\0';
3939 len--;
3941 done:
3942 free(abspath);
3943 free(resolved);
3944 free(cwd);
3945 if (err == NULL)
3946 *wt_path = path;
3947 else
3948 free(path);
3949 return err;
3952 struct schedule_addition_args {
3953 struct got_worktree *worktree;
3954 struct got_fileindex *fileindex;
3955 got_worktree_checkout_cb progress_cb;
3956 void *progress_arg;
3957 struct got_repository *repo;
3960 static const struct got_error *
3961 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3962 const char *relpath, struct got_object_id *blob_id,
3963 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3964 int dirfd, const char *de_name)
3966 struct schedule_addition_args *a = arg;
3967 const struct got_error *err = NULL;
3968 struct got_fileindex_entry *ie;
3969 struct stat sb;
3970 char *ondisk_path;
3972 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3973 relpath) == -1)
3974 return got_error_from_errno("asprintf");
3976 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3977 if (ie) {
3978 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3979 de_name, a->repo);
3980 if (err)
3981 goto done;
3982 /* Re-adding an existing entry is a no-op. */
3983 if (status == GOT_STATUS_ADD)
3984 goto done;
3985 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3986 if (err)
3987 goto done;
3990 if (status != GOT_STATUS_UNVERSIONED) {
3991 if (status == GOT_STATUS_NONEXISTENT)
3992 err = got_error_set_errno(ENOENT, ondisk_path);
3993 else
3994 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3995 goto done;
3998 err = got_fileindex_entry_alloc(&ie, relpath);
3999 if (err)
4000 goto done;
4001 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4002 relpath, NULL, NULL, 1);
4003 if (err) {
4004 got_fileindex_entry_free(ie);
4005 goto done;
4007 err = got_fileindex_entry_add(a->fileindex, ie);
4008 if (err) {
4009 got_fileindex_entry_free(ie);
4010 goto done;
4012 done:
4013 free(ondisk_path);
4014 if (err)
4015 return err;
4016 if (status == GOT_STATUS_ADD)
4017 return NULL;
4018 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4021 const struct got_error *
4022 got_worktree_schedule_add(struct got_worktree *worktree,
4023 struct got_pathlist_head *paths,
4024 got_worktree_checkout_cb progress_cb, void *progress_arg,
4025 struct got_repository *repo, int no_ignores)
4027 struct got_fileindex *fileindex = NULL;
4028 char *fileindex_path = NULL;
4029 const struct got_error *err = NULL, *sync_err, *unlockerr;
4030 struct got_pathlist_entry *pe;
4031 struct schedule_addition_args saa;
4033 err = lock_worktree(worktree, LOCK_EX);
4034 if (err)
4035 return err;
4037 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4038 if (err)
4039 goto done;
4041 saa.worktree = worktree;
4042 saa.fileindex = fileindex;
4043 saa.progress_cb = progress_cb;
4044 saa.progress_arg = progress_arg;
4045 saa.repo = repo;
4047 TAILQ_FOREACH(pe, paths, entry) {
4048 err = worktree_status(worktree, pe->path, fileindex, repo,
4049 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4050 if (err)
4051 break;
4053 sync_err = sync_fileindex(fileindex, fileindex_path);
4054 if (sync_err && err == NULL)
4055 err = sync_err;
4056 done:
4057 free(fileindex_path);
4058 if (fileindex)
4059 got_fileindex_free(fileindex);
4060 unlockerr = lock_worktree(worktree, LOCK_SH);
4061 if (unlockerr && err == NULL)
4062 err = unlockerr;
4063 return err;
4066 struct schedule_deletion_args {
4067 struct got_worktree *worktree;
4068 struct got_fileindex *fileindex;
4069 got_worktree_delete_cb progress_cb;
4070 void *progress_arg;
4071 struct got_repository *repo;
4072 int delete_local_mods;
4073 int keep_on_disk;
4074 int ignore_missing_paths;
4075 const char *status_codes;
4078 static const struct got_error *
4079 schedule_for_deletion(void *arg, unsigned char status,
4080 unsigned char staged_status, const char *relpath,
4081 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4082 struct got_object_id *commit_id, int dirfd, const char *de_name)
4084 struct schedule_deletion_args *a = arg;
4085 const struct got_error *err = NULL;
4086 struct got_fileindex_entry *ie = NULL;
4087 struct stat sb;
4088 char *ondisk_path;
4090 if (status == GOT_STATUS_NONEXISTENT) {
4091 if (a->ignore_missing_paths)
4092 return NULL;
4093 return got_error_set_errno(ENOENT, relpath);
4096 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4097 if (ie == NULL)
4098 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4100 staged_status = get_staged_status(ie);
4101 if (staged_status != GOT_STATUS_NO_CHANGE) {
4102 if (staged_status == GOT_STATUS_DELETE)
4103 return NULL;
4104 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4107 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4108 relpath) == -1)
4109 return got_error_from_errno("asprintf");
4111 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4112 a->repo);
4113 if (err)
4114 goto done;
4116 if (a->status_codes) {
4117 size_t ncodes = strlen(a->status_codes);
4118 int i;
4119 for (i = 0; i < ncodes ; i++) {
4120 if (status == a->status_codes[i])
4121 break;
4123 if (i == ncodes) {
4124 /* Do not delete files in non-matching status. */
4125 free(ondisk_path);
4126 return NULL;
4128 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4129 a->status_codes[i] != GOT_STATUS_MISSING) {
4130 static char msg[64];
4131 snprintf(msg, sizeof(msg),
4132 "invalid status code '%c'", a->status_codes[i]);
4133 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4134 goto done;
4138 if (status != GOT_STATUS_NO_CHANGE) {
4139 if (status == GOT_STATUS_DELETE)
4140 goto done;
4141 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4142 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4143 goto done;
4145 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4146 err = got_error_set_errno(ENOENT, relpath);
4147 goto done;
4149 if (status != GOT_STATUS_MODIFY &&
4150 status != GOT_STATUS_MISSING) {
4151 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4152 goto done;
4156 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4157 size_t root_len;
4159 if (dirfd != -1) {
4160 if (unlinkat(dirfd, de_name, 0) == -1) {
4161 err = got_error_from_errno2("unlinkat",
4162 ondisk_path);
4163 goto done;
4165 } else if (unlink(ondisk_path) == -1) {
4166 err = got_error_from_errno2("unlink", ondisk_path);
4167 goto done;
4170 root_len = strlen(a->worktree->root_path);
4171 do {
4172 char *parent;
4173 err = got_path_dirname(&parent, ondisk_path);
4174 if (err)
4175 goto done;
4176 free(ondisk_path);
4177 ondisk_path = parent;
4178 if (rmdir(ondisk_path) == -1) {
4179 if (errno != ENOTEMPTY)
4180 err = got_error_from_errno2("rmdir",
4181 ondisk_path);
4182 break;
4184 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4185 strlen(ondisk_path), root_len) != 0);
4188 got_fileindex_entry_mark_deleted_from_disk(ie);
4189 done:
4190 free(ondisk_path);
4191 if (err)
4192 return err;
4193 if (status == GOT_STATUS_DELETE)
4194 return NULL;
4195 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4196 staged_status, relpath);
4199 const struct got_error *
4200 got_worktree_schedule_delete(struct got_worktree *worktree,
4201 struct got_pathlist_head *paths, int delete_local_mods,
4202 const char *status_codes,
4203 got_worktree_delete_cb progress_cb, void *progress_arg,
4204 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4206 struct got_fileindex *fileindex = NULL;
4207 char *fileindex_path = NULL;
4208 const struct got_error *err = NULL, *sync_err, *unlockerr;
4209 struct got_pathlist_entry *pe;
4210 struct schedule_deletion_args sda;
4212 err = lock_worktree(worktree, LOCK_EX);
4213 if (err)
4214 return err;
4216 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4217 if (err)
4218 goto done;
4220 sda.worktree = worktree;
4221 sda.fileindex = fileindex;
4222 sda.progress_cb = progress_cb;
4223 sda.progress_arg = progress_arg;
4224 sda.repo = repo;
4225 sda.delete_local_mods = delete_local_mods;
4226 sda.keep_on_disk = keep_on_disk;
4227 sda.ignore_missing_paths = ignore_missing_paths;
4228 sda.status_codes = status_codes;
4230 TAILQ_FOREACH(pe, paths, entry) {
4231 err = worktree_status(worktree, pe->path, fileindex, repo,
4232 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4233 if (err)
4234 break;
4236 sync_err = sync_fileindex(fileindex, fileindex_path);
4237 if (sync_err && err == NULL)
4238 err = sync_err;
4239 done:
4240 free(fileindex_path);
4241 if (fileindex)
4242 got_fileindex_free(fileindex);
4243 unlockerr = lock_worktree(worktree, LOCK_SH);
4244 if (unlockerr && err == NULL)
4245 err = unlockerr;
4246 return err;
4249 static const struct got_error *
4250 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4252 const struct got_error *err = NULL;
4253 char *line = NULL;
4254 size_t linesize = 0, n;
4255 ssize_t linelen;
4257 linelen = getline(&line, &linesize, infile);
4258 if (linelen == -1) {
4259 if (ferror(infile)) {
4260 err = got_error_from_errno("getline");
4261 goto done;
4263 return NULL;
4265 if (outfile) {
4266 n = fwrite(line, 1, linelen, outfile);
4267 if (n != linelen) {
4268 err = got_ferror(outfile, GOT_ERR_IO);
4269 goto done;
4272 if (rejectfile) {
4273 n = fwrite(line, 1, linelen, rejectfile);
4274 if (n != linelen)
4275 err = got_ferror(rejectfile, GOT_ERR_IO);
4277 done:
4278 free(line);
4279 return err;
4282 static const struct got_error *
4283 skip_one_line(FILE *f)
4285 char *line = NULL;
4286 size_t linesize = 0;
4287 ssize_t linelen;
4289 linelen = getline(&line, &linesize, f);
4290 if (linelen == -1) {
4291 if (ferror(f))
4292 return got_error_from_errno("getline");
4293 return NULL;
4295 free(line);
4296 return NULL;
4299 static const struct got_error *
4300 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4301 int start_old, int end_old, int start_new, int end_new,
4302 FILE *outfile, FILE *rejectfile)
4304 const struct got_error *err;
4306 /* Copy old file's lines leading up to patch. */
4307 while (!feof(f1) && *line_cur1 < start_old) {
4308 err = copy_one_line(f1, outfile, NULL);
4309 if (err)
4310 return err;
4311 (*line_cur1)++;
4313 /* Skip new file's lines leading up to patch. */
4314 while (!feof(f2) && *line_cur2 < start_new) {
4315 if (rejectfile)
4316 err = copy_one_line(f2, NULL, rejectfile);
4317 else
4318 err = skip_one_line(f2);
4319 if (err)
4320 return err;
4321 (*line_cur2)++;
4323 /* Copy patched lines. */
4324 while (!feof(f2) && *line_cur2 <= end_new) {
4325 err = copy_one_line(f2, outfile, NULL);
4326 if (err)
4327 return err;
4328 (*line_cur2)++;
4330 /* Skip over old file's replaced lines. */
4331 while (!feof(f1) && *line_cur1 <= end_old) {
4332 if (rejectfile)
4333 err = copy_one_line(f1, NULL, rejectfile);
4334 else
4335 err = skip_one_line(f1);
4336 if (err)
4337 return err;
4338 (*line_cur1)++;
4341 return NULL;
4344 static const struct got_error *
4345 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4346 FILE *outfile, FILE *rejectfile)
4348 const struct got_error *err;
4350 if (outfile) {
4351 /* Copy old file's lines until EOF. */
4352 while (!feof(f1)) {
4353 err = copy_one_line(f1, outfile, NULL);
4354 if (err)
4355 return err;
4356 (*line_cur1)++;
4359 if (rejectfile) {
4360 /* Copy new file's lines until EOF. */
4361 while (!feof(f2)) {
4362 err = copy_one_line(f2, NULL, rejectfile);
4363 if (err)
4364 return err;
4365 (*line_cur2)++;
4369 return NULL;
4372 static const struct got_error *
4373 apply_or_reject_change(int *choice, int *nchunks_used,
4374 struct diff_result *diff_result, int n,
4375 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4376 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4377 got_worktree_patch_cb patch_cb, void *patch_arg)
4379 const struct got_error *err = NULL;
4380 struct diff_chunk_context cc = {};
4381 int start_old, end_old, start_new, end_new;
4382 FILE *hunkfile;
4383 struct diff_output_unidiff_state *diff_state;
4384 struct diff_input_info diff_info;
4385 int rc;
4387 *choice = GOT_PATCH_CHOICE_NONE;
4389 /* Get changed line numbers without context lines for copy_change(). */
4390 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4391 start_old = cc.left.start;
4392 end_old = cc.left.end;
4393 start_new = cc.right.start;
4394 end_new = cc.right.end;
4396 /* Get the same change with context lines for display. */
4397 memset(&cc, 0, sizeof(cc));
4398 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4400 memset(&diff_info, 0, sizeof(diff_info));
4401 diff_info.left_path = relpath;
4402 diff_info.right_path = relpath;
4404 diff_state = diff_output_unidiff_state_alloc();
4405 if (diff_state == NULL)
4406 return got_error_set_errno(ENOMEM,
4407 "diff_output_unidiff_state_alloc");
4409 hunkfile = got_opentemp();
4410 if (hunkfile == NULL) {
4411 err = got_error_from_errno("got_opentemp");
4412 goto done;
4415 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4416 diff_result, &cc);
4417 if (rc != DIFF_RC_OK) {
4418 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4419 goto done;
4422 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4423 err = got_ferror(hunkfile, GOT_ERR_IO);
4424 goto done;
4427 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4428 hunkfile, changeno, nchanges);
4429 if (err)
4430 goto done;
4432 switch (*choice) {
4433 case GOT_PATCH_CHOICE_YES:
4434 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4435 end_old, start_new, end_new, outfile, rejectfile);
4436 break;
4437 case GOT_PATCH_CHOICE_NO:
4438 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4439 end_old, start_new, end_new, rejectfile, outfile);
4440 break;
4441 case GOT_PATCH_CHOICE_QUIT:
4442 break;
4443 default:
4444 err = got_error(GOT_ERR_PATCH_CHOICE);
4445 break;
4447 done:
4448 diff_output_unidiff_state_free(diff_state);
4449 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4450 err = got_error_from_errno("fclose");
4451 return err;
4454 struct revert_file_args {
4455 struct got_worktree *worktree;
4456 struct got_fileindex *fileindex;
4457 got_worktree_checkout_cb progress_cb;
4458 void *progress_arg;
4459 got_worktree_patch_cb patch_cb;
4460 void *patch_arg;
4461 struct got_repository *repo;
4462 int unlink_added_files;
4465 static const struct got_error *
4466 create_patched_content(char **path_outfile, int reverse_patch,
4467 struct got_object_id *blob_id, const char *path2,
4468 int dirfd2, const char *de_name2,
4469 const char *relpath, struct got_repository *repo,
4470 got_worktree_patch_cb patch_cb, void *patch_arg)
4472 const struct got_error *err, *free_err;
4473 struct got_blob_object *blob = NULL;
4474 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4475 int fd = -1, fd2 = -1;
4476 char link_target[PATH_MAX];
4477 ssize_t link_len = 0;
4478 char *path1 = NULL, *id_str = NULL;
4479 struct stat sb2;
4480 struct got_diffreg_result *diffreg_result = NULL;
4481 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4482 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4484 *path_outfile = NULL;
4486 err = got_object_id_str(&id_str, blob_id);
4487 if (err)
4488 return err;
4490 if (dirfd2 != -1) {
4491 fd2 = openat(dirfd2, de_name2,
4492 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4493 if (fd2 == -1) {
4494 if (!got_err_open_nofollow_on_symlink()) {
4495 err = got_error_from_errno2("openat", path2);
4496 goto done;
4498 link_len = readlinkat(dirfd2, de_name2,
4499 link_target, sizeof(link_target));
4500 if (link_len == -1) {
4501 return got_error_from_errno2("readlinkat",
4502 path2);
4504 sb2.st_mode = S_IFLNK;
4505 sb2.st_size = link_len;
4507 } else {
4508 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4509 if (fd2 == -1) {
4510 if (!got_err_open_nofollow_on_symlink()) {
4511 err = got_error_from_errno2("open", path2);
4512 goto done;
4514 link_len = readlink(path2, link_target,
4515 sizeof(link_target));
4516 if (link_len == -1)
4517 return got_error_from_errno2("readlink", path2);
4518 sb2.st_mode = S_IFLNK;
4519 sb2.st_size = link_len;
4522 if (fd2 != -1) {
4523 if (fstat(fd2, &sb2) == -1) {
4524 err = got_error_from_errno2("fstat", path2);
4525 goto done;
4528 f2 = fdopen(fd2, "r");
4529 if (f2 == NULL) {
4530 err = got_error_from_errno2("fdopen", path2);
4531 goto done;
4533 fd2 = -1;
4534 } else {
4535 size_t n;
4536 f2 = got_opentemp();
4537 if (f2 == NULL) {
4538 err = got_error_from_errno2("got_opentemp", path2);
4539 goto done;
4541 n = fwrite(link_target, 1, link_len, f2);
4542 if (n != link_len) {
4543 err = got_ferror(f2, GOT_ERR_IO);
4544 goto done;
4546 if (fflush(f2) == EOF) {
4547 err = got_error_from_errno("fflush");
4548 goto done;
4550 rewind(f2);
4553 fd = got_opentempfd();
4554 if (fd == -1) {
4555 err = got_error_from_errno("got_opentempfd");
4556 goto done;
4559 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4560 if (err)
4561 goto done;
4563 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4564 if (err)
4565 goto done;
4567 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4568 if (err)
4569 goto done;
4571 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4572 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4573 if (err)
4574 goto done;
4576 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4577 "");
4578 if (err)
4579 goto done;
4581 if (fseek(f1, 0L, SEEK_SET) == -1)
4582 return got_ferror(f1, GOT_ERR_IO);
4583 if (fseek(f2, 0L, SEEK_SET) == -1)
4584 return got_ferror(f2, GOT_ERR_IO);
4586 /* Count the number of actual changes in the diff result. */
4587 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4588 struct diff_chunk_context cc = {};
4589 diff_chunk_context_load_change(&cc, &nchunks_used,
4590 diffreg_result->result, n, 0);
4591 nchanges++;
4593 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4594 int choice;
4595 err = apply_or_reject_change(&choice, &nchunks_used,
4596 diffreg_result->result, n, relpath, f1, f2,
4597 &line_cur1, &line_cur2,
4598 reverse_patch ? NULL : outfile,
4599 reverse_patch ? outfile : NULL,
4600 ++i, nchanges, patch_cb, patch_arg);
4601 if (err)
4602 goto done;
4603 if (choice == GOT_PATCH_CHOICE_YES)
4604 have_content = 1;
4605 else if (choice == GOT_PATCH_CHOICE_QUIT)
4606 break;
4608 if (have_content) {
4609 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4610 reverse_patch ? NULL : outfile,
4611 reverse_patch ? outfile : NULL);
4612 if (err)
4613 goto done;
4615 if (!S_ISLNK(sb2.st_mode)) {
4616 mode_t mode;
4618 mode = apply_umask(sb2.st_mode);
4619 if (fchmod(fileno(outfile), mode) == -1) {
4620 err = got_error_from_errno2("fchmod", path2);
4621 goto done;
4625 done:
4626 free(id_str);
4627 if (fd != -1 && close(fd) == -1 && err == NULL)
4628 err = got_error_from_errno("close");
4629 if (blob)
4630 got_object_blob_close(blob);
4631 free_err = got_diffreg_result_free(diffreg_result);
4632 if (err == NULL)
4633 err = free_err;
4634 if (f1 && fclose(f1) == EOF && err == NULL)
4635 err = got_error_from_errno2("fclose", path1);
4636 if (f2 && fclose(f2) == EOF && err == NULL)
4637 err = got_error_from_errno2("fclose", path2);
4638 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4639 err = got_error_from_errno2("close", path2);
4640 if (outfile && fclose(outfile) == EOF && err == NULL)
4641 err = got_error_from_errno2("fclose", *path_outfile);
4642 if (path1 && unlink(path1) == -1 && err == NULL)
4643 err = got_error_from_errno2("unlink", path1);
4644 if (err || !have_content) {
4645 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4646 err = got_error_from_errno2("unlink", *path_outfile);
4647 free(*path_outfile);
4648 *path_outfile = NULL;
4650 free(path1);
4651 return err;
4654 static const struct got_error *
4655 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4656 const char *relpath, struct got_object_id *blob_id,
4657 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4658 int dirfd, const char *de_name)
4660 struct revert_file_args *a = arg;
4661 const struct got_error *err = NULL;
4662 char *parent_path = NULL;
4663 struct got_fileindex_entry *ie;
4664 struct got_commit_object *base_commit = NULL;
4665 struct got_tree_object *tree = NULL;
4666 struct got_object_id *tree_id = NULL;
4667 const struct got_tree_entry *te = NULL;
4668 char *tree_path = NULL, *te_name;
4669 char *ondisk_path = NULL, *path_content = NULL;
4670 struct got_blob_object *blob = NULL;
4671 int fd = -1;
4673 /* Reverting a staged deletion is a no-op. */
4674 if (status == GOT_STATUS_DELETE &&
4675 staged_status != GOT_STATUS_NO_CHANGE)
4676 return NULL;
4678 if (status == GOT_STATUS_UNVERSIONED)
4679 return (*a->progress_cb)(a->progress_arg,
4680 GOT_STATUS_UNVERSIONED, relpath);
4682 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4683 if (ie == NULL)
4684 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4686 /* Construct in-repository path of tree which contains this blob. */
4687 err = got_path_dirname(&parent_path, ie->path);
4688 if (err) {
4689 if (err->code != GOT_ERR_BAD_PATH)
4690 goto done;
4691 parent_path = strdup("/");
4692 if (parent_path == NULL) {
4693 err = got_error_from_errno("strdup");
4694 goto done;
4697 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4698 tree_path = strdup(parent_path);
4699 if (tree_path == NULL) {
4700 err = got_error_from_errno("strdup");
4701 goto done;
4703 } else {
4704 if (got_path_is_root_dir(parent_path)) {
4705 tree_path = strdup(a->worktree->path_prefix);
4706 if (tree_path == NULL) {
4707 err = got_error_from_errno("strdup");
4708 goto done;
4710 } else {
4711 if (asprintf(&tree_path, "%s/%s",
4712 a->worktree->path_prefix, parent_path) == -1) {
4713 err = got_error_from_errno("asprintf");
4714 goto done;
4719 err = got_object_open_as_commit(&base_commit, a->repo,
4720 a->worktree->base_commit_id);
4721 if (err)
4722 goto done;
4724 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4725 if (err) {
4726 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4727 (status == GOT_STATUS_ADD ||
4728 staged_status == GOT_STATUS_ADD)))
4729 goto done;
4730 } else {
4731 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4732 if (err)
4733 goto done;
4735 err = got_path_basename(&te_name, ie->path);
4736 if (err)
4737 goto done;
4739 te = got_object_tree_find_entry(tree, te_name);
4740 free(te_name);
4741 if (te == NULL && status != GOT_STATUS_ADD &&
4742 staged_status != GOT_STATUS_ADD) {
4743 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4744 goto done;
4748 switch (status) {
4749 case GOT_STATUS_ADD:
4750 if (a->patch_cb) {
4751 int choice = GOT_PATCH_CHOICE_NONE;
4752 err = (*a->patch_cb)(&choice, a->patch_arg,
4753 status, ie->path, NULL, 1, 1);
4754 if (err)
4755 goto done;
4756 if (choice != GOT_PATCH_CHOICE_YES)
4757 break;
4759 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4760 ie->path);
4761 if (err)
4762 goto done;
4763 got_fileindex_entry_remove(a->fileindex, ie);
4764 if (a->unlink_added_files) {
4765 if (asprintf(&ondisk_path, "%s/%s",
4766 got_worktree_get_root_path(a->worktree),
4767 relpath) == -1) {
4768 err = got_error_from_errno("asprintf");
4769 goto done;
4771 if (unlink(ondisk_path) == -1) {
4772 err = got_error_from_errno2("unlink",
4773 ondisk_path);
4774 break;
4777 break;
4778 case GOT_STATUS_DELETE:
4779 if (a->patch_cb) {
4780 int choice = GOT_PATCH_CHOICE_NONE;
4781 err = (*a->patch_cb)(&choice, a->patch_arg,
4782 status, ie->path, NULL, 1, 1);
4783 if (err)
4784 goto done;
4785 if (choice != GOT_PATCH_CHOICE_YES)
4786 break;
4788 /* fall through */
4789 case GOT_STATUS_MODIFY:
4790 case GOT_STATUS_MODE_CHANGE:
4791 case GOT_STATUS_CONFLICT:
4792 case GOT_STATUS_MISSING: {
4793 struct got_object_id id;
4794 if (staged_status == GOT_STATUS_ADD ||
4795 staged_status == GOT_STATUS_MODIFY) {
4796 memcpy(id.hash, ie->staged_blob_hash,
4797 SHA1_DIGEST_LENGTH);
4798 } else
4799 memcpy(id.hash, ie->blob_hash,
4800 SHA1_DIGEST_LENGTH);
4801 id.algo = got_repo_get_hash_algorithm(a->repo);
4803 fd = got_opentempfd();
4804 if (fd == -1) {
4805 err = got_error_from_errno("got_opentempfd");
4806 goto done;
4809 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4810 if (err)
4811 goto done;
4813 if (asprintf(&ondisk_path, "%s/%s",
4814 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4815 err = got_error_from_errno("asprintf");
4816 goto done;
4819 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4820 status == GOT_STATUS_CONFLICT)) {
4821 int is_bad_symlink = 0;
4822 err = create_patched_content(&path_content, 1, &id,
4823 ondisk_path, dirfd, de_name, ie->path, a->repo,
4824 a->patch_cb, a->patch_arg);
4825 if (err || path_content == NULL)
4826 break;
4827 if (te && S_ISLNK(te->mode)) {
4828 if (unlink(path_content) == -1) {
4829 err = got_error_from_errno2("unlink",
4830 path_content);
4831 break;
4833 err = install_symlink(&is_bad_symlink,
4834 a->worktree, ondisk_path, ie->path,
4835 blob, 0, 1, 0, 0, a->repo,
4836 a->progress_cb, a->progress_arg);
4837 } else {
4838 if (rename(path_content, ondisk_path) == -1) {
4839 err = got_error_from_errno3("rename",
4840 path_content, ondisk_path);
4841 goto done;
4844 } else {
4845 int is_bad_symlink = 0;
4846 if (te && S_ISLNK(te->mode)) {
4847 err = install_symlink(&is_bad_symlink,
4848 a->worktree, ondisk_path, ie->path,
4849 blob, 0, 1, 0, 0, a->repo,
4850 a->progress_cb, a->progress_arg);
4851 } else {
4852 err = install_blob(a->worktree, ondisk_path,
4853 ie->path,
4854 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4855 got_fileindex_perms_to_st(ie), blob,
4856 0, 1, 0, 0, a->repo,
4857 a->progress_cb, a->progress_arg);
4859 if (err)
4860 goto done;
4861 if (status == GOT_STATUS_DELETE ||
4862 status == GOT_STATUS_MODE_CHANGE) {
4863 err = got_fileindex_entry_update(ie,
4864 a->worktree->root_fd, relpath,
4865 blob->id.hash,
4866 a->worktree->base_commit_id->hash, 1);
4867 if (err)
4868 goto done;
4870 if (is_bad_symlink) {
4871 got_fileindex_entry_filetype_set(ie,
4872 GOT_FILEIDX_MODE_BAD_SYMLINK);
4875 break;
4877 default:
4878 break;
4880 done:
4881 free(ondisk_path);
4882 free(path_content);
4883 free(parent_path);
4884 free(tree_path);
4885 if (fd != -1 && close(fd) == -1 && err == NULL)
4886 err = got_error_from_errno("close");
4887 if (blob)
4888 got_object_blob_close(blob);
4889 if (tree)
4890 got_object_tree_close(tree);
4891 free(tree_id);
4892 if (base_commit)
4893 got_object_commit_close(base_commit);
4894 return err;
4897 const struct got_error *
4898 got_worktree_revert(struct got_worktree *worktree,
4899 struct got_pathlist_head *paths,
4900 got_worktree_checkout_cb progress_cb, void *progress_arg,
4901 got_worktree_patch_cb patch_cb, void *patch_arg,
4902 struct got_repository *repo)
4904 struct got_fileindex *fileindex = NULL;
4905 char *fileindex_path = NULL;
4906 const struct got_error *err = NULL, *unlockerr = NULL;
4907 const struct got_error *sync_err = NULL;
4908 struct got_pathlist_entry *pe;
4909 struct revert_file_args rfa;
4911 err = lock_worktree(worktree, LOCK_EX);
4912 if (err)
4913 return err;
4915 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4916 if (err)
4917 goto done;
4919 rfa.worktree = worktree;
4920 rfa.fileindex = fileindex;
4921 rfa.progress_cb = progress_cb;
4922 rfa.progress_arg = progress_arg;
4923 rfa.patch_cb = patch_cb;
4924 rfa.patch_arg = patch_arg;
4925 rfa.repo = repo;
4926 rfa.unlink_added_files = 0;
4927 TAILQ_FOREACH(pe, paths, entry) {
4928 err = worktree_status(worktree, pe->path, fileindex, repo,
4929 revert_file, &rfa, NULL, NULL, 1, 0);
4930 if (err)
4931 break;
4933 sync_err = sync_fileindex(fileindex, fileindex_path);
4934 if (sync_err && err == NULL)
4935 err = sync_err;
4936 done:
4937 free(fileindex_path);
4938 if (fileindex)
4939 got_fileindex_free(fileindex);
4940 unlockerr = lock_worktree(worktree, LOCK_SH);
4941 if (unlockerr && err == NULL)
4942 err = unlockerr;
4943 return err;
4946 static void
4947 free_commitable(struct got_commitable *ct)
4949 free(ct->path);
4950 free(ct->in_repo_path);
4951 free(ct->ondisk_path);
4952 free(ct->blob_id);
4953 free(ct->base_blob_id);
4954 free(ct->staged_blob_id);
4955 free(ct->base_commit_id);
4956 free(ct);
4959 struct collect_commitables_arg {
4960 struct got_pathlist_head *commitable_paths;
4961 struct got_repository *repo;
4962 struct got_worktree *worktree;
4963 struct got_fileindex *fileindex;
4964 int have_staged_files;
4965 int allow_bad_symlinks;
4966 int diff_header_shown;
4967 FILE *diff_outfile;
4968 FILE *f1;
4969 FILE *f2;
4973 * Create a file which contains the target path of a symlink so we can feed
4974 * it as content to the diff engine.
4976 static const struct got_error *
4977 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4978 const char *abspath)
4980 const struct got_error *err = NULL;
4981 char target_path[PATH_MAX];
4982 ssize_t target_len, outlen;
4984 *fd = -1;
4986 if (dirfd != -1) {
4987 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4988 if (target_len == -1)
4989 return got_error_from_errno2("readlinkat", abspath);
4990 } else {
4991 target_len = readlink(abspath, target_path, PATH_MAX);
4992 if (target_len == -1)
4993 return got_error_from_errno2("readlink", abspath);
4996 *fd = got_opentempfd();
4997 if (*fd == -1)
4998 return got_error_from_errno("got_opentempfd");
5000 outlen = write(*fd, target_path, target_len);
5001 if (outlen == -1) {
5002 err = got_error_from_errno("got_opentempfd");
5003 goto done;
5006 if (lseek(*fd, 0, SEEK_SET) == -1) {
5007 err = got_error_from_errno2("lseek", abspath);
5008 goto done;
5010 done:
5011 if (err) {
5012 close(*fd);
5013 *fd = -1;
5015 return err;
5018 static const struct got_error *
5019 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5020 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5021 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5023 const struct got_error *err = NULL;
5024 struct got_blob_object *blob1 = NULL;
5025 int fd = -1, fd1 = -1, fd2 = -1;
5026 FILE *ondisk_file = NULL;
5027 char *label1 = NULL;
5028 struct stat sb;
5029 off_t size1 = 0;
5030 int f2_exists = 0;
5031 char *id_str = NULL;
5033 memset(&sb, 0, sizeof(sb));
5035 if (diff_staged) {
5036 if (ct->staged_status != GOT_STATUS_MODIFY &&
5037 ct->staged_status != GOT_STATUS_ADD &&
5038 ct->staged_status != GOT_STATUS_DELETE)
5039 return NULL;
5040 } else {
5041 if (ct->status != GOT_STATUS_MODIFY &&
5042 ct->status != GOT_STATUS_ADD &&
5043 ct->status != GOT_STATUS_DELETE)
5044 return NULL;
5047 err = got_opentemp_truncate(f1);
5048 if (err)
5049 return got_error_from_errno("got_opentemp_truncate");
5050 err = got_opentemp_truncate(f2);
5051 if (err)
5052 return got_error_from_errno("got_opentemp_truncate");
5054 if (!*diff_header_shown) {
5055 err = got_object_id_str(&id_str, worktree->base_commit_id);
5056 if (err)
5057 return err;
5058 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5059 got_worktree_get_root_path(worktree));
5060 fprintf(diff_outfile, "commit - %s\n", id_str);
5061 fprintf(diff_outfile, "path + %s%s\n",
5062 got_worktree_get_root_path(worktree),
5063 diff_staged ? " (staged changes)" : "");
5064 *diff_header_shown = 1;
5067 if (diff_staged) {
5068 const char *label1 = NULL, *label2 = NULL;
5069 switch (ct->staged_status) {
5070 case GOT_STATUS_MODIFY:
5071 label1 = ct->path;
5072 label2 = ct->path;
5073 break;
5074 case GOT_STATUS_ADD:
5075 label2 = ct->path;
5076 break;
5077 case GOT_STATUS_DELETE:
5078 label1 = ct->path;
5079 break;
5080 default:
5081 return got_error(GOT_ERR_FILE_STATUS);
5083 fd1 = got_opentempfd();
5084 if (fd1 == -1) {
5085 err = got_error_from_errno("got_opentempfd");
5086 goto done;
5088 fd2 = got_opentempfd();
5089 if (fd2 == -1) {
5090 err = got_error_from_errno("got_opentempfd");
5091 goto done;
5093 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5094 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5095 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5096 NULL, repo, diff_outfile);
5097 goto done;
5100 fd1 = got_opentempfd();
5101 if (fd1 == -1) {
5102 err = got_error_from_errno("got_opentempfd");
5103 goto done;
5106 if (ct->status != GOT_STATUS_ADD) {
5107 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5108 8192, fd1);
5109 if (err)
5110 goto done;
5113 if (ct->status != GOT_STATUS_DELETE) {
5114 if (dirfd != -1) {
5115 fd = openat(dirfd, de_name,
5116 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5117 if (fd == -1) {
5118 if (!got_err_open_nofollow_on_symlink()) {
5119 err = got_error_from_errno2("openat",
5120 ct->ondisk_path);
5121 goto done;
5123 err = get_symlink_target_file(&fd, dirfd,
5124 de_name, ct->ondisk_path);
5125 if (err)
5126 goto done;
5128 } else {
5129 fd = open(ct->ondisk_path,
5130 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5131 if (fd == -1) {
5132 if (!got_err_open_nofollow_on_symlink()) {
5133 err = got_error_from_errno2("open",
5134 ct->ondisk_path);
5135 goto done;
5137 err = get_symlink_target_file(&fd, dirfd,
5138 de_name, ct->ondisk_path);
5139 if (err)
5140 goto done;
5143 if (fstatat(fd, ct->ondisk_path, &sb,
5144 AT_SYMLINK_NOFOLLOW) == -1) {
5145 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5146 goto done;
5148 ondisk_file = fdopen(fd, "r");
5149 if (ondisk_file == NULL) {
5150 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5151 goto done;
5153 fd = -1;
5154 f2_exists = 1;
5157 if (blob1) {
5158 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5159 f1, blob1);
5160 if (err)
5161 goto done;
5164 err = got_diff_blob_file(blob1, f1, size1, label1,
5165 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5166 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5167 done:
5168 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5169 err = got_error_from_errno("close");
5170 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5171 err = got_error_from_errno("close");
5172 if (blob1)
5173 got_object_blob_close(blob1);
5174 if (fd != -1 && close(fd) == -1 && err == NULL)
5175 err = got_error_from_errno("close");
5176 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5177 err = got_error_from_errno("fclose");
5178 return err;
5181 static const struct got_error *
5182 collect_commitables(void *arg, unsigned char status,
5183 unsigned char staged_status, const char *relpath,
5184 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5185 struct got_object_id *commit_id, int dirfd, const char *de_name)
5187 struct collect_commitables_arg *a = arg;
5188 const struct got_error *err = NULL;
5189 struct got_commitable *ct = NULL;
5190 struct got_pathlist_entry *new = NULL;
5191 char *parent_path = NULL, *path = NULL;
5192 struct stat sb;
5194 if (a->have_staged_files) {
5195 if (staged_status != GOT_STATUS_MODIFY &&
5196 staged_status != GOT_STATUS_ADD &&
5197 staged_status != GOT_STATUS_DELETE)
5198 return NULL;
5199 } else {
5200 if (status == GOT_STATUS_CONFLICT)
5201 return got_error(GOT_ERR_COMMIT_CONFLICT);
5203 if (status != GOT_STATUS_MODIFY &&
5204 status != GOT_STATUS_MODE_CHANGE &&
5205 status != GOT_STATUS_ADD &&
5206 status != GOT_STATUS_DELETE)
5207 return NULL;
5210 if (asprintf(&path, "/%s", relpath) == -1) {
5211 err = got_error_from_errno("asprintf");
5212 goto done;
5214 if (strcmp(path, "/") == 0) {
5215 parent_path = strdup("");
5216 if (parent_path == NULL)
5217 return got_error_from_errno("strdup");
5218 } else {
5219 err = got_path_dirname(&parent_path, path);
5220 if (err)
5221 return err;
5224 ct = calloc(1, sizeof(*ct));
5225 if (ct == NULL) {
5226 err = got_error_from_errno("calloc");
5227 goto done;
5230 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5231 relpath) == -1) {
5232 err = got_error_from_errno("asprintf");
5233 goto done;
5236 if (staged_status == GOT_STATUS_ADD ||
5237 staged_status == GOT_STATUS_MODIFY) {
5238 struct got_fileindex_entry *ie;
5239 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5240 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5241 case GOT_FILEIDX_MODE_REGULAR_FILE:
5242 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5243 ct->mode = S_IFREG;
5244 break;
5245 case GOT_FILEIDX_MODE_SYMLINK:
5246 ct->mode = S_IFLNK;
5247 break;
5248 default:
5249 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5250 goto done;
5252 ct->mode |= got_fileindex_entry_perms_get(ie);
5253 } else if (status != GOT_STATUS_DELETE &&
5254 staged_status != GOT_STATUS_DELETE) {
5255 if (dirfd != -1) {
5256 if (fstatat(dirfd, de_name, &sb,
5257 AT_SYMLINK_NOFOLLOW) == -1) {
5258 err = got_error_from_errno2("fstatat",
5259 ct->ondisk_path);
5260 goto done;
5262 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5263 err = got_error_from_errno2("lstat", ct->ondisk_path);
5264 goto done;
5266 ct->mode = sb.st_mode;
5269 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5270 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5271 relpath) == -1) {
5272 err = got_error_from_errno("asprintf");
5273 goto done;
5276 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5277 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5278 int is_bad_symlink;
5279 char target_path[PATH_MAX];
5280 ssize_t target_len;
5281 target_len = readlink(ct->ondisk_path, target_path,
5282 sizeof(target_path));
5283 if (target_len == -1) {
5284 err = got_error_from_errno2("readlink",
5285 ct->ondisk_path);
5286 goto done;
5288 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5289 target_len, ct->ondisk_path, a->worktree->root_path);
5290 if (err)
5291 goto done;
5292 if (is_bad_symlink) {
5293 err = got_error_path(ct->ondisk_path,
5294 GOT_ERR_BAD_SYMLINK);
5295 goto done;
5300 ct->status = status;
5301 ct->staged_status = staged_status;
5302 ct->blob_id = NULL; /* will be filled in when blob gets created */
5303 if (ct->status != GOT_STATUS_ADD &&
5304 ct->staged_status != GOT_STATUS_ADD) {
5305 ct->base_blob_id = got_object_id_dup(blob_id);
5306 if (ct->base_blob_id == NULL) {
5307 err = got_error_from_errno("got_object_id_dup");
5308 goto done;
5310 ct->base_commit_id = got_object_id_dup(commit_id);
5311 if (ct->base_commit_id == NULL) {
5312 err = got_error_from_errno("got_object_id_dup");
5313 goto done;
5316 if (ct->staged_status == GOT_STATUS_ADD ||
5317 ct->staged_status == GOT_STATUS_MODIFY) {
5318 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5319 if (ct->staged_blob_id == NULL) {
5320 err = got_error_from_errno("got_object_id_dup");
5321 goto done;
5324 ct->path = strdup(path);
5325 if (ct->path == NULL) {
5326 err = got_error_from_errno("strdup");
5327 goto done;
5329 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5330 if (err)
5331 goto done;
5333 if (a->diff_outfile && ct && new != NULL) {
5334 err = append_ct_diff(ct, &a->diff_header_shown,
5335 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5336 a->have_staged_files, a->repo, a->worktree);
5337 if (err)
5338 goto done;
5340 done:
5341 if (ct && (err || new == NULL))
5342 free_commitable(ct);
5343 free(parent_path);
5344 free(path);
5345 return err;
5348 static const struct got_error *write_tree(struct got_object_id **, int *,
5349 struct got_tree_object *, const char *, struct got_pathlist_head *,
5350 got_worktree_status_cb status_cb, void *status_arg,
5351 struct got_repository *);
5353 static const struct got_error *
5354 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5355 struct got_tree_entry *te, const char *parent_path,
5356 struct got_pathlist_head *commitable_paths,
5357 got_worktree_status_cb status_cb, void *status_arg,
5358 struct got_repository *repo)
5360 const struct got_error *err = NULL;
5361 struct got_tree_object *subtree;
5362 char *subpath;
5364 if (asprintf(&subpath, "%s%s%s", parent_path,
5365 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5366 return got_error_from_errno("asprintf");
5368 err = got_object_open_as_tree(&subtree, repo, &te->id);
5369 if (err)
5370 return err;
5372 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5373 commitable_paths, status_cb, status_arg, repo);
5374 got_object_tree_close(subtree);
5375 free(subpath);
5376 return err;
5379 static const struct got_error *
5380 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5382 const struct got_error *err = NULL;
5383 char *ct_parent_path = NULL;
5385 *match = 0;
5387 if (strchr(ct->in_repo_path, '/') == NULL) {
5388 *match = got_path_is_root_dir(path);
5389 return NULL;
5392 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5393 if (err)
5394 return err;
5395 *match = (strcmp(path, ct_parent_path) == 0);
5396 free(ct_parent_path);
5397 return err;
5400 static mode_t
5401 get_ct_file_mode(struct got_commitable *ct)
5403 if (S_ISLNK(ct->mode))
5404 return S_IFLNK;
5406 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5409 static const struct got_error *
5410 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5411 struct got_tree_entry *te, struct got_commitable *ct)
5413 const struct got_error *err = NULL;
5415 *new_te = NULL;
5417 err = got_object_tree_entry_dup(new_te, te);
5418 if (err)
5419 goto done;
5421 (*new_te)->mode = get_ct_file_mode(ct);
5423 if (ct->staged_status == GOT_STATUS_MODIFY)
5424 memcpy(&(*new_te)->id, ct->staged_blob_id,
5425 sizeof((*new_te)->id));
5426 else
5427 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5428 done:
5429 if (err && *new_te) {
5430 free(*new_te);
5431 *new_te = NULL;
5433 return err;
5436 static const struct got_error *
5437 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5438 struct got_commitable *ct)
5440 const struct got_error *err = NULL;
5441 char *ct_name = NULL;
5443 *new_te = NULL;
5445 *new_te = calloc(1, sizeof(**new_te));
5446 if (*new_te == NULL)
5447 return got_error_from_errno("calloc");
5449 err = got_path_basename(&ct_name, ct->path);
5450 if (err)
5451 goto done;
5452 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5453 sizeof((*new_te)->name)) {
5454 err = got_error(GOT_ERR_NO_SPACE);
5455 goto done;
5458 (*new_te)->mode = get_ct_file_mode(ct);
5460 if (ct->staged_status == GOT_STATUS_ADD)
5461 memcpy(&(*new_te)->id, ct->staged_blob_id,
5462 sizeof((*new_te)->id));
5463 else
5464 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5465 done:
5466 free(ct_name);
5467 if (err && *new_te) {
5468 free(*new_te);
5469 *new_te = NULL;
5471 return err;
5474 static const struct got_error *
5475 insert_tree_entry(struct got_tree_entry *new_te,
5476 struct got_pathlist_head *paths)
5478 const struct got_error *err = NULL;
5479 struct got_pathlist_entry *new_pe;
5481 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5482 if (err)
5483 return err;
5484 if (new_pe == NULL)
5485 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5486 return NULL;
5489 static const struct got_error *
5490 report_ct_status(struct got_commitable *ct,
5491 got_worktree_status_cb status_cb, void *status_arg)
5493 const char *ct_path = ct->path;
5494 unsigned char status;
5496 if (status_cb == NULL) /* no commit progress output desired */
5497 return NULL;
5499 while (ct_path[0] == '/')
5500 ct_path++;
5502 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5503 status = ct->staged_status;
5504 else
5505 status = ct->status;
5507 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5508 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5511 static const struct got_error *
5512 match_modified_subtree(int *modified, struct got_tree_entry *te,
5513 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5515 const struct got_error *err = NULL;
5516 struct got_pathlist_entry *pe;
5517 char *te_path;
5519 *modified = 0;
5521 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5522 got_path_is_root_dir(base_tree_path) ? "" : "/",
5523 te->name) == -1)
5524 return got_error_from_errno("asprintf");
5526 TAILQ_FOREACH(pe, commitable_paths, entry) {
5527 struct got_commitable *ct = pe->data;
5528 *modified = got_path_is_child(ct->in_repo_path, te_path,
5529 strlen(te_path));
5530 if (*modified)
5531 break;
5534 free(te_path);
5535 return err;
5538 static const struct got_error *
5539 match_deleted_or_modified_ct(struct got_commitable **ctp,
5540 struct got_tree_entry *te, const char *base_tree_path,
5541 struct got_pathlist_head *commitable_paths)
5543 const struct got_error *err = NULL;
5544 struct got_pathlist_entry *pe;
5546 *ctp = NULL;
5548 TAILQ_FOREACH(pe, commitable_paths, entry) {
5549 struct got_commitable *ct = pe->data;
5550 char *ct_name = NULL;
5551 int path_matches;
5553 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5554 if (ct->status != GOT_STATUS_MODIFY &&
5555 ct->status != GOT_STATUS_MODE_CHANGE &&
5556 ct->status != GOT_STATUS_DELETE)
5557 continue;
5558 } else {
5559 if (ct->staged_status != GOT_STATUS_MODIFY &&
5560 ct->staged_status != GOT_STATUS_DELETE)
5561 continue;
5564 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5565 continue;
5567 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5568 if (err)
5569 return err;
5570 if (!path_matches)
5571 continue;
5573 err = got_path_basename(&ct_name, pe->path);
5574 if (err)
5575 return err;
5577 if (strcmp(te->name, ct_name) != 0) {
5578 free(ct_name);
5579 continue;
5581 free(ct_name);
5583 *ctp = ct;
5584 break;
5587 return err;
5590 static const struct got_error *
5591 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5592 const char *child_path, const char *path_base_tree,
5593 struct got_pathlist_head *commitable_paths,
5594 got_worktree_status_cb status_cb, void *status_arg,
5595 struct got_repository *repo)
5597 const struct got_error *err = NULL;
5598 struct got_tree_entry *new_te;
5599 char *subtree_path;
5600 struct got_object_id *id = NULL;
5601 int nentries;
5603 *new_tep = NULL;
5605 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5606 got_path_is_root_dir(path_base_tree) ? "" : "/",
5607 child_path) == -1)
5608 return got_error_from_errno("asprintf");
5610 new_te = calloc(1, sizeof(*new_te));
5611 if (new_te == NULL)
5612 return got_error_from_errno("calloc");
5613 new_te->mode = S_IFDIR;
5615 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5616 sizeof(new_te->name)) {
5617 err = got_error(GOT_ERR_NO_SPACE);
5618 goto done;
5620 err = write_tree(&id, &nentries, NULL, subtree_path,
5621 commitable_paths, status_cb, status_arg, repo);
5622 if (err) {
5623 free(new_te);
5624 goto done;
5626 memcpy(&new_te->id, id, sizeof(new_te->id));
5627 done:
5628 free(id);
5629 free(subtree_path);
5630 if (err == NULL)
5631 *new_tep = new_te;
5632 return err;
5635 static const struct got_error *
5636 write_tree(struct got_object_id **new_tree_id, int *nentries,
5637 struct got_tree_object *base_tree, const char *path_base_tree,
5638 struct got_pathlist_head *commitable_paths,
5639 got_worktree_status_cb status_cb, void *status_arg,
5640 struct got_repository *repo)
5642 const struct got_error *err = NULL;
5643 struct got_pathlist_head paths;
5644 struct got_tree_entry *te, *new_te = NULL;
5645 struct got_pathlist_entry *pe;
5647 TAILQ_INIT(&paths);
5648 *nentries = 0;
5650 /* Insert, and recurse into, newly added entries first. */
5651 TAILQ_FOREACH(pe, commitable_paths, entry) {
5652 struct got_commitable *ct = pe->data;
5653 char *child_path = NULL, *slash;
5655 if ((ct->status != GOT_STATUS_ADD &&
5656 ct->staged_status != GOT_STATUS_ADD) ||
5657 (ct->flags & GOT_COMMITABLE_ADDED))
5658 continue;
5660 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5661 strlen(path_base_tree)))
5662 continue;
5664 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5665 ct->in_repo_path);
5666 if (err)
5667 goto done;
5669 slash = strchr(child_path, '/');
5670 if (slash == NULL) {
5671 err = alloc_added_blob_tree_entry(&new_te, ct);
5672 if (err)
5673 goto done;
5674 err = report_ct_status(ct, status_cb, status_arg);
5675 if (err)
5676 goto done;
5677 ct->flags |= GOT_COMMITABLE_ADDED;
5678 err = insert_tree_entry(new_te, &paths);
5679 if (err)
5680 goto done;
5681 (*nentries)++;
5682 } else {
5683 *slash = '\0'; /* trim trailing path components */
5684 if (base_tree == NULL ||
5685 got_object_tree_find_entry(base_tree, child_path)
5686 == NULL) {
5687 err = make_subtree_for_added_blob(&new_te,
5688 child_path, path_base_tree,
5689 commitable_paths, status_cb, status_arg,
5690 repo);
5691 if (err)
5692 goto done;
5693 err = insert_tree_entry(new_te, &paths);
5694 if (err)
5695 goto done;
5696 (*nentries)++;
5701 if (base_tree) {
5702 int i, nbase_entries;
5703 /* Handle modified and deleted entries. */
5704 nbase_entries = got_object_tree_get_nentries(base_tree);
5705 for (i = 0; i < nbase_entries; i++) {
5706 struct got_commitable *ct = NULL;
5708 te = got_object_tree_get_entry(base_tree, i);
5709 if (got_object_tree_entry_is_submodule(te)) {
5710 /* Entry is a submodule; just copy it. */
5711 err = got_object_tree_entry_dup(&new_te, te);
5712 if (err)
5713 goto done;
5714 err = insert_tree_entry(new_te, &paths);
5715 if (err)
5716 goto done;
5717 (*nentries)++;
5718 continue;
5721 if (S_ISDIR(te->mode)) {
5722 int modified;
5723 err = got_object_tree_entry_dup(&new_te, te);
5724 if (err)
5725 goto done;
5726 err = match_modified_subtree(&modified, te,
5727 path_base_tree, commitable_paths);
5728 if (err)
5729 goto done;
5730 /* Avoid recursion into unmodified subtrees. */
5731 if (modified) {
5732 struct got_object_id *new_id;
5733 int nsubentries;
5734 err = write_subtree(&new_id,
5735 &nsubentries, te,
5736 path_base_tree, commitable_paths,
5737 status_cb, status_arg, repo);
5738 if (err)
5739 goto done;
5740 if (nsubentries == 0) {
5741 /* All entries were deleted. */
5742 free(new_id);
5743 continue;
5745 memcpy(&new_te->id, new_id,
5746 sizeof(new_te->id));
5747 free(new_id);
5749 err = insert_tree_entry(new_te, &paths);
5750 if (err)
5751 goto done;
5752 (*nentries)++;
5753 continue;
5756 err = match_deleted_or_modified_ct(&ct, te,
5757 path_base_tree, commitable_paths);
5758 if (err)
5759 goto done;
5760 if (ct) {
5761 /* NB: Deleted entries get dropped here. */
5762 if (ct->status == GOT_STATUS_MODIFY ||
5763 ct->status == GOT_STATUS_MODE_CHANGE ||
5764 ct->staged_status == GOT_STATUS_MODIFY) {
5765 err = alloc_modified_blob_tree_entry(
5766 &new_te, te, ct);
5767 if (err)
5768 goto done;
5769 err = insert_tree_entry(new_te, &paths);
5770 if (err)
5771 goto done;
5772 (*nentries)++;
5774 err = report_ct_status(ct, status_cb,
5775 status_arg);
5776 if (err)
5777 goto done;
5778 } else {
5779 /* Entry is unchanged; just copy it. */
5780 err = got_object_tree_entry_dup(&new_te, te);
5781 if (err)
5782 goto done;
5783 err = insert_tree_entry(new_te, &paths);
5784 if (err)
5785 goto done;
5786 (*nentries)++;
5791 /* Write new list of entries; deleted entries have been dropped. */
5792 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5793 done:
5794 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5795 return err;
5798 static const struct got_error *
5799 update_fileindex_after_commit(struct got_worktree *worktree,
5800 struct got_pathlist_head *commitable_paths,
5801 struct got_object_id *new_base_commit_id,
5802 struct got_fileindex *fileindex, int have_staged_files)
5804 const struct got_error *err = NULL;
5805 struct got_pathlist_entry *pe;
5806 char *relpath = NULL;
5808 TAILQ_FOREACH(pe, commitable_paths, entry) {
5809 struct got_fileindex_entry *ie;
5810 struct got_commitable *ct = pe->data;
5812 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5814 err = got_path_skip_common_ancestor(&relpath,
5815 worktree->root_path, ct->ondisk_path);
5816 if (err)
5817 goto done;
5819 if (ie) {
5820 if (ct->status == GOT_STATUS_DELETE ||
5821 ct->staged_status == GOT_STATUS_DELETE) {
5822 got_fileindex_entry_remove(fileindex, ie);
5823 } else if (ct->staged_status == GOT_STATUS_ADD ||
5824 ct->staged_status == GOT_STATUS_MODIFY) {
5825 got_fileindex_entry_stage_set(ie,
5826 GOT_FILEIDX_STAGE_NONE);
5827 got_fileindex_entry_staged_filetype_set(ie, 0);
5829 err = got_fileindex_entry_update(ie,
5830 worktree->root_fd, relpath,
5831 ct->staged_blob_id->hash,
5832 new_base_commit_id->hash,
5833 !have_staged_files);
5834 } else
5835 err = got_fileindex_entry_update(ie,
5836 worktree->root_fd, relpath,
5837 ct->blob_id->hash,
5838 new_base_commit_id->hash,
5839 !have_staged_files);
5840 } else {
5841 err = got_fileindex_entry_alloc(&ie, pe->path);
5842 if (err)
5843 goto done;
5844 err = got_fileindex_entry_update(ie,
5845 worktree->root_fd, relpath, ct->blob_id->hash,
5846 new_base_commit_id->hash, 1);
5847 if (err) {
5848 got_fileindex_entry_free(ie);
5849 goto done;
5851 err = got_fileindex_entry_add(fileindex, ie);
5852 if (err) {
5853 got_fileindex_entry_free(ie);
5854 goto done;
5857 free(relpath);
5858 relpath = NULL;
5860 done:
5861 free(relpath);
5862 return err;
5866 static const struct got_error *
5867 check_out_of_date(const char *in_repo_path, unsigned char status,
5868 unsigned char staged_status, struct got_object_id *base_blob_id,
5869 struct got_object_id *base_commit_id,
5870 struct got_object_id *head_commit_id, struct got_repository *repo,
5871 int ood_errcode)
5873 const struct got_error *err = NULL;
5874 struct got_commit_object *commit = NULL;
5875 struct got_object_id *id = NULL;
5877 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5878 /* Trivial case: base commit == head commit */
5879 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5880 return NULL;
5882 * Ensure file content which local changes were based
5883 * on matches file content in the branch head.
5885 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5886 if (err)
5887 goto done;
5888 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5889 if (err) {
5890 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5891 err = got_error(ood_errcode);
5892 goto done;
5893 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5894 err = got_error(ood_errcode);
5895 } else {
5896 /* Require that added files don't exist in the branch head. */
5897 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5898 if (err)
5899 goto done;
5900 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5901 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5902 goto done;
5903 err = id ? got_error(ood_errcode) : NULL;
5905 done:
5906 free(id);
5907 if (commit)
5908 got_object_commit_close(commit);
5909 return err;
5912 static const struct got_error *
5913 commit_worktree(struct got_object_id **new_commit_id,
5914 struct got_pathlist_head *commitable_paths,
5915 struct got_object_id *head_commit_id,
5916 struct got_object_id *parent_id2,
5917 struct got_worktree *worktree,
5918 const char *author, const char *committer, char *diff_path,
5919 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5920 got_worktree_status_cb status_cb, void *status_arg,
5921 struct got_repository *repo)
5923 const struct got_error *err = NULL, *unlockerr = NULL;
5924 struct got_pathlist_entry *pe;
5925 const char *head_ref_name = NULL;
5926 struct got_commit_object *head_commit = NULL;
5927 struct got_reference *head_ref2 = NULL;
5928 struct got_object_id *head_commit_id2 = NULL;
5929 struct got_tree_object *head_tree = NULL;
5930 struct got_object_id *new_tree_id = NULL;
5931 int nentries, nparents = 0;
5932 struct got_object_id_queue parent_ids;
5933 struct got_object_qid *pid = NULL;
5934 char *logmsg = NULL;
5935 time_t timestamp;
5937 *new_commit_id = NULL;
5939 STAILQ_INIT(&parent_ids);
5941 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5942 if (err)
5943 goto done;
5945 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5946 if (err)
5947 goto done;
5949 if (commit_msg_cb != NULL) {
5950 err = commit_msg_cb(commitable_paths, diff_path,
5951 &logmsg, commit_arg);
5952 if (err)
5953 goto done;
5956 if (logmsg == NULL || strlen(logmsg) == 0) {
5957 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5958 goto done;
5961 /* Create blobs from added and modified files and record their IDs. */
5962 TAILQ_FOREACH(pe, commitable_paths, entry) {
5963 struct got_commitable *ct = pe->data;
5964 char *ondisk_path;
5966 /* Blobs for staged files already exist. */
5967 if (ct->staged_status == GOT_STATUS_ADD ||
5968 ct->staged_status == GOT_STATUS_MODIFY)
5969 continue;
5971 if (ct->status != GOT_STATUS_ADD &&
5972 ct->status != GOT_STATUS_MODIFY &&
5973 ct->status != GOT_STATUS_MODE_CHANGE)
5974 continue;
5976 if (asprintf(&ondisk_path, "%s/%s",
5977 worktree->root_path, pe->path) == -1) {
5978 err = got_error_from_errno("asprintf");
5979 goto done;
5981 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5982 free(ondisk_path);
5983 if (err)
5984 goto done;
5987 /* Recursively write new tree objects. */
5988 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5989 commitable_paths, status_cb, status_arg, repo);
5990 if (err)
5991 goto done;
5993 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5994 if (err)
5995 goto done;
5996 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5997 nparents++;
5998 if (parent_id2) {
5999 err = got_object_qid_alloc(&pid, parent_id2);
6000 if (err)
6001 goto done;
6002 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6003 nparents++;
6005 timestamp = time(NULL);
6006 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6007 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6008 if (logmsg != NULL)
6009 free(logmsg);
6010 if (err)
6011 goto done;
6013 /* Check if a concurrent commit to our branch has occurred. */
6014 head_ref_name = got_worktree_get_head_ref_name(worktree);
6015 if (head_ref_name == NULL) {
6016 err = got_error_from_errno("got_worktree_get_head_ref_name");
6017 goto done;
6019 /* Lock the reference here to prevent concurrent modification. */
6020 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6021 if (err)
6022 goto done;
6023 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6024 if (err)
6025 goto done;
6026 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6027 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6028 goto done;
6030 /* Update branch head in repository. */
6031 err = got_ref_change_ref(head_ref2, *new_commit_id);
6032 if (err)
6033 goto done;
6034 err = got_ref_write(head_ref2, repo);
6035 if (err)
6036 goto done;
6038 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6039 if (err)
6040 goto done;
6042 err = ref_base_commit(worktree, repo);
6043 if (err)
6044 goto done;
6045 done:
6046 got_object_id_queue_free(&parent_ids);
6047 if (head_tree)
6048 got_object_tree_close(head_tree);
6049 if (head_commit)
6050 got_object_commit_close(head_commit);
6051 free(head_commit_id2);
6052 if (head_ref2) {
6053 unlockerr = got_ref_unlock(head_ref2);
6054 if (unlockerr && err == NULL)
6055 err = unlockerr;
6056 got_ref_close(head_ref2);
6058 return err;
6061 static const struct got_error *
6062 check_path_is_commitable(const char *path,
6063 struct got_pathlist_head *commitable_paths)
6065 struct got_pathlist_entry *cpe = NULL;
6066 size_t path_len = strlen(path);
6068 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6069 struct got_commitable *ct = cpe->data;
6070 const char *ct_path = ct->path;
6072 while (ct_path[0] == '/')
6073 ct_path++;
6075 if (strcmp(path, ct_path) == 0 ||
6076 got_path_is_child(ct_path, path, path_len))
6077 break;
6080 if (cpe == NULL)
6081 return got_error_path(path, GOT_ERR_BAD_PATH);
6083 return NULL;
6086 static const struct got_error *
6087 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6089 int *have_staged_files = arg;
6091 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6092 *have_staged_files = 1;
6093 return got_error(GOT_ERR_CANCELLED);
6096 return NULL;
6099 static const struct got_error *
6100 check_non_staged_files(struct got_fileindex *fileindex,
6101 struct got_pathlist_head *paths)
6103 struct got_pathlist_entry *pe;
6104 struct got_fileindex_entry *ie;
6106 TAILQ_FOREACH(pe, paths, entry) {
6107 if (pe->path[0] == '\0')
6108 continue;
6109 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6110 if (ie == NULL)
6111 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6112 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6113 return got_error_path(pe->path,
6114 GOT_ERR_FILE_NOT_STAGED);
6117 return NULL;
6120 const struct got_error *
6121 got_worktree_commit(struct got_object_id **new_commit_id,
6122 struct got_worktree *worktree, struct got_pathlist_head *paths,
6123 const char *author, const char *committer, int allow_bad_symlinks,
6124 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6125 got_worktree_status_cb status_cb, void *status_arg,
6126 struct got_repository *repo)
6128 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6129 struct got_fileindex *fileindex = NULL;
6130 char *fileindex_path = NULL;
6131 struct got_pathlist_head commitable_paths;
6132 struct collect_commitables_arg cc_arg;
6133 struct got_pathlist_entry *pe;
6134 struct got_reference *head_ref = NULL;
6135 struct got_object_id *head_commit_id = NULL;
6136 char *diff_path = NULL;
6137 int have_staged_files = 0;
6139 *new_commit_id = NULL;
6141 memset(&cc_arg, 0, sizeof(cc_arg));
6142 TAILQ_INIT(&commitable_paths);
6144 err = lock_worktree(worktree, LOCK_EX);
6145 if (err)
6146 goto done;
6148 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6149 if (err)
6150 goto done;
6152 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6153 if (err)
6154 goto done;
6156 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6157 if (err)
6158 goto done;
6160 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6161 &have_staged_files);
6162 if (err && err->code != GOT_ERR_CANCELLED)
6163 goto done;
6164 if (have_staged_files) {
6165 err = check_non_staged_files(fileindex, paths);
6166 if (err)
6167 goto done;
6170 cc_arg.commitable_paths = &commitable_paths;
6171 cc_arg.worktree = worktree;
6172 cc_arg.fileindex = fileindex;
6173 cc_arg.repo = repo;
6174 cc_arg.have_staged_files = have_staged_files;
6175 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6176 cc_arg.diff_header_shown = 0;
6177 if (show_diff) {
6178 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6179 GOT_TMPDIR_STR "/got", ".diff");
6180 if (err)
6181 goto done;
6182 cc_arg.f1 = got_opentemp();
6183 if (cc_arg.f1 == NULL) {
6184 err = got_error_from_errno("got_opentemp");
6185 goto done;
6187 cc_arg.f2 = got_opentemp();
6188 if (cc_arg.f2 == NULL) {
6189 err = got_error_from_errno("got_opentemp");
6190 goto done;
6194 TAILQ_FOREACH(pe, paths, entry) {
6195 err = worktree_status(worktree, pe->path, fileindex, repo,
6196 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6197 if (err)
6198 goto done;
6201 if (show_diff) {
6202 if (fflush(cc_arg.diff_outfile) == EOF) {
6203 err = got_error_from_errno("fflush");
6204 goto done;
6208 if (TAILQ_EMPTY(&commitable_paths)) {
6209 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6210 goto done;
6213 TAILQ_FOREACH(pe, paths, entry) {
6214 err = check_path_is_commitable(pe->path, &commitable_paths);
6215 if (err)
6216 goto done;
6219 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6220 struct got_commitable *ct = pe->data;
6221 const char *ct_path = ct->in_repo_path;
6223 while (ct_path[0] == '/')
6224 ct_path++;
6225 err = check_out_of_date(ct_path, ct->status,
6226 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6227 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6228 if (err)
6229 goto done;
6233 err = commit_worktree(new_commit_id, &commitable_paths,
6234 head_commit_id, NULL, worktree, author, committer,
6235 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6236 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6237 if (err)
6238 goto done;
6240 err = update_fileindex_after_commit(worktree, &commitable_paths,
6241 *new_commit_id, fileindex, have_staged_files);
6242 sync_err = sync_fileindex(fileindex, fileindex_path);
6243 if (sync_err && err == NULL)
6244 err = sync_err;
6245 done:
6246 if (fileindex)
6247 got_fileindex_free(fileindex);
6248 free(fileindex_path);
6249 unlockerr = lock_worktree(worktree, LOCK_SH);
6250 if (unlockerr && err == NULL)
6251 err = unlockerr;
6252 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6253 struct got_commitable *ct = pe->data;
6255 free_commitable(ct);
6257 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6258 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6259 err = got_error_from_errno2("unlink", diff_path);
6260 free(diff_path);
6261 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6262 err == NULL)
6263 err = got_error_from_errno("fclose");
6264 return err;
6267 const char *
6268 got_commitable_get_path(struct got_commitable *ct)
6270 return ct->path;
6273 unsigned int
6274 got_commitable_get_status(struct got_commitable *ct)
6276 return ct->status;
6279 struct check_rebase_ok_arg {
6280 struct got_worktree *worktree;
6281 struct got_repository *repo;
6284 static const struct got_error *
6285 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6287 const struct got_error *err = NULL;
6288 struct check_rebase_ok_arg *a = arg;
6289 unsigned char status;
6290 struct stat sb;
6291 char *ondisk_path;
6293 /* Reject rebase of a work tree with mixed base commits. */
6294 if (memcmp(ie->commit_hash, a->worktree->base_commit_id->hash,
6295 SHA1_DIGEST_LENGTH))
6296 return got_error(GOT_ERR_MIXED_COMMITS);
6298 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6299 == -1)
6300 return got_error_from_errno("asprintf");
6302 /* Reject rebase of a work tree with modified or staged files. */
6303 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6304 free(ondisk_path);
6305 if (err)
6306 return err;
6308 if (status != GOT_STATUS_NO_CHANGE)
6309 return got_error(GOT_ERR_MODIFIED);
6310 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6311 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6313 return NULL;
6316 const struct got_error *
6317 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6318 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6319 struct got_worktree *worktree, struct got_reference *branch,
6320 struct got_repository *repo)
6322 const struct got_error *err = NULL;
6323 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6324 char *branch_ref_name = NULL;
6325 char *fileindex_path = NULL;
6326 struct check_rebase_ok_arg ok_arg;
6327 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6328 struct got_object_id *wt_branch_tip = NULL;
6330 *new_base_branch_ref = NULL;
6331 *tmp_branch = NULL;
6332 *fileindex = NULL;
6334 err = lock_worktree(worktree, LOCK_EX);
6335 if (err)
6336 return err;
6338 err = open_fileindex(fileindex, &fileindex_path, worktree);
6339 if (err)
6340 goto done;
6342 ok_arg.worktree = worktree;
6343 ok_arg.repo = repo;
6344 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6345 &ok_arg);
6346 if (err)
6347 goto done;
6349 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6350 if (err)
6351 goto done;
6353 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6354 if (err)
6355 goto done;
6357 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6358 if (err)
6359 goto done;
6361 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6362 0);
6363 if (err)
6364 goto done;
6366 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6367 if (err)
6368 goto done;
6369 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6370 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6371 goto done;
6374 err = got_ref_alloc_symref(new_base_branch_ref,
6375 new_base_branch_ref_name, wt_branch);
6376 if (err)
6377 goto done;
6378 err = got_ref_write(*new_base_branch_ref, repo);
6379 if (err)
6380 goto done;
6382 /* TODO Lock original branch's ref while rebasing? */
6384 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6385 if (err)
6386 goto done;
6388 err = got_ref_write(branch_ref, repo);
6389 if (err)
6390 goto done;
6392 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6393 worktree->base_commit_id);
6394 if (err)
6395 goto done;
6396 err = got_ref_write(*tmp_branch, repo);
6397 if (err)
6398 goto done;
6400 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6401 if (err)
6402 goto done;
6403 done:
6404 free(fileindex_path);
6405 free(tmp_branch_name);
6406 free(new_base_branch_ref_name);
6407 free(branch_ref_name);
6408 if (branch_ref)
6409 got_ref_close(branch_ref);
6410 if (wt_branch)
6411 got_ref_close(wt_branch);
6412 free(wt_branch_tip);
6413 if (err) {
6414 if (*new_base_branch_ref) {
6415 got_ref_close(*new_base_branch_ref);
6416 *new_base_branch_ref = NULL;
6418 if (*tmp_branch) {
6419 got_ref_close(*tmp_branch);
6420 *tmp_branch = NULL;
6422 if (*fileindex) {
6423 got_fileindex_free(*fileindex);
6424 *fileindex = NULL;
6426 lock_worktree(worktree, LOCK_SH);
6428 return err;
6431 const struct got_error *
6432 got_worktree_rebase_continue(struct got_object_id **commit_id,
6433 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6434 struct got_reference **branch, struct got_fileindex **fileindex,
6435 struct got_worktree *worktree, struct got_repository *repo)
6437 const struct got_error *err;
6438 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6439 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6440 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6441 char *fileindex_path = NULL;
6442 int have_staged_files = 0;
6444 *commit_id = NULL;
6445 *new_base_branch = NULL;
6446 *tmp_branch = NULL;
6447 *branch = NULL;
6448 *fileindex = NULL;
6450 err = lock_worktree(worktree, LOCK_EX);
6451 if (err)
6452 return err;
6454 err = open_fileindex(fileindex, &fileindex_path, worktree);
6455 if (err)
6456 goto done;
6458 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6459 &have_staged_files);
6460 if (err && err->code != GOT_ERR_CANCELLED)
6461 goto done;
6462 if (have_staged_files) {
6463 err = got_error(GOT_ERR_STAGED_PATHS);
6464 goto done;
6467 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6468 if (err)
6469 goto done;
6471 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6472 if (err)
6473 goto done;
6475 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6476 if (err)
6477 goto done;
6479 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6480 if (err)
6481 goto done;
6483 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6484 if (err)
6485 goto done;
6487 err = got_ref_open(branch, repo,
6488 got_ref_get_symref_target(branch_ref), 0);
6489 if (err)
6490 goto done;
6492 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6493 if (err)
6494 goto done;
6496 err = got_ref_resolve(commit_id, repo, commit_ref);
6497 if (err)
6498 goto done;
6500 err = got_ref_open(new_base_branch, repo,
6501 new_base_branch_ref_name, 0);
6502 if (err)
6503 goto done;
6505 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6506 if (err)
6507 goto done;
6508 done:
6509 free(commit_ref_name);
6510 free(branch_ref_name);
6511 free(fileindex_path);
6512 if (commit_ref)
6513 got_ref_close(commit_ref);
6514 if (branch_ref)
6515 got_ref_close(branch_ref);
6516 if (err) {
6517 free(*commit_id);
6518 *commit_id = NULL;
6519 if (*tmp_branch) {
6520 got_ref_close(*tmp_branch);
6521 *tmp_branch = NULL;
6523 if (*new_base_branch) {
6524 got_ref_close(*new_base_branch);
6525 *new_base_branch = NULL;
6527 if (*branch) {
6528 got_ref_close(*branch);
6529 *branch = NULL;
6531 if (*fileindex) {
6532 got_fileindex_free(*fileindex);
6533 *fileindex = NULL;
6535 lock_worktree(worktree, LOCK_SH);
6537 return err;
6540 const struct got_error *
6541 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6543 const struct got_error *err;
6544 char *tmp_branch_name = NULL;
6546 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6547 if (err)
6548 return err;
6550 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6551 free(tmp_branch_name);
6552 return NULL;
6555 static const struct got_error *
6556 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6557 const char *diff_path, char **logmsg, void *arg)
6559 *logmsg = arg;
6560 return NULL;
6563 static const struct got_error *
6564 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6565 const char *path, struct got_object_id *blob_id,
6566 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6567 int dirfd, const char *de_name)
6569 return NULL;
6572 struct collect_merged_paths_arg {
6573 got_worktree_checkout_cb progress_cb;
6574 void *progress_arg;
6575 struct got_pathlist_head *merged_paths;
6578 static const struct got_error *
6579 collect_merged_paths(void *arg, unsigned char status, const char *path)
6581 const struct got_error *err;
6582 struct collect_merged_paths_arg *a = arg;
6583 char *p;
6584 struct got_pathlist_entry *new;
6586 err = (*a->progress_cb)(a->progress_arg, status, path);
6587 if (err)
6588 return err;
6590 if (status != GOT_STATUS_MERGE &&
6591 status != GOT_STATUS_ADD &&
6592 status != GOT_STATUS_DELETE &&
6593 status != GOT_STATUS_CONFLICT)
6594 return NULL;
6596 p = strdup(path);
6597 if (p == NULL)
6598 return got_error_from_errno("strdup");
6600 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6601 if (err || new == NULL)
6602 free(p);
6603 return err;
6606 static const struct got_error *
6607 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6608 int is_rebase, struct got_repository *repo)
6610 const struct got_error *err;
6611 struct got_reference *commit_ref = NULL;
6613 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6614 if (err) {
6615 if (err->code != GOT_ERR_NOT_REF)
6616 goto done;
6617 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6618 if (err)
6619 goto done;
6620 err = got_ref_write(commit_ref, repo);
6621 if (err)
6622 goto done;
6623 } else if (is_rebase) {
6624 struct got_object_id *stored_id;
6625 int cmp;
6627 err = got_ref_resolve(&stored_id, repo, commit_ref);
6628 if (err)
6629 goto done;
6630 cmp = got_object_id_cmp(commit_id, stored_id);
6631 free(stored_id);
6632 if (cmp != 0) {
6633 err = got_error(GOT_ERR_REBASE_COMMITID);
6634 goto done;
6637 done:
6638 if (commit_ref)
6639 got_ref_close(commit_ref);
6640 return err;
6643 static const struct got_error *
6644 rebase_merge_files(struct got_pathlist_head *merged_paths,
6645 const char *commit_ref_name, struct got_worktree *worktree,
6646 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6647 struct got_object_id *commit_id, struct got_repository *repo,
6648 got_worktree_checkout_cb progress_cb, void *progress_arg,
6649 got_cancel_cb cancel_cb, void *cancel_arg)
6651 const struct got_error *err;
6652 struct got_reference *commit_ref = NULL;
6653 struct collect_merged_paths_arg cmp_arg;
6654 char *fileindex_path;
6656 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6658 err = get_fileindex_path(&fileindex_path, worktree);
6659 if (err)
6660 return err;
6662 cmp_arg.progress_cb = progress_cb;
6663 cmp_arg.progress_arg = progress_arg;
6664 cmp_arg.merged_paths = merged_paths;
6665 err = merge_files(worktree, fileindex, fileindex_path,
6666 parent_commit_id, commit_id, repo, collect_merged_paths,
6667 &cmp_arg, cancel_cb, cancel_arg);
6668 if (commit_ref)
6669 got_ref_close(commit_ref);
6670 return err;
6673 const struct got_error *
6674 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6675 struct got_worktree *worktree, struct got_fileindex *fileindex,
6676 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6677 struct got_repository *repo,
6678 got_worktree_checkout_cb progress_cb, void *progress_arg,
6679 got_cancel_cb cancel_cb, void *cancel_arg)
6681 const struct got_error *err;
6682 char *commit_ref_name;
6684 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6685 if (err)
6686 return err;
6688 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6689 if (err)
6690 goto done;
6692 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6693 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6694 progress_arg, cancel_cb, cancel_arg);
6695 done:
6696 free(commit_ref_name);
6697 return err;
6700 const struct got_error *
6701 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6702 struct got_worktree *worktree, struct got_fileindex *fileindex,
6703 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6704 struct got_repository *repo,
6705 got_worktree_checkout_cb progress_cb, void *progress_arg,
6706 got_cancel_cb cancel_cb, void *cancel_arg)
6708 const struct got_error *err;
6709 char *commit_ref_name;
6711 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6712 if (err)
6713 return err;
6715 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6716 if (err)
6717 goto done;
6719 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6720 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6721 progress_arg, cancel_cb, cancel_arg);
6722 done:
6723 free(commit_ref_name);
6724 return err;
6727 static const struct got_error *
6728 rebase_commit(struct got_object_id **new_commit_id,
6729 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6730 struct got_worktree *worktree, struct got_fileindex *fileindex,
6731 struct got_reference *tmp_branch, const char *committer,
6732 struct got_commit_object *orig_commit, const char *new_logmsg,
6733 struct got_repository *repo)
6735 const struct got_error *err, *sync_err;
6736 struct got_pathlist_head commitable_paths;
6737 struct collect_commitables_arg cc_arg;
6738 char *fileindex_path = NULL;
6739 struct got_reference *head_ref = NULL;
6740 struct got_object_id *head_commit_id = NULL;
6741 char *logmsg = NULL;
6743 memset(&cc_arg, 0, sizeof(cc_arg));
6744 TAILQ_INIT(&commitable_paths);
6745 *new_commit_id = NULL;
6747 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6749 err = get_fileindex_path(&fileindex_path, worktree);
6750 if (err)
6751 return err;
6753 cc_arg.commitable_paths = &commitable_paths;
6754 cc_arg.worktree = worktree;
6755 cc_arg.repo = repo;
6756 cc_arg.have_staged_files = 0;
6758 * If possible get the status of individual files directly to
6759 * avoid crawling the entire work tree once per rebased commit.
6761 * Ideally, merged_paths would contain a list of commitables
6762 * we could use so we could skip worktree_status() entirely.
6763 * However, we would then need carefully keep track of cumulative
6764 * effects of operations such as file additions and deletions
6765 * in 'got histedit -f' (folding multiple commits into one),
6766 * and this extra complexity is not really worth it.
6768 if (merged_paths) {
6769 struct got_pathlist_entry *pe;
6770 TAILQ_FOREACH(pe, merged_paths, entry) {
6771 err = worktree_status(worktree, pe->path, fileindex,
6772 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6773 0);
6774 if (err)
6775 goto done;
6777 } else {
6778 err = worktree_status(worktree, "", fileindex, repo,
6779 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6780 if (err)
6781 goto done;
6784 if (TAILQ_EMPTY(&commitable_paths)) {
6785 /* No-op change; commit will be elided. */
6786 err = got_ref_delete(commit_ref, repo);
6787 if (err)
6788 goto done;
6789 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6790 goto done;
6793 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6794 if (err)
6795 goto done;
6797 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6798 if (err)
6799 goto done;
6801 if (new_logmsg) {
6802 logmsg = strdup(new_logmsg);
6803 if (logmsg == NULL) {
6804 err = got_error_from_errno("strdup");
6805 goto done;
6807 } else {
6808 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6809 if (err)
6810 goto done;
6813 /* NB: commit_worktree will call free(logmsg) */
6814 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6815 NULL, worktree, got_object_commit_get_author(orig_commit),
6816 committer ? committer :
6817 got_object_commit_get_committer(orig_commit), NULL,
6818 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6819 if (err)
6820 goto done;
6822 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6823 if (err)
6824 goto done;
6826 err = got_ref_delete(commit_ref, repo);
6827 if (err)
6828 goto done;
6830 err = update_fileindex_after_commit(worktree, &commitable_paths,
6831 *new_commit_id, fileindex, 0);
6832 sync_err = sync_fileindex(fileindex, fileindex_path);
6833 if (sync_err && err == NULL)
6834 err = sync_err;
6835 done:
6836 free(fileindex_path);
6837 free(head_commit_id);
6838 if (head_ref)
6839 got_ref_close(head_ref);
6840 if (err) {
6841 free(*new_commit_id);
6842 *new_commit_id = NULL;
6844 return err;
6847 const struct got_error *
6848 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6849 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6850 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6851 const char *committer, struct got_commit_object *orig_commit,
6852 struct got_object_id *orig_commit_id, struct got_repository *repo)
6854 const struct got_error *err;
6855 char *commit_ref_name;
6856 struct got_reference *commit_ref = NULL;
6857 struct got_object_id *commit_id = NULL;
6859 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6860 if (err)
6861 return err;
6863 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6864 if (err)
6865 goto done;
6866 err = got_ref_resolve(&commit_id, repo, commit_ref);
6867 if (err)
6868 goto done;
6869 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6870 err = got_error(GOT_ERR_REBASE_COMMITID);
6871 goto done;
6874 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6875 worktree, fileindex, tmp_branch, committer, orig_commit,
6876 NULL, repo);
6877 done:
6878 if (commit_ref)
6879 got_ref_close(commit_ref);
6880 free(commit_ref_name);
6881 free(commit_id);
6882 return err;
6885 const struct got_error *
6886 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6887 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6888 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6889 const char *committer, struct got_commit_object *orig_commit,
6890 struct got_object_id *orig_commit_id, const char *new_logmsg,
6891 struct got_repository *repo)
6893 const struct got_error *err;
6894 char *commit_ref_name;
6895 struct got_reference *commit_ref = NULL;
6897 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6898 if (err)
6899 return err;
6901 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6902 if (err)
6903 goto done;
6905 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6906 worktree, fileindex, tmp_branch, committer, orig_commit,
6907 new_logmsg, repo);
6908 done:
6909 if (commit_ref)
6910 got_ref_close(commit_ref);
6911 free(commit_ref_name);
6912 return err;
6915 const struct got_error *
6916 got_worktree_rebase_postpone(struct got_worktree *worktree,
6917 struct got_fileindex *fileindex)
6919 if (fileindex)
6920 got_fileindex_free(fileindex);
6921 return lock_worktree(worktree, LOCK_SH);
6924 static const struct got_error *
6925 delete_ref(const char *name, struct got_repository *repo)
6927 const struct got_error *err;
6928 struct got_reference *ref;
6930 err = got_ref_open(&ref, repo, name, 0);
6931 if (err) {
6932 if (err->code == GOT_ERR_NOT_REF)
6933 return NULL;
6934 return err;
6937 err = got_ref_delete(ref, repo);
6938 got_ref_close(ref);
6939 return err;
6942 static const struct got_error *
6943 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6945 const struct got_error *err;
6946 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6947 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6949 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6950 if (err)
6951 goto done;
6952 err = delete_ref(tmp_branch_name, repo);
6953 if (err)
6954 goto done;
6956 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6957 if (err)
6958 goto done;
6959 err = delete_ref(new_base_branch_ref_name, repo);
6960 if (err)
6961 goto done;
6963 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6964 if (err)
6965 goto done;
6966 err = delete_ref(branch_ref_name, repo);
6967 if (err)
6968 goto done;
6970 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6971 if (err)
6972 goto done;
6973 err = delete_ref(commit_ref_name, repo);
6974 if (err)
6975 goto done;
6977 done:
6978 free(tmp_branch_name);
6979 free(new_base_branch_ref_name);
6980 free(branch_ref_name);
6981 free(commit_ref_name);
6982 return err;
6985 static const struct got_error *
6986 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6987 struct got_object_id *new_commit_id, struct got_repository *repo)
6989 const struct got_error *err;
6990 struct got_reference *ref = NULL;
6991 struct got_object_id *old_commit_id = NULL;
6992 const char *branch_name = NULL;
6993 char *new_id_str = NULL;
6994 char *refname = NULL;
6996 branch_name = got_ref_get_name(branch);
6997 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6998 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6999 branch_name += 11;
7001 err = got_object_id_str(&new_id_str, new_commit_id);
7002 if (err)
7003 return err;
7005 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7006 new_id_str) == -1) {
7007 err = got_error_from_errno("asprintf");
7008 goto done;
7011 err = got_ref_resolve(&old_commit_id, repo, branch);
7012 if (err)
7013 goto done;
7015 err = got_ref_alloc(&ref, refname, old_commit_id);
7016 if (err)
7017 goto done;
7019 err = got_ref_write(ref, repo);
7020 done:
7021 free(new_id_str);
7022 free(refname);
7023 free(old_commit_id);
7024 if (ref)
7025 got_ref_close(ref);
7026 return err;
7029 const struct got_error *
7030 got_worktree_rebase_complete(struct got_worktree *worktree,
7031 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7032 struct got_reference *rebased_branch, struct got_repository *repo,
7033 int create_backup)
7035 const struct got_error *err, *unlockerr, *sync_err;
7036 struct got_object_id *new_head_commit_id = NULL;
7037 char *fileindex_path = NULL;
7039 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7040 if (err)
7041 return err;
7043 if (create_backup) {
7044 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7045 rebased_branch, new_head_commit_id, repo);
7046 if (err)
7047 goto done;
7050 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7051 if (err)
7052 goto done;
7054 err = got_ref_write(rebased_branch, repo);
7055 if (err)
7056 goto done;
7058 err = got_worktree_set_head_ref(worktree, rebased_branch);
7059 if (err)
7060 goto done;
7062 err = delete_rebase_refs(worktree, repo);
7063 if (err)
7064 goto done;
7066 err = get_fileindex_path(&fileindex_path, worktree);
7067 if (err)
7068 goto done;
7069 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7070 sync_err = sync_fileindex(fileindex, fileindex_path);
7071 if (sync_err && err == NULL)
7072 err = sync_err;
7073 done:
7074 got_fileindex_free(fileindex);
7075 free(fileindex_path);
7076 free(new_head_commit_id);
7077 unlockerr = lock_worktree(worktree, LOCK_SH);
7078 if (unlockerr && err == NULL)
7079 err = unlockerr;
7080 return err;
7083 const struct got_error *
7084 got_worktree_rebase_abort(struct got_worktree *worktree,
7085 struct got_fileindex *fileindex, struct got_repository *repo,
7086 struct got_reference *new_base_branch,
7087 got_worktree_checkout_cb progress_cb, void *progress_arg)
7089 const struct got_error *err, *unlockerr, *sync_err;
7090 struct got_reference *resolved = NULL;
7091 struct got_object_id *commit_id = NULL;
7092 struct got_commit_object *commit = NULL;
7093 char *fileindex_path = NULL;
7094 struct revert_file_args rfa;
7095 struct got_object_id *tree_id = NULL;
7097 err = lock_worktree(worktree, LOCK_EX);
7098 if (err)
7099 return err;
7101 err = got_object_open_as_commit(&commit, repo,
7102 worktree->base_commit_id);
7103 if (err)
7104 goto done;
7106 err = got_ref_open(&resolved, repo,
7107 got_ref_get_symref_target(new_base_branch), 0);
7108 if (err)
7109 goto done;
7111 err = got_worktree_set_head_ref(worktree, resolved);
7112 if (err)
7113 goto done;
7116 * XXX commits to the base branch could have happened while
7117 * we were busy rebasing; should we store the original commit ID
7118 * when rebase begins and read it back here?
7120 err = got_ref_resolve(&commit_id, repo, resolved);
7121 if (err)
7122 goto done;
7124 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7125 if (err)
7126 goto done;
7128 err = got_object_id_by_path(&tree_id, repo, commit,
7129 worktree->path_prefix);
7130 if (err)
7131 goto done;
7133 err = delete_rebase_refs(worktree, repo);
7134 if (err)
7135 goto done;
7137 err = get_fileindex_path(&fileindex_path, worktree);
7138 if (err)
7139 goto done;
7141 rfa.worktree = worktree;
7142 rfa.fileindex = fileindex;
7143 rfa.progress_cb = progress_cb;
7144 rfa.progress_arg = progress_arg;
7145 rfa.patch_cb = NULL;
7146 rfa.patch_arg = NULL;
7147 rfa.repo = repo;
7148 rfa.unlink_added_files = 0;
7149 err = worktree_status(worktree, "", fileindex, repo,
7150 revert_file, &rfa, NULL, NULL, 1, 0);
7151 if (err)
7152 goto sync;
7154 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7155 repo, progress_cb, progress_arg, NULL, NULL);
7156 sync:
7157 sync_err = sync_fileindex(fileindex, fileindex_path);
7158 if (sync_err && err == NULL)
7159 err = sync_err;
7160 done:
7161 got_ref_close(resolved);
7162 free(tree_id);
7163 free(commit_id);
7164 if (commit)
7165 got_object_commit_close(commit);
7166 if (fileindex)
7167 got_fileindex_free(fileindex);
7168 free(fileindex_path);
7170 unlockerr = lock_worktree(worktree, LOCK_SH);
7171 if (unlockerr && err == NULL)
7172 err = unlockerr;
7173 return err;
7176 const struct got_error *
7177 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7178 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7179 struct got_fileindex **fileindex, struct got_worktree *worktree,
7180 struct got_repository *repo)
7182 const struct got_error *err = NULL;
7183 char *tmp_branch_name = NULL;
7184 char *branch_ref_name = NULL;
7185 char *base_commit_ref_name = NULL;
7186 char *fileindex_path = NULL;
7187 struct check_rebase_ok_arg ok_arg;
7188 struct got_reference *wt_branch = NULL;
7189 struct got_reference *base_commit_ref = NULL;
7191 *tmp_branch = NULL;
7192 *branch_ref = NULL;
7193 *base_commit_id = NULL;
7194 *fileindex = NULL;
7196 err = lock_worktree(worktree, LOCK_EX);
7197 if (err)
7198 return err;
7200 err = open_fileindex(fileindex, &fileindex_path, worktree);
7201 if (err)
7202 goto done;
7204 ok_arg.worktree = worktree;
7205 ok_arg.repo = repo;
7206 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7207 &ok_arg);
7208 if (err)
7209 goto done;
7211 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7212 if (err)
7213 goto done;
7215 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7216 if (err)
7217 goto done;
7219 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7220 worktree);
7221 if (err)
7222 goto done;
7224 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7225 0);
7226 if (err)
7227 goto done;
7229 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7230 if (err)
7231 goto done;
7233 err = got_ref_write(*branch_ref, repo);
7234 if (err)
7235 goto done;
7237 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7238 worktree->base_commit_id);
7239 if (err)
7240 goto done;
7241 err = got_ref_write(base_commit_ref, repo);
7242 if (err)
7243 goto done;
7244 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7245 if (*base_commit_id == NULL) {
7246 err = got_error_from_errno("got_object_id_dup");
7247 goto done;
7250 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7251 worktree->base_commit_id);
7252 if (err)
7253 goto done;
7254 err = got_ref_write(*tmp_branch, repo);
7255 if (err)
7256 goto done;
7258 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7259 if (err)
7260 goto done;
7261 done:
7262 free(fileindex_path);
7263 free(tmp_branch_name);
7264 free(branch_ref_name);
7265 free(base_commit_ref_name);
7266 if (wt_branch)
7267 got_ref_close(wt_branch);
7268 if (err) {
7269 if (*branch_ref) {
7270 got_ref_close(*branch_ref);
7271 *branch_ref = NULL;
7273 if (*tmp_branch) {
7274 got_ref_close(*tmp_branch);
7275 *tmp_branch = NULL;
7277 free(*base_commit_id);
7278 if (*fileindex) {
7279 got_fileindex_free(*fileindex);
7280 *fileindex = NULL;
7282 lock_worktree(worktree, LOCK_SH);
7284 return err;
7287 const struct got_error *
7288 got_worktree_histedit_postpone(struct got_worktree *worktree,
7289 struct got_fileindex *fileindex)
7291 if (fileindex)
7292 got_fileindex_free(fileindex);
7293 return lock_worktree(worktree, LOCK_SH);
7296 const struct got_error *
7297 got_worktree_histedit_in_progress(int *in_progress,
7298 struct got_worktree *worktree)
7300 const struct got_error *err;
7301 char *tmp_branch_name = NULL;
7303 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7304 if (err)
7305 return err;
7307 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7308 free(tmp_branch_name);
7309 return NULL;
7312 const struct got_error *
7313 got_worktree_histedit_continue(struct got_object_id **commit_id,
7314 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7315 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7316 struct got_worktree *worktree, struct got_repository *repo)
7318 const struct got_error *err;
7319 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7320 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7321 struct got_reference *commit_ref = NULL;
7322 struct got_reference *base_commit_ref = NULL;
7323 char *fileindex_path = NULL;
7324 int have_staged_files = 0;
7326 *commit_id = NULL;
7327 *tmp_branch = NULL;
7328 *base_commit_id = NULL;
7329 *fileindex = NULL;
7331 err = lock_worktree(worktree, LOCK_EX);
7332 if (err)
7333 return err;
7335 err = open_fileindex(fileindex, &fileindex_path, worktree);
7336 if (err)
7337 goto done;
7339 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7340 &have_staged_files);
7341 if (err && err->code != GOT_ERR_CANCELLED)
7342 goto done;
7343 if (have_staged_files) {
7344 err = got_error(GOT_ERR_STAGED_PATHS);
7345 goto done;
7348 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7349 if (err)
7350 goto done;
7352 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7353 if (err)
7354 goto done;
7356 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7357 if (err)
7358 goto done;
7360 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7361 worktree);
7362 if (err)
7363 goto done;
7365 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7366 if (err)
7367 goto done;
7369 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7370 if (err)
7371 goto done;
7372 err = got_ref_resolve(commit_id, repo, commit_ref);
7373 if (err)
7374 goto done;
7376 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7377 if (err)
7378 goto done;
7379 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7380 if (err)
7381 goto done;
7383 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7384 if (err)
7385 goto done;
7386 done:
7387 free(commit_ref_name);
7388 free(branch_ref_name);
7389 free(fileindex_path);
7390 if (commit_ref)
7391 got_ref_close(commit_ref);
7392 if (base_commit_ref)
7393 got_ref_close(base_commit_ref);
7394 if (err) {
7395 free(*commit_id);
7396 *commit_id = NULL;
7397 free(*base_commit_id);
7398 *base_commit_id = NULL;
7399 if (*tmp_branch) {
7400 got_ref_close(*tmp_branch);
7401 *tmp_branch = NULL;
7403 if (*fileindex) {
7404 got_fileindex_free(*fileindex);
7405 *fileindex = NULL;
7407 lock_worktree(worktree, LOCK_EX);
7409 return err;
7412 static const struct got_error *
7413 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7415 const struct got_error *err;
7416 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7417 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7419 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7420 if (err)
7421 goto done;
7422 err = delete_ref(tmp_branch_name, repo);
7423 if (err)
7424 goto done;
7426 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7427 worktree);
7428 if (err)
7429 goto done;
7430 err = delete_ref(base_commit_ref_name, repo);
7431 if (err)
7432 goto done;
7434 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7435 if (err)
7436 goto done;
7437 err = delete_ref(branch_ref_name, repo);
7438 if (err)
7439 goto done;
7441 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7442 if (err)
7443 goto done;
7444 err = delete_ref(commit_ref_name, repo);
7445 if (err)
7446 goto done;
7447 done:
7448 free(tmp_branch_name);
7449 free(base_commit_ref_name);
7450 free(branch_ref_name);
7451 free(commit_ref_name);
7452 return err;
7455 const struct got_error *
7456 got_worktree_histedit_abort(struct got_worktree *worktree,
7457 struct got_fileindex *fileindex, struct got_repository *repo,
7458 struct got_reference *branch, struct got_object_id *base_commit_id,
7459 got_worktree_checkout_cb progress_cb, void *progress_arg)
7461 const struct got_error *err, *unlockerr, *sync_err;
7462 struct got_reference *resolved = NULL;
7463 char *fileindex_path = NULL;
7464 struct got_commit_object *commit = NULL;
7465 struct got_object_id *tree_id = NULL;
7466 struct revert_file_args rfa;
7468 err = lock_worktree(worktree, LOCK_EX);
7469 if (err)
7470 return err;
7472 err = got_object_open_as_commit(&commit, repo,
7473 worktree->base_commit_id);
7474 if (err)
7475 goto done;
7477 err = got_ref_open(&resolved, repo,
7478 got_ref_get_symref_target(branch), 0);
7479 if (err)
7480 goto done;
7482 err = got_worktree_set_head_ref(worktree, resolved);
7483 if (err)
7484 goto done;
7486 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7487 if (err)
7488 goto done;
7490 err = got_object_id_by_path(&tree_id, repo, commit,
7491 worktree->path_prefix);
7492 if (err)
7493 goto done;
7495 err = delete_histedit_refs(worktree, repo);
7496 if (err)
7497 goto done;
7499 err = get_fileindex_path(&fileindex_path, worktree);
7500 if (err)
7501 goto done;
7503 rfa.worktree = worktree;
7504 rfa.fileindex = fileindex;
7505 rfa.progress_cb = progress_cb;
7506 rfa.progress_arg = progress_arg;
7507 rfa.patch_cb = NULL;
7508 rfa.patch_arg = NULL;
7509 rfa.repo = repo;
7510 rfa.unlink_added_files = 0;
7511 err = worktree_status(worktree, "", fileindex, repo,
7512 revert_file, &rfa, NULL, NULL, 1, 0);
7513 if (err)
7514 goto sync;
7516 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7517 repo, progress_cb, progress_arg, NULL, NULL);
7518 sync:
7519 sync_err = sync_fileindex(fileindex, fileindex_path);
7520 if (sync_err && err == NULL)
7521 err = sync_err;
7522 done:
7523 got_ref_close(resolved);
7524 free(tree_id);
7525 free(fileindex_path);
7527 unlockerr = lock_worktree(worktree, LOCK_SH);
7528 if (unlockerr && err == NULL)
7529 err = unlockerr;
7530 return err;
7533 const struct got_error *
7534 got_worktree_histedit_complete(struct got_worktree *worktree,
7535 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7536 struct got_reference *edited_branch, struct got_repository *repo)
7538 const struct got_error *err, *unlockerr, *sync_err;
7539 struct got_object_id *new_head_commit_id = NULL;
7540 struct got_reference *resolved = NULL;
7541 char *fileindex_path = NULL;
7543 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7544 if (err)
7545 return err;
7547 err = got_ref_open(&resolved, repo,
7548 got_ref_get_symref_target(edited_branch), 0);
7549 if (err)
7550 goto done;
7552 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7553 resolved, new_head_commit_id, repo);
7554 if (err)
7555 goto done;
7557 err = got_ref_change_ref(resolved, new_head_commit_id);
7558 if (err)
7559 goto done;
7561 err = got_ref_write(resolved, repo);
7562 if (err)
7563 goto done;
7565 err = got_worktree_set_head_ref(worktree, resolved);
7566 if (err)
7567 goto done;
7569 err = delete_histedit_refs(worktree, repo);
7570 if (err)
7571 goto done;
7573 err = get_fileindex_path(&fileindex_path, worktree);
7574 if (err)
7575 goto done;
7576 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7577 sync_err = sync_fileindex(fileindex, fileindex_path);
7578 if (sync_err && err == NULL)
7579 err = sync_err;
7580 done:
7581 got_fileindex_free(fileindex);
7582 free(fileindex_path);
7583 free(new_head_commit_id);
7584 unlockerr = lock_worktree(worktree, LOCK_SH);
7585 if (unlockerr && err == NULL)
7586 err = unlockerr;
7587 return err;
7590 const struct got_error *
7591 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7592 struct got_object_id *commit_id, struct got_repository *repo)
7594 const struct got_error *err;
7595 char *commit_ref_name;
7597 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7598 if (err)
7599 return err;
7601 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7602 if (err)
7603 goto done;
7605 err = delete_ref(commit_ref_name, repo);
7606 done:
7607 free(commit_ref_name);
7608 return err;
7611 const struct got_error *
7612 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7613 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7614 struct got_worktree *worktree, const char *refname,
7615 struct got_repository *repo)
7617 const struct got_error *err = NULL;
7618 char *fileindex_path = NULL;
7619 struct check_rebase_ok_arg ok_arg;
7621 *fileindex = NULL;
7622 *branch_ref = NULL;
7623 *base_branch_ref = NULL;
7625 err = lock_worktree(worktree, LOCK_EX);
7626 if (err)
7627 return err;
7629 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7630 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7631 "cannot integrate a branch into itself; "
7632 "update -b or different branch name required");
7633 goto done;
7636 err = open_fileindex(fileindex, &fileindex_path, worktree);
7637 if (err)
7638 goto done;
7640 /* Preconditions are the same as for rebase. */
7641 ok_arg.worktree = worktree;
7642 ok_arg.repo = repo;
7643 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7644 &ok_arg);
7645 if (err)
7646 goto done;
7648 err = got_ref_open(branch_ref, repo, refname, 1);
7649 if (err)
7650 goto done;
7652 err = got_ref_open(base_branch_ref, repo,
7653 got_worktree_get_head_ref_name(worktree), 1);
7654 done:
7655 if (err) {
7656 if (*branch_ref) {
7657 got_ref_close(*branch_ref);
7658 *branch_ref = NULL;
7660 if (*base_branch_ref) {
7661 got_ref_close(*base_branch_ref);
7662 *base_branch_ref = NULL;
7664 if (*fileindex) {
7665 got_fileindex_free(*fileindex);
7666 *fileindex = NULL;
7668 lock_worktree(worktree, LOCK_SH);
7670 return err;
7673 const struct got_error *
7674 got_worktree_integrate_continue(struct got_worktree *worktree,
7675 struct got_fileindex *fileindex, struct got_repository *repo,
7676 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7677 got_worktree_checkout_cb progress_cb, void *progress_arg,
7678 got_cancel_cb cancel_cb, void *cancel_arg)
7680 const struct got_error *err = NULL, *sync_err, *unlockerr;
7681 char *fileindex_path = NULL;
7682 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7683 struct got_commit_object *commit = NULL;
7685 err = get_fileindex_path(&fileindex_path, worktree);
7686 if (err)
7687 goto done;
7689 err = got_ref_resolve(&commit_id, repo, branch_ref);
7690 if (err)
7691 goto done;
7693 err = got_object_open_as_commit(&commit, repo, commit_id);
7694 if (err)
7695 goto done;
7697 err = got_object_id_by_path(&tree_id, repo, commit,
7698 worktree->path_prefix);
7699 if (err)
7700 goto done;
7702 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7703 if (err)
7704 goto done;
7706 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7707 progress_cb, progress_arg, cancel_cb, cancel_arg);
7708 if (err)
7709 goto sync;
7711 err = got_ref_change_ref(base_branch_ref, commit_id);
7712 if (err)
7713 goto sync;
7715 err = got_ref_write(base_branch_ref, repo);
7716 if (err)
7717 goto sync;
7719 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7720 sync:
7721 sync_err = sync_fileindex(fileindex, fileindex_path);
7722 if (sync_err && err == NULL)
7723 err = sync_err;
7725 done:
7726 unlockerr = got_ref_unlock(branch_ref);
7727 if (unlockerr && err == NULL)
7728 err = unlockerr;
7729 got_ref_close(branch_ref);
7731 unlockerr = got_ref_unlock(base_branch_ref);
7732 if (unlockerr && err == NULL)
7733 err = unlockerr;
7734 got_ref_close(base_branch_ref);
7736 got_fileindex_free(fileindex);
7737 free(fileindex_path);
7738 free(tree_id);
7739 if (commit)
7740 got_object_commit_close(commit);
7742 unlockerr = lock_worktree(worktree, LOCK_SH);
7743 if (unlockerr && err == NULL)
7744 err = unlockerr;
7745 return err;
7748 const struct got_error *
7749 got_worktree_integrate_abort(struct got_worktree *worktree,
7750 struct got_fileindex *fileindex, struct got_repository *repo,
7751 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7753 const struct got_error *err = NULL, *unlockerr = NULL;
7755 got_fileindex_free(fileindex);
7757 err = lock_worktree(worktree, LOCK_SH);
7759 unlockerr = got_ref_unlock(branch_ref);
7760 if (unlockerr && err == NULL)
7761 err = unlockerr;
7762 got_ref_close(branch_ref);
7764 unlockerr = got_ref_unlock(base_branch_ref);
7765 if (unlockerr && err == NULL)
7766 err = unlockerr;
7767 got_ref_close(base_branch_ref);
7769 return err;
7772 const struct got_error *
7773 got_worktree_merge_postpone(struct got_worktree *worktree,
7774 struct got_fileindex *fileindex)
7776 const struct got_error *err, *sync_err;
7777 char *fileindex_path = NULL;
7779 err = get_fileindex_path(&fileindex_path, worktree);
7780 if (err)
7781 goto done;
7783 sync_err = sync_fileindex(fileindex, fileindex_path);
7785 err = lock_worktree(worktree, LOCK_SH);
7786 if (sync_err && err == NULL)
7787 err = sync_err;
7788 done:
7789 got_fileindex_free(fileindex);
7790 free(fileindex_path);
7791 return err;
7794 static const struct got_error *
7795 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7797 const struct got_error *err;
7798 char *branch_refname = NULL, *commit_refname = NULL;
7800 err = get_merge_branch_ref_name(&branch_refname, worktree);
7801 if (err)
7802 goto done;
7803 err = delete_ref(branch_refname, repo);
7804 if (err)
7805 goto done;
7807 err = get_merge_commit_ref_name(&commit_refname, worktree);
7808 if (err)
7809 goto done;
7810 err = delete_ref(commit_refname, repo);
7811 if (err)
7812 goto done;
7814 done:
7815 free(branch_refname);
7816 free(commit_refname);
7817 return err;
7820 struct merge_commit_msg_arg {
7821 struct got_worktree *worktree;
7822 const char *branch_name;
7825 static const struct got_error *
7826 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7827 const char *diff_path, char **logmsg, void *arg)
7829 struct merge_commit_msg_arg *a = arg;
7831 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7832 got_worktree_get_head_ref_name(a->worktree)) == -1)
7833 return got_error_from_errno("asprintf");
7835 return NULL;
7839 const struct got_error *
7840 got_worktree_merge_branch(struct got_worktree *worktree,
7841 struct got_fileindex *fileindex,
7842 struct got_object_id *yca_commit_id,
7843 struct got_object_id *branch_tip,
7844 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7845 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7847 const struct got_error *err;
7848 char *fileindex_path = NULL;
7850 err = get_fileindex_path(&fileindex_path, worktree);
7851 if (err)
7852 goto done;
7854 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7855 worktree);
7856 if (err)
7857 goto done;
7859 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7860 branch_tip, repo, progress_cb, progress_arg,
7861 cancel_cb, cancel_arg);
7862 done:
7863 free(fileindex_path);
7864 return err;
7867 const struct got_error *
7868 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7869 struct got_worktree *worktree, struct got_fileindex *fileindex,
7870 const char *author, const char *committer, int allow_bad_symlinks,
7871 struct got_object_id *branch_tip, const char *branch_name,
7872 struct got_repository *repo,
7873 got_worktree_status_cb status_cb, void *status_arg)
7876 const struct got_error *err = NULL, *sync_err;
7877 struct got_pathlist_head commitable_paths;
7878 struct collect_commitables_arg cc_arg;
7879 struct got_pathlist_entry *pe;
7880 struct got_reference *head_ref = NULL;
7881 struct got_object_id *head_commit_id = NULL;
7882 int have_staged_files = 0;
7883 struct merge_commit_msg_arg mcm_arg;
7884 char *fileindex_path = NULL;
7886 memset(&cc_arg, 0, sizeof(cc_arg));
7887 *new_commit_id = NULL;
7889 TAILQ_INIT(&commitable_paths);
7891 err = get_fileindex_path(&fileindex_path, worktree);
7892 if (err)
7893 goto done;
7895 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7896 if (err)
7897 goto done;
7899 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7900 if (err)
7901 goto done;
7903 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7904 &have_staged_files);
7905 if (err && err->code != GOT_ERR_CANCELLED)
7906 goto done;
7907 if (have_staged_files) {
7908 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7909 goto done;
7912 cc_arg.commitable_paths = &commitable_paths;
7913 cc_arg.worktree = worktree;
7914 cc_arg.fileindex = fileindex;
7915 cc_arg.repo = repo;
7916 cc_arg.have_staged_files = have_staged_files;
7917 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7918 err = worktree_status(worktree, "", fileindex, repo,
7919 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7920 if (err)
7921 goto done;
7923 if (TAILQ_EMPTY(&commitable_paths)) {
7924 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7925 "merge of %s cannot proceed", branch_name);
7926 goto done;
7929 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7930 struct got_commitable *ct = pe->data;
7931 const char *ct_path = ct->in_repo_path;
7933 while (ct_path[0] == '/')
7934 ct_path++;
7935 err = check_out_of_date(ct_path, ct->status,
7936 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7937 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7938 if (err)
7939 goto done;
7943 mcm_arg.worktree = worktree;
7944 mcm_arg.branch_name = branch_name;
7945 err = commit_worktree(new_commit_id, &commitable_paths,
7946 head_commit_id, branch_tip, worktree, author, committer, NULL,
7947 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7948 if (err)
7949 goto done;
7951 err = update_fileindex_after_commit(worktree, &commitable_paths,
7952 *new_commit_id, fileindex, have_staged_files);
7953 sync_err = sync_fileindex(fileindex, fileindex_path);
7954 if (sync_err && err == NULL)
7955 err = sync_err;
7956 done:
7957 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7958 struct got_commitable *ct = pe->data;
7960 free_commitable(ct);
7962 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7963 free(fileindex_path);
7964 return err;
7967 const struct got_error *
7968 got_worktree_merge_complete(struct got_worktree *worktree,
7969 struct got_fileindex *fileindex, struct got_repository *repo)
7971 const struct got_error *err, *unlockerr, *sync_err;
7972 char *fileindex_path = NULL;
7974 err = delete_merge_refs(worktree, repo);
7975 if (err)
7976 goto done;
7978 err = get_fileindex_path(&fileindex_path, worktree);
7979 if (err)
7980 goto done;
7981 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7982 sync_err = sync_fileindex(fileindex, fileindex_path);
7983 if (sync_err && err == NULL)
7984 err = sync_err;
7985 done:
7986 got_fileindex_free(fileindex);
7987 free(fileindex_path);
7988 unlockerr = lock_worktree(worktree, LOCK_SH);
7989 if (unlockerr && err == NULL)
7990 err = unlockerr;
7991 return err;
7994 const struct got_error *
7995 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7996 struct got_repository *repo)
7998 const struct got_error *err;
7999 char *branch_refname = NULL;
8000 struct got_reference *branch_ref = NULL;
8002 *in_progress = 0;
8004 err = get_merge_branch_ref_name(&branch_refname, worktree);
8005 if (err)
8006 return err;
8007 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8008 free(branch_refname);
8009 if (err) {
8010 if (err->code != GOT_ERR_NOT_REF)
8011 return err;
8012 } else
8013 *in_progress = 1;
8015 return NULL;
8018 const struct got_error *got_worktree_merge_prepare(
8019 struct got_fileindex **fileindex, struct got_worktree *worktree,
8020 struct got_reference *branch, struct got_repository *repo)
8022 const struct got_error *err = NULL;
8023 char *fileindex_path = NULL;
8024 char *branch_refname = NULL, *commit_refname = NULL;
8025 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8026 struct got_reference *commit_ref = NULL;
8027 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8028 struct check_rebase_ok_arg ok_arg;
8030 *fileindex = NULL;
8032 err = lock_worktree(worktree, LOCK_EX);
8033 if (err)
8034 return err;
8036 err = open_fileindex(fileindex, &fileindex_path, worktree);
8037 if (err)
8038 goto done;
8040 /* Preconditions are the same as for rebase. */
8041 ok_arg.worktree = worktree;
8042 ok_arg.repo = repo;
8043 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8044 &ok_arg);
8045 if (err)
8046 goto done;
8048 err = get_merge_branch_ref_name(&branch_refname, worktree);
8049 if (err)
8050 return err;
8052 err = get_merge_commit_ref_name(&commit_refname, worktree);
8053 if (err)
8054 return err;
8056 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8057 0);
8058 if (err)
8059 goto done;
8061 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8062 if (err)
8063 goto done;
8065 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8066 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8067 goto done;
8070 err = got_ref_resolve(&branch_tip, repo, branch);
8071 if (err)
8072 goto done;
8074 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8075 if (err)
8076 goto done;
8077 err = got_ref_write(branch_ref, repo);
8078 if (err)
8079 goto done;
8081 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8082 if (err)
8083 goto done;
8084 err = got_ref_write(commit_ref, repo);
8085 if (err)
8086 goto done;
8088 done:
8089 free(branch_refname);
8090 free(commit_refname);
8091 free(fileindex_path);
8092 if (branch_ref)
8093 got_ref_close(branch_ref);
8094 if (commit_ref)
8095 got_ref_close(commit_ref);
8096 if (wt_branch)
8097 got_ref_close(wt_branch);
8098 free(wt_branch_tip);
8099 if (err) {
8100 if (*fileindex) {
8101 got_fileindex_free(*fileindex);
8102 *fileindex = NULL;
8104 lock_worktree(worktree, LOCK_SH);
8106 return err;
8109 const struct got_error *
8110 got_worktree_merge_continue(char **branch_name,
8111 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8112 struct got_worktree *worktree, struct got_repository *repo)
8114 const struct got_error *err;
8115 char *commit_refname = NULL, *branch_refname = NULL;
8116 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8117 char *fileindex_path = NULL;
8118 int have_staged_files = 0;
8120 *branch_name = NULL;
8121 *branch_tip = NULL;
8122 *fileindex = NULL;
8124 err = lock_worktree(worktree, LOCK_EX);
8125 if (err)
8126 return err;
8128 err = open_fileindex(fileindex, &fileindex_path, worktree);
8129 if (err)
8130 goto done;
8132 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8133 &have_staged_files);
8134 if (err && err->code != GOT_ERR_CANCELLED)
8135 goto done;
8136 if (have_staged_files) {
8137 err = got_error(GOT_ERR_STAGED_PATHS);
8138 goto done;
8141 err = get_merge_branch_ref_name(&branch_refname, worktree);
8142 if (err)
8143 goto done;
8145 err = get_merge_commit_ref_name(&commit_refname, worktree);
8146 if (err)
8147 goto done;
8149 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8150 if (err)
8151 goto done;
8153 if (!got_ref_is_symbolic(branch_ref)) {
8154 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8155 "%s is not a symbolic reference",
8156 got_ref_get_name(branch_ref));
8157 goto done;
8159 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8160 if (*branch_name == NULL) {
8161 err = got_error_from_errno("strdup");
8162 goto done;
8165 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8166 if (err)
8167 goto done;
8169 err = got_ref_resolve(branch_tip, repo, commit_ref);
8170 if (err)
8171 goto done;
8172 done:
8173 free(commit_refname);
8174 free(branch_refname);
8175 free(fileindex_path);
8176 if (commit_ref)
8177 got_ref_close(commit_ref);
8178 if (branch_ref)
8179 got_ref_close(branch_ref);
8180 if (err) {
8181 if (*branch_name) {
8182 free(*branch_name);
8183 *branch_name = NULL;
8185 free(*branch_tip);
8186 *branch_tip = NULL;
8187 if (*fileindex) {
8188 got_fileindex_free(*fileindex);
8189 *fileindex = NULL;
8191 lock_worktree(worktree, LOCK_SH);
8193 return err;
8196 const struct got_error *
8197 got_worktree_merge_abort(struct got_worktree *worktree,
8198 struct got_fileindex *fileindex, struct got_repository *repo,
8199 got_worktree_checkout_cb progress_cb, void *progress_arg)
8201 const struct got_error *err, *unlockerr, *sync_err;
8202 struct got_object_id *commit_id = NULL;
8203 struct got_commit_object *commit = NULL;
8204 char *fileindex_path = NULL;
8205 struct revert_file_args rfa;
8206 struct got_object_id *tree_id = NULL;
8208 err = got_object_open_as_commit(&commit, repo,
8209 worktree->base_commit_id);
8210 if (err)
8211 goto done;
8213 err = got_object_id_by_path(&tree_id, repo, commit,
8214 worktree->path_prefix);
8215 if (err)
8216 goto done;
8218 err = delete_merge_refs(worktree, repo);
8219 if (err)
8220 goto done;
8222 err = get_fileindex_path(&fileindex_path, worktree);
8223 if (err)
8224 goto done;
8226 rfa.worktree = worktree;
8227 rfa.fileindex = fileindex;
8228 rfa.progress_cb = progress_cb;
8229 rfa.progress_arg = progress_arg;
8230 rfa.patch_cb = NULL;
8231 rfa.patch_arg = NULL;
8232 rfa.repo = repo;
8233 rfa.unlink_added_files = 1;
8234 err = worktree_status(worktree, "", fileindex, repo,
8235 revert_file, &rfa, NULL, NULL, 1, 0);
8236 if (err)
8237 goto sync;
8239 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8240 repo, progress_cb, progress_arg, NULL, NULL);
8241 sync:
8242 sync_err = sync_fileindex(fileindex, fileindex_path);
8243 if (sync_err && err == NULL)
8244 err = sync_err;
8245 done:
8246 free(tree_id);
8247 free(commit_id);
8248 if (commit)
8249 got_object_commit_close(commit);
8250 if (fileindex)
8251 got_fileindex_free(fileindex);
8252 free(fileindex_path);
8254 unlockerr = lock_worktree(worktree, LOCK_SH);
8255 if (unlockerr && err == NULL)
8256 err = unlockerr;
8257 return err;
8260 struct check_stage_ok_arg {
8261 struct got_object_id *head_commit_id;
8262 struct got_worktree *worktree;
8263 struct got_fileindex *fileindex;
8264 struct got_repository *repo;
8265 int have_changes;
8268 static const struct got_error *
8269 check_stage_ok(void *arg, unsigned char status,
8270 unsigned char staged_status, const char *relpath,
8271 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8272 struct got_object_id *commit_id, int dirfd, const char *de_name)
8274 struct check_stage_ok_arg *a = arg;
8275 const struct got_error *err = NULL;
8276 struct got_fileindex_entry *ie;
8277 struct got_object_id base_commit_id;
8278 struct got_object_id *base_commit_idp = NULL;
8279 char *in_repo_path = NULL, *p;
8281 if (status == GOT_STATUS_UNVERSIONED ||
8282 status == GOT_STATUS_NO_CHANGE)
8283 return NULL;
8284 if (status == GOT_STATUS_NONEXISTENT)
8285 return got_error_set_errno(ENOENT, relpath);
8287 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8288 if (ie == NULL)
8289 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8291 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8292 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8293 relpath) == -1)
8294 return got_error_from_errno("asprintf");
8296 if (got_fileindex_entry_has_commit(ie)) {
8297 memcpy(base_commit_id.hash, ie->commit_hash,
8298 SHA1_DIGEST_LENGTH);
8299 base_commit_id.algo = got_repo_get_hash_algorithm(a->repo);
8300 base_commit_idp = &base_commit_id;
8303 if (status == GOT_STATUS_CONFLICT) {
8304 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8305 goto done;
8306 } else if (status != GOT_STATUS_ADD &&
8307 status != GOT_STATUS_MODIFY &&
8308 status != GOT_STATUS_DELETE) {
8309 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8310 goto done;
8313 a->have_changes = 1;
8315 p = in_repo_path;
8316 while (p[0] == '/')
8317 p++;
8318 err = check_out_of_date(p, status, staged_status,
8319 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8320 GOT_ERR_STAGE_OUT_OF_DATE);
8321 done:
8322 free(in_repo_path);
8323 return err;
8326 struct stage_path_arg {
8327 struct got_worktree *worktree;
8328 struct got_fileindex *fileindex;
8329 struct got_repository *repo;
8330 got_worktree_status_cb status_cb;
8331 void *status_arg;
8332 got_worktree_patch_cb patch_cb;
8333 void *patch_arg;
8334 int staged_something;
8335 int allow_bad_symlinks;
8338 static const struct got_error *
8339 stage_path(void *arg, unsigned char status,
8340 unsigned char staged_status, const char *relpath,
8341 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8342 struct got_object_id *commit_id, int dirfd, const char *de_name)
8344 struct stage_path_arg *a = arg;
8345 const struct got_error *err = NULL;
8346 struct got_fileindex_entry *ie;
8347 char *ondisk_path = NULL, *path_content = NULL;
8348 uint32_t stage;
8349 struct got_object_id *new_staged_blob_id = NULL;
8350 struct stat sb;
8352 if (status == GOT_STATUS_UNVERSIONED)
8353 return NULL;
8355 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8356 if (ie == NULL)
8357 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8359 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8360 relpath)== -1)
8361 return got_error_from_errno("asprintf");
8363 switch (status) {
8364 case GOT_STATUS_ADD:
8365 case GOT_STATUS_MODIFY:
8366 /* XXX could sb.st_mode be passed in by our caller? */
8367 if (lstat(ondisk_path, &sb) == -1) {
8368 err = got_error_from_errno2("lstat", ondisk_path);
8369 break;
8371 if (a->patch_cb) {
8372 if (status == GOT_STATUS_ADD) {
8373 int choice = GOT_PATCH_CHOICE_NONE;
8374 err = (*a->patch_cb)(&choice, a->patch_arg,
8375 status, ie->path, NULL, 1, 1);
8376 if (err)
8377 break;
8378 if (choice != GOT_PATCH_CHOICE_YES)
8379 break;
8380 } else {
8381 err = create_patched_content(&path_content, 0,
8382 staged_blob_id ? staged_blob_id : blob_id,
8383 ondisk_path, dirfd, de_name, ie->path,
8384 a->repo, a->patch_cb, a->patch_arg);
8385 if (err || path_content == NULL)
8386 break;
8389 err = got_object_blob_create(&new_staged_blob_id,
8390 path_content ? path_content : ondisk_path, a->repo);
8391 if (err)
8392 break;
8393 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8394 SHA1_DIGEST_LENGTH);
8395 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8396 stage = GOT_FILEIDX_STAGE_ADD;
8397 else
8398 stage = GOT_FILEIDX_STAGE_MODIFY;
8399 got_fileindex_entry_stage_set(ie, stage);
8400 if (S_ISLNK(sb.st_mode)) {
8401 int is_bad_symlink = 0;
8402 if (!a->allow_bad_symlinks) {
8403 char target_path[PATH_MAX];
8404 ssize_t target_len;
8405 target_len = readlink(ondisk_path, target_path,
8406 sizeof(target_path));
8407 if (target_len == -1) {
8408 err = got_error_from_errno2("readlink",
8409 ondisk_path);
8410 break;
8412 err = is_bad_symlink_target(&is_bad_symlink,
8413 target_path, target_len, ondisk_path,
8414 a->worktree->root_path);
8415 if (err)
8416 break;
8417 if (is_bad_symlink) {
8418 err = got_error_path(ondisk_path,
8419 GOT_ERR_BAD_SYMLINK);
8420 break;
8423 if (is_bad_symlink)
8424 got_fileindex_entry_staged_filetype_set(ie,
8425 GOT_FILEIDX_MODE_BAD_SYMLINK);
8426 else
8427 got_fileindex_entry_staged_filetype_set(ie,
8428 GOT_FILEIDX_MODE_SYMLINK);
8429 } else {
8430 got_fileindex_entry_staged_filetype_set(ie,
8431 GOT_FILEIDX_MODE_REGULAR_FILE);
8433 a->staged_something = 1;
8434 if (a->status_cb == NULL)
8435 break;
8436 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8437 get_staged_status(ie), relpath, blob_id,
8438 new_staged_blob_id, NULL, dirfd, de_name);
8439 if (err)
8440 break;
8442 * When staging the reverse of the staged diff,
8443 * implicitly unstage the file.
8445 if (memcmp(ie->staged_blob_hash, ie->blob_hash,
8446 sizeof(ie->blob_hash)) == 0) {
8447 got_fileindex_entry_stage_set(ie,
8448 GOT_FILEIDX_STAGE_NONE);
8450 break;
8451 case GOT_STATUS_DELETE:
8452 if (staged_status == GOT_STATUS_DELETE)
8453 break;
8454 if (a->patch_cb) {
8455 int choice = GOT_PATCH_CHOICE_NONE;
8456 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8457 ie->path, NULL, 1, 1);
8458 if (err)
8459 break;
8460 if (choice == GOT_PATCH_CHOICE_NO)
8461 break;
8462 if (choice != GOT_PATCH_CHOICE_YES) {
8463 err = got_error(GOT_ERR_PATCH_CHOICE);
8464 break;
8467 stage = GOT_FILEIDX_STAGE_DELETE;
8468 got_fileindex_entry_stage_set(ie, stage);
8469 a->staged_something = 1;
8470 if (a->status_cb == NULL)
8471 break;
8472 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8473 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8474 de_name);
8475 break;
8476 case GOT_STATUS_NO_CHANGE:
8477 break;
8478 case GOT_STATUS_CONFLICT:
8479 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8480 break;
8481 case GOT_STATUS_NONEXISTENT:
8482 err = got_error_set_errno(ENOENT, relpath);
8483 break;
8484 default:
8485 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8486 break;
8489 if (path_content && unlink(path_content) == -1 && err == NULL)
8490 err = got_error_from_errno2("unlink", path_content);
8491 free(path_content);
8492 free(ondisk_path);
8493 free(new_staged_blob_id);
8494 return err;
8497 const struct got_error *
8498 got_worktree_stage(struct got_worktree *worktree,
8499 struct got_pathlist_head *paths,
8500 got_worktree_status_cb status_cb, void *status_arg,
8501 got_worktree_patch_cb patch_cb, void *patch_arg,
8502 int allow_bad_symlinks, struct got_repository *repo)
8504 const struct got_error *err = NULL, *sync_err, *unlockerr;
8505 struct got_pathlist_entry *pe;
8506 struct got_fileindex *fileindex = NULL;
8507 char *fileindex_path = NULL;
8508 struct got_reference *head_ref = NULL;
8509 struct got_object_id *head_commit_id = NULL;
8510 struct check_stage_ok_arg oka;
8511 struct stage_path_arg spa;
8513 err = lock_worktree(worktree, LOCK_EX);
8514 if (err)
8515 return err;
8517 err = got_ref_open(&head_ref, repo,
8518 got_worktree_get_head_ref_name(worktree), 0);
8519 if (err)
8520 goto done;
8521 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8522 if (err)
8523 goto done;
8524 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8525 if (err)
8526 goto done;
8528 /* Check pre-conditions before staging anything. */
8529 oka.head_commit_id = head_commit_id;
8530 oka.worktree = worktree;
8531 oka.fileindex = fileindex;
8532 oka.repo = repo;
8533 oka.have_changes = 0;
8534 TAILQ_FOREACH(pe, paths, entry) {
8535 err = worktree_status(worktree, pe->path, fileindex, repo,
8536 check_stage_ok, &oka, NULL, NULL, 1, 0);
8537 if (err)
8538 goto done;
8540 if (!oka.have_changes) {
8541 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8542 goto done;
8545 spa.worktree = worktree;
8546 spa.fileindex = fileindex;
8547 spa.repo = repo;
8548 spa.patch_cb = patch_cb;
8549 spa.patch_arg = patch_arg;
8550 spa.status_cb = status_cb;
8551 spa.status_arg = status_arg;
8552 spa.staged_something = 0;
8553 spa.allow_bad_symlinks = allow_bad_symlinks;
8554 TAILQ_FOREACH(pe, paths, entry) {
8555 err = worktree_status(worktree, pe->path, fileindex, repo,
8556 stage_path, &spa, NULL, NULL, 1, 0);
8557 if (err)
8558 goto done;
8560 if (!spa.staged_something) {
8561 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8562 goto done;
8565 sync_err = sync_fileindex(fileindex, fileindex_path);
8566 if (sync_err && err == NULL)
8567 err = sync_err;
8568 done:
8569 if (head_ref)
8570 got_ref_close(head_ref);
8571 free(head_commit_id);
8572 free(fileindex_path);
8573 if (fileindex)
8574 got_fileindex_free(fileindex);
8575 unlockerr = lock_worktree(worktree, LOCK_SH);
8576 if (unlockerr && err == NULL)
8577 err = unlockerr;
8578 return err;
8581 struct unstage_path_arg {
8582 struct got_worktree *worktree;
8583 struct got_fileindex *fileindex;
8584 struct got_repository *repo;
8585 got_worktree_checkout_cb progress_cb;
8586 void *progress_arg;
8587 got_worktree_patch_cb patch_cb;
8588 void *patch_arg;
8591 static const struct got_error *
8592 create_unstaged_content(char **path_unstaged_content,
8593 char **path_new_staged_content, struct got_object_id *blob_id,
8594 struct got_object_id *staged_blob_id, const char *relpath,
8595 struct got_repository *repo,
8596 got_worktree_patch_cb patch_cb, void *patch_arg)
8598 const struct got_error *err, *free_err;
8599 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8600 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8601 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8602 struct got_diffreg_result *diffreg_result = NULL;
8603 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8604 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8605 int fd1 = -1, fd2 = -1;
8607 *path_unstaged_content = NULL;
8608 *path_new_staged_content = NULL;
8610 err = got_object_id_str(&label1, blob_id);
8611 if (err)
8612 return err;
8614 fd1 = got_opentempfd();
8615 if (fd1 == -1) {
8616 err = got_error_from_errno("got_opentempfd");
8617 goto done;
8619 fd2 = got_opentempfd();
8620 if (fd2 == -1) {
8621 err = got_error_from_errno("got_opentempfd");
8622 goto done;
8625 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8626 if (err)
8627 goto done;
8629 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8630 if (err)
8631 goto done;
8633 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8634 if (err)
8635 goto done;
8637 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8638 fd2);
8639 if (err)
8640 goto done;
8642 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8643 if (err)
8644 goto done;
8646 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8647 if (err)
8648 goto done;
8650 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8651 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8652 if (err)
8653 goto done;
8655 err = got_opentemp_named(path_unstaged_content, &outfile,
8656 "got-unstaged-content", "");
8657 if (err)
8658 goto done;
8659 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8660 "got-new-staged-content", "");
8661 if (err)
8662 goto done;
8664 if (fseek(f1, 0L, SEEK_SET) == -1) {
8665 err = got_ferror(f1, GOT_ERR_IO);
8666 goto done;
8668 if (fseek(f2, 0L, SEEK_SET) == -1) {
8669 err = got_ferror(f2, GOT_ERR_IO);
8670 goto done;
8672 /* Count the number of actual changes in the diff result. */
8673 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8674 struct diff_chunk_context cc = {};
8675 diff_chunk_context_load_change(&cc, &nchunks_used,
8676 diffreg_result->result, n, 0);
8677 nchanges++;
8679 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8680 int choice;
8681 err = apply_or_reject_change(&choice, &nchunks_used,
8682 diffreg_result->result, n, relpath, f1, f2,
8683 &line_cur1, &line_cur2,
8684 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8685 if (err)
8686 goto done;
8687 if (choice == GOT_PATCH_CHOICE_YES)
8688 have_content = 1;
8689 else
8690 have_rejected_content = 1;
8691 if (choice == GOT_PATCH_CHOICE_QUIT)
8692 break;
8694 if (have_content || have_rejected_content)
8695 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8696 outfile, rejectfile);
8697 done:
8698 free(label1);
8699 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8700 err = got_error_from_errno("close");
8701 if (blob)
8702 got_object_blob_close(blob);
8703 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8704 err = got_error_from_errno("close");
8705 if (staged_blob)
8706 got_object_blob_close(staged_blob);
8707 free_err = got_diffreg_result_free(diffreg_result);
8708 if (free_err && err == NULL)
8709 err = free_err;
8710 if (f1 && fclose(f1) == EOF && err == NULL)
8711 err = got_error_from_errno2("fclose", path1);
8712 if (f2 && fclose(f2) == EOF && err == NULL)
8713 err = got_error_from_errno2("fclose", path2);
8714 if (outfile && fclose(outfile) == EOF && err == NULL)
8715 err = got_error_from_errno2("fclose", *path_unstaged_content);
8716 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8717 err = got_error_from_errno2("fclose", *path_new_staged_content);
8718 if (path1 && unlink(path1) == -1 && err == NULL)
8719 err = got_error_from_errno2("unlink", path1);
8720 if (path2 && unlink(path2) == -1 && err == NULL)
8721 err = got_error_from_errno2("unlink", path2);
8722 if (err || !have_content) {
8723 if (*path_unstaged_content &&
8724 unlink(*path_unstaged_content) == -1 && err == NULL)
8725 err = got_error_from_errno2("unlink",
8726 *path_unstaged_content);
8727 free(*path_unstaged_content);
8728 *path_unstaged_content = NULL;
8730 if (err || !have_content || !have_rejected_content) {
8731 if (*path_new_staged_content &&
8732 unlink(*path_new_staged_content) == -1 && err == NULL)
8733 err = got_error_from_errno2("unlink",
8734 *path_new_staged_content);
8735 free(*path_new_staged_content);
8736 *path_new_staged_content = NULL;
8738 free(path1);
8739 free(path2);
8740 return err;
8743 static const struct got_error *
8744 unstage_hunks(struct got_object_id *staged_blob_id,
8745 struct got_blob_object *blob_base,
8746 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8747 const char *ondisk_path, const char *label_orig,
8748 struct got_worktree *worktree, struct got_repository *repo,
8749 got_worktree_patch_cb patch_cb, void *patch_arg,
8750 got_worktree_checkout_cb progress_cb, void *progress_arg)
8752 const struct got_error *err = NULL;
8753 char *path_unstaged_content = NULL;
8754 char *path_new_staged_content = NULL;
8755 char *parent = NULL, *base_path = NULL;
8756 char *blob_base_path = NULL;
8757 struct got_object_id *new_staged_blob_id = NULL;
8758 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8759 struct stat sb;
8761 err = create_unstaged_content(&path_unstaged_content,
8762 &path_new_staged_content, blob_id, staged_blob_id,
8763 ie->path, repo, patch_cb, patch_arg);
8764 if (err)
8765 return err;
8767 if (path_unstaged_content == NULL)
8768 return NULL;
8770 if (path_new_staged_content) {
8771 err = got_object_blob_create(&new_staged_blob_id,
8772 path_new_staged_content, repo);
8773 if (err)
8774 goto done;
8777 f = fopen(path_unstaged_content, "re");
8778 if (f == NULL) {
8779 err = got_error_from_errno2("fopen",
8780 path_unstaged_content);
8781 goto done;
8783 if (fstat(fileno(f), &sb) == -1) {
8784 err = got_error_from_errno2("fstat", path_unstaged_content);
8785 goto done;
8787 if (got_fileindex_entry_staged_filetype_get(ie) ==
8788 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8789 char link_target[PATH_MAX];
8790 size_t r;
8791 r = fread(link_target, 1, sizeof(link_target), f);
8792 if (r == 0 && ferror(f)) {
8793 err = got_error_from_errno("fread");
8794 goto done;
8796 if (r >= sizeof(link_target)) { /* should not happen */
8797 err = got_error(GOT_ERR_NO_SPACE);
8798 goto done;
8800 link_target[r] = '\0';
8801 err = merge_symlink(worktree, blob_base,
8802 ondisk_path, ie->path, label_orig, link_target,
8803 worktree->base_commit_id, repo, progress_cb,
8804 progress_arg);
8805 } else {
8806 int local_changes_subsumed;
8808 err = got_path_dirname(&parent, ondisk_path);
8809 if (err)
8810 return err;
8812 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8813 parent) == -1) {
8814 err = got_error_from_errno("asprintf");
8815 base_path = NULL;
8816 goto done;
8819 err = got_opentemp_named(&blob_base_path, &f_base,
8820 base_path, "");
8821 if (err)
8822 goto done;
8823 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8824 blob_base);
8825 if (err)
8826 goto done;
8829 * In order the run a 3-way merge with a symlink we copy the symlink's
8830 * target path into a temporary file and use that file with diff3.
8832 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8833 err = dump_symlink_target_path_to_file(&f_deriv2,
8834 ondisk_path);
8835 if (err)
8836 goto done;
8837 } else {
8838 int fd;
8839 fd = open(ondisk_path,
8840 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8841 if (fd == -1) {
8842 err = got_error_from_errno2("open", ondisk_path);
8843 goto done;
8845 f_deriv2 = fdopen(fd, "r");
8846 if (f_deriv2 == NULL) {
8847 err = got_error_from_errno2("fdopen", ondisk_path);
8848 close(fd);
8849 goto done;
8853 err = merge_file(&local_changes_subsumed, worktree,
8854 f_base, f, f_deriv2, ondisk_path, ie->path,
8855 got_fileindex_perms_to_st(ie),
8856 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8857 repo, progress_cb, progress_arg);
8859 if (err)
8860 goto done;
8862 if (new_staged_blob_id) {
8863 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8864 SHA1_DIGEST_LENGTH);
8865 } else {
8866 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8867 got_fileindex_entry_staged_filetype_set(ie, 0);
8869 done:
8870 free(new_staged_blob_id);
8871 if (path_unstaged_content &&
8872 unlink(path_unstaged_content) == -1 && err == NULL)
8873 err = got_error_from_errno2("unlink", path_unstaged_content);
8874 if (path_new_staged_content &&
8875 unlink(path_new_staged_content) == -1 && err == NULL)
8876 err = got_error_from_errno2("unlink", path_new_staged_content);
8877 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8878 err = got_error_from_errno2("unlink", blob_base_path);
8879 if (f_base && fclose(f_base) == EOF && err == NULL)
8880 err = got_error_from_errno2("fclose", path_unstaged_content);
8881 if (f && fclose(f) == EOF && err == NULL)
8882 err = got_error_from_errno2("fclose", path_unstaged_content);
8883 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8884 err = got_error_from_errno2("fclose", ondisk_path);
8885 free(path_unstaged_content);
8886 free(path_new_staged_content);
8887 free(blob_base_path);
8888 free(parent);
8889 free(base_path);
8890 return err;
8893 static const struct got_error *
8894 unstage_path(void *arg, unsigned char status,
8895 unsigned char staged_status, const char *relpath,
8896 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8897 struct got_object_id *commit_id, int dirfd, const char *de_name)
8899 const struct got_error *err = NULL;
8900 struct unstage_path_arg *a = arg;
8901 struct got_fileindex_entry *ie;
8902 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8903 char *ondisk_path = NULL;
8904 char *id_str = NULL, *label_orig = NULL;
8905 int local_changes_subsumed;
8906 struct stat sb;
8907 int fd1 = -1, fd2 = -1;
8909 if (staged_status != GOT_STATUS_ADD &&
8910 staged_status != GOT_STATUS_MODIFY &&
8911 staged_status != GOT_STATUS_DELETE)
8912 return NULL;
8914 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8915 if (ie == NULL)
8916 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8918 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8919 == -1)
8920 return got_error_from_errno("asprintf");
8922 err = got_object_id_str(&id_str,
8923 commit_id ? commit_id : a->worktree->base_commit_id);
8924 if (err)
8925 goto done;
8926 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8927 id_str) == -1) {
8928 err = got_error_from_errno("asprintf");
8929 goto done;
8932 fd1 = got_opentempfd();
8933 if (fd1 == -1) {
8934 err = got_error_from_errno("got_opentempfd");
8935 goto done;
8937 fd2 = got_opentempfd();
8938 if (fd2 == -1) {
8939 err = got_error_from_errno("got_opentempfd");
8940 goto done;
8943 switch (staged_status) {
8944 case GOT_STATUS_MODIFY:
8945 err = got_object_open_as_blob(&blob_base, a->repo,
8946 blob_id, 8192, fd1);
8947 if (err)
8948 break;
8949 /* fall through */
8950 case GOT_STATUS_ADD:
8951 if (a->patch_cb) {
8952 if (staged_status == GOT_STATUS_ADD) {
8953 int choice = GOT_PATCH_CHOICE_NONE;
8954 err = (*a->patch_cb)(&choice, a->patch_arg,
8955 staged_status, ie->path, NULL, 1, 1);
8956 if (err)
8957 break;
8958 if (choice != GOT_PATCH_CHOICE_YES)
8959 break;
8960 } else {
8961 err = unstage_hunks(staged_blob_id,
8962 blob_base, blob_id, ie, ondisk_path,
8963 label_orig, a->worktree, a->repo,
8964 a->patch_cb, a->patch_arg,
8965 a->progress_cb, a->progress_arg);
8966 break; /* Done with this file. */
8969 err = got_object_open_as_blob(&blob_staged, a->repo,
8970 staged_blob_id, 8192, fd2);
8971 if (err)
8972 break;
8973 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8974 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8975 case GOT_FILEIDX_MODE_REGULAR_FILE:
8976 err = merge_blob(&local_changes_subsumed, a->worktree,
8977 blob_base, ondisk_path, relpath,
8978 got_fileindex_perms_to_st(ie), label_orig,
8979 blob_staged, commit_id ? commit_id :
8980 a->worktree->base_commit_id, a->repo,
8981 a->progress_cb, a->progress_arg);
8982 break;
8983 case GOT_FILEIDX_MODE_SYMLINK:
8984 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8985 char *staged_target;
8986 err = got_object_blob_read_to_str(
8987 &staged_target, blob_staged);
8988 if (err)
8989 goto done;
8990 err = merge_symlink(a->worktree, blob_base,
8991 ondisk_path, relpath, label_orig,
8992 staged_target, commit_id ? commit_id :
8993 a->worktree->base_commit_id,
8994 a->repo, a->progress_cb, a->progress_arg);
8995 free(staged_target);
8996 } else {
8997 err = merge_blob(&local_changes_subsumed,
8998 a->worktree, blob_base, ondisk_path,
8999 relpath, got_fileindex_perms_to_st(ie),
9000 label_orig, blob_staged,
9001 commit_id ? commit_id :
9002 a->worktree->base_commit_id, a->repo,
9003 a->progress_cb, a->progress_arg);
9005 break;
9006 default:
9007 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9008 break;
9010 if (err == NULL) {
9011 got_fileindex_entry_stage_set(ie,
9012 GOT_FILEIDX_STAGE_NONE);
9013 got_fileindex_entry_staged_filetype_set(ie, 0);
9015 break;
9016 case GOT_STATUS_DELETE:
9017 if (a->patch_cb) {
9018 int choice = GOT_PATCH_CHOICE_NONE;
9019 err = (*a->patch_cb)(&choice, a->patch_arg,
9020 staged_status, ie->path, NULL, 1, 1);
9021 if (err)
9022 break;
9023 if (choice == GOT_PATCH_CHOICE_NO)
9024 break;
9025 if (choice != GOT_PATCH_CHOICE_YES) {
9026 err = got_error(GOT_ERR_PATCH_CHOICE);
9027 break;
9030 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9031 got_fileindex_entry_staged_filetype_set(ie, 0);
9032 err = get_file_status(&status, &sb, ie, ondisk_path,
9033 dirfd, de_name, a->repo);
9034 if (err)
9035 break;
9036 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9037 break;
9039 done:
9040 free(ondisk_path);
9041 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9042 err = got_error_from_errno("close");
9043 if (blob_base)
9044 got_object_blob_close(blob_base);
9045 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9046 err = got_error_from_errno("close");
9047 if (blob_staged)
9048 got_object_blob_close(blob_staged);
9049 free(id_str);
9050 free(label_orig);
9051 return err;
9054 const struct got_error *
9055 got_worktree_unstage(struct got_worktree *worktree,
9056 struct got_pathlist_head *paths,
9057 got_worktree_checkout_cb progress_cb, void *progress_arg,
9058 got_worktree_patch_cb patch_cb, void *patch_arg,
9059 struct got_repository *repo)
9061 const struct got_error *err = NULL, *sync_err, *unlockerr;
9062 struct got_pathlist_entry *pe;
9063 struct got_fileindex *fileindex = NULL;
9064 char *fileindex_path = NULL;
9065 struct unstage_path_arg upa;
9067 err = lock_worktree(worktree, LOCK_EX);
9068 if (err)
9069 return err;
9071 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9072 if (err)
9073 goto done;
9075 upa.worktree = worktree;
9076 upa.fileindex = fileindex;
9077 upa.repo = repo;
9078 upa.progress_cb = progress_cb;
9079 upa.progress_arg = progress_arg;
9080 upa.patch_cb = patch_cb;
9081 upa.patch_arg = patch_arg;
9082 TAILQ_FOREACH(pe, paths, entry) {
9083 err = worktree_status(worktree, pe->path, fileindex, repo,
9084 unstage_path, &upa, NULL, NULL, 1, 0);
9085 if (err)
9086 goto done;
9089 sync_err = sync_fileindex(fileindex, fileindex_path);
9090 if (sync_err && err == NULL)
9091 err = sync_err;
9092 done:
9093 free(fileindex_path);
9094 if (fileindex)
9095 got_fileindex_free(fileindex);
9096 unlockerr = lock_worktree(worktree, LOCK_SH);
9097 if (unlockerr && err == NULL)
9098 err = unlockerr;
9099 return err;
9102 struct report_file_info_arg {
9103 struct got_worktree *worktree;
9104 got_worktree_path_info_cb info_cb;
9105 void *info_arg;
9106 struct got_pathlist_head *paths;
9107 got_cancel_cb cancel_cb;
9108 void *cancel_arg;
9111 static const struct got_error *
9112 report_file_info(void *arg, struct got_fileindex_entry *ie)
9114 struct report_file_info_arg *a = arg;
9115 struct got_pathlist_entry *pe;
9116 struct got_object_id blob_id, staged_blob_id, commit_id;
9117 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9118 struct got_object_id *commit_idp = NULL;
9119 int stage;
9121 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9122 return got_error(GOT_ERR_CANCELLED);
9124 TAILQ_FOREACH(pe, a->paths, entry) {
9125 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9126 got_path_is_child(ie->path, pe->path, pe->path_len))
9127 break;
9129 if (pe == NULL) /* not found */
9130 return NULL;
9132 if (got_fileindex_entry_has_blob(ie)) {
9133 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
9134 blob_id.algo = WORKTREE_ALGO(a->worktree);
9135 blob_idp = &blob_id;
9137 stage = got_fileindex_entry_stage_get(ie);
9138 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9139 stage == GOT_FILEIDX_STAGE_ADD) {
9140 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
9141 SHA1_DIGEST_LENGTH);
9142 staged_blob_id.algo = WORKTREE_ALGO(a->worktree);
9143 staged_blob_idp = &staged_blob_id;
9146 if (got_fileindex_entry_has_commit(ie)) {
9147 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
9148 commit_id.algo = WORKTREE_ALGO(a->worktree);
9149 commit_idp = &commit_id;
9152 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9153 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9156 const struct got_error *
9157 got_worktree_path_info(struct got_worktree *worktree,
9158 struct got_pathlist_head *paths,
9159 got_worktree_path_info_cb info_cb, void *info_arg,
9160 got_cancel_cb cancel_cb, void *cancel_arg)
9163 const struct got_error *err = NULL, *unlockerr;
9164 struct got_fileindex *fileindex = NULL;
9165 char *fileindex_path = NULL;
9166 struct report_file_info_arg arg;
9168 err = lock_worktree(worktree, LOCK_SH);
9169 if (err)
9170 return err;
9172 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9173 if (err)
9174 goto done;
9176 arg.worktree = worktree;
9177 arg.info_cb = info_cb;
9178 arg.info_arg = info_arg;
9179 arg.paths = paths;
9180 arg.cancel_cb = cancel_cb;
9181 arg.cancel_arg = cancel_arg;
9182 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9183 &arg);
9184 done:
9185 free(fileindex_path);
9186 if (fileindex)
9187 got_fileindex_free(fileindex);
9188 unlockerr = lock_worktree(worktree, LOCK_UN);
9189 if (unlockerr && err == NULL)
9190 err = unlockerr;
9191 return err;
9194 static const struct got_error *
9195 patch_check_path(const char *p, char **path, unsigned char *status,
9196 unsigned char *staged_status, struct got_fileindex *fileindex,
9197 struct got_worktree *worktree, struct got_repository *repo)
9199 const struct got_error *err;
9200 struct got_fileindex_entry *ie;
9201 struct stat sb;
9202 char *ondisk_path = NULL;
9204 err = got_worktree_resolve_path(path, worktree, p);
9205 if (err)
9206 return err;
9208 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9209 *path[0] ? "/" : "", *path) == -1)
9210 return got_error_from_errno("asprintf");
9212 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9213 if (ie) {
9214 *staged_status = get_staged_status(ie);
9215 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9216 repo);
9217 if (err)
9218 goto done;
9219 } else {
9220 *staged_status = GOT_STATUS_NO_CHANGE;
9221 *status = GOT_STATUS_UNVERSIONED;
9222 if (lstat(ondisk_path, &sb) == -1) {
9223 if (errno != ENOENT) {
9224 err = got_error_from_errno2("lstat",
9225 ondisk_path);
9226 goto done;
9228 *status = GOT_STATUS_NONEXISTENT;
9232 done:
9233 free(ondisk_path);
9234 return err;
9237 static const struct got_error *
9238 patch_can_rm(const char *path, unsigned char status,
9239 unsigned char staged_status)
9241 if (status == GOT_STATUS_NONEXISTENT)
9242 return got_error_set_errno(ENOENT, path);
9243 if (status != GOT_STATUS_NO_CHANGE &&
9244 status != GOT_STATUS_ADD &&
9245 status != GOT_STATUS_MODIFY &&
9246 status != GOT_STATUS_MODE_CHANGE)
9247 return got_error_path(path, GOT_ERR_FILE_STATUS);
9248 if (staged_status == GOT_STATUS_DELETE)
9249 return got_error_path(path, GOT_ERR_FILE_STATUS);
9250 return NULL;
9253 static const struct got_error *
9254 patch_can_add(const char *path, unsigned char status)
9256 if (status != GOT_STATUS_NONEXISTENT)
9257 return got_error_path(path, GOT_ERR_FILE_STATUS);
9258 return NULL;
9261 static const struct got_error *
9262 patch_can_edit(const char *path, unsigned char status,
9263 unsigned char staged_status)
9265 if (status == GOT_STATUS_NONEXISTENT)
9266 return got_error_set_errno(ENOENT, path);
9267 if (status != GOT_STATUS_NO_CHANGE &&
9268 status != GOT_STATUS_ADD &&
9269 status != GOT_STATUS_MODIFY)
9270 return got_error_path(path, GOT_ERR_FILE_STATUS);
9271 if (staged_status == GOT_STATUS_DELETE)
9272 return got_error_path(path, GOT_ERR_FILE_STATUS);
9273 return NULL;
9276 const struct got_error *
9277 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9278 char **fileindex_path, struct got_worktree *worktree)
9280 return open_fileindex(fileindex, fileindex_path, worktree);
9283 const struct got_error *
9284 got_worktree_patch_check_path(const char *old, const char *new,
9285 char **oldpath, char **newpath, struct got_worktree *worktree,
9286 struct got_repository *repo, struct got_fileindex *fileindex)
9288 const struct got_error *err = NULL;
9289 int file_renamed = 0;
9290 unsigned char status_old, staged_status_old;
9291 unsigned char status_new, staged_status_new;
9293 *oldpath = NULL;
9294 *newpath = NULL;
9296 err = patch_check_path(old != NULL ? old : new, oldpath,
9297 &status_old, &staged_status_old, fileindex, worktree, repo);
9298 if (err)
9299 goto done;
9301 err = patch_check_path(new != NULL ? new : old, newpath,
9302 &status_new, &staged_status_new, fileindex, worktree, repo);
9303 if (err)
9304 goto done;
9306 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9307 file_renamed = 1;
9309 if (old != NULL && new == NULL)
9310 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9311 else if (file_renamed) {
9312 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9313 if (err == NULL)
9314 err = patch_can_add(*newpath, status_new);
9315 } else if (old == NULL)
9316 err = patch_can_add(*newpath, status_new);
9317 else
9318 err = patch_can_edit(*newpath, status_new, staged_status_new);
9320 done:
9321 if (err) {
9322 free(*oldpath);
9323 *oldpath = NULL;
9324 free(*newpath);
9325 *newpath = NULL;
9327 return err;
9330 const struct got_error *
9331 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9332 struct got_worktree *worktree, struct got_fileindex *fileindex,
9333 got_worktree_checkout_cb progress_cb, void *progress_arg)
9335 struct schedule_addition_args saa;
9337 memset(&saa, 0, sizeof(saa));
9338 saa.worktree = worktree;
9339 saa.fileindex = fileindex;
9340 saa.progress_cb = progress_cb;
9341 saa.progress_arg = progress_arg;
9342 saa.repo = repo;
9344 return worktree_status(worktree, path, fileindex, repo,
9345 schedule_addition, &saa, NULL, NULL, 1, 0);
9348 const struct got_error *
9349 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9350 struct got_worktree *worktree, struct got_fileindex *fileindex,
9351 got_worktree_delete_cb progress_cb, void *progress_arg)
9353 struct schedule_deletion_args sda;
9355 memset(&sda, 0, sizeof(sda));
9356 sda.worktree = worktree;
9357 sda.fileindex = fileindex;
9358 sda.progress_cb = progress_cb;
9359 sda.progress_arg = progress_arg;
9360 sda.repo = repo;
9361 sda.delete_local_mods = 0;
9362 sda.keep_on_disk = 0;
9363 sda.ignore_missing_paths = 0;
9364 sda.status_codes = NULL;
9366 return worktree_status(worktree, path, fileindex, repo,
9367 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9370 const struct got_error *
9371 got_worktree_patch_complete(struct got_fileindex *fileindex,
9372 const char *fileindex_path)
9374 const struct got_error *err = NULL;
9376 err = sync_fileindex(fileindex, fileindex_path);
9377 got_fileindex_free(fileindex);
9379 return err;