Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
20 #include <dirent.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha2.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
35 #include "got_compat.h"
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_opentemp.h"
45 #include "got_diff.h"
47 #include "got_lib_worktree.h"
48 #include "got_lib_hash.h"
49 #include "got_lib_fileindex.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_object_idset.h"
56 #include "got_lib_diff.h"
57 #include "got_lib_gotconfig.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static mode_t apply_umask(mode_t);
68 static const struct got_error *
69 create_meta_file(const char *path_got, const char *name, const char *content)
70 {
71 const struct got_error *err = NULL;
72 char *path;
74 if (asprintf(&path, "%s/%s", path_got, name) == -1)
75 return got_error_from_errno("asprintf");
77 err = got_path_create_file(path, content);
78 free(path);
79 return err;
80 }
82 static const struct got_error *
83 update_meta_file(const char *path_got, const char *name, const char *content)
84 {
85 const struct got_error *err = NULL;
86 FILE *tmpfile = NULL;
87 char *tmppath = NULL;
88 char *path = NULL;
90 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
91 err = got_error_from_errno("asprintf");
92 path = NULL;
93 goto done;
94 }
96 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
97 if (err)
98 goto done;
100 if (content) {
101 int len = fprintf(tmpfile, "%s\n", content);
102 if (len != strlen(content) + 1) {
103 err = got_error_from_errno2("fprintf", tmppath);
104 goto done;
108 if (rename(tmppath, path) != 0) {
109 err = got_error_from_errno3("rename", tmppath, path);
110 unlink(tmppath);
111 goto done;
114 done:
115 if (fclose(tmpfile) == EOF && err == NULL)
116 err = got_error_from_errno2("fclose", tmppath);
117 free(tmppath);
118 return err;
121 static const struct got_error *
122 write_head_ref(const char *path_got, struct got_reference *head_ref)
124 const struct got_error *err = NULL;
125 char *refstr = NULL;
127 if (got_ref_is_symbolic(head_ref)) {
128 refstr = got_ref_to_str(head_ref);
129 if (refstr == NULL)
130 return got_error_from_errno("got_ref_to_str");
131 } else {
132 refstr = strdup(got_ref_get_name(head_ref));
133 if (refstr == NULL)
134 return got_error_from_errno("strdup");
136 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
137 free(refstr);
138 return err;
141 const struct got_error *
142 got_worktree_init(const char *path, struct got_reference *head_ref,
143 const char *prefix, struct got_repository *repo)
145 const struct got_error *err = NULL;
146 struct got_object_id *commit_id = NULL;
147 uuid_t uuid;
148 uint32_t uuid_status;
149 int obj_type;
150 char *path_got = NULL;
151 char *formatstr = NULL;
152 char *absprefix = NULL;
153 char *basestr = NULL;
154 char *uuidstr = NULL;
156 if (strcmp(path, got_repo_get_path(repo)) == 0) {
157 err = got_error(GOT_ERR_WORKTREE_REPO);
158 goto done;
161 err = got_ref_resolve(&commit_id, repo, head_ref);
162 if (err)
163 return err;
164 err = got_object_get_type(&obj_type, repo, commit_id);
165 if (err)
166 return err;
167 if (obj_type != GOT_OBJ_TYPE_COMMIT)
168 return got_error(GOT_ERR_OBJ_TYPE);
170 if (!got_path_is_absolute(prefix)) {
171 if (asprintf(&absprefix, "/%s", prefix) == -1)
172 return got_error_from_errno("asprintf");
175 /* Create top-level directory (may already exist). */
176 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
177 err = got_error_from_errno2("mkdir", path);
178 goto done;
181 /* Create .got directory (may already exist). */
182 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
183 err = got_error_from_errno("asprintf");
184 goto done;
186 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 err = got_error_from_errno2("mkdir", path_got);
188 goto done;
191 /* Create an empty lock file. */
192 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
193 if (err)
194 goto done;
196 /* Create an empty file index. */
197 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
198 if (err)
199 goto done;
201 /* Write the HEAD reference. */
202 err = write_head_ref(path_got, head_ref);
203 if (err)
204 goto done;
206 /* Record our base commit. */
207 err = got_object_id_str(&basestr, commit_id);
208 if (err)
209 goto done;
210 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
211 if (err)
212 goto done;
214 /* Store path to repository. */
215 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
216 got_repo_get_path(repo));
217 if (err)
218 goto done;
220 /* Store in-repository path prefix. */
221 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
222 absprefix ? absprefix : prefix);
223 if (err)
224 goto done;
226 /* Generate UUID. */
227 uuid_create(&uuid, &uuid_status);
228 if (uuid_status != uuid_s_ok) {
229 err = got_error_uuid(uuid_status, "uuid_create");
230 goto done;
232 uuid_to_string(&uuid, &uuidstr, &uuid_status);
233 if (uuid_status != uuid_s_ok) {
234 err = got_error_uuid(uuid_status, "uuid_to_string");
235 goto done;
237 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
238 if (err)
239 goto done;
241 /* Stamp work tree with format file. */
242 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
243 err = got_error_from_errno("asprintf");
244 goto done;
246 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
247 if (err)
248 goto done;
250 done:
251 free(commit_id);
252 free(path_got);
253 free(formatstr);
254 free(absprefix);
255 free(basestr);
256 free(uuidstr);
257 return err;
260 const struct got_error *
261 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
262 const char *path_prefix)
264 char *absprefix = NULL;
266 if (!got_path_is_absolute(path_prefix)) {
267 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
268 return got_error_from_errno("asprintf");
270 *match = (strcmp(absprefix ? absprefix : path_prefix,
271 worktree->path_prefix) == 0);
272 free(absprefix);
273 return NULL;
276 const char *
277 got_worktree_get_head_ref_name(struct got_worktree *worktree)
279 return worktree->head_ref_name;
282 const struct got_error *
283 got_worktree_set_head_ref(struct got_worktree *worktree,
284 struct got_reference *head_ref)
286 const struct got_error *err = NULL;
287 char *path_got = NULL, *head_ref_name = NULL;
289 if (asprintf(&path_got, "%s/%s", worktree->root_path,
290 GOT_WORKTREE_GOT_DIR) == -1) {
291 err = got_error_from_errno("asprintf");
292 path_got = NULL;
293 goto done;
296 head_ref_name = strdup(got_ref_get_name(head_ref));
297 if (head_ref_name == NULL) {
298 err = got_error_from_errno("strdup");
299 goto done;
302 err = write_head_ref(path_got, head_ref);
303 if (err)
304 goto done;
306 free(worktree->head_ref_name);
307 worktree->head_ref_name = head_ref_name;
308 done:
309 free(path_got);
310 if (err)
311 free(head_ref_name);
312 return err;
315 struct got_object_id *
316 got_worktree_get_base_commit_id(struct got_worktree *worktree)
318 return worktree->base_commit_id;
321 const struct got_error *
322 got_worktree_set_base_commit_id(struct got_worktree *worktree,
323 struct got_repository *repo, struct got_object_id *commit_id)
325 const struct got_error *err;
326 struct got_object *obj = NULL;
327 char *id_str = NULL;
328 char *path_got = NULL;
330 if (asprintf(&path_got, "%s/%s", worktree->root_path,
331 GOT_WORKTREE_GOT_DIR) == -1) {
332 err = got_error_from_errno("asprintf");
333 path_got = NULL;
334 goto done;
337 err = got_object_open(&obj, repo, commit_id);
338 if (err)
339 return err;
341 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
342 err = got_error(GOT_ERR_OBJ_TYPE);
343 goto done;
346 /* Record our base commit. */
347 err = got_object_id_str(&id_str, commit_id);
348 if (err)
349 goto done;
350 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
351 if (err)
352 goto done;
354 free(worktree->base_commit_id);
355 worktree->base_commit_id = got_object_id_dup(commit_id);
356 if (worktree->base_commit_id == NULL) {
357 err = got_error_from_errno("got_object_id_dup");
358 goto done;
360 done:
361 if (obj)
362 got_object_close(obj);
363 free(id_str);
364 free(path_got);
365 return err;
368 const struct got_gotconfig *
369 got_worktree_get_gotconfig(struct got_worktree *worktree)
371 return worktree->gotconfig;
374 static const struct got_error *
375 lock_worktree(struct got_worktree *worktree, int operation)
377 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
378 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
379 : got_error_from_errno2("flock",
380 got_worktree_get_root_path(worktree)));
381 return NULL;
384 static const struct got_error *
385 add_dir_on_disk(struct got_worktree *worktree, const char *path)
387 const struct got_error *err = NULL;
388 char *abspath;
390 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
391 return got_error_from_errno("asprintf");
393 err = got_path_mkdir(abspath);
394 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
395 struct stat sb;
396 err = NULL;
397 if (lstat(abspath, &sb) == -1) {
398 err = got_error_from_errno2("lstat", abspath);
399 } else if (!S_ISDIR(sb.st_mode)) {
400 /* TODO directory is obstructed; do something */
401 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
404 free(abspath);
405 return err;
408 static const struct got_error *
409 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
411 const struct got_error *err = NULL;
412 uint8_t fbuf1[8192];
413 uint8_t fbuf2[8192];
414 size_t flen1 = 0, flen2 = 0;
416 *same = 1;
418 for (;;) {
419 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
420 if (flen1 == 0 && ferror(f1)) {
421 err = got_error_from_errno("fread");
422 break;
424 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
425 if (flen2 == 0 && ferror(f2)) {
426 err = got_error_from_errno("fread");
427 break;
429 if (flen1 == 0) {
430 if (flen2 != 0)
431 *same = 0;
432 break;
433 } else if (flen2 == 0) {
434 if (flen1 != 0)
435 *same = 0;
436 break;
437 } else if (flen1 == flen2) {
438 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
439 *same = 0;
440 break;
442 } else {
443 *same = 0;
444 break;
448 return err;
451 static const struct got_error *
452 check_files_equal(int *same, FILE *f1, FILE *f2)
454 struct stat sb;
455 size_t size1, size2;
457 *same = 1;
459 if (fstat(fileno(f1), &sb) != 0)
460 return got_error_from_errno("fstat");
461 size1 = sb.st_size;
463 if (fstat(fileno(f2), &sb) != 0)
464 return got_error_from_errno("fstat");
465 size2 = sb.st_size;
467 if (size1 != size2) {
468 *same = 0;
469 return NULL;
472 if (fseek(f1, 0L, SEEK_SET) == -1)
473 return got_ferror(f1, GOT_ERR_IO);
474 if (fseek(f2, 0L, SEEK_SET) == -1)
475 return got_ferror(f2, GOT_ERR_IO);
477 return check_file_contents_equal(same, f1, f2);
480 static const struct got_error *
481 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
483 uint8_t fbuf[65536];
484 size_t flen;
485 ssize_t outlen;
487 *outsize = 0;
489 if (fseek(f, 0L, SEEK_SET) == -1)
490 return got_ferror(f, GOT_ERR_IO);
492 for (;;) {
493 flen = fread(fbuf, 1, sizeof(fbuf), f);
494 if (flen == 0) {
495 if (ferror(f))
496 return got_error_from_errno("fread");
497 if (feof(f))
498 break;
500 outlen = write(outfd, fbuf, flen);
501 if (outlen == -1)
502 return got_error_from_errno("write");
503 if (outlen != flen)
504 return got_error(GOT_ERR_IO);
505 *outsize += outlen;
508 return NULL;
511 static const struct got_error *
512 merge_binary_file(int *overlapcnt, int merged_fd,
513 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
514 const char *label_deriv, const char *label_orig, const char *label_deriv2,
515 const char *ondisk_path)
517 const struct got_error *err = NULL;
518 int same_content, changed_deriv, changed_deriv2;
519 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
520 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
521 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
522 char *base_path_orig = NULL, *base_path_deriv = NULL;
523 char *base_path_deriv2 = NULL;
525 *overlapcnt = 0;
527 err = check_files_equal(&same_content, f_deriv, f_deriv2);
528 if (err)
529 return err;
531 if (same_content)
532 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
534 err = check_files_equal(&same_content, f_deriv, f_orig);
535 if (err)
536 return err;
537 changed_deriv = !same_content;
538 err = check_files_equal(&same_content, f_deriv2, f_orig);
539 if (err)
540 return err;
541 changed_deriv2 = !same_content;
543 if (changed_deriv && changed_deriv2) {
544 *overlapcnt = 1;
545 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
546 err = got_error_from_errno("asprintf");
547 goto done;
549 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
551 goto done;
553 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
554 err = got_error_from_errno("asprintf");
555 goto done;
557 err = got_opentemp_named_fd(&path_orig, &fd_orig,
558 base_path_orig, "");
559 if (err)
560 goto done;
561 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
562 base_path_deriv, "");
563 if (err)
564 goto done;
565 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
566 base_path_deriv2, "");
567 if (err)
568 goto done;
569 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
570 if (err)
571 goto done;
572 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
573 if (err)
574 goto done;
575 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
576 if (err)
577 goto done;
578 if (dprintf(merged_fd, "Binary files differ and cannot be "
579 "merged automatically:\n") < 0) {
580 err = got_error_from_errno("dprintf");
581 goto done;
583 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
584 GOT_DIFF_CONFLICT_MARKER_BEGIN,
585 label_deriv ? " " : "",
586 label_deriv ? label_deriv : "",
587 path_deriv) < 0) {
588 err = got_error_from_errno("dprintf");
589 goto done;
591 if (size_orig > 0) {
592 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
593 GOT_DIFF_CONFLICT_MARKER_ORIG,
594 label_orig ? " " : "",
595 label_orig ? label_orig : "",
596 path_orig) < 0) {
597 err = got_error_from_errno("dprintf");
598 goto done;
601 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
602 GOT_DIFF_CONFLICT_MARKER_SEP,
603 path_deriv2,
604 GOT_DIFF_CONFLICT_MARKER_END,
605 label_deriv2 ? " " : "",
606 label_deriv2 ? label_deriv2 : "") < 0) {
607 err = got_error_from_errno("dprintf");
608 goto done;
610 } else if (changed_deriv)
611 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
612 else if (changed_deriv2)
613 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
614 done:
615 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
616 err == NULL)
617 err = got_error_from_errno2("unlink", path_orig);
618 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
619 err = got_error_from_errno2("close", path_orig);
620 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
621 err = got_error_from_errno2("close", path_deriv);
622 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_deriv2);
624 free(path_orig);
625 free(path_deriv);
626 free(path_deriv2);
627 free(base_path_orig);
628 free(base_path_deriv);
629 free(base_path_deriv2);
630 return err;
633 /*
634 * Perform a 3-way merge where the file f_orig acts as the common
635 * ancestor, the file f_deriv acts as the first derived version,
636 * and the file f_deriv2 acts as the second derived version.
637 * The merge result will be written to a new file at ondisk_path; any
638 * existing file at this path will be replaced.
639 */
640 static const struct got_error *
641 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
642 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
643 const char *path, uint16_t st_mode,
644 const char *label_orig, const char *label_deriv, const char *label_deriv2,
645 enum got_diff_algorithm diff_algo, struct got_repository *repo,
646 got_worktree_checkout_cb progress_cb, void *progress_arg)
648 const struct got_error *err = NULL;
649 int merged_fd = -1;
650 FILE *f_merged = NULL;
651 char *merged_path = NULL, *base_path = NULL;
652 int overlapcnt = 0;
653 char *parent = NULL;
655 *local_changes_subsumed = 0;
657 err = got_path_dirname(&parent, ondisk_path);
658 if (err)
659 return err;
661 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
662 err = got_error_from_errno("asprintf");
663 goto done;
666 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
667 if (err)
668 goto done;
670 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
671 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
672 if (err) {
673 if (err->code != GOT_ERR_FILE_BINARY)
674 goto done;
675 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
676 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
677 ondisk_path);
678 if (err)
679 goto done;
682 err = (*progress_cb)(progress_arg,
683 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
684 if (err)
685 goto done;
687 if (fsync(merged_fd) != 0) {
688 err = got_error_from_errno("fsync");
689 goto done;
692 f_merged = fdopen(merged_fd, "r");
693 if (f_merged == NULL) {
694 err = got_error_from_errno("fdopen");
695 goto done;
697 merged_fd = -1;
699 /* Check if a clean merge has subsumed all local changes. */
700 if (overlapcnt == 0) {
701 err = check_files_equal(local_changes_subsumed, f_deriv,
702 f_merged);
703 if (err)
704 goto done;
707 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
708 err = got_error_from_errno2("fchmod", merged_path);
709 goto done;
712 if (rename(merged_path, ondisk_path) != 0) {
713 err = got_error_from_errno3("rename", merged_path,
714 ondisk_path);
715 goto done;
717 done:
718 if (err) {
719 if (merged_path)
720 unlink(merged_path);
722 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
723 err = got_error_from_errno("close");
724 if (f_merged && fclose(f_merged) == EOF && err == NULL)
725 err = got_error_from_errno("fclose");
726 free(merged_path);
727 free(base_path);
728 free(parent);
729 return err;
732 static const struct got_error *
733 update_symlink(const char *ondisk_path, const char *target_path,
734 size_t target_len)
736 /* This is not atomic but matches what 'ln -sf' does. */
737 if (unlink(ondisk_path) == -1)
738 return got_error_from_errno2("unlink", ondisk_path);
739 if (symlink(target_path, ondisk_path) == -1)
740 return got_error_from_errno3("symlink", target_path,
741 ondisk_path);
742 return NULL;
745 /*
746 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
747 * in the work tree with a file that contains conflict markers and the
748 * conflicting target paths of the original version, a "derived version"
749 * of a symlink from an incoming change, and a local version of the symlink.
751 * The original versions's target path can be NULL if it is not available,
752 * such as if both derived versions added a new symlink at the same path.
754 * The incoming derived symlink target is NULL in case the incoming change
755 * has deleted this symlink.
756 */
757 static const struct got_error *
758 install_symlink_conflict(const char *deriv_target,
759 struct got_object_id *deriv_base_commit_id, const char *orig_target,
760 const char *label_orig, const char *local_target, const char *ondisk_path)
762 const struct got_error *err;
763 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
764 FILE *f = NULL;
766 err = got_object_id_str(&id_str, deriv_base_commit_id);
767 if (err)
768 return got_error_from_errno("asprintf");
770 if (asprintf(&label_deriv, "%s: commit %s",
771 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
772 err = got_error_from_errno("asprintf");
773 goto done;
776 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
777 if (err)
778 goto done;
780 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
781 err = got_error_from_errno2("fchmod", path);
782 goto done;
785 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
786 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
787 deriv_target ? deriv_target : "(symlink was deleted)",
788 orig_target ? label_orig : "",
789 orig_target ? "\n" : "",
790 orig_target ? orig_target : "",
791 orig_target ? "\n" : "",
792 GOT_DIFF_CONFLICT_MARKER_SEP,
793 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
794 err = got_error_from_errno2("fprintf", path);
795 goto done;
798 if (unlink(ondisk_path) == -1) {
799 err = got_error_from_errno2("unlink", ondisk_path);
800 goto done;
802 if (rename(path, ondisk_path) == -1) {
803 err = got_error_from_errno3("rename", path, ondisk_path);
804 goto done;
806 done:
807 if (f != NULL && fclose(f) == EOF && err == NULL)
808 err = got_error_from_errno2("fclose", path);
809 free(path);
810 free(id_str);
811 free(label_deriv);
812 return err;
815 /* forward declaration */
816 static const struct got_error *
817 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
818 const char *, const char *, uint16_t, const char *,
819 struct got_blob_object *, struct got_object_id *,
820 struct got_repository *, got_worktree_checkout_cb, void *);
822 /*
823 * Merge a symlink into the work tree, where blob_orig acts as the common
824 * ancestor, deriv_target is the link target of the first derived version,
825 * and the symlink on disk acts as the second derived version.
826 * Assume that contents of both blobs represent symlinks.
827 */
828 static const struct got_error *
829 merge_symlink(struct got_worktree *worktree,
830 struct got_blob_object *blob_orig, const char *ondisk_path,
831 const char *path, const char *label_orig, const char *deriv_target,
832 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
833 got_worktree_checkout_cb progress_cb, void *progress_arg)
835 const struct got_error *err = NULL;
836 char *ancestor_target = NULL;
837 struct stat sb;
838 ssize_t ondisk_len, deriv_len;
839 char ondisk_target[PATH_MAX];
840 int have_local_change = 0;
841 int have_incoming_change = 0;
843 if (lstat(ondisk_path, &sb) == -1)
844 return got_error_from_errno2("lstat", ondisk_path);
846 ondisk_len = readlink(ondisk_path, ondisk_target,
847 sizeof(ondisk_target));
848 if (ondisk_len == -1) {
849 err = got_error_from_errno2("readlink",
850 ondisk_path);
851 goto done;
853 ondisk_target[ondisk_len] = '\0';
855 if (blob_orig) {
856 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
857 if (err)
858 goto done;
861 if (ancestor_target == NULL ||
862 (ondisk_len != strlen(ancestor_target) ||
863 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
864 have_local_change = 1;
866 deriv_len = strlen(deriv_target);
867 if (ancestor_target == NULL ||
868 (deriv_len != strlen(ancestor_target) ||
869 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
870 have_incoming_change = 1;
872 if (!have_local_change && !have_incoming_change) {
873 if (ancestor_target) {
874 /* Both sides made the same change. */
875 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
876 path);
877 } else if (deriv_len == ondisk_len &&
878 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
879 /* Both sides added the same symlink. */
880 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
881 path);
882 } else {
883 /* Both sides added symlinks which don't match. */
884 err = install_symlink_conflict(deriv_target,
885 deriv_base_commit_id, ancestor_target,
886 label_orig, ondisk_target, ondisk_path);
887 if (err)
888 goto done;
889 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
890 path);
892 } else if (!have_local_change && have_incoming_change) {
893 /* Apply the incoming change. */
894 err = update_symlink(ondisk_path, deriv_target,
895 strlen(deriv_target));
896 if (err)
897 goto done;
898 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
899 } else if (have_local_change && have_incoming_change) {
900 if (deriv_len == ondisk_len &&
901 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
902 /* Both sides made the same change. */
903 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
904 path);
905 } else {
906 err = install_symlink_conflict(deriv_target,
907 deriv_base_commit_id, ancestor_target, label_orig,
908 ondisk_target, ondisk_path);
909 if (err)
910 goto done;
911 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
912 path);
916 done:
917 free(ancestor_target);
918 return err;
921 static const struct got_error *
922 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
924 const struct got_error *err = NULL;
925 char target_path[PATH_MAX];
926 ssize_t target_len;
927 size_t n;
928 FILE *f;
930 *outfile = NULL;
932 f = got_opentemp();
933 if (f == NULL)
934 return got_error_from_errno("got_opentemp");
935 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
936 if (target_len == -1) {
937 err = got_error_from_errno2("readlink", ondisk_path);
938 goto done;
940 n = fwrite(target_path, 1, target_len, f);
941 if (n != target_len) {
942 err = got_ferror(f, GOT_ERR_IO);
943 goto done;
945 if (fflush(f) == EOF) {
946 err = got_error_from_errno("fflush");
947 goto done;
949 if (fseek(f, 0L, SEEK_SET) == -1) {
950 err = got_ferror(f, GOT_ERR_IO);
951 goto done;
953 done:
954 if (err)
955 fclose(f);
956 else
957 *outfile = f;
958 return err;
961 /*
962 * Perform a 3-way merge where blob_orig acts as the common ancestor,
963 * blob_deriv acts as the first derived version, and the file on disk
964 * acts as the second derived version.
965 */
966 static const struct got_error *
967 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
968 struct got_blob_object *blob_orig, const char *ondisk_path,
969 const char *path, uint16_t st_mode, const char *label_orig,
970 struct got_blob_object *blob_deriv,
971 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
972 got_worktree_checkout_cb progress_cb, void *progress_arg)
974 const struct got_error *err = NULL;
975 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
976 char *blob_orig_path = NULL;
977 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
978 char *label_deriv = NULL, *parent = NULL;
980 *local_changes_subsumed = 0;
982 err = got_path_dirname(&parent, ondisk_path);
983 if (err)
984 return err;
986 if (blob_orig) {
987 if (asprintf(&base_path, "%s/got-merge-blob-orig",
988 parent) == -1) {
989 err = got_error_from_errno("asprintf");
990 base_path = NULL;
991 goto done;
994 err = got_opentemp_named(&blob_orig_path, &f_orig,
995 base_path, "");
996 if (err)
997 goto done;
998 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
999 blob_orig);
1000 if (err)
1001 goto done;
1002 free(base_path);
1003 } else {
1005 * No common ancestor exists. This is an "add vs add" conflict
1006 * and we simply use an empty ancestor file to make both files
1007 * appear in the merged result in their entirety.
1009 f_orig = got_opentemp();
1010 if (f_orig == NULL) {
1011 err = got_error_from_errno("got_opentemp");
1012 goto done;
1016 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1017 err = got_error_from_errno("asprintf");
1018 base_path = NULL;
1019 goto done;
1022 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1023 if (err)
1024 goto done;
1025 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1026 blob_deriv);
1027 if (err)
1028 goto done;
1030 err = got_object_id_str(&id_str, deriv_base_commit_id);
1031 if (err)
1032 goto done;
1033 if (asprintf(&label_deriv, "%s: commit %s",
1034 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1035 err = got_error_from_errno("asprintf");
1036 goto done;
1040 * In order the run a 3-way merge with a symlink we copy the symlink's
1041 * target path into a temporary file and use that file with diff3.
1043 if (S_ISLNK(st_mode)) {
1044 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1045 if (err)
1046 goto done;
1047 } else {
1048 int fd;
1049 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1050 if (fd == -1) {
1051 err = got_error_from_errno2("open", ondisk_path);
1052 goto done;
1054 f_deriv2 = fdopen(fd, "r");
1055 if (f_deriv2 == NULL) {
1056 err = got_error_from_errno2("fdopen", ondisk_path);
1057 close(fd);
1058 goto done;
1062 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1063 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1064 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1065 done:
1066 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 free(base_path);
1073 if (blob_orig_path) {
1074 unlink(blob_orig_path);
1075 free(blob_orig_path);
1077 if (blob_deriv_path) {
1078 unlink(blob_deriv_path);
1079 free(blob_deriv_path);
1081 free(id_str);
1082 free(label_deriv);
1083 free(parent);
1084 return err;
1087 static const struct got_error *
1088 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1089 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1090 int wt_fd, const char *path, struct got_object_id *blob_id)
1092 const struct got_error *err = NULL;
1093 struct got_fileindex_entry *new_ie;
1095 *new_iep = NULL;
1097 err = got_fileindex_entry_alloc(&new_ie, path);
1098 if (err)
1099 return err;
1101 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1102 blob_id->sha1, base_commit_id->sha1, 1);
1103 if (err)
1104 goto done;
1106 err = got_fileindex_entry_add(fileindex, new_ie);
1107 done:
1108 if (err)
1109 got_fileindex_entry_free(new_ie);
1110 else
1111 *new_iep = new_ie;
1112 return err;
1115 static mode_t
1116 get_ondisk_perms(int executable, mode_t st_mode)
1118 mode_t xbits = S_IXUSR;
1120 if (executable) {
1121 /* Map read bits to execute bits. */
1122 if (st_mode & S_IRGRP)
1123 xbits |= S_IXGRP;
1124 if (st_mode & S_IROTH)
1125 xbits |= S_IXOTH;
1126 return st_mode | xbits;
1129 return st_mode;
1132 static mode_t
1133 apply_umask(mode_t mode)
1135 mode_t um;
1137 um = umask(000);
1138 umask(um);
1139 return mode & ~um;
1142 /* forward declaration */
1143 static const struct got_error *
1144 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 const char *path, mode_t te_mode, mode_t st_mode,
1146 struct got_blob_object *blob, int restoring_missing_file,
1147 int reverting_versioned_file, int installing_bad_symlink,
1148 int path_is_unversioned, struct got_repository *repo,
1149 got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 * This function assumes that the provided symlink target points at a
1153 * safe location in the work tree!
1155 static const struct got_error *
1156 replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 const char *target_path, size_t target_len)
1159 const struct got_error *err = NULL;
1160 ssize_t elen;
1161 char etarget[PATH_MAX];
1162 int fd;
1164 *did_something = 0;
1167 * "Bad" symlinks (those pointing outside the work tree or into the
1168 * .got directory) are installed in the work tree as a regular file
1169 * which contains the bad symlink target path.
1170 * The new symlink target has already been checked for safety by our
1171 * caller. If we can successfully open a regular file then we simply
1172 * replace this file with a symlink below.
1174 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 if (fd == -1) {
1176 if (!got_err_open_nofollow_on_symlink())
1177 return got_error_from_errno2("open", ondisk_path);
1179 /* We are updating an existing on-disk symlink. */
1180 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 if (elen == -1)
1182 return got_error_from_errno2("readlink", ondisk_path);
1184 if (elen == target_len &&
1185 memcmp(etarget, target_path, target_len) == 0)
1186 return NULL; /* nothing to do */
1189 *did_something = 1;
1190 err = update_symlink(ondisk_path, target_path, target_len);
1191 if (fd != -1 && close(fd) == -1 && err == NULL)
1192 err = got_error_from_errno2("close", ondisk_path);
1193 return err;
1196 static const struct got_error *
1197 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 const struct got_error *err = NULL;
1201 char canonpath[PATH_MAX];
1202 char *path_got = NULL;
1204 *is_bad_symlink = 0;
1206 if (target_len >= sizeof(canonpath)) {
1207 *is_bad_symlink = 1;
1208 return NULL;
1212 * We do not use realpath(3) to resolve the symlink's target
1213 * path because we don't want to resolve symlinks recursively.
1214 * Instead we make the path absolute and then canonicalize it.
1215 * Relative symlink target lookup should begin at the directory
1216 * in which the blob object is being installed.
1218 if (!got_path_is_absolute(target_path)) {
1219 char *abspath, *parent;
1220 err = got_path_dirname(&parent, ondisk_path);
1221 if (err)
1222 return err;
1223 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 free(parent);
1225 return got_error_from_errno("asprintf");
1227 free(parent);
1228 if (strlen(abspath) >= sizeof(canonpath)) {
1229 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1230 free(abspath);
1231 return err;
1233 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1234 free(abspath);
1235 if (err)
1236 return err;
1237 } else {
1238 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1239 if (err)
1240 return err;
1243 /* Only allow symlinks pointing at paths within the work tree. */
1244 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 *is_bad_symlink = 1;
1246 return NULL;
1249 /* Do not allow symlinks pointing into the .got directory. */
1250 if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 GOT_WORKTREE_GOT_DIR) == -1)
1252 return got_error_from_errno("asprintf");
1253 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 *is_bad_symlink = 1;
1256 free(path_got);
1257 return NULL;
1260 static const struct got_error *
1261 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 int restoring_missing_file, int reverting_versioned_file,
1264 int path_is_unversioned, int allow_bad_symlinks,
1265 struct got_repository *repo,
1266 got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 const struct got_error *err = NULL;
1269 char target_path[PATH_MAX];
1270 size_t len, target_len = 0;
1271 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 *is_bad_symlink = 0;
1277 * Blob object content specifies the target path of the link.
1278 * If a symbolic link cannot be installed we instead create
1279 * a regular file which contains the link target path stored
1280 * in the blob object.
1282 do {
1283 err = got_object_blob_read_block(&len, blob);
1284 if (err)
1285 return err;
1287 if (len + target_len >= sizeof(target_path)) {
1288 /* Path too long; install as a regular file. */
1289 *is_bad_symlink = 1;
1290 got_object_blob_rewind(blob);
1291 return install_blob(worktree, ondisk_path, path,
1292 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 restoring_missing_file, reverting_versioned_file,
1294 1, path_is_unversioned, repo, progress_cb,
1295 progress_arg);
1297 if (len > 0) {
1298 /* Skip blob object header first time around. */
1299 memcpy(target_path + target_len, buf + hdrlen,
1300 len - hdrlen);
1301 target_len += len - hdrlen;
1302 hdrlen = 0;
1304 } while (len != 0);
1305 target_path[target_len] = '\0';
1307 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 ondisk_path, worktree->root_path);
1309 if (err)
1310 return err;
1312 if (*is_bad_symlink && !allow_bad_symlinks) {
1313 /* install as a regular file */
1314 got_object_blob_rewind(blob);
1315 err = install_blob(worktree, ondisk_path, path,
1316 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 restoring_missing_file, reverting_versioned_file, 1,
1318 path_is_unversioned, repo, progress_cb, progress_arg);
1319 return err;
1322 if (symlink(target_path, ondisk_path) == -1) {
1323 if (errno == EEXIST) {
1324 int symlink_replaced;
1325 if (path_is_unversioned) {
1326 err = (*progress_cb)(progress_arg,
1327 GOT_STATUS_UNVERSIONED, path);
1328 return err;
1330 err = replace_existing_symlink(&symlink_replaced,
1331 ondisk_path, target_path, target_len);
1332 if (err)
1333 return err;
1334 if (progress_cb) {
1335 if (symlink_replaced) {
1336 err = (*progress_cb)(progress_arg,
1337 reverting_versioned_file ?
1338 GOT_STATUS_REVERT :
1339 GOT_STATUS_UPDATE, path);
1340 } else {
1341 err = (*progress_cb)(progress_arg,
1342 GOT_STATUS_EXISTS, path);
1345 return err; /* Nothing else to do. */
1348 if (errno == ENOENT) {
1349 char *parent;
1350 err = got_path_dirname(&parent, ondisk_path);
1351 if (err)
1352 return err;
1353 err = add_dir_on_disk(worktree, parent);
1354 free(parent);
1355 if (err)
1356 return err;
1358 * Retry, and fall through to error handling
1359 * below if this second attempt fails.
1361 if (symlink(target_path, ondisk_path) != -1) {
1362 err = NULL; /* success */
1363 return err;
1367 /* Handle errors from first or second creation attempt. */
1368 if (errno == ENAMETOOLONG) {
1369 /* bad target path; install as a regular file */
1370 *is_bad_symlink = 1;
1371 got_object_blob_rewind(blob);
1372 err = install_blob(worktree, ondisk_path, path,
1373 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 restoring_missing_file, reverting_versioned_file, 1,
1375 path_is_unversioned, repo,
1376 progress_cb, progress_arg);
1377 } else if (errno == ENOTDIR) {
1378 err = got_error_path(ondisk_path,
1379 GOT_ERR_FILE_OBSTRUCTED);
1380 } else {
1381 err = got_error_from_errno3("symlink",
1382 target_path, ondisk_path);
1384 } else if (progress_cb)
1385 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1387 return err;
1390 static const struct got_error *
1391 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 const char *path, mode_t te_mode, mode_t st_mode,
1393 struct got_blob_object *blob, int restoring_missing_file,
1394 int reverting_versioned_file, int installing_bad_symlink,
1395 int path_is_unversioned, struct got_repository *repo,
1396 got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 const struct got_error *err = NULL;
1399 int fd = -1;
1400 size_t len, hdrlen;
1401 int update = 0;
1402 char *tmppath = NULL;
1403 mode_t mode;
1405 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1407 O_CLOEXEC, mode);
1408 if (fd == -1) {
1409 if (errno == ENOENT) {
1410 char *parent;
1411 err = got_path_dirname(&parent, path);
1412 if (err)
1413 return err;
1414 err = add_dir_on_disk(worktree, parent);
1415 free(parent);
1416 if (err)
1417 return err;
1418 fd = open(ondisk_path,
1419 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1420 mode);
1421 if (fd == -1)
1422 return got_error_from_errno2("open",
1423 ondisk_path);
1424 } else if (errno == EEXIST) {
1425 if (path_is_unversioned) {
1426 err = (*progress_cb)(progress_arg,
1427 GOT_STATUS_UNVERSIONED, path);
1428 goto done;
1430 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1431 !S_ISREG(st_mode) && !installing_bad_symlink) {
1432 /* TODO file is obstructed; do something */
1433 err = got_error_path(ondisk_path,
1434 GOT_ERR_FILE_OBSTRUCTED);
1435 goto done;
1436 } else {
1437 err = got_opentemp_named_fd(&tmppath, &fd,
1438 ondisk_path, "");
1439 if (err)
1440 goto done;
1441 update = 1;
1443 if (fchmod(fd, apply_umask(mode)) == -1) {
1444 err = got_error_from_errno2("fchmod",
1445 tmppath);
1446 goto done;
1449 } else
1450 return got_error_from_errno2("open", ondisk_path);
1453 if (progress_cb) {
1454 if (restoring_missing_file)
1455 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1456 path);
1457 else if (reverting_versioned_file)
1458 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1459 path);
1460 else
1461 err = (*progress_cb)(progress_arg,
1462 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1463 if (err)
1464 goto done;
1467 hdrlen = got_object_blob_get_hdrlen(blob);
1468 do {
1469 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1470 err = got_object_blob_read_block(&len, blob);
1471 if (err)
1472 break;
1473 if (len > 0) {
1474 /* Skip blob object header first time around. */
1475 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1476 if (outlen == -1) {
1477 err = got_error_from_errno("write");
1478 goto done;
1479 } else if (outlen != len - hdrlen) {
1480 err = got_error(GOT_ERR_IO);
1481 goto done;
1483 hdrlen = 0;
1485 } while (len != 0);
1487 if (fsync(fd) != 0) {
1488 err = got_error_from_errno("fsync");
1489 goto done;
1492 if (update) {
1493 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1494 err = got_error_from_errno2("unlink", ondisk_path);
1495 goto done;
1497 if (rename(tmppath, ondisk_path) != 0) {
1498 err = got_error_from_errno3("rename", tmppath,
1499 ondisk_path);
1500 goto done;
1502 free(tmppath);
1503 tmppath = NULL;
1506 done:
1507 if (fd != -1 && close(fd) == -1 && err == NULL)
1508 err = got_error_from_errno("close");
1509 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1510 err = got_error_from_errno2("unlink", tmppath);
1511 free(tmppath);
1512 return err;
1516 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1517 * conflict marker is found in newly added lines only.
1519 static const struct got_error *
1520 get_modified_file_content_status(unsigned char *status,
1521 struct got_blob_object *blob, const char *path, struct stat *sb,
1522 FILE *ondisk_file)
1524 const struct got_error *err, *free_err;
1525 const char *markers[3] = {
1526 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1527 GOT_DIFF_CONFLICT_MARKER_SEP,
1528 GOT_DIFF_CONFLICT_MARKER_END
1530 FILE *f1 = NULL;
1531 struct got_diffreg_result *diffreg_result = NULL;
1532 struct diff_result *r;
1533 int nchunks_parsed, n, i = 0, ln = 0;
1534 char *line = NULL;
1535 size_t linesize = 0;
1536 ssize_t linelen;
1538 if (*status != GOT_STATUS_MODIFY)
1539 return NULL;
1541 f1 = got_opentemp();
1542 if (f1 == NULL)
1543 return got_error_from_errno("got_opentemp");
1545 if (blob) {
1546 got_object_blob_rewind(blob);
1547 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1548 if (err)
1549 goto done;
1552 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1553 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1554 if (err)
1555 goto done;
1557 r = diffreg_result->result;
1559 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1560 struct diff_chunk *c;
1561 struct diff_chunk_context cc = {};
1562 off_t pos;
1565 * We can optimise a little by advancing straight
1566 * to the next chunk if this one has no added lines.
1568 c = diff_chunk_get(r, n);
1570 if (diff_chunk_type(c) != CHUNK_PLUS) {
1571 nchunks_parsed = 1;
1572 continue; /* removed or unchanged lines */
1575 pos = diff_chunk_get_right_start_pos(c);
1576 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1577 err = got_ferror(ondisk_file, GOT_ERR_IO);
1578 goto done;
1581 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1582 ln = cc.right.start;
1584 while (ln < cc.right.end) {
1585 linelen = getline(&line, &linesize, ondisk_file);
1586 if (linelen == -1) {
1587 if (feof(ondisk_file))
1588 break;
1589 err = got_ferror(ondisk_file, GOT_ERR_IO);
1590 break;
1593 if (line && strncmp(line, markers[i],
1594 strlen(markers[i])) == 0) {
1595 if (strcmp(markers[i],
1596 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1597 *status = GOT_STATUS_CONFLICT;
1598 goto done;
1599 } else
1600 i++;
1602 ++ln;
1606 done:
1607 free(line);
1608 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1609 err = got_error_from_errno("fclose");
1610 free_err = got_diffreg_result_free(diffreg_result);
1611 if (err == NULL)
1612 err = free_err;
1614 return err;
1617 static int
1618 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1620 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1621 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1624 static int
1625 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1627 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1628 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1629 ie->mtime_sec == sb->st_mtim.tv_sec &&
1630 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1631 ie->size == (sb->st_size & 0xffffffff) &&
1632 !xbit_differs(ie, sb->st_mode));
1635 static unsigned char
1636 get_staged_status(struct got_fileindex_entry *ie)
1638 switch (got_fileindex_entry_stage_get(ie)) {
1639 case GOT_FILEIDX_STAGE_ADD:
1640 return GOT_STATUS_ADD;
1641 case GOT_FILEIDX_STAGE_DELETE:
1642 return GOT_STATUS_DELETE;
1643 case GOT_FILEIDX_STAGE_MODIFY:
1644 return GOT_STATUS_MODIFY;
1645 default:
1646 return GOT_STATUS_NO_CHANGE;
1650 static const struct got_error *
1651 get_symlink_modification_status(unsigned char *status,
1652 struct got_fileindex_entry *ie, const char *abspath,
1653 int dirfd, const char *de_name, struct got_blob_object *blob)
1655 const struct got_error *err = NULL;
1656 char target_path[PATH_MAX];
1657 char etarget[PATH_MAX];
1658 ssize_t elen;
1659 size_t len, target_len = 0;
1660 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1661 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1663 *status = GOT_STATUS_NO_CHANGE;
1665 /* Blob object content specifies the target path of the link. */
1666 do {
1667 err = got_object_blob_read_block(&len, blob);
1668 if (err)
1669 return err;
1670 if (len + target_len >= sizeof(target_path)) {
1672 * Should not happen. The blob contents were OK
1673 * when this symlink was installed.
1675 return got_error(GOT_ERR_NO_SPACE);
1677 if (len > 0) {
1678 /* Skip blob object header first time around. */
1679 memcpy(target_path + target_len, buf + hdrlen,
1680 len - hdrlen);
1681 target_len += len - hdrlen;
1682 hdrlen = 0;
1684 } while (len != 0);
1685 target_path[target_len] = '\0';
1687 if (dirfd != -1) {
1688 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1689 if (elen == -1)
1690 return got_error_from_errno2("readlinkat", abspath);
1691 } else {
1692 elen = readlink(abspath, etarget, sizeof(etarget));
1693 if (elen == -1)
1694 return got_error_from_errno2("readlink", abspath);
1697 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1698 *status = GOT_STATUS_MODIFY;
1700 return NULL;
1703 static const struct got_error *
1704 get_file_status(unsigned char *status, struct stat *sb,
1705 struct got_fileindex_entry *ie, const char *abspath,
1706 int dirfd, const char *de_name, struct got_repository *repo)
1708 const struct got_error *err = NULL;
1709 struct got_object_id id;
1710 size_t hdrlen;
1711 int fd = -1, fd1 = -1;
1712 FILE *f = NULL;
1713 uint8_t fbuf[8192];
1714 struct got_blob_object *blob = NULL;
1715 size_t flen, blen;
1716 unsigned char staged_status;
1718 staged_status = get_staged_status(ie);
1719 *status = GOT_STATUS_NO_CHANGE;
1720 memset(sb, 0, sizeof(*sb));
1723 * Whenever the caller provides a directory descriptor and a
1724 * directory entry name for the file, use them! This prevents
1725 * race conditions if filesystem paths change beneath our feet.
1727 if (dirfd != -1) {
1728 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1729 if (errno == ENOENT) {
1730 if (got_fileindex_entry_has_file_on_disk(ie))
1731 *status = GOT_STATUS_MISSING;
1732 else
1733 *status = GOT_STATUS_DELETE;
1734 goto done;
1736 err = got_error_from_errno2("fstatat", abspath);
1737 goto done;
1739 } else {
1740 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1741 if (fd == -1 && errno != ENOENT &&
1742 !got_err_open_nofollow_on_symlink())
1743 return got_error_from_errno2("open", abspath);
1744 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1745 if (lstat(abspath, sb) == -1)
1746 return got_error_from_errno2("lstat", abspath);
1747 } else if (fd == -1 || fstat(fd, sb) == -1) {
1748 if (errno == ENOENT) {
1749 if (got_fileindex_entry_has_file_on_disk(ie))
1750 *status = GOT_STATUS_MISSING;
1751 else
1752 *status = GOT_STATUS_DELETE;
1753 goto done;
1755 err = got_error_from_errno2("fstat", abspath);
1756 goto done;
1760 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1761 *status = GOT_STATUS_OBSTRUCTED;
1762 goto done;
1765 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1766 *status = GOT_STATUS_DELETE;
1767 goto done;
1768 } else if (!got_fileindex_entry_has_blob(ie) &&
1769 staged_status != GOT_STATUS_ADD) {
1770 *status = GOT_STATUS_ADD;
1771 goto done;
1774 if (!stat_info_differs(ie, sb))
1775 goto done;
1777 if (S_ISLNK(sb->st_mode) &&
1778 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1779 *status = GOT_STATUS_MODIFY;
1780 goto done;
1783 if (staged_status == GOT_STATUS_MODIFY ||
1784 staged_status == GOT_STATUS_ADD)
1785 got_fileindex_entry_get_staged_blob_id(&id, ie);
1786 else
1787 got_fileindex_entry_get_blob_id(&id, ie);
1789 fd1 = got_opentempfd();
1790 if (fd1 == -1) {
1791 err = got_error_from_errno("got_opentempfd");
1792 goto done;
1794 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1795 if (err)
1796 goto done;
1798 if (S_ISLNK(sb->st_mode)) {
1799 err = get_symlink_modification_status(status, ie,
1800 abspath, dirfd, de_name, blob);
1801 goto done;
1804 if (dirfd != -1) {
1805 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1806 if (fd == -1) {
1807 err = got_error_from_errno2("openat", abspath);
1808 goto done;
1812 f = fdopen(fd, "r");
1813 if (f == NULL) {
1814 err = got_error_from_errno2("fdopen", abspath);
1815 goto done;
1817 fd = -1;
1818 hdrlen = got_object_blob_get_hdrlen(blob);
1819 for (;;) {
1820 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1821 err = got_object_blob_read_block(&blen, blob);
1822 if (err)
1823 goto done;
1824 /* Skip length of blob object header first time around. */
1825 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1826 if (flen == 0 && ferror(f)) {
1827 err = got_error_from_errno("fread");
1828 goto done;
1830 if (blen - hdrlen == 0) {
1831 if (flen != 0)
1832 *status = GOT_STATUS_MODIFY;
1833 break;
1834 } else if (flen == 0) {
1835 if (blen - hdrlen != 0)
1836 *status = GOT_STATUS_MODIFY;
1837 break;
1838 } else if (blen - hdrlen == flen) {
1839 /* Skip blob object header first time around. */
1840 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1841 *status = GOT_STATUS_MODIFY;
1842 break;
1844 } else {
1845 *status = GOT_STATUS_MODIFY;
1846 break;
1848 hdrlen = 0;
1851 if (*status == GOT_STATUS_MODIFY) {
1852 rewind(f);
1853 err = get_modified_file_content_status(status, blob, ie->path,
1854 sb, f);
1855 } else if (xbit_differs(ie, sb->st_mode))
1856 *status = GOT_STATUS_MODE_CHANGE;
1857 done:
1858 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1859 err = got_error_from_errno("close");
1860 if (blob)
1861 got_object_blob_close(blob);
1862 if (f != NULL && fclose(f) == EOF && err == NULL)
1863 err = got_error_from_errno2("fclose", abspath);
1864 if (fd != -1 && close(fd) == -1 && err == NULL)
1865 err = got_error_from_errno2("close", abspath);
1866 return err;
1870 * Update timestamps in the file index if a file is unmodified and
1871 * we had to run a full content comparison to find out.
1873 static const struct got_error *
1874 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1875 struct got_fileindex_entry *ie, struct stat *sb)
1877 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1878 return got_fileindex_entry_update(ie, wt_fd, path,
1879 ie->blob_sha1, ie->commit_sha1, 1);
1881 return NULL;
1884 static const struct got_error *
1885 update_blob(struct got_worktree *worktree,
1886 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1887 struct got_tree_entry *te, const char *path,
1888 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1889 void *progress_arg)
1891 const struct got_error *err = NULL;
1892 struct got_blob_object *blob = NULL;
1893 char *ondisk_path = NULL;
1894 unsigned char status = GOT_STATUS_NO_CHANGE;
1895 struct stat sb;
1896 int fd1 = -1, fd2 = -1;
1898 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1899 return got_error_from_errno("asprintf");
1901 if (ie) {
1902 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1903 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1904 goto done;
1906 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1907 repo);
1908 if (err)
1909 goto done;
1910 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1911 sb.st_mode = got_fileindex_perms_to_st(ie);
1912 } else {
1913 if (stat(ondisk_path, &sb) == -1) {
1914 if (errno != ENOENT) {
1915 err = got_error_from_errno2("stat",
1916 ondisk_path);
1917 goto done;
1919 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1920 status = GOT_STATUS_UNVERSIONED;
1921 } else {
1922 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1923 status = GOT_STATUS_UNVERSIONED;
1924 else
1925 status = GOT_STATUS_OBSTRUCTED;
1929 if (status == GOT_STATUS_OBSTRUCTED) {
1930 if (ie)
1931 got_fileindex_entry_mark_skipped(ie);
1932 err = (*progress_cb)(progress_arg, status, path);
1933 goto done;
1935 if (status == GOT_STATUS_CONFLICT) {
1936 if (ie)
1937 got_fileindex_entry_mark_skipped(ie);
1938 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1939 path);
1940 goto done;
1943 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1944 (S_ISLNK(te->mode) ||
1945 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1947 * This is a regular file or an installed bad symlink.
1948 * If the file index indicates that this file is already
1949 * up-to-date with respect to the repository we can skip
1950 * updating contents of this file.
1952 if (got_fileindex_entry_has_commit(ie) &&
1953 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1954 SHA1_DIGEST_LENGTH) == 0) {
1955 /* Same commit. */
1956 err = sync_timestamps(worktree->root_fd,
1957 path, status, ie, &sb);
1958 if (err)
1959 goto done;
1960 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1961 path);
1962 goto done;
1964 if (got_fileindex_entry_has_blob(ie) &&
1965 memcmp(ie->blob_sha1, te->id.sha1,
1966 SHA1_DIGEST_LENGTH) == 0) {
1967 /* Different commit but the same blob. */
1968 err = sync_timestamps(worktree->root_fd,
1969 path, status, ie, &sb);
1970 if (err)
1971 goto done;
1972 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1973 path);
1974 goto done;
1978 fd1 = got_opentempfd();
1979 if (fd1 == -1) {
1980 err = got_error_from_errno("got_opentempfd");
1981 goto done;
1983 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1984 if (err)
1985 goto done;
1987 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1988 int update_timestamps;
1989 struct got_blob_object *blob2 = NULL;
1990 char *label_orig = NULL;
1991 if (got_fileindex_entry_has_blob(ie)) {
1992 fd2 = got_opentempfd();
1993 if (fd2 == -1) {
1994 err = got_error_from_errno("got_opentempfd");
1995 goto done;
1997 struct got_object_id id2;
1998 got_fileindex_entry_get_blob_id(&id2, ie);
1999 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2000 fd2);
2001 if (err)
2002 goto done;
2004 if (got_fileindex_entry_has_commit(ie)) {
2005 char id_str[SHA1_DIGEST_STRING_LENGTH];
2006 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2007 sizeof(id_str)) == NULL) {
2008 err = got_error_path(id_str,
2009 GOT_ERR_BAD_OBJ_ID_STR);
2010 goto done;
2012 if (asprintf(&label_orig, "%s: commit %s",
2013 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2014 err = got_error_from_errno("asprintf");
2015 goto done;
2018 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2019 char *link_target;
2020 err = got_object_blob_read_to_str(&link_target, blob);
2021 if (err)
2022 goto done;
2023 err = merge_symlink(worktree, blob2, ondisk_path, path,
2024 label_orig, link_target, worktree->base_commit_id,
2025 repo, progress_cb, progress_arg);
2026 free(link_target);
2027 } else {
2028 err = merge_blob(&update_timestamps, worktree, blob2,
2029 ondisk_path, path, sb.st_mode, label_orig, blob,
2030 worktree->base_commit_id, repo,
2031 progress_cb, progress_arg);
2033 free(label_orig);
2034 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2035 err = got_error_from_errno("close");
2036 goto done;
2038 if (blob2)
2039 got_object_blob_close(blob2);
2040 if (err)
2041 goto done;
2043 * Do not update timestamps of files with local changes.
2044 * Otherwise, a future status walk would treat them as
2045 * unmodified files again.
2047 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2048 blob->id.sha1, worktree->base_commit_id->sha1,
2049 update_timestamps);
2050 } else if (status == GOT_STATUS_MODE_CHANGE) {
2051 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2052 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2053 } else if (status == GOT_STATUS_DELETE) {
2054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2055 if (err)
2056 goto done;
2057 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2058 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2059 if (err)
2060 goto done;
2061 } else {
2062 int is_bad_symlink = 0;
2063 if (S_ISLNK(te->mode)) {
2064 err = install_symlink(&is_bad_symlink, worktree,
2065 ondisk_path, path, blob,
2066 status == GOT_STATUS_MISSING, 0,
2067 status == GOT_STATUS_UNVERSIONED, 0,
2068 repo, progress_cb, progress_arg);
2069 } else {
2070 err = install_blob(worktree, ondisk_path, path,
2071 te->mode, sb.st_mode, blob,
2072 status == GOT_STATUS_MISSING, 0, 0,
2073 status == GOT_STATUS_UNVERSIONED, repo,
2074 progress_cb, progress_arg);
2076 if (err)
2077 goto done;
2079 if (ie) {
2080 err = got_fileindex_entry_update(ie,
2081 worktree->root_fd, path, blob->id.sha1,
2082 worktree->base_commit_id->sha1, 1);
2083 } else {
2084 err = create_fileindex_entry(&ie, fileindex,
2085 worktree->base_commit_id, worktree->root_fd, path,
2086 &blob->id);
2088 if (err)
2089 goto done;
2091 if (is_bad_symlink) {
2092 got_fileindex_entry_filetype_set(ie,
2093 GOT_FILEIDX_MODE_BAD_SYMLINK);
2097 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2098 err = got_error_from_errno("close");
2099 goto done;
2101 got_object_blob_close(blob);
2102 done:
2103 free(ondisk_path);
2104 return err;
2107 static const struct got_error *
2108 remove_ondisk_file(const char *root_path, const char *path)
2110 const struct got_error *err = NULL;
2111 char *ondisk_path = NULL, *parent = NULL;
2113 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2114 return got_error_from_errno("asprintf");
2116 if (unlink(ondisk_path) == -1) {
2117 if (errno != ENOENT)
2118 err = got_error_from_errno2("unlink", ondisk_path);
2119 } else {
2120 size_t root_len = strlen(root_path);
2121 err = got_path_dirname(&parent, ondisk_path);
2122 if (err)
2123 goto done;
2124 while (got_path_cmp(parent, root_path,
2125 strlen(parent), root_len) != 0) {
2126 free(ondisk_path);
2127 ondisk_path = parent;
2128 parent = NULL;
2129 if (rmdir(ondisk_path) == -1) {
2130 if (errno != ENOTEMPTY)
2131 err = got_error_from_errno2("rmdir",
2132 ondisk_path);
2133 break;
2135 err = got_path_dirname(&parent, ondisk_path);
2136 if (err)
2137 break;
2140 done:
2141 free(ondisk_path);
2142 free(parent);
2143 return err;
2146 static const struct got_error *
2147 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 struct got_fileindex_entry *ie, struct got_repository *repo,
2149 got_worktree_checkout_cb progress_cb, void *progress_arg)
2151 const struct got_error *err = NULL;
2152 unsigned char status;
2153 struct stat sb;
2154 char *ondisk_path;
2156 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2157 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2159 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2160 == -1)
2161 return got_error_from_errno("asprintf");
2163 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2164 if (err)
2165 goto done;
2167 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2168 char ondisk_target[PATH_MAX];
2169 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2170 sizeof(ondisk_target));
2171 if (ondisk_len == -1) {
2172 err = got_error_from_errno2("readlink", ondisk_path);
2173 goto done;
2175 ondisk_target[ondisk_len] = '\0';
2176 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2177 NULL, NULL, /* XXX pass common ancestor info? */
2178 ondisk_target, ondisk_path);
2179 if (err)
2180 goto done;
2181 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2182 ie->path);
2183 goto done;
2186 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2187 status == GOT_STATUS_ADD) {
2188 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2189 if (err)
2190 goto done;
2192 * Preserve the working file and change the deleted blob's
2193 * entry into a schedule-add entry.
2195 err = got_fileindex_entry_update(ie, worktree->root_fd,
2196 ie->path, NULL, NULL, 0);
2197 } else {
2198 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2199 if (err)
2200 goto done;
2201 if (status == GOT_STATUS_NO_CHANGE) {
2202 err = remove_ondisk_file(worktree->root_path, ie->path);
2203 if (err)
2204 goto done;
2206 got_fileindex_entry_remove(fileindex, ie);
2208 done:
2209 free(ondisk_path);
2210 return err;
2213 struct diff_cb_arg {
2214 struct got_fileindex *fileindex;
2215 struct got_worktree *worktree;
2216 struct got_repository *repo;
2217 got_worktree_checkout_cb progress_cb;
2218 void *progress_arg;
2219 got_cancel_cb cancel_cb;
2220 void *cancel_arg;
2223 static const struct got_error *
2224 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2225 struct got_tree_entry *te, const char *parent_path)
2227 struct diff_cb_arg *a = arg;
2229 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 return got_error(GOT_ERR_CANCELLED);
2232 return update_blob(a->worktree, a->fileindex, ie, te,
2233 ie->path, a->repo, a->progress_cb, a->progress_arg);
2236 static const struct got_error *
2237 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2239 struct diff_cb_arg *a = arg;
2241 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 return got_error(GOT_ERR_CANCELLED);
2244 return delete_blob(a->worktree, a->fileindex, ie,
2245 a->repo, a->progress_cb, a->progress_arg);
2248 static const struct got_error *
2249 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2251 struct diff_cb_arg *a = arg;
2252 const struct got_error *err;
2253 char *path;
2255 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 return got_error(GOT_ERR_CANCELLED);
2258 if (got_object_tree_entry_is_submodule(te))
2259 return NULL;
2261 if (asprintf(&path, "%s%s%s", parent_path,
2262 parent_path[0] ? "/" : "", te->name)
2263 == -1)
2264 return got_error_from_errno("asprintf");
2266 if (S_ISDIR(te->mode))
2267 err = add_dir_on_disk(a->worktree, path);
2268 else
2269 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2270 a->repo, a->progress_cb, a->progress_arg);
2272 free(path);
2273 return err;
2276 const struct got_error *
2277 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2279 uint32_t uuid_status;
2281 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2282 if (uuid_status != uuid_s_ok) {
2283 *uuidstr = NULL;
2284 return got_error_uuid(uuid_status, "uuid_to_string");
2287 return NULL;
2290 static const struct got_error *
2291 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2293 const struct got_error *err = NULL;
2294 char *uuidstr = NULL;
2296 *refname = NULL;
2298 err = got_worktree_get_uuid(&uuidstr, worktree);
2299 if (err)
2300 return err;
2302 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2303 err = got_error_from_errno("asprintf");
2304 *refname = NULL;
2306 free(uuidstr);
2307 return err;
2310 const struct got_error *
2311 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2312 const char *prefix)
2314 return get_ref_name(refname, worktree, prefix);
2317 const struct got_error *
2318 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2320 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2323 static const struct got_error *
2324 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree,
2327 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2330 static const struct got_error *
2331 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2333 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2336 static const struct got_error *
2337 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree,
2340 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2343 static const struct got_error *
2344 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2350 static const struct got_error *
2351 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2364 static const struct got_error *
2365 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2367 return get_ref_name(refname, worktree,
2368 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2371 static const struct got_error *
2372 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2374 return get_ref_name(refname, worktree,
2375 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2378 const struct got_error *
2379 got_worktree_get_histedit_script_path(char **path,
2380 struct got_worktree *worktree)
2382 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2383 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2384 *path = NULL;
2385 return got_error_from_errno("asprintf");
2387 return NULL;
2390 static const struct got_error *
2391 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree,
2394 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2397 static const struct got_error *
2398 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2400 return get_ref_name(refname, worktree,
2401 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2405 * Prevent Git's garbage collector from deleting our base commit by
2406 * setting a reference to our base commit's ID.
2408 static const struct got_error *
2409 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2411 const struct got_error *err = NULL;
2412 struct got_reference *ref = NULL;
2413 char *refname;
2415 err = got_worktree_get_base_ref_name(&refname, worktree);
2416 if (err)
2417 return err;
2419 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2420 if (err)
2421 goto done;
2423 err = got_ref_write(ref, repo);
2424 done:
2425 free(refname);
2426 if (ref)
2427 got_ref_close(ref);
2428 return err;
2431 static const struct got_error *
2432 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2434 const struct got_error *err = NULL;
2436 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2437 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2438 err = got_error_from_errno("asprintf");
2439 *fileindex_path = NULL;
2441 return err;
2445 static const struct got_error *
2446 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2447 struct got_worktree *worktree)
2449 const struct got_error *err = NULL;
2450 FILE *index = NULL;
2452 *fileindex_path = NULL;
2453 *fileindex = got_fileindex_alloc();
2454 if (*fileindex == NULL)
2455 return got_error_from_errno("got_fileindex_alloc");
2457 err = get_fileindex_path(fileindex_path, worktree);
2458 if (err)
2459 goto done;
2461 index = fopen(*fileindex_path, "rbe");
2462 if (index == NULL) {
2463 if (errno != ENOENT)
2464 err = got_error_from_errno2("fopen", *fileindex_path);
2465 } else {
2466 err = got_fileindex_read(*fileindex, index);
2467 if (fclose(index) == EOF && err == NULL)
2468 err = got_error_from_errno("fclose");
2470 done:
2471 if (err) {
2472 free(*fileindex_path);
2473 *fileindex_path = NULL;
2474 got_fileindex_free(*fileindex);
2475 *fileindex = NULL;
2477 return err;
2480 struct bump_base_commit_id_arg {
2481 struct got_object_id *base_commit_id;
2482 const char *path;
2483 size_t path_len;
2484 const char *entry_name;
2485 got_worktree_checkout_cb progress_cb;
2486 void *progress_arg;
2489 static const struct got_error *
2490 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2492 const struct got_error *err;
2493 struct bump_base_commit_id_arg *a = arg;
2495 if (a->entry_name) {
2496 if (strcmp(ie->path, a->path) != 0)
2497 return NULL;
2498 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2499 return NULL;
2501 if (got_fileindex_entry_was_skipped(ie))
2502 return NULL;
2504 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2505 SHA1_DIGEST_LENGTH) == 0)
2506 return NULL;
2508 if (a->progress_cb) {
2509 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2510 ie->path);
2511 if (err)
2512 return err;
2514 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2515 return NULL;
2518 /* Bump base commit ID of all files within an updated part of the work tree. */
2519 static const struct got_error *
2520 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2521 struct got_fileindex *fileindex,
2522 got_worktree_checkout_cb progress_cb, void *progress_arg)
2524 struct bump_base_commit_id_arg bbc_arg;
2526 bbc_arg.base_commit_id = worktree->base_commit_id;
2527 bbc_arg.entry_name = NULL;
2528 bbc_arg.path = "";
2529 bbc_arg.path_len = 0;
2530 bbc_arg.progress_cb = progress_cb;
2531 bbc_arg.progress_arg = progress_arg;
2533 return got_fileindex_for_each_entry_safe(fileindex,
2534 bump_base_commit_id, &bbc_arg);
2537 static const struct got_error *
2538 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2540 const struct got_error *err = NULL;
2541 char *new_fileindex_path = NULL;
2542 FILE *new_index = NULL;
2543 struct timespec timeout;
2545 err = got_opentemp_named(&new_fileindex_path, &new_index,
2546 fileindex_path, "");
2547 if (err)
2548 goto done;
2550 err = got_fileindex_write(fileindex, new_index);
2551 if (err)
2552 goto done;
2554 if (rename(new_fileindex_path, fileindex_path) != 0) {
2555 err = got_error_from_errno3("rename", new_fileindex_path,
2556 fileindex_path);
2557 unlink(new_fileindex_path);
2561 * Sleep for a short amount of time to ensure that files modified after
2562 * this program exits have a different time stamp from the one which
2563 * was recorded in the file index.
2565 timeout.tv_sec = 0;
2566 timeout.tv_nsec = 1;
2567 nanosleep(&timeout, NULL);
2568 done:
2569 if (new_index)
2570 fclose(new_index);
2571 free(new_fileindex_path);
2572 return err;
2575 static const struct got_error *
2576 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2577 struct got_object_id **tree_id, const char *wt_relpath,
2578 struct got_commit_object *base_commit, struct got_worktree *worktree,
2579 struct got_repository *repo)
2581 const struct got_error *err = NULL;
2582 struct got_object_id *id = NULL;
2583 char *in_repo_path = NULL;
2584 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2586 *entry_type = GOT_OBJ_TYPE_ANY;
2587 *tree_relpath = NULL;
2588 *tree_id = NULL;
2590 if (wt_relpath[0] == '\0') {
2591 /* Check out all files within the work tree. */
2592 *entry_type = GOT_OBJ_TYPE_TREE;
2593 *tree_relpath = strdup("");
2594 if (*tree_relpath == NULL) {
2595 err = got_error_from_errno("strdup");
2596 goto done;
2598 err = got_object_id_by_path(tree_id, repo, base_commit,
2599 worktree->path_prefix);
2600 if (err)
2601 goto done;
2602 return NULL;
2605 /* Check out a subset of files in the work tree. */
2607 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2608 is_root_wt ? "" : "/", wt_relpath) == -1) {
2609 err = got_error_from_errno("asprintf");
2610 goto done;
2613 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2614 if (err)
2615 goto done;
2617 free(in_repo_path);
2618 in_repo_path = NULL;
2620 err = got_object_get_type(entry_type, repo, id);
2621 if (err)
2622 goto done;
2624 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2625 /* Check out a single file. */
2626 if (strchr(wt_relpath, '/') == NULL) {
2627 /* Check out a single file in work tree's root dir. */
2628 in_repo_path = strdup(worktree->path_prefix);
2629 if (in_repo_path == NULL) {
2630 err = got_error_from_errno("strdup");
2631 goto done;
2633 *tree_relpath = strdup("");
2634 if (*tree_relpath == NULL) {
2635 err = got_error_from_errno("strdup");
2636 goto done;
2638 } else {
2639 /* Check out a single file in a subdirectory. */
2640 err = got_path_dirname(tree_relpath, wt_relpath);
2641 if (err)
2642 return err;
2643 if (asprintf(&in_repo_path, "%s%s%s",
2644 worktree->path_prefix, is_root_wt ? "" : "/",
2645 *tree_relpath) == -1) {
2646 err = got_error_from_errno("asprintf");
2647 goto done;
2650 err = got_object_id_by_path(tree_id, repo,
2651 base_commit, in_repo_path);
2652 } else {
2653 /* Check out all files within a subdirectory. */
2654 *tree_id = got_object_id_dup(id);
2655 if (*tree_id == NULL) {
2656 err = got_error_from_errno("got_object_id_dup");
2657 goto done;
2659 *tree_relpath = strdup(wt_relpath);
2660 if (*tree_relpath == NULL) {
2661 err = got_error_from_errno("strdup");
2662 goto done;
2665 done:
2666 free(id);
2667 free(in_repo_path);
2668 if (err) {
2669 *entry_type = GOT_OBJ_TYPE_ANY;
2670 free(*tree_relpath);
2671 *tree_relpath = NULL;
2672 free(*tree_id);
2673 *tree_id = NULL;
2675 return err;
2678 static const struct got_error *
2679 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2680 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2681 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2682 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2684 const struct got_error *err = NULL;
2685 struct got_commit_object *commit = NULL;
2686 struct got_tree_object *tree = NULL;
2687 struct got_fileindex_diff_tree_cb diff_cb;
2688 struct diff_cb_arg arg;
2690 err = ref_base_commit(worktree, repo);
2691 if (err) {
2692 if (!(err->code == GOT_ERR_ERRNO &&
2693 (errno == EACCES || errno == EROFS)))
2694 goto done;
2695 err = (*progress_cb)(progress_arg,
2696 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2697 if (err)
2698 return err;
2701 err = got_object_open_as_commit(&commit, repo,
2702 worktree->base_commit_id);
2703 if (err)
2704 goto done;
2706 err = got_object_open_as_tree(&tree, repo, tree_id);
2707 if (err)
2708 goto done;
2710 if (entry_name &&
2711 got_object_tree_find_entry(tree, entry_name) == NULL) {
2712 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2713 goto done;
2716 diff_cb.diff_old_new = diff_old_new;
2717 diff_cb.diff_old = diff_old;
2718 diff_cb.diff_new = diff_new;
2719 arg.fileindex = fileindex;
2720 arg.worktree = worktree;
2721 arg.repo = repo;
2722 arg.progress_cb = progress_cb;
2723 arg.progress_arg = progress_arg;
2724 arg.cancel_cb = cancel_cb;
2725 arg.cancel_arg = cancel_arg;
2726 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2727 entry_name, repo, &diff_cb, &arg);
2728 done:
2729 if (tree)
2730 got_object_tree_close(tree);
2731 if (commit)
2732 got_object_commit_close(commit);
2733 return err;
2736 const struct got_error *
2737 got_worktree_checkout_files(struct got_worktree *worktree,
2738 struct got_pathlist_head *paths, struct got_repository *repo,
2739 got_worktree_checkout_cb progress_cb, void *progress_arg,
2740 got_cancel_cb cancel_cb, void *cancel_arg)
2742 const struct got_error *err = NULL, *sync_err, *unlockerr;
2743 struct got_commit_object *commit = NULL;
2744 struct got_tree_object *tree = NULL;
2745 struct got_fileindex *fileindex = NULL;
2746 char *fileindex_path = NULL;
2747 struct got_pathlist_entry *pe;
2748 struct tree_path_data {
2749 STAILQ_ENTRY(tree_path_data) entry;
2750 struct got_object_id *tree_id;
2751 int entry_type;
2752 char *relpath;
2753 char *entry_name;
2754 } *tpd = NULL;
2755 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2757 STAILQ_INIT(&tree_paths);
2759 err = lock_worktree(worktree, LOCK_EX);
2760 if (err)
2761 return err;
2763 err = got_object_open_as_commit(&commit, repo,
2764 worktree->base_commit_id);
2765 if (err)
2766 goto done;
2768 /* Map all specified paths to in-repository trees. */
2769 TAILQ_FOREACH(pe, paths, entry) {
2770 tpd = malloc(sizeof(*tpd));
2771 if (tpd == NULL) {
2772 err = got_error_from_errno("malloc");
2773 goto done;
2776 err = find_tree_entry_for_checkout(&tpd->entry_type,
2777 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2778 worktree, repo);
2779 if (err) {
2780 free(tpd);
2781 goto done;
2784 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2785 err = got_path_basename(&tpd->entry_name, pe->path);
2786 if (err) {
2787 free(tpd->relpath);
2788 free(tpd->tree_id);
2789 free(tpd);
2790 goto done;
2792 } else
2793 tpd->entry_name = NULL;
2795 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2799 * Read the file index.
2800 * Checking out files is supposed to be an idempotent operation.
2801 * If the on-disk file index is incomplete we will try to complete it.
2803 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2804 if (err)
2805 goto done;
2807 tpd = STAILQ_FIRST(&tree_paths);
2808 TAILQ_FOREACH(pe, paths, entry) {
2809 struct bump_base_commit_id_arg bbc_arg;
2811 err = checkout_files(worktree, fileindex, tpd->relpath,
2812 tpd->tree_id, tpd->entry_name, repo,
2813 progress_cb, progress_arg, cancel_cb, cancel_arg);
2814 if (err)
2815 break;
2817 bbc_arg.base_commit_id = worktree->base_commit_id;
2818 bbc_arg.entry_name = tpd->entry_name;
2819 bbc_arg.path = pe->path;
2820 bbc_arg.path_len = pe->path_len;
2821 bbc_arg.progress_cb = progress_cb;
2822 bbc_arg.progress_arg = progress_arg;
2823 err = got_fileindex_for_each_entry_safe(fileindex,
2824 bump_base_commit_id, &bbc_arg);
2825 if (err)
2826 break;
2828 tpd = STAILQ_NEXT(tpd, entry);
2830 sync_err = sync_fileindex(fileindex, fileindex_path);
2831 if (sync_err && err == NULL)
2832 err = sync_err;
2833 done:
2834 free(fileindex_path);
2835 if (tree)
2836 got_object_tree_close(tree);
2837 if (commit)
2838 got_object_commit_close(commit);
2839 if (fileindex)
2840 got_fileindex_free(fileindex);
2841 while (!STAILQ_EMPTY(&tree_paths)) {
2842 tpd = STAILQ_FIRST(&tree_paths);
2843 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2844 free(tpd->relpath);
2845 free(tpd->tree_id);
2846 free(tpd);
2848 unlockerr = lock_worktree(worktree, LOCK_SH);
2849 if (unlockerr && err == NULL)
2850 err = unlockerr;
2851 return err;
2854 struct merge_file_cb_arg {
2855 struct got_worktree *worktree;
2856 struct got_fileindex *fileindex;
2857 got_worktree_checkout_cb progress_cb;
2858 void *progress_arg;
2859 got_cancel_cb cancel_cb;
2860 void *cancel_arg;
2861 const char *label_orig;
2862 struct got_object_id *commit_id2;
2863 int allow_bad_symlinks;
2866 static const struct got_error *
2867 merge_file_cb(void *arg, struct got_blob_object *blob1,
2868 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2869 struct got_object_id *id1, struct got_object_id *id2,
2870 const char *path1, const char *path2,
2871 mode_t mode1, mode_t mode2, struct got_repository *repo)
2873 static const struct got_error *err = NULL;
2874 struct merge_file_cb_arg *a = arg;
2875 struct got_fileindex_entry *ie;
2876 char *ondisk_path = NULL;
2877 struct stat sb;
2878 unsigned char status;
2879 int local_changes_subsumed;
2880 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2881 char *id_str = NULL, *label_deriv2 = NULL;
2883 if (blob1 && blob2) {
2884 ie = got_fileindex_entry_get(a->fileindex, path2,
2885 strlen(path2));
2886 if (ie == NULL)
2887 return (*a->progress_cb)(a->progress_arg,
2888 GOT_STATUS_MISSING, path2);
2890 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 path2) == -1)
2892 return got_error_from_errno("asprintf");
2894 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2895 repo);
2896 if (err)
2897 goto done;
2899 if (status == GOT_STATUS_DELETE) {
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_MERGE, path2);
2902 goto done;
2904 if (status != GOT_STATUS_NO_CHANGE &&
2905 status != GOT_STATUS_MODIFY &&
2906 status != GOT_STATUS_CONFLICT &&
2907 status != GOT_STATUS_ADD) {
2908 err = (*a->progress_cb)(a->progress_arg, status, path2);
2909 goto done;
2912 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2913 char *link_target2;
2914 err = got_object_blob_read_to_str(&link_target2, blob2);
2915 if (err)
2916 goto done;
2917 err = merge_symlink(a->worktree, blob1, ondisk_path,
2918 path2, a->label_orig, link_target2, a->commit_id2,
2919 repo, a->progress_cb, a->progress_arg);
2920 free(link_target2);
2921 } else {
2922 int fd;
2924 f_orig = got_opentemp();
2925 if (f_orig == NULL) {
2926 err = got_error_from_errno("got_opentemp");
2927 goto done;
2929 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2930 f_orig, blob1);
2931 if (err)
2932 goto done;
2934 f_deriv2 = got_opentemp();
2935 if (f_deriv2 == NULL)
2936 goto done;
2937 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2938 f_deriv2, blob2);
2939 if (err)
2940 goto done;
2942 fd = open(ondisk_path,
2943 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2944 if (fd == -1) {
2945 err = got_error_from_errno2("open",
2946 ondisk_path);
2947 goto done;
2949 f_deriv = fdopen(fd, "r");
2950 if (f_deriv == NULL) {
2951 err = got_error_from_errno2("fdopen",
2952 ondisk_path);
2953 close(fd);
2954 goto done;
2956 err = got_object_id_str(&id_str, a->commit_id2);
2957 if (err)
2958 goto done;
2959 if (asprintf(&label_deriv2, "%s: commit %s",
2960 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2961 err = got_error_from_errno("asprintf");
2962 goto done;
2964 err = merge_file(&local_changes_subsumed, a->worktree,
2965 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2966 mode2, a->label_orig, NULL, label_deriv2,
2967 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2968 a->progress_cb, a->progress_arg);
2970 } else if (blob1) {
2971 ie = got_fileindex_entry_get(a->fileindex, path1,
2972 strlen(path1));
2973 if (ie == NULL)
2974 return (*a->progress_cb)(a->progress_arg,
2975 GOT_STATUS_MISSING, path1);
2977 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2978 path1) == -1)
2979 return got_error_from_errno("asprintf");
2981 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2982 repo);
2983 if (err)
2984 goto done;
2986 switch (status) {
2987 case GOT_STATUS_NO_CHANGE:
2988 err = (*a->progress_cb)(a->progress_arg,
2989 GOT_STATUS_DELETE, path1);
2990 if (err)
2991 goto done;
2992 err = remove_ondisk_file(a->worktree->root_path, path1);
2993 if (err)
2994 goto done;
2995 if (ie)
2996 got_fileindex_entry_mark_deleted_from_disk(ie);
2997 break;
2998 case GOT_STATUS_DELETE:
2999 case GOT_STATUS_MISSING:
3000 err = (*a->progress_cb)(a->progress_arg,
3001 GOT_STATUS_DELETE, path1);
3002 if (err)
3003 goto done;
3004 if (ie)
3005 got_fileindex_entry_mark_deleted_from_disk(ie);
3006 break;
3007 case GOT_STATUS_ADD: {
3008 struct got_object_id *id;
3009 FILE *blob1_f;
3010 off_t blob1_size;
3012 * Delete the added file only if its content already
3013 * exists in the repository.
3015 err = got_object_blob_file_create(&id, &blob1_f,
3016 &blob1_size, path1);
3017 if (err)
3018 goto done;
3019 if (got_object_id_cmp(id, id1) == 0) {
3020 err = (*a->progress_cb)(a->progress_arg,
3021 GOT_STATUS_DELETE, path1);
3022 if (err)
3023 goto done;
3024 err = remove_ondisk_file(a->worktree->root_path,
3025 path1);
3026 if (err)
3027 goto done;
3028 if (ie)
3029 got_fileindex_entry_remove(a->fileindex,
3030 ie);
3031 } else {
3032 err = (*a->progress_cb)(a->progress_arg,
3033 GOT_STATUS_CANNOT_DELETE, path1);
3035 if (fclose(blob1_f) == EOF && err == NULL)
3036 err = got_error_from_errno("fclose");
3037 free(id);
3038 if (err)
3039 goto done;
3040 break;
3042 case GOT_STATUS_MODIFY:
3043 case GOT_STATUS_CONFLICT:
3044 err = (*a->progress_cb)(a->progress_arg,
3045 GOT_STATUS_CANNOT_DELETE, path1);
3046 if (err)
3047 goto done;
3048 break;
3049 case GOT_STATUS_OBSTRUCTED:
3050 err = (*a->progress_cb)(a->progress_arg, status, path1);
3051 if (err)
3052 goto done;
3053 break;
3054 default:
3055 break;
3057 } else if (blob2) {
3058 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3059 path2) == -1)
3060 return got_error_from_errno("asprintf");
3061 ie = got_fileindex_entry_get(a->fileindex, path2,
3062 strlen(path2));
3063 if (ie) {
3064 err = get_file_status(&status, &sb, ie, ondisk_path,
3065 -1, NULL, repo);
3066 if (err)
3067 goto done;
3068 if (status != GOT_STATUS_NO_CHANGE &&
3069 status != GOT_STATUS_MODIFY &&
3070 status != GOT_STATUS_CONFLICT &&
3071 status != GOT_STATUS_ADD) {
3072 err = (*a->progress_cb)(a->progress_arg,
3073 status, path2);
3074 goto done;
3076 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3077 char *link_target2;
3078 err = got_object_blob_read_to_str(&link_target2,
3079 blob2);
3080 if (err)
3081 goto done;
3082 err = merge_symlink(a->worktree, NULL,
3083 ondisk_path, path2, a->label_orig,
3084 link_target2, a->commit_id2, repo,
3085 a->progress_cb, a->progress_arg);
3086 free(link_target2);
3087 } else if (S_ISREG(sb.st_mode)) {
3088 err = merge_blob(&local_changes_subsumed,
3089 a->worktree, NULL, ondisk_path, path2,
3090 sb.st_mode, a->label_orig, blob2,
3091 a->commit_id2, repo, a->progress_cb,
3092 a->progress_arg);
3093 } else {
3094 err = got_error_path(ondisk_path,
3095 GOT_ERR_FILE_OBSTRUCTED);
3097 if (err)
3098 goto done;
3099 if (status == GOT_STATUS_DELETE) {
3100 err = got_fileindex_entry_update(ie,
3101 a->worktree->root_fd, path2, blob2->id.sha1,
3102 a->worktree->base_commit_id->sha1, 0);
3103 if (err)
3104 goto done;
3106 } else {
3107 int is_bad_symlink = 0;
3108 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3109 if (S_ISLNK(mode2)) {
3110 err = install_symlink(&is_bad_symlink,
3111 a->worktree, ondisk_path, path2, blob2, 0,
3112 0, 1, a->allow_bad_symlinks, repo,
3113 a->progress_cb, a->progress_arg);
3114 } else {
3115 err = install_blob(a->worktree, ondisk_path, path2,
3116 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3117 a->progress_cb, a->progress_arg);
3119 if (err)
3120 goto done;
3121 err = got_fileindex_entry_alloc(&ie, path2);
3122 if (err)
3123 goto done;
3124 err = got_fileindex_entry_update(ie,
3125 a->worktree->root_fd, path2, NULL, NULL, 1);
3126 if (err) {
3127 got_fileindex_entry_free(ie);
3128 goto done;
3130 err = got_fileindex_entry_add(a->fileindex, ie);
3131 if (err) {
3132 got_fileindex_entry_free(ie);
3133 goto done;
3135 if (is_bad_symlink) {
3136 got_fileindex_entry_filetype_set(ie,
3137 GOT_FILEIDX_MODE_BAD_SYMLINK);
3141 done:
3142 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3143 err = got_error_from_errno("fclose");
3144 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3145 err = got_error_from_errno("fclose");
3146 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3147 err = got_error_from_errno("fclose");
3148 free(id_str);
3149 free(label_deriv2);
3150 free(ondisk_path);
3151 return err;
3154 static const struct got_error *
3155 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3157 struct got_worktree *worktree = arg;
3159 /* Reject merges into a work tree with mixed base commits. */
3160 if (got_fileindex_entry_has_commit(ie) &&
3161 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3162 SHA1_DIGEST_LENGTH) != 0)
3163 return got_error(GOT_ERR_MIXED_COMMITS);
3165 return NULL;
3168 struct check_merge_conflicts_arg {
3169 struct got_worktree *worktree;
3170 struct got_fileindex *fileindex;
3171 struct got_repository *repo;
3174 static const struct got_error *
3175 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3176 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3177 struct got_object_id *id1, struct got_object_id *id2,
3178 const char *path1, const char *path2,
3179 mode_t mode1, mode_t mode2, struct got_repository *repo)
3181 const struct got_error *err = NULL;
3182 struct check_merge_conflicts_arg *a = arg;
3183 unsigned char status;
3184 struct stat sb;
3185 struct got_fileindex_entry *ie;
3186 const char *path = path2 ? path2 : path1;
3187 struct got_object_id *id = id2 ? id2 : id1;
3188 char *ondisk_path;
3190 if (id == NULL)
3191 return NULL;
3193 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3194 if (ie == NULL)
3195 return NULL;
3197 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3198 == -1)
3199 return got_error_from_errno("asprintf");
3201 /* Reject merges into a work tree with conflicted files. */
3202 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3203 free(ondisk_path);
3204 if (err)
3205 return err;
3206 if (status == GOT_STATUS_CONFLICT)
3207 return got_error(GOT_ERR_CONFLICTS);
3209 return NULL;
3212 static const struct got_error *
3213 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3214 const char *fileindex_path, struct got_object_id *commit_id1,
3215 struct got_object_id *commit_id2, struct got_repository *repo,
3216 got_worktree_checkout_cb progress_cb, void *progress_arg,
3217 got_cancel_cb cancel_cb, void *cancel_arg)
3219 const struct got_error *err = NULL, *sync_err;
3220 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3221 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3222 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3223 struct check_merge_conflicts_arg cmc_arg;
3224 struct merge_file_cb_arg arg;
3225 char *label_orig = NULL;
3226 FILE *f1 = NULL, *f2 = NULL;
3227 int fd1 = -1, fd2 = -1;
3229 if (commit_id1) {
3230 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3231 if (err)
3232 goto done;
3233 err = got_object_id_by_path(&tree_id1, repo, commit1,
3234 worktree->path_prefix);
3235 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3236 goto done;
3238 if (tree_id1) {
3239 char *id_str;
3241 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3242 if (err)
3243 goto done;
3245 err = got_object_id_str(&id_str, commit_id1);
3246 if (err)
3247 goto done;
3249 if (asprintf(&label_orig, "%s: commit %s",
3250 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3251 err = got_error_from_errno("asprintf");
3252 free(id_str);
3253 goto done;
3255 free(id_str);
3257 f1 = got_opentemp();
3258 if (f1 == NULL) {
3259 err = got_error_from_errno("got_opentemp");
3260 goto done;
3264 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3265 if (err)
3266 goto done;
3268 err = got_object_id_by_path(&tree_id2, repo, commit2,
3269 worktree->path_prefix);
3270 if (err)
3271 goto done;
3273 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3274 if (err)
3275 goto done;
3277 f2 = got_opentemp();
3278 if (f2 == NULL) {
3279 err = got_error_from_errno("got_opentemp");
3280 goto done;
3283 fd1 = got_opentempfd();
3284 if (fd1 == -1) {
3285 err = got_error_from_errno("got_opentempfd");
3286 goto done;
3289 fd2 = got_opentempfd();
3290 if (fd2 == -1) {
3291 err = got_error_from_errno("got_opentempfd");
3292 goto done;
3295 cmc_arg.worktree = worktree;
3296 cmc_arg.fileindex = fileindex;
3297 cmc_arg.repo = repo;
3298 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3299 check_merge_conflicts, &cmc_arg, 0);
3300 if (err)
3301 goto done;
3303 arg.worktree = worktree;
3304 arg.fileindex = fileindex;
3305 arg.progress_cb = progress_cb;
3306 arg.progress_arg = progress_arg;
3307 arg.cancel_cb = cancel_cb;
3308 arg.cancel_arg = cancel_arg;
3309 arg.label_orig = label_orig;
3310 arg.commit_id2 = commit_id2;
3311 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3312 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3313 merge_file_cb, &arg, 1);
3314 sync_err = sync_fileindex(fileindex, fileindex_path);
3315 if (sync_err && err == NULL)
3316 err = sync_err;
3317 done:
3318 if (commit1)
3319 got_object_commit_close(commit1);
3320 if (commit2)
3321 got_object_commit_close(commit2);
3322 if (tree1)
3323 got_object_tree_close(tree1);
3324 if (tree2)
3325 got_object_tree_close(tree2);
3326 if (f1 && fclose(f1) == EOF && err == NULL)
3327 err = got_error_from_errno("fclose");
3328 if (f2 && fclose(f2) == EOF && err == NULL)
3329 err = got_error_from_errno("fclose");
3330 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3331 err = got_error_from_errno("close");
3332 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3333 err = got_error_from_errno("close");
3334 free(label_orig);
3335 return err;
3338 const struct got_error *
3339 got_worktree_merge_files(struct got_worktree *worktree,
3340 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3341 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3342 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3344 const struct got_error *err, *unlockerr;
3345 char *fileindex_path = NULL;
3346 struct got_fileindex *fileindex = NULL;
3348 err = lock_worktree(worktree, LOCK_EX);
3349 if (err)
3350 return err;
3352 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3353 if (err)
3354 goto done;
3356 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3357 worktree);
3358 if (err)
3359 goto done;
3361 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3362 commit_id2, repo, progress_cb, progress_arg,
3363 cancel_cb, cancel_arg);
3364 done:
3365 if (fileindex)
3366 got_fileindex_free(fileindex);
3367 free(fileindex_path);
3368 unlockerr = lock_worktree(worktree, LOCK_SH);
3369 if (unlockerr && err == NULL)
3370 err = unlockerr;
3371 return err;
3374 struct diff_dir_cb_arg {
3375 struct got_fileindex *fileindex;
3376 struct got_worktree *worktree;
3377 const char *status_path;
3378 size_t status_path_len;
3379 struct got_repository *repo;
3380 got_worktree_status_cb status_cb;
3381 void *status_arg;
3382 got_cancel_cb cancel_cb;
3383 void *cancel_arg;
3384 /* A pathlist containing per-directory pathlists of ignore patterns. */
3385 struct got_pathlist_head *ignores;
3386 int report_unchanged;
3387 int no_ignores;
3390 static const struct got_error *
3391 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3392 int dirfd, const char *de_name,
3393 got_worktree_status_cb status_cb, void *status_arg,
3394 struct got_repository *repo, int report_unchanged)
3396 const struct got_error *err = NULL;
3397 unsigned char status = GOT_STATUS_NO_CHANGE;
3398 unsigned char staged_status;
3399 struct stat sb;
3400 struct got_object_id blob_id, commit_id, staged_blob_id;
3401 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3402 struct got_object_id *staged_blob_idp = NULL;
3404 staged_status = get_staged_status(ie);
3405 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3406 if (err)
3407 return err;
3409 if (status == GOT_STATUS_NO_CHANGE &&
3410 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3411 return NULL;
3413 if (got_fileindex_entry_has_blob(ie))
3414 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3415 if (got_fileindex_entry_has_commit(ie))
3416 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3417 if (staged_status == GOT_STATUS_ADD ||
3418 staged_status == GOT_STATUS_MODIFY) {
3419 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3420 &staged_blob_id, ie);
3423 return (*status_cb)(status_arg, status, staged_status,
3424 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3427 static const struct got_error *
3428 status_old_new(void *arg, struct got_fileindex_entry *ie,
3429 struct dirent *de, const char *parent_path, int dirfd)
3431 const struct got_error *err = NULL;
3432 struct diff_dir_cb_arg *a = arg;
3433 char *abspath;
3435 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3436 return got_error(GOT_ERR_CANCELLED);
3438 if (got_path_cmp(parent_path, a->status_path,
3439 strlen(parent_path), a->status_path_len) != 0 &&
3440 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3441 return NULL;
3443 if (parent_path[0]) {
3444 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3445 parent_path, de->d_name) == -1)
3446 return got_error_from_errno("asprintf");
3447 } else {
3448 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3449 de->d_name) == -1)
3450 return got_error_from_errno("asprintf");
3453 err = report_file_status(ie, abspath, dirfd, de->d_name,
3454 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3455 free(abspath);
3456 return err;
3459 static const struct got_error *
3460 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3462 struct diff_dir_cb_arg *a = arg;
3463 struct got_object_id blob_id, commit_id;
3464 unsigned char status;
3466 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3467 return got_error(GOT_ERR_CANCELLED);
3469 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3470 return NULL;
3472 got_fileindex_entry_get_blob_id(&blob_id, ie);
3473 got_fileindex_entry_get_commit_id(&commit_id, ie);
3474 if (got_fileindex_entry_has_file_on_disk(ie))
3475 status = GOT_STATUS_MISSING;
3476 else
3477 status = GOT_STATUS_DELETE;
3478 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3479 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3482 static void
3483 free_ignores(struct got_pathlist_head *ignores)
3485 struct got_pathlist_entry *pe;
3487 TAILQ_FOREACH(pe, ignores, entry) {
3488 struct got_pathlist_head *ignorelist = pe->data;
3490 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3492 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3495 static const struct got_error *
3496 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3498 const struct got_error *err = NULL;
3499 struct got_pathlist_entry *pe = NULL;
3500 struct got_pathlist_head *ignorelist;
3501 char *line = NULL, *pattern, *dirpath = NULL;
3502 size_t linesize = 0;
3503 ssize_t linelen;
3505 ignorelist = calloc(1, sizeof(*ignorelist));
3506 if (ignorelist == NULL)
3507 return got_error_from_errno("calloc");
3508 TAILQ_INIT(ignorelist);
3510 while ((linelen = getline(&line, &linesize, f)) != -1) {
3511 if (linelen > 0 && line[linelen - 1] == '\n')
3512 line[linelen - 1] = '\0';
3514 /* Git's ignores may contain comments. */
3515 if (line[0] == '#')
3516 continue;
3518 /* Git's negated patterns are not (yet?) supported. */
3519 if (line[0] == '!')
3520 continue;
3522 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3523 line) == -1) {
3524 err = got_error_from_errno("asprintf");
3525 goto done;
3527 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3528 if (err)
3529 goto done;
3531 if (ferror(f)) {
3532 err = got_error_from_errno("getline");
3533 goto done;
3536 dirpath = strdup(path);
3537 if (dirpath == NULL) {
3538 err = got_error_from_errno("strdup");
3539 goto done;
3541 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3542 done:
3543 free(line);
3544 if (err || pe == NULL) {
3545 free(dirpath);
3546 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3548 return err;
3551 static int
3552 match_path(const char *pattern, size_t pattern_len, const char *path,
3553 int flags)
3555 char buf[PATH_MAX];
3558 * Trailing slashes signify directories.
3559 * Append a * to make such patterns conform to fnmatch rules.
3561 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3562 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3563 return FNM_NOMATCH; /* XXX */
3565 return fnmatch(buf, path, flags);
3568 return fnmatch(pattern, path, flags);
3571 static int
3572 match_ignores(struct got_pathlist_head *ignores, const char *path)
3574 struct got_pathlist_entry *pe;
3576 /* Handle patterns which match in all directories. */
3577 TAILQ_FOREACH(pe, ignores, entry) {
3578 struct got_pathlist_head *ignorelist = pe->data;
3579 struct got_pathlist_entry *pi;
3581 TAILQ_FOREACH(pi, ignorelist, entry) {
3582 const char *p;
3584 if (pi->path_len < 3 ||
3585 strncmp(pi->path, "**/", 3) != 0)
3586 continue;
3587 p = path;
3588 while (*p) {
3589 if (match_path(pi->path + 3,
3590 pi->path_len - 3, p,
3591 FNM_PATHNAME | FNM_LEADING_DIR)) {
3592 /* Retry in next directory. */
3593 while (*p && *p != '/')
3594 p++;
3595 while (*p == '/')
3596 p++;
3597 continue;
3599 return 1;
3605 * The ignores pathlist contains ignore lists from children before
3606 * parents, so we can find the most specific ignorelist by walking
3607 * ignores backwards.
3609 pe = TAILQ_LAST(ignores, got_pathlist_head);
3610 while (pe) {
3611 if (got_path_is_child(path, pe->path, pe->path_len)) {
3612 struct got_pathlist_head *ignorelist = pe->data;
3613 struct got_pathlist_entry *pi;
3614 TAILQ_FOREACH(pi, ignorelist, entry) {
3615 int flags = FNM_LEADING_DIR;
3616 if (strstr(pi->path, "/**/") == NULL)
3617 flags |= FNM_PATHNAME;
3618 if (match_path(pi->path, pi->path_len,
3619 path, flags))
3620 continue;
3621 return 1;
3624 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3627 return 0;
3630 static const struct got_error *
3631 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3632 const char *path, int dirfd, const char *ignores_filename)
3634 const struct got_error *err = NULL;
3635 char *ignorespath;
3636 int fd = -1;
3637 FILE *ignoresfile = NULL;
3639 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3640 path[0] ? "/" : "", ignores_filename) == -1)
3641 return got_error_from_errno("asprintf");
3643 if (dirfd != -1) {
3644 fd = openat(dirfd, ignores_filename,
3645 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3646 if (fd == -1) {
3647 if (errno != ENOENT && errno != EACCES)
3648 err = got_error_from_errno2("openat",
3649 ignorespath);
3650 } else {
3651 ignoresfile = fdopen(fd, "r");
3652 if (ignoresfile == NULL)
3653 err = got_error_from_errno2("fdopen",
3654 ignorespath);
3655 else {
3656 fd = -1;
3657 err = read_ignores(ignores, path, ignoresfile);
3660 } else {
3661 ignoresfile = fopen(ignorespath, "re");
3662 if (ignoresfile == NULL) {
3663 if (errno != ENOENT && errno != EACCES)
3664 err = got_error_from_errno2("fopen",
3665 ignorespath);
3666 } else
3667 err = read_ignores(ignores, path, ignoresfile);
3670 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3671 err = got_error_from_errno2("fclose", path);
3672 if (fd != -1 && close(fd) == -1 && err == NULL)
3673 err = got_error_from_errno2("close", path);
3674 free(ignorespath);
3675 return err;
3678 static const struct got_error *
3679 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3680 int dirfd)
3682 const struct got_error *err = NULL;
3683 struct diff_dir_cb_arg *a = arg;
3684 char *path = NULL;
3686 if (ignore != NULL)
3687 *ignore = 0;
3689 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3690 return got_error(GOT_ERR_CANCELLED);
3692 if (parent_path[0]) {
3693 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3694 return got_error_from_errno("asprintf");
3695 } else {
3696 path = de->d_name;
3699 if (de->d_type == DT_DIR) {
3700 if (!a->no_ignores && ignore != NULL &&
3701 match_ignores(a->ignores, path))
3702 *ignore = 1;
3703 } else if (!match_ignores(a->ignores, path) &&
3704 got_path_is_child(path, a->status_path, a->status_path_len))
3705 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3706 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3707 if (parent_path[0])
3708 free(path);
3709 return err;
3712 static const struct got_error *
3713 status_traverse(void *arg, const char *path, int dirfd)
3715 const struct got_error *err = NULL;
3716 struct diff_dir_cb_arg *a = arg;
3718 if (a->no_ignores)
3719 return NULL;
3721 err = add_ignores(a->ignores, a->worktree->root_path,
3722 path, dirfd, ".cvsignore");
3723 if (err)
3724 return err;
3726 err = add_ignores(a->ignores, a->worktree->root_path, path,
3727 dirfd, ".gitignore");
3729 return err;
3732 static const struct got_error *
3733 report_single_file_status(const char *path, const char *ondisk_path,
3734 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3735 void *status_arg, struct got_repository *repo, int report_unchanged,
3736 struct got_pathlist_head *ignores, int no_ignores)
3738 struct got_fileindex_entry *ie;
3739 struct stat sb;
3741 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3742 if (ie)
3743 return report_file_status(ie, ondisk_path, -1, NULL,
3744 status_cb, status_arg, repo, report_unchanged);
3746 if (lstat(ondisk_path, &sb) == -1) {
3747 if (errno != ENOENT)
3748 return got_error_from_errno2("lstat", ondisk_path);
3749 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3750 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3753 if (!no_ignores && match_ignores(ignores, path))
3754 return NULL;
3756 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3757 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3758 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3760 return NULL;
3763 static const struct got_error *
3764 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3765 const char *root_path, const char *path)
3767 const struct got_error *err;
3768 char *parent_path, *next_parent_path = NULL;
3770 err = add_ignores(ignores, root_path, "", -1,
3771 ".cvsignore");
3772 if (err)
3773 return err;
3775 err = add_ignores(ignores, root_path, "", -1,
3776 ".gitignore");
3777 if (err)
3778 return err;
3780 err = got_path_dirname(&parent_path, path);
3781 if (err) {
3782 if (err->code == GOT_ERR_BAD_PATH)
3783 return NULL; /* cannot traverse parent */
3784 return err;
3786 for (;;) {
3787 err = add_ignores(ignores, root_path, parent_path, -1,
3788 ".cvsignore");
3789 if (err)
3790 break;
3791 err = add_ignores(ignores, root_path, parent_path, -1,
3792 ".gitignore");
3793 if (err)
3794 break;
3795 err = got_path_dirname(&next_parent_path, parent_path);
3796 if (err) {
3797 if (err->code == GOT_ERR_BAD_PATH)
3798 err = NULL; /* traversed everything */
3799 break;
3801 if (got_path_is_root_dir(parent_path))
3802 break;
3803 free(parent_path);
3804 parent_path = next_parent_path;
3805 next_parent_path = NULL;
3808 free(parent_path);
3809 free(next_parent_path);
3810 return err;
3813 static const struct got_error *
3814 worktree_status(struct got_worktree *worktree, const char *path,
3815 struct got_fileindex *fileindex, struct got_repository *repo,
3816 got_worktree_status_cb status_cb, void *status_arg,
3817 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3818 int report_unchanged)
3820 const struct got_error *err = NULL;
3821 int fd = -1;
3822 struct got_fileindex_diff_dir_cb fdiff_cb;
3823 struct diff_dir_cb_arg arg;
3824 char *ondisk_path = NULL;
3825 struct got_pathlist_head ignores;
3826 struct got_fileindex_entry *ie;
3828 TAILQ_INIT(&ignores);
3830 if (asprintf(&ondisk_path, "%s%s%s",
3831 worktree->root_path, path[0] ? "/" : "", path) == -1)
3832 return got_error_from_errno("asprintf");
3834 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3835 if (ie) {
3836 err = report_single_file_status(path, ondisk_path,
3837 fileindex, status_cb, status_arg, repo,
3838 report_unchanged, &ignores, no_ignores);
3839 goto done;
3842 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3843 if (fd == -1) {
3844 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3845 !got_err_open_nofollow_on_symlink())
3846 err = got_error_from_errno2("open", ondisk_path);
3847 else {
3848 if (!no_ignores) {
3849 err = add_ignores_from_parent_paths(&ignores,
3850 worktree->root_path, ondisk_path);
3851 if (err)
3852 goto done;
3854 err = report_single_file_status(path, ondisk_path,
3855 fileindex, status_cb, status_arg, repo,
3856 report_unchanged, &ignores, no_ignores);
3858 } else {
3859 fdiff_cb.diff_old_new = status_old_new;
3860 fdiff_cb.diff_old = status_old;
3861 fdiff_cb.diff_new = status_new;
3862 fdiff_cb.diff_traverse = status_traverse;
3863 arg.fileindex = fileindex;
3864 arg.worktree = worktree;
3865 arg.status_path = path;
3866 arg.status_path_len = strlen(path);
3867 arg.repo = repo;
3868 arg.status_cb = status_cb;
3869 arg.status_arg = status_arg;
3870 arg.cancel_cb = cancel_cb;
3871 arg.cancel_arg = cancel_arg;
3872 arg.report_unchanged = report_unchanged;
3873 arg.no_ignores = no_ignores;
3874 if (!no_ignores) {
3875 err = add_ignores_from_parent_paths(&ignores,
3876 worktree->root_path, path);
3877 if (err)
3878 goto done;
3880 arg.ignores = &ignores;
3881 err = got_fileindex_diff_dir(fileindex, fd,
3882 worktree->root_path, path, repo, &fdiff_cb, &arg);
3884 done:
3885 free_ignores(&ignores);
3886 if (fd != -1 && close(fd) == -1 && err == NULL)
3887 err = got_error_from_errno("close");
3888 free(ondisk_path);
3889 return err;
3892 const struct got_error *
3893 got_worktree_status(struct got_worktree *worktree,
3894 struct got_pathlist_head *paths, struct got_repository *repo,
3895 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3896 got_cancel_cb cancel_cb, void *cancel_arg)
3898 const struct got_error *err = NULL;
3899 char *fileindex_path = NULL;
3900 struct got_fileindex *fileindex = NULL;
3901 struct got_pathlist_entry *pe;
3903 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3904 if (err)
3905 return err;
3907 TAILQ_FOREACH(pe, paths, entry) {
3908 err = worktree_status(worktree, pe->path, fileindex, repo,
3909 status_cb, status_arg, cancel_cb, cancel_arg,
3910 no_ignores, 0);
3911 if (err)
3912 break;
3914 free(fileindex_path);
3915 got_fileindex_free(fileindex);
3916 return err;
3919 const struct got_error *
3920 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3921 const char *arg)
3923 const struct got_error *err = NULL;
3924 char *resolved = NULL, *cwd = NULL, *path = NULL;
3925 size_t len;
3926 struct stat sb;
3927 char *abspath = NULL;
3928 char canonpath[PATH_MAX];
3930 *wt_path = NULL;
3932 cwd = getcwd(NULL, 0);
3933 if (cwd == NULL)
3934 return got_error_from_errno("getcwd");
3936 if (lstat(arg, &sb) == -1) {
3937 if (errno != ENOENT) {
3938 err = got_error_from_errno2("lstat", arg);
3939 goto done;
3941 sb.st_mode = 0;
3943 if (S_ISLNK(sb.st_mode)) {
3945 * We cannot use realpath(3) with symlinks since we want to
3946 * operate on the symlink itself.
3947 * But we can make the path absolute, assuming it is relative
3948 * to the current working directory, and then canonicalize it.
3950 if (!got_path_is_absolute(arg)) {
3951 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3952 err = got_error_from_errno("asprintf");
3953 goto done;
3957 err = got_canonpath(abspath ? abspath : arg, canonpath,
3958 sizeof(canonpath));
3959 if (err)
3960 goto done;
3961 resolved = strdup(canonpath);
3962 if (resolved == NULL) {
3963 err = got_error_from_errno("strdup");
3964 goto done;
3966 } else {
3967 resolved = realpath(arg, NULL);
3968 if (resolved == NULL) {
3969 if (errno != ENOENT) {
3970 err = got_error_from_errno2("realpath", arg);
3971 goto done;
3973 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3974 err = got_error_from_errno("asprintf");
3975 goto done;
3977 err = got_canonpath(abspath, canonpath,
3978 sizeof(canonpath));
3979 if (err)
3980 goto done;
3981 resolved = strdup(canonpath);
3982 if (resolved == NULL) {
3983 err = got_error_from_errno("strdup");
3984 goto done;
3989 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3990 strlen(got_worktree_get_root_path(worktree)))) {
3991 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3992 goto done;
3995 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3996 err = got_path_skip_common_ancestor(&path,
3997 got_worktree_get_root_path(worktree), resolved);
3998 if (err)
3999 goto done;
4000 } else {
4001 path = strdup("");
4002 if (path == NULL) {
4003 err = got_error_from_errno("strdup");
4004 goto done;
4008 /* XXX status walk can't deal with trailing slash! */
4009 len = strlen(path);
4010 while (len > 0 && path[len - 1] == '/') {
4011 path[len - 1] = '\0';
4012 len--;
4014 done:
4015 free(abspath);
4016 free(resolved);
4017 free(cwd);
4018 if (err == NULL)
4019 *wt_path = path;
4020 else
4021 free(path);
4022 return err;
4025 struct schedule_addition_args {
4026 struct got_worktree *worktree;
4027 struct got_fileindex *fileindex;
4028 got_worktree_checkout_cb progress_cb;
4029 void *progress_arg;
4030 struct got_repository *repo;
4033 static const struct got_error *
4034 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4035 const char *relpath, struct got_object_id *blob_id,
4036 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4037 int dirfd, const char *de_name)
4039 struct schedule_addition_args *a = arg;
4040 const struct got_error *err = NULL;
4041 struct got_fileindex_entry *ie;
4042 struct stat sb;
4043 char *ondisk_path;
4045 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4046 relpath) == -1)
4047 return got_error_from_errno("asprintf");
4049 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4050 if (ie) {
4051 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4052 de_name, a->repo);
4053 if (err)
4054 goto done;
4055 /* Re-adding an existing entry is a no-op. */
4056 if (status == GOT_STATUS_ADD)
4057 goto done;
4058 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4059 if (err)
4060 goto done;
4063 if (status != GOT_STATUS_UNVERSIONED) {
4064 if (status == GOT_STATUS_NONEXISTENT)
4065 err = got_error_set_errno(ENOENT, ondisk_path);
4066 else
4067 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4068 goto done;
4071 err = got_fileindex_entry_alloc(&ie, relpath);
4072 if (err)
4073 goto done;
4074 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4075 relpath, NULL, NULL, 1);
4076 if (err) {
4077 got_fileindex_entry_free(ie);
4078 goto done;
4080 err = got_fileindex_entry_add(a->fileindex, ie);
4081 if (err) {
4082 got_fileindex_entry_free(ie);
4083 goto done;
4085 done:
4086 free(ondisk_path);
4087 if (err)
4088 return err;
4089 if (status == GOT_STATUS_ADD)
4090 return NULL;
4091 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4094 const struct got_error *
4095 got_worktree_schedule_add(struct got_worktree *worktree,
4096 struct got_pathlist_head *paths,
4097 got_worktree_checkout_cb progress_cb, void *progress_arg,
4098 struct got_repository *repo, int no_ignores)
4100 struct got_fileindex *fileindex = NULL;
4101 char *fileindex_path = NULL;
4102 const struct got_error *err = NULL, *sync_err, *unlockerr;
4103 struct got_pathlist_entry *pe;
4104 struct schedule_addition_args saa;
4106 err = lock_worktree(worktree, LOCK_EX);
4107 if (err)
4108 return err;
4110 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4111 if (err)
4112 goto done;
4114 saa.worktree = worktree;
4115 saa.fileindex = fileindex;
4116 saa.progress_cb = progress_cb;
4117 saa.progress_arg = progress_arg;
4118 saa.repo = repo;
4120 TAILQ_FOREACH(pe, paths, entry) {
4121 err = worktree_status(worktree, pe->path, fileindex, repo,
4122 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4123 if (err)
4124 break;
4126 sync_err = sync_fileindex(fileindex, fileindex_path);
4127 if (sync_err && err == NULL)
4128 err = sync_err;
4129 done:
4130 free(fileindex_path);
4131 if (fileindex)
4132 got_fileindex_free(fileindex);
4133 unlockerr = lock_worktree(worktree, LOCK_SH);
4134 if (unlockerr && err == NULL)
4135 err = unlockerr;
4136 return err;
4139 struct schedule_deletion_args {
4140 struct got_worktree *worktree;
4141 struct got_fileindex *fileindex;
4142 got_worktree_delete_cb progress_cb;
4143 void *progress_arg;
4144 struct got_repository *repo;
4145 int delete_local_mods;
4146 int keep_on_disk;
4147 int ignore_missing_paths;
4148 const char *status_codes;
4151 static const struct got_error *
4152 schedule_for_deletion(void *arg, unsigned char status,
4153 unsigned char staged_status, const char *relpath,
4154 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4155 struct got_object_id *commit_id, int dirfd, const char *de_name)
4157 struct schedule_deletion_args *a = arg;
4158 const struct got_error *err = NULL;
4159 struct got_fileindex_entry *ie = NULL;
4160 struct stat sb;
4161 char *ondisk_path;
4163 if (status == GOT_STATUS_NONEXISTENT) {
4164 if (a->ignore_missing_paths)
4165 return NULL;
4166 return got_error_set_errno(ENOENT, relpath);
4169 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4170 if (ie == NULL)
4171 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4173 staged_status = get_staged_status(ie);
4174 if (staged_status != GOT_STATUS_NO_CHANGE) {
4175 if (staged_status == GOT_STATUS_DELETE)
4176 return NULL;
4177 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4180 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4181 relpath) == -1)
4182 return got_error_from_errno("asprintf");
4184 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4185 a->repo);
4186 if (err)
4187 goto done;
4189 if (a->status_codes) {
4190 size_t ncodes = strlen(a->status_codes);
4191 int i;
4192 for (i = 0; i < ncodes ; i++) {
4193 if (status == a->status_codes[i])
4194 break;
4196 if (i == ncodes) {
4197 /* Do not delete files in non-matching status. */
4198 free(ondisk_path);
4199 return NULL;
4201 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4202 a->status_codes[i] != GOT_STATUS_MISSING) {
4203 static char msg[64];
4204 snprintf(msg, sizeof(msg),
4205 "invalid status code '%c'", a->status_codes[i]);
4206 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4207 goto done;
4211 if (status != GOT_STATUS_NO_CHANGE) {
4212 if (status == GOT_STATUS_DELETE)
4213 goto done;
4214 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4215 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4216 goto done;
4218 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4219 err = got_error_set_errno(ENOENT, relpath);
4220 goto done;
4222 if (status != GOT_STATUS_MODIFY &&
4223 status != GOT_STATUS_MISSING) {
4224 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4225 goto done;
4229 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4230 size_t root_len;
4232 if (dirfd != -1) {
4233 if (unlinkat(dirfd, de_name, 0) == -1) {
4234 err = got_error_from_errno2("unlinkat",
4235 ondisk_path);
4236 goto done;
4238 } else if (unlink(ondisk_path) == -1) {
4239 err = got_error_from_errno2("unlink", ondisk_path);
4240 goto done;
4243 root_len = strlen(a->worktree->root_path);
4244 do {
4245 char *parent;
4246 err = got_path_dirname(&parent, ondisk_path);
4247 if (err)
4248 goto done;
4249 free(ondisk_path);
4250 ondisk_path = parent;
4251 if (rmdir(ondisk_path) == -1) {
4252 if (errno != ENOTEMPTY)
4253 err = got_error_from_errno2("rmdir",
4254 ondisk_path);
4255 break;
4257 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4258 strlen(ondisk_path), root_len) != 0);
4261 got_fileindex_entry_mark_deleted_from_disk(ie);
4262 done:
4263 free(ondisk_path);
4264 if (err)
4265 return err;
4266 if (status == GOT_STATUS_DELETE)
4267 return NULL;
4268 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4269 staged_status, relpath);
4272 const struct got_error *
4273 got_worktree_schedule_delete(struct got_worktree *worktree,
4274 struct got_pathlist_head *paths, int delete_local_mods,
4275 const char *status_codes,
4276 got_worktree_delete_cb progress_cb, void *progress_arg,
4277 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4279 struct got_fileindex *fileindex = NULL;
4280 char *fileindex_path = NULL;
4281 const struct got_error *err = NULL, *sync_err, *unlockerr;
4282 struct got_pathlist_entry *pe;
4283 struct schedule_deletion_args sda;
4285 err = lock_worktree(worktree, LOCK_EX);
4286 if (err)
4287 return err;
4289 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4290 if (err)
4291 goto done;
4293 sda.worktree = worktree;
4294 sda.fileindex = fileindex;
4295 sda.progress_cb = progress_cb;
4296 sda.progress_arg = progress_arg;
4297 sda.repo = repo;
4298 sda.delete_local_mods = delete_local_mods;
4299 sda.keep_on_disk = keep_on_disk;
4300 sda.ignore_missing_paths = ignore_missing_paths;
4301 sda.status_codes = status_codes;
4303 TAILQ_FOREACH(pe, paths, entry) {
4304 err = worktree_status(worktree, pe->path, fileindex, repo,
4305 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4306 if (err)
4307 break;
4309 sync_err = sync_fileindex(fileindex, fileindex_path);
4310 if (sync_err && err == NULL)
4311 err = sync_err;
4312 done:
4313 free(fileindex_path);
4314 if (fileindex)
4315 got_fileindex_free(fileindex);
4316 unlockerr = lock_worktree(worktree, LOCK_SH);
4317 if (unlockerr && err == NULL)
4318 err = unlockerr;
4319 return err;
4322 static const struct got_error *
4323 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4325 const struct got_error *err = NULL;
4326 char *line = NULL;
4327 size_t linesize = 0, n;
4328 ssize_t linelen;
4330 linelen = getline(&line, &linesize, infile);
4331 if (linelen == -1) {
4332 if (ferror(infile)) {
4333 err = got_error_from_errno("getline");
4334 goto done;
4336 return NULL;
4338 if (outfile) {
4339 n = fwrite(line, 1, linelen, outfile);
4340 if (n != linelen) {
4341 err = got_ferror(outfile, GOT_ERR_IO);
4342 goto done;
4345 if (rejectfile) {
4346 n = fwrite(line, 1, linelen, rejectfile);
4347 if (n != linelen)
4348 err = got_ferror(rejectfile, GOT_ERR_IO);
4350 done:
4351 free(line);
4352 return err;
4355 static const struct got_error *
4356 skip_one_line(FILE *f)
4358 char *line = NULL;
4359 size_t linesize = 0;
4360 ssize_t linelen;
4362 linelen = getline(&line, &linesize, f);
4363 if (linelen == -1) {
4364 if (ferror(f))
4365 return got_error_from_errno("getline");
4366 return NULL;
4368 free(line);
4369 return NULL;
4372 static const struct got_error *
4373 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4374 int start_old, int end_old, int start_new, int end_new,
4375 FILE *outfile, FILE *rejectfile)
4377 const struct got_error *err;
4379 /* Copy old file's lines leading up to patch. */
4380 while (!feof(f1) && *line_cur1 < start_old) {
4381 err = copy_one_line(f1, outfile, NULL);
4382 if (err)
4383 return err;
4384 (*line_cur1)++;
4386 /* Skip new file's lines leading up to patch. */
4387 while (!feof(f2) && *line_cur2 < start_new) {
4388 if (rejectfile)
4389 err = copy_one_line(f2, NULL, rejectfile);
4390 else
4391 err = skip_one_line(f2);
4392 if (err)
4393 return err;
4394 (*line_cur2)++;
4396 /* Copy patched lines. */
4397 while (!feof(f2) && *line_cur2 <= end_new) {
4398 err = copy_one_line(f2, outfile, NULL);
4399 if (err)
4400 return err;
4401 (*line_cur2)++;
4403 /* Skip over old file's replaced lines. */
4404 while (!feof(f1) && *line_cur1 <= end_old) {
4405 if (rejectfile)
4406 err = copy_one_line(f1, NULL, rejectfile);
4407 else
4408 err = skip_one_line(f1);
4409 if (err)
4410 return err;
4411 (*line_cur1)++;
4414 return NULL;
4417 static const struct got_error *
4418 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4419 FILE *outfile, FILE *rejectfile)
4421 const struct got_error *err;
4423 if (outfile) {
4424 /* Copy old file's lines until EOF. */
4425 while (!feof(f1)) {
4426 err = copy_one_line(f1, outfile, NULL);
4427 if (err)
4428 return err;
4429 (*line_cur1)++;
4432 if (rejectfile) {
4433 /* Copy new file's lines until EOF. */
4434 while (!feof(f2)) {
4435 err = copy_one_line(f2, NULL, rejectfile);
4436 if (err)
4437 return err;
4438 (*line_cur2)++;
4442 return NULL;
4445 static const struct got_error *
4446 apply_or_reject_change(int *choice, int *nchunks_used,
4447 struct diff_result *diff_result, int n,
4448 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4449 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4450 got_worktree_patch_cb patch_cb, void *patch_arg)
4452 const struct got_error *err = NULL;
4453 struct diff_chunk_context cc = {};
4454 int start_old, end_old, start_new, end_new;
4455 FILE *hunkfile;
4456 struct diff_output_unidiff_state *diff_state;
4457 struct diff_input_info diff_info;
4458 int rc;
4460 *choice = GOT_PATCH_CHOICE_NONE;
4462 /* Get changed line numbers without context lines for copy_change(). */
4463 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4464 start_old = cc.left.start;
4465 end_old = cc.left.end;
4466 start_new = cc.right.start;
4467 end_new = cc.right.end;
4469 /* Get the same change with context lines for display. */
4470 memset(&cc, 0, sizeof(cc));
4471 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4473 memset(&diff_info, 0, sizeof(diff_info));
4474 diff_info.left_path = relpath;
4475 diff_info.right_path = relpath;
4477 diff_state = diff_output_unidiff_state_alloc();
4478 if (diff_state == NULL)
4479 return got_error_set_errno(ENOMEM,
4480 "diff_output_unidiff_state_alloc");
4482 hunkfile = got_opentemp();
4483 if (hunkfile == NULL) {
4484 err = got_error_from_errno("got_opentemp");
4485 goto done;
4488 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4489 diff_result, &cc);
4490 if (rc != DIFF_RC_OK) {
4491 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4492 goto done;
4495 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4496 err = got_ferror(hunkfile, GOT_ERR_IO);
4497 goto done;
4500 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4501 hunkfile, changeno, nchanges);
4502 if (err)
4503 goto done;
4505 switch (*choice) {
4506 case GOT_PATCH_CHOICE_YES:
4507 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4508 end_old, start_new, end_new, outfile, rejectfile);
4509 break;
4510 case GOT_PATCH_CHOICE_NO:
4511 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4512 end_old, start_new, end_new, rejectfile, outfile);
4513 break;
4514 case GOT_PATCH_CHOICE_QUIT:
4515 break;
4516 default:
4517 err = got_error(GOT_ERR_PATCH_CHOICE);
4518 break;
4520 done:
4521 diff_output_unidiff_state_free(diff_state);
4522 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4523 err = got_error_from_errno("fclose");
4524 return err;
4527 struct revert_file_args {
4528 struct got_worktree *worktree;
4529 struct got_fileindex *fileindex;
4530 got_worktree_checkout_cb progress_cb;
4531 void *progress_arg;
4532 got_worktree_patch_cb patch_cb;
4533 void *patch_arg;
4534 struct got_repository *repo;
4535 int unlink_added_files;
4538 static const struct got_error *
4539 create_patched_content(char **path_outfile, int reverse_patch,
4540 struct got_object_id *blob_id, const char *path2,
4541 int dirfd2, const char *de_name2,
4542 const char *relpath, struct got_repository *repo,
4543 got_worktree_patch_cb patch_cb, void *patch_arg)
4545 const struct got_error *err, *free_err;
4546 struct got_blob_object *blob = NULL;
4547 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4548 int fd = -1, fd2 = -1;
4549 char link_target[PATH_MAX];
4550 ssize_t link_len = 0;
4551 char *path1 = NULL, *id_str = NULL;
4552 struct stat sb2;
4553 struct got_diffreg_result *diffreg_result = NULL;
4554 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4555 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4557 *path_outfile = NULL;
4559 err = got_object_id_str(&id_str, blob_id);
4560 if (err)
4561 return err;
4563 if (dirfd2 != -1) {
4564 fd2 = openat(dirfd2, de_name2,
4565 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4566 if (fd2 == -1) {
4567 if (!got_err_open_nofollow_on_symlink()) {
4568 err = got_error_from_errno2("openat", path2);
4569 goto done;
4571 link_len = readlinkat(dirfd2, de_name2,
4572 link_target, sizeof(link_target));
4573 if (link_len == -1) {
4574 return got_error_from_errno2("readlinkat",
4575 path2);
4577 sb2.st_mode = S_IFLNK;
4578 sb2.st_size = link_len;
4580 } else {
4581 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4582 if (fd2 == -1) {
4583 if (!got_err_open_nofollow_on_symlink()) {
4584 err = got_error_from_errno2("open", path2);
4585 goto done;
4587 link_len = readlink(path2, link_target,
4588 sizeof(link_target));
4589 if (link_len == -1)
4590 return got_error_from_errno2("readlink", path2);
4591 sb2.st_mode = S_IFLNK;
4592 sb2.st_size = link_len;
4595 if (fd2 != -1) {
4596 if (fstat(fd2, &sb2) == -1) {
4597 err = got_error_from_errno2("fstat", path2);
4598 goto done;
4601 f2 = fdopen(fd2, "r");
4602 if (f2 == NULL) {
4603 err = got_error_from_errno2("fdopen", path2);
4604 goto done;
4606 fd2 = -1;
4607 } else {
4608 size_t n;
4609 f2 = got_opentemp();
4610 if (f2 == NULL) {
4611 err = got_error_from_errno2("got_opentemp", path2);
4612 goto done;
4614 n = fwrite(link_target, 1, link_len, f2);
4615 if (n != link_len) {
4616 err = got_ferror(f2, GOT_ERR_IO);
4617 goto done;
4619 if (fflush(f2) == EOF) {
4620 err = got_error_from_errno("fflush");
4621 goto done;
4623 rewind(f2);
4626 fd = got_opentempfd();
4627 if (fd == -1) {
4628 err = got_error_from_errno("got_opentempfd");
4629 goto done;
4632 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4633 if (err)
4634 goto done;
4636 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4637 if (err)
4638 goto done;
4640 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4641 if (err)
4642 goto done;
4644 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4645 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4646 if (err)
4647 goto done;
4649 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4650 "");
4651 if (err)
4652 goto done;
4654 if (fseek(f1, 0L, SEEK_SET) == -1)
4655 return got_ferror(f1, GOT_ERR_IO);
4656 if (fseek(f2, 0L, SEEK_SET) == -1)
4657 return got_ferror(f2, GOT_ERR_IO);
4659 /* Count the number of actual changes in the diff result. */
4660 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4661 struct diff_chunk_context cc = {};
4662 diff_chunk_context_load_change(&cc, &nchunks_used,
4663 diffreg_result->result, n, 0);
4664 nchanges++;
4666 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4667 int choice;
4668 err = apply_or_reject_change(&choice, &nchunks_used,
4669 diffreg_result->result, n, relpath, f1, f2,
4670 &line_cur1, &line_cur2,
4671 reverse_patch ? NULL : outfile,
4672 reverse_patch ? outfile : NULL,
4673 ++i, nchanges, patch_cb, patch_arg);
4674 if (err)
4675 goto done;
4676 if (choice == GOT_PATCH_CHOICE_YES)
4677 have_content = 1;
4678 else if (choice == GOT_PATCH_CHOICE_QUIT)
4679 break;
4681 if (have_content) {
4682 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4683 reverse_patch ? NULL : outfile,
4684 reverse_patch ? outfile : NULL);
4685 if (err)
4686 goto done;
4688 if (!S_ISLNK(sb2.st_mode)) {
4689 mode_t mode;
4691 mode = apply_umask(sb2.st_mode);
4692 if (fchmod(fileno(outfile), mode) == -1) {
4693 err = got_error_from_errno2("fchmod", path2);
4694 goto done;
4698 done:
4699 free(id_str);
4700 if (fd != -1 && close(fd) == -1 && err == NULL)
4701 err = got_error_from_errno("close");
4702 if (blob)
4703 got_object_blob_close(blob);
4704 free_err = got_diffreg_result_free(diffreg_result);
4705 if (err == NULL)
4706 err = free_err;
4707 if (f1 && fclose(f1) == EOF && err == NULL)
4708 err = got_error_from_errno2("fclose", path1);
4709 if (f2 && fclose(f2) == EOF && err == NULL)
4710 err = got_error_from_errno2("fclose", path2);
4711 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4712 err = got_error_from_errno2("close", path2);
4713 if (outfile && fclose(outfile) == EOF && err == NULL)
4714 err = got_error_from_errno2("fclose", *path_outfile);
4715 if (path1 && unlink(path1) == -1 && err == NULL)
4716 err = got_error_from_errno2("unlink", path1);
4717 if (err || !have_content) {
4718 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4719 err = got_error_from_errno2("unlink", *path_outfile);
4720 free(*path_outfile);
4721 *path_outfile = NULL;
4723 free(path1);
4724 return err;
4727 static const struct got_error *
4728 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4729 const char *relpath, struct got_object_id *blob_id,
4730 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4731 int dirfd, const char *de_name)
4733 struct revert_file_args *a = arg;
4734 const struct got_error *err = NULL;
4735 char *parent_path = NULL;
4736 struct got_fileindex_entry *ie;
4737 struct got_commit_object *base_commit = NULL;
4738 struct got_tree_object *tree = NULL;
4739 struct got_object_id *tree_id = NULL;
4740 const struct got_tree_entry *te = NULL;
4741 char *tree_path = NULL, *te_name;
4742 char *ondisk_path = NULL, *path_content = NULL;
4743 struct got_blob_object *blob = NULL;
4744 int fd = -1;
4746 /* Reverting a staged deletion is a no-op. */
4747 if (status == GOT_STATUS_DELETE &&
4748 staged_status != GOT_STATUS_NO_CHANGE)
4749 return NULL;
4751 if (status == GOT_STATUS_UNVERSIONED)
4752 return (*a->progress_cb)(a->progress_arg,
4753 GOT_STATUS_UNVERSIONED, relpath);
4755 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4756 if (ie == NULL)
4757 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4759 /* Construct in-repository path of tree which contains this blob. */
4760 err = got_path_dirname(&parent_path, ie->path);
4761 if (err) {
4762 if (err->code != GOT_ERR_BAD_PATH)
4763 goto done;
4764 parent_path = strdup("/");
4765 if (parent_path == NULL) {
4766 err = got_error_from_errno("strdup");
4767 goto done;
4770 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4771 tree_path = strdup(parent_path);
4772 if (tree_path == NULL) {
4773 err = got_error_from_errno("strdup");
4774 goto done;
4776 } else {
4777 if (got_path_is_root_dir(parent_path)) {
4778 tree_path = strdup(a->worktree->path_prefix);
4779 if (tree_path == NULL) {
4780 err = got_error_from_errno("strdup");
4781 goto done;
4783 } else {
4784 if (asprintf(&tree_path, "%s/%s",
4785 a->worktree->path_prefix, parent_path) == -1) {
4786 err = got_error_from_errno("asprintf");
4787 goto done;
4792 err = got_object_open_as_commit(&base_commit, a->repo,
4793 a->worktree->base_commit_id);
4794 if (err)
4795 goto done;
4797 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4798 if (err) {
4799 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4800 (status == GOT_STATUS_ADD ||
4801 staged_status == GOT_STATUS_ADD)))
4802 goto done;
4803 } else {
4804 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4805 if (err)
4806 goto done;
4808 err = got_path_basename(&te_name, ie->path);
4809 if (err)
4810 goto done;
4812 te = got_object_tree_find_entry(tree, te_name);
4813 free(te_name);
4814 if (te == NULL && status != GOT_STATUS_ADD &&
4815 staged_status != GOT_STATUS_ADD) {
4816 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4817 goto done;
4821 switch (status) {
4822 case GOT_STATUS_ADD:
4823 if (a->patch_cb) {
4824 int choice = GOT_PATCH_CHOICE_NONE;
4825 err = (*a->patch_cb)(&choice, a->patch_arg,
4826 status, ie->path, NULL, 1, 1);
4827 if (err)
4828 goto done;
4829 if (choice != GOT_PATCH_CHOICE_YES)
4830 break;
4832 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4833 ie->path);
4834 if (err)
4835 goto done;
4836 got_fileindex_entry_remove(a->fileindex, ie);
4837 if (a->unlink_added_files) {
4838 if (asprintf(&ondisk_path, "%s/%s",
4839 got_worktree_get_root_path(a->worktree),
4840 relpath) == -1) {
4841 err = got_error_from_errno("asprintf");
4842 goto done;
4844 if (unlink(ondisk_path) == -1) {
4845 err = got_error_from_errno2("unlink",
4846 ondisk_path);
4847 break;
4850 break;
4851 case GOT_STATUS_DELETE:
4852 if (a->patch_cb) {
4853 int choice = GOT_PATCH_CHOICE_NONE;
4854 err = (*a->patch_cb)(&choice, a->patch_arg,
4855 status, ie->path, NULL, 1, 1);
4856 if (err)
4857 goto done;
4858 if (choice != GOT_PATCH_CHOICE_YES)
4859 break;
4861 /* fall through */
4862 case GOT_STATUS_MODIFY:
4863 case GOT_STATUS_MODE_CHANGE:
4864 case GOT_STATUS_CONFLICT:
4865 case GOT_STATUS_MISSING: {
4866 struct got_object_id id;
4867 if (staged_status == GOT_STATUS_ADD ||
4868 staged_status == GOT_STATUS_MODIFY)
4869 got_fileindex_entry_get_staged_blob_id(&id, ie);
4870 else
4871 got_fileindex_entry_get_blob_id(&id, ie);
4872 fd = got_opentempfd();
4873 if (fd == -1) {
4874 err = got_error_from_errno("got_opentempfd");
4875 goto done;
4878 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4879 if (err)
4880 goto done;
4882 if (asprintf(&ondisk_path, "%s/%s",
4883 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4884 err = got_error_from_errno("asprintf");
4885 goto done;
4888 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4889 status == GOT_STATUS_CONFLICT)) {
4890 int is_bad_symlink = 0;
4891 err = create_patched_content(&path_content, 1, &id,
4892 ondisk_path, dirfd, de_name, ie->path, a->repo,
4893 a->patch_cb, a->patch_arg);
4894 if (err || path_content == NULL)
4895 break;
4896 if (te && S_ISLNK(te->mode)) {
4897 if (unlink(path_content) == -1) {
4898 err = got_error_from_errno2("unlink",
4899 path_content);
4900 break;
4902 err = install_symlink(&is_bad_symlink,
4903 a->worktree, ondisk_path, ie->path,
4904 blob, 0, 1, 0, 0, a->repo,
4905 a->progress_cb, a->progress_arg);
4906 } else {
4907 if (rename(path_content, ondisk_path) == -1) {
4908 err = got_error_from_errno3("rename",
4909 path_content, ondisk_path);
4910 goto done;
4913 } else {
4914 int is_bad_symlink = 0;
4915 if (te && S_ISLNK(te->mode)) {
4916 err = install_symlink(&is_bad_symlink,
4917 a->worktree, ondisk_path, ie->path,
4918 blob, 0, 1, 0, 0, a->repo,
4919 a->progress_cb, a->progress_arg);
4920 } else {
4921 err = install_blob(a->worktree, ondisk_path,
4922 ie->path,
4923 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4924 got_fileindex_perms_to_st(ie), blob,
4925 0, 1, 0, 0, a->repo,
4926 a->progress_cb, a->progress_arg);
4928 if (err)
4929 goto done;
4930 if (status == GOT_STATUS_DELETE ||
4931 status == GOT_STATUS_MODE_CHANGE) {
4932 err = got_fileindex_entry_update(ie,
4933 a->worktree->root_fd, relpath,
4934 blob->id.sha1,
4935 a->worktree->base_commit_id->sha1, 1);
4936 if (err)
4937 goto done;
4939 if (is_bad_symlink) {
4940 got_fileindex_entry_filetype_set(ie,
4941 GOT_FILEIDX_MODE_BAD_SYMLINK);
4944 break;
4946 default:
4947 break;
4949 done:
4950 free(ondisk_path);
4951 free(path_content);
4952 free(parent_path);
4953 free(tree_path);
4954 if (fd != -1 && close(fd) == -1 && err == NULL)
4955 err = got_error_from_errno("close");
4956 if (blob)
4957 got_object_blob_close(blob);
4958 if (tree)
4959 got_object_tree_close(tree);
4960 free(tree_id);
4961 if (base_commit)
4962 got_object_commit_close(base_commit);
4963 return err;
4966 const struct got_error *
4967 got_worktree_revert(struct got_worktree *worktree,
4968 struct got_pathlist_head *paths,
4969 got_worktree_checkout_cb progress_cb, void *progress_arg,
4970 got_worktree_patch_cb patch_cb, void *patch_arg,
4971 struct got_repository *repo)
4973 struct got_fileindex *fileindex = NULL;
4974 char *fileindex_path = NULL;
4975 const struct got_error *err = NULL, *unlockerr = NULL;
4976 const struct got_error *sync_err = NULL;
4977 struct got_pathlist_entry *pe;
4978 struct revert_file_args rfa;
4980 err = lock_worktree(worktree, LOCK_EX);
4981 if (err)
4982 return err;
4984 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4985 if (err)
4986 goto done;
4988 rfa.worktree = worktree;
4989 rfa.fileindex = fileindex;
4990 rfa.progress_cb = progress_cb;
4991 rfa.progress_arg = progress_arg;
4992 rfa.patch_cb = patch_cb;
4993 rfa.patch_arg = patch_arg;
4994 rfa.repo = repo;
4995 rfa.unlink_added_files = 0;
4996 TAILQ_FOREACH(pe, paths, entry) {
4997 err = worktree_status(worktree, pe->path, fileindex, repo,
4998 revert_file, &rfa, NULL, NULL, 1, 0);
4999 if (err)
5000 break;
5002 sync_err = sync_fileindex(fileindex, fileindex_path);
5003 if (sync_err && err == NULL)
5004 err = sync_err;
5005 done:
5006 free(fileindex_path);
5007 if (fileindex)
5008 got_fileindex_free(fileindex);
5009 unlockerr = lock_worktree(worktree, LOCK_SH);
5010 if (unlockerr && err == NULL)
5011 err = unlockerr;
5012 return err;
5015 static void
5016 free_commitable(struct got_commitable *ct)
5018 free(ct->path);
5019 free(ct->in_repo_path);
5020 free(ct->ondisk_path);
5021 free(ct->blob_id);
5022 free(ct->base_blob_id);
5023 free(ct->staged_blob_id);
5024 free(ct->base_commit_id);
5025 free(ct);
5028 struct collect_commitables_arg {
5029 struct got_pathlist_head *commitable_paths;
5030 struct got_repository *repo;
5031 struct got_worktree *worktree;
5032 struct got_fileindex *fileindex;
5033 int have_staged_files;
5034 int allow_bad_symlinks;
5035 int diff_header_shown;
5036 int commit_conflicts;
5037 FILE *diff_outfile;
5038 FILE *f1;
5039 FILE *f2;
5043 * Create a file which contains the target path of a symlink so we can feed
5044 * it as content to the diff engine.
5046 static const struct got_error *
5047 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5048 const char *abspath)
5050 const struct got_error *err = NULL;
5051 char target_path[PATH_MAX];
5052 ssize_t target_len, outlen;
5054 *fd = -1;
5056 if (dirfd != -1) {
5057 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5058 if (target_len == -1)
5059 return got_error_from_errno2("readlinkat", abspath);
5060 } else {
5061 target_len = readlink(abspath, target_path, PATH_MAX);
5062 if (target_len == -1)
5063 return got_error_from_errno2("readlink", abspath);
5066 *fd = got_opentempfd();
5067 if (*fd == -1)
5068 return got_error_from_errno("got_opentempfd");
5070 outlen = write(*fd, target_path, target_len);
5071 if (outlen == -1) {
5072 err = got_error_from_errno("got_opentempfd");
5073 goto done;
5076 if (lseek(*fd, 0, SEEK_SET) == -1) {
5077 err = got_error_from_errno2("lseek", abspath);
5078 goto done;
5080 done:
5081 if (err) {
5082 close(*fd);
5083 *fd = -1;
5085 return err;
5088 static const struct got_error *
5089 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5090 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5091 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5093 const struct got_error *err = NULL;
5094 struct got_blob_object *blob1 = NULL;
5095 int fd = -1, fd1 = -1, fd2 = -1;
5096 FILE *ondisk_file = NULL;
5097 char *label1 = NULL;
5098 struct stat sb;
5099 off_t size1 = 0;
5100 int f2_exists = 0;
5101 char *id_str = NULL;
5103 memset(&sb, 0, sizeof(sb));
5105 if (diff_staged) {
5106 if (ct->staged_status != GOT_STATUS_MODIFY &&
5107 ct->staged_status != GOT_STATUS_ADD &&
5108 ct->staged_status != GOT_STATUS_DELETE)
5109 return NULL;
5110 } else {
5111 if (ct->status != GOT_STATUS_MODIFY &&
5112 ct->status != GOT_STATUS_ADD &&
5113 ct->status != GOT_STATUS_DELETE &&
5114 ct->status != GOT_STATUS_CONFLICT)
5115 return NULL;
5118 err = got_opentemp_truncate(f1);
5119 if (err)
5120 return got_error_from_errno("got_opentemp_truncate");
5121 err = got_opentemp_truncate(f2);
5122 if (err)
5123 return got_error_from_errno("got_opentemp_truncate");
5125 if (!*diff_header_shown) {
5126 err = got_object_id_str(&id_str, worktree->base_commit_id);
5127 if (err)
5128 return err;
5129 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5130 got_worktree_get_root_path(worktree));
5131 fprintf(diff_outfile, "commit - %s\n", id_str);
5132 fprintf(diff_outfile, "path + %s%s\n",
5133 got_worktree_get_root_path(worktree),
5134 diff_staged ? " (staged changes)" : "");
5135 *diff_header_shown = 1;
5138 if (diff_staged) {
5139 const char *label1 = NULL, *label2 = NULL;
5140 switch (ct->staged_status) {
5141 case GOT_STATUS_MODIFY:
5142 label1 = ct->path;
5143 label2 = ct->path;
5144 break;
5145 case GOT_STATUS_ADD:
5146 label2 = ct->path;
5147 break;
5148 case GOT_STATUS_DELETE:
5149 label1 = ct->path;
5150 break;
5151 default:
5152 return got_error(GOT_ERR_FILE_STATUS);
5154 fd1 = got_opentempfd();
5155 if (fd1 == -1) {
5156 err = got_error_from_errno("got_opentempfd");
5157 goto done;
5159 fd2 = got_opentempfd();
5160 if (fd2 == -1) {
5161 err = got_error_from_errno("got_opentempfd");
5162 goto done;
5164 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5165 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5166 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5167 NULL, repo, diff_outfile);
5168 goto done;
5171 fd1 = got_opentempfd();
5172 if (fd1 == -1) {
5173 err = got_error_from_errno("got_opentempfd");
5174 goto done;
5177 if (ct->status != GOT_STATUS_ADD) {
5178 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5179 8192, fd1);
5180 if (err)
5181 goto done;
5184 if (ct->status != GOT_STATUS_DELETE) {
5185 if (dirfd != -1) {
5186 fd = openat(dirfd, de_name,
5187 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5188 if (fd == -1) {
5189 if (!got_err_open_nofollow_on_symlink()) {
5190 err = got_error_from_errno2("openat",
5191 ct->ondisk_path);
5192 goto done;
5194 err = get_symlink_target_file(&fd, dirfd,
5195 de_name, ct->ondisk_path);
5196 if (err)
5197 goto done;
5199 } else {
5200 fd = open(ct->ondisk_path,
5201 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5202 if (fd == -1) {
5203 if (!got_err_open_nofollow_on_symlink()) {
5204 err = got_error_from_errno2("open",
5205 ct->ondisk_path);
5206 goto done;
5208 err = get_symlink_target_file(&fd, dirfd,
5209 de_name, ct->ondisk_path);
5210 if (err)
5211 goto done;
5214 if (fstatat(fd, ct->ondisk_path, &sb,
5215 AT_SYMLINK_NOFOLLOW) == -1) {
5216 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5217 goto done;
5219 ondisk_file = fdopen(fd, "r");
5220 if (ondisk_file == NULL) {
5221 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5222 goto done;
5224 fd = -1;
5225 f2_exists = 1;
5228 if (blob1) {
5229 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5230 f1, blob1);
5231 if (err)
5232 goto done;
5235 err = got_diff_blob_file(blob1, f1, size1, label1,
5236 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5237 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5238 done:
5239 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5240 err = got_error_from_errno("close");
5241 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5242 err = got_error_from_errno("close");
5243 if (blob1)
5244 got_object_blob_close(blob1);
5245 if (fd != -1 && close(fd) == -1 && err == NULL)
5246 err = got_error_from_errno("close");
5247 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5248 err = got_error_from_errno("fclose");
5249 return err;
5252 static const struct got_error *
5253 collect_commitables(void *arg, unsigned char status,
5254 unsigned char staged_status, const char *relpath,
5255 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5256 struct got_object_id *commit_id, int dirfd, const char *de_name)
5258 struct collect_commitables_arg *a = arg;
5259 const struct got_error *err = NULL;
5260 struct got_commitable *ct = NULL;
5261 struct got_pathlist_entry *new = NULL;
5262 char *parent_path = NULL, *path = NULL;
5263 struct stat sb;
5265 if (a->have_staged_files) {
5266 if (staged_status != GOT_STATUS_MODIFY &&
5267 staged_status != GOT_STATUS_ADD &&
5268 staged_status != GOT_STATUS_DELETE)
5269 return NULL;
5270 } else {
5271 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5272 printf("C %s\n", relpath);
5273 return got_error(GOT_ERR_COMMIT_CONFLICT);
5276 if (status != GOT_STATUS_MODIFY &&
5277 status != GOT_STATUS_MODE_CHANGE &&
5278 status != GOT_STATUS_ADD &&
5279 status != GOT_STATUS_DELETE &&
5280 status != GOT_STATUS_CONFLICT)
5281 return NULL;
5284 if (asprintf(&path, "/%s", relpath) == -1) {
5285 err = got_error_from_errno("asprintf");
5286 goto done;
5288 if (strcmp(path, "/") == 0) {
5289 parent_path = strdup("");
5290 if (parent_path == NULL)
5291 return got_error_from_errno("strdup");
5292 } else {
5293 err = got_path_dirname(&parent_path, path);
5294 if (err)
5295 return err;
5298 ct = calloc(1, sizeof(*ct));
5299 if (ct == NULL) {
5300 err = got_error_from_errno("calloc");
5301 goto done;
5304 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5305 relpath) == -1) {
5306 err = got_error_from_errno("asprintf");
5307 goto done;
5310 if (staged_status == GOT_STATUS_ADD ||
5311 staged_status == GOT_STATUS_MODIFY) {
5312 struct got_fileindex_entry *ie;
5313 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5314 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5315 case GOT_FILEIDX_MODE_REGULAR_FILE:
5316 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5317 ct->mode = S_IFREG;
5318 break;
5319 case GOT_FILEIDX_MODE_SYMLINK:
5320 ct->mode = S_IFLNK;
5321 break;
5322 default:
5323 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5324 goto done;
5326 ct->mode |= got_fileindex_entry_perms_get(ie);
5327 } else if (status != GOT_STATUS_DELETE &&
5328 staged_status != GOT_STATUS_DELETE) {
5329 if (dirfd != -1) {
5330 if (fstatat(dirfd, de_name, &sb,
5331 AT_SYMLINK_NOFOLLOW) == -1) {
5332 err = got_error_from_errno2("fstatat",
5333 ct->ondisk_path);
5334 goto done;
5336 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5337 err = got_error_from_errno2("lstat", ct->ondisk_path);
5338 goto done;
5340 ct->mode = sb.st_mode;
5343 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5344 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5345 relpath) == -1) {
5346 err = got_error_from_errno("asprintf");
5347 goto done;
5350 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5351 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5352 int is_bad_symlink;
5353 char target_path[PATH_MAX];
5354 ssize_t target_len;
5355 target_len = readlink(ct->ondisk_path, target_path,
5356 sizeof(target_path));
5357 if (target_len == -1) {
5358 err = got_error_from_errno2("readlink",
5359 ct->ondisk_path);
5360 goto done;
5362 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5363 target_len, ct->ondisk_path, a->worktree->root_path);
5364 if (err)
5365 goto done;
5366 if (is_bad_symlink) {
5367 err = got_error_path(ct->ondisk_path,
5368 GOT_ERR_BAD_SYMLINK);
5369 goto done;
5374 ct->status = status;
5375 ct->staged_status = staged_status;
5376 ct->blob_id = NULL; /* will be filled in when blob gets created */
5377 if (ct->status != GOT_STATUS_ADD &&
5378 ct->staged_status != GOT_STATUS_ADD) {
5379 ct->base_blob_id = got_object_id_dup(blob_id);
5380 if (ct->base_blob_id == NULL) {
5381 err = got_error_from_errno("got_object_id_dup");
5382 goto done;
5384 ct->base_commit_id = got_object_id_dup(commit_id);
5385 if (ct->base_commit_id == NULL) {
5386 err = got_error_from_errno("got_object_id_dup");
5387 goto done;
5390 if (ct->staged_status == GOT_STATUS_ADD ||
5391 ct->staged_status == GOT_STATUS_MODIFY) {
5392 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5393 if (ct->staged_blob_id == NULL) {
5394 err = got_error_from_errno("got_object_id_dup");
5395 goto done;
5398 ct->path = strdup(path);
5399 if (ct->path == NULL) {
5400 err = got_error_from_errno("strdup");
5401 goto done;
5403 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5404 if (err)
5405 goto done;
5407 if (a->diff_outfile && ct && new != NULL) {
5408 err = append_ct_diff(ct, &a->diff_header_shown,
5409 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5410 a->have_staged_files, a->repo, a->worktree);
5411 if (err)
5412 goto done;
5414 done:
5415 if (ct && (err || new == NULL))
5416 free_commitable(ct);
5417 free(parent_path);
5418 free(path);
5419 return err;
5422 static const struct got_error *write_tree(struct got_object_id **, int *,
5423 struct got_tree_object *, const char *, struct got_pathlist_head *,
5424 got_worktree_status_cb status_cb, void *status_arg,
5425 struct got_repository *);
5427 static const struct got_error *
5428 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5429 struct got_tree_entry *te, const char *parent_path,
5430 struct got_pathlist_head *commitable_paths,
5431 got_worktree_status_cb status_cb, void *status_arg,
5432 struct got_repository *repo)
5434 const struct got_error *err = NULL;
5435 struct got_tree_object *subtree;
5436 char *subpath;
5438 if (asprintf(&subpath, "%s%s%s", parent_path,
5439 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5440 return got_error_from_errno("asprintf");
5442 err = got_object_open_as_tree(&subtree, repo, &te->id);
5443 if (err)
5444 return err;
5446 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5447 commitable_paths, status_cb, status_arg, repo);
5448 got_object_tree_close(subtree);
5449 free(subpath);
5450 return err;
5453 static const struct got_error *
5454 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5456 const struct got_error *err = NULL;
5457 char *ct_parent_path = NULL;
5459 *match = 0;
5461 if (strchr(ct->in_repo_path, '/') == NULL) {
5462 *match = got_path_is_root_dir(path);
5463 return NULL;
5466 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5467 if (err)
5468 return err;
5469 *match = (strcmp(path, ct_parent_path) == 0);
5470 free(ct_parent_path);
5471 return err;
5474 static mode_t
5475 get_ct_file_mode(struct got_commitable *ct)
5477 if (S_ISLNK(ct->mode))
5478 return S_IFLNK;
5480 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5483 static const struct got_error *
5484 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5485 struct got_tree_entry *te, struct got_commitable *ct)
5487 const struct got_error *err = NULL;
5489 *new_te = NULL;
5491 err = got_object_tree_entry_dup(new_te, te);
5492 if (err)
5493 goto done;
5495 (*new_te)->mode = get_ct_file_mode(ct);
5497 if (ct->staged_status == GOT_STATUS_MODIFY)
5498 memcpy(&(*new_te)->id, ct->staged_blob_id,
5499 sizeof((*new_te)->id));
5500 else
5501 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5502 done:
5503 if (err && *new_te) {
5504 free(*new_te);
5505 *new_te = NULL;
5507 return err;
5510 static const struct got_error *
5511 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5512 struct got_commitable *ct)
5514 const struct got_error *err = NULL;
5515 char *ct_name = NULL;
5517 *new_te = NULL;
5519 *new_te = calloc(1, sizeof(**new_te));
5520 if (*new_te == NULL)
5521 return got_error_from_errno("calloc");
5523 err = got_path_basename(&ct_name, ct->path);
5524 if (err)
5525 goto done;
5526 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5527 sizeof((*new_te)->name)) {
5528 err = got_error(GOT_ERR_NO_SPACE);
5529 goto done;
5532 (*new_te)->mode = get_ct_file_mode(ct);
5534 if (ct->staged_status == GOT_STATUS_ADD)
5535 memcpy(&(*new_te)->id, ct->staged_blob_id,
5536 sizeof((*new_te)->id));
5537 else
5538 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5539 done:
5540 free(ct_name);
5541 if (err && *new_te) {
5542 free(*new_te);
5543 *new_te = NULL;
5545 return err;
5548 static const struct got_error *
5549 insert_tree_entry(struct got_tree_entry *new_te,
5550 struct got_pathlist_head *paths)
5552 const struct got_error *err = NULL;
5553 struct got_pathlist_entry *new_pe;
5555 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5556 if (err)
5557 return err;
5558 if (new_pe == NULL)
5559 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5560 return NULL;
5563 static const struct got_error *
5564 report_ct_status(struct got_commitable *ct,
5565 got_worktree_status_cb status_cb, void *status_arg)
5567 const char *ct_path = ct->path;
5568 unsigned char status;
5570 if (status_cb == NULL) /* no commit progress output desired */
5571 return NULL;
5573 while (ct_path[0] == '/')
5574 ct_path++;
5576 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5577 status = ct->staged_status;
5578 else
5579 status = ct->status;
5581 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5582 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5585 static const struct got_error *
5586 match_modified_subtree(int *modified, struct got_tree_entry *te,
5587 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5589 const struct got_error *err = NULL;
5590 struct got_pathlist_entry *pe;
5591 char *te_path;
5593 *modified = 0;
5595 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5596 got_path_is_root_dir(base_tree_path) ? "" : "/",
5597 te->name) == -1)
5598 return got_error_from_errno("asprintf");
5600 TAILQ_FOREACH(pe, commitable_paths, entry) {
5601 struct got_commitable *ct = pe->data;
5602 *modified = got_path_is_child(ct->in_repo_path, te_path,
5603 strlen(te_path));
5604 if (*modified)
5605 break;
5608 free(te_path);
5609 return err;
5612 static const struct got_error *
5613 match_deleted_or_modified_ct(struct got_commitable **ctp,
5614 struct got_tree_entry *te, const char *base_tree_path,
5615 struct got_pathlist_head *commitable_paths)
5617 const struct got_error *err = NULL;
5618 struct got_pathlist_entry *pe;
5620 *ctp = NULL;
5622 TAILQ_FOREACH(pe, commitable_paths, entry) {
5623 struct got_commitable *ct = pe->data;
5624 char *ct_name = NULL;
5625 int path_matches;
5627 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5628 if (ct->status != GOT_STATUS_MODIFY &&
5629 ct->status != GOT_STATUS_MODE_CHANGE &&
5630 ct->status != GOT_STATUS_DELETE &&
5631 ct->status != GOT_STATUS_CONFLICT)
5632 continue;
5633 } else {
5634 if (ct->staged_status != GOT_STATUS_MODIFY &&
5635 ct->staged_status != GOT_STATUS_DELETE)
5636 continue;
5639 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5640 continue;
5642 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5643 if (err)
5644 return err;
5645 if (!path_matches)
5646 continue;
5648 err = got_path_basename(&ct_name, pe->path);
5649 if (err)
5650 return err;
5652 if (strcmp(te->name, ct_name) != 0) {
5653 free(ct_name);
5654 continue;
5656 free(ct_name);
5658 *ctp = ct;
5659 break;
5662 return err;
5665 static const struct got_error *
5666 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5667 const char *child_path, const char *path_base_tree,
5668 struct got_pathlist_head *commitable_paths,
5669 got_worktree_status_cb status_cb, void *status_arg,
5670 struct got_repository *repo)
5672 const struct got_error *err = NULL;
5673 struct got_tree_entry *new_te;
5674 char *subtree_path;
5675 struct got_object_id *id = NULL;
5676 int nentries;
5678 *new_tep = NULL;
5680 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5681 got_path_is_root_dir(path_base_tree) ? "" : "/",
5682 child_path) == -1)
5683 return got_error_from_errno("asprintf");
5685 new_te = calloc(1, sizeof(*new_te));
5686 if (new_te == NULL)
5687 return got_error_from_errno("calloc");
5688 new_te->mode = S_IFDIR;
5690 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5691 sizeof(new_te->name)) {
5692 err = got_error(GOT_ERR_NO_SPACE);
5693 goto done;
5695 err = write_tree(&id, &nentries, NULL, subtree_path,
5696 commitable_paths, status_cb, status_arg, repo);
5697 if (err) {
5698 free(new_te);
5699 goto done;
5701 memcpy(&new_te->id, id, sizeof(new_te->id));
5702 done:
5703 free(id);
5704 free(subtree_path);
5705 if (err == NULL)
5706 *new_tep = new_te;
5707 return err;
5710 static const struct got_error *
5711 write_tree(struct got_object_id **new_tree_id, int *nentries,
5712 struct got_tree_object *base_tree, const char *path_base_tree,
5713 struct got_pathlist_head *commitable_paths,
5714 got_worktree_status_cb status_cb, void *status_arg,
5715 struct got_repository *repo)
5717 const struct got_error *err = NULL;
5718 struct got_pathlist_head paths;
5719 struct got_tree_entry *te, *new_te = NULL;
5720 struct got_pathlist_entry *pe;
5722 TAILQ_INIT(&paths);
5723 *nentries = 0;
5725 /* Insert, and recurse into, newly added entries first. */
5726 TAILQ_FOREACH(pe, commitable_paths, entry) {
5727 struct got_commitable *ct = pe->data;
5728 char *child_path = NULL, *slash;
5730 if ((ct->status != GOT_STATUS_ADD &&
5731 ct->staged_status != GOT_STATUS_ADD) ||
5732 (ct->flags & GOT_COMMITABLE_ADDED))
5733 continue;
5735 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5736 strlen(path_base_tree)))
5737 continue;
5739 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5740 ct->in_repo_path);
5741 if (err)
5742 goto done;
5744 slash = strchr(child_path, '/');
5745 if (slash == NULL) {
5746 err = alloc_added_blob_tree_entry(&new_te, ct);
5747 if (err)
5748 goto done;
5749 err = report_ct_status(ct, status_cb, status_arg);
5750 if (err)
5751 goto done;
5752 ct->flags |= GOT_COMMITABLE_ADDED;
5753 err = insert_tree_entry(new_te, &paths);
5754 if (err)
5755 goto done;
5756 (*nentries)++;
5757 } else {
5758 *slash = '\0'; /* trim trailing path components */
5759 if (base_tree == NULL ||
5760 got_object_tree_find_entry(base_tree, child_path)
5761 == NULL) {
5762 err = make_subtree_for_added_blob(&new_te,
5763 child_path, path_base_tree,
5764 commitable_paths, status_cb, status_arg,
5765 repo);
5766 if (err)
5767 goto done;
5768 err = insert_tree_entry(new_te, &paths);
5769 if (err)
5770 goto done;
5771 (*nentries)++;
5776 if (base_tree) {
5777 int i, nbase_entries;
5778 /* Handle modified and deleted entries. */
5779 nbase_entries = got_object_tree_get_nentries(base_tree);
5780 for (i = 0; i < nbase_entries; i++) {
5781 struct got_commitable *ct = NULL;
5783 te = got_object_tree_get_entry(base_tree, i);
5784 if (got_object_tree_entry_is_submodule(te)) {
5785 /* Entry is a submodule; just copy it. */
5786 err = got_object_tree_entry_dup(&new_te, te);
5787 if (err)
5788 goto done;
5789 err = insert_tree_entry(new_te, &paths);
5790 if (err)
5791 goto done;
5792 (*nentries)++;
5793 continue;
5796 if (S_ISDIR(te->mode)) {
5797 int modified;
5798 err = got_object_tree_entry_dup(&new_te, te);
5799 if (err)
5800 goto done;
5801 err = match_modified_subtree(&modified, te,
5802 path_base_tree, commitable_paths);
5803 if (err)
5804 goto done;
5805 /* Avoid recursion into unmodified subtrees. */
5806 if (modified) {
5807 struct got_object_id *new_id;
5808 int nsubentries;
5809 err = write_subtree(&new_id,
5810 &nsubentries, te,
5811 path_base_tree, commitable_paths,
5812 status_cb, status_arg, repo);
5813 if (err)
5814 goto done;
5815 if (nsubentries == 0) {
5816 /* All entries were deleted. */
5817 free(new_id);
5818 continue;
5820 memcpy(&new_te->id, new_id,
5821 sizeof(new_te->id));
5822 free(new_id);
5824 err = insert_tree_entry(new_te, &paths);
5825 if (err)
5826 goto done;
5827 (*nentries)++;
5828 continue;
5831 err = match_deleted_or_modified_ct(&ct, te,
5832 path_base_tree, commitable_paths);
5833 if (err)
5834 goto done;
5835 if (ct) {
5836 /* NB: Deleted entries get dropped here. */
5837 if (ct->status == GOT_STATUS_MODIFY ||
5838 ct->status == GOT_STATUS_MODE_CHANGE ||
5839 ct->status == GOT_STATUS_CONFLICT ||
5840 ct->staged_status == GOT_STATUS_MODIFY) {
5841 err = alloc_modified_blob_tree_entry(
5842 &new_te, te, ct);
5843 if (err)
5844 goto done;
5845 err = insert_tree_entry(new_te, &paths);
5846 if (err)
5847 goto done;
5848 (*nentries)++;
5850 err = report_ct_status(ct, status_cb,
5851 status_arg);
5852 if (err)
5853 goto done;
5854 } else {
5855 /* Entry is unchanged; just copy it. */
5856 err = got_object_tree_entry_dup(&new_te, te);
5857 if (err)
5858 goto done;
5859 err = insert_tree_entry(new_te, &paths);
5860 if (err)
5861 goto done;
5862 (*nentries)++;
5867 /* Write new list of entries; deleted entries have been dropped. */
5868 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5869 done:
5870 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5871 return err;
5874 static const struct got_error *
5875 update_fileindex_after_commit(struct got_worktree *worktree,
5876 struct got_pathlist_head *commitable_paths,
5877 struct got_object_id *new_base_commit_id,
5878 struct got_fileindex *fileindex, int have_staged_files)
5880 const struct got_error *err = NULL;
5881 struct got_pathlist_entry *pe;
5882 char *relpath = NULL;
5884 TAILQ_FOREACH(pe, commitable_paths, entry) {
5885 struct got_fileindex_entry *ie;
5886 struct got_commitable *ct = pe->data;
5888 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5890 err = got_path_skip_common_ancestor(&relpath,
5891 worktree->root_path, ct->ondisk_path);
5892 if (err)
5893 goto done;
5895 if (ie) {
5896 if (ct->status == GOT_STATUS_DELETE ||
5897 ct->staged_status == GOT_STATUS_DELETE) {
5898 got_fileindex_entry_remove(fileindex, ie);
5899 } else if (ct->staged_status == GOT_STATUS_ADD ||
5900 ct->staged_status == GOT_STATUS_MODIFY) {
5901 got_fileindex_entry_stage_set(ie,
5902 GOT_FILEIDX_STAGE_NONE);
5903 got_fileindex_entry_staged_filetype_set(ie, 0);
5905 err = got_fileindex_entry_update(ie,
5906 worktree->root_fd, relpath,
5907 ct->staged_blob_id->sha1,
5908 new_base_commit_id->sha1,
5909 !have_staged_files);
5910 } else
5911 err = got_fileindex_entry_update(ie,
5912 worktree->root_fd, relpath,
5913 ct->blob_id->sha1,
5914 new_base_commit_id->sha1,
5915 !have_staged_files);
5916 } else {
5917 err = got_fileindex_entry_alloc(&ie, pe->path);
5918 if (err)
5919 goto done;
5920 err = got_fileindex_entry_update(ie,
5921 worktree->root_fd, relpath, ct->blob_id->sha1,
5922 new_base_commit_id->sha1, 1);
5923 if (err) {
5924 got_fileindex_entry_free(ie);
5925 goto done;
5927 err = got_fileindex_entry_add(fileindex, ie);
5928 if (err) {
5929 got_fileindex_entry_free(ie);
5930 goto done;
5933 free(relpath);
5934 relpath = NULL;
5936 done:
5937 free(relpath);
5938 return err;
5942 static const struct got_error *
5943 check_out_of_date(const char *in_repo_path, unsigned char status,
5944 unsigned char staged_status, struct got_object_id *base_blob_id,
5945 struct got_object_id *base_commit_id,
5946 struct got_object_id *head_commit_id, struct got_repository *repo,
5947 int ood_errcode)
5949 const struct got_error *err = NULL;
5950 struct got_commit_object *commit = NULL;
5951 struct got_object_id *id = NULL;
5953 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5954 /* Trivial case: base commit == head commit */
5955 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5956 return NULL;
5958 * Ensure file content which local changes were based
5959 * on matches file content in the branch head.
5961 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5962 if (err)
5963 goto done;
5964 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5965 if (err) {
5966 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5967 err = got_error(ood_errcode);
5968 goto done;
5969 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5970 err = got_error(ood_errcode);
5971 } else {
5972 /* Require that added files don't exist in the branch head. */
5973 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5974 if (err)
5975 goto done;
5976 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5977 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5978 goto done;
5979 err = id ? got_error(ood_errcode) : NULL;
5981 done:
5982 free(id);
5983 if (commit)
5984 got_object_commit_close(commit);
5985 return err;
5988 static const struct got_error *
5989 commit_worktree(struct got_object_id **new_commit_id,
5990 struct got_pathlist_head *commitable_paths,
5991 struct got_object_id *head_commit_id,
5992 struct got_object_id *parent_id2,
5993 struct got_worktree *worktree,
5994 const char *author, const char *committer, char *diff_path,
5995 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5996 got_worktree_status_cb status_cb, void *status_arg,
5997 struct got_repository *repo)
5999 const struct got_error *err = NULL, *unlockerr = NULL;
6000 struct got_pathlist_entry *pe;
6001 const char *head_ref_name = NULL;
6002 struct got_commit_object *head_commit = NULL;
6003 struct got_reference *head_ref2 = NULL;
6004 struct got_object_id *head_commit_id2 = NULL;
6005 struct got_tree_object *head_tree = NULL;
6006 struct got_object_id *new_tree_id = NULL;
6007 int nentries, nparents = 0;
6008 struct got_object_id_queue parent_ids;
6009 struct got_object_qid *pid = NULL;
6010 char *logmsg = NULL;
6011 time_t timestamp;
6013 *new_commit_id = NULL;
6015 STAILQ_INIT(&parent_ids);
6017 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6018 if (err)
6019 goto done;
6021 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6022 if (err)
6023 goto done;
6025 if (commit_msg_cb != NULL) {
6026 err = commit_msg_cb(commitable_paths, diff_path,
6027 &logmsg, commit_arg);
6028 if (err)
6029 goto done;
6032 if (logmsg == NULL || strlen(logmsg) == 0) {
6033 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6034 goto done;
6037 /* Create blobs from added and modified files and record their IDs. */
6038 TAILQ_FOREACH(pe, commitable_paths, entry) {
6039 struct got_commitable *ct = pe->data;
6040 char *ondisk_path;
6042 /* Blobs for staged files already exist. */
6043 if (ct->staged_status == GOT_STATUS_ADD ||
6044 ct->staged_status == GOT_STATUS_MODIFY)
6045 continue;
6047 if (ct->status != GOT_STATUS_ADD &&
6048 ct->status != GOT_STATUS_MODIFY &&
6049 ct->status != GOT_STATUS_MODE_CHANGE &&
6050 ct->status != GOT_STATUS_CONFLICT)
6051 continue;
6053 if (asprintf(&ondisk_path, "%s/%s",
6054 worktree->root_path, pe->path) == -1) {
6055 err = got_error_from_errno("asprintf");
6056 goto done;
6058 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6059 free(ondisk_path);
6060 if (err)
6061 goto done;
6064 /* Recursively write new tree objects. */
6065 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6066 commitable_paths, status_cb, status_arg, repo);
6067 if (err)
6068 goto done;
6070 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6071 if (err)
6072 goto done;
6073 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6074 nparents++;
6075 if (parent_id2) {
6076 err = got_object_qid_alloc(&pid, parent_id2);
6077 if (err)
6078 goto done;
6079 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6080 nparents++;
6082 timestamp = time(NULL);
6083 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6084 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6085 if (logmsg != NULL)
6086 free(logmsg);
6087 if (err)
6088 goto done;
6090 /* Check if a concurrent commit to our branch has occurred. */
6091 head_ref_name = got_worktree_get_head_ref_name(worktree);
6092 if (head_ref_name == NULL) {
6093 err = got_error_from_errno("got_worktree_get_head_ref_name");
6094 goto done;
6096 /* Lock the reference here to prevent concurrent modification. */
6097 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6098 if (err)
6099 goto done;
6100 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6101 if (err)
6102 goto done;
6103 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6104 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6105 goto done;
6107 /* Update branch head in repository. */
6108 err = got_ref_change_ref(head_ref2, *new_commit_id);
6109 if (err)
6110 goto done;
6111 err = got_ref_write(head_ref2, repo);
6112 if (err)
6113 goto done;
6115 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6116 if (err)
6117 goto done;
6119 err = ref_base_commit(worktree, repo);
6120 if (err)
6121 goto done;
6122 done:
6123 got_object_id_queue_free(&parent_ids);
6124 if (head_tree)
6125 got_object_tree_close(head_tree);
6126 if (head_commit)
6127 got_object_commit_close(head_commit);
6128 free(head_commit_id2);
6129 if (head_ref2) {
6130 unlockerr = got_ref_unlock(head_ref2);
6131 if (unlockerr && err == NULL)
6132 err = unlockerr;
6133 got_ref_close(head_ref2);
6135 return err;
6138 static const struct got_error *
6139 check_path_is_commitable(const char *path,
6140 struct got_pathlist_head *commitable_paths)
6142 struct got_pathlist_entry *cpe = NULL;
6143 size_t path_len = strlen(path);
6145 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6146 struct got_commitable *ct = cpe->data;
6147 const char *ct_path = ct->path;
6149 while (ct_path[0] == '/')
6150 ct_path++;
6152 if (strcmp(path, ct_path) == 0 ||
6153 got_path_is_child(ct_path, path, path_len))
6154 break;
6157 if (cpe == NULL)
6158 return got_error_path(path, GOT_ERR_BAD_PATH);
6160 return NULL;
6163 static const struct got_error *
6164 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6166 int *have_staged_files = arg;
6168 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6169 *have_staged_files = 1;
6170 return got_error(GOT_ERR_CANCELLED);
6173 return NULL;
6176 static const struct got_error *
6177 check_non_staged_files(struct got_fileindex *fileindex,
6178 struct got_pathlist_head *paths)
6180 struct got_pathlist_entry *pe;
6181 struct got_fileindex_entry *ie;
6183 TAILQ_FOREACH(pe, paths, entry) {
6184 if (pe->path[0] == '\0')
6185 continue;
6186 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6187 if (ie == NULL)
6188 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6189 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6190 return got_error_path(pe->path,
6191 GOT_ERR_FILE_NOT_STAGED);
6194 return NULL;
6197 const struct got_error *
6198 got_worktree_commit(struct got_object_id **new_commit_id,
6199 struct got_worktree *worktree, struct got_pathlist_head *paths,
6200 const char *author, const char *committer, int allow_bad_symlinks,
6201 int show_diff, int commit_conflicts,
6202 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6203 got_worktree_status_cb status_cb, void *status_arg,
6204 struct got_repository *repo)
6206 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6207 struct got_fileindex *fileindex = NULL;
6208 char *fileindex_path = NULL;
6209 struct got_pathlist_head commitable_paths;
6210 struct collect_commitables_arg cc_arg;
6211 struct got_pathlist_entry *pe;
6212 struct got_reference *head_ref = NULL;
6213 struct got_object_id *head_commit_id = NULL;
6214 char *diff_path = NULL;
6215 int have_staged_files = 0;
6217 *new_commit_id = NULL;
6219 memset(&cc_arg, 0, sizeof(cc_arg));
6220 TAILQ_INIT(&commitable_paths);
6222 err = lock_worktree(worktree, LOCK_EX);
6223 if (err)
6224 goto done;
6226 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6227 if (err)
6228 goto done;
6230 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6231 if (err)
6232 goto done;
6234 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6235 if (err)
6236 goto done;
6238 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6239 &have_staged_files);
6240 if (err && err->code != GOT_ERR_CANCELLED)
6241 goto done;
6242 if (have_staged_files) {
6243 err = check_non_staged_files(fileindex, paths);
6244 if (err)
6245 goto done;
6248 cc_arg.commitable_paths = &commitable_paths;
6249 cc_arg.worktree = worktree;
6250 cc_arg.fileindex = fileindex;
6251 cc_arg.repo = repo;
6252 cc_arg.have_staged_files = have_staged_files;
6253 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6254 cc_arg.diff_header_shown = 0;
6255 cc_arg.commit_conflicts = commit_conflicts;
6256 if (show_diff) {
6257 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6258 GOT_TMPDIR_STR "/got", ".diff");
6259 if (err)
6260 goto done;
6261 cc_arg.f1 = got_opentemp();
6262 if (cc_arg.f1 == NULL) {
6263 err = got_error_from_errno("got_opentemp");
6264 goto done;
6266 cc_arg.f2 = got_opentemp();
6267 if (cc_arg.f2 == NULL) {
6268 err = got_error_from_errno("got_opentemp");
6269 goto done;
6273 TAILQ_FOREACH(pe, paths, entry) {
6274 err = worktree_status(worktree, pe->path, fileindex, repo,
6275 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6276 if (err)
6277 goto done;
6280 if (show_diff) {
6281 if (fflush(cc_arg.diff_outfile) == EOF) {
6282 err = got_error_from_errno("fflush");
6283 goto done;
6287 if (TAILQ_EMPTY(&commitable_paths)) {
6288 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6289 goto done;
6292 TAILQ_FOREACH(pe, paths, entry) {
6293 err = check_path_is_commitable(pe->path, &commitable_paths);
6294 if (err)
6295 goto done;
6298 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6299 struct got_commitable *ct = pe->data;
6300 const char *ct_path = ct->in_repo_path;
6302 while (ct_path[0] == '/')
6303 ct_path++;
6304 err = check_out_of_date(ct_path, ct->status,
6305 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6306 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6307 if (err)
6308 goto done;
6312 err = commit_worktree(new_commit_id, &commitable_paths,
6313 head_commit_id, NULL, worktree, author, committer,
6314 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6315 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6316 if (err)
6317 goto done;
6319 err = update_fileindex_after_commit(worktree, &commitable_paths,
6320 *new_commit_id, fileindex, have_staged_files);
6321 sync_err = sync_fileindex(fileindex, fileindex_path);
6322 if (sync_err && err == NULL)
6323 err = sync_err;
6324 done:
6325 if (fileindex)
6326 got_fileindex_free(fileindex);
6327 free(fileindex_path);
6328 unlockerr = lock_worktree(worktree, LOCK_SH);
6329 if (unlockerr && err == NULL)
6330 err = unlockerr;
6331 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6332 struct got_commitable *ct = pe->data;
6334 free_commitable(ct);
6336 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6337 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6338 err = got_error_from_errno2("unlink", diff_path);
6339 free(diff_path);
6340 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6341 err == NULL)
6342 err = got_error_from_errno("fclose");
6343 return err;
6346 const char *
6347 got_commitable_get_path(struct got_commitable *ct)
6349 return ct->path;
6352 unsigned int
6353 got_commitable_get_status(struct got_commitable *ct)
6355 return ct->status;
6358 struct check_rebase_ok_arg {
6359 struct got_worktree *worktree;
6360 struct got_repository *repo;
6363 static const struct got_error *
6364 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6366 const struct got_error *err = NULL;
6367 struct check_rebase_ok_arg *a = arg;
6368 unsigned char status;
6369 struct stat sb;
6370 char *ondisk_path;
6372 /* Reject rebase of a work tree with mixed base commits. */
6373 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6374 SHA1_DIGEST_LENGTH))
6375 return got_error(GOT_ERR_MIXED_COMMITS);
6377 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6378 == -1)
6379 return got_error_from_errno("asprintf");
6381 /* Reject rebase of a work tree with modified or staged files. */
6382 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6383 free(ondisk_path);
6384 if (err)
6385 return err;
6387 if (status != GOT_STATUS_NO_CHANGE)
6388 return got_error(GOT_ERR_MODIFIED);
6389 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6390 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6392 return NULL;
6395 const struct got_error *
6396 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6397 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6398 struct got_worktree *worktree, struct got_reference *branch,
6399 struct got_repository *repo)
6401 const struct got_error *err = NULL;
6402 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6403 char *branch_ref_name = NULL;
6404 char *fileindex_path = NULL;
6405 struct check_rebase_ok_arg ok_arg;
6406 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6407 struct got_object_id *wt_branch_tip = NULL;
6409 *new_base_branch_ref = NULL;
6410 *tmp_branch = NULL;
6411 *fileindex = NULL;
6413 err = lock_worktree(worktree, LOCK_EX);
6414 if (err)
6415 return err;
6417 err = open_fileindex(fileindex, &fileindex_path, worktree);
6418 if (err)
6419 goto done;
6421 ok_arg.worktree = worktree;
6422 ok_arg.repo = repo;
6423 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6424 &ok_arg);
6425 if (err)
6426 goto done;
6428 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6429 if (err)
6430 goto done;
6432 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6433 if (err)
6434 goto done;
6436 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6437 if (err)
6438 goto done;
6440 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6441 0);
6442 if (err)
6443 goto done;
6445 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6446 if (err)
6447 goto done;
6448 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6449 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6450 goto done;
6453 err = got_ref_alloc_symref(new_base_branch_ref,
6454 new_base_branch_ref_name, wt_branch);
6455 if (err)
6456 goto done;
6457 err = got_ref_write(*new_base_branch_ref, repo);
6458 if (err)
6459 goto done;
6461 /* TODO Lock original branch's ref while rebasing? */
6463 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6464 if (err)
6465 goto done;
6467 err = got_ref_write(branch_ref, repo);
6468 if (err)
6469 goto done;
6471 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6472 worktree->base_commit_id);
6473 if (err)
6474 goto done;
6475 err = got_ref_write(*tmp_branch, repo);
6476 if (err)
6477 goto done;
6479 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6480 if (err)
6481 goto done;
6482 done:
6483 free(fileindex_path);
6484 free(tmp_branch_name);
6485 free(new_base_branch_ref_name);
6486 free(branch_ref_name);
6487 if (branch_ref)
6488 got_ref_close(branch_ref);
6489 if (wt_branch)
6490 got_ref_close(wt_branch);
6491 free(wt_branch_tip);
6492 if (err) {
6493 if (*new_base_branch_ref) {
6494 got_ref_close(*new_base_branch_ref);
6495 *new_base_branch_ref = NULL;
6497 if (*tmp_branch) {
6498 got_ref_close(*tmp_branch);
6499 *tmp_branch = NULL;
6501 if (*fileindex) {
6502 got_fileindex_free(*fileindex);
6503 *fileindex = NULL;
6505 lock_worktree(worktree, LOCK_SH);
6507 return err;
6510 const struct got_error *
6511 got_worktree_rebase_continue(struct got_object_id **commit_id,
6512 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6513 struct got_reference **branch, struct got_fileindex **fileindex,
6514 struct got_worktree *worktree, struct got_repository *repo)
6516 const struct got_error *err;
6517 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6518 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6519 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6520 char *fileindex_path = NULL;
6521 int have_staged_files = 0;
6523 *commit_id = NULL;
6524 *new_base_branch = NULL;
6525 *tmp_branch = NULL;
6526 *branch = NULL;
6527 *fileindex = NULL;
6529 err = lock_worktree(worktree, LOCK_EX);
6530 if (err)
6531 return err;
6533 err = open_fileindex(fileindex, &fileindex_path, worktree);
6534 if (err)
6535 goto done;
6537 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6538 &have_staged_files);
6539 if (err && err->code != GOT_ERR_CANCELLED)
6540 goto done;
6541 if (have_staged_files) {
6542 err = got_error(GOT_ERR_STAGED_PATHS);
6543 goto done;
6546 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6547 if (err)
6548 goto done;
6550 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6551 if (err)
6552 goto done;
6554 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6555 if (err)
6556 goto done;
6558 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6559 if (err)
6560 goto done;
6562 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6563 if (err)
6564 goto done;
6566 err = got_ref_open(branch, repo,
6567 got_ref_get_symref_target(branch_ref), 0);
6568 if (err)
6569 goto done;
6571 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6572 if (err)
6573 goto done;
6575 err = got_ref_resolve(commit_id, repo, commit_ref);
6576 if (err)
6577 goto done;
6579 err = got_ref_open(new_base_branch, repo,
6580 new_base_branch_ref_name, 0);
6581 if (err)
6582 goto done;
6584 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6585 if (err)
6586 goto done;
6587 done:
6588 free(commit_ref_name);
6589 free(branch_ref_name);
6590 free(fileindex_path);
6591 if (commit_ref)
6592 got_ref_close(commit_ref);
6593 if (branch_ref)
6594 got_ref_close(branch_ref);
6595 if (err) {
6596 free(*commit_id);
6597 *commit_id = NULL;
6598 if (*tmp_branch) {
6599 got_ref_close(*tmp_branch);
6600 *tmp_branch = NULL;
6602 if (*new_base_branch) {
6603 got_ref_close(*new_base_branch);
6604 *new_base_branch = NULL;
6606 if (*branch) {
6607 got_ref_close(*branch);
6608 *branch = NULL;
6610 if (*fileindex) {
6611 got_fileindex_free(*fileindex);
6612 *fileindex = NULL;
6614 lock_worktree(worktree, LOCK_SH);
6616 return err;
6619 const struct got_error *
6620 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6622 const struct got_error *err;
6623 char *tmp_branch_name = NULL;
6625 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6626 if (err)
6627 return err;
6629 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6630 free(tmp_branch_name);
6631 return NULL;
6634 static const struct got_error *
6635 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6636 const char *diff_path, char **logmsg, void *arg)
6638 *logmsg = arg;
6639 return NULL;
6642 static const struct got_error *
6643 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6644 const char *path, struct got_object_id *blob_id,
6645 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6646 int dirfd, const char *de_name)
6648 return NULL;
6651 struct collect_merged_paths_arg {
6652 got_worktree_checkout_cb progress_cb;
6653 void *progress_arg;
6654 struct got_pathlist_head *merged_paths;
6657 static const struct got_error *
6658 collect_merged_paths(void *arg, unsigned char status, const char *path)
6660 const struct got_error *err;
6661 struct collect_merged_paths_arg *a = arg;
6662 char *p;
6663 struct got_pathlist_entry *new;
6665 err = (*a->progress_cb)(a->progress_arg, status, path);
6666 if (err)
6667 return err;
6669 if (status != GOT_STATUS_MERGE &&
6670 status != GOT_STATUS_ADD &&
6671 status != GOT_STATUS_DELETE &&
6672 status != GOT_STATUS_CONFLICT)
6673 return NULL;
6675 p = strdup(path);
6676 if (p == NULL)
6677 return got_error_from_errno("strdup");
6679 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6680 if (err || new == NULL)
6681 free(p);
6682 return err;
6685 static const struct got_error *
6686 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6687 int is_rebase, struct got_repository *repo)
6689 const struct got_error *err;
6690 struct got_reference *commit_ref = NULL;
6692 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6693 if (err) {
6694 if (err->code != GOT_ERR_NOT_REF)
6695 goto done;
6696 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6697 if (err)
6698 goto done;
6699 err = got_ref_write(commit_ref, repo);
6700 if (err)
6701 goto done;
6702 } else if (is_rebase) {
6703 struct got_object_id *stored_id;
6704 int cmp;
6706 err = got_ref_resolve(&stored_id, repo, commit_ref);
6707 if (err)
6708 goto done;
6709 cmp = got_object_id_cmp(commit_id, stored_id);
6710 free(stored_id);
6711 if (cmp != 0) {
6712 err = got_error(GOT_ERR_REBASE_COMMITID);
6713 goto done;
6716 done:
6717 if (commit_ref)
6718 got_ref_close(commit_ref);
6719 return err;
6722 static const struct got_error *
6723 rebase_merge_files(struct got_pathlist_head *merged_paths,
6724 const char *commit_ref_name, struct got_worktree *worktree,
6725 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6726 struct got_object_id *commit_id, struct got_repository *repo,
6727 got_worktree_checkout_cb progress_cb, void *progress_arg,
6728 got_cancel_cb cancel_cb, void *cancel_arg)
6730 const struct got_error *err;
6731 struct got_reference *commit_ref = NULL;
6732 struct collect_merged_paths_arg cmp_arg;
6733 char *fileindex_path;
6735 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6737 err = get_fileindex_path(&fileindex_path, worktree);
6738 if (err)
6739 return err;
6741 cmp_arg.progress_cb = progress_cb;
6742 cmp_arg.progress_arg = progress_arg;
6743 cmp_arg.merged_paths = merged_paths;
6744 err = merge_files(worktree, fileindex, fileindex_path,
6745 parent_commit_id, commit_id, repo, collect_merged_paths,
6746 &cmp_arg, cancel_cb, cancel_arg);
6747 if (commit_ref)
6748 got_ref_close(commit_ref);
6749 return err;
6752 const struct got_error *
6753 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6754 struct got_worktree *worktree, struct got_fileindex *fileindex,
6755 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6756 struct got_repository *repo,
6757 got_worktree_checkout_cb progress_cb, void *progress_arg,
6758 got_cancel_cb cancel_cb, void *cancel_arg)
6760 const struct got_error *err;
6761 char *commit_ref_name;
6763 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6764 if (err)
6765 return err;
6767 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6768 if (err)
6769 goto done;
6771 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6772 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6773 progress_arg, cancel_cb, cancel_arg);
6774 done:
6775 free(commit_ref_name);
6776 return err;
6779 const struct got_error *
6780 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6781 struct got_worktree *worktree, struct got_fileindex *fileindex,
6782 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6783 struct got_repository *repo,
6784 got_worktree_checkout_cb progress_cb, void *progress_arg,
6785 got_cancel_cb cancel_cb, void *cancel_arg)
6787 const struct got_error *err;
6788 char *commit_ref_name;
6790 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6791 if (err)
6792 return err;
6794 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6795 if (err)
6796 goto done;
6798 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6799 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6800 progress_arg, cancel_cb, cancel_arg);
6801 done:
6802 free(commit_ref_name);
6803 return err;
6806 static const struct got_error *
6807 rebase_commit(struct got_object_id **new_commit_id,
6808 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6809 struct got_worktree *worktree, struct got_fileindex *fileindex,
6810 struct got_reference *tmp_branch, const char *committer,
6811 struct got_commit_object *orig_commit, const char *new_logmsg,
6812 int allow_conflict, struct got_repository *repo)
6814 const struct got_error *err, *sync_err;
6815 struct got_pathlist_head commitable_paths;
6816 struct collect_commitables_arg cc_arg;
6817 char *fileindex_path = NULL;
6818 struct got_reference *head_ref = NULL;
6819 struct got_object_id *head_commit_id = NULL;
6820 char *logmsg = NULL;
6822 memset(&cc_arg, 0, sizeof(cc_arg));
6823 TAILQ_INIT(&commitable_paths);
6824 *new_commit_id = NULL;
6826 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6828 err = get_fileindex_path(&fileindex_path, worktree);
6829 if (err)
6830 return err;
6832 cc_arg.commitable_paths = &commitable_paths;
6833 cc_arg.worktree = worktree;
6834 cc_arg.repo = repo;
6835 cc_arg.have_staged_files = 0;
6836 cc_arg.commit_conflicts = allow_conflict;
6838 * If possible get the status of individual files directly to
6839 * avoid crawling the entire work tree once per rebased commit.
6841 * Ideally, merged_paths would contain a list of commitables
6842 * we could use so we could skip worktree_status() entirely.
6843 * However, we would then need carefully keep track of cumulative
6844 * effects of operations such as file additions and deletions
6845 * in 'got histedit -f' (folding multiple commits into one),
6846 * and this extra complexity is not really worth it.
6848 if (merged_paths) {
6849 struct got_pathlist_entry *pe;
6850 TAILQ_FOREACH(pe, merged_paths, entry) {
6851 err = worktree_status(worktree, pe->path, fileindex,
6852 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6853 0);
6854 if (err)
6855 goto done;
6857 } else {
6858 err = worktree_status(worktree, "", fileindex, repo,
6859 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6860 if (err)
6861 goto done;
6864 if (TAILQ_EMPTY(&commitable_paths)) {
6865 /* No-op change; commit will be elided. */
6866 err = got_ref_delete(commit_ref, repo);
6867 if (err)
6868 goto done;
6869 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6870 goto done;
6873 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6874 if (err)
6875 goto done;
6877 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6878 if (err)
6879 goto done;
6881 if (new_logmsg) {
6882 logmsg = strdup(new_logmsg);
6883 if (logmsg == NULL) {
6884 err = got_error_from_errno("strdup");
6885 goto done;
6887 } else {
6888 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6889 if (err)
6890 goto done;
6893 /* NB: commit_worktree will call free(logmsg) */
6894 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6895 NULL, worktree, got_object_commit_get_author(orig_commit),
6896 committer ? committer :
6897 got_object_commit_get_committer(orig_commit), NULL,
6898 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6899 if (err)
6900 goto done;
6902 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6903 if (err)
6904 goto done;
6906 err = got_ref_delete(commit_ref, repo);
6907 if (err)
6908 goto done;
6910 err = update_fileindex_after_commit(worktree, &commitable_paths,
6911 *new_commit_id, fileindex, 0);
6912 sync_err = sync_fileindex(fileindex, fileindex_path);
6913 if (sync_err && err == NULL)
6914 err = sync_err;
6915 done:
6916 free(fileindex_path);
6917 free(head_commit_id);
6918 if (head_ref)
6919 got_ref_close(head_ref);
6920 if (err) {
6921 free(*new_commit_id);
6922 *new_commit_id = NULL;
6924 return err;
6927 const struct got_error *
6928 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6929 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6930 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6931 const char *committer, struct got_commit_object *orig_commit,
6932 struct got_object_id *orig_commit_id, int allow_conflict,
6933 struct got_repository *repo)
6935 const struct got_error *err;
6936 char *commit_ref_name;
6937 struct got_reference *commit_ref = NULL;
6938 struct got_object_id *commit_id = NULL;
6940 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6941 if (err)
6942 return err;
6944 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6945 if (err)
6946 goto done;
6947 err = got_ref_resolve(&commit_id, repo, commit_ref);
6948 if (err)
6949 goto done;
6950 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6951 err = got_error(GOT_ERR_REBASE_COMMITID);
6952 goto done;
6955 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6956 worktree, fileindex, tmp_branch, committer, orig_commit,
6957 NULL, allow_conflict, repo);
6958 done:
6959 if (commit_ref)
6960 got_ref_close(commit_ref);
6961 free(commit_ref_name);
6962 free(commit_id);
6963 return err;
6966 const struct got_error *
6967 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6968 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6969 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6970 const char *committer, struct got_commit_object *orig_commit,
6971 struct got_object_id *orig_commit_id, const char *new_logmsg,
6972 int allow_conflict, struct got_repository *repo)
6974 const struct got_error *err;
6975 char *commit_ref_name;
6976 struct got_reference *commit_ref = NULL;
6978 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6979 if (err)
6980 return err;
6982 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6983 if (err)
6984 goto done;
6986 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6987 worktree, fileindex, tmp_branch, committer, orig_commit,
6988 new_logmsg, allow_conflict, repo);
6989 done:
6990 if (commit_ref)
6991 got_ref_close(commit_ref);
6992 free(commit_ref_name);
6993 return err;
6996 const struct got_error *
6997 got_worktree_rebase_postpone(struct got_worktree *worktree,
6998 struct got_fileindex *fileindex)
7000 if (fileindex)
7001 got_fileindex_free(fileindex);
7002 return lock_worktree(worktree, LOCK_SH);
7005 static const struct got_error *
7006 delete_ref(const char *name, struct got_repository *repo)
7008 const struct got_error *err;
7009 struct got_reference *ref;
7011 err = got_ref_open(&ref, repo, name, 0);
7012 if (err) {
7013 if (err->code == GOT_ERR_NOT_REF)
7014 return NULL;
7015 return err;
7018 err = got_ref_delete(ref, repo);
7019 got_ref_close(ref);
7020 return err;
7023 static const struct got_error *
7024 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7026 const struct got_error *err;
7027 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7028 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7030 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7031 if (err)
7032 goto done;
7033 err = delete_ref(tmp_branch_name, repo);
7034 if (err)
7035 goto done;
7037 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7038 if (err)
7039 goto done;
7040 err = delete_ref(new_base_branch_ref_name, repo);
7041 if (err)
7042 goto done;
7044 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7045 if (err)
7046 goto done;
7047 err = delete_ref(branch_ref_name, repo);
7048 if (err)
7049 goto done;
7051 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7052 if (err)
7053 goto done;
7054 err = delete_ref(commit_ref_name, repo);
7055 if (err)
7056 goto done;
7058 done:
7059 free(tmp_branch_name);
7060 free(new_base_branch_ref_name);
7061 free(branch_ref_name);
7062 free(commit_ref_name);
7063 return err;
7066 static const struct got_error *
7067 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7068 struct got_object_id *new_commit_id, struct got_repository *repo)
7070 const struct got_error *err;
7071 struct got_reference *ref = NULL;
7072 struct got_object_id *old_commit_id = NULL;
7073 const char *branch_name = NULL;
7074 char *new_id_str = NULL;
7075 char *refname = NULL;
7077 branch_name = got_ref_get_name(branch);
7078 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7079 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7080 branch_name += 11;
7082 err = got_object_id_str(&new_id_str, new_commit_id);
7083 if (err)
7084 return err;
7086 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7087 new_id_str) == -1) {
7088 err = got_error_from_errno("asprintf");
7089 goto done;
7092 err = got_ref_resolve(&old_commit_id, repo, branch);
7093 if (err)
7094 goto done;
7096 err = got_ref_alloc(&ref, refname, old_commit_id);
7097 if (err)
7098 goto done;
7100 err = got_ref_write(ref, repo);
7101 done:
7102 free(new_id_str);
7103 free(refname);
7104 free(old_commit_id);
7105 if (ref)
7106 got_ref_close(ref);
7107 return err;
7110 const struct got_error *
7111 got_worktree_rebase_complete(struct got_worktree *worktree,
7112 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7113 struct got_reference *rebased_branch, struct got_repository *repo,
7114 int create_backup)
7116 const struct got_error *err, *unlockerr, *sync_err;
7117 struct got_object_id *new_head_commit_id = NULL;
7118 char *fileindex_path = NULL;
7120 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7121 if (err)
7122 return err;
7124 if (create_backup) {
7125 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7126 rebased_branch, new_head_commit_id, repo);
7127 if (err)
7128 goto done;
7131 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7132 if (err)
7133 goto done;
7135 err = got_ref_write(rebased_branch, repo);
7136 if (err)
7137 goto done;
7139 err = got_worktree_set_head_ref(worktree, rebased_branch);
7140 if (err)
7141 goto done;
7143 err = delete_rebase_refs(worktree, repo);
7144 if (err)
7145 goto done;
7147 err = get_fileindex_path(&fileindex_path, worktree);
7148 if (err)
7149 goto done;
7150 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7151 sync_err = sync_fileindex(fileindex, fileindex_path);
7152 if (sync_err && err == NULL)
7153 err = sync_err;
7154 done:
7155 got_fileindex_free(fileindex);
7156 free(fileindex_path);
7157 free(new_head_commit_id);
7158 unlockerr = lock_worktree(worktree, LOCK_SH);
7159 if (unlockerr && err == NULL)
7160 err = unlockerr;
7161 return err;
7164 const struct got_error *
7165 got_worktree_rebase_abort(struct got_worktree *worktree,
7166 struct got_fileindex *fileindex, struct got_repository *repo,
7167 struct got_reference *new_base_branch,
7168 got_worktree_checkout_cb progress_cb, void *progress_arg)
7170 const struct got_error *err, *unlockerr, *sync_err;
7171 struct got_reference *resolved = NULL;
7172 struct got_object_id *commit_id = NULL;
7173 struct got_commit_object *commit = NULL;
7174 char *fileindex_path = NULL;
7175 struct revert_file_args rfa;
7176 struct got_object_id *tree_id = NULL;
7178 err = lock_worktree(worktree, LOCK_EX);
7179 if (err)
7180 return err;
7182 err = got_object_open_as_commit(&commit, repo,
7183 worktree->base_commit_id);
7184 if (err)
7185 goto done;
7187 err = got_ref_open(&resolved, repo,
7188 got_ref_get_symref_target(new_base_branch), 0);
7189 if (err)
7190 goto done;
7192 err = got_worktree_set_head_ref(worktree, resolved);
7193 if (err)
7194 goto done;
7197 * XXX commits to the base branch could have happened while
7198 * we were busy rebasing; should we store the original commit ID
7199 * when rebase begins and read it back here?
7201 err = got_ref_resolve(&commit_id, repo, resolved);
7202 if (err)
7203 goto done;
7205 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7206 if (err)
7207 goto done;
7209 err = got_object_id_by_path(&tree_id, repo, commit,
7210 worktree->path_prefix);
7211 if (err)
7212 goto done;
7214 err = delete_rebase_refs(worktree, repo);
7215 if (err)
7216 goto done;
7218 err = get_fileindex_path(&fileindex_path, worktree);
7219 if (err)
7220 goto done;
7222 rfa.worktree = worktree;
7223 rfa.fileindex = fileindex;
7224 rfa.progress_cb = progress_cb;
7225 rfa.progress_arg = progress_arg;
7226 rfa.patch_cb = NULL;
7227 rfa.patch_arg = NULL;
7228 rfa.repo = repo;
7229 rfa.unlink_added_files = 0;
7230 err = worktree_status(worktree, "", fileindex, repo,
7231 revert_file, &rfa, NULL, NULL, 1, 0);
7232 if (err)
7233 goto sync;
7235 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7236 repo, progress_cb, progress_arg, NULL, NULL);
7237 sync:
7238 sync_err = sync_fileindex(fileindex, fileindex_path);
7239 if (sync_err && err == NULL)
7240 err = sync_err;
7241 done:
7242 got_ref_close(resolved);
7243 free(tree_id);
7244 free(commit_id);
7245 if (commit)
7246 got_object_commit_close(commit);
7247 if (fileindex)
7248 got_fileindex_free(fileindex);
7249 free(fileindex_path);
7251 unlockerr = lock_worktree(worktree, LOCK_SH);
7252 if (unlockerr && err == NULL)
7253 err = unlockerr;
7254 return err;
7257 const struct got_error *
7258 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7259 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7260 struct got_fileindex **fileindex, struct got_worktree *worktree,
7261 struct got_repository *repo)
7263 const struct got_error *err = NULL;
7264 char *tmp_branch_name = NULL;
7265 char *branch_ref_name = NULL;
7266 char *base_commit_ref_name = NULL;
7267 char *fileindex_path = NULL;
7268 struct check_rebase_ok_arg ok_arg;
7269 struct got_reference *wt_branch = NULL;
7270 struct got_reference *base_commit_ref = NULL;
7272 *tmp_branch = NULL;
7273 *branch_ref = NULL;
7274 *base_commit_id = NULL;
7275 *fileindex = NULL;
7277 err = lock_worktree(worktree, LOCK_EX);
7278 if (err)
7279 return err;
7281 err = open_fileindex(fileindex, &fileindex_path, worktree);
7282 if (err)
7283 goto done;
7285 ok_arg.worktree = worktree;
7286 ok_arg.repo = repo;
7287 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7288 &ok_arg);
7289 if (err)
7290 goto done;
7292 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7293 if (err)
7294 goto done;
7296 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7297 if (err)
7298 goto done;
7300 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7301 worktree);
7302 if (err)
7303 goto done;
7305 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7306 0);
7307 if (err)
7308 goto done;
7310 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7311 if (err)
7312 goto done;
7314 err = got_ref_write(*branch_ref, repo);
7315 if (err)
7316 goto done;
7318 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7319 worktree->base_commit_id);
7320 if (err)
7321 goto done;
7322 err = got_ref_write(base_commit_ref, repo);
7323 if (err)
7324 goto done;
7325 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7326 if (*base_commit_id == NULL) {
7327 err = got_error_from_errno("got_object_id_dup");
7328 goto done;
7331 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7332 worktree->base_commit_id);
7333 if (err)
7334 goto done;
7335 err = got_ref_write(*tmp_branch, repo);
7336 if (err)
7337 goto done;
7339 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7340 if (err)
7341 goto done;
7342 done:
7343 free(fileindex_path);
7344 free(tmp_branch_name);
7345 free(branch_ref_name);
7346 free(base_commit_ref_name);
7347 if (wt_branch)
7348 got_ref_close(wt_branch);
7349 if (err) {
7350 if (*branch_ref) {
7351 got_ref_close(*branch_ref);
7352 *branch_ref = NULL;
7354 if (*tmp_branch) {
7355 got_ref_close(*tmp_branch);
7356 *tmp_branch = NULL;
7358 free(*base_commit_id);
7359 if (*fileindex) {
7360 got_fileindex_free(*fileindex);
7361 *fileindex = NULL;
7363 lock_worktree(worktree, LOCK_SH);
7365 return err;
7368 const struct got_error *
7369 got_worktree_histedit_postpone(struct got_worktree *worktree,
7370 struct got_fileindex *fileindex)
7372 if (fileindex)
7373 got_fileindex_free(fileindex);
7374 return lock_worktree(worktree, LOCK_SH);
7377 const struct got_error *
7378 got_worktree_histedit_in_progress(int *in_progress,
7379 struct got_worktree *worktree)
7381 const struct got_error *err;
7382 char *tmp_branch_name = NULL;
7384 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7385 if (err)
7386 return err;
7388 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7389 free(tmp_branch_name);
7390 return NULL;
7393 const struct got_error *
7394 got_worktree_histedit_continue(struct got_object_id **commit_id,
7395 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7396 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7397 struct got_worktree *worktree, struct got_repository *repo)
7399 const struct got_error *err;
7400 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7401 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7402 struct got_reference *commit_ref = NULL;
7403 struct got_reference *base_commit_ref = NULL;
7404 char *fileindex_path = NULL;
7405 int have_staged_files = 0;
7407 *commit_id = NULL;
7408 *tmp_branch = NULL;
7409 *base_commit_id = NULL;
7410 *fileindex = NULL;
7412 err = lock_worktree(worktree, LOCK_EX);
7413 if (err)
7414 return err;
7416 err = open_fileindex(fileindex, &fileindex_path, worktree);
7417 if (err)
7418 goto done;
7420 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7421 &have_staged_files);
7422 if (err && err->code != GOT_ERR_CANCELLED)
7423 goto done;
7424 if (have_staged_files) {
7425 err = got_error(GOT_ERR_STAGED_PATHS);
7426 goto done;
7429 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7430 if (err)
7431 goto done;
7433 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7434 if (err)
7435 goto done;
7437 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7438 if (err)
7439 goto done;
7441 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7442 worktree);
7443 if (err)
7444 goto done;
7446 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7447 if (err)
7448 goto done;
7450 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7451 if (err)
7452 goto done;
7453 err = got_ref_resolve(commit_id, repo, commit_ref);
7454 if (err)
7455 goto done;
7457 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7458 if (err)
7459 goto done;
7460 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7461 if (err)
7462 goto done;
7464 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7465 if (err)
7466 goto done;
7467 done:
7468 free(commit_ref_name);
7469 free(branch_ref_name);
7470 free(fileindex_path);
7471 if (commit_ref)
7472 got_ref_close(commit_ref);
7473 if (base_commit_ref)
7474 got_ref_close(base_commit_ref);
7475 if (err) {
7476 free(*commit_id);
7477 *commit_id = NULL;
7478 free(*base_commit_id);
7479 *base_commit_id = NULL;
7480 if (*tmp_branch) {
7481 got_ref_close(*tmp_branch);
7482 *tmp_branch = NULL;
7484 if (*fileindex) {
7485 got_fileindex_free(*fileindex);
7486 *fileindex = NULL;
7488 lock_worktree(worktree, LOCK_EX);
7490 return err;
7493 static const struct got_error *
7494 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7496 const struct got_error *err;
7497 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7498 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7500 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7501 if (err)
7502 goto done;
7503 err = delete_ref(tmp_branch_name, repo);
7504 if (err)
7505 goto done;
7507 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7508 worktree);
7509 if (err)
7510 goto done;
7511 err = delete_ref(base_commit_ref_name, repo);
7512 if (err)
7513 goto done;
7515 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7516 if (err)
7517 goto done;
7518 err = delete_ref(branch_ref_name, repo);
7519 if (err)
7520 goto done;
7522 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7523 if (err)
7524 goto done;
7525 err = delete_ref(commit_ref_name, repo);
7526 if (err)
7527 goto done;
7528 done:
7529 free(tmp_branch_name);
7530 free(base_commit_ref_name);
7531 free(branch_ref_name);
7532 free(commit_ref_name);
7533 return err;
7536 const struct got_error *
7537 got_worktree_histedit_abort(struct got_worktree *worktree,
7538 struct got_fileindex *fileindex, struct got_repository *repo,
7539 struct got_reference *branch, struct got_object_id *base_commit_id,
7540 got_worktree_checkout_cb progress_cb, void *progress_arg)
7542 const struct got_error *err, *unlockerr, *sync_err;
7543 struct got_reference *resolved = NULL;
7544 char *fileindex_path = NULL;
7545 struct got_commit_object *commit = NULL;
7546 struct got_object_id *tree_id = NULL;
7547 struct revert_file_args rfa;
7549 err = lock_worktree(worktree, LOCK_EX);
7550 if (err)
7551 return err;
7553 err = got_object_open_as_commit(&commit, repo,
7554 worktree->base_commit_id);
7555 if (err)
7556 goto done;
7558 err = got_ref_open(&resolved, repo,
7559 got_ref_get_symref_target(branch), 0);
7560 if (err)
7561 goto done;
7563 err = got_worktree_set_head_ref(worktree, resolved);
7564 if (err)
7565 goto done;
7567 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7568 if (err)
7569 goto done;
7571 err = got_object_id_by_path(&tree_id, repo, commit,
7572 worktree->path_prefix);
7573 if (err)
7574 goto done;
7576 err = delete_histedit_refs(worktree, repo);
7577 if (err)
7578 goto done;
7580 err = get_fileindex_path(&fileindex_path, worktree);
7581 if (err)
7582 goto done;
7584 rfa.worktree = worktree;
7585 rfa.fileindex = fileindex;
7586 rfa.progress_cb = progress_cb;
7587 rfa.progress_arg = progress_arg;
7588 rfa.patch_cb = NULL;
7589 rfa.patch_arg = NULL;
7590 rfa.repo = repo;
7591 rfa.unlink_added_files = 0;
7592 err = worktree_status(worktree, "", fileindex, repo,
7593 revert_file, &rfa, NULL, NULL, 1, 0);
7594 if (err)
7595 goto sync;
7597 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7598 repo, progress_cb, progress_arg, NULL, NULL);
7599 sync:
7600 sync_err = sync_fileindex(fileindex, fileindex_path);
7601 if (sync_err && err == NULL)
7602 err = sync_err;
7603 done:
7604 got_ref_close(resolved);
7605 free(tree_id);
7606 free(fileindex_path);
7608 unlockerr = lock_worktree(worktree, LOCK_SH);
7609 if (unlockerr && err == NULL)
7610 err = unlockerr;
7611 return err;
7614 const struct got_error *
7615 got_worktree_histedit_complete(struct got_worktree *worktree,
7616 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7617 struct got_reference *edited_branch, struct got_repository *repo)
7619 const struct got_error *err, *unlockerr, *sync_err;
7620 struct got_object_id *new_head_commit_id = NULL;
7621 struct got_reference *resolved = NULL;
7622 char *fileindex_path = NULL;
7624 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7625 if (err)
7626 return err;
7628 err = got_ref_open(&resolved, repo,
7629 got_ref_get_symref_target(edited_branch), 0);
7630 if (err)
7631 goto done;
7633 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7634 resolved, new_head_commit_id, repo);
7635 if (err)
7636 goto done;
7638 err = got_ref_change_ref(resolved, new_head_commit_id);
7639 if (err)
7640 goto done;
7642 err = got_ref_write(resolved, repo);
7643 if (err)
7644 goto done;
7646 err = got_worktree_set_head_ref(worktree, resolved);
7647 if (err)
7648 goto done;
7650 err = delete_histedit_refs(worktree, repo);
7651 if (err)
7652 goto done;
7654 err = get_fileindex_path(&fileindex_path, worktree);
7655 if (err)
7656 goto done;
7657 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7658 sync_err = sync_fileindex(fileindex, fileindex_path);
7659 if (sync_err && err == NULL)
7660 err = sync_err;
7661 done:
7662 got_fileindex_free(fileindex);
7663 free(fileindex_path);
7664 free(new_head_commit_id);
7665 unlockerr = lock_worktree(worktree, LOCK_SH);
7666 if (unlockerr && err == NULL)
7667 err = unlockerr;
7668 return err;
7671 const struct got_error *
7672 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7673 struct got_object_id *commit_id, struct got_repository *repo)
7675 const struct got_error *err;
7676 char *commit_ref_name;
7678 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7679 if (err)
7680 return err;
7682 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7683 if (err)
7684 goto done;
7686 err = delete_ref(commit_ref_name, repo);
7687 done:
7688 free(commit_ref_name);
7689 return err;
7692 const struct got_error *
7693 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7694 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7695 struct got_worktree *worktree, const char *refname,
7696 struct got_repository *repo)
7698 const struct got_error *err = NULL;
7699 char *fileindex_path = NULL;
7700 struct check_rebase_ok_arg ok_arg;
7702 *fileindex = NULL;
7703 *branch_ref = NULL;
7704 *base_branch_ref = NULL;
7706 err = lock_worktree(worktree, LOCK_EX);
7707 if (err)
7708 return err;
7710 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7711 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7712 "cannot integrate a branch into itself; "
7713 "update -b or different branch name required");
7714 goto done;
7717 err = open_fileindex(fileindex, &fileindex_path, worktree);
7718 if (err)
7719 goto done;
7721 /* Preconditions are the same as for rebase. */
7722 ok_arg.worktree = worktree;
7723 ok_arg.repo = repo;
7724 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7725 &ok_arg);
7726 if (err)
7727 goto done;
7729 err = got_ref_open(branch_ref, repo, refname, 1);
7730 if (err)
7731 goto done;
7733 err = got_ref_open(base_branch_ref, repo,
7734 got_worktree_get_head_ref_name(worktree), 1);
7735 done:
7736 if (err) {
7737 if (*branch_ref) {
7738 got_ref_close(*branch_ref);
7739 *branch_ref = NULL;
7741 if (*base_branch_ref) {
7742 got_ref_close(*base_branch_ref);
7743 *base_branch_ref = NULL;
7745 if (*fileindex) {
7746 got_fileindex_free(*fileindex);
7747 *fileindex = NULL;
7749 lock_worktree(worktree, LOCK_SH);
7751 return err;
7754 const struct got_error *
7755 got_worktree_integrate_continue(struct got_worktree *worktree,
7756 struct got_fileindex *fileindex, struct got_repository *repo,
7757 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7758 got_worktree_checkout_cb progress_cb, void *progress_arg,
7759 got_cancel_cb cancel_cb, void *cancel_arg)
7761 const struct got_error *err = NULL, *sync_err, *unlockerr;
7762 char *fileindex_path = NULL;
7763 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7764 struct got_commit_object *commit = NULL;
7766 err = get_fileindex_path(&fileindex_path, worktree);
7767 if (err)
7768 goto done;
7770 err = got_ref_resolve(&commit_id, repo, branch_ref);
7771 if (err)
7772 goto done;
7774 err = got_object_open_as_commit(&commit, repo, commit_id);
7775 if (err)
7776 goto done;
7778 err = got_object_id_by_path(&tree_id, repo, commit,
7779 worktree->path_prefix);
7780 if (err)
7781 goto done;
7783 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7784 if (err)
7785 goto done;
7787 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7788 progress_cb, progress_arg, cancel_cb, cancel_arg);
7789 if (err)
7790 goto sync;
7792 err = got_ref_change_ref(base_branch_ref, commit_id);
7793 if (err)
7794 goto sync;
7796 err = got_ref_write(base_branch_ref, repo);
7797 if (err)
7798 goto sync;
7800 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7801 sync:
7802 sync_err = sync_fileindex(fileindex, fileindex_path);
7803 if (sync_err && err == NULL)
7804 err = sync_err;
7806 done:
7807 unlockerr = got_ref_unlock(branch_ref);
7808 if (unlockerr && err == NULL)
7809 err = unlockerr;
7810 got_ref_close(branch_ref);
7812 unlockerr = got_ref_unlock(base_branch_ref);
7813 if (unlockerr && err == NULL)
7814 err = unlockerr;
7815 got_ref_close(base_branch_ref);
7817 got_fileindex_free(fileindex);
7818 free(fileindex_path);
7819 free(tree_id);
7820 if (commit)
7821 got_object_commit_close(commit);
7823 unlockerr = lock_worktree(worktree, LOCK_SH);
7824 if (unlockerr && err == NULL)
7825 err = unlockerr;
7826 return err;
7829 const struct got_error *
7830 got_worktree_integrate_abort(struct got_worktree *worktree,
7831 struct got_fileindex *fileindex, struct got_repository *repo,
7832 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7834 const struct got_error *err = NULL, *unlockerr = NULL;
7836 got_fileindex_free(fileindex);
7838 err = lock_worktree(worktree, LOCK_SH);
7840 unlockerr = got_ref_unlock(branch_ref);
7841 if (unlockerr && err == NULL)
7842 err = unlockerr;
7843 got_ref_close(branch_ref);
7845 unlockerr = got_ref_unlock(base_branch_ref);
7846 if (unlockerr && err == NULL)
7847 err = unlockerr;
7848 got_ref_close(base_branch_ref);
7850 return err;
7853 const struct got_error *
7854 got_worktree_merge_postpone(struct got_worktree *worktree,
7855 struct got_fileindex *fileindex)
7857 const struct got_error *err, *sync_err;
7858 char *fileindex_path = NULL;
7860 err = get_fileindex_path(&fileindex_path, worktree);
7861 if (err)
7862 goto done;
7864 sync_err = sync_fileindex(fileindex, fileindex_path);
7866 err = lock_worktree(worktree, LOCK_SH);
7867 if (sync_err && err == NULL)
7868 err = sync_err;
7869 done:
7870 got_fileindex_free(fileindex);
7871 free(fileindex_path);
7872 return err;
7875 static const struct got_error *
7876 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7878 const struct got_error *err;
7879 char *branch_refname = NULL, *commit_refname = NULL;
7881 err = get_merge_branch_ref_name(&branch_refname, worktree);
7882 if (err)
7883 goto done;
7884 err = delete_ref(branch_refname, repo);
7885 if (err)
7886 goto done;
7888 err = get_merge_commit_ref_name(&commit_refname, worktree);
7889 if (err)
7890 goto done;
7891 err = delete_ref(commit_refname, repo);
7892 if (err)
7893 goto done;
7895 done:
7896 free(branch_refname);
7897 free(commit_refname);
7898 return err;
7901 struct merge_commit_msg_arg {
7902 struct got_worktree *worktree;
7903 const char *branch_name;
7906 static const struct got_error *
7907 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7908 const char *diff_path, char **logmsg, void *arg)
7910 struct merge_commit_msg_arg *a = arg;
7912 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7913 got_worktree_get_head_ref_name(a->worktree)) == -1)
7914 return got_error_from_errno("asprintf");
7916 return NULL;
7920 const struct got_error *
7921 got_worktree_merge_branch(struct got_worktree *worktree,
7922 struct got_fileindex *fileindex,
7923 struct got_object_id *yca_commit_id,
7924 struct got_object_id *branch_tip,
7925 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7926 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7928 const struct got_error *err;
7929 char *fileindex_path = NULL;
7931 err = get_fileindex_path(&fileindex_path, worktree);
7932 if (err)
7933 goto done;
7935 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7936 worktree);
7937 if (err)
7938 goto done;
7940 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7941 branch_tip, repo, progress_cb, progress_arg,
7942 cancel_cb, cancel_arg);
7943 done:
7944 free(fileindex_path);
7945 return err;
7948 const struct got_error *
7949 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7950 struct got_worktree *worktree, struct got_fileindex *fileindex,
7951 const char *author, const char *committer, int allow_bad_symlinks,
7952 struct got_object_id *branch_tip, const char *branch_name,
7953 int allow_conflict, struct got_repository *repo,
7954 got_worktree_status_cb status_cb, void *status_arg)
7957 const struct got_error *err = NULL, *sync_err;
7958 struct got_pathlist_head commitable_paths;
7959 struct collect_commitables_arg cc_arg;
7960 struct got_pathlist_entry *pe;
7961 struct got_reference *head_ref = NULL;
7962 struct got_object_id *head_commit_id = NULL;
7963 int have_staged_files = 0;
7964 struct merge_commit_msg_arg mcm_arg;
7965 char *fileindex_path = NULL;
7967 memset(&cc_arg, 0, sizeof(cc_arg));
7968 *new_commit_id = NULL;
7970 TAILQ_INIT(&commitable_paths);
7972 err = get_fileindex_path(&fileindex_path, worktree);
7973 if (err)
7974 goto done;
7976 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7977 if (err)
7978 goto done;
7980 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7981 if (err)
7982 goto done;
7984 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7985 &have_staged_files);
7986 if (err && err->code != GOT_ERR_CANCELLED)
7987 goto done;
7988 if (have_staged_files) {
7989 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7990 goto done;
7993 cc_arg.commitable_paths = &commitable_paths;
7994 cc_arg.worktree = worktree;
7995 cc_arg.fileindex = fileindex;
7996 cc_arg.repo = repo;
7997 cc_arg.have_staged_files = have_staged_files;
7998 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7999 cc_arg.commit_conflicts = allow_conflict;
8000 err = worktree_status(worktree, "", fileindex, repo,
8001 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8002 if (err)
8003 goto done;
8005 if (TAILQ_EMPTY(&commitable_paths)) {
8006 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8007 "merge of %s cannot proceed", branch_name);
8008 goto done;
8011 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8012 struct got_commitable *ct = pe->data;
8013 const char *ct_path = ct->in_repo_path;
8015 while (ct_path[0] == '/')
8016 ct_path++;
8017 err = check_out_of_date(ct_path, ct->status,
8018 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8019 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8020 if (err)
8021 goto done;
8025 mcm_arg.worktree = worktree;
8026 mcm_arg.branch_name = branch_name;
8027 err = commit_worktree(new_commit_id, &commitable_paths,
8028 head_commit_id, branch_tip, worktree, author, committer, NULL,
8029 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8030 if (err)
8031 goto done;
8033 err = update_fileindex_after_commit(worktree, &commitable_paths,
8034 *new_commit_id, fileindex, have_staged_files);
8035 sync_err = sync_fileindex(fileindex, fileindex_path);
8036 if (sync_err && err == NULL)
8037 err = sync_err;
8038 done:
8039 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8040 struct got_commitable *ct = pe->data;
8042 free_commitable(ct);
8044 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8045 free(fileindex_path);
8046 return err;
8049 const struct got_error *
8050 got_worktree_merge_complete(struct got_worktree *worktree,
8051 struct got_fileindex *fileindex, struct got_repository *repo)
8053 const struct got_error *err, *unlockerr, *sync_err;
8054 char *fileindex_path = NULL;
8056 err = delete_merge_refs(worktree, repo);
8057 if (err)
8058 goto done;
8060 err = get_fileindex_path(&fileindex_path, worktree);
8061 if (err)
8062 goto done;
8063 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8064 sync_err = sync_fileindex(fileindex, fileindex_path);
8065 if (sync_err && err == NULL)
8066 err = sync_err;
8067 done:
8068 got_fileindex_free(fileindex);
8069 free(fileindex_path);
8070 unlockerr = lock_worktree(worktree, LOCK_SH);
8071 if (unlockerr && err == NULL)
8072 err = unlockerr;
8073 return err;
8076 const struct got_error *
8077 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8078 struct got_repository *repo)
8080 const struct got_error *err;
8081 char *branch_refname = NULL;
8082 struct got_reference *branch_ref = NULL;
8084 *in_progress = 0;
8086 err = get_merge_branch_ref_name(&branch_refname, worktree);
8087 if (err)
8088 return err;
8089 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8090 free(branch_refname);
8091 if (err) {
8092 if (err->code != GOT_ERR_NOT_REF)
8093 return err;
8094 } else
8095 *in_progress = 1;
8097 return NULL;
8100 const struct got_error *got_worktree_merge_prepare(
8101 struct got_fileindex **fileindex, struct got_worktree *worktree,
8102 struct got_reference *branch, struct got_repository *repo)
8104 const struct got_error *err = NULL;
8105 char *fileindex_path = NULL;
8106 char *branch_refname = NULL, *commit_refname = NULL;
8107 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8108 struct got_reference *commit_ref = NULL;
8109 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8110 struct check_rebase_ok_arg ok_arg;
8112 *fileindex = NULL;
8114 err = lock_worktree(worktree, LOCK_EX);
8115 if (err)
8116 return err;
8118 err = open_fileindex(fileindex, &fileindex_path, worktree);
8119 if (err)
8120 goto done;
8122 /* Preconditions are the same as for rebase. */
8123 ok_arg.worktree = worktree;
8124 ok_arg.repo = repo;
8125 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8126 &ok_arg);
8127 if (err)
8128 goto done;
8130 err = get_merge_branch_ref_name(&branch_refname, worktree);
8131 if (err)
8132 return err;
8134 err = get_merge_commit_ref_name(&commit_refname, worktree);
8135 if (err)
8136 return err;
8138 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8139 0);
8140 if (err)
8141 goto done;
8143 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8144 if (err)
8145 goto done;
8147 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8148 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8149 goto done;
8152 err = got_ref_resolve(&branch_tip, repo, branch);
8153 if (err)
8154 goto done;
8156 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8157 if (err)
8158 goto done;
8159 err = got_ref_write(branch_ref, repo);
8160 if (err)
8161 goto done;
8163 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8164 if (err)
8165 goto done;
8166 err = got_ref_write(commit_ref, repo);
8167 if (err)
8168 goto done;
8170 done:
8171 free(branch_refname);
8172 free(commit_refname);
8173 free(fileindex_path);
8174 if (branch_ref)
8175 got_ref_close(branch_ref);
8176 if (commit_ref)
8177 got_ref_close(commit_ref);
8178 if (wt_branch)
8179 got_ref_close(wt_branch);
8180 free(wt_branch_tip);
8181 if (err) {
8182 if (*fileindex) {
8183 got_fileindex_free(*fileindex);
8184 *fileindex = NULL;
8186 lock_worktree(worktree, LOCK_SH);
8188 return err;
8191 const struct got_error *
8192 got_worktree_merge_continue(char **branch_name,
8193 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8194 struct got_worktree *worktree, struct got_repository *repo)
8196 const struct got_error *err;
8197 char *commit_refname = NULL, *branch_refname = NULL;
8198 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8199 char *fileindex_path = NULL;
8200 int have_staged_files = 0;
8202 *branch_name = NULL;
8203 *branch_tip = NULL;
8204 *fileindex = NULL;
8206 err = lock_worktree(worktree, LOCK_EX);
8207 if (err)
8208 return err;
8210 err = open_fileindex(fileindex, &fileindex_path, worktree);
8211 if (err)
8212 goto done;
8214 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8215 &have_staged_files);
8216 if (err && err->code != GOT_ERR_CANCELLED)
8217 goto done;
8218 if (have_staged_files) {
8219 err = got_error(GOT_ERR_STAGED_PATHS);
8220 goto done;
8223 err = get_merge_branch_ref_name(&branch_refname, worktree);
8224 if (err)
8225 goto done;
8227 err = get_merge_commit_ref_name(&commit_refname, worktree);
8228 if (err)
8229 goto done;
8231 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8232 if (err)
8233 goto done;
8235 if (!got_ref_is_symbolic(branch_ref)) {
8236 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8237 "%s is not a symbolic reference",
8238 got_ref_get_name(branch_ref));
8239 goto done;
8241 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8242 if (*branch_name == NULL) {
8243 err = got_error_from_errno("strdup");
8244 goto done;
8247 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8248 if (err)
8249 goto done;
8251 err = got_ref_resolve(branch_tip, repo, commit_ref);
8252 if (err)
8253 goto done;
8254 done:
8255 free(commit_refname);
8256 free(branch_refname);
8257 free(fileindex_path);
8258 if (commit_ref)
8259 got_ref_close(commit_ref);
8260 if (branch_ref)
8261 got_ref_close(branch_ref);
8262 if (err) {
8263 if (*branch_name) {
8264 free(*branch_name);
8265 *branch_name = NULL;
8267 free(*branch_tip);
8268 *branch_tip = NULL;
8269 if (*fileindex) {
8270 got_fileindex_free(*fileindex);
8271 *fileindex = NULL;
8273 lock_worktree(worktree, LOCK_SH);
8275 return err;
8278 const struct got_error *
8279 got_worktree_merge_abort(struct got_worktree *worktree,
8280 struct got_fileindex *fileindex, struct got_repository *repo,
8281 got_worktree_checkout_cb progress_cb, void *progress_arg)
8283 const struct got_error *err, *unlockerr, *sync_err;
8284 struct got_object_id *commit_id = NULL;
8285 struct got_commit_object *commit = NULL;
8286 char *fileindex_path = NULL;
8287 struct revert_file_args rfa;
8288 struct got_object_id *tree_id = NULL;
8290 err = got_object_open_as_commit(&commit, repo,
8291 worktree->base_commit_id);
8292 if (err)
8293 goto done;
8295 err = got_object_id_by_path(&tree_id, repo, commit,
8296 worktree->path_prefix);
8297 if (err)
8298 goto done;
8300 err = delete_merge_refs(worktree, repo);
8301 if (err)
8302 goto done;
8304 err = get_fileindex_path(&fileindex_path, worktree);
8305 if (err)
8306 goto done;
8308 rfa.worktree = worktree;
8309 rfa.fileindex = fileindex;
8310 rfa.progress_cb = progress_cb;
8311 rfa.progress_arg = progress_arg;
8312 rfa.patch_cb = NULL;
8313 rfa.patch_arg = NULL;
8314 rfa.repo = repo;
8315 rfa.unlink_added_files = 1;
8316 err = worktree_status(worktree, "", fileindex, repo,
8317 revert_file, &rfa, NULL, NULL, 1, 0);
8318 if (err)
8319 goto sync;
8321 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8322 repo, progress_cb, progress_arg, NULL, NULL);
8323 sync:
8324 sync_err = sync_fileindex(fileindex, fileindex_path);
8325 if (sync_err && err == NULL)
8326 err = sync_err;
8327 done:
8328 free(tree_id);
8329 free(commit_id);
8330 if (commit)
8331 got_object_commit_close(commit);
8332 if (fileindex)
8333 got_fileindex_free(fileindex);
8334 free(fileindex_path);
8336 unlockerr = lock_worktree(worktree, LOCK_SH);
8337 if (unlockerr && err == NULL)
8338 err = unlockerr;
8339 return err;
8342 struct check_stage_ok_arg {
8343 struct got_object_id *head_commit_id;
8344 struct got_worktree *worktree;
8345 struct got_fileindex *fileindex;
8346 struct got_repository *repo;
8347 int have_changes;
8350 static const struct got_error *
8351 check_stage_ok(void *arg, unsigned char status,
8352 unsigned char staged_status, const char *relpath,
8353 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8354 struct got_object_id *commit_id, int dirfd, const char *de_name)
8356 struct check_stage_ok_arg *a = arg;
8357 const struct got_error *err = NULL;
8358 struct got_fileindex_entry *ie;
8359 struct got_object_id base_commit_id;
8360 struct got_object_id *base_commit_idp = NULL;
8361 char *in_repo_path = NULL, *p;
8363 if (status == GOT_STATUS_UNVERSIONED ||
8364 status == GOT_STATUS_NO_CHANGE)
8365 return NULL;
8366 if (status == GOT_STATUS_NONEXISTENT)
8367 return got_error_set_errno(ENOENT, relpath);
8369 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8370 if (ie == NULL)
8371 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8373 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8374 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8375 relpath) == -1)
8376 return got_error_from_errno("asprintf");
8378 if (got_fileindex_entry_has_commit(ie)) {
8379 base_commit_idp = got_fileindex_entry_get_commit_id(
8380 &base_commit_id, ie);
8383 if (status == GOT_STATUS_CONFLICT) {
8384 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8385 goto done;
8386 } else if (status != GOT_STATUS_ADD &&
8387 status != GOT_STATUS_MODIFY &&
8388 status != GOT_STATUS_DELETE) {
8389 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8390 goto done;
8393 a->have_changes = 1;
8395 p = in_repo_path;
8396 while (p[0] == '/')
8397 p++;
8398 err = check_out_of_date(p, status, staged_status,
8399 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8400 GOT_ERR_STAGE_OUT_OF_DATE);
8401 done:
8402 free(in_repo_path);
8403 return err;
8406 struct stage_path_arg {
8407 struct got_worktree *worktree;
8408 struct got_fileindex *fileindex;
8409 struct got_repository *repo;
8410 got_worktree_status_cb status_cb;
8411 void *status_arg;
8412 got_worktree_patch_cb patch_cb;
8413 void *patch_arg;
8414 int staged_something;
8415 int allow_bad_symlinks;
8418 static const struct got_error *
8419 stage_path(void *arg, unsigned char status,
8420 unsigned char staged_status, const char *relpath,
8421 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8422 struct got_object_id *commit_id, int dirfd, const char *de_name)
8424 struct stage_path_arg *a = arg;
8425 const struct got_error *err = NULL;
8426 struct got_fileindex_entry *ie;
8427 char *ondisk_path = NULL, *path_content = NULL;
8428 uint32_t stage;
8429 struct got_object_id *new_staged_blob_id = NULL;
8430 struct stat sb;
8432 if (status == GOT_STATUS_UNVERSIONED)
8433 return NULL;
8435 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8436 if (ie == NULL)
8437 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8439 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8440 relpath)== -1)
8441 return got_error_from_errno("asprintf");
8443 switch (status) {
8444 case GOT_STATUS_ADD:
8445 case GOT_STATUS_MODIFY:
8446 /* XXX could sb.st_mode be passed in by our caller? */
8447 if (lstat(ondisk_path, &sb) == -1) {
8448 err = got_error_from_errno2("lstat", ondisk_path);
8449 break;
8451 if (a->patch_cb) {
8452 if (status == GOT_STATUS_ADD) {
8453 int choice = GOT_PATCH_CHOICE_NONE;
8454 err = (*a->patch_cb)(&choice, a->patch_arg,
8455 status, ie->path, NULL, 1, 1);
8456 if (err)
8457 break;
8458 if (choice != GOT_PATCH_CHOICE_YES)
8459 break;
8460 } else {
8461 err = create_patched_content(&path_content, 0,
8462 staged_blob_id ? staged_blob_id : blob_id,
8463 ondisk_path, dirfd, de_name, ie->path,
8464 a->repo, a->patch_cb, a->patch_arg);
8465 if (err || path_content == NULL)
8466 break;
8469 err = got_object_blob_create(&new_staged_blob_id,
8470 path_content ? path_content : ondisk_path, a->repo);
8471 if (err)
8472 break;
8473 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8474 SHA1_DIGEST_LENGTH);
8475 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8476 stage = GOT_FILEIDX_STAGE_ADD;
8477 else
8478 stage = GOT_FILEIDX_STAGE_MODIFY;
8479 got_fileindex_entry_stage_set(ie, stage);
8480 if (S_ISLNK(sb.st_mode)) {
8481 int is_bad_symlink = 0;
8482 if (!a->allow_bad_symlinks) {
8483 char target_path[PATH_MAX];
8484 ssize_t target_len;
8485 target_len = readlink(ondisk_path, target_path,
8486 sizeof(target_path));
8487 if (target_len == -1) {
8488 err = got_error_from_errno2("readlink",
8489 ondisk_path);
8490 break;
8492 err = is_bad_symlink_target(&is_bad_symlink,
8493 target_path, target_len, ondisk_path,
8494 a->worktree->root_path);
8495 if (err)
8496 break;
8497 if (is_bad_symlink) {
8498 err = got_error_path(ondisk_path,
8499 GOT_ERR_BAD_SYMLINK);
8500 break;
8503 if (is_bad_symlink)
8504 got_fileindex_entry_staged_filetype_set(ie,
8505 GOT_FILEIDX_MODE_BAD_SYMLINK);
8506 else
8507 got_fileindex_entry_staged_filetype_set(ie,
8508 GOT_FILEIDX_MODE_SYMLINK);
8509 } else {
8510 got_fileindex_entry_staged_filetype_set(ie,
8511 GOT_FILEIDX_MODE_REGULAR_FILE);
8513 a->staged_something = 1;
8514 if (a->status_cb == NULL)
8515 break;
8516 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8517 get_staged_status(ie), relpath, blob_id,
8518 new_staged_blob_id, NULL, dirfd, de_name);
8519 if (err)
8520 break;
8522 * When staging the reverse of the staged diff,
8523 * implicitly unstage the file.
8525 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8526 sizeof(ie->blob_sha1)) == 0) {
8527 got_fileindex_entry_stage_set(ie,
8528 GOT_FILEIDX_STAGE_NONE);
8530 break;
8531 case GOT_STATUS_DELETE:
8532 if (staged_status == GOT_STATUS_DELETE)
8533 break;
8534 if (a->patch_cb) {
8535 int choice = GOT_PATCH_CHOICE_NONE;
8536 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8537 ie->path, NULL, 1, 1);
8538 if (err)
8539 break;
8540 if (choice == GOT_PATCH_CHOICE_NO)
8541 break;
8542 if (choice != GOT_PATCH_CHOICE_YES) {
8543 err = got_error(GOT_ERR_PATCH_CHOICE);
8544 break;
8547 stage = GOT_FILEIDX_STAGE_DELETE;
8548 got_fileindex_entry_stage_set(ie, stage);
8549 a->staged_something = 1;
8550 if (a->status_cb == NULL)
8551 break;
8552 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8553 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8554 de_name);
8555 break;
8556 case GOT_STATUS_NO_CHANGE:
8557 break;
8558 case GOT_STATUS_CONFLICT:
8559 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8560 break;
8561 case GOT_STATUS_NONEXISTENT:
8562 err = got_error_set_errno(ENOENT, relpath);
8563 break;
8564 default:
8565 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8566 break;
8569 if (path_content && unlink(path_content) == -1 && err == NULL)
8570 err = got_error_from_errno2("unlink", path_content);
8571 free(path_content);
8572 free(ondisk_path);
8573 free(new_staged_blob_id);
8574 return err;
8577 const struct got_error *
8578 got_worktree_stage(struct got_worktree *worktree,
8579 struct got_pathlist_head *paths,
8580 got_worktree_status_cb status_cb, void *status_arg,
8581 got_worktree_patch_cb patch_cb, void *patch_arg,
8582 int allow_bad_symlinks, struct got_repository *repo)
8584 const struct got_error *err = NULL, *sync_err, *unlockerr;
8585 struct got_pathlist_entry *pe;
8586 struct got_fileindex *fileindex = NULL;
8587 char *fileindex_path = NULL;
8588 struct got_reference *head_ref = NULL;
8589 struct got_object_id *head_commit_id = NULL;
8590 struct check_stage_ok_arg oka;
8591 struct stage_path_arg spa;
8593 err = lock_worktree(worktree, LOCK_EX);
8594 if (err)
8595 return err;
8597 err = got_ref_open(&head_ref, repo,
8598 got_worktree_get_head_ref_name(worktree), 0);
8599 if (err)
8600 goto done;
8601 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8602 if (err)
8603 goto done;
8604 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8605 if (err)
8606 goto done;
8608 /* Check pre-conditions before staging anything. */
8609 oka.head_commit_id = head_commit_id;
8610 oka.worktree = worktree;
8611 oka.fileindex = fileindex;
8612 oka.repo = repo;
8613 oka.have_changes = 0;
8614 TAILQ_FOREACH(pe, paths, entry) {
8615 err = worktree_status(worktree, pe->path, fileindex, repo,
8616 check_stage_ok, &oka, NULL, NULL, 1, 0);
8617 if (err)
8618 goto done;
8620 if (!oka.have_changes) {
8621 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8622 goto done;
8625 spa.worktree = worktree;
8626 spa.fileindex = fileindex;
8627 spa.repo = repo;
8628 spa.patch_cb = patch_cb;
8629 spa.patch_arg = patch_arg;
8630 spa.status_cb = status_cb;
8631 spa.status_arg = status_arg;
8632 spa.staged_something = 0;
8633 spa.allow_bad_symlinks = allow_bad_symlinks;
8634 TAILQ_FOREACH(pe, paths, entry) {
8635 err = worktree_status(worktree, pe->path, fileindex, repo,
8636 stage_path, &spa, NULL, NULL, 1, 0);
8637 if (err)
8638 goto done;
8640 if (!spa.staged_something) {
8641 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8642 goto done;
8645 sync_err = sync_fileindex(fileindex, fileindex_path);
8646 if (sync_err && err == NULL)
8647 err = sync_err;
8648 done:
8649 if (head_ref)
8650 got_ref_close(head_ref);
8651 free(head_commit_id);
8652 free(fileindex_path);
8653 if (fileindex)
8654 got_fileindex_free(fileindex);
8655 unlockerr = lock_worktree(worktree, LOCK_SH);
8656 if (unlockerr && err == NULL)
8657 err = unlockerr;
8658 return err;
8661 struct unstage_path_arg {
8662 struct got_worktree *worktree;
8663 struct got_fileindex *fileindex;
8664 struct got_repository *repo;
8665 got_worktree_checkout_cb progress_cb;
8666 void *progress_arg;
8667 got_worktree_patch_cb patch_cb;
8668 void *patch_arg;
8671 static const struct got_error *
8672 create_unstaged_content(char **path_unstaged_content,
8673 char **path_new_staged_content, struct got_object_id *blob_id,
8674 struct got_object_id *staged_blob_id, const char *relpath,
8675 struct got_repository *repo,
8676 got_worktree_patch_cb patch_cb, void *patch_arg)
8678 const struct got_error *err, *free_err;
8679 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8680 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8681 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8682 struct got_diffreg_result *diffreg_result = NULL;
8683 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8684 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8685 int fd1 = -1, fd2 = -1;
8687 *path_unstaged_content = NULL;
8688 *path_new_staged_content = NULL;
8690 err = got_object_id_str(&label1, blob_id);
8691 if (err)
8692 return err;
8694 fd1 = got_opentempfd();
8695 if (fd1 == -1) {
8696 err = got_error_from_errno("got_opentempfd");
8697 goto done;
8699 fd2 = got_opentempfd();
8700 if (fd2 == -1) {
8701 err = got_error_from_errno("got_opentempfd");
8702 goto done;
8705 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8706 if (err)
8707 goto done;
8709 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8710 if (err)
8711 goto done;
8713 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8714 if (err)
8715 goto done;
8717 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8718 fd2);
8719 if (err)
8720 goto done;
8722 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8723 if (err)
8724 goto done;
8726 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8727 if (err)
8728 goto done;
8730 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8731 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8732 if (err)
8733 goto done;
8735 err = got_opentemp_named(path_unstaged_content, &outfile,
8736 "got-unstaged-content", "");
8737 if (err)
8738 goto done;
8739 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8740 "got-new-staged-content", "");
8741 if (err)
8742 goto done;
8744 if (fseek(f1, 0L, SEEK_SET) == -1) {
8745 err = got_ferror(f1, GOT_ERR_IO);
8746 goto done;
8748 if (fseek(f2, 0L, SEEK_SET) == -1) {
8749 err = got_ferror(f2, GOT_ERR_IO);
8750 goto done;
8752 /* Count the number of actual changes in the diff result. */
8753 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8754 struct diff_chunk_context cc = {};
8755 diff_chunk_context_load_change(&cc, &nchunks_used,
8756 diffreg_result->result, n, 0);
8757 nchanges++;
8759 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8760 int choice;
8761 err = apply_or_reject_change(&choice, &nchunks_used,
8762 diffreg_result->result, n, relpath, f1, f2,
8763 &line_cur1, &line_cur2,
8764 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8765 if (err)
8766 goto done;
8767 if (choice == GOT_PATCH_CHOICE_YES)
8768 have_content = 1;
8769 else
8770 have_rejected_content = 1;
8771 if (choice == GOT_PATCH_CHOICE_QUIT)
8772 break;
8774 if (have_content || have_rejected_content)
8775 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8776 outfile, rejectfile);
8777 done:
8778 free(label1);
8779 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8780 err = got_error_from_errno("close");
8781 if (blob)
8782 got_object_blob_close(blob);
8783 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8784 err = got_error_from_errno("close");
8785 if (staged_blob)
8786 got_object_blob_close(staged_blob);
8787 free_err = got_diffreg_result_free(diffreg_result);
8788 if (free_err && err == NULL)
8789 err = free_err;
8790 if (f1 && fclose(f1) == EOF && err == NULL)
8791 err = got_error_from_errno2("fclose", path1);
8792 if (f2 && fclose(f2) == EOF && err == NULL)
8793 err = got_error_from_errno2("fclose", path2);
8794 if (outfile && fclose(outfile) == EOF && err == NULL)
8795 err = got_error_from_errno2("fclose", *path_unstaged_content);
8796 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8797 err = got_error_from_errno2("fclose", *path_new_staged_content);
8798 if (path1 && unlink(path1) == -1 && err == NULL)
8799 err = got_error_from_errno2("unlink", path1);
8800 if (path2 && unlink(path2) == -1 && err == NULL)
8801 err = got_error_from_errno2("unlink", path2);
8802 if (err || !have_content) {
8803 if (*path_unstaged_content &&
8804 unlink(*path_unstaged_content) == -1 && err == NULL)
8805 err = got_error_from_errno2("unlink",
8806 *path_unstaged_content);
8807 free(*path_unstaged_content);
8808 *path_unstaged_content = NULL;
8810 if (err || !have_content || !have_rejected_content) {
8811 if (*path_new_staged_content &&
8812 unlink(*path_new_staged_content) == -1 && err == NULL)
8813 err = got_error_from_errno2("unlink",
8814 *path_new_staged_content);
8815 free(*path_new_staged_content);
8816 *path_new_staged_content = NULL;
8818 free(path1);
8819 free(path2);
8820 return err;
8823 static const struct got_error *
8824 unstage_hunks(struct got_object_id *staged_blob_id,
8825 struct got_blob_object *blob_base,
8826 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8827 const char *ondisk_path, const char *label_orig,
8828 struct got_worktree *worktree, struct got_repository *repo,
8829 got_worktree_patch_cb patch_cb, void *patch_arg,
8830 got_worktree_checkout_cb progress_cb, void *progress_arg)
8832 const struct got_error *err = NULL;
8833 char *path_unstaged_content = NULL;
8834 char *path_new_staged_content = NULL;
8835 char *parent = NULL, *base_path = NULL;
8836 char *blob_base_path = NULL;
8837 struct got_object_id *new_staged_blob_id = NULL;
8838 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8839 struct stat sb;
8841 err = create_unstaged_content(&path_unstaged_content,
8842 &path_new_staged_content, blob_id, staged_blob_id,
8843 ie->path, repo, patch_cb, patch_arg);
8844 if (err)
8845 return err;
8847 if (path_unstaged_content == NULL)
8848 return NULL;
8850 if (path_new_staged_content) {
8851 err = got_object_blob_create(&new_staged_blob_id,
8852 path_new_staged_content, repo);
8853 if (err)
8854 goto done;
8857 f = fopen(path_unstaged_content, "re");
8858 if (f == NULL) {
8859 err = got_error_from_errno2("fopen",
8860 path_unstaged_content);
8861 goto done;
8863 if (fstat(fileno(f), &sb) == -1) {
8864 err = got_error_from_errno2("fstat", path_unstaged_content);
8865 goto done;
8867 if (got_fileindex_entry_staged_filetype_get(ie) ==
8868 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8869 char link_target[PATH_MAX];
8870 size_t r;
8871 r = fread(link_target, 1, sizeof(link_target), f);
8872 if (r == 0 && ferror(f)) {
8873 err = got_error_from_errno("fread");
8874 goto done;
8876 if (r >= sizeof(link_target)) { /* should not happen */
8877 err = got_error(GOT_ERR_NO_SPACE);
8878 goto done;
8880 link_target[r] = '\0';
8881 err = merge_symlink(worktree, blob_base,
8882 ondisk_path, ie->path, label_orig, link_target,
8883 worktree->base_commit_id, repo, progress_cb,
8884 progress_arg);
8885 } else {
8886 int local_changes_subsumed;
8888 err = got_path_dirname(&parent, ondisk_path);
8889 if (err)
8890 return err;
8892 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8893 parent) == -1) {
8894 err = got_error_from_errno("asprintf");
8895 base_path = NULL;
8896 goto done;
8899 err = got_opentemp_named(&blob_base_path, &f_base,
8900 base_path, "");
8901 if (err)
8902 goto done;
8903 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8904 blob_base);
8905 if (err)
8906 goto done;
8909 * In order the run a 3-way merge with a symlink we copy the symlink's
8910 * target path into a temporary file and use that file with diff3.
8912 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8913 err = dump_symlink_target_path_to_file(&f_deriv2,
8914 ondisk_path);
8915 if (err)
8916 goto done;
8917 } else {
8918 int fd;
8919 fd = open(ondisk_path,
8920 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8921 if (fd == -1) {
8922 err = got_error_from_errno2("open", ondisk_path);
8923 goto done;
8925 f_deriv2 = fdopen(fd, "r");
8926 if (f_deriv2 == NULL) {
8927 err = got_error_from_errno2("fdopen", ondisk_path);
8928 close(fd);
8929 goto done;
8933 err = merge_file(&local_changes_subsumed, worktree,
8934 f_base, f, f_deriv2, ondisk_path, ie->path,
8935 got_fileindex_perms_to_st(ie),
8936 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8937 repo, progress_cb, progress_arg);
8939 if (err)
8940 goto done;
8942 if (new_staged_blob_id) {
8943 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8944 SHA1_DIGEST_LENGTH);
8945 } else {
8946 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8947 got_fileindex_entry_staged_filetype_set(ie, 0);
8949 done:
8950 free(new_staged_blob_id);
8951 if (path_unstaged_content &&
8952 unlink(path_unstaged_content) == -1 && err == NULL)
8953 err = got_error_from_errno2("unlink", path_unstaged_content);
8954 if (path_new_staged_content &&
8955 unlink(path_new_staged_content) == -1 && err == NULL)
8956 err = got_error_from_errno2("unlink", path_new_staged_content);
8957 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8958 err = got_error_from_errno2("unlink", blob_base_path);
8959 if (f_base && fclose(f_base) == EOF && err == NULL)
8960 err = got_error_from_errno2("fclose", path_unstaged_content);
8961 if (f && fclose(f) == EOF && err == NULL)
8962 err = got_error_from_errno2("fclose", path_unstaged_content);
8963 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8964 err = got_error_from_errno2("fclose", ondisk_path);
8965 free(path_unstaged_content);
8966 free(path_new_staged_content);
8967 free(blob_base_path);
8968 free(parent);
8969 free(base_path);
8970 return err;
8973 static const struct got_error *
8974 unstage_path(void *arg, unsigned char status,
8975 unsigned char staged_status, const char *relpath,
8976 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8977 struct got_object_id *commit_id, int dirfd, const char *de_name)
8979 const struct got_error *err = NULL;
8980 struct unstage_path_arg *a = arg;
8981 struct got_fileindex_entry *ie;
8982 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8983 char *ondisk_path = NULL;
8984 char *id_str = NULL, *label_orig = NULL;
8985 int local_changes_subsumed;
8986 struct stat sb;
8987 int fd1 = -1, fd2 = -1;
8989 if (staged_status != GOT_STATUS_ADD &&
8990 staged_status != GOT_STATUS_MODIFY &&
8991 staged_status != GOT_STATUS_DELETE)
8992 return NULL;
8994 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8995 if (ie == NULL)
8996 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8998 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8999 == -1)
9000 return got_error_from_errno("asprintf");
9002 err = got_object_id_str(&id_str,
9003 commit_id ? commit_id : a->worktree->base_commit_id);
9004 if (err)
9005 goto done;
9006 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9007 id_str) == -1) {
9008 err = got_error_from_errno("asprintf");
9009 goto done;
9012 fd1 = got_opentempfd();
9013 if (fd1 == -1) {
9014 err = got_error_from_errno("got_opentempfd");
9015 goto done;
9017 fd2 = got_opentempfd();
9018 if (fd2 == -1) {
9019 err = got_error_from_errno("got_opentempfd");
9020 goto done;
9023 switch (staged_status) {
9024 case GOT_STATUS_MODIFY:
9025 err = got_object_open_as_blob(&blob_base, a->repo,
9026 blob_id, 8192, fd1);
9027 if (err)
9028 break;
9029 /* fall through */
9030 case GOT_STATUS_ADD:
9031 if (a->patch_cb) {
9032 if (staged_status == GOT_STATUS_ADD) {
9033 int choice = GOT_PATCH_CHOICE_NONE;
9034 err = (*a->patch_cb)(&choice, a->patch_arg,
9035 staged_status, ie->path, NULL, 1, 1);
9036 if (err)
9037 break;
9038 if (choice != GOT_PATCH_CHOICE_YES)
9039 break;
9040 } else {
9041 err = unstage_hunks(staged_blob_id,
9042 blob_base, blob_id, ie, ondisk_path,
9043 label_orig, a->worktree, a->repo,
9044 a->patch_cb, a->patch_arg,
9045 a->progress_cb, a->progress_arg);
9046 break; /* Done with this file. */
9049 err = got_object_open_as_blob(&blob_staged, a->repo,
9050 staged_blob_id, 8192, fd2);
9051 if (err)
9052 break;
9053 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9054 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9055 case GOT_FILEIDX_MODE_REGULAR_FILE:
9056 err = merge_blob(&local_changes_subsumed, a->worktree,
9057 blob_base, ondisk_path, relpath,
9058 got_fileindex_perms_to_st(ie), label_orig,
9059 blob_staged, commit_id ? commit_id :
9060 a->worktree->base_commit_id, a->repo,
9061 a->progress_cb, a->progress_arg);
9062 break;
9063 case GOT_FILEIDX_MODE_SYMLINK:
9064 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9065 char *staged_target;
9066 err = got_object_blob_read_to_str(
9067 &staged_target, blob_staged);
9068 if (err)
9069 goto done;
9070 err = merge_symlink(a->worktree, blob_base,
9071 ondisk_path, relpath, label_orig,
9072 staged_target, commit_id ? commit_id :
9073 a->worktree->base_commit_id,
9074 a->repo, a->progress_cb, a->progress_arg);
9075 free(staged_target);
9076 } else {
9077 err = merge_blob(&local_changes_subsumed,
9078 a->worktree, blob_base, ondisk_path,
9079 relpath, got_fileindex_perms_to_st(ie),
9080 label_orig, blob_staged,
9081 commit_id ? commit_id :
9082 a->worktree->base_commit_id, a->repo,
9083 a->progress_cb, a->progress_arg);
9085 break;
9086 default:
9087 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9088 break;
9090 if (err == NULL) {
9091 got_fileindex_entry_stage_set(ie,
9092 GOT_FILEIDX_STAGE_NONE);
9093 got_fileindex_entry_staged_filetype_set(ie, 0);
9095 break;
9096 case GOT_STATUS_DELETE:
9097 if (a->patch_cb) {
9098 int choice = GOT_PATCH_CHOICE_NONE;
9099 err = (*a->patch_cb)(&choice, a->patch_arg,
9100 staged_status, ie->path, NULL, 1, 1);
9101 if (err)
9102 break;
9103 if (choice == GOT_PATCH_CHOICE_NO)
9104 break;
9105 if (choice != GOT_PATCH_CHOICE_YES) {
9106 err = got_error(GOT_ERR_PATCH_CHOICE);
9107 break;
9110 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9111 got_fileindex_entry_staged_filetype_set(ie, 0);
9112 err = get_file_status(&status, &sb, ie, ondisk_path,
9113 dirfd, de_name, a->repo);
9114 if (err)
9115 break;
9116 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9117 break;
9119 done:
9120 free(ondisk_path);
9121 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9122 err = got_error_from_errno("close");
9123 if (blob_base)
9124 got_object_blob_close(blob_base);
9125 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9126 err = got_error_from_errno("close");
9127 if (blob_staged)
9128 got_object_blob_close(blob_staged);
9129 free(id_str);
9130 free(label_orig);
9131 return err;
9134 const struct got_error *
9135 got_worktree_unstage(struct got_worktree *worktree,
9136 struct got_pathlist_head *paths,
9137 got_worktree_checkout_cb progress_cb, void *progress_arg,
9138 got_worktree_patch_cb patch_cb, void *patch_arg,
9139 struct got_repository *repo)
9141 const struct got_error *err = NULL, *sync_err, *unlockerr;
9142 struct got_pathlist_entry *pe;
9143 struct got_fileindex *fileindex = NULL;
9144 char *fileindex_path = NULL;
9145 struct unstage_path_arg upa;
9147 err = lock_worktree(worktree, LOCK_EX);
9148 if (err)
9149 return err;
9151 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9152 if (err)
9153 goto done;
9155 upa.worktree = worktree;
9156 upa.fileindex = fileindex;
9157 upa.repo = repo;
9158 upa.progress_cb = progress_cb;
9159 upa.progress_arg = progress_arg;
9160 upa.patch_cb = patch_cb;
9161 upa.patch_arg = patch_arg;
9162 TAILQ_FOREACH(pe, paths, entry) {
9163 err = worktree_status(worktree, pe->path, fileindex, repo,
9164 unstage_path, &upa, NULL, NULL, 1, 0);
9165 if (err)
9166 goto done;
9169 sync_err = sync_fileindex(fileindex, fileindex_path);
9170 if (sync_err && err == NULL)
9171 err = sync_err;
9172 done:
9173 free(fileindex_path);
9174 if (fileindex)
9175 got_fileindex_free(fileindex);
9176 unlockerr = lock_worktree(worktree, LOCK_SH);
9177 if (unlockerr && err == NULL)
9178 err = unlockerr;
9179 return err;
9182 struct report_file_info_arg {
9183 struct got_worktree *worktree;
9184 got_worktree_path_info_cb info_cb;
9185 void *info_arg;
9186 struct got_pathlist_head *paths;
9187 got_cancel_cb cancel_cb;
9188 void *cancel_arg;
9191 static const struct got_error *
9192 report_file_info(void *arg, struct got_fileindex_entry *ie)
9194 struct report_file_info_arg *a = arg;
9195 struct got_pathlist_entry *pe;
9196 struct got_object_id blob_id, staged_blob_id, commit_id;
9197 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9198 struct got_object_id *commit_idp = NULL;
9199 int stage;
9201 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9202 return got_error(GOT_ERR_CANCELLED);
9204 TAILQ_FOREACH(pe, a->paths, entry) {
9205 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9206 got_path_is_child(ie->path, pe->path, pe->path_len))
9207 break;
9209 if (pe == NULL) /* not found */
9210 return NULL;
9212 if (got_fileindex_entry_has_blob(ie))
9213 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9214 stage = got_fileindex_entry_stage_get(ie);
9215 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9216 stage == GOT_FILEIDX_STAGE_ADD) {
9217 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9218 &staged_blob_id, ie);
9221 if (got_fileindex_entry_has_commit(ie))
9222 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9224 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9225 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9228 const struct got_error *
9229 got_worktree_path_info(struct got_worktree *worktree,
9230 struct got_pathlist_head *paths,
9231 got_worktree_path_info_cb info_cb, void *info_arg,
9232 got_cancel_cb cancel_cb, void *cancel_arg)
9235 const struct got_error *err = NULL, *unlockerr;
9236 struct got_fileindex *fileindex = NULL;
9237 char *fileindex_path = NULL;
9238 struct report_file_info_arg arg;
9240 err = lock_worktree(worktree, LOCK_SH);
9241 if (err)
9242 return err;
9244 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9245 if (err)
9246 goto done;
9248 arg.worktree = worktree;
9249 arg.info_cb = info_cb;
9250 arg.info_arg = info_arg;
9251 arg.paths = paths;
9252 arg.cancel_cb = cancel_cb;
9253 arg.cancel_arg = cancel_arg;
9254 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9255 &arg);
9256 done:
9257 free(fileindex_path);
9258 if (fileindex)
9259 got_fileindex_free(fileindex);
9260 unlockerr = lock_worktree(worktree, LOCK_UN);
9261 if (unlockerr && err == NULL)
9262 err = unlockerr;
9263 return err;
9266 static const struct got_error *
9267 patch_check_path(const char *p, char **path, unsigned char *status,
9268 unsigned char *staged_status, struct got_fileindex *fileindex,
9269 struct got_worktree *worktree, struct got_repository *repo)
9271 const struct got_error *err;
9272 struct got_fileindex_entry *ie;
9273 struct stat sb;
9274 char *ondisk_path = NULL;
9276 err = got_worktree_resolve_path(path, worktree, p);
9277 if (err)
9278 return err;
9280 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9281 *path[0] ? "/" : "", *path) == -1)
9282 return got_error_from_errno("asprintf");
9284 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9285 if (ie) {
9286 *staged_status = get_staged_status(ie);
9287 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9288 repo);
9289 if (err)
9290 goto done;
9291 } else {
9292 *staged_status = GOT_STATUS_NO_CHANGE;
9293 *status = GOT_STATUS_UNVERSIONED;
9294 if (lstat(ondisk_path, &sb) == -1) {
9295 if (errno != ENOENT) {
9296 err = got_error_from_errno2("lstat",
9297 ondisk_path);
9298 goto done;
9300 *status = GOT_STATUS_NONEXISTENT;
9304 done:
9305 free(ondisk_path);
9306 return err;
9309 static const struct got_error *
9310 patch_can_rm(const char *path, unsigned char status,
9311 unsigned char staged_status)
9313 if (status == GOT_STATUS_NONEXISTENT)
9314 return got_error_set_errno(ENOENT, path);
9315 if (status != GOT_STATUS_NO_CHANGE &&
9316 status != GOT_STATUS_ADD &&
9317 status != GOT_STATUS_MODIFY &&
9318 status != GOT_STATUS_MODE_CHANGE)
9319 return got_error_path(path, GOT_ERR_FILE_STATUS);
9320 if (staged_status == GOT_STATUS_DELETE)
9321 return got_error_path(path, GOT_ERR_FILE_STATUS);
9322 return NULL;
9325 static const struct got_error *
9326 patch_can_add(const char *path, unsigned char status)
9328 if (status != GOT_STATUS_NONEXISTENT)
9329 return got_error_path(path, GOT_ERR_FILE_STATUS);
9330 return NULL;
9333 static const struct got_error *
9334 patch_can_edit(const char *path, unsigned char status,
9335 unsigned char staged_status)
9337 if (status == GOT_STATUS_NONEXISTENT)
9338 return got_error_set_errno(ENOENT, path);
9339 if (status != GOT_STATUS_NO_CHANGE &&
9340 status != GOT_STATUS_ADD &&
9341 status != GOT_STATUS_MODIFY)
9342 return got_error_path(path, GOT_ERR_FILE_STATUS);
9343 if (staged_status == GOT_STATUS_DELETE)
9344 return got_error_path(path, GOT_ERR_FILE_STATUS);
9345 return NULL;
9348 const struct got_error *
9349 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9350 char **fileindex_path, struct got_worktree *worktree)
9352 return open_fileindex(fileindex, fileindex_path, worktree);
9355 const struct got_error *
9356 got_worktree_patch_check_path(const char *old, const char *new,
9357 char **oldpath, char **newpath, struct got_worktree *worktree,
9358 struct got_repository *repo, struct got_fileindex *fileindex)
9360 const struct got_error *err = NULL;
9361 int file_renamed = 0;
9362 unsigned char status_old, staged_status_old;
9363 unsigned char status_new, staged_status_new;
9365 *oldpath = NULL;
9366 *newpath = NULL;
9368 err = patch_check_path(old != NULL ? old : new, oldpath,
9369 &status_old, &staged_status_old, fileindex, worktree, repo);
9370 if (err)
9371 goto done;
9373 err = patch_check_path(new != NULL ? new : old, newpath,
9374 &status_new, &staged_status_new, fileindex, worktree, repo);
9375 if (err)
9376 goto done;
9378 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9379 file_renamed = 1;
9381 if (old != NULL && new == NULL)
9382 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9383 else if (file_renamed) {
9384 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9385 if (err == NULL)
9386 err = patch_can_add(*newpath, status_new);
9387 } else if (old == NULL)
9388 err = patch_can_add(*newpath, status_new);
9389 else
9390 err = patch_can_edit(*newpath, status_new, staged_status_new);
9392 done:
9393 if (err) {
9394 free(*oldpath);
9395 *oldpath = NULL;
9396 free(*newpath);
9397 *newpath = NULL;
9399 return err;
9402 const struct got_error *
9403 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9404 struct got_worktree *worktree, struct got_fileindex *fileindex,
9405 got_worktree_checkout_cb progress_cb, void *progress_arg)
9407 struct schedule_addition_args saa;
9409 memset(&saa, 0, sizeof(saa));
9410 saa.worktree = worktree;
9411 saa.fileindex = fileindex;
9412 saa.progress_cb = progress_cb;
9413 saa.progress_arg = progress_arg;
9414 saa.repo = repo;
9416 return worktree_status(worktree, path, fileindex, repo,
9417 schedule_addition, &saa, NULL, NULL, 1, 0);
9420 const struct got_error *
9421 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9422 struct got_worktree *worktree, struct got_fileindex *fileindex,
9423 got_worktree_delete_cb progress_cb, void *progress_arg)
9425 struct schedule_deletion_args sda;
9427 memset(&sda, 0, sizeof(sda));
9428 sda.worktree = worktree;
9429 sda.fileindex = fileindex;
9430 sda.progress_cb = progress_cb;
9431 sda.progress_arg = progress_arg;
9432 sda.repo = repo;
9433 sda.delete_local_mods = 0;
9434 sda.keep_on_disk = 0;
9435 sda.ignore_missing_paths = 0;
9436 sda.status_codes = NULL;
9438 return worktree_status(worktree, path, fileindex, repo,
9439 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9442 const struct got_error *
9443 got_worktree_patch_complete(struct got_fileindex *fileindex,
9444 const char *fileindex_path)
9446 const struct got_error *err = NULL;
9448 err = sync_fileindex(fileindex, fileindex_path);
9449 got_fileindex_free(fileindex);
9451 return err;