Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
19 #include <dirent.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <zlib.h>
30 #include <fnmatch.h>
31 #include <libgen.h>
33 #include "got_compat.h"
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
39 #include "got_path.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
43 #include "got_diff.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
55 #include "got_lib_gotconfig.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 #define GOT_MERGE_LABEL_MERGED "merged change"
62 #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 static const struct got_error *
65 create_meta_file(const char *path_got, const char *name, const char *content)
66 {
67 const struct got_error *err = NULL;
68 char *path;
70 if (asprintf(&path, "%s/%s", path_got, name) == -1)
71 return got_error_from_errno("asprintf");
73 err = got_path_create_file(path, content);
74 free(path);
75 return err;
76 }
78 static const struct got_error *
79 update_meta_file(const char *path_got, const char *name, const char *content)
80 {
81 const struct got_error *err = NULL;
82 FILE *tmpfile = NULL;
83 char *tmppath = NULL;
84 char *path = NULL;
86 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
87 err = got_error_from_errno("asprintf");
88 path = NULL;
89 goto done;
90 }
92 err = got_opentemp_named(&tmppath, &tmpfile, path);
93 if (err)
94 goto done;
96 if (content) {
97 int len = fprintf(tmpfile, "%s\n", content);
98 if (len != strlen(content) + 1) {
99 err = got_error_from_errno2("fprintf", tmppath);
100 goto done;
104 if (rename(tmppath, path) != 0) {
105 err = got_error_from_errno3("rename", tmppath, path);
106 unlink(tmppath);
107 goto done;
110 done:
111 if (fclose(tmpfile) == EOF && err == NULL)
112 err = got_error_from_errno2("fclose", tmppath);
113 free(tmppath);
114 return err;
117 static const struct got_error *
118 write_head_ref(const char *path_got, struct got_reference *head_ref)
120 const struct got_error *err = NULL;
121 char *refstr = NULL;
123 if (got_ref_is_symbolic(head_ref)) {
124 refstr = got_ref_to_str(head_ref);
125 if (refstr == NULL)
126 return got_error_from_errno("got_ref_to_str");
127 } else {
128 refstr = strdup(got_ref_get_name(head_ref));
129 if (refstr == NULL)
130 return got_error_from_errno("strdup");
132 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
133 free(refstr);
134 return err;
137 const struct got_error *
138 got_worktree_init(const char *path, struct got_reference *head_ref,
139 const char *prefix, struct got_repository *repo)
141 const struct got_error *err = NULL;
142 struct got_object_id *commit_id = NULL;
143 uuid_t uuid;
144 uint32_t uuid_status;
145 int obj_type;
146 char *path_got = NULL;
147 char *formatstr = NULL;
148 char *absprefix = NULL;
149 char *basestr = NULL;
150 char *uuidstr = NULL;
152 if (strcmp(path, got_repo_get_path(repo)) == 0) {
153 err = got_error(GOT_ERR_WORKTREE_REPO);
154 goto done;
157 err = got_ref_resolve(&commit_id, repo, head_ref);
158 if (err)
159 return err;
160 err = got_object_get_type(&obj_type, repo, commit_id);
161 if (err)
162 return err;
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error(GOT_ERR_OBJ_TYPE);
166 if (!got_path_is_absolute(prefix)) {
167 if (asprintf(&absprefix, "/%s", prefix) == -1)
168 return got_error_from_errno("asprintf");
171 /* Create top-level directory (may already exist). */
172 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 err = got_error_from_errno2("mkdir", path);
174 goto done;
177 /* Create .got directory (may already exist). */
178 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 err = got_error_from_errno("asprintf");
180 goto done;
182 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno2("mkdir", path_got);
184 goto done;
187 /* Create an empty lock file. */
188 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
189 if (err)
190 goto done;
192 /* Create an empty file index. */
193 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
194 if (err)
195 goto done;
197 /* Write the HEAD reference. */
198 err = write_head_ref(path_got, head_ref);
199 if (err)
200 goto done;
202 /* Record our base commit. */
203 err = got_object_id_str(&basestr, commit_id);
204 if (err)
205 goto done;
206 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
207 if (err)
208 goto done;
210 /* Store path to repository. */
211 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
212 got_repo_get_path(repo));
213 if (err)
214 goto done;
216 /* Store in-repository path prefix. */
217 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
218 absprefix ? absprefix : prefix);
219 if (err)
220 goto done;
222 /* Generate UUID. */
223 uuid_create(&uuid, &uuid_status);
224 if (uuid_status != uuid_s_ok) {
225 err = got_error_uuid(uuid_status, "uuid_create");
226 goto done;
228 uuid_to_string(&uuid, &uuidstr, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_to_string");
231 goto done;
233 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
234 if (err)
235 goto done;
237 /* Stamp work tree with format file. */
238 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
239 err = got_error_from_errno("asprintf");
240 goto done;
242 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
243 if (err)
244 goto done;
246 done:
247 free(commit_id);
248 free(path_got);
249 free(formatstr);
250 free(absprefix);
251 free(basestr);
252 free(uuidstr);
253 return err;
256 const struct got_error *
257 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
258 const char *path_prefix)
260 char *absprefix = NULL;
262 if (!got_path_is_absolute(path_prefix)) {
263 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
264 return got_error_from_errno("asprintf");
266 *match = (strcmp(absprefix ? absprefix : path_prefix,
267 worktree->path_prefix) == 0);
268 free(absprefix);
269 return NULL;
272 const char *
273 got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 return worktree->head_ref_name;
278 const struct got_error *
279 got_worktree_set_head_ref(struct got_worktree *worktree,
280 struct got_reference *head_ref)
282 const struct got_error *err = NULL;
283 char *path_got = NULL, *head_ref_name = NULL;
285 if (asprintf(&path_got, "%s/%s", worktree->root_path,
286 GOT_WORKTREE_GOT_DIR) == -1) {
287 err = got_error_from_errno("asprintf");
288 path_got = NULL;
289 goto done;
292 head_ref_name = strdup(got_ref_get_name(head_ref));
293 if (head_ref_name == NULL) {
294 err = got_error_from_errno("strdup");
295 goto done;
298 err = write_head_ref(path_got, head_ref);
299 if (err)
300 goto done;
302 free(worktree->head_ref_name);
303 worktree->head_ref_name = head_ref_name;
304 done:
305 free(path_got);
306 if (err)
307 free(head_ref_name);
308 return err;
311 struct got_object_id *
312 got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 return worktree->base_commit_id;
317 const struct got_error *
318 got_worktree_set_base_commit_id(struct got_worktree *worktree,
319 struct got_repository *repo, struct got_object_id *commit_id)
321 const struct got_error *err;
322 struct got_object *obj = NULL;
323 char *id_str = NULL;
324 char *path_got = NULL;
326 if (asprintf(&path_got, "%s/%s", worktree->root_path,
327 GOT_WORKTREE_GOT_DIR) == -1) {
328 err = got_error_from_errno("asprintf");
329 path_got = NULL;
330 goto done;
333 err = got_object_open(&obj, repo, commit_id);
334 if (err)
335 return err;
337 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
339 goto done;
342 /* Record our base commit. */
343 err = got_object_id_str(&id_str, commit_id);
344 if (err)
345 goto done;
346 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
347 if (err)
348 goto done;
350 free(worktree->base_commit_id);
351 worktree->base_commit_id = got_object_id_dup(commit_id);
352 if (worktree->base_commit_id == NULL) {
353 err = got_error_from_errno("got_object_id_dup");
354 goto done;
356 done:
357 if (obj)
358 got_object_close(obj);
359 free(id_str);
360 free(path_got);
361 return err;
364 const struct got_gotconfig *
365 got_worktree_get_gotconfig(struct got_worktree *worktree)
367 return worktree->gotconfig;
370 static const struct got_error *
371 lock_worktree(struct got_worktree *worktree, int operation)
373 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
374 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
375 : got_error_from_errno2("flock",
376 got_worktree_get_root_path(worktree)));
377 return NULL;
380 static const struct got_error *
381 add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 const struct got_error *err = NULL;
384 char *abspath;
386 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
387 return got_error_from_errno("asprintf");
389 err = got_path_mkdir(abspath);
390 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
391 struct stat sb;
392 err = NULL;
393 if (lstat(abspath, &sb) == -1) {
394 err = got_error_from_errno2("lstat", abspath);
395 } else if (!S_ISDIR(sb.st_mode)) {
396 /* TODO directory is obstructed; do something */
397 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
400 free(abspath);
401 return err;
404 static const struct got_error *
405 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 const struct got_error *err = NULL;
408 uint8_t fbuf1[8192];
409 uint8_t fbuf2[8192];
410 size_t flen1 = 0, flen2 = 0;
412 *same = 1;
414 for (;;) {
415 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
416 if (flen1 == 0 && ferror(f1)) {
417 err = got_error_from_errno("fread");
418 break;
420 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
421 if (flen2 == 0 && ferror(f2)) {
422 err = got_error_from_errno("fread");
423 break;
425 if (flen1 == 0) {
426 if (flen2 != 0)
427 *same = 0;
428 break;
429 } else if (flen2 == 0) {
430 if (flen1 != 0)
431 *same = 0;
432 break;
433 } else if (flen1 == flen2) {
434 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
435 *same = 0;
436 break;
438 } else {
439 *same = 0;
440 break;
444 return err;
447 static const struct got_error *
448 check_files_equal(int *same, FILE *f1, FILE *f2)
450 struct stat sb;
451 size_t size1, size2;
453 *same = 1;
455 if (fstat(fileno(f1), &sb) != 0)
456 return got_error_from_errno("fstat");
457 size1 = sb.st_size;
459 if (fstat(fileno(f2), &sb) != 0)
460 return got_error_from_errno("fstat");
461 size2 = sb.st_size;
463 if (size1 != size2) {
464 *same = 0;
465 return NULL;
468 if (fseek(f1, 0L, SEEK_SET) == -1)
469 return got_ferror(f1, GOT_ERR_IO);
470 if (fseek(f2, 0L, SEEK_SET) == -1)
471 return got_ferror(f2, GOT_ERR_IO);
473 return check_file_contents_equal(same, f1, f2);
476 static const struct got_error *
477 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
479 uint8_t fbuf[65536];
480 size_t flen;
481 ssize_t outlen;
483 *outsize = 0;
485 if (fseek(f, 0L, SEEK_SET) == -1)
486 return got_ferror(f, GOT_ERR_IO);
488 for (;;) {
489 flen = fread(fbuf, 1, sizeof(fbuf), f);
490 if (flen == 0) {
491 if (ferror(f))
492 return got_error_from_errno("fread");
493 if (feof(f))
494 break;
496 outlen = write(outfd, fbuf, flen);
497 if (outlen == -1)
498 return got_error_from_errno("write");
499 if (outlen != flen)
500 return got_error(GOT_ERR_IO);
501 *outsize += outlen;
504 return NULL;
507 static const struct got_error *
508 merge_binary_file(int *overlapcnt, int merged_fd,
509 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
510 const char *label_deriv, const char *label_orig, const char *label_deriv2,
511 const char *ondisk_path)
513 const struct got_error *err = NULL;
514 int same_content, changed_deriv, changed_deriv2;
515 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
516 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
517 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
518 char *base_path_orig = NULL, *base_path_deriv = NULL;
519 char *base_path_deriv2 = NULL;
521 *overlapcnt = 0;
523 err = check_files_equal(&same_content, f_deriv, f_deriv2);
524 if (err)
525 return err;
527 if (same_content)
528 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 err = check_files_equal(&same_content, f_deriv, f_orig);
531 if (err)
532 return err;
533 changed_deriv = !same_content;
534 err = check_files_equal(&same_content, f_deriv2, f_orig);
535 if (err)
536 return err;
537 changed_deriv2 = !same_content;
539 if (changed_deriv && changed_deriv2) {
540 *overlapcnt = 1;
541 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
542 err = got_error_from_errno("asprintf");
543 goto done;
545 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
546 err = got_error_from_errno("asprintf");
547 goto done;
549 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
551 goto done;
553 err = got_opentemp_named_fd(&path_orig, &fd_orig,
554 base_path_orig);
555 if (err)
556 goto done;
557 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
558 base_path_deriv);
559 if (err)
560 goto done;
561 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
562 base_path_deriv2);
563 if (err)
564 goto done;
565 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
572 if (err)
573 goto done;
574 if (dprintf(merged_fd, "Binary files differ and cannot be "
575 "merged automatically:\n") < 0) {
576 err = got_error_from_errno("dprintf");
577 goto done;
579 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
580 GOT_DIFF_CONFLICT_MARKER_BEGIN,
581 label_deriv ? " " : "",
582 label_deriv ? label_deriv : "",
583 path_deriv) < 0) {
584 err = got_error_from_errno("dprintf");
585 goto done;
587 if (size_orig > 0) {
588 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
589 GOT_DIFF_CONFLICT_MARKER_ORIG,
590 label_orig ? " " : "",
591 label_orig ? label_orig : "",
592 path_orig) < 0) {
593 err = got_error_from_errno("dprintf");
594 goto done;
597 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
598 GOT_DIFF_CONFLICT_MARKER_SEP,
599 path_deriv2,
600 GOT_DIFF_CONFLICT_MARKER_END,
601 label_deriv2 ? " " : "",
602 label_deriv2 ? label_deriv2 : "") < 0) {
603 err = got_error_from_errno("dprintf");
604 goto done;
606 } else if (changed_deriv)
607 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
608 else if (changed_deriv2)
609 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
610 done:
611 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
612 err == NULL)
613 err = got_error_from_errno2("unlink", path_orig);
614 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
615 err = got_error_from_errno2("close", path_orig);
616 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
617 err = got_error_from_errno2("close", path_deriv);
618 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
619 err = got_error_from_errno2("close", path_deriv2);
620 free(path_orig);
621 free(path_deriv);
622 free(path_deriv2);
623 free(base_path_orig);
624 free(base_path_deriv);
625 free(base_path_deriv2);
626 return err;
629 /*
630 * Perform a 3-way merge where the file f_orig acts as the common
631 * ancestor, the file f_deriv acts as the first derived version,
632 * and the file f_deriv2 acts as the second derived version.
633 * The merge result will be written to a new file at ondisk_path; any
634 * existing file at this path will be replaced.
635 */
636 static const struct got_error *
637 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
638 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
639 const char *path, uint16_t st_mode,
640 const char *label_orig, const char *label_deriv, const char *label_deriv2,
641 enum got_diff_algorithm diff_algo, struct got_repository *repo,
642 got_worktree_checkout_cb progress_cb, void *progress_arg)
644 const struct got_error *err = NULL;
645 int merged_fd = -1;
646 FILE *f_merged = NULL;
647 char *merged_path = NULL, *base_path = NULL;
648 int overlapcnt = 0;
649 char *parent = NULL;
651 *local_changes_subsumed = 0;
653 err = got_path_dirname(&parent, ondisk_path);
654 if (err)
655 return err;
657 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
658 err = got_error_from_errno("asprintf");
659 goto done;
662 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
663 if (err)
664 goto done;
666 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
667 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
668 if (err) {
669 if (err->code != GOT_ERR_FILE_BINARY)
670 goto done;
671 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
672 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
673 ondisk_path);
674 if (err)
675 goto done;
678 err = (*progress_cb)(progress_arg,
679 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
680 if (err)
681 goto done;
683 if (fsync(merged_fd) != 0) {
684 err = got_error_from_errno("fsync");
685 goto done;
688 f_merged = fdopen(merged_fd, "r");
689 if (f_merged == NULL) {
690 err = got_error_from_errno("fdopen");
691 goto done;
693 merged_fd = -1;
695 /* Check if a clean merge has subsumed all local changes. */
696 if (overlapcnt == 0) {
697 err = check_files_equal(local_changes_subsumed, f_deriv,
698 f_merged);
699 if (err)
700 goto done;
703 if (fchmod(fileno(f_merged), st_mode) != 0) {
704 err = got_error_from_errno2("fchmod", merged_path);
705 goto done;
708 if (rename(merged_path, ondisk_path) != 0) {
709 err = got_error_from_errno3("rename", merged_path,
710 ondisk_path);
711 goto done;
713 done:
714 if (err) {
715 if (merged_path)
716 unlink(merged_path);
718 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
720 if (f_merged && fclose(f_merged) == EOF && err == NULL)
721 err = got_error_from_errno("fclose");
722 free(merged_path);
723 free(base_path);
724 free(parent);
725 return err;
728 static const struct got_error *
729 update_symlink(const char *ondisk_path, const char *target_path,
730 size_t target_len)
732 /* This is not atomic but matches what 'ln -sf' does. */
733 if (unlink(ondisk_path) == -1)
734 return got_error_from_errno2("unlink", ondisk_path);
735 if (symlink(target_path, ondisk_path) == -1)
736 return got_error_from_errno3("symlink", target_path,
737 ondisk_path);
738 return NULL;
741 /*
742 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
743 * in the work tree with a file that contains conflict markers and the
744 * conflicting target paths of the original version, a "derived version"
745 * of a symlink from an incoming change, and a local version of the symlink.
747 * The original versions's target path can be NULL if it is not available,
748 * such as if both derived versions added a new symlink at the same path.
750 * The incoming derived symlink target is NULL in case the incoming change
751 * has deleted this symlink.
752 */
753 static const struct got_error *
754 install_symlink_conflict(const char *deriv_target,
755 struct got_object_id *deriv_base_commit_id, const char *orig_target,
756 const char *label_orig, const char *local_target, const char *ondisk_path)
758 const struct got_error *err;
759 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
760 FILE *f = NULL;
762 err = got_object_id_str(&id_str, deriv_base_commit_id);
763 if (err)
764 return got_error_from_errno("asprintf");
766 if (asprintf(&label_deriv, "%s: commit %s",
767 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
768 err = got_error_from_errno("asprintf");
769 goto done;
772 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
773 if (err)
774 goto done;
776 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
777 err = got_error_from_errno2("fchmod", path);
778 goto done;
781 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
782 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
783 deriv_target ? deriv_target : "(symlink was deleted)",
784 orig_target ? label_orig : "",
785 orig_target ? "\n" : "",
786 orig_target ? orig_target : "",
787 orig_target ? "\n" : "",
788 GOT_DIFF_CONFLICT_MARKER_SEP,
789 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
790 err = got_error_from_errno2("fprintf", path);
791 goto done;
794 if (unlink(ondisk_path) == -1) {
795 err = got_error_from_errno2("unlink", ondisk_path);
796 goto done;
798 if (rename(path, ondisk_path) == -1) {
799 err = got_error_from_errno3("rename", path, ondisk_path);
800 goto done;
802 done:
803 if (f != NULL && fclose(f) == EOF && err == NULL)
804 err = got_error_from_errno2("fclose", path);
805 free(path);
806 free(id_str);
807 free(label_deriv);
808 return err;
811 /* forward declaration */
812 static const struct got_error *
813 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
814 const char *, const char *, uint16_t, const char *,
815 struct got_blob_object *, struct got_object_id *,
816 struct got_repository *, got_worktree_checkout_cb, void *);
818 /*
819 * Merge a symlink into the work tree, where blob_orig acts as the common
820 * ancestor, deriv_target is the link target of the first derived version,
821 * and the symlink on disk acts as the second derived version.
822 * Assume that contents of both blobs represent symlinks.
823 */
824 static const struct got_error *
825 merge_symlink(struct got_worktree *worktree,
826 struct got_blob_object *blob_orig, const char *ondisk_path,
827 const char *path, const char *label_orig, const char *deriv_target,
828 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
829 got_worktree_checkout_cb progress_cb, void *progress_arg)
831 const struct got_error *err = NULL;
832 char *ancestor_target = NULL;
833 struct stat sb;
834 ssize_t ondisk_len, deriv_len;
835 char ondisk_target[PATH_MAX];
836 int have_local_change = 0;
837 int have_incoming_change = 0;
839 if (lstat(ondisk_path, &sb) == -1)
840 return got_error_from_errno2("lstat", ondisk_path);
842 ondisk_len = readlink(ondisk_path, ondisk_target,
843 sizeof(ondisk_target));
844 if (ondisk_len == -1) {
845 err = got_error_from_errno2("readlink",
846 ondisk_path);
847 goto done;
849 ondisk_target[ondisk_len] = '\0';
851 if (blob_orig) {
852 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
853 if (err)
854 goto done;
857 if (ancestor_target == NULL ||
858 (ondisk_len != strlen(ancestor_target) ||
859 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
860 have_local_change = 1;
862 deriv_len = strlen(deriv_target);
863 if (ancestor_target == NULL ||
864 (deriv_len != strlen(ancestor_target) ||
865 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
866 have_incoming_change = 1;
868 if (!have_local_change && !have_incoming_change) {
869 if (ancestor_target) {
870 /* Both sides made the same change. */
871 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
872 path);
873 } else if (deriv_len == ondisk_len &&
874 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
875 /* Both sides added the same symlink. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else {
879 /* Both sides added symlinks which don't match. */
880 err = install_symlink_conflict(deriv_target,
881 deriv_base_commit_id, ancestor_target,
882 label_orig, ondisk_target, ondisk_path);
883 if (err)
884 goto done;
885 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
886 path);
888 } else if (!have_local_change && have_incoming_change) {
889 /* Apply the incoming change. */
890 err = update_symlink(ondisk_path, deriv_target,
891 strlen(deriv_target));
892 if (err)
893 goto done;
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
895 } else if (have_local_change && have_incoming_change) {
896 if (deriv_len == ondisk_len &&
897 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
898 /* Both sides made the same change. */
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
900 path);
901 } else {
902 err = install_symlink_conflict(deriv_target,
903 deriv_base_commit_id, ancestor_target, label_orig,
904 ondisk_target, ondisk_path);
905 if (err)
906 goto done;
907 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
908 path);
912 done:
913 free(ancestor_target);
914 return err;
917 static const struct got_error *
918 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 const struct got_error *err = NULL;
921 char target_path[PATH_MAX];
922 ssize_t target_len;
923 size_t n;
924 FILE *f;
926 *outfile = NULL;
928 f = got_opentemp();
929 if (f == NULL)
930 return got_error_from_errno("got_opentemp");
931 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
932 if (target_len == -1) {
933 err = got_error_from_errno2("readlink", ondisk_path);
934 goto done;
936 n = fwrite(target_path, 1, target_len, f);
937 if (n != target_len) {
938 err = got_ferror(f, GOT_ERR_IO);
939 goto done;
941 if (fflush(f) == EOF) {
942 err = got_error_from_errno("fflush");
943 goto done;
945 if (fseek(f, 0L, SEEK_SET) == -1) {
946 err = got_ferror(f, GOT_ERR_IO);
947 goto done;
949 done:
950 if (err)
951 fclose(f);
952 else
953 *outfile = f;
954 return err;
957 /*
958 * Perform a 3-way merge where blob_orig acts as the common ancestor,
959 * blob_deriv acts as the first derived version, and the file on disk
960 * acts as the second derived version.
961 */
962 static const struct got_error *
963 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
964 struct got_blob_object *blob_orig, const char *ondisk_path,
965 const char *path, uint16_t st_mode, const char *label_orig,
966 struct got_blob_object *blob_deriv,
967 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
968 got_worktree_checkout_cb progress_cb, void *progress_arg)
970 const struct got_error *err = NULL;
971 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
972 char *blob_orig_path = NULL;
973 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
974 char *label_deriv = NULL, *parent = NULL;
976 *local_changes_subsumed = 0;
978 err = got_path_dirname(&parent, ondisk_path);
979 if (err)
980 return err;
982 if (blob_orig) {
983 if (asprintf(&base_path, "%s/got-merge-blob-orig",
984 parent) == -1) {
985 err = got_error_from_errno("asprintf");
986 base_path = NULL;
987 goto done;
990 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
991 if (err)
992 goto done;
993 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
994 blob_orig);
995 if (err)
996 goto done;
997 free(base_path);
998 } else {
999 /*
1000 * No common ancestor exists. This is an "add vs add" conflict
1001 * and we simply use an empty ancestor file to make both files
1002 * appear in the merged result in their entirety.
1004 f_orig = got_opentemp();
1005 if (f_orig == NULL) {
1006 err = got_error_from_errno("got_opentemp");
1007 goto done;
1011 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1012 err = got_error_from_errno("asprintf");
1013 base_path = NULL;
1014 goto done;
1017 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1018 if (err)
1019 goto done;
1020 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1021 blob_deriv);
1022 if (err)
1023 goto done;
1025 err = got_object_id_str(&id_str, deriv_base_commit_id);
1026 if (err)
1027 goto done;
1028 if (asprintf(&label_deriv, "%s: commit %s",
1029 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1030 err = got_error_from_errno("asprintf");
1031 goto done;
1035 * In order the run a 3-way merge with a symlink we copy the symlink's
1036 * target path into a temporary file and use that file with diff3.
1038 if (S_ISLNK(st_mode)) {
1039 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1040 if (err)
1041 goto done;
1042 } else {
1043 int fd;
1044 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1045 if (fd == -1) {
1046 err = got_error_from_errno2("open", ondisk_path);
1047 goto done;
1049 f_deriv2 = fdopen(fd, "r");
1050 if (f_deriv2 == NULL) {
1051 err = got_error_from_errno2("fdopen", ondisk_path);
1052 close(fd);
1053 goto done;
1057 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1058 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1059 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1060 done:
1061 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1062 err = got_error_from_errno("fclose");
1063 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1064 err = got_error_from_errno("fclose");
1065 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 free(base_path);
1068 if (blob_orig_path) {
1069 unlink(blob_orig_path);
1070 free(blob_orig_path);
1072 if (blob_deriv_path) {
1073 unlink(blob_deriv_path);
1074 free(blob_deriv_path);
1076 free(id_str);
1077 free(label_deriv);
1078 free(parent);
1079 return err;
1082 static const struct got_error *
1083 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1084 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1085 int wt_fd, const char *path, struct got_object_id *blob_id)
1087 const struct got_error *err = NULL;
1088 struct got_fileindex_entry *new_ie;
1090 *new_iep = NULL;
1092 err = got_fileindex_entry_alloc(&new_ie, path);
1093 if (err)
1094 return err;
1096 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1097 blob_id->sha1, base_commit_id->sha1, 1);
1098 if (err)
1099 goto done;
1101 err = got_fileindex_entry_add(fileindex, new_ie);
1102 done:
1103 if (err)
1104 got_fileindex_entry_free(new_ie);
1105 else
1106 *new_iep = new_ie;
1107 return err;
1110 static mode_t
1111 get_ondisk_perms(int executable, mode_t st_mode)
1113 mode_t xbits = S_IXUSR;
1115 if (executable) {
1116 /* Map read bits to execute bits. */
1117 if (st_mode & S_IRGRP)
1118 xbits |= S_IXGRP;
1119 if (st_mode & S_IROTH)
1120 xbits |= S_IXOTH;
1121 return st_mode | xbits;
1124 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1127 /* forward declaration */
1128 static const struct got_error *
1129 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1130 const char *path, mode_t te_mode, mode_t st_mode,
1131 struct got_blob_object *blob, int restoring_missing_file,
1132 int reverting_versioned_file, int installing_bad_symlink,
1133 int path_is_unversioned, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg);
1137 * This function assumes that the provided symlink target points at a
1138 * safe location in the work tree!
1140 static const struct got_error *
1141 replace_existing_symlink(int *did_something, const char *ondisk_path,
1142 const char *target_path, size_t target_len)
1144 const struct got_error *err = NULL;
1145 ssize_t elen;
1146 char etarget[PATH_MAX];
1147 int fd;
1149 *did_something = 0;
1152 * "Bad" symlinks (those pointing outside the work tree or into the
1153 * .got directory) are installed in the work tree as a regular file
1154 * which contains the bad symlink target path.
1155 * The new symlink target has already been checked for safety by our
1156 * caller. If we can successfully open a regular file then we simply
1157 * replace this file with a symlink below.
1159 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1160 if (fd == -1) {
1161 if (!got_err_open_nofollow_on_symlink())
1162 return got_error_from_errno2("open", ondisk_path);
1164 /* We are updating an existing on-disk symlink. */
1165 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1166 if (elen == -1)
1167 return got_error_from_errno2("readlink", ondisk_path);
1169 if (elen == target_len &&
1170 memcmp(etarget, target_path, target_len) == 0)
1171 return NULL; /* nothing to do */
1174 *did_something = 1;
1175 err = update_symlink(ondisk_path, target_path, target_len);
1176 if (fd != -1 && close(fd) == -1 && err == NULL)
1177 err = got_error_from_errno2("close", ondisk_path);
1178 return err;
1181 static const struct got_error *
1182 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1183 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 const struct got_error *err = NULL;
1186 char canonpath[PATH_MAX];
1187 char *path_got = NULL;
1189 *is_bad_symlink = 0;
1191 if (target_len >= sizeof(canonpath)) {
1192 *is_bad_symlink = 1;
1193 return NULL;
1197 * We do not use realpath(3) to resolve the symlink's target
1198 * path because we don't want to resolve symlinks recursively.
1199 * Instead we make the path absolute and then canonicalize it.
1200 * Relative symlink target lookup should begin at the directory
1201 * in which the blob object is being installed.
1203 if (!got_path_is_absolute(target_path)) {
1204 char *abspath, *parent;
1205 err = got_path_dirname(&parent, ondisk_path);
1206 if (err)
1207 return err;
1208 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1209 free(parent);
1210 return got_error_from_errno("asprintf");
1212 free(parent);
1213 if (strlen(abspath) >= sizeof(canonpath)) {
1214 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1215 free(abspath);
1216 return err;
1218 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1219 free(abspath);
1220 if (err)
1221 return err;
1222 } else {
1223 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1224 if (err)
1225 return err;
1228 /* Only allow symlinks pointing at paths within the work tree. */
1229 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1230 *is_bad_symlink = 1;
1231 return NULL;
1234 /* Do not allow symlinks pointing into the .got directory. */
1235 if (asprintf(&path_got, "%s/%s", wtroot_path,
1236 GOT_WORKTREE_GOT_DIR) == -1)
1237 return got_error_from_errno("asprintf");
1238 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1239 *is_bad_symlink = 1;
1241 free(path_got);
1242 return NULL;
1245 static const struct got_error *
1246 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1247 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1248 int restoring_missing_file, int reverting_versioned_file,
1249 int path_is_unversioned, int allow_bad_symlinks,
1250 struct got_repository *repo,
1251 got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 const struct got_error *err = NULL;
1254 char target_path[PATH_MAX];
1255 size_t len, target_len = 0;
1256 char *path_got = NULL;
1257 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 *is_bad_symlink = 0;
1263 * Blob object content specifies the target path of the link.
1264 * If a symbolic link cannot be installed we instead create
1265 * a regular file which contains the link target path stored
1266 * in the blob object.
1268 do {
1269 err = got_object_blob_read_block(&len, blob);
1270 if (len + target_len >= sizeof(target_path)) {
1271 /* Path too long; install as a regular file. */
1272 *is_bad_symlink = 1;
1273 got_object_blob_rewind(blob);
1274 return install_blob(worktree, ondisk_path, path,
1275 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1276 restoring_missing_file, reverting_versioned_file,
1277 1, path_is_unversioned, repo, progress_cb,
1278 progress_arg);
1280 if (len > 0) {
1281 /* Skip blob object header first time around. */
1282 memcpy(target_path + target_len, buf + hdrlen,
1283 len - hdrlen);
1284 target_len += len - hdrlen;
1285 hdrlen = 0;
1287 } while (len != 0);
1288 target_path[target_len] = '\0';
1290 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1291 ondisk_path, worktree->root_path);
1292 if (err)
1293 return err;
1295 if (*is_bad_symlink && !allow_bad_symlinks) {
1296 /* install as a regular file */
1297 got_object_blob_rewind(blob);
1298 err = install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file, 1,
1301 path_is_unversioned, repo, progress_cb, progress_arg);
1302 goto done;
1305 if (symlink(target_path, ondisk_path) == -1) {
1306 if (errno == EEXIST) {
1307 int symlink_replaced;
1308 if (path_is_unversioned) {
1309 err = (*progress_cb)(progress_arg,
1310 GOT_STATUS_UNVERSIONED, path);
1311 goto done;
1313 err = replace_existing_symlink(&symlink_replaced,
1314 ondisk_path, target_path, target_len);
1315 if (err)
1316 goto done;
1317 if (progress_cb) {
1318 if (symlink_replaced) {
1319 err = (*progress_cb)(progress_arg,
1320 reverting_versioned_file ?
1321 GOT_STATUS_REVERT :
1322 GOT_STATUS_UPDATE, path);
1323 } else {
1324 err = (*progress_cb)(progress_arg,
1325 GOT_STATUS_EXISTS, path);
1328 goto done; /* Nothing else to do. */
1331 if (errno == ENOENT) {
1332 char *parent;
1333 err = got_path_dirname(&parent, ondisk_path);
1334 if (err)
1335 goto done;
1336 err = add_dir_on_disk(worktree, parent);
1337 free(parent);
1338 if (err)
1339 goto done;
1341 * Retry, and fall through to error handling
1342 * below if this second attempt fails.
1344 if (symlink(target_path, ondisk_path) != -1) {
1345 err = NULL; /* success */
1346 goto done;
1350 /* Handle errors from first or second creation attempt. */
1351 if (errno == ENAMETOOLONG) {
1352 /* bad target path; install as a regular file */
1353 *is_bad_symlink = 1;
1354 got_object_blob_rewind(blob);
1355 err = install_blob(worktree, ondisk_path, path,
1356 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1357 restoring_missing_file, reverting_versioned_file, 1,
1358 path_is_unversioned, repo,
1359 progress_cb, progress_arg);
1360 } else if (errno == ENOTDIR) {
1361 err = got_error_path(ondisk_path,
1362 GOT_ERR_FILE_OBSTRUCTED);
1363 } else {
1364 err = got_error_from_errno3("symlink",
1365 target_path, ondisk_path);
1367 } else if (progress_cb)
1368 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1369 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1370 done:
1371 free(path_got);
1372 return err;
1375 static const struct got_error *
1376 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1377 const char *path, mode_t te_mode, mode_t st_mode,
1378 struct got_blob_object *blob, int restoring_missing_file,
1379 int reverting_versioned_file, int installing_bad_symlink,
1380 int path_is_unversioned, struct got_repository *repo,
1381 got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 const struct got_error *err = NULL;
1384 int fd = -1;
1385 size_t len, hdrlen;
1386 int update = 0;
1387 char *tmppath = NULL;
1389 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1390 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1391 if (fd == -1) {
1392 if (errno == ENOENT) {
1393 char *parent;
1394 err = got_path_dirname(&parent, path);
1395 if (err)
1396 return err;
1397 err = add_dir_on_disk(worktree, parent);
1398 free(parent);
1399 if (err)
1400 return err;
1401 fd = open(ondisk_path,
1402 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1403 GOT_DEFAULT_FILE_MODE);
1404 if (fd == -1)
1405 return got_error_from_errno2("open",
1406 ondisk_path);
1407 } else if (errno == EEXIST) {
1408 if (path_is_unversioned) {
1409 err = (*progress_cb)(progress_arg,
1410 GOT_STATUS_UNVERSIONED, path);
1411 goto done;
1413 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1414 !S_ISREG(st_mode) && !installing_bad_symlink) {
1415 /* TODO file is obstructed; do something */
1416 err = got_error_path(ondisk_path,
1417 GOT_ERR_FILE_OBSTRUCTED);
1418 goto done;
1419 } else {
1420 err = got_opentemp_named_fd(&tmppath, &fd,
1421 ondisk_path);
1422 if (err)
1423 goto done;
1424 update = 1;
1426 } else
1427 return got_error_from_errno2("open", ondisk_path);
1430 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 err = got_error_from_errno2("fchmod",
1432 update ? tmppath : ondisk_path);
1433 goto done;
1436 if (progress_cb) {
1437 if (restoring_missing_file)
1438 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1439 path);
1440 else if (reverting_versioned_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1442 path);
1443 else
1444 err = (*progress_cb)(progress_arg,
1445 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1446 if (err)
1447 goto done;
1450 hdrlen = got_object_blob_get_hdrlen(blob);
1451 do {
1452 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1453 err = got_object_blob_read_block(&len, blob);
1454 if (err)
1455 break;
1456 if (len > 0) {
1457 /* Skip blob object header first time around. */
1458 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1459 if (outlen == -1) {
1460 err = got_error_from_errno("write");
1461 goto done;
1462 } else if (outlen != len - hdrlen) {
1463 err = got_error(GOT_ERR_IO);
1464 goto done;
1466 hdrlen = 0;
1468 } while (len != 0);
1470 if (fsync(fd) != 0) {
1471 err = got_error_from_errno("fsync");
1472 goto done;
1475 if (update) {
1476 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1477 err = got_error_from_errno2("unlink", ondisk_path);
1478 goto done;
1480 if (rename(tmppath, ondisk_path) != 0) {
1481 err = got_error_from_errno3("rename", tmppath,
1482 ondisk_path);
1483 goto done;
1485 free(tmppath);
1486 tmppath = NULL;
1489 done:
1490 if (fd != -1 && close(fd) == -1 && err == NULL)
1491 err = got_error_from_errno("close");
1492 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1493 err = got_error_from_errno2("unlink", tmppath);
1494 free(tmppath);
1495 return err;
1498 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1499 static const struct got_error *
1500 get_modified_file_content_status(unsigned char *status, FILE *f)
1502 const struct got_error *err = NULL;
1503 const char *markers[3] = {
1504 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1505 GOT_DIFF_CONFLICT_MARKER_SEP,
1506 GOT_DIFF_CONFLICT_MARKER_END
1508 int i = 0;
1509 char *line = NULL;
1510 size_t linesize = 0;
1511 ssize_t linelen;
1513 while (*status == GOT_STATUS_MODIFY) {
1514 linelen = getline(&line, &linesize, f);
1515 if (linelen == -1) {
1516 if (feof(f))
1517 break;
1518 err = got_ferror(f, GOT_ERR_IO);
1519 break;
1522 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1523 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1524 == 0)
1525 *status = GOT_STATUS_CONFLICT;
1526 else
1527 i++;
1530 free(line);
1532 return err;
1535 static int
1536 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1539 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1542 static int
1543 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1546 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1547 ie->mtime_sec == sb->st_mtim.tv_sec &&
1548 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1549 ie->size == (sb->st_size & 0xffffffff) &&
1550 !xbit_differs(ie, sb->st_mode));
1553 static unsigned char
1554 get_staged_status(struct got_fileindex_entry *ie)
1556 switch (got_fileindex_entry_stage_get(ie)) {
1557 case GOT_FILEIDX_STAGE_ADD:
1558 return GOT_STATUS_ADD;
1559 case GOT_FILEIDX_STAGE_DELETE:
1560 return GOT_STATUS_DELETE;
1561 case GOT_FILEIDX_STAGE_MODIFY:
1562 return GOT_STATUS_MODIFY;
1563 default:
1564 return GOT_STATUS_NO_CHANGE;
1568 static const struct got_error *
1569 get_symlink_modification_status(unsigned char *status,
1570 struct got_fileindex_entry *ie, const char *abspath,
1571 int dirfd, const char *de_name, struct got_blob_object *blob)
1573 const struct got_error *err = NULL;
1574 char target_path[PATH_MAX];
1575 char etarget[PATH_MAX];
1576 ssize_t elen;
1577 size_t len, target_len = 0;
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 *status = GOT_STATUS_NO_CHANGE;
1583 /* Blob object content specifies the target path of the link. */
1584 do {
1585 err = got_object_blob_read_block(&len, blob);
1586 if (err)
1587 return err;
1588 if (len + target_len >= sizeof(target_path)) {
1590 * Should not happen. The blob contents were OK
1591 * when this symlink was installed.
1593 return got_error(GOT_ERR_NO_SPACE);
1595 if (len > 0) {
1596 /* Skip blob object header first time around. */
1597 memcpy(target_path + target_len, buf + hdrlen,
1598 len - hdrlen);
1599 target_len += len - hdrlen;
1600 hdrlen = 0;
1602 } while (len != 0);
1603 target_path[target_len] = '\0';
1605 if (dirfd != -1) {
1606 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1607 if (elen == -1)
1608 return got_error_from_errno2("readlinkat", abspath);
1609 } else {
1610 elen = readlink(abspath, etarget, sizeof(etarget));
1611 if (elen == -1)
1612 return got_error_from_errno2("readlink", abspath);
1615 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1616 *status = GOT_STATUS_MODIFY;
1618 return NULL;
1621 static const struct got_error *
1622 get_file_status(unsigned char *status, struct stat *sb,
1623 struct got_fileindex_entry *ie, const char *abspath,
1624 int dirfd, const char *de_name, struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 struct got_object_id id;
1628 size_t hdrlen;
1629 int fd = -1;
1630 FILE *f = NULL;
1631 uint8_t fbuf[8192];
1632 struct got_blob_object *blob = NULL;
1633 size_t flen, blen;
1634 unsigned char staged_status = get_staged_status(ie);
1636 *status = GOT_STATUS_NO_CHANGE;
1637 memset(sb, 0, sizeof(*sb));
1640 * Whenever the caller provides a directory descriptor and a
1641 * directory entry name for the file, use them! This prevents
1642 * race conditions if filesystem paths change beneath our feet.
1644 if (dirfd != -1) {
1645 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1646 if (errno == ENOENT) {
1647 if (got_fileindex_entry_has_file_on_disk(ie))
1648 *status = GOT_STATUS_MISSING;
1649 else
1650 *status = GOT_STATUS_DELETE;
1651 goto done;
1653 err = got_error_from_errno2("fstatat", abspath);
1654 goto done;
1656 } else {
1657 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1658 if (fd == -1 && errno != ENOENT &&
1659 !got_err_open_nofollow_on_symlink())
1660 return got_error_from_errno2("open", abspath);
1661 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1662 if (lstat(abspath, sb) == -1)
1663 return got_error_from_errno2("lstat", abspath);
1664 } else if (fd == -1 || fstat(fd, sb) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1668 else
1669 *status = GOT_STATUS_DELETE;
1670 goto done;
1672 err = got_error_from_errno2("fstat", abspath);
1673 goto done;
1677 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1678 *status = GOT_STATUS_OBSTRUCTED;
1679 goto done;
1682 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1683 *status = GOT_STATUS_DELETE;
1684 goto done;
1685 } else if (!got_fileindex_entry_has_blob(ie) &&
1686 staged_status != GOT_STATUS_ADD) {
1687 *status = GOT_STATUS_ADD;
1688 goto done;
1691 if (!stat_info_differs(ie, sb))
1692 goto done;
1694 if (S_ISLNK(sb->st_mode) &&
1695 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1696 *status = GOT_STATUS_MODIFY;
1697 goto done;
1700 if (staged_status == GOT_STATUS_MODIFY ||
1701 staged_status == GOT_STATUS_ADD)
1702 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1703 else
1704 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1707 if (err)
1708 goto done;
1710 if (S_ISLNK(sb->st_mode)) {
1711 err = get_symlink_modification_status(status, ie,
1712 abspath, dirfd, de_name, blob);
1713 goto done;
1716 if (dirfd != -1) {
1717 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1718 if (fd == -1) {
1719 err = got_error_from_errno2("openat", abspath);
1720 goto done;
1724 f = fdopen(fd, "r");
1725 if (f == NULL) {
1726 err = got_error_from_errno2("fdopen", abspath);
1727 goto done;
1729 fd = -1;
1730 hdrlen = got_object_blob_get_hdrlen(blob);
1731 for (;;) {
1732 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1733 err = got_object_blob_read_block(&blen, blob);
1734 if (err)
1735 goto done;
1736 /* Skip length of blob object header first time around. */
1737 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1738 if (flen == 0 && ferror(f)) {
1739 err = got_error_from_errno("fread");
1740 goto done;
1742 if (blen - hdrlen == 0) {
1743 if (flen != 0)
1744 *status = GOT_STATUS_MODIFY;
1745 break;
1746 } else if (flen == 0) {
1747 if (blen - hdrlen != 0)
1748 *status = GOT_STATUS_MODIFY;
1749 break;
1750 } else if (blen - hdrlen == flen) {
1751 /* Skip blob object header first time around. */
1752 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1753 *status = GOT_STATUS_MODIFY;
1754 break;
1756 } else {
1757 *status = GOT_STATUS_MODIFY;
1758 break;
1760 hdrlen = 0;
1763 if (*status == GOT_STATUS_MODIFY) {
1764 rewind(f);
1765 err = get_modified_file_content_status(status, f);
1766 } else if (xbit_differs(ie, sb->st_mode))
1767 *status = GOT_STATUS_MODE_CHANGE;
1768 done:
1769 if (blob)
1770 got_object_blob_close(blob);
1771 if (f != NULL && fclose(f) == EOF && err == NULL)
1772 err = got_error_from_errno2("fclose", abspath);
1773 if (fd != -1 && close(fd) == -1 && err == NULL)
1774 err = got_error_from_errno2("close", abspath);
1775 return err;
1779 * Update timestamps in the file index if a file is unmodified and
1780 * we had to run a full content comparison to find out.
1782 static const struct got_error *
1783 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1784 struct got_fileindex_entry *ie, struct stat *sb)
1786 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1787 return got_fileindex_entry_update(ie, wt_fd, path,
1788 ie->blob_sha1, ie->commit_sha1, 1);
1790 return NULL;
1793 static const struct got_error *
1794 update_blob(struct got_worktree *worktree,
1795 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1796 struct got_tree_entry *te, const char *path,
1797 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1798 void *progress_arg)
1800 const struct got_error *err = NULL;
1801 struct got_blob_object *blob = NULL;
1802 char *ondisk_path;
1803 unsigned char status = GOT_STATUS_NO_CHANGE;
1804 struct stat sb;
1806 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1807 return got_error_from_errno("asprintf");
1809 if (ie) {
1810 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1811 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1812 goto done;
1814 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1815 repo);
1816 if (err)
1817 goto done;
1818 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1819 sb.st_mode = got_fileindex_perms_to_st(ie);
1820 } else {
1821 if (stat(ondisk_path, &sb) == -1) {
1822 if (errno != ENOENT) {
1823 err = got_error_from_errno2("stat",
1824 ondisk_path);
1825 goto done;
1827 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1828 status = GOT_STATUS_UNVERSIONED;
1829 } else {
1830 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1831 status = GOT_STATUS_UNVERSIONED;
1832 else
1833 status = GOT_STATUS_OBSTRUCTED;
1837 if (status == GOT_STATUS_OBSTRUCTED) {
1838 if (ie)
1839 got_fileindex_entry_mark_skipped(ie);
1840 err = (*progress_cb)(progress_arg, status, path);
1841 goto done;
1843 if (status == GOT_STATUS_CONFLICT) {
1844 if (ie)
1845 got_fileindex_entry_mark_skipped(ie);
1846 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1847 path);
1848 goto done;
1851 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1852 (S_ISLNK(te->mode) ||
1853 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1855 * This is a regular file or an installed bad symlink.
1856 * If the file index indicates that this file is already
1857 * up-to-date with respect to the repository we can skip
1858 * updating contents of this file.
1860 if (got_fileindex_entry_has_commit(ie) &&
1861 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1862 SHA1_DIGEST_LENGTH) == 0) {
1863 /* Same commit. */
1864 err = sync_timestamps(worktree->root_fd,
1865 path, status, ie, &sb);
1866 if (err)
1867 goto done;
1868 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1869 path);
1870 goto done;
1872 if (got_fileindex_entry_has_blob(ie) &&
1873 memcmp(ie->blob_sha1, te->id.sha1,
1874 SHA1_DIGEST_LENGTH) == 0) {
1875 /* Different commit but the same blob. */
1876 err = sync_timestamps(worktree->root_fd,
1877 path, status, ie, &sb);
1878 if (err)
1879 goto done;
1880 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1881 path);
1882 goto done;
1886 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1887 if (err)
1888 goto done;
1890 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 int update_timestamps;
1892 struct got_blob_object *blob2 = NULL;
1893 char *label_orig = NULL;
1894 if (got_fileindex_entry_has_blob(ie)) {
1895 struct got_object_id id2;
1896 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1898 if (err)
1899 goto done;
1901 if (got_fileindex_entry_has_commit(ie)) {
1902 char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 sizeof(id_str)) == NULL) {
1905 err = got_error_path(id_str,
1906 GOT_ERR_BAD_OBJ_ID_STR);
1907 goto done;
1909 if (asprintf(&label_orig, "%s: commit %s",
1910 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 err = got_error_from_errno("asprintf");
1912 goto done;
1915 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1916 char *link_target;
1917 err = got_object_blob_read_to_str(&link_target, blob);
1918 if (err)
1919 goto done;
1920 err = merge_symlink(worktree, blob2, ondisk_path, path,
1921 label_orig, link_target, worktree->base_commit_id,
1922 repo, progress_cb, progress_arg);
1923 free(link_target);
1924 } else {
1925 err = merge_blob(&update_timestamps, worktree, blob2,
1926 ondisk_path, path, sb.st_mode, label_orig, blob,
1927 worktree->base_commit_id, repo,
1928 progress_cb, progress_arg);
1930 free(label_orig);
1931 if (blob2)
1932 got_object_blob_close(blob2);
1933 if (err)
1934 goto done;
1936 * Do not update timestamps of files with local changes.
1937 * Otherwise, a future status walk would treat them as
1938 * unmodified files again.
1940 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1941 blob->id.sha1, worktree->base_commit_id->sha1,
1942 update_timestamps);
1943 } else if (status == GOT_STATUS_MODE_CHANGE) {
1944 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1945 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 } else if (status == GOT_STATUS_DELETE) {
1947 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1948 if (err)
1949 goto done;
1950 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1951 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1952 if (err)
1953 goto done;
1954 } else {
1955 int is_bad_symlink = 0;
1956 if (S_ISLNK(te->mode)) {
1957 err = install_symlink(&is_bad_symlink, worktree,
1958 ondisk_path, path, blob,
1959 status == GOT_STATUS_MISSING, 0,
1960 status == GOT_STATUS_UNVERSIONED, 0,
1961 repo, progress_cb, progress_arg);
1962 } else {
1963 err = install_blob(worktree, ondisk_path, path,
1964 te->mode, sb.st_mode, blob,
1965 status == GOT_STATUS_MISSING, 0, 0,
1966 status == GOT_STATUS_UNVERSIONED, repo,
1967 progress_cb, progress_arg);
1969 if (err)
1970 goto done;
1972 if (ie) {
1973 err = got_fileindex_entry_update(ie,
1974 worktree->root_fd, path, blob->id.sha1,
1975 worktree->base_commit_id->sha1, 1);
1976 } else {
1977 err = create_fileindex_entry(&ie, fileindex,
1978 worktree->base_commit_id, worktree->root_fd, path,
1979 &blob->id);
1981 if (err)
1982 goto done;
1984 if (is_bad_symlink) {
1985 got_fileindex_entry_filetype_set(ie,
1986 GOT_FILEIDX_MODE_BAD_SYMLINK);
1989 got_object_blob_close(blob);
1990 done:
1991 free(ondisk_path);
1992 return err;
1995 static const struct got_error *
1996 remove_ondisk_file(const char *root_path, const char *path)
1998 const struct got_error *err = NULL;
1999 char *ondisk_path = NULL, *parent = NULL;
2001 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2002 return got_error_from_errno("asprintf");
2004 if (unlink(ondisk_path) == -1) {
2005 if (errno != ENOENT)
2006 err = got_error_from_errno2("unlink", ondisk_path);
2007 } else {
2008 size_t root_len = strlen(root_path);
2009 err = got_path_dirname(&parent, ondisk_path);
2010 if (err)
2011 goto done;
2012 while (got_path_cmp(parent, root_path,
2013 strlen(parent), root_len) != 0) {
2014 free(ondisk_path);
2015 ondisk_path = parent;
2016 parent = NULL;
2017 if (rmdir(ondisk_path) == -1) {
2018 if (errno != ENOTEMPTY)
2019 err = got_error_from_errno2("rmdir",
2020 ondisk_path);
2021 break;
2023 err = got_path_dirname(&parent, ondisk_path);
2024 if (err)
2025 break;
2028 done:
2029 free(ondisk_path);
2030 free(parent);
2031 return err;
2034 static const struct got_error *
2035 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2036 struct got_fileindex_entry *ie, struct got_repository *repo,
2037 got_worktree_checkout_cb progress_cb, void *progress_arg)
2039 const struct got_error *err = NULL;
2040 unsigned char status;
2041 struct stat sb;
2042 char *ondisk_path;
2044 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2045 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2047 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2048 == -1)
2049 return got_error_from_errno("asprintf");
2051 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2052 if (err)
2053 goto done;
2055 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2056 char ondisk_target[PATH_MAX];
2057 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2058 sizeof(ondisk_target));
2059 if (ondisk_len == -1) {
2060 err = got_error_from_errno2("readlink", ondisk_path);
2061 goto done;
2063 ondisk_target[ondisk_len] = '\0';
2064 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2065 NULL, NULL, /* XXX pass common ancestor info? */
2066 ondisk_target, ondisk_path);
2067 if (err)
2068 goto done;
2069 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2070 ie->path);
2071 goto done;
2074 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2075 status == GOT_STATUS_ADD) {
2076 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2077 if (err)
2078 goto done;
2080 * Preserve the working file and change the deleted blob's
2081 * entry into a schedule-add entry.
2083 err = got_fileindex_entry_update(ie, worktree->root_fd,
2084 ie->path, NULL, NULL, 0);
2085 } else {
2086 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2087 if (err)
2088 goto done;
2089 if (status == GOT_STATUS_NO_CHANGE) {
2090 err = remove_ondisk_file(worktree->root_path, ie->path);
2091 if (err)
2092 goto done;
2094 got_fileindex_entry_remove(fileindex, ie);
2096 done:
2097 free(ondisk_path);
2098 return err;
2101 struct diff_cb_arg {
2102 struct got_fileindex *fileindex;
2103 struct got_worktree *worktree;
2104 struct got_repository *repo;
2105 got_worktree_checkout_cb progress_cb;
2106 void *progress_arg;
2107 got_cancel_cb cancel_cb;
2108 void *cancel_arg;
2111 static const struct got_error *
2112 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2113 struct got_tree_entry *te, const char *parent_path)
2115 struct diff_cb_arg *a = arg;
2117 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2118 return got_error(GOT_ERR_CANCELLED);
2120 return update_blob(a->worktree, a->fileindex, ie, te,
2121 ie->path, a->repo, a->progress_cb, a->progress_arg);
2124 static const struct got_error *
2125 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2127 struct diff_cb_arg *a = arg;
2129 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2130 return got_error(GOT_ERR_CANCELLED);
2132 return delete_blob(a->worktree, a->fileindex, ie,
2133 a->repo, a->progress_cb, a->progress_arg);
2136 static const struct got_error *
2137 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2139 struct diff_cb_arg *a = arg;
2140 const struct got_error *err;
2141 char *path;
2143 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2144 return got_error(GOT_ERR_CANCELLED);
2146 if (got_object_tree_entry_is_submodule(te))
2147 return NULL;
2149 if (asprintf(&path, "%s%s%s", parent_path,
2150 parent_path[0] ? "/" : "", te->name)
2151 == -1)
2152 return got_error_from_errno("asprintf");
2154 if (S_ISDIR(te->mode))
2155 err = add_dir_on_disk(a->worktree, path);
2156 else
2157 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2158 a->repo, a->progress_cb, a->progress_arg);
2160 free(path);
2161 return err;
2164 const struct got_error *
2165 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2167 uint32_t uuid_status;
2169 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2170 if (uuid_status != uuid_s_ok) {
2171 *uuidstr = NULL;
2172 return got_error_uuid(uuid_status, "uuid_to_string");
2175 return NULL;
2178 static const struct got_error *
2179 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2181 const struct got_error *err = NULL;
2182 char *uuidstr = NULL;
2184 *refname = NULL;
2186 err = got_worktree_get_uuid(&uuidstr, worktree);
2187 if (err)
2188 return err;
2190 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2191 err = got_error_from_errno("asprintf");
2192 *refname = NULL;
2194 free(uuidstr);
2195 return err;
2198 const struct got_error *
2199 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2201 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2204 static const struct got_error *
2205 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2207 return get_ref_name(refname, worktree,
2208 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2211 static const struct got_error *
2212 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2214 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2217 static const struct got_error *
2218 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2220 return get_ref_name(refname, worktree,
2221 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2224 static const struct got_error *
2225 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 return get_ref_name(refname, worktree,
2228 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2231 static const struct got_error *
2232 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 return get_ref_name(refname, worktree,
2235 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2238 static const struct got_error *
2239 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2241 return get_ref_name(refname, worktree,
2242 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2245 static const struct got_error *
2246 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2248 return get_ref_name(refname, worktree,
2249 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2252 static const struct got_error *
2253 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree,
2256 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2259 const struct got_error *
2260 got_worktree_get_histedit_script_path(char **path,
2261 struct got_worktree *worktree)
2263 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2264 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2265 *path = NULL;
2266 return got_error_from_errno("asprintf");
2268 return NULL;
2271 static const struct got_error *
2272 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2286 * Prevent Git's garbage collector from deleting our base commit by
2287 * setting a reference to our base commit's ID.
2289 static const struct got_error *
2290 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2292 const struct got_error *err = NULL;
2293 struct got_reference *ref = NULL;
2294 char *refname;
2296 err = got_worktree_get_base_ref_name(&refname, worktree);
2297 if (err)
2298 return err;
2300 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2301 if (err)
2302 goto done;
2304 err = got_ref_write(ref, repo);
2305 done:
2306 free(refname);
2307 if (ref)
2308 got_ref_close(ref);
2309 return err;
2312 static const struct got_error *
2313 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2315 const struct got_error *err = NULL;
2317 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 err = got_error_from_errno("asprintf");
2320 *fileindex_path = NULL;
2322 return err;
2326 static const struct got_error *
2327 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2328 struct got_worktree *worktree)
2330 const struct got_error *err = NULL;
2331 FILE *index = NULL;
2333 *fileindex_path = NULL;
2334 *fileindex = got_fileindex_alloc();
2335 if (*fileindex == NULL)
2336 return got_error_from_errno("got_fileindex_alloc");
2338 err = get_fileindex_path(fileindex_path, worktree);
2339 if (err)
2340 goto done;
2342 index = fopen(*fileindex_path, "rbe");
2343 if (index == NULL) {
2344 if (errno != ENOENT)
2345 err = got_error_from_errno2("fopen", *fileindex_path);
2346 } else {
2347 err = got_fileindex_read(*fileindex, index);
2348 if (fclose(index) == EOF && err == NULL)
2349 err = got_error_from_errno("fclose");
2351 done:
2352 if (err) {
2353 free(*fileindex_path);
2354 *fileindex_path = NULL;
2355 got_fileindex_free(*fileindex);
2356 *fileindex = NULL;
2358 return err;
2361 struct bump_base_commit_id_arg {
2362 struct got_object_id *base_commit_id;
2363 const char *path;
2364 size_t path_len;
2365 const char *entry_name;
2366 got_worktree_checkout_cb progress_cb;
2367 void *progress_arg;
2370 /* Bump base commit ID of all files within an updated part of the work tree. */
2371 static const struct got_error *
2372 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2374 const struct got_error *err;
2375 struct bump_base_commit_id_arg *a = arg;
2377 if (a->entry_name) {
2378 if (strcmp(ie->path, a->path) != 0)
2379 return NULL;
2380 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2381 return NULL;
2383 if (got_fileindex_entry_was_skipped(ie))
2384 return NULL;
2386 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2387 SHA1_DIGEST_LENGTH) == 0)
2388 return NULL;
2390 if (a->progress_cb) {
2391 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2392 ie->path);
2393 if (err)
2394 return err;
2396 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2397 return NULL;
2400 static const struct got_error *
2401 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2402 struct got_fileindex *fileindex,
2403 got_worktree_checkout_cb progress_cb, void *progress_arg)
2405 struct bump_base_commit_id_arg bbc_arg;
2407 bbc_arg.base_commit_id = worktree->base_commit_id;
2408 bbc_arg.entry_name = NULL;
2409 bbc_arg.path = "";
2410 bbc_arg.path_len = 0;
2411 bbc_arg.progress_cb = progress_cb;
2412 bbc_arg.progress_arg = progress_arg;
2414 return got_fileindex_for_each_entry_safe(fileindex,
2415 bump_base_commit_id, &bbc_arg);
2418 static const struct got_error *
2419 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2421 const struct got_error *err = NULL;
2422 char *new_fileindex_path = NULL;
2423 FILE *new_index = NULL;
2424 struct timespec timeout;
2426 err = got_opentemp_named(&new_fileindex_path, &new_index,
2427 fileindex_path);
2428 if (err)
2429 goto done;
2431 err = got_fileindex_write(fileindex, new_index);
2432 if (err)
2433 goto done;
2435 if (rename(new_fileindex_path, fileindex_path) != 0) {
2436 err = got_error_from_errno3("rename", new_fileindex_path,
2437 fileindex_path);
2438 unlink(new_fileindex_path);
2442 * Sleep for a short amount of time to ensure that files modified after
2443 * this program exits have a different time stamp from the one which
2444 * was recorded in the file index.
2446 timeout.tv_sec = 0;
2447 timeout.tv_nsec = 1;
2448 nanosleep(&timeout, NULL);
2449 done:
2450 if (new_index)
2451 fclose(new_index);
2452 free(new_fileindex_path);
2453 return err;
2456 static const struct got_error *
2457 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2458 struct got_object_id **tree_id, const char *wt_relpath,
2459 struct got_commit_object *base_commit, struct got_worktree *worktree,
2460 struct got_repository *repo)
2462 const struct got_error *err = NULL;
2463 struct got_object_id *id = NULL;
2464 char *in_repo_path = NULL;
2465 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2467 *entry_type = GOT_OBJ_TYPE_ANY;
2468 *tree_relpath = NULL;
2469 *tree_id = NULL;
2471 if (wt_relpath[0] == '\0') {
2472 /* Check out all files within the work tree. */
2473 *entry_type = GOT_OBJ_TYPE_TREE;
2474 *tree_relpath = strdup("");
2475 if (*tree_relpath == NULL) {
2476 err = got_error_from_errno("strdup");
2477 goto done;
2479 err = got_object_id_by_path(tree_id, repo, base_commit,
2480 worktree->path_prefix);
2481 if (err)
2482 goto done;
2483 return NULL;
2486 /* Check out a subset of files in the work tree. */
2488 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2489 is_root_wt ? "" : "/", wt_relpath) == -1) {
2490 err = got_error_from_errno("asprintf");
2491 goto done;
2494 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2495 if (err)
2496 goto done;
2498 free(in_repo_path);
2499 in_repo_path = NULL;
2501 err = got_object_get_type(entry_type, repo, id);
2502 if (err)
2503 goto done;
2505 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2506 /* Check out a single file. */
2507 if (strchr(wt_relpath, '/') == NULL) {
2508 /* Check out a single file in work tree's root dir. */
2509 in_repo_path = strdup(worktree->path_prefix);
2510 if (in_repo_path == NULL) {
2511 err = got_error_from_errno("strdup");
2512 goto done;
2514 *tree_relpath = strdup("");
2515 if (*tree_relpath == NULL) {
2516 err = got_error_from_errno("strdup");
2517 goto done;
2519 } else {
2520 /* Check out a single file in a subdirectory. */
2521 err = got_path_dirname(tree_relpath, wt_relpath);
2522 if (err)
2523 return err;
2524 if (asprintf(&in_repo_path, "%s%s%s",
2525 worktree->path_prefix, is_root_wt ? "" : "/",
2526 *tree_relpath) == -1) {
2527 err = got_error_from_errno("asprintf");
2528 goto done;
2531 err = got_object_id_by_path(tree_id, repo,
2532 base_commit, in_repo_path);
2533 } else {
2534 /* Check out all files within a subdirectory. */
2535 *tree_id = got_object_id_dup(id);
2536 if (*tree_id == NULL) {
2537 err = got_error_from_errno("got_object_id_dup");
2538 goto done;
2540 *tree_relpath = strdup(wt_relpath);
2541 if (*tree_relpath == NULL) {
2542 err = got_error_from_errno("strdup");
2543 goto done;
2546 done:
2547 free(id);
2548 free(in_repo_path);
2549 if (err) {
2550 *entry_type = GOT_OBJ_TYPE_ANY;
2551 free(*tree_relpath);
2552 *tree_relpath = NULL;
2553 free(*tree_id);
2554 *tree_id = NULL;
2556 return err;
2559 static const struct got_error *
2560 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2561 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2562 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2563 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2565 const struct got_error *err = NULL;
2566 struct got_commit_object *commit = NULL;
2567 struct got_tree_object *tree = NULL;
2568 struct got_fileindex_diff_tree_cb diff_cb;
2569 struct diff_cb_arg arg;
2571 err = ref_base_commit(worktree, repo);
2572 if (err) {
2573 if (!(err->code == GOT_ERR_ERRNO &&
2574 (errno == EACCES || errno == EROFS)))
2575 goto done;
2576 err = (*progress_cb)(progress_arg,
2577 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2578 if (err)
2579 return err;
2582 err = got_object_open_as_commit(&commit, repo,
2583 worktree->base_commit_id);
2584 if (err)
2585 goto done;
2587 err = got_object_open_as_tree(&tree, repo, tree_id);
2588 if (err)
2589 goto done;
2591 if (entry_name &&
2592 got_object_tree_find_entry(tree, entry_name) == NULL) {
2593 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2594 goto done;
2597 diff_cb.diff_old_new = diff_old_new;
2598 diff_cb.diff_old = diff_old;
2599 diff_cb.diff_new = diff_new;
2600 arg.fileindex = fileindex;
2601 arg.worktree = worktree;
2602 arg.repo = repo;
2603 arg.progress_cb = progress_cb;
2604 arg.progress_arg = progress_arg;
2605 arg.cancel_cb = cancel_cb;
2606 arg.cancel_arg = cancel_arg;
2607 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2608 entry_name, repo, &diff_cb, &arg);
2609 done:
2610 if (tree)
2611 got_object_tree_close(tree);
2612 if (commit)
2613 got_object_commit_close(commit);
2614 return err;
2617 const struct got_error *
2618 got_worktree_checkout_files(struct got_worktree *worktree,
2619 struct got_pathlist_head *paths, struct got_repository *repo,
2620 got_worktree_checkout_cb progress_cb, void *progress_arg,
2621 got_cancel_cb cancel_cb, void *cancel_arg)
2623 const struct got_error *err = NULL, *sync_err, *unlockerr;
2624 struct got_commit_object *commit = NULL;
2625 struct got_tree_object *tree = NULL;
2626 struct got_fileindex *fileindex = NULL;
2627 char *fileindex_path = NULL;
2628 struct got_pathlist_entry *pe;
2629 struct tree_path_data {
2630 STAILQ_ENTRY(tree_path_data) entry;
2631 struct got_object_id *tree_id;
2632 int entry_type;
2633 char *relpath;
2634 char *entry_name;
2635 } *tpd = NULL;
2636 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2638 STAILQ_INIT(&tree_paths);
2640 err = lock_worktree(worktree, LOCK_EX);
2641 if (err)
2642 return err;
2644 err = got_object_open_as_commit(&commit, repo,
2645 worktree->base_commit_id);
2646 if (err)
2647 goto done;
2649 /* Map all specified paths to in-repository trees. */
2650 TAILQ_FOREACH(pe, paths, entry) {
2651 tpd = malloc(sizeof(*tpd));
2652 if (tpd == NULL) {
2653 err = got_error_from_errno("malloc");
2654 goto done;
2657 err = find_tree_entry_for_checkout(&tpd->entry_type,
2658 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2659 worktree, repo);
2660 if (err) {
2661 free(tpd);
2662 goto done;
2665 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2666 err = got_path_basename(&tpd->entry_name, pe->path);
2667 if (err) {
2668 free(tpd->relpath);
2669 free(tpd->tree_id);
2670 free(tpd);
2671 goto done;
2673 } else
2674 tpd->entry_name = NULL;
2676 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2680 * Read the file index.
2681 * Checking out files is supposed to be an idempotent operation.
2682 * If the on-disk file index is incomplete we will try to complete it.
2684 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2685 if (err)
2686 goto done;
2688 tpd = STAILQ_FIRST(&tree_paths);
2689 TAILQ_FOREACH(pe, paths, entry) {
2690 struct bump_base_commit_id_arg bbc_arg;
2692 err = checkout_files(worktree, fileindex, tpd->relpath,
2693 tpd->tree_id, tpd->entry_name, repo,
2694 progress_cb, progress_arg, cancel_cb, cancel_arg);
2695 if (err)
2696 break;
2698 bbc_arg.base_commit_id = worktree->base_commit_id;
2699 bbc_arg.entry_name = tpd->entry_name;
2700 bbc_arg.path = pe->path;
2701 bbc_arg.path_len = pe->path_len;
2702 bbc_arg.progress_cb = progress_cb;
2703 bbc_arg.progress_arg = progress_arg;
2704 err = got_fileindex_for_each_entry_safe(fileindex,
2705 bump_base_commit_id, &bbc_arg);
2706 if (err)
2707 break;
2709 tpd = STAILQ_NEXT(tpd, entry);
2711 sync_err = sync_fileindex(fileindex, fileindex_path);
2712 if (sync_err && err == NULL)
2713 err = sync_err;
2714 done:
2715 free(fileindex_path);
2716 if (tree)
2717 got_object_tree_close(tree);
2718 if (commit)
2719 got_object_commit_close(commit);
2720 if (fileindex)
2721 got_fileindex_free(fileindex);
2722 while (!STAILQ_EMPTY(&tree_paths)) {
2723 tpd = STAILQ_FIRST(&tree_paths);
2724 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2725 free(tpd->relpath);
2726 free(tpd->tree_id);
2727 free(tpd);
2729 unlockerr = lock_worktree(worktree, LOCK_SH);
2730 if (unlockerr && err == NULL)
2731 err = unlockerr;
2732 return err;
2735 struct merge_file_cb_arg {
2736 struct got_worktree *worktree;
2737 struct got_fileindex *fileindex;
2738 got_worktree_checkout_cb progress_cb;
2739 void *progress_arg;
2740 got_cancel_cb cancel_cb;
2741 void *cancel_arg;
2742 const char *label_orig;
2743 struct got_object_id *commit_id2;
2744 int allow_bad_symlinks;
2747 static const struct got_error *
2748 merge_file_cb(void *arg, struct got_blob_object *blob1,
2749 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2750 struct got_object_id *id1, struct got_object_id *id2,
2751 const char *path1, const char *path2,
2752 mode_t mode1, mode_t mode2, struct got_repository *repo)
2754 static const struct got_error *err = NULL;
2755 struct merge_file_cb_arg *a = arg;
2756 struct got_fileindex_entry *ie;
2757 char *ondisk_path = NULL;
2758 struct stat sb;
2759 unsigned char status;
2760 int local_changes_subsumed;
2761 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2762 char *id_str = NULL, *label_deriv2 = NULL;
2764 if (blob1 && blob2) {
2765 ie = got_fileindex_entry_get(a->fileindex, path2,
2766 strlen(path2));
2767 if (ie == NULL)
2768 return (*a->progress_cb)(a->progress_arg,
2769 GOT_STATUS_MISSING, path2);
2771 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2772 path2) == -1)
2773 return got_error_from_errno("asprintf");
2775 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2776 repo);
2777 if (err)
2778 goto done;
2780 if (status == GOT_STATUS_DELETE) {
2781 err = (*a->progress_cb)(a->progress_arg,
2782 GOT_STATUS_MERGE, path2);
2783 goto done;
2785 if (status != GOT_STATUS_NO_CHANGE &&
2786 status != GOT_STATUS_MODIFY &&
2787 status != GOT_STATUS_CONFLICT &&
2788 status != GOT_STATUS_ADD) {
2789 err = (*a->progress_cb)(a->progress_arg, status, path2);
2790 goto done;
2793 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2794 char *link_target2;
2795 err = got_object_blob_read_to_str(&link_target2, blob2);
2796 if (err)
2797 goto done;
2798 err = merge_symlink(a->worktree, blob1, ondisk_path,
2799 path2, a->label_orig, link_target2, a->commit_id2,
2800 repo, a->progress_cb, a->progress_arg);
2801 free(link_target2);
2802 } else {
2803 int fd;
2805 f_orig = got_opentemp();
2806 if (f_orig == NULL) {
2807 err = got_error_from_errno("got_opentemp");
2808 goto done;
2810 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2811 f_orig, blob1);
2812 if (err)
2813 goto done;
2815 f_deriv2 = got_opentemp();
2816 if (f_deriv2 == NULL)
2817 goto done;
2818 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2819 f_deriv2, blob2);
2820 if (err)
2821 goto done;
2823 fd = open(ondisk_path,
2824 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2825 if (fd == -1) {
2826 err = got_error_from_errno2("open",
2827 ondisk_path);
2828 goto done;
2830 f_deriv = fdopen(fd, "r");
2831 if (f_deriv == NULL) {
2832 err = got_error_from_errno2("fdopen",
2833 ondisk_path);
2834 close(fd);
2835 goto done;
2837 err = got_object_id_str(&id_str, a->commit_id2);
2838 if (err)
2839 goto done;
2840 if (asprintf(&label_deriv2, "%s: commit %s",
2841 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2842 err = got_error_from_errno("asprintf");
2843 goto done;
2845 err = merge_file(&local_changes_subsumed, a->worktree,
2846 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2847 sb.st_mode, a->label_orig, NULL, label_deriv2,
2848 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2849 a->progress_cb, a->progress_arg);
2851 } else if (blob1) {
2852 ie = got_fileindex_entry_get(a->fileindex, path1,
2853 strlen(path1));
2854 if (ie == NULL)
2855 return (*a->progress_cb)(a->progress_arg,
2856 GOT_STATUS_MISSING, path1);
2858 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2859 path1) == -1)
2860 return got_error_from_errno("asprintf");
2862 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2863 repo);
2864 if (err)
2865 goto done;
2867 switch (status) {
2868 case GOT_STATUS_NO_CHANGE:
2869 err = (*a->progress_cb)(a->progress_arg,
2870 GOT_STATUS_DELETE, path1);
2871 if (err)
2872 goto done;
2873 err = remove_ondisk_file(a->worktree->root_path, path1);
2874 if (err)
2875 goto done;
2876 if (ie)
2877 got_fileindex_entry_mark_deleted_from_disk(ie);
2878 break;
2879 case GOT_STATUS_DELETE:
2880 case GOT_STATUS_MISSING:
2881 err = (*a->progress_cb)(a->progress_arg,
2882 GOT_STATUS_DELETE, path1);
2883 if (err)
2884 goto done;
2885 if (ie)
2886 got_fileindex_entry_mark_deleted_from_disk(ie);
2887 break;
2888 case GOT_STATUS_ADD: {
2889 struct got_object_id *id;
2890 FILE *blob1_f;
2891 off_t blob1_size;
2893 * Delete the added file only if its content already
2894 * exists in the repository.
2896 err = got_object_blob_file_create(&id, &blob1_f,
2897 &blob1_size, path1);
2898 if (err)
2899 goto done;
2900 if (got_object_id_cmp(id, id1) == 0) {
2901 err = (*a->progress_cb)(a->progress_arg,
2902 GOT_STATUS_DELETE, path1);
2903 if (err)
2904 goto done;
2905 err = remove_ondisk_file(a->worktree->root_path,
2906 path1);
2907 if (err)
2908 goto done;
2909 if (ie)
2910 got_fileindex_entry_remove(a->fileindex,
2911 ie);
2912 } else {
2913 err = (*a->progress_cb)(a->progress_arg,
2914 GOT_STATUS_CANNOT_DELETE, path1);
2916 if (fclose(blob1_f) == EOF && err == NULL)
2917 err = got_error_from_errno("fclose");
2918 free(id);
2919 if (err)
2920 goto done;
2921 break;
2923 case GOT_STATUS_MODIFY:
2924 case GOT_STATUS_CONFLICT:
2925 err = (*a->progress_cb)(a->progress_arg,
2926 GOT_STATUS_CANNOT_DELETE, path1);
2927 if (err)
2928 goto done;
2929 break;
2930 case GOT_STATUS_OBSTRUCTED:
2931 err = (*a->progress_cb)(a->progress_arg, status, path1);
2932 if (err)
2933 goto done;
2934 break;
2935 default:
2936 break;
2938 } else if (blob2) {
2939 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2940 path2) == -1)
2941 return got_error_from_errno("asprintf");
2942 ie = got_fileindex_entry_get(a->fileindex, path2,
2943 strlen(path2));
2944 if (ie) {
2945 err = get_file_status(&status, &sb, ie, ondisk_path,
2946 -1, NULL, repo);
2947 if (err)
2948 goto done;
2949 if (status != GOT_STATUS_NO_CHANGE &&
2950 status != GOT_STATUS_MODIFY &&
2951 status != GOT_STATUS_CONFLICT &&
2952 status != GOT_STATUS_ADD) {
2953 err = (*a->progress_cb)(a->progress_arg,
2954 status, path2);
2955 goto done;
2957 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2958 char *link_target2;
2959 err = got_object_blob_read_to_str(&link_target2,
2960 blob2);
2961 if (err)
2962 goto done;
2963 err = merge_symlink(a->worktree, NULL,
2964 ondisk_path, path2, a->label_orig,
2965 link_target2, a->commit_id2, repo,
2966 a->progress_cb, a->progress_arg);
2967 free(link_target2);
2968 } else if (S_ISREG(sb.st_mode)) {
2969 err = merge_blob(&local_changes_subsumed,
2970 a->worktree, NULL, ondisk_path, path2,
2971 sb.st_mode, a->label_orig, blob2,
2972 a->commit_id2, repo, a->progress_cb,
2973 a->progress_arg);
2974 } else {
2975 err = got_error_path(ondisk_path,
2976 GOT_ERR_FILE_OBSTRUCTED);
2978 if (err)
2979 goto done;
2980 if (status == GOT_STATUS_DELETE) {
2981 err = got_fileindex_entry_update(ie,
2982 a->worktree->root_fd, path2, blob2->id.sha1,
2983 a->worktree->base_commit_id->sha1, 0);
2984 if (err)
2985 goto done;
2987 } else {
2988 int is_bad_symlink = 0;
2989 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2990 if (S_ISLNK(mode2)) {
2991 err = install_symlink(&is_bad_symlink,
2992 a->worktree, ondisk_path, path2, blob2, 0,
2993 0, 1, a->allow_bad_symlinks, repo,
2994 a->progress_cb, a->progress_arg);
2995 } else {
2996 err = install_blob(a->worktree, ondisk_path, path2,
2997 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2998 a->progress_cb, a->progress_arg);
3000 if (err)
3001 goto done;
3002 err = got_fileindex_entry_alloc(&ie, path2);
3003 if (err)
3004 goto done;
3005 err = got_fileindex_entry_update(ie,
3006 a->worktree->root_fd, path2, NULL, NULL, 1);
3007 if (err) {
3008 got_fileindex_entry_free(ie);
3009 goto done;
3011 err = got_fileindex_entry_add(a->fileindex, ie);
3012 if (err) {
3013 got_fileindex_entry_free(ie);
3014 goto done;
3016 if (is_bad_symlink) {
3017 got_fileindex_entry_filetype_set(ie,
3018 GOT_FILEIDX_MODE_BAD_SYMLINK);
3022 done:
3023 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3024 err = got_error_from_errno("fclose");
3025 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3026 err = got_error_from_errno("fclose");
3027 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3028 err = got_error_from_errno("fclose");
3029 free(id_str);
3030 free(label_deriv2);
3031 free(ondisk_path);
3032 return err;
3035 static const struct got_error *
3036 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3038 struct got_worktree *worktree = arg;
3040 /* Reject merges into a work tree with mixed base commits. */
3041 if (got_fileindex_entry_has_commit(ie) &&
3042 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3043 SHA1_DIGEST_LENGTH) != 0)
3044 return got_error(GOT_ERR_MIXED_COMMITS);
3046 return NULL;
3049 struct check_merge_conflicts_arg {
3050 struct got_worktree *worktree;
3051 struct got_fileindex *fileindex;
3052 struct got_repository *repo;
3055 static const struct got_error *
3056 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3057 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3058 struct got_object_id *id1, struct got_object_id *id2,
3059 const char *path1, const char *path2,
3060 mode_t mode1, mode_t mode2, struct got_repository *repo)
3062 const struct got_error *err = NULL;
3063 struct check_merge_conflicts_arg *a = arg;
3064 unsigned char status;
3065 struct stat sb;
3066 struct got_fileindex_entry *ie;
3067 const char *path = path2 ? path2 : path1;
3068 struct got_object_id *id = id2 ? id2 : id1;
3069 char *ondisk_path;
3071 if (id == NULL)
3072 return NULL;
3074 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3075 if (ie == NULL)
3076 return NULL;
3078 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3079 == -1)
3080 return got_error_from_errno("asprintf");
3082 /* Reject merges into a work tree with conflicted files. */
3083 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3084 free(ondisk_path);
3085 if (err)
3086 return err;
3087 if (status == GOT_STATUS_CONFLICT)
3088 return got_error(GOT_ERR_CONFLICTS);
3090 return NULL;
3093 static const struct got_error *
3094 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3095 const char *fileindex_path, struct got_object_id *commit_id1,
3096 struct got_object_id *commit_id2, struct got_repository *repo,
3097 got_worktree_checkout_cb progress_cb, void *progress_arg,
3098 got_cancel_cb cancel_cb, void *cancel_arg)
3100 const struct got_error *err = NULL, *sync_err;
3101 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3102 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3103 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3104 struct check_merge_conflicts_arg cmc_arg;
3105 struct merge_file_cb_arg arg;
3106 char *label_orig = NULL;
3107 FILE *f1 = NULL, *f2 = NULL;
3109 if (commit_id1) {
3110 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3111 if (err)
3112 goto done;
3113 err = got_object_id_by_path(&tree_id1, repo, commit1,
3114 worktree->path_prefix);
3115 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3116 goto done;
3118 if (tree_id1) {
3119 char *id_str;
3121 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3122 if (err)
3123 goto done;
3125 err = got_object_id_str(&id_str, commit_id1);
3126 if (err)
3127 goto done;
3129 if (asprintf(&label_orig, "%s: commit %s",
3130 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3131 err = got_error_from_errno("asprintf");
3132 free(id_str);
3133 goto done;
3135 free(id_str);
3137 f1 = got_opentemp();
3138 if (f1 == NULL) {
3139 err = got_error_from_errno("got_opentemp");
3140 goto done;
3144 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3145 if (err)
3146 goto done;
3148 err = got_object_id_by_path(&tree_id2, repo, commit2,
3149 worktree->path_prefix);
3150 if (err)
3151 goto done;
3153 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3154 if (err)
3155 goto done;
3157 f2 = got_opentemp();
3158 if (f2 == NULL) {
3159 err = got_error_from_errno("got_opentemp");
3160 goto done;
3163 cmc_arg.worktree = worktree;
3164 cmc_arg.fileindex = fileindex;
3165 cmc_arg.repo = repo;
3166 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3167 check_merge_conflicts, &cmc_arg, 0);
3168 if (err)
3169 goto done;
3171 arg.worktree = worktree;
3172 arg.fileindex = fileindex;
3173 arg.progress_cb = progress_cb;
3174 arg.progress_arg = progress_arg;
3175 arg.cancel_cb = cancel_cb;
3176 arg.cancel_arg = cancel_arg;
3177 arg.label_orig = label_orig;
3178 arg.commit_id2 = commit_id2;
3179 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3180 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3181 merge_file_cb, &arg, 1);
3182 sync_err = sync_fileindex(fileindex, fileindex_path);
3183 if (sync_err && err == NULL)
3184 err = sync_err;
3185 done:
3186 if (commit1)
3187 got_object_commit_close(commit1);
3188 if (commit2)
3189 got_object_commit_close(commit2);
3190 if (tree1)
3191 got_object_tree_close(tree1);
3192 if (tree2)
3193 got_object_tree_close(tree2);
3194 if (f1 && fclose(f1) == EOF && err == NULL)
3195 err = got_error_from_errno("fclose");
3196 if (f2 && fclose(f2) == EOF && err == NULL)
3197 err = got_error_from_errno("fclose");
3198 free(label_orig);
3199 return err;
3202 const struct got_error *
3203 got_worktree_merge_files(struct got_worktree *worktree,
3204 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3205 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3206 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3208 const struct got_error *err, *unlockerr;
3209 char *fileindex_path = NULL;
3210 struct got_fileindex *fileindex = NULL;
3212 err = lock_worktree(worktree, LOCK_EX);
3213 if (err)
3214 return err;
3216 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3217 if (err)
3218 goto done;
3220 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3221 worktree);
3222 if (err)
3223 goto done;
3225 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3226 commit_id2, repo, progress_cb, progress_arg,
3227 cancel_cb, cancel_arg);
3228 done:
3229 if (fileindex)
3230 got_fileindex_free(fileindex);
3231 free(fileindex_path);
3232 unlockerr = lock_worktree(worktree, LOCK_SH);
3233 if (unlockerr && err == NULL)
3234 err = unlockerr;
3235 return err;
3238 struct diff_dir_cb_arg {
3239 struct got_fileindex *fileindex;
3240 struct got_worktree *worktree;
3241 const char *status_path;
3242 size_t status_path_len;
3243 struct got_repository *repo;
3244 got_worktree_status_cb status_cb;
3245 void *status_arg;
3246 got_cancel_cb cancel_cb;
3247 void *cancel_arg;
3248 /* A pathlist containing per-directory pathlists of ignore patterns. */
3249 struct got_pathlist_head *ignores;
3250 int report_unchanged;
3251 int no_ignores;
3254 static const struct got_error *
3255 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3256 int dirfd, const char *de_name,
3257 got_worktree_status_cb status_cb, void *status_arg,
3258 struct got_repository *repo, int report_unchanged)
3260 const struct got_error *err = NULL;
3261 unsigned char status = GOT_STATUS_NO_CHANGE;
3262 unsigned char staged_status = get_staged_status(ie);
3263 struct stat sb;
3264 struct got_object_id blob_id, commit_id, staged_blob_id;
3265 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3266 struct got_object_id *staged_blob_idp = NULL;
3268 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3269 if (err)
3270 return err;
3272 if (status == GOT_STATUS_NO_CHANGE &&
3273 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3274 return NULL;
3276 if (got_fileindex_entry_has_blob(ie)) {
3277 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3278 blob_idp = &blob_id;
3280 if (got_fileindex_entry_has_commit(ie)) {
3281 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3282 commit_idp = &commit_id;
3284 if (staged_status == GOT_STATUS_ADD ||
3285 staged_status == GOT_STATUS_MODIFY) {
3286 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3287 SHA1_DIGEST_LENGTH);
3288 staged_blob_idp = &staged_blob_id;
3291 return (*status_cb)(status_arg, status, staged_status,
3292 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3295 static const struct got_error *
3296 status_old_new(void *arg, struct got_fileindex_entry *ie,
3297 struct dirent *de, const char *parent_path, int dirfd)
3299 const struct got_error *err = NULL;
3300 struct diff_dir_cb_arg *a = arg;
3301 char *abspath;
3303 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3304 return got_error(GOT_ERR_CANCELLED);
3306 if (got_path_cmp(parent_path, a->status_path,
3307 strlen(parent_path), a->status_path_len) != 0 &&
3308 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3309 return NULL;
3311 if (parent_path[0]) {
3312 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3313 parent_path, de->d_name) == -1)
3314 return got_error_from_errno("asprintf");
3315 } else {
3316 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3317 de->d_name) == -1)
3318 return got_error_from_errno("asprintf");
3321 err = report_file_status(ie, abspath, dirfd, de->d_name,
3322 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3323 free(abspath);
3324 return err;
3327 static const struct got_error *
3328 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3330 struct diff_dir_cb_arg *a = arg;
3331 struct got_object_id blob_id, commit_id;
3332 unsigned char status;
3334 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3335 return got_error(GOT_ERR_CANCELLED);
3337 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3338 return NULL;
3340 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3341 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3342 if (got_fileindex_entry_has_file_on_disk(ie))
3343 status = GOT_STATUS_MISSING;
3344 else
3345 status = GOT_STATUS_DELETE;
3346 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3347 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3350 void
3351 free_ignorelist(struct got_pathlist_head *ignorelist)
3353 struct got_pathlist_entry *pe;
3355 TAILQ_FOREACH(pe, ignorelist, entry)
3356 free((char *)pe->path);
3357 got_pathlist_free(ignorelist);
3360 void
3361 free_ignores(struct got_pathlist_head *ignores)
3363 struct got_pathlist_entry *pe;
3365 TAILQ_FOREACH(pe, ignores, entry) {
3366 struct got_pathlist_head *ignorelist = pe->data;
3367 free_ignorelist(ignorelist);
3368 free((char *)pe->path);
3370 got_pathlist_free(ignores);
3373 static const struct got_error *
3374 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3376 const struct got_error *err = NULL;
3377 struct got_pathlist_entry *pe = NULL;
3378 struct got_pathlist_head *ignorelist;
3379 char *line = NULL, *pattern, *dirpath = NULL;
3380 size_t linesize = 0;
3381 ssize_t linelen;
3383 ignorelist = calloc(1, sizeof(*ignorelist));
3384 if (ignorelist == NULL)
3385 return got_error_from_errno("calloc");
3386 TAILQ_INIT(ignorelist);
3388 while ((linelen = getline(&line, &linesize, f)) != -1) {
3389 if (linelen > 0 && line[linelen - 1] == '\n')
3390 line[linelen - 1] = '\0';
3392 /* Git's ignores may contain comments. */
3393 if (line[0] == '#')
3394 continue;
3396 /* Git's negated patterns are not (yet?) supported. */
3397 if (line[0] == '!')
3398 continue;
3400 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3401 line) == -1) {
3402 err = got_error_from_errno("asprintf");
3403 goto done;
3405 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3406 if (err)
3407 goto done;
3409 if (ferror(f)) {
3410 err = got_error_from_errno("getline");
3411 goto done;
3414 dirpath = strdup(path);
3415 if (dirpath == NULL) {
3416 err = got_error_from_errno("strdup");
3417 goto done;
3419 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3420 done:
3421 free(line);
3422 if (err || pe == NULL) {
3423 free(dirpath);
3424 free_ignorelist(ignorelist);
3426 return err;
3429 int
3430 match_ignores(struct got_pathlist_head *ignores, const char *path)
3432 struct got_pathlist_entry *pe;
3434 /* Handle patterns which match in all directories. */
3435 TAILQ_FOREACH(pe, ignores, entry) {
3436 struct got_pathlist_head *ignorelist = pe->data;
3437 struct got_pathlist_entry *pi;
3439 TAILQ_FOREACH(pi, ignorelist, entry) {
3440 const char *p, *pattern = pi->path;
3442 if (strncmp(pattern, "**/", 3) != 0)
3443 continue;
3444 pattern += 3;
3445 p = path;
3446 while (*p) {
3447 if (fnmatch(pattern, p,
3448 FNM_PATHNAME | FNM_LEADING_DIR)) {
3449 /* Retry in next directory. */
3450 while (*p && *p != '/')
3451 p++;
3452 while (*p == '/')
3453 p++;
3454 continue;
3456 return 1;
3462 * The ignores pathlist contains ignore lists from children before
3463 * parents, so we can find the most specific ignorelist by walking
3464 * ignores backwards.
3466 pe = TAILQ_LAST(ignores, got_pathlist_head);
3467 while (pe) {
3468 if (got_path_is_child(path, pe->path, pe->path_len)) {
3469 struct got_pathlist_head *ignorelist = pe->data;
3470 struct got_pathlist_entry *pi;
3471 TAILQ_FOREACH(pi, ignorelist, entry) {
3472 const char *pattern = pi->path;
3473 int flags = FNM_LEADING_DIR;
3474 if (strstr(pattern, "/**/") == NULL)
3475 flags |= FNM_PATHNAME;
3476 if (fnmatch(pattern, path, flags))
3477 continue;
3478 return 1;
3481 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3484 return 0;
3487 static const struct got_error *
3488 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3489 const char *path, int dirfd, const char *ignores_filename)
3491 const struct got_error *err = NULL;
3492 char *ignorespath;
3493 int fd = -1;
3494 FILE *ignoresfile = NULL;
3496 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3497 path[0] ? "/" : "", ignores_filename) == -1)
3498 return got_error_from_errno("asprintf");
3500 if (dirfd != -1) {
3501 fd = openat(dirfd, ignores_filename,
3502 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3503 if (fd == -1) {
3504 if (errno != ENOENT && errno != EACCES)
3505 err = got_error_from_errno2("openat",
3506 ignorespath);
3507 } else {
3508 ignoresfile = fdopen(fd, "r");
3509 if (ignoresfile == NULL)
3510 err = got_error_from_errno2("fdopen",
3511 ignorespath);
3512 else {
3513 fd = -1;
3514 err = read_ignores(ignores, path, ignoresfile);
3517 } else {
3518 ignoresfile = fopen(ignorespath, "re");
3519 if (ignoresfile == NULL) {
3520 if (errno != ENOENT && errno != EACCES)
3521 err = got_error_from_errno2("fopen",
3522 ignorespath);
3523 } else
3524 err = read_ignores(ignores, path, ignoresfile);
3527 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3528 err = got_error_from_errno2("fclose", path);
3529 if (fd != -1 && close(fd) == -1 && err == NULL)
3530 err = got_error_from_errno2("close", path);
3531 free(ignorespath);
3532 return err;
3535 static const struct got_error *
3536 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3537 int dirfd)
3539 const struct got_error *err = NULL;
3540 struct diff_dir_cb_arg *a = arg;
3541 char *path = NULL;
3543 if (ignore != NULL)
3544 *ignore = 0;
3546 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3547 return got_error(GOT_ERR_CANCELLED);
3549 if (parent_path[0]) {
3550 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3551 return got_error_from_errno("asprintf");
3552 } else {
3553 path = de->d_name;
3556 if (de->d_type == DT_DIR) {
3557 if (!a->no_ignores && ignore != NULL &&
3558 match_ignores(a->ignores, path))
3559 *ignore = 1;
3560 } else if (!match_ignores(a->ignores, path) &&
3561 got_path_is_child(path, a->status_path, a->status_path_len))
3562 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3563 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3564 if (parent_path[0])
3565 free(path);
3566 return err;
3569 static const struct got_error *
3570 status_traverse(void *arg, const char *path, int dirfd)
3572 const struct got_error *err = NULL;
3573 struct diff_dir_cb_arg *a = arg;
3575 if (a->no_ignores)
3576 return NULL;
3578 err = add_ignores(a->ignores, a->worktree->root_path,
3579 path, dirfd, ".cvsignore");
3580 if (err)
3581 return err;
3583 err = add_ignores(a->ignores, a->worktree->root_path, path,
3584 dirfd, ".gitignore");
3586 return err;
3589 static const struct got_error *
3590 report_single_file_status(const char *path, const char *ondisk_path,
3591 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3592 void *status_arg, struct got_repository *repo, int report_unchanged,
3593 struct got_pathlist_head *ignores, int no_ignores)
3595 struct got_fileindex_entry *ie;
3596 struct stat sb;
3598 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3599 if (ie)
3600 return report_file_status(ie, ondisk_path, -1, NULL,
3601 status_cb, status_arg, repo, report_unchanged);
3603 if (lstat(ondisk_path, &sb) == -1) {
3604 if (errno != ENOENT)
3605 return got_error_from_errno2("lstat", ondisk_path);
3606 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3607 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3610 if (!no_ignores && match_ignores(ignores, path))
3611 return NULL;
3613 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3614 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3615 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3617 return NULL;
3620 static const struct got_error *
3621 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3622 const char *root_path, const char *path)
3624 const struct got_error *err;
3625 char *parent_path, *next_parent_path = NULL;
3627 err = add_ignores(ignores, root_path, "", -1,
3628 ".cvsignore");
3629 if (err)
3630 return err;
3632 err = add_ignores(ignores, root_path, "", -1,
3633 ".gitignore");
3634 if (err)
3635 return err;
3637 err = got_path_dirname(&parent_path, path);
3638 if (err) {
3639 if (err->code == GOT_ERR_BAD_PATH)
3640 return NULL; /* cannot traverse parent */
3641 return err;
3643 for (;;) {
3644 err = add_ignores(ignores, root_path, parent_path, -1,
3645 ".cvsignore");
3646 if (err)
3647 break;
3648 err = add_ignores(ignores, root_path, parent_path, -1,
3649 ".gitignore");
3650 if (err)
3651 break;
3652 err = got_path_dirname(&next_parent_path, parent_path);
3653 if (err) {
3654 if (err->code == GOT_ERR_BAD_PATH)
3655 err = NULL; /* traversed everything */
3656 break;
3658 if (got_path_is_root_dir(parent_path))
3659 break;
3660 free(parent_path);
3661 parent_path = next_parent_path;
3662 next_parent_path = NULL;
3665 free(parent_path);
3666 free(next_parent_path);
3667 return err;
3670 static const struct got_error *
3671 worktree_status(struct got_worktree *worktree, const char *path,
3672 struct got_fileindex *fileindex, struct got_repository *repo,
3673 got_worktree_status_cb status_cb, void *status_arg,
3674 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3675 int report_unchanged)
3677 const struct got_error *err = NULL;
3678 int fd = -1;
3679 struct got_fileindex_diff_dir_cb fdiff_cb;
3680 struct diff_dir_cb_arg arg;
3681 char *ondisk_path = NULL;
3682 struct got_pathlist_head ignores;
3683 struct got_fileindex_entry *ie;
3685 TAILQ_INIT(&ignores);
3687 if (asprintf(&ondisk_path, "%s%s%s",
3688 worktree->root_path, path[0] ? "/" : "", path) == -1)
3689 return got_error_from_errno("asprintf");
3691 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3692 if (ie) {
3693 err = report_single_file_status(path, ondisk_path,
3694 fileindex, status_cb, status_arg, repo,
3695 report_unchanged, &ignores, no_ignores);
3696 goto done;
3699 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3700 if (fd == -1) {
3701 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3702 !got_err_open_nofollow_on_symlink())
3703 err = got_error_from_errno2("open", ondisk_path);
3704 else {
3705 if (!no_ignores) {
3706 err = add_ignores_from_parent_paths(&ignores,
3707 worktree->root_path, ondisk_path);
3708 if (err)
3709 goto done;
3711 err = report_single_file_status(path, ondisk_path,
3712 fileindex, status_cb, status_arg, repo,
3713 report_unchanged, &ignores, no_ignores);
3715 } else {
3716 fdiff_cb.diff_old_new = status_old_new;
3717 fdiff_cb.diff_old = status_old;
3718 fdiff_cb.diff_new = status_new;
3719 fdiff_cb.diff_traverse = status_traverse;
3720 arg.fileindex = fileindex;
3721 arg.worktree = worktree;
3722 arg.status_path = path;
3723 arg.status_path_len = strlen(path);
3724 arg.repo = repo;
3725 arg.status_cb = status_cb;
3726 arg.status_arg = status_arg;
3727 arg.cancel_cb = cancel_cb;
3728 arg.cancel_arg = cancel_arg;
3729 arg.report_unchanged = report_unchanged;
3730 arg.no_ignores = no_ignores;
3731 if (!no_ignores) {
3732 err = add_ignores_from_parent_paths(&ignores,
3733 worktree->root_path, path);
3734 if (err)
3735 goto done;
3737 arg.ignores = &ignores;
3738 err = got_fileindex_diff_dir(fileindex, fd,
3739 worktree->root_path, path, repo, &fdiff_cb, &arg);
3741 done:
3742 free_ignores(&ignores);
3743 if (fd != -1 && close(fd) == -1 && err == NULL)
3744 err = got_error_from_errno("close");
3745 free(ondisk_path);
3746 return err;
3749 const struct got_error *
3750 got_worktree_status(struct got_worktree *worktree,
3751 struct got_pathlist_head *paths, struct got_repository *repo,
3752 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3753 got_cancel_cb cancel_cb, void *cancel_arg)
3755 const struct got_error *err = NULL;
3756 char *fileindex_path = NULL;
3757 struct got_fileindex *fileindex = NULL;
3758 struct got_pathlist_entry *pe;
3760 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3761 if (err)
3762 return err;
3764 TAILQ_FOREACH(pe, paths, entry) {
3765 err = worktree_status(worktree, pe->path, fileindex, repo,
3766 status_cb, status_arg, cancel_cb, cancel_arg,
3767 no_ignores, 0);
3768 if (err)
3769 break;
3771 free(fileindex_path);
3772 got_fileindex_free(fileindex);
3773 return err;
3776 const struct got_error *
3777 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3778 const char *arg)
3780 const struct got_error *err = NULL;
3781 char *resolved = NULL, *cwd = NULL, *path = NULL;
3782 size_t len;
3783 struct stat sb;
3784 char *abspath = NULL;
3785 char canonpath[PATH_MAX];
3787 *wt_path = NULL;
3789 cwd = getcwd(NULL, 0);
3790 if (cwd == NULL)
3791 return got_error_from_errno("getcwd");
3793 if (lstat(arg, &sb) == -1) {
3794 if (errno != ENOENT) {
3795 err = got_error_from_errno2("lstat", arg);
3796 goto done;
3798 sb.st_mode = 0;
3800 if (S_ISLNK(sb.st_mode)) {
3802 * We cannot use realpath(3) with symlinks since we want to
3803 * operate on the symlink itself.
3804 * But we can make the path absolute, assuming it is relative
3805 * to the current working directory, and then canonicalize it.
3807 if (!got_path_is_absolute(arg)) {
3808 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3809 err = got_error_from_errno("asprintf");
3810 goto done;
3814 err = got_canonpath(abspath ? abspath : arg, canonpath,
3815 sizeof(canonpath));
3816 if (err)
3817 goto done;
3818 resolved = strdup(canonpath);
3819 if (resolved == NULL) {
3820 err = got_error_from_errno("strdup");
3821 goto done;
3823 } else {
3824 resolved = realpath(arg, NULL);
3825 if (resolved == NULL) {
3826 if (errno != ENOENT) {
3827 err = got_error_from_errno2("realpath", arg);
3828 goto done;
3830 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3831 err = got_error_from_errno("asprintf");
3832 goto done;
3834 err = got_canonpath(abspath, canonpath,
3835 sizeof(canonpath));
3836 if (err)
3837 goto done;
3838 resolved = strdup(canonpath);
3839 if (resolved == NULL) {
3840 err = got_error_from_errno("strdup");
3841 goto done;
3846 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3847 strlen(got_worktree_get_root_path(worktree)))) {
3848 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3849 goto done;
3852 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3853 err = got_path_skip_common_ancestor(&path,
3854 got_worktree_get_root_path(worktree), resolved);
3855 if (err)
3856 goto done;
3857 } else {
3858 path = strdup("");
3859 if (path == NULL) {
3860 err = got_error_from_errno("strdup");
3861 goto done;
3865 /* XXX status walk can't deal with trailing slash! */
3866 len = strlen(path);
3867 while (len > 0 && path[len - 1] == '/') {
3868 path[len - 1] = '\0';
3869 len--;
3871 done:
3872 free(abspath);
3873 free(resolved);
3874 free(cwd);
3875 if (err == NULL)
3876 *wt_path = path;
3877 else
3878 free(path);
3879 return err;
3882 struct schedule_addition_args {
3883 struct got_worktree *worktree;
3884 struct got_fileindex *fileindex;
3885 got_worktree_checkout_cb progress_cb;
3886 void *progress_arg;
3887 struct got_repository *repo;
3890 static const struct got_error *
3891 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3892 const char *relpath, struct got_object_id *blob_id,
3893 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3894 int dirfd, const char *de_name)
3896 struct schedule_addition_args *a = arg;
3897 const struct got_error *err = NULL;
3898 struct got_fileindex_entry *ie;
3899 struct stat sb;
3900 char *ondisk_path;
3902 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3903 relpath) == -1)
3904 return got_error_from_errno("asprintf");
3906 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3907 if (ie) {
3908 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3909 de_name, a->repo);
3910 if (err)
3911 goto done;
3912 /* Re-adding an existing entry is a no-op. */
3913 if (status == GOT_STATUS_ADD)
3914 goto done;
3915 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3916 if (err)
3917 goto done;
3920 if (status != GOT_STATUS_UNVERSIONED) {
3921 if (status == GOT_STATUS_NONEXISTENT)
3922 err = got_error_set_errno(ENOENT, ondisk_path);
3923 else
3924 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3925 goto done;
3928 err = got_fileindex_entry_alloc(&ie, relpath);
3929 if (err)
3930 goto done;
3931 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3932 relpath, NULL, NULL, 1);
3933 if (err) {
3934 got_fileindex_entry_free(ie);
3935 goto done;
3937 err = got_fileindex_entry_add(a->fileindex, ie);
3938 if (err) {
3939 got_fileindex_entry_free(ie);
3940 goto done;
3942 done:
3943 free(ondisk_path);
3944 if (err)
3945 return err;
3946 if (status == GOT_STATUS_ADD)
3947 return NULL;
3948 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3951 const struct got_error *
3952 got_worktree_schedule_add(struct got_worktree *worktree,
3953 struct got_pathlist_head *paths,
3954 got_worktree_checkout_cb progress_cb, void *progress_arg,
3955 struct got_repository *repo, int no_ignores)
3957 struct got_fileindex *fileindex = NULL;
3958 char *fileindex_path = NULL;
3959 const struct got_error *err = NULL, *sync_err, *unlockerr;
3960 struct got_pathlist_entry *pe;
3961 struct schedule_addition_args saa;
3963 err = lock_worktree(worktree, LOCK_EX);
3964 if (err)
3965 return err;
3967 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3968 if (err)
3969 goto done;
3971 saa.worktree = worktree;
3972 saa.fileindex = fileindex;
3973 saa.progress_cb = progress_cb;
3974 saa.progress_arg = progress_arg;
3975 saa.repo = repo;
3977 TAILQ_FOREACH(pe, paths, entry) {
3978 err = worktree_status(worktree, pe->path, fileindex, repo,
3979 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3980 if (err)
3981 break;
3983 sync_err = sync_fileindex(fileindex, fileindex_path);
3984 if (sync_err && err == NULL)
3985 err = sync_err;
3986 done:
3987 free(fileindex_path);
3988 if (fileindex)
3989 got_fileindex_free(fileindex);
3990 unlockerr = lock_worktree(worktree, LOCK_SH);
3991 if (unlockerr && err == NULL)
3992 err = unlockerr;
3993 return err;
3996 struct schedule_deletion_args {
3997 struct got_worktree *worktree;
3998 struct got_fileindex *fileindex;
3999 got_worktree_delete_cb progress_cb;
4000 void *progress_arg;
4001 struct got_repository *repo;
4002 int delete_local_mods;
4003 int keep_on_disk;
4004 int ignore_missing_paths;
4005 const char *status_codes;
4008 static const struct got_error *
4009 schedule_for_deletion(void *arg, unsigned char status,
4010 unsigned char staged_status, const char *relpath,
4011 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4012 struct got_object_id *commit_id, int dirfd, const char *de_name)
4014 struct schedule_deletion_args *a = arg;
4015 const struct got_error *err = NULL;
4016 struct got_fileindex_entry *ie = NULL;
4017 struct stat sb;
4018 char *ondisk_path;
4020 if (status == GOT_STATUS_NONEXISTENT) {
4021 if (a->ignore_missing_paths)
4022 return NULL;
4023 return got_error_set_errno(ENOENT, relpath);
4026 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4027 if (ie == NULL)
4028 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4030 staged_status = get_staged_status(ie);
4031 if (staged_status != GOT_STATUS_NO_CHANGE) {
4032 if (staged_status == GOT_STATUS_DELETE)
4033 return NULL;
4034 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4037 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4038 relpath) == -1)
4039 return got_error_from_errno("asprintf");
4041 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4042 a->repo);
4043 if (err)
4044 goto done;
4046 if (a->status_codes) {
4047 size_t ncodes = strlen(a->status_codes);
4048 int i;
4049 for (i = 0; i < ncodes ; i++) {
4050 if (status == a->status_codes[i])
4051 break;
4053 if (i == ncodes) {
4054 /* Do not delete files in non-matching status. */
4055 free(ondisk_path);
4056 return NULL;
4058 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4059 a->status_codes[i] != GOT_STATUS_MISSING) {
4060 static char msg[64];
4061 snprintf(msg, sizeof(msg),
4062 "invalid status code '%c'", a->status_codes[i]);
4063 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4064 goto done;
4068 if (status != GOT_STATUS_NO_CHANGE) {
4069 if (status == GOT_STATUS_DELETE)
4070 goto done;
4071 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4072 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4073 goto done;
4075 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4076 err = got_error_set_errno(ENOENT, relpath);
4077 goto done;
4079 if (status != GOT_STATUS_MODIFY &&
4080 status != GOT_STATUS_MISSING) {
4081 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4082 goto done;
4086 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4087 size_t root_len;
4089 if (dirfd != -1) {
4090 if (unlinkat(dirfd, de_name, 0) != 0) {
4091 err = got_error_from_errno2("unlinkat",
4092 ondisk_path);
4093 goto done;
4095 } else if (unlink(ondisk_path) != 0) {
4096 err = got_error_from_errno2("unlink", ondisk_path);
4097 goto done;
4100 root_len = strlen(a->worktree->root_path);
4101 do {
4102 char *parent;
4103 err = got_path_dirname(&parent, ondisk_path);
4104 if (err)
4105 goto done;
4106 free(ondisk_path);
4107 ondisk_path = parent;
4108 if (rmdir(ondisk_path) == -1) {
4109 if (errno != ENOTEMPTY)
4110 err = got_error_from_errno2("rmdir",
4111 ondisk_path);
4112 break;
4114 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4115 strlen(ondisk_path), root_len) != 0);
4118 got_fileindex_entry_mark_deleted_from_disk(ie);
4119 done:
4120 free(ondisk_path);
4121 if (err)
4122 return err;
4123 if (status == GOT_STATUS_DELETE)
4124 return NULL;
4125 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4126 staged_status, relpath);
4129 const struct got_error *
4130 got_worktree_schedule_delete(struct got_worktree *worktree,
4131 struct got_pathlist_head *paths, int delete_local_mods,
4132 const char *status_codes,
4133 got_worktree_delete_cb progress_cb, void *progress_arg,
4134 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4136 struct got_fileindex *fileindex = NULL;
4137 char *fileindex_path = NULL;
4138 const struct got_error *err = NULL, *sync_err, *unlockerr;
4139 struct got_pathlist_entry *pe;
4140 struct schedule_deletion_args sda;
4142 err = lock_worktree(worktree, LOCK_EX);
4143 if (err)
4144 return err;
4146 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4147 if (err)
4148 goto done;
4150 sda.worktree = worktree;
4151 sda.fileindex = fileindex;
4152 sda.progress_cb = progress_cb;
4153 sda.progress_arg = progress_arg;
4154 sda.repo = repo;
4155 sda.delete_local_mods = delete_local_mods;
4156 sda.keep_on_disk = keep_on_disk;
4157 sda.ignore_missing_paths = ignore_missing_paths;
4158 sda.status_codes = status_codes;
4160 TAILQ_FOREACH(pe, paths, entry) {
4161 err = worktree_status(worktree, pe->path, fileindex, repo,
4162 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4163 if (err)
4164 break;
4166 sync_err = sync_fileindex(fileindex, fileindex_path);
4167 if (sync_err && err == NULL)
4168 err = sync_err;
4169 done:
4170 free(fileindex_path);
4171 if (fileindex)
4172 got_fileindex_free(fileindex);
4173 unlockerr = lock_worktree(worktree, LOCK_SH);
4174 if (unlockerr && err == NULL)
4175 err = unlockerr;
4176 return err;
4179 static const struct got_error *
4180 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4182 const struct got_error *err = NULL;
4183 char *line = NULL;
4184 size_t linesize = 0, n;
4185 ssize_t linelen;
4187 linelen = getline(&line, &linesize, infile);
4188 if (linelen == -1) {
4189 if (ferror(infile)) {
4190 err = got_error_from_errno("getline");
4191 goto done;
4193 return NULL;
4195 if (outfile) {
4196 n = fwrite(line, 1, linelen, outfile);
4197 if (n != linelen) {
4198 err = got_ferror(outfile, GOT_ERR_IO);
4199 goto done;
4202 if (rejectfile) {
4203 n = fwrite(line, 1, linelen, rejectfile);
4204 if (n != linelen)
4205 err = got_ferror(outfile, GOT_ERR_IO);
4207 done:
4208 free(line);
4209 return err;
4212 static const struct got_error *
4213 skip_one_line(FILE *f)
4215 char *line = NULL;
4216 size_t linesize = 0;
4217 ssize_t linelen;
4219 linelen = getline(&line, &linesize, f);
4220 if (linelen == -1) {
4221 if (ferror(f))
4222 return got_error_from_errno("getline");
4223 return NULL;
4225 free(line);
4226 return NULL;
4229 static const struct got_error *
4230 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4231 int start_old, int end_old, int start_new, int end_new,
4232 FILE *outfile, FILE *rejectfile)
4234 const struct got_error *err;
4236 /* Copy old file's lines leading up to patch. */
4237 while (!feof(f1) && *line_cur1 < start_old) {
4238 err = copy_one_line(f1, outfile, NULL);
4239 if (err)
4240 return err;
4241 (*line_cur1)++;
4243 /* Skip new file's lines leading up to patch. */
4244 while (!feof(f2) && *line_cur2 < start_new) {
4245 if (rejectfile)
4246 err = copy_one_line(f2, NULL, rejectfile);
4247 else
4248 err = skip_one_line(f2);
4249 if (err)
4250 return err;
4251 (*line_cur2)++;
4253 /* Copy patched lines. */
4254 while (!feof(f2) && *line_cur2 <= end_new) {
4255 err = copy_one_line(f2, outfile, NULL);
4256 if (err)
4257 return err;
4258 (*line_cur2)++;
4260 /* Skip over old file's replaced lines. */
4261 while (!feof(f1) && *line_cur1 <= end_old) {
4262 if (rejectfile)
4263 err = copy_one_line(f1, NULL, rejectfile);
4264 else
4265 err = skip_one_line(f1);
4266 if (err)
4267 return err;
4268 (*line_cur1)++;
4271 return NULL;
4274 static const struct got_error *
4275 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4276 FILE *outfile, FILE *rejectfile)
4278 const struct got_error *err;
4280 if (outfile) {
4281 /* Copy old file's lines until EOF. */
4282 while (!feof(f1)) {
4283 err = copy_one_line(f1, outfile, NULL);
4284 if (err)
4285 return err;
4286 (*line_cur1)++;
4289 if (rejectfile) {
4290 /* Copy new file's lines until EOF. */
4291 while (!feof(f2)) {
4292 err = copy_one_line(f2, NULL, rejectfile);
4293 if (err)
4294 return err;
4295 (*line_cur2)++;
4299 return NULL;
4302 static const struct got_error *
4303 apply_or_reject_change(int *choice, int *nchunks_used,
4304 struct diff_result *diff_result, int n,
4305 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4306 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4307 got_worktree_patch_cb patch_cb, void *patch_arg)
4309 const struct got_error *err = NULL;
4310 struct diff_chunk_context cc = {};
4311 int start_old, end_old, start_new, end_new;
4312 FILE *hunkfile;
4313 struct diff_output_unidiff_state *diff_state;
4314 struct diff_input_info diff_info;
4315 int rc;
4317 *choice = GOT_PATCH_CHOICE_NONE;
4319 /* Get changed line numbers without context lines for copy_change(). */
4320 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4321 start_old = cc.left.start;
4322 end_old = cc.left.end;
4323 start_new = cc.right.start;
4324 end_new = cc.right.end;
4326 /* Get the same change with context lines for display. */
4327 memset(&cc, 0, sizeof(cc));
4328 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4330 memset(&diff_info, 0, sizeof(diff_info));
4331 diff_info.left_path = relpath;
4332 diff_info.right_path = relpath;
4334 diff_state = diff_output_unidiff_state_alloc();
4335 if (diff_state == NULL)
4336 return got_error_set_errno(ENOMEM,
4337 "diff_output_unidiff_state_alloc");
4339 hunkfile = got_opentemp();
4340 if (hunkfile == NULL) {
4341 err = got_error_from_errno("got_opentemp");
4342 goto done;
4345 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4346 diff_result, &cc);
4347 if (rc != DIFF_RC_OK) {
4348 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4349 goto done;
4352 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4353 err = got_ferror(hunkfile, GOT_ERR_IO);
4354 goto done;
4357 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4358 hunkfile, changeno, nchanges);
4359 if (err)
4360 goto done;
4362 switch (*choice) {
4363 case GOT_PATCH_CHOICE_YES:
4364 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4365 end_old, start_new, end_new, outfile, rejectfile);
4366 break;
4367 case GOT_PATCH_CHOICE_NO:
4368 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4369 end_old, start_new, end_new, rejectfile, outfile);
4370 break;
4371 case GOT_PATCH_CHOICE_QUIT:
4372 break;
4373 default:
4374 err = got_error(GOT_ERR_PATCH_CHOICE);
4375 break;
4377 done:
4378 diff_output_unidiff_state_free(diff_state);
4379 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4380 err = got_error_from_errno("fclose");
4381 return err;
4384 struct revert_file_args {
4385 struct got_worktree *worktree;
4386 struct got_fileindex *fileindex;
4387 got_worktree_checkout_cb progress_cb;
4388 void *progress_arg;
4389 got_worktree_patch_cb patch_cb;
4390 void *patch_arg;
4391 struct got_repository *repo;
4392 int unlink_added_files;
4395 static const struct got_error *
4396 create_patched_content(char **path_outfile, int reverse_patch,
4397 struct got_object_id *blob_id, const char *path2,
4398 int dirfd2, const char *de_name2,
4399 const char *relpath, struct got_repository *repo,
4400 got_worktree_patch_cb patch_cb, void *patch_arg)
4402 const struct got_error *err, *free_err;
4403 struct got_blob_object *blob = NULL;
4404 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4405 int fd2 = -1;
4406 char link_target[PATH_MAX];
4407 ssize_t link_len = 0;
4408 char *path1 = NULL, *id_str = NULL;
4409 struct stat sb2;
4410 struct got_diffreg_result *diffreg_result = NULL;
4411 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4412 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4414 *path_outfile = NULL;
4416 err = got_object_id_str(&id_str, blob_id);
4417 if (err)
4418 return err;
4420 if (dirfd2 != -1) {
4421 fd2 = openat(dirfd2, de_name2,
4422 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4423 if (fd2 == -1) {
4424 if (!got_err_open_nofollow_on_symlink()) {
4425 err = got_error_from_errno2("openat", path2);
4426 goto done;
4428 link_len = readlinkat(dirfd2, de_name2,
4429 link_target, sizeof(link_target));
4430 if (link_len == -1) {
4431 return got_error_from_errno2("readlinkat",
4432 path2);
4434 sb2.st_mode = S_IFLNK;
4435 sb2.st_size = link_len;
4437 } else {
4438 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4439 if (fd2 == -1) {
4440 if (!got_err_open_nofollow_on_symlink()) {
4441 err = got_error_from_errno2("open", path2);
4442 goto done;
4444 link_len = readlink(path2, link_target,
4445 sizeof(link_target));
4446 if (link_len == -1)
4447 return got_error_from_errno2("readlink", path2);
4448 sb2.st_mode = S_IFLNK;
4449 sb2.st_size = link_len;
4452 if (fd2 != -1) {
4453 if (fstat(fd2, &sb2) == -1) {
4454 err = got_error_from_errno2("fstat", path2);
4455 goto done;
4458 f2 = fdopen(fd2, "r");
4459 if (f2 == NULL) {
4460 err = got_error_from_errno2("fdopen", path2);
4461 goto done;
4463 fd2 = -1;
4464 } else {
4465 size_t n;
4466 f2 = got_opentemp();
4467 if (f2 == NULL) {
4468 err = got_error_from_errno2("got_opentemp", path2);
4469 goto done;
4471 n = fwrite(link_target, 1, link_len, f2);
4472 if (n != link_len) {
4473 err = got_ferror(f2, GOT_ERR_IO);
4474 goto done;
4476 if (fflush(f2) == EOF) {
4477 err = got_error_from_errno("fflush");
4478 goto done;
4480 rewind(f2);
4483 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4484 if (err)
4485 goto done;
4487 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4488 if (err)
4489 goto done;
4491 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4492 if (err)
4493 goto done;
4495 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4496 NULL);
4497 if (err)
4498 goto done;
4500 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4501 if (err)
4502 goto done;
4504 if (fseek(f1, 0L, SEEK_SET) == -1)
4505 return got_ferror(f1, GOT_ERR_IO);
4506 if (fseek(f2, 0L, SEEK_SET) == -1)
4507 return got_ferror(f2, GOT_ERR_IO);
4509 /* Count the number of actual changes in the diff result. */
4510 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4511 struct diff_chunk_context cc = {};
4512 diff_chunk_context_load_change(&cc, &nchunks_used,
4513 diffreg_result->result, n, 0);
4514 nchanges++;
4516 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4517 int choice;
4518 err = apply_or_reject_change(&choice, &nchunks_used,
4519 diffreg_result->result, n, relpath, f1, f2,
4520 &line_cur1, &line_cur2,
4521 reverse_patch ? NULL : outfile,
4522 reverse_patch ? outfile : NULL,
4523 ++i, nchanges, patch_cb, patch_arg);
4524 if (err)
4525 goto done;
4526 if (choice == GOT_PATCH_CHOICE_YES)
4527 have_content = 1;
4528 else if (choice == GOT_PATCH_CHOICE_QUIT)
4529 break;
4531 if (have_content) {
4532 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4533 reverse_patch ? NULL : outfile,
4534 reverse_patch ? outfile : NULL);
4535 if (err)
4536 goto done;
4538 if (!S_ISLNK(sb2.st_mode)) {
4539 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4540 err = got_error_from_errno2("fchmod", path2);
4541 goto done;
4545 done:
4546 free(id_str);
4547 if (blob)
4548 got_object_blob_close(blob);
4549 free_err = got_diffreg_result_free(diffreg_result);
4550 if (err == NULL)
4551 err = free_err;
4552 if (f1 && fclose(f1) == EOF && err == NULL)
4553 err = got_error_from_errno2("fclose", path1);
4554 if (f2 && fclose(f2) == EOF && err == NULL)
4555 err = got_error_from_errno2("fclose", path2);
4556 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4557 err = got_error_from_errno2("close", path2);
4558 if (outfile && fclose(outfile) == EOF && err == NULL)
4559 err = got_error_from_errno2("fclose", *path_outfile);
4560 if (path1 && unlink(path1) == -1 && err == NULL)
4561 err = got_error_from_errno2("unlink", path1);
4562 if (err || !have_content) {
4563 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4564 err = got_error_from_errno2("unlink", *path_outfile);
4565 free(*path_outfile);
4566 *path_outfile = NULL;
4568 free(path1);
4569 return err;
4572 static const struct got_error *
4573 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4574 const char *relpath, struct got_object_id *blob_id,
4575 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4576 int dirfd, const char *de_name)
4578 struct revert_file_args *a = arg;
4579 const struct got_error *err = NULL;
4580 char *parent_path = NULL;
4581 struct got_fileindex_entry *ie;
4582 struct got_commit_object *base_commit = NULL;
4583 struct got_tree_object *tree = NULL;
4584 struct got_object_id *tree_id = NULL;
4585 const struct got_tree_entry *te = NULL;
4586 char *tree_path = NULL, *te_name;
4587 char *ondisk_path = NULL, *path_content = NULL;
4588 struct got_blob_object *blob = NULL;
4590 /* Reverting a staged deletion is a no-op. */
4591 if (status == GOT_STATUS_DELETE &&
4592 staged_status != GOT_STATUS_NO_CHANGE)
4593 return NULL;
4595 if (status == GOT_STATUS_UNVERSIONED)
4596 return (*a->progress_cb)(a->progress_arg,
4597 GOT_STATUS_UNVERSIONED, relpath);
4599 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4600 if (ie == NULL)
4601 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4603 /* Construct in-repository path of tree which contains this blob. */
4604 err = got_path_dirname(&parent_path, ie->path);
4605 if (err) {
4606 if (err->code != GOT_ERR_BAD_PATH)
4607 goto done;
4608 parent_path = strdup("/");
4609 if (parent_path == NULL) {
4610 err = got_error_from_errno("strdup");
4611 goto done;
4614 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4615 tree_path = strdup(parent_path);
4616 if (tree_path == NULL) {
4617 err = got_error_from_errno("strdup");
4618 goto done;
4620 } else {
4621 if (got_path_is_root_dir(parent_path)) {
4622 tree_path = strdup(a->worktree->path_prefix);
4623 if (tree_path == NULL) {
4624 err = got_error_from_errno("strdup");
4625 goto done;
4627 } else {
4628 if (asprintf(&tree_path, "%s/%s",
4629 a->worktree->path_prefix, parent_path) == -1) {
4630 err = got_error_from_errno("asprintf");
4631 goto done;
4636 err = got_object_open_as_commit(&base_commit, a->repo,
4637 a->worktree->base_commit_id);
4638 if (err)
4639 goto done;
4641 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4642 if (err) {
4643 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4644 (status == GOT_STATUS_ADD ||
4645 staged_status == GOT_STATUS_ADD)))
4646 goto done;
4647 } else {
4648 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4649 if (err)
4650 goto done;
4652 err = got_path_basename(&te_name, ie->path);
4653 if (err)
4654 goto done;
4656 te = got_object_tree_find_entry(tree, te_name);
4657 free(te_name);
4658 if (te == NULL && status != GOT_STATUS_ADD &&
4659 staged_status != GOT_STATUS_ADD) {
4660 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4661 goto done;
4665 switch (status) {
4666 case GOT_STATUS_ADD:
4667 if (a->patch_cb) {
4668 int choice = GOT_PATCH_CHOICE_NONE;
4669 err = (*a->patch_cb)(&choice, a->patch_arg,
4670 status, ie->path, NULL, 1, 1);
4671 if (err)
4672 goto done;
4673 if (choice != GOT_PATCH_CHOICE_YES)
4674 break;
4676 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4677 ie->path);
4678 if (err)
4679 goto done;
4680 got_fileindex_entry_remove(a->fileindex, ie);
4681 if (a->unlink_added_files) {
4682 if (asprintf(&ondisk_path, "%s/%s",
4683 got_worktree_get_root_path(a->worktree),
4684 relpath) == -1) {
4685 err = got_error_from_errno("asprintf");
4686 goto done;
4688 if (unlink(ondisk_path) == -1) {
4689 err = got_error_from_errno2("unlink",
4690 ondisk_path);
4691 break;
4694 break;
4695 case GOT_STATUS_DELETE:
4696 if (a->patch_cb) {
4697 int choice = GOT_PATCH_CHOICE_NONE;
4698 err = (*a->patch_cb)(&choice, a->patch_arg,
4699 status, ie->path, NULL, 1, 1);
4700 if (err)
4701 goto done;
4702 if (choice != GOT_PATCH_CHOICE_YES)
4703 break;
4705 /* fall through */
4706 case GOT_STATUS_MODIFY:
4707 case GOT_STATUS_MODE_CHANGE:
4708 case GOT_STATUS_CONFLICT:
4709 case GOT_STATUS_MISSING: {
4710 struct got_object_id id;
4711 if (staged_status == GOT_STATUS_ADD ||
4712 staged_status == GOT_STATUS_MODIFY) {
4713 memcpy(id.sha1, ie->staged_blob_sha1,
4714 SHA1_DIGEST_LENGTH);
4715 } else
4716 memcpy(id.sha1, ie->blob_sha1,
4717 SHA1_DIGEST_LENGTH);
4718 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4719 if (err)
4720 goto done;
4722 if (asprintf(&ondisk_path, "%s/%s",
4723 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4724 err = got_error_from_errno("asprintf");
4725 goto done;
4728 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4729 status == GOT_STATUS_CONFLICT)) {
4730 int is_bad_symlink = 0;
4731 err = create_patched_content(&path_content, 1, &id,
4732 ondisk_path, dirfd, de_name, ie->path, a->repo,
4733 a->patch_cb, a->patch_arg);
4734 if (err || path_content == NULL)
4735 break;
4736 if (te && S_ISLNK(te->mode)) {
4737 if (unlink(path_content) == -1) {
4738 err = got_error_from_errno2("unlink",
4739 path_content);
4740 break;
4742 err = install_symlink(&is_bad_symlink,
4743 a->worktree, ondisk_path, ie->path,
4744 blob, 0, 1, 0, 0, a->repo,
4745 a->progress_cb, a->progress_arg);
4746 } else {
4747 if (rename(path_content, ondisk_path) == -1) {
4748 err = got_error_from_errno3("rename",
4749 path_content, ondisk_path);
4750 goto done;
4753 } else {
4754 int is_bad_symlink = 0;
4755 if (te && S_ISLNK(te->mode)) {
4756 err = install_symlink(&is_bad_symlink,
4757 a->worktree, ondisk_path, ie->path,
4758 blob, 0, 1, 0, 0, a->repo,
4759 a->progress_cb, a->progress_arg);
4760 } else {
4761 err = install_blob(a->worktree, ondisk_path,
4762 ie->path,
4763 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4764 got_fileindex_perms_to_st(ie), blob,
4765 0, 1, 0, 0, a->repo,
4766 a->progress_cb, a->progress_arg);
4768 if (err)
4769 goto done;
4770 if (status == GOT_STATUS_DELETE ||
4771 status == GOT_STATUS_MODE_CHANGE) {
4772 err = got_fileindex_entry_update(ie,
4773 a->worktree->root_fd, relpath,
4774 blob->id.sha1,
4775 a->worktree->base_commit_id->sha1, 1);
4776 if (err)
4777 goto done;
4779 if (is_bad_symlink) {
4780 got_fileindex_entry_filetype_set(ie,
4781 GOT_FILEIDX_MODE_BAD_SYMLINK);
4784 break;
4786 default:
4787 break;
4789 done:
4790 free(ondisk_path);
4791 free(path_content);
4792 free(parent_path);
4793 free(tree_path);
4794 if (blob)
4795 got_object_blob_close(blob);
4796 if (tree)
4797 got_object_tree_close(tree);
4798 free(tree_id);
4799 if (base_commit)
4800 got_object_commit_close(base_commit);
4801 return err;
4804 const struct got_error *
4805 got_worktree_revert(struct got_worktree *worktree,
4806 struct got_pathlist_head *paths,
4807 got_worktree_checkout_cb progress_cb, void *progress_arg,
4808 got_worktree_patch_cb patch_cb, void *patch_arg,
4809 struct got_repository *repo)
4811 struct got_fileindex *fileindex = NULL;
4812 char *fileindex_path = NULL;
4813 const struct got_error *err = NULL, *unlockerr = NULL;
4814 const struct got_error *sync_err = NULL;
4815 struct got_pathlist_entry *pe;
4816 struct revert_file_args rfa;
4818 err = lock_worktree(worktree, LOCK_EX);
4819 if (err)
4820 return err;
4822 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4823 if (err)
4824 goto done;
4826 rfa.worktree = worktree;
4827 rfa.fileindex = fileindex;
4828 rfa.progress_cb = progress_cb;
4829 rfa.progress_arg = progress_arg;
4830 rfa.patch_cb = patch_cb;
4831 rfa.patch_arg = patch_arg;
4832 rfa.repo = repo;
4833 rfa.unlink_added_files = 0;
4834 TAILQ_FOREACH(pe, paths, entry) {
4835 err = worktree_status(worktree, pe->path, fileindex, repo,
4836 revert_file, &rfa, NULL, NULL, 1, 0);
4837 if (err)
4838 break;
4840 sync_err = sync_fileindex(fileindex, fileindex_path);
4841 if (sync_err && err == NULL)
4842 err = sync_err;
4843 done:
4844 free(fileindex_path);
4845 if (fileindex)
4846 got_fileindex_free(fileindex);
4847 unlockerr = lock_worktree(worktree, LOCK_SH);
4848 if (unlockerr && err == NULL)
4849 err = unlockerr;
4850 return err;
4853 static void
4854 free_commitable(struct got_commitable *ct)
4856 free(ct->path);
4857 free(ct->in_repo_path);
4858 free(ct->ondisk_path);
4859 free(ct->blob_id);
4860 free(ct->base_blob_id);
4861 free(ct->staged_blob_id);
4862 free(ct->base_commit_id);
4863 free(ct);
4866 struct collect_commitables_arg {
4867 struct got_pathlist_head *commitable_paths;
4868 struct got_repository *repo;
4869 struct got_worktree *worktree;
4870 struct got_fileindex *fileindex;
4871 int have_staged_files;
4872 int allow_bad_symlinks;
4875 static const struct got_error *
4876 collect_commitables(void *arg, unsigned char status,
4877 unsigned char staged_status, const char *relpath,
4878 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4879 struct got_object_id *commit_id, int dirfd, const char *de_name)
4881 struct collect_commitables_arg *a = arg;
4882 const struct got_error *err = NULL;
4883 struct got_commitable *ct = NULL;
4884 struct got_pathlist_entry *new = NULL;
4885 char *parent_path = NULL, *path = NULL;
4886 struct stat sb;
4888 if (a->have_staged_files) {
4889 if (staged_status != GOT_STATUS_MODIFY &&
4890 staged_status != GOT_STATUS_ADD &&
4891 staged_status != GOT_STATUS_DELETE)
4892 return NULL;
4893 } else {
4894 if (status == GOT_STATUS_CONFLICT)
4895 return got_error(GOT_ERR_COMMIT_CONFLICT);
4897 if (status != GOT_STATUS_MODIFY &&
4898 status != GOT_STATUS_MODE_CHANGE &&
4899 status != GOT_STATUS_ADD &&
4900 status != GOT_STATUS_DELETE)
4901 return NULL;
4904 if (asprintf(&path, "/%s", relpath) == -1) {
4905 err = got_error_from_errno("asprintf");
4906 goto done;
4908 if (strcmp(path, "/") == 0) {
4909 parent_path = strdup("");
4910 if (parent_path == NULL)
4911 return got_error_from_errno("strdup");
4912 } else {
4913 err = got_path_dirname(&parent_path, path);
4914 if (err)
4915 return err;
4918 ct = calloc(1, sizeof(*ct));
4919 if (ct == NULL) {
4920 err = got_error_from_errno("calloc");
4921 goto done;
4924 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4925 relpath) == -1) {
4926 err = got_error_from_errno("asprintf");
4927 goto done;
4930 if (staged_status == GOT_STATUS_ADD ||
4931 staged_status == GOT_STATUS_MODIFY) {
4932 struct got_fileindex_entry *ie;
4933 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4934 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4935 case GOT_FILEIDX_MODE_REGULAR_FILE:
4936 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4937 ct->mode = S_IFREG;
4938 break;
4939 case GOT_FILEIDX_MODE_SYMLINK:
4940 ct->mode = S_IFLNK;
4941 break;
4942 default:
4943 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4944 goto done;
4946 ct->mode |= got_fileindex_entry_perms_get(ie);
4947 } else if (status != GOT_STATUS_DELETE &&
4948 staged_status != GOT_STATUS_DELETE) {
4949 if (dirfd != -1) {
4950 if (fstatat(dirfd, de_name, &sb,
4951 AT_SYMLINK_NOFOLLOW) == -1) {
4952 err = got_error_from_errno2("fstatat",
4953 ct->ondisk_path);
4954 goto done;
4956 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4957 err = got_error_from_errno2("lstat", ct->ondisk_path);
4958 goto done;
4960 ct->mode = sb.st_mode;
4963 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4964 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4965 relpath) == -1) {
4966 err = got_error_from_errno("asprintf");
4967 goto done;
4970 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4971 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4972 int is_bad_symlink;
4973 char target_path[PATH_MAX];
4974 ssize_t target_len;
4975 target_len = readlink(ct->ondisk_path, target_path,
4976 sizeof(target_path));
4977 if (target_len == -1) {
4978 err = got_error_from_errno2("readlink",
4979 ct->ondisk_path);
4980 goto done;
4982 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4983 target_len, ct->ondisk_path, a->worktree->root_path);
4984 if (err)
4985 goto done;
4986 if (is_bad_symlink) {
4987 err = got_error_path(ct->ondisk_path,
4988 GOT_ERR_BAD_SYMLINK);
4989 goto done;
4994 ct->status = status;
4995 ct->staged_status = staged_status;
4996 ct->blob_id = NULL; /* will be filled in when blob gets created */
4997 if (ct->status != GOT_STATUS_ADD &&
4998 ct->staged_status != GOT_STATUS_ADD) {
4999 ct->base_blob_id = got_object_id_dup(blob_id);
5000 if (ct->base_blob_id == NULL) {
5001 err = got_error_from_errno("got_object_id_dup");
5002 goto done;
5004 ct->base_commit_id = got_object_id_dup(commit_id);
5005 if (ct->base_commit_id == NULL) {
5006 err = got_error_from_errno("got_object_id_dup");
5007 goto done;
5010 if (ct->staged_status == GOT_STATUS_ADD ||
5011 ct->staged_status == GOT_STATUS_MODIFY) {
5012 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5013 if (ct->staged_blob_id == NULL) {
5014 err = got_error_from_errno("got_object_id_dup");
5015 goto done;
5018 ct->path = strdup(path);
5019 if (ct->path == NULL) {
5020 err = got_error_from_errno("strdup");
5021 goto done;
5023 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5024 done:
5025 if (ct && (err || new == NULL))
5026 free_commitable(ct);
5027 free(parent_path);
5028 free(path);
5029 return err;
5032 static const struct got_error *write_tree(struct got_object_id **, int *,
5033 struct got_tree_object *, const char *, struct got_pathlist_head *,
5034 got_worktree_status_cb status_cb, void *status_arg,
5035 struct got_repository *);
5037 static const struct got_error *
5038 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5039 struct got_tree_entry *te, const char *parent_path,
5040 struct got_pathlist_head *commitable_paths,
5041 got_worktree_status_cb status_cb, void *status_arg,
5042 struct got_repository *repo)
5044 const struct got_error *err = NULL;
5045 struct got_tree_object *subtree;
5046 char *subpath;
5048 if (asprintf(&subpath, "%s%s%s", parent_path,
5049 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5050 return got_error_from_errno("asprintf");
5052 err = got_object_open_as_tree(&subtree, repo, &te->id);
5053 if (err)
5054 return err;
5056 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5057 commitable_paths, status_cb, status_arg, repo);
5058 got_object_tree_close(subtree);
5059 free(subpath);
5060 return err;
5063 static const struct got_error *
5064 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5066 const struct got_error *err = NULL;
5067 char *ct_parent_path = NULL;
5069 *match = 0;
5071 if (strchr(ct->in_repo_path, '/') == NULL) {
5072 *match = got_path_is_root_dir(path);
5073 return NULL;
5076 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5077 if (err)
5078 return err;
5079 *match = (strcmp(path, ct_parent_path) == 0);
5080 free(ct_parent_path);
5081 return err;
5084 static mode_t
5085 get_ct_file_mode(struct got_commitable *ct)
5087 if (S_ISLNK(ct->mode))
5088 return S_IFLNK;
5090 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5093 static const struct got_error *
5094 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5095 struct got_tree_entry *te, struct got_commitable *ct)
5097 const struct got_error *err = NULL;
5099 *new_te = NULL;
5101 err = got_object_tree_entry_dup(new_te, te);
5102 if (err)
5103 goto done;
5105 (*new_te)->mode = get_ct_file_mode(ct);
5107 if (ct->staged_status == GOT_STATUS_MODIFY)
5108 memcpy(&(*new_te)->id, ct->staged_blob_id,
5109 sizeof((*new_te)->id));
5110 else
5111 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5112 done:
5113 if (err && *new_te) {
5114 free(*new_te);
5115 *new_te = NULL;
5117 return err;
5120 static const struct got_error *
5121 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5122 struct got_commitable *ct)
5124 const struct got_error *err = NULL;
5125 char *ct_name = NULL;
5127 *new_te = NULL;
5129 *new_te = calloc(1, sizeof(**new_te));
5130 if (*new_te == NULL)
5131 return got_error_from_errno("calloc");
5133 err = got_path_basename(&ct_name, ct->path);
5134 if (err)
5135 goto done;
5136 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5137 sizeof((*new_te)->name)) {
5138 err = got_error(GOT_ERR_NO_SPACE);
5139 goto done;
5142 (*new_te)->mode = get_ct_file_mode(ct);
5144 if (ct->staged_status == GOT_STATUS_ADD)
5145 memcpy(&(*new_te)->id, ct->staged_blob_id,
5146 sizeof((*new_te)->id));
5147 else
5148 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5149 done:
5150 free(ct_name);
5151 if (err && *new_te) {
5152 free(*new_te);
5153 *new_te = NULL;
5155 return err;
5158 static const struct got_error *
5159 insert_tree_entry(struct got_tree_entry *new_te,
5160 struct got_pathlist_head *paths)
5162 const struct got_error *err = NULL;
5163 struct got_pathlist_entry *new_pe;
5165 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5166 if (err)
5167 return err;
5168 if (new_pe == NULL)
5169 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5170 return NULL;
5173 static const struct got_error *
5174 report_ct_status(struct got_commitable *ct,
5175 got_worktree_status_cb status_cb, void *status_arg)
5177 const char *ct_path = ct->path;
5178 unsigned char status;
5180 if (status_cb == NULL) /* no commit progress output desired */
5181 return NULL;
5183 while (ct_path[0] == '/')
5184 ct_path++;
5186 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5187 status = ct->staged_status;
5188 else
5189 status = ct->status;
5191 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5192 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5195 static const struct got_error *
5196 match_modified_subtree(int *modified, struct got_tree_entry *te,
5197 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5199 const struct got_error *err = NULL;
5200 struct got_pathlist_entry *pe;
5201 char *te_path;
5203 *modified = 0;
5205 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5206 got_path_is_root_dir(base_tree_path) ? "" : "/",
5207 te->name) == -1)
5208 return got_error_from_errno("asprintf");
5210 TAILQ_FOREACH(pe, commitable_paths, entry) {
5211 struct got_commitable *ct = pe->data;
5212 *modified = got_path_is_child(ct->in_repo_path, te_path,
5213 strlen(te_path));
5214 if (*modified)
5215 break;
5218 free(te_path);
5219 return err;
5222 static const struct got_error *
5223 match_deleted_or_modified_ct(struct got_commitable **ctp,
5224 struct got_tree_entry *te, const char *base_tree_path,
5225 struct got_pathlist_head *commitable_paths)
5227 const struct got_error *err = NULL;
5228 struct got_pathlist_entry *pe;
5230 *ctp = NULL;
5232 TAILQ_FOREACH(pe, commitable_paths, entry) {
5233 struct got_commitable *ct = pe->data;
5234 char *ct_name = NULL;
5235 int path_matches;
5237 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5238 if (ct->status != GOT_STATUS_MODIFY &&
5239 ct->status != GOT_STATUS_MODE_CHANGE &&
5240 ct->status != GOT_STATUS_DELETE)
5241 continue;
5242 } else {
5243 if (ct->staged_status != GOT_STATUS_MODIFY &&
5244 ct->staged_status != GOT_STATUS_DELETE)
5245 continue;
5248 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5249 continue;
5251 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5252 if (err)
5253 return err;
5254 if (!path_matches)
5255 continue;
5257 err = got_path_basename(&ct_name, pe->path);
5258 if (err)
5259 return err;
5261 if (strcmp(te->name, ct_name) != 0) {
5262 free(ct_name);
5263 continue;
5265 free(ct_name);
5267 *ctp = ct;
5268 break;
5271 return err;
5274 static const struct got_error *
5275 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5276 const char *child_path, const char *path_base_tree,
5277 struct got_pathlist_head *commitable_paths,
5278 got_worktree_status_cb status_cb, void *status_arg,
5279 struct got_repository *repo)
5281 const struct got_error *err = NULL;
5282 struct got_tree_entry *new_te;
5283 char *subtree_path;
5284 struct got_object_id *id = NULL;
5285 int nentries;
5287 *new_tep = NULL;
5289 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5290 got_path_is_root_dir(path_base_tree) ? "" : "/",
5291 child_path) == -1)
5292 return got_error_from_errno("asprintf");
5294 new_te = calloc(1, sizeof(*new_te));
5295 if (new_te == NULL)
5296 return got_error_from_errno("calloc");
5297 new_te->mode = S_IFDIR;
5299 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5300 sizeof(new_te->name)) {
5301 err = got_error(GOT_ERR_NO_SPACE);
5302 goto done;
5304 err = write_tree(&id, &nentries, NULL, subtree_path,
5305 commitable_paths, status_cb, status_arg, repo);
5306 if (err) {
5307 free(new_te);
5308 goto done;
5310 memcpy(&new_te->id, id, sizeof(new_te->id));
5311 done:
5312 free(id);
5313 free(subtree_path);
5314 if (err == NULL)
5315 *new_tep = new_te;
5316 return err;
5319 static const struct got_error *
5320 write_tree(struct got_object_id **new_tree_id, int *nentries,
5321 struct got_tree_object *base_tree, const char *path_base_tree,
5322 struct got_pathlist_head *commitable_paths,
5323 got_worktree_status_cb status_cb, void *status_arg,
5324 struct got_repository *repo)
5326 const struct got_error *err = NULL;
5327 struct got_pathlist_head paths;
5328 struct got_tree_entry *te, *new_te = NULL;
5329 struct got_pathlist_entry *pe;
5331 TAILQ_INIT(&paths);
5332 *nentries = 0;
5334 /* Insert, and recurse into, newly added entries first. */
5335 TAILQ_FOREACH(pe, commitable_paths, entry) {
5336 struct got_commitable *ct = pe->data;
5337 char *child_path = NULL, *slash;
5339 if ((ct->status != GOT_STATUS_ADD &&
5340 ct->staged_status != GOT_STATUS_ADD) ||
5341 (ct->flags & GOT_COMMITABLE_ADDED))
5342 continue;
5344 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5345 strlen(path_base_tree)))
5346 continue;
5348 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5349 ct->in_repo_path);
5350 if (err)
5351 goto done;
5353 slash = strchr(child_path, '/');
5354 if (slash == NULL) {
5355 err = alloc_added_blob_tree_entry(&new_te, ct);
5356 if (err)
5357 goto done;
5358 err = report_ct_status(ct, status_cb, status_arg);
5359 if (err)
5360 goto done;
5361 ct->flags |= GOT_COMMITABLE_ADDED;
5362 err = insert_tree_entry(new_te, &paths);
5363 if (err)
5364 goto done;
5365 (*nentries)++;
5366 } else {
5367 *slash = '\0'; /* trim trailing path components */
5368 if (base_tree == NULL ||
5369 got_object_tree_find_entry(base_tree, child_path)
5370 == NULL) {
5371 err = make_subtree_for_added_blob(&new_te,
5372 child_path, path_base_tree,
5373 commitable_paths, status_cb, status_arg,
5374 repo);
5375 if (err)
5376 goto done;
5377 err = insert_tree_entry(new_te, &paths);
5378 if (err)
5379 goto done;
5380 (*nentries)++;
5385 if (base_tree) {
5386 int i, nbase_entries;
5387 /* Handle modified and deleted entries. */
5388 nbase_entries = got_object_tree_get_nentries(base_tree);
5389 for (i = 0; i < nbase_entries; i++) {
5390 struct got_commitable *ct = NULL;
5392 te = got_object_tree_get_entry(base_tree, i);
5393 if (got_object_tree_entry_is_submodule(te)) {
5394 /* Entry is a submodule; just copy it. */
5395 err = got_object_tree_entry_dup(&new_te, te);
5396 if (err)
5397 goto done;
5398 err = insert_tree_entry(new_te, &paths);
5399 if (err)
5400 goto done;
5401 (*nentries)++;
5402 continue;
5405 if (S_ISDIR(te->mode)) {
5406 int modified;
5407 err = got_object_tree_entry_dup(&new_te, te);
5408 if (err)
5409 goto done;
5410 err = match_modified_subtree(&modified, te,
5411 path_base_tree, commitable_paths);
5412 if (err)
5413 goto done;
5414 /* Avoid recursion into unmodified subtrees. */
5415 if (modified) {
5416 struct got_object_id *new_id;
5417 int nsubentries;
5418 err = write_subtree(&new_id,
5419 &nsubentries, te,
5420 path_base_tree, commitable_paths,
5421 status_cb, status_arg, repo);
5422 if (err)
5423 goto done;
5424 if (nsubentries == 0) {
5425 /* All entries were deleted. */
5426 free(new_id);
5427 continue;
5429 memcpy(&new_te->id, new_id,
5430 sizeof(new_te->id));
5431 free(new_id);
5433 err = insert_tree_entry(new_te, &paths);
5434 if (err)
5435 goto done;
5436 (*nentries)++;
5437 continue;
5440 err = match_deleted_or_modified_ct(&ct, te,
5441 path_base_tree, commitable_paths);
5442 if (err)
5443 goto done;
5444 if (ct) {
5445 /* NB: Deleted entries get dropped here. */
5446 if (ct->status == GOT_STATUS_MODIFY ||
5447 ct->status == GOT_STATUS_MODE_CHANGE ||
5448 ct->staged_status == GOT_STATUS_MODIFY) {
5449 err = alloc_modified_blob_tree_entry(
5450 &new_te, te, ct);
5451 if (err)
5452 goto done;
5453 err = insert_tree_entry(new_te, &paths);
5454 if (err)
5455 goto done;
5456 (*nentries)++;
5458 err = report_ct_status(ct, status_cb,
5459 status_arg);
5460 if (err)
5461 goto done;
5462 } else {
5463 /* Entry is unchanged; just copy it. */
5464 err = got_object_tree_entry_dup(&new_te, te);
5465 if (err)
5466 goto done;
5467 err = insert_tree_entry(new_te, &paths);
5468 if (err)
5469 goto done;
5470 (*nentries)++;
5475 /* Write new list of entries; deleted entries have been dropped. */
5476 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5477 done:
5478 got_pathlist_free(&paths);
5479 return err;
5482 static const struct got_error *
5483 update_fileindex_after_commit(struct got_worktree *worktree,
5484 struct got_pathlist_head *commitable_paths,
5485 struct got_object_id *new_base_commit_id,
5486 struct got_fileindex *fileindex, int have_staged_files)
5488 const struct got_error *err = NULL;
5489 struct got_pathlist_entry *pe;
5490 char *relpath = NULL;
5492 TAILQ_FOREACH(pe, commitable_paths, entry) {
5493 struct got_fileindex_entry *ie;
5494 struct got_commitable *ct = pe->data;
5496 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5498 err = got_path_skip_common_ancestor(&relpath,
5499 worktree->root_path, ct->ondisk_path);
5500 if (err)
5501 goto done;
5503 if (ie) {
5504 if (ct->status == GOT_STATUS_DELETE ||
5505 ct->staged_status == GOT_STATUS_DELETE) {
5506 got_fileindex_entry_remove(fileindex, ie);
5507 } else if (ct->staged_status == GOT_STATUS_ADD ||
5508 ct->staged_status == GOT_STATUS_MODIFY) {
5509 got_fileindex_entry_stage_set(ie,
5510 GOT_FILEIDX_STAGE_NONE);
5511 got_fileindex_entry_staged_filetype_set(ie, 0);
5513 err = got_fileindex_entry_update(ie,
5514 worktree->root_fd, relpath,
5515 ct->staged_blob_id->sha1,
5516 new_base_commit_id->sha1,
5517 !have_staged_files);
5518 } else
5519 err = got_fileindex_entry_update(ie,
5520 worktree->root_fd, relpath,
5521 ct->blob_id->sha1,
5522 new_base_commit_id->sha1,
5523 !have_staged_files);
5524 } else {
5525 err = got_fileindex_entry_alloc(&ie, pe->path);
5526 if (err)
5527 goto done;
5528 err = got_fileindex_entry_update(ie,
5529 worktree->root_fd, relpath, ct->blob_id->sha1,
5530 new_base_commit_id->sha1, 1);
5531 if (err) {
5532 got_fileindex_entry_free(ie);
5533 goto done;
5535 err = got_fileindex_entry_add(fileindex, ie);
5536 if (err) {
5537 got_fileindex_entry_free(ie);
5538 goto done;
5541 free(relpath);
5542 relpath = NULL;
5544 done:
5545 free(relpath);
5546 return err;
5550 static const struct got_error *
5551 check_out_of_date(const char *in_repo_path, unsigned char status,
5552 unsigned char staged_status, struct got_object_id *base_blob_id,
5553 struct got_object_id *base_commit_id,
5554 struct got_object_id *head_commit_id, struct got_repository *repo,
5555 int ood_errcode)
5557 const struct got_error *err = NULL;
5558 struct got_commit_object *commit = NULL;
5559 struct got_object_id *id = NULL;
5561 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5562 /* Trivial case: base commit == head commit */
5563 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5564 return NULL;
5566 * Ensure file content which local changes were based
5567 * on matches file content in the branch head.
5569 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5570 if (err)
5571 goto done;
5572 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5573 if (err) {
5574 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5575 err = got_error(ood_errcode);
5576 goto done;
5577 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5578 err = got_error(ood_errcode);
5579 } else {
5580 /* Require that added files don't exist in the branch head. */
5581 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5582 if (err)
5583 goto done;
5584 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5585 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5586 goto done;
5587 err = id ? got_error(ood_errcode) : NULL;
5589 done:
5590 free(id);
5591 if (commit)
5592 got_object_commit_close(commit);
5593 return err;
5596 const struct got_error *
5597 commit_worktree(struct got_object_id **new_commit_id,
5598 struct got_pathlist_head *commitable_paths,
5599 struct got_object_id *head_commit_id,
5600 struct got_object_id *parent_id2,
5601 struct got_worktree *worktree,
5602 const char *author, const char *committer,
5603 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5604 got_worktree_status_cb status_cb, void *status_arg,
5605 struct got_repository *repo)
5607 const struct got_error *err = NULL, *unlockerr = NULL;
5608 struct got_pathlist_entry *pe;
5609 const char *head_ref_name = NULL;
5610 struct got_commit_object *head_commit = NULL;
5611 struct got_reference *head_ref2 = NULL;
5612 struct got_object_id *head_commit_id2 = NULL;
5613 struct got_tree_object *head_tree = NULL;
5614 struct got_object_id *new_tree_id = NULL;
5615 int nentries, nparents = 0;
5616 struct got_object_id_queue parent_ids;
5617 struct got_object_qid *pid = NULL;
5618 char *logmsg = NULL;
5620 *new_commit_id = NULL;
5622 STAILQ_INIT(&parent_ids);
5624 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5625 if (err)
5626 goto done;
5628 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5629 if (err)
5630 goto done;
5632 if (commit_msg_cb != NULL) {
5633 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5634 if (err)
5635 goto done;
5638 if (logmsg == NULL || strlen(logmsg) == 0) {
5639 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5640 goto done;
5643 /* Create blobs from added and modified files and record their IDs. */
5644 TAILQ_FOREACH(pe, commitable_paths, entry) {
5645 struct got_commitable *ct = pe->data;
5646 char *ondisk_path;
5648 /* Blobs for staged files already exist. */
5649 if (ct->staged_status == GOT_STATUS_ADD ||
5650 ct->staged_status == GOT_STATUS_MODIFY)
5651 continue;
5653 if (ct->status != GOT_STATUS_ADD &&
5654 ct->status != GOT_STATUS_MODIFY &&
5655 ct->status != GOT_STATUS_MODE_CHANGE)
5656 continue;
5658 if (asprintf(&ondisk_path, "%s/%s",
5659 worktree->root_path, pe->path) == -1) {
5660 err = got_error_from_errno("asprintf");
5661 goto done;
5663 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5664 free(ondisk_path);
5665 if (err)
5666 goto done;
5669 /* Recursively write new tree objects. */
5670 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5671 commitable_paths, status_cb, status_arg, repo);
5672 if (err)
5673 goto done;
5675 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5676 if (err)
5677 goto done;
5678 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5679 nparents++;
5680 if (parent_id2) {
5681 err = got_object_qid_alloc(&pid, parent_id2);
5682 if (err)
5683 goto done;
5684 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5685 nparents++;
5687 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5688 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5689 if (logmsg != NULL)
5690 free(logmsg);
5691 if (err)
5692 goto done;
5694 /* Check if a concurrent commit to our branch has occurred. */
5695 head_ref_name = got_worktree_get_head_ref_name(worktree);
5696 if (head_ref_name == NULL) {
5697 err = got_error_from_errno("got_worktree_get_head_ref_name");
5698 goto done;
5700 /* Lock the reference here to prevent concurrent modification. */
5701 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5702 if (err)
5703 goto done;
5704 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5705 if (err)
5706 goto done;
5707 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5708 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5709 goto done;
5711 /* Update branch head in repository. */
5712 err = got_ref_change_ref(head_ref2, *new_commit_id);
5713 if (err)
5714 goto done;
5715 err = got_ref_write(head_ref2, repo);
5716 if (err)
5717 goto done;
5719 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5720 if (err)
5721 goto done;
5723 err = ref_base_commit(worktree, repo);
5724 if (err)
5725 goto done;
5726 done:
5727 got_object_id_queue_free(&parent_ids);
5728 if (head_tree)
5729 got_object_tree_close(head_tree);
5730 if (head_commit)
5731 got_object_commit_close(head_commit);
5732 free(head_commit_id2);
5733 if (head_ref2) {
5734 unlockerr = got_ref_unlock(head_ref2);
5735 if (unlockerr && err == NULL)
5736 err = unlockerr;
5737 got_ref_close(head_ref2);
5739 return err;
5742 static const struct got_error *
5743 check_path_is_commitable(const char *path,
5744 struct got_pathlist_head *commitable_paths)
5746 struct got_pathlist_entry *cpe = NULL;
5747 size_t path_len = strlen(path);
5749 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5750 struct got_commitable *ct = cpe->data;
5751 const char *ct_path = ct->path;
5753 while (ct_path[0] == '/')
5754 ct_path++;
5756 if (strcmp(path, ct_path) == 0 ||
5757 got_path_is_child(ct_path, path, path_len))
5758 break;
5761 if (cpe == NULL)
5762 return got_error_path(path, GOT_ERR_BAD_PATH);
5764 return NULL;
5767 static const struct got_error *
5768 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5770 int *have_staged_files = arg;
5772 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5773 *have_staged_files = 1;
5774 return got_error(GOT_ERR_CANCELLED);
5777 return NULL;
5780 static const struct got_error *
5781 check_non_staged_files(struct got_fileindex *fileindex,
5782 struct got_pathlist_head *paths)
5784 struct got_pathlist_entry *pe;
5785 struct got_fileindex_entry *ie;
5787 TAILQ_FOREACH(pe, paths, entry) {
5788 if (pe->path[0] == '\0')
5789 continue;
5790 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5791 if (ie == NULL)
5792 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5793 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5794 return got_error_path(pe->path,
5795 GOT_ERR_FILE_NOT_STAGED);
5798 return NULL;
5801 const struct got_error *
5802 got_worktree_commit(struct got_object_id **new_commit_id,
5803 struct got_worktree *worktree, struct got_pathlist_head *paths,
5804 const char *author, const char *committer, int allow_bad_symlinks,
5805 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5806 got_worktree_status_cb status_cb, void *status_arg,
5807 struct got_repository *repo)
5809 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5810 struct got_fileindex *fileindex = NULL;
5811 char *fileindex_path = NULL;
5812 struct got_pathlist_head commitable_paths;
5813 struct collect_commitables_arg cc_arg;
5814 struct got_pathlist_entry *pe;
5815 struct got_reference *head_ref = NULL;
5816 struct got_object_id *head_commit_id = NULL;
5817 int have_staged_files = 0;
5819 *new_commit_id = NULL;
5821 TAILQ_INIT(&commitable_paths);
5823 err = lock_worktree(worktree, LOCK_EX);
5824 if (err)
5825 goto done;
5827 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5828 if (err)
5829 goto done;
5831 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5832 if (err)
5833 goto done;
5835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5836 if (err)
5837 goto done;
5839 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5840 &have_staged_files);
5841 if (err && err->code != GOT_ERR_CANCELLED)
5842 goto done;
5843 if (have_staged_files) {
5844 err = check_non_staged_files(fileindex, paths);
5845 if (err)
5846 goto done;
5849 cc_arg.commitable_paths = &commitable_paths;
5850 cc_arg.worktree = worktree;
5851 cc_arg.fileindex = fileindex;
5852 cc_arg.repo = repo;
5853 cc_arg.have_staged_files = have_staged_files;
5854 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5855 TAILQ_FOREACH(pe, paths, entry) {
5856 err = worktree_status(worktree, pe->path, fileindex, repo,
5857 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5858 if (err)
5859 goto done;
5862 if (TAILQ_EMPTY(&commitable_paths)) {
5863 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5864 goto done;
5867 TAILQ_FOREACH(pe, paths, entry) {
5868 err = check_path_is_commitable(pe->path, &commitable_paths);
5869 if (err)
5870 goto done;
5873 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5874 struct got_commitable *ct = pe->data;
5875 const char *ct_path = ct->in_repo_path;
5877 while (ct_path[0] == '/')
5878 ct_path++;
5879 err = check_out_of_date(ct_path, ct->status,
5880 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5881 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5882 if (err)
5883 goto done;
5887 err = commit_worktree(new_commit_id, &commitable_paths,
5888 head_commit_id, NULL, worktree, author, committer,
5889 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5890 if (err)
5891 goto done;
5893 err = update_fileindex_after_commit(worktree, &commitable_paths,
5894 *new_commit_id, fileindex, have_staged_files);
5895 sync_err = sync_fileindex(fileindex, fileindex_path);
5896 if (sync_err && err == NULL)
5897 err = sync_err;
5898 done:
5899 if (fileindex)
5900 got_fileindex_free(fileindex);
5901 free(fileindex_path);
5902 unlockerr = lock_worktree(worktree, LOCK_SH);
5903 if (unlockerr && err == NULL)
5904 err = unlockerr;
5905 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5906 struct got_commitable *ct = pe->data;
5907 free_commitable(ct);
5909 got_pathlist_free(&commitable_paths);
5910 return err;
5913 const char *
5914 got_commitable_get_path(struct got_commitable *ct)
5916 return ct->path;
5919 unsigned int
5920 got_commitable_get_status(struct got_commitable *ct)
5922 return ct->status;
5925 struct check_rebase_ok_arg {
5926 struct got_worktree *worktree;
5927 struct got_repository *repo;
5930 static const struct got_error *
5931 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5933 const struct got_error *err = NULL;
5934 struct check_rebase_ok_arg *a = arg;
5935 unsigned char status;
5936 struct stat sb;
5937 char *ondisk_path;
5939 /* Reject rebase of a work tree with mixed base commits. */
5940 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5941 SHA1_DIGEST_LENGTH))
5942 return got_error(GOT_ERR_MIXED_COMMITS);
5944 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5945 == -1)
5946 return got_error_from_errno("asprintf");
5948 /* Reject rebase of a work tree with modified or staged files. */
5949 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5950 free(ondisk_path);
5951 if (err)
5952 return err;
5954 if (status != GOT_STATUS_NO_CHANGE)
5955 return got_error(GOT_ERR_MODIFIED);
5956 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5957 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5959 return NULL;
5962 const struct got_error *
5963 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5964 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5965 struct got_worktree *worktree, struct got_reference *branch,
5966 struct got_repository *repo)
5968 const struct got_error *err = NULL;
5969 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5970 char *branch_ref_name = NULL;
5971 char *fileindex_path = NULL;
5972 struct check_rebase_ok_arg ok_arg;
5973 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5974 struct got_object_id *wt_branch_tip = NULL;
5976 *new_base_branch_ref = NULL;
5977 *tmp_branch = NULL;
5978 *fileindex = NULL;
5980 err = lock_worktree(worktree, LOCK_EX);
5981 if (err)
5982 return err;
5984 err = open_fileindex(fileindex, &fileindex_path, worktree);
5985 if (err)
5986 goto done;
5988 ok_arg.worktree = worktree;
5989 ok_arg.repo = repo;
5990 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5991 &ok_arg);
5992 if (err)
5993 goto done;
5995 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5996 if (err)
5997 goto done;
5999 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6000 if (err)
6001 goto done;
6003 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6004 if (err)
6005 goto done;
6007 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6008 0);
6009 if (err)
6010 goto done;
6012 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6013 if (err)
6014 goto done;
6015 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6016 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6017 goto done;
6020 err = got_ref_alloc_symref(new_base_branch_ref,
6021 new_base_branch_ref_name, wt_branch);
6022 if (err)
6023 goto done;
6024 err = got_ref_write(*new_base_branch_ref, repo);
6025 if (err)
6026 goto done;
6028 /* TODO Lock original branch's ref while rebasing? */
6030 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6031 if (err)
6032 goto done;
6034 err = got_ref_write(branch_ref, repo);
6035 if (err)
6036 goto done;
6038 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6039 worktree->base_commit_id);
6040 if (err)
6041 goto done;
6042 err = got_ref_write(*tmp_branch, repo);
6043 if (err)
6044 goto done;
6046 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6047 if (err)
6048 goto done;
6049 done:
6050 free(fileindex_path);
6051 free(tmp_branch_name);
6052 free(new_base_branch_ref_name);
6053 free(branch_ref_name);
6054 if (branch_ref)
6055 got_ref_close(branch_ref);
6056 if (wt_branch)
6057 got_ref_close(wt_branch);
6058 free(wt_branch_tip);
6059 if (err) {
6060 if (*new_base_branch_ref) {
6061 got_ref_close(*new_base_branch_ref);
6062 *new_base_branch_ref = NULL;
6064 if (*tmp_branch) {
6065 got_ref_close(*tmp_branch);
6066 *tmp_branch = NULL;
6068 if (*fileindex) {
6069 got_fileindex_free(*fileindex);
6070 *fileindex = NULL;
6072 lock_worktree(worktree, LOCK_SH);
6074 return err;
6077 const struct got_error *
6078 got_worktree_rebase_continue(struct got_object_id **commit_id,
6079 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6080 struct got_reference **branch, struct got_fileindex **fileindex,
6081 struct got_worktree *worktree, struct got_repository *repo)
6083 const struct got_error *err;
6084 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6085 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6086 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6087 char *fileindex_path = NULL;
6088 int have_staged_files = 0;
6090 *commit_id = NULL;
6091 *new_base_branch = NULL;
6092 *tmp_branch = NULL;
6093 *branch = NULL;
6094 *fileindex = NULL;
6096 err = lock_worktree(worktree, LOCK_EX);
6097 if (err)
6098 return err;
6100 err = open_fileindex(fileindex, &fileindex_path, worktree);
6101 if (err)
6102 goto done;
6104 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6105 &have_staged_files);
6106 if (err && err->code != GOT_ERR_CANCELLED)
6107 goto done;
6108 if (have_staged_files) {
6109 err = got_error(GOT_ERR_STAGED_PATHS);
6110 goto done;
6113 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6114 if (err)
6115 goto done;
6117 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6118 if (err)
6119 goto done;
6121 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6122 if (err)
6123 goto done;
6125 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6126 if (err)
6127 goto done;
6129 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6130 if (err)
6131 goto done;
6133 err = got_ref_open(branch, repo,
6134 got_ref_get_symref_target(branch_ref), 0);
6135 if (err)
6136 goto done;
6138 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6139 if (err)
6140 goto done;
6142 err = got_ref_resolve(commit_id, repo, commit_ref);
6143 if (err)
6144 goto done;
6146 err = got_ref_open(new_base_branch, repo,
6147 new_base_branch_ref_name, 0);
6148 if (err)
6149 goto done;
6151 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6152 if (err)
6153 goto done;
6154 done:
6155 free(commit_ref_name);
6156 free(branch_ref_name);
6157 free(fileindex_path);
6158 if (commit_ref)
6159 got_ref_close(commit_ref);
6160 if (branch_ref)
6161 got_ref_close(branch_ref);
6162 if (err) {
6163 free(*commit_id);
6164 *commit_id = NULL;
6165 if (*tmp_branch) {
6166 got_ref_close(*tmp_branch);
6167 *tmp_branch = NULL;
6169 if (*new_base_branch) {
6170 got_ref_close(*new_base_branch);
6171 *new_base_branch = NULL;
6173 if (*branch) {
6174 got_ref_close(*branch);
6175 *branch = NULL;
6177 if (*fileindex) {
6178 got_fileindex_free(*fileindex);
6179 *fileindex = NULL;
6181 lock_worktree(worktree, LOCK_SH);
6183 return err;
6186 const struct got_error *
6187 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6189 const struct got_error *err;
6190 char *tmp_branch_name = NULL;
6192 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6193 if (err)
6194 return err;
6196 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6197 free(tmp_branch_name);
6198 return NULL;
6201 static const struct got_error *
6202 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6203 char **logmsg, void *arg)
6205 *logmsg = arg;
6206 return NULL;
6209 static const struct got_error *
6210 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6211 const char *path, struct got_object_id *blob_id,
6212 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6213 int dirfd, const char *de_name)
6215 return NULL;
6218 struct collect_merged_paths_arg {
6219 got_worktree_checkout_cb progress_cb;
6220 void *progress_arg;
6221 struct got_pathlist_head *merged_paths;
6224 static const struct got_error *
6225 collect_merged_paths(void *arg, unsigned char status, const char *path)
6227 const struct got_error *err;
6228 struct collect_merged_paths_arg *a = arg;
6229 char *p;
6230 struct got_pathlist_entry *new;
6232 err = (*a->progress_cb)(a->progress_arg, status, path);
6233 if (err)
6234 return err;
6236 if (status != GOT_STATUS_MERGE &&
6237 status != GOT_STATUS_ADD &&
6238 status != GOT_STATUS_DELETE &&
6239 status != GOT_STATUS_CONFLICT)
6240 return NULL;
6242 p = strdup(path);
6243 if (p == NULL)
6244 return got_error_from_errno("strdup");
6246 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6247 if (err || new == NULL)
6248 free(p);
6249 return err;
6252 void
6253 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6255 struct got_pathlist_entry *pe;
6257 TAILQ_FOREACH(pe, merged_paths, entry)
6258 free((char *)pe->path);
6260 got_pathlist_free(merged_paths);
6263 static const struct got_error *
6264 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6265 int is_rebase, struct got_repository *repo)
6267 const struct got_error *err;
6268 struct got_reference *commit_ref = NULL;
6270 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6271 if (err) {
6272 if (err->code != GOT_ERR_NOT_REF)
6273 goto done;
6274 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6275 if (err)
6276 goto done;
6277 err = got_ref_write(commit_ref, repo);
6278 if (err)
6279 goto done;
6280 } else if (is_rebase) {
6281 struct got_object_id *stored_id;
6282 int cmp;
6284 err = got_ref_resolve(&stored_id, repo, commit_ref);
6285 if (err)
6286 goto done;
6287 cmp = got_object_id_cmp(commit_id, stored_id);
6288 free(stored_id);
6289 if (cmp != 0) {
6290 err = got_error(GOT_ERR_REBASE_COMMITID);
6291 goto done;
6294 done:
6295 if (commit_ref)
6296 got_ref_close(commit_ref);
6297 return err;
6300 static const struct got_error *
6301 rebase_merge_files(struct got_pathlist_head *merged_paths,
6302 const char *commit_ref_name, struct got_worktree *worktree,
6303 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6304 struct got_object_id *commit_id, struct got_repository *repo,
6305 got_worktree_checkout_cb progress_cb, void *progress_arg,
6306 got_cancel_cb cancel_cb, void *cancel_arg)
6308 const struct got_error *err;
6309 struct got_reference *commit_ref = NULL;
6310 struct collect_merged_paths_arg cmp_arg;
6311 char *fileindex_path;
6313 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6315 err = get_fileindex_path(&fileindex_path, worktree);
6316 if (err)
6317 return err;
6319 cmp_arg.progress_cb = progress_cb;
6320 cmp_arg.progress_arg = progress_arg;
6321 cmp_arg.merged_paths = merged_paths;
6322 err = merge_files(worktree, fileindex, fileindex_path,
6323 parent_commit_id, commit_id, repo, collect_merged_paths,
6324 &cmp_arg, cancel_cb, cancel_arg);
6325 if (commit_ref)
6326 got_ref_close(commit_ref);
6327 return err;
6330 const struct got_error *
6331 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6332 struct got_worktree *worktree, struct got_fileindex *fileindex,
6333 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6334 struct got_repository *repo,
6335 got_worktree_checkout_cb progress_cb, void *progress_arg,
6336 got_cancel_cb cancel_cb, void *cancel_arg)
6338 const struct got_error *err;
6339 char *commit_ref_name;
6341 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6342 if (err)
6343 return err;
6345 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6346 if (err)
6347 goto done;
6349 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6350 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6351 progress_arg, cancel_cb, cancel_arg);
6352 done:
6353 free(commit_ref_name);
6354 return err;
6357 const struct got_error *
6358 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6359 struct got_worktree *worktree, struct got_fileindex *fileindex,
6360 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6361 struct got_repository *repo,
6362 got_worktree_checkout_cb progress_cb, void *progress_arg,
6363 got_cancel_cb cancel_cb, void *cancel_arg)
6365 const struct got_error *err;
6366 char *commit_ref_name;
6368 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6369 if (err)
6370 return err;
6372 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6373 if (err)
6374 goto done;
6376 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6377 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6378 progress_arg, cancel_cb, cancel_arg);
6379 done:
6380 free(commit_ref_name);
6381 return err;
6384 static const struct got_error *
6385 rebase_commit(struct got_object_id **new_commit_id,
6386 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6387 struct got_worktree *worktree, struct got_fileindex *fileindex,
6388 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6389 const char *new_logmsg, struct got_repository *repo)
6391 const struct got_error *err, *sync_err;
6392 struct got_pathlist_head commitable_paths;
6393 struct collect_commitables_arg cc_arg;
6394 char *fileindex_path = NULL;
6395 struct got_reference *head_ref = NULL;
6396 struct got_object_id *head_commit_id = NULL;
6397 char *logmsg = NULL;
6399 TAILQ_INIT(&commitable_paths);
6400 *new_commit_id = NULL;
6402 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6404 err = get_fileindex_path(&fileindex_path, worktree);
6405 if (err)
6406 return err;
6408 cc_arg.commitable_paths = &commitable_paths;
6409 cc_arg.worktree = worktree;
6410 cc_arg.repo = repo;
6411 cc_arg.have_staged_files = 0;
6413 * If possible get the status of individual files directly to
6414 * avoid crawling the entire work tree once per rebased commit.
6416 * Ideally, merged_paths would contain a list of commitables
6417 * we could use so we could skip worktree_status() entirely.
6418 * However, we would then need carefully keep track of cumulative
6419 * effects of operations such as file additions and deletions
6420 * in 'got histedit -f' (folding multiple commits into one),
6421 * and this extra complexity is not really worth it.
6423 if (merged_paths) {
6424 struct got_pathlist_entry *pe;
6425 TAILQ_FOREACH(pe, merged_paths, entry) {
6426 err = worktree_status(worktree, pe->path, fileindex,
6427 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6428 0);
6429 if (err)
6430 goto done;
6432 } else {
6433 err = worktree_status(worktree, "", fileindex, repo,
6434 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6435 if (err)
6436 goto done;
6439 if (TAILQ_EMPTY(&commitable_paths)) {
6440 /* No-op change; commit will be elided. */
6441 err = got_ref_delete(commit_ref, repo);
6442 if (err)
6443 goto done;
6444 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6445 goto done;
6448 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6449 if (err)
6450 goto done;
6452 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6453 if (err)
6454 goto done;
6456 if (new_logmsg) {
6457 logmsg = strdup(new_logmsg);
6458 if (logmsg == NULL) {
6459 err = got_error_from_errno("strdup");
6460 goto done;
6462 } else {
6463 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6464 if (err)
6465 goto done;
6468 /* NB: commit_worktree will call free(logmsg) */
6469 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6470 NULL, worktree, got_object_commit_get_author(orig_commit),
6471 got_object_commit_get_committer(orig_commit),
6472 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6473 if (err)
6474 goto done;
6476 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6477 if (err)
6478 goto done;
6480 err = got_ref_delete(commit_ref, repo);
6481 if (err)
6482 goto done;
6484 err = update_fileindex_after_commit(worktree, &commitable_paths,
6485 *new_commit_id, fileindex, 0);
6486 sync_err = sync_fileindex(fileindex, fileindex_path);
6487 if (sync_err && err == NULL)
6488 err = sync_err;
6489 done:
6490 free(fileindex_path);
6491 free(head_commit_id);
6492 if (head_ref)
6493 got_ref_close(head_ref);
6494 if (err) {
6495 free(*new_commit_id);
6496 *new_commit_id = NULL;
6498 return err;
6501 const struct got_error *
6502 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6503 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6504 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6505 struct got_commit_object *orig_commit,
6506 struct got_object_id *orig_commit_id, struct got_repository *repo)
6508 const struct got_error *err;
6509 char *commit_ref_name;
6510 struct got_reference *commit_ref = NULL;
6511 struct got_object_id *commit_id = NULL;
6513 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6514 if (err)
6515 return err;
6517 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6518 if (err)
6519 goto done;
6520 err = got_ref_resolve(&commit_id, repo, commit_ref);
6521 if (err)
6522 goto done;
6523 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6524 err = got_error(GOT_ERR_REBASE_COMMITID);
6525 goto done;
6528 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6529 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6530 done:
6531 if (commit_ref)
6532 got_ref_close(commit_ref);
6533 free(commit_ref_name);
6534 free(commit_id);
6535 return err;
6538 const struct got_error *
6539 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6540 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6541 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6542 struct got_commit_object *orig_commit,
6543 struct got_object_id *orig_commit_id, const char *new_logmsg,
6544 struct got_repository *repo)
6546 const struct got_error *err;
6547 char *commit_ref_name;
6548 struct got_reference *commit_ref = NULL;
6550 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6551 if (err)
6552 return err;
6554 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6555 if (err)
6556 goto done;
6558 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6559 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6560 done:
6561 if (commit_ref)
6562 got_ref_close(commit_ref);
6563 free(commit_ref_name);
6564 return err;
6567 const struct got_error *
6568 got_worktree_rebase_postpone(struct got_worktree *worktree,
6569 struct got_fileindex *fileindex)
6571 if (fileindex)
6572 got_fileindex_free(fileindex);
6573 return lock_worktree(worktree, LOCK_SH);
6576 static const struct got_error *
6577 delete_ref(const char *name, struct got_repository *repo)
6579 const struct got_error *err;
6580 struct got_reference *ref;
6582 err = got_ref_open(&ref, repo, name, 0);
6583 if (err) {
6584 if (err->code == GOT_ERR_NOT_REF)
6585 return NULL;
6586 return err;
6589 err = got_ref_delete(ref, repo);
6590 got_ref_close(ref);
6591 return err;
6594 static const struct got_error *
6595 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6597 const struct got_error *err;
6598 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6599 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6601 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6602 if (err)
6603 goto done;
6604 err = delete_ref(tmp_branch_name, repo);
6605 if (err)
6606 goto done;
6608 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6609 if (err)
6610 goto done;
6611 err = delete_ref(new_base_branch_ref_name, repo);
6612 if (err)
6613 goto done;
6615 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6616 if (err)
6617 goto done;
6618 err = delete_ref(branch_ref_name, repo);
6619 if (err)
6620 goto done;
6622 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6623 if (err)
6624 goto done;
6625 err = delete_ref(commit_ref_name, repo);
6626 if (err)
6627 goto done;
6629 done:
6630 free(tmp_branch_name);
6631 free(new_base_branch_ref_name);
6632 free(branch_ref_name);
6633 free(commit_ref_name);
6634 return err;
6637 const struct got_error *
6638 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6639 struct got_object_id *new_commit_id, struct got_repository *repo)
6641 const struct got_error *err;
6642 struct got_reference *ref = NULL;
6643 struct got_object_id *old_commit_id = NULL;
6644 const char *branch_name = NULL;
6645 char *new_id_str = NULL;
6646 char *refname = NULL;
6648 branch_name = got_ref_get_name(branch);
6649 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6650 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6651 branch_name += 11;
6653 err = got_object_id_str(&new_id_str, new_commit_id);
6654 if (err)
6655 return err;
6657 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6658 new_id_str) == -1) {
6659 err = got_error_from_errno("asprintf");
6660 goto done;
6663 err = got_ref_resolve(&old_commit_id, repo, branch);
6664 if (err)
6665 goto done;
6667 err = got_ref_alloc(&ref, refname, old_commit_id);
6668 if (err)
6669 goto done;
6671 err = got_ref_write(ref, repo);
6672 done:
6673 free(new_id_str);
6674 free(refname);
6675 free(old_commit_id);
6676 if (ref)
6677 got_ref_close(ref);
6678 return err;
6681 const struct got_error *
6682 got_worktree_rebase_complete(struct got_worktree *worktree,
6683 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6684 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6685 struct got_repository *repo, int create_backup)
6687 const struct got_error *err, *unlockerr, *sync_err;
6688 struct got_object_id *new_head_commit_id = NULL;
6689 char *fileindex_path = NULL;
6691 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6692 if (err)
6693 return err;
6695 if (create_backup) {
6696 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6697 rebased_branch, new_head_commit_id, repo);
6698 if (err)
6699 goto done;
6702 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6703 if (err)
6704 goto done;
6706 err = got_ref_write(rebased_branch, repo);
6707 if (err)
6708 goto done;
6710 err = got_worktree_set_head_ref(worktree, rebased_branch);
6711 if (err)
6712 goto done;
6714 err = delete_rebase_refs(worktree, repo);
6715 if (err)
6716 goto done;
6718 err = get_fileindex_path(&fileindex_path, worktree);
6719 if (err)
6720 goto done;
6721 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6722 sync_err = sync_fileindex(fileindex, fileindex_path);
6723 if (sync_err && err == NULL)
6724 err = sync_err;
6725 done:
6726 got_fileindex_free(fileindex);
6727 free(fileindex_path);
6728 free(new_head_commit_id);
6729 unlockerr = lock_worktree(worktree, LOCK_SH);
6730 if (unlockerr && err == NULL)
6731 err = unlockerr;
6732 return err;
6735 const struct got_error *
6736 got_worktree_rebase_abort(struct got_worktree *worktree,
6737 struct got_fileindex *fileindex, struct got_repository *repo,
6738 struct got_reference *new_base_branch,
6739 got_worktree_checkout_cb progress_cb, void *progress_arg)
6741 const struct got_error *err, *unlockerr, *sync_err;
6742 struct got_reference *resolved = NULL;
6743 struct got_object_id *commit_id = NULL;
6744 struct got_commit_object *commit = NULL;
6745 char *fileindex_path = NULL;
6746 struct revert_file_args rfa;
6747 struct got_object_id *tree_id = NULL;
6749 err = lock_worktree(worktree, LOCK_EX);
6750 if (err)
6751 return err;
6753 err = got_object_open_as_commit(&commit, repo,
6754 worktree->base_commit_id);
6755 if (err)
6756 goto done;
6758 err = got_ref_open(&resolved, repo,
6759 got_ref_get_symref_target(new_base_branch), 0);
6760 if (err)
6761 goto done;
6763 err = got_worktree_set_head_ref(worktree, resolved);
6764 if (err)
6765 goto done;
6768 * XXX commits to the base branch could have happened while
6769 * we were busy rebasing; should we store the original commit ID
6770 * when rebase begins and read it back here?
6772 err = got_ref_resolve(&commit_id, repo, resolved);
6773 if (err)
6774 goto done;
6776 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6777 if (err)
6778 goto done;
6780 err = got_object_id_by_path(&tree_id, repo, commit,
6781 worktree->path_prefix);
6782 if (err)
6783 goto done;
6785 err = delete_rebase_refs(worktree, repo);
6786 if (err)
6787 goto done;
6789 err = get_fileindex_path(&fileindex_path, worktree);
6790 if (err)
6791 goto done;
6793 rfa.worktree = worktree;
6794 rfa.fileindex = fileindex;
6795 rfa.progress_cb = progress_cb;
6796 rfa.progress_arg = progress_arg;
6797 rfa.patch_cb = NULL;
6798 rfa.patch_arg = NULL;
6799 rfa.repo = repo;
6800 rfa.unlink_added_files = 0;
6801 err = worktree_status(worktree, "", fileindex, repo,
6802 revert_file, &rfa, NULL, NULL, 1, 0);
6803 if (err)
6804 goto sync;
6806 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6807 repo, progress_cb, progress_arg, NULL, NULL);
6808 sync:
6809 sync_err = sync_fileindex(fileindex, fileindex_path);
6810 if (sync_err && err == NULL)
6811 err = sync_err;
6812 done:
6813 got_ref_close(resolved);
6814 free(tree_id);
6815 free(commit_id);
6816 if (commit)
6817 got_object_commit_close(commit);
6818 if (fileindex)
6819 got_fileindex_free(fileindex);
6820 free(fileindex_path);
6822 unlockerr = lock_worktree(worktree, LOCK_SH);
6823 if (unlockerr && err == NULL)
6824 err = unlockerr;
6825 return err;
6828 const struct got_error *
6829 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6830 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6831 struct got_fileindex **fileindex, struct got_worktree *worktree,
6832 struct got_repository *repo)
6834 const struct got_error *err = NULL;
6835 char *tmp_branch_name = NULL;
6836 char *branch_ref_name = NULL;
6837 char *base_commit_ref_name = NULL;
6838 char *fileindex_path = NULL;
6839 struct check_rebase_ok_arg ok_arg;
6840 struct got_reference *wt_branch = NULL;
6841 struct got_reference *base_commit_ref = NULL;
6843 *tmp_branch = NULL;
6844 *branch_ref = NULL;
6845 *base_commit_id = NULL;
6846 *fileindex = NULL;
6848 err = lock_worktree(worktree, LOCK_EX);
6849 if (err)
6850 return err;
6852 err = open_fileindex(fileindex, &fileindex_path, worktree);
6853 if (err)
6854 goto done;
6856 ok_arg.worktree = worktree;
6857 ok_arg.repo = repo;
6858 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6859 &ok_arg);
6860 if (err)
6861 goto done;
6863 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6864 if (err)
6865 goto done;
6867 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6868 if (err)
6869 goto done;
6871 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6872 worktree);
6873 if (err)
6874 goto done;
6876 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6877 0);
6878 if (err)
6879 goto done;
6881 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6882 if (err)
6883 goto done;
6885 err = got_ref_write(*branch_ref, repo);
6886 if (err)
6887 goto done;
6889 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6890 worktree->base_commit_id);
6891 if (err)
6892 goto done;
6893 err = got_ref_write(base_commit_ref, repo);
6894 if (err)
6895 goto done;
6896 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6897 if (*base_commit_id == NULL) {
6898 err = got_error_from_errno("got_object_id_dup");
6899 goto done;
6902 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6903 worktree->base_commit_id);
6904 if (err)
6905 goto done;
6906 err = got_ref_write(*tmp_branch, repo);
6907 if (err)
6908 goto done;
6910 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6911 if (err)
6912 goto done;
6913 done:
6914 free(fileindex_path);
6915 free(tmp_branch_name);
6916 free(branch_ref_name);
6917 free(base_commit_ref_name);
6918 if (wt_branch)
6919 got_ref_close(wt_branch);
6920 if (err) {
6921 if (*branch_ref) {
6922 got_ref_close(*branch_ref);
6923 *branch_ref = NULL;
6925 if (*tmp_branch) {
6926 got_ref_close(*tmp_branch);
6927 *tmp_branch = NULL;
6929 free(*base_commit_id);
6930 if (*fileindex) {
6931 got_fileindex_free(*fileindex);
6932 *fileindex = NULL;
6934 lock_worktree(worktree, LOCK_SH);
6936 return err;
6939 const struct got_error *
6940 got_worktree_histedit_postpone(struct got_worktree *worktree,
6941 struct got_fileindex *fileindex)
6943 if (fileindex)
6944 got_fileindex_free(fileindex);
6945 return lock_worktree(worktree, LOCK_SH);
6948 const struct got_error *
6949 got_worktree_histedit_in_progress(int *in_progress,
6950 struct got_worktree *worktree)
6952 const struct got_error *err;
6953 char *tmp_branch_name = NULL;
6955 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6956 if (err)
6957 return err;
6959 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6960 free(tmp_branch_name);
6961 return NULL;
6964 const struct got_error *
6965 got_worktree_histedit_continue(struct got_object_id **commit_id,
6966 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6967 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6968 struct got_worktree *worktree, struct got_repository *repo)
6970 const struct got_error *err;
6971 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6972 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6973 struct got_reference *commit_ref = NULL;
6974 struct got_reference *base_commit_ref = NULL;
6975 char *fileindex_path = NULL;
6976 int have_staged_files = 0;
6978 *commit_id = NULL;
6979 *tmp_branch = NULL;
6980 *base_commit_id = NULL;
6981 *fileindex = NULL;
6983 err = lock_worktree(worktree, LOCK_EX);
6984 if (err)
6985 return err;
6987 err = open_fileindex(fileindex, &fileindex_path, worktree);
6988 if (err)
6989 goto done;
6991 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6992 &have_staged_files);
6993 if (err && err->code != GOT_ERR_CANCELLED)
6994 goto done;
6995 if (have_staged_files) {
6996 err = got_error(GOT_ERR_STAGED_PATHS);
6997 goto done;
7000 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7001 if (err)
7002 goto done;
7004 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7005 if (err)
7006 goto done;
7008 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7009 if (err)
7010 goto done;
7012 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7013 worktree);
7014 if (err)
7015 goto done;
7017 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7018 if (err)
7019 goto done;
7021 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7022 if (err)
7023 goto done;
7024 err = got_ref_resolve(commit_id, repo, commit_ref);
7025 if (err)
7026 goto done;
7028 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7029 if (err)
7030 goto done;
7031 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7032 if (err)
7033 goto done;
7035 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7036 if (err)
7037 goto done;
7038 done:
7039 free(commit_ref_name);
7040 free(branch_ref_name);
7041 free(fileindex_path);
7042 if (commit_ref)
7043 got_ref_close(commit_ref);
7044 if (base_commit_ref)
7045 got_ref_close(base_commit_ref);
7046 if (err) {
7047 free(*commit_id);
7048 *commit_id = NULL;
7049 free(*base_commit_id);
7050 *base_commit_id = NULL;
7051 if (*tmp_branch) {
7052 got_ref_close(*tmp_branch);
7053 *tmp_branch = NULL;
7055 if (*fileindex) {
7056 got_fileindex_free(*fileindex);
7057 *fileindex = NULL;
7059 lock_worktree(worktree, LOCK_EX);
7061 return err;
7064 static const struct got_error *
7065 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7067 const struct got_error *err;
7068 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7069 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7071 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7072 if (err)
7073 goto done;
7074 err = delete_ref(tmp_branch_name, repo);
7075 if (err)
7076 goto done;
7078 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7079 worktree);
7080 if (err)
7081 goto done;
7082 err = delete_ref(base_commit_ref_name, repo);
7083 if (err)
7084 goto done;
7086 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7087 if (err)
7088 goto done;
7089 err = delete_ref(branch_ref_name, repo);
7090 if (err)
7091 goto done;
7093 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7094 if (err)
7095 goto done;
7096 err = delete_ref(commit_ref_name, repo);
7097 if (err)
7098 goto done;
7099 done:
7100 free(tmp_branch_name);
7101 free(base_commit_ref_name);
7102 free(branch_ref_name);
7103 free(commit_ref_name);
7104 return err;
7107 const struct got_error *
7108 got_worktree_histedit_abort(struct got_worktree *worktree,
7109 struct got_fileindex *fileindex, struct got_repository *repo,
7110 struct got_reference *branch, struct got_object_id *base_commit_id,
7111 got_worktree_checkout_cb progress_cb, void *progress_arg)
7113 const struct got_error *err, *unlockerr, *sync_err;
7114 struct got_reference *resolved = NULL;
7115 char *fileindex_path = NULL;
7116 struct got_commit_object *commit = NULL;
7117 struct got_object_id *tree_id = NULL;
7118 struct revert_file_args rfa;
7120 err = lock_worktree(worktree, LOCK_EX);
7121 if (err)
7122 return err;
7124 err = got_object_open_as_commit(&commit, repo,
7125 worktree->base_commit_id);
7126 if (err)
7127 goto done;
7129 err = got_ref_open(&resolved, repo,
7130 got_ref_get_symref_target(branch), 0);
7131 if (err)
7132 goto done;
7134 err = got_worktree_set_head_ref(worktree, resolved);
7135 if (err)
7136 goto done;
7138 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7139 if (err)
7140 goto done;
7142 err = got_object_id_by_path(&tree_id, repo, commit,
7143 worktree->path_prefix);
7144 if (err)
7145 goto done;
7147 err = delete_histedit_refs(worktree, repo);
7148 if (err)
7149 goto done;
7151 err = get_fileindex_path(&fileindex_path, worktree);
7152 if (err)
7153 goto done;
7155 rfa.worktree = worktree;
7156 rfa.fileindex = fileindex;
7157 rfa.progress_cb = progress_cb;
7158 rfa.progress_arg = progress_arg;
7159 rfa.patch_cb = NULL;
7160 rfa.patch_arg = NULL;
7161 rfa.repo = repo;
7162 rfa.unlink_added_files = 0;
7163 err = worktree_status(worktree, "", fileindex, repo,
7164 revert_file, &rfa, NULL, NULL, 1, 0);
7165 if (err)
7166 goto sync;
7168 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7169 repo, progress_cb, progress_arg, NULL, NULL);
7170 sync:
7171 sync_err = sync_fileindex(fileindex, fileindex_path);
7172 if (sync_err && err == NULL)
7173 err = sync_err;
7174 done:
7175 got_ref_close(resolved);
7176 free(tree_id);
7177 free(fileindex_path);
7179 unlockerr = lock_worktree(worktree, LOCK_SH);
7180 if (unlockerr && err == NULL)
7181 err = unlockerr;
7182 return err;
7185 const struct got_error *
7186 got_worktree_histedit_complete(struct got_worktree *worktree,
7187 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7188 struct got_reference *edited_branch, struct got_repository *repo)
7190 const struct got_error *err, *unlockerr, *sync_err;
7191 struct got_object_id *new_head_commit_id = NULL;
7192 struct got_reference *resolved = NULL;
7193 char *fileindex_path = NULL;
7195 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7196 if (err)
7197 return err;
7199 err = got_ref_open(&resolved, repo,
7200 got_ref_get_symref_target(edited_branch), 0);
7201 if (err)
7202 goto done;
7204 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7205 resolved, new_head_commit_id, repo);
7206 if (err)
7207 goto done;
7209 err = got_ref_change_ref(resolved, new_head_commit_id);
7210 if (err)
7211 goto done;
7213 err = got_ref_write(resolved, repo);
7214 if (err)
7215 goto done;
7217 err = got_worktree_set_head_ref(worktree, resolved);
7218 if (err)
7219 goto done;
7221 err = delete_histedit_refs(worktree, repo);
7222 if (err)
7223 goto done;
7225 err = get_fileindex_path(&fileindex_path, worktree);
7226 if (err)
7227 goto done;
7228 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7229 sync_err = sync_fileindex(fileindex, fileindex_path);
7230 if (sync_err && err == NULL)
7231 err = sync_err;
7232 done:
7233 got_fileindex_free(fileindex);
7234 free(fileindex_path);
7235 free(new_head_commit_id);
7236 unlockerr = lock_worktree(worktree, LOCK_SH);
7237 if (unlockerr && err == NULL)
7238 err = unlockerr;
7239 return err;
7242 const struct got_error *
7243 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7244 struct got_object_id *commit_id, struct got_repository *repo)
7246 const struct got_error *err;
7247 char *commit_ref_name;
7249 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7250 if (err)
7251 return err;
7253 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7254 if (err)
7255 goto done;
7257 err = delete_ref(commit_ref_name, repo);
7258 done:
7259 free(commit_ref_name);
7260 return err;
7263 const struct got_error *
7264 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7265 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7266 struct got_worktree *worktree, const char *refname,
7267 struct got_repository *repo)
7269 const struct got_error *err = NULL;
7270 char *fileindex_path = NULL;
7271 struct check_rebase_ok_arg ok_arg;
7273 *fileindex = NULL;
7274 *branch_ref = NULL;
7275 *base_branch_ref = NULL;
7277 err = lock_worktree(worktree, LOCK_EX);
7278 if (err)
7279 return err;
7281 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7282 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7283 "cannot integrate a branch into itself; "
7284 "update -b or different branch name required");
7285 goto done;
7288 err = open_fileindex(fileindex, &fileindex_path, worktree);
7289 if (err)
7290 goto done;
7292 /* Preconditions are the same as for rebase. */
7293 ok_arg.worktree = worktree;
7294 ok_arg.repo = repo;
7295 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7296 &ok_arg);
7297 if (err)
7298 goto done;
7300 err = got_ref_open(branch_ref, repo, refname, 1);
7301 if (err)
7302 goto done;
7304 err = got_ref_open(base_branch_ref, repo,
7305 got_worktree_get_head_ref_name(worktree), 1);
7306 done:
7307 if (err) {
7308 if (*branch_ref) {
7309 got_ref_close(*branch_ref);
7310 *branch_ref = NULL;
7312 if (*base_branch_ref) {
7313 got_ref_close(*base_branch_ref);
7314 *base_branch_ref = NULL;
7316 if (*fileindex) {
7317 got_fileindex_free(*fileindex);
7318 *fileindex = NULL;
7320 lock_worktree(worktree, LOCK_SH);
7322 return err;
7325 const struct got_error *
7326 got_worktree_integrate_continue(struct got_worktree *worktree,
7327 struct got_fileindex *fileindex, struct got_repository *repo,
7328 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7329 got_worktree_checkout_cb progress_cb, void *progress_arg,
7330 got_cancel_cb cancel_cb, void *cancel_arg)
7332 const struct got_error *err = NULL, *sync_err, *unlockerr;
7333 char *fileindex_path = NULL;
7334 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7335 struct got_commit_object *commit = NULL;
7337 err = get_fileindex_path(&fileindex_path, worktree);
7338 if (err)
7339 goto done;
7341 err = got_ref_resolve(&commit_id, repo, branch_ref);
7342 if (err)
7343 goto done;
7345 err = got_object_open_as_commit(&commit, repo, commit_id);
7346 if (err)
7347 goto done;
7349 err = got_object_id_by_path(&tree_id, repo, commit,
7350 worktree->path_prefix);
7351 if (err)
7352 goto done;
7354 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7355 if (err)
7356 goto done;
7358 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7359 progress_cb, progress_arg, cancel_cb, cancel_arg);
7360 if (err)
7361 goto sync;
7363 err = got_ref_change_ref(base_branch_ref, commit_id);
7364 if (err)
7365 goto sync;
7367 err = got_ref_write(base_branch_ref, repo);
7368 if (err)
7369 goto sync;
7371 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7372 sync:
7373 sync_err = sync_fileindex(fileindex, fileindex_path);
7374 if (sync_err && err == NULL)
7375 err = sync_err;
7377 done:
7378 unlockerr = got_ref_unlock(branch_ref);
7379 if (unlockerr && err == NULL)
7380 err = unlockerr;
7381 got_ref_close(branch_ref);
7383 unlockerr = got_ref_unlock(base_branch_ref);
7384 if (unlockerr && err == NULL)
7385 err = unlockerr;
7386 got_ref_close(base_branch_ref);
7388 got_fileindex_free(fileindex);
7389 free(fileindex_path);
7390 free(tree_id);
7391 if (commit)
7392 got_object_commit_close(commit);
7394 unlockerr = lock_worktree(worktree, LOCK_SH);
7395 if (unlockerr && err == NULL)
7396 err = unlockerr;
7397 return err;
7400 const struct got_error *
7401 got_worktree_integrate_abort(struct got_worktree *worktree,
7402 struct got_fileindex *fileindex, struct got_repository *repo,
7403 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7405 const struct got_error *err = NULL, *unlockerr = NULL;
7407 got_fileindex_free(fileindex);
7409 err = lock_worktree(worktree, LOCK_SH);
7411 unlockerr = got_ref_unlock(branch_ref);
7412 if (unlockerr && err == NULL)
7413 err = unlockerr;
7414 got_ref_close(branch_ref);
7416 unlockerr = got_ref_unlock(base_branch_ref);
7417 if (unlockerr && err == NULL)
7418 err = unlockerr;
7419 got_ref_close(base_branch_ref);
7421 return err;
7424 const struct got_error *
7425 got_worktree_merge_postpone(struct got_worktree *worktree,
7426 struct got_fileindex *fileindex)
7428 const struct got_error *err, *sync_err;
7429 char *fileindex_path = NULL;
7431 err = get_fileindex_path(&fileindex_path, worktree);
7432 if (err)
7433 goto done;
7435 sync_err = sync_fileindex(fileindex, fileindex_path);
7437 err = lock_worktree(worktree, LOCK_SH);
7438 if (sync_err && err == NULL)
7439 err = sync_err;
7440 done:
7441 got_fileindex_free(fileindex);
7442 free(fileindex_path);
7443 return err;
7446 static const struct got_error *
7447 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7449 const struct got_error *err;
7450 char *branch_refname = NULL, *commit_refname = NULL;
7452 err = get_merge_branch_ref_name(&branch_refname, worktree);
7453 if (err)
7454 goto done;
7455 err = delete_ref(branch_refname, repo);
7456 if (err)
7457 goto done;
7459 err = get_merge_commit_ref_name(&commit_refname, worktree);
7460 if (err)
7461 goto done;
7462 err = delete_ref(commit_refname, repo);
7463 if (err)
7464 goto done;
7466 done:
7467 free(branch_refname);
7468 free(commit_refname);
7469 return err;
7472 struct merge_commit_msg_arg {
7473 struct got_worktree *worktree;
7474 const char *branch_name;
7477 static const struct got_error *
7478 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7479 void *arg)
7481 struct merge_commit_msg_arg *a = arg;
7483 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7484 got_worktree_get_head_ref_name(a->worktree)) == -1)
7485 return got_error_from_errno("asprintf");
7487 return NULL;
7491 const struct got_error *
7492 got_worktree_merge_branch(struct got_worktree *worktree,
7493 struct got_fileindex *fileindex,
7494 struct got_object_id *yca_commit_id,
7495 struct got_object_id *branch_tip,
7496 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7497 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7499 const struct got_error *err;
7500 char *fileindex_path = NULL;
7502 err = get_fileindex_path(&fileindex_path, worktree);
7503 if (err)
7504 goto done;
7506 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7507 worktree);
7508 if (err)
7509 goto done;
7511 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7512 branch_tip, repo, progress_cb, progress_arg,
7513 cancel_cb, cancel_arg);
7514 done:
7515 free(fileindex_path);
7516 return err;
7519 const struct got_error *
7520 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7521 struct got_worktree *worktree, struct got_fileindex *fileindex,
7522 const char *author, const char *committer, int allow_bad_symlinks,
7523 struct got_object_id *branch_tip, const char *branch_name,
7524 struct got_repository *repo,
7525 got_worktree_status_cb status_cb, void *status_arg)
7528 const struct got_error *err = NULL, *sync_err;
7529 struct got_pathlist_head commitable_paths;
7530 struct collect_commitables_arg cc_arg;
7531 struct got_pathlist_entry *pe;
7532 struct got_reference *head_ref = NULL;
7533 struct got_object_id *head_commit_id = NULL;
7534 int have_staged_files = 0;
7535 struct merge_commit_msg_arg mcm_arg;
7536 char *fileindex_path = NULL;
7538 *new_commit_id = NULL;
7540 TAILQ_INIT(&commitable_paths);
7542 err = get_fileindex_path(&fileindex_path, worktree);
7543 if (err)
7544 goto done;
7546 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7547 if (err)
7548 goto done;
7550 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7551 if (err)
7552 goto done;
7554 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7555 &have_staged_files);
7556 if (err && err->code != GOT_ERR_CANCELLED)
7557 goto done;
7558 if (have_staged_files) {
7559 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7560 goto done;
7563 cc_arg.commitable_paths = &commitable_paths;
7564 cc_arg.worktree = worktree;
7565 cc_arg.fileindex = fileindex;
7566 cc_arg.repo = repo;
7567 cc_arg.have_staged_files = have_staged_files;
7568 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7569 err = worktree_status(worktree, "", fileindex, repo,
7570 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7571 if (err)
7572 goto done;
7574 if (TAILQ_EMPTY(&commitable_paths)) {
7575 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7576 "merge of %s cannot proceed", branch_name);
7577 goto done;
7580 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7581 struct got_commitable *ct = pe->data;
7582 const char *ct_path = ct->in_repo_path;
7584 while (ct_path[0] == '/')
7585 ct_path++;
7586 err = check_out_of_date(ct_path, ct->status,
7587 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7588 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7589 if (err)
7590 goto done;
7594 mcm_arg.worktree = worktree;
7595 mcm_arg.branch_name = branch_name;
7596 err = commit_worktree(new_commit_id, &commitable_paths,
7597 head_commit_id, branch_tip, worktree, author, committer,
7598 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7599 if (err)
7600 goto done;
7602 err = update_fileindex_after_commit(worktree, &commitable_paths,
7603 *new_commit_id, fileindex, have_staged_files);
7604 sync_err = sync_fileindex(fileindex, fileindex_path);
7605 if (sync_err && err == NULL)
7606 err = sync_err;
7607 done:
7608 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7609 struct got_commitable *ct = pe->data;
7610 free_commitable(ct);
7612 got_pathlist_free(&commitable_paths);
7613 free(fileindex_path);
7614 return err;
7617 const struct got_error *
7618 got_worktree_merge_complete(struct got_worktree *worktree,
7619 struct got_fileindex *fileindex, struct got_repository *repo)
7621 const struct got_error *err, *unlockerr, *sync_err;
7622 char *fileindex_path = NULL;
7624 err = delete_merge_refs(worktree, repo);
7625 if (err)
7626 goto done;
7628 err = get_fileindex_path(&fileindex_path, worktree);
7629 if (err)
7630 goto done;
7631 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7632 sync_err = sync_fileindex(fileindex, fileindex_path);
7633 if (sync_err && err == NULL)
7634 err = sync_err;
7635 done:
7636 got_fileindex_free(fileindex);
7637 free(fileindex_path);
7638 unlockerr = lock_worktree(worktree, LOCK_SH);
7639 if (unlockerr && err == NULL)
7640 err = unlockerr;
7641 return err;
7644 const struct got_error *
7645 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7646 struct got_repository *repo)
7648 const struct got_error *err;
7649 char *branch_refname = NULL;
7650 struct got_reference *branch_ref = NULL;
7652 *in_progress = 0;
7654 err = get_merge_branch_ref_name(&branch_refname, worktree);
7655 if (err)
7656 return err;
7657 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7658 free(branch_refname);
7659 if (err) {
7660 if (err->code != GOT_ERR_NOT_REF)
7661 return err;
7662 } else
7663 *in_progress = 1;
7665 return NULL;
7668 const struct got_error *got_worktree_merge_prepare(
7669 struct got_fileindex **fileindex, struct got_worktree *worktree,
7670 struct got_reference *branch, struct got_repository *repo)
7672 const struct got_error *err = NULL;
7673 char *fileindex_path = NULL;
7674 char *branch_refname = NULL, *commit_refname = NULL;
7675 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7676 struct got_reference *commit_ref = NULL;
7677 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7678 struct check_rebase_ok_arg ok_arg;
7680 *fileindex = NULL;
7682 err = lock_worktree(worktree, LOCK_EX);
7683 if (err)
7684 return err;
7686 err = open_fileindex(fileindex, &fileindex_path, worktree);
7687 if (err)
7688 goto done;
7690 /* Preconditions are the same as for rebase. */
7691 ok_arg.worktree = worktree;
7692 ok_arg.repo = repo;
7693 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7694 &ok_arg);
7695 if (err)
7696 goto done;
7698 err = get_merge_branch_ref_name(&branch_refname, worktree);
7699 if (err)
7700 return err;
7702 err = get_merge_commit_ref_name(&commit_refname, worktree);
7703 if (err)
7704 return err;
7706 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7707 0);
7708 if (err)
7709 goto done;
7711 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7712 if (err)
7713 goto done;
7715 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7716 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7717 goto done;
7720 err = got_ref_resolve(&branch_tip, repo, branch);
7721 if (err)
7722 goto done;
7724 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7725 if (err)
7726 goto done;
7727 err = got_ref_write(branch_ref, repo);
7728 if (err)
7729 goto done;
7731 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7732 if (err)
7733 goto done;
7734 err = got_ref_write(commit_ref, repo);
7735 if (err)
7736 goto done;
7738 done:
7739 free(branch_refname);
7740 free(commit_refname);
7741 free(fileindex_path);
7742 if (branch_ref)
7743 got_ref_close(branch_ref);
7744 if (commit_ref)
7745 got_ref_close(commit_ref);
7746 if (wt_branch)
7747 got_ref_close(wt_branch);
7748 free(wt_branch_tip);
7749 if (err) {
7750 if (*fileindex) {
7751 got_fileindex_free(*fileindex);
7752 *fileindex = NULL;
7754 lock_worktree(worktree, LOCK_SH);
7756 return err;
7759 const struct got_error *
7760 got_worktree_merge_continue(char **branch_name,
7761 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7762 struct got_worktree *worktree, struct got_repository *repo)
7764 const struct got_error *err;
7765 char *commit_refname = NULL, *branch_refname = NULL;
7766 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7767 char *fileindex_path = NULL;
7768 int have_staged_files = 0;
7770 *branch_name = NULL;
7771 *branch_tip = NULL;
7772 *fileindex = NULL;
7774 err = lock_worktree(worktree, LOCK_EX);
7775 if (err)
7776 return err;
7778 err = open_fileindex(fileindex, &fileindex_path, worktree);
7779 if (err)
7780 goto done;
7782 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7783 &have_staged_files);
7784 if (err && err->code != GOT_ERR_CANCELLED)
7785 goto done;
7786 if (have_staged_files) {
7787 err = got_error(GOT_ERR_STAGED_PATHS);
7788 goto done;
7791 err = get_merge_branch_ref_name(&branch_refname, worktree);
7792 if (err)
7793 goto done;
7795 err = get_merge_commit_ref_name(&commit_refname, worktree);
7796 if (err)
7797 goto done;
7799 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7800 if (err)
7801 goto done;
7803 if (!got_ref_is_symbolic(branch_ref)) {
7804 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7805 "%s is not a symbolic reference",
7806 got_ref_get_name(branch_ref));
7807 goto done;
7809 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7810 if (*branch_name == NULL) {
7811 err = got_error_from_errno("strdup");
7812 goto done;
7815 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7816 if (err)
7817 goto done;
7819 err = got_ref_resolve(branch_tip, repo, commit_ref);
7820 if (err)
7821 goto done;
7822 done:
7823 free(commit_refname);
7824 free(branch_refname);
7825 free(fileindex_path);
7826 if (commit_ref)
7827 got_ref_close(commit_ref);
7828 if (branch_ref)
7829 got_ref_close(branch_ref);
7830 if (err) {
7831 if (*branch_name) {
7832 free(*branch_name);
7833 *branch_name = NULL;
7835 free(*branch_tip);
7836 *branch_tip = NULL;
7837 if (*fileindex) {
7838 got_fileindex_free(*fileindex);
7839 *fileindex = NULL;
7841 lock_worktree(worktree, LOCK_SH);
7843 return err;
7846 const struct got_error *
7847 got_worktree_merge_abort(struct got_worktree *worktree,
7848 struct got_fileindex *fileindex, struct got_repository *repo,
7849 got_worktree_checkout_cb progress_cb, void *progress_arg)
7851 const struct got_error *err, *unlockerr, *sync_err;
7852 struct got_object_id *commit_id = NULL;
7853 struct got_commit_object *commit = NULL;
7854 char *fileindex_path = NULL;
7855 struct revert_file_args rfa;
7856 struct got_object_id *tree_id = NULL;
7858 err = got_object_open_as_commit(&commit, repo,
7859 worktree->base_commit_id);
7860 if (err)
7861 goto done;
7863 err = got_object_id_by_path(&tree_id, repo, commit,
7864 worktree->path_prefix);
7865 if (err)
7866 goto done;
7868 err = delete_merge_refs(worktree, repo);
7869 if (err)
7870 goto done;
7872 err = get_fileindex_path(&fileindex_path, worktree);
7873 if (err)
7874 goto done;
7876 rfa.worktree = worktree;
7877 rfa.fileindex = fileindex;
7878 rfa.progress_cb = progress_cb;
7879 rfa.progress_arg = progress_arg;
7880 rfa.patch_cb = NULL;
7881 rfa.patch_arg = NULL;
7882 rfa.repo = repo;
7883 rfa.unlink_added_files = 1;
7884 err = worktree_status(worktree, "", fileindex, repo,
7885 revert_file, &rfa, NULL, NULL, 1, 0);
7886 if (err)
7887 goto sync;
7889 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7890 repo, progress_cb, progress_arg, NULL, NULL);
7891 sync:
7892 sync_err = sync_fileindex(fileindex, fileindex_path);
7893 if (sync_err && err == NULL)
7894 err = sync_err;
7895 done:
7896 free(tree_id);
7897 free(commit_id);
7898 if (commit)
7899 got_object_commit_close(commit);
7900 if (fileindex)
7901 got_fileindex_free(fileindex);
7902 free(fileindex_path);
7904 unlockerr = lock_worktree(worktree, LOCK_SH);
7905 if (unlockerr && err == NULL)
7906 err = unlockerr;
7907 return err;
7910 struct check_stage_ok_arg {
7911 struct got_object_id *head_commit_id;
7912 struct got_worktree *worktree;
7913 struct got_fileindex *fileindex;
7914 struct got_repository *repo;
7915 int have_changes;
7918 const struct got_error *
7919 check_stage_ok(void *arg, unsigned char status,
7920 unsigned char staged_status, const char *relpath,
7921 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7922 struct got_object_id *commit_id, int dirfd, const char *de_name)
7924 struct check_stage_ok_arg *a = arg;
7925 const struct got_error *err = NULL;
7926 struct got_fileindex_entry *ie;
7927 struct got_object_id base_commit_id;
7928 struct got_object_id *base_commit_idp = NULL;
7929 char *in_repo_path = NULL, *p;
7931 if (status == GOT_STATUS_UNVERSIONED ||
7932 status == GOT_STATUS_NO_CHANGE)
7933 return NULL;
7934 if (status == GOT_STATUS_NONEXISTENT)
7935 return got_error_set_errno(ENOENT, relpath);
7937 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7938 if (ie == NULL)
7939 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7941 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7942 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7943 relpath) == -1)
7944 return got_error_from_errno("asprintf");
7946 if (got_fileindex_entry_has_commit(ie)) {
7947 memcpy(base_commit_id.sha1, ie->commit_sha1,
7948 SHA1_DIGEST_LENGTH);
7949 base_commit_idp = &base_commit_id;
7952 if (status == GOT_STATUS_CONFLICT) {
7953 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7954 goto done;
7955 } else if (status != GOT_STATUS_ADD &&
7956 status != GOT_STATUS_MODIFY &&
7957 status != GOT_STATUS_DELETE) {
7958 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7959 goto done;
7962 a->have_changes = 1;
7964 p = in_repo_path;
7965 while (p[0] == '/')
7966 p++;
7967 err = check_out_of_date(p, status, staged_status,
7968 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7969 GOT_ERR_STAGE_OUT_OF_DATE);
7970 done:
7971 free(in_repo_path);
7972 return err;
7975 struct stage_path_arg {
7976 struct got_worktree *worktree;
7977 struct got_fileindex *fileindex;
7978 struct got_repository *repo;
7979 got_worktree_status_cb status_cb;
7980 void *status_arg;
7981 got_worktree_patch_cb patch_cb;
7982 void *patch_arg;
7983 int staged_something;
7984 int allow_bad_symlinks;
7987 static const struct got_error *
7988 stage_path(void *arg, unsigned char status,
7989 unsigned char staged_status, const char *relpath,
7990 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7991 struct got_object_id *commit_id, int dirfd, const char *de_name)
7993 struct stage_path_arg *a = arg;
7994 const struct got_error *err = NULL;
7995 struct got_fileindex_entry *ie;
7996 char *ondisk_path = NULL, *path_content = NULL;
7997 uint32_t stage;
7998 struct got_object_id *new_staged_blob_id = NULL;
7999 struct stat sb;
8001 if (status == GOT_STATUS_UNVERSIONED)
8002 return NULL;
8004 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8005 if (ie == NULL)
8006 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8008 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8009 relpath)== -1)
8010 return got_error_from_errno("asprintf");
8012 switch (status) {
8013 case GOT_STATUS_ADD:
8014 case GOT_STATUS_MODIFY:
8015 /* XXX could sb.st_mode be passed in by our caller? */
8016 if (lstat(ondisk_path, &sb) == -1) {
8017 err = got_error_from_errno2("lstat", ondisk_path);
8018 break;
8020 if (a->patch_cb) {
8021 if (status == GOT_STATUS_ADD) {
8022 int choice = GOT_PATCH_CHOICE_NONE;
8023 err = (*a->patch_cb)(&choice, a->patch_arg,
8024 status, ie->path, NULL, 1, 1);
8025 if (err)
8026 break;
8027 if (choice != GOT_PATCH_CHOICE_YES)
8028 break;
8029 } else {
8030 err = create_patched_content(&path_content, 0,
8031 staged_blob_id ? staged_blob_id : blob_id,
8032 ondisk_path, dirfd, de_name, ie->path,
8033 a->repo, a->patch_cb, a->patch_arg);
8034 if (err || path_content == NULL)
8035 break;
8038 err = got_object_blob_create(&new_staged_blob_id,
8039 path_content ? path_content : ondisk_path, a->repo);
8040 if (err)
8041 break;
8042 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8043 SHA1_DIGEST_LENGTH);
8044 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8045 stage = GOT_FILEIDX_STAGE_ADD;
8046 else
8047 stage = GOT_FILEIDX_STAGE_MODIFY;
8048 got_fileindex_entry_stage_set(ie, stage);
8049 if (S_ISLNK(sb.st_mode)) {
8050 int is_bad_symlink = 0;
8051 if (!a->allow_bad_symlinks) {
8052 char target_path[PATH_MAX];
8053 ssize_t target_len;
8054 target_len = readlink(ondisk_path, target_path,
8055 sizeof(target_path));
8056 if (target_len == -1) {
8057 err = got_error_from_errno2("readlink",
8058 ondisk_path);
8059 break;
8061 err = is_bad_symlink_target(&is_bad_symlink,
8062 target_path, target_len, ondisk_path,
8063 a->worktree->root_path);
8064 if (err)
8065 break;
8066 if (is_bad_symlink) {
8067 err = got_error_path(ondisk_path,
8068 GOT_ERR_BAD_SYMLINK);
8069 break;
8072 if (is_bad_symlink)
8073 got_fileindex_entry_staged_filetype_set(ie,
8074 GOT_FILEIDX_MODE_BAD_SYMLINK);
8075 else
8076 got_fileindex_entry_staged_filetype_set(ie,
8077 GOT_FILEIDX_MODE_SYMLINK);
8078 } else {
8079 got_fileindex_entry_staged_filetype_set(ie,
8080 GOT_FILEIDX_MODE_REGULAR_FILE);
8082 a->staged_something = 1;
8083 if (a->status_cb == NULL)
8084 break;
8085 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8086 get_staged_status(ie), relpath, blob_id,
8087 new_staged_blob_id, NULL, dirfd, de_name);
8088 if (err)
8089 break;
8091 * When staging the reverse of the staged diff,
8092 * implicitly unstage the file.
8094 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8095 sizeof(ie->blob_sha1)) == 0) {
8096 got_fileindex_entry_stage_set(ie,
8097 GOT_FILEIDX_STAGE_NONE);
8099 break;
8100 case GOT_STATUS_DELETE:
8101 if (staged_status == GOT_STATUS_DELETE)
8102 break;
8103 if (a->patch_cb) {
8104 int choice = GOT_PATCH_CHOICE_NONE;
8105 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8106 ie->path, NULL, 1, 1);
8107 if (err)
8108 break;
8109 if (choice == GOT_PATCH_CHOICE_NO)
8110 break;
8111 if (choice != GOT_PATCH_CHOICE_YES) {
8112 err = got_error(GOT_ERR_PATCH_CHOICE);
8113 break;
8116 stage = GOT_FILEIDX_STAGE_DELETE;
8117 got_fileindex_entry_stage_set(ie, stage);
8118 a->staged_something = 1;
8119 if (a->status_cb == NULL)
8120 break;
8121 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8122 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8123 de_name);
8124 break;
8125 case GOT_STATUS_NO_CHANGE:
8126 break;
8127 case GOT_STATUS_CONFLICT:
8128 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8129 break;
8130 case GOT_STATUS_NONEXISTENT:
8131 err = got_error_set_errno(ENOENT, relpath);
8132 break;
8133 default:
8134 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8135 break;
8138 if (path_content && unlink(path_content) == -1 && err == NULL)
8139 err = got_error_from_errno2("unlink", path_content);
8140 free(path_content);
8141 free(ondisk_path);
8142 free(new_staged_blob_id);
8143 return err;
8146 const struct got_error *
8147 got_worktree_stage(struct got_worktree *worktree,
8148 struct got_pathlist_head *paths,
8149 got_worktree_status_cb status_cb, void *status_arg,
8150 got_worktree_patch_cb patch_cb, void *patch_arg,
8151 int allow_bad_symlinks, struct got_repository *repo)
8153 const struct got_error *err = NULL, *sync_err, *unlockerr;
8154 struct got_pathlist_entry *pe;
8155 struct got_fileindex *fileindex = NULL;
8156 char *fileindex_path = NULL;
8157 struct got_reference *head_ref = NULL;
8158 struct got_object_id *head_commit_id = NULL;
8159 struct check_stage_ok_arg oka;
8160 struct stage_path_arg spa;
8162 err = lock_worktree(worktree, LOCK_EX);
8163 if (err)
8164 return err;
8166 err = got_ref_open(&head_ref, repo,
8167 got_worktree_get_head_ref_name(worktree), 0);
8168 if (err)
8169 goto done;
8170 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8171 if (err)
8172 goto done;
8173 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8174 if (err)
8175 goto done;
8177 /* Check pre-conditions before staging anything. */
8178 oka.head_commit_id = head_commit_id;
8179 oka.worktree = worktree;
8180 oka.fileindex = fileindex;
8181 oka.repo = repo;
8182 oka.have_changes = 0;
8183 TAILQ_FOREACH(pe, paths, entry) {
8184 err = worktree_status(worktree, pe->path, fileindex, repo,
8185 check_stage_ok, &oka, NULL, NULL, 1, 0);
8186 if (err)
8187 goto done;
8189 if (!oka.have_changes) {
8190 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8191 goto done;
8194 spa.worktree = worktree;
8195 spa.fileindex = fileindex;
8196 spa.repo = repo;
8197 spa.patch_cb = patch_cb;
8198 spa.patch_arg = patch_arg;
8199 spa.status_cb = status_cb;
8200 spa.status_arg = status_arg;
8201 spa.staged_something = 0;
8202 spa.allow_bad_symlinks = allow_bad_symlinks;
8203 TAILQ_FOREACH(pe, paths, entry) {
8204 err = worktree_status(worktree, pe->path, fileindex, repo,
8205 stage_path, &spa, NULL, NULL, 1, 0);
8206 if (err)
8207 goto done;
8209 if (!spa.staged_something) {
8210 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8211 goto done;
8214 sync_err = sync_fileindex(fileindex, fileindex_path);
8215 if (sync_err && err == NULL)
8216 err = sync_err;
8217 done:
8218 if (head_ref)
8219 got_ref_close(head_ref);
8220 free(head_commit_id);
8221 free(fileindex_path);
8222 if (fileindex)
8223 got_fileindex_free(fileindex);
8224 unlockerr = lock_worktree(worktree, LOCK_SH);
8225 if (unlockerr && err == NULL)
8226 err = unlockerr;
8227 return err;
8230 struct unstage_path_arg {
8231 struct got_worktree *worktree;
8232 struct got_fileindex *fileindex;
8233 struct got_repository *repo;
8234 got_worktree_checkout_cb progress_cb;
8235 void *progress_arg;
8236 got_worktree_patch_cb patch_cb;
8237 void *patch_arg;
8240 static const struct got_error *
8241 create_unstaged_content(char **path_unstaged_content,
8242 char **path_new_staged_content, struct got_object_id *blob_id,
8243 struct got_object_id *staged_blob_id, const char *relpath,
8244 struct got_repository *repo,
8245 got_worktree_patch_cb patch_cb, void *patch_arg)
8247 const struct got_error *err, *free_err;
8248 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8249 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8250 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8251 struct got_diffreg_result *diffreg_result = NULL;
8252 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8253 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8255 *path_unstaged_content = NULL;
8256 *path_new_staged_content = NULL;
8258 err = got_object_id_str(&label1, blob_id);
8259 if (err)
8260 return err;
8261 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8262 if (err)
8263 goto done;
8265 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8266 if (err)
8267 goto done;
8269 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8270 if (err)
8271 goto done;
8273 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8274 if (err)
8275 goto done;
8277 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8278 if (err)
8279 goto done;
8281 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8282 if (err)
8283 goto done;
8285 err = got_diff_files(&diffreg_result, f1, label1, f2,
8286 path2, 3, 0, 1, NULL);
8287 if (err)
8288 goto done;
8290 err = got_opentemp_named(path_unstaged_content, &outfile,
8291 "got-unstaged-content");
8292 if (err)
8293 goto done;
8294 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8295 "got-new-staged-content");
8296 if (err)
8297 goto done;
8299 if (fseek(f1, 0L, SEEK_SET) == -1) {
8300 err = got_ferror(f1, GOT_ERR_IO);
8301 goto done;
8303 if (fseek(f2, 0L, SEEK_SET) == -1) {
8304 err = got_ferror(f2, GOT_ERR_IO);
8305 goto done;
8307 /* Count the number of actual changes in the diff result. */
8308 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8309 struct diff_chunk_context cc = {};
8310 diff_chunk_context_load_change(&cc, &nchunks_used,
8311 diffreg_result->result, n, 0);
8312 nchanges++;
8314 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8315 int choice;
8316 err = apply_or_reject_change(&choice, &nchunks_used,
8317 diffreg_result->result, n, relpath, f1, f2,
8318 &line_cur1, &line_cur2,
8319 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8320 if (err)
8321 goto done;
8322 if (choice == GOT_PATCH_CHOICE_YES)
8323 have_content = 1;
8324 else
8325 have_rejected_content = 1;
8326 if (choice == GOT_PATCH_CHOICE_QUIT)
8327 break;
8329 if (have_content || have_rejected_content)
8330 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8331 outfile, rejectfile);
8332 done:
8333 free(label1);
8334 if (blob)
8335 got_object_blob_close(blob);
8336 if (staged_blob)
8337 got_object_blob_close(staged_blob);
8338 free_err = got_diffreg_result_free(diffreg_result);
8339 if (free_err && err == NULL)
8340 err = free_err;
8341 if (f1 && fclose(f1) == EOF && err == NULL)
8342 err = got_error_from_errno2("fclose", path1);
8343 if (f2 && fclose(f2) == EOF && err == NULL)
8344 err = got_error_from_errno2("fclose", path2);
8345 if (outfile && fclose(outfile) == EOF && err == NULL)
8346 err = got_error_from_errno2("fclose", *path_unstaged_content);
8347 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8348 err = got_error_from_errno2("fclose", *path_new_staged_content);
8349 if (path1 && unlink(path1) == -1 && err == NULL)
8350 err = got_error_from_errno2("unlink", path1);
8351 if (path2 && unlink(path2) == -1 && err == NULL)
8352 err = got_error_from_errno2("unlink", path2);
8353 if (err || !have_content) {
8354 if (*path_unstaged_content &&
8355 unlink(*path_unstaged_content) == -1 && err == NULL)
8356 err = got_error_from_errno2("unlink",
8357 *path_unstaged_content);
8358 free(*path_unstaged_content);
8359 *path_unstaged_content = NULL;
8361 if (err || !have_content || !have_rejected_content) {
8362 if (*path_new_staged_content &&
8363 unlink(*path_new_staged_content) == -1 && err == NULL)
8364 err = got_error_from_errno2("unlink",
8365 *path_new_staged_content);
8366 free(*path_new_staged_content);
8367 *path_new_staged_content = NULL;
8369 free(path1);
8370 free(path2);
8371 return err;
8374 static const struct got_error *
8375 unstage_hunks(struct got_object_id *staged_blob_id,
8376 struct got_blob_object *blob_base,
8377 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8378 const char *ondisk_path, const char *label_orig,
8379 struct got_worktree *worktree, struct got_repository *repo,
8380 got_worktree_patch_cb patch_cb, void *patch_arg,
8381 got_worktree_checkout_cb progress_cb, void *progress_arg)
8383 const struct got_error *err = NULL;
8384 char *path_unstaged_content = NULL;
8385 char *path_new_staged_content = NULL;
8386 char *parent = NULL, *base_path = NULL;
8387 char *blob_base_path = NULL;
8388 struct got_object_id *new_staged_blob_id = NULL;
8389 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8390 struct stat sb;
8392 err = create_unstaged_content(&path_unstaged_content,
8393 &path_new_staged_content, blob_id, staged_blob_id,
8394 ie->path, repo, patch_cb, patch_arg);
8395 if (err)
8396 return err;
8398 if (path_unstaged_content == NULL)
8399 return NULL;
8401 if (path_new_staged_content) {
8402 err = got_object_blob_create(&new_staged_blob_id,
8403 path_new_staged_content, repo);
8404 if (err)
8405 goto done;
8408 f = fopen(path_unstaged_content, "re");
8409 if (f == NULL) {
8410 err = got_error_from_errno2("fopen",
8411 path_unstaged_content);
8412 goto done;
8414 if (fstat(fileno(f), &sb) == -1) {
8415 err = got_error_from_errno2("fstat", path_unstaged_content);
8416 goto done;
8418 if (got_fileindex_entry_staged_filetype_get(ie) ==
8419 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8420 char link_target[PATH_MAX];
8421 size_t r;
8422 r = fread(link_target, 1, sizeof(link_target), f);
8423 if (r == 0 && ferror(f)) {
8424 err = got_error_from_errno("fread");
8425 goto done;
8427 if (r >= sizeof(link_target)) { /* should not happen */
8428 err = got_error(GOT_ERR_NO_SPACE);
8429 goto done;
8431 link_target[r] = '\0';
8432 err = merge_symlink(worktree, blob_base,
8433 ondisk_path, ie->path, label_orig, link_target,
8434 worktree->base_commit_id, repo, progress_cb,
8435 progress_arg);
8436 } else {
8437 int local_changes_subsumed;
8439 err = got_path_dirname(&parent, ondisk_path);
8440 if (err)
8441 return err;
8443 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8444 parent) == -1) {
8445 err = got_error_from_errno("asprintf");
8446 base_path = NULL;
8447 goto done;
8450 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8451 if (err)
8452 goto done;
8453 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8454 blob_base);
8455 if (err)
8456 goto done;
8459 * In order the run a 3-way merge with a symlink we copy the symlink's
8460 * target path into a temporary file and use that file with diff3.
8462 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8463 err = dump_symlink_target_path_to_file(&f_deriv2,
8464 ondisk_path);
8465 if (err)
8466 goto done;
8467 } else {
8468 int fd;
8469 fd = open(ondisk_path,
8470 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8471 if (fd == -1) {
8472 err = got_error_from_errno2("open", ondisk_path);
8473 goto done;
8475 f_deriv2 = fdopen(fd, "r");
8476 if (f_deriv2 == NULL) {
8477 err = got_error_from_errno2("fdopen", ondisk_path);
8478 close(fd);
8479 goto done;
8483 err = merge_file(&local_changes_subsumed, worktree,
8484 f_base, f, f_deriv2, ondisk_path, ie->path,
8485 got_fileindex_perms_to_st(ie),
8486 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8487 repo, progress_cb, progress_arg);
8489 if (err)
8490 goto done;
8492 if (new_staged_blob_id) {
8493 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8494 SHA1_DIGEST_LENGTH);
8495 } else {
8496 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8497 got_fileindex_entry_staged_filetype_set(ie, 0);
8499 done:
8500 free(new_staged_blob_id);
8501 if (path_unstaged_content &&
8502 unlink(path_unstaged_content) == -1 && err == NULL)
8503 err = got_error_from_errno2("unlink", path_unstaged_content);
8504 if (path_new_staged_content &&
8505 unlink(path_new_staged_content) == -1 && err == NULL)
8506 err = got_error_from_errno2("unlink", path_new_staged_content);
8507 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8508 err = got_error_from_errno2("unlink", blob_base_path);
8509 if (f_base && fclose(f_base) == EOF && err == NULL)
8510 err = got_error_from_errno2("fclose", path_unstaged_content);
8511 if (f && fclose(f) == EOF && err == NULL)
8512 err = got_error_from_errno2("fclose", path_unstaged_content);
8513 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8514 err = got_error_from_errno2("fclose", ondisk_path);
8515 free(path_unstaged_content);
8516 free(path_new_staged_content);
8517 free(blob_base_path);
8518 free(parent);
8519 free(base_path);
8520 return err;
8523 static const struct got_error *
8524 unstage_path(void *arg, unsigned char status,
8525 unsigned char staged_status, const char *relpath,
8526 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8527 struct got_object_id *commit_id, int dirfd, const char *de_name)
8529 const struct got_error *err = NULL;
8530 struct unstage_path_arg *a = arg;
8531 struct got_fileindex_entry *ie;
8532 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8533 char *ondisk_path = NULL;
8534 char *id_str = NULL, *label_orig = NULL;
8535 int local_changes_subsumed;
8536 struct stat sb;
8538 if (staged_status != GOT_STATUS_ADD &&
8539 staged_status != GOT_STATUS_MODIFY &&
8540 staged_status != GOT_STATUS_DELETE)
8541 return NULL;
8543 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8544 if (ie == NULL)
8545 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8547 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8548 == -1)
8549 return got_error_from_errno("asprintf");
8551 err = got_object_id_str(&id_str,
8552 commit_id ? commit_id : a->worktree->base_commit_id);
8553 if (err)
8554 goto done;
8555 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8556 id_str) == -1) {
8557 err = got_error_from_errno("asprintf");
8558 goto done;
8561 switch (staged_status) {
8562 case GOT_STATUS_MODIFY:
8563 err = got_object_open_as_blob(&blob_base, a->repo,
8564 blob_id, 8192);
8565 if (err)
8566 break;
8567 /* fall through */
8568 case GOT_STATUS_ADD:
8569 if (a->patch_cb) {
8570 if (staged_status == GOT_STATUS_ADD) {
8571 int choice = GOT_PATCH_CHOICE_NONE;
8572 err = (*a->patch_cb)(&choice, a->patch_arg,
8573 staged_status, ie->path, NULL, 1, 1);
8574 if (err)
8575 break;
8576 if (choice != GOT_PATCH_CHOICE_YES)
8577 break;
8578 } else {
8579 err = unstage_hunks(staged_blob_id,
8580 blob_base, blob_id, ie, ondisk_path,
8581 label_orig, a->worktree, a->repo,
8582 a->patch_cb, a->patch_arg,
8583 a->progress_cb, a->progress_arg);
8584 break; /* Done with this file. */
8587 err = got_object_open_as_blob(&blob_staged, a->repo,
8588 staged_blob_id, 8192);
8589 if (err)
8590 break;
8591 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8592 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8593 case GOT_FILEIDX_MODE_REGULAR_FILE:
8594 err = merge_blob(&local_changes_subsumed, a->worktree,
8595 blob_base, ondisk_path, relpath,
8596 got_fileindex_perms_to_st(ie), label_orig,
8597 blob_staged, commit_id ? commit_id :
8598 a->worktree->base_commit_id, a->repo,
8599 a->progress_cb, a->progress_arg);
8600 break;
8601 case GOT_FILEIDX_MODE_SYMLINK:
8602 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8603 char *staged_target;
8604 err = got_object_blob_read_to_str(
8605 &staged_target, blob_staged);
8606 if (err)
8607 goto done;
8608 err = merge_symlink(a->worktree, blob_base,
8609 ondisk_path, relpath, label_orig,
8610 staged_target, commit_id ? commit_id :
8611 a->worktree->base_commit_id,
8612 a->repo, a->progress_cb, a->progress_arg);
8613 free(staged_target);
8614 } else {
8615 err = merge_blob(&local_changes_subsumed,
8616 a->worktree, blob_base, ondisk_path,
8617 relpath, got_fileindex_perms_to_st(ie),
8618 label_orig, blob_staged,
8619 commit_id ? commit_id :
8620 a->worktree->base_commit_id, a->repo,
8621 a->progress_cb, a->progress_arg);
8623 break;
8624 default:
8625 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8626 break;
8628 if (err == NULL) {
8629 got_fileindex_entry_stage_set(ie,
8630 GOT_FILEIDX_STAGE_NONE);
8631 got_fileindex_entry_staged_filetype_set(ie, 0);
8633 break;
8634 case GOT_STATUS_DELETE:
8635 if (a->patch_cb) {
8636 int choice = GOT_PATCH_CHOICE_NONE;
8637 err = (*a->patch_cb)(&choice, a->patch_arg,
8638 staged_status, ie->path, NULL, 1, 1);
8639 if (err)
8640 break;
8641 if (choice == GOT_PATCH_CHOICE_NO)
8642 break;
8643 if (choice != GOT_PATCH_CHOICE_YES) {
8644 err = got_error(GOT_ERR_PATCH_CHOICE);
8645 break;
8648 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8649 got_fileindex_entry_staged_filetype_set(ie, 0);
8650 err = get_file_status(&status, &sb, ie, ondisk_path,
8651 dirfd, de_name, a->repo);
8652 if (err)
8653 break;
8654 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8655 break;
8657 done:
8658 free(ondisk_path);
8659 if (blob_base)
8660 got_object_blob_close(blob_base);
8661 if (blob_staged)
8662 got_object_blob_close(blob_staged);
8663 free(id_str);
8664 free(label_orig);
8665 return err;
8668 const struct got_error *
8669 got_worktree_unstage(struct got_worktree *worktree,
8670 struct got_pathlist_head *paths,
8671 got_worktree_checkout_cb progress_cb, void *progress_arg,
8672 got_worktree_patch_cb patch_cb, void *patch_arg,
8673 struct got_repository *repo)
8675 const struct got_error *err = NULL, *sync_err, *unlockerr;
8676 struct got_pathlist_entry *pe;
8677 struct got_fileindex *fileindex = NULL;
8678 char *fileindex_path = NULL;
8679 struct unstage_path_arg upa;
8681 err = lock_worktree(worktree, LOCK_EX);
8682 if (err)
8683 return err;
8685 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8686 if (err)
8687 goto done;
8689 upa.worktree = worktree;
8690 upa.fileindex = fileindex;
8691 upa.repo = repo;
8692 upa.progress_cb = progress_cb;
8693 upa.progress_arg = progress_arg;
8694 upa.patch_cb = patch_cb;
8695 upa.patch_arg = patch_arg;
8696 TAILQ_FOREACH(pe, paths, entry) {
8697 err = worktree_status(worktree, pe->path, fileindex, repo,
8698 unstage_path, &upa, NULL, NULL, 1, 0);
8699 if (err)
8700 goto done;
8703 sync_err = sync_fileindex(fileindex, fileindex_path);
8704 if (sync_err && err == NULL)
8705 err = sync_err;
8706 done:
8707 free(fileindex_path);
8708 if (fileindex)
8709 got_fileindex_free(fileindex);
8710 unlockerr = lock_worktree(worktree, LOCK_SH);
8711 if (unlockerr && err == NULL)
8712 err = unlockerr;
8713 return err;
8716 struct report_file_info_arg {
8717 struct got_worktree *worktree;
8718 got_worktree_path_info_cb info_cb;
8719 void *info_arg;
8720 struct got_pathlist_head *paths;
8721 got_cancel_cb cancel_cb;
8722 void *cancel_arg;
8725 static const struct got_error *
8726 report_file_info(void *arg, struct got_fileindex_entry *ie)
8728 struct report_file_info_arg *a = arg;
8729 struct got_pathlist_entry *pe;
8730 struct got_object_id blob_id, staged_blob_id, commit_id;
8731 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8732 struct got_object_id *commit_idp = NULL;
8733 int stage;
8735 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8736 return got_error(GOT_ERR_CANCELLED);
8738 TAILQ_FOREACH(pe, a->paths, entry) {
8739 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8740 got_path_is_child(ie->path, pe->path, pe->path_len))
8741 break;
8743 if (pe == NULL) /* not found */
8744 return NULL;
8746 if (got_fileindex_entry_has_blob(ie)) {
8747 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8748 blob_idp = &blob_id;
8750 stage = got_fileindex_entry_stage_get(ie);
8751 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8752 stage == GOT_FILEIDX_STAGE_ADD) {
8753 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8754 SHA1_DIGEST_LENGTH);
8755 staged_blob_idp = &staged_blob_id;
8758 if (got_fileindex_entry_has_commit(ie)) {
8759 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8760 commit_idp = &commit_id;
8763 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8764 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8767 const struct got_error *
8768 got_worktree_path_info(struct got_worktree *worktree,
8769 struct got_pathlist_head *paths,
8770 got_worktree_path_info_cb info_cb, void *info_arg,
8771 got_cancel_cb cancel_cb, void *cancel_arg)
8774 const struct got_error *err = NULL, *unlockerr;
8775 struct got_fileindex *fileindex = NULL;
8776 char *fileindex_path = NULL;
8777 struct report_file_info_arg arg;
8779 err = lock_worktree(worktree, LOCK_SH);
8780 if (err)
8781 return err;
8783 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8784 if (err)
8785 goto done;
8787 arg.worktree = worktree;
8788 arg.info_cb = info_cb;
8789 arg.info_arg = info_arg;
8790 arg.paths = paths;
8791 arg.cancel_cb = cancel_cb;
8792 arg.cancel_arg = cancel_arg;
8793 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8794 &arg);
8795 done:
8796 free(fileindex_path);
8797 if (fileindex)
8798 got_fileindex_free(fileindex);
8799 unlockerr = lock_worktree(worktree, LOCK_UN);
8800 if (unlockerr && err == NULL)
8801 err = unlockerr;
8802 return err;
8805 static const struct got_error *
8806 patch_check_path(const char *p, char **path, unsigned char *status,
8807 unsigned char *staged_status, struct got_fileindex *fileindex,
8808 struct got_worktree *worktree, struct got_repository *repo)
8810 const struct got_error *err;
8811 struct got_fileindex_entry *ie;
8812 struct stat sb;
8813 char *ondisk_path = NULL;
8815 err = got_worktree_resolve_path(path, worktree, p);
8816 if (err)
8817 return err;
8819 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8820 *path[0] ? "/" : "", *path) == -1)
8821 return got_error_from_errno("asprintf");
8823 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8824 if (ie) {
8825 *staged_status = get_staged_status(ie);
8826 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8827 repo);
8828 if (err)
8829 goto done;
8830 } else {
8831 *staged_status = GOT_STATUS_NO_CHANGE;
8832 *status = GOT_STATUS_UNVERSIONED;
8833 if (lstat(ondisk_path, &sb) == -1) {
8834 if (errno != ENOENT) {
8835 err = got_error_from_errno2("lstat",
8836 ondisk_path);
8837 goto done;
8839 *status = GOT_STATUS_NONEXISTENT;
8843 done:
8844 free(ondisk_path);
8845 return err;
8848 static const struct got_error *
8849 patch_can_rm(const char *path, unsigned char status,
8850 unsigned char staged_status)
8852 if (status == GOT_STATUS_NONEXISTENT)
8853 return got_error_set_errno(ENOENT, path);
8854 if (status != GOT_STATUS_NO_CHANGE &&
8855 status != GOT_STATUS_ADD &&
8856 status != GOT_STATUS_MODIFY &&
8857 status != GOT_STATUS_MODE_CHANGE)
8858 return got_error_path(path, GOT_ERR_FILE_STATUS);
8859 if (staged_status == GOT_STATUS_DELETE)
8860 return got_error_path(path, GOT_ERR_FILE_STATUS);
8861 return NULL;
8864 static const struct got_error *
8865 patch_can_add(const char *path, unsigned char status)
8867 if (status != GOT_STATUS_NONEXISTENT)
8868 return got_error_path(path, GOT_ERR_FILE_STATUS);
8869 return NULL;
8872 static const struct got_error *
8873 patch_can_edit(const char *path, unsigned char status,
8874 unsigned char staged_status)
8876 if (status == GOT_STATUS_NONEXISTENT)
8877 return got_error_set_errno(ENOENT, path);
8878 if (status != GOT_STATUS_NO_CHANGE &&
8879 status != GOT_STATUS_ADD &&
8880 status != GOT_STATUS_MODIFY)
8881 return got_error_path(path, GOT_ERR_FILE_STATUS);
8882 if (staged_status == GOT_STATUS_DELETE)
8883 return got_error_path(path, GOT_ERR_FILE_STATUS);
8884 return NULL;
8887 const struct got_error *
8888 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8889 char **fileindex_path, struct got_worktree *worktree)
8891 return open_fileindex(fileindex, fileindex_path, worktree);
8894 const struct got_error *
8895 got_worktree_patch_check_path(const char *old, const char *new,
8896 char **oldpath, char **newpath, struct got_worktree *worktree,
8897 struct got_repository *repo, struct got_fileindex *fileindex)
8899 const struct got_error *err = NULL;
8900 int file_renamed = 0;
8901 unsigned char status_old, staged_status_old;
8902 unsigned char status_new, staged_status_new;
8904 *oldpath = NULL;
8905 *newpath = NULL;
8907 err = patch_check_path(old != NULL ? old : new, oldpath,
8908 &status_old, &staged_status_old, fileindex, worktree, repo);
8909 if (err)
8910 goto done;
8912 err = patch_check_path(new != NULL ? new : old, newpath,
8913 &status_new, &staged_status_new, fileindex, worktree, repo);
8914 if (err)
8915 goto done;
8917 if (old != NULL && new != NULL && strcmp(old, new) != 0)
8918 file_renamed = 1;
8920 if (old != NULL && new == NULL)
8921 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8922 else if (file_renamed) {
8923 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8924 if (err == NULL)
8925 err = patch_can_add(*newpath, status_new);
8926 } else if (old == NULL)
8927 err = patch_can_add(*newpath, status_new);
8928 else
8929 err = patch_can_edit(*newpath, status_new, staged_status_new);
8931 done:
8932 if (err) {
8933 free(*oldpath);
8934 *oldpath = NULL;
8935 free(*newpath);
8936 *newpath = NULL;
8938 return err;
8941 const struct got_error *
8942 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
8943 struct got_worktree *worktree, struct got_fileindex *fileindex,
8944 got_worktree_checkout_cb progress_cb, void *progress_arg)
8946 struct schedule_addition_args saa;
8948 memset(&saa, 0, sizeof(saa));
8949 saa.worktree = worktree;
8950 saa.fileindex = fileindex;
8951 saa.progress_cb = progress_cb;
8952 saa.progress_arg = progress_arg;
8953 saa.repo = repo;
8955 return worktree_status(worktree, path, fileindex, repo,
8956 schedule_addition, &saa, NULL, NULL, 1, 0);
8959 const struct got_error *
8960 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
8961 struct got_worktree *worktree, struct got_fileindex *fileindex,
8962 got_worktree_delete_cb progress_cb, void *progress_arg)
8964 struct schedule_deletion_args sda;
8966 memset(&sda, 0, sizeof(sda));
8967 sda.worktree = worktree;
8968 sda.fileindex = fileindex;
8969 sda.progress_cb = progress_cb;
8970 sda.progress_arg = progress_arg;
8971 sda.repo = repo;
8972 sda.delete_local_mods = 0;
8973 sda.keep_on_disk = 0;
8974 sda.ignore_missing_paths = 0;
8975 sda.status_codes = NULL;
8977 return worktree_status(worktree, path, fileindex, repo,
8978 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
8981 const struct got_error *
8982 got_worktree_patch_complete(struct got_fileindex *fileindex,
8983 const char *fileindex_path)
8985 const struct got_error *err = NULL;
8987 err = sync_fileindex(fileindex, fileindex_path);
8988 got_fileindex_free(fileindex);
8990 return err;